blob: c0a784fadeb038bf1be24ccd55bc918b60672090 [file] [log] [blame]
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001/*******************************************************************************
2 *
3 * Intel Ethernet Controller XL710 Family Linux Driver
Catherine Sullivane8278452015-02-06 08:52:08 +00004 * Copyright(c) 2013 - 2015 Intel Corporation.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
Greg Rosedc641b72013-12-18 13:45:51 +000015 * You should have received a copy of the GNU General Public License along
16 * with this program. If not, see <http://www.gnu.org/licenses/>.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000017 *
18 * The full GNU General Public License is included in this distribution in
19 * the file called "COPYING".
20 *
21 * Contact Information:
22 * e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
23 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
24 *
25 ******************************************************************************/
26
27/* Local includes */
28#include "i40e.h"
Shannon Nelson4eb3f762014-03-06 08:59:58 +000029#include "i40e_diag.h"
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +000030#ifdef CONFIG_I40E_VXLAN
31#include <net/vxlan.h>
32#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000033
34const char i40e_driver_name[] = "i40e";
35static const char i40e_driver_string[] =
36 "Intel(R) Ethernet Connection XL710 Network Driver";
37
38#define DRV_KERN "-k"
39
Catherine Sullivane8e724d2014-07-10 07:58:26 +000040#define DRV_VERSION_MAJOR 1
Catherine Sullivan1c2df9e2015-10-21 19:56:24 -040041#define DRV_VERSION_MINOR 4
42#define DRV_VERSION_BUILD 2
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000043#define DRV_VERSION __stringify(DRV_VERSION_MAJOR) "." \
44 __stringify(DRV_VERSION_MINOR) "." \
45 __stringify(DRV_VERSION_BUILD) DRV_KERN
46const char i40e_driver_version_str[] = DRV_VERSION;
Jesse Brandeburg8fb905b2014-01-17 15:36:33 -080047static const char i40e_copyright[] = "Copyright (c) 2013 - 2014 Intel Corporation.";
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000048
49/* a bit of forward declarations */
50static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi);
51static void i40e_handle_reset_warning(struct i40e_pf *pf);
52static int i40e_add_vsi(struct i40e_vsi *vsi);
53static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi);
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000054static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000055static int i40e_setup_misc_vector(struct i40e_pf *pf);
56static void i40e_determine_queue_usage(struct i40e_pf *pf);
57static int i40e_setup_pf_filter_control(struct i40e_pf *pf);
Helin Zhange69ff812015-10-21 19:56:22 -040058static void i40e_fill_rss_lut(struct i40e_pf *pf, u8 *lut,
59 u16 rss_table_size, u16 rss_size);
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080060static void i40e_fdir_sb_setup(struct i40e_pf *pf);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -080061static int i40e_veb_get_bw_info(struct i40e_veb *veb);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000062
63/* i40e_pci_tbl - PCI Device ID Table
64 *
65 * Last entry must be all 0s
66 *
67 * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
68 * Class, Class Mask, private data (not used) }
69 */
Benoit Taine9baa3c32014-08-08 15:56:03 +020070static const struct pci_device_id i40e_pci_tbl[] = {
Shannon Nelsonab600852014-01-17 15:36:39 -080071 {PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_XL710), 0},
Shannon Nelsonab600852014-01-17 15:36:39 -080072 {PCI_VDEVICE(INTEL, I40E_DEV_ID_QEMU), 0},
73 {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_A), 0},
74 {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_B), 0},
75 {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_C), 0},
Shannon Nelsonab600852014-01-17 15:36:39 -080076 {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_A), 0},
77 {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_B), 0},
78 {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_C), 0},
Mitch Williams5960d332014-09-13 07:40:47 +000079 {PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T), 0},
Shannon Nelsonbc5166b92015-08-26 15:14:10 -040080 {PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T4), 0},
Jesse Brandeburgae24b402015-03-27 00:12:09 -070081 {PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2), 0},
Anjali Singhai Jain87e6c1d2015-06-05 12:20:25 -040082 {PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_X722), 0},
83 {PCI_VDEVICE(INTEL, I40E_DEV_ID_1G_BASE_T_X722), 0},
84 {PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T_X722), 0},
Shannon Nelson48a3b512015-07-23 16:54:39 -040085 {PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2), 0},
86 {PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2_A), 0},
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000087 /* required last entry */
88 {0, }
89};
90MODULE_DEVICE_TABLE(pci, i40e_pci_tbl);
91
92#define I40E_MAX_VF_COUNT 128
93static int debug = -1;
94module_param(debug, int, 0);
95MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)");
96
97MODULE_AUTHOR("Intel Corporation, <e1000-devel@lists.sourceforge.net>");
98MODULE_DESCRIPTION("Intel(R) Ethernet Connection XL710 Network Driver");
99MODULE_LICENSE("GPL");
100MODULE_VERSION(DRV_VERSION);
101
102/**
103 * i40e_allocate_dma_mem_d - OS specific memory alloc for shared code
104 * @hw: pointer to the HW structure
105 * @mem: ptr to mem struct to fill out
106 * @size: size of memory requested
107 * @alignment: what to align the allocation to
108 **/
109int i40e_allocate_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem,
110 u64 size, u32 alignment)
111{
112 struct i40e_pf *pf = (struct i40e_pf *)hw->back;
113
114 mem->size = ALIGN(size, alignment);
115 mem->va = dma_zalloc_coherent(&pf->pdev->dev, mem->size,
116 &mem->pa, GFP_KERNEL);
Jesse Brandeburg93bc73b2013-09-13 08:23:18 +0000117 if (!mem->va)
118 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000119
Jesse Brandeburg93bc73b2013-09-13 08:23:18 +0000120 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000121}
122
123/**
124 * i40e_free_dma_mem_d - OS specific memory free for shared code
125 * @hw: pointer to the HW structure
126 * @mem: ptr to mem struct to free
127 **/
128int i40e_free_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem)
129{
130 struct i40e_pf *pf = (struct i40e_pf *)hw->back;
131
132 dma_free_coherent(&pf->pdev->dev, mem->size, mem->va, mem->pa);
133 mem->va = NULL;
134 mem->pa = 0;
135 mem->size = 0;
136
137 return 0;
138}
139
140/**
141 * i40e_allocate_virt_mem_d - OS specific memory alloc for shared code
142 * @hw: pointer to the HW structure
143 * @mem: ptr to mem struct to fill out
144 * @size: size of memory requested
145 **/
146int i40e_allocate_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem,
147 u32 size)
148{
149 mem->size = size;
150 mem->va = kzalloc(size, GFP_KERNEL);
151
Jesse Brandeburg93bc73b2013-09-13 08:23:18 +0000152 if (!mem->va)
153 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000154
Jesse Brandeburg93bc73b2013-09-13 08:23:18 +0000155 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000156}
157
158/**
159 * i40e_free_virt_mem_d - OS specific memory free for shared code
160 * @hw: pointer to the HW structure
161 * @mem: ptr to mem struct to free
162 **/
163int i40e_free_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem)
164{
165 /* it's ok to kfree a NULL pointer */
166 kfree(mem->va);
167 mem->va = NULL;
168 mem->size = 0;
169
170 return 0;
171}
172
173/**
174 * i40e_get_lump - find a lump of free generic resource
175 * @pf: board private structure
176 * @pile: the pile of resource to search
177 * @needed: the number of items needed
178 * @id: an owner id to stick on the items assigned
179 *
180 * Returns the base item index of the lump, or negative for error
181 *
182 * The search_hint trick and lack of advanced fit-finding only work
183 * because we're highly likely to have all the same size lump requests.
184 * Linear search time and any fragmentation should be minimal.
185 **/
186static int i40e_get_lump(struct i40e_pf *pf, struct i40e_lump_tracking *pile,
187 u16 needed, u16 id)
188{
189 int ret = -ENOMEM;
Jesse Brandeburgddf434a2013-09-13 08:23:19 +0000190 int i, j;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000191
192 if (!pile || needed == 0 || id >= I40E_PILE_VALID_BIT) {
193 dev_info(&pf->pdev->dev,
194 "param err: pile=%p needed=%d id=0x%04x\n",
195 pile, needed, id);
196 return -EINVAL;
197 }
198
199 /* start the linear search with an imperfect hint */
200 i = pile->search_hint;
Jesse Brandeburgddf434a2013-09-13 08:23:19 +0000201 while (i < pile->num_entries) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000202 /* skip already allocated entries */
203 if (pile->list[i] & I40E_PILE_VALID_BIT) {
204 i++;
205 continue;
206 }
207
208 /* do we have enough in this lump? */
209 for (j = 0; (j < needed) && ((i+j) < pile->num_entries); j++) {
210 if (pile->list[i+j] & I40E_PILE_VALID_BIT)
211 break;
212 }
213
214 if (j == needed) {
215 /* there was enough, so assign it to the requestor */
216 for (j = 0; j < needed; j++)
217 pile->list[i+j] = id | I40E_PILE_VALID_BIT;
218 ret = i;
219 pile->search_hint = i + j;
Jesse Brandeburgddf434a2013-09-13 08:23:19 +0000220 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000221 }
Jesse Brandeburg6995b362015-08-28 17:55:54 -0400222
223 /* not enough, so skip over it and continue looking */
224 i += j;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000225 }
226
227 return ret;
228}
229
230/**
231 * i40e_put_lump - return a lump of generic resource
232 * @pile: the pile of resource to search
233 * @index: the base item index
234 * @id: the owner id of the items assigned
235 *
236 * Returns the count of items in the lump
237 **/
238static int i40e_put_lump(struct i40e_lump_tracking *pile, u16 index, u16 id)
239{
240 int valid_id = (id | I40E_PILE_VALID_BIT);
241 int count = 0;
242 int i;
243
244 if (!pile || index >= pile->num_entries)
245 return -EINVAL;
246
247 for (i = index;
248 i < pile->num_entries && pile->list[i] == valid_id;
249 i++) {
250 pile->list[i] = 0;
251 count++;
252 }
253
254 if (count && index < pile->search_hint)
255 pile->search_hint = index;
256
257 return count;
258}
259
260/**
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700261 * i40e_find_vsi_from_id - searches for the vsi with the given id
262 * @pf - the pf structure to search for the vsi
263 * @id - id of the vsi it is searching for
264 **/
265struct i40e_vsi *i40e_find_vsi_from_id(struct i40e_pf *pf, u16 id)
266{
267 int i;
268
269 for (i = 0; i < pf->num_alloc_vsi; i++)
270 if (pf->vsi[i] && (pf->vsi[i]->id == id))
271 return pf->vsi[i];
272
273 return NULL;
274}
275
276/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000277 * i40e_service_event_schedule - Schedule the service task to wake up
278 * @pf: board private structure
279 *
280 * If not already scheduled, this puts the task into the work queue
281 **/
282static void i40e_service_event_schedule(struct i40e_pf *pf)
283{
284 if (!test_bit(__I40E_DOWN, &pf->state) &&
285 !test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state) &&
286 !test_and_set_bit(__I40E_SERVICE_SCHED, &pf->state))
287 schedule_work(&pf->service_task);
288}
289
290/**
291 * i40e_tx_timeout - Respond to a Tx Hang
292 * @netdev: network interface device structure
293 *
294 * If any port has noticed a Tx timeout, it is likely that the whole
295 * device is munged, not just the one netdev port, so go for the full
296 * reset.
297 **/
Vasu Dev38e00432014-08-01 13:27:03 -0700298#ifdef I40E_FCOE
299void i40e_tx_timeout(struct net_device *netdev)
300#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000301static void i40e_tx_timeout(struct net_device *netdev)
Vasu Dev38e00432014-08-01 13:27:03 -0700302#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000303{
304 struct i40e_netdev_priv *np = netdev_priv(netdev);
305 struct i40e_vsi *vsi = np->vsi;
306 struct i40e_pf *pf = vsi->back;
Kiran Patilb03a8c12015-09-24 18:13:15 -0400307 struct i40e_ring *tx_ring = NULL;
308 unsigned int i, hung_queue = 0;
309 u32 head, val;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000310
311 pf->tx_timeout_count++;
312
Kiran Patilb03a8c12015-09-24 18:13:15 -0400313 /* find the stopped queue the same way the stack does */
314 for (i = 0; i < netdev->num_tx_queues; i++) {
315 struct netdev_queue *q;
316 unsigned long trans_start;
317
318 q = netdev_get_tx_queue(netdev, i);
319 trans_start = q->trans_start ? : netdev->trans_start;
320 if (netif_xmit_stopped(q) &&
321 time_after(jiffies,
322 (trans_start + netdev->watchdog_timeo))) {
323 hung_queue = i;
324 break;
325 }
326 }
327
328 if (i == netdev->num_tx_queues) {
329 netdev_info(netdev, "tx_timeout: no netdev hung queue found\n");
330 } else {
331 /* now that we have an index, find the tx_ring struct */
332 for (i = 0; i < vsi->num_queue_pairs; i++) {
333 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc) {
334 if (hung_queue ==
335 vsi->tx_rings[i]->queue_index) {
336 tx_ring = vsi->tx_rings[i];
337 break;
338 }
339 }
340 }
341 }
342
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000343 if (time_after(jiffies, (pf->tx_timeout_last_recovery + HZ*20)))
Kiran Patilb03a8c12015-09-24 18:13:15 -0400344 pf->tx_timeout_recovery_level = 1; /* reset after some time */
345 else if (time_before(jiffies,
346 (pf->tx_timeout_last_recovery + netdev->watchdog_timeo)))
347 return; /* don't do any new action before the next timeout */
348
349 if (tx_ring) {
350 head = i40e_get_head(tx_ring);
351 /* Read interrupt register */
352 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
353 val = rd32(&pf->hw,
354 I40E_PFINT_DYN_CTLN(tx_ring->q_vector->v_idx +
355 tx_ring->vsi->base_vector - 1));
356 else
357 val = rd32(&pf->hw, I40E_PFINT_DYN_CTL0);
358
359 netdev_info(netdev, "tx_timeout: VSI_seid: %d, Q %d, NTC: 0x%x, HWB: 0x%x, NTU: 0x%x, TAIL: 0x%x, INT: 0x%x\n",
360 vsi->seid, hung_queue, tx_ring->next_to_clean,
361 head, tx_ring->next_to_use,
362 readl(tx_ring->tail), val);
363 }
364
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000365 pf->tx_timeout_last_recovery = jiffies;
Kiran Patilb03a8c12015-09-24 18:13:15 -0400366 netdev_info(netdev, "tx_timeout recovery level %d, hung_queue %d\n",
367 pf->tx_timeout_recovery_level, hung_queue);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000368
369 switch (pf->tx_timeout_recovery_level) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000370 case 1:
371 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
372 break;
373 case 2:
374 set_bit(__I40E_CORE_RESET_REQUESTED, &pf->state);
375 break;
376 case 3:
377 set_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state);
378 break;
379 default:
380 netdev_err(netdev, "tx_timeout recovery unsuccessful\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000381 break;
382 }
Kiran Patilb03a8c12015-09-24 18:13:15 -0400383
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000384 i40e_service_event_schedule(pf);
385 pf->tx_timeout_recovery_level++;
386}
387
388/**
389 * i40e_release_rx_desc - Store the new tail and head values
390 * @rx_ring: ring to bump
391 * @val: new head index
392 **/
393static inline void i40e_release_rx_desc(struct i40e_ring *rx_ring, u32 val)
394{
395 rx_ring->next_to_use = val;
396
397 /* Force memory writes to complete before letting h/w
398 * know there are new descriptors to fetch. (Only
399 * applicable for weak-ordered memory model archs,
400 * such as IA-64).
401 */
402 wmb();
403 writel(val, rx_ring->tail);
404}
405
406/**
407 * i40e_get_vsi_stats_struct - Get System Network Statistics
408 * @vsi: the VSI we care about
409 *
410 * Returns the address of the device statistics structure.
411 * The statistics are actually updated from the service task.
412 **/
413struct rtnl_link_stats64 *i40e_get_vsi_stats_struct(struct i40e_vsi *vsi)
414{
415 return &vsi->net_stats;
416}
417
418/**
419 * i40e_get_netdev_stats_struct - Get statistics for netdev interface
420 * @netdev: network interface device structure
421 *
422 * Returns the address of the device statistics structure.
423 * The statistics are actually updated from the service task.
424 **/
Vasu Dev38e00432014-08-01 13:27:03 -0700425#ifdef I40E_FCOE
426struct rtnl_link_stats64 *i40e_get_netdev_stats_struct(
427 struct net_device *netdev,
428 struct rtnl_link_stats64 *stats)
429#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000430static struct rtnl_link_stats64 *i40e_get_netdev_stats_struct(
431 struct net_device *netdev,
Alexander Duyck980e9b12013-09-28 06:01:03 +0000432 struct rtnl_link_stats64 *stats)
Vasu Dev38e00432014-08-01 13:27:03 -0700433#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000434{
435 struct i40e_netdev_priv *np = netdev_priv(netdev);
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +0000436 struct i40e_ring *tx_ring, *rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000437 struct i40e_vsi *vsi = np->vsi;
Alexander Duyck980e9b12013-09-28 06:01:03 +0000438 struct rtnl_link_stats64 *vsi_stats = i40e_get_vsi_stats_struct(vsi);
439 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000440
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +0000441 if (test_bit(__I40E_DOWN, &vsi->state))
442 return stats;
443
Jesse Brandeburg3c325ce2013-12-14 03:26:45 -0800444 if (!vsi->tx_rings)
445 return stats;
446
Alexander Duyck980e9b12013-09-28 06:01:03 +0000447 rcu_read_lock();
448 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck980e9b12013-09-28 06:01:03 +0000449 u64 bytes, packets;
450 unsigned int start;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000451
Alexander Duyck980e9b12013-09-28 06:01:03 +0000452 tx_ring = ACCESS_ONCE(vsi->tx_rings[i]);
453 if (!tx_ring)
454 continue;
455
456 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700457 start = u64_stats_fetch_begin_irq(&tx_ring->syncp);
Alexander Duyck980e9b12013-09-28 06:01:03 +0000458 packets = tx_ring->stats.packets;
459 bytes = tx_ring->stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700460 } while (u64_stats_fetch_retry_irq(&tx_ring->syncp, start));
Alexander Duyck980e9b12013-09-28 06:01:03 +0000461
462 stats->tx_packets += packets;
463 stats->tx_bytes += bytes;
464 rx_ring = &tx_ring[1];
465
466 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700467 start = u64_stats_fetch_begin_irq(&rx_ring->syncp);
Alexander Duyck980e9b12013-09-28 06:01:03 +0000468 packets = rx_ring->stats.packets;
469 bytes = rx_ring->stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700470 } while (u64_stats_fetch_retry_irq(&rx_ring->syncp, start));
Alexander Duyck980e9b12013-09-28 06:01:03 +0000471
472 stats->rx_packets += packets;
473 stats->rx_bytes += bytes;
474 }
475 rcu_read_unlock();
476
Akeem G Abodunrina5282f42014-05-10 04:49:03 +0000477 /* following stats updated by i40e_watchdog_subtask() */
Alexander Duyck980e9b12013-09-28 06:01:03 +0000478 stats->multicast = vsi_stats->multicast;
479 stats->tx_errors = vsi_stats->tx_errors;
480 stats->tx_dropped = vsi_stats->tx_dropped;
481 stats->rx_errors = vsi_stats->rx_errors;
Jesse Brandeburgd8201e22015-07-23 16:54:35 -0400482 stats->rx_dropped = vsi_stats->rx_dropped;
Alexander Duyck980e9b12013-09-28 06:01:03 +0000483 stats->rx_crc_errors = vsi_stats->rx_crc_errors;
484 stats->rx_length_errors = vsi_stats->rx_length_errors;
485
486 return stats;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000487}
488
489/**
490 * i40e_vsi_reset_stats - Resets all stats of the given vsi
491 * @vsi: the VSI to have its stats reset
492 **/
493void i40e_vsi_reset_stats(struct i40e_vsi *vsi)
494{
495 struct rtnl_link_stats64 *ns;
496 int i;
497
498 if (!vsi)
499 return;
500
501 ns = i40e_get_vsi_stats_struct(vsi);
502 memset(ns, 0, sizeof(*ns));
503 memset(&vsi->net_stats_offsets, 0, sizeof(vsi->net_stats_offsets));
504 memset(&vsi->eth_stats, 0, sizeof(vsi->eth_stats));
505 memset(&vsi->eth_stats_offsets, 0, sizeof(vsi->eth_stats_offsets));
Greg Rose8e9dca52013-12-18 13:45:53 +0000506 if (vsi->rx_rings && vsi->rx_rings[0]) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000507 for (i = 0; i < vsi->num_queue_pairs; i++) {
Jesse Brandeburg6995b362015-08-28 17:55:54 -0400508 memset(&vsi->rx_rings[i]->stats, 0,
Alexander Duyck9f65e15b2013-09-28 06:00:58 +0000509 sizeof(vsi->rx_rings[i]->stats));
Jesse Brandeburg6995b362015-08-28 17:55:54 -0400510 memset(&vsi->rx_rings[i]->rx_stats, 0,
Alexander Duyck9f65e15b2013-09-28 06:00:58 +0000511 sizeof(vsi->rx_rings[i]->rx_stats));
Jesse Brandeburg6995b362015-08-28 17:55:54 -0400512 memset(&vsi->tx_rings[i]->stats, 0,
Alexander Duyck9f65e15b2013-09-28 06:00:58 +0000513 sizeof(vsi->tx_rings[i]->stats));
514 memset(&vsi->tx_rings[i]->tx_stats, 0,
515 sizeof(vsi->tx_rings[i]->tx_stats));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000516 }
Greg Rose8e9dca52013-12-18 13:45:53 +0000517 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000518 vsi->stat_offsets_loaded = false;
519}
520
521/**
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000522 * i40e_pf_reset_stats - Reset all of the stats for the given PF
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000523 * @pf: the PF to be reset
524 **/
525void i40e_pf_reset_stats(struct i40e_pf *pf)
526{
Shannon Nelsone91fdf72014-06-03 23:50:18 +0000527 int i;
528
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000529 memset(&pf->stats, 0, sizeof(pf->stats));
530 memset(&pf->stats_offsets, 0, sizeof(pf->stats_offsets));
531 pf->stat_offsets_loaded = false;
Shannon Nelsone91fdf72014-06-03 23:50:18 +0000532
533 for (i = 0; i < I40E_MAX_VEB; i++) {
534 if (pf->veb[i]) {
535 memset(&pf->veb[i]->stats, 0,
536 sizeof(pf->veb[i]->stats));
537 memset(&pf->veb[i]->stats_offsets, 0,
538 sizeof(pf->veb[i]->stats_offsets));
539 pf->veb[i]->stat_offsets_loaded = false;
540 }
541 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000542}
543
544/**
545 * i40e_stat_update48 - read and update a 48 bit stat from the chip
546 * @hw: ptr to the hardware info
547 * @hireg: the high 32 bit reg to read
548 * @loreg: the low 32 bit reg to read
549 * @offset_loaded: has the initial offset been loaded yet
550 * @offset: ptr to current offset value
551 * @stat: ptr to the stat
552 *
553 * Since the device stats are not reset at PFReset, they likely will not
554 * be zeroed when the driver starts. We'll save the first values read
555 * and use them as offsets to be subtracted from the raw values in order
556 * to report stats that count from zero. In the process, we also manage
557 * the potential roll-over.
558 **/
559static void i40e_stat_update48(struct i40e_hw *hw, u32 hireg, u32 loreg,
560 bool offset_loaded, u64 *offset, u64 *stat)
561{
562 u64 new_data;
563
Shannon Nelsonab600852014-01-17 15:36:39 -0800564 if (hw->device_id == I40E_DEV_ID_QEMU) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000565 new_data = rd32(hw, loreg);
566 new_data |= ((u64)(rd32(hw, hireg) & 0xFFFF)) << 32;
567 } else {
568 new_data = rd64(hw, loreg);
569 }
570 if (!offset_loaded)
571 *offset = new_data;
572 if (likely(new_data >= *offset))
573 *stat = new_data - *offset;
574 else
Jesse Brandeburg41a1d042015-06-04 16:24:02 -0400575 *stat = (new_data + BIT_ULL(48)) - *offset;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000576 *stat &= 0xFFFFFFFFFFFFULL;
577}
578
579/**
580 * i40e_stat_update32 - read and update a 32 bit stat from the chip
581 * @hw: ptr to the hardware info
582 * @reg: the hw reg to read
583 * @offset_loaded: has the initial offset been loaded yet
584 * @offset: ptr to current offset value
585 * @stat: ptr to the stat
586 **/
587static void i40e_stat_update32(struct i40e_hw *hw, u32 reg,
588 bool offset_loaded, u64 *offset, u64 *stat)
589{
590 u32 new_data;
591
592 new_data = rd32(hw, reg);
593 if (!offset_loaded)
594 *offset = new_data;
595 if (likely(new_data >= *offset))
596 *stat = (u32)(new_data - *offset);
597 else
Jesse Brandeburg41a1d042015-06-04 16:24:02 -0400598 *stat = (u32)((new_data + BIT_ULL(32)) - *offset);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000599}
600
601/**
602 * i40e_update_eth_stats - Update VSI-specific ethernet statistics counters.
603 * @vsi: the VSI to be updated
604 **/
605void i40e_update_eth_stats(struct i40e_vsi *vsi)
606{
607 int stat_idx = le16_to_cpu(vsi->info.stat_counter_idx);
608 struct i40e_pf *pf = vsi->back;
609 struct i40e_hw *hw = &pf->hw;
610 struct i40e_eth_stats *oes;
611 struct i40e_eth_stats *es; /* device's eth stats */
612
613 es = &vsi->eth_stats;
614 oes = &vsi->eth_stats_offsets;
615
616 /* Gather up the stats that the hw collects */
617 i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
618 vsi->stat_offsets_loaded,
619 &oes->tx_errors, &es->tx_errors);
620 i40e_stat_update32(hw, I40E_GLV_RDPC(stat_idx),
621 vsi->stat_offsets_loaded,
622 &oes->rx_discards, &es->rx_discards);
Shannon Nelson41a9e552014-04-23 04:50:20 +0000623 i40e_stat_update32(hw, I40E_GLV_RUPP(stat_idx),
624 vsi->stat_offsets_loaded,
625 &oes->rx_unknown_protocol, &es->rx_unknown_protocol);
626 i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
627 vsi->stat_offsets_loaded,
628 &oes->tx_errors, &es->tx_errors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000629
630 i40e_stat_update48(hw, I40E_GLV_GORCH(stat_idx),
631 I40E_GLV_GORCL(stat_idx),
632 vsi->stat_offsets_loaded,
633 &oes->rx_bytes, &es->rx_bytes);
634 i40e_stat_update48(hw, I40E_GLV_UPRCH(stat_idx),
635 I40E_GLV_UPRCL(stat_idx),
636 vsi->stat_offsets_loaded,
637 &oes->rx_unicast, &es->rx_unicast);
638 i40e_stat_update48(hw, I40E_GLV_MPRCH(stat_idx),
639 I40E_GLV_MPRCL(stat_idx),
640 vsi->stat_offsets_loaded,
641 &oes->rx_multicast, &es->rx_multicast);
642 i40e_stat_update48(hw, I40E_GLV_BPRCH(stat_idx),
643 I40E_GLV_BPRCL(stat_idx),
644 vsi->stat_offsets_loaded,
645 &oes->rx_broadcast, &es->rx_broadcast);
646
647 i40e_stat_update48(hw, I40E_GLV_GOTCH(stat_idx),
648 I40E_GLV_GOTCL(stat_idx),
649 vsi->stat_offsets_loaded,
650 &oes->tx_bytes, &es->tx_bytes);
651 i40e_stat_update48(hw, I40E_GLV_UPTCH(stat_idx),
652 I40E_GLV_UPTCL(stat_idx),
653 vsi->stat_offsets_loaded,
654 &oes->tx_unicast, &es->tx_unicast);
655 i40e_stat_update48(hw, I40E_GLV_MPTCH(stat_idx),
656 I40E_GLV_MPTCL(stat_idx),
657 vsi->stat_offsets_loaded,
658 &oes->tx_multicast, &es->tx_multicast);
659 i40e_stat_update48(hw, I40E_GLV_BPTCH(stat_idx),
660 I40E_GLV_BPTCL(stat_idx),
661 vsi->stat_offsets_loaded,
662 &oes->tx_broadcast, &es->tx_broadcast);
663 vsi->stat_offsets_loaded = true;
664}
665
666/**
667 * i40e_update_veb_stats - Update Switch component statistics
668 * @veb: the VEB being updated
669 **/
670static void i40e_update_veb_stats(struct i40e_veb *veb)
671{
672 struct i40e_pf *pf = veb->pf;
673 struct i40e_hw *hw = &pf->hw;
674 struct i40e_eth_stats *oes;
675 struct i40e_eth_stats *es; /* device's eth stats */
Neerav Parikhfe860af2015-07-10 19:36:02 -0400676 struct i40e_veb_tc_stats *veb_oes;
677 struct i40e_veb_tc_stats *veb_es;
678 int i, idx = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000679
680 idx = veb->stats_idx;
681 es = &veb->stats;
682 oes = &veb->stats_offsets;
Neerav Parikhfe860af2015-07-10 19:36:02 -0400683 veb_es = &veb->tc_stats;
684 veb_oes = &veb->tc_stats_offsets;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000685
686 /* Gather up the stats that the hw collects */
687 i40e_stat_update32(hw, I40E_GLSW_TDPC(idx),
688 veb->stat_offsets_loaded,
689 &oes->tx_discards, &es->tx_discards);
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +0000690 if (hw->revision_id > 0)
691 i40e_stat_update32(hw, I40E_GLSW_RUPP(idx),
692 veb->stat_offsets_loaded,
693 &oes->rx_unknown_protocol,
694 &es->rx_unknown_protocol);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000695 i40e_stat_update48(hw, I40E_GLSW_GORCH(idx), I40E_GLSW_GORCL(idx),
696 veb->stat_offsets_loaded,
697 &oes->rx_bytes, &es->rx_bytes);
698 i40e_stat_update48(hw, I40E_GLSW_UPRCH(idx), I40E_GLSW_UPRCL(idx),
699 veb->stat_offsets_loaded,
700 &oes->rx_unicast, &es->rx_unicast);
701 i40e_stat_update48(hw, I40E_GLSW_MPRCH(idx), I40E_GLSW_MPRCL(idx),
702 veb->stat_offsets_loaded,
703 &oes->rx_multicast, &es->rx_multicast);
704 i40e_stat_update48(hw, I40E_GLSW_BPRCH(idx), I40E_GLSW_BPRCL(idx),
705 veb->stat_offsets_loaded,
706 &oes->rx_broadcast, &es->rx_broadcast);
707
708 i40e_stat_update48(hw, I40E_GLSW_GOTCH(idx), I40E_GLSW_GOTCL(idx),
709 veb->stat_offsets_loaded,
710 &oes->tx_bytes, &es->tx_bytes);
711 i40e_stat_update48(hw, I40E_GLSW_UPTCH(idx), I40E_GLSW_UPTCL(idx),
712 veb->stat_offsets_loaded,
713 &oes->tx_unicast, &es->tx_unicast);
714 i40e_stat_update48(hw, I40E_GLSW_MPTCH(idx), I40E_GLSW_MPTCL(idx),
715 veb->stat_offsets_loaded,
716 &oes->tx_multicast, &es->tx_multicast);
717 i40e_stat_update48(hw, I40E_GLSW_BPTCH(idx), I40E_GLSW_BPTCL(idx),
718 veb->stat_offsets_loaded,
719 &oes->tx_broadcast, &es->tx_broadcast);
Neerav Parikhfe860af2015-07-10 19:36:02 -0400720 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
721 i40e_stat_update48(hw, I40E_GLVEBTC_RPCH(i, idx),
722 I40E_GLVEBTC_RPCL(i, idx),
723 veb->stat_offsets_loaded,
724 &veb_oes->tc_rx_packets[i],
725 &veb_es->tc_rx_packets[i]);
726 i40e_stat_update48(hw, I40E_GLVEBTC_RBCH(i, idx),
727 I40E_GLVEBTC_RBCL(i, idx),
728 veb->stat_offsets_loaded,
729 &veb_oes->tc_rx_bytes[i],
730 &veb_es->tc_rx_bytes[i]);
731 i40e_stat_update48(hw, I40E_GLVEBTC_TPCH(i, idx),
732 I40E_GLVEBTC_TPCL(i, idx),
733 veb->stat_offsets_loaded,
734 &veb_oes->tc_tx_packets[i],
735 &veb_es->tc_tx_packets[i]);
736 i40e_stat_update48(hw, I40E_GLVEBTC_TBCH(i, idx),
737 I40E_GLVEBTC_TBCL(i, idx),
738 veb->stat_offsets_loaded,
739 &veb_oes->tc_tx_bytes[i],
740 &veb_es->tc_tx_bytes[i]);
741 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000742 veb->stat_offsets_loaded = true;
743}
744
Vasu Dev38e00432014-08-01 13:27:03 -0700745#ifdef I40E_FCOE
746/**
747 * i40e_update_fcoe_stats - Update FCoE-specific ethernet statistics counters.
748 * @vsi: the VSI that is capable of doing FCoE
749 **/
750static void i40e_update_fcoe_stats(struct i40e_vsi *vsi)
751{
752 struct i40e_pf *pf = vsi->back;
753 struct i40e_hw *hw = &pf->hw;
754 struct i40e_fcoe_stats *ofs;
755 struct i40e_fcoe_stats *fs; /* device's eth stats */
756 int idx;
757
758 if (vsi->type != I40E_VSI_FCOE)
759 return;
760
761 idx = (pf->pf_seid - I40E_BASE_PF_SEID) + I40E_FCOE_PF_STAT_OFFSET;
762 fs = &vsi->fcoe_stats;
763 ofs = &vsi->fcoe_stats_offsets;
764
765 i40e_stat_update32(hw, I40E_GL_FCOEPRC(idx),
766 vsi->fcoe_stat_offsets_loaded,
767 &ofs->rx_fcoe_packets, &fs->rx_fcoe_packets);
768 i40e_stat_update48(hw, I40E_GL_FCOEDWRCH(idx), I40E_GL_FCOEDWRCL(idx),
769 vsi->fcoe_stat_offsets_loaded,
770 &ofs->rx_fcoe_dwords, &fs->rx_fcoe_dwords);
771 i40e_stat_update32(hw, I40E_GL_FCOERPDC(idx),
772 vsi->fcoe_stat_offsets_loaded,
773 &ofs->rx_fcoe_dropped, &fs->rx_fcoe_dropped);
774 i40e_stat_update32(hw, I40E_GL_FCOEPTC(idx),
775 vsi->fcoe_stat_offsets_loaded,
776 &ofs->tx_fcoe_packets, &fs->tx_fcoe_packets);
777 i40e_stat_update48(hw, I40E_GL_FCOEDWTCH(idx), I40E_GL_FCOEDWTCL(idx),
778 vsi->fcoe_stat_offsets_loaded,
779 &ofs->tx_fcoe_dwords, &fs->tx_fcoe_dwords);
780 i40e_stat_update32(hw, I40E_GL_FCOECRC(idx),
781 vsi->fcoe_stat_offsets_loaded,
782 &ofs->fcoe_bad_fccrc, &fs->fcoe_bad_fccrc);
783 i40e_stat_update32(hw, I40E_GL_FCOELAST(idx),
784 vsi->fcoe_stat_offsets_loaded,
785 &ofs->fcoe_last_error, &fs->fcoe_last_error);
786 i40e_stat_update32(hw, I40E_GL_FCOEDDPC(idx),
787 vsi->fcoe_stat_offsets_loaded,
788 &ofs->fcoe_ddp_count, &fs->fcoe_ddp_count);
789
790 vsi->fcoe_stat_offsets_loaded = true;
791}
792
793#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000794/**
795 * i40e_update_link_xoff_rx - Update XOFF received in link flow control mode
796 * @pf: the corresponding PF
797 *
798 * Update the Rx XOFF counter (PAUSE frames) in link flow control mode
799 **/
800static void i40e_update_link_xoff_rx(struct i40e_pf *pf)
801{
802 struct i40e_hw_port_stats *osd = &pf->stats_offsets;
803 struct i40e_hw_port_stats *nsd = &pf->stats;
804 struct i40e_hw *hw = &pf->hw;
805 u64 xoff = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000806
807 if ((hw->fc.current_mode != I40E_FC_FULL) &&
808 (hw->fc.current_mode != I40E_FC_RX_PAUSE))
809 return;
810
811 xoff = nsd->link_xoff_rx;
812 i40e_stat_update32(hw, I40E_GLPRT_LXOFFRXC(hw->port),
813 pf->stat_offsets_loaded,
814 &osd->link_xoff_rx, &nsd->link_xoff_rx);
815
816 /* No new LFC xoff rx */
817 if (!(nsd->link_xoff_rx - xoff))
818 return;
819
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000820}
821
822/**
823 * i40e_update_prio_xoff_rx - Update XOFF received in PFC mode
824 * @pf: the corresponding PF
825 *
826 * Update the Rx XOFF counter (PAUSE frames) in PFC mode
827 **/
828static void i40e_update_prio_xoff_rx(struct i40e_pf *pf)
829{
830 struct i40e_hw_port_stats *osd = &pf->stats_offsets;
831 struct i40e_hw_port_stats *nsd = &pf->stats;
832 bool xoff[I40E_MAX_TRAFFIC_CLASS] = {false};
833 struct i40e_dcbx_config *dcb_cfg;
834 struct i40e_hw *hw = &pf->hw;
Kiran Patilb03a8c12015-09-24 18:13:15 -0400835 u16 i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000836 u8 tc;
837
838 dcb_cfg = &hw->local_dcbx_config;
839
Neerav Parikhe1208142015-04-16 20:05:58 -0400840 /* Collect Link XOFF stats when PFC is disabled */
841 if (!dcb_cfg->pfc.pfcenable) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000842 i40e_update_link_xoff_rx(pf);
843 return;
844 }
845
846 for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
847 u64 prio_xoff = nsd->priority_xoff_rx[i];
Jesse Brandeburg6995b362015-08-28 17:55:54 -0400848
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000849 i40e_stat_update32(hw, I40E_GLPRT_PXOFFRXC(hw->port, i),
850 pf->stat_offsets_loaded,
851 &osd->priority_xoff_rx[i],
852 &nsd->priority_xoff_rx[i]);
853
854 /* No new PFC xoff rx */
855 if (!(nsd->priority_xoff_rx[i] - prio_xoff))
856 continue;
857 /* Get the TC for given priority */
858 tc = dcb_cfg->etscfg.prioritytable[i];
859 xoff[tc] = true;
860 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000861}
862
863/**
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000864 * i40e_update_vsi_stats - Update the vsi statistics counters.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000865 * @vsi: the VSI to be updated
866 *
867 * There are a few instances where we store the same stat in a
868 * couple of different structs. This is partly because we have
869 * the netdev stats that need to be filled out, which is slightly
870 * different from the "eth_stats" defined by the chip and used in
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000871 * VF communications. We sort it out here.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000872 **/
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000873static void i40e_update_vsi_stats(struct i40e_vsi *vsi)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000874{
875 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000876 struct rtnl_link_stats64 *ons;
877 struct rtnl_link_stats64 *ns; /* netdev stats */
878 struct i40e_eth_stats *oes;
879 struct i40e_eth_stats *es; /* device's eth stats */
880 u32 tx_restart, tx_busy;
Akeem G Abodunrinbf00b372014-10-17 03:14:39 +0000881 struct i40e_ring *p;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000882 u32 rx_page, rx_buf;
Akeem G Abodunrinbf00b372014-10-17 03:14:39 +0000883 u64 bytes, packets;
884 unsigned int start;
Anjali Singhai Jain2fc3d712015-08-27 11:42:29 -0400885 u64 tx_linearize;
Anjali Singhai Jain164c9f52015-10-21 19:47:08 -0400886 u64 tx_force_wb;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000887 u64 rx_p, rx_b;
888 u64 tx_p, tx_b;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000889 u16 q;
890
891 if (test_bit(__I40E_DOWN, &vsi->state) ||
892 test_bit(__I40E_CONFIG_BUSY, &pf->state))
893 return;
894
895 ns = i40e_get_vsi_stats_struct(vsi);
896 ons = &vsi->net_stats_offsets;
897 es = &vsi->eth_stats;
898 oes = &vsi->eth_stats_offsets;
899
900 /* Gather up the netdev and vsi stats that the driver collects
901 * on the fly during packet processing
902 */
903 rx_b = rx_p = 0;
904 tx_b = tx_p = 0;
Anjali Singhai Jain164c9f52015-10-21 19:47:08 -0400905 tx_restart = tx_busy = tx_linearize = tx_force_wb = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000906 rx_page = 0;
907 rx_buf = 0;
Alexander Duyck980e9b12013-09-28 06:01:03 +0000908 rcu_read_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000909 for (q = 0; q < vsi->num_queue_pairs; q++) {
Alexander Duyck980e9b12013-09-28 06:01:03 +0000910 /* locate Tx ring */
911 p = ACCESS_ONCE(vsi->tx_rings[q]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000912
Alexander Duyck980e9b12013-09-28 06:01:03 +0000913 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700914 start = u64_stats_fetch_begin_irq(&p->syncp);
Alexander Duyck980e9b12013-09-28 06:01:03 +0000915 packets = p->stats.packets;
916 bytes = p->stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700917 } while (u64_stats_fetch_retry_irq(&p->syncp, start));
Alexander Duyck980e9b12013-09-28 06:01:03 +0000918 tx_b += bytes;
919 tx_p += packets;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000920 tx_restart += p->tx_stats.restart_queue;
921 tx_busy += p->tx_stats.tx_busy;
Anjali Singhai Jain2fc3d712015-08-27 11:42:29 -0400922 tx_linearize += p->tx_stats.tx_linearize;
Anjali Singhai Jain164c9f52015-10-21 19:47:08 -0400923 tx_force_wb += p->tx_stats.tx_force_wb;
Alexander Duyck980e9b12013-09-28 06:01:03 +0000924
925 /* Rx queue is part of the same block as Tx queue */
926 p = &p[1];
927 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700928 start = u64_stats_fetch_begin_irq(&p->syncp);
Alexander Duyck980e9b12013-09-28 06:01:03 +0000929 packets = p->stats.packets;
930 bytes = p->stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700931 } while (u64_stats_fetch_retry_irq(&p->syncp, start));
Alexander Duyck980e9b12013-09-28 06:01:03 +0000932 rx_b += bytes;
933 rx_p += packets;
Mitch Williams420136c2013-12-18 13:45:59 +0000934 rx_buf += p->rx_stats.alloc_buff_failed;
935 rx_page += p->rx_stats.alloc_page_failed;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000936 }
Alexander Duyck980e9b12013-09-28 06:01:03 +0000937 rcu_read_unlock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000938 vsi->tx_restart = tx_restart;
939 vsi->tx_busy = tx_busy;
Anjali Singhai Jain2fc3d712015-08-27 11:42:29 -0400940 vsi->tx_linearize = tx_linearize;
Anjali Singhai Jain164c9f52015-10-21 19:47:08 -0400941 vsi->tx_force_wb = tx_force_wb;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000942 vsi->rx_page_failed = rx_page;
943 vsi->rx_buf_failed = rx_buf;
944
945 ns->rx_packets = rx_p;
946 ns->rx_bytes = rx_b;
947 ns->tx_packets = tx_p;
948 ns->tx_bytes = tx_b;
949
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000950 /* update netdev stats from eth stats */
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000951 i40e_update_eth_stats(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000952 ons->tx_errors = oes->tx_errors;
953 ns->tx_errors = es->tx_errors;
954 ons->multicast = oes->rx_multicast;
955 ns->multicast = es->rx_multicast;
Shannon Nelson41a9e552014-04-23 04:50:20 +0000956 ons->rx_dropped = oes->rx_discards;
957 ns->rx_dropped = es->rx_discards;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000958 ons->tx_dropped = oes->tx_discards;
959 ns->tx_dropped = es->tx_discards;
960
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000961 /* pull in a couple PF stats if this is the main vsi */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000962 if (vsi == pf->vsi[pf->lan_vsi]) {
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000963 ns->rx_crc_errors = pf->stats.crc_errors;
964 ns->rx_errors = pf->stats.crc_errors + pf->stats.illegal_bytes;
965 ns->rx_length_errors = pf->stats.rx_length_errors;
966 }
967}
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000968
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000969/**
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000970 * i40e_update_pf_stats - Update the PF statistics counters.
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000971 * @pf: the PF to be updated
972 **/
973static void i40e_update_pf_stats(struct i40e_pf *pf)
974{
975 struct i40e_hw_port_stats *osd = &pf->stats_offsets;
976 struct i40e_hw_port_stats *nsd = &pf->stats;
977 struct i40e_hw *hw = &pf->hw;
978 u32 val;
979 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000980
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000981 i40e_stat_update48(hw, I40E_GLPRT_GORCH(hw->port),
982 I40E_GLPRT_GORCL(hw->port),
983 pf->stat_offsets_loaded,
984 &osd->eth.rx_bytes, &nsd->eth.rx_bytes);
985 i40e_stat_update48(hw, I40E_GLPRT_GOTCH(hw->port),
986 I40E_GLPRT_GOTCL(hw->port),
987 pf->stat_offsets_loaded,
988 &osd->eth.tx_bytes, &nsd->eth.tx_bytes);
989 i40e_stat_update32(hw, I40E_GLPRT_RDPC(hw->port),
990 pf->stat_offsets_loaded,
991 &osd->eth.rx_discards,
992 &nsd->eth.rx_discards);
Shannon Nelson532d2832014-04-23 04:50:09 +0000993 i40e_stat_update48(hw, I40E_GLPRT_UPRCH(hw->port),
994 I40E_GLPRT_UPRCL(hw->port),
995 pf->stat_offsets_loaded,
996 &osd->eth.rx_unicast,
997 &nsd->eth.rx_unicast);
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000998 i40e_stat_update48(hw, I40E_GLPRT_MPRCH(hw->port),
999 I40E_GLPRT_MPRCL(hw->port),
1000 pf->stat_offsets_loaded,
1001 &osd->eth.rx_multicast,
1002 &nsd->eth.rx_multicast);
Shannon Nelson532d2832014-04-23 04:50:09 +00001003 i40e_stat_update48(hw, I40E_GLPRT_BPRCH(hw->port),
1004 I40E_GLPRT_BPRCL(hw->port),
1005 pf->stat_offsets_loaded,
1006 &osd->eth.rx_broadcast,
1007 &nsd->eth.rx_broadcast);
1008 i40e_stat_update48(hw, I40E_GLPRT_UPTCH(hw->port),
1009 I40E_GLPRT_UPTCL(hw->port),
1010 pf->stat_offsets_loaded,
1011 &osd->eth.tx_unicast,
1012 &nsd->eth.tx_unicast);
1013 i40e_stat_update48(hw, I40E_GLPRT_MPTCH(hw->port),
1014 I40E_GLPRT_MPTCL(hw->port),
1015 pf->stat_offsets_loaded,
1016 &osd->eth.tx_multicast,
1017 &nsd->eth.tx_multicast);
1018 i40e_stat_update48(hw, I40E_GLPRT_BPTCH(hw->port),
1019 I40E_GLPRT_BPTCL(hw->port),
1020 pf->stat_offsets_loaded,
1021 &osd->eth.tx_broadcast,
1022 &nsd->eth.tx_broadcast);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001023
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001024 i40e_stat_update32(hw, I40E_GLPRT_TDOLD(hw->port),
1025 pf->stat_offsets_loaded,
1026 &osd->tx_dropped_link_down,
1027 &nsd->tx_dropped_link_down);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001028
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001029 i40e_stat_update32(hw, I40E_GLPRT_CRCERRS(hw->port),
1030 pf->stat_offsets_loaded,
1031 &osd->crc_errors, &nsd->crc_errors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001032
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001033 i40e_stat_update32(hw, I40E_GLPRT_ILLERRC(hw->port),
1034 pf->stat_offsets_loaded,
1035 &osd->illegal_bytes, &nsd->illegal_bytes);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001036
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001037 i40e_stat_update32(hw, I40E_GLPRT_MLFC(hw->port),
1038 pf->stat_offsets_loaded,
1039 &osd->mac_local_faults,
1040 &nsd->mac_local_faults);
1041 i40e_stat_update32(hw, I40E_GLPRT_MRFC(hw->port),
1042 pf->stat_offsets_loaded,
1043 &osd->mac_remote_faults,
1044 &nsd->mac_remote_faults);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001045
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001046 i40e_stat_update32(hw, I40E_GLPRT_RLEC(hw->port),
1047 pf->stat_offsets_loaded,
1048 &osd->rx_length_errors,
1049 &nsd->rx_length_errors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001050
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001051 i40e_stat_update32(hw, I40E_GLPRT_LXONRXC(hw->port),
1052 pf->stat_offsets_loaded,
1053 &osd->link_xon_rx, &nsd->link_xon_rx);
1054 i40e_stat_update32(hw, I40E_GLPRT_LXONTXC(hw->port),
1055 pf->stat_offsets_loaded,
1056 &osd->link_xon_tx, &nsd->link_xon_tx);
1057 i40e_update_prio_xoff_rx(pf); /* handles I40E_GLPRT_LXOFFRXC */
1058 i40e_stat_update32(hw, I40E_GLPRT_LXOFFTXC(hw->port),
1059 pf->stat_offsets_loaded,
1060 &osd->link_xoff_tx, &nsd->link_xoff_tx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001061
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001062 for (i = 0; i < 8; i++) {
1063 i40e_stat_update32(hw, I40E_GLPRT_PXONRXC(hw->port, i),
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001064 pf->stat_offsets_loaded,
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001065 &osd->priority_xon_rx[i],
1066 &nsd->priority_xon_rx[i]);
1067 i40e_stat_update32(hw, I40E_GLPRT_PXONTXC(hw->port, i),
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001068 pf->stat_offsets_loaded,
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001069 &osd->priority_xon_tx[i],
1070 &nsd->priority_xon_tx[i]);
1071 i40e_stat_update32(hw, I40E_GLPRT_PXOFFTXC(hw->port, i),
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001072 pf->stat_offsets_loaded,
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001073 &osd->priority_xoff_tx[i],
1074 &nsd->priority_xoff_tx[i]);
1075 i40e_stat_update32(hw,
1076 I40E_GLPRT_RXON2OFFCNT(hw->port, i),
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001077 pf->stat_offsets_loaded,
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001078 &osd->priority_xon_2_xoff[i],
1079 &nsd->priority_xon_2_xoff[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001080 }
1081
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001082 i40e_stat_update48(hw, I40E_GLPRT_PRC64H(hw->port),
1083 I40E_GLPRT_PRC64L(hw->port),
1084 pf->stat_offsets_loaded,
1085 &osd->rx_size_64, &nsd->rx_size_64);
1086 i40e_stat_update48(hw, I40E_GLPRT_PRC127H(hw->port),
1087 I40E_GLPRT_PRC127L(hw->port),
1088 pf->stat_offsets_loaded,
1089 &osd->rx_size_127, &nsd->rx_size_127);
1090 i40e_stat_update48(hw, I40E_GLPRT_PRC255H(hw->port),
1091 I40E_GLPRT_PRC255L(hw->port),
1092 pf->stat_offsets_loaded,
1093 &osd->rx_size_255, &nsd->rx_size_255);
1094 i40e_stat_update48(hw, I40E_GLPRT_PRC511H(hw->port),
1095 I40E_GLPRT_PRC511L(hw->port),
1096 pf->stat_offsets_loaded,
1097 &osd->rx_size_511, &nsd->rx_size_511);
1098 i40e_stat_update48(hw, I40E_GLPRT_PRC1023H(hw->port),
1099 I40E_GLPRT_PRC1023L(hw->port),
1100 pf->stat_offsets_loaded,
1101 &osd->rx_size_1023, &nsd->rx_size_1023);
1102 i40e_stat_update48(hw, I40E_GLPRT_PRC1522H(hw->port),
1103 I40E_GLPRT_PRC1522L(hw->port),
1104 pf->stat_offsets_loaded,
1105 &osd->rx_size_1522, &nsd->rx_size_1522);
1106 i40e_stat_update48(hw, I40E_GLPRT_PRC9522H(hw->port),
1107 I40E_GLPRT_PRC9522L(hw->port),
1108 pf->stat_offsets_loaded,
1109 &osd->rx_size_big, &nsd->rx_size_big);
1110
1111 i40e_stat_update48(hw, I40E_GLPRT_PTC64H(hw->port),
1112 I40E_GLPRT_PTC64L(hw->port),
1113 pf->stat_offsets_loaded,
1114 &osd->tx_size_64, &nsd->tx_size_64);
1115 i40e_stat_update48(hw, I40E_GLPRT_PTC127H(hw->port),
1116 I40E_GLPRT_PTC127L(hw->port),
1117 pf->stat_offsets_loaded,
1118 &osd->tx_size_127, &nsd->tx_size_127);
1119 i40e_stat_update48(hw, I40E_GLPRT_PTC255H(hw->port),
1120 I40E_GLPRT_PTC255L(hw->port),
1121 pf->stat_offsets_loaded,
1122 &osd->tx_size_255, &nsd->tx_size_255);
1123 i40e_stat_update48(hw, I40E_GLPRT_PTC511H(hw->port),
1124 I40E_GLPRT_PTC511L(hw->port),
1125 pf->stat_offsets_loaded,
1126 &osd->tx_size_511, &nsd->tx_size_511);
1127 i40e_stat_update48(hw, I40E_GLPRT_PTC1023H(hw->port),
1128 I40E_GLPRT_PTC1023L(hw->port),
1129 pf->stat_offsets_loaded,
1130 &osd->tx_size_1023, &nsd->tx_size_1023);
1131 i40e_stat_update48(hw, I40E_GLPRT_PTC1522H(hw->port),
1132 I40E_GLPRT_PTC1522L(hw->port),
1133 pf->stat_offsets_loaded,
1134 &osd->tx_size_1522, &nsd->tx_size_1522);
1135 i40e_stat_update48(hw, I40E_GLPRT_PTC9522H(hw->port),
1136 I40E_GLPRT_PTC9522L(hw->port),
1137 pf->stat_offsets_loaded,
1138 &osd->tx_size_big, &nsd->tx_size_big);
1139
1140 i40e_stat_update32(hw, I40E_GLPRT_RUC(hw->port),
1141 pf->stat_offsets_loaded,
1142 &osd->rx_undersize, &nsd->rx_undersize);
1143 i40e_stat_update32(hw, I40E_GLPRT_RFC(hw->port),
1144 pf->stat_offsets_loaded,
1145 &osd->rx_fragments, &nsd->rx_fragments);
1146 i40e_stat_update32(hw, I40E_GLPRT_ROC(hw->port),
1147 pf->stat_offsets_loaded,
1148 &osd->rx_oversize, &nsd->rx_oversize);
1149 i40e_stat_update32(hw, I40E_GLPRT_RJC(hw->port),
1150 pf->stat_offsets_loaded,
1151 &osd->rx_jabber, &nsd->rx_jabber);
1152
Anjali Singhai Jain433c47d2014-05-22 06:32:17 +00001153 /* FDIR stats */
Anjali Singhai Jain0bf4b1b2015-04-16 20:06:02 -04001154 i40e_stat_update32(hw,
1155 I40E_GLQF_PCNT(I40E_FD_ATR_STAT_IDX(pf->hw.pf_id)),
Anjali Singhai Jain433c47d2014-05-22 06:32:17 +00001156 pf->stat_offsets_loaded,
1157 &osd->fd_atr_match, &nsd->fd_atr_match);
Anjali Singhai Jain0bf4b1b2015-04-16 20:06:02 -04001158 i40e_stat_update32(hw,
1159 I40E_GLQF_PCNT(I40E_FD_SB_STAT_IDX(pf->hw.pf_id)),
Anjali Singhai Jain433c47d2014-05-22 06:32:17 +00001160 pf->stat_offsets_loaded,
1161 &osd->fd_sb_match, &nsd->fd_sb_match);
Anjali Singhai Jain60ccd452015-04-16 20:06:01 -04001162 i40e_stat_update32(hw,
1163 I40E_GLQF_PCNT(I40E_FD_ATR_TUNNEL_STAT_IDX(pf->hw.pf_id)),
1164 pf->stat_offsets_loaded,
1165 &osd->fd_atr_tunnel_match, &nsd->fd_atr_tunnel_match);
Anjali Singhai Jain433c47d2014-05-22 06:32:17 +00001166
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001167 val = rd32(hw, I40E_PRTPM_EEE_STAT);
1168 nsd->tx_lpi_status =
1169 (val & I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_MASK) >>
1170 I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_SHIFT;
1171 nsd->rx_lpi_status =
1172 (val & I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_MASK) >>
1173 I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_SHIFT;
1174 i40e_stat_update32(hw, I40E_PRTPM_TLPIC,
1175 pf->stat_offsets_loaded,
1176 &osd->tx_lpi_count, &nsd->tx_lpi_count);
1177 i40e_stat_update32(hw, I40E_PRTPM_RLPIC,
1178 pf->stat_offsets_loaded,
1179 &osd->rx_lpi_count, &nsd->rx_lpi_count);
1180
Anjali Singhai Jaind0389e52015-04-22 19:34:05 -04001181 if (pf->flags & I40E_FLAG_FD_SB_ENABLED &&
1182 !(pf->auto_disable_flags & I40E_FLAG_FD_SB_ENABLED))
1183 nsd->fd_sb_status = true;
1184 else
1185 nsd->fd_sb_status = false;
1186
1187 if (pf->flags & I40E_FLAG_FD_ATR_ENABLED &&
1188 !(pf->auto_disable_flags & I40E_FLAG_FD_ATR_ENABLED))
1189 nsd->fd_atr_status = true;
1190 else
1191 nsd->fd_atr_status = false;
1192
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001193 pf->stat_offsets_loaded = true;
1194}
1195
1196/**
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001197 * i40e_update_stats - Update the various statistics counters.
1198 * @vsi: the VSI to be updated
1199 *
1200 * Update the various stats for this VSI and its related entities.
1201 **/
1202void i40e_update_stats(struct i40e_vsi *vsi)
1203{
1204 struct i40e_pf *pf = vsi->back;
1205
1206 if (vsi == pf->vsi[pf->lan_vsi])
1207 i40e_update_pf_stats(pf);
1208
1209 i40e_update_vsi_stats(vsi);
Vasu Dev38e00432014-08-01 13:27:03 -07001210#ifdef I40E_FCOE
1211 i40e_update_fcoe_stats(vsi);
1212#endif
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001213}
1214
1215/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001216 * i40e_find_filter - Search VSI filter list for specific mac/vlan filter
1217 * @vsi: the VSI to be searched
1218 * @macaddr: the MAC address
1219 * @vlan: the vlan
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001220 * @is_vf: make sure its a VF filter, else doesn't matter
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001221 * @is_netdev: make sure its a netdev filter, else doesn't matter
1222 *
1223 * Returns ptr to the filter object or NULL
1224 **/
1225static struct i40e_mac_filter *i40e_find_filter(struct i40e_vsi *vsi,
1226 u8 *macaddr, s16 vlan,
1227 bool is_vf, bool is_netdev)
1228{
1229 struct i40e_mac_filter *f;
1230
1231 if (!vsi || !macaddr)
1232 return NULL;
1233
1234 list_for_each_entry(f, &vsi->mac_filter_list, list) {
1235 if ((ether_addr_equal(macaddr, f->macaddr)) &&
1236 (vlan == f->vlan) &&
1237 (!is_vf || f->is_vf) &&
1238 (!is_netdev || f->is_netdev))
1239 return f;
1240 }
1241 return NULL;
1242}
1243
1244/**
1245 * i40e_find_mac - Find a mac addr in the macvlan filters list
1246 * @vsi: the VSI to be searched
1247 * @macaddr: the MAC address we are searching for
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001248 * @is_vf: make sure its a VF filter, else doesn't matter
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001249 * @is_netdev: make sure its a netdev filter, else doesn't matter
1250 *
1251 * Returns the first filter with the provided MAC address or NULL if
1252 * MAC address was not found
1253 **/
1254struct i40e_mac_filter *i40e_find_mac(struct i40e_vsi *vsi, u8 *macaddr,
1255 bool is_vf, bool is_netdev)
1256{
1257 struct i40e_mac_filter *f;
1258
1259 if (!vsi || !macaddr)
1260 return NULL;
1261
1262 list_for_each_entry(f, &vsi->mac_filter_list, list) {
1263 if ((ether_addr_equal(macaddr, f->macaddr)) &&
1264 (!is_vf || f->is_vf) &&
1265 (!is_netdev || f->is_netdev))
1266 return f;
1267 }
1268 return NULL;
1269}
1270
1271/**
1272 * i40e_is_vsi_in_vlan - Check if VSI is in vlan mode
1273 * @vsi: the VSI to be searched
1274 *
1275 * Returns true if VSI is in vlan mode or false otherwise
1276 **/
1277bool i40e_is_vsi_in_vlan(struct i40e_vsi *vsi)
1278{
1279 struct i40e_mac_filter *f;
1280
1281 /* Only -1 for all the filters denotes not in vlan mode
1282 * so we have to go through all the list in order to make sure
1283 */
1284 list_for_each_entry(f, &vsi->mac_filter_list, list) {
Greg Rosed9b68f82015-07-23 16:54:31 -04001285 if (f->vlan >= 0 || vsi->info.pvid)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001286 return true;
1287 }
1288
1289 return false;
1290}
1291
1292/**
1293 * i40e_put_mac_in_vlan - Make macvlan filters from macaddrs and vlans
1294 * @vsi: the VSI to be searched
1295 * @macaddr: the mac address to be filtered
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001296 * @is_vf: true if it is a VF
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001297 * @is_netdev: true if it is a netdev
1298 *
1299 * Goes through all the macvlan filters and adds a
1300 * macvlan filter for each unique vlan that already exists
1301 *
1302 * Returns first filter found on success, else NULL
1303 **/
1304struct i40e_mac_filter *i40e_put_mac_in_vlan(struct i40e_vsi *vsi, u8 *macaddr,
1305 bool is_vf, bool is_netdev)
1306{
1307 struct i40e_mac_filter *f;
1308
1309 list_for_each_entry(f, &vsi->mac_filter_list, list) {
Mitch Williamsecbb44e2015-07-10 19:35:56 -04001310 if (vsi->info.pvid)
1311 f->vlan = le16_to_cpu(vsi->info.pvid);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001312 if (!i40e_find_filter(vsi, macaddr, f->vlan,
1313 is_vf, is_netdev)) {
1314 if (!i40e_add_filter(vsi, macaddr, f->vlan,
Jesse Brandeburg8fb905b2014-01-17 15:36:33 -08001315 is_vf, is_netdev))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001316 return NULL;
1317 }
1318 }
1319
1320 return list_first_entry_or_null(&vsi->mac_filter_list,
1321 struct i40e_mac_filter, list);
1322}
1323
1324/**
Greg Rose8c27d422014-05-22 06:31:56 +00001325 * i40e_rm_default_mac_filter - Remove the default MAC filter set by NVM
1326 * @vsi: the PF Main VSI - inappropriate for any other VSI
1327 * @macaddr: the MAC address
Shannon Nelson30650cc2014-07-29 04:01:50 +00001328 *
1329 * Some older firmware configurations set up a default promiscuous VLAN
1330 * filter that needs to be removed.
Greg Rose8c27d422014-05-22 06:31:56 +00001331 **/
Shannon Nelson30650cc2014-07-29 04:01:50 +00001332static int i40e_rm_default_mac_filter(struct i40e_vsi *vsi, u8 *macaddr)
Greg Rose8c27d422014-05-22 06:31:56 +00001333{
1334 struct i40e_aqc_remove_macvlan_element_data element;
1335 struct i40e_pf *pf = vsi->back;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04001336 i40e_status ret;
Greg Rose8c27d422014-05-22 06:31:56 +00001337
1338 /* Only appropriate for the PF main VSI */
1339 if (vsi->type != I40E_VSI_MAIN)
Shannon Nelson30650cc2014-07-29 04:01:50 +00001340 return -EINVAL;
Greg Rose8c27d422014-05-22 06:31:56 +00001341
Shannon Nelson30650cc2014-07-29 04:01:50 +00001342 memset(&element, 0, sizeof(element));
Greg Rose8c27d422014-05-22 06:31:56 +00001343 ether_addr_copy(element.mac_addr, macaddr);
1344 element.vlan_tag = 0;
1345 element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH |
1346 I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04001347 ret = i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1348 if (ret)
Shannon Nelson30650cc2014-07-29 04:01:50 +00001349 return -ENOENT;
1350
1351 return 0;
Greg Rose8c27d422014-05-22 06:31:56 +00001352}
1353
1354/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001355 * i40e_add_filter - Add a mac/vlan filter to the VSI
1356 * @vsi: the VSI to be searched
1357 * @macaddr: the MAC address
1358 * @vlan: the vlan
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001359 * @is_vf: make sure its a VF filter, else doesn't matter
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001360 * @is_netdev: make sure its a netdev filter, else doesn't matter
1361 *
1362 * Returns ptr to the filter object or NULL when no memory available.
Kiran Patil21659032015-09-30 14:09:03 -04001363 *
1364 * NOTE: This function is expected to be called with mac_filter_list_lock
1365 * being held.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001366 **/
1367struct i40e_mac_filter *i40e_add_filter(struct i40e_vsi *vsi,
1368 u8 *macaddr, s16 vlan,
1369 bool is_vf, bool is_netdev)
1370{
1371 struct i40e_mac_filter *f;
1372
1373 if (!vsi || !macaddr)
1374 return NULL;
1375
1376 f = i40e_find_filter(vsi, macaddr, vlan, is_vf, is_netdev);
1377 if (!f) {
1378 f = kzalloc(sizeof(*f), GFP_ATOMIC);
1379 if (!f)
1380 goto add_filter_out;
1381
Greg Rose9a173902014-05-22 06:32:02 +00001382 ether_addr_copy(f->macaddr, macaddr);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001383 f->vlan = vlan;
1384 f->changed = true;
1385
1386 INIT_LIST_HEAD(&f->list);
1387 list_add(&f->list, &vsi->mac_filter_list);
1388 }
1389
1390 /* increment counter and add a new flag if needed */
1391 if (is_vf) {
1392 if (!f->is_vf) {
1393 f->is_vf = true;
1394 f->counter++;
1395 }
1396 } else if (is_netdev) {
1397 if (!f->is_netdev) {
1398 f->is_netdev = true;
1399 f->counter++;
1400 }
1401 } else {
1402 f->counter++;
1403 }
1404
1405 /* changed tells sync_filters_subtask to
1406 * push the filter down to the firmware
1407 */
1408 if (f->changed) {
1409 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1410 vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
1411 }
1412
1413add_filter_out:
1414 return f;
1415}
1416
1417/**
1418 * i40e_del_filter - Remove a mac/vlan filter from the VSI
1419 * @vsi: the VSI to be searched
1420 * @macaddr: the MAC address
1421 * @vlan: the vlan
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001422 * @is_vf: make sure it's a VF filter, else doesn't matter
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001423 * @is_netdev: make sure it's a netdev filter, else doesn't matter
Kiran Patil21659032015-09-30 14:09:03 -04001424 *
1425 * NOTE: This function is expected to be called with mac_filter_list_lock
1426 * being held.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001427 **/
1428void i40e_del_filter(struct i40e_vsi *vsi,
1429 u8 *macaddr, s16 vlan,
1430 bool is_vf, bool is_netdev)
1431{
1432 struct i40e_mac_filter *f;
1433
1434 if (!vsi || !macaddr)
1435 return;
1436
1437 f = i40e_find_filter(vsi, macaddr, vlan, is_vf, is_netdev);
1438 if (!f || f->counter == 0)
1439 return;
1440
1441 if (is_vf) {
1442 if (f->is_vf) {
1443 f->is_vf = false;
1444 f->counter--;
1445 }
1446 } else if (is_netdev) {
1447 if (f->is_netdev) {
1448 f->is_netdev = false;
1449 f->counter--;
1450 }
1451 } else {
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001452 /* make sure we don't remove a filter in use by VF or netdev */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001453 int min_f = 0;
Jesse Brandeburg6995b362015-08-28 17:55:54 -04001454
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001455 min_f += (f->is_vf ? 1 : 0);
1456 min_f += (f->is_netdev ? 1 : 0);
1457
1458 if (f->counter > min_f)
1459 f->counter--;
1460 }
1461
1462 /* counter == 0 tells sync_filters_subtask to
1463 * remove the filter from the firmware's list
1464 */
1465 if (f->counter == 0) {
1466 f->changed = true;
1467 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1468 vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
1469 }
1470}
1471
1472/**
1473 * i40e_set_mac - NDO callback to set mac address
1474 * @netdev: network interface device structure
1475 * @p: pointer to an address structure
1476 *
1477 * Returns 0 on success, negative on failure
1478 **/
Vasu Dev38e00432014-08-01 13:27:03 -07001479#ifdef I40E_FCOE
1480int i40e_set_mac(struct net_device *netdev, void *p)
1481#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001482static int i40e_set_mac(struct net_device *netdev, void *p)
Vasu Dev38e00432014-08-01 13:27:03 -07001483#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001484{
1485 struct i40e_netdev_priv *np = netdev_priv(netdev);
1486 struct i40e_vsi *vsi = np->vsi;
Shannon Nelson30650cc2014-07-29 04:01:50 +00001487 struct i40e_pf *pf = vsi->back;
1488 struct i40e_hw *hw = &pf->hw;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001489 struct sockaddr *addr = p;
1490 struct i40e_mac_filter *f;
1491
1492 if (!is_valid_ether_addr(addr->sa_data))
1493 return -EADDRNOTAVAIL;
1494
Shannon Nelson30650cc2014-07-29 04:01:50 +00001495 if (ether_addr_equal(netdev->dev_addr, addr->sa_data)) {
1496 netdev_info(netdev, "already using mac address %pM\n",
1497 addr->sa_data);
1498 return 0;
1499 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001500
Anjali Singhai Jain80f64282013-11-28 06:39:47 +00001501 if (test_bit(__I40E_DOWN, &vsi->back->state) ||
1502 test_bit(__I40E_RESET_RECOVERY_PENDING, &vsi->back->state))
1503 return -EADDRNOTAVAIL;
1504
Shannon Nelson30650cc2014-07-29 04:01:50 +00001505 if (ether_addr_equal(hw->mac.addr, addr->sa_data))
1506 netdev_info(netdev, "returning to hw mac address %pM\n",
1507 hw->mac.addr);
1508 else
1509 netdev_info(netdev, "set new mac address %pM\n", addr->sa_data);
1510
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001511 if (vsi->type == I40E_VSI_MAIN) {
1512 i40e_status ret;
Jesse Brandeburg6995b362015-08-28 17:55:54 -04001513
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001514 ret = i40e_aq_mac_address_write(&vsi->back->hw,
Shannon Nelsoncc412222014-06-04 01:23:21 +00001515 I40E_AQC_WRITE_TYPE_LAA_WOL,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001516 addr->sa_data, NULL);
1517 if (ret) {
1518 netdev_info(netdev,
1519 "Addr change for Main VSI failed: %d\n",
1520 ret);
1521 return -EADDRNOTAVAIL;
1522 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001523 }
1524
Shannon Nelson30650cc2014-07-29 04:01:50 +00001525 if (ether_addr_equal(netdev->dev_addr, hw->mac.addr)) {
1526 struct i40e_aqc_remove_macvlan_element_data element;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001527
Shannon Nelson30650cc2014-07-29 04:01:50 +00001528 memset(&element, 0, sizeof(element));
1529 ether_addr_copy(element.mac_addr, netdev->dev_addr);
1530 element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
1531 i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1532 } else {
Kiran Patil21659032015-09-30 14:09:03 -04001533 spin_lock_bh(&vsi->mac_filter_list_lock);
Shannon Nelson6c8ad1b2014-06-04 01:23:22 +00001534 i40e_del_filter(vsi, netdev->dev_addr, I40E_VLAN_ANY,
1535 false, false);
Kiran Patil21659032015-09-30 14:09:03 -04001536 spin_unlock_bh(&vsi->mac_filter_list_lock);
Shannon Nelson6c8ad1b2014-06-04 01:23:22 +00001537 }
1538
Shannon Nelson30650cc2014-07-29 04:01:50 +00001539 if (ether_addr_equal(addr->sa_data, hw->mac.addr)) {
1540 struct i40e_aqc_add_macvlan_element_data element;
1541
1542 memset(&element, 0, sizeof(element));
1543 ether_addr_copy(element.mac_addr, hw->mac.addr);
1544 element.flags = cpu_to_le16(I40E_AQC_MACVLAN_ADD_PERFECT_MATCH);
1545 i40e_aq_add_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1546 } else {
Kiran Patil21659032015-09-30 14:09:03 -04001547 spin_lock_bh(&vsi->mac_filter_list_lock);
Shannon Nelson30650cc2014-07-29 04:01:50 +00001548 f = i40e_add_filter(vsi, addr->sa_data, I40E_VLAN_ANY,
1549 false, false);
1550 if (f)
1551 f->is_laa = true;
Kiran Patil21659032015-09-30 14:09:03 -04001552 spin_unlock_bh(&vsi->mac_filter_list_lock);
Shannon Nelson30650cc2014-07-29 04:01:50 +00001553 }
1554
Anjali Singhai30e25612015-09-28 13:37:12 -07001555 i40e_sync_vsi_filters(vsi, false);
Shannon Nelson30650cc2014-07-29 04:01:50 +00001556 ether_addr_copy(netdev->dev_addr, addr->sa_data);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001557
1558 return 0;
1559}
1560
1561/**
1562 * i40e_vsi_setup_queue_map - Setup a VSI queue map based on enabled_tc
1563 * @vsi: the VSI being setup
1564 * @ctxt: VSI context structure
1565 * @enabled_tc: Enabled TCs bitmap
1566 * @is_add: True if called before Add VSI
1567 *
1568 * Setup VSI queue mapping for enabled traffic classes.
1569 **/
Vasu Dev38e00432014-08-01 13:27:03 -07001570#ifdef I40E_FCOE
1571void i40e_vsi_setup_queue_map(struct i40e_vsi *vsi,
1572 struct i40e_vsi_context *ctxt,
1573 u8 enabled_tc,
1574 bool is_add)
1575#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001576static void i40e_vsi_setup_queue_map(struct i40e_vsi *vsi,
1577 struct i40e_vsi_context *ctxt,
1578 u8 enabled_tc,
1579 bool is_add)
Vasu Dev38e00432014-08-01 13:27:03 -07001580#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001581{
1582 struct i40e_pf *pf = vsi->back;
1583 u16 sections = 0;
1584 u8 netdev_tc = 0;
1585 u16 numtc = 0;
1586 u16 qcount;
1587 u8 offset;
1588 u16 qmap;
1589 int i;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001590 u16 num_tc_qps = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001591
1592 sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
1593 offset = 0;
1594
1595 if (enabled_tc && (vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
1596 /* Find numtc from enabled TC bitmap */
1597 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04001598 if (enabled_tc & BIT_ULL(i)) /* TC is enabled */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001599 numtc++;
1600 }
1601 if (!numtc) {
1602 dev_warn(&pf->pdev->dev, "DCB is enabled but no TC enabled, forcing TC0\n");
1603 numtc = 1;
1604 }
1605 } else {
1606 /* At least TC0 is enabled in case of non-DCB case */
1607 numtc = 1;
1608 }
1609
1610 vsi->tc_config.numtc = numtc;
1611 vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001612 /* Number of queues per enabled TC */
Anjali Singhai7f9ff472015-02-21 06:43:19 +00001613 /* In MFP case we can have a much lower count of MSIx
1614 * vectors available and so we need to lower the used
1615 * q count.
1616 */
Anjali Singhai Jain26cdc442015-07-10 19:36:00 -04001617 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
1618 qcount = min_t(int, vsi->alloc_queue_pairs, pf->num_lan_msix);
1619 else
1620 qcount = vsi->alloc_queue_pairs;
Anjali Singhai7f9ff472015-02-21 06:43:19 +00001621 num_tc_qps = qcount / numtc;
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04001622 num_tc_qps = min_t(int, num_tc_qps, i40e_pf_get_max_q_per_tc(pf));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001623
1624 /* Setup queue offset/count for all TCs for given VSI */
1625 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1626 /* See if the given TC is enabled for the given VSI */
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04001627 if (vsi->tc_config.enabled_tc & BIT_ULL(i)) {
1628 /* TC is enabled */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001629 int pow, num_qps;
1630
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001631 switch (vsi->type) {
1632 case I40E_VSI_MAIN:
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001633 qcount = min_t(int, pf->rss_size, num_tc_qps);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001634 break;
Vasu Dev38e00432014-08-01 13:27:03 -07001635#ifdef I40E_FCOE
1636 case I40E_VSI_FCOE:
1637 qcount = num_tc_qps;
1638 break;
1639#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001640 case I40E_VSI_FDIR:
1641 case I40E_VSI_SRIOV:
1642 case I40E_VSI_VMDQ2:
1643 default:
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001644 qcount = num_tc_qps;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001645 WARN_ON(i != 0);
1646 break;
1647 }
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001648 vsi->tc_config.tc_info[i].qoffset = offset;
1649 vsi->tc_config.tc_info[i].qcount = qcount;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001650
Shannon Nelson1e200e42015-02-27 09:15:24 +00001651 /* find the next higher power-of-2 of num queue pairs */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001652 num_qps = qcount;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001653 pow = 0;
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04001654 while (num_qps && (BIT_ULL(pow) < qcount)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001655 pow++;
1656 num_qps >>= 1;
1657 }
1658
1659 vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
1660 qmap =
1661 (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
1662 (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
1663
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001664 offset += qcount;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001665 } else {
1666 /* TC is not enabled so set the offset to
1667 * default queue and allocate one queue
1668 * for the given TC.
1669 */
1670 vsi->tc_config.tc_info[i].qoffset = 0;
1671 vsi->tc_config.tc_info[i].qcount = 1;
1672 vsi->tc_config.tc_info[i].netdev_tc = 0;
1673
1674 qmap = 0;
1675 }
1676 ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
1677 }
1678
1679 /* Set actual Tx/Rx queue pairs */
1680 vsi->num_queue_pairs = offset;
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00001681 if ((vsi->type == I40E_VSI_MAIN) && (numtc == 1)) {
1682 if (vsi->req_queue_pairs > 0)
1683 vsi->num_queue_pairs = vsi->req_queue_pairs;
Anjali Singhai Jain26cdc442015-07-10 19:36:00 -04001684 else if (pf->flags & I40E_FLAG_MSIX_ENABLED)
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00001685 vsi->num_queue_pairs = pf->num_lan_msix;
1686 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001687
1688 /* Scheduler section valid can only be set for ADD VSI */
1689 if (is_add) {
1690 sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
1691
1692 ctxt->info.up_enable_bits = enabled_tc;
1693 }
1694 if (vsi->type == I40E_VSI_SRIOV) {
1695 ctxt->info.mapping_flags |=
1696 cpu_to_le16(I40E_AQ_VSI_QUE_MAP_NONCONTIG);
1697 for (i = 0; i < vsi->num_queue_pairs; i++)
1698 ctxt->info.queue_mapping[i] =
1699 cpu_to_le16(vsi->base_queue + i);
1700 } else {
1701 ctxt->info.mapping_flags |=
1702 cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
1703 ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
1704 }
1705 ctxt->info.valid_sections |= cpu_to_le16(sections);
1706}
1707
1708/**
1709 * i40e_set_rx_mode - NDO callback to set the netdev filters
1710 * @netdev: network interface device structure
1711 **/
Vasu Dev38e00432014-08-01 13:27:03 -07001712#ifdef I40E_FCOE
1713void i40e_set_rx_mode(struct net_device *netdev)
1714#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001715static void i40e_set_rx_mode(struct net_device *netdev)
Vasu Dev38e00432014-08-01 13:27:03 -07001716#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001717{
1718 struct i40e_netdev_priv *np = netdev_priv(netdev);
1719 struct i40e_mac_filter *f, *ftmp;
1720 struct i40e_vsi *vsi = np->vsi;
1721 struct netdev_hw_addr *uca;
1722 struct netdev_hw_addr *mca;
1723 struct netdev_hw_addr *ha;
1724
Kiran Patil21659032015-09-30 14:09:03 -04001725 spin_lock_bh(&vsi->mac_filter_list_lock);
1726
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001727 /* add addr if not already in the filter list */
1728 netdev_for_each_uc_addr(uca, netdev) {
1729 if (!i40e_find_mac(vsi, uca->addr, false, true)) {
1730 if (i40e_is_vsi_in_vlan(vsi))
1731 i40e_put_mac_in_vlan(vsi, uca->addr,
1732 false, true);
1733 else
1734 i40e_add_filter(vsi, uca->addr, I40E_VLAN_ANY,
1735 false, true);
1736 }
1737 }
1738
1739 netdev_for_each_mc_addr(mca, netdev) {
1740 if (!i40e_find_mac(vsi, mca->addr, false, true)) {
1741 if (i40e_is_vsi_in_vlan(vsi))
1742 i40e_put_mac_in_vlan(vsi, mca->addr,
1743 false, true);
1744 else
1745 i40e_add_filter(vsi, mca->addr, I40E_VLAN_ANY,
1746 false, true);
1747 }
1748 }
1749
1750 /* remove filter if not in netdev list */
1751 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001752
1753 if (!f->is_netdev)
1754 continue;
1755
Shannon Nelson2f41f332015-08-26 15:14:20 -04001756 netdev_for_each_mc_addr(mca, netdev)
1757 if (ether_addr_equal(mca->addr, f->macaddr))
1758 goto bottom_of_search_loop;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001759
Shannon Nelson2f41f332015-08-26 15:14:20 -04001760 netdev_for_each_uc_addr(uca, netdev)
1761 if (ether_addr_equal(uca->addr, f->macaddr))
1762 goto bottom_of_search_loop;
1763
1764 for_each_dev_addr(netdev, ha)
1765 if (ether_addr_equal(ha->addr, f->macaddr))
1766 goto bottom_of_search_loop;
1767
1768 /* f->macaddr wasn't found in uc, mc, or ha list so delete it */
1769 i40e_del_filter(vsi, f->macaddr, I40E_VLAN_ANY, false, true);
1770
1771bottom_of_search_loop:
1772 continue;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001773 }
Kiran Patil21659032015-09-30 14:09:03 -04001774 spin_unlock_bh(&vsi->mac_filter_list_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001775
1776 /* check for other flag changes */
1777 if (vsi->current_netdev_flags != vsi->netdev->flags) {
1778 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1779 vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
1780 }
1781}
1782
1783/**
Kiran Patil21659032015-09-30 14:09:03 -04001784 * i40e_mac_filter_entry_clone - Clones a MAC filter entry
1785 * @src: source MAC filter entry to be clones
1786 *
1787 * Returns the pointer to newly cloned MAC filter entry or NULL
1788 * in case of error
1789 **/
1790static struct i40e_mac_filter *i40e_mac_filter_entry_clone(
1791 struct i40e_mac_filter *src)
1792{
1793 struct i40e_mac_filter *f;
1794
1795 f = kzalloc(sizeof(*f), GFP_ATOMIC);
1796 if (!f)
1797 return NULL;
1798 *f = *src;
1799
1800 INIT_LIST_HEAD(&f->list);
1801
1802 return f;
1803}
1804
1805/**
1806 * i40e_undo_del_filter_entries - Undo the changes made to MAC filter entries
1807 * @vsi: pointer to vsi struct
1808 * @from: Pointer to list which contains MAC filter entries - changes to
1809 * those entries needs to be undone.
1810 *
1811 * MAC filter entries from list were slated to be removed from device.
1812 **/
1813static void i40e_undo_del_filter_entries(struct i40e_vsi *vsi,
1814 struct list_head *from)
1815{
1816 struct i40e_mac_filter *f, *ftmp;
1817
1818 list_for_each_entry_safe(f, ftmp, from, list) {
1819 f->changed = true;
1820 /* Move the element back into MAC filter list*/
1821 list_move_tail(&f->list, &vsi->mac_filter_list);
1822 }
1823}
1824
1825/**
1826 * i40e_undo_add_filter_entries - Undo the changes made to MAC filter entries
1827 * @vsi: pointer to vsi struct
1828 *
1829 * MAC filter entries from list were slated to be added from device.
1830 **/
1831static void i40e_undo_add_filter_entries(struct i40e_vsi *vsi)
1832{
1833 struct i40e_mac_filter *f, *ftmp;
1834
1835 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list) {
1836 if (!f->changed && f->counter)
1837 f->changed = true;
1838 }
1839}
1840
1841/**
1842 * i40e_cleanup_add_list - Deletes the element from add list and release
1843 * memory
1844 * @add_list: Pointer to list which contains MAC filter entries
1845 **/
1846static void i40e_cleanup_add_list(struct list_head *add_list)
1847{
1848 struct i40e_mac_filter *f, *ftmp;
1849
1850 list_for_each_entry_safe(f, ftmp, add_list, list) {
1851 list_del(&f->list);
1852 kfree(f);
1853 }
1854}
1855
1856/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001857 * i40e_sync_vsi_filters - Update the VSI filter list to the HW
1858 * @vsi: ptr to the VSI
Anjali Singhai30e25612015-09-28 13:37:12 -07001859 * @grab_rtnl: whether RTNL needs to be grabbed
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001860 *
1861 * Push any outstanding VSI filter changes through the AdminQ.
1862 *
1863 * Returns 0 or error value
1864 **/
Anjali Singhai30e25612015-09-28 13:37:12 -07001865int i40e_sync_vsi_filters(struct i40e_vsi *vsi, bool grab_rtnl)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001866{
Kiran Patil21659032015-09-30 14:09:03 -04001867 struct list_head tmp_del_list, tmp_add_list;
1868 struct i40e_mac_filter *f, *ftmp, *fclone;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001869 bool promisc_forced_on = false;
1870 bool add_happened = false;
1871 int filter_list_len = 0;
1872 u32 changed_flags = 0;
Kiran Patil21659032015-09-30 14:09:03 -04001873 bool err_cond = false;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04001874 i40e_status ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001875 struct i40e_pf *pf;
1876 int num_add = 0;
1877 int num_del = 0;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04001878 int aq_err = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001879 u16 cmd_flags;
1880
1881 /* empty array typed pointers, kcalloc later */
1882 struct i40e_aqc_add_macvlan_element_data *add_list;
1883 struct i40e_aqc_remove_macvlan_element_data *del_list;
1884
1885 while (test_and_set_bit(__I40E_CONFIG_BUSY, &vsi->state))
1886 usleep_range(1000, 2000);
1887 pf = vsi->back;
1888
1889 if (vsi->netdev) {
1890 changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
1891 vsi->current_netdev_flags = vsi->netdev->flags;
1892 }
1893
Kiran Patil21659032015-09-30 14:09:03 -04001894 INIT_LIST_HEAD(&tmp_del_list);
1895 INIT_LIST_HEAD(&tmp_add_list);
1896
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001897 if (vsi->flags & I40E_VSI_FLAG_FILTER_CHANGED) {
1898 vsi->flags &= ~I40E_VSI_FLAG_FILTER_CHANGED;
1899
Kiran Patil21659032015-09-30 14:09:03 -04001900 spin_lock_bh(&vsi->mac_filter_list_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001901 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list) {
1902 if (!f->changed)
1903 continue;
1904
1905 if (f->counter != 0)
1906 continue;
1907 f->changed = false;
Kiran Patil21659032015-09-30 14:09:03 -04001908
1909 /* Move the element into temporary del_list */
1910 list_move_tail(&f->list, &tmp_del_list);
1911 }
1912
1913 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list) {
1914 if (!f->changed)
1915 continue;
1916
1917 if (f->counter == 0)
1918 continue;
1919 f->changed = false;
1920
1921 /* Clone MAC filter entry and add into temporary list */
1922 fclone = i40e_mac_filter_entry_clone(f);
1923 if (!fclone) {
1924 err_cond = true;
1925 break;
1926 }
1927 list_add_tail(&fclone->list, &tmp_add_list);
1928 }
1929
1930 /* if failed to clone MAC filter entry - undo */
1931 if (err_cond) {
1932 i40e_undo_del_filter_entries(vsi, &tmp_del_list);
1933 i40e_undo_add_filter_entries(vsi);
1934 }
1935 spin_unlock_bh(&vsi->mac_filter_list_lock);
1936
1937 if (err_cond)
1938 i40e_cleanup_add_list(&tmp_add_list);
1939 }
1940
1941 /* Now process 'del_list' outside the lock */
1942 if (!list_empty(&tmp_del_list)) {
1943 filter_list_len = pf->hw.aq.asq_buf_size /
1944 sizeof(struct i40e_aqc_remove_macvlan_element_data);
1945 del_list = kcalloc(filter_list_len,
1946 sizeof(struct i40e_aqc_remove_macvlan_element_data),
1947 GFP_KERNEL);
1948 if (!del_list) {
1949 i40e_cleanup_add_list(&tmp_add_list);
1950
1951 /* Undo VSI's MAC filter entry element updates */
1952 spin_lock_bh(&vsi->mac_filter_list_lock);
1953 i40e_undo_del_filter_entries(vsi, &tmp_del_list);
1954 i40e_undo_add_filter_entries(vsi);
1955 spin_unlock_bh(&vsi->mac_filter_list_lock);
1956 return -ENOMEM;
1957 }
1958
1959 list_for_each_entry_safe(f, ftmp, &tmp_del_list, list) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001960 cmd_flags = 0;
1961
1962 /* add to delete list */
Greg Rose9a173902014-05-22 06:32:02 +00001963 ether_addr_copy(del_list[num_del].mac_addr, f->macaddr);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001964 del_list[num_del].vlan_tag =
1965 cpu_to_le16((u16)(f->vlan ==
1966 I40E_VLAN_ANY ? 0 : f->vlan));
1967
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001968 cmd_flags |= I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
1969 del_list[num_del].flags = cmd_flags;
1970 num_del++;
1971
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001972 /* flush a full buffer */
1973 if (num_del == filter_list_len) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04001974 ret = i40e_aq_remove_macvlan(&pf->hw,
1975 vsi->seid, del_list, num_del,
1976 NULL);
1977 aq_err = pf->hw.aq.asq_last_status;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001978 num_del = 0;
1979 memset(del_list, 0, sizeof(*del_list));
1980
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04001981 if (ret && aq_err != I40E_AQ_RC_ENOENT)
Kiran Patil21659032015-09-30 14:09:03 -04001982 dev_err(&pf->pdev->dev,
1983 "ignoring delete macvlan error, err %s, aq_err %s while flushing a full buffer\n",
1984 i40e_stat_str(&pf->hw, ret),
1985 i40e_aq_str(&pf->hw, aq_err));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001986 }
Kiran Patil21659032015-09-30 14:09:03 -04001987 /* Release memory for MAC filter entries which were
1988 * synced up with HW.
1989 */
1990 list_del(&f->list);
1991 kfree(f);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001992 }
Kiran Patil21659032015-09-30 14:09:03 -04001993
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001994 if (num_del) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04001995 ret = i40e_aq_remove_macvlan(&pf->hw, vsi->seid,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001996 del_list, num_del, NULL);
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04001997 aq_err = pf->hw.aq.asq_last_status;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001998 num_del = 0;
1999
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002000 if (ret && aq_err != I40E_AQ_RC_ENOENT)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002001 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002002 "ignoring delete macvlan error, err %s aq_err %s\n",
2003 i40e_stat_str(&pf->hw, ret),
2004 i40e_aq_str(&pf->hw, aq_err));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002005 }
2006
2007 kfree(del_list);
2008 del_list = NULL;
Kiran Patil21659032015-09-30 14:09:03 -04002009 }
2010
2011 if (!list_empty(&tmp_add_list)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002012
2013 /* do all the adds now */
2014 filter_list_len = pf->hw.aq.asq_buf_size /
2015 sizeof(struct i40e_aqc_add_macvlan_element_data),
2016 add_list = kcalloc(filter_list_len,
2017 sizeof(struct i40e_aqc_add_macvlan_element_data),
2018 GFP_KERNEL);
Kiran Patil21659032015-09-30 14:09:03 -04002019 if (!add_list) {
2020 /* Purge element from temporary lists */
2021 i40e_cleanup_add_list(&tmp_add_list);
2022
2023 /* Undo add filter entries from VSI MAC filter list */
2024 spin_lock_bh(&vsi->mac_filter_list_lock);
2025 i40e_undo_add_filter_entries(vsi);
2026 spin_unlock_bh(&vsi->mac_filter_list_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002027 return -ENOMEM;
Kiran Patil21659032015-09-30 14:09:03 -04002028 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002029
Kiran Patil21659032015-09-30 14:09:03 -04002030 list_for_each_entry_safe(f, ftmp, &tmp_add_list, list) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002031
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002032 add_happened = true;
2033 cmd_flags = 0;
2034
2035 /* add to add array */
Greg Rose9a173902014-05-22 06:32:02 +00002036 ether_addr_copy(add_list[num_add].mac_addr, f->macaddr);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002037 add_list[num_add].vlan_tag =
2038 cpu_to_le16(
2039 (u16)(f->vlan == I40E_VLAN_ANY ? 0 : f->vlan));
2040 add_list[num_add].queue_number = 0;
2041
2042 cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002043 add_list[num_add].flags = cpu_to_le16(cmd_flags);
2044 num_add++;
2045
2046 /* flush a full buffer */
2047 if (num_add == filter_list_len) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002048 ret = i40e_aq_add_macvlan(&pf->hw, vsi->seid,
2049 add_list, num_add,
2050 NULL);
2051 aq_err = pf->hw.aq.asq_last_status;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002052 num_add = 0;
2053
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002054 if (ret)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002055 break;
2056 memset(add_list, 0, sizeof(*add_list));
2057 }
Kiran Patil21659032015-09-30 14:09:03 -04002058 /* Entries from tmp_add_list were cloned from MAC
2059 * filter list, hence clean those cloned entries
2060 */
2061 list_del(&f->list);
2062 kfree(f);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002063 }
Kiran Patil21659032015-09-30 14:09:03 -04002064
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002065 if (num_add) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002066 ret = i40e_aq_add_macvlan(&pf->hw, vsi->seid,
2067 add_list, num_add, NULL);
2068 aq_err = pf->hw.aq.asq_last_status;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002069 num_add = 0;
2070 }
2071 kfree(add_list);
2072 add_list = NULL;
2073
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002074 if (add_happened && ret && aq_err != I40E_AQ_RC_EINVAL) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002075 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002076 "add filter failed, err %s aq_err %s\n",
2077 i40e_stat_str(&pf->hw, ret),
2078 i40e_aq_str(&pf->hw, aq_err));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002079 if ((pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOSPC) &&
2080 !test_bit(__I40E_FILTER_OVERFLOW_PROMISC,
2081 &vsi->state)) {
2082 promisc_forced_on = true;
2083 set_bit(__I40E_FILTER_OVERFLOW_PROMISC,
2084 &vsi->state);
2085 dev_info(&pf->pdev->dev, "promiscuous mode forced on\n");
2086 }
2087 }
2088 }
2089
2090 /* check for changes in promiscuous modes */
2091 if (changed_flags & IFF_ALLMULTI) {
2092 bool cur_multipromisc;
Jesse Brandeburg6995b362015-08-28 17:55:54 -04002093
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002094 cur_multipromisc = !!(vsi->current_netdev_flags & IFF_ALLMULTI);
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002095 ret = i40e_aq_set_vsi_multicast_promiscuous(&vsi->back->hw,
2096 vsi->seid,
2097 cur_multipromisc,
2098 NULL);
2099 if (ret)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002100 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002101 "set multi promisc failed, err %s aq_err %s\n",
2102 i40e_stat_str(&pf->hw, ret),
2103 i40e_aq_str(&pf->hw,
2104 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002105 }
2106 if ((changed_flags & IFF_PROMISC) || promisc_forced_on) {
2107 bool cur_promisc;
Jesse Brandeburg6995b362015-08-28 17:55:54 -04002108
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002109 cur_promisc = (!!(vsi->current_netdev_flags & IFF_PROMISC) ||
2110 test_bit(__I40E_FILTER_OVERFLOW_PROMISC,
2111 &vsi->state));
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002112 if (vsi->type == I40E_VSI_MAIN && pf->lan_veb != I40E_NO_VEB) {
2113 /* set defport ON for Main VSI instead of true promisc
2114 * this way we will get all unicast/multicast and VLAN
2115 * promisc behavior but will not get VF or VMDq traffic
2116 * replicated on the Main VSI.
2117 */
2118 if (pf->cur_promisc != cur_promisc) {
2119 pf->cur_promisc = cur_promisc;
Anjali Singhai30e25612015-09-28 13:37:12 -07002120 if (grab_rtnl)
2121 i40e_do_reset_safe(pf,
2122 BIT(__I40E_PF_RESET_REQUESTED));
2123 else
2124 i40e_do_reset(pf,
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002125 BIT(__I40E_PF_RESET_REQUESTED));
2126 }
2127 } else {
2128 ret = i40e_aq_set_vsi_unicast_promiscuous(
2129 &vsi->back->hw,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002130 vsi->seid,
2131 cur_promisc, NULL);
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002132 if (ret)
2133 dev_info(&pf->pdev->dev,
2134 "set unicast promisc failed, err %d, aq_err %d\n",
2135 ret, pf->hw.aq.asq_last_status);
2136 ret = i40e_aq_set_vsi_multicast_promiscuous(
2137 &vsi->back->hw,
2138 vsi->seid,
2139 cur_promisc, NULL);
2140 if (ret)
2141 dev_info(&pf->pdev->dev,
2142 "set multicast promisc failed, err %d, aq_err %d\n",
2143 ret, pf->hw.aq.asq_last_status);
2144 }
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002145 ret = i40e_aq_set_vsi_broadcast(&vsi->back->hw,
2146 vsi->seid,
2147 cur_promisc, NULL);
2148 if (ret)
Greg Rose1a103702013-11-28 06:42:39 +00002149 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002150 "set brdcast promisc failed, err %s, aq_err %s\n",
2151 i40e_stat_str(&pf->hw, ret),
2152 i40e_aq_str(&pf->hw,
2153 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002154 }
2155
2156 clear_bit(__I40E_CONFIG_BUSY, &vsi->state);
2157 return 0;
2158}
2159
2160/**
2161 * i40e_sync_filters_subtask - Sync the VSI filter list with HW
2162 * @pf: board private structure
2163 **/
2164static void i40e_sync_filters_subtask(struct i40e_pf *pf)
2165{
2166 int v;
2167
2168 if (!pf || !(pf->flags & I40E_FLAG_FILTER_SYNC))
2169 return;
2170 pf->flags &= ~I40E_FLAG_FILTER_SYNC;
2171
Mitch Williams505682c2014-05-20 08:01:37 +00002172 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002173 if (pf->vsi[v] &&
2174 (pf->vsi[v]->flags & I40E_VSI_FLAG_FILTER_CHANGED))
Anjali Singhai30e25612015-09-28 13:37:12 -07002175 i40e_sync_vsi_filters(pf->vsi[v], true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002176 }
2177}
2178
2179/**
2180 * i40e_change_mtu - NDO callback to change the Maximum Transfer Unit
2181 * @netdev: network interface device structure
2182 * @new_mtu: new value for maximum frame size
2183 *
2184 * Returns 0 on success, negative on failure
2185 **/
2186static int i40e_change_mtu(struct net_device *netdev, int new_mtu)
2187{
2188 struct i40e_netdev_priv *np = netdev_priv(netdev);
Jesse Brandeburg61a46a42014-04-23 04:50:05 +00002189 int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002190 struct i40e_vsi *vsi = np->vsi;
2191
2192 /* MTU < 68 is an error and causes problems on some kernels */
2193 if ((new_mtu < 68) || (max_frame > I40E_MAX_RXBUFFER))
2194 return -EINVAL;
2195
2196 netdev_info(netdev, "changing MTU from %d to %d\n",
2197 netdev->mtu, new_mtu);
2198 netdev->mtu = new_mtu;
2199 if (netif_running(netdev))
2200 i40e_vsi_reinit_locked(vsi);
2201
2202 return 0;
2203}
2204
2205/**
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00002206 * i40e_ioctl - Access the hwtstamp interface
2207 * @netdev: network interface device structure
2208 * @ifr: interface request data
2209 * @cmd: ioctl command
2210 **/
2211int i40e_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
2212{
2213 struct i40e_netdev_priv *np = netdev_priv(netdev);
2214 struct i40e_pf *pf = np->vsi->back;
2215
2216 switch (cmd) {
2217 case SIOCGHWTSTAMP:
2218 return i40e_ptp_get_ts_config(pf, ifr);
2219 case SIOCSHWTSTAMP:
2220 return i40e_ptp_set_ts_config(pf, ifr);
2221 default:
2222 return -EOPNOTSUPP;
2223 }
2224}
2225
2226/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002227 * i40e_vlan_stripping_enable - Turn on vlan stripping for the VSI
2228 * @vsi: the vsi being adjusted
2229 **/
2230void i40e_vlan_stripping_enable(struct i40e_vsi *vsi)
2231{
2232 struct i40e_vsi_context ctxt;
2233 i40e_status ret;
2234
2235 if ((vsi->info.valid_sections &
2236 cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
2237 ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_MODE_MASK) == 0))
2238 return; /* already enabled */
2239
2240 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2241 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
2242 I40E_AQ_VSI_PVLAN_EMOD_STR_BOTH;
2243
2244 ctxt.seid = vsi->seid;
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07002245 ctxt.info = vsi->info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002246 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2247 if (ret) {
2248 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002249 "update vlan stripping failed, err %s aq_err %s\n",
2250 i40e_stat_str(&vsi->back->hw, ret),
2251 i40e_aq_str(&vsi->back->hw,
2252 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002253 }
2254}
2255
2256/**
2257 * i40e_vlan_stripping_disable - Turn off vlan stripping for the VSI
2258 * @vsi: the vsi being adjusted
2259 **/
2260void i40e_vlan_stripping_disable(struct i40e_vsi *vsi)
2261{
2262 struct i40e_vsi_context ctxt;
2263 i40e_status ret;
2264
2265 if ((vsi->info.valid_sections &
2266 cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
2267 ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_EMOD_MASK) ==
2268 I40E_AQ_VSI_PVLAN_EMOD_MASK))
2269 return; /* already disabled */
2270
2271 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2272 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
2273 I40E_AQ_VSI_PVLAN_EMOD_NOTHING;
2274
2275 ctxt.seid = vsi->seid;
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07002276 ctxt.info = vsi->info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002277 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2278 if (ret) {
2279 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002280 "update vlan stripping failed, err %s aq_err %s\n",
2281 i40e_stat_str(&vsi->back->hw, ret),
2282 i40e_aq_str(&vsi->back->hw,
2283 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002284 }
2285}
2286
2287/**
2288 * i40e_vlan_rx_register - Setup or shutdown vlan offload
2289 * @netdev: network interface to be adjusted
2290 * @features: netdev features to test if VLAN offload is enabled or not
2291 **/
2292static void i40e_vlan_rx_register(struct net_device *netdev, u32 features)
2293{
2294 struct i40e_netdev_priv *np = netdev_priv(netdev);
2295 struct i40e_vsi *vsi = np->vsi;
2296
2297 if (features & NETIF_F_HW_VLAN_CTAG_RX)
2298 i40e_vlan_stripping_enable(vsi);
2299 else
2300 i40e_vlan_stripping_disable(vsi);
2301}
2302
2303/**
2304 * i40e_vsi_add_vlan - Add vsi membership for given vlan
2305 * @vsi: the vsi being configured
2306 * @vid: vlan id to be added (0 = untagged only , -1 = any)
2307 **/
2308int i40e_vsi_add_vlan(struct i40e_vsi *vsi, s16 vid)
2309{
2310 struct i40e_mac_filter *f, *add_f;
2311 bool is_netdev, is_vf;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002312
2313 is_vf = (vsi->type == I40E_VSI_SRIOV);
2314 is_netdev = !!(vsi->netdev);
2315
Kiran Patil21659032015-09-30 14:09:03 -04002316 /* Locked once because all functions invoked below iterates list*/
2317 spin_lock_bh(&vsi->mac_filter_list_lock);
2318
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002319 if (is_netdev) {
2320 add_f = i40e_add_filter(vsi, vsi->netdev->dev_addr, vid,
2321 is_vf, is_netdev);
2322 if (!add_f) {
2323 dev_info(&vsi->back->pdev->dev,
2324 "Could not add vlan filter %d for %pM\n",
2325 vid, vsi->netdev->dev_addr);
Kiran Patil21659032015-09-30 14:09:03 -04002326 spin_unlock_bh(&vsi->mac_filter_list_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002327 return -ENOMEM;
2328 }
2329 }
2330
2331 list_for_each_entry(f, &vsi->mac_filter_list, list) {
2332 add_f = i40e_add_filter(vsi, f->macaddr, vid, is_vf, is_netdev);
2333 if (!add_f) {
2334 dev_info(&vsi->back->pdev->dev,
2335 "Could not add vlan filter %d for %pM\n",
2336 vid, f->macaddr);
Kiran Patil21659032015-09-30 14:09:03 -04002337 spin_unlock_bh(&vsi->mac_filter_list_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002338 return -ENOMEM;
2339 }
2340 }
2341
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002342 /* Now if we add a vlan tag, make sure to check if it is the first
2343 * tag (i.e. a "tag" -1 does exist) and if so replace the -1 "tag"
2344 * with 0, so we now accept untagged and specified tagged traffic
2345 * (and not any taged and untagged)
2346 */
2347 if (vid > 0) {
2348 if (is_netdev && i40e_find_filter(vsi, vsi->netdev->dev_addr,
2349 I40E_VLAN_ANY,
2350 is_vf, is_netdev)) {
2351 i40e_del_filter(vsi, vsi->netdev->dev_addr,
2352 I40E_VLAN_ANY, is_vf, is_netdev);
2353 add_f = i40e_add_filter(vsi, vsi->netdev->dev_addr, 0,
2354 is_vf, is_netdev);
2355 if (!add_f) {
2356 dev_info(&vsi->back->pdev->dev,
2357 "Could not add filter 0 for %pM\n",
2358 vsi->netdev->dev_addr);
Kiran Patil21659032015-09-30 14:09:03 -04002359 spin_unlock_bh(&vsi->mac_filter_list_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002360 return -ENOMEM;
2361 }
2362 }
Greg Rose8d82a7c2014-01-13 16:13:04 -08002363 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002364
Greg Rose8d82a7c2014-01-13 16:13:04 -08002365 /* Do not assume that I40E_VLAN_ANY should be reset to VLAN 0 */
2366 if (vid > 0 && !vsi->info.pvid) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002367 list_for_each_entry(f, &vsi->mac_filter_list, list) {
Kiran Patil21659032015-09-30 14:09:03 -04002368 if (!i40e_find_filter(vsi, f->macaddr, I40E_VLAN_ANY,
2369 is_vf, is_netdev))
2370 continue;
2371 i40e_del_filter(vsi, f->macaddr, I40E_VLAN_ANY,
2372 is_vf, is_netdev);
2373 add_f = i40e_add_filter(vsi, f->macaddr,
2374 0, is_vf, is_netdev);
2375 if (!add_f) {
2376 dev_info(&vsi->back->pdev->dev,
2377 "Could not add filter 0 for %pM\n",
2378 f->macaddr);
2379 spin_unlock_bh(&vsi->mac_filter_list_lock);
2380 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002381 }
2382 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002383 }
2384
Kiran Patil21659032015-09-30 14:09:03 -04002385 /* Make sure to release before sync_vsi_filter because that
2386 * function will lock/unlock as necessary
2387 */
2388 spin_unlock_bh(&vsi->mac_filter_list_lock);
2389
Anjali Singhai Jain80f64282013-11-28 06:39:47 +00002390 if (test_bit(__I40E_DOWN, &vsi->back->state) ||
2391 test_bit(__I40E_RESET_RECOVERY_PENDING, &vsi->back->state))
2392 return 0;
2393
Anjali Singhai30e25612015-09-28 13:37:12 -07002394 return i40e_sync_vsi_filters(vsi, false);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002395}
2396
2397/**
2398 * i40e_vsi_kill_vlan - Remove vsi membership for given vlan
2399 * @vsi: the vsi being configured
2400 * @vid: vlan id to be removed (0 = untagged only , -1 = any)
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002401 *
2402 * Return: 0 on success or negative otherwise
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002403 **/
2404int i40e_vsi_kill_vlan(struct i40e_vsi *vsi, s16 vid)
2405{
2406 struct net_device *netdev = vsi->netdev;
2407 struct i40e_mac_filter *f, *add_f;
2408 bool is_vf, is_netdev;
2409 int filter_count = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002410
2411 is_vf = (vsi->type == I40E_VSI_SRIOV);
2412 is_netdev = !!(netdev);
2413
Kiran Patil21659032015-09-30 14:09:03 -04002414 /* Locked once because all functions invoked below iterates list */
2415 spin_lock_bh(&vsi->mac_filter_list_lock);
2416
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002417 if (is_netdev)
2418 i40e_del_filter(vsi, netdev->dev_addr, vid, is_vf, is_netdev);
2419
2420 list_for_each_entry(f, &vsi->mac_filter_list, list)
2421 i40e_del_filter(vsi, f->macaddr, vid, is_vf, is_netdev);
2422
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002423 /* go through all the filters for this VSI and if there is only
2424 * vid == 0 it means there are no other filters, so vid 0 must
2425 * be replaced with -1. This signifies that we should from now
2426 * on accept any traffic (with any tag present, or untagged)
2427 */
2428 list_for_each_entry(f, &vsi->mac_filter_list, list) {
2429 if (is_netdev) {
2430 if (f->vlan &&
2431 ether_addr_equal(netdev->dev_addr, f->macaddr))
2432 filter_count++;
2433 }
2434
2435 if (f->vlan)
2436 filter_count++;
2437 }
2438
2439 if (!filter_count && is_netdev) {
2440 i40e_del_filter(vsi, netdev->dev_addr, 0, is_vf, is_netdev);
2441 f = i40e_add_filter(vsi, netdev->dev_addr, I40E_VLAN_ANY,
2442 is_vf, is_netdev);
2443 if (!f) {
2444 dev_info(&vsi->back->pdev->dev,
2445 "Could not add filter %d for %pM\n",
2446 I40E_VLAN_ANY, netdev->dev_addr);
Kiran Patil21659032015-09-30 14:09:03 -04002447 spin_unlock_bh(&vsi->mac_filter_list_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002448 return -ENOMEM;
2449 }
2450 }
2451
2452 if (!filter_count) {
2453 list_for_each_entry(f, &vsi->mac_filter_list, list) {
2454 i40e_del_filter(vsi, f->macaddr, 0, is_vf, is_netdev);
2455 add_f = i40e_add_filter(vsi, f->macaddr, I40E_VLAN_ANY,
Kiran Patil21659032015-09-30 14:09:03 -04002456 is_vf, is_netdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002457 if (!add_f) {
2458 dev_info(&vsi->back->pdev->dev,
2459 "Could not add filter %d for %pM\n",
2460 I40E_VLAN_ANY, f->macaddr);
Kiran Patil21659032015-09-30 14:09:03 -04002461 spin_unlock_bh(&vsi->mac_filter_list_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002462 return -ENOMEM;
2463 }
2464 }
2465 }
2466
Kiran Patil21659032015-09-30 14:09:03 -04002467 /* Make sure to release before sync_vsi_filter because that
2468 * function with lock/unlock as necessary
2469 */
2470 spin_unlock_bh(&vsi->mac_filter_list_lock);
2471
Anjali Singhai Jain80f64282013-11-28 06:39:47 +00002472 if (test_bit(__I40E_DOWN, &vsi->back->state) ||
2473 test_bit(__I40E_RESET_RECOVERY_PENDING, &vsi->back->state))
2474 return 0;
2475
Anjali Singhai30e25612015-09-28 13:37:12 -07002476 return i40e_sync_vsi_filters(vsi, false);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002477}
2478
2479/**
2480 * i40e_vlan_rx_add_vid - Add a vlan id filter to HW offload
2481 * @netdev: network interface to be adjusted
2482 * @vid: vlan id to be added
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002483 *
2484 * net_device_ops implementation for adding vlan ids
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002485 **/
Vasu Dev38e00432014-08-01 13:27:03 -07002486#ifdef I40E_FCOE
2487int i40e_vlan_rx_add_vid(struct net_device *netdev,
2488 __always_unused __be16 proto, u16 vid)
2489#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002490static int i40e_vlan_rx_add_vid(struct net_device *netdev,
2491 __always_unused __be16 proto, u16 vid)
Vasu Dev38e00432014-08-01 13:27:03 -07002492#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002493{
2494 struct i40e_netdev_priv *np = netdev_priv(netdev);
2495 struct i40e_vsi *vsi = np->vsi;
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002496 int ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002497
2498 if (vid > 4095)
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002499 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002500
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002501 netdev_info(netdev, "adding %pM vid=%d\n", netdev->dev_addr, vid);
2502
Anjali Singhai Jain6982d422014-02-06 05:51:10 +00002503 /* If the network stack called us with vid = 0 then
2504 * it is asking to receive priority tagged packets with
2505 * vlan id 0. Our HW receives them by default when configured
2506 * to receive untagged packets so there is no need to add an
2507 * extra filter for vlan 0 tagged packets.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002508 */
Anjali Singhai Jain6982d422014-02-06 05:51:10 +00002509 if (vid)
2510 ret = i40e_vsi_add_vlan(vsi, vid);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002511
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002512 if (!ret && (vid < VLAN_N_VID))
2513 set_bit(vid, vsi->active_vlans);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002514
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002515 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002516}
2517
2518/**
2519 * i40e_vlan_rx_kill_vid - Remove a vlan id filter from HW offload
2520 * @netdev: network interface to be adjusted
2521 * @vid: vlan id to be removed
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002522 *
Akeem G Abodunrinfdfd9432014-02-11 08:24:15 +00002523 * net_device_ops implementation for removing vlan ids
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002524 **/
Vasu Dev38e00432014-08-01 13:27:03 -07002525#ifdef I40E_FCOE
2526int i40e_vlan_rx_kill_vid(struct net_device *netdev,
2527 __always_unused __be16 proto, u16 vid)
2528#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002529static int i40e_vlan_rx_kill_vid(struct net_device *netdev,
2530 __always_unused __be16 proto, u16 vid)
Vasu Dev38e00432014-08-01 13:27:03 -07002531#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002532{
2533 struct i40e_netdev_priv *np = netdev_priv(netdev);
2534 struct i40e_vsi *vsi = np->vsi;
2535
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002536 netdev_info(netdev, "removing %pM vid=%d\n", netdev->dev_addr, vid);
2537
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002538 /* return code is ignored as there is nothing a user
2539 * can do about failure to remove and a log message was
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002540 * already printed from the other function
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002541 */
2542 i40e_vsi_kill_vlan(vsi, vid);
2543
2544 clear_bit(vid, vsi->active_vlans);
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002545
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002546 return 0;
2547}
2548
2549/**
2550 * i40e_restore_vlan - Reinstate vlans when vsi/netdev comes back up
2551 * @vsi: the vsi being brought back up
2552 **/
2553static void i40e_restore_vlan(struct i40e_vsi *vsi)
2554{
2555 u16 vid;
2556
2557 if (!vsi->netdev)
2558 return;
2559
2560 i40e_vlan_rx_register(vsi->netdev, vsi->netdev->features);
2561
2562 for_each_set_bit(vid, vsi->active_vlans, VLAN_N_VID)
2563 i40e_vlan_rx_add_vid(vsi->netdev, htons(ETH_P_8021Q),
2564 vid);
2565}
2566
2567/**
2568 * i40e_vsi_add_pvid - Add pvid for the VSI
2569 * @vsi: the vsi being adjusted
2570 * @vid: the vlan id to set as a PVID
2571 **/
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002572int i40e_vsi_add_pvid(struct i40e_vsi *vsi, u16 vid)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002573{
2574 struct i40e_vsi_context ctxt;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002575 i40e_status ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002576
2577 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2578 vsi->info.pvid = cpu_to_le16(vid);
Greg Rose6c12fcb2013-11-28 06:39:34 +00002579 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_TAGGED |
2580 I40E_AQ_VSI_PVLAN_INSERT_PVID |
Greg Roseb774c7d2013-11-28 06:39:44 +00002581 I40E_AQ_VSI_PVLAN_EMOD_STR;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002582
2583 ctxt.seid = vsi->seid;
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07002584 ctxt.info = vsi->info;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002585 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2586 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002587 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002588 "add pvid failed, err %s aq_err %s\n",
2589 i40e_stat_str(&vsi->back->hw, ret),
2590 i40e_aq_str(&vsi->back->hw,
2591 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002592 return -ENOENT;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002593 }
2594
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002595 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002596}
2597
2598/**
2599 * i40e_vsi_remove_pvid - Remove the pvid from the VSI
2600 * @vsi: the vsi being adjusted
2601 *
2602 * Just use the vlan_rx_register() service to put it back to normal
2603 **/
2604void i40e_vsi_remove_pvid(struct i40e_vsi *vsi)
2605{
Greg Rose6c12fcb2013-11-28 06:39:34 +00002606 i40e_vlan_stripping_disable(vsi);
2607
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002608 vsi->info.pvid = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002609}
2610
2611/**
2612 * i40e_vsi_setup_tx_resources - Allocate VSI Tx queue resources
2613 * @vsi: ptr to the VSI
2614 *
2615 * If this function returns with an error, then it's possible one or
2616 * more of the rings is populated (while the rest are not). It is the
2617 * callers duty to clean those orphaned rings.
2618 *
2619 * Return 0 on success, negative on failure
2620 **/
2621static int i40e_vsi_setup_tx_resources(struct i40e_vsi *vsi)
2622{
2623 int i, err = 0;
2624
2625 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00002626 err = i40e_setup_tx_descriptors(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002627
2628 return err;
2629}
2630
2631/**
2632 * i40e_vsi_free_tx_resources - Free Tx resources for VSI queues
2633 * @vsi: ptr to the VSI
2634 *
2635 * Free VSI's transmit software resources
2636 **/
2637static void i40e_vsi_free_tx_resources(struct i40e_vsi *vsi)
2638{
2639 int i;
2640
Greg Rose8e9dca52013-12-18 13:45:53 +00002641 if (!vsi->tx_rings)
2642 return;
2643
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002644 for (i = 0; i < vsi->num_queue_pairs; i++)
Greg Rose8e9dca52013-12-18 13:45:53 +00002645 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00002646 i40e_free_tx_resources(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002647}
2648
2649/**
2650 * i40e_vsi_setup_rx_resources - Allocate VSI queues Rx resources
2651 * @vsi: ptr to the VSI
2652 *
2653 * If this function returns with an error, then it's possible one or
2654 * more of the rings is populated (while the rest are not). It is the
2655 * callers duty to clean those orphaned rings.
2656 *
2657 * Return 0 on success, negative on failure
2658 **/
2659static int i40e_vsi_setup_rx_resources(struct i40e_vsi *vsi)
2660{
2661 int i, err = 0;
2662
2663 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00002664 err = i40e_setup_rx_descriptors(vsi->rx_rings[i]);
Vasu Dev38e00432014-08-01 13:27:03 -07002665#ifdef I40E_FCOE
2666 i40e_fcoe_setup_ddp_resources(vsi);
2667#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002668 return err;
2669}
2670
2671/**
2672 * i40e_vsi_free_rx_resources - Free Rx Resources for VSI queues
2673 * @vsi: ptr to the VSI
2674 *
2675 * Free all receive software resources
2676 **/
2677static void i40e_vsi_free_rx_resources(struct i40e_vsi *vsi)
2678{
2679 int i;
2680
Greg Rose8e9dca52013-12-18 13:45:53 +00002681 if (!vsi->rx_rings)
2682 return;
2683
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002684 for (i = 0; i < vsi->num_queue_pairs; i++)
Greg Rose8e9dca52013-12-18 13:45:53 +00002685 if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00002686 i40e_free_rx_resources(vsi->rx_rings[i]);
Vasu Dev38e00432014-08-01 13:27:03 -07002687#ifdef I40E_FCOE
2688 i40e_fcoe_free_ddp_resources(vsi);
2689#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002690}
2691
2692/**
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002693 * i40e_config_xps_tx_ring - Configure XPS for a Tx ring
2694 * @ring: The Tx ring to configure
2695 *
2696 * This enables/disables XPS for a given Tx descriptor ring
2697 * based on the TCs enabled for the VSI that ring belongs to.
2698 **/
2699static void i40e_config_xps_tx_ring(struct i40e_ring *ring)
2700{
2701 struct i40e_vsi *vsi = ring->vsi;
2702 cpumask_var_t mask;
2703
Jesse Brandeburg9a660ee2015-02-26 16:13:22 +00002704 if (!ring->q_vector || !ring->netdev)
2705 return;
2706
2707 /* Single TC mode enable XPS */
2708 if (vsi->tc_config.numtc <= 1) {
2709 if (!test_and_set_bit(__I40E_TX_XPS_INIT_DONE, &ring->state))
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002710 netif_set_xps_queue(ring->netdev,
2711 &ring->q_vector->affinity_mask,
2712 ring->queue_index);
Jesse Brandeburg9a660ee2015-02-26 16:13:22 +00002713 } else if (alloc_cpumask_var(&mask, GFP_KERNEL)) {
2714 /* Disable XPS to allow selection based on TC */
2715 bitmap_zero(cpumask_bits(mask), nr_cpumask_bits);
2716 netif_set_xps_queue(ring->netdev, mask, ring->queue_index);
2717 free_cpumask_var(mask);
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002718 }
2719}
2720
2721/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002722 * i40e_configure_tx_ring - Configure a transmit ring context and rest
2723 * @ring: The Tx ring to configure
2724 *
2725 * Configure the Tx descriptor ring in the HMC context.
2726 **/
2727static int i40e_configure_tx_ring(struct i40e_ring *ring)
2728{
2729 struct i40e_vsi *vsi = ring->vsi;
2730 u16 pf_q = vsi->base_queue + ring->queue_index;
2731 struct i40e_hw *hw = &vsi->back->hw;
2732 struct i40e_hmc_obj_txq tx_ctx;
2733 i40e_status err = 0;
2734 u32 qtx_ctl = 0;
2735
2736 /* some ATR related tx ring init */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08002737 if (vsi->back->flags & I40E_FLAG_FD_ATR_ENABLED) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002738 ring->atr_sample_rate = vsi->back->atr_sample_rate;
2739 ring->atr_count = 0;
2740 } else {
2741 ring->atr_sample_rate = 0;
2742 }
2743
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002744 /* configure XPS */
2745 i40e_config_xps_tx_ring(ring);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002746
2747 /* clear the context structure first */
2748 memset(&tx_ctx, 0, sizeof(tx_ctx));
2749
2750 tx_ctx.new_context = 1;
2751 tx_ctx.base = (ring->dma / 128);
2752 tx_ctx.qlen = ring->count;
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08002753 tx_ctx.fd_ena = !!(vsi->back->flags & (I40E_FLAG_FD_SB_ENABLED |
2754 I40E_FLAG_FD_ATR_ENABLED));
Vasu Dev38e00432014-08-01 13:27:03 -07002755#ifdef I40E_FCOE
2756 tx_ctx.fc_ena = (vsi->type == I40E_VSI_FCOE);
2757#endif
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00002758 tx_ctx.timesync_ena = !!(vsi->back->flags & I40E_FLAG_PTP);
Jesse Brandeburg1943d8b2014-02-14 02:14:40 +00002759 /* FDIR VSI tx ring can still use RS bit and writebacks */
2760 if (vsi->type != I40E_VSI_FDIR)
2761 tx_ctx.head_wb_ena = 1;
2762 tx_ctx.head_wb_addr = ring->dma +
2763 (ring->count * sizeof(struct i40e_tx_desc));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002764
2765 /* As part of VSI creation/update, FW allocates certain
2766 * Tx arbitration queue sets for each TC enabled for
2767 * the VSI. The FW returns the handles to these queue
2768 * sets as part of the response buffer to Add VSI,
2769 * Update VSI, etc. AQ commands. It is expected that
2770 * these queue set handles be associated with the Tx
2771 * queues by the driver as part of the TX queue context
2772 * initialization. This has to be done regardless of
2773 * DCB as by default everything is mapped to TC0.
2774 */
2775 tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[ring->dcb_tc]);
2776 tx_ctx.rdylist_act = 0;
2777
2778 /* clear the context in the HMC */
2779 err = i40e_clear_lan_tx_queue_context(hw, pf_q);
2780 if (err) {
2781 dev_info(&vsi->back->pdev->dev,
2782 "Failed to clear LAN Tx queue context on Tx ring %d (pf_q %d), error: %d\n",
2783 ring->queue_index, pf_q, err);
2784 return -ENOMEM;
2785 }
2786
2787 /* set the context in the HMC */
2788 err = i40e_set_lan_tx_queue_context(hw, pf_q, &tx_ctx);
2789 if (err) {
2790 dev_info(&vsi->back->pdev->dev,
2791 "Failed to set LAN Tx queue context on Tx ring %d (pf_q %d, error: %d\n",
2792 ring->queue_index, pf_q, err);
2793 return -ENOMEM;
2794 }
2795
2796 /* Now associate this queue with this PCI function */
Mitch Williams7a28d882014-10-17 03:14:52 +00002797 if (vsi->type == I40E_VSI_VMDQ2) {
Shannon Nelson9d8bf542014-01-14 00:49:50 -08002798 qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
Mitch Williams7a28d882014-10-17 03:14:52 +00002799 qtx_ctl |= ((vsi->id) << I40E_QTX_CTL_VFVM_INDX_SHIFT) &
2800 I40E_QTX_CTL_VFVM_INDX_MASK;
2801 } else {
Shannon Nelson9d8bf542014-01-14 00:49:50 -08002802 qtx_ctl = I40E_QTX_CTL_PF_QUEUE;
Mitch Williams7a28d882014-10-17 03:14:52 +00002803 }
2804
Shannon Nelson13fd9772013-09-28 07:14:19 +00002805 qtx_ctl |= ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT) &
2806 I40E_QTX_CTL_PF_INDX_MASK);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002807 wr32(hw, I40E_QTX_CTL(pf_q), qtx_ctl);
2808 i40e_flush(hw);
2809
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002810 /* cache tail off for easier writes later */
2811 ring->tail = hw->hw_addr + I40E_QTX_TAIL(pf_q);
2812
2813 return 0;
2814}
2815
2816/**
2817 * i40e_configure_rx_ring - Configure a receive ring context
2818 * @ring: The Rx ring to configure
2819 *
2820 * Configure the Rx descriptor ring in the HMC context.
2821 **/
2822static int i40e_configure_rx_ring(struct i40e_ring *ring)
2823{
2824 struct i40e_vsi *vsi = ring->vsi;
2825 u32 chain_len = vsi->back->hw.func_caps.rx_buf_chain_len;
2826 u16 pf_q = vsi->base_queue + ring->queue_index;
2827 struct i40e_hw *hw = &vsi->back->hw;
2828 struct i40e_hmc_obj_rxq rx_ctx;
2829 i40e_status err = 0;
2830
2831 ring->state = 0;
2832
2833 /* clear the context structure first */
2834 memset(&rx_ctx, 0, sizeof(rx_ctx));
2835
2836 ring->rx_buf_len = vsi->rx_buf_len;
2837 ring->rx_hdr_len = vsi->rx_hdr_len;
2838
2839 rx_ctx.dbuff = ring->rx_buf_len >> I40E_RXQ_CTX_DBUFF_SHIFT;
2840 rx_ctx.hbuff = ring->rx_hdr_len >> I40E_RXQ_CTX_HBUFF_SHIFT;
2841
2842 rx_ctx.base = (ring->dma / 128);
2843 rx_ctx.qlen = ring->count;
2844
2845 if (vsi->back->flags & I40E_FLAG_16BYTE_RX_DESC_ENABLED) {
2846 set_ring_16byte_desc_enabled(ring);
2847 rx_ctx.dsize = 0;
2848 } else {
2849 rx_ctx.dsize = 1;
2850 }
2851
2852 rx_ctx.dtype = vsi->dtype;
2853 if (vsi->dtype) {
2854 set_ring_ps_enabled(ring);
2855 rx_ctx.hsplit_0 = I40E_RX_SPLIT_L2 |
2856 I40E_RX_SPLIT_IP |
2857 I40E_RX_SPLIT_TCP_UDP |
2858 I40E_RX_SPLIT_SCTP;
2859 } else {
2860 rx_ctx.hsplit_0 = 0;
2861 }
2862
2863 rx_ctx.rxmax = min_t(u16, vsi->max_frame,
2864 (chain_len * ring->rx_buf_len));
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00002865 if (hw->revision_id == 0)
2866 rx_ctx.lrxqthresh = 0;
2867 else
2868 rx_ctx.lrxqthresh = 2;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002869 rx_ctx.crcstrip = 1;
2870 rx_ctx.l2tsel = 1;
Jesse Brandeburgc4bbac32015-09-28 11:21:48 -07002871 /* this controls whether VLAN is stripped from inner headers */
2872 rx_ctx.showiv = 0;
Vasu Dev38e00432014-08-01 13:27:03 -07002873#ifdef I40E_FCOE
2874 rx_ctx.fc_ena = (vsi->type == I40E_VSI_FCOE);
2875#endif
Catherine Sullivanacb36762014-03-06 09:02:30 +00002876 /* set the prefena field to 1 because the manual says to */
2877 rx_ctx.prefena = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002878
2879 /* clear the context in the HMC */
2880 err = i40e_clear_lan_rx_queue_context(hw, pf_q);
2881 if (err) {
2882 dev_info(&vsi->back->pdev->dev,
2883 "Failed to clear LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
2884 ring->queue_index, pf_q, err);
2885 return -ENOMEM;
2886 }
2887
2888 /* set the context in the HMC */
2889 err = i40e_set_lan_rx_queue_context(hw, pf_q, &rx_ctx);
2890 if (err) {
2891 dev_info(&vsi->back->pdev->dev,
2892 "Failed to set LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
2893 ring->queue_index, pf_q, err);
2894 return -ENOMEM;
2895 }
2896
2897 /* cache tail for quicker writes, and clear the reg before use */
2898 ring->tail = hw->hw_addr + I40E_QRX_TAIL(pf_q);
2899 writel(0, ring->tail);
2900
Mitch Williamsa132af22015-01-24 09:58:35 +00002901 if (ring_is_ps_enabled(ring)) {
2902 i40e_alloc_rx_headers(ring);
2903 i40e_alloc_rx_buffers_ps(ring, I40E_DESC_UNUSED(ring));
2904 } else {
2905 i40e_alloc_rx_buffers_1buf(ring, I40E_DESC_UNUSED(ring));
2906 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002907
2908 return 0;
2909}
2910
2911/**
2912 * i40e_vsi_configure_tx - Configure the VSI for Tx
2913 * @vsi: VSI structure describing this set of rings and resources
2914 *
2915 * Configure the Tx VSI for operation.
2916 **/
2917static int i40e_vsi_configure_tx(struct i40e_vsi *vsi)
2918{
2919 int err = 0;
2920 u16 i;
2921
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00002922 for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
2923 err = i40e_configure_tx_ring(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002924
2925 return err;
2926}
2927
2928/**
2929 * i40e_vsi_configure_rx - Configure the VSI for Rx
2930 * @vsi: the VSI being configured
2931 *
2932 * Configure the Rx VSI for operation.
2933 **/
2934static int i40e_vsi_configure_rx(struct i40e_vsi *vsi)
2935{
2936 int err = 0;
2937 u16 i;
2938
2939 if (vsi->netdev && (vsi->netdev->mtu > ETH_DATA_LEN))
2940 vsi->max_frame = vsi->netdev->mtu + ETH_HLEN
2941 + ETH_FCS_LEN + VLAN_HLEN;
2942 else
2943 vsi->max_frame = I40E_RXBUFFER_2048;
2944
2945 /* figure out correct receive buffer length */
2946 switch (vsi->back->flags & (I40E_FLAG_RX_1BUF_ENABLED |
2947 I40E_FLAG_RX_PS_ENABLED)) {
2948 case I40E_FLAG_RX_1BUF_ENABLED:
2949 vsi->rx_hdr_len = 0;
2950 vsi->rx_buf_len = vsi->max_frame;
2951 vsi->dtype = I40E_RX_DTYPE_NO_SPLIT;
2952 break;
2953 case I40E_FLAG_RX_PS_ENABLED:
2954 vsi->rx_hdr_len = I40E_RX_HDR_SIZE;
2955 vsi->rx_buf_len = I40E_RXBUFFER_2048;
2956 vsi->dtype = I40E_RX_DTYPE_HEADER_SPLIT;
2957 break;
2958 default:
2959 vsi->rx_hdr_len = I40E_RX_HDR_SIZE;
2960 vsi->rx_buf_len = I40E_RXBUFFER_2048;
2961 vsi->dtype = I40E_RX_DTYPE_SPLIT_ALWAYS;
2962 break;
2963 }
2964
Vasu Dev38e00432014-08-01 13:27:03 -07002965#ifdef I40E_FCOE
2966 /* setup rx buffer for FCoE */
2967 if ((vsi->type == I40E_VSI_FCOE) &&
2968 (vsi->back->flags & I40E_FLAG_FCOE_ENABLED)) {
2969 vsi->rx_hdr_len = 0;
2970 vsi->rx_buf_len = I40E_RXBUFFER_3072;
2971 vsi->max_frame = I40E_RXBUFFER_3072;
2972 vsi->dtype = I40E_RX_DTYPE_NO_SPLIT;
2973 }
2974
2975#endif /* I40E_FCOE */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002976 /* round up for the chip's needs */
2977 vsi->rx_hdr_len = ALIGN(vsi->rx_hdr_len,
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04002978 BIT_ULL(I40E_RXQ_CTX_HBUFF_SHIFT));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002979 vsi->rx_buf_len = ALIGN(vsi->rx_buf_len,
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04002980 BIT_ULL(I40E_RXQ_CTX_DBUFF_SHIFT));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002981
2982 /* set up individual rings */
2983 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00002984 err = i40e_configure_rx_ring(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002985
2986 return err;
2987}
2988
2989/**
2990 * i40e_vsi_config_dcb_rings - Update rings to reflect DCB TC
2991 * @vsi: ptr to the VSI
2992 **/
2993static void i40e_vsi_config_dcb_rings(struct i40e_vsi *vsi)
2994{
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +00002995 struct i40e_ring *tx_ring, *rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002996 u16 qoffset, qcount;
2997 int i, n;
2998
Parikh, Neeravcd238a32015-02-21 06:43:37 +00002999 if (!(vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
3000 /* Reset the TC information */
3001 for (i = 0; i < vsi->num_queue_pairs; i++) {
3002 rx_ring = vsi->rx_rings[i];
3003 tx_ring = vsi->tx_rings[i];
3004 rx_ring->dcb_tc = 0;
3005 tx_ring->dcb_tc = 0;
3006 }
3007 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003008
3009 for (n = 0; n < I40E_MAX_TRAFFIC_CLASS; n++) {
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04003010 if (!(vsi->tc_config.enabled_tc & BIT_ULL(n)))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003011 continue;
3012
3013 qoffset = vsi->tc_config.tc_info[n].qoffset;
3014 qcount = vsi->tc_config.tc_info[n].qcount;
3015 for (i = qoffset; i < (qoffset + qcount); i++) {
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +00003016 rx_ring = vsi->rx_rings[i];
3017 tx_ring = vsi->tx_rings[i];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003018 rx_ring->dcb_tc = n;
3019 tx_ring->dcb_tc = n;
3020 }
3021 }
3022}
3023
3024/**
3025 * i40e_set_vsi_rx_mode - Call set_rx_mode on a VSI
3026 * @vsi: ptr to the VSI
3027 **/
3028static void i40e_set_vsi_rx_mode(struct i40e_vsi *vsi)
3029{
3030 if (vsi->netdev)
3031 i40e_set_rx_mode(vsi->netdev);
3032}
3033
3034/**
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00003035 * i40e_fdir_filter_restore - Restore the Sideband Flow Director filters
3036 * @vsi: Pointer to the targeted VSI
3037 *
3038 * This function replays the hlist on the hw where all the SB Flow Director
3039 * filters were saved.
3040 **/
3041static void i40e_fdir_filter_restore(struct i40e_vsi *vsi)
3042{
3043 struct i40e_fdir_filter *filter;
3044 struct i40e_pf *pf = vsi->back;
3045 struct hlist_node *node;
3046
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00003047 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
3048 return;
3049
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00003050 hlist_for_each_entry_safe(filter, node,
3051 &pf->fdir_filter_list, fdir_node) {
3052 i40e_add_del_fdir(vsi, filter, true);
3053 }
3054}
3055
3056/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003057 * i40e_vsi_configure - Set up the VSI for action
3058 * @vsi: the VSI being configured
3059 **/
3060static int i40e_vsi_configure(struct i40e_vsi *vsi)
3061{
3062 int err;
3063
3064 i40e_set_vsi_rx_mode(vsi);
3065 i40e_restore_vlan(vsi);
3066 i40e_vsi_config_dcb_rings(vsi);
3067 err = i40e_vsi_configure_tx(vsi);
3068 if (!err)
3069 err = i40e_vsi_configure_rx(vsi);
3070
3071 return err;
3072}
3073
3074/**
3075 * i40e_vsi_configure_msix - MSIX mode Interrupt Config in the HW
3076 * @vsi: the VSI being configured
3077 **/
3078static void i40e_vsi_configure_msix(struct i40e_vsi *vsi)
3079{
3080 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003081 struct i40e_hw *hw = &pf->hw;
3082 u16 vector;
3083 int i, q;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003084 u32 qp;
3085
3086 /* The interrupt indexing is offset by 1 in the PFINT_ITRn
3087 * and PFINT_LNKLSTn registers, e.g.:
3088 * PFINT_ITRn[0..n-1] gets msix-1..msix-n (qpair interrupts)
3089 */
3090 qp = vsi->base_queue;
3091 vector = vsi->base_vector;
Alexander Duyck493fb302013-09-28 07:01:44 +00003092 for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
Jesse Brandeburgac26fc12015-09-28 14:12:37 -04003093 struct i40e_q_vector *q_vector = vsi->q_vectors[i];
3094
Jesse Brandeburgee2319c2015-09-28 14:16:54 -04003095 q_vector->itr_countdown = ITR_COUNTDOWN_START;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003096 q_vector->rx.itr = ITR_TO_REG(vsi->rx_itr_setting);
3097 q_vector->rx.latency_range = I40E_LOW_LATENCY;
3098 wr32(hw, I40E_PFINT_ITRN(I40E_RX_ITR, vector - 1),
3099 q_vector->rx.itr);
3100 q_vector->tx.itr = ITR_TO_REG(vsi->tx_itr_setting);
3101 q_vector->tx.latency_range = I40E_LOW_LATENCY;
3102 wr32(hw, I40E_PFINT_ITRN(I40E_TX_ITR, vector - 1),
3103 q_vector->tx.itr);
Jesse Brandeburgac26fc12015-09-28 14:12:37 -04003104 wr32(hw, I40E_PFINT_RATEN(vector - 1),
3105 INTRL_USEC_TO_REG(vsi->int_rate_limit));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003106
3107 /* Linked list for the queuepairs assigned to this vector */
3108 wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), qp);
3109 for (q = 0; q < q_vector->num_ringpairs; q++) {
Jesse Brandeburgac26fc12015-09-28 14:12:37 -04003110 u32 val;
3111
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003112 val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3113 (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
3114 (vector << I40E_QINT_RQCTL_MSIX_INDX_SHIFT) |
3115 (qp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT)|
3116 (I40E_QUEUE_TYPE_TX
3117 << I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT);
3118
3119 wr32(hw, I40E_QINT_RQCTL(qp), val);
3120
3121 val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3122 (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3123 (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
3124 ((qp+1) << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT)|
3125 (I40E_QUEUE_TYPE_RX
3126 << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3127
3128 /* Terminate the linked list */
3129 if (q == (q_vector->num_ringpairs - 1))
3130 val |= (I40E_QUEUE_END_OF_LIST
3131 << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
3132
3133 wr32(hw, I40E_QINT_TQCTL(qp), val);
3134 qp++;
3135 }
3136 }
3137
3138 i40e_flush(hw);
3139}
3140
3141/**
3142 * i40e_enable_misc_int_causes - enable the non-queue interrupts
3143 * @hw: ptr to the hardware info
3144 **/
Jacob Kellerab437b52014-12-14 01:55:08 +00003145static void i40e_enable_misc_int_causes(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003146{
Jacob Kellerab437b52014-12-14 01:55:08 +00003147 struct i40e_hw *hw = &pf->hw;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003148 u32 val;
3149
3150 /* clear things first */
3151 wr32(hw, I40E_PFINT_ICR0_ENA, 0); /* disable all */
3152 rd32(hw, I40E_PFINT_ICR0); /* read to clear */
3153
3154 val = I40E_PFINT_ICR0_ENA_ECC_ERR_MASK |
3155 I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK |
3156 I40E_PFINT_ICR0_ENA_GRST_MASK |
3157 I40E_PFINT_ICR0_ENA_PCI_EXCEPTION_MASK |
3158 I40E_PFINT_ICR0_ENA_GPIO_MASK |
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003159 I40E_PFINT_ICR0_ENA_HMC_ERR_MASK |
3160 I40E_PFINT_ICR0_ENA_VFLR_MASK |
3161 I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
3162
Anjali Singhai Jain0d8e1432015-06-05 12:20:32 -04003163 if (pf->flags & I40E_FLAG_IWARP_ENABLED)
3164 val |= I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
3165
Jacob Kellerab437b52014-12-14 01:55:08 +00003166 if (pf->flags & I40E_FLAG_PTP)
3167 val |= I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
3168
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003169 wr32(hw, I40E_PFINT_ICR0_ENA, val);
3170
3171 /* SW_ITR_IDX = 0, but don't change INTENA */
Anjali Singhai Jain84ed40e2013-11-26 10:49:32 +00003172 wr32(hw, I40E_PFINT_DYN_CTL0, I40E_PFINT_DYN_CTL0_SW_ITR_INDX_MASK |
3173 I40E_PFINT_DYN_CTL0_INTENA_MSK_MASK);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003174
3175 /* OTHER_ITR_IDX = 0 */
3176 wr32(hw, I40E_PFINT_STAT_CTL0, 0);
3177}
3178
3179/**
3180 * i40e_configure_msi_and_legacy - Legacy mode interrupt config in the HW
3181 * @vsi: the VSI being configured
3182 **/
3183static void i40e_configure_msi_and_legacy(struct i40e_vsi *vsi)
3184{
Alexander Duyck493fb302013-09-28 07:01:44 +00003185 struct i40e_q_vector *q_vector = vsi->q_vectors[0];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003186 struct i40e_pf *pf = vsi->back;
3187 struct i40e_hw *hw = &pf->hw;
3188 u32 val;
3189
3190 /* set the ITR configuration */
Jesse Brandeburgee2319c2015-09-28 14:16:54 -04003191 q_vector->itr_countdown = ITR_COUNTDOWN_START;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003192 q_vector->rx.itr = ITR_TO_REG(vsi->rx_itr_setting);
3193 q_vector->rx.latency_range = I40E_LOW_LATENCY;
3194 wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), q_vector->rx.itr);
3195 q_vector->tx.itr = ITR_TO_REG(vsi->tx_itr_setting);
3196 q_vector->tx.latency_range = I40E_LOW_LATENCY;
3197 wr32(hw, I40E_PFINT_ITR0(I40E_TX_ITR), q_vector->tx.itr);
3198
Jacob Kellerab437b52014-12-14 01:55:08 +00003199 i40e_enable_misc_int_causes(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003200
3201 /* FIRSTQ_INDX = 0, FIRSTQ_TYPE = 0 (rx) */
3202 wr32(hw, I40E_PFINT_LNKLST0, 0);
3203
Jesse Brandeburgf29eaa32014-02-11 08:24:12 +00003204 /* Associate the queue pair to the vector and enable the queue int */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003205 val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3206 (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
3207 (I40E_QUEUE_TYPE_TX << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3208
3209 wr32(hw, I40E_QINT_RQCTL(0), val);
3210
3211 val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3212 (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3213 (I40E_QUEUE_END_OF_LIST << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
3214
3215 wr32(hw, I40E_QINT_TQCTL(0), val);
3216 i40e_flush(hw);
3217}
3218
3219/**
Mitch Williams2ef28cf2013-11-28 06:39:32 +00003220 * i40e_irq_dynamic_disable_icr0 - Disable default interrupt generation for icr0
3221 * @pf: board private structure
3222 **/
3223void i40e_irq_dynamic_disable_icr0(struct i40e_pf *pf)
3224{
3225 struct i40e_hw *hw = &pf->hw;
3226
3227 wr32(hw, I40E_PFINT_DYN_CTL0,
3228 I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
3229 i40e_flush(hw);
3230}
3231
3232/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003233 * i40e_irq_dynamic_enable_icr0 - Enable default interrupt generation for icr0
3234 * @pf: board private structure
3235 **/
Shannon Nelson116a57d2013-09-28 07:13:59 +00003236void i40e_irq_dynamic_enable_icr0(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003237{
3238 struct i40e_hw *hw = &pf->hw;
3239 u32 val;
3240
3241 val = I40E_PFINT_DYN_CTL0_INTENA_MASK |
3242 I40E_PFINT_DYN_CTL0_CLEARPBA_MASK |
3243 (I40E_ITR_NONE << I40E_PFINT_DYN_CTL0_ITR_INDX_SHIFT);
3244
3245 wr32(hw, I40E_PFINT_DYN_CTL0, val);
3246 i40e_flush(hw);
3247}
3248
3249/**
Carolyn Wyborny5c2cebd2014-06-04 01:23:18 +00003250 * i40e_irq_dynamic_disable - Disable default interrupt generation settings
3251 * @vsi: pointer to a vsi
Greg Rose03147772015-01-24 09:58:29 +00003252 * @vector: disable a particular Hw Interrupt vector
Carolyn Wyborny5c2cebd2014-06-04 01:23:18 +00003253 **/
3254void i40e_irq_dynamic_disable(struct i40e_vsi *vsi, int vector)
3255{
3256 struct i40e_pf *pf = vsi->back;
3257 struct i40e_hw *hw = &pf->hw;
3258 u32 val;
3259
3260 val = I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT;
3261 wr32(hw, I40E_PFINT_DYN_CTLN(vector - 1), val);
3262 i40e_flush(hw);
3263}
3264
3265/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003266 * i40e_msix_clean_rings - MSIX mode Interrupt Handler
3267 * @irq: interrupt number
3268 * @data: pointer to a q_vector
3269 **/
3270static irqreturn_t i40e_msix_clean_rings(int irq, void *data)
3271{
3272 struct i40e_q_vector *q_vector = data;
3273
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003274 if (!q_vector->tx.ring && !q_vector->rx.ring)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003275 return IRQ_HANDLED;
3276
Alexander Duyck5d3465a2015-09-29 15:19:50 -07003277 napi_schedule_irqoff(&q_vector->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003278
3279 return IRQ_HANDLED;
3280}
3281
3282/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003283 * i40e_vsi_request_irq_msix - Initialize MSI-X interrupts
3284 * @vsi: the VSI being configured
3285 * @basename: name for the vector
3286 *
3287 * Allocates MSI-X vectors and requests interrupts from the kernel.
3288 **/
3289static int i40e_vsi_request_irq_msix(struct i40e_vsi *vsi, char *basename)
3290{
3291 int q_vectors = vsi->num_q_vectors;
3292 struct i40e_pf *pf = vsi->back;
3293 int base = vsi->base_vector;
3294 int rx_int_idx = 0;
3295 int tx_int_idx = 0;
3296 int vector, err;
3297
3298 for (vector = 0; vector < q_vectors; vector++) {
Alexander Duyck493fb302013-09-28 07:01:44 +00003299 struct i40e_q_vector *q_vector = vsi->q_vectors[vector];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003300
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003301 if (q_vector->tx.ring && q_vector->rx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003302 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3303 "%s-%s-%d", basename, "TxRx", rx_int_idx++);
3304 tx_int_idx++;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003305 } else if (q_vector->rx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003306 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3307 "%s-%s-%d", basename, "rx", rx_int_idx++);
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003308 } else if (q_vector->tx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003309 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3310 "%s-%s-%d", basename, "tx", tx_int_idx++);
3311 } else {
3312 /* skip this unused q_vector */
3313 continue;
3314 }
3315 err = request_irq(pf->msix_entries[base + vector].vector,
3316 vsi->irq_handler,
3317 0,
3318 q_vector->name,
3319 q_vector);
3320 if (err) {
3321 dev_info(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04003322 "MSIX request_irq failed, error: %d\n", err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003323 goto free_queue_irqs;
3324 }
3325 /* assign the mask for this irq */
3326 irq_set_affinity_hint(pf->msix_entries[base + vector].vector,
3327 &q_vector->affinity_mask);
3328 }
3329
Shannon Nelson63741842014-04-23 04:50:16 +00003330 vsi->irqs_ready = true;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003331 return 0;
3332
3333free_queue_irqs:
3334 while (vector) {
3335 vector--;
3336 irq_set_affinity_hint(pf->msix_entries[base + vector].vector,
3337 NULL);
3338 free_irq(pf->msix_entries[base + vector].vector,
3339 &(vsi->q_vectors[vector]));
3340 }
3341 return err;
3342}
3343
3344/**
3345 * i40e_vsi_disable_irq - Mask off queue interrupt generation on the VSI
3346 * @vsi: the VSI being un-configured
3347 **/
3348static void i40e_vsi_disable_irq(struct i40e_vsi *vsi)
3349{
3350 struct i40e_pf *pf = vsi->back;
3351 struct i40e_hw *hw = &pf->hw;
3352 int base = vsi->base_vector;
3353 int i;
3354
3355 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00003356 wr32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx), 0);
3357 wr32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx), 0);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003358 }
3359
3360 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3361 for (i = vsi->base_vector;
3362 i < (vsi->num_q_vectors + vsi->base_vector); i++)
3363 wr32(hw, I40E_PFINT_DYN_CTLN(i - 1), 0);
3364
3365 i40e_flush(hw);
3366 for (i = 0; i < vsi->num_q_vectors; i++)
3367 synchronize_irq(pf->msix_entries[i + base].vector);
3368 } else {
3369 /* Legacy and MSI mode - this stops all interrupt handling */
3370 wr32(hw, I40E_PFINT_ICR0_ENA, 0);
3371 wr32(hw, I40E_PFINT_DYN_CTL0, 0);
3372 i40e_flush(hw);
3373 synchronize_irq(pf->pdev->irq);
3374 }
3375}
3376
3377/**
3378 * i40e_vsi_enable_irq - Enable IRQ for the given VSI
3379 * @vsi: the VSI being configured
3380 **/
3381static int i40e_vsi_enable_irq(struct i40e_vsi *vsi)
3382{
3383 struct i40e_pf *pf = vsi->back;
3384 int i;
3385
3386 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
Jesse Brandeburg78455482015-07-23 16:54:41 -04003387 for (i = 0; i < vsi->num_q_vectors; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003388 i40e_irq_dynamic_enable(vsi, i);
3389 } else {
3390 i40e_irq_dynamic_enable_icr0(pf);
3391 }
3392
Jesse Brandeburg1022cb62013-09-28 07:13:08 +00003393 i40e_flush(&pf->hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003394 return 0;
3395}
3396
3397/**
3398 * i40e_stop_misc_vector - Stop the vector that handles non-queue events
3399 * @pf: board private structure
3400 **/
3401static void i40e_stop_misc_vector(struct i40e_pf *pf)
3402{
3403 /* Disable ICR 0 */
3404 wr32(&pf->hw, I40E_PFINT_ICR0_ENA, 0);
3405 i40e_flush(&pf->hw);
3406}
3407
3408/**
3409 * i40e_intr - MSI/Legacy and non-queue interrupt handler
3410 * @irq: interrupt number
3411 * @data: pointer to a q_vector
3412 *
3413 * This is the handler used for all MSI/Legacy interrupts, and deals
3414 * with both queue and non-queue interrupts. This is also used in
3415 * MSIX mode to handle the non-queue interrupts.
3416 **/
3417static irqreturn_t i40e_intr(int irq, void *data)
3418{
3419 struct i40e_pf *pf = (struct i40e_pf *)data;
3420 struct i40e_hw *hw = &pf->hw;
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003421 irqreturn_t ret = IRQ_NONE;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003422 u32 icr0, icr0_remaining;
3423 u32 val, ena_mask;
3424
3425 icr0 = rd32(hw, I40E_PFINT_ICR0);
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003426 ena_mask = rd32(hw, I40E_PFINT_ICR0_ENA);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003427
Shannon Nelson116a57d2013-09-28 07:13:59 +00003428 /* if sharing a legacy IRQ, we might get called w/o an intr pending */
3429 if ((icr0 & I40E_PFINT_ICR0_INTEVENT_MASK) == 0)
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003430 goto enable_intr;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003431
Shannon Nelsoncd92e722013-11-16 10:00:44 +00003432 /* if interrupt but no bits showing, must be SWINT */
3433 if (((icr0 & ~I40E_PFINT_ICR0_INTEVENT_MASK) == 0) ||
3434 (icr0 & I40E_PFINT_ICR0_SWINT_MASK))
3435 pf->sw_int_count++;
3436
Anjali Singhai Jain0d8e1432015-06-05 12:20:32 -04003437 if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
3438 (ena_mask & I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK)) {
3439 ena_mask &= ~I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
3440 icr0 &= ~I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
3441 dev_info(&pf->pdev->dev, "cleared PE_CRITERR\n");
3442 }
3443
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003444 /* only q0 is used in MSI/Legacy mode, and none are used in MSIX */
3445 if (icr0 & I40E_PFINT_ICR0_QUEUE_0_MASK) {
Alexander Duyck5d3465a2015-09-29 15:19:50 -07003446 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
3447 struct i40e_q_vector *q_vector = vsi->q_vectors[0];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003448
3449 /* temporarily disable queue cause for NAPI processing */
3450 u32 qval = rd32(hw, I40E_QINT_RQCTL(0));
Jesse Brandeburg6995b362015-08-28 17:55:54 -04003451
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003452 qval &= ~I40E_QINT_RQCTL_CAUSE_ENA_MASK;
3453 wr32(hw, I40E_QINT_RQCTL(0), qval);
3454
3455 qval = rd32(hw, I40E_QINT_TQCTL(0));
3456 qval &= ~I40E_QINT_TQCTL_CAUSE_ENA_MASK;
3457 wr32(hw, I40E_QINT_TQCTL(0), qval);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003458
3459 if (!test_bit(__I40E_DOWN, &pf->state))
Alexander Duyck5d3465a2015-09-29 15:19:50 -07003460 napi_schedule_irqoff(&q_vector->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003461 }
3462
3463 if (icr0 & I40E_PFINT_ICR0_ADMINQ_MASK) {
3464 ena_mask &= ~I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
3465 set_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state);
3466 }
3467
3468 if (icr0 & I40E_PFINT_ICR0_MAL_DETECT_MASK) {
3469 ena_mask &= ~I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
3470 set_bit(__I40E_MDD_EVENT_PENDING, &pf->state);
3471 }
3472
3473 if (icr0 & I40E_PFINT_ICR0_VFLR_MASK) {
3474 ena_mask &= ~I40E_PFINT_ICR0_ENA_VFLR_MASK;
3475 set_bit(__I40E_VFLR_EVENT_PENDING, &pf->state);
3476 }
3477
3478 if (icr0 & I40E_PFINT_ICR0_GRST_MASK) {
3479 if (!test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
3480 set_bit(__I40E_RESET_INTR_RECEIVED, &pf->state);
3481 ena_mask &= ~I40E_PFINT_ICR0_ENA_GRST_MASK;
3482 val = rd32(hw, I40E_GLGEN_RSTAT);
3483 val = (val & I40E_GLGEN_RSTAT_RESET_TYPE_MASK)
3484 >> I40E_GLGEN_RSTAT_RESET_TYPE_SHIFT;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003485 if (val == I40E_RESET_CORER) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003486 pf->corer_count++;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003487 } else if (val == I40E_RESET_GLOBR) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003488 pf->globr_count++;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003489 } else if (val == I40E_RESET_EMPR) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003490 pf->empr_count++;
Anjali Singhai Jain9df42d12015-01-24 09:58:40 +00003491 set_bit(__I40E_EMP_RESET_INTR_RECEIVED, &pf->state);
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003492 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003493 }
3494
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003495 if (icr0 & I40E_PFINT_ICR0_HMC_ERR_MASK) {
3496 icr0 &= ~I40E_PFINT_ICR0_HMC_ERR_MASK;
3497 dev_info(&pf->pdev->dev, "HMC error interrupt\n");
Anjali Singhai Jain25fc0e62015-03-31 00:45:01 -07003498 dev_info(&pf->pdev->dev, "HMC error info 0x%x, HMC error data 0x%x\n",
3499 rd32(hw, I40E_PFHMC_ERRORINFO),
3500 rd32(hw, I40E_PFHMC_ERRORDATA));
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003501 }
3502
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003503 if (icr0 & I40E_PFINT_ICR0_TIMESYNC_MASK) {
3504 u32 prttsyn_stat = rd32(hw, I40E_PRTTSYN_STAT_0);
3505
3506 if (prttsyn_stat & I40E_PRTTSYN_STAT_0_TXTIME_MASK) {
Jacob Kellercafa1fc2014-04-24 18:05:03 -07003507 icr0 &= ~I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003508 i40e_ptp_tx_hwtstamp(pf);
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003509 }
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003510 }
3511
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003512 /* If a critical error is pending we have no choice but to reset the
3513 * device.
3514 * Report and mask out any remaining unexpected interrupts.
3515 */
3516 icr0_remaining = icr0 & ena_mask;
3517 if (icr0_remaining) {
3518 dev_info(&pf->pdev->dev, "unhandled interrupt icr0=0x%08x\n",
3519 icr0_remaining);
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003520 if ((icr0_remaining & I40E_PFINT_ICR0_PE_CRITERR_MASK) ||
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003521 (icr0_remaining & I40E_PFINT_ICR0_PCI_EXCEPTION_MASK) ||
Anjali Singhai Jainc0c28972014-02-12 01:45:34 +00003522 (icr0_remaining & I40E_PFINT_ICR0_ECC_ERR_MASK)) {
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003523 dev_info(&pf->pdev->dev, "device will be reset\n");
3524 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
3525 i40e_service_event_schedule(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003526 }
3527 ena_mask &= ~icr0_remaining;
3528 }
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003529 ret = IRQ_HANDLED;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003530
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003531enable_intr:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003532 /* re-enable interrupt causes */
3533 wr32(hw, I40E_PFINT_ICR0_ENA, ena_mask);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003534 if (!test_bit(__I40E_DOWN, &pf->state)) {
3535 i40e_service_event_schedule(pf);
3536 i40e_irq_dynamic_enable_icr0(pf);
3537 }
3538
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003539 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003540}
3541
3542/**
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003543 * i40e_clean_fdir_tx_irq - Reclaim resources after transmit completes
3544 * @tx_ring: tx ring to clean
3545 * @budget: how many cleans we're allowed
3546 *
3547 * Returns true if there's any budget left (e.g. the clean is finished)
3548 **/
3549static bool i40e_clean_fdir_tx_irq(struct i40e_ring *tx_ring, int budget)
3550{
3551 struct i40e_vsi *vsi = tx_ring->vsi;
3552 u16 i = tx_ring->next_to_clean;
3553 struct i40e_tx_buffer *tx_buf;
3554 struct i40e_tx_desc *tx_desc;
3555
3556 tx_buf = &tx_ring->tx_bi[i];
3557 tx_desc = I40E_TX_DESC(tx_ring, i);
3558 i -= tx_ring->count;
3559
3560 do {
3561 struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
3562
3563 /* if next_to_watch is not set then there is no work pending */
3564 if (!eop_desc)
3565 break;
3566
3567 /* prevent any other reads prior to eop_desc */
3568 read_barrier_depends();
3569
3570 /* if the descriptor isn't done, no work yet to do */
3571 if (!(eop_desc->cmd_type_offset_bsz &
3572 cpu_to_le64(I40E_TX_DESC_DTYPE_DESC_DONE)))
3573 break;
3574
3575 /* clear next_to_watch to prevent false hangs */
3576 tx_buf->next_to_watch = NULL;
3577
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003578 tx_desc->buffer_addr = 0;
3579 tx_desc->cmd_type_offset_bsz = 0;
3580 /* move past filter desc */
3581 tx_buf++;
3582 tx_desc++;
3583 i++;
3584 if (unlikely(!i)) {
3585 i -= tx_ring->count;
3586 tx_buf = tx_ring->tx_bi;
3587 tx_desc = I40E_TX_DESC(tx_ring, 0);
3588 }
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003589 /* unmap skb header data */
3590 dma_unmap_single(tx_ring->dev,
3591 dma_unmap_addr(tx_buf, dma),
3592 dma_unmap_len(tx_buf, len),
3593 DMA_TO_DEVICE);
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003594 if (tx_buf->tx_flags & I40E_TX_FLAGS_FD_SB)
3595 kfree(tx_buf->raw_buf);
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003596
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003597 tx_buf->raw_buf = NULL;
3598 tx_buf->tx_flags = 0;
3599 tx_buf->next_to_watch = NULL;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003600 dma_unmap_len_set(tx_buf, len, 0);
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003601 tx_desc->buffer_addr = 0;
3602 tx_desc->cmd_type_offset_bsz = 0;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003603
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003604 /* move us past the eop_desc for start of next FD desc */
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003605 tx_buf++;
3606 tx_desc++;
3607 i++;
3608 if (unlikely(!i)) {
3609 i -= tx_ring->count;
3610 tx_buf = tx_ring->tx_bi;
3611 tx_desc = I40E_TX_DESC(tx_ring, 0);
3612 }
3613
3614 /* update budget accounting */
3615 budget--;
3616 } while (likely(budget));
3617
3618 i += tx_ring->count;
3619 tx_ring->next_to_clean = i;
3620
Jesse Brandeburg6995b362015-08-28 17:55:54 -04003621 if (vsi->back->flags & I40E_FLAG_MSIX_ENABLED)
Jesse Brandeburg78455482015-07-23 16:54:41 -04003622 i40e_irq_dynamic_enable(vsi, tx_ring->q_vector->v_idx);
Jesse Brandeburg6995b362015-08-28 17:55:54 -04003623
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003624 return budget > 0;
3625}
3626
3627/**
3628 * i40e_fdir_clean_ring - Interrupt Handler for FDIR SB ring
3629 * @irq: interrupt number
3630 * @data: pointer to a q_vector
3631 **/
3632static irqreturn_t i40e_fdir_clean_ring(int irq, void *data)
3633{
3634 struct i40e_q_vector *q_vector = data;
3635 struct i40e_vsi *vsi;
3636
3637 if (!q_vector->tx.ring)
3638 return IRQ_HANDLED;
3639
3640 vsi = q_vector->tx.ring->vsi;
3641 i40e_clean_fdir_tx_irq(q_vector->tx.ring, vsi->work_limit);
3642
3643 return IRQ_HANDLED;
3644}
3645
3646/**
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003647 * i40e_map_vector_to_qp - Assigns the queue pair to the vector
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003648 * @vsi: the VSI being configured
3649 * @v_idx: vector index
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003650 * @qp_idx: queue pair index
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003651 **/
Anjali Singhai Jain26cdc442015-07-10 19:36:00 -04003652static void i40e_map_vector_to_qp(struct i40e_vsi *vsi, int v_idx, int qp_idx)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003653{
Alexander Duyck493fb302013-09-28 07:01:44 +00003654 struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00003655 struct i40e_ring *tx_ring = vsi->tx_rings[qp_idx];
3656 struct i40e_ring *rx_ring = vsi->rx_rings[qp_idx];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003657
3658 tx_ring->q_vector = q_vector;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003659 tx_ring->next = q_vector->tx.ring;
3660 q_vector->tx.ring = tx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003661 q_vector->tx.count++;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003662
3663 rx_ring->q_vector = q_vector;
3664 rx_ring->next = q_vector->rx.ring;
3665 q_vector->rx.ring = rx_ring;
3666 q_vector->rx.count++;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003667}
3668
3669/**
3670 * i40e_vsi_map_rings_to_vectors - Maps descriptor rings to vectors
3671 * @vsi: the VSI being configured
3672 *
3673 * This function maps descriptor rings to the queue-specific vectors
3674 * we were allotted through the MSI-X enabling code. Ideally, we'd have
3675 * one vector per queue pair, but on a constrained vector budget, we
3676 * group the queue pairs as "efficiently" as possible.
3677 **/
3678static void i40e_vsi_map_rings_to_vectors(struct i40e_vsi *vsi)
3679{
3680 int qp_remaining = vsi->num_queue_pairs;
3681 int q_vectors = vsi->num_q_vectors;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003682 int num_ringpairs;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003683 int v_start = 0;
3684 int qp_idx = 0;
3685
3686 /* If we don't have enough vectors for a 1-to-1 mapping, we'll have to
3687 * group them so there are multiple queues per vector.
Anjali Singhai Jain70114ec2014-06-03 23:50:14 +00003688 * It is also important to go through all the vectors available to be
3689 * sure that if we don't use all the vectors, that the remaining vectors
3690 * are cleared. This is especially important when decreasing the
3691 * number of queues in use.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003692 */
Anjali Singhai Jain70114ec2014-06-03 23:50:14 +00003693 for (; v_start < q_vectors; v_start++) {
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003694 struct i40e_q_vector *q_vector = vsi->q_vectors[v_start];
3695
3696 num_ringpairs = DIV_ROUND_UP(qp_remaining, q_vectors - v_start);
3697
3698 q_vector->num_ringpairs = num_ringpairs;
3699
3700 q_vector->rx.count = 0;
3701 q_vector->tx.count = 0;
3702 q_vector->rx.ring = NULL;
3703 q_vector->tx.ring = NULL;
3704
3705 while (num_ringpairs--) {
Anjali Singhai Jain26cdc442015-07-10 19:36:00 -04003706 i40e_map_vector_to_qp(vsi, v_start, qp_idx);
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003707 qp_idx++;
3708 qp_remaining--;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003709 }
3710 }
3711}
3712
3713/**
3714 * i40e_vsi_request_irq - Request IRQ from the OS
3715 * @vsi: the VSI being configured
3716 * @basename: name for the vector
3717 **/
3718static int i40e_vsi_request_irq(struct i40e_vsi *vsi, char *basename)
3719{
3720 struct i40e_pf *pf = vsi->back;
3721 int err;
3722
3723 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
3724 err = i40e_vsi_request_irq_msix(vsi, basename);
3725 else if (pf->flags & I40E_FLAG_MSI_ENABLED)
3726 err = request_irq(pf->pdev->irq, i40e_intr, 0,
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00003727 pf->int_name, pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003728 else
3729 err = request_irq(pf->pdev->irq, i40e_intr, IRQF_SHARED,
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00003730 pf->int_name, pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003731
3732 if (err)
3733 dev_info(&pf->pdev->dev, "request_irq failed, Error %d\n", err);
3734
3735 return err;
3736}
3737
3738#ifdef CONFIG_NET_POLL_CONTROLLER
3739/**
3740 * i40e_netpoll - A Polling 'interrupt'handler
3741 * @netdev: network interface device structure
3742 *
3743 * This is used by netconsole to send skbs without having to re-enable
3744 * interrupts. It's not called while the normal interrupt routine is executing.
3745 **/
Vasu Dev38e00432014-08-01 13:27:03 -07003746#ifdef I40E_FCOE
3747void i40e_netpoll(struct net_device *netdev)
3748#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003749static void i40e_netpoll(struct net_device *netdev)
Vasu Dev38e00432014-08-01 13:27:03 -07003750#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003751{
3752 struct i40e_netdev_priv *np = netdev_priv(netdev);
3753 struct i40e_vsi *vsi = np->vsi;
3754 struct i40e_pf *pf = vsi->back;
3755 int i;
3756
3757 /* if interface is down do nothing */
3758 if (test_bit(__I40E_DOWN, &vsi->state))
3759 return;
3760
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003761 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3762 for (i = 0; i < vsi->num_q_vectors; i++)
Alexander Duyck493fb302013-09-28 07:01:44 +00003763 i40e_msix_clean_rings(0, vsi->q_vectors[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003764 } else {
3765 i40e_intr(pf->pdev->irq, netdev);
3766 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003767}
3768#endif
3769
3770/**
Neerav Parikh23527302014-06-03 23:50:15 +00003771 * i40e_pf_txq_wait - Wait for a PF's Tx queue to be enabled or disabled
3772 * @pf: the PF being configured
3773 * @pf_q: the PF queue
3774 * @enable: enable or disable state of the queue
3775 *
3776 * This routine will wait for the given Tx queue of the PF to reach the
3777 * enabled or disabled state.
3778 * Returns -ETIMEDOUT in case of failing to reach the requested state after
3779 * multiple retries; else will return 0 in case of success.
3780 **/
3781static int i40e_pf_txq_wait(struct i40e_pf *pf, int pf_q, bool enable)
3782{
3783 int i;
3784 u32 tx_reg;
3785
3786 for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
3787 tx_reg = rd32(&pf->hw, I40E_QTX_ENA(pf_q));
3788 if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
3789 break;
3790
Neerav Parikhf98a2002014-09-13 07:40:44 +00003791 usleep_range(10, 20);
Neerav Parikh23527302014-06-03 23:50:15 +00003792 }
3793 if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
3794 return -ETIMEDOUT;
3795
3796 return 0;
3797}
3798
3799/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003800 * i40e_vsi_control_tx - Start or stop a VSI's rings
3801 * @vsi: the VSI being configured
3802 * @enable: start or stop the rings
3803 **/
3804static int i40e_vsi_control_tx(struct i40e_vsi *vsi, bool enable)
3805{
3806 struct i40e_pf *pf = vsi->back;
3807 struct i40e_hw *hw = &pf->hw;
Neerav Parikh23527302014-06-03 23:50:15 +00003808 int i, j, pf_q, ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003809 u32 tx_reg;
3810
3811 pf_q = vsi->base_queue;
3812 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
Matt Jared351499a2014-04-23 04:50:03 +00003813
3814 /* warn the TX unit of coming changes */
3815 i40e_pre_tx_queue_cfg(&pf->hw, pf_q, enable);
3816 if (!enable)
Neerav Parikhf98a2002014-09-13 07:40:44 +00003817 usleep_range(10, 20);
Matt Jared351499a2014-04-23 04:50:03 +00003818
Mitch Williams6c5ef622014-02-20 19:29:16 -08003819 for (j = 0; j < 50; j++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003820 tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
Mitch Williams6c5ef622014-02-20 19:29:16 -08003821 if (((tx_reg >> I40E_QTX_ENA_QENA_REQ_SHIFT) & 1) ==
3822 ((tx_reg >> I40E_QTX_ENA_QENA_STAT_SHIFT) & 1))
3823 break;
3824 usleep_range(1000, 2000);
3825 }
Mitch Williamsfda972f2013-11-28 06:39:29 +00003826 /* Skip if the queue is already in the requested state */
Catherine Sullivan7c122002014-03-14 07:32:29 +00003827 if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
Mitch Williamsfda972f2013-11-28 06:39:29 +00003828 continue;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003829
3830 /* turn on/off the queue */
Shannon Nelsonc5c9eb92013-12-21 05:44:48 +00003831 if (enable) {
3832 wr32(hw, I40E_QTX_HEAD(pf_q), 0);
Mitch Williams6c5ef622014-02-20 19:29:16 -08003833 tx_reg |= I40E_QTX_ENA_QENA_REQ_MASK;
Shannon Nelsonc5c9eb92013-12-21 05:44:48 +00003834 } else {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003835 tx_reg &= ~I40E_QTX_ENA_QENA_REQ_MASK;
Shannon Nelsonc5c9eb92013-12-21 05:44:48 +00003836 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003837
3838 wr32(hw, I40E_QTX_ENA(pf_q), tx_reg);
Neerav Parikh69129dc2014-11-12 00:18:46 +00003839 /* No waiting for the Tx queue to disable */
3840 if (!enable && test_bit(__I40E_PORT_TX_SUSPENDED, &pf->state))
3841 continue;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003842
3843 /* wait for the change to finish */
Neerav Parikh23527302014-06-03 23:50:15 +00003844 ret = i40e_pf_txq_wait(pf, pf_q, enable);
3845 if (ret) {
3846 dev_info(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04003847 "VSI seid %d Tx ring %d %sable timeout\n",
3848 vsi->seid, pf_q, (enable ? "en" : "dis"));
Neerav Parikh23527302014-06-03 23:50:15 +00003849 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003850 }
3851 }
3852
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00003853 if (hw->revision_id == 0)
3854 mdelay(50);
Neerav Parikh23527302014-06-03 23:50:15 +00003855 return ret;
3856}
3857
3858/**
3859 * i40e_pf_rxq_wait - Wait for a PF's Rx queue to be enabled or disabled
3860 * @pf: the PF being configured
3861 * @pf_q: the PF queue
3862 * @enable: enable or disable state of the queue
3863 *
3864 * This routine will wait for the given Rx queue of the PF to reach the
3865 * enabled or disabled state.
3866 * Returns -ETIMEDOUT in case of failing to reach the requested state after
3867 * multiple retries; else will return 0 in case of success.
3868 **/
3869static int i40e_pf_rxq_wait(struct i40e_pf *pf, int pf_q, bool enable)
3870{
3871 int i;
3872 u32 rx_reg;
3873
3874 for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
3875 rx_reg = rd32(&pf->hw, I40E_QRX_ENA(pf_q));
3876 if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
3877 break;
3878
Neerav Parikhf98a2002014-09-13 07:40:44 +00003879 usleep_range(10, 20);
Neerav Parikh23527302014-06-03 23:50:15 +00003880 }
3881 if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
3882 return -ETIMEDOUT;
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00003883
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003884 return 0;
3885}
3886
3887/**
3888 * i40e_vsi_control_rx - Start or stop a VSI's rings
3889 * @vsi: the VSI being configured
3890 * @enable: start or stop the rings
3891 **/
3892static int i40e_vsi_control_rx(struct i40e_vsi *vsi, bool enable)
3893{
3894 struct i40e_pf *pf = vsi->back;
3895 struct i40e_hw *hw = &pf->hw;
Neerav Parikh23527302014-06-03 23:50:15 +00003896 int i, j, pf_q, ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003897 u32 rx_reg;
3898
3899 pf_q = vsi->base_queue;
3900 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
Mitch Williams6c5ef622014-02-20 19:29:16 -08003901 for (j = 0; j < 50; j++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003902 rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
Mitch Williams6c5ef622014-02-20 19:29:16 -08003903 if (((rx_reg >> I40E_QRX_ENA_QENA_REQ_SHIFT) & 1) ==
3904 ((rx_reg >> I40E_QRX_ENA_QENA_STAT_SHIFT) & 1))
3905 break;
3906 usleep_range(1000, 2000);
3907 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003908
Catherine Sullivan7c122002014-03-14 07:32:29 +00003909 /* Skip if the queue is already in the requested state */
3910 if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
3911 continue;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003912
3913 /* turn on/off the queue */
3914 if (enable)
Mitch Williams6c5ef622014-02-20 19:29:16 -08003915 rx_reg |= I40E_QRX_ENA_QENA_REQ_MASK;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003916 else
Mitch Williams6c5ef622014-02-20 19:29:16 -08003917 rx_reg &= ~I40E_QRX_ENA_QENA_REQ_MASK;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003918 wr32(hw, I40E_QRX_ENA(pf_q), rx_reg);
3919
3920 /* wait for the change to finish */
Neerav Parikh23527302014-06-03 23:50:15 +00003921 ret = i40e_pf_rxq_wait(pf, pf_q, enable);
3922 if (ret) {
3923 dev_info(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04003924 "VSI seid %d Rx ring %d %sable timeout\n",
3925 vsi->seid, pf_q, (enable ? "en" : "dis"));
Neerav Parikh23527302014-06-03 23:50:15 +00003926 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003927 }
3928 }
3929
Neerav Parikh23527302014-06-03 23:50:15 +00003930 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003931}
3932
3933/**
3934 * i40e_vsi_control_rings - Start or stop a VSI's rings
3935 * @vsi: the VSI being configured
3936 * @enable: start or stop the rings
3937 **/
Mitch Williamsfc18eaa2013-11-28 06:39:27 +00003938int i40e_vsi_control_rings(struct i40e_vsi *vsi, bool request)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003939{
Anjali Singhai Jain3b867b22013-12-21 05:44:44 +00003940 int ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003941
3942 /* do rx first for enable and last for disable */
3943 if (request) {
3944 ret = i40e_vsi_control_rx(vsi, request);
3945 if (ret)
3946 return ret;
3947 ret = i40e_vsi_control_tx(vsi, request);
3948 } else {
Anjali Singhai Jain3b867b22013-12-21 05:44:44 +00003949 /* Ignore return value, we need to shutdown whatever we can */
3950 i40e_vsi_control_tx(vsi, request);
3951 i40e_vsi_control_rx(vsi, request);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003952 }
3953
3954 return ret;
3955}
3956
3957/**
3958 * i40e_vsi_free_irq - Free the irq association with the OS
3959 * @vsi: the VSI being configured
3960 **/
3961static void i40e_vsi_free_irq(struct i40e_vsi *vsi)
3962{
3963 struct i40e_pf *pf = vsi->back;
3964 struct i40e_hw *hw = &pf->hw;
3965 int base = vsi->base_vector;
3966 u32 val, qp;
3967 int i;
3968
3969 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3970 if (!vsi->q_vectors)
3971 return;
3972
Shannon Nelson63741842014-04-23 04:50:16 +00003973 if (!vsi->irqs_ready)
3974 return;
3975
3976 vsi->irqs_ready = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003977 for (i = 0; i < vsi->num_q_vectors; i++) {
3978 u16 vector = i + base;
3979
3980 /* free only the irqs that were actually requested */
Shannon Nelson78681b12013-11-28 06:39:36 +00003981 if (!vsi->q_vectors[i] ||
3982 !vsi->q_vectors[i]->num_ringpairs)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003983 continue;
3984
3985 /* clear the affinity_mask in the IRQ descriptor */
3986 irq_set_affinity_hint(pf->msix_entries[vector].vector,
3987 NULL);
3988 free_irq(pf->msix_entries[vector].vector,
Alexander Duyck493fb302013-09-28 07:01:44 +00003989 vsi->q_vectors[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003990
3991 /* Tear down the interrupt queue link list
3992 *
3993 * We know that they come in pairs and always
3994 * the Rx first, then the Tx. To clear the
3995 * link list, stick the EOL value into the
3996 * next_q field of the registers.
3997 */
3998 val = rd32(hw, I40E_PFINT_LNKLSTN(vector - 1));
3999 qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
4000 >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4001 val |= I40E_QUEUE_END_OF_LIST
4002 << I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4003 wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), val);
4004
4005 while (qp != I40E_QUEUE_END_OF_LIST) {
4006 u32 next;
4007
4008 val = rd32(hw, I40E_QINT_RQCTL(qp));
4009
4010 val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
4011 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
4012 I40E_QINT_RQCTL_CAUSE_ENA_MASK |
4013 I40E_QINT_RQCTL_INTEVENT_MASK);
4014
4015 val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
4016 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
4017
4018 wr32(hw, I40E_QINT_RQCTL(qp), val);
4019
4020 val = rd32(hw, I40E_QINT_TQCTL(qp));
4021
4022 next = (val & I40E_QINT_TQCTL_NEXTQ_INDX_MASK)
4023 >> I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT;
4024
4025 val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
4026 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
4027 I40E_QINT_TQCTL_CAUSE_ENA_MASK |
4028 I40E_QINT_TQCTL_INTEVENT_MASK);
4029
4030 val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
4031 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
4032
4033 wr32(hw, I40E_QINT_TQCTL(qp), val);
4034 qp = next;
4035 }
4036 }
4037 } else {
4038 free_irq(pf->pdev->irq, pf);
4039
4040 val = rd32(hw, I40E_PFINT_LNKLST0);
4041 qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
4042 >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4043 val |= I40E_QUEUE_END_OF_LIST
4044 << I40E_PFINT_LNKLST0_FIRSTQ_INDX_SHIFT;
4045 wr32(hw, I40E_PFINT_LNKLST0, val);
4046
4047 val = rd32(hw, I40E_QINT_RQCTL(qp));
4048 val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
4049 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
4050 I40E_QINT_RQCTL_CAUSE_ENA_MASK |
4051 I40E_QINT_RQCTL_INTEVENT_MASK);
4052
4053 val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
4054 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
4055
4056 wr32(hw, I40E_QINT_RQCTL(qp), val);
4057
4058 val = rd32(hw, I40E_QINT_TQCTL(qp));
4059
4060 val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
4061 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
4062 I40E_QINT_TQCTL_CAUSE_ENA_MASK |
4063 I40E_QINT_TQCTL_INTEVENT_MASK);
4064
4065 val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
4066 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
4067
4068 wr32(hw, I40E_QINT_TQCTL(qp), val);
4069 }
4070}
4071
4072/**
Alexander Duyck493fb302013-09-28 07:01:44 +00004073 * i40e_free_q_vector - Free memory allocated for specific interrupt vector
4074 * @vsi: the VSI being configured
4075 * @v_idx: Index of vector to be freed
4076 *
4077 * This function frees the memory allocated to the q_vector. In addition if
4078 * NAPI is enabled it will delete any references to the NAPI struct prior
4079 * to freeing the q_vector.
4080 **/
4081static void i40e_free_q_vector(struct i40e_vsi *vsi, int v_idx)
4082{
4083 struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00004084 struct i40e_ring *ring;
Alexander Duyck493fb302013-09-28 07:01:44 +00004085
4086 if (!q_vector)
4087 return;
4088
4089 /* disassociate q_vector from rings */
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00004090 i40e_for_each_ring(ring, q_vector->tx)
4091 ring->q_vector = NULL;
4092
4093 i40e_for_each_ring(ring, q_vector->rx)
4094 ring->q_vector = NULL;
Alexander Duyck493fb302013-09-28 07:01:44 +00004095
4096 /* only VSI w/ an associated netdev is set up w/ NAPI */
4097 if (vsi->netdev)
4098 netif_napi_del(&q_vector->napi);
4099
4100 vsi->q_vectors[v_idx] = NULL;
4101
4102 kfree_rcu(q_vector, rcu);
4103}
4104
4105/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004106 * i40e_vsi_free_q_vectors - Free memory allocated for interrupt vectors
4107 * @vsi: the VSI being un-configured
4108 *
4109 * This frees the memory allocated to the q_vectors and
4110 * deletes references to the NAPI struct.
4111 **/
4112static void i40e_vsi_free_q_vectors(struct i40e_vsi *vsi)
4113{
4114 int v_idx;
4115
Alexander Duyck493fb302013-09-28 07:01:44 +00004116 for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++)
4117 i40e_free_q_vector(vsi, v_idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004118}
4119
4120/**
4121 * i40e_reset_interrupt_capability - Disable interrupt setup in OS
4122 * @pf: board private structure
4123 **/
4124static void i40e_reset_interrupt_capability(struct i40e_pf *pf)
4125{
4126 /* If we're in Legacy mode, the interrupt was cleaned in vsi_close */
4127 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4128 pci_disable_msix(pf->pdev);
4129 kfree(pf->msix_entries);
4130 pf->msix_entries = NULL;
Shannon Nelson3b444392015-02-26 16:15:57 +00004131 kfree(pf->irq_pile);
4132 pf->irq_pile = NULL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004133 } else if (pf->flags & I40E_FLAG_MSI_ENABLED) {
4134 pci_disable_msi(pf->pdev);
4135 }
4136 pf->flags &= ~(I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
4137}
4138
4139/**
4140 * i40e_clear_interrupt_scheme - Clear the current interrupt scheme settings
4141 * @pf: board private structure
4142 *
4143 * We go through and clear interrupt specific resources and reset the structure
4144 * to pre-load conditions
4145 **/
4146static void i40e_clear_interrupt_scheme(struct i40e_pf *pf)
4147{
4148 int i;
4149
Shannon Nelsone1477582015-02-21 06:44:33 +00004150 i40e_stop_misc_vector(pf);
4151 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4152 synchronize_irq(pf->msix_entries[0].vector);
4153 free_irq(pf->msix_entries[0].vector, pf);
4154 }
4155
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004156 i40e_put_lump(pf->irq_pile, 0, I40E_PILE_VALID_BIT-1);
Mitch Williams505682c2014-05-20 08:01:37 +00004157 for (i = 0; i < pf->num_alloc_vsi; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004158 if (pf->vsi[i])
4159 i40e_vsi_free_q_vectors(pf->vsi[i]);
4160 i40e_reset_interrupt_capability(pf);
4161}
4162
4163/**
4164 * i40e_napi_enable_all - Enable NAPI for all q_vectors in the VSI
4165 * @vsi: the VSI being configured
4166 **/
4167static void i40e_napi_enable_all(struct i40e_vsi *vsi)
4168{
4169 int q_idx;
4170
4171 if (!vsi->netdev)
4172 return;
4173
4174 for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++)
Alexander Duyck493fb302013-09-28 07:01:44 +00004175 napi_enable(&vsi->q_vectors[q_idx]->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004176}
4177
4178/**
4179 * i40e_napi_disable_all - Disable NAPI for all q_vectors in the VSI
4180 * @vsi: the VSI being configured
4181 **/
4182static void i40e_napi_disable_all(struct i40e_vsi *vsi)
4183{
4184 int q_idx;
4185
4186 if (!vsi->netdev)
4187 return;
4188
4189 for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++)
Alexander Duyck493fb302013-09-28 07:01:44 +00004190 napi_disable(&vsi->q_vectors[q_idx]->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004191}
4192
4193/**
Shannon Nelson90ef8d42014-03-14 07:32:26 +00004194 * i40e_vsi_close - Shut down a VSI
4195 * @vsi: the vsi to be quelled
4196 **/
4197static void i40e_vsi_close(struct i40e_vsi *vsi)
4198{
4199 if (!test_and_set_bit(__I40E_DOWN, &vsi->state))
4200 i40e_down(vsi);
4201 i40e_vsi_free_irq(vsi);
4202 i40e_vsi_free_tx_resources(vsi);
4203 i40e_vsi_free_rx_resources(vsi);
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04004204 vsi->current_netdev_flags = 0;
Shannon Nelson90ef8d42014-03-14 07:32:26 +00004205}
4206
4207/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004208 * i40e_quiesce_vsi - Pause a given VSI
4209 * @vsi: the VSI being paused
4210 **/
4211static void i40e_quiesce_vsi(struct i40e_vsi *vsi)
4212{
4213 if (test_bit(__I40E_DOWN, &vsi->state))
4214 return;
4215
Neerav Parikhd341b7a2014-11-12 00:18:51 +00004216 /* No need to disable FCoE VSI when Tx suspended */
4217 if ((test_bit(__I40E_PORT_TX_SUSPENDED, &vsi->back->state)) &&
4218 vsi->type == I40E_VSI_FCOE) {
4219 dev_dbg(&vsi->back->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04004220 "VSI seid %d skipping FCoE VSI disable\n", vsi->seid);
Neerav Parikhd341b7a2014-11-12 00:18:51 +00004221 return;
4222 }
4223
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004224 set_bit(__I40E_NEEDS_RESTART, &vsi->state);
Jesse Brandeburg6995b362015-08-28 17:55:54 -04004225 if (vsi->netdev && netif_running(vsi->netdev))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004226 vsi->netdev->netdev_ops->ndo_stop(vsi->netdev);
Jesse Brandeburg6995b362015-08-28 17:55:54 -04004227 else
Shannon Nelson90ef8d42014-03-14 07:32:26 +00004228 i40e_vsi_close(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004229}
4230
4231/**
4232 * i40e_unquiesce_vsi - Resume a given VSI
4233 * @vsi: the VSI being resumed
4234 **/
4235static void i40e_unquiesce_vsi(struct i40e_vsi *vsi)
4236{
4237 if (!test_bit(__I40E_NEEDS_RESTART, &vsi->state))
4238 return;
4239
4240 clear_bit(__I40E_NEEDS_RESTART, &vsi->state);
4241 if (vsi->netdev && netif_running(vsi->netdev))
4242 vsi->netdev->netdev_ops->ndo_open(vsi->netdev);
4243 else
Shannon Nelson8276f752014-03-14 07:32:27 +00004244 i40e_vsi_open(vsi); /* this clears the DOWN bit */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004245}
4246
4247/**
4248 * i40e_pf_quiesce_all_vsi - Pause all VSIs on a PF
4249 * @pf: the PF
4250 **/
4251static void i40e_pf_quiesce_all_vsi(struct i40e_pf *pf)
4252{
4253 int v;
4254
Mitch Williams505682c2014-05-20 08:01:37 +00004255 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004256 if (pf->vsi[v])
4257 i40e_quiesce_vsi(pf->vsi[v]);
4258 }
4259}
4260
4261/**
4262 * i40e_pf_unquiesce_all_vsi - Resume all VSIs on a PF
4263 * @pf: the PF
4264 **/
4265static void i40e_pf_unquiesce_all_vsi(struct i40e_pf *pf)
4266{
4267 int v;
4268
Mitch Williams505682c2014-05-20 08:01:37 +00004269 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004270 if (pf->vsi[v])
4271 i40e_unquiesce_vsi(pf->vsi[v]);
4272 }
4273}
4274
Neerav Parikh69129dc2014-11-12 00:18:46 +00004275#ifdef CONFIG_I40E_DCB
4276/**
4277 * i40e_vsi_wait_txq_disabled - Wait for VSI's queues to be disabled
4278 * @vsi: the VSI being configured
4279 *
4280 * This function waits for the given VSI's Tx queues to be disabled.
4281 **/
4282static int i40e_vsi_wait_txq_disabled(struct i40e_vsi *vsi)
4283{
4284 struct i40e_pf *pf = vsi->back;
4285 int i, pf_q, ret;
4286
4287 pf_q = vsi->base_queue;
4288 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4289 /* Check and wait for the disable status of the queue */
4290 ret = i40e_pf_txq_wait(pf, pf_q, false);
4291 if (ret) {
4292 dev_info(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04004293 "VSI seid %d Tx ring %d disable timeout\n",
4294 vsi->seid, pf_q);
Neerav Parikh69129dc2014-11-12 00:18:46 +00004295 return ret;
4296 }
4297 }
4298
4299 return 0;
4300}
4301
4302/**
4303 * i40e_pf_wait_txq_disabled - Wait for all queues of PF VSIs to be disabled
4304 * @pf: the PF
4305 *
4306 * This function waits for the Tx queues to be in disabled state for all the
4307 * VSIs that are managed by this PF.
4308 **/
4309static int i40e_pf_wait_txq_disabled(struct i40e_pf *pf)
4310{
4311 int v, ret = 0;
4312
4313 for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
Neerav Parikhd341b7a2014-11-12 00:18:51 +00004314 /* No need to wait for FCoE VSI queues */
4315 if (pf->vsi[v] && pf->vsi[v]->type != I40E_VSI_FCOE) {
Neerav Parikh69129dc2014-11-12 00:18:46 +00004316 ret = i40e_vsi_wait_txq_disabled(pf->vsi[v]);
4317 if (ret)
4318 break;
4319 }
4320 }
4321
4322 return ret;
4323}
4324
4325#endif
Kiran Patilb03a8c12015-09-24 18:13:15 -04004326
4327/**
4328 * i40e_detect_recover_hung_queue - Function to detect and recover hung_queue
4329 * @q_idx: TX queue number
4330 * @vsi: Pointer to VSI struct
4331 *
4332 * This function checks specified queue for given VSI. Detects hung condition.
4333 * Sets hung bit since it is two step process. Before next run of service task
4334 * if napi_poll runs, it reset 'hung' bit for respective q_vector. If not,
4335 * hung condition remain unchanged and during subsequent run, this function
4336 * issues SW interrupt to recover from hung condition.
4337 **/
4338static void i40e_detect_recover_hung_queue(int q_idx, struct i40e_vsi *vsi)
4339{
4340 struct i40e_ring *tx_ring = NULL;
4341 struct i40e_pf *pf;
4342 u32 head, val, tx_pending;
4343 int i;
4344
4345 pf = vsi->back;
4346
4347 /* now that we have an index, find the tx_ring struct */
4348 for (i = 0; i < vsi->num_queue_pairs; i++) {
4349 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc) {
4350 if (q_idx == vsi->tx_rings[i]->queue_index) {
4351 tx_ring = vsi->tx_rings[i];
4352 break;
4353 }
4354 }
4355 }
4356
4357 if (!tx_ring)
4358 return;
4359
4360 /* Read interrupt register */
4361 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
4362 val = rd32(&pf->hw,
4363 I40E_PFINT_DYN_CTLN(tx_ring->q_vector->v_idx +
4364 tx_ring->vsi->base_vector - 1));
4365 else
4366 val = rd32(&pf->hw, I40E_PFINT_DYN_CTL0);
4367
4368 head = i40e_get_head(tx_ring);
4369
4370 tx_pending = i40e_get_tx_pending(tx_ring);
4371
4372 /* Interrupts are disabled and TX pending is non-zero,
4373 * trigger the SW interrupt (don't wait). Worst case
4374 * there will be one extra interrupt which may result
4375 * into not cleaning any queues because queues are cleaned.
4376 */
4377 if (tx_pending && (!(val & I40E_PFINT_DYN_CTLN_INTENA_MASK)))
4378 i40e_force_wb(vsi, tx_ring->q_vector);
4379}
4380
4381/**
4382 * i40e_detect_recover_hung - Function to detect and recover hung_queues
4383 * @pf: pointer to PF struct
4384 *
4385 * LAN VSI has netdev and netdev has TX queues. This function is to check
4386 * each of those TX queues if they are hung, trigger recovery by issuing
4387 * SW interrupt.
4388 **/
4389static void i40e_detect_recover_hung(struct i40e_pf *pf)
4390{
4391 struct net_device *netdev;
4392 struct i40e_vsi *vsi;
4393 int i;
4394
4395 /* Only for LAN VSI */
4396 vsi = pf->vsi[pf->lan_vsi];
4397
4398 if (!vsi)
4399 return;
4400
4401 /* Make sure, VSI state is not DOWN/RECOVERY_PENDING */
4402 if (test_bit(__I40E_DOWN, &vsi->back->state) ||
4403 test_bit(__I40E_RESET_RECOVERY_PENDING, &vsi->back->state))
4404 return;
4405
4406 /* Make sure type is MAIN VSI */
4407 if (vsi->type != I40E_VSI_MAIN)
4408 return;
4409
4410 netdev = vsi->netdev;
4411 if (!netdev)
4412 return;
4413
4414 /* Bail out if netif_carrier is not OK */
4415 if (!netif_carrier_ok(netdev))
4416 return;
4417
4418 /* Go thru' TX queues for netdev */
4419 for (i = 0; i < netdev->num_tx_queues; i++) {
4420 struct netdev_queue *q;
4421
4422 q = netdev_get_tx_queue(netdev, i);
4423 if (q)
4424 i40e_detect_recover_hung_queue(i, vsi);
4425 }
4426}
4427
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004428/**
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004429 * i40e_get_iscsi_tc_map - Return TC map for iSCSI APP
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00004430 * @pf: pointer to PF
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004431 *
4432 * Get TC map for ISCSI PF type that will include iSCSI TC
4433 * and LAN TC.
4434 **/
4435static u8 i40e_get_iscsi_tc_map(struct i40e_pf *pf)
4436{
4437 struct i40e_dcb_app_priority_table app;
4438 struct i40e_hw *hw = &pf->hw;
4439 u8 enabled_tc = 1; /* TC0 is always enabled */
4440 u8 tc, i;
4441 /* Get the iSCSI APP TLV */
4442 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
4443
4444 for (i = 0; i < dcbcfg->numapps; i++) {
4445 app = dcbcfg->app[i];
4446 if (app.selector == I40E_APP_SEL_TCPIP &&
4447 app.protocolid == I40E_APP_PROTOID_ISCSI) {
4448 tc = dcbcfg->etscfg.prioritytable[app.priority];
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04004449 enabled_tc |= BIT_ULL(tc);
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004450 break;
4451 }
4452 }
4453
4454 return enabled_tc;
4455}
4456
4457/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004458 * i40e_dcb_get_num_tc - Get the number of TCs from DCBx config
4459 * @dcbcfg: the corresponding DCBx configuration structure
4460 *
4461 * Return the number of TCs from given DCBx configuration
4462 **/
4463static u8 i40e_dcb_get_num_tc(struct i40e_dcbx_config *dcbcfg)
4464{
Jesse Brandeburg078b5872013-09-25 23:41:14 +00004465 u8 num_tc = 0;
4466 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004467
4468 /* Scan the ETS Config Priority Table to find
4469 * traffic class enabled for a given priority
4470 * and use the traffic class index to get the
4471 * number of traffic classes enabled
4472 */
4473 for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
4474 if (dcbcfg->etscfg.prioritytable[i] > num_tc)
4475 num_tc = dcbcfg->etscfg.prioritytable[i];
4476 }
4477
4478 /* Traffic class index starts from zero so
4479 * increment to return the actual count
4480 */
Jesse Brandeburg078b5872013-09-25 23:41:14 +00004481 return num_tc + 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004482}
4483
4484/**
4485 * i40e_dcb_get_enabled_tc - Get enabled traffic classes
4486 * @dcbcfg: the corresponding DCBx configuration structure
4487 *
4488 * Query the current DCB configuration and return the number of
4489 * traffic classes enabled from the given DCBX config
4490 **/
4491static u8 i40e_dcb_get_enabled_tc(struct i40e_dcbx_config *dcbcfg)
4492{
4493 u8 num_tc = i40e_dcb_get_num_tc(dcbcfg);
4494 u8 enabled_tc = 1;
4495 u8 i;
4496
4497 for (i = 0; i < num_tc; i++)
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04004498 enabled_tc |= BIT(i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004499
4500 return enabled_tc;
4501}
4502
4503/**
4504 * i40e_pf_get_num_tc - Get enabled traffic classes for PF
4505 * @pf: PF being queried
4506 *
4507 * Return number of traffic classes enabled for the given PF
4508 **/
4509static u8 i40e_pf_get_num_tc(struct i40e_pf *pf)
4510{
4511 struct i40e_hw *hw = &pf->hw;
4512 u8 i, enabled_tc;
4513 u8 num_tc = 0;
4514 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
4515
4516 /* If DCB is not enabled then always in single TC */
4517 if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
4518 return 1;
4519
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004520 /* SFP mode will be enabled for all TCs on port */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004521 if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
4522 return i40e_dcb_get_num_tc(dcbcfg);
4523
4524 /* MFP mode return count of enabled TCs for this PF */
4525 if (pf->hw.func_caps.iscsi)
4526 enabled_tc = i40e_get_iscsi_tc_map(pf);
4527 else
Neerav Parikhfc51de92015-02-24 06:58:53 +00004528 return 1; /* Only TC0 */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004529
4530 /* At least have TC0 */
4531 enabled_tc = (enabled_tc ? enabled_tc : 0x1);
4532 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04004533 if (enabled_tc & BIT_ULL(i))
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004534 num_tc++;
4535 }
4536 return num_tc;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004537}
4538
4539/**
4540 * i40e_pf_get_default_tc - Get bitmap for first enabled TC
4541 * @pf: PF being queried
4542 *
4543 * Return a bitmap for first enabled traffic class for this PF.
4544 **/
4545static u8 i40e_pf_get_default_tc(struct i40e_pf *pf)
4546{
4547 u8 enabled_tc = pf->hw.func_caps.enabled_tcmap;
4548 u8 i = 0;
4549
4550 if (!enabled_tc)
4551 return 0x1; /* TC0 */
4552
4553 /* Find the first enabled TC */
4554 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04004555 if (enabled_tc & BIT_ULL(i))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004556 break;
4557 }
4558
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04004559 return BIT(i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004560}
4561
4562/**
4563 * i40e_pf_get_pf_tc_map - Get bitmap for enabled traffic classes
4564 * @pf: PF being queried
4565 *
4566 * Return a bitmap for enabled traffic classes for this PF.
4567 **/
4568static u8 i40e_pf_get_tc_map(struct i40e_pf *pf)
4569{
4570 /* If DCB is not enabled for this PF then just return default TC */
4571 if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
4572 return i40e_pf_get_default_tc(pf);
4573
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004574 /* SFP mode we want PF to be enabled for all TCs */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004575 if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
4576 return i40e_dcb_get_enabled_tc(&pf->hw.local_dcbx_config);
4577
Neerav Parikhfc51de92015-02-24 06:58:53 +00004578 /* MFP enabled and iSCSI PF type */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004579 if (pf->hw.func_caps.iscsi)
4580 return i40e_get_iscsi_tc_map(pf);
4581 else
Neerav Parikhfc51de92015-02-24 06:58:53 +00004582 return i40e_pf_get_default_tc(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004583}
4584
4585/**
4586 * i40e_vsi_get_bw_info - Query VSI BW Information
4587 * @vsi: the VSI being queried
4588 *
4589 * Returns 0 on success, negative value on failure
4590 **/
4591static int i40e_vsi_get_bw_info(struct i40e_vsi *vsi)
4592{
4593 struct i40e_aqc_query_vsi_ets_sla_config_resp bw_ets_config = {0};
4594 struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
4595 struct i40e_pf *pf = vsi->back;
4596 struct i40e_hw *hw = &pf->hw;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004597 i40e_status ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004598 u32 tc_bw_max;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004599 int i;
4600
4601 /* Get the VSI level BW configuration */
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004602 ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid, &bw_config, NULL);
4603 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004604 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004605 "couldn't get PF vsi bw config, err %s aq_err %s\n",
4606 i40e_stat_str(&pf->hw, ret),
4607 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004608 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004609 }
4610
4611 /* Get the VSI level BW configuration per TC */
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004612 ret = i40e_aq_query_vsi_ets_sla_config(hw, vsi->seid, &bw_ets_config,
4613 NULL);
4614 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004615 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004616 "couldn't get PF vsi ets bw config, err %s aq_err %s\n",
4617 i40e_stat_str(&pf->hw, ret),
4618 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004619 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004620 }
4621
4622 if (bw_config.tc_valid_bits != bw_ets_config.tc_valid_bits) {
4623 dev_info(&pf->pdev->dev,
4624 "Enabled TCs mismatch from querying VSI BW info 0x%08x 0x%08x\n",
4625 bw_config.tc_valid_bits,
4626 bw_ets_config.tc_valid_bits);
4627 /* Still continuing */
4628 }
4629
4630 vsi->bw_limit = le16_to_cpu(bw_config.port_bw_limit);
4631 vsi->bw_max_quanta = bw_config.max_bw;
4632 tc_bw_max = le16_to_cpu(bw_ets_config.tc_bw_max[0]) |
4633 (le16_to_cpu(bw_ets_config.tc_bw_max[1]) << 16);
4634 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
4635 vsi->bw_ets_share_credits[i] = bw_ets_config.share_credits[i];
4636 vsi->bw_ets_limit_credits[i] =
4637 le16_to_cpu(bw_ets_config.credits[i]);
4638 /* 3 bits out of 4 for each TC */
4639 vsi->bw_ets_max_quanta[i] = (u8)((tc_bw_max >> (i*4)) & 0x7);
4640 }
Jesse Brandeburg078b5872013-09-25 23:41:14 +00004641
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004642 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004643}
4644
4645/**
4646 * i40e_vsi_configure_bw_alloc - Configure VSI BW allocation per TC
4647 * @vsi: the VSI being configured
4648 * @enabled_tc: TC bitmap
4649 * @bw_credits: BW shared credits per TC
4650 *
4651 * Returns 0 on success, negative value on failure
4652 **/
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004653static int i40e_vsi_configure_bw_alloc(struct i40e_vsi *vsi, u8 enabled_tc,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004654 u8 *bw_share)
4655{
4656 struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004657 i40e_status ret;
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004658 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004659
4660 bw_data.tc_valid_bits = enabled_tc;
4661 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
4662 bw_data.tc_bw_credits[i] = bw_share[i];
4663
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004664 ret = i40e_aq_config_vsi_tc_bw(&vsi->back->hw, vsi->seid, &bw_data,
4665 NULL);
4666 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004667 dev_info(&vsi->back->pdev->dev,
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004668 "AQ command Config VSI BW allocation per TC failed = %d\n",
4669 vsi->back->hw.aq.asq_last_status);
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004670 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004671 }
4672
4673 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
4674 vsi->info.qs_handle[i] = bw_data.qs_handles[i];
4675
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004676 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004677}
4678
4679/**
4680 * i40e_vsi_config_netdev_tc - Setup the netdev TC configuration
4681 * @vsi: the VSI being configured
4682 * @enabled_tc: TC map to be enabled
4683 *
4684 **/
4685static void i40e_vsi_config_netdev_tc(struct i40e_vsi *vsi, u8 enabled_tc)
4686{
4687 struct net_device *netdev = vsi->netdev;
4688 struct i40e_pf *pf = vsi->back;
4689 struct i40e_hw *hw = &pf->hw;
4690 u8 netdev_tc = 0;
4691 int i;
4692 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
4693
4694 if (!netdev)
4695 return;
4696
4697 if (!enabled_tc) {
4698 netdev_reset_tc(netdev);
4699 return;
4700 }
4701
4702 /* Set up actual enabled TCs on the VSI */
4703 if (netdev_set_num_tc(netdev, vsi->tc_config.numtc))
4704 return;
4705
4706 /* set per TC queues for the VSI */
4707 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
4708 /* Only set TC queues for enabled tcs
4709 *
4710 * e.g. For a VSI that has TC0 and TC3 enabled the
4711 * enabled_tc bitmap would be 0x00001001; the driver
4712 * will set the numtc for netdev as 2 that will be
4713 * referenced by the netdev layer as TC 0 and 1.
4714 */
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04004715 if (vsi->tc_config.enabled_tc & BIT_ULL(i))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004716 netdev_set_tc_queue(netdev,
4717 vsi->tc_config.tc_info[i].netdev_tc,
4718 vsi->tc_config.tc_info[i].qcount,
4719 vsi->tc_config.tc_info[i].qoffset);
4720 }
4721
4722 /* Assign UP2TC map for the VSI */
4723 for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
4724 /* Get the actual TC# for the UP */
4725 u8 ets_tc = dcbcfg->etscfg.prioritytable[i];
4726 /* Get the mapped netdev TC# for the UP */
4727 netdev_tc = vsi->tc_config.tc_info[ets_tc].netdev_tc;
4728 netdev_set_prio_tc_map(netdev, i, netdev_tc);
4729 }
4730}
4731
4732/**
4733 * i40e_vsi_update_queue_map - Update our copy of VSi info with new queue map
4734 * @vsi: the VSI being configured
4735 * @ctxt: the ctxt buffer returned from AQ VSI update param command
4736 **/
4737static void i40e_vsi_update_queue_map(struct i40e_vsi *vsi,
4738 struct i40e_vsi_context *ctxt)
4739{
4740 /* copy just the sections touched not the entire info
4741 * since not all sections are valid as returned by
4742 * update vsi params
4743 */
4744 vsi->info.mapping_flags = ctxt->info.mapping_flags;
4745 memcpy(&vsi->info.queue_mapping,
4746 &ctxt->info.queue_mapping, sizeof(vsi->info.queue_mapping));
4747 memcpy(&vsi->info.tc_mapping, ctxt->info.tc_mapping,
4748 sizeof(vsi->info.tc_mapping));
4749}
4750
4751/**
4752 * i40e_vsi_config_tc - Configure VSI Tx Scheduler for given TC map
4753 * @vsi: VSI to be configured
4754 * @enabled_tc: TC bitmap
4755 *
4756 * This configures a particular VSI for TCs that are mapped to the
4757 * given TC bitmap. It uses default bandwidth share for TCs across
4758 * VSIs to configure TC for a particular VSI.
4759 *
4760 * NOTE:
4761 * It is expected that the VSI queues have been quisced before calling
4762 * this function.
4763 **/
4764static int i40e_vsi_config_tc(struct i40e_vsi *vsi, u8 enabled_tc)
4765{
4766 u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
4767 struct i40e_vsi_context ctxt;
4768 int ret = 0;
4769 int i;
4770
4771 /* Check if enabled_tc is same as existing or new TCs */
4772 if (vsi->tc_config.enabled_tc == enabled_tc)
4773 return ret;
4774
4775 /* Enable ETS TCs with equal BW Share for now across all VSIs */
4776 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04004777 if (enabled_tc & BIT_ULL(i))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004778 bw_share[i] = 1;
4779 }
4780
4781 ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
4782 if (ret) {
4783 dev_info(&vsi->back->pdev->dev,
4784 "Failed configuring TC map %d for VSI %d\n",
4785 enabled_tc, vsi->seid);
4786 goto out;
4787 }
4788
4789 /* Update Queue Pairs Mapping for currently enabled UPs */
4790 ctxt.seid = vsi->seid;
4791 ctxt.pf_num = vsi->back->hw.pf_id;
4792 ctxt.vf_num = 0;
4793 ctxt.uplink_seid = vsi->uplink_seid;
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07004794 ctxt.info = vsi->info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004795 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
4796
4797 /* Update the VSI after updating the VSI queue-mapping information */
4798 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
4799 if (ret) {
4800 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004801 "Update vsi tc config failed, err %s aq_err %s\n",
4802 i40e_stat_str(&vsi->back->hw, ret),
4803 i40e_aq_str(&vsi->back->hw,
4804 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004805 goto out;
4806 }
4807 /* update the local VSI info with updated queue map */
4808 i40e_vsi_update_queue_map(vsi, &ctxt);
4809 vsi->info.valid_sections = 0;
4810
4811 /* Update current VSI BW information */
4812 ret = i40e_vsi_get_bw_info(vsi);
4813 if (ret) {
4814 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004815 "Failed updating vsi bw info, err %s aq_err %s\n",
4816 i40e_stat_str(&vsi->back->hw, ret),
4817 i40e_aq_str(&vsi->back->hw,
4818 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004819 goto out;
4820 }
4821
4822 /* Update the netdev TC setup */
4823 i40e_vsi_config_netdev_tc(vsi, enabled_tc);
4824out:
4825 return ret;
4826}
4827
4828/**
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004829 * i40e_veb_config_tc - Configure TCs for given VEB
4830 * @veb: given VEB
4831 * @enabled_tc: TC bitmap
4832 *
4833 * Configures given TC bitmap for VEB (switching) element
4834 **/
4835int i40e_veb_config_tc(struct i40e_veb *veb, u8 enabled_tc)
4836{
4837 struct i40e_aqc_configure_switching_comp_bw_config_data bw_data = {0};
4838 struct i40e_pf *pf = veb->pf;
4839 int ret = 0;
4840 int i;
4841
4842 /* No TCs or already enabled TCs just return */
4843 if (!enabled_tc || veb->enabled_tc == enabled_tc)
4844 return ret;
4845
4846 bw_data.tc_valid_bits = enabled_tc;
4847 /* bw_data.absolute_credits is not set (relative) */
4848
4849 /* Enable ETS TCs with equal BW Share for now */
4850 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04004851 if (enabled_tc & BIT_ULL(i))
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004852 bw_data.tc_bw_share_credits[i] = 1;
4853 }
4854
4855 ret = i40e_aq_config_switch_comp_bw_config(&pf->hw, veb->seid,
4856 &bw_data, NULL);
4857 if (ret) {
4858 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004859 "VEB bw config failed, err %s aq_err %s\n",
4860 i40e_stat_str(&pf->hw, ret),
4861 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004862 goto out;
4863 }
4864
4865 /* Update the BW information */
4866 ret = i40e_veb_get_bw_info(veb);
4867 if (ret) {
4868 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004869 "Failed getting veb bw config, err %s aq_err %s\n",
4870 i40e_stat_str(&pf->hw, ret),
4871 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004872 }
4873
4874out:
4875 return ret;
4876}
4877
4878#ifdef CONFIG_I40E_DCB
4879/**
4880 * i40e_dcb_reconfigure - Reconfigure all VEBs and VSIs
4881 * @pf: PF struct
4882 *
4883 * Reconfigure VEB/VSIs on a given PF; it is assumed that
4884 * the caller would've quiesce all the VSIs before calling
4885 * this function
4886 **/
4887static void i40e_dcb_reconfigure(struct i40e_pf *pf)
4888{
4889 u8 tc_map = 0;
4890 int ret;
4891 u8 v;
4892
4893 /* Enable the TCs available on PF to all VEBs */
4894 tc_map = i40e_pf_get_tc_map(pf);
4895 for (v = 0; v < I40E_MAX_VEB; v++) {
4896 if (!pf->veb[v])
4897 continue;
4898 ret = i40e_veb_config_tc(pf->veb[v], tc_map);
4899 if (ret) {
4900 dev_info(&pf->pdev->dev,
4901 "Failed configuring TC for VEB seid=%d\n",
4902 pf->veb[v]->seid);
4903 /* Will try to configure as many components */
4904 }
4905 }
4906
4907 /* Update each VSI */
Mitch Williams505682c2014-05-20 08:01:37 +00004908 for (v = 0; v < pf->num_alloc_vsi; v++) {
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004909 if (!pf->vsi[v])
4910 continue;
4911
4912 /* - Enable all TCs for the LAN VSI
Vasu Dev38e00432014-08-01 13:27:03 -07004913#ifdef I40E_FCOE
4914 * - For FCoE VSI only enable the TC configured
4915 * as per the APP TLV
4916#endif
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004917 * - For all others keep them at TC0 for now
4918 */
4919 if (v == pf->lan_vsi)
4920 tc_map = i40e_pf_get_tc_map(pf);
4921 else
4922 tc_map = i40e_pf_get_default_tc(pf);
Vasu Dev38e00432014-08-01 13:27:03 -07004923#ifdef I40E_FCOE
4924 if (pf->vsi[v]->type == I40E_VSI_FCOE)
4925 tc_map = i40e_get_fcoe_tc_map(pf);
4926#endif /* #ifdef I40E_FCOE */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004927
4928 ret = i40e_vsi_config_tc(pf->vsi[v], tc_map);
4929 if (ret) {
4930 dev_info(&pf->pdev->dev,
4931 "Failed configuring TC for VSI seid=%d\n",
4932 pf->vsi[v]->seid);
4933 /* Will try to configure as many components */
4934 } else {
Neerav Parikh0672a092014-04-01 07:11:47 +00004935 /* Re-configure VSI vectors based on updated TC map */
4936 i40e_vsi_map_rings_to_vectors(pf->vsi[v]);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004937 if (pf->vsi[v]->netdev)
4938 i40e_dcbnl_set_all(pf->vsi[v]);
4939 }
4940 }
4941}
4942
4943/**
Neerav Parikh2fd75f32014-11-12 00:18:20 +00004944 * i40e_resume_port_tx - Resume port Tx
4945 * @pf: PF struct
4946 *
4947 * Resume a port's Tx and issue a PF reset in case of failure to
4948 * resume.
4949 **/
4950static int i40e_resume_port_tx(struct i40e_pf *pf)
4951{
4952 struct i40e_hw *hw = &pf->hw;
4953 int ret;
4954
4955 ret = i40e_aq_resume_port_tx(hw, NULL);
4956 if (ret) {
4957 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004958 "Resume Port Tx failed, err %s aq_err %s\n",
4959 i40e_stat_str(&pf->hw, ret),
4960 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh2fd75f32014-11-12 00:18:20 +00004961 /* Schedule PF reset to recover */
4962 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
4963 i40e_service_event_schedule(pf);
4964 }
4965
4966 return ret;
4967}
4968
4969/**
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004970 * i40e_init_pf_dcb - Initialize DCB configuration
4971 * @pf: PF being configured
4972 *
4973 * Query the current DCB configuration and cache it
4974 * in the hardware structure
4975 **/
4976static int i40e_init_pf_dcb(struct i40e_pf *pf)
4977{
4978 struct i40e_hw *hw = &pf->hw;
4979 int err = 0;
4980
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +00004981 /* Do not enable DCB for SW1 and SW2 images even if the FW is capable */
4982 if (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 33)) ||
4983 (pf->hw.aq.fw_maj_ver < 4))
4984 goto out;
4985
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004986 /* Get the initial DCB configuration */
4987 err = i40e_init_dcb(hw);
4988 if (!err) {
4989 /* Device/Function is not DCBX capable */
4990 if ((!hw->func_caps.dcb) ||
4991 (hw->dcbx_status == I40E_DCBX_STATUS_DISABLED)) {
4992 dev_info(&pf->pdev->dev,
4993 "DCBX offload is not supported or is disabled for this PF.\n");
4994
4995 if (pf->flags & I40E_FLAG_MFP_ENABLED)
4996 goto out;
4997
4998 } else {
4999 /* When status is not DISABLED then DCBX in FW */
5000 pf->dcbx_cap = DCB_CAP_DCBX_LLD_MANAGED |
5001 DCB_CAP_DCBX_VER_IEEE;
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005002
5003 pf->flags |= I40E_FLAG_DCB_CAPABLE;
5004 /* Enable DCB tagging only when more than one TC */
5005 if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
5006 pf->flags |= I40E_FLAG_DCB_ENABLED;
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005007 dev_dbg(&pf->pdev->dev,
5008 "DCBX offload is supported for this PF.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005009 }
Neerav Parikh014269f2014-04-01 07:11:48 +00005010 } else {
Shannon Nelsonaebfc812014-12-11 07:06:38 +00005011 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005012 "Query for DCB configuration failed, err %s aq_err %s\n",
5013 i40e_stat_str(&pf->hw, err),
5014 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005015 }
5016
5017out:
5018 return err;
5019}
5020#endif /* CONFIG_I40E_DCB */
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005021#define SPEED_SIZE 14
5022#define FC_SIZE 8
5023/**
5024 * i40e_print_link_message - print link up or down
5025 * @vsi: the VSI for which link needs a message
5026 */
Matt Jaredc156f852015-08-27 11:42:39 -04005027void i40e_print_link_message(struct i40e_vsi *vsi, bool isup)
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005028{
Shannon Nelsona9165492015-09-03 17:19:00 -04005029 char *speed = "Unknown";
5030 char *fc = "Unknown";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005031
Matt Jaredc156f852015-08-27 11:42:39 -04005032 if (vsi->current_isup == isup)
5033 return;
5034 vsi->current_isup = isup;
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005035 if (!isup) {
5036 netdev_info(vsi->netdev, "NIC Link is Down\n");
5037 return;
5038 }
5039
Greg Rose148c2d82014-12-11 07:06:27 +00005040 /* Warn user if link speed on NPAR enabled partition is not at
5041 * least 10GB
5042 */
5043 if (vsi->back->hw.func_caps.npar_enable &&
5044 (vsi->back->hw.phy.link_info.link_speed == I40E_LINK_SPEED_1GB ||
5045 vsi->back->hw.phy.link_info.link_speed == I40E_LINK_SPEED_100MB))
5046 netdev_warn(vsi->netdev,
5047 "The partition detected link speed that is less than 10Gbps\n");
5048
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005049 switch (vsi->back->hw.phy.link_info.link_speed) {
5050 case I40E_LINK_SPEED_40GB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005051 speed = "40 G";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005052 break;
Jesse Brandeburgae24b402015-03-27 00:12:09 -07005053 case I40E_LINK_SPEED_20GB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005054 speed = "20 G";
Jesse Brandeburgae24b402015-03-27 00:12:09 -07005055 break;
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005056 case I40E_LINK_SPEED_10GB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005057 speed = "10 G";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005058 break;
5059 case I40E_LINK_SPEED_1GB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005060 speed = "1000 M";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005061 break;
Mitch Williams5960d332014-09-13 07:40:47 +00005062 case I40E_LINK_SPEED_100MB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005063 speed = "100 M";
Mitch Williams5960d332014-09-13 07:40:47 +00005064 break;
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005065 default:
5066 break;
5067 }
5068
5069 switch (vsi->back->hw.fc.current_mode) {
5070 case I40E_FC_FULL:
Shannon Nelsona9165492015-09-03 17:19:00 -04005071 fc = "RX/TX";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005072 break;
5073 case I40E_FC_TX_PAUSE:
Shannon Nelsona9165492015-09-03 17:19:00 -04005074 fc = "TX";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005075 break;
5076 case I40E_FC_RX_PAUSE:
Shannon Nelsona9165492015-09-03 17:19:00 -04005077 fc = "RX";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005078 break;
5079 default:
Shannon Nelsona9165492015-09-03 17:19:00 -04005080 fc = "None";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005081 break;
5082 }
5083
Shannon Nelsona9165492015-09-03 17:19:00 -04005084 netdev_info(vsi->netdev, "NIC Link is Up %sbps Full Duplex, Flow Control: %s\n",
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005085 speed, fc);
5086}
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005087
5088/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005089 * i40e_up_complete - Finish the last steps of bringing up a connection
5090 * @vsi: the VSI being configured
5091 **/
5092static int i40e_up_complete(struct i40e_vsi *vsi)
5093{
5094 struct i40e_pf *pf = vsi->back;
5095 int err;
5096
5097 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
5098 i40e_vsi_configure_msix(vsi);
5099 else
5100 i40e_configure_msi_and_legacy(vsi);
5101
5102 /* start rings */
5103 err = i40e_vsi_control_rings(vsi, true);
5104 if (err)
5105 return err;
5106
5107 clear_bit(__I40E_DOWN, &vsi->state);
5108 i40e_napi_enable_all(vsi);
5109 i40e_vsi_enable_irq(vsi);
5110
5111 if ((pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP) &&
5112 (vsi->netdev)) {
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005113 i40e_print_link_message(vsi, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005114 netif_tx_start_all_queues(vsi->netdev);
5115 netif_carrier_on(vsi->netdev);
Anjali Singhai6d779b42013-09-28 06:00:02 +00005116 } else if (vsi->netdev) {
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005117 i40e_print_link_message(vsi, false);
Carolyn Wyborny7b592f62014-07-10 07:58:19 +00005118 /* need to check for qualified module here*/
5119 if ((pf->hw.phy.link_info.link_info &
5120 I40E_AQ_MEDIA_AVAILABLE) &&
5121 (!(pf->hw.phy.link_info.an_info &
5122 I40E_AQ_QUALIFIED_MODULE)))
5123 netdev_err(vsi->netdev,
5124 "the driver failed to link because an unqualified module was detected.");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005125 }
Anjali Singhai Jainca64fa42014-02-11 08:26:30 +00005126
5127 /* replay FDIR SB filters */
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005128 if (vsi->type == I40E_VSI_FDIR) {
5129 /* reset fd counters */
5130 pf->fd_add_err = pf->fd_atr_cnt = 0;
5131 if (pf->fd_tcp_rule > 0) {
5132 pf->flags &= ~I40E_FLAG_FD_ATR_ENABLED;
Anjali Singhai Jain2e4875e2015-04-16 20:06:06 -04005133 if (I40E_DEBUG_FD & pf->hw.debug_mask)
5134 dev_info(&pf->pdev->dev, "Forcing ATR off, sideband rules for TCP/IPv4 exist\n");
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005135 pf->fd_tcp_rule = 0;
5136 }
Anjali Singhai Jainca64fa42014-02-11 08:26:30 +00005137 i40e_fdir_filter_restore(vsi);
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005138 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005139 i40e_service_event_schedule(pf);
5140
5141 return 0;
5142}
5143
5144/**
5145 * i40e_vsi_reinit_locked - Reset the VSI
5146 * @vsi: the VSI being configured
5147 *
5148 * Rebuild the ring structs after some configuration
5149 * has changed, e.g. MTU size.
5150 **/
5151static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi)
5152{
5153 struct i40e_pf *pf = vsi->back;
5154
5155 WARN_ON(in_interrupt());
5156 while (test_and_set_bit(__I40E_CONFIG_BUSY, &pf->state))
5157 usleep_range(1000, 2000);
5158 i40e_down(vsi);
5159
5160 /* Give a VF some time to respond to the reset. The
5161 * two second wait is based upon the watchdog cycle in
5162 * the VF driver.
5163 */
5164 if (vsi->type == I40E_VSI_SRIOV)
5165 msleep(2000);
5166 i40e_up(vsi);
5167 clear_bit(__I40E_CONFIG_BUSY, &pf->state);
5168}
5169
5170/**
5171 * i40e_up - Bring the connection back up after being down
5172 * @vsi: the VSI being configured
5173 **/
5174int i40e_up(struct i40e_vsi *vsi)
5175{
5176 int err;
5177
5178 err = i40e_vsi_configure(vsi);
5179 if (!err)
5180 err = i40e_up_complete(vsi);
5181
5182 return err;
5183}
5184
5185/**
5186 * i40e_down - Shutdown the connection processing
5187 * @vsi: the VSI being stopped
5188 **/
5189void i40e_down(struct i40e_vsi *vsi)
5190{
5191 int i;
5192
5193 /* It is assumed that the caller of this function
5194 * sets the vsi->state __I40E_DOWN bit.
5195 */
5196 if (vsi->netdev) {
5197 netif_carrier_off(vsi->netdev);
5198 netif_tx_disable(vsi->netdev);
5199 }
5200 i40e_vsi_disable_irq(vsi);
5201 i40e_vsi_control_rings(vsi, false);
5202 i40e_napi_disable_all(vsi);
5203
5204 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00005205 i40e_clean_tx_ring(vsi->tx_rings[i]);
5206 i40e_clean_rx_ring(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005207 }
5208}
5209
5210/**
5211 * i40e_setup_tc - configure multiple traffic classes
5212 * @netdev: net device to configure
5213 * @tc: number of traffic classes to enable
5214 **/
Vasu Dev38e00432014-08-01 13:27:03 -07005215#ifdef I40E_FCOE
5216int i40e_setup_tc(struct net_device *netdev, u8 tc)
5217#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005218static int i40e_setup_tc(struct net_device *netdev, u8 tc)
Vasu Dev38e00432014-08-01 13:27:03 -07005219#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005220{
5221 struct i40e_netdev_priv *np = netdev_priv(netdev);
5222 struct i40e_vsi *vsi = np->vsi;
5223 struct i40e_pf *pf = vsi->back;
5224 u8 enabled_tc = 0;
5225 int ret = -EINVAL;
5226 int i;
5227
5228 /* Check if DCB enabled to continue */
5229 if (!(pf->flags & I40E_FLAG_DCB_ENABLED)) {
5230 netdev_info(netdev, "DCB is not enabled for adapter\n");
5231 goto exit;
5232 }
5233
5234 /* Check if MFP enabled */
5235 if (pf->flags & I40E_FLAG_MFP_ENABLED) {
5236 netdev_info(netdev, "Configuring TC not supported in MFP mode\n");
5237 goto exit;
5238 }
5239
5240 /* Check whether tc count is within enabled limit */
5241 if (tc > i40e_pf_get_num_tc(pf)) {
5242 netdev_info(netdev, "TC count greater than enabled on link for adapter\n");
5243 goto exit;
5244 }
5245
5246 /* Generate TC map for number of tc requested */
5247 for (i = 0; i < tc; i++)
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005248 enabled_tc |= BIT_ULL(i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005249
5250 /* Requesting same TC configuration as already enabled */
5251 if (enabled_tc == vsi->tc_config.enabled_tc)
5252 return 0;
5253
5254 /* Quiesce VSI queues */
5255 i40e_quiesce_vsi(vsi);
5256
5257 /* Configure VSI for enabled TCs */
5258 ret = i40e_vsi_config_tc(vsi, enabled_tc);
5259 if (ret) {
5260 netdev_info(netdev, "Failed configuring TC for VSI seid=%d\n",
5261 vsi->seid);
5262 goto exit;
5263 }
5264
5265 /* Unquiesce VSI */
5266 i40e_unquiesce_vsi(vsi);
5267
5268exit:
5269 return ret;
5270}
5271
5272/**
5273 * i40e_open - Called when a network interface is made active
5274 * @netdev: network interface device structure
5275 *
5276 * The open entry point is called when a network interface is made
5277 * active by the system (IFF_UP). At this point all resources needed
5278 * for transmit and receive operations are allocated, the interrupt
5279 * handler is registered with the OS, the netdev watchdog subtask is
5280 * enabled, and the stack is notified that the interface is ready.
5281 *
5282 * Returns 0 on success, negative value on failure
5283 **/
Vasu Dev38e00432014-08-01 13:27:03 -07005284int i40e_open(struct net_device *netdev)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005285{
5286 struct i40e_netdev_priv *np = netdev_priv(netdev);
5287 struct i40e_vsi *vsi = np->vsi;
5288 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005289 int err;
5290
Shannon Nelson4eb3f762014-03-06 08:59:58 +00005291 /* disallow open during test or if eeprom is broken */
5292 if (test_bit(__I40E_TESTING, &pf->state) ||
5293 test_bit(__I40E_BAD_EEPROM, &pf->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005294 return -EBUSY;
5295
5296 netif_carrier_off(netdev);
5297
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005298 err = i40e_vsi_open(vsi);
5299 if (err)
5300 return err;
5301
Jesse Brandeburg059dab62014-04-01 09:07:20 +00005302 /* configure global TSO hardware offload settings */
5303 wr32(&pf->hw, I40E_GLLAN_TSOMSK_F, be32_to_cpu(TCP_FLAG_PSH |
5304 TCP_FLAG_FIN) >> 16);
5305 wr32(&pf->hw, I40E_GLLAN_TSOMSK_M, be32_to_cpu(TCP_FLAG_PSH |
5306 TCP_FLAG_FIN |
5307 TCP_FLAG_CWR) >> 16);
5308 wr32(&pf->hw, I40E_GLLAN_TSOMSK_L, be32_to_cpu(TCP_FLAG_CWR) >> 16);
5309
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005310#ifdef CONFIG_I40E_VXLAN
5311 vxlan_get_rx_port(netdev);
5312#endif
5313
5314 return 0;
5315}
5316
5317/**
5318 * i40e_vsi_open -
5319 * @vsi: the VSI to open
5320 *
5321 * Finish initialization of the VSI.
5322 *
5323 * Returns 0 on success, negative value on failure
5324 **/
5325int i40e_vsi_open(struct i40e_vsi *vsi)
5326{
5327 struct i40e_pf *pf = vsi->back;
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00005328 char int_name[I40E_INT_NAME_STR_LEN];
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005329 int err;
5330
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005331 /* allocate descriptors */
5332 err = i40e_vsi_setup_tx_resources(vsi);
5333 if (err)
5334 goto err_setup_tx;
5335 err = i40e_vsi_setup_rx_resources(vsi);
5336 if (err)
5337 goto err_setup_rx;
5338
5339 err = i40e_vsi_configure(vsi);
5340 if (err)
5341 goto err_setup_rx;
5342
Shannon Nelsonc22e3c62014-03-14 07:32:25 +00005343 if (vsi->netdev) {
5344 snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
5345 dev_driver_string(&pf->pdev->dev), vsi->netdev->name);
5346 err = i40e_vsi_request_irq(vsi, int_name);
5347 if (err)
5348 goto err_setup_rx;
5349
5350 /* Notify the stack of the actual queue counts. */
5351 err = netif_set_real_num_tx_queues(vsi->netdev,
5352 vsi->num_queue_pairs);
5353 if (err)
5354 goto err_set_queues;
5355
5356 err = netif_set_real_num_rx_queues(vsi->netdev,
5357 vsi->num_queue_pairs);
5358 if (err)
5359 goto err_set_queues;
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00005360
5361 } else if (vsi->type == I40E_VSI_FDIR) {
Carolyn Wybornye240f672014-12-11 07:06:37 +00005362 snprintf(int_name, sizeof(int_name) - 1, "%s-%s:fdir",
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +00005363 dev_driver_string(&pf->pdev->dev),
5364 dev_name(&pf->pdev->dev));
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00005365 err = i40e_vsi_request_irq(vsi, int_name);
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +00005366
Shannon Nelsonc22e3c62014-03-14 07:32:25 +00005367 } else {
Jean Sacrence9ccb12014-05-01 14:31:18 +00005368 err = -EINVAL;
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005369 goto err_setup_rx;
5370 }
Anjali Singhai Jain25946dd2013-11-26 10:49:14 +00005371
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005372 err = i40e_up_complete(vsi);
5373 if (err)
5374 goto err_up_complete;
5375
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005376 return 0;
5377
5378err_up_complete:
5379 i40e_down(vsi);
Anjali Singhai Jain25946dd2013-11-26 10:49:14 +00005380err_set_queues:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005381 i40e_vsi_free_irq(vsi);
5382err_setup_rx:
5383 i40e_vsi_free_rx_resources(vsi);
5384err_setup_tx:
5385 i40e_vsi_free_tx_resources(vsi);
5386 if (vsi == pf->vsi[pf->lan_vsi])
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005387 i40e_do_reset(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005388
5389 return err;
5390}
5391
5392/**
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00005393 * i40e_fdir_filter_exit - Cleans up the Flow Director accounting
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00005394 * @pf: Pointer to PF
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00005395 *
5396 * This function destroys the hlist where all the Flow Director
5397 * filters were saved.
5398 **/
5399static void i40e_fdir_filter_exit(struct i40e_pf *pf)
5400{
5401 struct i40e_fdir_filter *filter;
5402 struct hlist_node *node2;
5403
5404 hlist_for_each_entry_safe(filter, node2,
5405 &pf->fdir_filter_list, fdir_node) {
5406 hlist_del(&filter->fdir_node);
5407 kfree(filter);
5408 }
5409 pf->fdir_pf_active_filters = 0;
5410}
5411
5412/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005413 * i40e_close - Disables a network interface
5414 * @netdev: network interface device structure
5415 *
5416 * The close entry point is called when an interface is de-activated
5417 * by the OS. The hardware is still under the driver's control, but
5418 * this netdev interface is disabled.
5419 *
5420 * Returns 0, this is not allowed to fail
5421 **/
Vasu Dev38e00432014-08-01 13:27:03 -07005422#ifdef I40E_FCOE
5423int i40e_close(struct net_device *netdev)
5424#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005425static int i40e_close(struct net_device *netdev)
Vasu Dev38e00432014-08-01 13:27:03 -07005426#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005427{
5428 struct i40e_netdev_priv *np = netdev_priv(netdev);
5429 struct i40e_vsi *vsi = np->vsi;
5430
Shannon Nelson90ef8d42014-03-14 07:32:26 +00005431 i40e_vsi_close(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005432
5433 return 0;
5434}
5435
5436/**
5437 * i40e_do_reset - Start a PF or Core Reset sequence
5438 * @pf: board private structure
5439 * @reset_flags: which reset is requested
5440 *
5441 * The essential difference in resets is that the PF Reset
5442 * doesn't clear the packet buffers, doesn't reset the PE
5443 * firmware, and doesn't bother the other PFs on the chip.
5444 **/
5445void i40e_do_reset(struct i40e_pf *pf, u32 reset_flags)
5446{
5447 u32 val;
5448
5449 WARN_ON(in_interrupt());
5450
Mitch Williams263fc482014-04-23 04:50:11 +00005451 if (i40e_check_asq_alive(&pf->hw))
5452 i40e_vc_notify_reset(pf);
5453
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005454 /* do the biggest reset indicated */
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005455 if (reset_flags & BIT_ULL(__I40E_GLOBAL_RESET_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005456
5457 /* Request a Global Reset
5458 *
5459 * This will start the chip's countdown to the actual full
5460 * chip reset event, and a warning interrupt to be sent
5461 * to all PFs, including the requestor. Our handler
5462 * for the warning interrupt will deal with the shutdown
5463 * and recovery of the switch setup.
5464 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005465 dev_dbg(&pf->pdev->dev, "GlobalR requested\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005466 val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
5467 val |= I40E_GLGEN_RTRIG_GLOBR_MASK;
5468 wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
5469
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005470 } else if (reset_flags & BIT_ULL(__I40E_CORE_RESET_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005471
5472 /* Request a Core Reset
5473 *
5474 * Same as Global Reset, except does *not* include the MAC/PHY
5475 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005476 dev_dbg(&pf->pdev->dev, "CoreR requested\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005477 val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
5478 val |= I40E_GLGEN_RTRIG_CORER_MASK;
5479 wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
5480 i40e_flush(&pf->hw);
5481
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005482 } else if (reset_flags & BIT_ULL(__I40E_PF_RESET_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005483
5484 /* Request a PF Reset
5485 *
5486 * Resets only the PF-specific registers
5487 *
5488 * This goes directly to the tear-down and rebuild of
5489 * the switch, since we need to do all the recovery as
5490 * for the Core Reset.
5491 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005492 dev_dbg(&pf->pdev->dev, "PFR requested\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005493 i40e_handle_reset_warning(pf);
5494
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005495 } else if (reset_flags & BIT_ULL(__I40E_REINIT_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005496 int v;
5497
5498 /* Find the VSI(s) that requested a re-init */
5499 dev_info(&pf->pdev->dev,
5500 "VSI reinit requested\n");
Mitch Williams505682c2014-05-20 08:01:37 +00005501 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005502 struct i40e_vsi *vsi = pf->vsi[v];
Jesse Brandeburg6995b362015-08-28 17:55:54 -04005503
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005504 if (vsi != NULL &&
5505 test_bit(__I40E_REINIT_REQUESTED, &vsi->state)) {
5506 i40e_vsi_reinit_locked(pf->vsi[v]);
5507 clear_bit(__I40E_REINIT_REQUESTED, &vsi->state);
5508 }
5509 }
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005510 } else if (reset_flags & BIT_ULL(__I40E_DOWN_REQUESTED)) {
Neerav Parikhb5d06f02014-06-03 23:50:17 +00005511 int v;
5512
5513 /* Find the VSI(s) that needs to be brought down */
5514 dev_info(&pf->pdev->dev, "VSI down requested\n");
5515 for (v = 0; v < pf->num_alloc_vsi; v++) {
5516 struct i40e_vsi *vsi = pf->vsi[v];
Jesse Brandeburg6995b362015-08-28 17:55:54 -04005517
Neerav Parikhb5d06f02014-06-03 23:50:17 +00005518 if (vsi != NULL &&
5519 test_bit(__I40E_DOWN_REQUESTED, &vsi->state)) {
5520 set_bit(__I40E_DOWN, &vsi->state);
5521 i40e_down(vsi);
5522 clear_bit(__I40E_DOWN_REQUESTED, &vsi->state);
5523 }
5524 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005525 } else {
5526 dev_info(&pf->pdev->dev,
5527 "bad reset request 0x%08x\n", reset_flags);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005528 }
5529}
5530
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005531#ifdef CONFIG_I40E_DCB
5532/**
5533 * i40e_dcb_need_reconfig - Check if DCB needs reconfig
5534 * @pf: board private structure
5535 * @old_cfg: current DCB config
5536 * @new_cfg: new DCB config
5537 **/
5538bool i40e_dcb_need_reconfig(struct i40e_pf *pf,
5539 struct i40e_dcbx_config *old_cfg,
5540 struct i40e_dcbx_config *new_cfg)
5541{
5542 bool need_reconfig = false;
5543
5544 /* Check if ETS configuration has changed */
5545 if (memcmp(&new_cfg->etscfg,
5546 &old_cfg->etscfg,
5547 sizeof(new_cfg->etscfg))) {
5548 /* If Priority Table has changed reconfig is needed */
5549 if (memcmp(&new_cfg->etscfg.prioritytable,
5550 &old_cfg->etscfg.prioritytable,
5551 sizeof(new_cfg->etscfg.prioritytable))) {
5552 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005553 dev_dbg(&pf->pdev->dev, "ETS UP2TC changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005554 }
5555
5556 if (memcmp(&new_cfg->etscfg.tcbwtable,
5557 &old_cfg->etscfg.tcbwtable,
5558 sizeof(new_cfg->etscfg.tcbwtable)))
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005559 dev_dbg(&pf->pdev->dev, "ETS TC BW Table changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005560
5561 if (memcmp(&new_cfg->etscfg.tsatable,
5562 &old_cfg->etscfg.tsatable,
5563 sizeof(new_cfg->etscfg.tsatable)))
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005564 dev_dbg(&pf->pdev->dev, "ETS TSA Table changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005565 }
5566
5567 /* Check if PFC configuration has changed */
5568 if (memcmp(&new_cfg->pfc,
5569 &old_cfg->pfc,
5570 sizeof(new_cfg->pfc))) {
5571 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005572 dev_dbg(&pf->pdev->dev, "PFC config change detected.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005573 }
5574
5575 /* Check if APP Table has changed */
5576 if (memcmp(&new_cfg->app,
5577 &old_cfg->app,
Dave Jones3d9667a2014-01-27 23:11:09 -05005578 sizeof(new_cfg->app))) {
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005579 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005580 dev_dbg(&pf->pdev->dev, "APP Table change detected.\n");
Dave Jones3d9667a2014-01-27 23:11:09 -05005581 }
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005582
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04005583 dev_dbg(&pf->pdev->dev, "dcb need_reconfig=%d\n", need_reconfig);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005584 return need_reconfig;
5585}
5586
5587/**
5588 * i40e_handle_lldp_event - Handle LLDP Change MIB event
5589 * @pf: board private structure
5590 * @e: event info posted on ARQ
5591 **/
5592static int i40e_handle_lldp_event(struct i40e_pf *pf,
5593 struct i40e_arq_event_info *e)
5594{
5595 struct i40e_aqc_lldp_get_mib *mib =
5596 (struct i40e_aqc_lldp_get_mib *)&e->desc.params.raw;
5597 struct i40e_hw *hw = &pf->hw;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005598 struct i40e_dcbx_config tmp_dcbx_cfg;
5599 bool need_reconfig = false;
5600 int ret = 0;
5601 u8 type;
5602
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005603 /* Not DCB capable or capability disabled */
5604 if (!(pf->flags & I40E_FLAG_DCB_CAPABLE))
5605 return ret;
5606
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005607 /* Ignore if event is not for Nearest Bridge */
5608 type = ((mib->type >> I40E_AQ_LLDP_BRIDGE_TYPE_SHIFT)
5609 & I40E_AQ_LLDP_BRIDGE_TYPE_MASK);
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04005610 dev_dbg(&pf->pdev->dev, "LLDP event mib bridge type 0x%x\n", type);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005611 if (type != I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE)
5612 return ret;
5613
5614 /* Check MIB Type and return if event for Remote MIB update */
5615 type = mib->type & I40E_AQ_LLDP_MIB_TYPE_MASK;
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005616 dev_dbg(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04005617 "LLDP event mib type %s\n", type ? "remote" : "local");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005618 if (type == I40E_AQ_LLDP_MIB_REMOTE) {
5619 /* Update the remote cached instance and return */
5620 ret = i40e_aq_get_dcb_config(hw, I40E_AQ_LLDP_MIB_REMOTE,
5621 I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE,
5622 &hw->remote_dcbx_config);
5623 goto exit;
5624 }
5625
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005626 /* Store the old configuration */
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07005627 tmp_dcbx_cfg = hw->local_dcbx_config;
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005628
Neerav Parikh750fcbc2015-02-24 06:58:47 +00005629 /* Reset the old DCBx configuration data */
5630 memset(&hw->local_dcbx_config, 0, sizeof(hw->local_dcbx_config));
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005631 /* Get updated DCBX data from firmware */
5632 ret = i40e_get_dcb_config(&pf->hw);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005633 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005634 dev_info(&pf->pdev->dev,
5635 "Failed querying DCB configuration data from firmware, err %s aq_err %s\n",
5636 i40e_stat_str(&pf->hw, ret),
5637 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005638 goto exit;
5639 }
5640
5641 /* No change detected in DCBX configs */
Neerav Parikh750fcbc2015-02-24 06:58:47 +00005642 if (!memcmp(&tmp_dcbx_cfg, &hw->local_dcbx_config,
5643 sizeof(tmp_dcbx_cfg))) {
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005644 dev_dbg(&pf->pdev->dev, "No change detected in DCBX configuration.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005645 goto exit;
5646 }
5647
Neerav Parikh750fcbc2015-02-24 06:58:47 +00005648 need_reconfig = i40e_dcb_need_reconfig(pf, &tmp_dcbx_cfg,
5649 &hw->local_dcbx_config);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005650
Neerav Parikh750fcbc2015-02-24 06:58:47 +00005651 i40e_dcbnl_flush_apps(pf, &tmp_dcbx_cfg, &hw->local_dcbx_config);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005652
5653 if (!need_reconfig)
5654 goto exit;
5655
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005656 /* Enable DCB tagging only when more than one TC */
Neerav Parikh750fcbc2015-02-24 06:58:47 +00005657 if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005658 pf->flags |= I40E_FLAG_DCB_ENABLED;
5659 else
5660 pf->flags &= ~I40E_FLAG_DCB_ENABLED;
5661
Neerav Parikh69129dc2014-11-12 00:18:46 +00005662 set_bit(__I40E_PORT_TX_SUSPENDED, &pf->state);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005663 /* Reconfiguration needed quiesce all VSIs */
5664 i40e_pf_quiesce_all_vsi(pf);
5665
5666 /* Changes in configuration update VEB/VSI */
5667 i40e_dcb_reconfigure(pf);
5668
Neerav Parikh2fd75f32014-11-12 00:18:20 +00005669 ret = i40e_resume_port_tx(pf);
5670
Neerav Parikh69129dc2014-11-12 00:18:46 +00005671 clear_bit(__I40E_PORT_TX_SUSPENDED, &pf->state);
Neerav Parikh2fd75f32014-11-12 00:18:20 +00005672 /* In case of error no point in resuming VSIs */
Neerav Parikh69129dc2014-11-12 00:18:46 +00005673 if (ret)
5674 goto exit;
5675
5676 /* Wait for the PF's Tx queues to be disabled */
5677 ret = i40e_pf_wait_txq_disabled(pf);
Parikh, Neerav11e47702015-02-21 06:43:55 +00005678 if (ret) {
5679 /* Schedule PF reset to recover */
5680 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
5681 i40e_service_event_schedule(pf);
5682 } else {
Neerav Parikh2fd75f32014-11-12 00:18:20 +00005683 i40e_pf_unquiesce_all_vsi(pf);
Parikh, Neerav11e47702015-02-21 06:43:55 +00005684 }
5685
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005686exit:
5687 return ret;
5688}
5689#endif /* CONFIG_I40E_DCB */
5690
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005691/**
Anjali Singhai Jain23326182013-11-26 10:49:22 +00005692 * i40e_do_reset_safe - Protected reset path for userland calls.
5693 * @pf: board private structure
5694 * @reset_flags: which reset is requested
5695 *
5696 **/
5697void i40e_do_reset_safe(struct i40e_pf *pf, u32 reset_flags)
5698{
5699 rtnl_lock();
5700 i40e_do_reset(pf, reset_flags);
5701 rtnl_unlock();
5702}
5703
5704/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005705 * i40e_handle_lan_overflow_event - Handler for LAN queue overflow event
5706 * @pf: board private structure
5707 * @e: event info posted on ARQ
5708 *
5709 * Handler for LAN Queue Overflow Event generated by the firmware for PF
5710 * and VF queues
5711 **/
5712static void i40e_handle_lan_overflow_event(struct i40e_pf *pf,
5713 struct i40e_arq_event_info *e)
5714{
5715 struct i40e_aqc_lan_overflow *data =
5716 (struct i40e_aqc_lan_overflow *)&e->desc.params.raw;
5717 u32 queue = le32_to_cpu(data->prtdcb_rupto);
5718 u32 qtx_ctl = le32_to_cpu(data->otx_ctl);
5719 struct i40e_hw *hw = &pf->hw;
5720 struct i40e_vf *vf;
5721 u16 vf_id;
5722
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005723 dev_dbg(&pf->pdev->dev, "overflow Rx Queue Number = %d QTX_CTL=0x%08x\n",
5724 queue, qtx_ctl);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005725
5726 /* Queue belongs to VF, find the VF and issue VF reset */
5727 if (((qtx_ctl & I40E_QTX_CTL_PFVF_Q_MASK)
5728 >> I40E_QTX_CTL_PFVF_Q_SHIFT) == I40E_QTX_CTL_VF_QUEUE) {
5729 vf_id = (u16)((qtx_ctl & I40E_QTX_CTL_VFVM_INDX_MASK)
5730 >> I40E_QTX_CTL_VFVM_INDX_SHIFT);
5731 vf_id -= hw->func_caps.vf_base_id;
5732 vf = &pf->vf[vf_id];
5733 i40e_vc_notify_vf_reset(vf);
5734 /* Allow VF to process pending reset notification */
5735 msleep(20);
5736 i40e_reset_vf(vf, false);
5737 }
5738}
5739
5740/**
5741 * i40e_service_event_complete - Finish up the service event
5742 * @pf: board private structure
5743 **/
5744static void i40e_service_event_complete(struct i40e_pf *pf)
5745{
Shannon Nelsonb875f992015-10-21 19:47:03 -04005746 WARN_ON(!test_bit(__I40E_SERVICE_SCHED, &pf->state));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005747
5748 /* flush memory to make sure state is correct before next watchog */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01005749 smp_mb__before_atomic();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005750 clear_bit(__I40E_SERVICE_SCHED, &pf->state);
5751}
5752
5753/**
Anjali Singhai Jain12957382014-06-04 04:22:47 +00005754 * i40e_get_cur_guaranteed_fd_count - Get the consumed guaranteed FD filters
5755 * @pf: board private structure
5756 **/
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005757u32 i40e_get_cur_guaranteed_fd_count(struct i40e_pf *pf)
Anjali Singhai Jain12957382014-06-04 04:22:47 +00005758{
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005759 u32 val, fcnt_prog;
Anjali Singhai Jain12957382014-06-04 04:22:47 +00005760
5761 val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
5762 fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK);
5763 return fcnt_prog;
5764}
5765
5766/**
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005767 * i40e_get_current_fd_count - Get total FD filters programmed for this PF
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005768 * @pf: board private structure
5769 **/
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005770u32 i40e_get_current_fd_count(struct i40e_pf *pf)
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005771{
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005772 u32 val, fcnt_prog;
5773
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005774 val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
5775 fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK) +
5776 ((val & I40E_PFQF_FDSTAT_BEST_CNT_MASK) >>
5777 I40E_PFQF_FDSTAT_BEST_CNT_SHIFT);
5778 return fcnt_prog;
5779}
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005780
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005781/**
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005782 * i40e_get_global_fd_count - Get total FD filters programmed on device
5783 * @pf: board private structure
5784 **/
5785u32 i40e_get_global_fd_count(struct i40e_pf *pf)
5786{
5787 u32 val, fcnt_prog;
5788
5789 val = rd32(&pf->hw, I40E_GLQF_FDCNT_0);
5790 fcnt_prog = (val & I40E_GLQF_FDCNT_0_GUARANT_CNT_MASK) +
5791 ((val & I40E_GLQF_FDCNT_0_BESTCNT_MASK) >>
5792 I40E_GLQF_FDCNT_0_BESTCNT_SHIFT);
5793 return fcnt_prog;
5794}
5795
5796/**
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005797 * i40e_fdir_check_and_reenable - Function to reenabe FD ATR or SB if disabled
5798 * @pf: board private structure
5799 **/
5800void i40e_fdir_check_and_reenable(struct i40e_pf *pf)
5801{
Carolyn Wyborny3487b6c2015-08-27 11:42:38 -04005802 struct i40e_fdir_filter *filter;
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005803 u32 fcnt_prog, fcnt_avail;
Carolyn Wyborny3487b6c2015-08-27 11:42:38 -04005804 struct hlist_node *node;
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005805
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005806 if (test_bit(__I40E_FD_FLUSH_REQUESTED, &pf->state))
5807 return;
5808
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005809 /* Check if, FD SB or ATR was auto disabled and if there is enough room
5810 * to re-enable
5811 */
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005812 fcnt_prog = i40e_get_global_fd_count(pf);
Anjali Singhai Jain12957382014-06-04 04:22:47 +00005813 fcnt_avail = pf->fdir_pf_filter_count;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005814 if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM)) ||
5815 (pf->fd_add_err == 0) ||
5816 (i40e_get_current_atr_cnt(pf) < pf->fd_atr_cnt)) {
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005817 if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
5818 (pf->auto_disable_flags & I40E_FLAG_FD_SB_ENABLED)) {
5819 pf->auto_disable_flags &= ~I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jain2e4875e2015-04-16 20:06:06 -04005820 if (I40E_DEBUG_FD & pf->hw.debug_mask)
5821 dev_info(&pf->pdev->dev, "FD Sideband/ntuple is being enabled since we have space in the table now\n");
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005822 }
5823 }
5824 /* Wait for some more space to be available to turn on ATR */
5825 if (fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM * 2)) {
5826 if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
5827 (pf->auto_disable_flags & I40E_FLAG_FD_ATR_ENABLED)) {
5828 pf->auto_disable_flags &= ~I40E_FLAG_FD_ATR_ENABLED;
Anjali Singhai Jain2e4875e2015-04-16 20:06:06 -04005829 if (I40E_DEBUG_FD & pf->hw.debug_mask)
5830 dev_info(&pf->pdev->dev, "ATR is being enabled since we have space in the table now\n");
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005831 }
5832 }
Carolyn Wyborny3487b6c2015-08-27 11:42:38 -04005833
5834 /* if hw had a problem adding a filter, delete it */
5835 if (pf->fd_inv > 0) {
5836 hlist_for_each_entry_safe(filter, node,
5837 &pf->fdir_filter_list, fdir_node) {
5838 if (filter->fd_id == pf->fd_inv) {
5839 hlist_del(&filter->fdir_node);
5840 kfree(filter);
5841 pf->fdir_pf_active_filters--;
5842 }
5843 }
5844 }
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005845}
5846
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005847#define I40E_MIN_FD_FLUSH_INTERVAL 10
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005848#define I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE 30
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005849/**
5850 * i40e_fdir_flush_and_replay - Function to flush all FD filters and replay SB
5851 * @pf: board private structure
5852 **/
5853static void i40e_fdir_flush_and_replay(struct i40e_pf *pf)
5854{
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005855 unsigned long min_flush_time;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005856 int flush_wait_retry = 50;
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005857 bool disable_atr = false;
5858 int fd_room;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005859 int reg;
5860
Akeem G Abodunrin1790ed02014-10-17 03:14:41 +00005861 if (!(pf->flags & (I40E_FLAG_FD_SB_ENABLED | I40E_FLAG_FD_ATR_ENABLED)))
5862 return;
5863
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04005864 if (!time_after(jiffies, pf->fd_flush_timestamp +
5865 (I40E_MIN_FD_FLUSH_INTERVAL * HZ)))
5866 return;
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005867
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04005868 /* If the flush is happening too quick and we have mostly SB rules we
5869 * should not re-enable ATR for some time.
5870 */
5871 min_flush_time = pf->fd_flush_timestamp +
5872 (I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE * HZ);
5873 fd_room = pf->fdir_pf_filter_count - pf->fdir_pf_active_filters;
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005874
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04005875 if (!(time_after(jiffies, min_flush_time)) &&
5876 (fd_room < I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) {
5877 if (I40E_DEBUG_FD & pf->hw.debug_mask)
5878 dev_info(&pf->pdev->dev, "ATR disabled, not enough FD filter space.\n");
5879 disable_atr = true;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005880 }
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04005881
5882 pf->fd_flush_timestamp = jiffies;
5883 pf->flags &= ~I40E_FLAG_FD_ATR_ENABLED;
5884 /* flush all filters */
5885 wr32(&pf->hw, I40E_PFQF_CTL_1,
5886 I40E_PFQF_CTL_1_CLEARFDTABLE_MASK);
5887 i40e_flush(&pf->hw);
5888 pf->fd_flush_cnt++;
5889 pf->fd_add_err = 0;
5890 do {
5891 /* Check FD flush status every 5-6msec */
5892 usleep_range(5000, 6000);
5893 reg = rd32(&pf->hw, I40E_PFQF_CTL_1);
5894 if (!(reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK))
5895 break;
5896 } while (flush_wait_retry--);
5897 if (reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK) {
5898 dev_warn(&pf->pdev->dev, "FD table did not flush, needs more time\n");
5899 } else {
5900 /* replay sideband filters */
5901 i40e_fdir_filter_restore(pf->vsi[pf->lan_vsi]);
5902 if (!disable_atr)
5903 pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
5904 clear_bit(__I40E_FD_FLUSH_REQUESTED, &pf->state);
5905 if (I40E_DEBUG_FD & pf->hw.debug_mask)
5906 dev_info(&pf->pdev->dev, "FD Filter table flushed and FD-SB replayed.\n");
5907 }
5908
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005909}
5910
5911/**
5912 * i40e_get_current_atr_count - Get the count of total FD ATR filters programmed
5913 * @pf: board private structure
5914 **/
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005915u32 i40e_get_current_atr_cnt(struct i40e_pf *pf)
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005916{
5917 return i40e_get_current_fd_count(pf) - pf->fdir_pf_active_filters;
5918}
5919
5920/* We can see up to 256 filter programming desc in transit if the filters are
5921 * being applied really fast; before we see the first
5922 * filter miss error on Rx queue 0. Accumulating enough error messages before
5923 * reacting will make sure we don't cause flush too often.
5924 */
5925#define I40E_MAX_FD_PROGRAM_ERROR 256
5926
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005927/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005928 * i40e_fdir_reinit_subtask - Worker thread to reinit FDIR filter table
5929 * @pf: board private structure
5930 **/
5931static void i40e_fdir_reinit_subtask(struct i40e_pf *pf)
5932{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005933
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005934 /* if interface is down do nothing */
5935 if (test_bit(__I40E_DOWN, &pf->state))
5936 return;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005937
Akeem G Abodunrin1790ed02014-10-17 03:14:41 +00005938 if (!(pf->flags & (I40E_FLAG_FD_SB_ENABLED | I40E_FLAG_FD_ATR_ENABLED)))
5939 return;
5940
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005941 if (test_bit(__I40E_FD_FLUSH_REQUESTED, &pf->state))
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005942 i40e_fdir_flush_and_replay(pf);
5943
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005944 i40e_fdir_check_and_reenable(pf);
5945
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005946}
5947
5948/**
5949 * i40e_vsi_link_event - notify VSI of a link event
5950 * @vsi: vsi to be notified
5951 * @link_up: link up or down
5952 **/
5953static void i40e_vsi_link_event(struct i40e_vsi *vsi, bool link_up)
5954{
Jesse Brandeburg32b5b812014-08-12 06:33:14 +00005955 if (!vsi || test_bit(__I40E_DOWN, &vsi->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005956 return;
5957
5958 switch (vsi->type) {
5959 case I40E_VSI_MAIN:
Vasu Dev38e00432014-08-01 13:27:03 -07005960#ifdef I40E_FCOE
5961 case I40E_VSI_FCOE:
5962#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005963 if (!vsi->netdev || !vsi->netdev_registered)
5964 break;
5965
5966 if (link_up) {
5967 netif_carrier_on(vsi->netdev);
5968 netif_tx_wake_all_queues(vsi->netdev);
5969 } else {
5970 netif_carrier_off(vsi->netdev);
5971 netif_tx_stop_all_queues(vsi->netdev);
5972 }
5973 break;
5974
5975 case I40E_VSI_SRIOV:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005976 case I40E_VSI_VMDQ2:
5977 case I40E_VSI_CTRL:
5978 case I40E_VSI_MIRROR:
5979 default:
5980 /* there is no notification for other VSIs */
5981 break;
5982 }
5983}
5984
5985/**
5986 * i40e_veb_link_event - notify elements on the veb of a link event
5987 * @veb: veb to be notified
5988 * @link_up: link up or down
5989 **/
5990static void i40e_veb_link_event(struct i40e_veb *veb, bool link_up)
5991{
5992 struct i40e_pf *pf;
5993 int i;
5994
5995 if (!veb || !veb->pf)
5996 return;
5997 pf = veb->pf;
5998
5999 /* depth first... */
6000 for (i = 0; i < I40E_MAX_VEB; i++)
6001 if (pf->veb[i] && (pf->veb[i]->uplink_seid == veb->seid))
6002 i40e_veb_link_event(pf->veb[i], link_up);
6003
6004 /* ... now the local VSIs */
Mitch Williams505682c2014-05-20 08:01:37 +00006005 for (i = 0; i < pf->num_alloc_vsi; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006006 if (pf->vsi[i] && (pf->vsi[i]->uplink_seid == veb->seid))
6007 i40e_vsi_link_event(pf->vsi[i], link_up);
6008}
6009
6010/**
6011 * i40e_link_event - Update netif_carrier status
6012 * @pf: board private structure
6013 **/
6014static void i40e_link_event(struct i40e_pf *pf)
6015{
Mitch Williams320684c2014-10-17 03:14:43 +00006016 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
Catherine Sullivanfef59dd2014-12-11 07:06:33 +00006017 u8 new_link_speed, old_link_speed;
Jesse Brandeburga72a5abc2015-08-26 15:14:19 -04006018 i40e_status status;
6019 bool new_link, old_link;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006020
Catherine Sullivan1f9610e2015-10-21 19:47:09 -04006021 /* save off old link status information */
6022 pf->hw.phy.link_info_old = pf->hw.phy.link_info;
6023
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00006024 /* set this to force the get_link_status call to refresh state */
6025 pf->hw.phy.get_link_info = true;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006026
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00006027 old_link = (pf->hw.phy.link_info_old.link_info & I40E_AQ_LINK_UP);
Jesse Brandeburga72a5abc2015-08-26 15:14:19 -04006028
6029 status = i40e_get_link_status(&pf->hw, &new_link);
6030 if (status) {
6031 dev_dbg(&pf->pdev->dev, "couldn't get link state, status: %d\n",
6032 status);
6033 return;
6034 }
6035
Catherine Sullivanfef59dd2014-12-11 07:06:33 +00006036 old_link_speed = pf->hw.phy.link_info_old.link_speed;
6037 new_link_speed = pf->hw.phy.link_info.link_speed;
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00006038
6039 if (new_link == old_link &&
Catherine Sullivanfef59dd2014-12-11 07:06:33 +00006040 new_link_speed == old_link_speed &&
Mitch Williams320684c2014-10-17 03:14:43 +00006041 (test_bit(__I40E_DOWN, &vsi->state) ||
6042 new_link == netif_carrier_ok(vsi->netdev)))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006043 return;
Mitch Williams320684c2014-10-17 03:14:43 +00006044
6045 if (!test_bit(__I40E_DOWN, &vsi->state))
6046 i40e_print_link_message(vsi, new_link);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006047
6048 /* Notify the base of the switch tree connected to
6049 * the link. Floating VEBs are not notified.
6050 */
6051 if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
6052 i40e_veb_link_event(pf->veb[pf->lan_veb], new_link);
6053 else
Mitch Williams320684c2014-10-17 03:14:43 +00006054 i40e_vsi_link_event(vsi, new_link);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006055
6056 if (pf->vf)
6057 i40e_vc_notify_link_state(pf);
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00006058
6059 if (pf->flags & I40E_FLAG_PTP)
6060 i40e_ptp_set_increment(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006061}
6062
6063/**
Shannon Nelson21536712014-10-25 10:35:25 +00006064 * i40e_watchdog_subtask - periodic checks not using event driven response
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006065 * @pf: board private structure
6066 **/
6067static void i40e_watchdog_subtask(struct i40e_pf *pf)
6068{
6069 int i;
6070
6071 /* if interface is down do nothing */
6072 if (test_bit(__I40E_DOWN, &pf->state) ||
6073 test_bit(__I40E_CONFIG_BUSY, &pf->state))
6074 return;
6075
Shannon Nelson21536712014-10-25 10:35:25 +00006076 /* make sure we don't do these things too often */
6077 if (time_before(jiffies, (pf->service_timer_previous +
6078 pf->service_timer_period)))
6079 return;
6080 pf->service_timer_previous = jiffies;
6081
Shannon Nelson9ac77262015-08-27 11:42:40 -04006082 if (pf->flags & I40E_FLAG_LINK_POLLING_ENABLED)
6083 i40e_link_event(pf);
Shannon Nelson21536712014-10-25 10:35:25 +00006084
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006085 /* Update the stats for active netdevs so the network stack
6086 * can look at updated numbers whenever it cares to
6087 */
Mitch Williams505682c2014-05-20 08:01:37 +00006088 for (i = 0; i < pf->num_alloc_vsi; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006089 if (pf->vsi[i] && pf->vsi[i]->netdev)
6090 i40e_update_stats(pf->vsi[i]);
6091
Anjali Singhai Jaind1a8d272015-07-23 16:54:40 -04006092 if (pf->flags & I40E_FLAG_VEB_STATS_ENABLED) {
6093 /* Update the stats for the active switching components */
6094 for (i = 0; i < I40E_MAX_VEB; i++)
6095 if (pf->veb[i])
6096 i40e_update_veb_stats(pf->veb[i]);
6097 }
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00006098
6099 i40e_ptp_rx_hang(pf->vsi[pf->lan_vsi]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006100}
6101
6102/**
6103 * i40e_reset_subtask - Set up for resetting the device and driver
6104 * @pf: board private structure
6105 **/
6106static void i40e_reset_subtask(struct i40e_pf *pf)
6107{
6108 u32 reset_flags = 0;
6109
Anjali Singhai Jain23326182013-11-26 10:49:22 +00006110 rtnl_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006111 if (test_bit(__I40E_REINIT_REQUESTED, &pf->state)) {
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04006112 reset_flags |= BIT_ULL(__I40E_REINIT_REQUESTED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006113 clear_bit(__I40E_REINIT_REQUESTED, &pf->state);
6114 }
6115 if (test_bit(__I40E_PF_RESET_REQUESTED, &pf->state)) {
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04006116 reset_flags |= BIT_ULL(__I40E_PF_RESET_REQUESTED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006117 clear_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
6118 }
6119 if (test_bit(__I40E_CORE_RESET_REQUESTED, &pf->state)) {
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04006120 reset_flags |= BIT_ULL(__I40E_CORE_RESET_REQUESTED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006121 clear_bit(__I40E_CORE_RESET_REQUESTED, &pf->state);
6122 }
6123 if (test_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state)) {
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04006124 reset_flags |= BIT_ULL(__I40E_GLOBAL_RESET_REQUESTED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006125 clear_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state);
6126 }
Neerav Parikhb5d06f02014-06-03 23:50:17 +00006127 if (test_bit(__I40E_DOWN_REQUESTED, &pf->state)) {
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04006128 reset_flags |= BIT_ULL(__I40E_DOWN_REQUESTED);
Neerav Parikhb5d06f02014-06-03 23:50:17 +00006129 clear_bit(__I40E_DOWN_REQUESTED, &pf->state);
6130 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006131
6132 /* If there's a recovery already waiting, it takes
6133 * precedence before starting a new reset sequence.
6134 */
6135 if (test_bit(__I40E_RESET_INTR_RECEIVED, &pf->state)) {
6136 i40e_handle_reset_warning(pf);
Anjali Singhai Jain23326182013-11-26 10:49:22 +00006137 goto unlock;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006138 }
6139
6140 /* If we're already down or resetting, just bail */
6141 if (reset_flags &&
6142 !test_bit(__I40E_DOWN, &pf->state) &&
6143 !test_bit(__I40E_CONFIG_BUSY, &pf->state))
6144 i40e_do_reset(pf, reset_flags);
Anjali Singhai Jain23326182013-11-26 10:49:22 +00006145
6146unlock:
6147 rtnl_unlock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006148}
6149
6150/**
6151 * i40e_handle_link_event - Handle link event
6152 * @pf: board private structure
6153 * @e: event info posted on ARQ
6154 **/
6155static void i40e_handle_link_event(struct i40e_pf *pf,
6156 struct i40e_arq_event_info *e)
6157{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006158 struct i40e_aqc_get_link_status *status =
6159 (struct i40e_aqc_get_link_status *)&e->desc.params.raw;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006160
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00006161 /* Do a new status request to re-enable LSE reporting
6162 * and load new status information into the hw struct
6163 * This completely ignores any state information
6164 * in the ARQ event info, instead choosing to always
6165 * issue the AQ update link status command.
6166 */
6167 i40e_link_event(pf);
6168
Carolyn Wyborny7b592f62014-07-10 07:58:19 +00006169 /* check for unqualified module, if link is down */
6170 if ((status->link_info & I40E_AQ_MEDIA_AVAILABLE) &&
6171 (!(status->an_info & I40E_AQ_QUALIFIED_MODULE)) &&
6172 (!(status->link_info & I40E_AQ_LINK_UP)))
6173 dev_err(&pf->pdev->dev,
6174 "The driver failed to link because an unqualified module was detected.\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006175}
6176
6177/**
6178 * i40e_clean_adminq_subtask - Clean the AdminQ rings
6179 * @pf: board private structure
6180 **/
6181static void i40e_clean_adminq_subtask(struct i40e_pf *pf)
6182{
6183 struct i40e_arq_event_info event;
6184 struct i40e_hw *hw = &pf->hw;
6185 u16 pending, i = 0;
6186 i40e_status ret;
6187 u16 opcode;
Shannon Nelson86df2422014-05-20 08:01:35 +00006188 u32 oldval;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006189 u32 val;
6190
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00006191 /* Do not run clean AQ when PF reset fails */
6192 if (test_bit(__I40E_RESET_FAILED, &pf->state))
6193 return;
6194
Shannon Nelson86df2422014-05-20 08:01:35 +00006195 /* check for error indications */
6196 val = rd32(&pf->hw, pf->hw.aq.arq.len);
6197 oldval = val;
6198 if (val & I40E_PF_ARQLEN_ARQVFE_MASK) {
6199 dev_info(&pf->pdev->dev, "ARQ VF Error detected\n");
6200 val &= ~I40E_PF_ARQLEN_ARQVFE_MASK;
6201 }
6202 if (val & I40E_PF_ARQLEN_ARQOVFL_MASK) {
6203 dev_info(&pf->pdev->dev, "ARQ Overflow Error detected\n");
6204 val &= ~I40E_PF_ARQLEN_ARQOVFL_MASK;
6205 }
6206 if (val & I40E_PF_ARQLEN_ARQCRIT_MASK) {
6207 dev_info(&pf->pdev->dev, "ARQ Critical Error detected\n");
6208 val &= ~I40E_PF_ARQLEN_ARQCRIT_MASK;
6209 }
6210 if (oldval != val)
6211 wr32(&pf->hw, pf->hw.aq.arq.len, val);
6212
6213 val = rd32(&pf->hw, pf->hw.aq.asq.len);
6214 oldval = val;
6215 if (val & I40E_PF_ATQLEN_ATQVFE_MASK) {
6216 dev_info(&pf->pdev->dev, "ASQ VF Error detected\n");
6217 val &= ~I40E_PF_ATQLEN_ATQVFE_MASK;
6218 }
6219 if (val & I40E_PF_ATQLEN_ATQOVFL_MASK) {
6220 dev_info(&pf->pdev->dev, "ASQ Overflow Error detected\n");
6221 val &= ~I40E_PF_ATQLEN_ATQOVFL_MASK;
6222 }
6223 if (val & I40E_PF_ATQLEN_ATQCRIT_MASK) {
6224 dev_info(&pf->pdev->dev, "ASQ Critical Error detected\n");
6225 val &= ~I40E_PF_ATQLEN_ATQCRIT_MASK;
6226 }
6227 if (oldval != val)
6228 wr32(&pf->hw, pf->hw.aq.asq.len, val);
6229
Mitch Williams1001dc32014-11-11 20:02:19 +00006230 event.buf_len = I40E_MAX_AQ_BUF_SIZE;
6231 event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006232 if (!event.msg_buf)
6233 return;
6234
6235 do {
6236 ret = i40e_clean_arq_element(hw, &event, &pending);
Mitch Williams56497972014-06-04 08:45:18 +00006237 if (ret == I40E_ERR_ADMIN_QUEUE_NO_WORK)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006238 break;
Mitch Williams56497972014-06-04 08:45:18 +00006239 else if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006240 dev_info(&pf->pdev->dev, "ARQ event error %d\n", ret);
6241 break;
6242 }
6243
6244 opcode = le16_to_cpu(event.desc.opcode);
6245 switch (opcode) {
6246
6247 case i40e_aqc_opc_get_link_status:
6248 i40e_handle_link_event(pf, &event);
6249 break;
6250 case i40e_aqc_opc_send_msg_to_pf:
6251 ret = i40e_vc_process_vf_msg(pf,
6252 le16_to_cpu(event.desc.retval),
6253 le32_to_cpu(event.desc.cookie_high),
6254 le32_to_cpu(event.desc.cookie_low),
6255 event.msg_buf,
Mitch Williams1001dc32014-11-11 20:02:19 +00006256 event.msg_len);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006257 break;
6258 case i40e_aqc_opc_lldp_update_mib:
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006259 dev_dbg(&pf->pdev->dev, "ARQ: Update LLDP MIB event received\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006260#ifdef CONFIG_I40E_DCB
6261 rtnl_lock();
6262 ret = i40e_handle_lldp_event(pf, &event);
6263 rtnl_unlock();
6264#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006265 break;
6266 case i40e_aqc_opc_event_lan_overflow:
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006267 dev_dbg(&pf->pdev->dev, "ARQ LAN queue overflow event received\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006268 i40e_handle_lan_overflow_event(pf, &event);
6269 break;
Shannon Nelson0467bc92013-12-18 13:45:58 +00006270 case i40e_aqc_opc_send_msg_to_peer:
6271 dev_info(&pf->pdev->dev, "ARQ: Msg from other pf\n");
6272 break;
Shannon Nelson91a0f932015-03-19 14:32:01 -07006273 case i40e_aqc_opc_nvm_erase:
6274 case i40e_aqc_opc_nvm_update:
6275 i40e_debug(&pf->hw, I40E_DEBUG_NVM, "ARQ NVM operation completed\n");
6276 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006277 default:
6278 dev_info(&pf->pdev->dev,
Shannon Nelson0467bc92013-12-18 13:45:58 +00006279 "ARQ Error: Unknown event 0x%04x received\n",
6280 opcode);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006281 break;
6282 }
6283 } while (pending && (i++ < pf->adminq_work_limit));
6284
6285 clear_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state);
6286 /* re-enable Admin queue interrupt cause */
6287 val = rd32(hw, I40E_PFINT_ICR0_ENA);
6288 val |= I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
6289 wr32(hw, I40E_PFINT_ICR0_ENA, val);
6290 i40e_flush(hw);
6291
6292 kfree(event.msg_buf);
6293}
6294
6295/**
Shannon Nelson4eb3f762014-03-06 08:59:58 +00006296 * i40e_verify_eeprom - make sure eeprom is good to use
6297 * @pf: board private structure
6298 **/
6299static void i40e_verify_eeprom(struct i40e_pf *pf)
6300{
6301 int err;
6302
6303 err = i40e_diag_eeprom_test(&pf->hw);
6304 if (err) {
6305 /* retry in case of garbage read */
6306 err = i40e_diag_eeprom_test(&pf->hw);
6307 if (err) {
6308 dev_info(&pf->pdev->dev, "eeprom check failed (%d), Tx/Rx traffic disabled\n",
6309 err);
6310 set_bit(__I40E_BAD_EEPROM, &pf->state);
6311 }
6312 }
6313
6314 if (!err && test_bit(__I40E_BAD_EEPROM, &pf->state)) {
6315 dev_info(&pf->pdev->dev, "eeprom check passed, Tx/Rx traffic enabled\n");
6316 clear_bit(__I40E_BAD_EEPROM, &pf->state);
6317 }
6318}
6319
6320/**
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006321 * i40e_enable_pf_switch_lb
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00006322 * @pf: pointer to the PF structure
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006323 *
6324 * enable switch loop back or die - no point in a return value
6325 **/
6326static void i40e_enable_pf_switch_lb(struct i40e_pf *pf)
6327{
6328 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
6329 struct i40e_vsi_context ctxt;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006330 int ret;
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006331
6332 ctxt.seid = pf->main_vsi_seid;
6333 ctxt.pf_num = pf->hw.pf_id;
6334 ctxt.vf_num = 0;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006335 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
6336 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006337 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006338 "couldn't get PF vsi config, err %s aq_err %s\n",
6339 i40e_stat_str(&pf->hw, ret),
6340 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006341 return;
6342 }
6343 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
6344 ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
6345 ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
6346
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006347 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
6348 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006349 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006350 "update vsi switch failed, err %s aq_err %s\n",
6351 i40e_stat_str(&pf->hw, ret),
6352 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006353 }
6354}
6355
6356/**
6357 * i40e_disable_pf_switch_lb
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00006358 * @pf: pointer to the PF structure
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006359 *
6360 * disable switch loop back or die - no point in a return value
6361 **/
6362static void i40e_disable_pf_switch_lb(struct i40e_pf *pf)
6363{
6364 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
6365 struct i40e_vsi_context ctxt;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006366 int ret;
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006367
6368 ctxt.seid = pf->main_vsi_seid;
6369 ctxt.pf_num = pf->hw.pf_id;
6370 ctxt.vf_num = 0;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006371 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
6372 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006373 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006374 "couldn't get PF vsi config, err %s aq_err %s\n",
6375 i40e_stat_str(&pf->hw, ret),
6376 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006377 return;
6378 }
6379 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
6380 ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
6381 ctxt.info.switch_id &= ~cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
6382
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006383 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
6384 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006385 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006386 "update vsi switch failed, err %s aq_err %s\n",
6387 i40e_stat_str(&pf->hw, ret),
6388 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006389 }
6390}
6391
6392/**
Neerav Parikh51616012015-02-06 08:52:14 +00006393 * i40e_config_bridge_mode - Configure the HW bridge mode
6394 * @veb: pointer to the bridge instance
6395 *
6396 * Configure the loop back mode for the LAN VSI that is downlink to the
6397 * specified HW bridge instance. It is expected this function is called
6398 * when a new HW bridge is instantiated.
6399 **/
6400static void i40e_config_bridge_mode(struct i40e_veb *veb)
6401{
6402 struct i40e_pf *pf = veb->pf;
6403
Shannon Nelson6dec1012015-09-28 14:12:30 -04006404 if (pf->hw.debug_mask & I40E_DEBUG_LAN)
6405 dev_info(&pf->pdev->dev, "enabling bridge mode: %s\n",
6406 veb->bridge_mode == BRIDGE_MODE_VEPA ? "VEPA" : "VEB");
Neerav Parikh51616012015-02-06 08:52:14 +00006407 if (veb->bridge_mode & BRIDGE_MODE_VEPA)
6408 i40e_disable_pf_switch_lb(pf);
6409 else
6410 i40e_enable_pf_switch_lb(pf);
6411}
6412
6413/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006414 * i40e_reconstitute_veb - rebuild the VEB and anything connected to it
6415 * @veb: pointer to the VEB instance
6416 *
6417 * This is a recursive function that first builds the attached VSIs then
6418 * recurses in to build the next layer of VEB. We track the connections
6419 * through our own index numbers because the seid's from the HW could
6420 * change across the reset.
6421 **/
6422static int i40e_reconstitute_veb(struct i40e_veb *veb)
6423{
6424 struct i40e_vsi *ctl_vsi = NULL;
6425 struct i40e_pf *pf = veb->pf;
6426 int v, veb_idx;
6427 int ret;
6428
6429 /* build VSI that owns this VEB, temporarily attached to base VEB */
Mitch Williams505682c2014-05-20 08:01:37 +00006430 for (v = 0; v < pf->num_alloc_vsi && !ctl_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006431 if (pf->vsi[v] &&
6432 pf->vsi[v]->veb_idx == veb->idx &&
6433 pf->vsi[v]->flags & I40E_VSI_FLAG_VEB_OWNER) {
6434 ctl_vsi = pf->vsi[v];
6435 break;
6436 }
6437 }
6438 if (!ctl_vsi) {
6439 dev_info(&pf->pdev->dev,
6440 "missing owner VSI for veb_idx %d\n", veb->idx);
6441 ret = -ENOENT;
6442 goto end_reconstitute;
6443 }
6444 if (ctl_vsi != pf->vsi[pf->lan_vsi])
6445 ctl_vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
6446 ret = i40e_add_vsi(ctl_vsi);
6447 if (ret) {
6448 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006449 "rebuild of veb_idx %d owner VSI failed: %d\n",
6450 veb->idx, ret);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006451 goto end_reconstitute;
6452 }
6453 i40e_vsi_reset_stats(ctl_vsi);
6454
6455 /* create the VEB in the switch and move the VSI onto the VEB */
6456 ret = i40e_add_veb(veb, ctl_vsi);
6457 if (ret)
6458 goto end_reconstitute;
6459
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07006460 if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
6461 veb->bridge_mode = BRIDGE_MODE_VEB;
6462 else
6463 veb->bridge_mode = BRIDGE_MODE_VEPA;
Neerav Parikh51616012015-02-06 08:52:14 +00006464 i40e_config_bridge_mode(veb);
Anjali Singhai Jainb64ba082014-11-13 03:06:15 +00006465
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006466 /* create the remaining VSIs attached to this VEB */
Mitch Williams505682c2014-05-20 08:01:37 +00006467 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006468 if (!pf->vsi[v] || pf->vsi[v] == ctl_vsi)
6469 continue;
6470
6471 if (pf->vsi[v]->veb_idx == veb->idx) {
6472 struct i40e_vsi *vsi = pf->vsi[v];
Jesse Brandeburg6995b362015-08-28 17:55:54 -04006473
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006474 vsi->uplink_seid = veb->seid;
6475 ret = i40e_add_vsi(vsi);
6476 if (ret) {
6477 dev_info(&pf->pdev->dev,
6478 "rebuild of vsi_idx %d failed: %d\n",
6479 v, ret);
6480 goto end_reconstitute;
6481 }
6482 i40e_vsi_reset_stats(vsi);
6483 }
6484 }
6485
6486 /* create any VEBs attached to this VEB - RECURSION */
6487 for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
6488 if (pf->veb[veb_idx] && pf->veb[veb_idx]->veb_idx == veb->idx) {
6489 pf->veb[veb_idx]->uplink_seid = veb->seid;
6490 ret = i40e_reconstitute_veb(pf->veb[veb_idx]);
6491 if (ret)
6492 break;
6493 }
6494 }
6495
6496end_reconstitute:
6497 return ret;
6498}
6499
6500/**
6501 * i40e_get_capabilities - get info about the HW
6502 * @pf: the PF struct
6503 **/
6504static int i40e_get_capabilities(struct i40e_pf *pf)
6505{
6506 struct i40e_aqc_list_capabilities_element_resp *cap_buf;
6507 u16 data_size;
6508 int buf_len;
6509 int err;
6510
6511 buf_len = 40 * sizeof(struct i40e_aqc_list_capabilities_element_resp);
6512 do {
6513 cap_buf = kzalloc(buf_len, GFP_KERNEL);
6514 if (!cap_buf)
6515 return -ENOMEM;
6516
6517 /* this loads the data into the hw struct for us */
6518 err = i40e_aq_discover_capabilities(&pf->hw, cap_buf, buf_len,
6519 &data_size,
6520 i40e_aqc_opc_list_func_capabilities,
6521 NULL);
6522 /* data loaded, buffer no longer needed */
6523 kfree(cap_buf);
6524
6525 if (pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOMEM) {
6526 /* retry with a larger buffer */
6527 buf_len = data_size;
6528 } else if (pf->hw.aq.asq_last_status != I40E_AQ_RC_OK) {
6529 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006530 "capability discovery failed, err %s aq_err %s\n",
6531 i40e_stat_str(&pf->hw, err),
6532 i40e_aq_str(&pf->hw,
6533 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006534 return -ENODEV;
6535 }
6536 } while (err);
6537
6538 if (pf->hw.debug_mask & I40E_DEBUG_USER)
6539 dev_info(&pf->pdev->dev,
6540 "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",
6541 pf->hw.pf_id, pf->hw.func_caps.num_vfs,
6542 pf->hw.func_caps.num_msix_vectors,
6543 pf->hw.func_caps.num_msix_vectors_vf,
6544 pf->hw.func_caps.fd_filters_guaranteed,
6545 pf->hw.func_caps.fd_filters_best_effort,
6546 pf->hw.func_caps.num_tx_qp,
6547 pf->hw.func_caps.num_vsis);
6548
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00006549#define DEF_NUM_VSI (1 + (pf->hw.func_caps.fcoe ? 1 : 0) \
6550 + pf->hw.func_caps.num_vfs)
6551 if (pf->hw.revision_id == 0 && (DEF_NUM_VSI > pf->hw.func_caps.num_vsis)) {
6552 dev_info(&pf->pdev->dev,
6553 "got num_vsis %d, setting num_vsis to %d\n",
6554 pf->hw.func_caps.num_vsis, DEF_NUM_VSI);
6555 pf->hw.func_caps.num_vsis = DEF_NUM_VSI;
6556 }
6557
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006558 return 0;
6559}
6560
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006561static int i40e_vsi_clear(struct i40e_vsi *vsi);
6562
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006563/**
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006564 * i40e_fdir_sb_setup - initialize the Flow Director resources for Sideband
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006565 * @pf: board private structure
6566 **/
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006567static void i40e_fdir_sb_setup(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006568{
6569 struct i40e_vsi *vsi;
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00006570 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006571
Jesse Brandeburg407e0632014-06-03 23:50:12 +00006572 /* quick workaround for an NVM issue that leaves a critical register
6573 * uninitialized
6574 */
6575 if (!rd32(&pf->hw, I40E_GLQF_HKEY(0))) {
6576 static const u32 hkey[] = {
6577 0xe640d33f, 0xcdfe98ab, 0x73fa7161, 0x0d7a7d36,
6578 0xeacb7d61, 0xaa4f05b6, 0x9c5c89ed, 0xfc425ddb,
6579 0xa4654832, 0xfc7461d4, 0x8f827619, 0xf5c63c21,
6580 0x95b3a76d};
6581
6582 for (i = 0; i <= I40E_GLQF_HKEY_MAX_INDEX; i++)
6583 wr32(&pf->hw, I40E_GLQF_HKEY(i), hkey[i]);
6584 }
6585
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006586 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006587 return;
6588
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006589 /* find existing VSI and see if it needs configuring */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006590 vsi = NULL;
Mitch Williams505682c2014-05-20 08:01:37 +00006591 for (i = 0; i < pf->num_alloc_vsi; i++) {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006592 if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006593 vsi = pf->vsi[i];
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006594 break;
6595 }
6596 }
6597
6598 /* create a new VSI if none exists */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006599 if (!vsi) {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006600 vsi = i40e_vsi_setup(pf, I40E_VSI_FDIR,
6601 pf->vsi[pf->lan_vsi]->seid, 0);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006602 if (!vsi) {
6603 dev_info(&pf->pdev->dev, "Couldn't create FDir VSI\n");
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00006604 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
6605 return;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006606 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006607 }
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00006608
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006609 i40e_vsi_setup_irqhandler(vsi, i40e_fdir_clean_ring);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006610}
6611
6612/**
6613 * i40e_fdir_teardown - release the Flow Director resources
6614 * @pf: board private structure
6615 **/
6616static void i40e_fdir_teardown(struct i40e_pf *pf)
6617{
6618 int i;
6619
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00006620 i40e_fdir_filter_exit(pf);
Mitch Williams505682c2014-05-20 08:01:37 +00006621 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006622 if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
6623 i40e_vsi_release(pf->vsi[i]);
6624 break;
6625 }
6626 }
6627}
6628
6629/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006630 * i40e_prep_for_reset - prep for the core to reset
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006631 * @pf: board private structure
6632 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00006633 * Close up the VFs and other things in prep for PF Reset.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006634 **/
Shannon Nelson23cfbe02014-06-04 01:23:14 +00006635static void i40e_prep_for_reset(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006636{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006637 struct i40e_hw *hw = &pf->hw;
Shannon Nelson60442de2014-04-23 04:50:13 +00006638 i40e_status ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006639 u32 v;
6640
6641 clear_bit(__I40E_RESET_INTR_RECEIVED, &pf->state);
6642 if (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
Shannon Nelson23cfbe02014-06-04 01:23:14 +00006643 return;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006644
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006645 dev_dbg(&pf->pdev->dev, "Tearing down internal switch for reset\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006646
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006647 /* quiesce the VSIs and their queues that are not already DOWN */
6648 i40e_pf_quiesce_all_vsi(pf);
6649
Mitch Williams505682c2014-05-20 08:01:37 +00006650 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006651 if (pf->vsi[v])
6652 pf->vsi[v]->seid = 0;
6653 }
6654
6655 i40e_shutdown_adminq(&pf->hw);
6656
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006657 /* call shutdown HMC */
Shannon Nelson60442de2014-04-23 04:50:13 +00006658 if (hw->hmc.hmc_obj) {
6659 ret = i40e_shutdown_lan_hmc(hw);
Shannon Nelson23cfbe02014-06-04 01:23:14 +00006660 if (ret)
Shannon Nelson60442de2014-04-23 04:50:13 +00006661 dev_warn(&pf->pdev->dev,
6662 "shutdown_lan_hmc failed: %d\n", ret);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006663 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006664}
6665
6666/**
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00006667 * i40e_send_version - update firmware with driver version
6668 * @pf: PF struct
6669 */
6670static void i40e_send_version(struct i40e_pf *pf)
6671{
6672 struct i40e_driver_version dv;
6673
6674 dv.major_version = DRV_VERSION_MAJOR;
6675 dv.minor_version = DRV_VERSION_MINOR;
6676 dv.build_version = DRV_VERSION_BUILD;
6677 dv.subbuild_version = 0;
Rickard Strandqvist35a7d802014-07-29 09:26:25 +00006678 strlcpy(dv.driver_string, DRV_VERSION, sizeof(dv.driver_string));
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00006679 i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
6680}
6681
6682/**
Jesse Brandeburg4dda12e2013-12-18 13:46:01 +00006683 * i40e_reset_and_rebuild - reset and rebuild using a saved config
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006684 * @pf: board private structure
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00006685 * @reinit: if the Main VSI needs to re-initialized.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006686 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00006687static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006688{
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006689 struct i40e_hw *hw = &pf->hw;
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00006690 u8 set_fc_aq_fail = 0;
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006691 i40e_status ret;
Anjali Singhai Jain4f2f017c2015-10-21 19:47:07 -04006692 u32 val;
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006693 u32 v;
6694
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006695 /* Now we wait for GRST to settle out.
6696 * We don't have to delete the VEBs or VSIs from the hw switch
6697 * because the reset will make them disappear.
6698 */
6699 ret = i40e_pf_reset(hw);
Akeem G Abodunrinb5565402014-04-09 05:59:04 +00006700 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006701 dev_info(&pf->pdev->dev, "PF reset failed, %d\n", ret);
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00006702 set_bit(__I40E_RESET_FAILED, &pf->state);
6703 goto clear_recovery;
Akeem G Abodunrinb5565402014-04-09 05:59:04 +00006704 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006705 pf->pfr_count++;
6706
6707 if (test_bit(__I40E_DOWN, &pf->state))
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00006708 goto clear_recovery;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006709 dev_dbg(&pf->pdev->dev, "Rebuilding internal switch\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006710
6711 /* rebuild the basics for the AdminQ, HMC, and initial HW switch */
6712 ret = i40e_init_adminq(&pf->hw);
6713 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006714 dev_info(&pf->pdev->dev, "Rebuild AdminQ failed, err %s aq_err %s\n",
6715 i40e_stat_str(&pf->hw, ret),
6716 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00006717 goto clear_recovery;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006718 }
6719
Shannon Nelson4eb3f762014-03-06 08:59:58 +00006720 /* re-verify the eeprom if we just had an EMP reset */
Anjali Singhai Jain9df42d12015-01-24 09:58:40 +00006721 if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, &pf->state))
Shannon Nelson4eb3f762014-03-06 08:59:58 +00006722 i40e_verify_eeprom(pf);
Shannon Nelson4eb3f762014-03-06 08:59:58 +00006723
Shannon Nelsone78ac4bf2014-05-10 04:49:09 +00006724 i40e_clear_pxe_mode(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006725 ret = i40e_get_capabilities(pf);
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006726 if (ret)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006727 goto end_core_reset;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006728
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006729 ret = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
6730 hw->func_caps.num_rx_qp,
6731 pf->fcoe_hmc_cntx_num, pf->fcoe_hmc_filt_num);
6732 if (ret) {
6733 dev_info(&pf->pdev->dev, "init_lan_hmc failed: %d\n", ret);
6734 goto end_core_reset;
6735 }
6736 ret = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
6737 if (ret) {
6738 dev_info(&pf->pdev->dev, "configure_lan_hmc failed: %d\n", ret);
6739 goto end_core_reset;
6740 }
6741
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006742#ifdef CONFIG_I40E_DCB
6743 ret = i40e_init_pf_dcb(pf);
6744 if (ret) {
Shannon Nelsonaebfc812014-12-11 07:06:38 +00006745 dev_info(&pf->pdev->dev, "DCB init failed %d, disabled\n", ret);
6746 pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
6747 /* Continue without DCB enabled */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006748 }
6749#endif /* CONFIG_I40E_DCB */
Vasu Dev38e00432014-08-01 13:27:03 -07006750#ifdef I40E_FCOE
Shannon Nelson21364bc2015-08-26 15:14:13 -04006751 i40e_init_pf_fcoe(pf);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006752
Vasu Dev38e00432014-08-01 13:27:03 -07006753#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006754 /* do basic switch setup */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00006755 ret = i40e_setup_pf_switch(pf, reinit);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006756 if (ret)
6757 goto end_core_reset;
6758
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +00006759 /* driver is only interested in link up/down and module qualification
6760 * reports from firmware
6761 */
6762 ret = i40e_aq_set_phy_int_mask(&pf->hw,
6763 I40E_AQ_EVENT_LINK_UPDOWN |
6764 I40E_AQ_EVENT_MODULE_QUAL_FAIL, NULL);
6765 if (ret)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006766 dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
6767 i40e_stat_str(&pf->hw, ret),
6768 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +00006769
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00006770 /* make sure our flow control settings are restored */
6771 ret = i40e_set_fc(&pf->hw, &set_fc_aq_fail, true);
6772 if (ret)
Neerav Parikh8279e492015-09-03 17:18:50 -04006773 dev_dbg(&pf->pdev->dev, "setting flow control: ret = %s last_status = %s\n",
6774 i40e_stat_str(&pf->hw, ret),
6775 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00006776
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006777 /* Rebuild the VSIs and VEBs that existed before reset.
6778 * They are still in our local switch element arrays, so only
6779 * need to rebuild the switch model in the HW.
6780 *
6781 * If there were VEBs but the reconstitution failed, we'll try
6782 * try to recover minimal use by getting the basic PF VSI working.
6783 */
6784 if (pf->vsi[pf->lan_vsi]->uplink_seid != pf->mac_seid) {
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006785 dev_dbg(&pf->pdev->dev, "attempting to rebuild switch\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006786 /* find the one VEB connected to the MAC, and find orphans */
6787 for (v = 0; v < I40E_MAX_VEB; v++) {
6788 if (!pf->veb[v])
6789 continue;
6790
6791 if (pf->veb[v]->uplink_seid == pf->mac_seid ||
6792 pf->veb[v]->uplink_seid == 0) {
6793 ret = i40e_reconstitute_veb(pf->veb[v]);
6794
6795 if (!ret)
6796 continue;
6797
6798 /* If Main VEB failed, we're in deep doodoo,
6799 * so give up rebuilding the switch and set up
6800 * for minimal rebuild of PF VSI.
6801 * If orphan failed, we'll report the error
6802 * but try to keep going.
6803 */
6804 if (pf->veb[v]->uplink_seid == pf->mac_seid) {
6805 dev_info(&pf->pdev->dev,
6806 "rebuild of switch failed: %d, will try to set up simple PF connection\n",
6807 ret);
6808 pf->vsi[pf->lan_vsi]->uplink_seid
6809 = pf->mac_seid;
6810 break;
6811 } else if (pf->veb[v]->uplink_seid == 0) {
6812 dev_info(&pf->pdev->dev,
6813 "rebuild of orphan VEB failed: %d\n",
6814 ret);
6815 }
6816 }
6817 }
6818 }
6819
6820 if (pf->vsi[pf->lan_vsi]->uplink_seid == pf->mac_seid) {
Shannon Nelsoncde4cbc2014-06-04 01:23:17 +00006821 dev_dbg(&pf->pdev->dev, "attempting to rebuild PF VSI\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006822 /* no VEB, so rebuild only the Main VSI */
6823 ret = i40e_add_vsi(pf->vsi[pf->lan_vsi]);
6824 if (ret) {
6825 dev_info(&pf->pdev->dev,
6826 "rebuild of Main VSI failed: %d\n", ret);
6827 goto end_core_reset;
6828 }
6829 }
6830
Anjali Singhai Jain4f2f017c2015-10-21 19:47:07 -04006831 /* Reconfigure hardware for allowing smaller MSS in the case
6832 * of TSO, so that we avoid the MDD being fired and causing
6833 * a reset in the case of small MSS+TSO.
6834 */
6835#define I40E_REG_MSS 0x000E64DC
6836#define I40E_REG_MSS_MIN_MASK 0x3FF0000
6837#define I40E_64BYTE_MSS 0x400000
6838 val = rd32(hw, I40E_REG_MSS);
6839 if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
6840 val &= ~I40E_REG_MSS_MIN_MASK;
6841 val |= I40E_64BYTE_MSS;
6842 wr32(hw, I40E_REG_MSS, val);
6843 }
6844
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +00006845 if (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 33)) ||
6846 (pf->hw.aq.fw_maj_ver < 4)) {
6847 msleep(75);
6848 ret = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
6849 if (ret)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006850 dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
6851 i40e_stat_str(&pf->hw, ret),
6852 i40e_aq_str(&pf->hw,
6853 pf->hw.aq.asq_last_status));
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00006854 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006855 /* reinit the misc interrupt */
6856 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
6857 ret = i40e_setup_misc_vector(pf);
6858
Anjali Singhai Jaine7358f52015-10-01 14:37:34 -04006859 /* Add a filter to drop all Flow control frames from any VSI from being
6860 * transmitted. By doing so we stop a malicious VF from sending out
6861 * PAUSE or PFC frames and potentially controlling traffic for other
6862 * PF/VF VSIs.
6863 * The FW can still send Flow control frames if enabled.
6864 */
6865 i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
6866 pf->main_vsi_seid);
6867
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006868 /* restart the VSIs that were rebuilt and running before the reset */
6869 i40e_pf_unquiesce_all_vsi(pf);
6870
Mitch Williams69f64b22014-02-13 03:48:46 -08006871 if (pf->num_alloc_vfs) {
6872 for (v = 0; v < pf->num_alloc_vfs; v++)
6873 i40e_reset_vf(&pf->vf[v], true);
6874 }
6875
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006876 /* tell the firmware that we're starting */
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00006877 i40e_send_version(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006878
6879end_core_reset:
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00006880 clear_bit(__I40E_RESET_FAILED, &pf->state);
6881clear_recovery:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006882 clear_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state);
6883}
6884
6885/**
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00006886 * i40e_handle_reset_warning - prep for the PF to reset, reset and rebuild
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006887 * @pf: board private structure
6888 *
6889 * Close up the VFs and other things in prep for a Core Reset,
6890 * then get ready to rebuild the world.
6891 **/
6892static void i40e_handle_reset_warning(struct i40e_pf *pf)
6893{
Shannon Nelson23cfbe02014-06-04 01:23:14 +00006894 i40e_prep_for_reset(pf);
6895 i40e_reset_and_rebuild(pf, false);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006896}
6897
6898/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006899 * i40e_handle_mdd_event
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00006900 * @pf: pointer to the PF structure
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006901 *
6902 * Called from the MDD irq handler to identify possibly malicious vfs
6903 **/
6904static void i40e_handle_mdd_event(struct i40e_pf *pf)
6905{
6906 struct i40e_hw *hw = &pf->hw;
6907 bool mdd_detected = false;
Neerav Parikhdf430b12014-06-04 01:23:15 +00006908 bool pf_mdd_detected = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006909 struct i40e_vf *vf;
6910 u32 reg;
6911 int i;
6912
6913 if (!test_bit(__I40E_MDD_EVENT_PENDING, &pf->state))
6914 return;
6915
6916 /* find what triggered the MDD event */
6917 reg = rd32(hw, I40E_GL_MDET_TX);
6918 if (reg & I40E_GL_MDET_TX_VALID_MASK) {
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00006919 u8 pf_num = (reg & I40E_GL_MDET_TX_PF_NUM_MASK) >>
6920 I40E_GL_MDET_TX_PF_NUM_SHIFT;
Mitch Williams2089ad02014-10-17 03:14:53 +00006921 u16 vf_num = (reg & I40E_GL_MDET_TX_VF_NUM_MASK) >>
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00006922 I40E_GL_MDET_TX_VF_NUM_SHIFT;
Dan Carpenter013f6572014-10-22 20:06:29 -07006923 u8 event = (reg & I40E_GL_MDET_TX_EVENT_MASK) >>
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00006924 I40E_GL_MDET_TX_EVENT_SHIFT;
Mitch Williams2089ad02014-10-17 03:14:53 +00006925 u16 queue = ((reg & I40E_GL_MDET_TX_QUEUE_MASK) >>
6926 I40E_GL_MDET_TX_QUEUE_SHIFT) -
6927 pf->hw.func_caps.base_queue;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00006928 if (netif_msg_tx_err(pf))
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00006929 dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on TX queue %d PF number 0x%02x VF number 0x%02x\n",
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00006930 event, queue, pf_num, vf_num);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006931 wr32(hw, I40E_GL_MDET_TX, 0xffffffff);
6932 mdd_detected = true;
6933 }
6934 reg = rd32(hw, I40E_GL_MDET_RX);
6935 if (reg & I40E_GL_MDET_RX_VALID_MASK) {
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00006936 u8 func = (reg & I40E_GL_MDET_RX_FUNCTION_MASK) >>
6937 I40E_GL_MDET_RX_FUNCTION_SHIFT;
Dan Carpenter013f6572014-10-22 20:06:29 -07006938 u8 event = (reg & I40E_GL_MDET_RX_EVENT_MASK) >>
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00006939 I40E_GL_MDET_RX_EVENT_SHIFT;
Mitch Williams2089ad02014-10-17 03:14:53 +00006940 u16 queue = ((reg & I40E_GL_MDET_RX_QUEUE_MASK) >>
6941 I40E_GL_MDET_RX_QUEUE_SHIFT) -
6942 pf->hw.func_caps.base_queue;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00006943 if (netif_msg_rx_err(pf))
6944 dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on RX queue %d of function 0x%02x\n",
6945 event, queue, func);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006946 wr32(hw, I40E_GL_MDET_RX, 0xffffffff);
6947 mdd_detected = true;
6948 }
6949
Neerav Parikhdf430b12014-06-04 01:23:15 +00006950 if (mdd_detected) {
6951 reg = rd32(hw, I40E_PF_MDET_TX);
6952 if (reg & I40E_PF_MDET_TX_VALID_MASK) {
6953 wr32(hw, I40E_PF_MDET_TX, 0xFFFF);
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00006954 dev_info(&pf->pdev->dev, "TX driver issue detected, PF reset issued\n");
Neerav Parikhdf430b12014-06-04 01:23:15 +00006955 pf_mdd_detected = true;
6956 }
6957 reg = rd32(hw, I40E_PF_MDET_RX);
6958 if (reg & I40E_PF_MDET_RX_VALID_MASK) {
6959 wr32(hw, I40E_PF_MDET_RX, 0xFFFF);
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00006960 dev_info(&pf->pdev->dev, "RX driver issue detected, PF reset issued\n");
Neerav Parikhdf430b12014-06-04 01:23:15 +00006961 pf_mdd_detected = true;
6962 }
6963 /* Queue belongs to the PF, initiate a reset */
6964 if (pf_mdd_detected) {
6965 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
6966 i40e_service_event_schedule(pf);
6967 }
6968 }
6969
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006970 /* see if one of the VFs needs its hand slapped */
6971 for (i = 0; i < pf->num_alloc_vfs && mdd_detected; i++) {
6972 vf = &(pf->vf[i]);
6973 reg = rd32(hw, I40E_VP_MDET_TX(i));
6974 if (reg & I40E_VP_MDET_TX_VALID_MASK) {
6975 wr32(hw, I40E_VP_MDET_TX(i), 0xFFFF);
6976 vf->num_mdd_events++;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00006977 dev_info(&pf->pdev->dev, "TX driver issue detected on VF %d\n",
6978 i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006979 }
6980
6981 reg = rd32(hw, I40E_VP_MDET_RX(i));
6982 if (reg & I40E_VP_MDET_RX_VALID_MASK) {
6983 wr32(hw, I40E_VP_MDET_RX(i), 0xFFFF);
6984 vf->num_mdd_events++;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00006985 dev_info(&pf->pdev->dev, "RX driver issue detected on VF %d\n",
6986 i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006987 }
6988
6989 if (vf->num_mdd_events > I40E_DEFAULT_NUM_MDD_EVENTS_ALLOWED) {
6990 dev_info(&pf->pdev->dev,
6991 "Too many MDD events on VF %d, disabled\n", i);
6992 dev_info(&pf->pdev->dev,
6993 "Use PF Control I/F to re-enable the VF\n");
6994 set_bit(I40E_VF_STAT_DISABLED, &vf->vf_states);
6995 }
6996 }
6997
6998 /* re-enable mdd interrupt cause */
6999 clear_bit(__I40E_MDD_EVENT_PENDING, &pf->state);
7000 reg = rd32(hw, I40E_PFINT_ICR0_ENA);
7001 reg |= I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
7002 wr32(hw, I40E_PFINT_ICR0_ENA, reg);
7003 i40e_flush(hw);
7004}
7005
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007006#ifdef CONFIG_I40E_VXLAN
7007/**
7008 * i40e_sync_vxlan_filters_subtask - Sync the VSI filter list with HW
7009 * @pf: board private structure
7010 **/
7011static void i40e_sync_vxlan_filters_subtask(struct i40e_pf *pf)
7012{
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007013 struct i40e_hw *hw = &pf->hw;
7014 i40e_status ret;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007015 __be16 port;
7016 int i;
7017
7018 if (!(pf->flags & I40E_FLAG_VXLAN_FILTER_SYNC))
7019 return;
7020
7021 pf->flags &= ~I40E_FLAG_VXLAN_FILTER_SYNC;
7022
7023 for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04007024 if (pf->pending_vxlan_bitmap & BIT_ULL(i)) {
7025 pf->pending_vxlan_bitmap &= ~BIT_ULL(i);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007026 port = pf->vxlan_ports[i];
Shannon Nelsonc22c06c2015-03-31 00:45:04 -07007027 if (port)
7028 ret = i40e_aq_add_udp_tunnel(hw, ntohs(port),
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007029 I40E_AQC_TUNNEL_TYPE_VXLAN,
Shannon Nelsonc22c06c2015-03-31 00:45:04 -07007030 NULL, NULL);
7031 else
7032 ret = i40e_aq_del_udp_tunnel(hw, i, NULL);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007033
7034 if (ret) {
Shannon Nelsonc22c06c2015-03-31 00:45:04 -07007035 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04007036 "%s vxlan port %d, index %d failed, err %s aq_err %s\n",
Shannon Nelsonc22c06c2015-03-31 00:45:04 -07007037 port ? "add" : "delete",
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04007038 ntohs(port), i,
7039 i40e_stat_str(&pf->hw, ret),
7040 i40e_aq_str(&pf->hw,
7041 pf->hw.aq.asq_last_status));
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007042 pf->vxlan_ports[i] = 0;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007043 }
7044 }
7045 }
7046}
7047
7048#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007049/**
7050 * i40e_service_task - Run the driver's async subtasks
7051 * @work: pointer to work_struct containing our data
7052 **/
7053static void i40e_service_task(struct work_struct *work)
7054{
7055 struct i40e_pf *pf = container_of(work,
7056 struct i40e_pf,
7057 service_task);
7058 unsigned long start_time = jiffies;
7059
Shannon Nelsone57a2fe2014-06-03 23:50:19 +00007060 /* don't bother with service tasks if a reset is in progress */
7061 if (test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state)) {
7062 i40e_service_event_complete(pf);
7063 return;
7064 }
7065
Kiran Patilb03a8c12015-09-24 18:13:15 -04007066 i40e_detect_recover_hung(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007067 i40e_reset_subtask(pf);
7068 i40e_handle_mdd_event(pf);
7069 i40e_vc_process_vflr_event(pf);
7070 i40e_watchdog_subtask(pf);
7071 i40e_fdir_reinit_subtask(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007072 i40e_sync_filters_subtask(pf);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007073#ifdef CONFIG_I40E_VXLAN
7074 i40e_sync_vxlan_filters_subtask(pf);
7075#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007076 i40e_clean_adminq_subtask(pf);
7077
7078 i40e_service_event_complete(pf);
7079
7080 /* If the tasks have taken longer than one timer cycle or there
7081 * is more work to be done, reschedule the service task now
7082 * rather than wait for the timer to tick again.
7083 */
7084 if (time_after(jiffies, (start_time + pf->service_timer_period)) ||
7085 test_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state) ||
7086 test_bit(__I40E_MDD_EVENT_PENDING, &pf->state) ||
7087 test_bit(__I40E_VFLR_EVENT_PENDING, &pf->state))
7088 i40e_service_event_schedule(pf);
7089}
7090
7091/**
7092 * i40e_service_timer - timer callback
7093 * @data: pointer to PF struct
7094 **/
7095static void i40e_service_timer(unsigned long data)
7096{
7097 struct i40e_pf *pf = (struct i40e_pf *)data;
7098
7099 mod_timer(&pf->service_timer,
7100 round_jiffies(jiffies + pf->service_timer_period));
7101 i40e_service_event_schedule(pf);
7102}
7103
7104/**
7105 * i40e_set_num_rings_in_vsi - Determine number of rings in the VSI
7106 * @vsi: the VSI being configured
7107 **/
7108static int i40e_set_num_rings_in_vsi(struct i40e_vsi *vsi)
7109{
7110 struct i40e_pf *pf = vsi->back;
7111
7112 switch (vsi->type) {
7113 case I40E_VSI_MAIN:
7114 vsi->alloc_queue_pairs = pf->num_lan_qps;
7115 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
7116 I40E_REQ_DESCRIPTOR_MULTIPLE);
7117 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
7118 vsi->num_q_vectors = pf->num_lan_msix;
7119 else
7120 vsi->num_q_vectors = 1;
7121
7122 break;
7123
7124 case I40E_VSI_FDIR:
7125 vsi->alloc_queue_pairs = 1;
7126 vsi->num_desc = ALIGN(I40E_FDIR_RING_COUNT,
7127 I40E_REQ_DESCRIPTOR_MULTIPLE);
7128 vsi->num_q_vectors = 1;
7129 break;
7130
7131 case I40E_VSI_VMDQ2:
7132 vsi->alloc_queue_pairs = pf->num_vmdq_qps;
7133 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
7134 I40E_REQ_DESCRIPTOR_MULTIPLE);
7135 vsi->num_q_vectors = pf->num_vmdq_msix;
7136 break;
7137
7138 case I40E_VSI_SRIOV:
7139 vsi->alloc_queue_pairs = pf->num_vf_qps;
7140 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
7141 I40E_REQ_DESCRIPTOR_MULTIPLE);
7142 break;
7143
Vasu Dev38e00432014-08-01 13:27:03 -07007144#ifdef I40E_FCOE
7145 case I40E_VSI_FCOE:
7146 vsi->alloc_queue_pairs = pf->num_fcoe_qps;
7147 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
7148 I40E_REQ_DESCRIPTOR_MULTIPLE);
7149 vsi->num_q_vectors = pf->num_fcoe_msix;
7150 break;
7151
7152#endif /* I40E_FCOE */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007153 default:
7154 WARN_ON(1);
7155 return -ENODATA;
7156 }
7157
7158 return 0;
7159}
7160
7161/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007162 * i40e_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the vsi
7163 * @type: VSI pointer
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007164 * @alloc_qvectors: a bool to specify if q_vectors need to be allocated.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007165 *
7166 * On error: returns error code (negative)
7167 * On success: returns 0
7168 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007169static int i40e_vsi_alloc_arrays(struct i40e_vsi *vsi, bool alloc_qvectors)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007170{
7171 int size;
7172 int ret = 0;
7173
Shannon Nelsonac6c5e32013-11-20 10:02:57 +00007174 /* allocate memory for both Tx and Rx ring pointers */
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007175 size = sizeof(struct i40e_ring *) * vsi->alloc_queue_pairs * 2;
7176 vsi->tx_rings = kzalloc(size, GFP_KERNEL);
7177 if (!vsi->tx_rings)
7178 return -ENOMEM;
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007179 vsi->rx_rings = &vsi->tx_rings[vsi->alloc_queue_pairs];
7180
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007181 if (alloc_qvectors) {
7182 /* allocate memory for q_vector pointers */
Julia Lawallf57e4fb2014-07-30 03:11:09 +00007183 size = sizeof(struct i40e_q_vector *) * vsi->num_q_vectors;
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007184 vsi->q_vectors = kzalloc(size, GFP_KERNEL);
7185 if (!vsi->q_vectors) {
7186 ret = -ENOMEM;
7187 goto err_vectors;
7188 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007189 }
7190 return ret;
7191
7192err_vectors:
7193 kfree(vsi->tx_rings);
7194 return ret;
7195}
7196
7197/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007198 * i40e_vsi_mem_alloc - Allocates the next available struct vsi in the PF
7199 * @pf: board private structure
7200 * @type: type of VSI
7201 *
7202 * On error: returns error code (negative)
7203 * On success: returns vsi index in PF (positive)
7204 **/
7205static int i40e_vsi_mem_alloc(struct i40e_pf *pf, enum i40e_vsi_type type)
7206{
7207 int ret = -ENODEV;
7208 struct i40e_vsi *vsi;
7209 int vsi_idx;
7210 int i;
7211
7212 /* Need to protect the allocation of the VSIs at the PF level */
7213 mutex_lock(&pf->switch_mutex);
7214
7215 /* VSI list may be fragmented if VSI creation/destruction has
7216 * been happening. We can afford to do a quick scan to look
7217 * for any free VSIs in the list.
7218 *
7219 * find next empty vsi slot, looping back around if necessary
7220 */
7221 i = pf->next_vsi;
Mitch Williams505682c2014-05-20 08:01:37 +00007222 while (i < pf->num_alloc_vsi && pf->vsi[i])
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007223 i++;
Mitch Williams505682c2014-05-20 08:01:37 +00007224 if (i >= pf->num_alloc_vsi) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007225 i = 0;
7226 while (i < pf->next_vsi && pf->vsi[i])
7227 i++;
7228 }
7229
Mitch Williams505682c2014-05-20 08:01:37 +00007230 if (i < pf->num_alloc_vsi && !pf->vsi[i]) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007231 vsi_idx = i; /* Found one! */
7232 } else {
7233 ret = -ENODEV;
Alexander Duyck493fb302013-09-28 07:01:44 +00007234 goto unlock_pf; /* out of VSI slots! */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007235 }
7236 pf->next_vsi = ++i;
7237
7238 vsi = kzalloc(sizeof(*vsi), GFP_KERNEL);
7239 if (!vsi) {
7240 ret = -ENOMEM;
Alexander Duyck493fb302013-09-28 07:01:44 +00007241 goto unlock_pf;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007242 }
7243 vsi->type = type;
7244 vsi->back = pf;
7245 set_bit(__I40E_DOWN, &vsi->state);
7246 vsi->flags = 0;
7247 vsi->idx = vsi_idx;
7248 vsi->rx_itr_setting = pf->rx_itr_default;
7249 vsi->tx_itr_setting = pf->tx_itr_default;
Jesse Brandeburgac26fc12015-09-28 14:12:37 -04007250 vsi->int_rate_limit = 0;
Anjali Singhai Jain5db4cb52015-02-24 06:58:49 +00007251 vsi->rss_table_size = (vsi->type == I40E_VSI_MAIN) ?
7252 pf->rss_table_size : 64;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007253 vsi->netdev_registered = false;
7254 vsi->work_limit = I40E_DEFAULT_IRQ_WORK;
7255 INIT_LIST_HEAD(&vsi->mac_filter_list);
Shannon Nelson63741842014-04-23 04:50:16 +00007256 vsi->irqs_ready = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007257
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00007258 ret = i40e_set_num_rings_in_vsi(vsi);
7259 if (ret)
7260 goto err_rings;
7261
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007262 ret = i40e_vsi_alloc_arrays(vsi, true);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007263 if (ret)
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00007264 goto err_rings;
Alexander Duyck493fb302013-09-28 07:01:44 +00007265
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007266 /* Setup default MSIX irq handler for VSI */
7267 i40e_vsi_setup_irqhandler(vsi, i40e_msix_clean_rings);
7268
Kiran Patil21659032015-09-30 14:09:03 -04007269 /* Initialize VSI lock */
7270 spin_lock_init(&vsi->mac_filter_list_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007271 pf->vsi[vsi_idx] = vsi;
7272 ret = vsi_idx;
Alexander Duyck493fb302013-09-28 07:01:44 +00007273 goto unlock_pf;
7274
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00007275err_rings:
Alexander Duyck493fb302013-09-28 07:01:44 +00007276 pf->next_vsi = i - 1;
7277 kfree(vsi);
7278unlock_pf:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007279 mutex_unlock(&pf->switch_mutex);
7280 return ret;
7281}
7282
7283/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007284 * i40e_vsi_free_arrays - Free queue and vector pointer arrays for the VSI
7285 * @type: VSI pointer
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007286 * @free_qvectors: a bool to specify if q_vectors need to be freed.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007287 *
7288 * On error: returns error code (negative)
7289 * On success: returns 0
7290 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007291static void i40e_vsi_free_arrays(struct i40e_vsi *vsi, bool free_qvectors)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007292{
7293 /* free the ring and vector containers */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007294 if (free_qvectors) {
7295 kfree(vsi->q_vectors);
7296 vsi->q_vectors = NULL;
7297 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007298 kfree(vsi->tx_rings);
7299 vsi->tx_rings = NULL;
7300 vsi->rx_rings = NULL;
7301}
7302
7303/**
Helin Zhang28c58692015-10-26 19:44:27 -04007304 * i40e_clear_rss_config_user - clear the user configured RSS hash keys
7305 * and lookup table
7306 * @vsi: Pointer to VSI structure
7307 */
7308static void i40e_clear_rss_config_user(struct i40e_vsi *vsi)
7309{
7310 if (!vsi)
7311 return;
7312
7313 kfree(vsi->rss_hkey_user);
7314 vsi->rss_hkey_user = NULL;
7315
7316 kfree(vsi->rss_lut_user);
7317 vsi->rss_lut_user = NULL;
7318}
7319
7320/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007321 * i40e_vsi_clear - Deallocate the VSI provided
7322 * @vsi: the VSI being un-configured
7323 **/
7324static int i40e_vsi_clear(struct i40e_vsi *vsi)
7325{
7326 struct i40e_pf *pf;
7327
7328 if (!vsi)
7329 return 0;
7330
7331 if (!vsi->back)
7332 goto free_vsi;
7333 pf = vsi->back;
7334
7335 mutex_lock(&pf->switch_mutex);
7336 if (!pf->vsi[vsi->idx]) {
7337 dev_err(&pf->pdev->dev, "pf->vsi[%d] is NULL, just free vsi[%d](%p,type %d)\n",
7338 vsi->idx, vsi->idx, vsi, vsi->type);
7339 goto unlock_vsi;
7340 }
7341
7342 if (pf->vsi[vsi->idx] != vsi) {
7343 dev_err(&pf->pdev->dev,
7344 "pf->vsi[%d](%p, type %d) != vsi[%d](%p,type %d): no free!\n",
7345 pf->vsi[vsi->idx]->idx,
7346 pf->vsi[vsi->idx],
7347 pf->vsi[vsi->idx]->type,
7348 vsi->idx, vsi, vsi->type);
7349 goto unlock_vsi;
7350 }
7351
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00007352 /* updates the PF for this cleared vsi */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007353 i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
7354 i40e_put_lump(pf->irq_pile, vsi->base_vector, vsi->idx);
7355
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007356 i40e_vsi_free_arrays(vsi, true);
Helin Zhang28c58692015-10-26 19:44:27 -04007357 i40e_clear_rss_config_user(vsi);
Alexander Duyck493fb302013-09-28 07:01:44 +00007358
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007359 pf->vsi[vsi->idx] = NULL;
7360 if (vsi->idx < pf->next_vsi)
7361 pf->next_vsi = vsi->idx;
7362
7363unlock_vsi:
7364 mutex_unlock(&pf->switch_mutex);
7365free_vsi:
7366 kfree(vsi);
7367
7368 return 0;
7369}
7370
7371/**
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00007372 * i40e_vsi_clear_rings - Deallocates the Rx and Tx rings for the provided VSI
7373 * @vsi: the VSI being cleaned
7374 **/
Shannon Nelsonbe1d5ee2013-11-28 06:39:23 +00007375static void i40e_vsi_clear_rings(struct i40e_vsi *vsi)
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00007376{
7377 int i;
7378
Greg Rose8e9dca52013-12-18 13:45:53 +00007379 if (vsi->tx_rings && vsi->tx_rings[0]) {
Neerav Parikhd7397642013-11-28 06:39:37 +00007380 for (i = 0; i < vsi->alloc_queue_pairs; i++) {
Mitch Williams00403f02013-09-28 07:13:13 +00007381 kfree_rcu(vsi->tx_rings[i], rcu);
7382 vsi->tx_rings[i] = NULL;
7383 vsi->rx_rings[i] = NULL;
7384 }
Shannon Nelsonbe1d5ee2013-11-28 06:39:23 +00007385 }
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00007386}
7387
7388/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007389 * i40e_alloc_rings - Allocates the Rx and Tx rings for the provided VSI
7390 * @vsi: the VSI being configured
7391 **/
7392static int i40e_alloc_rings(struct i40e_vsi *vsi)
7393{
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +00007394 struct i40e_ring *tx_ring, *rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007395 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007396 int i;
7397
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007398 /* Set basic values in the rings to be used later during open() */
Neerav Parikhd7397642013-11-28 06:39:37 +00007399 for (i = 0; i < vsi->alloc_queue_pairs; i++) {
Shannon Nelsonac6c5e32013-11-20 10:02:57 +00007400 /* allocate space for both Tx and Rx in one shot */
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00007401 tx_ring = kzalloc(sizeof(struct i40e_ring) * 2, GFP_KERNEL);
7402 if (!tx_ring)
7403 goto err_out;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007404
7405 tx_ring->queue_index = i;
7406 tx_ring->reg_idx = vsi->base_queue + i;
7407 tx_ring->ring_active = false;
7408 tx_ring->vsi = vsi;
7409 tx_ring->netdev = vsi->netdev;
7410 tx_ring->dev = &pf->pdev->dev;
7411 tx_ring->count = vsi->num_desc;
7412 tx_ring->size = 0;
7413 tx_ring->dcb_tc = 0;
Anjali Singhai Jain8e0764b2015-06-05 12:20:30 -04007414 if (vsi->back->flags & I40E_FLAG_WB_ON_ITR_CAPABLE)
7415 tx_ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
Anjali Singhai Jain527274c2015-06-05 12:20:31 -04007416 if (vsi->back->flags & I40E_FLAG_OUTER_UDP_CSUM_CAPABLE)
7417 tx_ring->flags |= I40E_TXR_FLAGS_OUTER_UDP_CSUM;
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00007418 vsi->tx_rings[i] = tx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007419
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00007420 rx_ring = &tx_ring[1];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007421 rx_ring->queue_index = i;
7422 rx_ring->reg_idx = vsi->base_queue + i;
7423 rx_ring->ring_active = false;
7424 rx_ring->vsi = vsi;
7425 rx_ring->netdev = vsi->netdev;
7426 rx_ring->dev = &pf->pdev->dev;
7427 rx_ring->count = vsi->num_desc;
7428 rx_ring->size = 0;
7429 rx_ring->dcb_tc = 0;
7430 if (pf->flags & I40E_FLAG_16BYTE_RX_DESC_ENABLED)
7431 set_ring_16byte_desc_enabled(rx_ring);
7432 else
7433 clear_ring_16byte_desc_enabled(rx_ring);
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00007434 vsi->rx_rings[i] = rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007435 }
7436
7437 return 0;
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00007438
7439err_out:
7440 i40e_vsi_clear_rings(vsi);
7441 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007442}
7443
7444/**
7445 * i40e_reserve_msix_vectors - Reserve MSI-X vectors in the kernel
7446 * @pf: board private structure
7447 * @vectors: the number of MSI-X vectors to request
7448 *
7449 * Returns the number of vectors reserved, or error
7450 **/
7451static int i40e_reserve_msix_vectors(struct i40e_pf *pf, int vectors)
7452{
Alexander Gordeev7b37f372014-02-18 11:11:42 +01007453 vectors = pci_enable_msix_range(pf->pdev, pf->msix_entries,
7454 I40E_MIN_MSIX, vectors);
7455 if (vectors < 0) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007456 dev_info(&pf->pdev->dev,
Alexander Gordeev7b37f372014-02-18 11:11:42 +01007457 "MSI-X vector reservation failed: %d\n", vectors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007458 vectors = 0;
7459 }
7460
7461 return vectors;
7462}
7463
7464/**
7465 * i40e_init_msix - Setup the MSIX capability
7466 * @pf: board private structure
7467 *
7468 * Work with the OS to set up the MSIX vectors needed.
7469 *
Shannon Nelson3b444392015-02-26 16:15:57 +00007470 * Returns the number of vectors reserved or negative on failure
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007471 **/
7472static int i40e_init_msix(struct i40e_pf *pf)
7473{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007474 struct i40e_hw *hw = &pf->hw;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007475 int vectors_left;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007476 int v_budget, i;
Shannon Nelson3b444392015-02-26 16:15:57 +00007477 int v_actual;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007478
7479 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
7480 return -ENODEV;
7481
7482 /* The number of vectors we'll request will be comprised of:
7483 * - Add 1 for "other" cause for Admin Queue events, etc.
7484 * - The number of LAN queue pairs
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00007485 * - Queues being used for RSS.
7486 * We don't need as many as max_rss_size vectors.
7487 * use rss_size instead in the calculation since that
7488 * is governed by number of cpus in the system.
7489 * - assumes symmetric Tx/Rx pairing
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007490 * - The number of VMDq pairs
Vasu Dev38e00432014-08-01 13:27:03 -07007491#ifdef I40E_FCOE
7492 * - The number of FCOE qps.
7493#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007494 * Once we count this up, try the request.
7495 *
7496 * If we can't get what we want, we'll simplify to nearly nothing
7497 * and try again. If that still fails, we punt.
7498 */
Shannon Nelson1e200e42015-02-27 09:15:24 +00007499 vectors_left = hw->func_caps.num_msix_vectors;
7500 v_budget = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007501
Shannon Nelson1e200e42015-02-27 09:15:24 +00007502 /* reserve one vector for miscellaneous handler */
7503 if (vectors_left) {
7504 v_budget++;
7505 vectors_left--;
7506 }
7507
7508 /* reserve vectors for the main PF traffic queues */
7509 pf->num_lan_msix = min_t(int, num_online_cpus(), vectors_left);
7510 vectors_left -= pf->num_lan_msix;
7511 v_budget += pf->num_lan_msix;
7512
7513 /* reserve one vector for sideband flow director */
7514 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
7515 if (vectors_left) {
7516 v_budget++;
7517 vectors_left--;
7518 } else {
7519 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
7520 }
7521 }
John W Linville83840e42015-01-14 03:06:28 +00007522
Vasu Dev38e00432014-08-01 13:27:03 -07007523#ifdef I40E_FCOE
Shannon Nelson1e200e42015-02-27 09:15:24 +00007524 /* can we reserve enough for FCoE? */
Vasu Dev38e00432014-08-01 13:27:03 -07007525 if (pf->flags & I40E_FLAG_FCOE_ENABLED) {
Shannon Nelson1e200e42015-02-27 09:15:24 +00007526 if (!vectors_left)
7527 pf->num_fcoe_msix = 0;
7528 else if (vectors_left >= pf->num_fcoe_qps)
7529 pf->num_fcoe_msix = pf->num_fcoe_qps;
7530 else
7531 pf->num_fcoe_msix = 1;
Vasu Dev38e00432014-08-01 13:27:03 -07007532 v_budget += pf->num_fcoe_msix;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007533 vectors_left -= pf->num_fcoe_msix;
Vasu Dev38e00432014-08-01 13:27:03 -07007534 }
Shannon Nelson1e200e42015-02-27 09:15:24 +00007535
Vasu Dev38e00432014-08-01 13:27:03 -07007536#endif
Shannon Nelson1e200e42015-02-27 09:15:24 +00007537 /* any vectors left over go for VMDq support */
7538 if (pf->flags & I40E_FLAG_VMDQ_ENABLED) {
7539 int vmdq_vecs_wanted = pf->num_vmdq_vsis * pf->num_vmdq_qps;
7540 int vmdq_vecs = min_t(int, vectors_left, vmdq_vecs_wanted);
7541
7542 /* if we're short on vectors for what's desired, we limit
7543 * the queues per vmdq. If this is still more than are
7544 * available, the user will need to change the number of
7545 * queues/vectors used by the PF later with the ethtool
7546 * channels command
7547 */
7548 if (vmdq_vecs < vmdq_vecs_wanted)
7549 pf->num_vmdq_qps = 1;
7550 pf->num_vmdq_msix = pf->num_vmdq_qps;
7551
7552 v_budget += vmdq_vecs;
7553 vectors_left -= vmdq_vecs;
7554 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007555
7556 pf->msix_entries = kcalloc(v_budget, sizeof(struct msix_entry),
7557 GFP_KERNEL);
7558 if (!pf->msix_entries)
7559 return -ENOMEM;
7560
7561 for (i = 0; i < v_budget; i++)
7562 pf->msix_entries[i].entry = i;
Shannon Nelson3b444392015-02-26 16:15:57 +00007563 v_actual = i40e_reserve_msix_vectors(pf, v_budget);
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007564
Shannon Nelson3b444392015-02-26 16:15:57 +00007565 if (v_actual != v_budget) {
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007566 /* If we have limited resources, we will start with no vectors
7567 * for the special features and then allocate vectors to some
7568 * of these features based on the policy and at the end disable
7569 * the features that did not get any vectors.
7570 */
Vasu Dev38e00432014-08-01 13:27:03 -07007571#ifdef I40E_FCOE
7572 pf->num_fcoe_qps = 0;
7573 pf->num_fcoe_msix = 0;
7574#endif
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007575 pf->num_vmdq_msix = 0;
7576 }
7577
Shannon Nelson3b444392015-02-26 16:15:57 +00007578 if (v_actual < I40E_MIN_MSIX) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007579 pf->flags &= ~I40E_FLAG_MSIX_ENABLED;
7580 kfree(pf->msix_entries);
7581 pf->msix_entries = NULL;
7582 return -ENODEV;
7583
Shannon Nelson3b444392015-02-26 16:15:57 +00007584 } else if (v_actual == I40E_MIN_MSIX) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007585 /* Adjust for minimal MSIX use */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007586 pf->num_vmdq_vsis = 0;
7587 pf->num_vmdq_qps = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007588 pf->num_lan_qps = 1;
7589 pf->num_lan_msix = 1;
7590
Shannon Nelson3b444392015-02-26 16:15:57 +00007591 } else if (v_actual != v_budget) {
7592 int vec;
7593
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007594 /* reserve the misc vector */
Shannon Nelson3b444392015-02-26 16:15:57 +00007595 vec = v_actual - 1;
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007596
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007597 /* Scale vector usage down */
7598 pf->num_vmdq_msix = 1; /* force VMDqs to only one vector */
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007599 pf->num_vmdq_vsis = 1;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007600 pf->num_vmdq_qps = 1;
7601 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007602
7603 /* partition out the remaining vectors */
7604 switch (vec) {
7605 case 2:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007606 pf->num_lan_msix = 1;
7607 break;
7608 case 3:
Vasu Dev38e00432014-08-01 13:27:03 -07007609#ifdef I40E_FCOE
7610 /* give one vector to FCoE */
7611 if (pf->flags & I40E_FLAG_FCOE_ENABLED) {
7612 pf->num_lan_msix = 1;
7613 pf->num_fcoe_msix = 1;
7614 }
7615#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007616 pf->num_lan_msix = 2;
Vasu Dev38e00432014-08-01 13:27:03 -07007617#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007618 break;
7619 default:
Vasu Dev38e00432014-08-01 13:27:03 -07007620#ifdef I40E_FCOE
7621 /* give one vector to FCoE */
7622 if (pf->flags & I40E_FLAG_FCOE_ENABLED) {
7623 pf->num_fcoe_msix = 1;
7624 vec--;
7625 }
7626#endif
Shannon Nelson1e200e42015-02-27 09:15:24 +00007627 /* give the rest to the PF */
7628 pf->num_lan_msix = min_t(int, vec, pf->num_lan_qps);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007629 break;
7630 }
7631 }
7632
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007633 if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
7634 (pf->num_vmdq_msix == 0)) {
7635 dev_info(&pf->pdev->dev, "VMDq disabled, not enough MSI-X vectors\n");
7636 pf->flags &= ~I40E_FLAG_VMDQ_ENABLED;
7637 }
Vasu Dev38e00432014-08-01 13:27:03 -07007638#ifdef I40E_FCOE
7639
7640 if ((pf->flags & I40E_FLAG_FCOE_ENABLED) && (pf->num_fcoe_msix == 0)) {
7641 dev_info(&pf->pdev->dev, "FCOE disabled, not enough MSI-X vectors\n");
7642 pf->flags &= ~I40E_FLAG_FCOE_ENABLED;
7643 }
7644#endif
Shannon Nelson3b444392015-02-26 16:15:57 +00007645 return v_actual;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007646}
7647
7648/**
Greg Rose90e04072014-03-06 08:59:57 +00007649 * i40e_vsi_alloc_q_vector - Allocate memory for a single interrupt vector
Alexander Duyck493fb302013-09-28 07:01:44 +00007650 * @vsi: the VSI being configured
7651 * @v_idx: index of the vector in the vsi struct
7652 *
7653 * We allocate one q_vector. If allocation fails we return -ENOMEM.
7654 **/
Greg Rose90e04072014-03-06 08:59:57 +00007655static int i40e_vsi_alloc_q_vector(struct i40e_vsi *vsi, int v_idx)
Alexander Duyck493fb302013-09-28 07:01:44 +00007656{
7657 struct i40e_q_vector *q_vector;
7658
7659 /* allocate q_vector */
7660 q_vector = kzalloc(sizeof(struct i40e_q_vector), GFP_KERNEL);
7661 if (!q_vector)
7662 return -ENOMEM;
7663
7664 q_vector->vsi = vsi;
7665 q_vector->v_idx = v_idx;
7666 cpumask_set_cpu(v_idx, &q_vector->affinity_mask);
7667 if (vsi->netdev)
7668 netif_napi_add(vsi->netdev, &q_vector->napi,
Jesse Brandeburgeefeace2014-05-10 04:49:13 +00007669 i40e_napi_poll, NAPI_POLL_WEIGHT);
Alexander Duyck493fb302013-09-28 07:01:44 +00007670
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00007671 q_vector->rx.latency_range = I40E_LOW_LATENCY;
7672 q_vector->tx.latency_range = I40E_LOW_LATENCY;
7673
Alexander Duyck493fb302013-09-28 07:01:44 +00007674 /* tie q_vector and vsi together */
7675 vsi->q_vectors[v_idx] = q_vector;
7676
7677 return 0;
7678}
7679
7680/**
Greg Rose90e04072014-03-06 08:59:57 +00007681 * i40e_vsi_alloc_q_vectors - Allocate memory for interrupt vectors
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007682 * @vsi: the VSI being configured
7683 *
7684 * We allocate one q_vector per queue interrupt. If allocation fails we
7685 * return -ENOMEM.
7686 **/
Greg Rose90e04072014-03-06 08:59:57 +00007687static int i40e_vsi_alloc_q_vectors(struct i40e_vsi *vsi)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007688{
7689 struct i40e_pf *pf = vsi->back;
7690 int v_idx, num_q_vectors;
Alexander Duyck493fb302013-09-28 07:01:44 +00007691 int err;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007692
7693 /* if not MSIX, give the one vector only to the LAN VSI */
7694 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
7695 num_q_vectors = vsi->num_q_vectors;
7696 else if (vsi == pf->vsi[pf->lan_vsi])
7697 num_q_vectors = 1;
7698 else
7699 return -EINVAL;
7700
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007701 for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
Greg Rose90e04072014-03-06 08:59:57 +00007702 err = i40e_vsi_alloc_q_vector(vsi, v_idx);
Alexander Duyck493fb302013-09-28 07:01:44 +00007703 if (err)
7704 goto err_out;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007705 }
7706
7707 return 0;
Alexander Duyck493fb302013-09-28 07:01:44 +00007708
7709err_out:
7710 while (v_idx--)
7711 i40e_free_q_vector(vsi, v_idx);
7712
7713 return err;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007714}
7715
7716/**
7717 * i40e_init_interrupt_scheme - Determine proper interrupt scheme
7718 * @pf: board private structure to initialize
7719 **/
Jesse Brandeburgc11472802015-04-07 19:45:39 -04007720static int i40e_init_interrupt_scheme(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007721{
Shannon Nelson3b444392015-02-26 16:15:57 +00007722 int vectors = 0;
7723 ssize_t size;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007724
7725 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
Shannon Nelson3b444392015-02-26 16:15:57 +00007726 vectors = i40e_init_msix(pf);
7727 if (vectors < 0) {
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08007728 pf->flags &= ~(I40E_FLAG_MSIX_ENABLED |
Vasu Dev38e00432014-08-01 13:27:03 -07007729#ifdef I40E_FCOE
7730 I40E_FLAG_FCOE_ENABLED |
7731#endif
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08007732 I40E_FLAG_RSS_ENABLED |
Neerav Parikh4d9b6042014-05-22 06:31:51 +00007733 I40E_FLAG_DCB_CAPABLE |
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08007734 I40E_FLAG_SRIOV_ENABLED |
7735 I40E_FLAG_FD_SB_ENABLED |
7736 I40E_FLAG_FD_ATR_ENABLED |
7737 I40E_FLAG_VMDQ_ENABLED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007738
7739 /* rework the queue expectations without MSIX */
7740 i40e_determine_queue_usage(pf);
7741 }
7742 }
7743
7744 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED) &&
7745 (pf->flags & I40E_FLAG_MSI_ENABLED)) {
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08007746 dev_info(&pf->pdev->dev, "MSI-X not available, trying MSI\n");
Shannon Nelson3b444392015-02-26 16:15:57 +00007747 vectors = pci_enable_msi(pf->pdev);
7748 if (vectors < 0) {
7749 dev_info(&pf->pdev->dev, "MSI init failed - %d\n",
7750 vectors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007751 pf->flags &= ~I40E_FLAG_MSI_ENABLED;
7752 }
Shannon Nelson3b444392015-02-26 16:15:57 +00007753 vectors = 1; /* one MSI or Legacy vector */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007754 }
7755
Shannon Nelson958a3e32013-09-28 07:13:28 +00007756 if (!(pf->flags & (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED)))
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08007757 dev_info(&pf->pdev->dev, "MSI-X and MSI not available, falling back to Legacy IRQ\n");
Shannon Nelson958a3e32013-09-28 07:13:28 +00007758
Shannon Nelson3b444392015-02-26 16:15:57 +00007759 /* set up vector assignment tracking */
7760 size = sizeof(struct i40e_lump_tracking) + (sizeof(u16) * vectors);
7761 pf->irq_pile = kzalloc(size, GFP_KERNEL);
Jesse Brandeburgc11472802015-04-07 19:45:39 -04007762 if (!pf->irq_pile) {
7763 dev_err(&pf->pdev->dev, "error allocating irq_pile memory\n");
7764 return -ENOMEM;
7765 }
Shannon Nelson3b444392015-02-26 16:15:57 +00007766 pf->irq_pile->num_entries = vectors;
7767 pf->irq_pile->search_hint = 0;
7768
Jesse Brandeburgc11472802015-04-07 19:45:39 -04007769 /* track first vector for misc interrupts, ignore return */
Shannon Nelson3b444392015-02-26 16:15:57 +00007770 (void)i40e_get_lump(pf, pf->irq_pile, 1, I40E_PILE_VALID_BIT - 1);
Jesse Brandeburgc11472802015-04-07 19:45:39 -04007771
7772 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007773}
7774
7775/**
7776 * i40e_setup_misc_vector - Setup the misc vector to handle non queue events
7777 * @pf: board private structure
7778 *
7779 * This sets up the handler for MSIX 0, which is used to manage the
7780 * non-queue interrupts, e.g. AdminQ and errors. This is not used
7781 * when in MSI or Legacy interrupt mode.
7782 **/
7783static int i40e_setup_misc_vector(struct i40e_pf *pf)
7784{
7785 struct i40e_hw *hw = &pf->hw;
7786 int err = 0;
7787
7788 /* Only request the irq if this is the first time through, and
7789 * not when we're rebuilding after a Reset
7790 */
7791 if (!test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state)) {
7792 err = request_irq(pf->msix_entries[0].vector,
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00007793 i40e_intr, 0, pf->int_name, pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007794 if (err) {
7795 dev_info(&pf->pdev->dev,
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08007796 "request_irq for %s failed: %d\n",
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00007797 pf->int_name, err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007798 return -EFAULT;
7799 }
7800 }
7801
Jacob Kellerab437b52014-12-14 01:55:08 +00007802 i40e_enable_misc_int_causes(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007803
7804 /* associate no queues to the misc vector */
7805 wr32(hw, I40E_PFINT_LNKLST0, I40E_QUEUE_END_OF_LIST);
7806 wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), I40E_ITR_8K);
7807
7808 i40e_flush(hw);
7809
7810 i40e_irq_dynamic_enable_icr0(pf);
7811
7812 return err;
7813}
7814
7815/**
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04007816 * i40e_config_rss_aq - Prepare for RSS using AQ commands
7817 * @vsi: vsi structure
7818 * @seed: RSS hash seed
7819 **/
Helin Zhange69ff812015-10-21 19:56:22 -04007820static int i40e_config_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
7821 u8 *lut, u16 lut_size)
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04007822{
7823 struct i40e_aqc_get_set_rss_key_data rss_key;
7824 struct i40e_pf *pf = vsi->back;
7825 struct i40e_hw *hw = &pf->hw;
7826 bool pf_lut = false;
7827 u8 *rss_lut;
7828 int ret, i;
7829
7830 memset(&rss_key, 0, sizeof(rss_key));
7831 memcpy(&rss_key, seed, sizeof(rss_key));
7832
7833 rss_lut = kzalloc(pf->rss_table_size, GFP_KERNEL);
7834 if (!rss_lut)
7835 return -ENOMEM;
7836
7837 /* Populate the LUT with max no. of queues in round robin fashion */
7838 for (i = 0; i < vsi->rss_table_size; i++)
7839 rss_lut[i] = i % vsi->rss_size;
7840
7841 ret = i40e_aq_set_rss_key(hw, vsi->id, &rss_key);
7842 if (ret) {
7843 dev_info(&pf->pdev->dev,
7844 "Cannot set RSS key, err %s aq_err %s\n",
7845 i40e_stat_str(&pf->hw, ret),
7846 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Anjali Singhai126b63d2015-08-24 13:26:53 -07007847 goto config_rss_aq_out;
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04007848 }
7849
7850 if (vsi->type == I40E_VSI_MAIN)
7851 pf_lut = true;
7852
7853 ret = i40e_aq_set_rss_lut(hw, vsi->id, pf_lut, rss_lut,
7854 vsi->rss_table_size);
7855 if (ret)
7856 dev_info(&pf->pdev->dev,
7857 "Cannot set RSS lut, err %s aq_err %s\n",
7858 i40e_stat_str(&pf->hw, ret),
7859 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
7860
Anjali Singhai126b63d2015-08-24 13:26:53 -07007861config_rss_aq_out:
7862 kfree(rss_lut);
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04007863 return ret;
7864}
7865
7866/**
7867 * i40e_vsi_config_rss - Prepare for VSI(VMDq) RSS if used
7868 * @vsi: VSI structure
7869 **/
7870static int i40e_vsi_config_rss(struct i40e_vsi *vsi)
7871{
7872 u8 seed[I40E_HKEY_ARRAY_SIZE];
7873 struct i40e_pf *pf = vsi->back;
Helin Zhange69ff812015-10-21 19:56:22 -04007874 u8 *lut;
7875 int ret;
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04007876
Helin Zhange69ff812015-10-21 19:56:22 -04007877 if (!(pf->flags & I40E_FLAG_RSS_AQ_CAPABLE))
7878 return 0;
7879
7880 lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
7881 if (!lut)
7882 return -ENOMEM;
7883
7884 i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04007885 netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
7886 vsi->rss_size = min_t(int, pf->rss_size, vsi->num_queue_pairs);
Helin Zhange69ff812015-10-21 19:56:22 -04007887 ret = i40e_config_rss_aq(vsi, seed, lut, vsi->rss_table_size);
7888 kfree(lut);
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04007889
Helin Zhange69ff812015-10-21 19:56:22 -04007890 return ret;
7891}
7892
7893/**
Helin Zhang043dd652015-10-21 19:56:23 -04007894 * i40e_config_rss_reg - Configure RSS keys and lut by writing registers
Helin Zhange69ff812015-10-21 19:56:22 -04007895 * @vsi: Pointer to vsi structure
7896 * @seed: RSS hash seed
7897 * @lut: Lookup table
7898 * @lut_size: Lookup table size
7899 *
7900 * Returns 0 on success, negative on failure
7901 **/
7902static int i40e_config_rss_reg(struct i40e_vsi *vsi, const u8 *seed,
7903 const u8 *lut, u16 lut_size)
7904{
7905 struct i40e_pf *pf = vsi->back;
7906 struct i40e_hw *hw = &pf->hw;
7907 u8 i;
7908
7909 /* Fill out hash function seed */
7910 if (seed) {
7911 u32 *seed_dw = (u32 *)seed;
7912
7913 for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
7914 wr32(hw, I40E_PFQF_HKEY(i), seed_dw[i]);
7915 }
7916
7917 if (lut) {
7918 u32 *lut_dw = (u32 *)lut;
7919
7920 if (lut_size != I40E_HLUT_ARRAY_SIZE)
7921 return -EINVAL;
7922
7923 for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
7924 wr32(hw, I40E_PFQF_HLUT(i), lut_dw[i]);
7925 }
7926 i40e_flush(hw);
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04007927
7928 return 0;
7929}
7930
7931/**
Helin Zhang043dd652015-10-21 19:56:23 -04007932 * i40e_get_rss_reg - Get the RSS keys and lut by reading registers
7933 * @vsi: Pointer to VSI structure
7934 * @seed: Buffer to store the keys
7935 * @lut: Buffer to store the lookup table entries
7936 * @lut_size: Size of buffer to store the lookup table entries
7937 *
7938 * Returns 0 on success, negative on failure
7939 */
7940static int i40e_get_rss_reg(struct i40e_vsi *vsi, u8 *seed,
7941 u8 *lut, u16 lut_size)
7942{
7943 struct i40e_pf *pf = vsi->back;
7944 struct i40e_hw *hw = &pf->hw;
7945 u16 i;
7946
7947 if (seed) {
7948 u32 *seed_dw = (u32 *)seed;
7949
7950 for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
7951 seed_dw[i] = rd32(hw, I40E_PFQF_HKEY(i));
7952 }
7953 if (lut) {
7954 u32 *lut_dw = (u32 *)lut;
7955
7956 if (lut_size != I40E_HLUT_ARRAY_SIZE)
7957 return -EINVAL;
7958 for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
7959 lut_dw[i] = rd32(hw, I40E_PFQF_HLUT(i));
7960 }
7961
7962 return 0;
7963}
7964
7965/**
7966 * i40e_config_rss - Configure RSS keys and lut
7967 * @vsi: Pointer to VSI structure
7968 * @seed: RSS hash seed
7969 * @lut: Lookup table
7970 * @lut_size: Lookup table size
7971 *
7972 * Returns 0 on success, negative on failure
7973 */
7974int i40e_config_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
7975{
7976 struct i40e_pf *pf = vsi->back;
7977
7978 if (pf->flags & I40E_FLAG_RSS_AQ_CAPABLE)
7979 return i40e_config_rss_aq(vsi, seed, lut, lut_size);
7980 else
7981 return i40e_config_rss_reg(vsi, seed, lut, lut_size);
7982}
7983
7984/**
7985 * i40e_get_rss - Get RSS keys and lut
7986 * @vsi: Pointer to VSI structure
7987 * @seed: Buffer to store the keys
7988 * @lut: Buffer to store the lookup table entries
7989 * lut_size: Size of buffer to store the lookup table entries
7990 *
7991 * Returns 0 on success, negative on failure
7992 */
7993int i40e_get_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
7994{
7995 return i40e_get_rss_reg(vsi, seed, lut, lut_size);
7996}
7997
7998/**
Helin Zhange69ff812015-10-21 19:56:22 -04007999 * i40e_fill_rss_lut - Fill the RSS lookup table with default values
8000 * @pf: Pointer to board private structure
8001 * @lut: Lookup table
8002 * @rss_table_size: Lookup table size
8003 * @rss_size: Range of queue number for hashing
8004 */
8005static void i40e_fill_rss_lut(struct i40e_pf *pf, u8 *lut,
8006 u16 rss_table_size, u16 rss_size)
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04008007{
Helin Zhange69ff812015-10-21 19:56:22 -04008008 u16 i;
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04008009
Helin Zhange69ff812015-10-21 19:56:22 -04008010 for (i = 0; i < rss_table_size; i++)
8011 lut[i] = i % rss_size;
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04008012}
8013
8014/**
Helin Zhang043dd652015-10-21 19:56:23 -04008015 * i40e_pf_config_rss - Prepare for RSS if used
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008016 * @pf: board private structure
8017 **/
Helin Zhang043dd652015-10-21 19:56:23 -04008018static int i40e_pf_config_rss(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008019{
Anjali Singhai Jain66ddcff2015-02-24 06:58:50 +00008020 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04008021 u8 seed[I40E_HKEY_ARRAY_SIZE];
Helin Zhange69ff812015-10-21 19:56:22 -04008022 u8 *lut;
Anjali Singhai Jain4617e8c2013-11-20 10:02:56 +00008023 struct i40e_hw *hw = &pf->hw;
Carolyn Wybornye157ea32014-06-03 23:50:22 +00008024 u32 reg_val;
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04008025 u64 hena;
Helin Zhange69ff812015-10-21 19:56:22 -04008026 int ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008027
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008028 /* By default we enable TCP/UDP with IPv4/IPv6 ptypes */
8029 hena = (u64)rd32(hw, I40E_PFQF_HENA(0)) |
8030 ((u64)rd32(hw, I40E_PFQF_HENA(1)) << 32);
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04008031 hena |= i40e_pf_get_default_rss_hena(pf);
8032
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008033 wr32(hw, I40E_PFQF_HENA(0), (u32)hena);
8034 wr32(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
8035
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04008036 /* Determine the RSS table size based on the hardware capabilities */
Carolyn Wybornye157ea32014-06-03 23:50:22 +00008037 reg_val = rd32(hw, I40E_PFQF_CTL_0);
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04008038 reg_val = (pf->rss_table_size == 512) ?
8039 (reg_val | I40E_PFQF_CTL_0_HASHLUTSIZE_512) :
8040 (reg_val & ~I40E_PFQF_CTL_0_HASHLUTSIZE_512);
Carolyn Wybornye157ea32014-06-03 23:50:22 +00008041 wr32(hw, I40E_PFQF_CTL_0, reg_val);
8042
Helin Zhang28c58692015-10-26 19:44:27 -04008043 /* Determine the RSS size of the VSI */
8044 if (!vsi->rss_size)
8045 vsi->rss_size = min_t(int, pf->rss_size, vsi->num_queue_pairs);
8046
Helin Zhange69ff812015-10-21 19:56:22 -04008047 lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
8048 if (!lut)
8049 return -ENOMEM;
8050
Helin Zhang28c58692015-10-26 19:44:27 -04008051 /* Use user configured lut if there is one, otherwise use default */
8052 if (vsi->rss_lut_user)
8053 memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
8054 else
8055 i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
Helin Zhange69ff812015-10-21 19:56:22 -04008056
Helin Zhang28c58692015-10-26 19:44:27 -04008057 /* Use user configured hash key if there is one, otherwise
8058 * use default.
8059 */
8060 if (vsi->rss_hkey_user)
8061 memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
8062 else
8063 netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
Helin Zhang043dd652015-10-21 19:56:23 -04008064 ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
Helin Zhange69ff812015-10-21 19:56:22 -04008065 kfree(lut);
8066
8067 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008068}
8069
8070/**
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008071 * i40e_reconfig_rss_queues - change number of queues for rss and rebuild
8072 * @pf: board private structure
8073 * @queue_count: the requested queue count for rss.
8074 *
8075 * returns 0 if rss is not enabled, if enabled returns the final rss queue
8076 * count which may be different from the requested queue count.
8077 **/
8078int i40e_reconfig_rss_queues(struct i40e_pf *pf, int queue_count)
8079{
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00008080 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
8081 int new_rss_size;
8082
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008083 if (!(pf->flags & I40E_FLAG_RSS_ENABLED))
8084 return 0;
8085
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00008086 new_rss_size = min_t(int, queue_count, pf->rss_size_max);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008087
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00008088 if (queue_count != vsi->num_queue_pairs) {
8089 vsi->req_queue_pairs = queue_count;
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008090 i40e_prep_for_reset(pf);
8091
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00008092 pf->rss_size = new_rss_size;
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008093
8094 i40e_reset_and_rebuild(pf, true);
Helin Zhang28c58692015-10-26 19:44:27 -04008095
8096 /* Discard the user configured hash keys and lut, if less
8097 * queues are enabled.
8098 */
8099 if (queue_count < vsi->rss_size) {
8100 i40e_clear_rss_config_user(vsi);
8101 dev_dbg(&pf->pdev->dev,
8102 "discard user configured hash keys and lut\n");
8103 }
8104
8105 /* Reset vsi->rss_size, as number of enabled queues changed */
8106 vsi->rss_size = min_t(int, pf->rss_size, vsi->num_queue_pairs);
8107
Helin Zhang043dd652015-10-21 19:56:23 -04008108 i40e_pf_config_rss(pf);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008109 }
8110 dev_info(&pf->pdev->dev, "RSS count: %d\n", pf->rss_size);
8111 return pf->rss_size;
8112}
8113
8114/**
Greg Rosef4492db2015-02-06 08:52:12 +00008115 * i40e_get_npar_bw_setting - Retrieve BW settings for this PF partition
8116 * @pf: board private structure
8117 **/
8118i40e_status i40e_get_npar_bw_setting(struct i40e_pf *pf)
8119{
8120 i40e_status status;
8121 bool min_valid, max_valid;
8122 u32 max_bw, min_bw;
8123
8124 status = i40e_read_bw_from_alt_ram(&pf->hw, &max_bw, &min_bw,
8125 &min_valid, &max_valid);
8126
8127 if (!status) {
8128 if (min_valid)
8129 pf->npar_min_bw = min_bw;
8130 if (max_valid)
8131 pf->npar_max_bw = max_bw;
8132 }
8133
8134 return status;
8135}
8136
8137/**
8138 * i40e_set_npar_bw_setting - Set BW settings for this PF partition
8139 * @pf: board private structure
8140 **/
8141i40e_status i40e_set_npar_bw_setting(struct i40e_pf *pf)
8142{
8143 struct i40e_aqc_configure_partition_bw_data bw_data;
8144 i40e_status status;
8145
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00008146 /* Set the valid bit for this PF */
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04008147 bw_data.pf_valid_bits = cpu_to_le16(BIT(pf->hw.pf_id));
Greg Rosef4492db2015-02-06 08:52:12 +00008148 bw_data.max_bw[pf->hw.pf_id] = pf->npar_max_bw & I40E_ALT_BW_VALUE_MASK;
8149 bw_data.min_bw[pf->hw.pf_id] = pf->npar_min_bw & I40E_ALT_BW_VALUE_MASK;
8150
8151 /* Set the new bandwidths */
8152 status = i40e_aq_configure_partition_bw(&pf->hw, &bw_data, NULL);
8153
8154 return status;
8155}
8156
8157/**
8158 * i40e_commit_npar_bw_setting - Commit BW settings for this PF partition
8159 * @pf: board private structure
8160 **/
8161i40e_status i40e_commit_npar_bw_setting(struct i40e_pf *pf)
8162{
8163 /* Commit temporary BW setting to permanent NVM image */
8164 enum i40e_admin_queue_err last_aq_status;
8165 i40e_status ret;
8166 u16 nvm_word;
8167
8168 if (pf->hw.partition_id != 1) {
8169 dev_info(&pf->pdev->dev,
8170 "Commit BW only works on partition 1! This is partition %d",
8171 pf->hw.partition_id);
8172 ret = I40E_NOT_SUPPORTED;
8173 goto bw_commit_out;
8174 }
8175
8176 /* Acquire NVM for read access */
8177 ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_READ);
8178 last_aq_status = pf->hw.aq.asq_last_status;
8179 if (ret) {
8180 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008181 "Cannot acquire NVM for read access, err %s aq_err %s\n",
8182 i40e_stat_str(&pf->hw, ret),
8183 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +00008184 goto bw_commit_out;
8185 }
8186
8187 /* Read word 0x10 of NVM - SW compatibility word 1 */
8188 ret = i40e_aq_read_nvm(&pf->hw,
8189 I40E_SR_NVM_CONTROL_WORD,
8190 0x10, sizeof(nvm_word), &nvm_word,
8191 false, NULL);
8192 /* Save off last admin queue command status before releasing
8193 * the NVM
8194 */
8195 last_aq_status = pf->hw.aq.asq_last_status;
8196 i40e_release_nvm(&pf->hw);
8197 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008198 dev_info(&pf->pdev->dev, "NVM read error, err %s aq_err %s\n",
8199 i40e_stat_str(&pf->hw, ret),
8200 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +00008201 goto bw_commit_out;
8202 }
8203
8204 /* Wait a bit for NVM release to complete */
8205 msleep(50);
8206
8207 /* Acquire NVM for write access */
8208 ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_WRITE);
8209 last_aq_status = pf->hw.aq.asq_last_status;
8210 if (ret) {
8211 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008212 "Cannot acquire NVM for write access, err %s aq_err %s\n",
8213 i40e_stat_str(&pf->hw, ret),
8214 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +00008215 goto bw_commit_out;
8216 }
8217 /* Write it back out unchanged to initiate update NVM,
8218 * which will force a write of the shadow (alt) RAM to
8219 * the NVM - thus storing the bandwidth values permanently.
8220 */
8221 ret = i40e_aq_update_nvm(&pf->hw,
8222 I40E_SR_NVM_CONTROL_WORD,
8223 0x10, sizeof(nvm_word),
8224 &nvm_word, true, NULL);
8225 /* Save off last admin queue command status before releasing
8226 * the NVM
8227 */
8228 last_aq_status = pf->hw.aq.asq_last_status;
8229 i40e_release_nvm(&pf->hw);
8230 if (ret)
8231 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008232 "BW settings NOT SAVED, err %s aq_err %s\n",
8233 i40e_stat_str(&pf->hw, ret),
8234 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +00008235bw_commit_out:
8236
8237 return ret;
8238}
8239
8240/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008241 * i40e_sw_init - Initialize general software structures (struct i40e_pf)
8242 * @pf: board private structure to initialize
8243 *
8244 * i40e_sw_init initializes the Adapter private data structure.
8245 * Fields are initialized based on PCI device information and
8246 * OS network device settings (MTU size).
8247 **/
8248static int i40e_sw_init(struct i40e_pf *pf)
8249{
8250 int err = 0;
8251 int size;
8252
8253 pf->msg_enable = netif_msg_init(I40E_DEFAULT_MSG_ENABLE,
8254 (NETIF_MSG_DRV|NETIF_MSG_PROBE|NETIF_MSG_LINK));
Shannon Nelson27599972013-11-16 10:00:43 +00008255 pf->hw.debug_mask = pf->msg_enable | I40E_DEBUG_DIAG;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008256 if (debug != -1 && debug != I40E_DEFAULT_MSG_ENABLE) {
8257 if (I40E_DEBUG_USER & debug)
8258 pf->hw.debug_mask = debug;
8259 pf->msg_enable = netif_msg_init((debug & ~I40E_DEBUG_USER),
8260 I40E_DEFAULT_MSG_ENABLE);
8261 }
8262
8263 /* Set default capability flags */
8264 pf->flags = I40E_FLAG_RX_CSUM_ENABLED |
8265 I40E_FLAG_MSI_ENABLED |
Shannon Nelson9ac77262015-08-27 11:42:40 -04008266 I40E_FLAG_LINK_POLLING_ENABLED |
Mitch Williams2bc7ee82015-02-06 08:52:11 +00008267 I40E_FLAG_MSIX_ENABLED;
8268
8269 if (iommu_present(&pci_bus_type))
8270 pf->flags |= I40E_FLAG_RX_PS_ENABLED;
8271 else
8272 pf->flags |= I40E_FLAG_RX_1BUF_ENABLED;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008273
Mitch Williamsca99eb92014-04-04 04:43:07 +00008274 /* Set default ITR */
8275 pf->rx_itr_default = I40E_ITR_DYNAMIC | I40E_ITR_RX_DEF;
8276 pf->tx_itr_default = I40E_ITR_DYNAMIC | I40E_ITR_TX_DEF;
8277
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00008278 /* Depending on PF configurations, it is possible that the RSS
8279 * maximum might end up larger than the available queues
8280 */
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04008281 pf->rss_size_max = BIT(pf->hw.func_caps.rss_table_entry_width);
Paul M Stillwell Jrec9a7db2014-07-09 07:46:10 +00008282 pf->rss_size = 1;
Anjali Singhai Jain5db4cb52015-02-24 06:58:49 +00008283 pf->rss_table_size = pf->hw.func_caps.rss_table_size;
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00008284 pf->rss_size_max = min_t(int, pf->rss_size_max,
8285 pf->hw.func_caps.num_tx_qp);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008286 if (pf->hw.func_caps.rss) {
8287 pf->flags |= I40E_FLAG_RSS_ENABLED;
Jesse Brandeburgbf051a32013-11-26 10:49:17 +00008288 pf->rss_size = min_t(int, pf->rss_size_max, num_online_cpus());
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008289 }
8290
Catherine Sullivan2050bc62013-12-18 13:46:03 +00008291 /* MFP mode enabled */
Pawel Orlowskic78b9532015-04-22 19:34:06 -04008292 if (pf->hw.func_caps.npar_enable || pf->hw.func_caps.flex10_enable) {
Catherine Sullivan2050bc62013-12-18 13:46:03 +00008293 pf->flags |= I40E_FLAG_MFP_ENABLED;
8294 dev_info(&pf->pdev->dev, "MFP mode Enabled\n");
Greg Rosef4492db2015-02-06 08:52:12 +00008295 if (i40e_get_npar_bw_setting(pf))
8296 dev_warn(&pf->pdev->dev,
8297 "Could not get NPAR bw settings\n");
8298 else
8299 dev_info(&pf->pdev->dev,
8300 "Min BW = %8.8x, Max BW = %8.8x\n",
8301 pf->npar_min_bw, pf->npar_max_bw);
Catherine Sullivan2050bc62013-12-18 13:46:03 +00008302 }
8303
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008304 /* FW/NVM is not yet fixed in this regard */
8305 if ((pf->hw.func_caps.fd_filters_guaranteed > 0) ||
8306 (pf->hw.func_caps.fd_filters_best_effort > 0)) {
8307 pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
8308 pf->atr_sample_rate = I40E_DEFAULT_ATR_SAMPLE_RATE;
Shannon Nelson6eae9c62015-09-03 17:18:55 -04008309 if (pf->flags & I40E_FLAG_MFP_ENABLED &&
8310 pf->hw.num_partitions > 1)
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008311 dev_info(&pf->pdev->dev,
Anjali Singhai Jain0b675842014-03-06 08:59:51 +00008312 "Flow Director Sideband mode Disabled in MFP mode\n");
Shannon Nelson6eae9c62015-09-03 17:18:55 -04008313 else
8314 pf->flags |= I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008315 pf->fdir_pf_filter_count =
8316 pf->hw.func_caps.fd_filters_guaranteed;
8317 pf->hw.fdir_shared_filter_count =
8318 pf->hw.func_caps.fd_filters_best_effort;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008319 }
8320
8321 if (pf->hw.func_caps.vmdq) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008322 pf->num_vmdq_vsis = I40E_DEFAULT_NUM_VMDQ_VSI;
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04008323 pf->flags |= I40E_FLAG_VMDQ_ENABLED;
Jesse Brandeburge9e53662015-10-02 17:57:21 -07008324 pf->num_vmdq_qps = i40e_default_queues_per_vmdq(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008325 }
8326
Vasu Dev38e00432014-08-01 13:27:03 -07008327#ifdef I40E_FCOE
Shannon Nelson21364bc2015-08-26 15:14:13 -04008328 i40e_init_pf_fcoe(pf);
Vasu Dev38e00432014-08-01 13:27:03 -07008329
8330#endif /* I40E_FCOE */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008331#ifdef CONFIG_PCI_IOV
Shannon Nelsonba252f12014-12-11 07:06:34 +00008332 if (pf->hw.func_caps.num_vfs && pf->hw.partition_id == 1) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008333 pf->num_vf_qps = I40E_DEFAULT_QUEUES_PER_VF;
8334 pf->flags |= I40E_FLAG_SRIOV_ENABLED;
8335 pf->num_req_vfs = min_t(int,
8336 pf->hw.func_caps.num_vfs,
8337 I40E_MAX_VF_COUNT);
8338 }
8339#endif /* CONFIG_PCI_IOV */
Anjali Singhai Jaind502ce02015-06-05 12:20:26 -04008340 if (pf->hw.mac.type == I40E_MAC_X722) {
8341 pf->flags |= I40E_FLAG_RSS_AQ_CAPABLE |
8342 I40E_FLAG_128_QP_RSS_CAPABLE |
8343 I40E_FLAG_HW_ATR_EVICT_CAPABLE |
8344 I40E_FLAG_OUTER_UDP_CSUM_CAPABLE |
8345 I40E_FLAG_WB_ON_ITR_CAPABLE |
8346 I40E_FLAG_MULTIPLE_TCP_UDP_RSS_PCTYPE;
8347 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008348 pf->eeprom_version = 0xDEAD;
8349 pf->lan_veb = I40E_NO_VEB;
8350 pf->lan_vsi = I40E_NO_VSI;
8351
Anjali Singhai Jaind1a8d272015-07-23 16:54:40 -04008352 /* By default FW has this off for performance reasons */
8353 pf->flags &= ~I40E_FLAG_VEB_STATS_ENABLED;
8354
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008355 /* set up queue assignment tracking */
8356 size = sizeof(struct i40e_lump_tracking)
8357 + (sizeof(u16) * pf->hw.func_caps.num_tx_qp);
8358 pf->qp_pile = kzalloc(size, GFP_KERNEL);
8359 if (!pf->qp_pile) {
8360 err = -ENOMEM;
8361 goto sw_init_done;
8362 }
8363 pf->qp_pile->num_entries = pf->hw.func_caps.num_tx_qp;
8364 pf->qp_pile->search_hint = 0;
8365
Anjali Singhai Jain327fe042014-06-04 01:23:26 +00008366 pf->tx_timeout_recovery_level = 1;
8367
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008368 mutex_init(&pf->switch_mutex);
8369
Greg Rosec668a122015-02-26 16:10:39 +00008370 /* If NPAR is enabled nudge the Tx scheduler */
8371 if (pf->hw.func_caps.npar_enable && (!i40e_get_npar_bw_setting(pf)))
8372 i40e_set_npar_bw_setting(pf);
8373
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008374sw_init_done:
8375 return err;
8376}
8377
8378/**
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008379 * i40e_set_ntuple - set the ntuple feature flag and take action
8380 * @pf: board private structure to initialize
8381 * @features: the feature set that the stack is suggesting
8382 *
8383 * returns a bool to indicate if reset needs to happen
8384 **/
8385bool i40e_set_ntuple(struct i40e_pf *pf, netdev_features_t features)
8386{
8387 bool need_reset = false;
8388
8389 /* Check if Flow Director n-tuple support was enabled or disabled. If
8390 * the state changed, we need to reset.
8391 */
8392 if (features & NETIF_F_NTUPLE) {
8393 /* Enable filters and mark for reset */
8394 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
8395 need_reset = true;
8396 pf->flags |= I40E_FLAG_FD_SB_ENABLED;
8397 } else {
8398 /* turn off filters, mark for reset and clear SW filter list */
8399 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
8400 need_reset = true;
8401 i40e_fdir_filter_exit(pf);
8402 }
8403 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jain8a4f34f2014-06-04 08:45:20 +00008404 pf->auto_disable_flags &= ~I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00008405 /* reset fd counters */
8406 pf->fd_add_err = pf->fd_atr_cnt = pf->fd_tcp_rule = 0;
8407 pf->fdir_pf_active_filters = 0;
8408 pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
Anjali Singhai Jain2e4875e2015-04-16 20:06:06 -04008409 if (I40E_DEBUG_FD & pf->hw.debug_mask)
8410 dev_info(&pf->pdev->dev, "ATR re-enabled.\n");
Anjali Singhai Jain8a4f34f2014-06-04 08:45:20 +00008411 /* if ATR was auto disabled it can be re-enabled. */
8412 if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
8413 (pf->auto_disable_flags & I40E_FLAG_FD_ATR_ENABLED))
8414 pf->auto_disable_flags &= ~I40E_FLAG_FD_ATR_ENABLED;
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008415 }
8416 return need_reset;
8417}
8418
8419/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008420 * i40e_set_features - set the netdev feature flags
8421 * @netdev: ptr to the netdev being adjusted
8422 * @features: the feature set that the stack is suggesting
8423 **/
8424static int i40e_set_features(struct net_device *netdev,
8425 netdev_features_t features)
8426{
8427 struct i40e_netdev_priv *np = netdev_priv(netdev);
8428 struct i40e_vsi *vsi = np->vsi;
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008429 struct i40e_pf *pf = vsi->back;
8430 bool need_reset;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008431
8432 if (features & NETIF_F_HW_VLAN_CTAG_RX)
8433 i40e_vlan_stripping_enable(vsi);
8434 else
8435 i40e_vlan_stripping_disable(vsi);
8436
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008437 need_reset = i40e_set_ntuple(pf, features);
8438
8439 if (need_reset)
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04008440 i40e_do_reset(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED));
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008441
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008442 return 0;
8443}
8444
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008445#ifdef CONFIG_I40E_VXLAN
8446/**
8447 * i40e_get_vxlan_port_idx - Lookup a possibly offloaded for Rx UDP port
8448 * @pf: board private structure
8449 * @port: The UDP port to look up
8450 *
8451 * Returns the index number or I40E_MAX_PF_UDP_OFFLOAD_PORTS if port not found
8452 **/
8453static u8 i40e_get_vxlan_port_idx(struct i40e_pf *pf, __be16 port)
8454{
8455 u8 i;
8456
8457 for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
8458 if (pf->vxlan_ports[i] == port)
8459 return i;
8460 }
8461
8462 return i;
8463}
8464
8465/**
8466 * i40e_add_vxlan_port - Get notifications about VXLAN ports that come up
8467 * @netdev: This physical port's netdev
8468 * @sa_family: Socket Family that VXLAN is notifying us about
8469 * @port: New UDP port number that VXLAN started listening to
8470 **/
8471static void i40e_add_vxlan_port(struct net_device *netdev,
8472 sa_family_t sa_family, __be16 port)
8473{
8474 struct i40e_netdev_priv *np = netdev_priv(netdev);
8475 struct i40e_vsi *vsi = np->vsi;
8476 struct i40e_pf *pf = vsi->back;
8477 u8 next_idx;
8478 u8 idx;
8479
8480 if (sa_family == AF_INET6)
8481 return;
8482
8483 idx = i40e_get_vxlan_port_idx(pf, port);
8484
8485 /* Check if port already exists */
8486 if (idx < I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
Shannon Nelsonc22c06c2015-03-31 00:45:04 -07008487 netdev_info(netdev, "vxlan port %d already offloaded\n",
8488 ntohs(port));
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008489 return;
8490 }
8491
8492 /* Now check if there is space to add the new port */
8493 next_idx = i40e_get_vxlan_port_idx(pf, 0);
8494
8495 if (next_idx == I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
Shannon Nelsonc22c06c2015-03-31 00:45:04 -07008496 netdev_info(netdev, "maximum number of vxlan UDP ports reached, not adding port %d\n",
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008497 ntohs(port));
8498 return;
8499 }
8500
8501 /* New port: add it and mark its index in the bitmap */
8502 pf->vxlan_ports[next_idx] = port;
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04008503 pf->pending_vxlan_bitmap |= BIT_ULL(next_idx);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008504 pf->flags |= I40E_FLAG_VXLAN_FILTER_SYNC;
8505}
8506
8507/**
8508 * i40e_del_vxlan_port - Get notifications about VXLAN ports that go away
8509 * @netdev: This physical port's netdev
8510 * @sa_family: Socket Family that VXLAN is notifying us about
8511 * @port: UDP port number that VXLAN stopped listening to
8512 **/
8513static void i40e_del_vxlan_port(struct net_device *netdev,
8514 sa_family_t sa_family, __be16 port)
8515{
8516 struct i40e_netdev_priv *np = netdev_priv(netdev);
8517 struct i40e_vsi *vsi = np->vsi;
8518 struct i40e_pf *pf = vsi->back;
8519 u8 idx;
8520
8521 if (sa_family == AF_INET6)
8522 return;
8523
8524 idx = i40e_get_vxlan_port_idx(pf, port);
8525
8526 /* Check if port already exists */
8527 if (idx < I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
8528 /* if port exists, set it to 0 (mark for deletion)
8529 * and make it pending
8530 */
8531 pf->vxlan_ports[idx] = 0;
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04008532 pf->pending_vxlan_bitmap |= BIT_ULL(idx);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008533 pf->flags |= I40E_FLAG_VXLAN_FILTER_SYNC;
8534 } else {
Shannon Nelsonc22c06c2015-03-31 00:45:04 -07008535 netdev_warn(netdev, "vxlan port %d was not found, not deleting\n",
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008536 ntohs(port));
8537 }
8538}
8539
8540#endif
Neerav Parikh1f224ad2014-02-12 01:45:31 +00008541static int i40e_get_phys_port_id(struct net_device *netdev,
Jiri Pirko02637fc2014-11-28 14:34:16 +01008542 struct netdev_phys_item_id *ppid)
Neerav Parikh1f224ad2014-02-12 01:45:31 +00008543{
8544 struct i40e_netdev_priv *np = netdev_priv(netdev);
8545 struct i40e_pf *pf = np->vsi->back;
8546 struct i40e_hw *hw = &pf->hw;
8547
8548 if (!(pf->flags & I40E_FLAG_PORT_ID_VALID))
8549 return -EOPNOTSUPP;
8550
8551 ppid->id_len = min_t(int, sizeof(hw->mac.port_addr), sizeof(ppid->id));
8552 memcpy(ppid->id, hw->mac.port_addr, ppid->id_len);
8553
8554 return 0;
8555}
8556
Jesse Brandeburg2f90ade2014-11-20 16:30:02 -08008557/**
8558 * i40e_ndo_fdb_add - add an entry to the hardware database
8559 * @ndm: the input from the stack
8560 * @tb: pointer to array of nladdr (unused)
8561 * @dev: the net device pointer
8562 * @addr: the MAC address entry being added
8563 * @flags: instructions from stack about fdb operation
8564 */
Greg Rose4ba0dea2014-03-06 08:59:55 +00008565static int i40e_ndo_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
8566 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +01008567 const unsigned char *addr, u16 vid,
Greg Rose4ba0dea2014-03-06 08:59:55 +00008568 u16 flags)
Greg Rose4ba0dea2014-03-06 08:59:55 +00008569{
8570 struct i40e_netdev_priv *np = netdev_priv(dev);
8571 struct i40e_pf *pf = np->vsi->back;
8572 int err = 0;
8573
8574 if (!(pf->flags & I40E_FLAG_SRIOV_ENABLED))
8575 return -EOPNOTSUPP;
8576
Or Gerlitz65891fe2014-12-14 18:19:05 +02008577 if (vid) {
8578 pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
8579 return -EINVAL;
8580 }
8581
Greg Rose4ba0dea2014-03-06 08:59:55 +00008582 /* Hardware does not support aging addresses so if a
8583 * ndm_state is given only allow permanent addresses
8584 */
8585 if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
8586 netdev_info(dev, "FDB only supports static addresses\n");
8587 return -EINVAL;
8588 }
8589
8590 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
8591 err = dev_uc_add_excl(dev, addr);
8592 else if (is_multicast_ether_addr(addr))
8593 err = dev_mc_add_excl(dev, addr);
8594 else
8595 err = -EINVAL;
8596
8597 /* Only return duplicate errors if NLM_F_EXCL is set */
8598 if (err == -EEXIST && !(flags & NLM_F_EXCL))
8599 err = 0;
8600
8601 return err;
8602}
8603
Neerav Parikh51616012015-02-06 08:52:14 +00008604/**
8605 * i40e_ndo_bridge_setlink - Set the hardware bridge mode
8606 * @dev: the netdev being configured
8607 * @nlh: RTNL message
8608 *
8609 * Inserts a new hardware bridge if not already created and
8610 * enables the bridging mode requested (VEB or VEPA). If the
8611 * hardware bridge has already been inserted and the request
8612 * is to change the mode then that requires a PF reset to
8613 * allow rebuild of the components with required hardware
8614 * bridge mode enabled.
8615 **/
8616static int i40e_ndo_bridge_setlink(struct net_device *dev,
Carolyn Wyborny9df70b62015-04-27 14:57:11 -04008617 struct nlmsghdr *nlh,
8618 u16 flags)
Neerav Parikh51616012015-02-06 08:52:14 +00008619{
8620 struct i40e_netdev_priv *np = netdev_priv(dev);
8621 struct i40e_vsi *vsi = np->vsi;
8622 struct i40e_pf *pf = vsi->back;
8623 struct i40e_veb *veb = NULL;
8624 struct nlattr *attr, *br_spec;
8625 int i, rem;
8626
8627 /* Only for PF VSI for now */
8628 if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
8629 return -EOPNOTSUPP;
8630
8631 /* Find the HW bridge for PF VSI */
8632 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
8633 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
8634 veb = pf->veb[i];
8635 }
8636
8637 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
8638
8639 nla_for_each_nested(attr, br_spec, rem) {
8640 __u16 mode;
8641
8642 if (nla_type(attr) != IFLA_BRIDGE_MODE)
8643 continue;
8644
8645 mode = nla_get_u16(attr);
8646 if ((mode != BRIDGE_MODE_VEPA) &&
8647 (mode != BRIDGE_MODE_VEB))
8648 return -EINVAL;
8649
8650 /* Insert a new HW bridge */
8651 if (!veb) {
8652 veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
8653 vsi->tc_config.enabled_tc);
8654 if (veb) {
8655 veb->bridge_mode = mode;
8656 i40e_config_bridge_mode(veb);
8657 } else {
8658 /* No Bridge HW offload available */
8659 return -ENOENT;
8660 }
8661 break;
8662 } else if (mode != veb->bridge_mode) {
8663 /* Existing HW bridge but different mode needs reset */
8664 veb->bridge_mode = mode;
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07008665 /* TODO: If no VFs or VMDq VSIs, disallow VEB mode */
8666 if (mode == BRIDGE_MODE_VEB)
8667 pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
8668 else
8669 pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
8670 i40e_do_reset(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED));
Neerav Parikh51616012015-02-06 08:52:14 +00008671 break;
8672 }
8673 }
8674
8675 return 0;
8676}
8677
8678/**
8679 * i40e_ndo_bridge_getlink - Get the hardware bridge mode
8680 * @skb: skb buff
8681 * @pid: process id
8682 * @seq: RTNL message seq #
8683 * @dev: the netdev being configured
8684 * @filter_mask: unused
Jesse Brandeburgd4b2f9f2015-09-03 17:18:48 -04008685 * @nlflags: netlink flags passed in
Neerav Parikh51616012015-02-06 08:52:14 +00008686 *
8687 * Return the mode in which the hardware bridge is operating in
8688 * i.e VEB or VEPA.
8689 **/
Neerav Parikh51616012015-02-06 08:52:14 +00008690static int i40e_ndo_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
8691 struct net_device *dev,
Carolyn Wyborny9f4ffc42015-08-31 19:54:42 -04008692 u32 __always_unused filter_mask,
8693 int nlflags)
Neerav Parikh51616012015-02-06 08:52:14 +00008694{
8695 struct i40e_netdev_priv *np = netdev_priv(dev);
8696 struct i40e_vsi *vsi = np->vsi;
8697 struct i40e_pf *pf = vsi->back;
8698 struct i40e_veb *veb = NULL;
8699 int i;
8700
8701 /* Only for PF VSI for now */
8702 if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
8703 return -EOPNOTSUPP;
8704
8705 /* Find the HW bridge for the PF VSI */
8706 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
8707 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
8708 veb = pf->veb[i];
8709 }
8710
8711 if (!veb)
8712 return 0;
8713
Nicolas Dichtel46c264d2015-04-28 18:33:49 +02008714 return ndo_dflt_bridge_getlink(skb, pid, seq, dev, veb->bridge_mode,
Scott Feldman7d4f8d82015-06-22 00:27:17 -07008715 nlflags, 0, 0, filter_mask, NULL);
Neerav Parikh51616012015-02-06 08:52:14 +00008716}
Neerav Parikh51616012015-02-06 08:52:14 +00008717
Joe Stringerf44a75e2015-04-14 17:09:14 -07008718#define I40E_MAX_TUNNEL_HDR_LEN 80
8719/**
8720 * i40e_features_check - Validate encapsulated packet conforms to limits
8721 * @skb: skb buff
Jean Sacren2bc11c62015-09-19 05:08:43 -06008722 * @dev: This physical port's netdev
Joe Stringerf44a75e2015-04-14 17:09:14 -07008723 * @features: Offload features that the stack believes apply
8724 **/
8725static netdev_features_t i40e_features_check(struct sk_buff *skb,
8726 struct net_device *dev,
8727 netdev_features_t features)
8728{
8729 if (skb->encapsulation &&
8730 (skb_inner_mac_header(skb) - skb_transport_header(skb) >
8731 I40E_MAX_TUNNEL_HDR_LEN))
8732 return features & ~(NETIF_F_ALL_CSUM | NETIF_F_GSO_MASK);
8733
8734 return features;
8735}
8736
Shannon Nelson37a29732015-02-27 09:15:19 +00008737static const struct net_device_ops i40e_netdev_ops = {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008738 .ndo_open = i40e_open,
8739 .ndo_stop = i40e_close,
8740 .ndo_start_xmit = i40e_lan_xmit_frame,
8741 .ndo_get_stats64 = i40e_get_netdev_stats_struct,
8742 .ndo_set_rx_mode = i40e_set_rx_mode,
8743 .ndo_validate_addr = eth_validate_addr,
8744 .ndo_set_mac_address = i40e_set_mac,
8745 .ndo_change_mtu = i40e_change_mtu,
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00008746 .ndo_do_ioctl = i40e_ioctl,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008747 .ndo_tx_timeout = i40e_tx_timeout,
8748 .ndo_vlan_rx_add_vid = i40e_vlan_rx_add_vid,
8749 .ndo_vlan_rx_kill_vid = i40e_vlan_rx_kill_vid,
8750#ifdef CONFIG_NET_POLL_CONTROLLER
8751 .ndo_poll_controller = i40e_netpoll,
8752#endif
8753 .ndo_setup_tc = i40e_setup_tc,
Vasu Dev38e00432014-08-01 13:27:03 -07008754#ifdef I40E_FCOE
8755 .ndo_fcoe_enable = i40e_fcoe_enable,
8756 .ndo_fcoe_disable = i40e_fcoe_disable,
8757#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008758 .ndo_set_features = i40e_set_features,
8759 .ndo_set_vf_mac = i40e_ndo_set_vf_mac,
8760 .ndo_set_vf_vlan = i40e_ndo_set_vf_port_vlan,
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04008761 .ndo_set_vf_rate = i40e_ndo_set_vf_bw,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008762 .ndo_get_vf_config = i40e_ndo_get_vf_config,
Mitch Williams588aefa2014-02-11 08:27:49 +00008763 .ndo_set_vf_link_state = i40e_ndo_set_vf_link_state,
Serey Konge6d90042014-07-12 07:28:14 +00008764 .ndo_set_vf_spoofchk = i40e_ndo_set_vf_spoofchk,
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008765#ifdef CONFIG_I40E_VXLAN
8766 .ndo_add_vxlan_port = i40e_add_vxlan_port,
8767 .ndo_del_vxlan_port = i40e_del_vxlan_port,
8768#endif
Neerav Parikh1f224ad2014-02-12 01:45:31 +00008769 .ndo_get_phys_port_id = i40e_get_phys_port_id,
Greg Rose4ba0dea2014-03-06 08:59:55 +00008770 .ndo_fdb_add = i40e_ndo_fdb_add,
Joe Stringerf44a75e2015-04-14 17:09:14 -07008771 .ndo_features_check = i40e_features_check,
Neerav Parikh51616012015-02-06 08:52:14 +00008772 .ndo_bridge_getlink = i40e_ndo_bridge_getlink,
8773 .ndo_bridge_setlink = i40e_ndo_bridge_setlink,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008774};
8775
8776/**
8777 * i40e_config_netdev - Setup the netdev flags
8778 * @vsi: the VSI being configured
8779 *
8780 * Returns 0 on success, negative value on failure
8781 **/
8782static int i40e_config_netdev(struct i40e_vsi *vsi)
8783{
Greg Rose1a103702013-11-28 06:42:39 +00008784 u8 brdcast[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008785 struct i40e_pf *pf = vsi->back;
8786 struct i40e_hw *hw = &pf->hw;
8787 struct i40e_netdev_priv *np;
8788 struct net_device *netdev;
8789 u8 mac_addr[ETH_ALEN];
8790 int etherdev_size;
8791
8792 etherdev_size = sizeof(struct i40e_netdev_priv);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008793 netdev = alloc_etherdev_mq(etherdev_size, vsi->alloc_queue_pairs);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008794 if (!netdev)
8795 return -ENOMEM;
8796
8797 vsi->netdev = netdev;
8798 np = netdev_priv(netdev);
8799 np->vsi = vsi;
8800
Or Gerlitzd70e9412014-03-18 10:36:45 +02008801 netdev->hw_enc_features |= NETIF_F_IP_CSUM |
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008802 NETIF_F_GSO_UDP_TUNNEL |
Jesse Brandeburgfec31ff2015-10-01 18:25:42 -07008803 NETIF_F_GSO_GRE |
Or Gerlitzd70e9412014-03-18 10:36:45 +02008804 NETIF_F_TSO;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008805
8806 netdev->features = NETIF_F_SG |
8807 NETIF_F_IP_CSUM |
8808 NETIF_F_SCTP_CSUM |
8809 NETIF_F_HIGHDMA |
8810 NETIF_F_GSO_UDP_TUNNEL |
Jesse Brandeburgfec31ff2015-10-01 18:25:42 -07008811 NETIF_F_GSO_GRE |
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008812 NETIF_F_HW_VLAN_CTAG_TX |
8813 NETIF_F_HW_VLAN_CTAG_RX |
8814 NETIF_F_HW_VLAN_CTAG_FILTER |
8815 NETIF_F_IPV6_CSUM |
8816 NETIF_F_TSO |
Jesse Brandeburg059dab62014-04-01 09:07:20 +00008817 NETIF_F_TSO_ECN |
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008818 NETIF_F_TSO6 |
8819 NETIF_F_RXCSUM |
8820 NETIF_F_RXHASH |
8821 0;
8822
Anjali Singhai Jain2e86a0b2014-04-01 07:11:53 +00008823 if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
8824 netdev->features |= NETIF_F_NTUPLE;
8825
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008826 /* copy netdev features into list of user selectable features */
8827 netdev->hw_features |= netdev->features;
8828
8829 if (vsi->type == I40E_VSI_MAIN) {
8830 SET_NETDEV_DEV(netdev, &pf->pdev->dev);
Greg Rose9a173902014-05-22 06:32:02 +00008831 ether_addr_copy(mac_addr, hw->mac.perm_addr);
Shannon Nelson30650cc2014-07-29 04:01:50 +00008832 /* The following steps are necessary to prevent reception
8833 * of tagged packets - some older NVM configurations load a
8834 * default a MAC-VLAN filter that accepts any tagged packet
8835 * which must be replaced by a normal filter.
Greg Rose8c27d422014-05-22 06:31:56 +00008836 */
Kiran Patil21659032015-09-30 14:09:03 -04008837 if (!i40e_rm_default_mac_filter(vsi, mac_addr)) {
8838 spin_lock_bh(&vsi->mac_filter_list_lock);
Shannon Nelson30650cc2014-07-29 04:01:50 +00008839 i40e_add_filter(vsi, mac_addr,
8840 I40E_VLAN_ANY, false, true);
Kiran Patil21659032015-09-30 14:09:03 -04008841 spin_unlock_bh(&vsi->mac_filter_list_lock);
8842 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008843 } else {
8844 /* relate the VSI_VMDQ name to the VSI_MAIN name */
8845 snprintf(netdev->name, IFNAMSIZ, "%sv%%d",
8846 pf->vsi[pf->lan_vsi]->netdev->name);
8847 random_ether_addr(mac_addr);
Kiran Patil21659032015-09-30 14:09:03 -04008848
8849 spin_lock_bh(&vsi->mac_filter_list_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008850 i40e_add_filter(vsi, mac_addr, I40E_VLAN_ANY, false, false);
Kiran Patil21659032015-09-30 14:09:03 -04008851 spin_unlock_bh(&vsi->mac_filter_list_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008852 }
Kiran Patil21659032015-09-30 14:09:03 -04008853
8854 spin_lock_bh(&vsi->mac_filter_list_lock);
Greg Rose1a103702013-11-28 06:42:39 +00008855 i40e_add_filter(vsi, brdcast, I40E_VLAN_ANY, false, false);
Kiran Patil21659032015-09-30 14:09:03 -04008856 spin_unlock_bh(&vsi->mac_filter_list_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008857
Greg Rose9a173902014-05-22 06:32:02 +00008858 ether_addr_copy(netdev->dev_addr, mac_addr);
8859 ether_addr_copy(netdev->perm_addr, mac_addr);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008860 /* vlan gets same features (except vlan offload)
8861 * after any tweaks for specific VSI types
8862 */
8863 netdev->vlan_features = netdev->features & ~(NETIF_F_HW_VLAN_CTAG_TX |
8864 NETIF_F_HW_VLAN_CTAG_RX |
8865 NETIF_F_HW_VLAN_CTAG_FILTER);
8866 netdev->priv_flags |= IFF_UNICAST_FLT;
8867 netdev->priv_flags |= IFF_SUPP_NOFCS;
8868 /* Setup netdev TC information */
8869 i40e_vsi_config_netdev_tc(vsi, vsi->tc_config.enabled_tc);
8870
8871 netdev->netdev_ops = &i40e_netdev_ops;
8872 netdev->watchdog_timeo = 5 * HZ;
8873 i40e_set_ethtool_ops(netdev);
Vasu Dev38e00432014-08-01 13:27:03 -07008874#ifdef I40E_FCOE
8875 i40e_fcoe_config_netdev(netdev, vsi);
8876#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008877
8878 return 0;
8879}
8880
8881/**
8882 * i40e_vsi_delete - Delete a VSI from the switch
8883 * @vsi: the VSI being removed
8884 *
8885 * Returns 0 on success, negative value on failure
8886 **/
8887static void i40e_vsi_delete(struct i40e_vsi *vsi)
8888{
8889 /* remove default VSI is not allowed */
8890 if (vsi == vsi->back->vsi[vsi->back->lan_vsi])
8891 return;
8892
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008893 i40e_aq_delete_element(&vsi->back->hw, vsi->seid, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008894}
8895
8896/**
Neerav Parikh51616012015-02-06 08:52:14 +00008897 * i40e_is_vsi_uplink_mode_veb - Check if the VSI's uplink bridge mode is VEB
8898 * @vsi: the VSI being queried
8899 *
8900 * Returns 1 if HW bridge mode is VEB and return 0 in case of VEPA mode
8901 **/
8902int i40e_is_vsi_uplink_mode_veb(struct i40e_vsi *vsi)
8903{
8904 struct i40e_veb *veb;
8905 struct i40e_pf *pf = vsi->back;
8906
8907 /* Uplink is not a bridge so default to VEB */
8908 if (vsi->veb_idx == I40E_NO_VEB)
8909 return 1;
8910
8911 veb = pf->veb[vsi->veb_idx];
Akeem G Abodunrin09603ea2015-10-01 14:37:36 -04008912 if (!veb) {
8913 dev_info(&pf->pdev->dev,
8914 "There is no veb associated with the bridge\n");
8915 return -ENOENT;
8916 }
Neerav Parikh51616012015-02-06 08:52:14 +00008917
Akeem G Abodunrin09603ea2015-10-01 14:37:36 -04008918 /* Uplink is a bridge in VEPA mode */
8919 if (veb->bridge_mode & BRIDGE_MODE_VEPA) {
8920 return 0;
8921 } else {
8922 /* Uplink is a bridge in VEB mode */
8923 return 1;
8924 }
8925
8926 /* VEPA is now default bridge, so return 0 */
8927 return 0;
Neerav Parikh51616012015-02-06 08:52:14 +00008928}
8929
8930/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008931 * i40e_add_vsi - Add a VSI to the switch
8932 * @vsi: the VSI being configured
8933 *
8934 * This initializes a VSI context depending on the VSI type to be added and
8935 * passes it down to the add_vsi aq command.
8936 **/
8937static int i40e_add_vsi(struct i40e_vsi *vsi)
8938{
8939 int ret = -ENODEV;
Kiran Patil21659032015-09-30 14:09:03 -04008940 u8 laa_macaddr[ETH_ALEN];
8941 bool found_laa_mac_filter = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008942 struct i40e_pf *pf = vsi->back;
8943 struct i40e_hw *hw = &pf->hw;
8944 struct i40e_vsi_context ctxt;
Kiran Patil21659032015-09-30 14:09:03 -04008945 struct i40e_mac_filter *f, *ftmp;
8946
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008947 u8 enabled_tc = 0x1; /* TC0 enabled */
8948 int f_count = 0;
8949
8950 memset(&ctxt, 0, sizeof(ctxt));
8951 switch (vsi->type) {
8952 case I40E_VSI_MAIN:
8953 /* The PF's main VSI is already setup as part of the
8954 * device initialization, so we'll not bother with
8955 * the add_vsi call, but we will retrieve the current
8956 * VSI context.
8957 */
8958 ctxt.seid = pf->main_vsi_seid;
8959 ctxt.pf_num = pf->hw.pf_id;
8960 ctxt.vf_num = 0;
8961 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
8962 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
8963 if (ret) {
8964 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008965 "couldn't get PF vsi config, err %s aq_err %s\n",
8966 i40e_stat_str(&pf->hw, ret),
8967 i40e_aq_str(&pf->hw,
8968 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008969 return -ENOENT;
8970 }
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07008971 vsi->info = ctxt.info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008972 vsi->info.valid_sections = 0;
8973
8974 vsi->seid = ctxt.seid;
8975 vsi->id = ctxt.vsi_number;
8976
8977 enabled_tc = i40e_pf_get_tc_map(pf);
8978
8979 /* MFP mode setup queue map and update VSI */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00008980 if ((pf->flags & I40E_FLAG_MFP_ENABLED) &&
8981 !(pf->hw.func_caps.iscsi)) { /* NIC type PF */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008982 memset(&ctxt, 0, sizeof(ctxt));
8983 ctxt.seid = pf->main_vsi_seid;
8984 ctxt.pf_num = pf->hw.pf_id;
8985 ctxt.vf_num = 0;
8986 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
8987 ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
8988 if (ret) {
8989 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008990 "update vsi failed, err %s aq_err %s\n",
8991 i40e_stat_str(&pf->hw, ret),
8992 i40e_aq_str(&pf->hw,
8993 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008994 ret = -ENOENT;
8995 goto err;
8996 }
8997 /* update the local VSI info queue map */
8998 i40e_vsi_update_queue_map(vsi, &ctxt);
8999 vsi->info.valid_sections = 0;
9000 } else {
9001 /* Default/Main VSI is only enabled for TC0
9002 * reconfigure it to enable all TCs that are
9003 * available on the port in SFP mode.
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00009004 * For MFP case the iSCSI PF would use this
9005 * flow to enable LAN+iSCSI TC.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009006 */
9007 ret = i40e_vsi_config_tc(vsi, enabled_tc);
9008 if (ret) {
9009 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009010 "failed to configure TCs for main VSI tc_map 0x%08x, err %s aq_err %s\n",
9011 enabled_tc,
9012 i40e_stat_str(&pf->hw, ret),
9013 i40e_aq_str(&pf->hw,
9014 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009015 ret = -ENOENT;
9016 }
9017 }
9018 break;
9019
9020 case I40E_VSI_FDIR:
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009021 ctxt.pf_num = hw->pf_id;
9022 ctxt.vf_num = 0;
9023 ctxt.uplink_seid = vsi->uplink_seid;
Neerav Parikh2b18e592015-01-24 09:58:38 +00009024 ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009025 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07009026 if ((pf->flags & I40E_FLAG_VEB_MODE_ENABLED) &&
9027 (i40e_is_vsi_uplink_mode_veb(vsi))) {
Neerav Parikh51616012015-02-06 08:52:14 +00009028 ctxt.info.valid_sections |=
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07009029 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
Neerav Parikh51616012015-02-06 08:52:14 +00009030 ctxt.info.switch_id =
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07009031 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
Neerav Parikh51616012015-02-06 08:52:14 +00009032 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009033 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009034 break;
9035
9036 case I40E_VSI_VMDQ2:
9037 ctxt.pf_num = hw->pf_id;
9038 ctxt.vf_num = 0;
9039 ctxt.uplink_seid = vsi->uplink_seid;
Neerav Parikh2b18e592015-01-24 09:58:38 +00009040 ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009041 ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
9042
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009043 /* This VSI is connected to VEB so the switch_id
9044 * should be set to zero by default.
9045 */
Neerav Parikh51616012015-02-06 08:52:14 +00009046 if (i40e_is_vsi_uplink_mode_veb(vsi)) {
9047 ctxt.info.valid_sections |=
9048 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
9049 ctxt.info.switch_id =
9050 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
9051 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009052
9053 /* Setup the VSI tx/rx queue map for TC0 only for now */
9054 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
9055 break;
9056
9057 case I40E_VSI_SRIOV:
9058 ctxt.pf_num = hw->pf_id;
9059 ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
9060 ctxt.uplink_seid = vsi->uplink_seid;
Neerav Parikh2b18e592015-01-24 09:58:38 +00009061 ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009062 ctxt.flags = I40E_AQ_VSI_TYPE_VF;
9063
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009064 /* This VSI is connected to VEB so the switch_id
9065 * should be set to zero by default.
9066 */
Neerav Parikh51616012015-02-06 08:52:14 +00009067 if (i40e_is_vsi_uplink_mode_veb(vsi)) {
9068 ctxt.info.valid_sections |=
9069 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
9070 ctxt.info.switch_id =
9071 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
9072 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009073
9074 ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
9075 ctxt.info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_MODE_ALL;
Mitch Williamsc674d122014-05-20 08:01:40 +00009076 if (pf->vf[vsi->vf_id].spoofchk) {
9077 ctxt.info.valid_sections |=
9078 cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
9079 ctxt.info.sec_flags |=
9080 (I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
9081 I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
9082 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009083 /* Setup the VSI tx/rx queue map for TC0 only for now */
9084 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
9085 break;
9086
Vasu Dev38e00432014-08-01 13:27:03 -07009087#ifdef I40E_FCOE
9088 case I40E_VSI_FCOE:
9089 ret = i40e_fcoe_vsi_init(vsi, &ctxt);
9090 if (ret) {
9091 dev_info(&pf->pdev->dev, "failed to initialize FCoE VSI\n");
9092 return ret;
9093 }
9094 break;
9095
9096#endif /* I40E_FCOE */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009097 default:
9098 return -ENODEV;
9099 }
9100
9101 if (vsi->type != I40E_VSI_MAIN) {
9102 ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
9103 if (ret) {
9104 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009105 "add vsi failed, err %s aq_err %s\n",
9106 i40e_stat_str(&pf->hw, ret),
9107 i40e_aq_str(&pf->hw,
9108 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009109 ret = -ENOENT;
9110 goto err;
9111 }
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07009112 vsi->info = ctxt.info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009113 vsi->info.valid_sections = 0;
9114 vsi->seid = ctxt.seid;
9115 vsi->id = ctxt.vsi_number;
9116 }
9117
Kiran Patil21659032015-09-30 14:09:03 -04009118 spin_lock_bh(&vsi->mac_filter_list_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009119 /* If macvlan filters already exist, force them to get loaded */
9120 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list) {
9121 f->changed = true;
9122 f_count++;
Shannon Nelson6252c7e2014-06-04 01:23:23 +00009123
Kiran Patil21659032015-09-30 14:09:03 -04009124 /* Expected to have only one MAC filter entry for LAA in list */
Shannon Nelson6252c7e2014-06-04 01:23:23 +00009125 if (f->is_laa && vsi->type == I40E_VSI_MAIN) {
Kiran Patil21659032015-09-30 14:09:03 -04009126 ether_addr_copy(laa_macaddr, f->macaddr);
9127 found_laa_mac_filter = true;
Shannon Nelson6252c7e2014-06-04 01:23:23 +00009128 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009129 }
Kiran Patil21659032015-09-30 14:09:03 -04009130 spin_unlock_bh(&vsi->mac_filter_list_lock);
9131
9132 if (found_laa_mac_filter) {
9133 struct i40e_aqc_remove_macvlan_element_data element;
9134
9135 memset(&element, 0, sizeof(element));
9136 ether_addr_copy(element.mac_addr, laa_macaddr);
9137 element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
9138 ret = i40e_aq_remove_macvlan(hw, vsi->seid,
9139 &element, 1, NULL);
9140 if (ret) {
9141 /* some older FW has a different default */
9142 element.flags |=
9143 I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
9144 i40e_aq_remove_macvlan(hw, vsi->seid,
9145 &element, 1, NULL);
9146 }
9147
9148 i40e_aq_mac_address_write(hw,
9149 I40E_AQC_WRITE_TYPE_LAA_WOL,
9150 laa_macaddr, NULL);
9151 }
9152
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009153 if (f_count) {
9154 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
9155 pf->flags |= I40E_FLAG_FILTER_SYNC;
9156 }
9157
9158 /* Update VSI BW information */
9159 ret = i40e_vsi_get_bw_info(vsi);
9160 if (ret) {
9161 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009162 "couldn't get vsi bw info, err %s aq_err %s\n",
9163 i40e_stat_str(&pf->hw, ret),
9164 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009165 /* VSI is already added so not tearing that up */
9166 ret = 0;
9167 }
9168
9169err:
9170 return ret;
9171}
9172
9173/**
9174 * i40e_vsi_release - Delete a VSI and free its resources
9175 * @vsi: the VSI being removed
9176 *
9177 * Returns 0 on success or < 0 on error
9178 **/
9179int i40e_vsi_release(struct i40e_vsi *vsi)
9180{
9181 struct i40e_mac_filter *f, *ftmp;
9182 struct i40e_veb *veb = NULL;
9183 struct i40e_pf *pf;
9184 u16 uplink_seid;
9185 int i, n;
9186
9187 pf = vsi->back;
9188
9189 /* release of a VEB-owner or last VSI is not allowed */
9190 if (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) {
9191 dev_info(&pf->pdev->dev, "VSI %d has existing VEB %d\n",
9192 vsi->seid, vsi->uplink_seid);
9193 return -ENODEV;
9194 }
9195 if (vsi == pf->vsi[pf->lan_vsi] &&
9196 !test_bit(__I40E_DOWN, &pf->state)) {
9197 dev_info(&pf->pdev->dev, "Can't remove PF VSI\n");
9198 return -ENODEV;
9199 }
9200
9201 uplink_seid = vsi->uplink_seid;
9202 if (vsi->type != I40E_VSI_SRIOV) {
9203 if (vsi->netdev_registered) {
9204 vsi->netdev_registered = false;
9205 if (vsi->netdev) {
9206 /* results in a call to i40e_close() */
9207 unregister_netdev(vsi->netdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009208 }
9209 } else {
Shannon Nelson90ef8d42014-03-14 07:32:26 +00009210 i40e_vsi_close(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009211 }
9212 i40e_vsi_disable_irq(vsi);
9213 }
9214
Kiran Patil21659032015-09-30 14:09:03 -04009215 spin_lock_bh(&vsi->mac_filter_list_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009216 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list)
9217 i40e_del_filter(vsi, f->macaddr, f->vlan,
9218 f->is_vf, f->is_netdev);
Kiran Patil21659032015-09-30 14:09:03 -04009219 spin_unlock_bh(&vsi->mac_filter_list_lock);
9220
Anjali Singhai30e25612015-09-28 13:37:12 -07009221 i40e_sync_vsi_filters(vsi, false);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009222
9223 i40e_vsi_delete(vsi);
9224 i40e_vsi_free_q_vectors(vsi);
Shannon Nelsona4866592014-02-11 08:24:07 +00009225 if (vsi->netdev) {
9226 free_netdev(vsi->netdev);
9227 vsi->netdev = NULL;
9228 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009229 i40e_vsi_clear_rings(vsi);
9230 i40e_vsi_clear(vsi);
9231
9232 /* If this was the last thing on the VEB, except for the
9233 * controlling VSI, remove the VEB, which puts the controlling
9234 * VSI onto the next level down in the switch.
9235 *
9236 * Well, okay, there's one more exception here: don't remove
9237 * the orphan VEBs yet. We'll wait for an explicit remove request
9238 * from up the network stack.
9239 */
Mitch Williams505682c2014-05-20 08:01:37 +00009240 for (n = 0, i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009241 if (pf->vsi[i] &&
9242 pf->vsi[i]->uplink_seid == uplink_seid &&
9243 (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
9244 n++; /* count the VSIs */
9245 }
9246 }
9247 for (i = 0; i < I40E_MAX_VEB; i++) {
9248 if (!pf->veb[i])
9249 continue;
9250 if (pf->veb[i]->uplink_seid == uplink_seid)
9251 n++; /* count the VEBs */
9252 if (pf->veb[i]->seid == uplink_seid)
9253 veb = pf->veb[i];
9254 }
9255 if (n == 0 && veb && veb->uplink_seid != 0)
9256 i40e_veb_release(veb);
9257
9258 return 0;
9259}
9260
9261/**
9262 * i40e_vsi_setup_vectors - Set up the q_vectors for the given VSI
9263 * @vsi: ptr to the VSI
9264 *
9265 * This should only be called after i40e_vsi_mem_alloc() which allocates the
9266 * corresponding SW VSI structure and initializes num_queue_pairs for the
9267 * newly allocated VSI.
9268 *
9269 * Returns 0 on success or negative on failure
9270 **/
9271static int i40e_vsi_setup_vectors(struct i40e_vsi *vsi)
9272{
9273 int ret = -ENOENT;
9274 struct i40e_pf *pf = vsi->back;
9275
Alexander Duyck493fb302013-09-28 07:01:44 +00009276 if (vsi->q_vectors[0]) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009277 dev_info(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
9278 vsi->seid);
9279 return -EEXIST;
9280 }
9281
9282 if (vsi->base_vector) {
Jesse Brandeburgf29eaa32014-02-11 08:24:12 +00009283 dev_info(&pf->pdev->dev, "VSI %d has non-zero base vector %d\n",
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009284 vsi->seid, vsi->base_vector);
9285 return -EEXIST;
9286 }
9287
Greg Rose90e04072014-03-06 08:59:57 +00009288 ret = i40e_vsi_alloc_q_vectors(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009289 if (ret) {
9290 dev_info(&pf->pdev->dev,
9291 "failed to allocate %d q_vector for VSI %d, ret=%d\n",
9292 vsi->num_q_vectors, vsi->seid, ret);
9293 vsi->num_q_vectors = 0;
9294 goto vector_setup_out;
9295 }
9296
Anjali Singhai Jain26cdc442015-07-10 19:36:00 -04009297 /* In Legacy mode, we do not have to get any other vector since we
9298 * piggyback on the misc/ICR0 for queue interrupts.
9299 */
9300 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
9301 return ret;
Shannon Nelson958a3e32013-09-28 07:13:28 +00009302 if (vsi->num_q_vectors)
9303 vsi->base_vector = i40e_get_lump(pf, pf->irq_pile,
9304 vsi->num_q_vectors, vsi->idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009305 if (vsi->base_vector < 0) {
9306 dev_info(&pf->pdev->dev,
Shannon Nelson049a2be2014-10-17 03:14:50 +00009307 "failed to get tracking for %d vectors for VSI %d, err=%d\n",
9308 vsi->num_q_vectors, vsi->seid, vsi->base_vector);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009309 i40e_vsi_free_q_vectors(vsi);
9310 ret = -ENOENT;
9311 goto vector_setup_out;
9312 }
9313
9314vector_setup_out:
9315 return ret;
9316}
9317
9318/**
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00009319 * i40e_vsi_reinit_setup - return and reallocate resources for a VSI
9320 * @vsi: pointer to the vsi.
9321 *
9322 * This re-allocates a vsi's queue resources.
9323 *
9324 * Returns pointer to the successfully allocated and configured VSI sw struct
9325 * on success, otherwise returns NULL on failure.
9326 **/
9327static struct i40e_vsi *i40e_vsi_reinit_setup(struct i40e_vsi *vsi)
9328{
9329 struct i40e_pf *pf = vsi->back;
9330 u8 enabled_tc;
9331 int ret;
9332
9333 i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
9334 i40e_vsi_clear_rings(vsi);
9335
9336 i40e_vsi_free_arrays(vsi, false);
9337 i40e_set_num_rings_in_vsi(vsi);
9338 ret = i40e_vsi_alloc_arrays(vsi, false);
9339 if (ret)
9340 goto err_vsi;
9341
9342 ret = i40e_get_lump(pf, pf->qp_pile, vsi->alloc_queue_pairs, vsi->idx);
9343 if (ret < 0) {
Shannon Nelson049a2be2014-10-17 03:14:50 +00009344 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009345 "failed to get tracking for %d queues for VSI %d err %d\n",
Shannon Nelson049a2be2014-10-17 03:14:50 +00009346 vsi->alloc_queue_pairs, vsi->seid, ret);
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00009347 goto err_vsi;
9348 }
9349 vsi->base_queue = ret;
9350
9351 /* Update the FW view of the VSI. Force a reset of TC and queue
9352 * layout configurations.
9353 */
9354 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
9355 pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
9356 pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
9357 i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
9358
9359 /* assign it some queues */
9360 ret = i40e_alloc_rings(vsi);
9361 if (ret)
9362 goto err_rings;
9363
9364 /* map all of the rings to the q_vectors */
9365 i40e_vsi_map_rings_to_vectors(vsi);
9366 return vsi;
9367
9368err_rings:
9369 i40e_vsi_free_q_vectors(vsi);
9370 if (vsi->netdev_registered) {
9371 vsi->netdev_registered = false;
9372 unregister_netdev(vsi->netdev);
9373 free_netdev(vsi->netdev);
9374 vsi->netdev = NULL;
9375 }
9376 i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
9377err_vsi:
9378 i40e_vsi_clear(vsi);
9379 return NULL;
9380}
9381
9382/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009383 * i40e_vsi_setup - Set up a VSI by a given type
9384 * @pf: board private structure
9385 * @type: VSI type
9386 * @uplink_seid: the switch element to link to
9387 * @param1: usage depends upon VSI type. For VF types, indicates VF id
9388 *
9389 * This allocates the sw VSI structure and its queue resources, then add a VSI
9390 * to the identified VEB.
9391 *
9392 * Returns pointer to the successfully allocated and configure VSI sw struct on
9393 * success, otherwise returns NULL on failure.
9394 **/
9395struct i40e_vsi *i40e_vsi_setup(struct i40e_pf *pf, u8 type,
9396 u16 uplink_seid, u32 param1)
9397{
9398 struct i40e_vsi *vsi = NULL;
9399 struct i40e_veb *veb = NULL;
9400 int ret, i;
9401 int v_idx;
9402
9403 /* The requested uplink_seid must be either
9404 * - the PF's port seid
9405 * no VEB is needed because this is the PF
9406 * or this is a Flow Director special case VSI
9407 * - seid of an existing VEB
9408 * - seid of a VSI that owns an existing VEB
9409 * - seid of a VSI that doesn't own a VEB
9410 * a new VEB is created and the VSI becomes the owner
9411 * - seid of the PF VSI, which is what creates the first VEB
9412 * this is a special case of the previous
9413 *
9414 * Find which uplink_seid we were given and create a new VEB if needed
9415 */
9416 for (i = 0; i < I40E_MAX_VEB; i++) {
9417 if (pf->veb[i] && pf->veb[i]->seid == uplink_seid) {
9418 veb = pf->veb[i];
9419 break;
9420 }
9421 }
9422
9423 if (!veb && uplink_seid != pf->mac_seid) {
9424
Mitch Williams505682c2014-05-20 08:01:37 +00009425 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009426 if (pf->vsi[i] && pf->vsi[i]->seid == uplink_seid) {
9427 vsi = pf->vsi[i];
9428 break;
9429 }
9430 }
9431 if (!vsi) {
9432 dev_info(&pf->pdev->dev, "no such uplink_seid %d\n",
9433 uplink_seid);
9434 return NULL;
9435 }
9436
9437 if (vsi->uplink_seid == pf->mac_seid)
9438 veb = i40e_veb_setup(pf, 0, pf->mac_seid, vsi->seid,
9439 vsi->tc_config.enabled_tc);
9440 else if ((vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
9441 veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
9442 vsi->tc_config.enabled_tc);
Anjali Singhai Jain79c21a82014-11-13 03:06:14 +00009443 if (veb) {
9444 if (vsi->seid != pf->vsi[pf->lan_vsi]->seid) {
9445 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04009446 "New VSI creation error, uplink seid of LAN VSI expected.\n");
Anjali Singhai Jain79c21a82014-11-13 03:06:14 +00009447 return NULL;
9448 }
Anjali Singhai Jainfa11cb32015-05-27 12:06:14 -04009449 /* We come up by default in VEPA mode if SRIOV is not
9450 * already enabled, in which case we can't force VEPA
9451 * mode.
9452 */
9453 if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
9454 veb->bridge_mode = BRIDGE_MODE_VEPA;
9455 pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
9456 }
Neerav Parikh51616012015-02-06 08:52:14 +00009457 i40e_config_bridge_mode(veb);
Anjali Singhai Jain79c21a82014-11-13 03:06:14 +00009458 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009459 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
9460 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
9461 veb = pf->veb[i];
9462 }
9463 if (!veb) {
9464 dev_info(&pf->pdev->dev, "couldn't add VEB\n");
9465 return NULL;
9466 }
9467
9468 vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
9469 uplink_seid = veb->seid;
9470 }
9471
9472 /* get vsi sw struct */
9473 v_idx = i40e_vsi_mem_alloc(pf, type);
9474 if (v_idx < 0)
9475 goto err_alloc;
9476 vsi = pf->vsi[v_idx];
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009477 if (!vsi)
9478 goto err_alloc;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009479 vsi->type = type;
9480 vsi->veb_idx = (veb ? veb->idx : I40E_NO_VEB);
9481
9482 if (type == I40E_VSI_MAIN)
9483 pf->lan_vsi = v_idx;
9484 else if (type == I40E_VSI_SRIOV)
9485 vsi->vf_id = param1;
9486 /* assign it some queues */
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009487 ret = i40e_get_lump(pf, pf->qp_pile, vsi->alloc_queue_pairs,
9488 vsi->idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009489 if (ret < 0) {
Shannon Nelson049a2be2014-10-17 03:14:50 +00009490 dev_info(&pf->pdev->dev,
9491 "failed to get tracking for %d queues for VSI %d err=%d\n",
9492 vsi->alloc_queue_pairs, vsi->seid, ret);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009493 goto err_vsi;
9494 }
9495 vsi->base_queue = ret;
9496
9497 /* get a VSI from the hardware */
9498 vsi->uplink_seid = uplink_seid;
9499 ret = i40e_add_vsi(vsi);
9500 if (ret)
9501 goto err_vsi;
9502
9503 switch (vsi->type) {
9504 /* setup the netdev if needed */
9505 case I40E_VSI_MAIN:
9506 case I40E_VSI_VMDQ2:
Vasu Dev38e00432014-08-01 13:27:03 -07009507 case I40E_VSI_FCOE:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009508 ret = i40e_config_netdev(vsi);
9509 if (ret)
9510 goto err_netdev;
9511 ret = register_netdev(vsi->netdev);
9512 if (ret)
9513 goto err_netdev;
9514 vsi->netdev_registered = true;
9515 netif_carrier_off(vsi->netdev);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08009516#ifdef CONFIG_I40E_DCB
9517 /* Setup DCB netlink interface */
9518 i40e_dcbnl_setup(vsi);
9519#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009520 /* fall through */
9521
9522 case I40E_VSI_FDIR:
9523 /* set up vectors and rings if needed */
9524 ret = i40e_vsi_setup_vectors(vsi);
9525 if (ret)
9526 goto err_msix;
9527
9528 ret = i40e_alloc_rings(vsi);
9529 if (ret)
9530 goto err_rings;
9531
9532 /* map all of the rings to the q_vectors */
9533 i40e_vsi_map_rings_to_vectors(vsi);
9534
9535 i40e_vsi_reset_stats(vsi);
9536 break;
9537
9538 default:
9539 /* no netdev or rings for the other VSI types */
9540 break;
9541 }
9542
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04009543 if ((pf->flags & I40E_FLAG_RSS_AQ_CAPABLE) &&
9544 (vsi->type == I40E_VSI_VMDQ2)) {
9545 ret = i40e_vsi_config_rss(vsi);
9546 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009547 return vsi;
9548
9549err_rings:
9550 i40e_vsi_free_q_vectors(vsi);
9551err_msix:
9552 if (vsi->netdev_registered) {
9553 vsi->netdev_registered = false;
9554 unregister_netdev(vsi->netdev);
9555 free_netdev(vsi->netdev);
9556 vsi->netdev = NULL;
9557 }
9558err_netdev:
9559 i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
9560err_vsi:
9561 i40e_vsi_clear(vsi);
9562err_alloc:
9563 return NULL;
9564}
9565
9566/**
9567 * i40e_veb_get_bw_info - Query VEB BW information
9568 * @veb: the veb to query
9569 *
9570 * Query the Tx scheduler BW configuration data for given VEB
9571 **/
9572static int i40e_veb_get_bw_info(struct i40e_veb *veb)
9573{
9574 struct i40e_aqc_query_switching_comp_ets_config_resp ets_data;
9575 struct i40e_aqc_query_switching_comp_bw_config_resp bw_data;
9576 struct i40e_pf *pf = veb->pf;
9577 struct i40e_hw *hw = &pf->hw;
9578 u32 tc_bw_max;
9579 int ret = 0;
9580 int i;
9581
9582 ret = i40e_aq_query_switch_comp_bw_config(hw, veb->seid,
9583 &bw_data, NULL);
9584 if (ret) {
9585 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009586 "query veb bw config failed, err %s aq_err %s\n",
9587 i40e_stat_str(&pf->hw, ret),
9588 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009589 goto out;
9590 }
9591
9592 ret = i40e_aq_query_switch_comp_ets_config(hw, veb->seid,
9593 &ets_data, NULL);
9594 if (ret) {
9595 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009596 "query veb bw ets config failed, err %s aq_err %s\n",
9597 i40e_stat_str(&pf->hw, ret),
9598 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009599 goto out;
9600 }
9601
9602 veb->bw_limit = le16_to_cpu(ets_data.port_bw_limit);
9603 veb->bw_max_quanta = ets_data.tc_bw_max;
9604 veb->is_abs_credits = bw_data.absolute_credits_enable;
Neerav Parikh23cd1f02014-11-12 00:18:41 +00009605 veb->enabled_tc = ets_data.tc_valid_bits;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009606 tc_bw_max = le16_to_cpu(bw_data.tc_bw_max[0]) |
9607 (le16_to_cpu(bw_data.tc_bw_max[1]) << 16);
9608 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
9609 veb->bw_tc_share_credits[i] = bw_data.tc_bw_share_credits[i];
9610 veb->bw_tc_limit_credits[i] =
9611 le16_to_cpu(bw_data.tc_bw_limits[i]);
9612 veb->bw_tc_max_quanta[i] = ((tc_bw_max >> (i*4)) & 0x7);
9613 }
9614
9615out:
9616 return ret;
9617}
9618
9619/**
9620 * i40e_veb_mem_alloc - Allocates the next available struct veb in the PF
9621 * @pf: board private structure
9622 *
9623 * On error: returns error code (negative)
9624 * On success: returns vsi index in PF (positive)
9625 **/
9626static int i40e_veb_mem_alloc(struct i40e_pf *pf)
9627{
9628 int ret = -ENOENT;
9629 struct i40e_veb *veb;
9630 int i;
9631
9632 /* Need to protect the allocation of switch elements at the PF level */
9633 mutex_lock(&pf->switch_mutex);
9634
9635 /* VEB list may be fragmented if VEB creation/destruction has
9636 * been happening. We can afford to do a quick scan to look
9637 * for any free slots in the list.
9638 *
9639 * find next empty veb slot, looping back around if necessary
9640 */
9641 i = 0;
9642 while ((i < I40E_MAX_VEB) && (pf->veb[i] != NULL))
9643 i++;
9644 if (i >= I40E_MAX_VEB) {
9645 ret = -ENOMEM;
9646 goto err_alloc_veb; /* out of VEB slots! */
9647 }
9648
9649 veb = kzalloc(sizeof(*veb), GFP_KERNEL);
9650 if (!veb) {
9651 ret = -ENOMEM;
9652 goto err_alloc_veb;
9653 }
9654 veb->pf = pf;
9655 veb->idx = i;
9656 veb->enabled_tc = 1;
9657
9658 pf->veb[i] = veb;
9659 ret = i;
9660err_alloc_veb:
9661 mutex_unlock(&pf->switch_mutex);
9662 return ret;
9663}
9664
9665/**
9666 * i40e_switch_branch_release - Delete a branch of the switch tree
9667 * @branch: where to start deleting
9668 *
9669 * This uses recursion to find the tips of the branch to be
9670 * removed, deleting until we get back to and can delete this VEB.
9671 **/
9672static void i40e_switch_branch_release(struct i40e_veb *branch)
9673{
9674 struct i40e_pf *pf = branch->pf;
9675 u16 branch_seid = branch->seid;
9676 u16 veb_idx = branch->idx;
9677 int i;
9678
9679 /* release any VEBs on this VEB - RECURSION */
9680 for (i = 0; i < I40E_MAX_VEB; i++) {
9681 if (!pf->veb[i])
9682 continue;
9683 if (pf->veb[i]->uplink_seid == branch->seid)
9684 i40e_switch_branch_release(pf->veb[i]);
9685 }
9686
9687 /* Release the VSIs on this VEB, but not the owner VSI.
9688 *
9689 * NOTE: Removing the last VSI on a VEB has the SIDE EFFECT of removing
9690 * the VEB itself, so don't use (*branch) after this loop.
9691 */
Mitch Williams505682c2014-05-20 08:01:37 +00009692 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009693 if (!pf->vsi[i])
9694 continue;
9695 if (pf->vsi[i]->uplink_seid == branch_seid &&
9696 (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
9697 i40e_vsi_release(pf->vsi[i]);
9698 }
9699 }
9700
9701 /* There's one corner case where the VEB might not have been
9702 * removed, so double check it here and remove it if needed.
9703 * This case happens if the veb was created from the debugfs
9704 * commands and no VSIs were added to it.
9705 */
9706 if (pf->veb[veb_idx])
9707 i40e_veb_release(pf->veb[veb_idx]);
9708}
9709
9710/**
9711 * i40e_veb_clear - remove veb struct
9712 * @veb: the veb to remove
9713 **/
9714static void i40e_veb_clear(struct i40e_veb *veb)
9715{
9716 if (!veb)
9717 return;
9718
9719 if (veb->pf) {
9720 struct i40e_pf *pf = veb->pf;
9721
9722 mutex_lock(&pf->switch_mutex);
9723 if (pf->veb[veb->idx] == veb)
9724 pf->veb[veb->idx] = NULL;
9725 mutex_unlock(&pf->switch_mutex);
9726 }
9727
9728 kfree(veb);
9729}
9730
9731/**
9732 * i40e_veb_release - Delete a VEB and free its resources
9733 * @veb: the VEB being removed
9734 **/
9735void i40e_veb_release(struct i40e_veb *veb)
9736{
9737 struct i40e_vsi *vsi = NULL;
9738 struct i40e_pf *pf;
9739 int i, n = 0;
9740
9741 pf = veb->pf;
9742
9743 /* find the remaining VSI and check for extras */
Mitch Williams505682c2014-05-20 08:01:37 +00009744 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009745 if (pf->vsi[i] && pf->vsi[i]->uplink_seid == veb->seid) {
9746 n++;
9747 vsi = pf->vsi[i];
9748 }
9749 }
9750 if (n != 1) {
9751 dev_info(&pf->pdev->dev,
9752 "can't remove VEB %d with %d VSIs left\n",
9753 veb->seid, n);
9754 return;
9755 }
9756
9757 /* move the remaining VSI to uplink veb */
9758 vsi->flags &= ~I40E_VSI_FLAG_VEB_OWNER;
9759 if (veb->uplink_seid) {
9760 vsi->uplink_seid = veb->uplink_seid;
9761 if (veb->uplink_seid == pf->mac_seid)
9762 vsi->veb_idx = I40E_NO_VEB;
9763 else
9764 vsi->veb_idx = veb->veb_idx;
9765 } else {
9766 /* floating VEB */
9767 vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
9768 vsi->veb_idx = pf->vsi[pf->lan_vsi]->veb_idx;
9769 }
9770
9771 i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
9772 i40e_veb_clear(veb);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009773}
9774
9775/**
9776 * i40e_add_veb - create the VEB in the switch
9777 * @veb: the VEB to be instantiated
9778 * @vsi: the controlling VSI
9779 **/
9780static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi)
9781{
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009782 struct i40e_pf *pf = veb->pf;
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04009783 bool is_default = veb->pf->cur_promisc;
Kevin Scotte1c51b952013-11-20 10:02:51 +00009784 bool is_cloud = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009785 int ret;
9786
9787 /* get a VEB from the hardware */
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009788 ret = i40e_aq_add_veb(&pf->hw, veb->uplink_seid, vsi->seid,
Kevin Scotte1c51b952013-11-20 10:02:51 +00009789 veb->enabled_tc, is_default,
9790 is_cloud, &veb->seid, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009791 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009792 dev_info(&pf->pdev->dev,
9793 "couldn't add VEB, err %s aq_err %s\n",
9794 i40e_stat_str(&pf->hw, ret),
9795 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009796 return -EPERM;
9797 }
9798
9799 /* get statistics counter */
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009800 ret = i40e_aq_get_veb_parameters(&pf->hw, veb->seid, NULL, NULL,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009801 &veb->stats_idx, NULL, NULL, NULL);
9802 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009803 dev_info(&pf->pdev->dev,
9804 "couldn't get VEB statistics idx, err %s aq_err %s\n",
9805 i40e_stat_str(&pf->hw, ret),
9806 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009807 return -EPERM;
9808 }
9809 ret = i40e_veb_get_bw_info(veb);
9810 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009811 dev_info(&pf->pdev->dev,
9812 "couldn't get VEB bw info, err %s aq_err %s\n",
9813 i40e_stat_str(&pf->hw, ret),
9814 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
9815 i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009816 return -ENOENT;
9817 }
9818
9819 vsi->uplink_seid = veb->seid;
9820 vsi->veb_idx = veb->idx;
9821 vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
9822
9823 return 0;
9824}
9825
9826/**
9827 * i40e_veb_setup - Set up a VEB
9828 * @pf: board private structure
9829 * @flags: VEB setup flags
9830 * @uplink_seid: the switch element to link to
9831 * @vsi_seid: the initial VSI seid
9832 * @enabled_tc: Enabled TC bit-map
9833 *
9834 * This allocates the sw VEB structure and links it into the switch
9835 * It is possible and legal for this to be a duplicate of an already
9836 * existing VEB. It is also possible for both uplink and vsi seids
9837 * to be zero, in order to create a floating VEB.
9838 *
9839 * Returns pointer to the successfully allocated VEB sw struct on
9840 * success, otherwise returns NULL on failure.
9841 **/
9842struct i40e_veb *i40e_veb_setup(struct i40e_pf *pf, u16 flags,
9843 u16 uplink_seid, u16 vsi_seid,
9844 u8 enabled_tc)
9845{
9846 struct i40e_veb *veb, *uplink_veb = NULL;
9847 int vsi_idx, veb_idx;
9848 int ret;
9849
9850 /* if one seid is 0, the other must be 0 to create a floating relay */
9851 if ((uplink_seid == 0 || vsi_seid == 0) &&
9852 (uplink_seid + vsi_seid != 0)) {
9853 dev_info(&pf->pdev->dev,
9854 "one, not both seid's are 0: uplink=%d vsi=%d\n",
9855 uplink_seid, vsi_seid);
9856 return NULL;
9857 }
9858
9859 /* make sure there is such a vsi and uplink */
Mitch Williams505682c2014-05-20 08:01:37 +00009860 for (vsi_idx = 0; vsi_idx < pf->num_alloc_vsi; vsi_idx++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009861 if (pf->vsi[vsi_idx] && pf->vsi[vsi_idx]->seid == vsi_seid)
9862 break;
Mitch Williams505682c2014-05-20 08:01:37 +00009863 if (vsi_idx >= pf->num_alloc_vsi && vsi_seid != 0) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009864 dev_info(&pf->pdev->dev, "vsi seid %d not found\n",
9865 vsi_seid);
9866 return NULL;
9867 }
9868
9869 if (uplink_seid && uplink_seid != pf->mac_seid) {
9870 for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
9871 if (pf->veb[veb_idx] &&
9872 pf->veb[veb_idx]->seid == uplink_seid) {
9873 uplink_veb = pf->veb[veb_idx];
9874 break;
9875 }
9876 }
9877 if (!uplink_veb) {
9878 dev_info(&pf->pdev->dev,
9879 "uplink seid %d not found\n", uplink_seid);
9880 return NULL;
9881 }
9882 }
9883
9884 /* get veb sw struct */
9885 veb_idx = i40e_veb_mem_alloc(pf);
9886 if (veb_idx < 0)
9887 goto err_alloc;
9888 veb = pf->veb[veb_idx];
9889 veb->flags = flags;
9890 veb->uplink_seid = uplink_seid;
9891 veb->veb_idx = (uplink_veb ? uplink_veb->idx : I40E_NO_VEB);
9892 veb->enabled_tc = (enabled_tc ? enabled_tc : 0x1);
9893
9894 /* create the VEB in the switch */
9895 ret = i40e_add_veb(veb, pf->vsi[vsi_idx]);
9896 if (ret)
9897 goto err_veb;
Shannon Nelson1bb8b932014-04-23 04:49:54 +00009898 if (vsi_idx == pf->lan_vsi)
9899 pf->lan_veb = veb->idx;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009900
9901 return veb;
9902
9903err_veb:
9904 i40e_veb_clear(veb);
9905err_alloc:
9906 return NULL;
9907}
9908
9909/**
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00009910 * i40e_setup_pf_switch_element - set PF vars based on switch type
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009911 * @pf: board private structure
9912 * @ele: element we are building info from
9913 * @num_reported: total number of elements
9914 * @printconfig: should we print the contents
9915 *
9916 * helper function to assist in extracting a few useful SEID values.
9917 **/
9918static void i40e_setup_pf_switch_element(struct i40e_pf *pf,
9919 struct i40e_aqc_switch_config_element_resp *ele,
9920 u16 num_reported, bool printconfig)
9921{
9922 u16 downlink_seid = le16_to_cpu(ele->downlink_seid);
9923 u16 uplink_seid = le16_to_cpu(ele->uplink_seid);
9924 u8 element_type = ele->element_type;
9925 u16 seid = le16_to_cpu(ele->seid);
9926
9927 if (printconfig)
9928 dev_info(&pf->pdev->dev,
9929 "type=%d seid=%d uplink=%d downlink=%d\n",
9930 element_type, seid, uplink_seid, downlink_seid);
9931
9932 switch (element_type) {
9933 case I40E_SWITCH_ELEMENT_TYPE_MAC:
9934 pf->mac_seid = seid;
9935 break;
9936 case I40E_SWITCH_ELEMENT_TYPE_VEB:
9937 /* Main VEB? */
9938 if (uplink_seid != pf->mac_seid)
9939 break;
9940 if (pf->lan_veb == I40E_NO_VEB) {
9941 int v;
9942
9943 /* find existing or else empty VEB */
9944 for (v = 0; v < I40E_MAX_VEB; v++) {
9945 if (pf->veb[v] && (pf->veb[v]->seid == seid)) {
9946 pf->lan_veb = v;
9947 break;
9948 }
9949 }
9950 if (pf->lan_veb == I40E_NO_VEB) {
9951 v = i40e_veb_mem_alloc(pf);
9952 if (v < 0)
9953 break;
9954 pf->lan_veb = v;
9955 }
9956 }
9957
9958 pf->veb[pf->lan_veb]->seid = seid;
9959 pf->veb[pf->lan_veb]->uplink_seid = pf->mac_seid;
9960 pf->veb[pf->lan_veb]->pf = pf;
9961 pf->veb[pf->lan_veb]->veb_idx = I40E_NO_VEB;
9962 break;
9963 case I40E_SWITCH_ELEMENT_TYPE_VSI:
9964 if (num_reported != 1)
9965 break;
9966 /* This is immediately after a reset so we can assume this is
9967 * the PF's VSI
9968 */
9969 pf->mac_seid = uplink_seid;
9970 pf->pf_seid = downlink_seid;
9971 pf->main_vsi_seid = seid;
9972 if (printconfig)
9973 dev_info(&pf->pdev->dev,
9974 "pf_seid=%d main_vsi_seid=%d\n",
9975 pf->pf_seid, pf->main_vsi_seid);
9976 break;
9977 case I40E_SWITCH_ELEMENT_TYPE_PF:
9978 case I40E_SWITCH_ELEMENT_TYPE_VF:
9979 case I40E_SWITCH_ELEMENT_TYPE_EMP:
9980 case I40E_SWITCH_ELEMENT_TYPE_BMC:
9981 case I40E_SWITCH_ELEMENT_TYPE_PE:
9982 case I40E_SWITCH_ELEMENT_TYPE_PA:
9983 /* ignore these for now */
9984 break;
9985 default:
9986 dev_info(&pf->pdev->dev, "unknown element type=%d seid=%d\n",
9987 element_type, seid);
9988 break;
9989 }
9990}
9991
9992/**
9993 * i40e_fetch_switch_configuration - Get switch config from firmware
9994 * @pf: board private structure
9995 * @printconfig: should we print the contents
9996 *
9997 * Get the current switch configuration from the device and
9998 * extract a few useful SEID values.
9999 **/
10000int i40e_fetch_switch_configuration(struct i40e_pf *pf, bool printconfig)
10001{
10002 struct i40e_aqc_get_switch_config_resp *sw_config;
10003 u16 next_seid = 0;
10004 int ret = 0;
10005 u8 *aq_buf;
10006 int i;
10007
10008 aq_buf = kzalloc(I40E_AQ_LARGE_BUF, GFP_KERNEL);
10009 if (!aq_buf)
10010 return -ENOMEM;
10011
10012 sw_config = (struct i40e_aqc_get_switch_config_resp *)aq_buf;
10013 do {
10014 u16 num_reported, num_total;
10015
10016 ret = i40e_aq_get_switch_config(&pf->hw, sw_config,
10017 I40E_AQ_LARGE_BUF,
10018 &next_seid, NULL);
10019 if (ret) {
10020 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010021 "get switch config failed err %s aq_err %s\n",
10022 i40e_stat_str(&pf->hw, ret),
10023 i40e_aq_str(&pf->hw,
10024 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010025 kfree(aq_buf);
10026 return -ENOENT;
10027 }
10028
10029 num_reported = le16_to_cpu(sw_config->header.num_reported);
10030 num_total = le16_to_cpu(sw_config->header.num_total);
10031
10032 if (printconfig)
10033 dev_info(&pf->pdev->dev,
10034 "header: %d reported %d total\n",
10035 num_reported, num_total);
10036
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010037 for (i = 0; i < num_reported; i++) {
10038 struct i40e_aqc_switch_config_element_resp *ele =
10039 &sw_config->element[i];
10040
10041 i40e_setup_pf_switch_element(pf, ele, num_reported,
10042 printconfig);
10043 }
10044 } while (next_seid != 0);
10045
10046 kfree(aq_buf);
10047 return ret;
10048}
10049
10050/**
10051 * i40e_setup_pf_switch - Setup the HW switch on startup or after reset
10052 * @pf: board private structure
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010053 * @reinit: if the Main VSI needs to re-initialized.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010054 *
10055 * Returns 0 on success, negative value on failure
10056 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010057static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010058{
10059 int ret;
10060
10061 /* find out what's out there already */
10062 ret = i40e_fetch_switch_configuration(pf, false);
10063 if (ret) {
10064 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010065 "couldn't fetch switch config, err %s aq_err %s\n",
10066 i40e_stat_str(&pf->hw, ret),
10067 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010068 return ret;
10069 }
10070 i40e_pf_reset_stats(pf);
10071
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010072 /* first time setup */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010073 if (pf->lan_vsi == I40E_NO_VSI || reinit) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010074 struct i40e_vsi *vsi = NULL;
10075 u16 uplink_seid;
10076
10077 /* Set up the PF VSI associated with the PF's main VSI
10078 * that is already in the HW switch
10079 */
10080 if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
10081 uplink_seid = pf->veb[pf->lan_veb]->seid;
10082 else
10083 uplink_seid = pf->mac_seid;
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010084 if (pf->lan_vsi == I40E_NO_VSI)
10085 vsi = i40e_vsi_setup(pf, I40E_VSI_MAIN, uplink_seid, 0);
10086 else if (reinit)
10087 vsi = i40e_vsi_reinit_setup(pf->vsi[pf->lan_vsi]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010088 if (!vsi) {
10089 dev_info(&pf->pdev->dev, "setup of MAIN VSI failed\n");
10090 i40e_fdir_teardown(pf);
10091 return -EAGAIN;
10092 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010093 } else {
10094 /* force a reset of TC and queue layout configurations */
10095 u8 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
Jesse Brandeburg6995b362015-08-28 17:55:54 -040010096
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010097 pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
10098 pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
10099 i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
10100 }
10101 i40e_vlan_stripping_disable(pf->vsi[pf->lan_vsi]);
10102
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010103 i40e_fdir_sb_setup(pf);
10104
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010105 /* Setup static PF queue filter control settings */
10106 ret = i40e_setup_pf_filter_control(pf);
10107 if (ret) {
10108 dev_info(&pf->pdev->dev, "setup_pf_filter_control failed: %d\n",
10109 ret);
10110 /* Failure here should not stop continuing other steps */
10111 }
10112
10113 /* enable RSS in the HW, even for only one queue, as the stack can use
10114 * the hash
10115 */
10116 if ((pf->flags & I40E_FLAG_RSS_ENABLED))
Helin Zhang043dd652015-10-21 19:56:23 -040010117 i40e_pf_config_rss(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010118
10119 /* fill in link information and enable LSE reporting */
Catherine Sullivan0a862b42015-08-31 19:54:53 -040010120 i40e_update_link_info(&pf->hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010121 i40e_link_event(pf);
10122
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +000010123 /* Initialize user-specific link properties */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010124 pf->fc_autoneg_status = ((pf->hw.phy.link_info.an_info &
10125 I40E_AQ_AN_COMPLETED) ? true : false);
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +000010126
Jacob Kellerbeb0dff2014-01-11 05:43:19 +000010127 i40e_ptp_init(pf);
10128
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010129 return ret;
10130}
10131
10132/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010133 * i40e_determine_queue_usage - Work out queue distribution
10134 * @pf: board private structure
10135 **/
10136static void i40e_determine_queue_usage(struct i40e_pf *pf)
10137{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010138 int queues_left;
10139
10140 pf->num_lan_qps = 0;
Vasu Dev38e00432014-08-01 13:27:03 -070010141#ifdef I40E_FCOE
10142 pf->num_fcoe_qps = 0;
10143#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010144
10145 /* Find the max queues to be put into basic use. We'll always be
10146 * using TC0, whether or not DCB is running, and TC0 will get the
10147 * big RSS set.
10148 */
10149 queues_left = pf->hw.func_caps.num_tx_qp;
10150
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010151 if ((queues_left == 1) ||
Frank Zhang9aa7e932014-05-20 08:01:42 +000010152 !(pf->flags & I40E_FLAG_MSIX_ENABLED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010153 /* one qp for PF, no queues for anything else */
10154 queues_left = 0;
10155 pf->rss_size = pf->num_lan_qps = 1;
10156
10157 /* make sure all the fancies are disabled */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080010158 pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
Vasu Dev38e00432014-08-01 13:27:03 -070010159#ifdef I40E_FCOE
10160 I40E_FLAG_FCOE_ENABLED |
10161#endif
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080010162 I40E_FLAG_FD_SB_ENABLED |
10163 I40E_FLAG_FD_ATR_ENABLED |
Neerav Parikh4d9b6042014-05-22 06:31:51 +000010164 I40E_FLAG_DCB_CAPABLE |
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080010165 I40E_FLAG_SRIOV_ENABLED |
10166 I40E_FLAG_VMDQ_ENABLED);
Frank Zhang9aa7e932014-05-20 08:01:42 +000010167 } else if (!(pf->flags & (I40E_FLAG_RSS_ENABLED |
10168 I40E_FLAG_FD_SB_ENABLED |
Anjali Singhai Jainbbe7d0e2014-05-20 08:01:44 +000010169 I40E_FLAG_FD_ATR_ENABLED |
Neerav Parikh4d9b6042014-05-22 06:31:51 +000010170 I40E_FLAG_DCB_CAPABLE))) {
Frank Zhang9aa7e932014-05-20 08:01:42 +000010171 /* one qp for PF */
10172 pf->rss_size = pf->num_lan_qps = 1;
10173 queues_left -= pf->num_lan_qps;
10174
10175 pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
Vasu Dev38e00432014-08-01 13:27:03 -070010176#ifdef I40E_FCOE
10177 I40E_FLAG_FCOE_ENABLED |
10178#endif
Frank Zhang9aa7e932014-05-20 08:01:42 +000010179 I40E_FLAG_FD_SB_ENABLED |
10180 I40E_FLAG_FD_ATR_ENABLED |
10181 I40E_FLAG_DCB_ENABLED |
10182 I40E_FLAG_VMDQ_ENABLED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010183 } else {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010184 /* Not enough queues for all TCs */
Neerav Parikh4d9b6042014-05-22 06:31:51 +000010185 if ((pf->flags & I40E_FLAG_DCB_CAPABLE) &&
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010186 (queues_left < I40E_MAX_TRAFFIC_CLASS)) {
Neerav Parikh4d9b6042014-05-22 06:31:51 +000010187 pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010188 dev_info(&pf->pdev->dev, "not enough queues for DCB. DCB is disabled.\n");
10189 }
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +000010190 pf->num_lan_qps = max_t(int, pf->rss_size_max,
10191 num_online_cpus());
10192 pf->num_lan_qps = min_t(int, pf->num_lan_qps,
10193 pf->hw.func_caps.num_tx_qp);
10194
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010195 queues_left -= pf->num_lan_qps;
10196 }
10197
Vasu Dev38e00432014-08-01 13:27:03 -070010198#ifdef I40E_FCOE
10199 if (pf->flags & I40E_FLAG_FCOE_ENABLED) {
10200 if (I40E_DEFAULT_FCOE <= queues_left) {
10201 pf->num_fcoe_qps = I40E_DEFAULT_FCOE;
10202 } else if (I40E_MINIMUM_FCOE <= queues_left) {
10203 pf->num_fcoe_qps = I40E_MINIMUM_FCOE;
10204 } else {
10205 pf->num_fcoe_qps = 0;
10206 pf->flags &= ~I40E_FLAG_FCOE_ENABLED;
10207 dev_info(&pf->pdev->dev, "not enough queues for FCoE. FCoE feature will be disabled\n");
10208 }
10209
10210 queues_left -= pf->num_fcoe_qps;
10211 }
10212
10213#endif
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010214 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
10215 if (queues_left > 1) {
10216 queues_left -= 1; /* save 1 queue for FD */
10217 } else {
10218 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
10219 dev_info(&pf->pdev->dev, "not enough queues for Flow Director. Flow Director feature is disabled\n");
10220 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010221 }
10222
10223 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
10224 pf->num_vf_qps && pf->num_req_vfs && queues_left) {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010225 pf->num_req_vfs = min_t(int, pf->num_req_vfs,
10226 (queues_left / pf->num_vf_qps));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010227 queues_left -= (pf->num_req_vfs * pf->num_vf_qps);
10228 }
10229
10230 if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
10231 pf->num_vmdq_vsis && pf->num_vmdq_qps && queues_left) {
10232 pf->num_vmdq_vsis = min_t(int, pf->num_vmdq_vsis,
10233 (queues_left / pf->num_vmdq_qps));
10234 queues_left -= (pf->num_vmdq_vsis * pf->num_vmdq_qps);
10235 }
10236
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +000010237 pf->queues_left = queues_left;
Neerav Parikh8279e492015-09-03 17:18:50 -040010238 dev_dbg(&pf->pdev->dev,
10239 "qs_avail=%d FD SB=%d lan_qs=%d lan_tc0=%d vf=%d*%d vmdq=%d*%d, remaining=%d\n",
10240 pf->hw.func_caps.num_tx_qp,
10241 !!(pf->flags & I40E_FLAG_FD_SB_ENABLED),
10242 pf->num_lan_qps, pf->rss_size, pf->num_req_vfs, pf->num_vf_qps,
10243 pf->num_vmdq_vsis, pf->num_vmdq_qps, queues_left);
Vasu Dev38e00432014-08-01 13:27:03 -070010244#ifdef I40E_FCOE
Neerav Parikh8279e492015-09-03 17:18:50 -040010245 dev_dbg(&pf->pdev->dev, "fcoe queues = %d\n", pf->num_fcoe_qps);
Vasu Dev38e00432014-08-01 13:27:03 -070010246#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010247}
10248
10249/**
10250 * i40e_setup_pf_filter_control - Setup PF static filter control
10251 * @pf: PF to be setup
10252 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +000010253 * i40e_setup_pf_filter_control sets up a PF's initial filter control
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010254 * settings. If PE/FCoE are enabled then it will also set the per PF
10255 * based filter sizes required for them. It also enables Flow director,
10256 * ethertype and macvlan type filter settings for the pf.
10257 *
10258 * Returns 0 on success, negative on failure
10259 **/
10260static int i40e_setup_pf_filter_control(struct i40e_pf *pf)
10261{
10262 struct i40e_filter_control_settings *settings = &pf->filter_settings;
10263
10264 settings->hash_lut_size = I40E_HASH_LUT_SIZE_128;
10265
10266 /* Flow Director is enabled */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080010267 if (pf->flags & (I40E_FLAG_FD_SB_ENABLED | I40E_FLAG_FD_ATR_ENABLED))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010268 settings->enable_fdir = true;
10269
10270 /* Ethtype and MACVLAN filters enabled for PF */
10271 settings->enable_ethtype = true;
10272 settings->enable_macvlan = true;
10273
10274 if (i40e_set_filter_control(&pf->hw, settings))
10275 return -ENOENT;
10276
10277 return 0;
10278}
10279
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010280#define INFO_STRING_LEN 255
Shannon Nelson7fd89542015-10-21 19:47:04 -040010281#define REMAIN(__x) (INFO_STRING_LEN - (__x))
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010282static void i40e_print_features(struct i40e_pf *pf)
10283{
10284 struct i40e_hw *hw = &pf->hw;
10285 char *buf, *string;
Shannon Nelson7fd89542015-10-21 19:47:04 -040010286 int i = 0;
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010287
10288 string = kzalloc(INFO_STRING_LEN, GFP_KERNEL);
10289 if (!string) {
10290 dev_err(&pf->pdev->dev, "Features string allocation failed\n");
10291 return;
10292 }
10293
10294 buf = string;
10295
Shannon Nelson7fd89542015-10-21 19:47:04 -040010296 i += snprintf(&buf[i], REMAIN(i), "Features: PF-id[%d] ", hw->pf_id);
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010297#ifdef CONFIG_PCI_IOV
Shannon Nelson7fd89542015-10-21 19:47:04 -040010298 i += snprintf(&buf[i], REMAIN(i), "VFs: %d ", pf->num_req_vfs);
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010299#endif
Shannon Nelson7fd89542015-10-21 19:47:04 -040010300 i += snprintf(&buf[i], REMAIN(i), "VSIs: %d QP: %d RX: %s ",
10301 pf->hw.func_caps.num_vsis,
10302 pf->vsi[pf->lan_vsi]->num_queue_pairs,
10303 pf->flags & I40E_FLAG_RX_PS_ENABLED ? "PS" : "1BUF");
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010304
10305 if (pf->flags & I40E_FLAG_RSS_ENABLED)
Shannon Nelson7fd89542015-10-21 19:47:04 -040010306 i += snprintf(&buf[i], REMAIN(i), "RSS ");
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010307 if (pf->flags & I40E_FLAG_FD_ATR_ENABLED)
Shannon Nelson7fd89542015-10-21 19:47:04 -040010308 i += snprintf(&buf[i], REMAIN(i), "FD_ATR ");
Akeem G Abodunrinc6423ff2014-05-10 04:49:08 +000010309 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
Shannon Nelson7fd89542015-10-21 19:47:04 -040010310 i += snprintf(&buf[i], REMAIN(i), "FD_SB ");
10311 i += snprintf(&buf[i], REMAIN(i), "NTUPLE ");
Akeem G Abodunrinc6423ff2014-05-10 04:49:08 +000010312 }
Neerav Parikh4d9b6042014-05-22 06:31:51 +000010313 if (pf->flags & I40E_FLAG_DCB_CAPABLE)
Shannon Nelson7fd89542015-10-21 19:47:04 -040010314 i += snprintf(&buf[i], REMAIN(i), "DCB ");
Jesse Brandeburgce6fcb32015-08-28 17:55:59 -040010315#if IS_ENABLED(CONFIG_VXLAN)
Shannon Nelson7fd89542015-10-21 19:47:04 -040010316 i += snprintf(&buf[i], REMAIN(i), "VxLAN ");
Jesse Brandeburgce6fcb32015-08-28 17:55:59 -040010317#endif
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010318 if (pf->flags & I40E_FLAG_PTP)
Shannon Nelson7fd89542015-10-21 19:47:04 -040010319 i += snprintf(&buf[i], REMAIN(i), "PTP ");
Vasu Dev38e00432014-08-01 13:27:03 -070010320#ifdef I40E_FCOE
10321 if (pf->flags & I40E_FLAG_FCOE_ENABLED)
Shannon Nelson7fd89542015-10-21 19:47:04 -040010322 i += snprintf(&buf[i], REMAIN(i), "FCOE ");
Vasu Dev38e00432014-08-01 13:27:03 -070010323#endif
Shannon Nelson6dec1012015-09-28 14:12:30 -040010324 if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
Shannon Nelson7fd89542015-10-21 19:47:04 -040010325 i += snprintf(&buf[i], REMAIN(i), "VEPA ");
Shannon Nelson6dec1012015-09-28 14:12:30 -040010326 else
10327 buf += sprintf(buf, "VEPA ");
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010328
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010329 dev_info(&pf->pdev->dev, "%s\n", string);
10330 kfree(string);
Shannon Nelson7fd89542015-10-21 19:47:04 -040010331 WARN_ON(i > INFO_STRING_LEN);
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010332}
10333
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010334/**
10335 * i40e_probe - Device initialization routine
10336 * @pdev: PCI device information struct
10337 * @ent: entry in i40e_pci_tbl
10338 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +000010339 * i40e_probe initializes a PF identified by a pci_dev structure.
10340 * The OS initialization, configuring of the PF private structure,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010341 * and a hardware reset occur.
10342 *
10343 * Returns 0 on success, negative on failure
10344 **/
10345static int i40e_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
10346{
Catherine Sullivane8278452015-02-06 08:52:08 +000010347 struct i40e_aq_get_phy_abilities_resp abilities;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010348 struct i40e_pf *pf;
10349 struct i40e_hw *hw;
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +000010350 static u16 pfs_found;
Shannon Nelson1d5109d2015-08-26 15:14:08 -040010351 u16 wol_nvm_bits;
Catherine Sullivand4dfb812013-11-28 06:39:21 +000010352 u16 link_status;
Jean Sacren6f66a482015-09-19 05:08:45 -060010353 int err;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010354 u32 len;
Anjali Singhai Jain4f2f017c2015-10-21 19:47:07 -040010355 u32 val;
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +000010356 u32 i;
Helin Zhang58fc3262015-10-01 14:37:38 -040010357 u8 set_fc_aq_fail;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010358
10359 err = pci_enable_device_mem(pdev);
10360 if (err)
10361 return err;
10362
10363 /* set up for high or low dma */
Mitch Williams64942942014-02-11 08:26:33 +000010364 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
Mitch Williams64942942014-02-11 08:26:33 +000010365 if (err) {
Jean Sacrene3e3bfd2014-03-25 04:30:27 +000010366 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
10367 if (err) {
10368 dev_err(&pdev->dev,
10369 "DMA configuration failed: 0x%x\n", err);
10370 goto err_dma;
10371 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010372 }
10373
10374 /* set up pci connections */
10375 err = pci_request_selected_regions(pdev, pci_select_bars(pdev,
10376 IORESOURCE_MEM), i40e_driver_name);
10377 if (err) {
10378 dev_info(&pdev->dev,
10379 "pci_request_selected_regions failed %d\n", err);
10380 goto err_pci_reg;
10381 }
10382
10383 pci_enable_pcie_error_reporting(pdev);
10384 pci_set_master(pdev);
10385
10386 /* Now that we have a PCI connection, we need to do the
10387 * low level device setup. This is primarily setting up
10388 * the Admin Queue structures and then querying for the
10389 * device's current profile information.
10390 */
10391 pf = kzalloc(sizeof(*pf), GFP_KERNEL);
10392 if (!pf) {
10393 err = -ENOMEM;
10394 goto err_pf_alloc;
10395 }
10396 pf->next_vsi = 0;
10397 pf->pdev = pdev;
10398 set_bit(__I40E_DOWN, &pf->state);
10399
10400 hw = &pf->hw;
10401 hw->back = pf;
Anjali Singhai232f4702015-02-26 16:15:39 +000010402
Shannon Nelson2ac8b672015-07-23 16:54:37 -040010403 pf->ioremap_len = min_t(int, pci_resource_len(pdev, 0),
10404 I40E_MAX_CSR_SPACE);
Anjali Singhai232f4702015-02-26 16:15:39 +000010405
Shannon Nelson2ac8b672015-07-23 16:54:37 -040010406 hw->hw_addr = ioremap(pci_resource_start(pdev, 0), pf->ioremap_len);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010407 if (!hw->hw_addr) {
10408 err = -EIO;
10409 dev_info(&pdev->dev, "ioremap(0x%04x, 0x%04x) failed: 0x%x\n",
10410 (unsigned int)pci_resource_start(pdev, 0),
Shannon Nelson2ac8b672015-07-23 16:54:37 -040010411 pf->ioremap_len, err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010412 goto err_ioremap;
10413 }
10414 hw->vendor_id = pdev->vendor;
10415 hw->device_id = pdev->device;
10416 pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
10417 hw->subsystem_vendor_id = pdev->subsystem_vendor;
10418 hw->subsystem_device_id = pdev->subsystem_device;
10419 hw->bus.device = PCI_SLOT(pdev->devfn);
10420 hw->bus.func = PCI_FUNC(pdev->devfn);
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +000010421 pf->instance = pfs_found;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010422
Shannon Nelson5b5faa42014-10-17 03:14:51 +000010423 if (debug != -1) {
10424 pf->msg_enable = pf->hw.debug_mask;
10425 pf->msg_enable = debug;
10426 }
10427
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +000010428 /* do a special CORER for clearing PXE mode once at init */
10429 if (hw->revision_id == 0 &&
10430 (rd32(hw, I40E_GLLAN_RCTL_0) & I40E_GLLAN_RCTL_0_PXE_MODE_MASK)) {
10431 wr32(hw, I40E_GLGEN_RTRIG, I40E_GLGEN_RTRIG_CORER_MASK);
10432 i40e_flush(hw);
10433 msleep(200);
10434 pf->corer_count++;
10435
10436 i40e_clear_pxe_mode(hw);
10437 }
10438
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010439 /* Reset here to make sure all is clean and to define PF 'n' */
Shannon Nelson838d41d2014-06-04 20:41:27 +000010440 i40e_clear_hw(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010441 err = i40e_pf_reset(hw);
10442 if (err) {
10443 dev_info(&pdev->dev, "Initial pf_reset failed: %d\n", err);
10444 goto err_pf_reset;
10445 }
10446 pf->pfr_count++;
10447
10448 hw->aq.num_arq_entries = I40E_AQ_LEN;
10449 hw->aq.num_asq_entries = I40E_AQ_LEN;
10450 hw->aq.arq_buf_size = I40E_MAX_AQ_BUF_SIZE;
10451 hw->aq.asq_buf_size = I40E_MAX_AQ_BUF_SIZE;
10452 pf->adminq_work_limit = I40E_AQ_WORK_LIMIT;
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +000010453
Carolyn Wybornyb294ac72014-12-11 07:06:39 +000010454 snprintf(pf->int_name, sizeof(pf->int_name) - 1,
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +000010455 "%s-%s:misc",
10456 dev_driver_string(&pf->pdev->dev), dev_name(&pdev->dev));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010457
10458 err = i40e_init_shared_code(hw);
10459 if (err) {
Anjali Singhai Jainb2a75c52015-04-27 14:57:20 -040010460 dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
10461 err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010462 goto err_pf_reset;
10463 }
10464
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +000010465 /* set up a default setting for link flow control */
10466 pf->hw.fc.requested_mode = I40E_FC_NONE;
10467
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010468 err = i40e_init_adminq(hw);
Carolyn Wybornyf0b44442015-08-31 19:54:49 -040010469
Shannon Nelson6dec1012015-09-28 14:12:30 -040010470 /* provide nvm, fw, api versions */
10471 dev_info(&pdev->dev, "fw %d.%d.%05d api %d.%d nvm %s\n",
10472 hw->aq.fw_maj_ver, hw->aq.fw_min_ver, hw->aq.fw_build,
10473 hw->aq.api_maj_ver, hw->aq.api_min_ver,
10474 i40e_nvm_version_str(hw));
Carolyn Wybornyf0b44442015-08-31 19:54:49 -040010475
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010476 if (err) {
10477 dev_info(&pdev->dev,
Catherine Sullivan7aa67612014-07-09 07:46:17 +000010478 "The driver for the device stopped because the NVM image is newer than expected. You must install the most recent version of the network driver.\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010479 goto err_pf_reset;
10480 }
10481
Catherine Sullivan7aa67612014-07-09 07:46:17 +000010482 if (hw->aq.api_maj_ver == I40E_FW_API_VERSION_MAJOR &&
10483 hw->aq.api_min_ver > I40E_FW_API_VERSION_MINOR)
Shannon Nelson278b6f62014-06-04 01:41:03 +000010484 dev_info(&pdev->dev,
Catherine Sullivan7aa67612014-07-09 07:46:17 +000010485 "The driver for the device detected a newer version of the NVM image than expected. Please install the most recent version of the network driver.\n");
10486 else if (hw->aq.api_maj_ver < I40E_FW_API_VERSION_MAJOR ||
10487 hw->aq.api_min_ver < (I40E_FW_API_VERSION_MINOR - 1))
Shannon Nelson278b6f62014-06-04 01:41:03 +000010488 dev_info(&pdev->dev,
Catherine Sullivan7aa67612014-07-09 07:46:17 +000010489 "The driver for the device detected an older version of the NVM image than expected. Please update the NVM image.\n");
Shannon Nelson278b6f62014-06-04 01:41:03 +000010490
Shannon Nelson4eb3f762014-03-06 08:59:58 +000010491 i40e_verify_eeprom(pf);
10492
Jesse Brandeburg2c5fe332014-04-23 04:49:57 +000010493 /* Rev 0 hardware was never productized */
10494 if (hw->revision_id < 1)
10495 dev_warn(&pdev->dev, "This device is a pre-production adapter/LOM. Please be aware there may be issues with your hardware. If you are experiencing problems please contact your Intel or hardware representative who provided you with this hardware.\n");
10496
Shannon Nelson6ff4ef82013-12-21 05:44:49 +000010497 i40e_clear_pxe_mode(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010498 err = i40e_get_capabilities(pf);
10499 if (err)
10500 goto err_adminq_setup;
10501
10502 err = i40e_sw_init(pf);
10503 if (err) {
10504 dev_info(&pdev->dev, "sw_init failed: %d\n", err);
10505 goto err_sw_init;
10506 }
10507
10508 err = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
10509 hw->func_caps.num_rx_qp,
10510 pf->fcoe_hmc_cntx_num, pf->fcoe_hmc_filt_num);
10511 if (err) {
10512 dev_info(&pdev->dev, "init_lan_hmc failed: %d\n", err);
10513 goto err_init_lan_hmc;
10514 }
10515
10516 err = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
10517 if (err) {
10518 dev_info(&pdev->dev, "configure_lan_hmc failed: %d\n", err);
10519 err = -ENOENT;
10520 goto err_configure_lan_hmc;
10521 }
10522
Neerav Parikhb686ece2014-12-14 01:55:11 +000010523 /* Disable LLDP for NICs that have firmware versions lower than v4.3.
10524 * Ignore error return codes because if it was already disabled via
10525 * hardware settings this will fail
10526 */
10527 if (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 3)) ||
10528 (pf->hw.aq.fw_maj_ver < 4)) {
10529 dev_info(&pdev->dev, "Stopping firmware LLDP agent.\n");
10530 i40e_aq_stop_lldp(hw, true, NULL);
10531 }
10532
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010533 i40e_get_mac_addr(hw, hw->mac.addr);
Jesse Brandeburgf62b5062013-11-28 06:39:27 +000010534 if (!is_valid_ether_addr(hw->mac.addr)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010535 dev_info(&pdev->dev, "invalid MAC address %pM\n", hw->mac.addr);
10536 err = -EIO;
10537 goto err_mac_addr;
10538 }
10539 dev_info(&pdev->dev, "MAC address: %pM\n", hw->mac.addr);
Greg Rose9a173902014-05-22 06:32:02 +000010540 ether_addr_copy(hw->mac.perm_addr, hw->mac.addr);
Neerav Parikh1f224ad2014-02-12 01:45:31 +000010541 i40e_get_port_mac_addr(hw, hw->mac.port_addr);
10542 if (is_valid_ether_addr(hw->mac.port_addr))
10543 pf->flags |= I40E_FLAG_PORT_ID_VALID;
Vasu Dev38e00432014-08-01 13:27:03 -070010544#ifdef I40E_FCOE
10545 err = i40e_get_san_mac_addr(hw, hw->mac.san_addr);
10546 if (err)
10547 dev_info(&pdev->dev,
10548 "(non-fatal) SAN MAC retrieval failed: %d\n", err);
10549 if (!is_valid_ether_addr(hw->mac.san_addr)) {
10550 dev_warn(&pdev->dev, "invalid SAN MAC address %pM, falling back to LAN MAC\n",
10551 hw->mac.san_addr);
10552 ether_addr_copy(hw->mac.san_addr, hw->mac.addr);
10553 }
10554 dev_info(&pf->pdev->dev, "SAN MAC: %pM\n", hw->mac.san_addr);
10555#endif /* I40E_FCOE */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010556
10557 pci_set_drvdata(pdev, pf);
10558 pci_save_state(pdev);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -080010559#ifdef CONFIG_I40E_DCB
10560 err = i40e_init_pf_dcb(pf);
10561 if (err) {
Shannon Nelsonaebfc812014-12-11 07:06:38 +000010562 dev_info(&pdev->dev, "DCB init failed %d, disabled\n", err);
Neerav Parikh4d9b6042014-05-22 06:31:51 +000010563 pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
Neerav Parikh014269f2014-04-01 07:11:48 +000010564 /* Continue without DCB enabled */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -080010565 }
10566#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010567
10568 /* set up periodic task facility */
10569 setup_timer(&pf->service_timer, i40e_service_timer, (unsigned long)pf);
10570 pf->service_timer_period = HZ;
10571
10572 INIT_WORK(&pf->service_task, i40e_service_task);
10573 clear_bit(__I40E_SERVICE_SCHED, &pf->state);
10574 pf->flags |= I40E_FLAG_NEED_LINK_UPDATE;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010575
Shannon Nelson1d5109d2015-08-26 15:14:08 -040010576 /* NVM bit on means WoL disabled for the port */
10577 i40e_read_nvm_word(hw, I40E_SR_NVM_WAKE_ON_LAN, &wol_nvm_bits);
10578 if ((1 << hw->port) & wol_nvm_bits || hw->partition_id != 1)
10579 pf->wol_en = false;
10580 else
10581 pf->wol_en = true;
Shannon Nelson8e2773a2013-11-28 06:39:22 +000010582 device_set_wakeup_enable(&pf->pdev->dev, pf->wol_en);
10583
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010584 /* set up the main switch operations */
10585 i40e_determine_queue_usage(pf);
Jesse Brandeburgc11472802015-04-07 19:45:39 -040010586 err = i40e_init_interrupt_scheme(pf);
10587 if (err)
10588 goto err_switch_setup;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010589
Mitch Williams505682c2014-05-20 08:01:37 +000010590 /* The number of VSIs reported by the FW is the minimum guaranteed
10591 * to us; HW supports far more and we share the remaining pool with
10592 * the other PFs. We allocate space for more than the guarantee with
10593 * the understanding that we might not get them all later.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010594 */
Mitch Williams505682c2014-05-20 08:01:37 +000010595 if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
10596 pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
10597 else
10598 pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
10599
10600 /* Set up the *vsi struct and our local tracking of the MAIN PF vsi. */
10601 len = sizeof(struct i40e_vsi *) * pf->num_alloc_vsi;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010602 pf->vsi = kzalloc(len, GFP_KERNEL);
Wei Yongjuned87ac02013-09-24 05:17:25 +000010603 if (!pf->vsi) {
10604 err = -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010605 goto err_switch_setup;
Wei Yongjuned87ac02013-09-24 05:17:25 +000010606 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010607
Anjali Singhai Jainfa11cb32015-05-27 12:06:14 -040010608#ifdef CONFIG_PCI_IOV
10609 /* prep for VF support */
10610 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
10611 (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
10612 !test_bit(__I40E_BAD_EEPROM, &pf->state)) {
10613 if (pci_num_vf(pdev))
10614 pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
10615 }
10616#endif
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010617 err = i40e_setup_pf_switch(pf, false);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010618 if (err) {
10619 dev_info(&pdev->dev, "setup_pf_switch failed: %d\n", err);
10620 goto err_vsis;
10621 }
Helin Zhang58fc3262015-10-01 14:37:38 -040010622
10623 /* Make sure flow control is set according to current settings */
10624 err = i40e_set_fc(hw, &set_fc_aq_fail, true);
10625 if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_GET)
10626 dev_dbg(&pf->pdev->dev,
10627 "Set fc with err %s aq_err %s on get_phy_cap\n",
10628 i40e_stat_str(hw, err),
10629 i40e_aq_str(hw, hw->aq.asq_last_status));
10630 if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_SET)
10631 dev_dbg(&pf->pdev->dev,
10632 "Set fc with err %s aq_err %s on set_phy_config\n",
10633 i40e_stat_str(hw, err),
10634 i40e_aq_str(hw, hw->aq.asq_last_status));
10635 if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_UPDATE)
10636 dev_dbg(&pf->pdev->dev,
10637 "Set fc with err %s aq_err %s on get_link_info\n",
10638 i40e_stat_str(hw, err),
10639 i40e_aq_str(hw, hw->aq.asq_last_status));
10640
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +000010641 /* if FDIR VSI was set up, start it now */
Mitch Williams505682c2014-05-20 08:01:37 +000010642 for (i = 0; i < pf->num_alloc_vsi; i++) {
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +000010643 if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
10644 i40e_vsi_open(pf->vsi[i]);
10645 break;
10646 }
10647 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010648
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +000010649 /* driver is only interested in link up/down and module qualification
10650 * reports from firmware
10651 */
10652 err = i40e_aq_set_phy_int_mask(&pf->hw,
10653 I40E_AQ_EVENT_LINK_UPDOWN |
10654 I40E_AQ_EVENT_MODULE_QUAL_FAIL, NULL);
10655 if (err)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010656 dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
10657 i40e_stat_str(&pf->hw, err),
10658 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +000010659
Anjali Singhai Jain4f2f017c2015-10-21 19:47:07 -040010660 /* Reconfigure hardware for allowing smaller MSS in the case
10661 * of TSO, so that we avoid the MDD being fired and causing
10662 * a reset in the case of small MSS+TSO.
10663 */
10664 val = rd32(hw, I40E_REG_MSS);
10665 if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
10666 val &= ~I40E_REG_MSS_MIN_MASK;
10667 val |= I40E_64BYTE_MSS;
10668 wr32(hw, I40E_REG_MSS, val);
10669 }
10670
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +000010671 if (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 33)) ||
10672 (pf->hw.aq.fw_maj_ver < 4)) {
10673 msleep(75);
10674 err = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
10675 if (err)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010676 dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
10677 i40e_stat_str(&pf->hw, err),
10678 i40e_aq_str(&pf->hw,
10679 pf->hw.aq.asq_last_status));
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +000010680 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010681 /* The main driver is (mostly) up and happy. We need to set this state
10682 * before setting up the misc vector or we get a race and the vector
10683 * ends up disabled forever.
10684 */
10685 clear_bit(__I40E_DOWN, &pf->state);
10686
10687 /* In case of MSIX we are going to setup the misc vector right here
10688 * to handle admin queue events etc. In case of legacy and MSI
10689 * the misc functionality and queue processing is combined in
10690 * the same vector and that gets setup at open.
10691 */
10692 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
10693 err = i40e_setup_misc_vector(pf);
10694 if (err) {
10695 dev_info(&pdev->dev,
10696 "setup of misc vector failed: %d\n", err);
10697 goto err_vsis;
10698 }
10699 }
10700
Greg Rosedf805f62014-04-04 04:43:16 +000010701#ifdef CONFIG_PCI_IOV
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010702 /* prep for VF support */
10703 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
Shannon Nelson4eb3f762014-03-06 08:59:58 +000010704 (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
10705 !test_bit(__I40E_BAD_EEPROM, &pf->state)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010706 u32 val;
10707
10708 /* disable link interrupts for VFs */
10709 val = rd32(hw, I40E_PFGEN_PORTMDIO_NUM);
10710 val &= ~I40E_PFGEN_PORTMDIO_NUM_VFLINK_STAT_ENA_MASK;
10711 wr32(hw, I40E_PFGEN_PORTMDIO_NUM, val);
10712 i40e_flush(hw);
Mitch Williams4aeec012014-02-13 03:48:47 -080010713
10714 if (pci_num_vf(pdev)) {
10715 dev_info(&pdev->dev,
10716 "Active VFs found, allocating resources.\n");
10717 err = i40e_alloc_vfs(pf, pci_num_vf(pdev));
10718 if (err)
10719 dev_info(&pdev->dev,
10720 "Error %d allocating resources for existing VFs\n",
10721 err);
10722 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010723 }
Greg Rosedf805f62014-04-04 04:43:16 +000010724#endif /* CONFIG_PCI_IOV */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010725
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +000010726 pfs_found++;
10727
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010728 i40e_dbg_pf_init(pf);
10729
10730 /* tell the firmware that we're starting */
Jesse Brandeburg44033fa2014-04-23 04:50:15 +000010731 i40e_send_version(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010732
10733 /* since everything's happy, start the service_task timer */
10734 mod_timer(&pf->service_timer,
10735 round_jiffies(jiffies + pf->service_timer_period));
10736
Vasu Dev38e00432014-08-01 13:27:03 -070010737#ifdef I40E_FCOE
10738 /* create FCoE interface */
10739 i40e_fcoe_vsi_setup(pf);
10740
10741#endif
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040010742#define PCI_SPEED_SIZE 8
10743#define PCI_WIDTH_SIZE 8
10744 /* Devices on the IOSF bus do not have this information
10745 * and will report PCI Gen 1 x 1 by default so don't bother
10746 * checking them.
10747 */
10748 if (!(pf->flags & I40E_FLAG_NO_PCI_LINK_CHECK)) {
10749 char speed[PCI_SPEED_SIZE] = "Unknown";
10750 char width[PCI_WIDTH_SIZE] = "Unknown";
Catherine Sullivand4dfb812013-11-28 06:39:21 +000010751
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040010752 /* Get the negotiated link width and speed from PCI config
10753 * space
10754 */
10755 pcie_capability_read_word(pf->pdev, PCI_EXP_LNKSTA,
10756 &link_status);
Catherine Sullivand4dfb812013-11-28 06:39:21 +000010757
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040010758 i40e_set_pci_config_data(hw, link_status);
Catherine Sullivand4dfb812013-11-28 06:39:21 +000010759
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040010760 switch (hw->bus.speed) {
10761 case i40e_bus_speed_8000:
10762 strncpy(speed, "8.0", PCI_SPEED_SIZE); break;
10763 case i40e_bus_speed_5000:
10764 strncpy(speed, "5.0", PCI_SPEED_SIZE); break;
10765 case i40e_bus_speed_2500:
10766 strncpy(speed, "2.5", PCI_SPEED_SIZE); break;
10767 default:
10768 break;
10769 }
10770 switch (hw->bus.width) {
10771 case i40e_bus_width_pcie_x8:
10772 strncpy(width, "8", PCI_WIDTH_SIZE); break;
10773 case i40e_bus_width_pcie_x4:
10774 strncpy(width, "4", PCI_WIDTH_SIZE); break;
10775 case i40e_bus_width_pcie_x2:
10776 strncpy(width, "2", PCI_WIDTH_SIZE); break;
10777 case i40e_bus_width_pcie_x1:
10778 strncpy(width, "1", PCI_WIDTH_SIZE); break;
10779 default:
10780 break;
10781 }
10782
10783 dev_info(&pdev->dev, "PCI-Express: Speed %sGT/s Width x%s\n",
10784 speed, width);
10785
10786 if (hw->bus.width < i40e_bus_width_pcie_x8 ||
10787 hw->bus.speed < i40e_bus_speed_8000) {
10788 dev_warn(&pdev->dev, "PCI-Express bandwidth available for this device may be insufficient for optimal performance.\n");
10789 dev_warn(&pdev->dev, "Please move the device to a different PCI-e link with more lanes and/or higher transfer rate.\n");
10790 }
Catherine Sullivand4dfb812013-11-28 06:39:21 +000010791 }
10792
Catherine Sullivane8278452015-02-06 08:52:08 +000010793 /* get the requested speeds from the fw */
10794 err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities, NULL);
10795 if (err)
Neerav Parikh8279e492015-09-03 17:18:50 -040010796 dev_dbg(&pf->pdev->dev, "get requested speeds ret = %s last_status = %s\n",
10797 i40e_stat_str(&pf->hw, err),
10798 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Catherine Sullivane8278452015-02-06 08:52:08 +000010799 pf->hw.phy.link_info.requested_speeds = abilities.link_speed;
10800
Catherine Sullivanfc72dbc2015-09-01 11:36:30 -040010801 /* get the supported phy types from the fw */
10802 err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities, NULL);
10803 if (err)
10804 dev_dbg(&pf->pdev->dev, "get supported phy types ret = %s last_status = %s\n",
10805 i40e_stat_str(&pf->hw, err),
10806 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10807 pf->hw.phy.phy_types = le32_to_cpu(abilities.phy_type);
10808
Anjali Singhai Jaine7358f52015-10-01 14:37:34 -040010809 /* Add a filter to drop all Flow control frames from any VSI from being
10810 * transmitted. By doing so we stop a malicious VF from sending out
10811 * PAUSE or PFC frames and potentially controlling traffic for other
10812 * PF/VF VSIs.
10813 * The FW can still send Flow control frames if enabled.
10814 */
10815 i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
10816 pf->main_vsi_seid);
10817
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010818 /* print a string summarizing features */
10819 i40e_print_features(pf);
10820
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010821 return 0;
10822
10823 /* Unwind what we've done if something failed in the setup */
10824err_vsis:
10825 set_bit(__I40E_DOWN, &pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010826 i40e_clear_interrupt_scheme(pf);
10827 kfree(pf->vsi);
Shannon Nelson04b03012013-11-28 06:39:34 +000010828err_switch_setup:
10829 i40e_reset_interrupt_capability(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010830 del_timer_sync(&pf->service_timer);
10831err_mac_addr:
10832err_configure_lan_hmc:
10833 (void)i40e_shutdown_lan_hmc(hw);
10834err_init_lan_hmc:
10835 kfree(pf->qp_pile);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010836err_sw_init:
10837err_adminq_setup:
10838 (void)i40e_shutdown_adminq(hw);
10839err_pf_reset:
10840 iounmap(hw->hw_addr);
10841err_ioremap:
10842 kfree(pf);
10843err_pf_alloc:
10844 pci_disable_pcie_error_reporting(pdev);
10845 pci_release_selected_regions(pdev,
10846 pci_select_bars(pdev, IORESOURCE_MEM));
10847err_pci_reg:
10848err_dma:
10849 pci_disable_device(pdev);
10850 return err;
10851}
10852
10853/**
10854 * i40e_remove - Device removal routine
10855 * @pdev: PCI device information struct
10856 *
10857 * i40e_remove is called by the PCI subsystem to alert the driver
10858 * that is should release a PCI device. This could be caused by a
10859 * Hot-Plug event, or because the driver is going to be removed from
10860 * memory.
10861 **/
10862static void i40e_remove(struct pci_dev *pdev)
10863{
10864 struct i40e_pf *pf = pci_get_drvdata(pdev);
Carolyn Wybornybcab2db2015-09-28 14:16:55 -040010865 struct i40e_hw *hw = &pf->hw;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010866 i40e_status ret_code;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010867 int i;
10868
10869 i40e_dbg_pf_exit(pf);
10870
Jacob Kellerbeb0dff2014-01-11 05:43:19 +000010871 i40e_ptp_stop(pf);
10872
Carolyn Wybornybcab2db2015-09-28 14:16:55 -040010873 /* Disable RSS in hw */
10874 wr32(hw, I40E_PFQF_HENA(0), 0);
10875 wr32(hw, I40E_PFQF_HENA(1), 0);
10876
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010877 /* no more scheduling of any task */
10878 set_bit(__I40E_DOWN, &pf->state);
10879 del_timer_sync(&pf->service_timer);
10880 cancel_work_sync(&pf->service_task);
Mitch A Williams33c62b32015-02-21 06:44:51 +000010881 i40e_fdir_teardown(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010882
Mitch Williamseb2d80b2014-02-13 03:48:48 -080010883 if (pf->flags & I40E_FLAG_SRIOV_ENABLED) {
10884 i40e_free_vfs(pf);
10885 pf->flags &= ~I40E_FLAG_SRIOV_ENABLED;
10886 }
10887
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010888 i40e_fdir_teardown(pf);
10889
10890 /* If there is a switch structure or any orphans, remove them.
10891 * This will leave only the PF's VSI remaining.
10892 */
10893 for (i = 0; i < I40E_MAX_VEB; i++) {
10894 if (!pf->veb[i])
10895 continue;
10896
10897 if (pf->veb[i]->uplink_seid == pf->mac_seid ||
10898 pf->veb[i]->uplink_seid == 0)
10899 i40e_switch_branch_release(pf->veb[i]);
10900 }
10901
10902 /* Now we can shutdown the PF's VSI, just before we kill
10903 * adminq and hmc.
10904 */
10905 if (pf->vsi[pf->lan_vsi])
10906 i40e_vsi_release(pf->vsi[pf->lan_vsi]);
10907
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010908 /* shutdown and destroy the HMC */
Shannon Nelson60442de2014-04-23 04:50:13 +000010909 if (pf->hw.hmc.hmc_obj) {
10910 ret_code = i40e_shutdown_lan_hmc(&pf->hw);
10911 if (ret_code)
10912 dev_warn(&pdev->dev,
10913 "Failed to destroy the HMC resources: %d\n",
10914 ret_code);
10915 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010916
10917 /* shutdown the adminq */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010918 ret_code = i40e_shutdown_adminq(&pf->hw);
10919 if (ret_code)
10920 dev_warn(&pdev->dev,
10921 "Failed to destroy the Admin Queue resources: %d\n",
10922 ret_code);
10923
10924 /* Clear all dynamic memory lists of rings, q_vectors, and VSIs */
10925 i40e_clear_interrupt_scheme(pf);
Mitch Williams505682c2014-05-20 08:01:37 +000010926 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010927 if (pf->vsi[i]) {
10928 i40e_vsi_clear_rings(pf->vsi[i]);
10929 i40e_vsi_clear(pf->vsi[i]);
10930 pf->vsi[i] = NULL;
10931 }
10932 }
10933
10934 for (i = 0; i < I40E_MAX_VEB; i++) {
10935 kfree(pf->veb[i]);
10936 pf->veb[i] = NULL;
10937 }
10938
10939 kfree(pf->qp_pile);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010940 kfree(pf->vsi);
10941
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010942 iounmap(pf->hw.hw_addr);
10943 kfree(pf);
10944 pci_release_selected_regions(pdev,
10945 pci_select_bars(pdev, IORESOURCE_MEM));
10946
10947 pci_disable_pcie_error_reporting(pdev);
10948 pci_disable_device(pdev);
10949}
10950
10951/**
10952 * i40e_pci_error_detected - warning that something funky happened in PCI land
10953 * @pdev: PCI device information struct
10954 *
10955 * Called to warn that something happened and the error handling steps
10956 * are in progress. Allows the driver to quiesce things, be ready for
10957 * remediation.
10958 **/
10959static pci_ers_result_t i40e_pci_error_detected(struct pci_dev *pdev,
10960 enum pci_channel_state error)
10961{
10962 struct i40e_pf *pf = pci_get_drvdata(pdev);
10963
10964 dev_info(&pdev->dev, "%s: error %d\n", __func__, error);
10965
10966 /* shutdown all operations */
Shannon Nelson9007bcc2013-11-26 10:49:23 +000010967 if (!test_bit(__I40E_SUSPENDED, &pf->state)) {
10968 rtnl_lock();
10969 i40e_prep_for_reset(pf);
10970 rtnl_unlock();
10971 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010972
10973 /* Request a slot reset */
10974 return PCI_ERS_RESULT_NEED_RESET;
10975}
10976
10977/**
10978 * i40e_pci_error_slot_reset - a PCI slot reset just happened
10979 * @pdev: PCI device information struct
10980 *
10981 * Called to find if the driver can work with the device now that
10982 * the pci slot has been reset. If a basic connection seems good
10983 * (registers are readable and have sane content) then return a
10984 * happy little PCI_ERS_RESULT_xxx.
10985 **/
10986static pci_ers_result_t i40e_pci_error_slot_reset(struct pci_dev *pdev)
10987{
10988 struct i40e_pf *pf = pci_get_drvdata(pdev);
10989 pci_ers_result_t result;
10990 int err;
10991 u32 reg;
10992
Shannon Nelsonfb43201f2015-08-26 15:14:17 -040010993 dev_dbg(&pdev->dev, "%s\n", __func__);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010994 if (pci_enable_device_mem(pdev)) {
10995 dev_info(&pdev->dev,
10996 "Cannot re-enable PCI device after reset.\n");
10997 result = PCI_ERS_RESULT_DISCONNECT;
10998 } else {
10999 pci_set_master(pdev);
11000 pci_restore_state(pdev);
11001 pci_save_state(pdev);
11002 pci_wake_from_d3(pdev, false);
11003
11004 reg = rd32(&pf->hw, I40E_GLGEN_RTRIG);
11005 if (reg == 0)
11006 result = PCI_ERS_RESULT_RECOVERED;
11007 else
11008 result = PCI_ERS_RESULT_DISCONNECT;
11009 }
11010
11011 err = pci_cleanup_aer_uncorrect_error_status(pdev);
11012 if (err) {
11013 dev_info(&pdev->dev,
11014 "pci_cleanup_aer_uncorrect_error_status failed 0x%0x\n",
11015 err);
11016 /* non-fatal, continue */
11017 }
11018
11019 return result;
11020}
11021
11022/**
11023 * i40e_pci_error_resume - restart operations after PCI error recovery
11024 * @pdev: PCI device information struct
11025 *
11026 * Called to allow the driver to bring things back up after PCI error
11027 * and/or reset recovery has finished.
11028 **/
11029static void i40e_pci_error_resume(struct pci_dev *pdev)
11030{
11031 struct i40e_pf *pf = pci_get_drvdata(pdev);
11032
Shannon Nelsonfb43201f2015-08-26 15:14:17 -040011033 dev_dbg(&pdev->dev, "%s\n", __func__);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011034 if (test_bit(__I40E_SUSPENDED, &pf->state))
11035 return;
11036
11037 rtnl_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011038 i40e_handle_reset_warning(pf);
Vasily Averin4c4935a2015-07-08 15:04:26 +030011039 rtnl_unlock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011040}
11041
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011042/**
11043 * i40e_shutdown - PCI callback for shutting down
11044 * @pdev: PCI device information struct
11045 **/
11046static void i40e_shutdown(struct pci_dev *pdev)
11047{
11048 struct i40e_pf *pf = pci_get_drvdata(pdev);
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011049 struct i40e_hw *hw = &pf->hw;
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011050
11051 set_bit(__I40E_SUSPENDED, &pf->state);
11052 set_bit(__I40E_DOWN, &pf->state);
11053 rtnl_lock();
11054 i40e_prep_for_reset(pf);
11055 rtnl_unlock();
11056
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011057 wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
11058 wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
11059
Catherine Sullivan02b42492015-07-10 19:35:59 -040011060 del_timer_sync(&pf->service_timer);
11061 cancel_work_sync(&pf->service_task);
11062 i40e_fdir_teardown(pf);
11063
11064 rtnl_lock();
11065 i40e_prep_for_reset(pf);
11066 rtnl_unlock();
11067
11068 wr32(hw, I40E_PFPM_APM,
11069 (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
11070 wr32(hw, I40E_PFPM_WUFC,
11071 (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
11072
Shannon Nelsone1477582015-02-21 06:44:33 +000011073 i40e_clear_interrupt_scheme(pf);
11074
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011075 if (system_state == SYSTEM_POWER_OFF) {
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011076 pci_wake_from_d3(pdev, pf->wol_en);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011077 pci_set_power_state(pdev, PCI_D3hot);
11078 }
11079}
11080
11081#ifdef CONFIG_PM
11082/**
11083 * i40e_suspend - PCI callback for moving to D3
11084 * @pdev: PCI device information struct
11085 **/
11086static int i40e_suspend(struct pci_dev *pdev, pm_message_t state)
11087{
11088 struct i40e_pf *pf = pci_get_drvdata(pdev);
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011089 struct i40e_hw *hw = &pf->hw;
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011090
11091 set_bit(__I40E_SUSPENDED, &pf->state);
11092 set_bit(__I40E_DOWN, &pf->state);
Mitch Williams3932dbf2015-03-31 00:45:04 -070011093
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011094 rtnl_lock();
11095 i40e_prep_for_reset(pf);
11096 rtnl_unlock();
11097
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011098 wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
11099 wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
11100
11101 pci_wake_from_d3(pdev, pf->wol_en);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011102 pci_set_power_state(pdev, PCI_D3hot);
11103
11104 return 0;
11105}
11106
11107/**
11108 * i40e_resume - PCI callback for waking up from D3
11109 * @pdev: PCI device information struct
11110 **/
11111static int i40e_resume(struct pci_dev *pdev)
11112{
11113 struct i40e_pf *pf = pci_get_drvdata(pdev);
11114 u32 err;
11115
11116 pci_set_power_state(pdev, PCI_D0);
11117 pci_restore_state(pdev);
11118 /* pci_restore_state() clears dev->state_saves, so
11119 * call pci_save_state() again to restore it.
11120 */
11121 pci_save_state(pdev);
11122
11123 err = pci_enable_device_mem(pdev);
11124 if (err) {
Shannon Nelsonfb43201f2015-08-26 15:14:17 -040011125 dev_err(&pdev->dev, "Cannot enable PCI device from suspend\n");
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011126 return err;
11127 }
11128 pci_set_master(pdev);
11129
11130 /* no wakeup events while running */
11131 pci_wake_from_d3(pdev, false);
11132
11133 /* handling the reset will rebuild the device state */
11134 if (test_and_clear_bit(__I40E_SUSPENDED, &pf->state)) {
11135 clear_bit(__I40E_DOWN, &pf->state);
11136 rtnl_lock();
11137 i40e_reset_and_rebuild(pf, false);
11138 rtnl_unlock();
11139 }
11140
11141 return 0;
11142}
11143
11144#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011145static const struct pci_error_handlers i40e_err_handler = {
11146 .error_detected = i40e_pci_error_detected,
11147 .slot_reset = i40e_pci_error_slot_reset,
11148 .resume = i40e_pci_error_resume,
11149};
11150
11151static struct pci_driver i40e_driver = {
11152 .name = i40e_driver_name,
11153 .id_table = i40e_pci_tbl,
11154 .probe = i40e_probe,
11155 .remove = i40e_remove,
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011156#ifdef CONFIG_PM
11157 .suspend = i40e_suspend,
11158 .resume = i40e_resume,
11159#endif
11160 .shutdown = i40e_shutdown,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011161 .err_handler = &i40e_err_handler,
11162 .sriov_configure = i40e_pci_sriov_configure,
11163};
11164
11165/**
11166 * i40e_init_module - Driver registration routine
11167 *
11168 * i40e_init_module is the first routine called when the driver is
11169 * loaded. All it does is register with the PCI subsystem.
11170 **/
11171static int __init i40e_init_module(void)
11172{
11173 pr_info("%s: %s - version %s\n", i40e_driver_name,
11174 i40e_driver_string, i40e_driver_version_str);
11175 pr_info("%s: %s\n", i40e_driver_name, i40e_copyright);
Greg Rose96664482015-02-06 08:52:13 +000011176
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011177 i40e_dbg_init();
11178 return pci_register_driver(&i40e_driver);
11179}
11180module_init(i40e_init_module);
11181
11182/**
11183 * i40e_exit_module - Driver exit cleanup routine
11184 *
11185 * i40e_exit_module is called just before the driver is removed
11186 * from memory.
11187 **/
11188static void __exit i40e_exit_module(void)
11189{
11190 pci_unregister_driver(&i40e_driver);
11191 i40e_dbg_exit();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011192}
11193module_exit(i40e_exit_module);