blob: cb678ed7a2ad15e5e174021c25f30470b65e2f2d [file] [log] [blame]
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001/*******************************************************************************
2 *
3 * Intel Ethernet Controller XL710 Family Linux Driver
Jesse Brandeburg2818ccd2016-01-13 16:51:38 -08004 * Copyright(c) 2013 - 2016 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
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050027#include <linux/etherdevice.h>
28#include <linux/of_net.h>
29#include <linux/pci.h>
30
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000031/* Local includes */
32#include "i40e.h"
Shannon Nelson4eb3f762014-03-06 08:59:58 +000033#include "i40e_diag.h"
Alexander Duyck06a5f7f2016-06-16 12:22:06 -070034#include <net/udp_tunnel.h>
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000035
36const char i40e_driver_name[] = "i40e";
37static const char i40e_driver_string[] =
38 "Intel(R) Ethernet Connection XL710 Network Driver";
39
40#define DRV_KERN "-k"
41
Catherine Sullivane8e724d2014-07-10 07:58:26 +000042#define DRV_VERSION_MAJOR 1
Bimmy Pujari07061952016-05-16 10:26:45 -070043#define DRV_VERSION_MINOR 6
Bimmy Pujaricfffef72016-11-28 16:06:11 -080044#define DRV_VERSION_BUILD 27
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000045#define DRV_VERSION __stringify(DRV_VERSION_MAJOR) "." \
46 __stringify(DRV_VERSION_MINOR) "." \
47 __stringify(DRV_VERSION_BUILD) DRV_KERN
48const char i40e_driver_version_str[] = DRV_VERSION;
Jesse Brandeburg8fb905b2014-01-17 15:36:33 -080049static const char i40e_copyright[] = "Copyright (c) 2013 - 2014 Intel Corporation.";
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000050
51/* a bit of forward declarations */
52static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi);
53static void i40e_handle_reset_warning(struct i40e_pf *pf);
54static int i40e_add_vsi(struct i40e_vsi *vsi);
55static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi);
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000056static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000057static int i40e_setup_misc_vector(struct i40e_pf *pf);
58static void i40e_determine_queue_usage(struct i40e_pf *pf);
59static int i40e_setup_pf_filter_control(struct i40e_pf *pf);
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},
Shannon Nelsonab600852014-01-17 15:36:39 -080073 {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_B), 0},
74 {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_C), 0},
Shannon Nelsonab600852014-01-17 15:36:39 -080075 {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_A), 0},
76 {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_B), 0},
77 {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_C), 0},
Mitch Williams5960d332014-09-13 07:40:47 +000078 {PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T), 0},
Shannon Nelsonbc5166b92015-08-26 15:14:10 -040079 {PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T4), 0},
Anjali Singhai Jain35dae512015-12-22 14:25:03 -080080 {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_X722), 0},
81 {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_X722), 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},
Catherine Sullivand6bf58c2016-03-18 12:18:08 -070085 {PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_I_X722), 0},
Shannon Nelson48a3b512015-07-23 16:54:39 -040086 {PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2), 0},
87 {PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2_A), 0},
Carolyn Wyborny31232372016-11-21 13:03:48 -080088 {PCI_VDEVICE(INTEL, I40E_DEV_ID_25G_B), 0},
89 {PCI_VDEVICE(INTEL, I40E_DEV_ID_25G_SFP28), 0},
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000090 /* required last entry */
91 {0, }
92};
93MODULE_DEVICE_TABLE(pci, i40e_pci_tbl);
94
95#define I40E_MAX_VF_COUNT 128
96static int debug = -1;
Alexander Duyck5d4ca232016-09-30 08:21:46 -040097module_param(debug, uint, 0);
98MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all), Debug mask (0x8XXXXXXX)");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000099
100MODULE_AUTHOR("Intel Corporation, <e1000-devel@lists.sourceforge.net>");
101MODULE_DESCRIPTION("Intel(R) Ethernet Connection XL710 Network Driver");
102MODULE_LICENSE("GPL");
103MODULE_VERSION(DRV_VERSION);
104
Jesse Brandeburg2803b162015-12-22 14:25:08 -0800105static struct workqueue_struct *i40e_wq;
106
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000107/**
108 * i40e_allocate_dma_mem_d - OS specific memory alloc for shared code
109 * @hw: pointer to the HW structure
110 * @mem: ptr to mem struct to fill out
111 * @size: size of memory requested
112 * @alignment: what to align the allocation to
113 **/
114int i40e_allocate_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem,
115 u64 size, u32 alignment)
116{
117 struct i40e_pf *pf = (struct i40e_pf *)hw->back;
118
119 mem->size = ALIGN(size, alignment);
120 mem->va = dma_zalloc_coherent(&pf->pdev->dev, mem->size,
121 &mem->pa, GFP_KERNEL);
Jesse Brandeburg93bc73b2013-09-13 08:23:18 +0000122 if (!mem->va)
123 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000124
Jesse Brandeburg93bc73b2013-09-13 08:23:18 +0000125 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000126}
127
128/**
129 * i40e_free_dma_mem_d - OS specific memory free for shared code
130 * @hw: pointer to the HW structure
131 * @mem: ptr to mem struct to free
132 **/
133int i40e_free_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem)
134{
135 struct i40e_pf *pf = (struct i40e_pf *)hw->back;
136
137 dma_free_coherent(&pf->pdev->dev, mem->size, mem->va, mem->pa);
138 mem->va = NULL;
139 mem->pa = 0;
140 mem->size = 0;
141
142 return 0;
143}
144
145/**
146 * i40e_allocate_virt_mem_d - OS specific memory alloc for shared code
147 * @hw: pointer to the HW structure
148 * @mem: ptr to mem struct to fill out
149 * @size: size of memory requested
150 **/
151int i40e_allocate_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem,
152 u32 size)
153{
154 mem->size = size;
155 mem->va = kzalloc(size, GFP_KERNEL);
156
Jesse Brandeburg93bc73b2013-09-13 08:23:18 +0000157 if (!mem->va)
158 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000159
Jesse Brandeburg93bc73b2013-09-13 08:23:18 +0000160 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000161}
162
163/**
164 * i40e_free_virt_mem_d - OS specific memory free for shared code
165 * @hw: pointer to the HW structure
166 * @mem: ptr to mem struct to free
167 **/
168int i40e_free_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem)
169{
170 /* it's ok to kfree a NULL pointer */
171 kfree(mem->va);
172 mem->va = NULL;
173 mem->size = 0;
174
175 return 0;
176}
177
178/**
179 * i40e_get_lump - find a lump of free generic resource
180 * @pf: board private structure
181 * @pile: the pile of resource to search
182 * @needed: the number of items needed
183 * @id: an owner id to stick on the items assigned
184 *
185 * Returns the base item index of the lump, or negative for error
186 *
187 * The search_hint trick and lack of advanced fit-finding only work
188 * because we're highly likely to have all the same size lump requests.
189 * Linear search time and any fragmentation should be minimal.
190 **/
191static int i40e_get_lump(struct i40e_pf *pf, struct i40e_lump_tracking *pile,
192 u16 needed, u16 id)
193{
194 int ret = -ENOMEM;
Jesse Brandeburgddf434a2013-09-13 08:23:19 +0000195 int i, j;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000196
197 if (!pile || needed == 0 || id >= I40E_PILE_VALID_BIT) {
198 dev_info(&pf->pdev->dev,
199 "param err: pile=%p needed=%d id=0x%04x\n",
200 pile, needed, id);
201 return -EINVAL;
202 }
203
204 /* start the linear search with an imperfect hint */
205 i = pile->search_hint;
Jesse Brandeburgddf434a2013-09-13 08:23:19 +0000206 while (i < pile->num_entries) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000207 /* skip already allocated entries */
208 if (pile->list[i] & I40E_PILE_VALID_BIT) {
209 i++;
210 continue;
211 }
212
213 /* do we have enough in this lump? */
214 for (j = 0; (j < needed) && ((i+j) < pile->num_entries); j++) {
215 if (pile->list[i+j] & I40E_PILE_VALID_BIT)
216 break;
217 }
218
219 if (j == needed) {
220 /* there was enough, so assign it to the requestor */
221 for (j = 0; j < needed; j++)
222 pile->list[i+j] = id | I40E_PILE_VALID_BIT;
223 ret = i;
224 pile->search_hint = i + j;
Jesse Brandeburgddf434a2013-09-13 08:23:19 +0000225 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000226 }
Jesse Brandeburg6995b362015-08-28 17:55:54 -0400227
228 /* not enough, so skip over it and continue looking */
229 i += j;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000230 }
231
232 return ret;
233}
234
235/**
236 * i40e_put_lump - return a lump of generic resource
237 * @pile: the pile of resource to search
238 * @index: the base item index
239 * @id: the owner id of the items assigned
240 *
241 * Returns the count of items in the lump
242 **/
243static int i40e_put_lump(struct i40e_lump_tracking *pile, u16 index, u16 id)
244{
245 int valid_id = (id | I40E_PILE_VALID_BIT);
246 int count = 0;
247 int i;
248
249 if (!pile || index >= pile->num_entries)
250 return -EINVAL;
251
252 for (i = index;
253 i < pile->num_entries && pile->list[i] == valid_id;
254 i++) {
255 pile->list[i] = 0;
256 count++;
257 }
258
259 if (count && index < pile->search_hint)
260 pile->search_hint = index;
261
262 return count;
263}
264
265/**
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700266 * i40e_find_vsi_from_id - searches for the vsi with the given id
267 * @pf - the pf structure to search for the vsi
268 * @id - id of the vsi it is searching for
269 **/
270struct i40e_vsi *i40e_find_vsi_from_id(struct i40e_pf *pf, u16 id)
271{
272 int i;
273
274 for (i = 0; i < pf->num_alloc_vsi; i++)
275 if (pf->vsi[i] && (pf->vsi[i]->id == id))
276 return pf->vsi[i];
277
278 return NULL;
279}
280
281/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000282 * i40e_service_event_schedule - Schedule the service task to wake up
283 * @pf: board private structure
284 *
285 * If not already scheduled, this puts the task into the work queue
286 **/
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -0600287void i40e_service_event_schedule(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000288{
289 if (!test_bit(__I40E_DOWN, &pf->state) &&
Mitch Williams91089032016-11-21 13:03:51 -0800290 !test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
Jesse Brandeburg2803b162015-12-22 14:25:08 -0800291 queue_work(i40e_wq, &pf->service_task);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000292}
293
294/**
295 * i40e_tx_timeout - Respond to a Tx Hang
296 * @netdev: network interface device structure
297 *
298 * If any port has noticed a Tx timeout, it is likely that the whole
299 * device is munged, not just the one netdev port, so go for the full
300 * reset.
301 **/
Vasu Dev38e00432014-08-01 13:27:03 -0700302#ifdef I40E_FCOE
303void i40e_tx_timeout(struct net_device *netdev)
304#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000305static void i40e_tx_timeout(struct net_device *netdev)
Vasu Dev38e00432014-08-01 13:27:03 -0700306#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000307{
308 struct i40e_netdev_priv *np = netdev_priv(netdev);
309 struct i40e_vsi *vsi = np->vsi;
310 struct i40e_pf *pf = vsi->back;
Kiran Patilb03a8c12015-09-24 18:13:15 -0400311 struct i40e_ring *tx_ring = NULL;
312 unsigned int i, hung_queue = 0;
313 u32 head, val;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000314
315 pf->tx_timeout_count++;
316
Kiran Patilb03a8c12015-09-24 18:13:15 -0400317 /* find the stopped queue the same way the stack does */
318 for (i = 0; i < netdev->num_tx_queues; i++) {
319 struct netdev_queue *q;
320 unsigned long trans_start;
321
322 q = netdev_get_tx_queue(netdev, i);
Florian Westphal9b366272016-05-03 16:33:14 +0200323 trans_start = q->trans_start;
Kiran Patilb03a8c12015-09-24 18:13:15 -0400324 if (netif_xmit_stopped(q) &&
325 time_after(jiffies,
326 (trans_start + netdev->watchdog_timeo))) {
327 hung_queue = i;
328 break;
329 }
330 }
331
332 if (i == netdev->num_tx_queues) {
333 netdev_info(netdev, "tx_timeout: no netdev hung queue found\n");
334 } else {
335 /* now that we have an index, find the tx_ring struct */
336 for (i = 0; i < vsi->num_queue_pairs; i++) {
337 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc) {
338 if (hung_queue ==
339 vsi->tx_rings[i]->queue_index) {
340 tx_ring = vsi->tx_rings[i];
341 break;
342 }
343 }
344 }
345 }
346
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000347 if (time_after(jiffies, (pf->tx_timeout_last_recovery + HZ*20)))
Kiran Patilb03a8c12015-09-24 18:13:15 -0400348 pf->tx_timeout_recovery_level = 1; /* reset after some time */
349 else if (time_before(jiffies,
350 (pf->tx_timeout_last_recovery + netdev->watchdog_timeo)))
351 return; /* don't do any new action before the next timeout */
352
353 if (tx_ring) {
354 head = i40e_get_head(tx_ring);
355 /* Read interrupt register */
356 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
357 val = rd32(&pf->hw,
358 I40E_PFINT_DYN_CTLN(tx_ring->q_vector->v_idx +
359 tx_ring->vsi->base_vector - 1));
360 else
361 val = rd32(&pf->hw, I40E_PFINT_DYN_CTL0);
362
363 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",
364 vsi->seid, hung_queue, tx_ring->next_to_clean,
365 head, tx_ring->next_to_use,
366 readl(tx_ring->tail), val);
367 }
368
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000369 pf->tx_timeout_last_recovery = jiffies;
Kiran Patilb03a8c12015-09-24 18:13:15 -0400370 netdev_info(netdev, "tx_timeout recovery level %d, hung_queue %d\n",
371 pf->tx_timeout_recovery_level, hung_queue);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000372
373 switch (pf->tx_timeout_recovery_level) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000374 case 1:
375 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
376 break;
377 case 2:
378 set_bit(__I40E_CORE_RESET_REQUESTED, &pf->state);
379 break;
380 case 3:
381 set_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state);
382 break;
383 default:
384 netdev_err(netdev, "tx_timeout recovery unsuccessful\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000385 break;
386 }
Kiran Patilb03a8c12015-09-24 18:13:15 -0400387
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000388 i40e_service_event_schedule(pf);
389 pf->tx_timeout_recovery_level++;
390}
391
392/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000393 * i40e_get_vsi_stats_struct - Get System Network Statistics
394 * @vsi: the VSI we care about
395 *
396 * Returns the address of the device statistics structure.
397 * The statistics are actually updated from the service task.
398 **/
399struct rtnl_link_stats64 *i40e_get_vsi_stats_struct(struct i40e_vsi *vsi)
400{
401 return &vsi->net_stats;
402}
403
404/**
405 * i40e_get_netdev_stats_struct - Get statistics for netdev interface
406 * @netdev: network interface device structure
407 *
408 * Returns the address of the device statistics structure.
409 * The statistics are actually updated from the service task.
410 **/
stephen hemmingerbc1f4472017-01-06 19:12:52 -0800411#ifndef I40E_FCOE
412static
Vasu Dev38e00432014-08-01 13:27:03 -0700413#endif
stephen hemmingerbc1f4472017-01-06 19:12:52 -0800414void i40e_get_netdev_stats_struct(struct net_device *netdev,
415 struct rtnl_link_stats64 *stats)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000416{
417 struct i40e_netdev_priv *np = netdev_priv(netdev);
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +0000418 struct i40e_ring *tx_ring, *rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000419 struct i40e_vsi *vsi = np->vsi;
Alexander Duyck980e9b12013-09-28 06:01:03 +0000420 struct rtnl_link_stats64 *vsi_stats = i40e_get_vsi_stats_struct(vsi);
421 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000422
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +0000423 if (test_bit(__I40E_DOWN, &vsi->state))
stephen hemmingerbc1f4472017-01-06 19:12:52 -0800424 return;
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +0000425
Jesse Brandeburg3c325ce2013-12-14 03:26:45 -0800426 if (!vsi->tx_rings)
stephen hemmingerbc1f4472017-01-06 19:12:52 -0800427 return;
Jesse Brandeburg3c325ce2013-12-14 03:26:45 -0800428
Alexander Duyck980e9b12013-09-28 06:01:03 +0000429 rcu_read_lock();
430 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck980e9b12013-09-28 06:01:03 +0000431 u64 bytes, packets;
432 unsigned int start;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000433
Alexander Duyck980e9b12013-09-28 06:01:03 +0000434 tx_ring = ACCESS_ONCE(vsi->tx_rings[i]);
435 if (!tx_ring)
436 continue;
437
438 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700439 start = u64_stats_fetch_begin_irq(&tx_ring->syncp);
Alexander Duyck980e9b12013-09-28 06:01:03 +0000440 packets = tx_ring->stats.packets;
441 bytes = tx_ring->stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700442 } while (u64_stats_fetch_retry_irq(&tx_ring->syncp, start));
Alexander Duyck980e9b12013-09-28 06:01:03 +0000443
444 stats->tx_packets += packets;
445 stats->tx_bytes += bytes;
446 rx_ring = &tx_ring[1];
447
448 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700449 start = u64_stats_fetch_begin_irq(&rx_ring->syncp);
Alexander Duyck980e9b12013-09-28 06:01:03 +0000450 packets = rx_ring->stats.packets;
451 bytes = rx_ring->stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700452 } while (u64_stats_fetch_retry_irq(&rx_ring->syncp, start));
Alexander Duyck980e9b12013-09-28 06:01:03 +0000453
454 stats->rx_packets += packets;
455 stats->rx_bytes += bytes;
456 }
457 rcu_read_unlock();
458
Akeem G Abodunrina5282f42014-05-10 04:49:03 +0000459 /* following stats updated by i40e_watchdog_subtask() */
Alexander Duyck980e9b12013-09-28 06:01:03 +0000460 stats->multicast = vsi_stats->multicast;
461 stats->tx_errors = vsi_stats->tx_errors;
462 stats->tx_dropped = vsi_stats->tx_dropped;
463 stats->rx_errors = vsi_stats->rx_errors;
Jesse Brandeburgd8201e22015-07-23 16:54:35 -0400464 stats->rx_dropped = vsi_stats->rx_dropped;
Alexander Duyck980e9b12013-09-28 06:01:03 +0000465 stats->rx_crc_errors = vsi_stats->rx_crc_errors;
466 stats->rx_length_errors = vsi_stats->rx_length_errors;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000467}
468
469/**
470 * i40e_vsi_reset_stats - Resets all stats of the given vsi
471 * @vsi: the VSI to have its stats reset
472 **/
473void i40e_vsi_reset_stats(struct i40e_vsi *vsi)
474{
475 struct rtnl_link_stats64 *ns;
476 int i;
477
478 if (!vsi)
479 return;
480
481 ns = i40e_get_vsi_stats_struct(vsi);
482 memset(ns, 0, sizeof(*ns));
483 memset(&vsi->net_stats_offsets, 0, sizeof(vsi->net_stats_offsets));
484 memset(&vsi->eth_stats, 0, sizeof(vsi->eth_stats));
485 memset(&vsi->eth_stats_offsets, 0, sizeof(vsi->eth_stats_offsets));
Greg Rose8e9dca52013-12-18 13:45:53 +0000486 if (vsi->rx_rings && vsi->rx_rings[0]) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000487 for (i = 0; i < vsi->num_queue_pairs; i++) {
Jesse Brandeburg6995b362015-08-28 17:55:54 -0400488 memset(&vsi->rx_rings[i]->stats, 0,
Alexander Duyck9f65e152013-09-28 06:00:58 +0000489 sizeof(vsi->rx_rings[i]->stats));
Jesse Brandeburg6995b362015-08-28 17:55:54 -0400490 memset(&vsi->rx_rings[i]->rx_stats, 0,
Alexander Duyck9f65e152013-09-28 06:00:58 +0000491 sizeof(vsi->rx_rings[i]->rx_stats));
Jesse Brandeburg6995b362015-08-28 17:55:54 -0400492 memset(&vsi->tx_rings[i]->stats, 0,
Alexander Duyck9f65e152013-09-28 06:00:58 +0000493 sizeof(vsi->tx_rings[i]->stats));
494 memset(&vsi->tx_rings[i]->tx_stats, 0,
495 sizeof(vsi->tx_rings[i]->tx_stats));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000496 }
Greg Rose8e9dca52013-12-18 13:45:53 +0000497 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000498 vsi->stat_offsets_loaded = false;
499}
500
501/**
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000502 * i40e_pf_reset_stats - Reset all of the stats for the given PF
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000503 * @pf: the PF to be reset
504 **/
505void i40e_pf_reset_stats(struct i40e_pf *pf)
506{
Shannon Nelsone91fdf72014-06-03 23:50:18 +0000507 int i;
508
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000509 memset(&pf->stats, 0, sizeof(pf->stats));
510 memset(&pf->stats_offsets, 0, sizeof(pf->stats_offsets));
511 pf->stat_offsets_loaded = false;
Shannon Nelsone91fdf72014-06-03 23:50:18 +0000512
513 for (i = 0; i < I40E_MAX_VEB; i++) {
514 if (pf->veb[i]) {
515 memset(&pf->veb[i]->stats, 0,
516 sizeof(pf->veb[i]->stats));
517 memset(&pf->veb[i]->stats_offsets, 0,
518 sizeof(pf->veb[i]->stats_offsets));
519 pf->veb[i]->stat_offsets_loaded = false;
520 }
521 }
Catherine Sullivan42bce042016-07-27 12:02:32 -0700522 pf->hw_csum_rx_error = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000523}
524
525/**
526 * i40e_stat_update48 - read and update a 48 bit stat from the chip
527 * @hw: ptr to the hardware info
528 * @hireg: the high 32 bit reg to read
529 * @loreg: the low 32 bit reg to read
530 * @offset_loaded: has the initial offset been loaded yet
531 * @offset: ptr to current offset value
532 * @stat: ptr to the stat
533 *
534 * Since the device stats are not reset at PFReset, they likely will not
535 * be zeroed when the driver starts. We'll save the first values read
536 * and use them as offsets to be subtracted from the raw values in order
537 * to report stats that count from zero. In the process, we also manage
538 * the potential roll-over.
539 **/
540static void i40e_stat_update48(struct i40e_hw *hw, u32 hireg, u32 loreg,
541 bool offset_loaded, u64 *offset, u64 *stat)
542{
543 u64 new_data;
544
Shannon Nelsonab600852014-01-17 15:36:39 -0800545 if (hw->device_id == I40E_DEV_ID_QEMU) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000546 new_data = rd32(hw, loreg);
547 new_data |= ((u64)(rd32(hw, hireg) & 0xFFFF)) << 32;
548 } else {
549 new_data = rd64(hw, loreg);
550 }
551 if (!offset_loaded)
552 *offset = new_data;
553 if (likely(new_data >= *offset))
554 *stat = new_data - *offset;
555 else
Jesse Brandeburg41a1d042015-06-04 16:24:02 -0400556 *stat = (new_data + BIT_ULL(48)) - *offset;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000557 *stat &= 0xFFFFFFFFFFFFULL;
558}
559
560/**
561 * i40e_stat_update32 - read and update a 32 bit stat from the chip
562 * @hw: ptr to the hardware info
563 * @reg: the hw reg to read
564 * @offset_loaded: has the initial offset been loaded yet
565 * @offset: ptr to current offset value
566 * @stat: ptr to the stat
567 **/
568static void i40e_stat_update32(struct i40e_hw *hw, u32 reg,
569 bool offset_loaded, u64 *offset, u64 *stat)
570{
571 u32 new_data;
572
573 new_data = rd32(hw, reg);
574 if (!offset_loaded)
575 *offset = new_data;
576 if (likely(new_data >= *offset))
577 *stat = (u32)(new_data - *offset);
578 else
Jesse Brandeburg41a1d042015-06-04 16:24:02 -0400579 *stat = (u32)((new_data + BIT_ULL(32)) - *offset);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000580}
581
582/**
583 * i40e_update_eth_stats - Update VSI-specific ethernet statistics counters.
584 * @vsi: the VSI to be updated
585 **/
586void i40e_update_eth_stats(struct i40e_vsi *vsi)
587{
588 int stat_idx = le16_to_cpu(vsi->info.stat_counter_idx);
589 struct i40e_pf *pf = vsi->back;
590 struct i40e_hw *hw = &pf->hw;
591 struct i40e_eth_stats *oes;
592 struct i40e_eth_stats *es; /* device's eth stats */
593
594 es = &vsi->eth_stats;
595 oes = &vsi->eth_stats_offsets;
596
597 /* Gather up the stats that the hw collects */
598 i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
599 vsi->stat_offsets_loaded,
600 &oes->tx_errors, &es->tx_errors);
601 i40e_stat_update32(hw, I40E_GLV_RDPC(stat_idx),
602 vsi->stat_offsets_loaded,
603 &oes->rx_discards, &es->rx_discards);
Shannon Nelson41a9e552014-04-23 04:50:20 +0000604 i40e_stat_update32(hw, I40E_GLV_RUPP(stat_idx),
605 vsi->stat_offsets_loaded,
606 &oes->rx_unknown_protocol, &es->rx_unknown_protocol);
607 i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
608 vsi->stat_offsets_loaded,
609 &oes->tx_errors, &es->tx_errors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000610
611 i40e_stat_update48(hw, I40E_GLV_GORCH(stat_idx),
612 I40E_GLV_GORCL(stat_idx),
613 vsi->stat_offsets_loaded,
614 &oes->rx_bytes, &es->rx_bytes);
615 i40e_stat_update48(hw, I40E_GLV_UPRCH(stat_idx),
616 I40E_GLV_UPRCL(stat_idx),
617 vsi->stat_offsets_loaded,
618 &oes->rx_unicast, &es->rx_unicast);
619 i40e_stat_update48(hw, I40E_GLV_MPRCH(stat_idx),
620 I40E_GLV_MPRCL(stat_idx),
621 vsi->stat_offsets_loaded,
622 &oes->rx_multicast, &es->rx_multicast);
623 i40e_stat_update48(hw, I40E_GLV_BPRCH(stat_idx),
624 I40E_GLV_BPRCL(stat_idx),
625 vsi->stat_offsets_loaded,
626 &oes->rx_broadcast, &es->rx_broadcast);
627
628 i40e_stat_update48(hw, I40E_GLV_GOTCH(stat_idx),
629 I40E_GLV_GOTCL(stat_idx),
630 vsi->stat_offsets_loaded,
631 &oes->tx_bytes, &es->tx_bytes);
632 i40e_stat_update48(hw, I40E_GLV_UPTCH(stat_idx),
633 I40E_GLV_UPTCL(stat_idx),
634 vsi->stat_offsets_loaded,
635 &oes->tx_unicast, &es->tx_unicast);
636 i40e_stat_update48(hw, I40E_GLV_MPTCH(stat_idx),
637 I40E_GLV_MPTCL(stat_idx),
638 vsi->stat_offsets_loaded,
639 &oes->tx_multicast, &es->tx_multicast);
640 i40e_stat_update48(hw, I40E_GLV_BPTCH(stat_idx),
641 I40E_GLV_BPTCL(stat_idx),
642 vsi->stat_offsets_loaded,
643 &oes->tx_broadcast, &es->tx_broadcast);
644 vsi->stat_offsets_loaded = true;
645}
646
647/**
648 * i40e_update_veb_stats - Update Switch component statistics
649 * @veb: the VEB being updated
650 **/
651static void i40e_update_veb_stats(struct i40e_veb *veb)
652{
653 struct i40e_pf *pf = veb->pf;
654 struct i40e_hw *hw = &pf->hw;
655 struct i40e_eth_stats *oes;
656 struct i40e_eth_stats *es; /* device's eth stats */
Neerav Parikhfe860af2015-07-10 19:36:02 -0400657 struct i40e_veb_tc_stats *veb_oes;
658 struct i40e_veb_tc_stats *veb_es;
659 int i, idx = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000660
661 idx = veb->stats_idx;
662 es = &veb->stats;
663 oes = &veb->stats_offsets;
Neerav Parikhfe860af2015-07-10 19:36:02 -0400664 veb_es = &veb->tc_stats;
665 veb_oes = &veb->tc_stats_offsets;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000666
667 /* Gather up the stats that the hw collects */
668 i40e_stat_update32(hw, I40E_GLSW_TDPC(idx),
669 veb->stat_offsets_loaded,
670 &oes->tx_discards, &es->tx_discards);
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +0000671 if (hw->revision_id > 0)
672 i40e_stat_update32(hw, I40E_GLSW_RUPP(idx),
673 veb->stat_offsets_loaded,
674 &oes->rx_unknown_protocol,
675 &es->rx_unknown_protocol);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000676 i40e_stat_update48(hw, I40E_GLSW_GORCH(idx), I40E_GLSW_GORCL(idx),
677 veb->stat_offsets_loaded,
678 &oes->rx_bytes, &es->rx_bytes);
679 i40e_stat_update48(hw, I40E_GLSW_UPRCH(idx), I40E_GLSW_UPRCL(idx),
680 veb->stat_offsets_loaded,
681 &oes->rx_unicast, &es->rx_unicast);
682 i40e_stat_update48(hw, I40E_GLSW_MPRCH(idx), I40E_GLSW_MPRCL(idx),
683 veb->stat_offsets_loaded,
684 &oes->rx_multicast, &es->rx_multicast);
685 i40e_stat_update48(hw, I40E_GLSW_BPRCH(idx), I40E_GLSW_BPRCL(idx),
686 veb->stat_offsets_loaded,
687 &oes->rx_broadcast, &es->rx_broadcast);
688
689 i40e_stat_update48(hw, I40E_GLSW_GOTCH(idx), I40E_GLSW_GOTCL(idx),
690 veb->stat_offsets_loaded,
691 &oes->tx_bytes, &es->tx_bytes);
692 i40e_stat_update48(hw, I40E_GLSW_UPTCH(idx), I40E_GLSW_UPTCL(idx),
693 veb->stat_offsets_loaded,
694 &oes->tx_unicast, &es->tx_unicast);
695 i40e_stat_update48(hw, I40E_GLSW_MPTCH(idx), I40E_GLSW_MPTCL(idx),
696 veb->stat_offsets_loaded,
697 &oes->tx_multicast, &es->tx_multicast);
698 i40e_stat_update48(hw, I40E_GLSW_BPTCH(idx), I40E_GLSW_BPTCL(idx),
699 veb->stat_offsets_loaded,
700 &oes->tx_broadcast, &es->tx_broadcast);
Neerav Parikhfe860af2015-07-10 19:36:02 -0400701 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
702 i40e_stat_update48(hw, I40E_GLVEBTC_RPCH(i, idx),
703 I40E_GLVEBTC_RPCL(i, idx),
704 veb->stat_offsets_loaded,
705 &veb_oes->tc_rx_packets[i],
706 &veb_es->tc_rx_packets[i]);
707 i40e_stat_update48(hw, I40E_GLVEBTC_RBCH(i, idx),
708 I40E_GLVEBTC_RBCL(i, idx),
709 veb->stat_offsets_loaded,
710 &veb_oes->tc_rx_bytes[i],
711 &veb_es->tc_rx_bytes[i]);
712 i40e_stat_update48(hw, I40E_GLVEBTC_TPCH(i, idx),
713 I40E_GLVEBTC_TPCL(i, idx),
714 veb->stat_offsets_loaded,
715 &veb_oes->tc_tx_packets[i],
716 &veb_es->tc_tx_packets[i]);
717 i40e_stat_update48(hw, I40E_GLVEBTC_TBCH(i, idx),
718 I40E_GLVEBTC_TBCL(i, idx),
719 veb->stat_offsets_loaded,
720 &veb_oes->tc_tx_bytes[i],
721 &veb_es->tc_tx_bytes[i]);
722 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000723 veb->stat_offsets_loaded = true;
724}
725
Vasu Dev38e00432014-08-01 13:27:03 -0700726#ifdef I40E_FCOE
727/**
728 * i40e_update_fcoe_stats - Update FCoE-specific ethernet statistics counters.
729 * @vsi: the VSI that is capable of doing FCoE
730 **/
731static void i40e_update_fcoe_stats(struct i40e_vsi *vsi)
732{
733 struct i40e_pf *pf = vsi->back;
734 struct i40e_hw *hw = &pf->hw;
735 struct i40e_fcoe_stats *ofs;
736 struct i40e_fcoe_stats *fs; /* device's eth stats */
737 int idx;
738
739 if (vsi->type != I40E_VSI_FCOE)
740 return;
741
Kiran Patil4147e2c2016-01-15 14:33:14 -0800742 idx = hw->pf_id + I40E_FCOE_PF_STAT_OFFSET;
Vasu Dev38e00432014-08-01 13:27:03 -0700743 fs = &vsi->fcoe_stats;
744 ofs = &vsi->fcoe_stats_offsets;
745
746 i40e_stat_update32(hw, I40E_GL_FCOEPRC(idx),
747 vsi->fcoe_stat_offsets_loaded,
748 &ofs->rx_fcoe_packets, &fs->rx_fcoe_packets);
749 i40e_stat_update48(hw, I40E_GL_FCOEDWRCH(idx), I40E_GL_FCOEDWRCL(idx),
750 vsi->fcoe_stat_offsets_loaded,
751 &ofs->rx_fcoe_dwords, &fs->rx_fcoe_dwords);
752 i40e_stat_update32(hw, I40E_GL_FCOERPDC(idx),
753 vsi->fcoe_stat_offsets_loaded,
754 &ofs->rx_fcoe_dropped, &fs->rx_fcoe_dropped);
755 i40e_stat_update32(hw, I40E_GL_FCOEPTC(idx),
756 vsi->fcoe_stat_offsets_loaded,
757 &ofs->tx_fcoe_packets, &fs->tx_fcoe_packets);
758 i40e_stat_update48(hw, I40E_GL_FCOEDWTCH(idx), I40E_GL_FCOEDWTCL(idx),
759 vsi->fcoe_stat_offsets_loaded,
760 &ofs->tx_fcoe_dwords, &fs->tx_fcoe_dwords);
761 i40e_stat_update32(hw, I40E_GL_FCOECRC(idx),
762 vsi->fcoe_stat_offsets_loaded,
763 &ofs->fcoe_bad_fccrc, &fs->fcoe_bad_fccrc);
764 i40e_stat_update32(hw, I40E_GL_FCOELAST(idx),
765 vsi->fcoe_stat_offsets_loaded,
766 &ofs->fcoe_last_error, &fs->fcoe_last_error);
767 i40e_stat_update32(hw, I40E_GL_FCOEDDPC(idx),
768 vsi->fcoe_stat_offsets_loaded,
769 &ofs->fcoe_ddp_count, &fs->fcoe_ddp_count);
770
771 vsi->fcoe_stat_offsets_loaded = true;
772}
773
774#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000775/**
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000776 * i40e_update_vsi_stats - Update the vsi statistics counters.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000777 * @vsi: the VSI to be updated
778 *
779 * There are a few instances where we store the same stat in a
780 * couple of different structs. This is partly because we have
781 * the netdev stats that need to be filled out, which is slightly
782 * different from the "eth_stats" defined by the chip and used in
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000783 * VF communications. We sort it out here.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000784 **/
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000785static void i40e_update_vsi_stats(struct i40e_vsi *vsi)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000786{
787 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000788 struct rtnl_link_stats64 *ons;
789 struct rtnl_link_stats64 *ns; /* netdev stats */
790 struct i40e_eth_stats *oes;
791 struct i40e_eth_stats *es; /* device's eth stats */
792 u32 tx_restart, tx_busy;
Anjali Singhai Jaindd353102016-01-15 14:33:12 -0800793 u64 tx_lost_interrupt;
Akeem G Abodunrinbf00b372014-10-17 03:14:39 +0000794 struct i40e_ring *p;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000795 u32 rx_page, rx_buf;
Akeem G Abodunrinbf00b372014-10-17 03:14:39 +0000796 u64 bytes, packets;
797 unsigned int start;
Anjali Singhai Jain2fc3d712015-08-27 11:42:29 -0400798 u64 tx_linearize;
Anjali Singhai Jain164c9f52015-10-21 19:47:08 -0400799 u64 tx_force_wb;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000800 u64 rx_p, rx_b;
801 u64 tx_p, tx_b;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000802 u16 q;
803
804 if (test_bit(__I40E_DOWN, &vsi->state) ||
805 test_bit(__I40E_CONFIG_BUSY, &pf->state))
806 return;
807
808 ns = i40e_get_vsi_stats_struct(vsi);
809 ons = &vsi->net_stats_offsets;
810 es = &vsi->eth_stats;
811 oes = &vsi->eth_stats_offsets;
812
813 /* Gather up the netdev and vsi stats that the driver collects
814 * on the fly during packet processing
815 */
816 rx_b = rx_p = 0;
817 tx_b = tx_p = 0;
Anjali Singhai Jain164c9f52015-10-21 19:47:08 -0400818 tx_restart = tx_busy = tx_linearize = tx_force_wb = 0;
Anjali Singhai Jaindd353102016-01-15 14:33:12 -0800819 tx_lost_interrupt = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000820 rx_page = 0;
821 rx_buf = 0;
Alexander Duyck980e9b12013-09-28 06:01:03 +0000822 rcu_read_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000823 for (q = 0; q < vsi->num_queue_pairs; q++) {
Alexander Duyck980e9b12013-09-28 06:01:03 +0000824 /* locate Tx ring */
825 p = ACCESS_ONCE(vsi->tx_rings[q]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000826
Alexander Duyck980e9b12013-09-28 06:01:03 +0000827 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700828 start = u64_stats_fetch_begin_irq(&p->syncp);
Alexander Duyck980e9b12013-09-28 06:01:03 +0000829 packets = p->stats.packets;
830 bytes = p->stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700831 } while (u64_stats_fetch_retry_irq(&p->syncp, start));
Alexander Duyck980e9b12013-09-28 06:01:03 +0000832 tx_b += bytes;
833 tx_p += packets;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000834 tx_restart += p->tx_stats.restart_queue;
835 tx_busy += p->tx_stats.tx_busy;
Anjali Singhai Jain2fc3d712015-08-27 11:42:29 -0400836 tx_linearize += p->tx_stats.tx_linearize;
Anjali Singhai Jain164c9f52015-10-21 19:47:08 -0400837 tx_force_wb += p->tx_stats.tx_force_wb;
Anjali Singhai Jaindd353102016-01-15 14:33:12 -0800838 tx_lost_interrupt += p->tx_stats.tx_lost_interrupt;
Alexander Duyck980e9b12013-09-28 06:01:03 +0000839
840 /* Rx queue is part of the same block as Tx queue */
841 p = &p[1];
842 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700843 start = u64_stats_fetch_begin_irq(&p->syncp);
Alexander Duyck980e9b12013-09-28 06:01:03 +0000844 packets = p->stats.packets;
845 bytes = p->stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700846 } while (u64_stats_fetch_retry_irq(&p->syncp, start));
Alexander Duyck980e9b12013-09-28 06:01:03 +0000847 rx_b += bytes;
848 rx_p += packets;
Mitch Williams420136c2013-12-18 13:45:59 +0000849 rx_buf += p->rx_stats.alloc_buff_failed;
850 rx_page += p->rx_stats.alloc_page_failed;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000851 }
Alexander Duyck980e9b12013-09-28 06:01:03 +0000852 rcu_read_unlock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000853 vsi->tx_restart = tx_restart;
854 vsi->tx_busy = tx_busy;
Anjali Singhai Jain2fc3d712015-08-27 11:42:29 -0400855 vsi->tx_linearize = tx_linearize;
Anjali Singhai Jain164c9f52015-10-21 19:47:08 -0400856 vsi->tx_force_wb = tx_force_wb;
Anjali Singhai Jaindd353102016-01-15 14:33:12 -0800857 vsi->tx_lost_interrupt = tx_lost_interrupt;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000858 vsi->rx_page_failed = rx_page;
859 vsi->rx_buf_failed = rx_buf;
860
861 ns->rx_packets = rx_p;
862 ns->rx_bytes = rx_b;
863 ns->tx_packets = tx_p;
864 ns->tx_bytes = tx_b;
865
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000866 /* update netdev stats from eth stats */
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000867 i40e_update_eth_stats(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000868 ons->tx_errors = oes->tx_errors;
869 ns->tx_errors = es->tx_errors;
870 ons->multicast = oes->rx_multicast;
871 ns->multicast = es->rx_multicast;
Shannon Nelson41a9e552014-04-23 04:50:20 +0000872 ons->rx_dropped = oes->rx_discards;
873 ns->rx_dropped = es->rx_discards;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000874 ons->tx_dropped = oes->tx_discards;
875 ns->tx_dropped = es->tx_discards;
876
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000877 /* pull in a couple PF stats if this is the main vsi */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000878 if (vsi == pf->vsi[pf->lan_vsi]) {
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000879 ns->rx_crc_errors = pf->stats.crc_errors;
880 ns->rx_errors = pf->stats.crc_errors + pf->stats.illegal_bytes;
881 ns->rx_length_errors = pf->stats.rx_length_errors;
882 }
883}
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000884
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000885/**
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000886 * i40e_update_pf_stats - Update the PF statistics counters.
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000887 * @pf: the PF to be updated
888 **/
889static void i40e_update_pf_stats(struct i40e_pf *pf)
890{
891 struct i40e_hw_port_stats *osd = &pf->stats_offsets;
892 struct i40e_hw_port_stats *nsd = &pf->stats;
893 struct i40e_hw *hw = &pf->hw;
894 u32 val;
895 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000896
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000897 i40e_stat_update48(hw, I40E_GLPRT_GORCH(hw->port),
898 I40E_GLPRT_GORCL(hw->port),
899 pf->stat_offsets_loaded,
900 &osd->eth.rx_bytes, &nsd->eth.rx_bytes);
901 i40e_stat_update48(hw, I40E_GLPRT_GOTCH(hw->port),
902 I40E_GLPRT_GOTCL(hw->port),
903 pf->stat_offsets_loaded,
904 &osd->eth.tx_bytes, &nsd->eth.tx_bytes);
905 i40e_stat_update32(hw, I40E_GLPRT_RDPC(hw->port),
906 pf->stat_offsets_loaded,
907 &osd->eth.rx_discards,
908 &nsd->eth.rx_discards);
Shannon Nelson532d2832014-04-23 04:50:09 +0000909 i40e_stat_update48(hw, I40E_GLPRT_UPRCH(hw->port),
910 I40E_GLPRT_UPRCL(hw->port),
911 pf->stat_offsets_loaded,
912 &osd->eth.rx_unicast,
913 &nsd->eth.rx_unicast);
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000914 i40e_stat_update48(hw, I40E_GLPRT_MPRCH(hw->port),
915 I40E_GLPRT_MPRCL(hw->port),
916 pf->stat_offsets_loaded,
917 &osd->eth.rx_multicast,
918 &nsd->eth.rx_multicast);
Shannon Nelson532d2832014-04-23 04:50:09 +0000919 i40e_stat_update48(hw, I40E_GLPRT_BPRCH(hw->port),
920 I40E_GLPRT_BPRCL(hw->port),
921 pf->stat_offsets_loaded,
922 &osd->eth.rx_broadcast,
923 &nsd->eth.rx_broadcast);
924 i40e_stat_update48(hw, I40E_GLPRT_UPTCH(hw->port),
925 I40E_GLPRT_UPTCL(hw->port),
926 pf->stat_offsets_loaded,
927 &osd->eth.tx_unicast,
928 &nsd->eth.tx_unicast);
929 i40e_stat_update48(hw, I40E_GLPRT_MPTCH(hw->port),
930 I40E_GLPRT_MPTCL(hw->port),
931 pf->stat_offsets_loaded,
932 &osd->eth.tx_multicast,
933 &nsd->eth.tx_multicast);
934 i40e_stat_update48(hw, I40E_GLPRT_BPTCH(hw->port),
935 I40E_GLPRT_BPTCL(hw->port),
936 pf->stat_offsets_loaded,
937 &osd->eth.tx_broadcast,
938 &nsd->eth.tx_broadcast);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000939
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000940 i40e_stat_update32(hw, I40E_GLPRT_TDOLD(hw->port),
941 pf->stat_offsets_loaded,
942 &osd->tx_dropped_link_down,
943 &nsd->tx_dropped_link_down);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000944
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000945 i40e_stat_update32(hw, I40E_GLPRT_CRCERRS(hw->port),
946 pf->stat_offsets_loaded,
947 &osd->crc_errors, &nsd->crc_errors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000948
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000949 i40e_stat_update32(hw, I40E_GLPRT_ILLERRC(hw->port),
950 pf->stat_offsets_loaded,
951 &osd->illegal_bytes, &nsd->illegal_bytes);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000952
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000953 i40e_stat_update32(hw, I40E_GLPRT_MLFC(hw->port),
954 pf->stat_offsets_loaded,
955 &osd->mac_local_faults,
956 &nsd->mac_local_faults);
957 i40e_stat_update32(hw, I40E_GLPRT_MRFC(hw->port),
958 pf->stat_offsets_loaded,
959 &osd->mac_remote_faults,
960 &nsd->mac_remote_faults);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000961
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000962 i40e_stat_update32(hw, I40E_GLPRT_RLEC(hw->port),
963 pf->stat_offsets_loaded,
964 &osd->rx_length_errors,
965 &nsd->rx_length_errors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000966
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000967 i40e_stat_update32(hw, I40E_GLPRT_LXONRXC(hw->port),
968 pf->stat_offsets_loaded,
969 &osd->link_xon_rx, &nsd->link_xon_rx);
970 i40e_stat_update32(hw, I40E_GLPRT_LXONTXC(hw->port),
971 pf->stat_offsets_loaded,
972 &osd->link_xon_tx, &nsd->link_xon_tx);
Neerav Parikh95db2392015-11-06 15:26:09 -0800973 i40e_stat_update32(hw, I40E_GLPRT_LXOFFRXC(hw->port),
974 pf->stat_offsets_loaded,
975 &osd->link_xoff_rx, &nsd->link_xoff_rx);
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000976 i40e_stat_update32(hw, I40E_GLPRT_LXOFFTXC(hw->port),
977 pf->stat_offsets_loaded,
978 &osd->link_xoff_tx, &nsd->link_xoff_tx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000979
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000980 for (i = 0; i < 8; i++) {
Neerav Parikh95db2392015-11-06 15:26:09 -0800981 i40e_stat_update32(hw, I40E_GLPRT_PXOFFRXC(hw->port, i),
982 pf->stat_offsets_loaded,
983 &osd->priority_xoff_rx[i],
984 &nsd->priority_xoff_rx[i]);
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000985 i40e_stat_update32(hw, I40E_GLPRT_PXONRXC(hw->port, i),
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000986 pf->stat_offsets_loaded,
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000987 &osd->priority_xon_rx[i],
988 &nsd->priority_xon_rx[i]);
989 i40e_stat_update32(hw, I40E_GLPRT_PXONTXC(hw->port, i),
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000990 pf->stat_offsets_loaded,
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000991 &osd->priority_xon_tx[i],
992 &nsd->priority_xon_tx[i]);
993 i40e_stat_update32(hw, I40E_GLPRT_PXOFFTXC(hw->port, i),
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000994 pf->stat_offsets_loaded,
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000995 &osd->priority_xoff_tx[i],
996 &nsd->priority_xoff_tx[i]);
997 i40e_stat_update32(hw,
998 I40E_GLPRT_RXON2OFFCNT(hw->port, i),
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000999 pf->stat_offsets_loaded,
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001000 &osd->priority_xon_2_xoff[i],
1001 &nsd->priority_xon_2_xoff[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001002 }
1003
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001004 i40e_stat_update48(hw, I40E_GLPRT_PRC64H(hw->port),
1005 I40E_GLPRT_PRC64L(hw->port),
1006 pf->stat_offsets_loaded,
1007 &osd->rx_size_64, &nsd->rx_size_64);
1008 i40e_stat_update48(hw, I40E_GLPRT_PRC127H(hw->port),
1009 I40E_GLPRT_PRC127L(hw->port),
1010 pf->stat_offsets_loaded,
1011 &osd->rx_size_127, &nsd->rx_size_127);
1012 i40e_stat_update48(hw, I40E_GLPRT_PRC255H(hw->port),
1013 I40E_GLPRT_PRC255L(hw->port),
1014 pf->stat_offsets_loaded,
1015 &osd->rx_size_255, &nsd->rx_size_255);
1016 i40e_stat_update48(hw, I40E_GLPRT_PRC511H(hw->port),
1017 I40E_GLPRT_PRC511L(hw->port),
1018 pf->stat_offsets_loaded,
1019 &osd->rx_size_511, &nsd->rx_size_511);
1020 i40e_stat_update48(hw, I40E_GLPRT_PRC1023H(hw->port),
1021 I40E_GLPRT_PRC1023L(hw->port),
1022 pf->stat_offsets_loaded,
1023 &osd->rx_size_1023, &nsd->rx_size_1023);
1024 i40e_stat_update48(hw, I40E_GLPRT_PRC1522H(hw->port),
1025 I40E_GLPRT_PRC1522L(hw->port),
1026 pf->stat_offsets_loaded,
1027 &osd->rx_size_1522, &nsd->rx_size_1522);
1028 i40e_stat_update48(hw, I40E_GLPRT_PRC9522H(hw->port),
1029 I40E_GLPRT_PRC9522L(hw->port),
1030 pf->stat_offsets_loaded,
1031 &osd->rx_size_big, &nsd->rx_size_big);
1032
1033 i40e_stat_update48(hw, I40E_GLPRT_PTC64H(hw->port),
1034 I40E_GLPRT_PTC64L(hw->port),
1035 pf->stat_offsets_loaded,
1036 &osd->tx_size_64, &nsd->tx_size_64);
1037 i40e_stat_update48(hw, I40E_GLPRT_PTC127H(hw->port),
1038 I40E_GLPRT_PTC127L(hw->port),
1039 pf->stat_offsets_loaded,
1040 &osd->tx_size_127, &nsd->tx_size_127);
1041 i40e_stat_update48(hw, I40E_GLPRT_PTC255H(hw->port),
1042 I40E_GLPRT_PTC255L(hw->port),
1043 pf->stat_offsets_loaded,
1044 &osd->tx_size_255, &nsd->tx_size_255);
1045 i40e_stat_update48(hw, I40E_GLPRT_PTC511H(hw->port),
1046 I40E_GLPRT_PTC511L(hw->port),
1047 pf->stat_offsets_loaded,
1048 &osd->tx_size_511, &nsd->tx_size_511);
1049 i40e_stat_update48(hw, I40E_GLPRT_PTC1023H(hw->port),
1050 I40E_GLPRT_PTC1023L(hw->port),
1051 pf->stat_offsets_loaded,
1052 &osd->tx_size_1023, &nsd->tx_size_1023);
1053 i40e_stat_update48(hw, I40E_GLPRT_PTC1522H(hw->port),
1054 I40E_GLPRT_PTC1522L(hw->port),
1055 pf->stat_offsets_loaded,
1056 &osd->tx_size_1522, &nsd->tx_size_1522);
1057 i40e_stat_update48(hw, I40E_GLPRT_PTC9522H(hw->port),
1058 I40E_GLPRT_PTC9522L(hw->port),
1059 pf->stat_offsets_loaded,
1060 &osd->tx_size_big, &nsd->tx_size_big);
1061
1062 i40e_stat_update32(hw, I40E_GLPRT_RUC(hw->port),
1063 pf->stat_offsets_loaded,
1064 &osd->rx_undersize, &nsd->rx_undersize);
1065 i40e_stat_update32(hw, I40E_GLPRT_RFC(hw->port),
1066 pf->stat_offsets_loaded,
1067 &osd->rx_fragments, &nsd->rx_fragments);
1068 i40e_stat_update32(hw, I40E_GLPRT_ROC(hw->port),
1069 pf->stat_offsets_loaded,
1070 &osd->rx_oversize, &nsd->rx_oversize);
1071 i40e_stat_update32(hw, I40E_GLPRT_RJC(hw->port),
1072 pf->stat_offsets_loaded,
1073 &osd->rx_jabber, &nsd->rx_jabber);
1074
Anjali Singhai Jain433c47d2014-05-22 06:32:17 +00001075 /* FDIR stats */
Anjali Singhai Jain0bf4b1b2015-04-16 20:06:02 -04001076 i40e_stat_update32(hw,
1077 I40E_GLQF_PCNT(I40E_FD_ATR_STAT_IDX(pf->hw.pf_id)),
Anjali Singhai Jain433c47d2014-05-22 06:32:17 +00001078 pf->stat_offsets_loaded,
1079 &osd->fd_atr_match, &nsd->fd_atr_match);
Anjali Singhai Jain0bf4b1b2015-04-16 20:06:02 -04001080 i40e_stat_update32(hw,
1081 I40E_GLQF_PCNT(I40E_FD_SB_STAT_IDX(pf->hw.pf_id)),
Anjali Singhai Jain433c47d2014-05-22 06:32:17 +00001082 pf->stat_offsets_loaded,
1083 &osd->fd_sb_match, &nsd->fd_sb_match);
Anjali Singhai Jain60ccd452015-04-16 20:06:01 -04001084 i40e_stat_update32(hw,
1085 I40E_GLQF_PCNT(I40E_FD_ATR_TUNNEL_STAT_IDX(pf->hw.pf_id)),
1086 pf->stat_offsets_loaded,
1087 &osd->fd_atr_tunnel_match, &nsd->fd_atr_tunnel_match);
Anjali Singhai Jain433c47d2014-05-22 06:32:17 +00001088
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001089 val = rd32(hw, I40E_PRTPM_EEE_STAT);
1090 nsd->tx_lpi_status =
1091 (val & I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_MASK) >>
1092 I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_SHIFT;
1093 nsd->rx_lpi_status =
1094 (val & I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_MASK) >>
1095 I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_SHIFT;
1096 i40e_stat_update32(hw, I40E_PRTPM_TLPIC,
1097 pf->stat_offsets_loaded,
1098 &osd->tx_lpi_count, &nsd->tx_lpi_count);
1099 i40e_stat_update32(hw, I40E_PRTPM_RLPIC,
1100 pf->stat_offsets_loaded,
1101 &osd->rx_lpi_count, &nsd->rx_lpi_count);
1102
Anjali Singhai Jaind0389e52015-04-22 19:34:05 -04001103 if (pf->flags & I40E_FLAG_FD_SB_ENABLED &&
1104 !(pf->auto_disable_flags & I40E_FLAG_FD_SB_ENABLED))
1105 nsd->fd_sb_status = true;
1106 else
1107 nsd->fd_sb_status = false;
1108
1109 if (pf->flags & I40E_FLAG_FD_ATR_ENABLED &&
1110 !(pf->auto_disable_flags & I40E_FLAG_FD_ATR_ENABLED))
1111 nsd->fd_atr_status = true;
1112 else
1113 nsd->fd_atr_status = false;
1114
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001115 pf->stat_offsets_loaded = true;
1116}
1117
1118/**
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001119 * i40e_update_stats - Update the various statistics counters.
1120 * @vsi: the VSI to be updated
1121 *
1122 * Update the various stats for this VSI and its related entities.
1123 **/
1124void i40e_update_stats(struct i40e_vsi *vsi)
1125{
1126 struct i40e_pf *pf = vsi->back;
1127
1128 if (vsi == pf->vsi[pf->lan_vsi])
1129 i40e_update_pf_stats(pf);
1130
1131 i40e_update_vsi_stats(vsi);
Vasu Dev38e00432014-08-01 13:27:03 -07001132#ifdef I40E_FCOE
1133 i40e_update_fcoe_stats(vsi);
1134#endif
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001135}
1136
1137/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001138 * i40e_find_filter - Search VSI filter list for specific mac/vlan filter
1139 * @vsi: the VSI to be searched
1140 * @macaddr: the MAC address
1141 * @vlan: the vlan
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001142 *
1143 * Returns ptr to the filter object or NULL
1144 **/
1145static struct i40e_mac_filter *i40e_find_filter(struct i40e_vsi *vsi,
Jacob Keller6622f5c2016-10-05 09:30:32 -07001146 const u8 *macaddr, s16 vlan)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001147{
1148 struct i40e_mac_filter *f;
Jacob Keller278e7d02016-10-05 09:30:37 -07001149 u64 key;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001150
1151 if (!vsi || !macaddr)
1152 return NULL;
1153
Jacob Keller278e7d02016-10-05 09:30:37 -07001154 key = i40e_addr_to_hkey(macaddr);
1155 hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001156 if ((ether_addr_equal(macaddr, f->macaddr)) &&
Jacob Keller1bc87e82016-10-05 09:30:31 -07001157 (vlan == f->vlan))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001158 return f;
1159 }
1160 return NULL;
1161}
1162
1163/**
1164 * i40e_find_mac - Find a mac addr in the macvlan filters list
1165 * @vsi: the VSI to be searched
1166 * @macaddr: the MAC address we are searching for
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001167 *
1168 * Returns the first filter with the provided MAC address or NULL if
1169 * MAC address was not found
1170 **/
Jacob Keller6622f5c2016-10-05 09:30:32 -07001171struct i40e_mac_filter *i40e_find_mac(struct i40e_vsi *vsi, const u8 *macaddr)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001172{
1173 struct i40e_mac_filter *f;
Jacob Keller278e7d02016-10-05 09:30:37 -07001174 u64 key;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001175
1176 if (!vsi || !macaddr)
1177 return NULL;
1178
Jacob Keller278e7d02016-10-05 09:30:37 -07001179 key = i40e_addr_to_hkey(macaddr);
1180 hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
Jacob Keller1bc87e82016-10-05 09:30:31 -07001181 if ((ether_addr_equal(macaddr, f->macaddr)))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001182 return f;
1183 }
1184 return NULL;
1185}
1186
1187/**
1188 * i40e_is_vsi_in_vlan - Check if VSI is in vlan mode
1189 * @vsi: the VSI to be searched
1190 *
1191 * Returns true if VSI is in vlan mode or false otherwise
1192 **/
1193bool i40e_is_vsi_in_vlan(struct i40e_vsi *vsi)
1194{
Jacob Kellercbebb852016-10-05 09:30:40 -07001195 /* If we have a PVID, always operate in VLAN mode */
1196 if (vsi->info.pvid)
1197 return true;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001198
Jacob Kellercbebb852016-10-05 09:30:40 -07001199 /* We need to operate in VLAN mode whenever we have any filters with
1200 * a VLAN other than I40E_VLAN_ALL. We could check the table each
1201 * time, incurring search cost repeatedly. However, we can notice two
1202 * things:
1203 *
1204 * 1) the only place where we can gain a VLAN filter is in
1205 * i40e_add_filter.
1206 *
1207 * 2) the only place where filters are actually removed is in
Jacob Keller0b7c8b52016-10-25 16:08:52 -07001208 * i40e_sync_filters_subtask.
Jacob Kellercbebb852016-10-05 09:30:40 -07001209 *
1210 * Thus, we can simply use a boolean value, has_vlan_filters which we
1211 * will set to true when we add a VLAN filter in i40e_add_filter. Then
1212 * we have to perform the full search after deleting filters in
Jacob Keller0b7c8b52016-10-25 16:08:52 -07001213 * i40e_sync_filters_subtask, but we already have to search
Jacob Kellercbebb852016-10-05 09:30:40 -07001214 * filters here and can perform the check at the same time. This
1215 * results in avoiding embedding a loop for VLAN mode inside another
1216 * loop over all the filters, and should maintain correctness as noted
1217 * above.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001218 */
Jacob Kellercbebb852016-10-05 09:30:40 -07001219 return vsi->has_vlan_filter;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001220}
1221
1222/**
Jacob Keller489a3262016-11-11 12:39:31 -08001223 * i40e_correct_mac_vlan_filters - Correct non-VLAN filters if necessary
1224 * @vsi: the VSI to configure
1225 * @tmp_add_list: list of filters ready to be added
1226 * @tmp_del_list: list of filters ready to be deleted
1227 * @vlan_filters: the number of active VLAN filters
1228 *
1229 * Update VLAN=0 and VLAN=-1 (I40E_VLAN_ANY) filters properly so that they
1230 * behave as expected. If we have any active VLAN filters remaining or about
1231 * to be added then we need to update non-VLAN filters to be marked as VLAN=0
1232 * so that they only match against untagged traffic. If we no longer have any
1233 * active VLAN filters, we need to make all non-VLAN filters marked as VLAN=-1
1234 * so that they match against both tagged and untagged traffic. In this way,
1235 * we ensure that we correctly receive the desired traffic. This ensures that
1236 * when we have an active VLAN we will receive only untagged traffic and
1237 * traffic matching active VLANs. If we have no active VLANs then we will
1238 * operate in non-VLAN mode and receive all traffic, tagged or untagged.
1239 *
1240 * Finally, in a similar fashion, this function also corrects filters when
1241 * there is an active PVID assigned to this VSI.
1242 *
1243 * In case of memory allocation failure return -ENOMEM. Otherwise, return 0.
1244 *
1245 * This function is only expected to be called from within
1246 * i40e_sync_vsi_filters.
1247 *
1248 * NOTE: This function expects to be called while under the
1249 * mac_filter_hash_lock
1250 */
1251static int i40e_correct_mac_vlan_filters(struct i40e_vsi *vsi,
1252 struct hlist_head *tmp_add_list,
1253 struct hlist_head *tmp_del_list,
1254 int vlan_filters)
1255{
Jacob Keller5cb25902016-12-12 15:44:15 -08001256 s16 pvid = le16_to_cpu(vsi->info.pvid);
Jacob Keller489a3262016-11-11 12:39:31 -08001257 struct i40e_mac_filter *f, *add_head;
Jacob Keller671889e2016-12-02 12:33:00 -08001258 struct i40e_new_mac_filter *new;
Jacob Keller489a3262016-11-11 12:39:31 -08001259 struct hlist_node *h;
1260 int bkt, new_vlan;
1261
1262 /* To determine if a particular filter needs to be replaced we
1263 * have the three following conditions:
1264 *
1265 * a) if we have a PVID assigned, then all filters which are
1266 * not marked as VLAN=PVID must be replaced with filters that
1267 * are.
1268 * b) otherwise, if we have any active VLANS, all filters
1269 * which are marked as VLAN=-1 must be replaced with
1270 * filters marked as VLAN=0
1271 * c) finally, if we do not have any active VLANS, all filters
1272 * which are marked as VLAN=0 must be replaced with filters
1273 * marked as VLAN=-1
1274 */
1275
1276 /* Update the filters about to be added in place */
Jacob Keller671889e2016-12-02 12:33:00 -08001277 hlist_for_each_entry(new, tmp_add_list, hlist) {
Jacob Keller5cb25902016-12-12 15:44:15 -08001278 if (pvid && new->f->vlan != pvid)
1279 new->f->vlan = pvid;
Jacob Keller671889e2016-12-02 12:33:00 -08001280 else if (vlan_filters && new->f->vlan == I40E_VLAN_ANY)
1281 new->f->vlan = 0;
1282 else if (!vlan_filters && new->f->vlan == 0)
1283 new->f->vlan = I40E_VLAN_ANY;
Jacob Keller489a3262016-11-11 12:39:31 -08001284 }
1285
1286 /* Update the remaining active filters */
1287 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1288 /* Combine the checks for whether a filter needs to be changed
1289 * and then determine the new VLAN inside the if block, in
1290 * order to avoid duplicating code for adding the new filter
1291 * then deleting the old filter.
1292 */
Jacob Keller5cb25902016-12-12 15:44:15 -08001293 if ((pvid && f->vlan != pvid) ||
Jacob Keller489a3262016-11-11 12:39:31 -08001294 (vlan_filters && f->vlan == I40E_VLAN_ANY) ||
1295 (!vlan_filters && f->vlan == 0)) {
1296 /* Determine the new vlan we will be adding */
Jacob Keller5cb25902016-12-12 15:44:15 -08001297 if (pvid)
1298 new_vlan = pvid;
Jacob Keller489a3262016-11-11 12:39:31 -08001299 else if (vlan_filters)
1300 new_vlan = 0;
1301 else
1302 new_vlan = I40E_VLAN_ANY;
1303
1304 /* Create the new filter */
1305 add_head = i40e_add_filter(vsi, f->macaddr, new_vlan);
1306 if (!add_head)
1307 return -ENOMEM;
1308
Jacob Keller671889e2016-12-02 12:33:00 -08001309 /* Create a temporary i40e_new_mac_filter */
1310 new = kzalloc(sizeof(*new), GFP_ATOMIC);
1311 if (!new)
1312 return -ENOMEM;
1313
1314 new->f = add_head;
1315 new->state = add_head->state;
1316
1317 /* Add the new filter to the tmp list */
1318 hlist_add_head(&new->hlist, tmp_add_list);
Jacob Keller489a3262016-11-11 12:39:31 -08001319
1320 /* Put the original filter into the delete list */
1321 f->state = I40E_FILTER_REMOVE;
1322 hash_del(&f->hlist);
1323 hlist_add_head(&f->hlist, tmp_del_list);
1324 }
1325 }
1326
1327 vsi->has_vlan_filter = !!vlan_filters;
1328
1329 return 0;
1330}
1331
1332/**
Jacob Keller1596b5d2016-11-08 13:05:15 -08001333 * i40e_rm_default_mac_filter - Remove the default MAC filter set by NVM
1334 * @vsi: the PF Main VSI - inappropriate for any other VSI
1335 * @macaddr: the MAC address
1336 *
1337 * Remove whatever filter the firmware set up so the driver can manage
1338 * its own filtering intelligently.
1339 **/
1340static void i40e_rm_default_mac_filter(struct i40e_vsi *vsi, u8 *macaddr)
1341{
1342 struct i40e_aqc_remove_macvlan_element_data element;
1343 struct i40e_pf *pf = vsi->back;
1344
1345 /* Only appropriate for the PF main VSI */
1346 if (vsi->type != I40E_VSI_MAIN)
1347 return;
1348
1349 memset(&element, 0, sizeof(element));
1350 ether_addr_copy(element.mac_addr, macaddr);
1351 element.vlan_tag = 0;
1352 /* Ignore error returns, some firmware does it this way... */
1353 element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
1354 i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1355
1356 memset(&element, 0, sizeof(element));
1357 ether_addr_copy(element.mac_addr, macaddr);
1358 element.vlan_tag = 0;
1359 /* ...and some firmware does it this way. */
1360 element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH |
1361 I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
1362 i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1363}
1364
1365/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001366 * i40e_add_filter - Add a mac/vlan filter to the VSI
1367 * @vsi: the VSI to be searched
1368 * @macaddr: the MAC address
1369 * @vlan: the vlan
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001370 *
1371 * Returns ptr to the filter object or NULL when no memory available.
Kiran Patil21659032015-09-30 14:09:03 -04001372 *
Jacob Keller278e7d02016-10-05 09:30:37 -07001373 * NOTE: This function is expected to be called with mac_filter_hash_lock
Kiran Patil21659032015-09-30 14:09:03 -04001374 * being held.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001375 **/
1376struct i40e_mac_filter *i40e_add_filter(struct i40e_vsi *vsi,
Jacob Keller6622f5c2016-10-05 09:30:32 -07001377 const u8 *macaddr, s16 vlan)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001378{
1379 struct i40e_mac_filter *f;
Jacob Keller278e7d02016-10-05 09:30:37 -07001380 u64 key;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001381
1382 if (!vsi || !macaddr)
1383 return NULL;
1384
Jacob Keller1bc87e82016-10-05 09:30:31 -07001385 f = i40e_find_filter(vsi, macaddr, vlan);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001386 if (!f) {
1387 f = kzalloc(sizeof(*f), GFP_ATOMIC);
1388 if (!f)
Jacob Keller1bc87e82016-10-05 09:30:31 -07001389 return NULL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001390
Jacob Kellercbebb852016-10-05 09:30:40 -07001391 /* Update the boolean indicating if we need to function in
1392 * VLAN mode.
1393 */
1394 if (vlan >= 0)
1395 vsi->has_vlan_filter = true;
1396
Greg Rose9a173902014-05-22 06:32:02 +00001397 ether_addr_copy(f->macaddr, macaddr);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001398 f->vlan = vlan;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001399 /* If we're in overflow promisc mode, set the state directly
1400 * to failed, so we don't bother to try sending the filter
1401 * to the hardware.
1402 */
1403 if (test_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state))
1404 f->state = I40E_FILTER_FAILED;
1405 else
1406 f->state = I40E_FILTER_NEW;
Jacob Keller278e7d02016-10-05 09:30:37 -07001407 INIT_HLIST_NODE(&f->hlist);
1408
1409 key = i40e_addr_to_hkey(macaddr);
1410 hash_add(vsi->mac_filter_hash, &f->hlist, key);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001411
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001412 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1413 vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
1414 }
1415
Jacob Keller1bc87e82016-10-05 09:30:31 -07001416 /* If we're asked to add a filter that has been marked for removal, it
1417 * is safe to simply restore it to active state. __i40e_del_filter
1418 * will have simply deleted any filters which were previously marked
1419 * NEW or FAILED, so if it is currently marked REMOVE it must have
1420 * previously been ACTIVE. Since we haven't yet run the sync filters
1421 * task, just restore this filter to the ACTIVE state so that the
1422 * sync task leaves it in place
1423 */
1424 if (f->state == I40E_FILTER_REMOVE)
1425 f->state = I40E_FILTER_ACTIVE;
1426
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001427 return f;
1428}
1429
1430/**
Jacob Keller290d2552016-10-05 09:30:36 -07001431 * __i40e_del_filter - Remove a specific filter from the VSI
1432 * @vsi: VSI to remove from
1433 * @f: the filter to remove from the list
1434 *
1435 * This function should be called instead of i40e_del_filter only if you know
1436 * the exact filter you will remove already, such as via i40e_find_filter or
1437 * i40e_find_mac.
Kiran Patil21659032015-09-30 14:09:03 -04001438 *
Jacob Keller278e7d02016-10-05 09:30:37 -07001439 * NOTE: This function is expected to be called with mac_filter_hash_lock
Kiran Patil21659032015-09-30 14:09:03 -04001440 * being held.
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001441 * ANOTHER NOTE: This function MUST be called from within the context of
1442 * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
1443 * instead of list_for_each_entry().
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001444 **/
Jacob Keller148141b2016-11-11 12:39:36 -08001445void __i40e_del_filter(struct i40e_vsi *vsi, struct i40e_mac_filter *f)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001446{
Jacob Keller1bc87e82016-10-05 09:30:31 -07001447 if (!f)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001448 return;
1449
Alan Bradya410c822016-12-12 15:44:07 -08001450 /* If the filter was never added to firmware then we can just delete it
1451 * directly and we don't want to set the status to remove or else an
1452 * admin queue command will unnecessarily fire.
1453 */
Jacob Keller1bc87e82016-10-05 09:30:31 -07001454 if ((f->state == I40E_FILTER_FAILED) ||
1455 (f->state == I40E_FILTER_NEW)) {
Jacob Keller278e7d02016-10-05 09:30:37 -07001456 hash_del(&f->hlist);
Jacob Keller1bc87e82016-10-05 09:30:31 -07001457 kfree(f);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001458 } else {
Jacob Keller1bc87e82016-10-05 09:30:31 -07001459 f->state = I40E_FILTER_REMOVE;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001460 }
Alan Bradya410c822016-12-12 15:44:07 -08001461
1462 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1463 vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001464}
1465
1466/**
Jacob Keller290d2552016-10-05 09:30:36 -07001467 * i40e_del_filter - Remove a MAC/VLAN filter from the VSI
1468 * @vsi: the VSI to be searched
1469 * @macaddr: the MAC address
1470 * @vlan: the VLAN
1471 *
Jacob Keller278e7d02016-10-05 09:30:37 -07001472 * NOTE: This function is expected to be called with mac_filter_hash_lock
Jacob Keller290d2552016-10-05 09:30:36 -07001473 * being held.
1474 * ANOTHER NOTE: This function MUST be called from within the context of
1475 * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
1476 * instead of list_for_each_entry().
1477 **/
1478void i40e_del_filter(struct i40e_vsi *vsi, const u8 *macaddr, s16 vlan)
1479{
1480 struct i40e_mac_filter *f;
1481
1482 if (!vsi || !macaddr)
1483 return;
1484
1485 f = i40e_find_filter(vsi, macaddr, vlan);
1486 __i40e_del_filter(vsi, f);
1487}
1488
1489/**
Jacob Kellerfeffdbe2016-11-11 12:39:35 -08001490 * i40e_add_mac_filter - Add a MAC filter for all active VLANs
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001491 * @vsi: the VSI to be searched
1492 * @macaddr: the mac address to be filtered
1493 *
Jacob Kellerfeffdbe2016-11-11 12:39:35 -08001494 * If we're not in VLAN mode, just add the filter to I40E_VLAN_ANY. Otherwise,
1495 * go through all the macvlan filters and add a macvlan filter for each
Jacob Keller5feb3d72016-10-05 09:30:34 -07001496 * unique vlan that already exists. If a PVID has been assigned, instead only
1497 * add the macaddr to that VLAN.
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001498 *
Jacob Keller5feb3d72016-10-05 09:30:34 -07001499 * Returns last filter added on success, else NULL
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001500 **/
Jacob Kellerfeffdbe2016-11-11 12:39:35 -08001501struct i40e_mac_filter *i40e_add_mac_filter(struct i40e_vsi *vsi,
1502 const u8 *macaddr)
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001503{
Jacob Keller5feb3d72016-10-05 09:30:34 -07001504 struct i40e_mac_filter *f, *add = NULL;
Jacob Keller278e7d02016-10-05 09:30:37 -07001505 struct hlist_node *h;
1506 int bkt;
Jacob Keller5feb3d72016-10-05 09:30:34 -07001507
1508 if (vsi->info.pvid)
1509 return i40e_add_filter(vsi, macaddr,
1510 le16_to_cpu(vsi->info.pvid));
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001511
Jacob Keller7aaf95362016-11-11 12:39:33 -08001512 if (!i40e_is_vsi_in_vlan(vsi))
1513 return i40e_add_filter(vsi, macaddr, I40E_VLAN_ANY);
1514
Jacob Keller278e7d02016-10-05 09:30:37 -07001515 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Jacob Keller57b341d2016-10-05 09:30:35 -07001516 if (f->state == I40E_FILTER_REMOVE)
1517 continue;
Jacob Keller5feb3d72016-10-05 09:30:34 -07001518 add = i40e_add_filter(vsi, macaddr, f->vlan);
1519 if (!add)
1520 return NULL;
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001521 }
1522
Jacob Keller5feb3d72016-10-05 09:30:34 -07001523 return add;
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001524}
1525
1526/**
Jacob Kellerfeffdbe2016-11-11 12:39:35 -08001527 * i40e_del_mac_filter - Remove a MAC filter from all VLANs
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001528 * @vsi: the VSI to be searched
1529 * @macaddr: the mac address to be removed
1530 *
Jacob Kellerfeffdbe2016-11-11 12:39:35 -08001531 * Removes a given MAC address from a VSI regardless of what VLAN it has been
1532 * associated with.
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001533 *
1534 * Returns 0 for success, or error
1535 **/
Jacob Kellerfeffdbe2016-11-11 12:39:35 -08001536int i40e_del_mac_filter(struct i40e_vsi *vsi, const u8 *macaddr)
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001537{
Jacob Keller278e7d02016-10-05 09:30:37 -07001538 struct i40e_mac_filter *f;
1539 struct hlist_node *h;
Jacob Keller290d2552016-10-05 09:30:36 -07001540 bool found = false;
Jacob Keller278e7d02016-10-05 09:30:37 -07001541 int bkt;
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001542
Jacob Keller278e7d02016-10-05 09:30:37 -07001543 WARN(!spin_is_locked(&vsi->mac_filter_hash_lock),
1544 "Missing mac_filter_hash_lock\n");
1545 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Jacob Keller290d2552016-10-05 09:30:36 -07001546 if (ether_addr_equal(macaddr, f->macaddr)) {
1547 __i40e_del_filter(vsi, f);
1548 found = true;
1549 }
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001550 }
Jacob Keller290d2552016-10-05 09:30:36 -07001551
1552 if (found)
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001553 return 0;
Jacob Keller290d2552016-10-05 09:30:36 -07001554 else
1555 return -ENOENT;
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001556}
1557
1558/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001559 * i40e_set_mac - NDO callback to set mac address
1560 * @netdev: network interface device structure
1561 * @p: pointer to an address structure
1562 *
1563 * Returns 0 on success, negative on failure
1564 **/
Vasu Dev38e00432014-08-01 13:27:03 -07001565#ifdef I40E_FCOE
1566int i40e_set_mac(struct net_device *netdev, void *p)
1567#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001568static int i40e_set_mac(struct net_device *netdev, void *p)
Vasu Dev38e00432014-08-01 13:27:03 -07001569#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001570{
1571 struct i40e_netdev_priv *np = netdev_priv(netdev);
1572 struct i40e_vsi *vsi = np->vsi;
Shannon Nelson30650cc2014-07-29 04:01:50 +00001573 struct i40e_pf *pf = vsi->back;
1574 struct i40e_hw *hw = &pf->hw;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001575 struct sockaddr *addr = p;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001576
1577 if (!is_valid_ether_addr(addr->sa_data))
1578 return -EADDRNOTAVAIL;
1579
Shannon Nelson30650cc2014-07-29 04:01:50 +00001580 if (ether_addr_equal(netdev->dev_addr, addr->sa_data)) {
1581 netdev_info(netdev, "already using mac address %pM\n",
1582 addr->sa_data);
1583 return 0;
1584 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001585
Anjali Singhai Jain80f64282013-11-28 06:39:47 +00001586 if (test_bit(__I40E_DOWN, &vsi->back->state) ||
1587 test_bit(__I40E_RESET_RECOVERY_PENDING, &vsi->back->state))
1588 return -EADDRNOTAVAIL;
1589
Shannon Nelson30650cc2014-07-29 04:01:50 +00001590 if (ether_addr_equal(hw->mac.addr, addr->sa_data))
1591 netdev_info(netdev, "returning to hw mac address %pM\n",
1592 hw->mac.addr);
1593 else
1594 netdev_info(netdev, "set new mac address %pM\n", addr->sa_data);
1595
Jacob Keller278e7d02016-10-05 09:30:37 -07001596 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Kellerfeffdbe2016-11-11 12:39:35 -08001597 i40e_del_mac_filter(vsi, netdev->dev_addr);
1598 i40e_add_mac_filter(vsi, addr->sa_data);
Jacob Keller278e7d02016-10-05 09:30:37 -07001599 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001600 ether_addr_copy(netdev->dev_addr, addr->sa_data);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001601 if (vsi->type == I40E_VSI_MAIN) {
1602 i40e_status ret;
Jesse Brandeburg6995b362015-08-28 17:55:54 -04001603
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001604 ret = i40e_aq_mac_address_write(&vsi->back->hw,
Shannon Nelsoncc412222014-06-04 01:23:21 +00001605 I40E_AQC_WRITE_TYPE_LAA_WOL,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001606 addr->sa_data, NULL);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001607 if (ret)
1608 netdev_info(netdev, "Ignoring error from firmware on LAA update, status %s, AQ ret %s\n",
1609 i40e_stat_str(hw, ret),
1610 i40e_aq_str(hw, hw->aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001611 }
1612
Jesse Brandeburgc53934c2016-01-04 10:33:06 -08001613 /* schedule our worker thread which will take care of
1614 * applying the new filter changes
1615 */
1616 i40e_service_event_schedule(vsi->back);
1617 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001618}
1619
1620/**
1621 * i40e_vsi_setup_queue_map - Setup a VSI queue map based on enabled_tc
1622 * @vsi: the VSI being setup
1623 * @ctxt: VSI context structure
1624 * @enabled_tc: Enabled TCs bitmap
1625 * @is_add: True if called before Add VSI
1626 *
1627 * Setup VSI queue mapping for enabled traffic classes.
1628 **/
Vasu Dev38e00432014-08-01 13:27:03 -07001629#ifdef I40E_FCOE
1630void i40e_vsi_setup_queue_map(struct i40e_vsi *vsi,
1631 struct i40e_vsi_context *ctxt,
1632 u8 enabled_tc,
1633 bool is_add)
1634#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001635static void i40e_vsi_setup_queue_map(struct i40e_vsi *vsi,
1636 struct i40e_vsi_context *ctxt,
1637 u8 enabled_tc,
1638 bool is_add)
Vasu Dev38e00432014-08-01 13:27:03 -07001639#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001640{
1641 struct i40e_pf *pf = vsi->back;
1642 u16 sections = 0;
1643 u8 netdev_tc = 0;
1644 u16 numtc = 0;
1645 u16 qcount;
1646 u8 offset;
1647 u16 qmap;
1648 int i;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001649 u16 num_tc_qps = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001650
1651 sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
1652 offset = 0;
1653
1654 if (enabled_tc && (vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
1655 /* Find numtc from enabled TC bitmap */
1656 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08001657 if (enabled_tc & BIT(i)) /* TC is enabled */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001658 numtc++;
1659 }
1660 if (!numtc) {
1661 dev_warn(&pf->pdev->dev, "DCB is enabled but no TC enabled, forcing TC0\n");
1662 numtc = 1;
1663 }
1664 } else {
1665 /* At least TC0 is enabled in case of non-DCB case */
1666 numtc = 1;
1667 }
1668
1669 vsi->tc_config.numtc = numtc;
1670 vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001671 /* Number of queues per enabled TC */
Catherine Sullivan7d644022016-05-16 10:26:41 -07001672 qcount = vsi->alloc_queue_pairs;
1673
Anjali Singhai7f9ff472015-02-21 06:43:19 +00001674 num_tc_qps = qcount / numtc;
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04001675 num_tc_qps = min_t(int, num_tc_qps, i40e_pf_get_max_q_per_tc(pf));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001676
1677 /* Setup queue offset/count for all TCs for given VSI */
1678 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1679 /* See if the given TC is enabled for the given VSI */
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08001680 if (vsi->tc_config.enabled_tc & BIT(i)) {
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04001681 /* TC is enabled */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001682 int pow, num_qps;
1683
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001684 switch (vsi->type) {
1685 case I40E_VSI_MAIN:
Helin Zhangacd65442015-10-26 19:44:28 -04001686 qcount = min_t(int, pf->alloc_rss_size,
1687 num_tc_qps);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001688 break;
Vasu Dev38e00432014-08-01 13:27:03 -07001689#ifdef I40E_FCOE
1690 case I40E_VSI_FCOE:
1691 qcount = num_tc_qps;
1692 break;
1693#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001694 case I40E_VSI_FDIR:
1695 case I40E_VSI_SRIOV:
1696 case I40E_VSI_VMDQ2:
1697 default:
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001698 qcount = num_tc_qps;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001699 WARN_ON(i != 0);
1700 break;
1701 }
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001702 vsi->tc_config.tc_info[i].qoffset = offset;
1703 vsi->tc_config.tc_info[i].qcount = qcount;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001704
Shannon Nelson1e200e42015-02-27 09:15:24 +00001705 /* find the next higher power-of-2 of num queue pairs */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001706 num_qps = qcount;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001707 pow = 0;
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04001708 while (num_qps && (BIT_ULL(pow) < qcount)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001709 pow++;
1710 num_qps >>= 1;
1711 }
1712
1713 vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
1714 qmap =
1715 (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
1716 (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
1717
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001718 offset += qcount;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001719 } else {
1720 /* TC is not enabled so set the offset to
1721 * default queue and allocate one queue
1722 * for the given TC.
1723 */
1724 vsi->tc_config.tc_info[i].qoffset = 0;
1725 vsi->tc_config.tc_info[i].qcount = 1;
1726 vsi->tc_config.tc_info[i].netdev_tc = 0;
1727
1728 qmap = 0;
1729 }
1730 ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
1731 }
1732
1733 /* Set actual Tx/Rx queue pairs */
1734 vsi->num_queue_pairs = offset;
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00001735 if ((vsi->type == I40E_VSI_MAIN) && (numtc == 1)) {
1736 if (vsi->req_queue_pairs > 0)
1737 vsi->num_queue_pairs = vsi->req_queue_pairs;
Anjali Singhai Jain26cdc442015-07-10 19:36:00 -04001738 else if (pf->flags & I40E_FLAG_MSIX_ENABLED)
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00001739 vsi->num_queue_pairs = pf->num_lan_msix;
1740 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001741
1742 /* Scheduler section valid can only be set for ADD VSI */
1743 if (is_add) {
1744 sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
1745
1746 ctxt->info.up_enable_bits = enabled_tc;
1747 }
1748 if (vsi->type == I40E_VSI_SRIOV) {
1749 ctxt->info.mapping_flags |=
1750 cpu_to_le16(I40E_AQ_VSI_QUE_MAP_NONCONTIG);
1751 for (i = 0; i < vsi->num_queue_pairs; i++)
1752 ctxt->info.queue_mapping[i] =
1753 cpu_to_le16(vsi->base_queue + i);
1754 } else {
1755 ctxt->info.mapping_flags |=
1756 cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
1757 ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
1758 }
1759 ctxt->info.valid_sections |= cpu_to_le16(sections);
1760}
1761
1762/**
Jacob Keller6622f5c2016-10-05 09:30:32 -07001763 * i40e_addr_sync - Callback for dev_(mc|uc)_sync to add address
1764 * @netdev: the netdevice
1765 * @addr: address to add
1766 *
1767 * Called by __dev_(mc|uc)_sync when an address needs to be added. We call
1768 * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
1769 */
1770static int i40e_addr_sync(struct net_device *netdev, const u8 *addr)
1771{
1772 struct i40e_netdev_priv *np = netdev_priv(netdev);
1773 struct i40e_vsi *vsi = np->vsi;
Jacob Keller6622f5c2016-10-05 09:30:32 -07001774
Jacob Kellerfeffdbe2016-11-11 12:39:35 -08001775 if (i40e_add_mac_filter(vsi, addr))
Jacob Keller6622f5c2016-10-05 09:30:32 -07001776 return 0;
1777 else
1778 return -ENOMEM;
1779}
1780
1781/**
1782 * i40e_addr_unsync - Callback for dev_(mc|uc)_sync to remove address
1783 * @netdev: the netdevice
1784 * @addr: address to add
1785 *
1786 * Called by __dev_(mc|uc)_sync when an address needs to be removed. We call
1787 * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
1788 */
1789static int i40e_addr_unsync(struct net_device *netdev, const u8 *addr)
1790{
1791 struct i40e_netdev_priv *np = netdev_priv(netdev);
1792 struct i40e_vsi *vsi = np->vsi;
1793
Jacob Kellerfeffdbe2016-11-11 12:39:35 -08001794 i40e_del_mac_filter(vsi, addr);
Jacob Keller6622f5c2016-10-05 09:30:32 -07001795
1796 return 0;
1797}
1798
1799/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001800 * i40e_set_rx_mode - NDO callback to set the netdev filters
1801 * @netdev: network interface device structure
1802 **/
Vasu Dev38e00432014-08-01 13:27:03 -07001803#ifdef I40E_FCOE
1804void i40e_set_rx_mode(struct net_device *netdev)
1805#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001806static void i40e_set_rx_mode(struct net_device *netdev)
Vasu Dev38e00432014-08-01 13:27:03 -07001807#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001808{
1809 struct i40e_netdev_priv *np = netdev_priv(netdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001810 struct i40e_vsi *vsi = np->vsi;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001811
Jacob Keller278e7d02016-10-05 09:30:37 -07001812 spin_lock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -04001813
Jacob Keller6622f5c2016-10-05 09:30:32 -07001814 __dev_uc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
1815 __dev_mc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001816
Jacob Keller278e7d02016-10-05 09:30:37 -07001817 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001818
1819 /* check for other flag changes */
1820 if (vsi->current_netdev_flags != vsi->netdev->flags) {
1821 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1822 vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
1823 }
Jesse Brandeburgc53934c2016-01-04 10:33:06 -08001824
1825 /* schedule our worker thread which will take care of
1826 * applying the new filter changes
1827 */
1828 i40e_service_event_schedule(vsi->back);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001829}
1830
1831/**
Jacob Keller671889e2016-12-02 12:33:00 -08001832 * i40e_undo_del_filter_entries - Undo the changes made to MAC filter entries
Jacob Keller4a2ce272016-10-05 09:30:38 -07001833 * @vsi: Pointer to VSI struct
Kiran Patil21659032015-09-30 14:09:03 -04001834 * @from: Pointer to list which contains MAC filter entries - changes to
1835 * those entries needs to be undone.
1836 *
Jacob Keller671889e2016-12-02 12:33:00 -08001837 * MAC filter entries from this list were slated for deletion.
Kiran Patil21659032015-09-30 14:09:03 -04001838 **/
Jacob Keller671889e2016-12-02 12:33:00 -08001839static void i40e_undo_del_filter_entries(struct i40e_vsi *vsi,
1840 struct hlist_head *from)
Kiran Patil21659032015-09-30 14:09:03 -04001841{
Jacob Keller278e7d02016-10-05 09:30:37 -07001842 struct i40e_mac_filter *f;
1843 struct hlist_node *h;
Kiran Patil21659032015-09-30 14:09:03 -04001844
Jacob Keller278e7d02016-10-05 09:30:37 -07001845 hlist_for_each_entry_safe(f, h, from, hlist) {
1846 u64 key = i40e_addr_to_hkey(f->macaddr);
1847
Kiran Patil21659032015-09-30 14:09:03 -04001848 /* Move the element back into MAC filter list*/
Jacob Keller278e7d02016-10-05 09:30:37 -07001849 hlist_del(&f->hlist);
1850 hash_add(vsi->mac_filter_hash, &f->hlist, key);
Kiran Patil21659032015-09-30 14:09:03 -04001851 }
1852}
1853
1854/**
Jacob Keller671889e2016-12-02 12:33:00 -08001855 * i40e_undo_add_filter_entries - Undo the changes made to MAC filter entries
1856 * @vsi: Pointer to vsi struct
1857 * @from: Pointer to list which contains MAC filter entries - changes to
1858 * those entries needs to be undone.
1859 *
1860 * MAC filter entries from this list were slated for addition.
1861 **/
1862static void i40e_undo_add_filter_entries(struct i40e_vsi *vsi,
1863 struct hlist_head *from)
1864{
1865 struct i40e_new_mac_filter *new;
1866 struct hlist_node *h;
1867
1868 hlist_for_each_entry_safe(new, h, from, hlist) {
1869 /* We can simply free the wrapper structure */
1870 hlist_del(&new->hlist);
1871 kfree(new);
1872 }
1873}
1874
1875/**
Jacob Kellerd88d40b2016-12-02 12:32:59 -08001876 * i40e_next_entry - Get the next non-broadcast filter from a list
Jacob Keller671889e2016-12-02 12:33:00 -08001877 * @next: pointer to filter in list
Jacob Kellerd88d40b2016-12-02 12:32:59 -08001878 *
1879 * Returns the next non-broadcast filter in the list. Required so that we
1880 * ignore broadcast filters within the list, since these are not handled via
1881 * the normal firmware update path.
1882 */
Jacob Keller671889e2016-12-02 12:33:00 -08001883static
1884struct i40e_new_mac_filter *i40e_next_filter(struct i40e_new_mac_filter *next)
Jacob Kellerd88d40b2016-12-02 12:32:59 -08001885{
Jacob Keller671889e2016-12-02 12:33:00 -08001886 while (next) {
1887 next = hlist_entry(next->hlist.next,
1888 typeof(struct i40e_new_mac_filter),
1889 hlist);
Jacob Kellerd88d40b2016-12-02 12:32:59 -08001890
1891 /* keep going if we found a broadcast filter */
Jacob Keller671889e2016-12-02 12:33:00 -08001892 if (next && is_broadcast_ether_addr(next->f->macaddr))
Jacob Kellerd88d40b2016-12-02 12:32:59 -08001893 continue;
1894
1895 break;
1896 }
1897
Jacob Keller671889e2016-12-02 12:33:00 -08001898 return next;
Jacob Kellerd88d40b2016-12-02 12:32:59 -08001899}
1900
1901/**
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001902 * i40e_update_filter_state - Update filter state based on return data
1903 * from firmware
1904 * @count: Number of filters added
1905 * @add_list: return data from fw
1906 * @head: pointer to first filter in current batch
Kiran Patil21659032015-09-30 14:09:03 -04001907 *
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001908 * MAC filter entries from list were slated to be added to device. Returns
1909 * number of successful filters. Note that 0 does NOT mean success!
Kiran Patil21659032015-09-30 14:09:03 -04001910 **/
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001911static int
1912i40e_update_filter_state(int count,
1913 struct i40e_aqc_add_macvlan_element_data *add_list,
Jacob Keller671889e2016-12-02 12:33:00 -08001914 struct i40e_new_mac_filter *add_head)
Kiran Patil21659032015-09-30 14:09:03 -04001915{
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001916 int retval = 0;
1917 int i;
Kiran Patil21659032015-09-30 14:09:03 -04001918
Jacob Kellerac9e2392016-11-11 12:39:27 -08001919 for (i = 0; i < count; i++) {
1920 /* Always check status of each filter. We don't need to check
1921 * the firmware return status because we pre-set the filter
1922 * status to I40E_AQC_MM_ERR_NO_RES when sending the filter
1923 * request to the adminq. Thus, if it no longer matches then
1924 * we know the filter is active.
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001925 */
Jacob Kellerac9e2392016-11-11 12:39:27 -08001926 if (add_list[i].match_method == I40E_AQC_MM_ERR_NO_RES) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001927 add_head->state = I40E_FILTER_FAILED;
Jacob Kellerac9e2392016-11-11 12:39:27 -08001928 } else {
1929 add_head->state = I40E_FILTER_ACTIVE;
1930 retval++;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001931 }
Jacob Kellerac9e2392016-11-11 12:39:27 -08001932
Jacob Kellerd88d40b2016-12-02 12:32:59 -08001933 add_head = i40e_next_filter(add_head);
1934 if (!add_head)
1935 break;
Kiran Patil21659032015-09-30 14:09:03 -04001936 }
Jacob Kellerac9e2392016-11-11 12:39:27 -08001937
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001938 return retval;
Kiran Patil21659032015-09-30 14:09:03 -04001939}
1940
1941/**
Jacob Keller00936312016-10-05 09:30:41 -07001942 * i40e_aqc_del_filters - Request firmware to delete a set of filters
1943 * @vsi: ptr to the VSI
1944 * @vsi_name: name to display in messages
1945 * @list: the list of filters to send to firmware
1946 * @num_del: the number of filters to delete
1947 * @retval: Set to -EIO on failure to delete
1948 *
1949 * Send a request to firmware via AdminQ to delete a set of filters. Uses
1950 * *retval instead of a return value so that success does not force ret_val to
1951 * be set to 0. This ensures that a sequence of calls to this function
1952 * preserve the previous value of *retval on successful delete.
1953 */
1954static
1955void i40e_aqc_del_filters(struct i40e_vsi *vsi, const char *vsi_name,
1956 struct i40e_aqc_remove_macvlan_element_data *list,
1957 int num_del, int *retval)
1958{
1959 struct i40e_hw *hw = &vsi->back->hw;
1960 i40e_status aq_ret;
1961 int aq_err;
1962
1963 aq_ret = i40e_aq_remove_macvlan(hw, vsi->seid, list, num_del, NULL);
1964 aq_err = hw->aq.asq_last_status;
1965
1966 /* Explicitly ignore and do not report when firmware returns ENOENT */
1967 if (aq_ret && !(aq_err == I40E_AQ_RC_ENOENT)) {
1968 *retval = -EIO;
1969 dev_info(&vsi->back->pdev->dev,
1970 "ignoring delete macvlan error on %s, err %s, aq_err %s\n",
1971 vsi_name, i40e_stat_str(hw, aq_ret),
1972 i40e_aq_str(hw, aq_err));
1973 }
1974}
1975
1976/**
1977 * i40e_aqc_add_filters - Request firmware to add a set of filters
1978 * @vsi: ptr to the VSI
1979 * @vsi_name: name to display in messages
1980 * @list: the list of filters to send to firmware
1981 * @add_head: Position in the add hlist
1982 * @num_add: the number of filters to add
1983 * @promisc_change: set to true on exit if promiscuous mode was forced on
1984 *
1985 * Send a request to firmware via AdminQ to add a chunk of filters. Will set
1986 * promisc_changed to true if the firmware has run out of space for more
1987 * filters.
1988 */
1989static
1990void i40e_aqc_add_filters(struct i40e_vsi *vsi, const char *vsi_name,
1991 struct i40e_aqc_add_macvlan_element_data *list,
Jacob Keller671889e2016-12-02 12:33:00 -08001992 struct i40e_new_mac_filter *add_head,
Jacob Keller00936312016-10-05 09:30:41 -07001993 int num_add, bool *promisc_changed)
1994{
1995 struct i40e_hw *hw = &vsi->back->hw;
Jacob Keller00936312016-10-05 09:30:41 -07001996 int aq_err, fcnt;
1997
Jacob Kellerac9e2392016-11-11 12:39:27 -08001998 i40e_aq_add_macvlan(hw, vsi->seid, list, num_add, NULL);
Jacob Keller00936312016-10-05 09:30:41 -07001999 aq_err = hw->aq.asq_last_status;
Jacob Kellerac9e2392016-11-11 12:39:27 -08002000 fcnt = i40e_update_filter_state(num_add, list, add_head);
Jacob Keller00936312016-10-05 09:30:41 -07002001
2002 if (fcnt != num_add) {
2003 *promisc_changed = true;
2004 set_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state);
Jacob Keller00936312016-10-05 09:30:41 -07002005 dev_warn(&vsi->back->pdev->dev,
2006 "Error %s adding RX filters on %s, promiscuous mode forced on\n",
2007 i40e_aq_str(hw, aq_err),
2008 vsi_name);
2009 }
2010}
2011
2012/**
Jacob Keller435c0842016-11-08 13:05:10 -08002013 * i40e_aqc_broadcast_filter - Set promiscuous broadcast flags
2014 * @vsi: pointer to the VSI
2015 * @f: filter data
2016 *
2017 * This function sets or clears the promiscuous broadcast flags for VLAN
2018 * filters in order to properly receive broadcast frames. Assumes that only
2019 * broadcast filters are passed.
Jacob Keller671889e2016-12-02 12:33:00 -08002020 *
2021 * Returns status indicating success or failure;
Jacob Keller435c0842016-11-08 13:05:10 -08002022 **/
Jacob Keller671889e2016-12-02 12:33:00 -08002023static i40e_status
2024i40e_aqc_broadcast_filter(struct i40e_vsi *vsi, const char *vsi_name,
2025 struct i40e_mac_filter *f)
Jacob Keller435c0842016-11-08 13:05:10 -08002026{
2027 bool enable = f->state == I40E_FILTER_NEW;
2028 struct i40e_hw *hw = &vsi->back->hw;
2029 i40e_status aq_ret;
2030
2031 if (f->vlan == I40E_VLAN_ANY) {
2032 aq_ret = i40e_aq_set_vsi_broadcast(hw,
2033 vsi->seid,
2034 enable,
2035 NULL);
2036 } else {
2037 aq_ret = i40e_aq_set_vsi_bc_promisc_on_vlan(hw,
2038 vsi->seid,
2039 enable,
2040 f->vlan,
2041 NULL);
2042 }
2043
Jacob Keller671889e2016-12-02 12:33:00 -08002044 if (aq_ret)
Jacob Keller435c0842016-11-08 13:05:10 -08002045 dev_warn(&vsi->back->pdev->dev,
2046 "Error %s setting broadcast promiscuous mode on %s\n",
2047 i40e_aq_str(hw, hw->aq.asq_last_status),
2048 vsi_name);
Jacob Keller671889e2016-12-02 12:33:00 -08002049
2050 return aq_ret;
Jacob Keller435c0842016-11-08 13:05:10 -08002051}
2052
2053/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002054 * i40e_sync_vsi_filters - Update the VSI filter list to the HW
2055 * @vsi: ptr to the VSI
2056 *
2057 * Push any outstanding VSI filter changes through the AdminQ.
2058 *
2059 * Returns 0 or error value
2060 **/
Jesse Brandeburg17652c62015-11-05 17:01:02 -08002061int i40e_sync_vsi_filters(struct i40e_vsi *vsi)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002062{
Jacob Keller278e7d02016-10-05 09:30:37 -07002063 struct hlist_head tmp_add_list, tmp_del_list;
Jacob Keller671889e2016-12-02 12:33:00 -08002064 struct i40e_mac_filter *f;
2065 struct i40e_new_mac_filter *new, *add_head = NULL;
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002066 struct i40e_hw *hw = &vsi->back->hw;
Jacob Keller38326212016-11-11 12:39:26 -08002067 unsigned int failed_filters = 0;
Alan Brady84f5ca62016-10-05 09:30:39 -07002068 unsigned int vlan_filters = 0;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002069 bool promisc_changed = false;
Shannon Nelson2d1de822016-05-16 10:26:44 -07002070 char vsi_name[16] = "PF";
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002071 int filter_list_len = 0;
Mitch Williamsea02e902015-11-09 15:35:50 -08002072 i40e_status aq_ret = 0;
Alan Brady84f5ca62016-10-05 09:30:39 -07002073 u32 changed_flags = 0;
Jacob Keller278e7d02016-10-05 09:30:37 -07002074 struct hlist_node *h;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002075 struct i40e_pf *pf;
2076 int num_add = 0;
2077 int num_del = 0;
Alan Brady84f5ca62016-10-05 09:30:39 -07002078 int retval = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002079 u16 cmd_flags;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002080 int list_size;
Jacob Keller278e7d02016-10-05 09:30:37 -07002081 int bkt;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002082
2083 /* empty array typed pointers, kcalloc later */
2084 struct i40e_aqc_add_macvlan_element_data *add_list;
2085 struct i40e_aqc_remove_macvlan_element_data *del_list;
2086
2087 while (test_and_set_bit(__I40E_CONFIG_BUSY, &vsi->state))
2088 usleep_range(1000, 2000);
2089 pf = vsi->back;
2090
2091 if (vsi->netdev) {
2092 changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
2093 vsi->current_netdev_flags = vsi->netdev->flags;
2094 }
2095
Jacob Keller278e7d02016-10-05 09:30:37 -07002096 INIT_HLIST_HEAD(&tmp_add_list);
2097 INIT_HLIST_HEAD(&tmp_del_list);
Kiran Patil21659032015-09-30 14:09:03 -04002098
Shannon Nelson2d1de822016-05-16 10:26:44 -07002099 if (vsi->type == I40E_VSI_SRIOV)
2100 snprintf(vsi_name, sizeof(vsi_name) - 1, "VF %d", vsi->vf_id);
2101 else if (vsi->type != I40E_VSI_MAIN)
2102 snprintf(vsi_name, sizeof(vsi_name) - 1, "vsi %d", vsi->seid);
2103
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002104 if (vsi->flags & I40E_VSI_FLAG_FILTER_CHANGED) {
2105 vsi->flags &= ~I40E_VSI_FLAG_FILTER_CHANGED;
2106
Jacob Keller278e7d02016-10-05 09:30:37 -07002107 spin_lock_bh(&vsi->mac_filter_hash_lock);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002108 /* Create a list of filters to delete. */
Jacob Keller278e7d02016-10-05 09:30:37 -07002109 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002110 if (f->state == I40E_FILTER_REMOVE) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002111 /* Move the element into temporary del_list */
Jacob Keller278e7d02016-10-05 09:30:37 -07002112 hash_del(&f->hlist);
2113 hlist_add_head(&f->hlist, &tmp_del_list);
Alan Brady84f5ca62016-10-05 09:30:39 -07002114
2115 /* Avoid counting removed filters */
2116 continue;
Kiran Patil21659032015-09-30 14:09:03 -04002117 }
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002118 if (f->state == I40E_FILTER_NEW) {
Jacob Keller671889e2016-12-02 12:33:00 -08002119 /* Create a temporary i40e_new_mac_filter */
2120 new = kzalloc(sizeof(*new), GFP_ATOMIC);
2121 if (!new)
2122 goto err_no_memory_locked;
2123
2124 /* Store pointer to the real filter */
2125 new->f = f;
2126 new->state = f->state;
2127
2128 /* Add it to the hash list */
2129 hlist_add_head(&new->hlist, &tmp_add_list);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002130 }
Alan Brady84f5ca62016-10-05 09:30:39 -07002131
Jacob Keller489a3262016-11-11 12:39:31 -08002132 /* Count the number of active (current and new) VLAN
2133 * filters we have now. Does not count filters which
2134 * are marked for deletion.
Alan Brady84f5ca62016-10-05 09:30:39 -07002135 */
2136 if (f->vlan > 0)
2137 vlan_filters++;
Alan Brady84f5ca62016-10-05 09:30:39 -07002138 }
2139
Jacob Keller489a3262016-11-11 12:39:31 -08002140 retval = i40e_correct_mac_vlan_filters(vsi,
2141 &tmp_add_list,
2142 &tmp_del_list,
2143 vlan_filters);
2144 if (retval)
2145 goto err_no_memory_locked;
Alan Brady84f5ca62016-10-05 09:30:39 -07002146
Jacob Keller278e7d02016-10-05 09:30:37 -07002147 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -04002148 }
2149
2150 /* Now process 'del_list' outside the lock */
Jacob Keller278e7d02016-10-05 09:30:37 -07002151 if (!hlist_empty(&tmp_del_list)) {
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002152 filter_list_len = hw->aq.asq_buf_size /
Kiran Patil21659032015-09-30 14:09:03 -04002153 sizeof(struct i40e_aqc_remove_macvlan_element_data);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002154 list_size = filter_list_len *
Shannon Nelsonf1199992015-11-19 11:34:23 -08002155 sizeof(struct i40e_aqc_remove_macvlan_element_data);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002156 del_list = kzalloc(list_size, GFP_ATOMIC);
Jacob Keller4a2ce272016-10-05 09:30:38 -07002157 if (!del_list)
2158 goto err_no_memory;
Kiran Patil21659032015-09-30 14:09:03 -04002159
Jacob Keller278e7d02016-10-05 09:30:37 -07002160 hlist_for_each_entry_safe(f, h, &tmp_del_list, hlist) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002161 cmd_flags = 0;
2162
Jacob Keller435c0842016-11-08 13:05:10 -08002163 /* handle broadcast filters by updating the broadcast
Jacob Kellerd88d40b2016-12-02 12:32:59 -08002164 * promiscuous flag and release filter list.
Jacob Keller435c0842016-11-08 13:05:10 -08002165 */
2166 if (is_broadcast_ether_addr(f->macaddr)) {
2167 i40e_aqc_broadcast_filter(vsi, vsi_name, f);
2168
2169 hlist_del(&f->hlist);
2170 kfree(f);
2171 continue;
2172 }
2173
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002174 /* add to delete list */
Greg Rose9a173902014-05-22 06:32:02 +00002175 ether_addr_copy(del_list[num_del].mac_addr, f->macaddr);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002176 if (f->vlan == I40E_VLAN_ANY) {
2177 del_list[num_del].vlan_tag = 0;
Alan Bradya6cb9142016-09-06 18:05:07 -07002178 cmd_flags |= I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002179 } else {
2180 del_list[num_del].vlan_tag =
2181 cpu_to_le16((u16)(f->vlan));
2182 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002183
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002184 cmd_flags |= I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
2185 del_list[num_del].flags = cmd_flags;
2186 num_del++;
2187
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002188 /* flush a full buffer */
2189 if (num_del == filter_list_len) {
Jacob Keller00936312016-10-05 09:30:41 -07002190 i40e_aqc_del_filters(vsi, vsi_name, del_list,
2191 num_del, &retval);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002192 memset(del_list, 0, list_size);
Jacob Keller00936312016-10-05 09:30:41 -07002193 num_del = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002194 }
Kiran Patil21659032015-09-30 14:09:03 -04002195 /* Release memory for MAC filter entries which were
2196 * synced up with HW.
2197 */
Jacob Keller278e7d02016-10-05 09:30:37 -07002198 hlist_del(&f->hlist);
Kiran Patil21659032015-09-30 14:09:03 -04002199 kfree(f);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002200 }
Kiran Patil21659032015-09-30 14:09:03 -04002201
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002202 if (num_del) {
Jacob Keller00936312016-10-05 09:30:41 -07002203 i40e_aqc_del_filters(vsi, vsi_name, del_list,
2204 num_del, &retval);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002205 }
2206
2207 kfree(del_list);
2208 del_list = NULL;
Kiran Patil21659032015-09-30 14:09:03 -04002209 }
2210
Jacob Keller278e7d02016-10-05 09:30:37 -07002211 if (!hlist_empty(&tmp_add_list)) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002212 /* Do all the adds now. */
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002213 filter_list_len = hw->aq.asq_buf_size /
Shannon Nelsonf1199992015-11-19 11:34:23 -08002214 sizeof(struct i40e_aqc_add_macvlan_element_data);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002215 list_size = filter_list_len *
2216 sizeof(struct i40e_aqc_add_macvlan_element_data);
2217 add_list = kzalloc(list_size, GFP_ATOMIC);
Jacob Keller4a2ce272016-10-05 09:30:38 -07002218 if (!add_list)
2219 goto err_no_memory;
2220
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002221 num_add = 0;
Jacob Keller671889e2016-12-02 12:33:00 -08002222 hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002223 if (test_bit(__I40E_FILTER_OVERFLOW_PROMISC,
2224 &vsi->state)) {
Jacob Keller671889e2016-12-02 12:33:00 -08002225 new->state = I40E_FILTER_FAILED;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002226 continue;
2227 }
Jacob Keller435c0842016-11-08 13:05:10 -08002228
2229 /* handle broadcast filters by updating the broadcast
2230 * promiscuous flag instead of adding a MAC filter.
2231 */
Jacob Keller671889e2016-12-02 12:33:00 -08002232 if (is_broadcast_ether_addr(new->f->macaddr)) {
2233 if (i40e_aqc_broadcast_filter(vsi, vsi_name,
2234 new->f))
2235 new->state = I40E_FILTER_FAILED;
2236 else
2237 new->state = I40E_FILTER_ACTIVE;
Jacob Keller435c0842016-11-08 13:05:10 -08002238 continue;
2239 }
2240
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002241 /* add to add array */
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002242 if (num_add == 0)
Jacob Keller671889e2016-12-02 12:33:00 -08002243 add_head = new;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002244 cmd_flags = 0;
Jacob Keller671889e2016-12-02 12:33:00 -08002245 ether_addr_copy(add_list[num_add].mac_addr,
2246 new->f->macaddr);
2247 if (new->f->vlan == I40E_VLAN_ANY) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002248 add_list[num_add].vlan_tag = 0;
2249 cmd_flags |= I40E_AQC_MACVLAN_ADD_IGNORE_VLAN;
2250 } else {
2251 add_list[num_add].vlan_tag =
Jacob Keller671889e2016-12-02 12:33:00 -08002252 cpu_to_le16((u16)(new->f->vlan));
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002253 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002254 add_list[num_add].queue_number = 0;
Jacob Kellerac9e2392016-11-11 12:39:27 -08002255 /* set invalid match method for later detection */
Keller, Jacob E0266ac42016-12-09 13:39:21 -08002256 add_list[num_add].match_method = I40E_AQC_MM_ERR_NO_RES;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002257 cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002258 add_list[num_add].flags = cpu_to_le16(cmd_flags);
2259 num_add++;
2260
2261 /* flush a full buffer */
2262 if (num_add == filter_list_len) {
Jacob Keller00936312016-10-05 09:30:41 -07002263 i40e_aqc_add_filters(vsi, vsi_name, add_list,
2264 add_head, num_add,
2265 &promisc_changed);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002266 memset(add_list, 0, list_size);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002267 num_add = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002268 }
2269 }
2270 if (num_add) {
Jacob Keller00936312016-10-05 09:30:41 -07002271 i40e_aqc_add_filters(vsi, vsi_name, add_list, add_head,
2272 num_add, &promisc_changed);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002273 }
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002274 /* Now move all of the filters from the temp add list back to
2275 * the VSI's list.
2276 */
Jacob Keller278e7d02016-10-05 09:30:37 -07002277 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller671889e2016-12-02 12:33:00 -08002278 hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
2279 /* Only update the state if we're still NEW */
2280 if (new->f->state == I40E_FILTER_NEW)
2281 new->f->state = new->state;
2282 hlist_del(&new->hlist);
2283 kfree(new);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002284 }
Jacob Keller278e7d02016-10-05 09:30:37 -07002285 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002286 kfree(add_list);
2287 add_list = NULL;
2288 }
2289
Jacob Keller38326212016-11-11 12:39:26 -08002290 /* Determine the number of active and failed filters. */
2291 spin_lock_bh(&vsi->mac_filter_hash_lock);
2292 vsi->active_filters = 0;
2293 hash_for_each(vsi->mac_filter_hash, bkt, f, hlist) {
2294 if (f->state == I40E_FILTER_ACTIVE)
2295 vsi->active_filters++;
2296 else if (f->state == I40E_FILTER_FAILED)
2297 failed_filters++;
2298 }
2299 spin_unlock_bh(&vsi->mac_filter_hash_lock);
2300
2301 /* If promiscuous mode has changed, we need to calculate a new
2302 * threshold for when we are safe to exit
2303 */
2304 if (promisc_changed)
2305 vsi->promisc_threshold = (vsi->active_filters * 3) / 4;
2306
2307 /* Check if we are able to exit overflow promiscuous mode. We can
2308 * safely exit if we didn't just enter, we no longer have any failed
2309 * filters, and we have reduced filters below the threshold value.
2310 */
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002311 if (test_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state) &&
Jacob Keller38326212016-11-11 12:39:26 -08002312 !promisc_changed && !failed_filters &&
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002313 (vsi->active_filters < vsi->promisc_threshold)) {
Jacob Keller38326212016-11-11 12:39:26 -08002314 dev_info(&pf->pdev->dev,
2315 "filter logjam cleared on %s, leaving overflow promiscuous mode\n",
2316 vsi_name);
2317 clear_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state);
2318 promisc_changed = true;
2319 vsi->promisc_threshold = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002320 }
2321
Anjali Singhai Jaina856b5c2016-04-13 03:08:23 -07002322 /* if the VF is not trusted do not do promisc */
2323 if ((vsi->type == I40E_VSI_SRIOV) && !pf->vf[vsi->vf_id].trusted) {
2324 clear_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state);
2325 goto out;
2326 }
2327
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002328 /* check for changes in promiscuous modes */
2329 if (changed_flags & IFF_ALLMULTI) {
2330 bool cur_multipromisc;
Jesse Brandeburg6995b362015-08-28 17:55:54 -04002331
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002332 cur_multipromisc = !!(vsi->current_netdev_flags & IFF_ALLMULTI);
Mitch Williamsea02e902015-11-09 15:35:50 -08002333 aq_ret = i40e_aq_set_vsi_multicast_promiscuous(&vsi->back->hw,
2334 vsi->seid,
2335 cur_multipromisc,
2336 NULL);
2337 if (aq_ret) {
2338 retval = i40e_aq_rc_to_posix(aq_ret,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002339 hw->aq.asq_last_status);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002340 dev_info(&pf->pdev->dev,
Shannon Nelson2d1de822016-05-16 10:26:44 -07002341 "set multi promisc failed on %s, err %s aq_err %s\n",
2342 vsi_name,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002343 i40e_stat_str(hw, aq_ret),
2344 i40e_aq_str(hw, hw->aq.asq_last_status));
Mitch Williamsea02e902015-11-09 15:35:50 -08002345 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002346 }
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002347 if ((changed_flags & IFF_PROMISC) ||
2348 (promisc_changed &&
2349 test_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state))) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002350 bool cur_promisc;
Jesse Brandeburg6995b362015-08-28 17:55:54 -04002351
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002352 cur_promisc = (!!(vsi->current_netdev_flags & IFF_PROMISC) ||
2353 test_bit(__I40E_FILTER_OVERFLOW_PROMISC,
2354 &vsi->state));
Anjali Singhai Jain6784ed52016-01-15 14:33:13 -08002355 if ((vsi->type == I40E_VSI_MAIN) &&
2356 (pf->lan_veb != I40E_NO_VEB) &&
2357 !(pf->flags & I40E_FLAG_MFP_ENABLED)) {
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002358 /* set defport ON for Main VSI instead of true promisc
2359 * this way we will get all unicast/multicast and VLAN
2360 * promisc behavior but will not get VF or VMDq traffic
2361 * replicated on the Main VSI.
2362 */
2363 if (pf->cur_promisc != cur_promisc) {
2364 pf->cur_promisc = cur_promisc;
Mitch Williams5bc16032016-05-16 10:26:43 -07002365 if (cur_promisc)
2366 aq_ret =
2367 i40e_aq_set_default_vsi(hw,
2368 vsi->seid,
2369 NULL);
2370 else
2371 aq_ret =
2372 i40e_aq_clear_default_vsi(hw,
2373 vsi->seid,
2374 NULL);
2375 if (aq_ret) {
2376 retval = i40e_aq_rc_to_posix(aq_ret,
2377 hw->aq.asq_last_status);
2378 dev_info(&pf->pdev->dev,
Shannon Nelson2d1de822016-05-16 10:26:44 -07002379 "Set default VSI failed on %s, err %s, aq_err %s\n",
2380 vsi_name,
Mitch Williams5bc16032016-05-16 10:26:43 -07002381 i40e_stat_str(hw, aq_ret),
2382 i40e_aq_str(hw,
2383 hw->aq.asq_last_status));
2384 }
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002385 }
2386 } else {
Mitch Williamsea02e902015-11-09 15:35:50 -08002387 aq_ret = i40e_aq_set_vsi_unicast_promiscuous(
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002388 hw,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002389 vsi->seid,
Anjali Singhai Jainb5569892016-05-03 15:13:12 -07002390 cur_promisc, NULL,
2391 true);
Mitch Williamsea02e902015-11-09 15:35:50 -08002392 if (aq_ret) {
2393 retval =
2394 i40e_aq_rc_to_posix(aq_ret,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002395 hw->aq.asq_last_status);
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002396 dev_info(&pf->pdev->dev,
Shannon Nelson2d1de822016-05-16 10:26:44 -07002397 "set unicast promisc failed on %s, err %s, aq_err %s\n",
2398 vsi_name,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002399 i40e_stat_str(hw, aq_ret),
2400 i40e_aq_str(hw,
2401 hw->aq.asq_last_status));
Mitch Williamsea02e902015-11-09 15:35:50 -08002402 }
2403 aq_ret = i40e_aq_set_vsi_multicast_promiscuous(
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002404 hw,
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002405 vsi->seid,
2406 cur_promisc, NULL);
Mitch Williamsea02e902015-11-09 15:35:50 -08002407 if (aq_ret) {
2408 retval =
2409 i40e_aq_rc_to_posix(aq_ret,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002410 hw->aq.asq_last_status);
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002411 dev_info(&pf->pdev->dev,
Shannon Nelson2d1de822016-05-16 10:26:44 -07002412 "set multicast promisc failed on %s, err %s, aq_err %s\n",
2413 vsi_name,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002414 i40e_stat_str(hw, aq_ret),
2415 i40e_aq_str(hw,
2416 hw->aq.asq_last_status));
Mitch Williamsea02e902015-11-09 15:35:50 -08002417 }
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002418 }
Mitch Williamsea02e902015-11-09 15:35:50 -08002419 aq_ret = i40e_aq_set_vsi_broadcast(&vsi->back->hw,
2420 vsi->seid,
2421 cur_promisc, NULL);
2422 if (aq_ret) {
2423 retval = i40e_aq_rc_to_posix(aq_ret,
2424 pf->hw.aq.asq_last_status);
Greg Rose1a103702013-11-28 06:42:39 +00002425 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002426 "set brdcast promisc failed, err %s, aq_err %s\n",
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002427 i40e_stat_str(hw, aq_ret),
2428 i40e_aq_str(hw,
2429 hw->aq.asq_last_status));
Mitch Williamsea02e902015-11-09 15:35:50 -08002430 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002431 }
Mitch Williamsea02e902015-11-09 15:35:50 -08002432out:
Jesse Brandeburg2818ccd2016-01-13 16:51:38 -08002433 /* if something went wrong then set the changed flag so we try again */
2434 if (retval)
2435 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2436
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002437 clear_bit(__I40E_CONFIG_BUSY, &vsi->state);
Mitch Williamsea02e902015-11-09 15:35:50 -08002438 return retval;
Jacob Keller4a2ce272016-10-05 09:30:38 -07002439
2440err_no_memory:
2441 /* Restore elements on the temporary add and delete lists */
2442 spin_lock_bh(&vsi->mac_filter_hash_lock);
Alan Brady84f5ca62016-10-05 09:30:39 -07002443err_no_memory_locked:
Jacob Keller671889e2016-12-02 12:33:00 -08002444 i40e_undo_del_filter_entries(vsi, &tmp_del_list);
2445 i40e_undo_add_filter_entries(vsi, &tmp_add_list);
Jacob Keller4a2ce272016-10-05 09:30:38 -07002446 spin_unlock_bh(&vsi->mac_filter_hash_lock);
2447
2448 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2449 clear_bit(__I40E_CONFIG_BUSY, &vsi->state);
2450 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002451}
2452
2453/**
2454 * i40e_sync_filters_subtask - Sync the VSI filter list with HW
2455 * @pf: board private structure
2456 **/
2457static void i40e_sync_filters_subtask(struct i40e_pf *pf)
2458{
2459 int v;
2460
2461 if (!pf || !(pf->flags & I40E_FLAG_FILTER_SYNC))
2462 return;
2463 pf->flags &= ~I40E_FLAG_FILTER_SYNC;
2464
Mitch Williams505682c2014-05-20 08:01:37 +00002465 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002466 if (pf->vsi[v] &&
Jesse Brandeburg17652c62015-11-05 17:01:02 -08002467 (pf->vsi[v]->flags & I40E_VSI_FLAG_FILTER_CHANGED)) {
2468 int ret = i40e_sync_vsi_filters(pf->vsi[v]);
2469
2470 if (ret) {
2471 /* come back and try again later */
2472 pf->flags |= I40E_FLAG_FILTER_SYNC;
2473 break;
2474 }
2475 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002476 }
2477}
2478
2479/**
2480 * i40e_change_mtu - NDO callback to change the Maximum Transfer Unit
2481 * @netdev: network interface device structure
2482 * @new_mtu: new value for maximum frame size
2483 *
2484 * Returns 0 on success, negative on failure
2485 **/
2486static int i40e_change_mtu(struct net_device *netdev, int new_mtu)
2487{
2488 struct i40e_netdev_priv *np = netdev_priv(netdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002489 struct i40e_vsi *vsi = np->vsi;
Mitch Williams0ef2d5a2017-01-24 10:24:00 -08002490 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002491
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002492 netdev_info(netdev, "changing MTU from %d to %d\n",
2493 netdev->mtu, new_mtu);
2494 netdev->mtu = new_mtu;
2495 if (netif_running(netdev))
2496 i40e_vsi_reinit_locked(vsi);
Mitch Williams0ef2d5a2017-01-24 10:24:00 -08002497 pf->flags |= (I40E_FLAG_SERVICE_CLIENT_REQUESTED |
2498 I40E_FLAG_CLIENT_L2_CHANGE);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002499 return 0;
2500}
2501
2502/**
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00002503 * i40e_ioctl - Access the hwtstamp interface
2504 * @netdev: network interface device structure
2505 * @ifr: interface request data
2506 * @cmd: ioctl command
2507 **/
2508int i40e_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
2509{
2510 struct i40e_netdev_priv *np = netdev_priv(netdev);
2511 struct i40e_pf *pf = np->vsi->back;
2512
2513 switch (cmd) {
2514 case SIOCGHWTSTAMP:
2515 return i40e_ptp_get_ts_config(pf, ifr);
2516 case SIOCSHWTSTAMP:
2517 return i40e_ptp_set_ts_config(pf, ifr);
2518 default:
2519 return -EOPNOTSUPP;
2520 }
2521}
2522
2523/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002524 * i40e_vlan_stripping_enable - Turn on vlan stripping for the VSI
2525 * @vsi: the vsi being adjusted
2526 **/
2527void i40e_vlan_stripping_enable(struct i40e_vsi *vsi)
2528{
2529 struct i40e_vsi_context ctxt;
2530 i40e_status ret;
2531
2532 if ((vsi->info.valid_sections &
2533 cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
2534 ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_MODE_MASK) == 0))
2535 return; /* already enabled */
2536
2537 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2538 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
2539 I40E_AQ_VSI_PVLAN_EMOD_STR_BOTH;
2540
2541 ctxt.seid = vsi->seid;
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07002542 ctxt.info = vsi->info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002543 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2544 if (ret) {
2545 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002546 "update vlan stripping failed, err %s aq_err %s\n",
2547 i40e_stat_str(&vsi->back->hw, ret),
2548 i40e_aq_str(&vsi->back->hw,
2549 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002550 }
2551}
2552
2553/**
2554 * i40e_vlan_stripping_disable - Turn off vlan stripping for the VSI
2555 * @vsi: the vsi being adjusted
2556 **/
2557void i40e_vlan_stripping_disable(struct i40e_vsi *vsi)
2558{
2559 struct i40e_vsi_context ctxt;
2560 i40e_status ret;
2561
2562 if ((vsi->info.valid_sections &
2563 cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
2564 ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_EMOD_MASK) ==
2565 I40E_AQ_VSI_PVLAN_EMOD_MASK))
2566 return; /* already disabled */
2567
2568 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2569 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
2570 I40E_AQ_VSI_PVLAN_EMOD_NOTHING;
2571
2572 ctxt.seid = vsi->seid;
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07002573 ctxt.info = vsi->info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002574 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2575 if (ret) {
2576 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002577 "update vlan stripping failed, err %s aq_err %s\n",
2578 i40e_stat_str(&vsi->back->hw, ret),
2579 i40e_aq_str(&vsi->back->hw,
2580 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002581 }
2582}
2583
2584/**
2585 * i40e_vlan_rx_register - Setup or shutdown vlan offload
2586 * @netdev: network interface to be adjusted
2587 * @features: netdev features to test if VLAN offload is enabled or not
2588 **/
2589static void i40e_vlan_rx_register(struct net_device *netdev, u32 features)
2590{
2591 struct i40e_netdev_priv *np = netdev_priv(netdev);
2592 struct i40e_vsi *vsi = np->vsi;
2593
2594 if (features & NETIF_F_HW_VLAN_CTAG_RX)
2595 i40e_vlan_stripping_enable(vsi);
2596 else
2597 i40e_vlan_stripping_disable(vsi);
2598}
2599
2600/**
Jacob Keller490a4ad2016-11-11 12:39:29 -08002601 * i40e_add_vlan_all_mac - Add a MAC/VLAN filter for each existing MAC address
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002602 * @vsi: the vsi being configured
2603 * @vid: vlan id to be added (0 = untagged only , -1 = any)
Jacob Keller490a4ad2016-11-11 12:39:29 -08002604 *
2605 * This is a helper function for adding a new MAC/VLAN filter with the
2606 * specified VLAN for each existing MAC address already in the hash table.
2607 * This function does *not* perform any accounting to update filters based on
2608 * VLAN mode.
2609 *
2610 * NOTE: this function expects to be called while under the
2611 * mac_filter_hash_lock
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002612 **/
Jacob Keller9af52f62016-11-11 12:39:30 -08002613int i40e_add_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002614{
Jacob Keller490a4ad2016-11-11 12:39:29 -08002615 struct i40e_mac_filter *f, *add_f;
Jacob Keller278e7d02016-10-05 09:30:37 -07002616 struct hlist_node *h;
2617 int bkt;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002618
Jacob Keller278e7d02016-10-05 09:30:37 -07002619 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Jacob Keller57b341d2016-10-05 09:30:35 -07002620 if (f->state == I40E_FILTER_REMOVE)
2621 continue;
Jacob Keller1bc87e82016-10-05 09:30:31 -07002622 add_f = i40e_add_filter(vsi, f->macaddr, vid);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002623 if (!add_f) {
2624 dev_info(&vsi->back->pdev->dev,
2625 "Could not add vlan filter %d for %pM\n",
2626 vid, f->macaddr);
2627 return -ENOMEM;
2628 }
2629 }
2630
Jacob Keller490a4ad2016-11-11 12:39:29 -08002631 return 0;
2632}
2633
2634/**
2635 * i40e_vsi_add_vlan - Add VSI membership for given VLAN
2636 * @vsi: the VSI being configured
Jacob Kellerf94484b2016-12-07 14:05:34 -08002637 * @vid: VLAN id to be added
Jacob Keller490a4ad2016-11-11 12:39:29 -08002638 **/
Jacob Kellerf94484b2016-12-07 14:05:34 -08002639int i40e_vsi_add_vlan(struct i40e_vsi *vsi, u16 vid)
Jacob Keller490a4ad2016-11-11 12:39:29 -08002640{
Jacob Keller489a3262016-11-11 12:39:31 -08002641 int err;
Jacob Keller490a4ad2016-11-11 12:39:29 -08002642
Jacob Kellerf94484b2016-12-07 14:05:34 -08002643 if (!vid || vsi->info.pvid)
2644 return -EINVAL;
2645
Jacob Keller490a4ad2016-11-11 12:39:29 -08002646 /* Locked once because all functions invoked below iterates list*/
2647 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller490a4ad2016-11-11 12:39:29 -08002648 err = i40e_add_vlan_all_mac(vsi, vid);
Jacob Keller278e7d02016-10-05 09:30:37 -07002649 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller489a3262016-11-11 12:39:31 -08002650 if (err)
2651 return err;
Kiran Patil21659032015-09-30 14:09:03 -04002652
Jesse Brandeburg0e4425e2015-11-05 17:01:01 -08002653 /* schedule our worker thread which will take care of
2654 * applying the new filter changes
2655 */
2656 i40e_service_event_schedule(vsi->back);
2657 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002658}
2659
2660/**
Jacob Keller490a4ad2016-11-11 12:39:29 -08002661 * i40e_rm_vlan_all_mac - Remove MAC/VLAN pair for all MAC with the given VLAN
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002662 * @vsi: the vsi being configured
2663 * @vid: vlan id to be removed (0 = untagged only , -1 = any)
Jacob Keller490a4ad2016-11-11 12:39:29 -08002664 *
2665 * This function should be used to remove all VLAN filters which match the
2666 * given VID. It does not schedule the service event and does not take the
2667 * mac_filter_hash_lock so it may be combined with other operations under
2668 * a single invocation of the mac_filter_hash_lock.
2669 *
2670 * NOTE: this function expects to be called while under the
2671 * mac_filter_hash_lock
2672 */
Jacob Keller9af52f62016-11-11 12:39:30 -08002673void i40e_rm_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002674{
Alan Brady84f5ca62016-10-05 09:30:39 -07002675 struct i40e_mac_filter *f;
Jacob Keller278e7d02016-10-05 09:30:37 -07002676 struct hlist_node *h;
Jacob Keller278e7d02016-10-05 09:30:37 -07002677 int bkt;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002678
Jacob Keller278e7d02016-10-05 09:30:37 -07002679 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Jacob Keller290d2552016-10-05 09:30:36 -07002680 if (f->vlan == vid)
2681 __i40e_del_filter(vsi, f);
2682 }
Jacob Keller490a4ad2016-11-11 12:39:29 -08002683}
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002684
Jacob Keller490a4ad2016-11-11 12:39:29 -08002685/**
2686 * i40e_vsi_kill_vlan - Remove VSI membership for given VLAN
2687 * @vsi: the VSI being configured
Jacob Kellerf94484b2016-12-07 14:05:34 -08002688 * @vid: VLAN id to be removed
Jacob Keller490a4ad2016-11-11 12:39:29 -08002689 **/
Jacob Kellerf94484b2016-12-07 14:05:34 -08002690void i40e_vsi_kill_vlan(struct i40e_vsi *vsi, u16 vid)
Jacob Keller490a4ad2016-11-11 12:39:29 -08002691{
Jacob Kellerf94484b2016-12-07 14:05:34 -08002692 if (!vid || vsi->info.pvid)
2693 return;
2694
Jacob Keller490a4ad2016-11-11 12:39:29 -08002695 spin_lock_bh(&vsi->mac_filter_hash_lock);
2696 i40e_rm_vlan_all_mac(vsi, vid);
Jacob Keller278e7d02016-10-05 09:30:37 -07002697 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -04002698
Jesse Brandeburg0e4425e2015-11-05 17:01:01 -08002699 /* schedule our worker thread which will take care of
2700 * applying the new filter changes
2701 */
2702 i40e_service_event_schedule(vsi->back);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002703}
2704
2705/**
2706 * i40e_vlan_rx_add_vid - Add a vlan id filter to HW offload
2707 * @netdev: network interface to be adjusted
2708 * @vid: vlan id to be added
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002709 *
2710 * net_device_ops implementation for adding vlan ids
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002711 **/
Vasu Dev38e00432014-08-01 13:27:03 -07002712#ifdef I40E_FCOE
2713int i40e_vlan_rx_add_vid(struct net_device *netdev,
2714 __always_unused __be16 proto, u16 vid)
2715#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002716static int i40e_vlan_rx_add_vid(struct net_device *netdev,
2717 __always_unused __be16 proto, u16 vid)
Vasu Dev38e00432014-08-01 13:27:03 -07002718#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002719{
2720 struct i40e_netdev_priv *np = netdev_priv(netdev);
2721 struct i40e_vsi *vsi = np->vsi;
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002722 int ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002723
Jacob Keller6a1127852016-10-25 16:08:49 -07002724 if (vid >= VLAN_N_VID)
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002725 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002726
Anjali Singhai Jain6982d422014-02-06 05:51:10 +00002727 /* If the network stack called us with vid = 0 then
2728 * it is asking to receive priority tagged packets with
2729 * vlan id 0. Our HW receives them by default when configured
2730 * to receive untagged packets so there is no need to add an
2731 * extra filter for vlan 0 tagged packets.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002732 */
Anjali Singhai Jain6982d422014-02-06 05:51:10 +00002733 if (vid)
2734 ret = i40e_vsi_add_vlan(vsi, vid);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002735
Jacob Keller6a1127852016-10-25 16:08:49 -07002736 if (!ret)
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002737 set_bit(vid, vsi->active_vlans);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002738
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002739 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002740}
2741
2742/**
2743 * i40e_vlan_rx_kill_vid - Remove a vlan id filter from HW offload
2744 * @netdev: network interface to be adjusted
2745 * @vid: vlan id to be removed
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002746 *
Akeem G Abodunrinfdfd9432014-02-11 08:24:15 +00002747 * net_device_ops implementation for removing vlan ids
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002748 **/
Vasu Dev38e00432014-08-01 13:27:03 -07002749#ifdef I40E_FCOE
2750int i40e_vlan_rx_kill_vid(struct net_device *netdev,
2751 __always_unused __be16 proto, u16 vid)
2752#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002753static int i40e_vlan_rx_kill_vid(struct net_device *netdev,
2754 __always_unused __be16 proto, u16 vid)
Vasu Dev38e00432014-08-01 13:27:03 -07002755#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002756{
2757 struct i40e_netdev_priv *np = netdev_priv(netdev);
2758 struct i40e_vsi *vsi = np->vsi;
2759
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002760 /* return code is ignored as there is nothing a user
2761 * can do about failure to remove and a log message was
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002762 * already printed from the other function
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002763 */
2764 i40e_vsi_kill_vlan(vsi, vid);
2765
2766 clear_bit(vid, vsi->active_vlans);
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002767
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002768 return 0;
2769}
2770
2771/**
Tushar Daveb1b15df2016-07-01 10:11:20 -07002772 * i40e_macaddr_init - explicitly write the mac address filters
2773 *
2774 * @vsi: pointer to the vsi
2775 * @macaddr: the MAC address
2776 *
2777 * This is needed when the macaddr has been obtained by other
2778 * means than the default, e.g., from Open Firmware or IDPROM.
2779 * Returns 0 on success, negative on failure
2780 **/
2781static int i40e_macaddr_init(struct i40e_vsi *vsi, u8 *macaddr)
2782{
2783 int ret;
2784 struct i40e_aqc_add_macvlan_element_data element;
2785
2786 ret = i40e_aq_mac_address_write(&vsi->back->hw,
2787 I40E_AQC_WRITE_TYPE_LAA_WOL,
2788 macaddr, NULL);
2789 if (ret) {
2790 dev_info(&vsi->back->pdev->dev,
2791 "Addr change for VSI failed: %d\n", ret);
2792 return -EADDRNOTAVAIL;
2793 }
2794
2795 memset(&element, 0, sizeof(element));
2796 ether_addr_copy(element.mac_addr, macaddr);
2797 element.flags = cpu_to_le16(I40E_AQC_MACVLAN_ADD_PERFECT_MATCH);
2798 ret = i40e_aq_add_macvlan(&vsi->back->hw, vsi->seid, &element, 1, NULL);
2799 if (ret) {
2800 dev_info(&vsi->back->pdev->dev,
2801 "add filter failed err %s aq_err %s\n",
2802 i40e_stat_str(&vsi->back->hw, ret),
2803 i40e_aq_str(&vsi->back->hw,
2804 vsi->back->hw.aq.asq_last_status));
2805 }
2806 return ret;
2807}
2808
2809/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002810 * i40e_restore_vlan - Reinstate vlans when vsi/netdev comes back up
2811 * @vsi: the vsi being brought back up
2812 **/
2813static void i40e_restore_vlan(struct i40e_vsi *vsi)
2814{
2815 u16 vid;
2816
2817 if (!vsi->netdev)
2818 return;
2819
2820 i40e_vlan_rx_register(vsi->netdev, vsi->netdev->features);
2821
2822 for_each_set_bit(vid, vsi->active_vlans, VLAN_N_VID)
2823 i40e_vlan_rx_add_vid(vsi->netdev, htons(ETH_P_8021Q),
2824 vid);
2825}
2826
2827/**
2828 * i40e_vsi_add_pvid - Add pvid for the VSI
2829 * @vsi: the vsi being adjusted
2830 * @vid: the vlan id to set as a PVID
2831 **/
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002832int i40e_vsi_add_pvid(struct i40e_vsi *vsi, u16 vid)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002833{
2834 struct i40e_vsi_context ctxt;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002835 i40e_status ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002836
2837 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2838 vsi->info.pvid = cpu_to_le16(vid);
Greg Rose6c12fcb2013-11-28 06:39:34 +00002839 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_TAGGED |
2840 I40E_AQ_VSI_PVLAN_INSERT_PVID |
Greg Roseb774c7d2013-11-28 06:39:44 +00002841 I40E_AQ_VSI_PVLAN_EMOD_STR;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002842
2843 ctxt.seid = vsi->seid;
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07002844 ctxt.info = vsi->info;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002845 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2846 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002847 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002848 "add pvid failed, err %s aq_err %s\n",
2849 i40e_stat_str(&vsi->back->hw, ret),
2850 i40e_aq_str(&vsi->back->hw,
2851 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002852 return -ENOENT;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002853 }
2854
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002855 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002856}
2857
2858/**
2859 * i40e_vsi_remove_pvid - Remove the pvid from the VSI
2860 * @vsi: the vsi being adjusted
2861 *
2862 * Just use the vlan_rx_register() service to put it back to normal
2863 **/
2864void i40e_vsi_remove_pvid(struct i40e_vsi *vsi)
2865{
Greg Rose6c12fcb2013-11-28 06:39:34 +00002866 i40e_vlan_stripping_disable(vsi);
2867
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002868 vsi->info.pvid = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002869}
2870
2871/**
2872 * i40e_vsi_setup_tx_resources - Allocate VSI Tx queue resources
2873 * @vsi: ptr to the VSI
2874 *
2875 * If this function returns with an error, then it's possible one or
2876 * more of the rings is populated (while the rest are not). It is the
2877 * callers duty to clean those orphaned rings.
2878 *
2879 * Return 0 on success, negative on failure
2880 **/
2881static int i40e_vsi_setup_tx_resources(struct i40e_vsi *vsi)
2882{
2883 int i, err = 0;
2884
2885 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e152013-09-28 06:00:58 +00002886 err = i40e_setup_tx_descriptors(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002887
2888 return err;
2889}
2890
2891/**
2892 * i40e_vsi_free_tx_resources - Free Tx resources for VSI queues
2893 * @vsi: ptr to the VSI
2894 *
2895 * Free VSI's transmit software resources
2896 **/
2897static void i40e_vsi_free_tx_resources(struct i40e_vsi *vsi)
2898{
2899 int i;
2900
Greg Rose8e9dca52013-12-18 13:45:53 +00002901 if (!vsi->tx_rings)
2902 return;
2903
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002904 for (i = 0; i < vsi->num_queue_pairs; i++)
Greg Rose8e9dca52013-12-18 13:45:53 +00002905 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
Alexander Duyck9f65e152013-09-28 06:00:58 +00002906 i40e_free_tx_resources(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002907}
2908
2909/**
2910 * i40e_vsi_setup_rx_resources - Allocate VSI queues Rx resources
2911 * @vsi: ptr to the VSI
2912 *
2913 * If this function returns with an error, then it's possible one or
2914 * more of the rings is populated (while the rest are not). It is the
2915 * callers duty to clean those orphaned rings.
2916 *
2917 * Return 0 on success, negative on failure
2918 **/
2919static int i40e_vsi_setup_rx_resources(struct i40e_vsi *vsi)
2920{
2921 int i, err = 0;
2922
2923 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e152013-09-28 06:00:58 +00002924 err = i40e_setup_rx_descriptors(vsi->rx_rings[i]);
Vasu Dev38e00432014-08-01 13:27:03 -07002925#ifdef I40E_FCOE
2926 i40e_fcoe_setup_ddp_resources(vsi);
2927#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002928 return err;
2929}
2930
2931/**
2932 * i40e_vsi_free_rx_resources - Free Rx Resources for VSI queues
2933 * @vsi: ptr to the VSI
2934 *
2935 * Free all receive software resources
2936 **/
2937static void i40e_vsi_free_rx_resources(struct i40e_vsi *vsi)
2938{
2939 int i;
2940
Greg Rose8e9dca52013-12-18 13:45:53 +00002941 if (!vsi->rx_rings)
2942 return;
2943
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002944 for (i = 0; i < vsi->num_queue_pairs; i++)
Greg Rose8e9dca52013-12-18 13:45:53 +00002945 if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
Alexander Duyck9f65e152013-09-28 06:00:58 +00002946 i40e_free_rx_resources(vsi->rx_rings[i]);
Vasu Dev38e00432014-08-01 13:27:03 -07002947#ifdef I40E_FCOE
2948 i40e_fcoe_free_ddp_resources(vsi);
2949#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002950}
2951
2952/**
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002953 * i40e_config_xps_tx_ring - Configure XPS for a Tx ring
2954 * @ring: The Tx ring to configure
2955 *
2956 * This enables/disables XPS for a given Tx descriptor ring
2957 * based on the TCs enabled for the VSI that ring belongs to.
2958 **/
2959static void i40e_config_xps_tx_ring(struct i40e_ring *ring)
2960{
2961 struct i40e_vsi *vsi = ring->vsi;
2962 cpumask_var_t mask;
2963
Jesse Brandeburg9a660ee2015-02-26 16:13:22 +00002964 if (!ring->q_vector || !ring->netdev)
2965 return;
2966
2967 /* Single TC mode enable XPS */
2968 if (vsi->tc_config.numtc <= 1) {
2969 if (!test_and_set_bit(__I40E_TX_XPS_INIT_DONE, &ring->state))
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002970 netif_set_xps_queue(ring->netdev,
2971 &ring->q_vector->affinity_mask,
2972 ring->queue_index);
Jesse Brandeburg9a660ee2015-02-26 16:13:22 +00002973 } else if (alloc_cpumask_var(&mask, GFP_KERNEL)) {
2974 /* Disable XPS to allow selection based on TC */
2975 bitmap_zero(cpumask_bits(mask), nr_cpumask_bits);
2976 netif_set_xps_queue(ring->netdev, mask, ring->queue_index);
2977 free_cpumask_var(mask);
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002978 }
Jesse Brandeburg0e4425e2015-11-05 17:01:01 -08002979
2980 /* schedule our worker thread which will take care of
2981 * applying the new filter changes
2982 */
2983 i40e_service_event_schedule(vsi->back);
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002984}
2985
2986/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002987 * i40e_configure_tx_ring - Configure a transmit ring context and rest
2988 * @ring: The Tx ring to configure
2989 *
2990 * Configure the Tx descriptor ring in the HMC context.
2991 **/
2992static int i40e_configure_tx_ring(struct i40e_ring *ring)
2993{
2994 struct i40e_vsi *vsi = ring->vsi;
2995 u16 pf_q = vsi->base_queue + ring->queue_index;
2996 struct i40e_hw *hw = &vsi->back->hw;
2997 struct i40e_hmc_obj_txq tx_ctx;
2998 i40e_status err = 0;
2999 u32 qtx_ctl = 0;
3000
3001 /* some ATR related tx ring init */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08003002 if (vsi->back->flags & I40E_FLAG_FD_ATR_ENABLED) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003003 ring->atr_sample_rate = vsi->back->atr_sample_rate;
3004 ring->atr_count = 0;
3005 } else {
3006 ring->atr_sample_rate = 0;
3007 }
3008
Neerav Parikh3ffa0372014-11-12 00:19:02 +00003009 /* configure XPS */
3010 i40e_config_xps_tx_ring(ring);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003011
3012 /* clear the context structure first */
3013 memset(&tx_ctx, 0, sizeof(tx_ctx));
3014
3015 tx_ctx.new_context = 1;
3016 tx_ctx.base = (ring->dma / 128);
3017 tx_ctx.qlen = ring->count;
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08003018 tx_ctx.fd_ena = !!(vsi->back->flags & (I40E_FLAG_FD_SB_ENABLED |
3019 I40E_FLAG_FD_ATR_ENABLED));
Vasu Dev38e00432014-08-01 13:27:03 -07003020#ifdef I40E_FCOE
3021 tx_ctx.fc_ena = (vsi->type == I40E_VSI_FCOE);
3022#endif
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003023 tx_ctx.timesync_ena = !!(vsi->back->flags & I40E_FLAG_PTP);
Jesse Brandeburg1943d8b2014-02-14 02:14:40 +00003024 /* FDIR VSI tx ring can still use RS bit and writebacks */
3025 if (vsi->type != I40E_VSI_FDIR)
3026 tx_ctx.head_wb_ena = 1;
3027 tx_ctx.head_wb_addr = ring->dma +
3028 (ring->count * sizeof(struct i40e_tx_desc));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003029
3030 /* As part of VSI creation/update, FW allocates certain
3031 * Tx arbitration queue sets for each TC enabled for
3032 * the VSI. The FW returns the handles to these queue
3033 * sets as part of the response buffer to Add VSI,
3034 * Update VSI, etc. AQ commands. It is expected that
3035 * these queue set handles be associated with the Tx
3036 * queues by the driver as part of the TX queue context
3037 * initialization. This has to be done regardless of
3038 * DCB as by default everything is mapped to TC0.
3039 */
3040 tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[ring->dcb_tc]);
3041 tx_ctx.rdylist_act = 0;
3042
3043 /* clear the context in the HMC */
3044 err = i40e_clear_lan_tx_queue_context(hw, pf_q);
3045 if (err) {
3046 dev_info(&vsi->back->pdev->dev,
3047 "Failed to clear LAN Tx queue context on Tx ring %d (pf_q %d), error: %d\n",
3048 ring->queue_index, pf_q, err);
3049 return -ENOMEM;
3050 }
3051
3052 /* set the context in the HMC */
3053 err = i40e_set_lan_tx_queue_context(hw, pf_q, &tx_ctx);
3054 if (err) {
3055 dev_info(&vsi->back->pdev->dev,
3056 "Failed to set LAN Tx queue context on Tx ring %d (pf_q %d, error: %d\n",
3057 ring->queue_index, pf_q, err);
3058 return -ENOMEM;
3059 }
3060
3061 /* Now associate this queue with this PCI function */
Mitch Williams7a28d882014-10-17 03:14:52 +00003062 if (vsi->type == I40E_VSI_VMDQ2) {
Shannon Nelson9d8bf542014-01-14 00:49:50 -08003063 qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
Mitch Williams7a28d882014-10-17 03:14:52 +00003064 qtx_ctl |= ((vsi->id) << I40E_QTX_CTL_VFVM_INDX_SHIFT) &
3065 I40E_QTX_CTL_VFVM_INDX_MASK;
3066 } else {
Shannon Nelson9d8bf542014-01-14 00:49:50 -08003067 qtx_ctl = I40E_QTX_CTL_PF_QUEUE;
Mitch Williams7a28d882014-10-17 03:14:52 +00003068 }
3069
Shannon Nelson13fd977492013-09-28 07:14:19 +00003070 qtx_ctl |= ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT) &
3071 I40E_QTX_CTL_PF_INDX_MASK);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003072 wr32(hw, I40E_QTX_CTL(pf_q), qtx_ctl);
3073 i40e_flush(hw);
3074
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003075 /* cache tail off for easier writes later */
3076 ring->tail = hw->hw_addr + I40E_QTX_TAIL(pf_q);
3077
3078 return 0;
3079}
3080
3081/**
3082 * i40e_configure_rx_ring - Configure a receive ring context
3083 * @ring: The Rx ring to configure
3084 *
3085 * Configure the Rx descriptor ring in the HMC context.
3086 **/
3087static int i40e_configure_rx_ring(struct i40e_ring *ring)
3088{
3089 struct i40e_vsi *vsi = ring->vsi;
3090 u32 chain_len = vsi->back->hw.func_caps.rx_buf_chain_len;
3091 u16 pf_q = vsi->base_queue + ring->queue_index;
3092 struct i40e_hw *hw = &vsi->back->hw;
3093 struct i40e_hmc_obj_rxq rx_ctx;
3094 i40e_status err = 0;
3095
3096 ring->state = 0;
3097
3098 /* clear the context structure first */
3099 memset(&rx_ctx, 0, sizeof(rx_ctx));
3100
3101 ring->rx_buf_len = vsi->rx_buf_len;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003102
3103 rx_ctx.dbuff = ring->rx_buf_len >> I40E_RXQ_CTX_DBUFF_SHIFT;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003104
3105 rx_ctx.base = (ring->dma / 128);
3106 rx_ctx.qlen = ring->count;
3107
Jesse Brandeburgbec60fc2016-04-18 11:33:47 -07003108 /* use 32 byte descriptors */
3109 rx_ctx.dsize = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003110
Jesse Brandeburgbec60fc2016-04-18 11:33:47 -07003111 /* descriptor type is always zero
3112 * rx_ctx.dtype = 0;
3113 */
Jesse Brandeburgb32bfa12016-04-18 11:33:42 -07003114 rx_ctx.hsplit_0 = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003115
Jesse Brandeburgb32bfa12016-04-18 11:33:42 -07003116 rx_ctx.rxmax = min_t(u16, vsi->max_frame, chain_len * ring->rx_buf_len);
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00003117 if (hw->revision_id == 0)
3118 rx_ctx.lrxqthresh = 0;
3119 else
3120 rx_ctx.lrxqthresh = 2;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003121 rx_ctx.crcstrip = 1;
3122 rx_ctx.l2tsel = 1;
Jesse Brandeburgc4bbac32015-09-28 11:21:48 -07003123 /* this controls whether VLAN is stripped from inner headers */
3124 rx_ctx.showiv = 0;
Vasu Dev38e00432014-08-01 13:27:03 -07003125#ifdef I40E_FCOE
3126 rx_ctx.fc_ena = (vsi->type == I40E_VSI_FCOE);
3127#endif
Catherine Sullivanacb36762014-03-06 09:02:30 +00003128 /* set the prefena field to 1 because the manual says to */
3129 rx_ctx.prefena = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003130
3131 /* clear the context in the HMC */
3132 err = i40e_clear_lan_rx_queue_context(hw, pf_q);
3133 if (err) {
3134 dev_info(&vsi->back->pdev->dev,
3135 "Failed to clear LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
3136 ring->queue_index, pf_q, err);
3137 return -ENOMEM;
3138 }
3139
3140 /* set the context in the HMC */
3141 err = i40e_set_lan_rx_queue_context(hw, pf_q, &rx_ctx);
3142 if (err) {
3143 dev_info(&vsi->back->pdev->dev,
3144 "Failed to set LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
3145 ring->queue_index, pf_q, err);
3146 return -ENOMEM;
3147 }
3148
3149 /* cache tail for quicker writes, and clear the reg before use */
3150 ring->tail = hw->hw_addr + I40E_QRX_TAIL(pf_q);
3151 writel(0, ring->tail);
3152
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07003153 i40e_alloc_rx_buffers(ring, I40E_DESC_UNUSED(ring));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003154
3155 return 0;
3156}
3157
3158/**
3159 * i40e_vsi_configure_tx - Configure the VSI for Tx
3160 * @vsi: VSI structure describing this set of rings and resources
3161 *
3162 * Configure the Tx VSI for operation.
3163 **/
3164static int i40e_vsi_configure_tx(struct i40e_vsi *vsi)
3165{
3166 int err = 0;
3167 u16 i;
3168
Alexander Duyck9f65e152013-09-28 06:00:58 +00003169 for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
3170 err = i40e_configure_tx_ring(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003171
3172 return err;
3173}
3174
3175/**
3176 * i40e_vsi_configure_rx - Configure the VSI for Rx
3177 * @vsi: the VSI being configured
3178 *
3179 * Configure the Rx VSI for operation.
3180 **/
3181static int i40e_vsi_configure_rx(struct i40e_vsi *vsi)
3182{
3183 int err = 0;
3184 u16 i;
3185
3186 if (vsi->netdev && (vsi->netdev->mtu > ETH_DATA_LEN))
3187 vsi->max_frame = vsi->netdev->mtu + ETH_HLEN
3188 + ETH_FCS_LEN + VLAN_HLEN;
3189 else
3190 vsi->max_frame = I40E_RXBUFFER_2048;
3191
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07003192 vsi->rx_buf_len = I40E_RXBUFFER_2048;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003193
Vasu Dev38e00432014-08-01 13:27:03 -07003194#ifdef I40E_FCOE
3195 /* setup rx buffer for FCoE */
3196 if ((vsi->type == I40E_VSI_FCOE) &&
3197 (vsi->back->flags & I40E_FLAG_FCOE_ENABLED)) {
Vasu Dev38e00432014-08-01 13:27:03 -07003198 vsi->rx_buf_len = I40E_RXBUFFER_3072;
3199 vsi->max_frame = I40E_RXBUFFER_3072;
Vasu Dev38e00432014-08-01 13:27:03 -07003200 }
3201
3202#endif /* I40E_FCOE */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003203 /* round up for the chip's needs */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003204 vsi->rx_buf_len = ALIGN(vsi->rx_buf_len,
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04003205 BIT_ULL(I40E_RXQ_CTX_DBUFF_SHIFT));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003206
3207 /* set up individual rings */
3208 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e152013-09-28 06:00:58 +00003209 err = i40e_configure_rx_ring(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003210
3211 return err;
3212}
3213
3214/**
3215 * i40e_vsi_config_dcb_rings - Update rings to reflect DCB TC
3216 * @vsi: ptr to the VSI
3217 **/
3218static void i40e_vsi_config_dcb_rings(struct i40e_vsi *vsi)
3219{
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +00003220 struct i40e_ring *tx_ring, *rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003221 u16 qoffset, qcount;
3222 int i, n;
3223
Parikh, Neeravcd238a32015-02-21 06:43:37 +00003224 if (!(vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
3225 /* Reset the TC information */
3226 for (i = 0; i < vsi->num_queue_pairs; i++) {
3227 rx_ring = vsi->rx_rings[i];
3228 tx_ring = vsi->tx_rings[i];
3229 rx_ring->dcb_tc = 0;
3230 tx_ring->dcb_tc = 0;
3231 }
3232 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003233
3234 for (n = 0; n < I40E_MAX_TRAFFIC_CLASS; n++) {
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04003235 if (!(vsi->tc_config.enabled_tc & BIT_ULL(n)))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003236 continue;
3237
3238 qoffset = vsi->tc_config.tc_info[n].qoffset;
3239 qcount = vsi->tc_config.tc_info[n].qcount;
3240 for (i = qoffset; i < (qoffset + qcount); i++) {
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +00003241 rx_ring = vsi->rx_rings[i];
3242 tx_ring = vsi->tx_rings[i];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003243 rx_ring->dcb_tc = n;
3244 tx_ring->dcb_tc = n;
3245 }
3246 }
3247}
3248
3249/**
3250 * i40e_set_vsi_rx_mode - Call set_rx_mode on a VSI
3251 * @vsi: ptr to the VSI
3252 **/
3253static void i40e_set_vsi_rx_mode(struct i40e_vsi *vsi)
3254{
Tushar Daveb1b15df2016-07-01 10:11:20 -07003255 struct i40e_pf *pf = vsi->back;
3256 int err;
3257
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003258 if (vsi->netdev)
3259 i40e_set_rx_mode(vsi->netdev);
Tushar Daveb1b15df2016-07-01 10:11:20 -07003260
3261 if (!!(pf->flags & I40E_FLAG_PF_MAC)) {
3262 err = i40e_macaddr_init(vsi, pf->hw.mac.addr);
3263 if (err) {
3264 dev_warn(&pf->pdev->dev,
3265 "could not set up macaddr; err %d\n", err);
3266 }
3267 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003268}
3269
3270/**
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00003271 * i40e_fdir_filter_restore - Restore the Sideband Flow Director filters
3272 * @vsi: Pointer to the targeted VSI
3273 *
3274 * This function replays the hlist on the hw where all the SB Flow Director
3275 * filters were saved.
3276 **/
3277static void i40e_fdir_filter_restore(struct i40e_vsi *vsi)
3278{
3279 struct i40e_fdir_filter *filter;
3280 struct i40e_pf *pf = vsi->back;
3281 struct hlist_node *node;
3282
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00003283 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
3284 return;
3285
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00003286 hlist_for_each_entry_safe(filter, node,
3287 &pf->fdir_filter_list, fdir_node) {
3288 i40e_add_del_fdir(vsi, filter, true);
3289 }
3290}
3291
3292/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003293 * i40e_vsi_configure - Set up the VSI for action
3294 * @vsi: the VSI being configured
3295 **/
3296static int i40e_vsi_configure(struct i40e_vsi *vsi)
3297{
3298 int err;
3299
3300 i40e_set_vsi_rx_mode(vsi);
3301 i40e_restore_vlan(vsi);
3302 i40e_vsi_config_dcb_rings(vsi);
3303 err = i40e_vsi_configure_tx(vsi);
3304 if (!err)
3305 err = i40e_vsi_configure_rx(vsi);
3306
3307 return err;
3308}
3309
3310/**
3311 * i40e_vsi_configure_msix - MSIX mode Interrupt Config in the HW
3312 * @vsi: the VSI being configured
3313 **/
3314static void i40e_vsi_configure_msix(struct i40e_vsi *vsi)
3315{
3316 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003317 struct i40e_hw *hw = &pf->hw;
3318 u16 vector;
3319 int i, q;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003320 u32 qp;
3321
3322 /* The interrupt indexing is offset by 1 in the PFINT_ITRn
3323 * and PFINT_LNKLSTn registers, e.g.:
3324 * PFINT_ITRn[0..n-1] gets msix-1..msix-n (qpair interrupts)
3325 */
3326 qp = vsi->base_queue;
3327 vector = vsi->base_vector;
Alexander Duyck493fb302013-09-28 07:01:44 +00003328 for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
Jesse Brandeburgac26fc12015-09-28 14:12:37 -04003329 struct i40e_q_vector *q_vector = vsi->q_vectors[i];
3330
Jesse Brandeburgee2319c2015-09-28 14:16:54 -04003331 q_vector->itr_countdown = ITR_COUNTDOWN_START;
Kan Lianga75e8002016-02-19 09:24:04 -05003332 q_vector->rx.itr = ITR_TO_REG(vsi->rx_rings[i]->rx_itr_setting);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003333 q_vector->rx.latency_range = I40E_LOW_LATENCY;
3334 wr32(hw, I40E_PFINT_ITRN(I40E_RX_ITR, vector - 1),
3335 q_vector->rx.itr);
Kan Lianga75e8002016-02-19 09:24:04 -05003336 q_vector->tx.itr = ITR_TO_REG(vsi->tx_rings[i]->tx_itr_setting);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003337 q_vector->tx.latency_range = I40E_LOW_LATENCY;
3338 wr32(hw, I40E_PFINT_ITRN(I40E_TX_ITR, vector - 1),
3339 q_vector->tx.itr);
Jesse Brandeburgac26fc12015-09-28 14:12:37 -04003340 wr32(hw, I40E_PFINT_RATEN(vector - 1),
Alan Brady1c0e6a32016-11-28 16:06:02 -08003341 i40e_intrl_usec_to_reg(vsi->int_rate_limit));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003342
3343 /* Linked list for the queuepairs assigned to this vector */
3344 wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), qp);
3345 for (q = 0; q < q_vector->num_ringpairs; q++) {
Jesse Brandeburgac26fc12015-09-28 14:12:37 -04003346 u32 val;
3347
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003348 val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3349 (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
3350 (vector << I40E_QINT_RQCTL_MSIX_INDX_SHIFT) |
3351 (qp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT)|
3352 (I40E_QUEUE_TYPE_TX
3353 << I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT);
3354
3355 wr32(hw, I40E_QINT_RQCTL(qp), val);
3356
3357 val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3358 (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3359 (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
3360 ((qp+1) << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT)|
3361 (I40E_QUEUE_TYPE_RX
3362 << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3363
3364 /* Terminate the linked list */
3365 if (q == (q_vector->num_ringpairs - 1))
3366 val |= (I40E_QUEUE_END_OF_LIST
3367 << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
3368
3369 wr32(hw, I40E_QINT_TQCTL(qp), val);
3370 qp++;
3371 }
3372 }
3373
3374 i40e_flush(hw);
3375}
3376
3377/**
3378 * i40e_enable_misc_int_causes - enable the non-queue interrupts
3379 * @hw: ptr to the hardware info
3380 **/
Jacob Kellerab437b52014-12-14 01:55:08 +00003381static void i40e_enable_misc_int_causes(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003382{
Jacob Kellerab437b52014-12-14 01:55:08 +00003383 struct i40e_hw *hw = &pf->hw;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003384 u32 val;
3385
3386 /* clear things first */
3387 wr32(hw, I40E_PFINT_ICR0_ENA, 0); /* disable all */
3388 rd32(hw, I40E_PFINT_ICR0); /* read to clear */
3389
3390 val = I40E_PFINT_ICR0_ENA_ECC_ERR_MASK |
3391 I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK |
3392 I40E_PFINT_ICR0_ENA_GRST_MASK |
3393 I40E_PFINT_ICR0_ENA_PCI_EXCEPTION_MASK |
3394 I40E_PFINT_ICR0_ENA_GPIO_MASK |
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003395 I40E_PFINT_ICR0_ENA_HMC_ERR_MASK |
3396 I40E_PFINT_ICR0_ENA_VFLR_MASK |
3397 I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
3398
Anjali Singhai Jain0d8e1432015-06-05 12:20:32 -04003399 if (pf->flags & I40E_FLAG_IWARP_ENABLED)
3400 val |= I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
3401
Jacob Kellerab437b52014-12-14 01:55:08 +00003402 if (pf->flags & I40E_FLAG_PTP)
3403 val |= I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
3404
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003405 wr32(hw, I40E_PFINT_ICR0_ENA, val);
3406
3407 /* SW_ITR_IDX = 0, but don't change INTENA */
Anjali Singhai Jain84ed40e2013-11-26 10:49:32 +00003408 wr32(hw, I40E_PFINT_DYN_CTL0, I40E_PFINT_DYN_CTL0_SW_ITR_INDX_MASK |
3409 I40E_PFINT_DYN_CTL0_INTENA_MSK_MASK);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003410
3411 /* OTHER_ITR_IDX = 0 */
3412 wr32(hw, I40E_PFINT_STAT_CTL0, 0);
3413}
3414
3415/**
3416 * i40e_configure_msi_and_legacy - Legacy mode interrupt config in the HW
3417 * @vsi: the VSI being configured
3418 **/
3419static void i40e_configure_msi_and_legacy(struct i40e_vsi *vsi)
3420{
Alexander Duyck493fb302013-09-28 07:01:44 +00003421 struct i40e_q_vector *q_vector = vsi->q_vectors[0];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003422 struct i40e_pf *pf = vsi->back;
3423 struct i40e_hw *hw = &pf->hw;
3424 u32 val;
3425
3426 /* set the ITR configuration */
Jesse Brandeburgee2319c2015-09-28 14:16:54 -04003427 q_vector->itr_countdown = ITR_COUNTDOWN_START;
Kan Lianga75e8002016-02-19 09:24:04 -05003428 q_vector->rx.itr = ITR_TO_REG(vsi->rx_rings[0]->rx_itr_setting);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003429 q_vector->rx.latency_range = I40E_LOW_LATENCY;
3430 wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), q_vector->rx.itr);
Kan Lianga75e8002016-02-19 09:24:04 -05003431 q_vector->tx.itr = ITR_TO_REG(vsi->tx_rings[0]->tx_itr_setting);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003432 q_vector->tx.latency_range = I40E_LOW_LATENCY;
3433 wr32(hw, I40E_PFINT_ITR0(I40E_TX_ITR), q_vector->tx.itr);
3434
Jacob Kellerab437b52014-12-14 01:55:08 +00003435 i40e_enable_misc_int_causes(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003436
3437 /* FIRSTQ_INDX = 0, FIRSTQ_TYPE = 0 (rx) */
3438 wr32(hw, I40E_PFINT_LNKLST0, 0);
3439
Jesse Brandeburgf29eaa32014-02-11 08:24:12 +00003440 /* Associate the queue pair to the vector and enable the queue int */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003441 val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3442 (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
3443 (I40E_QUEUE_TYPE_TX << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3444
3445 wr32(hw, I40E_QINT_RQCTL(0), val);
3446
3447 val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3448 (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3449 (I40E_QUEUE_END_OF_LIST << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
3450
3451 wr32(hw, I40E_QINT_TQCTL(0), val);
3452 i40e_flush(hw);
3453}
3454
3455/**
Mitch Williams2ef28cf2013-11-28 06:39:32 +00003456 * i40e_irq_dynamic_disable_icr0 - Disable default interrupt generation for icr0
3457 * @pf: board private structure
3458 **/
3459void i40e_irq_dynamic_disable_icr0(struct i40e_pf *pf)
3460{
3461 struct i40e_hw *hw = &pf->hw;
3462
3463 wr32(hw, I40E_PFINT_DYN_CTL0,
3464 I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
3465 i40e_flush(hw);
3466}
3467
3468/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003469 * i40e_irq_dynamic_enable_icr0 - Enable default interrupt generation for icr0
3470 * @pf: board private structure
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08003471 * @clearpba: true when all pending interrupt events should be cleared
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003472 **/
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08003473void i40e_irq_dynamic_enable_icr0(struct i40e_pf *pf, bool clearpba)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003474{
3475 struct i40e_hw *hw = &pf->hw;
3476 u32 val;
3477
3478 val = I40E_PFINT_DYN_CTL0_INTENA_MASK |
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08003479 (clearpba ? I40E_PFINT_DYN_CTL0_CLEARPBA_MASK : 0) |
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003480 (I40E_ITR_NONE << I40E_PFINT_DYN_CTL0_ITR_INDX_SHIFT);
3481
3482 wr32(hw, I40E_PFINT_DYN_CTL0, val);
3483 i40e_flush(hw);
3484}
3485
3486/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003487 * i40e_msix_clean_rings - MSIX mode Interrupt Handler
3488 * @irq: interrupt number
3489 * @data: pointer to a q_vector
3490 **/
3491static irqreturn_t i40e_msix_clean_rings(int irq, void *data)
3492{
3493 struct i40e_q_vector *q_vector = data;
3494
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003495 if (!q_vector->tx.ring && !q_vector->rx.ring)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003496 return IRQ_HANDLED;
3497
Alexander Duyck5d3465a2015-09-29 15:19:50 -07003498 napi_schedule_irqoff(&q_vector->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003499
3500 return IRQ_HANDLED;
3501}
3502
3503/**
Alan Brady96db7762016-09-14 16:24:38 -07003504 * i40e_irq_affinity_notify - Callback for affinity changes
3505 * @notify: context as to what irq was changed
3506 * @mask: the new affinity mask
3507 *
3508 * This is a callback function used by the irq_set_affinity_notifier function
3509 * so that we may register to receive changes to the irq affinity masks.
3510 **/
3511static void i40e_irq_affinity_notify(struct irq_affinity_notify *notify,
3512 const cpumask_t *mask)
3513{
3514 struct i40e_q_vector *q_vector =
3515 container_of(notify, struct i40e_q_vector, affinity_notify);
3516
3517 q_vector->affinity_mask = *mask;
3518}
3519
3520/**
3521 * i40e_irq_affinity_release - Callback for affinity notifier release
3522 * @ref: internal core kernel usage
3523 *
3524 * This is a callback function used by the irq_set_affinity_notifier function
3525 * to inform the current notification subscriber that they will no longer
3526 * receive notifications.
3527 **/
3528static void i40e_irq_affinity_release(struct kref *ref) {}
3529
3530/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003531 * i40e_vsi_request_irq_msix - Initialize MSI-X interrupts
3532 * @vsi: the VSI being configured
3533 * @basename: name for the vector
3534 *
3535 * Allocates MSI-X vectors and requests interrupts from the kernel.
3536 **/
3537static int i40e_vsi_request_irq_msix(struct i40e_vsi *vsi, char *basename)
3538{
3539 int q_vectors = vsi->num_q_vectors;
3540 struct i40e_pf *pf = vsi->back;
3541 int base = vsi->base_vector;
3542 int rx_int_idx = 0;
3543 int tx_int_idx = 0;
3544 int vector, err;
Alan Brady96db7762016-09-14 16:24:38 -07003545 int irq_num;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003546
3547 for (vector = 0; vector < q_vectors; vector++) {
Alexander Duyck493fb302013-09-28 07:01:44 +00003548 struct i40e_q_vector *q_vector = vsi->q_vectors[vector];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003549
Alan Brady96db7762016-09-14 16:24:38 -07003550 irq_num = pf->msix_entries[base + vector].vector;
3551
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003552 if (q_vector->tx.ring && q_vector->rx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003553 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3554 "%s-%s-%d", basename, "TxRx", rx_int_idx++);
3555 tx_int_idx++;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003556 } else if (q_vector->rx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003557 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3558 "%s-%s-%d", basename, "rx", rx_int_idx++);
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003559 } else if (q_vector->tx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003560 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3561 "%s-%s-%d", basename, "tx", tx_int_idx++);
3562 } else {
3563 /* skip this unused q_vector */
3564 continue;
3565 }
Alan Brady96db7762016-09-14 16:24:38 -07003566 err = request_irq(irq_num,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003567 vsi->irq_handler,
3568 0,
3569 q_vector->name,
3570 q_vector);
3571 if (err) {
3572 dev_info(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04003573 "MSIX request_irq failed, error: %d\n", err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003574 goto free_queue_irqs;
3575 }
Alan Brady96db7762016-09-14 16:24:38 -07003576
3577 /* register for affinity change notifications */
3578 q_vector->affinity_notify.notify = i40e_irq_affinity_notify;
3579 q_vector->affinity_notify.release = i40e_irq_affinity_release;
3580 irq_set_affinity_notifier(irq_num, &q_vector->affinity_notify);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003581 /* assign the mask for this irq */
Alan Brady96db7762016-09-14 16:24:38 -07003582 irq_set_affinity_hint(irq_num, &q_vector->affinity_mask);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003583 }
3584
Shannon Nelson63741842014-04-23 04:50:16 +00003585 vsi->irqs_ready = true;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003586 return 0;
3587
3588free_queue_irqs:
3589 while (vector) {
3590 vector--;
Alan Brady96db7762016-09-14 16:24:38 -07003591 irq_num = pf->msix_entries[base + vector].vector;
3592 irq_set_affinity_notifier(irq_num, NULL);
3593 irq_set_affinity_hint(irq_num, NULL);
3594 free_irq(irq_num, &vsi->q_vectors[vector]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003595 }
3596 return err;
3597}
3598
3599/**
3600 * i40e_vsi_disable_irq - Mask off queue interrupt generation on the VSI
3601 * @vsi: the VSI being un-configured
3602 **/
3603static void i40e_vsi_disable_irq(struct i40e_vsi *vsi)
3604{
3605 struct i40e_pf *pf = vsi->back;
3606 struct i40e_hw *hw = &pf->hw;
3607 int base = vsi->base_vector;
3608 int i;
3609
3610 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck9f65e152013-09-28 06:00:58 +00003611 wr32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx), 0);
3612 wr32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx), 0);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003613 }
3614
3615 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3616 for (i = vsi->base_vector;
3617 i < (vsi->num_q_vectors + vsi->base_vector); i++)
3618 wr32(hw, I40E_PFINT_DYN_CTLN(i - 1), 0);
3619
3620 i40e_flush(hw);
3621 for (i = 0; i < vsi->num_q_vectors; i++)
3622 synchronize_irq(pf->msix_entries[i + base].vector);
3623 } else {
3624 /* Legacy and MSI mode - this stops all interrupt handling */
3625 wr32(hw, I40E_PFINT_ICR0_ENA, 0);
3626 wr32(hw, I40E_PFINT_DYN_CTL0, 0);
3627 i40e_flush(hw);
3628 synchronize_irq(pf->pdev->irq);
3629 }
3630}
3631
3632/**
3633 * i40e_vsi_enable_irq - Enable IRQ for the given VSI
3634 * @vsi: the VSI being configured
3635 **/
3636static int i40e_vsi_enable_irq(struct i40e_vsi *vsi)
3637{
3638 struct i40e_pf *pf = vsi->back;
3639 int i;
3640
3641 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
Jesse Brandeburg78455482015-07-23 16:54:41 -04003642 for (i = 0; i < vsi->num_q_vectors; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003643 i40e_irq_dynamic_enable(vsi, i);
3644 } else {
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08003645 i40e_irq_dynamic_enable_icr0(pf, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003646 }
3647
Jesse Brandeburg1022cb62013-09-28 07:13:08 +00003648 i40e_flush(&pf->hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003649 return 0;
3650}
3651
3652/**
3653 * i40e_stop_misc_vector - Stop the vector that handles non-queue events
3654 * @pf: board private structure
3655 **/
3656static void i40e_stop_misc_vector(struct i40e_pf *pf)
3657{
3658 /* Disable ICR 0 */
3659 wr32(&pf->hw, I40E_PFINT_ICR0_ENA, 0);
3660 i40e_flush(&pf->hw);
3661}
3662
3663/**
3664 * i40e_intr - MSI/Legacy and non-queue interrupt handler
3665 * @irq: interrupt number
3666 * @data: pointer to a q_vector
3667 *
3668 * This is the handler used for all MSI/Legacy interrupts, and deals
3669 * with both queue and non-queue interrupts. This is also used in
3670 * MSIX mode to handle the non-queue interrupts.
3671 **/
3672static irqreturn_t i40e_intr(int irq, void *data)
3673{
3674 struct i40e_pf *pf = (struct i40e_pf *)data;
3675 struct i40e_hw *hw = &pf->hw;
Anjali Singhai Jain5e8230662013-12-18 13:45:49 +00003676 irqreturn_t ret = IRQ_NONE;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003677 u32 icr0, icr0_remaining;
3678 u32 val, ena_mask;
3679
3680 icr0 = rd32(hw, I40E_PFINT_ICR0);
Anjali Singhai Jain5e8230662013-12-18 13:45:49 +00003681 ena_mask = rd32(hw, I40E_PFINT_ICR0_ENA);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003682
Shannon Nelson116a57d2013-09-28 07:13:59 +00003683 /* if sharing a legacy IRQ, we might get called w/o an intr pending */
3684 if ((icr0 & I40E_PFINT_ICR0_INTEVENT_MASK) == 0)
Anjali Singhai Jain5e8230662013-12-18 13:45:49 +00003685 goto enable_intr;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003686
Shannon Nelsoncd92e722013-11-16 10:00:44 +00003687 /* if interrupt but no bits showing, must be SWINT */
3688 if (((icr0 & ~I40E_PFINT_ICR0_INTEVENT_MASK) == 0) ||
3689 (icr0 & I40E_PFINT_ICR0_SWINT_MASK))
3690 pf->sw_int_count++;
3691
Anjali Singhai Jain0d8e1432015-06-05 12:20:32 -04003692 if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
3693 (ena_mask & I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK)) {
3694 ena_mask &= ~I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
3695 icr0 &= ~I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
Carolyn Wyborny23bb6dc2016-11-08 13:05:12 -08003696 dev_dbg(&pf->pdev->dev, "cleared PE_CRITERR\n");
Anjali Singhai Jain0d8e1432015-06-05 12:20:32 -04003697 }
3698
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003699 /* only q0 is used in MSI/Legacy mode, and none are used in MSIX */
3700 if (icr0 & I40E_PFINT_ICR0_QUEUE_0_MASK) {
Alexander Duyck5d3465a2015-09-29 15:19:50 -07003701 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
3702 struct i40e_q_vector *q_vector = vsi->q_vectors[0];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003703
Anjali Singhai Jaina16ae2d2016-01-15 14:33:16 -08003704 /* We do not have a way to disarm Queue causes while leaving
3705 * interrupt enabled for all other causes, ideally
3706 * interrupt should be disabled while we are in NAPI but
3707 * this is not a performance path and napi_schedule()
3708 * can deal with rescheduling.
3709 */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003710 if (!test_bit(__I40E_DOWN, &pf->state))
Alexander Duyck5d3465a2015-09-29 15:19:50 -07003711 napi_schedule_irqoff(&q_vector->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003712 }
3713
3714 if (icr0 & I40E_PFINT_ICR0_ADMINQ_MASK) {
3715 ena_mask &= ~I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
3716 set_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state);
Shannon Nelson6e93d0c2016-01-15 14:33:18 -08003717 i40e_debug(&pf->hw, I40E_DEBUG_NVM, "AdminQ event\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003718 }
3719
3720 if (icr0 & I40E_PFINT_ICR0_MAL_DETECT_MASK) {
3721 ena_mask &= ~I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
3722 set_bit(__I40E_MDD_EVENT_PENDING, &pf->state);
3723 }
3724
3725 if (icr0 & I40E_PFINT_ICR0_VFLR_MASK) {
3726 ena_mask &= ~I40E_PFINT_ICR0_ENA_VFLR_MASK;
3727 set_bit(__I40E_VFLR_EVENT_PENDING, &pf->state);
3728 }
3729
3730 if (icr0 & I40E_PFINT_ICR0_GRST_MASK) {
3731 if (!test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
3732 set_bit(__I40E_RESET_INTR_RECEIVED, &pf->state);
3733 ena_mask &= ~I40E_PFINT_ICR0_ENA_GRST_MASK;
3734 val = rd32(hw, I40E_GLGEN_RSTAT);
3735 val = (val & I40E_GLGEN_RSTAT_RESET_TYPE_MASK)
3736 >> I40E_GLGEN_RSTAT_RESET_TYPE_SHIFT;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003737 if (val == I40E_RESET_CORER) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003738 pf->corer_count++;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003739 } else if (val == I40E_RESET_GLOBR) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003740 pf->globr_count++;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003741 } else if (val == I40E_RESET_EMPR) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003742 pf->empr_count++;
Anjali Singhai Jain9df42d12015-01-24 09:58:40 +00003743 set_bit(__I40E_EMP_RESET_INTR_RECEIVED, &pf->state);
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003744 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003745 }
3746
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003747 if (icr0 & I40E_PFINT_ICR0_HMC_ERR_MASK) {
3748 icr0 &= ~I40E_PFINT_ICR0_HMC_ERR_MASK;
3749 dev_info(&pf->pdev->dev, "HMC error interrupt\n");
Anjali Singhai Jain25fc0e62015-03-31 00:45:01 -07003750 dev_info(&pf->pdev->dev, "HMC error info 0x%x, HMC error data 0x%x\n",
3751 rd32(hw, I40E_PFHMC_ERRORINFO),
3752 rd32(hw, I40E_PFHMC_ERRORDATA));
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003753 }
3754
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003755 if (icr0 & I40E_PFINT_ICR0_TIMESYNC_MASK) {
3756 u32 prttsyn_stat = rd32(hw, I40E_PRTTSYN_STAT_0);
3757
3758 if (prttsyn_stat & I40E_PRTTSYN_STAT_0_TXTIME_MASK) {
Jacob Kellercafa1fc2014-04-24 18:05:03 -07003759 icr0 &= ~I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003760 i40e_ptp_tx_hwtstamp(pf);
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003761 }
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003762 }
3763
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003764 /* If a critical error is pending we have no choice but to reset the
3765 * device.
3766 * Report and mask out any remaining unexpected interrupts.
3767 */
3768 icr0_remaining = icr0 & ena_mask;
3769 if (icr0_remaining) {
3770 dev_info(&pf->pdev->dev, "unhandled interrupt icr0=0x%08x\n",
3771 icr0_remaining);
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003772 if ((icr0_remaining & I40E_PFINT_ICR0_PE_CRITERR_MASK) ||
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003773 (icr0_remaining & I40E_PFINT_ICR0_PCI_EXCEPTION_MASK) ||
Anjali Singhai Jainc0c28972014-02-12 01:45:34 +00003774 (icr0_remaining & I40E_PFINT_ICR0_ECC_ERR_MASK)) {
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003775 dev_info(&pf->pdev->dev, "device will be reset\n");
3776 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
3777 i40e_service_event_schedule(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003778 }
3779 ena_mask &= ~icr0_remaining;
3780 }
Anjali Singhai Jain5e8230662013-12-18 13:45:49 +00003781 ret = IRQ_HANDLED;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003782
Anjali Singhai Jain5e8230662013-12-18 13:45:49 +00003783enable_intr:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003784 /* re-enable interrupt causes */
3785 wr32(hw, I40E_PFINT_ICR0_ENA, ena_mask);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003786 if (!test_bit(__I40E_DOWN, &pf->state)) {
3787 i40e_service_event_schedule(pf);
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08003788 i40e_irq_dynamic_enable_icr0(pf, false);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003789 }
3790
Anjali Singhai Jain5e8230662013-12-18 13:45:49 +00003791 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003792}
3793
3794/**
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003795 * i40e_clean_fdir_tx_irq - Reclaim resources after transmit completes
3796 * @tx_ring: tx ring to clean
3797 * @budget: how many cleans we're allowed
3798 *
3799 * Returns true if there's any budget left (e.g. the clean is finished)
3800 **/
3801static bool i40e_clean_fdir_tx_irq(struct i40e_ring *tx_ring, int budget)
3802{
3803 struct i40e_vsi *vsi = tx_ring->vsi;
3804 u16 i = tx_ring->next_to_clean;
3805 struct i40e_tx_buffer *tx_buf;
3806 struct i40e_tx_desc *tx_desc;
3807
3808 tx_buf = &tx_ring->tx_bi[i];
3809 tx_desc = I40E_TX_DESC(tx_ring, i);
3810 i -= tx_ring->count;
3811
3812 do {
3813 struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
3814
3815 /* if next_to_watch is not set then there is no work pending */
3816 if (!eop_desc)
3817 break;
3818
3819 /* prevent any other reads prior to eop_desc */
3820 read_barrier_depends();
3821
3822 /* if the descriptor isn't done, no work yet to do */
3823 if (!(eop_desc->cmd_type_offset_bsz &
3824 cpu_to_le64(I40E_TX_DESC_DTYPE_DESC_DONE)))
3825 break;
3826
3827 /* clear next_to_watch to prevent false hangs */
3828 tx_buf->next_to_watch = NULL;
3829
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003830 tx_desc->buffer_addr = 0;
3831 tx_desc->cmd_type_offset_bsz = 0;
3832 /* move past filter desc */
3833 tx_buf++;
3834 tx_desc++;
3835 i++;
3836 if (unlikely(!i)) {
3837 i -= tx_ring->count;
3838 tx_buf = tx_ring->tx_bi;
3839 tx_desc = I40E_TX_DESC(tx_ring, 0);
3840 }
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003841 /* unmap skb header data */
3842 dma_unmap_single(tx_ring->dev,
3843 dma_unmap_addr(tx_buf, dma),
3844 dma_unmap_len(tx_buf, len),
3845 DMA_TO_DEVICE);
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003846 if (tx_buf->tx_flags & I40E_TX_FLAGS_FD_SB)
3847 kfree(tx_buf->raw_buf);
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003848
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003849 tx_buf->raw_buf = NULL;
3850 tx_buf->tx_flags = 0;
3851 tx_buf->next_to_watch = NULL;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003852 dma_unmap_len_set(tx_buf, len, 0);
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003853 tx_desc->buffer_addr = 0;
3854 tx_desc->cmd_type_offset_bsz = 0;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003855
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003856 /* move us past the eop_desc for start of next FD desc */
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003857 tx_buf++;
3858 tx_desc++;
3859 i++;
3860 if (unlikely(!i)) {
3861 i -= tx_ring->count;
3862 tx_buf = tx_ring->tx_bi;
3863 tx_desc = I40E_TX_DESC(tx_ring, 0);
3864 }
3865
3866 /* update budget accounting */
3867 budget--;
3868 } while (likely(budget));
3869
3870 i += tx_ring->count;
3871 tx_ring->next_to_clean = i;
3872
Jesse Brandeburg6995b362015-08-28 17:55:54 -04003873 if (vsi->back->flags & I40E_FLAG_MSIX_ENABLED)
Jesse Brandeburg78455482015-07-23 16:54:41 -04003874 i40e_irq_dynamic_enable(vsi, tx_ring->q_vector->v_idx);
Jesse Brandeburg6995b362015-08-28 17:55:54 -04003875
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003876 return budget > 0;
3877}
3878
3879/**
3880 * i40e_fdir_clean_ring - Interrupt Handler for FDIR SB ring
3881 * @irq: interrupt number
3882 * @data: pointer to a q_vector
3883 **/
3884static irqreturn_t i40e_fdir_clean_ring(int irq, void *data)
3885{
3886 struct i40e_q_vector *q_vector = data;
3887 struct i40e_vsi *vsi;
3888
3889 if (!q_vector->tx.ring)
3890 return IRQ_HANDLED;
3891
3892 vsi = q_vector->tx.ring->vsi;
3893 i40e_clean_fdir_tx_irq(q_vector->tx.ring, vsi->work_limit);
3894
3895 return IRQ_HANDLED;
3896}
3897
3898/**
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003899 * i40e_map_vector_to_qp - Assigns the queue pair to the vector
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003900 * @vsi: the VSI being configured
3901 * @v_idx: vector index
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003902 * @qp_idx: queue pair index
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003903 **/
Anjali Singhai Jain26cdc442015-07-10 19:36:00 -04003904static void i40e_map_vector_to_qp(struct i40e_vsi *vsi, int v_idx, int qp_idx)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003905{
Alexander Duyck493fb302013-09-28 07:01:44 +00003906 struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
Alexander Duyck9f65e152013-09-28 06:00:58 +00003907 struct i40e_ring *tx_ring = vsi->tx_rings[qp_idx];
3908 struct i40e_ring *rx_ring = vsi->rx_rings[qp_idx];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003909
3910 tx_ring->q_vector = q_vector;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003911 tx_ring->next = q_vector->tx.ring;
3912 q_vector->tx.ring = tx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003913 q_vector->tx.count++;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003914
3915 rx_ring->q_vector = q_vector;
3916 rx_ring->next = q_vector->rx.ring;
3917 q_vector->rx.ring = rx_ring;
3918 q_vector->rx.count++;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003919}
3920
3921/**
3922 * i40e_vsi_map_rings_to_vectors - Maps descriptor rings to vectors
3923 * @vsi: the VSI being configured
3924 *
3925 * This function maps descriptor rings to the queue-specific vectors
3926 * we were allotted through the MSI-X enabling code. Ideally, we'd have
3927 * one vector per queue pair, but on a constrained vector budget, we
3928 * group the queue pairs as "efficiently" as possible.
3929 **/
3930static void i40e_vsi_map_rings_to_vectors(struct i40e_vsi *vsi)
3931{
3932 int qp_remaining = vsi->num_queue_pairs;
3933 int q_vectors = vsi->num_q_vectors;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003934 int num_ringpairs;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003935 int v_start = 0;
3936 int qp_idx = 0;
3937
3938 /* If we don't have enough vectors for a 1-to-1 mapping, we'll have to
3939 * group them so there are multiple queues per vector.
Anjali Singhai Jain70114ec2014-06-03 23:50:14 +00003940 * It is also important to go through all the vectors available to be
3941 * sure that if we don't use all the vectors, that the remaining vectors
3942 * are cleared. This is especially important when decreasing the
3943 * number of queues in use.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003944 */
Anjali Singhai Jain70114ec2014-06-03 23:50:14 +00003945 for (; v_start < q_vectors; v_start++) {
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003946 struct i40e_q_vector *q_vector = vsi->q_vectors[v_start];
3947
3948 num_ringpairs = DIV_ROUND_UP(qp_remaining, q_vectors - v_start);
3949
3950 q_vector->num_ringpairs = num_ringpairs;
3951
3952 q_vector->rx.count = 0;
3953 q_vector->tx.count = 0;
3954 q_vector->rx.ring = NULL;
3955 q_vector->tx.ring = NULL;
3956
3957 while (num_ringpairs--) {
Anjali Singhai Jain26cdc442015-07-10 19:36:00 -04003958 i40e_map_vector_to_qp(vsi, v_start, qp_idx);
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003959 qp_idx++;
3960 qp_remaining--;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003961 }
3962 }
3963}
3964
3965/**
3966 * i40e_vsi_request_irq - Request IRQ from the OS
3967 * @vsi: the VSI being configured
3968 * @basename: name for the vector
3969 **/
3970static int i40e_vsi_request_irq(struct i40e_vsi *vsi, char *basename)
3971{
3972 struct i40e_pf *pf = vsi->back;
3973 int err;
3974
3975 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
3976 err = i40e_vsi_request_irq_msix(vsi, basename);
3977 else if (pf->flags & I40E_FLAG_MSI_ENABLED)
3978 err = request_irq(pf->pdev->irq, i40e_intr, 0,
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00003979 pf->int_name, pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003980 else
3981 err = request_irq(pf->pdev->irq, i40e_intr, IRQF_SHARED,
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00003982 pf->int_name, pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003983
3984 if (err)
3985 dev_info(&pf->pdev->dev, "request_irq failed, Error %d\n", err);
3986
3987 return err;
3988}
3989
3990#ifdef CONFIG_NET_POLL_CONTROLLER
3991/**
Jesse Brandeburgd89d9672016-01-04 10:33:02 -08003992 * i40e_netpoll - A Polling 'interrupt' handler
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003993 * @netdev: network interface device structure
3994 *
3995 * This is used by netconsole to send skbs without having to re-enable
3996 * interrupts. It's not called while the normal interrupt routine is executing.
3997 **/
Vasu Dev38e00432014-08-01 13:27:03 -07003998#ifdef I40E_FCOE
3999void i40e_netpoll(struct net_device *netdev)
4000#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004001static void i40e_netpoll(struct net_device *netdev)
Vasu Dev38e00432014-08-01 13:27:03 -07004002#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004003{
4004 struct i40e_netdev_priv *np = netdev_priv(netdev);
4005 struct i40e_vsi *vsi = np->vsi;
4006 struct i40e_pf *pf = vsi->back;
4007 int i;
4008
4009 /* if interface is down do nothing */
4010 if (test_bit(__I40E_DOWN, &vsi->state))
4011 return;
4012
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004013 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4014 for (i = 0; i < vsi->num_q_vectors; i++)
Alexander Duyck493fb302013-09-28 07:01:44 +00004015 i40e_msix_clean_rings(0, vsi->q_vectors[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004016 } else {
4017 i40e_intr(pf->pdev->irq, netdev);
4018 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004019}
4020#endif
4021
4022/**
Neerav Parikh23527302014-06-03 23:50:15 +00004023 * i40e_pf_txq_wait - Wait for a PF's Tx queue to be enabled or disabled
4024 * @pf: the PF being configured
4025 * @pf_q: the PF queue
4026 * @enable: enable or disable state of the queue
4027 *
4028 * This routine will wait for the given Tx queue of the PF to reach the
4029 * enabled or disabled state.
4030 * Returns -ETIMEDOUT in case of failing to reach the requested state after
4031 * multiple retries; else will return 0 in case of success.
4032 **/
4033static int i40e_pf_txq_wait(struct i40e_pf *pf, int pf_q, bool enable)
4034{
4035 int i;
4036 u32 tx_reg;
4037
4038 for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
4039 tx_reg = rd32(&pf->hw, I40E_QTX_ENA(pf_q));
4040 if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
4041 break;
4042
Neerav Parikhf98a2002014-09-13 07:40:44 +00004043 usleep_range(10, 20);
Neerav Parikh23527302014-06-03 23:50:15 +00004044 }
4045 if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
4046 return -ETIMEDOUT;
4047
4048 return 0;
4049}
4050
4051/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004052 * i40e_vsi_control_tx - Start or stop a VSI's rings
4053 * @vsi: the VSI being configured
4054 * @enable: start or stop the rings
4055 **/
4056static int i40e_vsi_control_tx(struct i40e_vsi *vsi, bool enable)
4057{
4058 struct i40e_pf *pf = vsi->back;
4059 struct i40e_hw *hw = &pf->hw;
Neerav Parikh23527302014-06-03 23:50:15 +00004060 int i, j, pf_q, ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004061 u32 tx_reg;
4062
4063 pf_q = vsi->base_queue;
4064 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
Matt Jared351499ab2014-04-23 04:50:03 +00004065
4066 /* warn the TX unit of coming changes */
4067 i40e_pre_tx_queue_cfg(&pf->hw, pf_q, enable);
4068 if (!enable)
Neerav Parikhf98a2002014-09-13 07:40:44 +00004069 usleep_range(10, 20);
Matt Jared351499ab2014-04-23 04:50:03 +00004070
Mitch Williams6c5ef622014-02-20 19:29:16 -08004071 for (j = 0; j < 50; j++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004072 tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
Mitch Williams6c5ef622014-02-20 19:29:16 -08004073 if (((tx_reg >> I40E_QTX_ENA_QENA_REQ_SHIFT) & 1) ==
4074 ((tx_reg >> I40E_QTX_ENA_QENA_STAT_SHIFT) & 1))
4075 break;
4076 usleep_range(1000, 2000);
4077 }
Mitch Williamsfda972f2013-11-28 06:39:29 +00004078 /* Skip if the queue is already in the requested state */
Catherine Sullivan7c1220072014-03-14 07:32:29 +00004079 if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
Mitch Williamsfda972f2013-11-28 06:39:29 +00004080 continue;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004081
4082 /* turn on/off the queue */
Shannon Nelsonc5c9eb92013-12-21 05:44:48 +00004083 if (enable) {
4084 wr32(hw, I40E_QTX_HEAD(pf_q), 0);
Mitch Williams6c5ef622014-02-20 19:29:16 -08004085 tx_reg |= I40E_QTX_ENA_QENA_REQ_MASK;
Shannon Nelsonc5c9eb92013-12-21 05:44:48 +00004086 } else {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004087 tx_reg &= ~I40E_QTX_ENA_QENA_REQ_MASK;
Shannon Nelsonc5c9eb92013-12-21 05:44:48 +00004088 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004089
4090 wr32(hw, I40E_QTX_ENA(pf_q), tx_reg);
Neerav Parikh69129dc2014-11-12 00:18:46 +00004091 /* No waiting for the Tx queue to disable */
4092 if (!enable && test_bit(__I40E_PORT_TX_SUSPENDED, &pf->state))
4093 continue;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004094
4095 /* wait for the change to finish */
Neerav Parikh23527302014-06-03 23:50:15 +00004096 ret = i40e_pf_txq_wait(pf, pf_q, enable);
4097 if (ret) {
4098 dev_info(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04004099 "VSI seid %d Tx ring %d %sable timeout\n",
4100 vsi->seid, pf_q, (enable ? "en" : "dis"));
Neerav Parikh23527302014-06-03 23:50:15 +00004101 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004102 }
4103 }
4104
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00004105 if (hw->revision_id == 0)
4106 mdelay(50);
Neerav Parikh23527302014-06-03 23:50:15 +00004107 return ret;
4108}
4109
4110/**
4111 * i40e_pf_rxq_wait - Wait for a PF's Rx queue to be enabled or disabled
4112 * @pf: the PF being configured
4113 * @pf_q: the PF queue
4114 * @enable: enable or disable state of the queue
4115 *
4116 * This routine will wait for the given Rx queue of the PF to reach the
4117 * enabled or disabled state.
4118 * Returns -ETIMEDOUT in case of failing to reach the requested state after
4119 * multiple retries; else will return 0 in case of success.
4120 **/
4121static int i40e_pf_rxq_wait(struct i40e_pf *pf, int pf_q, bool enable)
4122{
4123 int i;
4124 u32 rx_reg;
4125
4126 for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
4127 rx_reg = rd32(&pf->hw, I40E_QRX_ENA(pf_q));
4128 if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4129 break;
4130
Neerav Parikhf98a2002014-09-13 07:40:44 +00004131 usleep_range(10, 20);
Neerav Parikh23527302014-06-03 23:50:15 +00004132 }
4133 if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
4134 return -ETIMEDOUT;
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00004135
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004136 return 0;
4137}
4138
4139/**
4140 * i40e_vsi_control_rx - Start or stop a VSI's rings
4141 * @vsi: the VSI being configured
4142 * @enable: start or stop the rings
4143 **/
4144static int i40e_vsi_control_rx(struct i40e_vsi *vsi, bool enable)
4145{
4146 struct i40e_pf *pf = vsi->back;
4147 struct i40e_hw *hw = &pf->hw;
Neerav Parikh23527302014-06-03 23:50:15 +00004148 int i, j, pf_q, ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004149 u32 rx_reg;
4150
4151 pf_q = vsi->base_queue;
4152 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
Mitch Williams6c5ef622014-02-20 19:29:16 -08004153 for (j = 0; j < 50; j++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004154 rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
Mitch Williams6c5ef622014-02-20 19:29:16 -08004155 if (((rx_reg >> I40E_QRX_ENA_QENA_REQ_SHIFT) & 1) ==
4156 ((rx_reg >> I40E_QRX_ENA_QENA_STAT_SHIFT) & 1))
4157 break;
4158 usleep_range(1000, 2000);
4159 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004160
Catherine Sullivan7c1220072014-03-14 07:32:29 +00004161 /* Skip if the queue is already in the requested state */
4162 if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4163 continue;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004164
4165 /* turn on/off the queue */
4166 if (enable)
Mitch Williams6c5ef622014-02-20 19:29:16 -08004167 rx_reg |= I40E_QRX_ENA_QENA_REQ_MASK;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004168 else
Mitch Williams6c5ef622014-02-20 19:29:16 -08004169 rx_reg &= ~I40E_QRX_ENA_QENA_REQ_MASK;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004170 wr32(hw, I40E_QRX_ENA(pf_q), rx_reg);
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004171 /* No waiting for the Tx queue to disable */
4172 if (!enable && test_bit(__I40E_PORT_TX_SUSPENDED, &pf->state))
4173 continue;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004174
4175 /* wait for the change to finish */
Neerav Parikh23527302014-06-03 23:50:15 +00004176 ret = i40e_pf_rxq_wait(pf, pf_q, enable);
4177 if (ret) {
4178 dev_info(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04004179 "VSI seid %d Rx ring %d %sable timeout\n",
4180 vsi->seid, pf_q, (enable ? "en" : "dis"));
Neerav Parikh23527302014-06-03 23:50:15 +00004181 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004182 }
4183 }
4184
Neerav Parikh23527302014-06-03 23:50:15 +00004185 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004186}
4187
4188/**
Filip Sadowski3aa7b742016-10-11 15:26:58 -07004189 * i40e_vsi_start_rings - Start a VSI's rings
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004190 * @vsi: the VSI being configured
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004191 **/
Filip Sadowski3aa7b742016-10-11 15:26:58 -07004192int i40e_vsi_start_rings(struct i40e_vsi *vsi)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004193{
Anjali Singhai Jain3b867b22013-12-21 05:44:44 +00004194 int ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004195
4196 /* do rx first for enable and last for disable */
Filip Sadowski3aa7b742016-10-11 15:26:58 -07004197 ret = i40e_vsi_control_rx(vsi, true);
4198 if (ret)
4199 return ret;
4200 ret = i40e_vsi_control_tx(vsi, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004201
4202 return ret;
4203}
4204
4205/**
Filip Sadowski3aa7b742016-10-11 15:26:58 -07004206 * i40e_vsi_stop_rings - Stop a VSI's rings
4207 * @vsi: the VSI being configured
4208 **/
4209void i40e_vsi_stop_rings(struct i40e_vsi *vsi)
4210{
4211 /* do rx first for enable and last for disable
4212 * Ignore return value, we need to shutdown whatever we can
4213 */
4214 i40e_vsi_control_tx(vsi, false);
4215 i40e_vsi_control_rx(vsi, false);
4216}
4217
4218/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004219 * i40e_vsi_free_irq - Free the irq association with the OS
4220 * @vsi: the VSI being configured
4221 **/
4222static void i40e_vsi_free_irq(struct i40e_vsi *vsi)
4223{
4224 struct i40e_pf *pf = vsi->back;
4225 struct i40e_hw *hw = &pf->hw;
4226 int base = vsi->base_vector;
4227 u32 val, qp;
4228 int i;
4229
4230 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4231 if (!vsi->q_vectors)
4232 return;
4233
Shannon Nelson63741842014-04-23 04:50:16 +00004234 if (!vsi->irqs_ready)
4235 return;
4236
4237 vsi->irqs_ready = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004238 for (i = 0; i < vsi->num_q_vectors; i++) {
Alan Brady96db7762016-09-14 16:24:38 -07004239 int irq_num;
4240 u16 vector;
4241
4242 vector = i + base;
4243 irq_num = pf->msix_entries[vector].vector;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004244
4245 /* free only the irqs that were actually requested */
Shannon Nelson78681b12013-11-28 06:39:36 +00004246 if (!vsi->q_vectors[i] ||
4247 !vsi->q_vectors[i]->num_ringpairs)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004248 continue;
4249
Alan Brady96db7762016-09-14 16:24:38 -07004250 /* clear the affinity notifier in the IRQ descriptor */
4251 irq_set_affinity_notifier(irq_num, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004252 /* clear the affinity_mask in the IRQ descriptor */
Alan Brady96db7762016-09-14 16:24:38 -07004253 irq_set_affinity_hint(irq_num, NULL);
4254 synchronize_irq(irq_num);
4255 free_irq(irq_num, vsi->q_vectors[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004256
4257 /* Tear down the interrupt queue link list
4258 *
4259 * We know that they come in pairs and always
4260 * the Rx first, then the Tx. To clear the
4261 * link list, stick the EOL value into the
4262 * next_q field of the registers.
4263 */
4264 val = rd32(hw, I40E_PFINT_LNKLSTN(vector - 1));
4265 qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
4266 >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4267 val |= I40E_QUEUE_END_OF_LIST
4268 << I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4269 wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), val);
4270
4271 while (qp != I40E_QUEUE_END_OF_LIST) {
4272 u32 next;
4273
4274 val = rd32(hw, I40E_QINT_RQCTL(qp));
4275
4276 val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
4277 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
4278 I40E_QINT_RQCTL_CAUSE_ENA_MASK |
4279 I40E_QINT_RQCTL_INTEVENT_MASK);
4280
4281 val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
4282 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
4283
4284 wr32(hw, I40E_QINT_RQCTL(qp), val);
4285
4286 val = rd32(hw, I40E_QINT_TQCTL(qp));
4287
4288 next = (val & I40E_QINT_TQCTL_NEXTQ_INDX_MASK)
4289 >> I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT;
4290
4291 val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
4292 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
4293 I40E_QINT_TQCTL_CAUSE_ENA_MASK |
4294 I40E_QINT_TQCTL_INTEVENT_MASK);
4295
4296 val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
4297 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
4298
4299 wr32(hw, I40E_QINT_TQCTL(qp), val);
4300 qp = next;
4301 }
4302 }
4303 } else {
4304 free_irq(pf->pdev->irq, pf);
4305
4306 val = rd32(hw, I40E_PFINT_LNKLST0);
4307 qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
4308 >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4309 val |= I40E_QUEUE_END_OF_LIST
4310 << I40E_PFINT_LNKLST0_FIRSTQ_INDX_SHIFT;
4311 wr32(hw, I40E_PFINT_LNKLST0, val);
4312
4313 val = rd32(hw, I40E_QINT_RQCTL(qp));
4314 val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
4315 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
4316 I40E_QINT_RQCTL_CAUSE_ENA_MASK |
4317 I40E_QINT_RQCTL_INTEVENT_MASK);
4318
4319 val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
4320 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
4321
4322 wr32(hw, I40E_QINT_RQCTL(qp), val);
4323
4324 val = rd32(hw, I40E_QINT_TQCTL(qp));
4325
4326 val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
4327 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
4328 I40E_QINT_TQCTL_CAUSE_ENA_MASK |
4329 I40E_QINT_TQCTL_INTEVENT_MASK);
4330
4331 val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
4332 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
4333
4334 wr32(hw, I40E_QINT_TQCTL(qp), val);
4335 }
4336}
4337
4338/**
Alexander Duyck493fb302013-09-28 07:01:44 +00004339 * i40e_free_q_vector - Free memory allocated for specific interrupt vector
4340 * @vsi: the VSI being configured
4341 * @v_idx: Index of vector to be freed
4342 *
4343 * This function frees the memory allocated to the q_vector. In addition if
4344 * NAPI is enabled it will delete any references to the NAPI struct prior
4345 * to freeing the q_vector.
4346 **/
4347static void i40e_free_q_vector(struct i40e_vsi *vsi, int v_idx)
4348{
4349 struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00004350 struct i40e_ring *ring;
Alexander Duyck493fb302013-09-28 07:01:44 +00004351
4352 if (!q_vector)
4353 return;
4354
4355 /* disassociate q_vector from rings */
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00004356 i40e_for_each_ring(ring, q_vector->tx)
4357 ring->q_vector = NULL;
4358
4359 i40e_for_each_ring(ring, q_vector->rx)
4360 ring->q_vector = NULL;
Alexander Duyck493fb302013-09-28 07:01:44 +00004361
4362 /* only VSI w/ an associated netdev is set up w/ NAPI */
4363 if (vsi->netdev)
4364 netif_napi_del(&q_vector->napi);
4365
4366 vsi->q_vectors[v_idx] = NULL;
4367
4368 kfree_rcu(q_vector, rcu);
4369}
4370
4371/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004372 * i40e_vsi_free_q_vectors - Free memory allocated for interrupt vectors
4373 * @vsi: the VSI being un-configured
4374 *
4375 * This frees the memory allocated to the q_vectors and
4376 * deletes references to the NAPI struct.
4377 **/
4378static void i40e_vsi_free_q_vectors(struct i40e_vsi *vsi)
4379{
4380 int v_idx;
4381
Alexander Duyck493fb302013-09-28 07:01:44 +00004382 for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++)
4383 i40e_free_q_vector(vsi, v_idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004384}
4385
4386/**
4387 * i40e_reset_interrupt_capability - Disable interrupt setup in OS
4388 * @pf: board private structure
4389 **/
4390static void i40e_reset_interrupt_capability(struct i40e_pf *pf)
4391{
4392 /* If we're in Legacy mode, the interrupt was cleaned in vsi_close */
4393 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4394 pci_disable_msix(pf->pdev);
4395 kfree(pf->msix_entries);
4396 pf->msix_entries = NULL;
Shannon Nelson3b444392015-02-26 16:15:57 +00004397 kfree(pf->irq_pile);
4398 pf->irq_pile = NULL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004399 } else if (pf->flags & I40E_FLAG_MSI_ENABLED) {
4400 pci_disable_msi(pf->pdev);
4401 }
4402 pf->flags &= ~(I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
4403}
4404
4405/**
4406 * i40e_clear_interrupt_scheme - Clear the current interrupt scheme settings
4407 * @pf: board private structure
4408 *
4409 * We go through and clear interrupt specific resources and reset the structure
4410 * to pre-load conditions
4411 **/
4412static void i40e_clear_interrupt_scheme(struct i40e_pf *pf)
4413{
4414 int i;
4415
Shannon Nelsone1477582015-02-21 06:44:33 +00004416 i40e_stop_misc_vector(pf);
Shannon Nelson69278392016-03-10 14:59:43 -08004417 if (pf->flags & I40E_FLAG_MSIX_ENABLED && pf->msix_entries) {
Shannon Nelsone1477582015-02-21 06:44:33 +00004418 synchronize_irq(pf->msix_entries[0].vector);
4419 free_irq(pf->msix_entries[0].vector, pf);
4420 }
4421
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06004422 i40e_put_lump(pf->irq_pile, pf->iwarp_base_vector,
4423 I40E_IWARP_IRQ_PILE_ID);
4424
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004425 i40e_put_lump(pf->irq_pile, 0, I40E_PILE_VALID_BIT-1);
Mitch Williams505682c2014-05-20 08:01:37 +00004426 for (i = 0; i < pf->num_alloc_vsi; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004427 if (pf->vsi[i])
4428 i40e_vsi_free_q_vectors(pf->vsi[i]);
4429 i40e_reset_interrupt_capability(pf);
4430}
4431
4432/**
4433 * i40e_napi_enable_all - Enable NAPI for all q_vectors in the VSI
4434 * @vsi: the VSI being configured
4435 **/
4436static void i40e_napi_enable_all(struct i40e_vsi *vsi)
4437{
4438 int q_idx;
4439
4440 if (!vsi->netdev)
4441 return;
4442
4443 for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++)
Alexander Duyck493fb302013-09-28 07:01:44 +00004444 napi_enable(&vsi->q_vectors[q_idx]->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004445}
4446
4447/**
4448 * i40e_napi_disable_all - Disable NAPI for all q_vectors in the VSI
4449 * @vsi: the VSI being configured
4450 **/
4451static void i40e_napi_disable_all(struct i40e_vsi *vsi)
4452{
4453 int q_idx;
4454
4455 if (!vsi->netdev)
4456 return;
4457
4458 for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++)
Alexander Duyck493fb302013-09-28 07:01:44 +00004459 napi_disable(&vsi->q_vectors[q_idx]->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004460}
4461
4462/**
Shannon Nelson90ef8d42014-03-14 07:32:26 +00004463 * i40e_vsi_close - Shut down a VSI
4464 * @vsi: the vsi to be quelled
4465 **/
4466static void i40e_vsi_close(struct i40e_vsi *vsi)
4467{
Mitch Williams0ef2d5a2017-01-24 10:24:00 -08004468 struct i40e_pf *pf = vsi->back;
Shannon Nelson90ef8d42014-03-14 07:32:26 +00004469 if (!test_and_set_bit(__I40E_DOWN, &vsi->state))
4470 i40e_down(vsi);
4471 i40e_vsi_free_irq(vsi);
4472 i40e_vsi_free_tx_resources(vsi);
4473 i40e_vsi_free_rx_resources(vsi);
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04004474 vsi->current_netdev_flags = 0;
Mitch Williams0ef2d5a2017-01-24 10:24:00 -08004475 pf->flags |= I40E_FLAG_SERVICE_CLIENT_REQUESTED;
4476 if (test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
4477 pf->flags |= I40E_FLAG_CLIENT_RESET;
Shannon Nelson90ef8d42014-03-14 07:32:26 +00004478}
4479
4480/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004481 * i40e_quiesce_vsi - Pause a given VSI
4482 * @vsi: the VSI being paused
4483 **/
4484static void i40e_quiesce_vsi(struct i40e_vsi *vsi)
4485{
4486 if (test_bit(__I40E_DOWN, &vsi->state))
4487 return;
4488
Neerav Parikhd341b7a2014-11-12 00:18:51 +00004489 /* No need to disable FCoE VSI when Tx suspended */
4490 if ((test_bit(__I40E_PORT_TX_SUSPENDED, &vsi->back->state)) &&
4491 vsi->type == I40E_VSI_FCOE) {
4492 dev_dbg(&vsi->back->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04004493 "VSI seid %d skipping FCoE VSI disable\n", vsi->seid);
Neerav Parikhd341b7a2014-11-12 00:18:51 +00004494 return;
4495 }
4496
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004497 set_bit(__I40E_NEEDS_RESTART, &vsi->state);
Jesse Brandeburg6995b362015-08-28 17:55:54 -04004498 if (vsi->netdev && netif_running(vsi->netdev))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004499 vsi->netdev->netdev_ops->ndo_stop(vsi->netdev);
Jesse Brandeburg6995b362015-08-28 17:55:54 -04004500 else
Shannon Nelson90ef8d42014-03-14 07:32:26 +00004501 i40e_vsi_close(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004502}
4503
4504/**
4505 * i40e_unquiesce_vsi - Resume a given VSI
4506 * @vsi: the VSI being resumed
4507 **/
4508static void i40e_unquiesce_vsi(struct i40e_vsi *vsi)
4509{
4510 if (!test_bit(__I40E_NEEDS_RESTART, &vsi->state))
4511 return;
4512
4513 clear_bit(__I40E_NEEDS_RESTART, &vsi->state);
4514 if (vsi->netdev && netif_running(vsi->netdev))
4515 vsi->netdev->netdev_ops->ndo_open(vsi->netdev);
4516 else
Shannon Nelson8276f752014-03-14 07:32:27 +00004517 i40e_vsi_open(vsi); /* this clears the DOWN bit */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004518}
4519
4520/**
4521 * i40e_pf_quiesce_all_vsi - Pause all VSIs on a PF
4522 * @pf: the PF
4523 **/
4524static void i40e_pf_quiesce_all_vsi(struct i40e_pf *pf)
4525{
4526 int v;
4527
Mitch Williams505682c2014-05-20 08:01:37 +00004528 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004529 if (pf->vsi[v])
4530 i40e_quiesce_vsi(pf->vsi[v]);
4531 }
4532}
4533
4534/**
4535 * i40e_pf_unquiesce_all_vsi - Resume all VSIs on a PF
4536 * @pf: the PF
4537 **/
4538static void i40e_pf_unquiesce_all_vsi(struct i40e_pf *pf)
4539{
4540 int v;
4541
Mitch Williams505682c2014-05-20 08:01:37 +00004542 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004543 if (pf->vsi[v])
4544 i40e_unquiesce_vsi(pf->vsi[v]);
4545 }
4546}
4547
Neerav Parikh69129dc2014-11-12 00:18:46 +00004548#ifdef CONFIG_I40E_DCB
4549/**
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004550 * i40e_vsi_wait_queues_disabled - Wait for VSI's queues to be disabled
Neerav Parikh69129dc2014-11-12 00:18:46 +00004551 * @vsi: the VSI being configured
4552 *
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004553 * This function waits for the given VSI's queues to be disabled.
Neerav Parikh69129dc2014-11-12 00:18:46 +00004554 **/
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004555static int i40e_vsi_wait_queues_disabled(struct i40e_vsi *vsi)
Neerav Parikh69129dc2014-11-12 00:18:46 +00004556{
4557 struct i40e_pf *pf = vsi->back;
4558 int i, pf_q, ret;
4559
4560 pf_q = vsi->base_queue;
4561 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4562 /* Check and wait for the disable status of the queue */
4563 ret = i40e_pf_txq_wait(pf, pf_q, false);
4564 if (ret) {
4565 dev_info(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04004566 "VSI seid %d Tx ring %d disable timeout\n",
4567 vsi->seid, pf_q);
Neerav Parikh69129dc2014-11-12 00:18:46 +00004568 return ret;
4569 }
4570 }
4571
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004572 pf_q = vsi->base_queue;
4573 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4574 /* Check and wait for the disable status of the queue */
4575 ret = i40e_pf_rxq_wait(pf, pf_q, false);
4576 if (ret) {
4577 dev_info(&pf->pdev->dev,
4578 "VSI seid %d Rx ring %d disable timeout\n",
4579 vsi->seid, pf_q);
4580 return ret;
4581 }
4582 }
4583
Neerav Parikh69129dc2014-11-12 00:18:46 +00004584 return 0;
4585}
4586
4587/**
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004588 * i40e_pf_wait_queues_disabled - Wait for all queues of PF VSIs to be disabled
Neerav Parikh69129dc2014-11-12 00:18:46 +00004589 * @pf: the PF
4590 *
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004591 * This function waits for the queues to be in disabled state for all the
Neerav Parikh69129dc2014-11-12 00:18:46 +00004592 * VSIs that are managed by this PF.
4593 **/
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004594static int i40e_pf_wait_queues_disabled(struct i40e_pf *pf)
Neerav Parikh69129dc2014-11-12 00:18:46 +00004595{
4596 int v, ret = 0;
4597
4598 for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
Neerav Parikhd341b7a2014-11-12 00:18:51 +00004599 /* No need to wait for FCoE VSI queues */
4600 if (pf->vsi[v] && pf->vsi[v]->type != I40E_VSI_FCOE) {
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004601 ret = i40e_vsi_wait_queues_disabled(pf->vsi[v]);
Neerav Parikh69129dc2014-11-12 00:18:46 +00004602 if (ret)
4603 break;
4604 }
4605 }
4606
4607 return ret;
4608}
4609
4610#endif
Kiran Patilb03a8c12015-09-24 18:13:15 -04004611
4612/**
4613 * i40e_detect_recover_hung_queue - Function to detect and recover hung_queue
4614 * @q_idx: TX queue number
4615 * @vsi: Pointer to VSI struct
4616 *
4617 * This function checks specified queue for given VSI. Detects hung condition.
4618 * Sets hung bit since it is two step process. Before next run of service task
4619 * if napi_poll runs, it reset 'hung' bit for respective q_vector. If not,
4620 * hung condition remain unchanged and during subsequent run, this function
4621 * issues SW interrupt to recover from hung condition.
4622 **/
4623static void i40e_detect_recover_hung_queue(int q_idx, struct i40e_vsi *vsi)
4624{
4625 struct i40e_ring *tx_ring = NULL;
4626 struct i40e_pf *pf;
Anjali Singhai Jaindd353102016-01-15 14:33:12 -08004627 u32 head, val, tx_pending_hw;
Kiran Patilb03a8c12015-09-24 18:13:15 -04004628 int i;
4629
4630 pf = vsi->back;
4631
4632 /* now that we have an index, find the tx_ring struct */
4633 for (i = 0; i < vsi->num_queue_pairs; i++) {
4634 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc) {
4635 if (q_idx == vsi->tx_rings[i]->queue_index) {
4636 tx_ring = vsi->tx_rings[i];
4637 break;
4638 }
4639 }
4640 }
4641
4642 if (!tx_ring)
4643 return;
4644
4645 /* Read interrupt register */
4646 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
4647 val = rd32(&pf->hw,
4648 I40E_PFINT_DYN_CTLN(tx_ring->q_vector->v_idx +
4649 tx_ring->vsi->base_vector - 1));
4650 else
4651 val = rd32(&pf->hw, I40E_PFINT_DYN_CTL0);
4652
4653 head = i40e_get_head(tx_ring);
4654
Anjali Singhai Jaindd353102016-01-15 14:33:12 -08004655 tx_pending_hw = i40e_get_tx_pending(tx_ring, false);
Kiran Patilb03a8c12015-09-24 18:13:15 -04004656
Kiran Patil9c6c1252015-11-06 15:26:02 -08004657 /* HW is done executing descriptors, updated HEAD write back,
4658 * but SW hasn't processed those descriptors. If interrupt is
4659 * not generated from this point ON, it could result into
4660 * dev_watchdog detecting timeout on those netdev_queue,
4661 * hence proactively trigger SW interrupt.
Kiran Patilb03a8c12015-09-24 18:13:15 -04004662 */
Anjali Singhai Jaindd353102016-01-15 14:33:12 -08004663 if (tx_pending_hw && (!(val & I40E_PFINT_DYN_CTLN_INTENA_MASK))) {
Kiran Patil9c6c1252015-11-06 15:26:02 -08004664 /* NAPI Poll didn't run and clear since it was set */
4665 if (test_and_clear_bit(I40E_Q_VECTOR_HUNG_DETECT,
4666 &tx_ring->q_vector->hung_detected)) {
Anjali Singhai Jaindd353102016-01-15 14:33:12 -08004667 netdev_info(vsi->netdev, "VSI_seid %d, Hung TX queue %d, tx_pending_hw: %d, NTC:0x%x, HWB: 0x%x, NTU: 0x%x, TAIL: 0x%x\n",
4668 vsi->seid, q_idx, tx_pending_hw,
Kiran Patil9c6c1252015-11-06 15:26:02 -08004669 tx_ring->next_to_clean, head,
4670 tx_ring->next_to_use,
4671 readl(tx_ring->tail));
4672 netdev_info(vsi->netdev, "VSI_seid %d, Issuing force_wb for TX queue %d, Interrupt Reg: 0x%x\n",
4673 vsi->seid, q_idx, val);
4674 i40e_force_wb(vsi, tx_ring->q_vector);
4675 } else {
4676 /* First Chance - detected possible hung */
4677 set_bit(I40E_Q_VECTOR_HUNG_DETECT,
4678 &tx_ring->q_vector->hung_detected);
4679 }
4680 }
Anjali Singhai Jaindd353102016-01-15 14:33:12 -08004681
4682 /* This is the case where we have interrupts missing,
4683 * so the tx_pending in HW will most likely be 0, but we
4684 * will have tx_pending in SW since the WB happened but the
4685 * interrupt got lost.
4686 */
4687 if ((!tx_pending_hw) && i40e_get_tx_pending(tx_ring, true) &&
4688 (!(val & I40E_PFINT_DYN_CTLN_INTENA_MASK))) {
Benjamin Poirier83a0c6e2017-01-12 17:04:14 -08004689 local_bh_disable();
Anjali Singhai Jaindd353102016-01-15 14:33:12 -08004690 if (napi_reschedule(&tx_ring->q_vector->napi))
4691 tx_ring->tx_stats.tx_lost_interrupt++;
Benjamin Poirier83a0c6e2017-01-12 17:04:14 -08004692 local_bh_enable();
Anjali Singhai Jaindd353102016-01-15 14:33:12 -08004693 }
Kiran Patilb03a8c12015-09-24 18:13:15 -04004694}
4695
4696/**
4697 * i40e_detect_recover_hung - Function to detect and recover hung_queues
4698 * @pf: pointer to PF struct
4699 *
4700 * LAN VSI has netdev and netdev has TX queues. This function is to check
4701 * each of those TX queues if they are hung, trigger recovery by issuing
4702 * SW interrupt.
4703 **/
4704static void i40e_detect_recover_hung(struct i40e_pf *pf)
4705{
4706 struct net_device *netdev;
4707 struct i40e_vsi *vsi;
4708 int i;
4709
4710 /* Only for LAN VSI */
4711 vsi = pf->vsi[pf->lan_vsi];
4712
4713 if (!vsi)
4714 return;
4715
4716 /* Make sure, VSI state is not DOWN/RECOVERY_PENDING */
4717 if (test_bit(__I40E_DOWN, &vsi->back->state) ||
4718 test_bit(__I40E_RESET_RECOVERY_PENDING, &vsi->back->state))
4719 return;
4720
4721 /* Make sure type is MAIN VSI */
4722 if (vsi->type != I40E_VSI_MAIN)
4723 return;
4724
4725 netdev = vsi->netdev;
4726 if (!netdev)
4727 return;
4728
4729 /* Bail out if netif_carrier is not OK */
4730 if (!netif_carrier_ok(netdev))
4731 return;
4732
4733 /* Go thru' TX queues for netdev */
4734 for (i = 0; i < netdev->num_tx_queues; i++) {
4735 struct netdev_queue *q;
4736
4737 q = netdev_get_tx_queue(netdev, i);
4738 if (q)
4739 i40e_detect_recover_hung_queue(i, vsi);
4740 }
4741}
4742
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004743/**
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004744 * i40e_get_iscsi_tc_map - Return TC map for iSCSI APP
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00004745 * @pf: pointer to PF
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004746 *
4747 * Get TC map for ISCSI PF type that will include iSCSI TC
4748 * and LAN TC.
4749 **/
4750static u8 i40e_get_iscsi_tc_map(struct i40e_pf *pf)
4751{
4752 struct i40e_dcb_app_priority_table app;
4753 struct i40e_hw *hw = &pf->hw;
4754 u8 enabled_tc = 1; /* TC0 is always enabled */
4755 u8 tc, i;
4756 /* Get the iSCSI APP TLV */
4757 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
4758
4759 for (i = 0; i < dcbcfg->numapps; i++) {
4760 app = dcbcfg->app[i];
4761 if (app.selector == I40E_APP_SEL_TCPIP &&
4762 app.protocolid == I40E_APP_PROTOID_ISCSI) {
4763 tc = dcbcfg->etscfg.prioritytable[app.priority];
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08004764 enabled_tc |= BIT(tc);
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004765 break;
4766 }
4767 }
4768
4769 return enabled_tc;
4770}
4771
4772/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004773 * i40e_dcb_get_num_tc - Get the number of TCs from DCBx config
4774 * @dcbcfg: the corresponding DCBx configuration structure
4775 *
4776 * Return the number of TCs from given DCBx configuration
4777 **/
4778static u8 i40e_dcb_get_num_tc(struct i40e_dcbx_config *dcbcfg)
4779{
Dave Ertmanfbfe12c2016-08-12 09:56:32 -07004780 int i, tc_unused = 0;
Jesse Brandeburg078b5872013-09-25 23:41:14 +00004781 u8 num_tc = 0;
Dave Ertmanfbfe12c2016-08-12 09:56:32 -07004782 u8 ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004783
4784 /* Scan the ETS Config Priority Table to find
4785 * traffic class enabled for a given priority
Dave Ertmanfbfe12c2016-08-12 09:56:32 -07004786 * and create a bitmask of enabled TCs
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004787 */
Dave Ertmanfbfe12c2016-08-12 09:56:32 -07004788 for (i = 0; i < I40E_MAX_USER_PRIORITY; i++)
4789 num_tc |= BIT(dcbcfg->etscfg.prioritytable[i]);
4790
4791 /* Now scan the bitmask to check for
4792 * contiguous TCs starting with TC0
4793 */
4794 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
4795 if (num_tc & BIT(i)) {
4796 if (!tc_unused) {
4797 ret++;
4798 } else {
4799 pr_err("Non-contiguous TC - Disabling DCB\n");
4800 return 1;
4801 }
4802 } else {
4803 tc_unused = 1;
4804 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004805 }
4806
Dave Ertmanfbfe12c2016-08-12 09:56:32 -07004807 /* There is always at least TC0 */
4808 if (!ret)
4809 ret = 1;
4810
4811 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004812}
4813
4814/**
4815 * i40e_dcb_get_enabled_tc - Get enabled traffic classes
4816 * @dcbcfg: the corresponding DCBx configuration structure
4817 *
4818 * Query the current DCB configuration and return the number of
4819 * traffic classes enabled from the given DCBX config
4820 **/
4821static u8 i40e_dcb_get_enabled_tc(struct i40e_dcbx_config *dcbcfg)
4822{
4823 u8 num_tc = i40e_dcb_get_num_tc(dcbcfg);
4824 u8 enabled_tc = 1;
4825 u8 i;
4826
4827 for (i = 0; i < num_tc; i++)
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04004828 enabled_tc |= BIT(i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004829
4830 return enabled_tc;
4831}
4832
4833/**
4834 * i40e_pf_get_num_tc - Get enabled traffic classes for PF
4835 * @pf: PF being queried
4836 *
4837 * Return number of traffic classes enabled for the given PF
4838 **/
4839static u8 i40e_pf_get_num_tc(struct i40e_pf *pf)
4840{
4841 struct i40e_hw *hw = &pf->hw;
Dave Ertman52a08ca2016-07-27 12:02:34 -07004842 u8 i, enabled_tc = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004843 u8 num_tc = 0;
4844 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
4845
4846 /* If DCB is not enabled then always in single TC */
4847 if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
4848 return 1;
4849
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004850 /* SFP mode will be enabled for all TCs on port */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004851 if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
4852 return i40e_dcb_get_num_tc(dcbcfg);
4853
4854 /* MFP mode return count of enabled TCs for this PF */
4855 if (pf->hw.func_caps.iscsi)
4856 enabled_tc = i40e_get_iscsi_tc_map(pf);
4857 else
Neerav Parikhfc51de92015-02-24 06:58:53 +00004858 return 1; /* Only TC0 */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004859
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004860 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08004861 if (enabled_tc & BIT(i))
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004862 num_tc++;
4863 }
4864 return num_tc;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004865}
4866
4867/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004868 * i40e_pf_get_pf_tc_map - Get bitmap for enabled traffic classes
4869 * @pf: PF being queried
4870 *
4871 * Return a bitmap for enabled traffic classes for this PF.
4872 **/
4873static u8 i40e_pf_get_tc_map(struct i40e_pf *pf)
4874{
4875 /* If DCB is not enabled for this PF then just return default TC */
4876 if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
David Ertmanea6acb72016-09-20 07:10:50 -07004877 return I40E_DEFAULT_TRAFFIC_CLASS;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004878
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004879 /* SFP mode we want PF to be enabled for all TCs */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004880 if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
4881 return i40e_dcb_get_enabled_tc(&pf->hw.local_dcbx_config);
4882
Neerav Parikhfc51de92015-02-24 06:58:53 +00004883 /* MFP enabled and iSCSI PF type */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004884 if (pf->hw.func_caps.iscsi)
4885 return i40e_get_iscsi_tc_map(pf);
4886 else
David Ertmanea6acb72016-09-20 07:10:50 -07004887 return I40E_DEFAULT_TRAFFIC_CLASS;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004888}
4889
4890/**
4891 * i40e_vsi_get_bw_info - Query VSI BW Information
4892 * @vsi: the VSI being queried
4893 *
4894 * Returns 0 on success, negative value on failure
4895 **/
4896static int i40e_vsi_get_bw_info(struct i40e_vsi *vsi)
4897{
4898 struct i40e_aqc_query_vsi_ets_sla_config_resp bw_ets_config = {0};
4899 struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
4900 struct i40e_pf *pf = vsi->back;
4901 struct i40e_hw *hw = &pf->hw;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004902 i40e_status ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004903 u32 tc_bw_max;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004904 int i;
4905
4906 /* Get the VSI level BW configuration */
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004907 ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid, &bw_config, NULL);
4908 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004909 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004910 "couldn't get PF vsi bw config, err %s aq_err %s\n",
4911 i40e_stat_str(&pf->hw, ret),
4912 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004913 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004914 }
4915
4916 /* Get the VSI level BW configuration per TC */
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004917 ret = i40e_aq_query_vsi_ets_sla_config(hw, vsi->seid, &bw_ets_config,
4918 NULL);
4919 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004920 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004921 "couldn't get PF vsi ets bw config, err %s aq_err %s\n",
4922 i40e_stat_str(&pf->hw, ret),
4923 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004924 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004925 }
4926
4927 if (bw_config.tc_valid_bits != bw_ets_config.tc_valid_bits) {
4928 dev_info(&pf->pdev->dev,
4929 "Enabled TCs mismatch from querying VSI BW info 0x%08x 0x%08x\n",
4930 bw_config.tc_valid_bits,
4931 bw_ets_config.tc_valid_bits);
4932 /* Still continuing */
4933 }
4934
4935 vsi->bw_limit = le16_to_cpu(bw_config.port_bw_limit);
4936 vsi->bw_max_quanta = bw_config.max_bw;
4937 tc_bw_max = le16_to_cpu(bw_ets_config.tc_bw_max[0]) |
4938 (le16_to_cpu(bw_ets_config.tc_bw_max[1]) << 16);
4939 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
4940 vsi->bw_ets_share_credits[i] = bw_ets_config.share_credits[i];
4941 vsi->bw_ets_limit_credits[i] =
4942 le16_to_cpu(bw_ets_config.credits[i]);
4943 /* 3 bits out of 4 for each TC */
4944 vsi->bw_ets_max_quanta[i] = (u8)((tc_bw_max >> (i*4)) & 0x7);
4945 }
Jesse Brandeburg078b5872013-09-25 23:41:14 +00004946
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004947 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004948}
4949
4950/**
4951 * i40e_vsi_configure_bw_alloc - Configure VSI BW allocation per TC
4952 * @vsi: the VSI being configured
4953 * @enabled_tc: TC bitmap
4954 * @bw_credits: BW shared credits per TC
4955 *
4956 * Returns 0 on success, negative value on failure
4957 **/
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004958static int i40e_vsi_configure_bw_alloc(struct i40e_vsi *vsi, u8 enabled_tc,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004959 u8 *bw_share)
4960{
4961 struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004962 i40e_status ret;
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004963 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004964
4965 bw_data.tc_valid_bits = enabled_tc;
4966 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
4967 bw_data.tc_bw_credits[i] = bw_share[i];
4968
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004969 ret = i40e_aq_config_vsi_tc_bw(&vsi->back->hw, vsi->seid, &bw_data,
4970 NULL);
4971 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004972 dev_info(&vsi->back->pdev->dev,
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004973 "AQ command Config VSI BW allocation per TC failed = %d\n",
4974 vsi->back->hw.aq.asq_last_status);
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004975 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004976 }
4977
4978 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
4979 vsi->info.qs_handle[i] = bw_data.qs_handles[i];
4980
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004981 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004982}
4983
4984/**
4985 * i40e_vsi_config_netdev_tc - Setup the netdev TC configuration
4986 * @vsi: the VSI being configured
4987 * @enabled_tc: TC map to be enabled
4988 *
4989 **/
4990static void i40e_vsi_config_netdev_tc(struct i40e_vsi *vsi, u8 enabled_tc)
4991{
4992 struct net_device *netdev = vsi->netdev;
4993 struct i40e_pf *pf = vsi->back;
4994 struct i40e_hw *hw = &pf->hw;
4995 u8 netdev_tc = 0;
4996 int i;
4997 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
4998
4999 if (!netdev)
5000 return;
5001
5002 if (!enabled_tc) {
5003 netdev_reset_tc(netdev);
5004 return;
5005 }
5006
5007 /* Set up actual enabled TCs on the VSI */
5008 if (netdev_set_num_tc(netdev, vsi->tc_config.numtc))
5009 return;
5010
5011 /* set per TC queues for the VSI */
5012 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5013 /* Only set TC queues for enabled tcs
5014 *
5015 * e.g. For a VSI that has TC0 and TC3 enabled the
5016 * enabled_tc bitmap would be 0x00001001; the driver
5017 * will set the numtc for netdev as 2 that will be
5018 * referenced by the netdev layer as TC 0 and 1.
5019 */
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08005020 if (vsi->tc_config.enabled_tc & BIT(i))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005021 netdev_set_tc_queue(netdev,
5022 vsi->tc_config.tc_info[i].netdev_tc,
5023 vsi->tc_config.tc_info[i].qcount,
5024 vsi->tc_config.tc_info[i].qoffset);
5025 }
5026
5027 /* Assign UP2TC map for the VSI */
5028 for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
5029 /* Get the actual TC# for the UP */
5030 u8 ets_tc = dcbcfg->etscfg.prioritytable[i];
5031 /* Get the mapped netdev TC# for the UP */
5032 netdev_tc = vsi->tc_config.tc_info[ets_tc].netdev_tc;
5033 netdev_set_prio_tc_map(netdev, i, netdev_tc);
5034 }
5035}
5036
5037/**
5038 * i40e_vsi_update_queue_map - Update our copy of VSi info with new queue map
5039 * @vsi: the VSI being configured
5040 * @ctxt: the ctxt buffer returned from AQ VSI update param command
5041 **/
5042static void i40e_vsi_update_queue_map(struct i40e_vsi *vsi,
5043 struct i40e_vsi_context *ctxt)
5044{
5045 /* copy just the sections touched not the entire info
5046 * since not all sections are valid as returned by
5047 * update vsi params
5048 */
5049 vsi->info.mapping_flags = ctxt->info.mapping_flags;
5050 memcpy(&vsi->info.queue_mapping,
5051 &ctxt->info.queue_mapping, sizeof(vsi->info.queue_mapping));
5052 memcpy(&vsi->info.tc_mapping, ctxt->info.tc_mapping,
5053 sizeof(vsi->info.tc_mapping));
5054}
5055
5056/**
5057 * i40e_vsi_config_tc - Configure VSI Tx Scheduler for given TC map
5058 * @vsi: VSI to be configured
5059 * @enabled_tc: TC bitmap
5060 *
5061 * This configures a particular VSI for TCs that are mapped to the
5062 * given TC bitmap. It uses default bandwidth share for TCs across
5063 * VSIs to configure TC for a particular VSI.
5064 *
5065 * NOTE:
5066 * It is expected that the VSI queues have been quisced before calling
5067 * this function.
5068 **/
5069static int i40e_vsi_config_tc(struct i40e_vsi *vsi, u8 enabled_tc)
5070{
5071 u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
5072 struct i40e_vsi_context ctxt;
5073 int ret = 0;
5074 int i;
5075
5076 /* Check if enabled_tc is same as existing or new TCs */
5077 if (vsi->tc_config.enabled_tc == enabled_tc)
5078 return ret;
5079
5080 /* Enable ETS TCs with equal BW Share for now across all VSIs */
5081 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08005082 if (enabled_tc & BIT(i))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005083 bw_share[i] = 1;
5084 }
5085
5086 ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
5087 if (ret) {
5088 dev_info(&vsi->back->pdev->dev,
5089 "Failed configuring TC map %d for VSI %d\n",
5090 enabled_tc, vsi->seid);
5091 goto out;
5092 }
5093
5094 /* Update Queue Pairs Mapping for currently enabled UPs */
5095 ctxt.seid = vsi->seid;
5096 ctxt.pf_num = vsi->back->hw.pf_id;
5097 ctxt.vf_num = 0;
5098 ctxt.uplink_seid = vsi->uplink_seid;
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07005099 ctxt.info = vsi->info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005100 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
5101
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06005102 if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
5103 ctxt.info.valid_sections |=
5104 cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
5105 ctxt.info.queueing_opt_flags |= I40E_AQ_VSI_QUE_OPT_TCP_ENA;
5106 }
5107
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005108 /* Update the VSI after updating the VSI queue-mapping information */
5109 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
5110 if (ret) {
5111 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005112 "Update vsi tc config failed, err %s aq_err %s\n",
5113 i40e_stat_str(&vsi->back->hw, ret),
5114 i40e_aq_str(&vsi->back->hw,
5115 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005116 goto out;
5117 }
5118 /* update the local VSI info with updated queue map */
5119 i40e_vsi_update_queue_map(vsi, &ctxt);
5120 vsi->info.valid_sections = 0;
5121
5122 /* Update current VSI BW information */
5123 ret = i40e_vsi_get_bw_info(vsi);
5124 if (ret) {
5125 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005126 "Failed updating vsi bw info, err %s aq_err %s\n",
5127 i40e_stat_str(&vsi->back->hw, ret),
5128 i40e_aq_str(&vsi->back->hw,
5129 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005130 goto out;
5131 }
5132
5133 /* Update the netdev TC setup */
5134 i40e_vsi_config_netdev_tc(vsi, enabled_tc);
5135out:
5136 return ret;
5137}
5138
5139/**
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005140 * i40e_veb_config_tc - Configure TCs for given VEB
5141 * @veb: given VEB
5142 * @enabled_tc: TC bitmap
5143 *
5144 * Configures given TC bitmap for VEB (switching) element
5145 **/
5146int i40e_veb_config_tc(struct i40e_veb *veb, u8 enabled_tc)
5147{
5148 struct i40e_aqc_configure_switching_comp_bw_config_data bw_data = {0};
5149 struct i40e_pf *pf = veb->pf;
5150 int ret = 0;
5151 int i;
5152
5153 /* No TCs or already enabled TCs just return */
5154 if (!enabled_tc || veb->enabled_tc == enabled_tc)
5155 return ret;
5156
5157 bw_data.tc_valid_bits = enabled_tc;
5158 /* bw_data.absolute_credits is not set (relative) */
5159
5160 /* Enable ETS TCs with equal BW Share for now */
5161 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08005162 if (enabled_tc & BIT(i))
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005163 bw_data.tc_bw_share_credits[i] = 1;
5164 }
5165
5166 ret = i40e_aq_config_switch_comp_bw_config(&pf->hw, veb->seid,
5167 &bw_data, NULL);
5168 if (ret) {
5169 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005170 "VEB bw config failed, err %s aq_err %s\n",
5171 i40e_stat_str(&pf->hw, ret),
5172 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005173 goto out;
5174 }
5175
5176 /* Update the BW information */
5177 ret = i40e_veb_get_bw_info(veb);
5178 if (ret) {
5179 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005180 "Failed getting veb bw config, err %s aq_err %s\n",
5181 i40e_stat_str(&pf->hw, ret),
5182 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005183 }
5184
5185out:
5186 return ret;
5187}
5188
5189#ifdef CONFIG_I40E_DCB
5190/**
5191 * i40e_dcb_reconfigure - Reconfigure all VEBs and VSIs
5192 * @pf: PF struct
5193 *
5194 * Reconfigure VEB/VSIs on a given PF; it is assumed that
5195 * the caller would've quiesce all the VSIs before calling
5196 * this function
5197 **/
5198static void i40e_dcb_reconfigure(struct i40e_pf *pf)
5199{
5200 u8 tc_map = 0;
5201 int ret;
5202 u8 v;
5203
5204 /* Enable the TCs available on PF to all VEBs */
5205 tc_map = i40e_pf_get_tc_map(pf);
5206 for (v = 0; v < I40E_MAX_VEB; v++) {
5207 if (!pf->veb[v])
5208 continue;
5209 ret = i40e_veb_config_tc(pf->veb[v], tc_map);
5210 if (ret) {
5211 dev_info(&pf->pdev->dev,
5212 "Failed configuring TC for VEB seid=%d\n",
5213 pf->veb[v]->seid);
5214 /* Will try to configure as many components */
5215 }
5216 }
5217
5218 /* Update each VSI */
Mitch Williams505682c2014-05-20 08:01:37 +00005219 for (v = 0; v < pf->num_alloc_vsi; v++) {
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005220 if (!pf->vsi[v])
5221 continue;
5222
5223 /* - Enable all TCs for the LAN VSI
Vasu Dev38e00432014-08-01 13:27:03 -07005224#ifdef I40E_FCOE
5225 * - For FCoE VSI only enable the TC configured
5226 * as per the APP TLV
5227#endif
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005228 * - For all others keep them at TC0 for now
5229 */
5230 if (v == pf->lan_vsi)
5231 tc_map = i40e_pf_get_tc_map(pf);
5232 else
David Ertmanea6acb72016-09-20 07:10:50 -07005233 tc_map = I40E_DEFAULT_TRAFFIC_CLASS;
Vasu Dev38e00432014-08-01 13:27:03 -07005234#ifdef I40E_FCOE
5235 if (pf->vsi[v]->type == I40E_VSI_FCOE)
5236 tc_map = i40e_get_fcoe_tc_map(pf);
5237#endif /* #ifdef I40E_FCOE */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005238
5239 ret = i40e_vsi_config_tc(pf->vsi[v], tc_map);
5240 if (ret) {
5241 dev_info(&pf->pdev->dev,
5242 "Failed configuring TC for VSI seid=%d\n",
5243 pf->vsi[v]->seid);
5244 /* Will try to configure as many components */
5245 } else {
Neerav Parikh0672a092014-04-01 07:11:47 +00005246 /* Re-configure VSI vectors based on updated TC map */
5247 i40e_vsi_map_rings_to_vectors(pf->vsi[v]);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005248 if (pf->vsi[v]->netdev)
5249 i40e_dcbnl_set_all(pf->vsi[v]);
5250 }
5251 }
5252}
5253
5254/**
Neerav Parikh2fd75f32014-11-12 00:18:20 +00005255 * i40e_resume_port_tx - Resume port Tx
5256 * @pf: PF struct
5257 *
5258 * Resume a port's Tx and issue a PF reset in case of failure to
5259 * resume.
5260 **/
5261static int i40e_resume_port_tx(struct i40e_pf *pf)
5262{
5263 struct i40e_hw *hw = &pf->hw;
5264 int ret;
5265
5266 ret = i40e_aq_resume_port_tx(hw, NULL);
5267 if (ret) {
5268 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005269 "Resume Port Tx failed, err %s aq_err %s\n",
5270 i40e_stat_str(&pf->hw, ret),
5271 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh2fd75f32014-11-12 00:18:20 +00005272 /* Schedule PF reset to recover */
5273 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
5274 i40e_service_event_schedule(pf);
5275 }
5276
5277 return ret;
5278}
5279
5280/**
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005281 * i40e_init_pf_dcb - Initialize DCB configuration
5282 * @pf: PF being configured
5283 *
5284 * Query the current DCB configuration and cache it
5285 * in the hardware structure
5286 **/
5287static int i40e_init_pf_dcb(struct i40e_pf *pf)
5288{
5289 struct i40e_hw *hw = &pf->hw;
5290 int err = 0;
5291
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +00005292 /* Do not enable DCB for SW1 and SW2 images even if the FW is capable */
Neerav Parikhf1bbad32016-01-13 16:51:39 -08005293 if (pf->flags & I40E_FLAG_NO_DCB_SUPPORT)
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +00005294 goto out;
5295
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005296 /* Get the initial DCB configuration */
5297 err = i40e_init_dcb(hw);
5298 if (!err) {
5299 /* Device/Function is not DCBX capable */
5300 if ((!hw->func_caps.dcb) ||
5301 (hw->dcbx_status == I40E_DCBX_STATUS_DISABLED)) {
5302 dev_info(&pf->pdev->dev,
5303 "DCBX offload is not supported or is disabled for this PF.\n");
5304
5305 if (pf->flags & I40E_FLAG_MFP_ENABLED)
5306 goto out;
5307
5308 } else {
5309 /* When status is not DISABLED then DCBX in FW */
5310 pf->dcbx_cap = DCB_CAP_DCBX_LLD_MANAGED |
5311 DCB_CAP_DCBX_VER_IEEE;
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005312
5313 pf->flags |= I40E_FLAG_DCB_CAPABLE;
Dave Ertmana0362442016-08-29 17:38:26 -07005314 /* Enable DCB tagging only when more than one TC
5315 * or explicitly disable if only one TC
5316 */
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005317 if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
5318 pf->flags |= I40E_FLAG_DCB_ENABLED;
Dave Ertmana0362442016-08-29 17:38:26 -07005319 else
5320 pf->flags &= ~I40E_FLAG_DCB_ENABLED;
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005321 dev_dbg(&pf->pdev->dev,
5322 "DCBX offload is supported for this PF.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005323 }
Neerav Parikh014269f2014-04-01 07:11:48 +00005324 } else {
Shannon Nelsonaebfc812014-12-11 07:06:38 +00005325 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005326 "Query for DCB configuration failed, err %s aq_err %s\n",
5327 i40e_stat_str(&pf->hw, err),
5328 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005329 }
5330
5331out:
5332 return err;
5333}
5334#endif /* CONFIG_I40E_DCB */
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005335#define SPEED_SIZE 14
5336#define FC_SIZE 8
5337/**
5338 * i40e_print_link_message - print link up or down
5339 * @vsi: the VSI for which link needs a message
5340 */
Matt Jaredc156f852015-08-27 11:42:39 -04005341void i40e_print_link_message(struct i40e_vsi *vsi, bool isup)
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005342{
Filip Sadowski7ec9ba12016-11-08 13:05:13 -08005343 enum i40e_aq_link_speed new_speed;
Shannon Nelsona9165492015-09-03 17:19:00 -04005344 char *speed = "Unknown";
5345 char *fc = "Unknown";
Henry Tieman3e03d7c2016-12-02 12:32:57 -08005346 char *fec = "";
5347 char *an = "";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005348
Filip Sadowski7ec9ba12016-11-08 13:05:13 -08005349 new_speed = vsi->back->hw.phy.link_info.link_speed;
5350
5351 if ((vsi->current_isup == isup) && (vsi->current_speed == new_speed))
Matt Jaredc156f852015-08-27 11:42:39 -04005352 return;
5353 vsi->current_isup = isup;
Filip Sadowski7ec9ba12016-11-08 13:05:13 -08005354 vsi->current_speed = new_speed;
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005355 if (!isup) {
5356 netdev_info(vsi->netdev, "NIC Link is Down\n");
5357 return;
5358 }
5359
Greg Rose148c2d82014-12-11 07:06:27 +00005360 /* Warn user if link speed on NPAR enabled partition is not at
5361 * least 10GB
5362 */
5363 if (vsi->back->hw.func_caps.npar_enable &&
5364 (vsi->back->hw.phy.link_info.link_speed == I40E_LINK_SPEED_1GB ||
5365 vsi->back->hw.phy.link_info.link_speed == I40E_LINK_SPEED_100MB))
5366 netdev_warn(vsi->netdev,
5367 "The partition detected link speed that is less than 10Gbps\n");
5368
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005369 switch (vsi->back->hw.phy.link_info.link_speed) {
5370 case I40E_LINK_SPEED_40GB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005371 speed = "40 G";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005372 break;
Jesse Brandeburgae24b402015-03-27 00:12:09 -07005373 case I40E_LINK_SPEED_20GB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005374 speed = "20 G";
Jesse Brandeburgae24b402015-03-27 00:12:09 -07005375 break;
Carolyn Wyborny31232372016-11-21 13:03:48 -08005376 case I40E_LINK_SPEED_25GB:
5377 speed = "25 G";
5378 break;
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005379 case I40E_LINK_SPEED_10GB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005380 speed = "10 G";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005381 break;
5382 case I40E_LINK_SPEED_1GB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005383 speed = "1000 M";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005384 break;
Mitch Williams5960d332014-09-13 07:40:47 +00005385 case I40E_LINK_SPEED_100MB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005386 speed = "100 M";
Mitch Williams5960d332014-09-13 07:40:47 +00005387 break;
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005388 default:
5389 break;
5390 }
5391
5392 switch (vsi->back->hw.fc.current_mode) {
5393 case I40E_FC_FULL:
Shannon Nelsona9165492015-09-03 17:19:00 -04005394 fc = "RX/TX";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005395 break;
5396 case I40E_FC_TX_PAUSE:
Shannon Nelsona9165492015-09-03 17:19:00 -04005397 fc = "TX";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005398 break;
5399 case I40E_FC_RX_PAUSE:
Shannon Nelsona9165492015-09-03 17:19:00 -04005400 fc = "RX";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005401 break;
5402 default:
Shannon Nelsona9165492015-09-03 17:19:00 -04005403 fc = "None";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005404 break;
5405 }
5406
Henry Tieman3e03d7c2016-12-02 12:32:57 -08005407 if (vsi->back->hw.phy.link_info.link_speed == I40E_LINK_SPEED_25GB) {
5408 fec = ", FEC: None";
5409 an = ", Autoneg: False";
5410
5411 if (vsi->back->hw.phy.link_info.an_info & I40E_AQ_AN_COMPLETED)
5412 an = ", Autoneg: True";
5413
5414 if (vsi->back->hw.phy.link_info.fec_info &
5415 I40E_AQ_CONFIG_FEC_KR_ENA)
5416 fec = ", FEC: CL74 FC-FEC/BASE-R";
5417 else if (vsi->back->hw.phy.link_info.fec_info &
5418 I40E_AQ_CONFIG_FEC_RS_ENA)
5419 fec = ", FEC: CL108 RS-FEC";
5420 }
5421
5422 netdev_info(vsi->netdev, "NIC Link is Up, %sbps Full Duplex%s%s, Flow Control: %s\n",
5423 speed, fec, an, fc);
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005424}
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005425
5426/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005427 * i40e_up_complete - Finish the last steps of bringing up a connection
5428 * @vsi: the VSI being configured
5429 **/
5430static int i40e_up_complete(struct i40e_vsi *vsi)
5431{
5432 struct i40e_pf *pf = vsi->back;
5433 int err;
5434
5435 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
5436 i40e_vsi_configure_msix(vsi);
5437 else
5438 i40e_configure_msi_and_legacy(vsi);
5439
5440 /* start rings */
Filip Sadowski3aa7b742016-10-11 15:26:58 -07005441 err = i40e_vsi_start_rings(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005442 if (err)
5443 return err;
5444
5445 clear_bit(__I40E_DOWN, &vsi->state);
5446 i40e_napi_enable_all(vsi);
5447 i40e_vsi_enable_irq(vsi);
5448
5449 if ((pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP) &&
5450 (vsi->netdev)) {
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005451 i40e_print_link_message(vsi, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005452 netif_tx_start_all_queues(vsi->netdev);
5453 netif_carrier_on(vsi->netdev);
Anjali Singhai6d779b42013-09-28 06:00:02 +00005454 } else if (vsi->netdev) {
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005455 i40e_print_link_message(vsi, false);
Carolyn Wyborny7b592f62014-07-10 07:58:19 +00005456 /* need to check for qualified module here*/
5457 if ((pf->hw.phy.link_info.link_info &
5458 I40E_AQ_MEDIA_AVAILABLE) &&
5459 (!(pf->hw.phy.link_info.an_info &
5460 I40E_AQ_QUALIFIED_MODULE)))
5461 netdev_err(vsi->netdev,
5462 "the driver failed to link because an unqualified module was detected.");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005463 }
Anjali Singhai Jainca64fa42014-02-11 08:26:30 +00005464
5465 /* replay FDIR SB filters */
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005466 if (vsi->type == I40E_VSI_FDIR) {
5467 /* reset fd counters */
5468 pf->fd_add_err = pf->fd_atr_cnt = 0;
5469 if (pf->fd_tcp_rule > 0) {
Jacob Keller234dc4e2016-09-06 18:05:09 -07005470 pf->auto_disable_flags |= I40E_FLAG_FD_ATR_ENABLED;
Anjali Singhai Jain2e4875e2015-04-16 20:06:06 -04005471 if (I40E_DEBUG_FD & pf->hw.debug_mask)
5472 dev_info(&pf->pdev->dev, "Forcing ATR off, sideband rules for TCP/IPv4 exist\n");
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005473 pf->fd_tcp_rule = 0;
5474 }
Anjali Singhai Jainca64fa42014-02-11 08:26:30 +00005475 i40e_fdir_filter_restore(vsi);
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005476 }
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06005477
5478 /* On the next run of the service_task, notify any clients of the new
5479 * opened netdev
5480 */
5481 pf->flags |= I40E_FLAG_SERVICE_CLIENT_REQUESTED;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005482 i40e_service_event_schedule(pf);
5483
5484 return 0;
5485}
5486
5487/**
5488 * i40e_vsi_reinit_locked - Reset the VSI
5489 * @vsi: the VSI being configured
5490 *
5491 * Rebuild the ring structs after some configuration
5492 * has changed, e.g. MTU size.
5493 **/
5494static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi)
5495{
5496 struct i40e_pf *pf = vsi->back;
5497
5498 WARN_ON(in_interrupt());
5499 while (test_and_set_bit(__I40E_CONFIG_BUSY, &pf->state))
5500 usleep_range(1000, 2000);
5501 i40e_down(vsi);
5502
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005503 i40e_up(vsi);
5504 clear_bit(__I40E_CONFIG_BUSY, &pf->state);
5505}
5506
5507/**
5508 * i40e_up - Bring the connection back up after being down
5509 * @vsi: the VSI being configured
5510 **/
5511int i40e_up(struct i40e_vsi *vsi)
5512{
5513 int err;
5514
5515 err = i40e_vsi_configure(vsi);
5516 if (!err)
5517 err = i40e_up_complete(vsi);
5518
5519 return err;
5520}
5521
5522/**
5523 * i40e_down - Shutdown the connection processing
5524 * @vsi: the VSI being stopped
5525 **/
5526void i40e_down(struct i40e_vsi *vsi)
5527{
5528 int i;
5529
5530 /* It is assumed that the caller of this function
5531 * sets the vsi->state __I40E_DOWN bit.
5532 */
5533 if (vsi->netdev) {
5534 netif_carrier_off(vsi->netdev);
5535 netif_tx_disable(vsi->netdev);
5536 }
5537 i40e_vsi_disable_irq(vsi);
Filip Sadowski3aa7b742016-10-11 15:26:58 -07005538 i40e_vsi_stop_rings(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005539 i40e_napi_disable_all(vsi);
5540
5541 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck9f65e152013-09-28 06:00:58 +00005542 i40e_clean_tx_ring(vsi->tx_rings[i]);
5543 i40e_clean_rx_ring(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005544 }
Catherine Sullivanf980d442016-05-16 10:26:34 -07005545
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005546}
5547
5548/**
5549 * i40e_setup_tc - configure multiple traffic classes
5550 * @netdev: net device to configure
5551 * @tc: number of traffic classes to enable
5552 **/
5553static int i40e_setup_tc(struct net_device *netdev, u8 tc)
5554{
5555 struct i40e_netdev_priv *np = netdev_priv(netdev);
5556 struct i40e_vsi *vsi = np->vsi;
5557 struct i40e_pf *pf = vsi->back;
5558 u8 enabled_tc = 0;
5559 int ret = -EINVAL;
5560 int i;
5561
5562 /* Check if DCB enabled to continue */
5563 if (!(pf->flags & I40E_FLAG_DCB_ENABLED)) {
5564 netdev_info(netdev, "DCB is not enabled for adapter\n");
5565 goto exit;
5566 }
5567
5568 /* Check if MFP enabled */
5569 if (pf->flags & I40E_FLAG_MFP_ENABLED) {
5570 netdev_info(netdev, "Configuring TC not supported in MFP mode\n");
5571 goto exit;
5572 }
5573
5574 /* Check whether tc count is within enabled limit */
5575 if (tc > i40e_pf_get_num_tc(pf)) {
5576 netdev_info(netdev, "TC count greater than enabled on link for adapter\n");
5577 goto exit;
5578 }
5579
5580 /* Generate TC map for number of tc requested */
5581 for (i = 0; i < tc; i++)
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08005582 enabled_tc |= BIT(i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005583
5584 /* Requesting same TC configuration as already enabled */
5585 if (enabled_tc == vsi->tc_config.enabled_tc)
5586 return 0;
5587
5588 /* Quiesce VSI queues */
5589 i40e_quiesce_vsi(vsi);
5590
5591 /* Configure VSI for enabled TCs */
5592 ret = i40e_vsi_config_tc(vsi, enabled_tc);
5593 if (ret) {
5594 netdev_info(netdev, "Failed configuring TC for VSI seid=%d\n",
5595 vsi->seid);
5596 goto exit;
5597 }
5598
5599 /* Unquiesce VSI */
5600 i40e_unquiesce_vsi(vsi);
5601
5602exit:
5603 return ret;
5604}
5605
John Fastabende4c67342016-02-16 21:16:15 -08005606#ifdef I40E_FCOE
John Fastabend16e5cc62016-02-16 21:16:43 -08005607int __i40e_setup_tc(struct net_device *netdev, u32 handle, __be16 proto,
5608 struct tc_to_netdev *tc)
John Fastabende4c67342016-02-16 21:16:15 -08005609#else
John Fastabend16e5cc62016-02-16 21:16:43 -08005610static int __i40e_setup_tc(struct net_device *netdev, u32 handle, __be16 proto,
5611 struct tc_to_netdev *tc)
John Fastabende4c67342016-02-16 21:16:15 -08005612#endif
5613{
John Fastabend16e5cc62016-02-16 21:16:43 -08005614 if (handle != TC_H_ROOT || tc->type != TC_SETUP_MQPRIO)
John Fastabende4c67342016-02-16 21:16:15 -08005615 return -EINVAL;
John Fastabend16e5cc62016-02-16 21:16:43 -08005616 return i40e_setup_tc(netdev, tc->tc);
John Fastabende4c67342016-02-16 21:16:15 -08005617}
5618
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005619/**
5620 * i40e_open - Called when a network interface is made active
5621 * @netdev: network interface device structure
5622 *
5623 * The open entry point is called when a network interface is made
5624 * active by the system (IFF_UP). At this point all resources needed
5625 * for transmit and receive operations are allocated, the interrupt
5626 * handler is registered with the OS, the netdev watchdog subtask is
5627 * enabled, and the stack is notified that the interface is ready.
5628 *
5629 * Returns 0 on success, negative value on failure
5630 **/
Vasu Dev38e00432014-08-01 13:27:03 -07005631int i40e_open(struct net_device *netdev)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005632{
5633 struct i40e_netdev_priv *np = netdev_priv(netdev);
5634 struct i40e_vsi *vsi = np->vsi;
5635 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005636 int err;
5637
Shannon Nelson4eb3f762014-03-06 08:59:58 +00005638 /* disallow open during test or if eeprom is broken */
5639 if (test_bit(__I40E_TESTING, &pf->state) ||
5640 test_bit(__I40E_BAD_EEPROM, &pf->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005641 return -EBUSY;
5642
5643 netif_carrier_off(netdev);
5644
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005645 err = i40e_vsi_open(vsi);
5646 if (err)
5647 return err;
5648
Jesse Brandeburg059dab62014-04-01 09:07:20 +00005649 /* configure global TSO hardware offload settings */
5650 wr32(&pf->hw, I40E_GLLAN_TSOMSK_F, be32_to_cpu(TCP_FLAG_PSH |
5651 TCP_FLAG_FIN) >> 16);
5652 wr32(&pf->hw, I40E_GLLAN_TSOMSK_M, be32_to_cpu(TCP_FLAG_PSH |
5653 TCP_FLAG_FIN |
5654 TCP_FLAG_CWR) >> 16);
5655 wr32(&pf->hw, I40E_GLLAN_TSOMSK_L, be32_to_cpu(TCP_FLAG_CWR) >> 16);
5656
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07005657 udp_tunnel_get_rx_info(netdev);
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06005658
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005659 return 0;
5660}
5661
5662/**
5663 * i40e_vsi_open -
5664 * @vsi: the VSI to open
5665 *
5666 * Finish initialization of the VSI.
5667 *
5668 * Returns 0 on success, negative value on failure
5669 **/
5670int i40e_vsi_open(struct i40e_vsi *vsi)
5671{
5672 struct i40e_pf *pf = vsi->back;
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00005673 char int_name[I40E_INT_NAME_STR_LEN];
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005674 int err;
5675
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005676 /* allocate descriptors */
5677 err = i40e_vsi_setup_tx_resources(vsi);
5678 if (err)
5679 goto err_setup_tx;
5680 err = i40e_vsi_setup_rx_resources(vsi);
5681 if (err)
5682 goto err_setup_rx;
5683
5684 err = i40e_vsi_configure(vsi);
5685 if (err)
5686 goto err_setup_rx;
5687
Shannon Nelsonc22e3c62014-03-14 07:32:25 +00005688 if (vsi->netdev) {
5689 snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
5690 dev_driver_string(&pf->pdev->dev), vsi->netdev->name);
5691 err = i40e_vsi_request_irq(vsi, int_name);
5692 if (err)
5693 goto err_setup_rx;
5694
5695 /* Notify the stack of the actual queue counts. */
5696 err = netif_set_real_num_tx_queues(vsi->netdev,
5697 vsi->num_queue_pairs);
5698 if (err)
5699 goto err_set_queues;
5700
5701 err = netif_set_real_num_rx_queues(vsi->netdev,
5702 vsi->num_queue_pairs);
5703 if (err)
5704 goto err_set_queues;
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00005705
5706 } else if (vsi->type == I40E_VSI_FDIR) {
Carolyn Wybornye240f672014-12-11 07:06:37 +00005707 snprintf(int_name, sizeof(int_name) - 1, "%s-%s:fdir",
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +00005708 dev_driver_string(&pf->pdev->dev),
5709 dev_name(&pf->pdev->dev));
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00005710 err = i40e_vsi_request_irq(vsi, int_name);
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +00005711
Shannon Nelsonc22e3c62014-03-14 07:32:25 +00005712 } else {
Jean Sacrence9ccb12014-05-01 14:31:18 +00005713 err = -EINVAL;
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005714 goto err_setup_rx;
5715 }
Anjali Singhai Jain25946dd2013-11-26 10:49:14 +00005716
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005717 err = i40e_up_complete(vsi);
5718 if (err)
5719 goto err_up_complete;
5720
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005721 return 0;
5722
5723err_up_complete:
5724 i40e_down(vsi);
Anjali Singhai Jain25946dd2013-11-26 10:49:14 +00005725err_set_queues:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005726 i40e_vsi_free_irq(vsi);
5727err_setup_rx:
5728 i40e_vsi_free_rx_resources(vsi);
5729err_setup_tx:
5730 i40e_vsi_free_tx_resources(vsi);
5731 if (vsi == pf->vsi[pf->lan_vsi])
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005732 i40e_do_reset(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005733
5734 return err;
5735}
5736
5737/**
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00005738 * i40e_fdir_filter_exit - Cleans up the Flow Director accounting
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00005739 * @pf: Pointer to PF
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00005740 *
5741 * This function destroys the hlist where all the Flow Director
5742 * filters were saved.
5743 **/
5744static void i40e_fdir_filter_exit(struct i40e_pf *pf)
5745{
5746 struct i40e_fdir_filter *filter;
5747 struct hlist_node *node2;
5748
5749 hlist_for_each_entry_safe(filter, node2,
5750 &pf->fdir_filter_list, fdir_node) {
5751 hlist_del(&filter->fdir_node);
5752 kfree(filter);
5753 }
5754 pf->fdir_pf_active_filters = 0;
5755}
5756
5757/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005758 * i40e_close - Disables a network interface
5759 * @netdev: network interface device structure
5760 *
5761 * The close entry point is called when an interface is de-activated
5762 * by the OS. The hardware is still under the driver's control, but
5763 * this netdev interface is disabled.
5764 *
5765 * Returns 0, this is not allowed to fail
5766 **/
Vasu Dev38e00432014-08-01 13:27:03 -07005767int i40e_close(struct net_device *netdev)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005768{
5769 struct i40e_netdev_priv *np = netdev_priv(netdev);
5770 struct i40e_vsi *vsi = np->vsi;
5771
Shannon Nelson90ef8d42014-03-14 07:32:26 +00005772 i40e_vsi_close(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005773
5774 return 0;
5775}
5776
5777/**
5778 * i40e_do_reset - Start a PF or Core Reset sequence
5779 * @pf: board private structure
5780 * @reset_flags: which reset is requested
5781 *
5782 * The essential difference in resets is that the PF Reset
5783 * doesn't clear the packet buffers, doesn't reset the PE
5784 * firmware, and doesn't bother the other PFs on the chip.
5785 **/
5786void i40e_do_reset(struct i40e_pf *pf, u32 reset_flags)
5787{
5788 u32 val;
5789
5790 WARN_ON(in_interrupt());
5791
Mitch Williams263fc482014-04-23 04:50:11 +00005792
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005793 /* do the biggest reset indicated */
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005794 if (reset_flags & BIT_ULL(__I40E_GLOBAL_RESET_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005795
5796 /* Request a Global Reset
5797 *
5798 * This will start the chip's countdown to the actual full
5799 * chip reset event, and a warning interrupt to be sent
5800 * to all PFs, including the requestor. Our handler
5801 * for the warning interrupt will deal with the shutdown
5802 * and recovery of the switch setup.
5803 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005804 dev_dbg(&pf->pdev->dev, "GlobalR requested\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005805 val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
5806 val |= I40E_GLGEN_RTRIG_GLOBR_MASK;
5807 wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
5808
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005809 } else if (reset_flags & BIT_ULL(__I40E_CORE_RESET_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005810
5811 /* Request a Core Reset
5812 *
5813 * Same as Global Reset, except does *not* include the MAC/PHY
5814 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005815 dev_dbg(&pf->pdev->dev, "CoreR requested\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005816 val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
5817 val |= I40E_GLGEN_RTRIG_CORER_MASK;
5818 wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
5819 i40e_flush(&pf->hw);
5820
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005821 } else if (reset_flags & BIT_ULL(__I40E_PF_RESET_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005822
5823 /* Request a PF Reset
5824 *
5825 * Resets only the PF-specific registers
5826 *
5827 * This goes directly to the tear-down and rebuild of
5828 * the switch, since we need to do all the recovery as
5829 * for the Core Reset.
5830 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005831 dev_dbg(&pf->pdev->dev, "PFR requested\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005832 i40e_handle_reset_warning(pf);
5833
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005834 } else if (reset_flags & BIT_ULL(__I40E_REINIT_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005835 int v;
5836
5837 /* Find the VSI(s) that requested a re-init */
5838 dev_info(&pf->pdev->dev,
5839 "VSI reinit requested\n");
Mitch Williams505682c2014-05-20 08:01:37 +00005840 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005841 struct i40e_vsi *vsi = pf->vsi[v];
Jesse Brandeburg6995b362015-08-28 17:55:54 -04005842
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005843 if (vsi != NULL &&
5844 test_bit(__I40E_REINIT_REQUESTED, &vsi->state)) {
5845 i40e_vsi_reinit_locked(pf->vsi[v]);
5846 clear_bit(__I40E_REINIT_REQUESTED, &vsi->state);
5847 }
5848 }
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005849 } else if (reset_flags & BIT_ULL(__I40E_DOWN_REQUESTED)) {
Neerav Parikhb5d06f02014-06-03 23:50:17 +00005850 int v;
5851
5852 /* Find the VSI(s) that needs to be brought down */
5853 dev_info(&pf->pdev->dev, "VSI down requested\n");
5854 for (v = 0; v < pf->num_alloc_vsi; v++) {
5855 struct i40e_vsi *vsi = pf->vsi[v];
Jesse Brandeburg6995b362015-08-28 17:55:54 -04005856
Neerav Parikhb5d06f02014-06-03 23:50:17 +00005857 if (vsi != NULL &&
5858 test_bit(__I40E_DOWN_REQUESTED, &vsi->state)) {
5859 set_bit(__I40E_DOWN, &vsi->state);
5860 i40e_down(vsi);
5861 clear_bit(__I40E_DOWN_REQUESTED, &vsi->state);
5862 }
5863 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005864 } else {
5865 dev_info(&pf->pdev->dev,
5866 "bad reset request 0x%08x\n", reset_flags);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005867 }
5868}
5869
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005870#ifdef CONFIG_I40E_DCB
5871/**
5872 * i40e_dcb_need_reconfig - Check if DCB needs reconfig
5873 * @pf: board private structure
5874 * @old_cfg: current DCB config
5875 * @new_cfg: new DCB config
5876 **/
5877bool i40e_dcb_need_reconfig(struct i40e_pf *pf,
5878 struct i40e_dcbx_config *old_cfg,
5879 struct i40e_dcbx_config *new_cfg)
5880{
5881 bool need_reconfig = false;
5882
5883 /* Check if ETS configuration has changed */
5884 if (memcmp(&new_cfg->etscfg,
5885 &old_cfg->etscfg,
5886 sizeof(new_cfg->etscfg))) {
5887 /* If Priority Table has changed reconfig is needed */
5888 if (memcmp(&new_cfg->etscfg.prioritytable,
5889 &old_cfg->etscfg.prioritytable,
5890 sizeof(new_cfg->etscfg.prioritytable))) {
5891 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005892 dev_dbg(&pf->pdev->dev, "ETS UP2TC changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005893 }
5894
5895 if (memcmp(&new_cfg->etscfg.tcbwtable,
5896 &old_cfg->etscfg.tcbwtable,
5897 sizeof(new_cfg->etscfg.tcbwtable)))
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005898 dev_dbg(&pf->pdev->dev, "ETS TC BW Table changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005899
5900 if (memcmp(&new_cfg->etscfg.tsatable,
5901 &old_cfg->etscfg.tsatable,
5902 sizeof(new_cfg->etscfg.tsatable)))
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005903 dev_dbg(&pf->pdev->dev, "ETS TSA Table changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005904 }
5905
5906 /* Check if PFC configuration has changed */
5907 if (memcmp(&new_cfg->pfc,
5908 &old_cfg->pfc,
5909 sizeof(new_cfg->pfc))) {
5910 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005911 dev_dbg(&pf->pdev->dev, "PFC config change detected.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005912 }
5913
5914 /* Check if APP Table has changed */
5915 if (memcmp(&new_cfg->app,
5916 &old_cfg->app,
Dave Jones3d9667a2014-01-27 23:11:09 -05005917 sizeof(new_cfg->app))) {
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005918 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005919 dev_dbg(&pf->pdev->dev, "APP Table change detected.\n");
Dave Jones3d9667a2014-01-27 23:11:09 -05005920 }
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005921
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04005922 dev_dbg(&pf->pdev->dev, "dcb need_reconfig=%d\n", need_reconfig);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005923 return need_reconfig;
5924}
5925
5926/**
5927 * i40e_handle_lldp_event - Handle LLDP Change MIB event
5928 * @pf: board private structure
5929 * @e: event info posted on ARQ
5930 **/
5931static int i40e_handle_lldp_event(struct i40e_pf *pf,
5932 struct i40e_arq_event_info *e)
5933{
5934 struct i40e_aqc_lldp_get_mib *mib =
5935 (struct i40e_aqc_lldp_get_mib *)&e->desc.params.raw;
5936 struct i40e_hw *hw = &pf->hw;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005937 struct i40e_dcbx_config tmp_dcbx_cfg;
5938 bool need_reconfig = false;
5939 int ret = 0;
5940 u8 type;
5941
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005942 /* Not DCB capable or capability disabled */
David Ertmanea6acb72016-09-20 07:10:50 -07005943 if (!(pf->flags & I40E_FLAG_DCB_CAPABLE))
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005944 return ret;
5945
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005946 /* Ignore if event is not for Nearest Bridge */
5947 type = ((mib->type >> I40E_AQ_LLDP_BRIDGE_TYPE_SHIFT)
5948 & I40E_AQ_LLDP_BRIDGE_TYPE_MASK);
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04005949 dev_dbg(&pf->pdev->dev, "LLDP event mib bridge type 0x%x\n", type);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005950 if (type != I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE)
5951 return ret;
5952
5953 /* Check MIB Type and return if event for Remote MIB update */
5954 type = mib->type & I40E_AQ_LLDP_MIB_TYPE_MASK;
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005955 dev_dbg(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04005956 "LLDP event mib type %s\n", type ? "remote" : "local");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005957 if (type == I40E_AQ_LLDP_MIB_REMOTE) {
5958 /* Update the remote cached instance and return */
5959 ret = i40e_aq_get_dcb_config(hw, I40E_AQ_LLDP_MIB_REMOTE,
5960 I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE,
5961 &hw->remote_dcbx_config);
5962 goto exit;
5963 }
5964
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005965 /* Store the old configuration */
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07005966 tmp_dcbx_cfg = hw->local_dcbx_config;
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005967
Neerav Parikh750fcbc2015-02-24 06:58:47 +00005968 /* Reset the old DCBx configuration data */
5969 memset(&hw->local_dcbx_config, 0, sizeof(hw->local_dcbx_config));
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005970 /* Get updated DCBX data from firmware */
5971 ret = i40e_get_dcb_config(&pf->hw);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005972 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005973 dev_info(&pf->pdev->dev,
5974 "Failed querying DCB configuration data from firmware, err %s aq_err %s\n",
5975 i40e_stat_str(&pf->hw, ret),
5976 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005977 goto exit;
5978 }
5979
5980 /* No change detected in DCBX configs */
Neerav Parikh750fcbc2015-02-24 06:58:47 +00005981 if (!memcmp(&tmp_dcbx_cfg, &hw->local_dcbx_config,
5982 sizeof(tmp_dcbx_cfg))) {
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005983 dev_dbg(&pf->pdev->dev, "No change detected in DCBX configuration.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005984 goto exit;
5985 }
5986
Neerav Parikh750fcbc2015-02-24 06:58:47 +00005987 need_reconfig = i40e_dcb_need_reconfig(pf, &tmp_dcbx_cfg,
5988 &hw->local_dcbx_config);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005989
Neerav Parikh750fcbc2015-02-24 06:58:47 +00005990 i40e_dcbnl_flush_apps(pf, &tmp_dcbx_cfg, &hw->local_dcbx_config);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005991
5992 if (!need_reconfig)
5993 goto exit;
5994
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005995 /* Enable DCB tagging only when more than one TC */
Neerav Parikh750fcbc2015-02-24 06:58:47 +00005996 if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005997 pf->flags |= I40E_FLAG_DCB_ENABLED;
5998 else
5999 pf->flags &= ~I40E_FLAG_DCB_ENABLED;
6000
Neerav Parikh69129dc2014-11-12 00:18:46 +00006001 set_bit(__I40E_PORT_TX_SUSPENDED, &pf->state);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006002 /* Reconfiguration needed quiesce all VSIs */
6003 i40e_pf_quiesce_all_vsi(pf);
6004
6005 /* Changes in configuration update VEB/VSI */
6006 i40e_dcb_reconfigure(pf);
6007
Neerav Parikh2fd75f32014-11-12 00:18:20 +00006008 ret = i40e_resume_port_tx(pf);
6009
Neerav Parikh69129dc2014-11-12 00:18:46 +00006010 clear_bit(__I40E_PORT_TX_SUSPENDED, &pf->state);
Neerav Parikh2fd75f32014-11-12 00:18:20 +00006011 /* In case of error no point in resuming VSIs */
Neerav Parikh69129dc2014-11-12 00:18:46 +00006012 if (ret)
6013 goto exit;
6014
Neerav Parikh3fe06f42016-02-17 16:12:15 -08006015 /* Wait for the PF's queues to be disabled */
6016 ret = i40e_pf_wait_queues_disabled(pf);
Parikh, Neerav11e47702015-02-21 06:43:55 +00006017 if (ret) {
6018 /* Schedule PF reset to recover */
6019 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
6020 i40e_service_event_schedule(pf);
6021 } else {
Neerav Parikh2fd75f32014-11-12 00:18:20 +00006022 i40e_pf_unquiesce_all_vsi(pf);
Mitch Williams0ef2d5a2017-01-24 10:24:00 -08006023 pf->flags |= (I40E_FLAG_SERVICE_CLIENT_REQUESTED |
6024 I40E_FLAG_CLIENT_L2_CHANGE);
Parikh, Neerav11e47702015-02-21 06:43:55 +00006025 }
6026
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006027exit:
6028 return ret;
6029}
6030#endif /* CONFIG_I40E_DCB */
6031
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006032/**
Anjali Singhai Jain23326182013-11-26 10:49:22 +00006033 * i40e_do_reset_safe - Protected reset path for userland calls.
6034 * @pf: board private structure
6035 * @reset_flags: which reset is requested
6036 *
6037 **/
6038void i40e_do_reset_safe(struct i40e_pf *pf, u32 reset_flags)
6039{
6040 rtnl_lock();
6041 i40e_do_reset(pf, reset_flags);
6042 rtnl_unlock();
6043}
6044
6045/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006046 * i40e_handle_lan_overflow_event - Handler for LAN queue overflow event
6047 * @pf: board private structure
6048 * @e: event info posted on ARQ
6049 *
6050 * Handler for LAN Queue Overflow Event generated by the firmware for PF
6051 * and VF queues
6052 **/
6053static void i40e_handle_lan_overflow_event(struct i40e_pf *pf,
6054 struct i40e_arq_event_info *e)
6055{
6056 struct i40e_aqc_lan_overflow *data =
6057 (struct i40e_aqc_lan_overflow *)&e->desc.params.raw;
6058 u32 queue = le32_to_cpu(data->prtdcb_rupto);
6059 u32 qtx_ctl = le32_to_cpu(data->otx_ctl);
6060 struct i40e_hw *hw = &pf->hw;
6061 struct i40e_vf *vf;
6062 u16 vf_id;
6063
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006064 dev_dbg(&pf->pdev->dev, "overflow Rx Queue Number = %d QTX_CTL=0x%08x\n",
6065 queue, qtx_ctl);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006066
6067 /* Queue belongs to VF, find the VF and issue VF reset */
6068 if (((qtx_ctl & I40E_QTX_CTL_PFVF_Q_MASK)
6069 >> I40E_QTX_CTL_PFVF_Q_SHIFT) == I40E_QTX_CTL_VF_QUEUE) {
6070 vf_id = (u16)((qtx_ctl & I40E_QTX_CTL_VFVM_INDX_MASK)
6071 >> I40E_QTX_CTL_VFVM_INDX_SHIFT);
6072 vf_id -= hw->func_caps.vf_base_id;
6073 vf = &pf->vf[vf_id];
6074 i40e_vc_notify_vf_reset(vf);
6075 /* Allow VF to process pending reset notification */
6076 msleep(20);
6077 i40e_reset_vf(vf, false);
6078 }
6079}
6080
6081/**
Anjali Singhai Jain12957382014-06-04 04:22:47 +00006082 * i40e_get_cur_guaranteed_fd_count - Get the consumed guaranteed FD filters
6083 * @pf: board private structure
6084 **/
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006085u32 i40e_get_cur_guaranteed_fd_count(struct i40e_pf *pf)
Anjali Singhai Jain12957382014-06-04 04:22:47 +00006086{
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006087 u32 val, fcnt_prog;
Anjali Singhai Jain12957382014-06-04 04:22:47 +00006088
6089 val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
6090 fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK);
6091 return fcnt_prog;
6092}
6093
6094/**
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006095 * i40e_get_current_fd_count - Get total FD filters programmed for this PF
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006096 * @pf: board private structure
6097 **/
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006098u32 i40e_get_current_fd_count(struct i40e_pf *pf)
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006099{
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006100 u32 val, fcnt_prog;
6101
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006102 val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
6103 fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK) +
6104 ((val & I40E_PFQF_FDSTAT_BEST_CNT_MASK) >>
6105 I40E_PFQF_FDSTAT_BEST_CNT_SHIFT);
6106 return fcnt_prog;
6107}
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006108
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006109/**
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006110 * i40e_get_global_fd_count - Get total FD filters programmed on device
6111 * @pf: board private structure
6112 **/
6113u32 i40e_get_global_fd_count(struct i40e_pf *pf)
6114{
6115 u32 val, fcnt_prog;
6116
6117 val = rd32(&pf->hw, I40E_GLQF_FDCNT_0);
6118 fcnt_prog = (val & I40E_GLQF_FDCNT_0_GUARANT_CNT_MASK) +
6119 ((val & I40E_GLQF_FDCNT_0_BESTCNT_MASK) >>
6120 I40E_GLQF_FDCNT_0_BESTCNT_SHIFT);
6121 return fcnt_prog;
6122}
6123
6124/**
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006125 * i40e_fdir_check_and_reenable - Function to reenabe FD ATR or SB if disabled
6126 * @pf: board private structure
6127 **/
6128void i40e_fdir_check_and_reenable(struct i40e_pf *pf)
6129{
Carolyn Wyborny3487b6c2015-08-27 11:42:38 -04006130 struct i40e_fdir_filter *filter;
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006131 u32 fcnt_prog, fcnt_avail;
Carolyn Wyborny3487b6c2015-08-27 11:42:38 -04006132 struct hlist_node *node;
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006133
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006134 if (test_bit(__I40E_FD_FLUSH_REQUESTED, &pf->state))
6135 return;
6136
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006137 /* Check if, FD SB or ATR was auto disabled and if there is enough room
6138 * to re-enable
6139 */
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006140 fcnt_prog = i40e_get_global_fd_count(pf);
Anjali Singhai Jain12957382014-06-04 04:22:47 +00006141 fcnt_avail = pf->fdir_pf_filter_count;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006142 if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM)) ||
6143 (pf->fd_add_err == 0) ||
6144 (i40e_get_current_atr_cnt(pf) < pf->fd_atr_cnt)) {
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006145 if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
6146 (pf->auto_disable_flags & I40E_FLAG_FD_SB_ENABLED)) {
6147 pf->auto_disable_flags &= ~I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jain2e4875e2015-04-16 20:06:06 -04006148 if (I40E_DEBUG_FD & pf->hw.debug_mask)
6149 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 +00006150 }
6151 }
Jacob Kellera3417d22016-09-06 18:05:10 -07006152
6153 /* Wait for some more space to be available to turn on ATR. We also
6154 * must check that no existing ntuple rules for TCP are in effect
6155 */
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006156 if (fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM * 2)) {
6157 if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
Jacob Kellera3417d22016-09-06 18:05:10 -07006158 (pf->auto_disable_flags & I40E_FLAG_FD_ATR_ENABLED) &&
6159 (pf->fd_tcp_rule == 0)) {
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006160 pf->auto_disable_flags &= ~I40E_FLAG_FD_ATR_ENABLED;
Anjali Singhai Jain2e4875e2015-04-16 20:06:06 -04006161 if (I40E_DEBUG_FD & pf->hw.debug_mask)
Jacob Kellera3417d22016-09-06 18:05:10 -07006162 dev_info(&pf->pdev->dev, "ATR is being enabled since we have space in the table and there are no conflicting ntuple rules\n");
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006163 }
6164 }
Carolyn Wyborny3487b6c2015-08-27 11:42:38 -04006165
6166 /* if hw had a problem adding a filter, delete it */
6167 if (pf->fd_inv > 0) {
6168 hlist_for_each_entry_safe(filter, node,
6169 &pf->fdir_filter_list, fdir_node) {
6170 if (filter->fd_id == pf->fd_inv) {
6171 hlist_del(&filter->fdir_node);
6172 kfree(filter);
6173 pf->fdir_pf_active_filters--;
6174 }
6175 }
6176 }
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006177}
6178
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006179#define I40E_MIN_FD_FLUSH_INTERVAL 10
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006180#define I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE 30
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006181/**
6182 * i40e_fdir_flush_and_replay - Function to flush all FD filters and replay SB
6183 * @pf: board private structure
6184 **/
6185static void i40e_fdir_flush_and_replay(struct i40e_pf *pf)
6186{
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006187 unsigned long min_flush_time;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006188 int flush_wait_retry = 50;
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006189 bool disable_atr = false;
6190 int fd_room;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006191 int reg;
6192
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04006193 if (!time_after(jiffies, pf->fd_flush_timestamp +
6194 (I40E_MIN_FD_FLUSH_INTERVAL * HZ)))
6195 return;
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006196
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04006197 /* If the flush is happening too quick and we have mostly SB rules we
6198 * should not re-enable ATR for some time.
6199 */
6200 min_flush_time = pf->fd_flush_timestamp +
6201 (I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE * HZ);
6202 fd_room = pf->fdir_pf_filter_count - pf->fdir_pf_active_filters;
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006203
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04006204 if (!(time_after(jiffies, min_flush_time)) &&
6205 (fd_room < I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) {
6206 if (I40E_DEBUG_FD & pf->hw.debug_mask)
6207 dev_info(&pf->pdev->dev, "ATR disabled, not enough FD filter space.\n");
6208 disable_atr = true;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006209 }
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04006210
6211 pf->fd_flush_timestamp = jiffies;
Jacob Keller234dc4e2016-09-06 18:05:09 -07006212 pf->auto_disable_flags |= I40E_FLAG_FD_ATR_ENABLED;
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04006213 /* flush all filters */
6214 wr32(&pf->hw, I40E_PFQF_CTL_1,
6215 I40E_PFQF_CTL_1_CLEARFDTABLE_MASK);
6216 i40e_flush(&pf->hw);
6217 pf->fd_flush_cnt++;
6218 pf->fd_add_err = 0;
6219 do {
6220 /* Check FD flush status every 5-6msec */
6221 usleep_range(5000, 6000);
6222 reg = rd32(&pf->hw, I40E_PFQF_CTL_1);
6223 if (!(reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK))
6224 break;
6225 } while (flush_wait_retry--);
6226 if (reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK) {
6227 dev_warn(&pf->pdev->dev, "FD table did not flush, needs more time\n");
6228 } else {
6229 /* replay sideband filters */
6230 i40e_fdir_filter_restore(pf->vsi[pf->lan_vsi]);
6231 if (!disable_atr)
Jacob Keller234dc4e2016-09-06 18:05:09 -07006232 pf->auto_disable_flags &= ~I40E_FLAG_FD_ATR_ENABLED;
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04006233 clear_bit(__I40E_FD_FLUSH_REQUESTED, &pf->state);
6234 if (I40E_DEBUG_FD & pf->hw.debug_mask)
6235 dev_info(&pf->pdev->dev, "FD Filter table flushed and FD-SB replayed.\n");
6236 }
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006237}
6238
6239/**
6240 * i40e_get_current_atr_count - Get the count of total FD ATR filters programmed
6241 * @pf: board private structure
6242 **/
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006243u32 i40e_get_current_atr_cnt(struct i40e_pf *pf)
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006244{
6245 return i40e_get_current_fd_count(pf) - pf->fdir_pf_active_filters;
6246}
6247
6248/* We can see up to 256 filter programming desc in transit if the filters are
6249 * being applied really fast; before we see the first
6250 * filter miss error on Rx queue 0. Accumulating enough error messages before
6251 * reacting will make sure we don't cause flush too often.
6252 */
6253#define I40E_MAX_FD_PROGRAM_ERROR 256
6254
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006255/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006256 * i40e_fdir_reinit_subtask - Worker thread to reinit FDIR filter table
6257 * @pf: board private structure
6258 **/
6259static void i40e_fdir_reinit_subtask(struct i40e_pf *pf)
6260{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006261
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006262 /* if interface is down do nothing */
6263 if (test_bit(__I40E_DOWN, &pf->state))
6264 return;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006265
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006266 if (test_bit(__I40E_FD_FLUSH_REQUESTED, &pf->state))
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006267 i40e_fdir_flush_and_replay(pf);
6268
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006269 i40e_fdir_check_and_reenable(pf);
6270
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006271}
6272
6273/**
6274 * i40e_vsi_link_event - notify VSI of a link event
6275 * @vsi: vsi to be notified
6276 * @link_up: link up or down
6277 **/
6278static void i40e_vsi_link_event(struct i40e_vsi *vsi, bool link_up)
6279{
Jesse Brandeburg32b5b812014-08-12 06:33:14 +00006280 if (!vsi || test_bit(__I40E_DOWN, &vsi->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006281 return;
6282
6283 switch (vsi->type) {
6284 case I40E_VSI_MAIN:
Vasu Dev38e00432014-08-01 13:27:03 -07006285#ifdef I40E_FCOE
6286 case I40E_VSI_FCOE:
6287#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006288 if (!vsi->netdev || !vsi->netdev_registered)
6289 break;
6290
6291 if (link_up) {
6292 netif_carrier_on(vsi->netdev);
6293 netif_tx_wake_all_queues(vsi->netdev);
6294 } else {
6295 netif_carrier_off(vsi->netdev);
6296 netif_tx_stop_all_queues(vsi->netdev);
6297 }
6298 break;
6299
6300 case I40E_VSI_SRIOV:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006301 case I40E_VSI_VMDQ2:
6302 case I40E_VSI_CTRL:
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06006303 case I40E_VSI_IWARP:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006304 case I40E_VSI_MIRROR:
6305 default:
6306 /* there is no notification for other VSIs */
6307 break;
6308 }
6309}
6310
6311/**
6312 * i40e_veb_link_event - notify elements on the veb of a link event
6313 * @veb: veb to be notified
6314 * @link_up: link up or down
6315 **/
6316static void i40e_veb_link_event(struct i40e_veb *veb, bool link_up)
6317{
6318 struct i40e_pf *pf;
6319 int i;
6320
6321 if (!veb || !veb->pf)
6322 return;
6323 pf = veb->pf;
6324
6325 /* depth first... */
6326 for (i = 0; i < I40E_MAX_VEB; i++)
6327 if (pf->veb[i] && (pf->veb[i]->uplink_seid == veb->seid))
6328 i40e_veb_link_event(pf->veb[i], link_up);
6329
6330 /* ... now the local VSIs */
Mitch Williams505682c2014-05-20 08:01:37 +00006331 for (i = 0; i < pf->num_alloc_vsi; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006332 if (pf->vsi[i] && (pf->vsi[i]->uplink_seid == veb->seid))
6333 i40e_vsi_link_event(pf->vsi[i], link_up);
6334}
6335
6336/**
6337 * i40e_link_event - Update netif_carrier status
6338 * @pf: board private structure
6339 **/
6340static void i40e_link_event(struct i40e_pf *pf)
6341{
Mitch Williams320684c2014-10-17 03:14:43 +00006342 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
Catherine Sullivanfef59dd2014-12-11 07:06:33 +00006343 u8 new_link_speed, old_link_speed;
Jesse Brandeburga72a5abc2015-08-26 15:14:19 -04006344 i40e_status status;
6345 bool new_link, old_link;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006346
Catherine Sullivan1f9610e2015-10-21 19:47:09 -04006347 /* save off old link status information */
6348 pf->hw.phy.link_info_old = pf->hw.phy.link_info;
6349
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00006350 /* set this to force the get_link_status call to refresh state */
6351 pf->hw.phy.get_link_info = true;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006352
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00006353 old_link = (pf->hw.phy.link_info_old.link_info & I40E_AQ_LINK_UP);
Jesse Brandeburga72a5abc2015-08-26 15:14:19 -04006354
6355 status = i40e_get_link_status(&pf->hw, &new_link);
Harshitha Ramamurthyae136702016-12-12 15:44:16 -08006356
6357 /* On success, disable temp link polling */
6358 if (status == I40E_SUCCESS) {
6359 if (pf->flags & I40E_FLAG_TEMP_LINK_POLLING)
6360 pf->flags &= ~I40E_FLAG_TEMP_LINK_POLLING;
6361 } else {
6362 /* Enable link polling temporarily until i40e_get_link_status
6363 * returns I40E_SUCCESS
6364 */
6365 pf->flags |= I40E_FLAG_TEMP_LINK_POLLING;
Jesse Brandeburga72a5abc2015-08-26 15:14:19 -04006366 dev_dbg(&pf->pdev->dev, "couldn't get link state, status: %d\n",
6367 status);
6368 return;
6369 }
6370
Catherine Sullivanfef59dd2014-12-11 07:06:33 +00006371 old_link_speed = pf->hw.phy.link_info_old.link_speed;
6372 new_link_speed = pf->hw.phy.link_info.link_speed;
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00006373
6374 if (new_link == old_link &&
Catherine Sullivanfef59dd2014-12-11 07:06:33 +00006375 new_link_speed == old_link_speed &&
Mitch Williams320684c2014-10-17 03:14:43 +00006376 (test_bit(__I40E_DOWN, &vsi->state) ||
6377 new_link == netif_carrier_ok(vsi->netdev)))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006378 return;
Mitch Williams320684c2014-10-17 03:14:43 +00006379
6380 if (!test_bit(__I40E_DOWN, &vsi->state))
6381 i40e_print_link_message(vsi, new_link);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006382
6383 /* Notify the base of the switch tree connected to
6384 * the link. Floating VEBs are not notified.
6385 */
6386 if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
6387 i40e_veb_link_event(pf->veb[pf->lan_veb], new_link);
6388 else
Mitch Williams320684c2014-10-17 03:14:43 +00006389 i40e_vsi_link_event(vsi, new_link);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006390
6391 if (pf->vf)
6392 i40e_vc_notify_link_state(pf);
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00006393
6394 if (pf->flags & I40E_FLAG_PTP)
6395 i40e_ptp_set_increment(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006396}
6397
6398/**
Shannon Nelson21536712014-10-25 10:35:25 +00006399 * i40e_watchdog_subtask - periodic checks not using event driven response
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006400 * @pf: board private structure
6401 **/
6402static void i40e_watchdog_subtask(struct i40e_pf *pf)
6403{
6404 int i;
6405
6406 /* if interface is down do nothing */
6407 if (test_bit(__I40E_DOWN, &pf->state) ||
6408 test_bit(__I40E_CONFIG_BUSY, &pf->state))
6409 return;
6410
Shannon Nelson21536712014-10-25 10:35:25 +00006411 /* make sure we don't do these things too often */
6412 if (time_before(jiffies, (pf->service_timer_previous +
6413 pf->service_timer_period)))
6414 return;
6415 pf->service_timer_previous = jiffies;
6416
Harshitha Ramamurthyae136702016-12-12 15:44:16 -08006417 if ((pf->flags & I40E_FLAG_LINK_POLLING_ENABLED) ||
6418 (pf->flags & I40E_FLAG_TEMP_LINK_POLLING))
Shannon Nelson9ac77262015-08-27 11:42:40 -04006419 i40e_link_event(pf);
Shannon Nelson21536712014-10-25 10:35:25 +00006420
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006421 /* Update the stats for active netdevs so the network stack
6422 * can look at updated numbers whenever it cares to
6423 */
Mitch Williams505682c2014-05-20 08:01:37 +00006424 for (i = 0; i < pf->num_alloc_vsi; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006425 if (pf->vsi[i] && pf->vsi[i]->netdev)
6426 i40e_update_stats(pf->vsi[i]);
6427
Anjali Singhai Jaind1a8d272015-07-23 16:54:40 -04006428 if (pf->flags & I40E_FLAG_VEB_STATS_ENABLED) {
6429 /* Update the stats for the active switching components */
6430 for (i = 0; i < I40E_MAX_VEB; i++)
6431 if (pf->veb[i])
6432 i40e_update_veb_stats(pf->veb[i]);
6433 }
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00006434
6435 i40e_ptp_rx_hang(pf->vsi[pf->lan_vsi]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006436}
6437
6438/**
6439 * i40e_reset_subtask - Set up for resetting the device and driver
6440 * @pf: board private structure
6441 **/
6442static void i40e_reset_subtask(struct i40e_pf *pf)
6443{
6444 u32 reset_flags = 0;
6445
Anjali Singhai Jain23326182013-11-26 10:49:22 +00006446 rtnl_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006447 if (test_bit(__I40E_REINIT_REQUESTED, &pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006448 reset_flags |= BIT(__I40E_REINIT_REQUESTED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006449 clear_bit(__I40E_REINIT_REQUESTED, &pf->state);
6450 }
6451 if (test_bit(__I40E_PF_RESET_REQUESTED, &pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006452 reset_flags |= BIT(__I40E_PF_RESET_REQUESTED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006453 clear_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
6454 }
6455 if (test_bit(__I40E_CORE_RESET_REQUESTED, &pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006456 reset_flags |= BIT(__I40E_CORE_RESET_REQUESTED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006457 clear_bit(__I40E_CORE_RESET_REQUESTED, &pf->state);
6458 }
6459 if (test_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006460 reset_flags |= BIT(__I40E_GLOBAL_RESET_REQUESTED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006461 clear_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state);
6462 }
Neerav Parikhb5d06f02014-06-03 23:50:17 +00006463 if (test_bit(__I40E_DOWN_REQUESTED, &pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006464 reset_flags |= BIT(__I40E_DOWN_REQUESTED);
Neerav Parikhb5d06f02014-06-03 23:50:17 +00006465 clear_bit(__I40E_DOWN_REQUESTED, &pf->state);
6466 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006467
6468 /* If there's a recovery already waiting, it takes
6469 * precedence before starting a new reset sequence.
6470 */
6471 if (test_bit(__I40E_RESET_INTR_RECEIVED, &pf->state)) {
6472 i40e_handle_reset_warning(pf);
Anjali Singhai Jain23326182013-11-26 10:49:22 +00006473 goto unlock;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006474 }
6475
6476 /* If we're already down or resetting, just bail */
6477 if (reset_flags &&
6478 !test_bit(__I40E_DOWN, &pf->state) &&
6479 !test_bit(__I40E_CONFIG_BUSY, &pf->state))
6480 i40e_do_reset(pf, reset_flags);
Anjali Singhai Jain23326182013-11-26 10:49:22 +00006481
6482unlock:
6483 rtnl_unlock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006484}
6485
6486/**
6487 * i40e_handle_link_event - Handle link event
6488 * @pf: board private structure
6489 * @e: event info posted on ARQ
6490 **/
6491static void i40e_handle_link_event(struct i40e_pf *pf,
6492 struct i40e_arq_event_info *e)
6493{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006494 struct i40e_aqc_get_link_status *status =
6495 (struct i40e_aqc_get_link_status *)&e->desc.params.raw;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006496
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00006497 /* Do a new status request to re-enable LSE reporting
6498 * and load new status information into the hw struct
6499 * This completely ignores any state information
6500 * in the ARQ event info, instead choosing to always
6501 * issue the AQ update link status command.
6502 */
6503 i40e_link_event(pf);
6504
Carolyn Wyborny7b592f62014-07-10 07:58:19 +00006505 /* check for unqualified module, if link is down */
6506 if ((status->link_info & I40E_AQ_MEDIA_AVAILABLE) &&
6507 (!(status->an_info & I40E_AQ_QUALIFIED_MODULE)) &&
6508 (!(status->link_info & I40E_AQ_LINK_UP)))
6509 dev_err(&pf->pdev->dev,
6510 "The driver failed to link because an unqualified module was detected.\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006511}
6512
6513/**
6514 * i40e_clean_adminq_subtask - Clean the AdminQ rings
6515 * @pf: board private structure
6516 **/
6517static void i40e_clean_adminq_subtask(struct i40e_pf *pf)
6518{
6519 struct i40e_arq_event_info event;
6520 struct i40e_hw *hw = &pf->hw;
6521 u16 pending, i = 0;
6522 i40e_status ret;
6523 u16 opcode;
Shannon Nelson86df2422014-05-20 08:01:35 +00006524 u32 oldval;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006525 u32 val;
6526
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00006527 /* Do not run clean AQ when PF reset fails */
6528 if (test_bit(__I40E_RESET_FAILED, &pf->state))
6529 return;
6530
Shannon Nelson86df2422014-05-20 08:01:35 +00006531 /* check for error indications */
6532 val = rd32(&pf->hw, pf->hw.aq.arq.len);
6533 oldval = val;
6534 if (val & I40E_PF_ARQLEN_ARQVFE_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006535 if (hw->debug_mask & I40E_DEBUG_AQ)
6536 dev_info(&pf->pdev->dev, "ARQ VF Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006537 val &= ~I40E_PF_ARQLEN_ARQVFE_MASK;
6538 }
6539 if (val & I40E_PF_ARQLEN_ARQOVFL_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006540 if (hw->debug_mask & I40E_DEBUG_AQ)
6541 dev_info(&pf->pdev->dev, "ARQ Overflow Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006542 val &= ~I40E_PF_ARQLEN_ARQOVFL_MASK;
Mitch Williams1d0a4ad2015-12-23 12:05:48 -08006543 pf->arq_overflows++;
Shannon Nelson86df2422014-05-20 08:01:35 +00006544 }
6545 if (val & I40E_PF_ARQLEN_ARQCRIT_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006546 if (hw->debug_mask & I40E_DEBUG_AQ)
6547 dev_info(&pf->pdev->dev, "ARQ Critical Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006548 val &= ~I40E_PF_ARQLEN_ARQCRIT_MASK;
6549 }
6550 if (oldval != val)
6551 wr32(&pf->hw, pf->hw.aq.arq.len, val);
6552
6553 val = rd32(&pf->hw, pf->hw.aq.asq.len);
6554 oldval = val;
6555 if (val & I40E_PF_ATQLEN_ATQVFE_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006556 if (pf->hw.debug_mask & I40E_DEBUG_AQ)
6557 dev_info(&pf->pdev->dev, "ASQ VF Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006558 val &= ~I40E_PF_ATQLEN_ATQVFE_MASK;
6559 }
6560 if (val & I40E_PF_ATQLEN_ATQOVFL_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006561 if (pf->hw.debug_mask & I40E_DEBUG_AQ)
6562 dev_info(&pf->pdev->dev, "ASQ Overflow Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006563 val &= ~I40E_PF_ATQLEN_ATQOVFL_MASK;
6564 }
6565 if (val & I40E_PF_ATQLEN_ATQCRIT_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006566 if (pf->hw.debug_mask & I40E_DEBUG_AQ)
6567 dev_info(&pf->pdev->dev, "ASQ Critical Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006568 val &= ~I40E_PF_ATQLEN_ATQCRIT_MASK;
6569 }
6570 if (oldval != val)
6571 wr32(&pf->hw, pf->hw.aq.asq.len, val);
6572
Mitch Williams1001dc32014-11-11 20:02:19 +00006573 event.buf_len = I40E_MAX_AQ_BUF_SIZE;
6574 event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006575 if (!event.msg_buf)
6576 return;
6577
6578 do {
6579 ret = i40e_clean_arq_element(hw, &event, &pending);
Mitch Williams56497972014-06-04 08:45:18 +00006580 if (ret == I40E_ERR_ADMIN_QUEUE_NO_WORK)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006581 break;
Mitch Williams56497972014-06-04 08:45:18 +00006582 else if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006583 dev_info(&pf->pdev->dev, "ARQ event error %d\n", ret);
6584 break;
6585 }
6586
6587 opcode = le16_to_cpu(event.desc.opcode);
6588 switch (opcode) {
6589
6590 case i40e_aqc_opc_get_link_status:
6591 i40e_handle_link_event(pf, &event);
6592 break;
6593 case i40e_aqc_opc_send_msg_to_pf:
6594 ret = i40e_vc_process_vf_msg(pf,
6595 le16_to_cpu(event.desc.retval),
6596 le32_to_cpu(event.desc.cookie_high),
6597 le32_to_cpu(event.desc.cookie_low),
6598 event.msg_buf,
Mitch Williams1001dc32014-11-11 20:02:19 +00006599 event.msg_len);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006600 break;
6601 case i40e_aqc_opc_lldp_update_mib:
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006602 dev_dbg(&pf->pdev->dev, "ARQ: Update LLDP MIB event received\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006603#ifdef CONFIG_I40E_DCB
6604 rtnl_lock();
6605 ret = i40e_handle_lldp_event(pf, &event);
6606 rtnl_unlock();
6607#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006608 break;
6609 case i40e_aqc_opc_event_lan_overflow:
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006610 dev_dbg(&pf->pdev->dev, "ARQ LAN queue overflow event received\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006611 i40e_handle_lan_overflow_event(pf, &event);
6612 break;
Shannon Nelson0467bc92013-12-18 13:45:58 +00006613 case i40e_aqc_opc_send_msg_to_peer:
6614 dev_info(&pf->pdev->dev, "ARQ: Msg from other pf\n");
6615 break;
Shannon Nelson91a0f932015-03-19 14:32:01 -07006616 case i40e_aqc_opc_nvm_erase:
6617 case i40e_aqc_opc_nvm_update:
Michal Kosiarz00ada502015-11-19 11:34:20 -08006618 case i40e_aqc_opc_oem_post_update:
Shannon Nelson6e93d0c2016-01-15 14:33:18 -08006619 i40e_debug(&pf->hw, I40E_DEBUG_NVM,
6620 "ARQ NVM operation 0x%04x completed\n",
6621 opcode);
Shannon Nelson91a0f932015-03-19 14:32:01 -07006622 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006623 default:
6624 dev_info(&pf->pdev->dev,
Shannon Nelson56e5ca62016-03-10 14:59:48 -08006625 "ARQ: Unknown event 0x%04x ignored\n",
Shannon Nelson0467bc92013-12-18 13:45:58 +00006626 opcode);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006627 break;
6628 }
6629 } while (pending && (i++ < pf->adminq_work_limit));
6630
6631 clear_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state);
6632 /* re-enable Admin queue interrupt cause */
6633 val = rd32(hw, I40E_PFINT_ICR0_ENA);
6634 val |= I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
6635 wr32(hw, I40E_PFINT_ICR0_ENA, val);
6636 i40e_flush(hw);
6637
6638 kfree(event.msg_buf);
6639}
6640
6641/**
Shannon Nelson4eb3f762014-03-06 08:59:58 +00006642 * i40e_verify_eeprom - make sure eeprom is good to use
6643 * @pf: board private structure
6644 **/
6645static void i40e_verify_eeprom(struct i40e_pf *pf)
6646{
6647 int err;
6648
6649 err = i40e_diag_eeprom_test(&pf->hw);
6650 if (err) {
6651 /* retry in case of garbage read */
6652 err = i40e_diag_eeprom_test(&pf->hw);
6653 if (err) {
6654 dev_info(&pf->pdev->dev, "eeprom check failed (%d), Tx/Rx traffic disabled\n",
6655 err);
6656 set_bit(__I40E_BAD_EEPROM, &pf->state);
6657 }
6658 }
6659
6660 if (!err && test_bit(__I40E_BAD_EEPROM, &pf->state)) {
6661 dev_info(&pf->pdev->dev, "eeprom check passed, Tx/Rx traffic enabled\n");
6662 clear_bit(__I40E_BAD_EEPROM, &pf->state);
6663 }
6664}
6665
6666/**
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006667 * i40e_enable_pf_switch_lb
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00006668 * @pf: pointer to the PF structure
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006669 *
6670 * enable switch loop back or die - no point in a return value
6671 **/
6672static void i40e_enable_pf_switch_lb(struct i40e_pf *pf)
6673{
6674 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
6675 struct i40e_vsi_context ctxt;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006676 int ret;
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006677
6678 ctxt.seid = pf->main_vsi_seid;
6679 ctxt.pf_num = pf->hw.pf_id;
6680 ctxt.vf_num = 0;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006681 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
6682 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006683 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006684 "couldn't get PF vsi config, err %s aq_err %s\n",
6685 i40e_stat_str(&pf->hw, ret),
6686 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006687 return;
6688 }
6689 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
6690 ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
6691 ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
6692
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006693 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
6694 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006695 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006696 "update vsi switch failed, err %s aq_err %s\n",
6697 i40e_stat_str(&pf->hw, ret),
6698 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006699 }
6700}
6701
6702/**
6703 * i40e_disable_pf_switch_lb
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00006704 * @pf: pointer to the PF structure
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006705 *
6706 * disable switch loop back or die - no point in a return value
6707 **/
6708static void i40e_disable_pf_switch_lb(struct i40e_pf *pf)
6709{
6710 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
6711 struct i40e_vsi_context ctxt;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006712 int ret;
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006713
6714 ctxt.seid = pf->main_vsi_seid;
6715 ctxt.pf_num = pf->hw.pf_id;
6716 ctxt.vf_num = 0;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006717 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
6718 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006719 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006720 "couldn't get PF vsi config, err %s aq_err %s\n",
6721 i40e_stat_str(&pf->hw, ret),
6722 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006723 return;
6724 }
6725 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
6726 ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
6727 ctxt.info.switch_id &= ~cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
6728
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006729 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
6730 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006731 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006732 "update vsi switch failed, err %s aq_err %s\n",
6733 i40e_stat_str(&pf->hw, ret),
6734 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006735 }
6736}
6737
6738/**
Neerav Parikh51616012015-02-06 08:52:14 +00006739 * i40e_config_bridge_mode - Configure the HW bridge mode
6740 * @veb: pointer to the bridge instance
6741 *
6742 * Configure the loop back mode for the LAN VSI that is downlink to the
6743 * specified HW bridge instance. It is expected this function is called
6744 * when a new HW bridge is instantiated.
6745 **/
6746static void i40e_config_bridge_mode(struct i40e_veb *veb)
6747{
6748 struct i40e_pf *pf = veb->pf;
6749
Shannon Nelson6dec1012015-09-28 14:12:30 -04006750 if (pf->hw.debug_mask & I40E_DEBUG_LAN)
6751 dev_info(&pf->pdev->dev, "enabling bridge mode: %s\n",
6752 veb->bridge_mode == BRIDGE_MODE_VEPA ? "VEPA" : "VEB");
Neerav Parikh51616012015-02-06 08:52:14 +00006753 if (veb->bridge_mode & BRIDGE_MODE_VEPA)
6754 i40e_disable_pf_switch_lb(pf);
6755 else
6756 i40e_enable_pf_switch_lb(pf);
6757}
6758
6759/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006760 * i40e_reconstitute_veb - rebuild the VEB and anything connected to it
6761 * @veb: pointer to the VEB instance
6762 *
6763 * This is a recursive function that first builds the attached VSIs then
6764 * recurses in to build the next layer of VEB. We track the connections
6765 * through our own index numbers because the seid's from the HW could
6766 * change across the reset.
6767 **/
6768static int i40e_reconstitute_veb(struct i40e_veb *veb)
6769{
6770 struct i40e_vsi *ctl_vsi = NULL;
6771 struct i40e_pf *pf = veb->pf;
6772 int v, veb_idx;
6773 int ret;
6774
6775 /* build VSI that owns this VEB, temporarily attached to base VEB */
Mitch Williams505682c2014-05-20 08:01:37 +00006776 for (v = 0; v < pf->num_alloc_vsi && !ctl_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006777 if (pf->vsi[v] &&
6778 pf->vsi[v]->veb_idx == veb->idx &&
6779 pf->vsi[v]->flags & I40E_VSI_FLAG_VEB_OWNER) {
6780 ctl_vsi = pf->vsi[v];
6781 break;
6782 }
6783 }
6784 if (!ctl_vsi) {
6785 dev_info(&pf->pdev->dev,
6786 "missing owner VSI for veb_idx %d\n", veb->idx);
6787 ret = -ENOENT;
6788 goto end_reconstitute;
6789 }
6790 if (ctl_vsi != pf->vsi[pf->lan_vsi])
6791 ctl_vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
6792 ret = i40e_add_vsi(ctl_vsi);
6793 if (ret) {
6794 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006795 "rebuild of veb_idx %d owner VSI failed: %d\n",
6796 veb->idx, ret);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006797 goto end_reconstitute;
6798 }
6799 i40e_vsi_reset_stats(ctl_vsi);
6800
6801 /* create the VEB in the switch and move the VSI onto the VEB */
6802 ret = i40e_add_veb(veb, ctl_vsi);
6803 if (ret)
6804 goto end_reconstitute;
6805
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07006806 if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
6807 veb->bridge_mode = BRIDGE_MODE_VEB;
6808 else
6809 veb->bridge_mode = BRIDGE_MODE_VEPA;
Neerav Parikh51616012015-02-06 08:52:14 +00006810 i40e_config_bridge_mode(veb);
Anjali Singhai Jainb64ba082014-11-13 03:06:15 +00006811
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006812 /* create the remaining VSIs attached to this VEB */
Mitch Williams505682c2014-05-20 08:01:37 +00006813 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006814 if (!pf->vsi[v] || pf->vsi[v] == ctl_vsi)
6815 continue;
6816
6817 if (pf->vsi[v]->veb_idx == veb->idx) {
6818 struct i40e_vsi *vsi = pf->vsi[v];
Jesse Brandeburg6995b362015-08-28 17:55:54 -04006819
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006820 vsi->uplink_seid = veb->seid;
6821 ret = i40e_add_vsi(vsi);
6822 if (ret) {
6823 dev_info(&pf->pdev->dev,
6824 "rebuild of vsi_idx %d failed: %d\n",
6825 v, ret);
6826 goto end_reconstitute;
6827 }
6828 i40e_vsi_reset_stats(vsi);
6829 }
6830 }
6831
6832 /* create any VEBs attached to this VEB - RECURSION */
6833 for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
6834 if (pf->veb[veb_idx] && pf->veb[veb_idx]->veb_idx == veb->idx) {
6835 pf->veb[veb_idx]->uplink_seid = veb->seid;
6836 ret = i40e_reconstitute_veb(pf->veb[veb_idx]);
6837 if (ret)
6838 break;
6839 }
6840 }
6841
6842end_reconstitute:
6843 return ret;
6844}
6845
6846/**
6847 * i40e_get_capabilities - get info about the HW
6848 * @pf: the PF struct
6849 **/
6850static int i40e_get_capabilities(struct i40e_pf *pf)
6851{
6852 struct i40e_aqc_list_capabilities_element_resp *cap_buf;
6853 u16 data_size;
6854 int buf_len;
6855 int err;
6856
6857 buf_len = 40 * sizeof(struct i40e_aqc_list_capabilities_element_resp);
6858 do {
6859 cap_buf = kzalloc(buf_len, GFP_KERNEL);
6860 if (!cap_buf)
6861 return -ENOMEM;
6862
6863 /* this loads the data into the hw struct for us */
6864 err = i40e_aq_discover_capabilities(&pf->hw, cap_buf, buf_len,
6865 &data_size,
6866 i40e_aqc_opc_list_func_capabilities,
6867 NULL);
6868 /* data loaded, buffer no longer needed */
6869 kfree(cap_buf);
6870
6871 if (pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOMEM) {
6872 /* retry with a larger buffer */
6873 buf_len = data_size;
6874 } else if (pf->hw.aq.asq_last_status != I40E_AQ_RC_OK) {
6875 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006876 "capability discovery failed, err %s aq_err %s\n",
6877 i40e_stat_str(&pf->hw, err),
6878 i40e_aq_str(&pf->hw,
6879 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006880 return -ENODEV;
6881 }
6882 } while (err);
6883
6884 if (pf->hw.debug_mask & I40E_DEBUG_USER)
6885 dev_info(&pf->pdev->dev,
6886 "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",
6887 pf->hw.pf_id, pf->hw.func_caps.num_vfs,
6888 pf->hw.func_caps.num_msix_vectors,
6889 pf->hw.func_caps.num_msix_vectors_vf,
6890 pf->hw.func_caps.fd_filters_guaranteed,
6891 pf->hw.func_caps.fd_filters_best_effort,
6892 pf->hw.func_caps.num_tx_qp,
6893 pf->hw.func_caps.num_vsis);
6894
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00006895#define DEF_NUM_VSI (1 + (pf->hw.func_caps.fcoe ? 1 : 0) \
6896 + pf->hw.func_caps.num_vfs)
6897 if (pf->hw.revision_id == 0 && (DEF_NUM_VSI > pf->hw.func_caps.num_vsis)) {
6898 dev_info(&pf->pdev->dev,
6899 "got num_vsis %d, setting num_vsis to %d\n",
6900 pf->hw.func_caps.num_vsis, DEF_NUM_VSI);
6901 pf->hw.func_caps.num_vsis = DEF_NUM_VSI;
6902 }
6903
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006904 return 0;
6905}
6906
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006907static int i40e_vsi_clear(struct i40e_vsi *vsi);
6908
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006909/**
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006910 * i40e_fdir_sb_setup - initialize the Flow Director resources for Sideband
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006911 * @pf: board private structure
6912 **/
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006913static void i40e_fdir_sb_setup(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006914{
6915 struct i40e_vsi *vsi;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006916
Jesse Brandeburg407e0632014-06-03 23:50:12 +00006917 /* quick workaround for an NVM issue that leaves a critical register
6918 * uninitialized
6919 */
6920 if (!rd32(&pf->hw, I40E_GLQF_HKEY(0))) {
6921 static const u32 hkey[] = {
6922 0xe640d33f, 0xcdfe98ab, 0x73fa7161, 0x0d7a7d36,
6923 0xeacb7d61, 0xaa4f05b6, 0x9c5c89ed, 0xfc425ddb,
6924 0xa4654832, 0xfc7461d4, 0x8f827619, 0xf5c63c21,
6925 0x95b3a76d};
Alexander Duyck4b816442016-10-11 15:26:53 -07006926 int i;
Jesse Brandeburg407e0632014-06-03 23:50:12 +00006927
6928 for (i = 0; i <= I40E_GLQF_HKEY_MAX_INDEX; i++)
6929 wr32(&pf->hw, I40E_GLQF_HKEY(i), hkey[i]);
6930 }
6931
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006932 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006933 return;
6934
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006935 /* find existing VSI and see if it needs configuring */
Alexander Duyck4b816442016-10-11 15:26:53 -07006936 vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006937
6938 /* create a new VSI if none exists */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006939 if (!vsi) {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006940 vsi = i40e_vsi_setup(pf, I40E_VSI_FDIR,
6941 pf->vsi[pf->lan_vsi]->seid, 0);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006942 if (!vsi) {
6943 dev_info(&pf->pdev->dev, "Couldn't create FDir VSI\n");
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00006944 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
6945 return;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006946 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006947 }
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00006948
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006949 i40e_vsi_setup_irqhandler(vsi, i40e_fdir_clean_ring);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006950}
6951
6952/**
6953 * i40e_fdir_teardown - release the Flow Director resources
6954 * @pf: board private structure
6955 **/
6956static void i40e_fdir_teardown(struct i40e_pf *pf)
6957{
Alexander Duyck4b816442016-10-11 15:26:53 -07006958 struct i40e_vsi *vsi;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006959
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00006960 i40e_fdir_filter_exit(pf);
Alexander Duyck4b816442016-10-11 15:26:53 -07006961 vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
6962 if (vsi)
6963 i40e_vsi_release(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006964}
6965
6966/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006967 * i40e_prep_for_reset - prep for the core to reset
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006968 * @pf: board private structure
6969 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00006970 * Close up the VFs and other things in prep for PF Reset.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006971 **/
Shannon Nelson23cfbe02014-06-04 01:23:14 +00006972static void i40e_prep_for_reset(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006973{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006974 struct i40e_hw *hw = &pf->hw;
Shannon Nelson60442de2014-04-23 04:50:13 +00006975 i40e_status ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006976 u32 v;
6977
6978 clear_bit(__I40E_RESET_INTR_RECEIVED, &pf->state);
6979 if (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
Shannon Nelson23cfbe02014-06-04 01:23:14 +00006980 return;
Mitch Williamsd3ce57342016-03-10 14:59:46 -08006981 if (i40e_check_asq_alive(&pf->hw))
6982 i40e_vc_notify_reset(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006983
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006984 dev_dbg(&pf->pdev->dev, "Tearing down internal switch for reset\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006985
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006986 /* quiesce the VSIs and their queues that are not already DOWN */
6987 i40e_pf_quiesce_all_vsi(pf);
6988
Mitch Williams505682c2014-05-20 08:01:37 +00006989 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006990 if (pf->vsi[v])
6991 pf->vsi[v]->seid = 0;
6992 }
6993
6994 i40e_shutdown_adminq(&pf->hw);
6995
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006996 /* call shutdown HMC */
Shannon Nelson60442de2014-04-23 04:50:13 +00006997 if (hw->hmc.hmc_obj) {
6998 ret = i40e_shutdown_lan_hmc(hw);
Shannon Nelson23cfbe02014-06-04 01:23:14 +00006999 if (ret)
Shannon Nelson60442de2014-04-23 04:50:13 +00007000 dev_warn(&pf->pdev->dev,
7001 "shutdown_lan_hmc failed: %d\n", ret);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007002 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007003}
7004
7005/**
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00007006 * i40e_send_version - update firmware with driver version
7007 * @pf: PF struct
7008 */
7009static void i40e_send_version(struct i40e_pf *pf)
7010{
7011 struct i40e_driver_version dv;
7012
7013 dv.major_version = DRV_VERSION_MAJOR;
7014 dv.minor_version = DRV_VERSION_MINOR;
7015 dv.build_version = DRV_VERSION_BUILD;
7016 dv.subbuild_version = 0;
Rickard Strandqvist35a7d802014-07-29 09:26:25 +00007017 strlcpy(dv.driver_string, DRV_VERSION, sizeof(dv.driver_string));
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00007018 i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
7019}
7020
7021/**
Jesse Brandeburg4dda12e2013-12-18 13:46:01 +00007022 * i40e_reset_and_rebuild - reset and rebuild using a saved config
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007023 * @pf: board private structure
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007024 * @reinit: if the Main VSI needs to re-initialized.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007025 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007026static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007027{
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007028 struct i40e_hw *hw = &pf->hw;
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00007029 u8 set_fc_aq_fail = 0;
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007030 i40e_status ret;
Anjali Singhai Jain4f2f017c2015-10-21 19:47:07 -04007031 u32 val;
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007032 u32 v;
7033
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007034 /* Now we wait for GRST to settle out.
7035 * We don't have to delete the VEBs or VSIs from the hw switch
7036 * because the reset will make them disappear.
7037 */
7038 ret = i40e_pf_reset(hw);
Akeem G Abodunrinb5565402014-04-09 05:59:04 +00007039 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007040 dev_info(&pf->pdev->dev, "PF reset failed, %d\n", ret);
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00007041 set_bit(__I40E_RESET_FAILED, &pf->state);
7042 goto clear_recovery;
Akeem G Abodunrinb5565402014-04-09 05:59:04 +00007043 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007044 pf->pfr_count++;
7045
7046 if (test_bit(__I40E_DOWN, &pf->state))
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00007047 goto clear_recovery;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00007048 dev_dbg(&pf->pdev->dev, "Rebuilding internal switch\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007049
7050 /* rebuild the basics for the AdminQ, HMC, and initial HW switch */
7051 ret = i40e_init_adminq(&pf->hw);
7052 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04007053 dev_info(&pf->pdev->dev, "Rebuild AdminQ failed, err %s aq_err %s\n",
7054 i40e_stat_str(&pf->hw, ret),
7055 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00007056 goto clear_recovery;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007057 }
7058
Shannon Nelson4eb3f762014-03-06 08:59:58 +00007059 /* re-verify the eeprom if we just had an EMP reset */
Anjali Singhai Jain9df42d12015-01-24 09:58:40 +00007060 if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, &pf->state))
Shannon Nelson4eb3f762014-03-06 08:59:58 +00007061 i40e_verify_eeprom(pf);
Shannon Nelson4eb3f762014-03-06 08:59:58 +00007062
Shannon Nelsone78ac4bf2014-05-10 04:49:09 +00007063 i40e_clear_pxe_mode(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007064 ret = i40e_get_capabilities(pf);
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04007065 if (ret)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007066 goto end_core_reset;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007067
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007068 ret = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
7069 hw->func_caps.num_rx_qp,
7070 pf->fcoe_hmc_cntx_num, pf->fcoe_hmc_filt_num);
7071 if (ret) {
7072 dev_info(&pf->pdev->dev, "init_lan_hmc failed: %d\n", ret);
7073 goto end_core_reset;
7074 }
7075 ret = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
7076 if (ret) {
7077 dev_info(&pf->pdev->dev, "configure_lan_hmc failed: %d\n", ret);
7078 goto end_core_reset;
7079 }
7080
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08007081#ifdef CONFIG_I40E_DCB
7082 ret = i40e_init_pf_dcb(pf);
7083 if (ret) {
Shannon Nelsonaebfc812014-12-11 07:06:38 +00007084 dev_info(&pf->pdev->dev, "DCB init failed %d, disabled\n", ret);
7085 pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
7086 /* Continue without DCB enabled */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08007087 }
7088#endif /* CONFIG_I40E_DCB */
Vasu Dev38e00432014-08-01 13:27:03 -07007089#ifdef I40E_FCOE
Shannon Nelson21364bc2015-08-26 15:14:13 -04007090 i40e_init_pf_fcoe(pf);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08007091
Vasu Dev38e00432014-08-01 13:27:03 -07007092#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007093 /* do basic switch setup */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007094 ret = i40e_setup_pf_switch(pf, reinit);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007095 if (ret)
7096 goto end_core_reset;
7097
Shannon Nelson2f0aff42016-01-04 10:33:08 -08007098 /* The driver only wants link up/down and module qualification
7099 * reports from firmware. Note the negative logic.
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +00007100 */
7101 ret = i40e_aq_set_phy_int_mask(&pf->hw,
Shannon Nelson2f0aff42016-01-04 10:33:08 -08007102 ~(I40E_AQ_EVENT_LINK_UPDOWN |
Shannon Nelson867a79e2016-03-18 12:18:15 -07007103 I40E_AQ_EVENT_MEDIA_NA |
Shannon Nelson2f0aff42016-01-04 10:33:08 -08007104 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +00007105 if (ret)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04007106 dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
7107 i40e_stat_str(&pf->hw, ret),
7108 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +00007109
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00007110 /* make sure our flow control settings are restored */
7111 ret = i40e_set_fc(&pf->hw, &set_fc_aq_fail, true);
7112 if (ret)
Neerav Parikh8279e492015-09-03 17:18:50 -04007113 dev_dbg(&pf->pdev->dev, "setting flow control: ret = %s last_status = %s\n",
7114 i40e_stat_str(&pf->hw, ret),
7115 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00007116
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007117 /* Rebuild the VSIs and VEBs that existed before reset.
7118 * They are still in our local switch element arrays, so only
7119 * need to rebuild the switch model in the HW.
7120 *
7121 * If there were VEBs but the reconstitution failed, we'll try
7122 * try to recover minimal use by getting the basic PF VSI working.
7123 */
7124 if (pf->vsi[pf->lan_vsi]->uplink_seid != pf->mac_seid) {
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00007125 dev_dbg(&pf->pdev->dev, "attempting to rebuild switch\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007126 /* find the one VEB connected to the MAC, and find orphans */
7127 for (v = 0; v < I40E_MAX_VEB; v++) {
7128 if (!pf->veb[v])
7129 continue;
7130
7131 if (pf->veb[v]->uplink_seid == pf->mac_seid ||
7132 pf->veb[v]->uplink_seid == 0) {
7133 ret = i40e_reconstitute_veb(pf->veb[v]);
7134
7135 if (!ret)
7136 continue;
7137
7138 /* If Main VEB failed, we're in deep doodoo,
7139 * so give up rebuilding the switch and set up
7140 * for minimal rebuild of PF VSI.
7141 * If orphan failed, we'll report the error
7142 * but try to keep going.
7143 */
7144 if (pf->veb[v]->uplink_seid == pf->mac_seid) {
7145 dev_info(&pf->pdev->dev,
7146 "rebuild of switch failed: %d, will try to set up simple PF connection\n",
7147 ret);
7148 pf->vsi[pf->lan_vsi]->uplink_seid
7149 = pf->mac_seid;
7150 break;
7151 } else if (pf->veb[v]->uplink_seid == 0) {
7152 dev_info(&pf->pdev->dev,
7153 "rebuild of orphan VEB failed: %d\n",
7154 ret);
7155 }
7156 }
7157 }
7158 }
7159
7160 if (pf->vsi[pf->lan_vsi]->uplink_seid == pf->mac_seid) {
Shannon Nelsoncde4cbc2014-06-04 01:23:17 +00007161 dev_dbg(&pf->pdev->dev, "attempting to rebuild PF VSI\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007162 /* no VEB, so rebuild only the Main VSI */
7163 ret = i40e_add_vsi(pf->vsi[pf->lan_vsi]);
7164 if (ret) {
7165 dev_info(&pf->pdev->dev,
7166 "rebuild of Main VSI failed: %d\n", ret);
7167 goto end_core_reset;
7168 }
7169 }
7170
Anjali Singhai Jain4f2f017c2015-10-21 19:47:07 -04007171 /* Reconfigure hardware for allowing smaller MSS in the case
7172 * of TSO, so that we avoid the MDD being fired and causing
7173 * a reset in the case of small MSS+TSO.
7174 */
7175#define I40E_REG_MSS 0x000E64DC
7176#define I40E_REG_MSS_MIN_MASK 0x3FF0000
7177#define I40E_64BYTE_MSS 0x400000
7178 val = rd32(hw, I40E_REG_MSS);
7179 if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
7180 val &= ~I40E_REG_MSS_MIN_MASK;
7181 val |= I40E_64BYTE_MSS;
7182 wr32(hw, I40E_REG_MSS, val);
7183 }
7184
Anjali Singhai Jain8eed76f2015-12-09 15:50:31 -08007185 if (pf->flags & I40E_FLAG_RESTART_AUTONEG) {
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +00007186 msleep(75);
7187 ret = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
7188 if (ret)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04007189 dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
7190 i40e_stat_str(&pf->hw, ret),
7191 i40e_aq_str(&pf->hw,
7192 pf->hw.aq.asq_last_status));
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00007193 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007194 /* reinit the misc interrupt */
7195 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
7196 ret = i40e_setup_misc_vector(pf);
7197
Anjali Singhai Jaine7358f52015-10-01 14:37:34 -04007198 /* Add a filter to drop all Flow control frames from any VSI from being
7199 * transmitted. By doing so we stop a malicious VF from sending out
7200 * PAUSE or PFC frames and potentially controlling traffic for other
7201 * PF/VF VSIs.
7202 * The FW can still send Flow control frames if enabled.
7203 */
7204 i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
7205 pf->main_vsi_seid);
7206
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007207 /* restart the VSIs that were rebuilt and running before the reset */
7208 i40e_pf_unquiesce_all_vsi(pf);
7209
Mitch Williams69f64b22014-02-13 03:48:46 -08007210 if (pf->num_alloc_vfs) {
7211 for (v = 0; v < pf->num_alloc_vfs; v++)
7212 i40e_reset_vf(&pf->vf[v], true);
7213 }
7214
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007215 /* tell the firmware that we're starting */
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00007216 i40e_send_version(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007217
7218end_core_reset:
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00007219 clear_bit(__I40E_RESET_FAILED, &pf->state);
7220clear_recovery:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007221 clear_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state);
7222}
7223
7224/**
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00007225 * i40e_handle_reset_warning - prep for the PF to reset, reset and rebuild
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007226 * @pf: board private structure
7227 *
7228 * Close up the VFs and other things in prep for a Core Reset,
7229 * then get ready to rebuild the world.
7230 **/
7231static void i40e_handle_reset_warning(struct i40e_pf *pf)
7232{
Shannon Nelson23cfbe02014-06-04 01:23:14 +00007233 i40e_prep_for_reset(pf);
7234 i40e_reset_and_rebuild(pf, false);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007235}
7236
7237/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007238 * i40e_handle_mdd_event
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00007239 * @pf: pointer to the PF structure
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007240 *
7241 * Called from the MDD irq handler to identify possibly malicious vfs
7242 **/
7243static void i40e_handle_mdd_event(struct i40e_pf *pf)
7244{
7245 struct i40e_hw *hw = &pf->hw;
7246 bool mdd_detected = false;
Neerav Parikhdf430b12014-06-04 01:23:15 +00007247 bool pf_mdd_detected = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007248 struct i40e_vf *vf;
7249 u32 reg;
7250 int i;
7251
7252 if (!test_bit(__I40E_MDD_EVENT_PENDING, &pf->state))
7253 return;
7254
7255 /* find what triggered the MDD event */
7256 reg = rd32(hw, I40E_GL_MDET_TX);
7257 if (reg & I40E_GL_MDET_TX_VALID_MASK) {
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00007258 u8 pf_num = (reg & I40E_GL_MDET_TX_PF_NUM_MASK) >>
7259 I40E_GL_MDET_TX_PF_NUM_SHIFT;
Mitch Williams2089ad02014-10-17 03:14:53 +00007260 u16 vf_num = (reg & I40E_GL_MDET_TX_VF_NUM_MASK) >>
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00007261 I40E_GL_MDET_TX_VF_NUM_SHIFT;
Dan Carpenter013f6572014-10-22 20:06:29 -07007262 u8 event = (reg & I40E_GL_MDET_TX_EVENT_MASK) >>
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00007263 I40E_GL_MDET_TX_EVENT_SHIFT;
Mitch Williams2089ad02014-10-17 03:14:53 +00007264 u16 queue = ((reg & I40E_GL_MDET_TX_QUEUE_MASK) >>
7265 I40E_GL_MDET_TX_QUEUE_SHIFT) -
7266 pf->hw.func_caps.base_queue;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007267 if (netif_msg_tx_err(pf))
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00007268 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 +00007269 event, queue, pf_num, vf_num);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007270 wr32(hw, I40E_GL_MDET_TX, 0xffffffff);
7271 mdd_detected = true;
7272 }
7273 reg = rd32(hw, I40E_GL_MDET_RX);
7274 if (reg & I40E_GL_MDET_RX_VALID_MASK) {
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00007275 u8 func = (reg & I40E_GL_MDET_RX_FUNCTION_MASK) >>
7276 I40E_GL_MDET_RX_FUNCTION_SHIFT;
Dan Carpenter013f6572014-10-22 20:06:29 -07007277 u8 event = (reg & I40E_GL_MDET_RX_EVENT_MASK) >>
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00007278 I40E_GL_MDET_RX_EVENT_SHIFT;
Mitch Williams2089ad02014-10-17 03:14:53 +00007279 u16 queue = ((reg & I40E_GL_MDET_RX_QUEUE_MASK) >>
7280 I40E_GL_MDET_RX_QUEUE_SHIFT) -
7281 pf->hw.func_caps.base_queue;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007282 if (netif_msg_rx_err(pf))
7283 dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on RX queue %d of function 0x%02x\n",
7284 event, queue, func);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007285 wr32(hw, I40E_GL_MDET_RX, 0xffffffff);
7286 mdd_detected = true;
7287 }
7288
Neerav Parikhdf430b12014-06-04 01:23:15 +00007289 if (mdd_detected) {
7290 reg = rd32(hw, I40E_PF_MDET_TX);
7291 if (reg & I40E_PF_MDET_TX_VALID_MASK) {
7292 wr32(hw, I40E_PF_MDET_TX, 0xFFFF);
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007293 dev_info(&pf->pdev->dev, "TX driver issue detected, PF reset issued\n");
Neerav Parikhdf430b12014-06-04 01:23:15 +00007294 pf_mdd_detected = true;
7295 }
7296 reg = rd32(hw, I40E_PF_MDET_RX);
7297 if (reg & I40E_PF_MDET_RX_VALID_MASK) {
7298 wr32(hw, I40E_PF_MDET_RX, 0xFFFF);
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007299 dev_info(&pf->pdev->dev, "RX driver issue detected, PF reset issued\n");
Neerav Parikhdf430b12014-06-04 01:23:15 +00007300 pf_mdd_detected = true;
7301 }
7302 /* Queue belongs to the PF, initiate a reset */
7303 if (pf_mdd_detected) {
7304 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
7305 i40e_service_event_schedule(pf);
7306 }
7307 }
7308
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007309 /* see if one of the VFs needs its hand slapped */
7310 for (i = 0; i < pf->num_alloc_vfs && mdd_detected; i++) {
7311 vf = &(pf->vf[i]);
7312 reg = rd32(hw, I40E_VP_MDET_TX(i));
7313 if (reg & I40E_VP_MDET_TX_VALID_MASK) {
7314 wr32(hw, I40E_VP_MDET_TX(i), 0xFFFF);
7315 vf->num_mdd_events++;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007316 dev_info(&pf->pdev->dev, "TX driver issue detected on VF %d\n",
7317 i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007318 }
7319
7320 reg = rd32(hw, I40E_VP_MDET_RX(i));
7321 if (reg & I40E_VP_MDET_RX_VALID_MASK) {
7322 wr32(hw, I40E_VP_MDET_RX(i), 0xFFFF);
7323 vf->num_mdd_events++;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007324 dev_info(&pf->pdev->dev, "RX driver issue detected on VF %d\n",
7325 i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007326 }
7327
7328 if (vf->num_mdd_events > I40E_DEFAULT_NUM_MDD_EVENTS_ALLOWED) {
7329 dev_info(&pf->pdev->dev,
7330 "Too many MDD events on VF %d, disabled\n", i);
7331 dev_info(&pf->pdev->dev,
7332 "Use PF Control I/F to re-enable the VF\n");
7333 set_bit(I40E_VF_STAT_DISABLED, &vf->vf_states);
7334 }
7335 }
7336
7337 /* re-enable mdd interrupt cause */
7338 clear_bit(__I40E_MDD_EVENT_PENDING, &pf->state);
7339 reg = rd32(hw, I40E_PFINT_ICR0_ENA);
7340 reg |= I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
7341 wr32(hw, I40E_PFINT_ICR0_ENA, reg);
7342 i40e_flush(hw);
7343}
7344
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007345/**
Singhai, Anjali6a899022015-12-14 12:21:18 -08007346 * i40e_sync_udp_filters_subtask - Sync the VSI filter list with HW
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007347 * @pf: board private structure
7348 **/
Singhai, Anjali6a899022015-12-14 12:21:18 -08007349static void i40e_sync_udp_filters_subtask(struct i40e_pf *pf)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007350{
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007351 struct i40e_hw *hw = &pf->hw;
7352 i40e_status ret;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007353 __be16 port;
7354 int i;
7355
Singhai, Anjali6a899022015-12-14 12:21:18 -08007356 if (!(pf->flags & I40E_FLAG_UDP_FILTER_SYNC))
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007357 return;
7358
Singhai, Anjali6a899022015-12-14 12:21:18 -08007359 pf->flags &= ~I40E_FLAG_UDP_FILTER_SYNC;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007360
7361 for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
Singhai, Anjali6a899022015-12-14 12:21:18 -08007362 if (pf->pending_udp_bitmap & BIT_ULL(i)) {
7363 pf->pending_udp_bitmap &= ~BIT_ULL(i);
7364 port = pf->udp_ports[i].index;
Shannon Nelsonc22c06c2015-03-31 00:45:04 -07007365 if (port)
Carolyn Wybornyb3f5c7b2016-08-24 11:33:51 -07007366 ret = i40e_aq_add_udp_tunnel(hw, port,
7367 pf->udp_ports[i].type,
7368 NULL, NULL);
Shannon Nelsonc22c06c2015-03-31 00:45:04 -07007369 else
7370 ret = i40e_aq_del_udp_tunnel(hw, i, NULL);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007371
7372 if (ret) {
Carolyn Wyborny730a8f82016-02-17 16:12:16 -08007373 dev_dbg(&pf->pdev->dev,
7374 "%s %s port %d, index %d failed, err %s aq_err %s\n",
7375 pf->udp_ports[i].type ? "vxlan" : "geneve",
7376 port ? "add" : "delete",
7377 ntohs(port), i,
7378 i40e_stat_str(&pf->hw, ret),
7379 i40e_aq_str(&pf->hw,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04007380 pf->hw.aq.asq_last_status));
Singhai, Anjali6a899022015-12-14 12:21:18 -08007381 pf->udp_ports[i].index = 0;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007382 }
7383 }
7384 }
7385}
7386
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007387/**
7388 * i40e_service_task - Run the driver's async subtasks
7389 * @work: pointer to work_struct containing our data
7390 **/
7391static void i40e_service_task(struct work_struct *work)
7392{
7393 struct i40e_pf *pf = container_of(work,
7394 struct i40e_pf,
7395 service_task);
7396 unsigned long start_time = jiffies;
7397
Shannon Nelsone57a2fe2014-06-03 23:50:19 +00007398 /* don't bother with service tasks if a reset is in progress */
7399 if (test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state)) {
Shannon Nelsone57a2fe2014-06-03 23:50:19 +00007400 return;
7401 }
7402
Mitch Williams91089032016-11-21 13:03:51 -08007403 if (test_and_set_bit(__I40E_SERVICE_SCHED, &pf->state))
7404 return;
7405
Kiran Patilb03a8c12015-09-24 18:13:15 -04007406 i40e_detect_recover_hung(pf);
Jesse Brandeburg2818ccd2016-01-13 16:51:38 -08007407 i40e_sync_filters_subtask(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007408 i40e_reset_subtask(pf);
7409 i40e_handle_mdd_event(pf);
7410 i40e_vc_process_vflr_event(pf);
7411 i40e_watchdog_subtask(pf);
7412 i40e_fdir_reinit_subtask(pf);
Mitch Williams0ef2d5a2017-01-24 10:24:00 -08007413 if (pf->flags & I40E_FLAG_CLIENT_RESET) {
7414 /* Client subtask will reopen next time through. */
7415 i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], true);
7416 pf->flags &= ~I40E_FLAG_CLIENT_RESET;
7417 } else {
7418 i40e_client_subtask(pf);
7419 if (pf->flags & I40E_FLAG_CLIENT_L2_CHANGE) {
7420 i40e_notify_client_of_l2_param_changes(
7421 pf->vsi[pf->lan_vsi]);
7422 pf->flags &= ~I40E_FLAG_CLIENT_L2_CHANGE;
7423 }
7424 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007425 i40e_sync_filters_subtask(pf);
Singhai, Anjali6a899022015-12-14 12:21:18 -08007426 i40e_sync_udp_filters_subtask(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007427 i40e_clean_adminq_subtask(pf);
7428
Mitch Williams91089032016-11-21 13:03:51 -08007429 /* flush memory to make sure state is correct before next watchdog */
7430 smp_mb__before_atomic();
7431 clear_bit(__I40E_SERVICE_SCHED, &pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007432
7433 /* If the tasks have taken longer than one timer cycle or there
7434 * is more work to be done, reschedule the service task now
7435 * rather than wait for the timer to tick again.
7436 */
7437 if (time_after(jiffies, (start_time + pf->service_timer_period)) ||
7438 test_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state) ||
7439 test_bit(__I40E_MDD_EVENT_PENDING, &pf->state) ||
7440 test_bit(__I40E_VFLR_EVENT_PENDING, &pf->state))
7441 i40e_service_event_schedule(pf);
7442}
7443
7444/**
7445 * i40e_service_timer - timer callback
7446 * @data: pointer to PF struct
7447 **/
7448static void i40e_service_timer(unsigned long data)
7449{
7450 struct i40e_pf *pf = (struct i40e_pf *)data;
7451
7452 mod_timer(&pf->service_timer,
7453 round_jiffies(jiffies + pf->service_timer_period));
7454 i40e_service_event_schedule(pf);
7455}
7456
7457/**
7458 * i40e_set_num_rings_in_vsi - Determine number of rings in the VSI
7459 * @vsi: the VSI being configured
7460 **/
7461static int i40e_set_num_rings_in_vsi(struct i40e_vsi *vsi)
7462{
7463 struct i40e_pf *pf = vsi->back;
7464
7465 switch (vsi->type) {
7466 case I40E_VSI_MAIN:
7467 vsi->alloc_queue_pairs = pf->num_lan_qps;
7468 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
7469 I40E_REQ_DESCRIPTOR_MULTIPLE);
7470 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
7471 vsi->num_q_vectors = pf->num_lan_msix;
7472 else
7473 vsi->num_q_vectors = 1;
7474
7475 break;
7476
7477 case I40E_VSI_FDIR:
7478 vsi->alloc_queue_pairs = 1;
7479 vsi->num_desc = ALIGN(I40E_FDIR_RING_COUNT,
7480 I40E_REQ_DESCRIPTOR_MULTIPLE);
Tushar Davea70e4072016-05-16 12:40:53 -07007481 vsi->num_q_vectors = pf->num_fdsb_msix;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007482 break;
7483
7484 case I40E_VSI_VMDQ2:
7485 vsi->alloc_queue_pairs = pf->num_vmdq_qps;
7486 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
7487 I40E_REQ_DESCRIPTOR_MULTIPLE);
7488 vsi->num_q_vectors = pf->num_vmdq_msix;
7489 break;
7490
7491 case I40E_VSI_SRIOV:
7492 vsi->alloc_queue_pairs = pf->num_vf_qps;
7493 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
7494 I40E_REQ_DESCRIPTOR_MULTIPLE);
7495 break;
7496
Vasu Dev38e00432014-08-01 13:27:03 -07007497#ifdef I40E_FCOE
7498 case I40E_VSI_FCOE:
7499 vsi->alloc_queue_pairs = pf->num_fcoe_qps;
7500 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
7501 I40E_REQ_DESCRIPTOR_MULTIPLE);
7502 vsi->num_q_vectors = pf->num_fcoe_msix;
7503 break;
7504
7505#endif /* I40E_FCOE */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007506 default:
7507 WARN_ON(1);
7508 return -ENODATA;
7509 }
7510
7511 return 0;
7512}
7513
7514/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007515 * i40e_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the vsi
7516 * @type: VSI pointer
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007517 * @alloc_qvectors: a bool to specify if q_vectors need to be allocated.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007518 *
7519 * On error: returns error code (negative)
7520 * On success: returns 0
7521 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007522static int i40e_vsi_alloc_arrays(struct i40e_vsi *vsi, bool alloc_qvectors)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007523{
7524 int size;
7525 int ret = 0;
7526
Shannon Nelsonac6c5e32013-11-20 10:02:57 +00007527 /* allocate memory for both Tx and Rx ring pointers */
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007528 size = sizeof(struct i40e_ring *) * vsi->alloc_queue_pairs * 2;
7529 vsi->tx_rings = kzalloc(size, GFP_KERNEL);
7530 if (!vsi->tx_rings)
7531 return -ENOMEM;
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007532 vsi->rx_rings = &vsi->tx_rings[vsi->alloc_queue_pairs];
7533
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007534 if (alloc_qvectors) {
7535 /* allocate memory for q_vector pointers */
Julia Lawallf57e4fb2014-07-30 03:11:09 +00007536 size = sizeof(struct i40e_q_vector *) * vsi->num_q_vectors;
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007537 vsi->q_vectors = kzalloc(size, GFP_KERNEL);
7538 if (!vsi->q_vectors) {
7539 ret = -ENOMEM;
7540 goto err_vectors;
7541 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007542 }
7543 return ret;
7544
7545err_vectors:
7546 kfree(vsi->tx_rings);
7547 return ret;
7548}
7549
7550/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007551 * i40e_vsi_mem_alloc - Allocates the next available struct vsi in the PF
7552 * @pf: board private structure
7553 * @type: type of VSI
7554 *
7555 * On error: returns error code (negative)
7556 * On success: returns vsi index in PF (positive)
7557 **/
7558static int i40e_vsi_mem_alloc(struct i40e_pf *pf, enum i40e_vsi_type type)
7559{
7560 int ret = -ENODEV;
7561 struct i40e_vsi *vsi;
7562 int vsi_idx;
7563 int i;
7564
7565 /* Need to protect the allocation of the VSIs at the PF level */
7566 mutex_lock(&pf->switch_mutex);
7567
7568 /* VSI list may be fragmented if VSI creation/destruction has
7569 * been happening. We can afford to do a quick scan to look
7570 * for any free VSIs in the list.
7571 *
7572 * find next empty vsi slot, looping back around if necessary
7573 */
7574 i = pf->next_vsi;
Mitch Williams505682c2014-05-20 08:01:37 +00007575 while (i < pf->num_alloc_vsi && pf->vsi[i])
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007576 i++;
Mitch Williams505682c2014-05-20 08:01:37 +00007577 if (i >= pf->num_alloc_vsi) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007578 i = 0;
7579 while (i < pf->next_vsi && pf->vsi[i])
7580 i++;
7581 }
7582
Mitch Williams505682c2014-05-20 08:01:37 +00007583 if (i < pf->num_alloc_vsi && !pf->vsi[i]) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007584 vsi_idx = i; /* Found one! */
7585 } else {
7586 ret = -ENODEV;
Alexander Duyck493fb302013-09-28 07:01:44 +00007587 goto unlock_pf; /* out of VSI slots! */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007588 }
7589 pf->next_vsi = ++i;
7590
7591 vsi = kzalloc(sizeof(*vsi), GFP_KERNEL);
7592 if (!vsi) {
7593 ret = -ENOMEM;
Alexander Duyck493fb302013-09-28 07:01:44 +00007594 goto unlock_pf;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007595 }
7596 vsi->type = type;
7597 vsi->back = pf;
7598 set_bit(__I40E_DOWN, &vsi->state);
7599 vsi->flags = 0;
7600 vsi->idx = vsi_idx;
Jesse Brandeburgac26fc12015-09-28 14:12:37 -04007601 vsi->int_rate_limit = 0;
Anjali Singhai Jain5db4cb52015-02-24 06:58:49 +00007602 vsi->rss_table_size = (vsi->type == I40E_VSI_MAIN) ?
7603 pf->rss_table_size : 64;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007604 vsi->netdev_registered = false;
7605 vsi->work_limit = I40E_DEFAULT_IRQ_WORK;
Jacob Keller278e7d02016-10-05 09:30:37 -07007606 hash_init(vsi->mac_filter_hash);
Shannon Nelson63741842014-04-23 04:50:16 +00007607 vsi->irqs_ready = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007608
Alexander Duyck9f65e152013-09-28 06:00:58 +00007609 ret = i40e_set_num_rings_in_vsi(vsi);
7610 if (ret)
7611 goto err_rings;
7612
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007613 ret = i40e_vsi_alloc_arrays(vsi, true);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007614 if (ret)
Alexander Duyck9f65e152013-09-28 06:00:58 +00007615 goto err_rings;
Alexander Duyck493fb302013-09-28 07:01:44 +00007616
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007617 /* Setup default MSIX irq handler for VSI */
7618 i40e_vsi_setup_irqhandler(vsi, i40e_msix_clean_rings);
7619
Kiran Patil21659032015-09-30 14:09:03 -04007620 /* Initialize VSI lock */
Jacob Keller278e7d02016-10-05 09:30:37 -07007621 spin_lock_init(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007622 pf->vsi[vsi_idx] = vsi;
7623 ret = vsi_idx;
Alexander Duyck493fb302013-09-28 07:01:44 +00007624 goto unlock_pf;
7625
Alexander Duyck9f65e152013-09-28 06:00:58 +00007626err_rings:
Alexander Duyck493fb302013-09-28 07:01:44 +00007627 pf->next_vsi = i - 1;
7628 kfree(vsi);
7629unlock_pf:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007630 mutex_unlock(&pf->switch_mutex);
7631 return ret;
7632}
7633
7634/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007635 * i40e_vsi_free_arrays - Free queue and vector pointer arrays for the VSI
7636 * @type: VSI pointer
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007637 * @free_qvectors: a bool to specify if q_vectors need to be freed.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007638 *
7639 * On error: returns error code (negative)
7640 * On success: returns 0
7641 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007642static void i40e_vsi_free_arrays(struct i40e_vsi *vsi, bool free_qvectors)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007643{
7644 /* free the ring and vector containers */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007645 if (free_qvectors) {
7646 kfree(vsi->q_vectors);
7647 vsi->q_vectors = NULL;
7648 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007649 kfree(vsi->tx_rings);
7650 vsi->tx_rings = NULL;
7651 vsi->rx_rings = NULL;
7652}
7653
7654/**
Helin Zhang28c58692015-10-26 19:44:27 -04007655 * i40e_clear_rss_config_user - clear the user configured RSS hash keys
7656 * and lookup table
7657 * @vsi: Pointer to VSI structure
7658 */
7659static void i40e_clear_rss_config_user(struct i40e_vsi *vsi)
7660{
7661 if (!vsi)
7662 return;
7663
7664 kfree(vsi->rss_hkey_user);
7665 vsi->rss_hkey_user = NULL;
7666
7667 kfree(vsi->rss_lut_user);
7668 vsi->rss_lut_user = NULL;
7669}
7670
7671/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007672 * i40e_vsi_clear - Deallocate the VSI provided
7673 * @vsi: the VSI being un-configured
7674 **/
7675static int i40e_vsi_clear(struct i40e_vsi *vsi)
7676{
7677 struct i40e_pf *pf;
7678
7679 if (!vsi)
7680 return 0;
7681
7682 if (!vsi->back)
7683 goto free_vsi;
7684 pf = vsi->back;
7685
7686 mutex_lock(&pf->switch_mutex);
7687 if (!pf->vsi[vsi->idx]) {
7688 dev_err(&pf->pdev->dev, "pf->vsi[%d] is NULL, just free vsi[%d](%p,type %d)\n",
7689 vsi->idx, vsi->idx, vsi, vsi->type);
7690 goto unlock_vsi;
7691 }
7692
7693 if (pf->vsi[vsi->idx] != vsi) {
7694 dev_err(&pf->pdev->dev,
7695 "pf->vsi[%d](%p, type %d) != vsi[%d](%p,type %d): no free!\n",
7696 pf->vsi[vsi->idx]->idx,
7697 pf->vsi[vsi->idx],
7698 pf->vsi[vsi->idx]->type,
7699 vsi->idx, vsi, vsi->type);
7700 goto unlock_vsi;
7701 }
7702
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00007703 /* updates the PF for this cleared vsi */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007704 i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
7705 i40e_put_lump(pf->irq_pile, vsi->base_vector, vsi->idx);
7706
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007707 i40e_vsi_free_arrays(vsi, true);
Helin Zhang28c58692015-10-26 19:44:27 -04007708 i40e_clear_rss_config_user(vsi);
Alexander Duyck493fb302013-09-28 07:01:44 +00007709
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007710 pf->vsi[vsi->idx] = NULL;
7711 if (vsi->idx < pf->next_vsi)
7712 pf->next_vsi = vsi->idx;
7713
7714unlock_vsi:
7715 mutex_unlock(&pf->switch_mutex);
7716free_vsi:
7717 kfree(vsi);
7718
7719 return 0;
7720}
7721
7722/**
Alexander Duyck9f65e152013-09-28 06:00:58 +00007723 * i40e_vsi_clear_rings - Deallocates the Rx and Tx rings for the provided VSI
7724 * @vsi: the VSI being cleaned
7725 **/
Shannon Nelsonbe1d5ee2013-11-28 06:39:23 +00007726static void i40e_vsi_clear_rings(struct i40e_vsi *vsi)
Alexander Duyck9f65e152013-09-28 06:00:58 +00007727{
7728 int i;
7729
Greg Rose8e9dca52013-12-18 13:45:53 +00007730 if (vsi->tx_rings && vsi->tx_rings[0]) {
Neerav Parikhd7397642013-11-28 06:39:37 +00007731 for (i = 0; i < vsi->alloc_queue_pairs; i++) {
Mitch Williams00403f02013-09-28 07:13:13 +00007732 kfree_rcu(vsi->tx_rings[i], rcu);
7733 vsi->tx_rings[i] = NULL;
7734 vsi->rx_rings[i] = NULL;
7735 }
Shannon Nelsonbe1d5ee2013-11-28 06:39:23 +00007736 }
Alexander Duyck9f65e152013-09-28 06:00:58 +00007737}
7738
7739/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007740 * i40e_alloc_rings - Allocates the Rx and Tx rings for the provided VSI
7741 * @vsi: the VSI being configured
7742 **/
7743static int i40e_alloc_rings(struct i40e_vsi *vsi)
7744{
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +00007745 struct i40e_ring *tx_ring, *rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007746 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007747 int i;
7748
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007749 /* Set basic values in the rings to be used later during open() */
Neerav Parikhd7397642013-11-28 06:39:37 +00007750 for (i = 0; i < vsi->alloc_queue_pairs; i++) {
Shannon Nelsonac6c5e32013-11-20 10:02:57 +00007751 /* allocate space for both Tx and Rx in one shot */
Alexander Duyck9f65e152013-09-28 06:00:58 +00007752 tx_ring = kzalloc(sizeof(struct i40e_ring) * 2, GFP_KERNEL);
7753 if (!tx_ring)
7754 goto err_out;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007755
7756 tx_ring->queue_index = i;
7757 tx_ring->reg_idx = vsi->base_queue + i;
7758 tx_ring->ring_active = false;
7759 tx_ring->vsi = vsi;
7760 tx_ring->netdev = vsi->netdev;
7761 tx_ring->dev = &pf->pdev->dev;
7762 tx_ring->count = vsi->num_desc;
7763 tx_ring->size = 0;
7764 tx_ring->dcb_tc = 0;
Anjali Singhai Jain8e0764b2015-06-05 12:20:30 -04007765 if (vsi->back->flags & I40E_FLAG_WB_ON_ITR_CAPABLE)
7766 tx_ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
Kan Lianga75e8002016-02-19 09:24:04 -05007767 tx_ring->tx_itr_setting = pf->tx_itr_default;
Alexander Duyck9f65e152013-09-28 06:00:58 +00007768 vsi->tx_rings[i] = tx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007769
Alexander Duyck9f65e152013-09-28 06:00:58 +00007770 rx_ring = &tx_ring[1];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007771 rx_ring->queue_index = i;
7772 rx_ring->reg_idx = vsi->base_queue + i;
7773 rx_ring->ring_active = false;
7774 rx_ring->vsi = vsi;
7775 rx_ring->netdev = vsi->netdev;
7776 rx_ring->dev = &pf->pdev->dev;
7777 rx_ring->count = vsi->num_desc;
7778 rx_ring->size = 0;
7779 rx_ring->dcb_tc = 0;
Kan Lianga75e8002016-02-19 09:24:04 -05007780 rx_ring->rx_itr_setting = pf->rx_itr_default;
Alexander Duyck9f65e152013-09-28 06:00:58 +00007781 vsi->rx_rings[i] = rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007782 }
7783
7784 return 0;
Alexander Duyck9f65e152013-09-28 06:00:58 +00007785
7786err_out:
7787 i40e_vsi_clear_rings(vsi);
7788 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007789}
7790
7791/**
7792 * i40e_reserve_msix_vectors - Reserve MSI-X vectors in the kernel
7793 * @pf: board private structure
7794 * @vectors: the number of MSI-X vectors to request
7795 *
7796 * Returns the number of vectors reserved, or error
7797 **/
7798static int i40e_reserve_msix_vectors(struct i40e_pf *pf, int vectors)
7799{
Alexander Gordeev7b37f372014-02-18 11:11:42 +01007800 vectors = pci_enable_msix_range(pf->pdev, pf->msix_entries,
7801 I40E_MIN_MSIX, vectors);
7802 if (vectors < 0) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007803 dev_info(&pf->pdev->dev,
Alexander Gordeev7b37f372014-02-18 11:11:42 +01007804 "MSI-X vector reservation failed: %d\n", vectors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007805 vectors = 0;
7806 }
7807
7808 return vectors;
7809}
7810
7811/**
7812 * i40e_init_msix - Setup the MSIX capability
7813 * @pf: board private structure
7814 *
7815 * Work with the OS to set up the MSIX vectors needed.
7816 *
Shannon Nelson3b444392015-02-26 16:15:57 +00007817 * Returns the number of vectors reserved or negative on failure
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007818 **/
7819static int i40e_init_msix(struct i40e_pf *pf)
7820{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007821 struct i40e_hw *hw = &pf->hw;
Jacob Kellerc0cf70a2017-01-24 10:24:01 -08007822 int cpus, extra_vectors;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007823 int vectors_left;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007824 int v_budget, i;
Shannon Nelson3b444392015-02-26 16:15:57 +00007825 int v_actual;
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007826 int iwarp_requested = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007827
7828 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
7829 return -ENODEV;
7830
7831 /* The number of vectors we'll request will be comprised of:
7832 * - Add 1 for "other" cause for Admin Queue events, etc.
7833 * - The number of LAN queue pairs
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00007834 * - Queues being used for RSS.
7835 * We don't need as many as max_rss_size vectors.
7836 * use rss_size instead in the calculation since that
7837 * is governed by number of cpus in the system.
7838 * - assumes symmetric Tx/Rx pairing
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007839 * - The number of VMDq pairs
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007840 * - The CPU count within the NUMA node if iWARP is enabled
Vasu Dev38e00432014-08-01 13:27:03 -07007841#ifdef I40E_FCOE
7842 * - The number of FCOE qps.
7843#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007844 * Once we count this up, try the request.
7845 *
7846 * If we can't get what we want, we'll simplify to nearly nothing
7847 * and try again. If that still fails, we punt.
7848 */
Shannon Nelson1e200e42015-02-27 09:15:24 +00007849 vectors_left = hw->func_caps.num_msix_vectors;
7850 v_budget = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007851
Shannon Nelson1e200e42015-02-27 09:15:24 +00007852 /* reserve one vector for miscellaneous handler */
7853 if (vectors_left) {
7854 v_budget++;
7855 vectors_left--;
7856 }
7857
Jacob Kellerc0cf70a2017-01-24 10:24:01 -08007858 /* reserve some vectors for the main PF traffic queues. Initially we
7859 * only reserve at most 50% of the available vectors, in the case that
7860 * the number of online CPUs is large. This ensures that we can enable
7861 * extra features as well. Once we've enabled the other features, we
7862 * will use any remaining vectors to reach as close as we can to the
7863 * number of online CPUs.
7864 */
7865 cpus = num_online_cpus();
7866 pf->num_lan_msix = min_t(int, cpus, vectors_left / 2);
Shannon Nelson1e200e42015-02-27 09:15:24 +00007867 vectors_left -= pf->num_lan_msix;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007868
7869 /* reserve one vector for sideband flow director */
7870 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
7871 if (vectors_left) {
Tushar Davea70e4072016-05-16 12:40:53 -07007872 pf->num_fdsb_msix = 1;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007873 v_budget++;
7874 vectors_left--;
7875 } else {
Tushar Davea70e4072016-05-16 12:40:53 -07007876 pf->num_fdsb_msix = 0;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007877 }
7878 }
John W Linville83840e42015-01-14 03:06:28 +00007879
Vasu Dev38e00432014-08-01 13:27:03 -07007880#ifdef I40E_FCOE
Shannon Nelson1e200e42015-02-27 09:15:24 +00007881 /* can we reserve enough for FCoE? */
Vasu Dev38e00432014-08-01 13:27:03 -07007882 if (pf->flags & I40E_FLAG_FCOE_ENABLED) {
Shannon Nelson1e200e42015-02-27 09:15:24 +00007883 if (!vectors_left)
7884 pf->num_fcoe_msix = 0;
7885 else if (vectors_left >= pf->num_fcoe_qps)
7886 pf->num_fcoe_msix = pf->num_fcoe_qps;
7887 else
7888 pf->num_fcoe_msix = 1;
Vasu Dev38e00432014-08-01 13:27:03 -07007889 v_budget += pf->num_fcoe_msix;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007890 vectors_left -= pf->num_fcoe_msix;
Vasu Dev38e00432014-08-01 13:27:03 -07007891 }
Shannon Nelson1e200e42015-02-27 09:15:24 +00007892
Vasu Dev38e00432014-08-01 13:27:03 -07007893#endif
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007894 /* can we reserve enough for iWARP? */
7895 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02007896 iwarp_requested = pf->num_iwarp_msix;
7897
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007898 if (!vectors_left)
7899 pf->num_iwarp_msix = 0;
7900 else if (vectors_left < pf->num_iwarp_msix)
7901 pf->num_iwarp_msix = 1;
7902 v_budget += pf->num_iwarp_msix;
7903 vectors_left -= pf->num_iwarp_msix;
7904 }
7905
Shannon Nelson1e200e42015-02-27 09:15:24 +00007906 /* any vectors left over go for VMDq support */
7907 if (pf->flags & I40E_FLAG_VMDQ_ENABLED) {
7908 int vmdq_vecs_wanted = pf->num_vmdq_vsis * pf->num_vmdq_qps;
7909 int vmdq_vecs = min_t(int, vectors_left, vmdq_vecs_wanted);
7910
Stefan Assmann9ca57e92016-09-19 13:37:49 +02007911 if (!vectors_left) {
7912 pf->num_vmdq_msix = 0;
7913 pf->num_vmdq_qps = 0;
7914 } else {
7915 /* if we're short on vectors for what's desired, we limit
7916 * the queues per vmdq. If this is still more than are
7917 * available, the user will need to change the number of
7918 * queues/vectors used by the PF later with the ethtool
7919 * channels command
7920 */
7921 if (vmdq_vecs < vmdq_vecs_wanted)
7922 pf->num_vmdq_qps = 1;
7923 pf->num_vmdq_msix = pf->num_vmdq_qps;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007924
Stefan Assmann9ca57e92016-09-19 13:37:49 +02007925 v_budget += vmdq_vecs;
7926 vectors_left -= vmdq_vecs;
7927 }
Shannon Nelson1e200e42015-02-27 09:15:24 +00007928 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007929
Jacob Kellerc0cf70a2017-01-24 10:24:01 -08007930 /* On systems with a large number of SMP cores, we previously limited
7931 * the number of vectors for num_lan_msix to be at most 50% of the
7932 * available vectors, to allow for other features. Now, we add back
7933 * the remaining vectors. However, we ensure that the total
7934 * num_lan_msix will not exceed num_online_cpus(). To do this, we
7935 * calculate the number of vectors we can add without going over the
7936 * cap of CPUs. For systems with a small number of CPUs this will be
7937 * zero.
7938 */
7939 extra_vectors = min_t(int, cpus - pf->num_lan_msix, vectors_left);
7940 pf->num_lan_msix += extra_vectors;
7941 vectors_left -= extra_vectors;
7942
7943 WARN(vectors_left < 0,
7944 "Calculation of remaining vectors underflowed. This is an accounting bug when determining total MSI-X vectors.\n");
7945
7946 v_budget += pf->num_lan_msix;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007947 pf->msix_entries = kcalloc(v_budget, sizeof(struct msix_entry),
7948 GFP_KERNEL);
7949 if (!pf->msix_entries)
7950 return -ENOMEM;
7951
7952 for (i = 0; i < v_budget; i++)
7953 pf->msix_entries[i].entry = i;
Shannon Nelson3b444392015-02-26 16:15:57 +00007954 v_actual = i40e_reserve_msix_vectors(pf, v_budget);
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007955
Shannon Nelson3b444392015-02-26 16:15:57 +00007956 if (v_actual < I40E_MIN_MSIX) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007957 pf->flags &= ~I40E_FLAG_MSIX_ENABLED;
7958 kfree(pf->msix_entries);
7959 pf->msix_entries = NULL;
Guilherme G Piccoli4c95aa52016-09-22 10:03:58 -03007960 pci_disable_msix(pf->pdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007961 return -ENODEV;
7962
Shannon Nelson3b444392015-02-26 16:15:57 +00007963 } else if (v_actual == I40E_MIN_MSIX) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007964 /* Adjust for minimal MSIX use */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007965 pf->num_vmdq_vsis = 0;
7966 pf->num_vmdq_qps = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007967 pf->num_lan_qps = 1;
7968 pf->num_lan_msix = 1;
7969
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02007970 } else if (!vectors_left) {
7971 /* If we have limited resources, we will start with no vectors
7972 * for the special features and then allocate vectors to some
7973 * of these features based on the policy and at the end disable
7974 * the features that did not get any vectors.
7975 */
Shannon Nelson3b444392015-02-26 16:15:57 +00007976 int vec;
7977
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02007978 dev_info(&pf->pdev->dev,
7979 "MSI-X vector limit reached, attempting to redistribute vectors\n");
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007980 /* reserve the misc vector */
Shannon Nelson3b444392015-02-26 16:15:57 +00007981 vec = v_actual - 1;
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007982
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007983 /* Scale vector usage down */
7984 pf->num_vmdq_msix = 1; /* force VMDqs to only one vector */
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007985 pf->num_vmdq_vsis = 1;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007986 pf->num_vmdq_qps = 1;
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02007987#ifdef I40E_FCOE
7988 pf->num_fcoe_qps = 0;
7989 pf->num_fcoe_msix = 0;
7990#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007991
7992 /* partition out the remaining vectors */
7993 switch (vec) {
7994 case 2:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007995 pf->num_lan_msix = 1;
7996 break;
7997 case 3:
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007998 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
7999 pf->num_lan_msix = 1;
8000 pf->num_iwarp_msix = 1;
8001 } else {
8002 pf->num_lan_msix = 2;
8003 }
Vasu Dev38e00432014-08-01 13:27:03 -07008004#ifdef I40E_FCOE
8005 /* give one vector to FCoE */
8006 if (pf->flags & I40E_FLAG_FCOE_ENABLED) {
8007 pf->num_lan_msix = 1;
8008 pf->num_fcoe_msix = 1;
8009 }
Vasu Dev38e00432014-08-01 13:27:03 -07008010#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008011 break;
8012 default:
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06008013 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
8014 pf->num_iwarp_msix = min_t(int, (vec / 3),
8015 iwarp_requested);
8016 pf->num_vmdq_vsis = min_t(int, (vec / 3),
8017 I40E_DEFAULT_NUM_VMDQ_VSI);
8018 } else {
8019 pf->num_vmdq_vsis = min_t(int, (vec / 2),
8020 I40E_DEFAULT_NUM_VMDQ_VSI);
8021 }
Stefan Assmannabd97a92016-09-19 13:37:51 +02008022 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
8023 pf->num_fdsb_msix = 1;
8024 vec--;
8025 }
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06008026 pf->num_lan_msix = min_t(int,
8027 (vec - (pf->num_iwarp_msix + pf->num_vmdq_vsis)),
8028 pf->num_lan_msix);
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02008029 pf->num_lan_qps = pf->num_lan_msix;
Vasu Dev38e00432014-08-01 13:27:03 -07008030#ifdef I40E_FCOE
8031 /* give one vector to FCoE */
8032 if (pf->flags & I40E_FLAG_FCOE_ENABLED) {
8033 pf->num_fcoe_msix = 1;
8034 vec--;
8035 }
8036#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008037 break;
8038 }
8039 }
8040
Stefan Assmannabd97a92016-09-19 13:37:51 +02008041 if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
8042 (pf->num_fdsb_msix == 0)) {
8043 dev_info(&pf->pdev->dev, "Sideband Flowdir disabled, not enough MSI-X vectors\n");
8044 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
8045 }
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00008046 if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
8047 (pf->num_vmdq_msix == 0)) {
8048 dev_info(&pf->pdev->dev, "VMDq disabled, not enough MSI-X vectors\n");
8049 pf->flags &= ~I40E_FLAG_VMDQ_ENABLED;
8050 }
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06008051
8052 if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
8053 (pf->num_iwarp_msix == 0)) {
8054 dev_info(&pf->pdev->dev, "IWARP disabled, not enough MSI-X vectors\n");
8055 pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
8056 }
Vasu Dev38e00432014-08-01 13:27:03 -07008057#ifdef I40E_FCOE
8058
8059 if ((pf->flags & I40E_FLAG_FCOE_ENABLED) && (pf->num_fcoe_msix == 0)) {
8060 dev_info(&pf->pdev->dev, "FCOE disabled, not enough MSI-X vectors\n");
8061 pf->flags &= ~I40E_FLAG_FCOE_ENABLED;
8062 }
8063#endif
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02008064 i40e_debug(&pf->hw, I40E_DEBUG_INIT,
8065 "MSI-X vector distribution: PF %d, VMDq %d, FDSB %d, iWARP %d\n",
8066 pf->num_lan_msix,
8067 pf->num_vmdq_msix * pf->num_vmdq_vsis,
8068 pf->num_fdsb_msix,
8069 pf->num_iwarp_msix);
8070
Shannon Nelson3b444392015-02-26 16:15:57 +00008071 return v_actual;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008072}
8073
8074/**
Greg Rose90e04072014-03-06 08:59:57 +00008075 * i40e_vsi_alloc_q_vector - Allocate memory for a single interrupt vector
Alexander Duyck493fb302013-09-28 07:01:44 +00008076 * @vsi: the VSI being configured
8077 * @v_idx: index of the vector in the vsi struct
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03008078 * @cpu: cpu to be used on affinity_mask
Alexander Duyck493fb302013-09-28 07:01:44 +00008079 *
8080 * We allocate one q_vector. If allocation fails we return -ENOMEM.
8081 **/
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03008082static int i40e_vsi_alloc_q_vector(struct i40e_vsi *vsi, int v_idx, int cpu)
Alexander Duyck493fb302013-09-28 07:01:44 +00008083{
8084 struct i40e_q_vector *q_vector;
8085
8086 /* allocate q_vector */
8087 q_vector = kzalloc(sizeof(struct i40e_q_vector), GFP_KERNEL);
8088 if (!q_vector)
8089 return -ENOMEM;
8090
8091 q_vector->vsi = vsi;
8092 q_vector->v_idx = v_idx;
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03008093 cpumask_set_cpu(cpu, &q_vector->affinity_mask);
8094
Alexander Duyck493fb302013-09-28 07:01:44 +00008095 if (vsi->netdev)
8096 netif_napi_add(vsi->netdev, &q_vector->napi,
Jesse Brandeburgeefeace2014-05-10 04:49:13 +00008097 i40e_napi_poll, NAPI_POLL_WEIGHT);
Alexander Duyck493fb302013-09-28 07:01:44 +00008098
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00008099 q_vector->rx.latency_range = I40E_LOW_LATENCY;
8100 q_vector->tx.latency_range = I40E_LOW_LATENCY;
8101
Alexander Duyck493fb302013-09-28 07:01:44 +00008102 /* tie q_vector and vsi together */
8103 vsi->q_vectors[v_idx] = q_vector;
8104
8105 return 0;
8106}
8107
8108/**
Greg Rose90e04072014-03-06 08:59:57 +00008109 * i40e_vsi_alloc_q_vectors - Allocate memory for interrupt vectors
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008110 * @vsi: the VSI being configured
8111 *
8112 * We allocate one q_vector per queue interrupt. If allocation fails we
8113 * return -ENOMEM.
8114 **/
Greg Rose90e04072014-03-06 08:59:57 +00008115static int i40e_vsi_alloc_q_vectors(struct i40e_vsi *vsi)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008116{
8117 struct i40e_pf *pf = vsi->back;
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03008118 int err, v_idx, num_q_vectors, current_cpu;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008119
8120 /* if not MSIX, give the one vector only to the LAN VSI */
8121 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
8122 num_q_vectors = vsi->num_q_vectors;
8123 else if (vsi == pf->vsi[pf->lan_vsi])
8124 num_q_vectors = 1;
8125 else
8126 return -EINVAL;
8127
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03008128 current_cpu = cpumask_first(cpu_online_mask);
8129
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008130 for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03008131 err = i40e_vsi_alloc_q_vector(vsi, v_idx, current_cpu);
Alexander Duyck493fb302013-09-28 07:01:44 +00008132 if (err)
8133 goto err_out;
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03008134 current_cpu = cpumask_next(current_cpu, cpu_online_mask);
8135 if (unlikely(current_cpu >= nr_cpu_ids))
8136 current_cpu = cpumask_first(cpu_online_mask);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008137 }
8138
8139 return 0;
Alexander Duyck493fb302013-09-28 07:01:44 +00008140
8141err_out:
8142 while (v_idx--)
8143 i40e_free_q_vector(vsi, v_idx);
8144
8145 return err;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008146}
8147
8148/**
8149 * i40e_init_interrupt_scheme - Determine proper interrupt scheme
8150 * @pf: board private structure to initialize
8151 **/
Jesse Brandeburgc11472802015-04-07 19:45:39 -04008152static int i40e_init_interrupt_scheme(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008153{
Shannon Nelson3b444392015-02-26 16:15:57 +00008154 int vectors = 0;
8155 ssize_t size;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008156
8157 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
Shannon Nelson3b444392015-02-26 16:15:57 +00008158 vectors = i40e_init_msix(pf);
8159 if (vectors < 0) {
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08008160 pf->flags &= ~(I40E_FLAG_MSIX_ENABLED |
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06008161 I40E_FLAG_IWARP_ENABLED |
Vasu Dev38e00432014-08-01 13:27:03 -07008162#ifdef I40E_FCOE
8163 I40E_FLAG_FCOE_ENABLED |
8164#endif
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08008165 I40E_FLAG_RSS_ENABLED |
Neerav Parikh4d9b6042014-05-22 06:31:51 +00008166 I40E_FLAG_DCB_CAPABLE |
Dave Ertmana0362442016-08-29 17:38:26 -07008167 I40E_FLAG_DCB_ENABLED |
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08008168 I40E_FLAG_SRIOV_ENABLED |
8169 I40E_FLAG_FD_SB_ENABLED |
8170 I40E_FLAG_FD_ATR_ENABLED |
8171 I40E_FLAG_VMDQ_ENABLED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008172
8173 /* rework the queue expectations without MSIX */
8174 i40e_determine_queue_usage(pf);
8175 }
8176 }
8177
8178 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED) &&
8179 (pf->flags & I40E_FLAG_MSI_ENABLED)) {
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08008180 dev_info(&pf->pdev->dev, "MSI-X not available, trying MSI\n");
Shannon Nelson3b444392015-02-26 16:15:57 +00008181 vectors = pci_enable_msi(pf->pdev);
8182 if (vectors < 0) {
8183 dev_info(&pf->pdev->dev, "MSI init failed - %d\n",
8184 vectors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008185 pf->flags &= ~I40E_FLAG_MSI_ENABLED;
8186 }
Shannon Nelson3b444392015-02-26 16:15:57 +00008187 vectors = 1; /* one MSI or Legacy vector */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008188 }
8189
Shannon Nelson958a3e32013-09-28 07:13:28 +00008190 if (!(pf->flags & (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED)))
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08008191 dev_info(&pf->pdev->dev, "MSI-X and MSI not available, falling back to Legacy IRQ\n");
Shannon Nelson958a3e32013-09-28 07:13:28 +00008192
Shannon Nelson3b444392015-02-26 16:15:57 +00008193 /* set up vector assignment tracking */
8194 size = sizeof(struct i40e_lump_tracking) + (sizeof(u16) * vectors);
8195 pf->irq_pile = kzalloc(size, GFP_KERNEL);
Jesse Brandeburgc11472802015-04-07 19:45:39 -04008196 if (!pf->irq_pile) {
8197 dev_err(&pf->pdev->dev, "error allocating irq_pile memory\n");
8198 return -ENOMEM;
8199 }
Shannon Nelson3b444392015-02-26 16:15:57 +00008200 pf->irq_pile->num_entries = vectors;
8201 pf->irq_pile->search_hint = 0;
8202
Jesse Brandeburgc11472802015-04-07 19:45:39 -04008203 /* track first vector for misc interrupts, ignore return */
Shannon Nelson3b444392015-02-26 16:15:57 +00008204 (void)i40e_get_lump(pf, pf->irq_pile, 1, I40E_PILE_VALID_BIT - 1);
Jesse Brandeburgc11472802015-04-07 19:45:39 -04008205
8206 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008207}
8208
8209/**
8210 * i40e_setup_misc_vector - Setup the misc vector to handle non queue events
8211 * @pf: board private structure
8212 *
8213 * This sets up the handler for MSIX 0, which is used to manage the
8214 * non-queue interrupts, e.g. AdminQ and errors. This is not used
8215 * when in MSI or Legacy interrupt mode.
8216 **/
8217static int i40e_setup_misc_vector(struct i40e_pf *pf)
8218{
8219 struct i40e_hw *hw = &pf->hw;
8220 int err = 0;
8221
8222 /* Only request the irq if this is the first time through, and
8223 * not when we're rebuilding after a Reset
8224 */
8225 if (!test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state)) {
8226 err = request_irq(pf->msix_entries[0].vector,
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00008227 i40e_intr, 0, pf->int_name, pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008228 if (err) {
8229 dev_info(&pf->pdev->dev,
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08008230 "request_irq for %s failed: %d\n",
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00008231 pf->int_name, err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008232 return -EFAULT;
8233 }
8234 }
8235
Jacob Kellerab437b52014-12-14 01:55:08 +00008236 i40e_enable_misc_int_causes(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008237
8238 /* associate no queues to the misc vector */
8239 wr32(hw, I40E_PFINT_LNKLST0, I40E_QUEUE_END_OF_LIST);
8240 wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), I40E_ITR_8K);
8241
8242 i40e_flush(hw);
8243
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08008244 i40e_irq_dynamic_enable_icr0(pf, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008245
8246 return err;
8247}
8248
8249/**
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04008250 * i40e_config_rss_aq - Prepare for RSS using AQ commands
8251 * @vsi: vsi structure
8252 * @seed: RSS hash seed
8253 **/
Helin Zhange69ff812015-10-21 19:56:22 -04008254static int i40e_config_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
8255 u8 *lut, u16 lut_size)
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04008256{
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04008257 struct i40e_pf *pf = vsi->back;
8258 struct i40e_hw *hw = &pf->hw;
Jacob Keller776b2e12016-07-19 16:23:30 -07008259 int ret = 0;
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04008260
Jacob Keller776b2e12016-07-19 16:23:30 -07008261 if (seed) {
8262 struct i40e_aqc_get_set_rss_key_data *seed_dw =
8263 (struct i40e_aqc_get_set_rss_key_data *)seed;
8264 ret = i40e_aq_set_rss_key(hw, vsi->id, seed_dw);
8265 if (ret) {
8266 dev_info(&pf->pdev->dev,
8267 "Cannot set RSS key, err %s aq_err %s\n",
8268 i40e_stat_str(hw, ret),
8269 i40e_aq_str(hw, hw->aq.asq_last_status));
8270 return ret;
8271 }
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04008272 }
Jacob Keller776b2e12016-07-19 16:23:30 -07008273 if (lut) {
8274 bool pf_lut = vsi->type == I40E_VSI_MAIN ? true : false;
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04008275
Jacob Keller776b2e12016-07-19 16:23:30 -07008276 ret = i40e_aq_set_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
8277 if (ret) {
8278 dev_info(&pf->pdev->dev,
8279 "Cannot set RSS lut, err %s aq_err %s\n",
8280 i40e_stat_str(hw, ret),
8281 i40e_aq_str(hw, hw->aq.asq_last_status));
8282 return ret;
8283 }
8284 }
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04008285 return ret;
8286}
8287
8288/**
Anjali Singhai Jain95a73782015-12-22 14:25:04 -08008289 * i40e_get_rss_aq - Get RSS keys and lut by using AQ commands
8290 * @vsi: Pointer to vsi structure
8291 * @seed: Buffter to store the hash keys
8292 * @lut: Buffer to store the lookup table entries
8293 * @lut_size: Size of buffer to store the lookup table entries
8294 *
8295 * Return 0 on success, negative on failure
8296 */
8297static int i40e_get_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
8298 u8 *lut, u16 lut_size)
8299{
8300 struct i40e_pf *pf = vsi->back;
8301 struct i40e_hw *hw = &pf->hw;
8302 int ret = 0;
8303
8304 if (seed) {
8305 ret = i40e_aq_get_rss_key(hw, vsi->id,
8306 (struct i40e_aqc_get_set_rss_key_data *)seed);
8307 if (ret) {
8308 dev_info(&pf->pdev->dev,
8309 "Cannot get RSS key, err %s aq_err %s\n",
8310 i40e_stat_str(&pf->hw, ret),
8311 i40e_aq_str(&pf->hw,
8312 pf->hw.aq.asq_last_status));
8313 return ret;
8314 }
8315 }
8316
8317 if (lut) {
8318 bool pf_lut = vsi->type == I40E_VSI_MAIN ? true : false;
8319
8320 ret = i40e_aq_get_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
8321 if (ret) {
8322 dev_info(&pf->pdev->dev,
8323 "Cannot get RSS lut, err %s aq_err %s\n",
8324 i40e_stat_str(&pf->hw, ret),
8325 i40e_aq_str(&pf->hw,
8326 pf->hw.aq.asq_last_status));
8327 return ret;
8328 }
8329 }
8330
8331 return ret;
8332}
8333
8334/**
Jacob Keller0582b962016-07-19 16:23:29 -07008335 * i40e_vsi_config_rss - Prepare for VSI(VMDq) RSS if used
8336 * @vsi: VSI structure
8337 **/
8338static int i40e_vsi_config_rss(struct i40e_vsi *vsi)
8339{
8340 u8 seed[I40E_HKEY_ARRAY_SIZE];
8341 struct i40e_pf *pf = vsi->back;
8342 u8 *lut;
8343 int ret;
8344
8345 if (!(pf->flags & I40E_FLAG_RSS_AQ_CAPABLE))
8346 return 0;
8347
Jacob Keller552b9962016-07-19 16:23:31 -07008348 if (!vsi->rss_size)
8349 vsi->rss_size = min_t(int, pf->alloc_rss_size,
8350 vsi->num_queue_pairs);
8351 if (!vsi->rss_size)
8352 return -EINVAL;
8353
Jacob Keller0582b962016-07-19 16:23:29 -07008354 lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
8355 if (!lut)
8356 return -ENOMEM;
Jacob Keller552b9962016-07-19 16:23:31 -07008357 /* Use the user configured hash keys and lookup table if there is one,
8358 * otherwise use default
8359 */
8360 if (vsi->rss_lut_user)
8361 memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
8362 else
8363 i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
8364 if (vsi->rss_hkey_user)
8365 memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
8366 else
8367 netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
Jacob Keller0582b962016-07-19 16:23:29 -07008368 ret = i40e_config_rss_aq(vsi, seed, lut, vsi->rss_table_size);
8369 kfree(lut);
8370
8371 return ret;
8372}
8373
8374/**
Helin Zhang043dd652015-10-21 19:56:23 -04008375 * i40e_config_rss_reg - Configure RSS keys and lut by writing registers
Helin Zhange69ff812015-10-21 19:56:22 -04008376 * @vsi: Pointer to vsi structure
8377 * @seed: RSS hash seed
8378 * @lut: Lookup table
8379 * @lut_size: Lookup table size
8380 *
8381 * Returns 0 on success, negative on failure
8382 **/
8383static int i40e_config_rss_reg(struct i40e_vsi *vsi, const u8 *seed,
8384 const u8 *lut, u16 lut_size)
8385{
8386 struct i40e_pf *pf = vsi->back;
8387 struct i40e_hw *hw = &pf->hw;
Mitch Williamsc4e18682016-04-12 08:30:40 -07008388 u16 vf_id = vsi->vf_id;
Helin Zhange69ff812015-10-21 19:56:22 -04008389 u8 i;
8390
8391 /* Fill out hash function seed */
8392 if (seed) {
8393 u32 *seed_dw = (u32 *)seed;
8394
Mitch Williamsc4e18682016-04-12 08:30:40 -07008395 if (vsi->type == I40E_VSI_MAIN) {
8396 for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
8397 i40e_write_rx_ctl(hw, I40E_PFQF_HKEY(i),
8398 seed_dw[i]);
8399 } else if (vsi->type == I40E_VSI_SRIOV) {
8400 for (i = 0; i <= I40E_VFQF_HKEY1_MAX_INDEX; i++)
8401 i40e_write_rx_ctl(hw,
8402 I40E_VFQF_HKEY1(i, vf_id),
8403 seed_dw[i]);
8404 } else {
8405 dev_err(&pf->pdev->dev, "Cannot set RSS seed - invalid VSI type\n");
8406 }
Helin Zhange69ff812015-10-21 19:56:22 -04008407 }
8408
8409 if (lut) {
8410 u32 *lut_dw = (u32 *)lut;
8411
Mitch Williamsc4e18682016-04-12 08:30:40 -07008412 if (vsi->type == I40E_VSI_MAIN) {
8413 if (lut_size != I40E_HLUT_ARRAY_SIZE)
8414 return -EINVAL;
8415 for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
8416 wr32(hw, I40E_PFQF_HLUT(i), lut_dw[i]);
8417 } else if (vsi->type == I40E_VSI_SRIOV) {
8418 if (lut_size != I40E_VF_HLUT_ARRAY_SIZE)
8419 return -EINVAL;
8420 for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
8421 i40e_write_rx_ctl(hw,
8422 I40E_VFQF_HLUT1(i, vf_id),
8423 lut_dw[i]);
8424 } else {
8425 dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
8426 }
Helin Zhange69ff812015-10-21 19:56:22 -04008427 }
8428 i40e_flush(hw);
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04008429
8430 return 0;
8431}
8432
8433/**
Helin Zhang043dd652015-10-21 19:56:23 -04008434 * i40e_get_rss_reg - Get the RSS keys and lut by reading registers
8435 * @vsi: Pointer to VSI structure
8436 * @seed: Buffer to store the keys
8437 * @lut: Buffer to store the lookup table entries
8438 * @lut_size: Size of buffer to store the lookup table entries
8439 *
8440 * Returns 0 on success, negative on failure
8441 */
8442static int i40e_get_rss_reg(struct i40e_vsi *vsi, u8 *seed,
8443 u8 *lut, u16 lut_size)
8444{
8445 struct i40e_pf *pf = vsi->back;
8446 struct i40e_hw *hw = &pf->hw;
8447 u16 i;
8448
8449 if (seed) {
8450 u32 *seed_dw = (u32 *)seed;
8451
8452 for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
Shannon Nelson272cdaf22016-02-17 16:12:21 -08008453 seed_dw[i] = i40e_read_rx_ctl(hw, I40E_PFQF_HKEY(i));
Helin Zhang043dd652015-10-21 19:56:23 -04008454 }
8455 if (lut) {
8456 u32 *lut_dw = (u32 *)lut;
8457
8458 if (lut_size != I40E_HLUT_ARRAY_SIZE)
8459 return -EINVAL;
8460 for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
8461 lut_dw[i] = rd32(hw, I40E_PFQF_HLUT(i));
8462 }
8463
8464 return 0;
8465}
8466
8467/**
8468 * i40e_config_rss - Configure RSS keys and lut
8469 * @vsi: Pointer to VSI structure
8470 * @seed: RSS hash seed
8471 * @lut: Lookup table
8472 * @lut_size: Lookup table size
8473 *
8474 * Returns 0 on success, negative on failure
8475 */
8476int i40e_config_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
8477{
8478 struct i40e_pf *pf = vsi->back;
8479
8480 if (pf->flags & I40E_FLAG_RSS_AQ_CAPABLE)
8481 return i40e_config_rss_aq(vsi, seed, lut, lut_size);
8482 else
8483 return i40e_config_rss_reg(vsi, seed, lut, lut_size);
8484}
8485
8486/**
8487 * i40e_get_rss - Get RSS keys and lut
8488 * @vsi: Pointer to VSI structure
8489 * @seed: Buffer to store the keys
8490 * @lut: Buffer to store the lookup table entries
8491 * lut_size: Size of buffer to store the lookup table entries
8492 *
8493 * Returns 0 on success, negative on failure
8494 */
8495int i40e_get_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
8496{
Anjali Singhai Jain95a73782015-12-22 14:25:04 -08008497 struct i40e_pf *pf = vsi->back;
8498
8499 if (pf->flags & I40E_FLAG_RSS_AQ_CAPABLE)
8500 return i40e_get_rss_aq(vsi, seed, lut, lut_size);
8501 else
8502 return i40e_get_rss_reg(vsi, seed, lut, lut_size);
Helin Zhang043dd652015-10-21 19:56:23 -04008503}
8504
8505/**
Helin Zhange69ff812015-10-21 19:56:22 -04008506 * i40e_fill_rss_lut - Fill the RSS lookup table with default values
8507 * @pf: Pointer to board private structure
8508 * @lut: Lookup table
8509 * @rss_table_size: Lookup table size
8510 * @rss_size: Range of queue number for hashing
8511 */
Alan Bradyf1582352016-08-24 11:33:46 -07008512void i40e_fill_rss_lut(struct i40e_pf *pf, u8 *lut,
8513 u16 rss_table_size, u16 rss_size)
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04008514{
Helin Zhange69ff812015-10-21 19:56:22 -04008515 u16 i;
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04008516
Helin Zhange69ff812015-10-21 19:56:22 -04008517 for (i = 0; i < rss_table_size; i++)
8518 lut[i] = i % rss_size;
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04008519}
8520
8521/**
Helin Zhang043dd652015-10-21 19:56:23 -04008522 * i40e_pf_config_rss - Prepare for RSS if used
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008523 * @pf: board private structure
8524 **/
Helin Zhang043dd652015-10-21 19:56:23 -04008525static int i40e_pf_config_rss(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008526{
Anjali Singhai Jain66ddcff2015-02-24 06:58:50 +00008527 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04008528 u8 seed[I40E_HKEY_ARRAY_SIZE];
Helin Zhange69ff812015-10-21 19:56:22 -04008529 u8 *lut;
Anjali Singhai Jain4617e8c2013-11-20 10:02:56 +00008530 struct i40e_hw *hw = &pf->hw;
Carolyn Wybornye157ea32014-06-03 23:50:22 +00008531 u32 reg_val;
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04008532 u64 hena;
Helin Zhange69ff812015-10-21 19:56:22 -04008533 int ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008534
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008535 /* By default we enable TCP/UDP with IPv4/IPv6 ptypes */
Shannon Nelson272cdaf22016-02-17 16:12:21 -08008536 hena = (u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(0)) |
8537 ((u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(1)) << 32);
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04008538 hena |= i40e_pf_get_default_rss_hena(pf);
8539
Shannon Nelson272cdaf22016-02-17 16:12:21 -08008540 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), (u32)hena);
8541 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008542
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04008543 /* Determine the RSS table size based on the hardware capabilities */
Shannon Nelson272cdaf22016-02-17 16:12:21 -08008544 reg_val = i40e_read_rx_ctl(hw, I40E_PFQF_CTL_0);
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04008545 reg_val = (pf->rss_table_size == 512) ?
8546 (reg_val | I40E_PFQF_CTL_0_HASHLUTSIZE_512) :
8547 (reg_val & ~I40E_PFQF_CTL_0_HASHLUTSIZE_512);
Shannon Nelson272cdaf22016-02-17 16:12:21 -08008548 i40e_write_rx_ctl(hw, I40E_PFQF_CTL_0, reg_val);
Carolyn Wybornye157ea32014-06-03 23:50:22 +00008549
Helin Zhang28c58692015-10-26 19:44:27 -04008550 /* Determine the RSS size of the VSI */
8551 if (!vsi->rss_size)
Helin Zhangacd65442015-10-26 19:44:28 -04008552 vsi->rss_size = min_t(int, pf->alloc_rss_size,
8553 vsi->num_queue_pairs);
Mitch Williamsa4fa59c2016-09-12 14:18:43 -07008554 if (!vsi->rss_size)
8555 return -EINVAL;
Helin Zhang28c58692015-10-26 19:44:27 -04008556
Helin Zhange69ff812015-10-21 19:56:22 -04008557 lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
8558 if (!lut)
8559 return -ENOMEM;
8560
Helin Zhang28c58692015-10-26 19:44:27 -04008561 /* Use user configured lut if there is one, otherwise use default */
8562 if (vsi->rss_lut_user)
8563 memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
8564 else
8565 i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
Helin Zhange69ff812015-10-21 19:56:22 -04008566
Helin Zhang28c58692015-10-26 19:44:27 -04008567 /* Use user configured hash key if there is one, otherwise
8568 * use default.
8569 */
8570 if (vsi->rss_hkey_user)
8571 memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
8572 else
8573 netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
Helin Zhang043dd652015-10-21 19:56:23 -04008574 ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
Helin Zhange69ff812015-10-21 19:56:22 -04008575 kfree(lut);
8576
8577 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008578}
8579
8580/**
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008581 * i40e_reconfig_rss_queues - change number of queues for rss and rebuild
8582 * @pf: board private structure
8583 * @queue_count: the requested queue count for rss.
8584 *
8585 * returns 0 if rss is not enabled, if enabled returns the final rss queue
8586 * count which may be different from the requested queue count.
8587 **/
8588int i40e_reconfig_rss_queues(struct i40e_pf *pf, int queue_count)
8589{
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00008590 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
8591 int new_rss_size;
8592
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008593 if (!(pf->flags & I40E_FLAG_RSS_ENABLED))
8594 return 0;
8595
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00008596 new_rss_size = min_t(int, queue_count, pf->rss_size_max);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008597
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00008598 if (queue_count != vsi->num_queue_pairs) {
8599 vsi->req_queue_pairs = queue_count;
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008600 i40e_prep_for_reset(pf);
8601
Helin Zhangacd65442015-10-26 19:44:28 -04008602 pf->alloc_rss_size = new_rss_size;
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008603
8604 i40e_reset_and_rebuild(pf, true);
Helin Zhang28c58692015-10-26 19:44:27 -04008605
8606 /* Discard the user configured hash keys and lut, if less
8607 * queues are enabled.
8608 */
8609 if (queue_count < vsi->rss_size) {
8610 i40e_clear_rss_config_user(vsi);
8611 dev_dbg(&pf->pdev->dev,
8612 "discard user configured hash keys and lut\n");
8613 }
8614
8615 /* Reset vsi->rss_size, as number of enabled queues changed */
Helin Zhangacd65442015-10-26 19:44:28 -04008616 vsi->rss_size = min_t(int, pf->alloc_rss_size,
8617 vsi->num_queue_pairs);
Helin Zhang28c58692015-10-26 19:44:27 -04008618
Helin Zhang043dd652015-10-21 19:56:23 -04008619 i40e_pf_config_rss(pf);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008620 }
Lihong Yang12815052016-09-27 11:28:48 -07008621 dev_info(&pf->pdev->dev, "User requested queue count/HW max RSS count: %d/%d\n",
8622 vsi->req_queue_pairs, pf->rss_size_max);
Helin Zhangacd65442015-10-26 19:44:28 -04008623 return pf->alloc_rss_size;
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008624}
8625
8626/**
Greg Rosef4492db2015-02-06 08:52:12 +00008627 * i40e_get_npar_bw_setting - Retrieve BW settings for this PF partition
8628 * @pf: board private structure
8629 **/
8630i40e_status i40e_get_npar_bw_setting(struct i40e_pf *pf)
8631{
8632 i40e_status status;
8633 bool min_valid, max_valid;
8634 u32 max_bw, min_bw;
8635
8636 status = i40e_read_bw_from_alt_ram(&pf->hw, &max_bw, &min_bw,
8637 &min_valid, &max_valid);
8638
8639 if (!status) {
8640 if (min_valid)
8641 pf->npar_min_bw = min_bw;
8642 if (max_valid)
8643 pf->npar_max_bw = max_bw;
8644 }
8645
8646 return status;
8647}
8648
8649/**
8650 * i40e_set_npar_bw_setting - Set BW settings for this PF partition
8651 * @pf: board private structure
8652 **/
8653i40e_status i40e_set_npar_bw_setting(struct i40e_pf *pf)
8654{
8655 struct i40e_aqc_configure_partition_bw_data bw_data;
8656 i40e_status status;
8657
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00008658 /* Set the valid bit for this PF */
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04008659 bw_data.pf_valid_bits = cpu_to_le16(BIT(pf->hw.pf_id));
Greg Rosef4492db2015-02-06 08:52:12 +00008660 bw_data.max_bw[pf->hw.pf_id] = pf->npar_max_bw & I40E_ALT_BW_VALUE_MASK;
8661 bw_data.min_bw[pf->hw.pf_id] = pf->npar_min_bw & I40E_ALT_BW_VALUE_MASK;
8662
8663 /* Set the new bandwidths */
8664 status = i40e_aq_configure_partition_bw(&pf->hw, &bw_data, NULL);
8665
8666 return status;
8667}
8668
8669/**
8670 * i40e_commit_npar_bw_setting - Commit BW settings for this PF partition
8671 * @pf: board private structure
8672 **/
8673i40e_status i40e_commit_npar_bw_setting(struct i40e_pf *pf)
8674{
8675 /* Commit temporary BW setting to permanent NVM image */
8676 enum i40e_admin_queue_err last_aq_status;
8677 i40e_status ret;
8678 u16 nvm_word;
8679
8680 if (pf->hw.partition_id != 1) {
8681 dev_info(&pf->pdev->dev,
8682 "Commit BW only works on partition 1! This is partition %d",
8683 pf->hw.partition_id);
8684 ret = I40E_NOT_SUPPORTED;
8685 goto bw_commit_out;
8686 }
8687
8688 /* Acquire NVM for read access */
8689 ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_READ);
8690 last_aq_status = pf->hw.aq.asq_last_status;
8691 if (ret) {
8692 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008693 "Cannot acquire NVM for read access, err %s aq_err %s\n",
8694 i40e_stat_str(&pf->hw, ret),
8695 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +00008696 goto bw_commit_out;
8697 }
8698
8699 /* Read word 0x10 of NVM - SW compatibility word 1 */
8700 ret = i40e_aq_read_nvm(&pf->hw,
8701 I40E_SR_NVM_CONTROL_WORD,
8702 0x10, sizeof(nvm_word), &nvm_word,
8703 false, NULL);
8704 /* Save off last admin queue command status before releasing
8705 * the NVM
8706 */
8707 last_aq_status = pf->hw.aq.asq_last_status;
8708 i40e_release_nvm(&pf->hw);
8709 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008710 dev_info(&pf->pdev->dev, "NVM read error, err %s aq_err %s\n",
8711 i40e_stat_str(&pf->hw, ret),
8712 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +00008713 goto bw_commit_out;
8714 }
8715
8716 /* Wait a bit for NVM release to complete */
8717 msleep(50);
8718
8719 /* Acquire NVM for write access */
8720 ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_WRITE);
8721 last_aq_status = pf->hw.aq.asq_last_status;
8722 if (ret) {
8723 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008724 "Cannot acquire NVM for write access, err %s aq_err %s\n",
8725 i40e_stat_str(&pf->hw, ret),
8726 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +00008727 goto bw_commit_out;
8728 }
8729 /* Write it back out unchanged to initiate update NVM,
8730 * which will force a write of the shadow (alt) RAM to
8731 * the NVM - thus storing the bandwidth values permanently.
8732 */
8733 ret = i40e_aq_update_nvm(&pf->hw,
8734 I40E_SR_NVM_CONTROL_WORD,
8735 0x10, sizeof(nvm_word),
8736 &nvm_word, true, NULL);
8737 /* Save off last admin queue command status before releasing
8738 * the NVM
8739 */
8740 last_aq_status = pf->hw.aq.asq_last_status;
8741 i40e_release_nvm(&pf->hw);
8742 if (ret)
8743 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008744 "BW settings NOT SAVED, err %s aq_err %s\n",
8745 i40e_stat_str(&pf->hw, ret),
8746 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +00008747bw_commit_out:
8748
8749 return ret;
8750}
8751
8752/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008753 * i40e_sw_init - Initialize general software structures (struct i40e_pf)
8754 * @pf: board private structure to initialize
8755 *
8756 * i40e_sw_init initializes the Adapter private data structure.
8757 * Fields are initialized based on PCI device information and
8758 * OS network device settings (MTU size).
8759 **/
8760static int i40e_sw_init(struct i40e_pf *pf)
8761{
8762 int err = 0;
8763 int size;
8764
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008765 /* Set default capability flags */
8766 pf->flags = I40E_FLAG_RX_CSUM_ENABLED |
8767 I40E_FLAG_MSI_ENABLED |
Mitch Williams2bc7ee82015-02-06 08:52:11 +00008768 I40E_FLAG_MSIX_ENABLED;
8769
Mitch Williamsca99eb92014-04-04 04:43:07 +00008770 /* Set default ITR */
8771 pf->rx_itr_default = I40E_ITR_DYNAMIC | I40E_ITR_RX_DEF;
8772 pf->tx_itr_default = I40E_ITR_DYNAMIC | I40E_ITR_TX_DEF;
8773
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00008774 /* Depending on PF configurations, it is possible that the RSS
8775 * maximum might end up larger than the available queues
8776 */
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04008777 pf->rss_size_max = BIT(pf->hw.func_caps.rss_table_entry_width);
Helin Zhangacd65442015-10-26 19:44:28 -04008778 pf->alloc_rss_size = 1;
Anjali Singhai Jain5db4cb52015-02-24 06:58:49 +00008779 pf->rss_table_size = pf->hw.func_caps.rss_table_size;
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00008780 pf->rss_size_max = min_t(int, pf->rss_size_max,
8781 pf->hw.func_caps.num_tx_qp);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008782 if (pf->hw.func_caps.rss) {
8783 pf->flags |= I40E_FLAG_RSS_ENABLED;
Helin Zhangacd65442015-10-26 19:44:28 -04008784 pf->alloc_rss_size = min_t(int, pf->rss_size_max,
8785 num_online_cpus());
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008786 }
8787
Catherine Sullivan2050bc62013-12-18 13:46:03 +00008788 /* MFP mode enabled */
Pawel Orlowskic78b9532015-04-22 19:34:06 -04008789 if (pf->hw.func_caps.npar_enable || pf->hw.func_caps.flex10_enable) {
Catherine Sullivan2050bc62013-12-18 13:46:03 +00008790 pf->flags |= I40E_FLAG_MFP_ENABLED;
8791 dev_info(&pf->pdev->dev, "MFP mode Enabled\n");
Greg Rosef4492db2015-02-06 08:52:12 +00008792 if (i40e_get_npar_bw_setting(pf))
8793 dev_warn(&pf->pdev->dev,
8794 "Could not get NPAR bw settings\n");
8795 else
8796 dev_info(&pf->pdev->dev,
8797 "Min BW = %8.8x, Max BW = %8.8x\n",
8798 pf->npar_min_bw, pf->npar_max_bw);
Catherine Sullivan2050bc62013-12-18 13:46:03 +00008799 }
8800
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008801 /* FW/NVM is not yet fixed in this regard */
8802 if ((pf->hw.func_caps.fd_filters_guaranteed > 0) ||
8803 (pf->hw.func_caps.fd_filters_best_effort > 0)) {
8804 pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
8805 pf->atr_sample_rate = I40E_DEFAULT_ATR_SAMPLE_RATE;
Shannon Nelson6eae9c62015-09-03 17:18:55 -04008806 if (pf->flags & I40E_FLAG_MFP_ENABLED &&
8807 pf->hw.num_partitions > 1)
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008808 dev_info(&pf->pdev->dev,
Anjali Singhai Jain0b675842014-03-06 08:59:51 +00008809 "Flow Director Sideband mode Disabled in MFP mode\n");
Shannon Nelson6eae9c62015-09-03 17:18:55 -04008810 else
8811 pf->flags |= I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008812 pf->fdir_pf_filter_count =
8813 pf->hw.func_caps.fd_filters_guaranteed;
8814 pf->hw.fdir_shared_filter_count =
8815 pf->hw.func_caps.fd_filters_best_effort;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008816 }
8817
Bimmy Pujari6de432c2016-11-11 12:39:38 -08008818 if ((pf->hw.mac.type == I40E_MAC_XL710) &&
Anjali Singhai Jain8eed76f2015-12-09 15:50:31 -08008819 (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 33)) ||
Neerav Parikhf1bbad32016-01-13 16:51:39 -08008820 (pf->hw.aq.fw_maj_ver < 4))) {
Anjali Singhai Jain8eed76f2015-12-09 15:50:31 -08008821 pf->flags |= I40E_FLAG_RESTART_AUTONEG;
Neerav Parikhf1bbad32016-01-13 16:51:39 -08008822 /* No DCB support for FW < v4.33 */
8823 pf->flags |= I40E_FLAG_NO_DCB_SUPPORT;
8824 }
8825
8826 /* Disable FW LLDP if FW < v4.3 */
Bimmy Pujari6de432c2016-11-11 12:39:38 -08008827 if ((pf->hw.mac.type == I40E_MAC_XL710) &&
Neerav Parikhf1bbad32016-01-13 16:51:39 -08008828 (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 3)) ||
8829 (pf->hw.aq.fw_maj_ver < 4)))
8830 pf->flags |= I40E_FLAG_STOP_FW_LLDP;
8831
8832 /* Use the FW Set LLDP MIB API if FW > v4.40 */
Bimmy Pujari6de432c2016-11-11 12:39:38 -08008833 if ((pf->hw.mac.type == I40E_MAC_XL710) &&
Neerav Parikhf1bbad32016-01-13 16:51:39 -08008834 (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver >= 40)) ||
8835 (pf->hw.aq.fw_maj_ver >= 5)))
8836 pf->flags |= I40E_FLAG_USE_SET_LLDP_MIB;
Anjali Singhai Jain8eed76f2015-12-09 15:50:31 -08008837
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008838 if (pf->hw.func_caps.vmdq) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008839 pf->num_vmdq_vsis = I40E_DEFAULT_NUM_VMDQ_VSI;
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04008840 pf->flags |= I40E_FLAG_VMDQ_ENABLED;
Jesse Brandeburge9e53662015-10-02 17:57:21 -07008841 pf->num_vmdq_qps = i40e_default_queues_per_vmdq(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008842 }
8843
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06008844 if (pf->hw.func_caps.iwarp) {
8845 pf->flags |= I40E_FLAG_IWARP_ENABLED;
8846 /* IWARP needs one extra vector for CQP just like MISC.*/
8847 pf->num_iwarp_msix = (int)num_online_cpus() + 1;
8848 }
8849
Vasu Dev38e00432014-08-01 13:27:03 -07008850#ifdef I40E_FCOE
Shannon Nelson21364bc2015-08-26 15:14:13 -04008851 i40e_init_pf_fcoe(pf);
Vasu Dev38e00432014-08-01 13:27:03 -07008852
8853#endif /* I40E_FCOE */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008854#ifdef CONFIG_PCI_IOV
Shannon Nelsonba252f132014-12-11 07:06:34 +00008855 if (pf->hw.func_caps.num_vfs && pf->hw.partition_id == 1) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008856 pf->num_vf_qps = I40E_DEFAULT_QUEUES_PER_VF;
8857 pf->flags |= I40E_FLAG_SRIOV_ENABLED;
8858 pf->num_req_vfs = min_t(int,
8859 pf->hw.func_caps.num_vfs,
8860 I40E_MAX_VF_COUNT);
8861 }
8862#endif /* CONFIG_PCI_IOV */
Anjali Singhai Jaind502ce02015-06-05 12:20:26 -04008863 if (pf->hw.mac.type == I40E_MAC_X722) {
Joshua Hay1d680052016-12-12 15:44:08 -08008864 pf->flags |= I40E_FLAG_RSS_AQ_CAPABLE
8865 | I40E_FLAG_128_QP_RSS_CAPABLE
8866 | I40E_FLAG_HW_ATR_EVICT_CAPABLE
8867 | I40E_FLAG_OUTER_UDP_CSUM_CAPABLE
8868 | I40E_FLAG_WB_ON_ITR_CAPABLE
8869 | I40E_FLAG_MULTIPLE_TCP_UDP_RSS_PCTYPE
8870 | I40E_FLAG_NO_PCI_LINK_CHECK
8871 | I40E_FLAG_USE_SET_LLDP_MIB
8872 | I40E_FLAG_GENEVE_OFFLOAD_CAPABLE
8873 | I40E_FLAG_PTP_L4_CAPABLE
8874 | I40E_FLAG_WOL_MC_MAGIC_PKT_WAKE;
Anjali Singhaia340c782016-01-06 11:49:28 -08008875 } else if ((pf->hw.aq.api_maj_ver > 1) ||
8876 ((pf->hw.aq.api_maj_ver == 1) &&
8877 (pf->hw.aq.api_min_ver > 4))) {
8878 /* Supported in FW API version higher than 1.4 */
8879 pf->flags |= I40E_FLAG_GENEVE_OFFLOAD_CAPABLE;
Anjali Singhai Jain72b74862016-01-08 17:50:21 -08008880 pf->auto_disable_flags = I40E_FLAG_HW_ATR_EVICT_CAPABLE;
8881 } else {
8882 pf->auto_disable_flags = I40E_FLAG_HW_ATR_EVICT_CAPABLE;
Anjali Singhai Jaind502ce02015-06-05 12:20:26 -04008883 }
Anjali Singhaia340c782016-01-06 11:49:28 -08008884
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008885 pf->eeprom_version = 0xDEAD;
8886 pf->lan_veb = I40E_NO_VEB;
8887 pf->lan_vsi = I40E_NO_VSI;
8888
Anjali Singhai Jaind1a8d272015-07-23 16:54:40 -04008889 /* By default FW has this off for performance reasons */
8890 pf->flags &= ~I40E_FLAG_VEB_STATS_ENABLED;
8891
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008892 /* set up queue assignment tracking */
8893 size = sizeof(struct i40e_lump_tracking)
8894 + (sizeof(u16) * pf->hw.func_caps.num_tx_qp);
8895 pf->qp_pile = kzalloc(size, GFP_KERNEL);
8896 if (!pf->qp_pile) {
8897 err = -ENOMEM;
8898 goto sw_init_done;
8899 }
8900 pf->qp_pile->num_entries = pf->hw.func_caps.num_tx_qp;
8901 pf->qp_pile->search_hint = 0;
8902
Anjali Singhai Jain327fe042014-06-04 01:23:26 +00008903 pf->tx_timeout_recovery_level = 1;
8904
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008905 mutex_init(&pf->switch_mutex);
8906
Greg Rosec668a122015-02-26 16:10:39 +00008907 /* If NPAR is enabled nudge the Tx scheduler */
8908 if (pf->hw.func_caps.npar_enable && (!i40e_get_npar_bw_setting(pf)))
8909 i40e_set_npar_bw_setting(pf);
8910
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008911sw_init_done:
8912 return err;
8913}
8914
8915/**
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008916 * i40e_set_ntuple - set the ntuple feature flag and take action
8917 * @pf: board private structure to initialize
8918 * @features: the feature set that the stack is suggesting
8919 *
8920 * returns a bool to indicate if reset needs to happen
8921 **/
8922bool i40e_set_ntuple(struct i40e_pf *pf, netdev_features_t features)
8923{
8924 bool need_reset = false;
8925
8926 /* Check if Flow Director n-tuple support was enabled or disabled. If
8927 * the state changed, we need to reset.
8928 */
8929 if (features & NETIF_F_NTUPLE) {
8930 /* Enable filters and mark for reset */
8931 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
8932 need_reset = true;
Tushar Davea70e4072016-05-16 12:40:53 -07008933 /* enable FD_SB only if there is MSI-X vector */
8934 if (pf->num_fdsb_msix > 0)
8935 pf->flags |= I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008936 } else {
8937 /* turn off filters, mark for reset and clear SW filter list */
8938 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
8939 need_reset = true;
8940 i40e_fdir_filter_exit(pf);
8941 }
8942 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jain8a4f34f2014-06-04 08:45:20 +00008943 pf->auto_disable_flags &= ~I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00008944 /* reset fd counters */
8945 pf->fd_add_err = pf->fd_atr_cnt = pf->fd_tcp_rule = 0;
8946 pf->fdir_pf_active_filters = 0;
Anjali Singhai Jain8a4f34f2014-06-04 08:45:20 +00008947 /* if ATR was auto disabled it can be re-enabled. */
8948 if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
Jacob Keller234dc4e2016-09-06 18:05:09 -07008949 (pf->auto_disable_flags & I40E_FLAG_FD_ATR_ENABLED)) {
Anjali Singhai Jain8a4f34f2014-06-04 08:45:20 +00008950 pf->auto_disable_flags &= ~I40E_FLAG_FD_ATR_ENABLED;
Jacob Keller234dc4e2016-09-06 18:05:09 -07008951 if (I40E_DEBUG_FD & pf->hw.debug_mask)
8952 dev_info(&pf->pdev->dev, "ATR re-enabled.\n");
8953 }
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008954 }
8955 return need_reset;
8956}
8957
8958/**
Alan Bradyd8ec9862016-07-27 12:02:38 -07008959 * i40e_clear_rss_lut - clear the rx hash lookup table
8960 * @vsi: the VSI being configured
8961 **/
8962static void i40e_clear_rss_lut(struct i40e_vsi *vsi)
8963{
8964 struct i40e_pf *pf = vsi->back;
8965 struct i40e_hw *hw = &pf->hw;
8966 u16 vf_id = vsi->vf_id;
8967 u8 i;
8968
8969 if (vsi->type == I40E_VSI_MAIN) {
8970 for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
8971 wr32(hw, I40E_PFQF_HLUT(i), 0);
8972 } else if (vsi->type == I40E_VSI_SRIOV) {
8973 for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
8974 i40e_write_rx_ctl(hw, I40E_VFQF_HLUT1(i, vf_id), 0);
8975 } else {
8976 dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
8977 }
8978}
8979
8980/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008981 * i40e_set_features - set the netdev feature flags
8982 * @netdev: ptr to the netdev being adjusted
8983 * @features: the feature set that the stack is suggesting
8984 **/
8985static int i40e_set_features(struct net_device *netdev,
8986 netdev_features_t features)
8987{
8988 struct i40e_netdev_priv *np = netdev_priv(netdev);
8989 struct i40e_vsi *vsi = np->vsi;
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008990 struct i40e_pf *pf = vsi->back;
8991 bool need_reset;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008992
Alan Bradyd8ec9862016-07-27 12:02:38 -07008993 if (features & NETIF_F_RXHASH && !(netdev->features & NETIF_F_RXHASH))
8994 i40e_pf_config_rss(pf);
8995 else if (!(features & NETIF_F_RXHASH) &&
8996 netdev->features & NETIF_F_RXHASH)
8997 i40e_clear_rss_lut(vsi);
8998
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008999 if (features & NETIF_F_HW_VLAN_CTAG_RX)
9000 i40e_vlan_stripping_enable(vsi);
9001 else
9002 i40e_vlan_stripping_disable(vsi);
9003
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00009004 need_reset = i40e_set_ntuple(pf, features);
9005
9006 if (need_reset)
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04009007 i40e_do_reset(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED));
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00009008
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009009 return 0;
9010}
9011
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009012/**
Singhai, Anjali6a899022015-12-14 12:21:18 -08009013 * i40e_get_udp_port_idx - Lookup a possibly offloaded for Rx UDP port
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009014 * @pf: board private structure
9015 * @port: The UDP port to look up
9016 *
9017 * Returns the index number or I40E_MAX_PF_UDP_OFFLOAD_PORTS if port not found
9018 **/
Singhai, Anjali6a899022015-12-14 12:21:18 -08009019static u8 i40e_get_udp_port_idx(struct i40e_pf *pf, __be16 port)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009020{
9021 u8 i;
9022
9023 for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
Singhai, Anjali6a899022015-12-14 12:21:18 -08009024 if (pf->udp_ports[i].index == port)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009025 return i;
9026 }
9027
9028 return i;
9029}
9030
9031/**
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009032 * i40e_udp_tunnel_add - Get notifications about UDP tunnel ports that come up
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009033 * @netdev: This physical port's netdev
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009034 * @ti: Tunnel endpoint information
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009035 **/
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009036static void i40e_udp_tunnel_add(struct net_device *netdev,
9037 struct udp_tunnel_info *ti)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009038{
9039 struct i40e_netdev_priv *np = netdev_priv(netdev);
9040 struct i40e_vsi *vsi = np->vsi;
9041 struct i40e_pf *pf = vsi->back;
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009042 __be16 port = ti->port;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009043 u8 next_idx;
9044 u8 idx;
9045
Singhai, Anjali6a899022015-12-14 12:21:18 -08009046 idx = i40e_get_udp_port_idx(pf, port);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009047
9048 /* Check if port already exists */
9049 if (idx < I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009050 netdev_info(netdev, "port %d already offloaded\n",
Shannon Nelsonc22c06c2015-03-31 00:45:04 -07009051 ntohs(port));
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009052 return;
9053 }
9054
9055 /* Now check if there is space to add the new port */
Singhai, Anjali6a899022015-12-14 12:21:18 -08009056 next_idx = i40e_get_udp_port_idx(pf, 0);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009057
9058 if (next_idx == I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009059 netdev_info(netdev, "maximum number of offloaded UDP ports reached, not adding port %d\n",
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009060 ntohs(port));
9061 return;
9062 }
9063
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009064 switch (ti->type) {
9065 case UDP_TUNNEL_TYPE_VXLAN:
9066 pf->udp_ports[next_idx].type = I40E_AQC_TUNNEL_TYPE_VXLAN;
9067 break;
9068 case UDP_TUNNEL_TYPE_GENEVE:
9069 if (!(pf->flags & I40E_FLAG_GENEVE_OFFLOAD_CAPABLE))
9070 return;
9071 pf->udp_ports[next_idx].type = I40E_AQC_TUNNEL_TYPE_NGE;
9072 break;
9073 default:
9074 return;
9075 }
9076
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009077 /* New port: add it and mark its index in the bitmap */
Singhai, Anjali6a899022015-12-14 12:21:18 -08009078 pf->udp_ports[next_idx].index = port;
Singhai, Anjali6a899022015-12-14 12:21:18 -08009079 pf->pending_udp_bitmap |= BIT_ULL(next_idx);
9080 pf->flags |= I40E_FLAG_UDP_FILTER_SYNC;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009081}
9082
9083/**
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009084 * i40e_udp_tunnel_del - Get notifications about UDP tunnel ports that go away
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009085 * @netdev: This physical port's netdev
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009086 * @ti: Tunnel endpoint information
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009087 **/
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009088static void i40e_udp_tunnel_del(struct net_device *netdev,
9089 struct udp_tunnel_info *ti)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009090{
9091 struct i40e_netdev_priv *np = netdev_priv(netdev);
9092 struct i40e_vsi *vsi = np->vsi;
9093 struct i40e_pf *pf = vsi->back;
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009094 __be16 port = ti->port;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009095 u8 idx;
9096
Singhai, Anjali6a899022015-12-14 12:21:18 -08009097 idx = i40e_get_udp_port_idx(pf, port);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009098
9099 /* Check if port already exists */
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009100 if (idx >= I40E_MAX_PF_UDP_OFFLOAD_PORTS)
9101 goto not_found;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009102
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009103 switch (ti->type) {
9104 case UDP_TUNNEL_TYPE_VXLAN:
9105 if (pf->udp_ports[idx].type != I40E_AQC_TUNNEL_TYPE_VXLAN)
9106 goto not_found;
9107 break;
9108 case UDP_TUNNEL_TYPE_GENEVE:
9109 if (pf->udp_ports[idx].type != I40E_AQC_TUNNEL_TYPE_NGE)
9110 goto not_found;
9111 break;
9112 default:
9113 goto not_found;
Singhai, Anjali6a899022015-12-14 12:21:18 -08009114 }
9115
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009116 /* if port exists, set it to 0 (mark for deletion)
9117 * and make it pending
9118 */
9119 pf->udp_ports[idx].index = 0;
9120 pf->pending_udp_bitmap |= BIT_ULL(idx);
Singhai, Anjali6a899022015-12-14 12:21:18 -08009121 pf->flags |= I40E_FLAG_UDP_FILTER_SYNC;
9122
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009123 return;
9124not_found:
9125 netdev_warn(netdev, "UDP port %d was not found, not deleting\n",
9126 ntohs(port));
Singhai, Anjali6a899022015-12-14 12:21:18 -08009127}
9128
Neerav Parikh1f224ad2014-02-12 01:45:31 +00009129static int i40e_get_phys_port_id(struct net_device *netdev,
Jiri Pirko02637fc2014-11-28 14:34:16 +01009130 struct netdev_phys_item_id *ppid)
Neerav Parikh1f224ad2014-02-12 01:45:31 +00009131{
9132 struct i40e_netdev_priv *np = netdev_priv(netdev);
9133 struct i40e_pf *pf = np->vsi->back;
9134 struct i40e_hw *hw = &pf->hw;
9135
9136 if (!(pf->flags & I40E_FLAG_PORT_ID_VALID))
9137 return -EOPNOTSUPP;
9138
9139 ppid->id_len = min_t(int, sizeof(hw->mac.port_addr), sizeof(ppid->id));
9140 memcpy(ppid->id, hw->mac.port_addr, ppid->id_len);
9141
9142 return 0;
9143}
9144
Jesse Brandeburg2f90ade2014-11-20 16:30:02 -08009145/**
9146 * i40e_ndo_fdb_add - add an entry to the hardware database
9147 * @ndm: the input from the stack
9148 * @tb: pointer to array of nladdr (unused)
9149 * @dev: the net device pointer
9150 * @addr: the MAC address entry being added
9151 * @flags: instructions from stack about fdb operation
9152 */
Greg Rose4ba0dea2014-03-06 08:59:55 +00009153static int i40e_ndo_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
9154 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +01009155 const unsigned char *addr, u16 vid,
Greg Rose4ba0dea2014-03-06 08:59:55 +00009156 u16 flags)
Greg Rose4ba0dea2014-03-06 08:59:55 +00009157{
9158 struct i40e_netdev_priv *np = netdev_priv(dev);
9159 struct i40e_pf *pf = np->vsi->back;
9160 int err = 0;
9161
9162 if (!(pf->flags & I40E_FLAG_SRIOV_ENABLED))
9163 return -EOPNOTSUPP;
9164
Or Gerlitz65891fe2014-12-14 18:19:05 +02009165 if (vid) {
9166 pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
9167 return -EINVAL;
9168 }
9169
Greg Rose4ba0dea2014-03-06 08:59:55 +00009170 /* Hardware does not support aging addresses so if a
9171 * ndm_state is given only allow permanent addresses
9172 */
9173 if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
9174 netdev_info(dev, "FDB only supports static addresses\n");
9175 return -EINVAL;
9176 }
9177
9178 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
9179 err = dev_uc_add_excl(dev, addr);
9180 else if (is_multicast_ether_addr(addr))
9181 err = dev_mc_add_excl(dev, addr);
9182 else
9183 err = -EINVAL;
9184
9185 /* Only return duplicate errors if NLM_F_EXCL is set */
9186 if (err == -EEXIST && !(flags & NLM_F_EXCL))
9187 err = 0;
9188
9189 return err;
9190}
9191
Neerav Parikh51616012015-02-06 08:52:14 +00009192/**
9193 * i40e_ndo_bridge_setlink - Set the hardware bridge mode
9194 * @dev: the netdev being configured
9195 * @nlh: RTNL message
9196 *
9197 * Inserts a new hardware bridge if not already created and
9198 * enables the bridging mode requested (VEB or VEPA). If the
9199 * hardware bridge has already been inserted and the request
9200 * is to change the mode then that requires a PF reset to
9201 * allow rebuild of the components with required hardware
9202 * bridge mode enabled.
9203 **/
9204static int i40e_ndo_bridge_setlink(struct net_device *dev,
Carolyn Wyborny9df70b62015-04-27 14:57:11 -04009205 struct nlmsghdr *nlh,
9206 u16 flags)
Neerav Parikh51616012015-02-06 08:52:14 +00009207{
9208 struct i40e_netdev_priv *np = netdev_priv(dev);
9209 struct i40e_vsi *vsi = np->vsi;
9210 struct i40e_pf *pf = vsi->back;
9211 struct i40e_veb *veb = NULL;
9212 struct nlattr *attr, *br_spec;
9213 int i, rem;
9214
9215 /* Only for PF VSI for now */
9216 if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
9217 return -EOPNOTSUPP;
9218
9219 /* Find the HW bridge for PF VSI */
9220 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
9221 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
9222 veb = pf->veb[i];
9223 }
9224
9225 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
9226
9227 nla_for_each_nested(attr, br_spec, rem) {
9228 __u16 mode;
9229
9230 if (nla_type(attr) != IFLA_BRIDGE_MODE)
9231 continue;
9232
9233 mode = nla_get_u16(attr);
9234 if ((mode != BRIDGE_MODE_VEPA) &&
9235 (mode != BRIDGE_MODE_VEB))
9236 return -EINVAL;
9237
9238 /* Insert a new HW bridge */
9239 if (!veb) {
9240 veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
9241 vsi->tc_config.enabled_tc);
9242 if (veb) {
9243 veb->bridge_mode = mode;
9244 i40e_config_bridge_mode(veb);
9245 } else {
9246 /* No Bridge HW offload available */
9247 return -ENOENT;
9248 }
9249 break;
9250 } else if (mode != veb->bridge_mode) {
9251 /* Existing HW bridge but different mode needs reset */
9252 veb->bridge_mode = mode;
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07009253 /* TODO: If no VFs or VMDq VSIs, disallow VEB mode */
9254 if (mode == BRIDGE_MODE_VEB)
9255 pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
9256 else
9257 pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
9258 i40e_do_reset(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED));
Neerav Parikh51616012015-02-06 08:52:14 +00009259 break;
9260 }
9261 }
9262
9263 return 0;
9264}
9265
9266/**
9267 * i40e_ndo_bridge_getlink - Get the hardware bridge mode
9268 * @skb: skb buff
9269 * @pid: process id
9270 * @seq: RTNL message seq #
9271 * @dev: the netdev being configured
9272 * @filter_mask: unused
Jesse Brandeburgd4b2f9f2015-09-03 17:18:48 -04009273 * @nlflags: netlink flags passed in
Neerav Parikh51616012015-02-06 08:52:14 +00009274 *
9275 * Return the mode in which the hardware bridge is operating in
9276 * i.e VEB or VEPA.
9277 **/
Neerav Parikh51616012015-02-06 08:52:14 +00009278static int i40e_ndo_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
9279 struct net_device *dev,
Carolyn Wyborny9f4ffc42015-08-31 19:54:42 -04009280 u32 __always_unused filter_mask,
9281 int nlflags)
Neerav Parikh51616012015-02-06 08:52:14 +00009282{
9283 struct i40e_netdev_priv *np = netdev_priv(dev);
9284 struct i40e_vsi *vsi = np->vsi;
9285 struct i40e_pf *pf = vsi->back;
9286 struct i40e_veb *veb = NULL;
9287 int i;
9288
9289 /* Only for PF VSI for now */
9290 if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
9291 return -EOPNOTSUPP;
9292
9293 /* Find the HW bridge for the PF VSI */
9294 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
9295 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
9296 veb = pf->veb[i];
9297 }
9298
9299 if (!veb)
9300 return 0;
9301
Nicolas Dichtel46c264d2015-04-28 18:33:49 +02009302 return ndo_dflt_bridge_getlink(skb, pid, seq, dev, veb->bridge_mode,
Huaibin Wang599b0762016-09-26 09:51:18 +02009303 0, 0, nlflags, filter_mask, NULL);
Neerav Parikh51616012015-02-06 08:52:14 +00009304}
Neerav Parikh51616012015-02-06 08:52:14 +00009305
Joe Stringerf44a75e2015-04-14 17:09:14 -07009306/**
9307 * i40e_features_check - Validate encapsulated packet conforms to limits
9308 * @skb: skb buff
Jean Sacren2bc11c62015-09-19 05:08:43 -06009309 * @dev: This physical port's netdev
Joe Stringerf44a75e2015-04-14 17:09:14 -07009310 * @features: Offload features that the stack believes apply
9311 **/
9312static netdev_features_t i40e_features_check(struct sk_buff *skb,
9313 struct net_device *dev,
9314 netdev_features_t features)
9315{
Alexander Duyckf114dca2016-10-25 16:08:46 -07009316 size_t len;
9317
9318 /* No point in doing any of this if neither checksum nor GSO are
9319 * being requested for this frame. We can rule out both by just
9320 * checking for CHECKSUM_PARTIAL
9321 */
9322 if (skb->ip_summed != CHECKSUM_PARTIAL)
9323 return features;
9324
9325 /* We cannot support GSO if the MSS is going to be less than
9326 * 64 bytes. If it is then we need to drop support for GSO.
9327 */
9328 if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
9329 features &= ~NETIF_F_GSO_MASK;
9330
9331 /* MACLEN can support at most 63 words */
9332 len = skb_network_header(skb) - skb->data;
9333 if (len & ~(63 * 2))
9334 goto out_err;
9335
9336 /* IPLEN and EIPLEN can support at most 127 dwords */
9337 len = skb_transport_header(skb) - skb_network_header(skb);
9338 if (len & ~(127 * 4))
9339 goto out_err;
9340
9341 if (skb->encapsulation) {
9342 /* L4TUNLEN can support 127 words */
9343 len = skb_inner_network_header(skb) - skb_transport_header(skb);
9344 if (len & ~(127 * 2))
9345 goto out_err;
9346
9347 /* IPLEN can support at most 127 dwords */
9348 len = skb_inner_transport_header(skb) -
9349 skb_inner_network_header(skb);
9350 if (len & ~(127 * 4))
9351 goto out_err;
9352 }
9353
9354 /* No need to validate L4LEN as TCP is the only protocol with a
9355 * a flexible value and we support all possible values supported
9356 * by TCP, which is at most 15 dwords
9357 */
Joe Stringerf44a75e2015-04-14 17:09:14 -07009358
9359 return features;
Alexander Duyckf114dca2016-10-25 16:08:46 -07009360out_err:
9361 return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
Joe Stringerf44a75e2015-04-14 17:09:14 -07009362}
9363
Shannon Nelson37a29732015-02-27 09:15:19 +00009364static const struct net_device_ops i40e_netdev_ops = {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009365 .ndo_open = i40e_open,
9366 .ndo_stop = i40e_close,
9367 .ndo_start_xmit = i40e_lan_xmit_frame,
9368 .ndo_get_stats64 = i40e_get_netdev_stats_struct,
9369 .ndo_set_rx_mode = i40e_set_rx_mode,
9370 .ndo_validate_addr = eth_validate_addr,
9371 .ndo_set_mac_address = i40e_set_mac,
9372 .ndo_change_mtu = i40e_change_mtu,
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00009373 .ndo_do_ioctl = i40e_ioctl,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009374 .ndo_tx_timeout = i40e_tx_timeout,
9375 .ndo_vlan_rx_add_vid = i40e_vlan_rx_add_vid,
9376 .ndo_vlan_rx_kill_vid = i40e_vlan_rx_kill_vid,
9377#ifdef CONFIG_NET_POLL_CONTROLLER
9378 .ndo_poll_controller = i40e_netpoll,
9379#endif
John Fastabende4c67342016-02-16 21:16:15 -08009380 .ndo_setup_tc = __i40e_setup_tc,
Vasu Dev38e00432014-08-01 13:27:03 -07009381#ifdef I40E_FCOE
9382 .ndo_fcoe_enable = i40e_fcoe_enable,
9383 .ndo_fcoe_disable = i40e_fcoe_disable,
9384#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009385 .ndo_set_features = i40e_set_features,
9386 .ndo_set_vf_mac = i40e_ndo_set_vf_mac,
9387 .ndo_set_vf_vlan = i40e_ndo_set_vf_port_vlan,
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04009388 .ndo_set_vf_rate = i40e_ndo_set_vf_bw,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009389 .ndo_get_vf_config = i40e_ndo_get_vf_config,
Mitch Williams588aefa2014-02-11 08:27:49 +00009390 .ndo_set_vf_link_state = i40e_ndo_set_vf_link_state,
Serey Konge6d90042014-07-12 07:28:14 +00009391 .ndo_set_vf_spoofchk = i40e_ndo_set_vf_spoofchk,
Anjali Singhai Jainc3bbbd22016-04-01 03:56:07 -07009392 .ndo_set_vf_trust = i40e_ndo_set_vf_trust,
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009393 .ndo_udp_tunnel_add = i40e_udp_tunnel_add,
9394 .ndo_udp_tunnel_del = i40e_udp_tunnel_del,
Neerav Parikh1f224ad2014-02-12 01:45:31 +00009395 .ndo_get_phys_port_id = i40e_get_phys_port_id,
Greg Rose4ba0dea2014-03-06 08:59:55 +00009396 .ndo_fdb_add = i40e_ndo_fdb_add,
Joe Stringerf44a75e2015-04-14 17:09:14 -07009397 .ndo_features_check = i40e_features_check,
Neerav Parikh51616012015-02-06 08:52:14 +00009398 .ndo_bridge_getlink = i40e_ndo_bridge_getlink,
9399 .ndo_bridge_setlink = i40e_ndo_bridge_setlink,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009400};
9401
9402/**
9403 * i40e_config_netdev - Setup the netdev flags
9404 * @vsi: the VSI being configured
9405 *
9406 * Returns 0 on success, negative value on failure
9407 **/
9408static int i40e_config_netdev(struct i40e_vsi *vsi)
9409{
9410 struct i40e_pf *pf = vsi->back;
9411 struct i40e_hw *hw = &pf->hw;
9412 struct i40e_netdev_priv *np;
9413 struct net_device *netdev;
Jacob Keller435c0842016-11-08 13:05:10 -08009414 u8 broadcast[ETH_ALEN];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009415 u8 mac_addr[ETH_ALEN];
9416 int etherdev_size;
9417
9418 etherdev_size = sizeof(struct i40e_netdev_priv);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00009419 netdev = alloc_etherdev_mq(etherdev_size, vsi->alloc_queue_pairs);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009420 if (!netdev)
9421 return -ENOMEM;
9422
9423 vsi->netdev = netdev;
9424 np = netdev_priv(netdev);
9425 np->vsi = vsi;
9426
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009427 netdev->hw_enc_features |= NETIF_F_SG |
9428 NETIF_F_IP_CSUM |
9429 NETIF_F_IPV6_CSUM |
9430 NETIF_F_HIGHDMA |
9431 NETIF_F_SOFT_FEATURES |
9432 NETIF_F_TSO |
9433 NETIF_F_TSO_ECN |
9434 NETIF_F_TSO6 |
9435 NETIF_F_GSO_GRE |
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04009436 NETIF_F_GSO_GRE_CSUM |
Tom Herbert7e133182016-05-18 09:06:10 -07009437 NETIF_F_GSO_IPXIP4 |
Alexander Duyckbf2d1df2016-05-18 10:44:53 -07009438 NETIF_F_GSO_IPXIP6 |
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009439 NETIF_F_GSO_UDP_TUNNEL |
9440 NETIF_F_GSO_UDP_TUNNEL_CSUM |
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04009441 NETIF_F_GSO_PARTIAL |
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009442 NETIF_F_SCTP_CRC |
9443 NETIF_F_RXHASH |
9444 NETIF_F_RXCSUM |
Jesse Brandeburg5afdaaa2015-12-10 11:38:50 -08009445 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009446
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009447 if (!(pf->flags & I40E_FLAG_OUTER_UDP_CSUM_CAPABLE))
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04009448 netdev->gso_partial_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM;
9449
9450 netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009451
9452 /* record features VLANs can make use of */
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04009453 netdev->vlan_features |= netdev->hw_enc_features |
9454 NETIF_F_TSO_MANGLEID;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009455
Anjali Singhai Jain2e86a0b2014-04-01 07:11:53 +00009456 if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009457 netdev->hw_features |= NETIF_F_NTUPLE;
Anjali Singhai Jain2e86a0b2014-04-01 07:11:53 +00009458
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009459 netdev->hw_features |= netdev->hw_enc_features |
9460 NETIF_F_HW_VLAN_CTAG_TX |
9461 NETIF_F_HW_VLAN_CTAG_RX;
9462
9463 netdev->features |= netdev->hw_features | NETIF_F_HW_VLAN_CTAG_FILTER;
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04009464 netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009465
9466 if (vsi->type == I40E_VSI_MAIN) {
9467 SET_NETDEV_DEV(netdev, &pf->pdev->dev);
Greg Rose9a173902014-05-22 06:32:02 +00009468 ether_addr_copy(mac_addr, hw->mac.perm_addr);
Jacob Keller1596b5d2016-11-08 13:05:15 -08009469 /* The following steps are necessary to prevent reception
9470 * of tagged packets - some older NVM configurations load a
9471 * default a MAC-VLAN filter that accepts any tagged packet
9472 * which must be replaced by a normal filter.
9473 */
9474 i40e_rm_default_mac_filter(vsi, mac_addr);
Jacob Keller278e7d02016-10-05 09:30:37 -07009475 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller9569a9a2016-11-11 12:39:37 -08009476 i40e_add_mac_filter(vsi, mac_addr);
Jacob Keller278e7d02016-10-05 09:30:37 -07009477 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009478 } else {
9479 /* relate the VSI_VMDQ name to the VSI_MAIN name */
9480 snprintf(netdev->name, IFNAMSIZ, "%sv%%d",
9481 pf->vsi[pf->lan_vsi]->netdev->name);
9482 random_ether_addr(mac_addr);
Kiran Patil21659032015-09-30 14:09:03 -04009483
Jacob Keller278e7d02016-10-05 09:30:37 -07009484 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller9569a9a2016-11-11 12:39:37 -08009485 i40e_add_mac_filter(vsi, mac_addr);
Jacob Keller278e7d02016-10-05 09:30:37 -07009486 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009487 }
Kiran Patil21659032015-09-30 14:09:03 -04009488
Jacob Keller435c0842016-11-08 13:05:10 -08009489 /* Add the broadcast filter so that we initially will receive
9490 * broadcast packets. Note that when a new VLAN is first added the
9491 * driver will convert all filters marked I40E_VLAN_ANY into VLAN
9492 * specific filters as part of transitioning into "vlan" operation.
9493 * When more VLANs are added, the driver will copy each existing MAC
9494 * filter and add it for the new VLAN.
9495 *
9496 * Broadcast filters are handled specially by
9497 * i40e_sync_filters_subtask, as the driver must to set the broadcast
9498 * promiscuous bit instead of adding this directly as a MAC/VLAN
9499 * filter. The subtask will update the correct broadcast promiscuous
9500 * bits as VLANs become active or inactive.
9501 */
9502 eth_broadcast_addr(broadcast);
9503 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller9569a9a2016-11-11 12:39:37 -08009504 i40e_add_mac_filter(vsi, broadcast);
Jacob Keller435c0842016-11-08 13:05:10 -08009505 spin_unlock_bh(&vsi->mac_filter_hash_lock);
9506
Greg Rose9a173902014-05-22 06:32:02 +00009507 ether_addr_copy(netdev->dev_addr, mac_addr);
9508 ether_addr_copy(netdev->perm_addr, mac_addr);
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009509
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009510 netdev->priv_flags |= IFF_UNICAST_FLT;
9511 netdev->priv_flags |= IFF_SUPP_NOFCS;
9512 /* Setup netdev TC information */
9513 i40e_vsi_config_netdev_tc(vsi, vsi->tc_config.enabled_tc);
9514
9515 netdev->netdev_ops = &i40e_netdev_ops;
9516 netdev->watchdog_timeo = 5 * HZ;
9517 i40e_set_ethtool_ops(netdev);
Vasu Dev38e00432014-08-01 13:27:03 -07009518#ifdef I40E_FCOE
9519 i40e_fcoe_config_netdev(netdev, vsi);
9520#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009521
Jarod Wilson91c527a2016-10-17 15:54:05 -04009522 /* MTU range: 68 - 9706 */
9523 netdev->min_mtu = ETH_MIN_MTU;
9524 netdev->max_mtu = I40E_MAX_RXBUFFER -
9525 (ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN);
9526
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009527 return 0;
9528}
9529
9530/**
9531 * i40e_vsi_delete - Delete a VSI from the switch
9532 * @vsi: the VSI being removed
9533 *
9534 * Returns 0 on success, negative value on failure
9535 **/
9536static void i40e_vsi_delete(struct i40e_vsi *vsi)
9537{
9538 /* remove default VSI is not allowed */
9539 if (vsi == vsi->back->vsi[vsi->back->lan_vsi])
9540 return;
9541
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009542 i40e_aq_delete_element(&vsi->back->hw, vsi->seid, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009543}
9544
9545/**
Neerav Parikh51616012015-02-06 08:52:14 +00009546 * i40e_is_vsi_uplink_mode_veb - Check if the VSI's uplink bridge mode is VEB
9547 * @vsi: the VSI being queried
9548 *
9549 * Returns 1 if HW bridge mode is VEB and return 0 in case of VEPA mode
9550 **/
9551int i40e_is_vsi_uplink_mode_veb(struct i40e_vsi *vsi)
9552{
9553 struct i40e_veb *veb;
9554 struct i40e_pf *pf = vsi->back;
9555
9556 /* Uplink is not a bridge so default to VEB */
9557 if (vsi->veb_idx == I40E_NO_VEB)
9558 return 1;
9559
9560 veb = pf->veb[vsi->veb_idx];
Akeem G Abodunrin09603ea2015-10-01 14:37:36 -04009561 if (!veb) {
9562 dev_info(&pf->pdev->dev,
9563 "There is no veb associated with the bridge\n");
9564 return -ENOENT;
9565 }
Neerav Parikh51616012015-02-06 08:52:14 +00009566
Akeem G Abodunrin09603ea2015-10-01 14:37:36 -04009567 /* Uplink is a bridge in VEPA mode */
9568 if (veb->bridge_mode & BRIDGE_MODE_VEPA) {
9569 return 0;
9570 } else {
9571 /* Uplink is a bridge in VEB mode */
9572 return 1;
9573 }
9574
9575 /* VEPA is now default bridge, so return 0 */
9576 return 0;
Neerav Parikh51616012015-02-06 08:52:14 +00009577}
9578
9579/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009580 * i40e_add_vsi - Add a VSI to the switch
9581 * @vsi: the VSI being configured
9582 *
9583 * This initializes a VSI context depending on the VSI type to be added and
9584 * passes it down to the add_vsi aq command.
9585 **/
9586static int i40e_add_vsi(struct i40e_vsi *vsi)
9587{
9588 int ret = -ENODEV;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009589 struct i40e_pf *pf = vsi->back;
9590 struct i40e_hw *hw = &pf->hw;
9591 struct i40e_vsi_context ctxt;
Jacob Keller278e7d02016-10-05 09:30:37 -07009592 struct i40e_mac_filter *f;
9593 struct hlist_node *h;
9594 int bkt;
Kiran Patil21659032015-09-30 14:09:03 -04009595
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009596 u8 enabled_tc = 0x1; /* TC0 enabled */
9597 int f_count = 0;
9598
9599 memset(&ctxt, 0, sizeof(ctxt));
9600 switch (vsi->type) {
9601 case I40E_VSI_MAIN:
9602 /* The PF's main VSI is already setup as part of the
9603 * device initialization, so we'll not bother with
9604 * the add_vsi call, but we will retrieve the current
9605 * VSI context.
9606 */
9607 ctxt.seid = pf->main_vsi_seid;
9608 ctxt.pf_num = pf->hw.pf_id;
9609 ctxt.vf_num = 0;
9610 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
9611 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
9612 if (ret) {
9613 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009614 "couldn't get PF vsi config, err %s aq_err %s\n",
9615 i40e_stat_str(&pf->hw, ret),
9616 i40e_aq_str(&pf->hw,
9617 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009618 return -ENOENT;
9619 }
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07009620 vsi->info = ctxt.info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009621 vsi->info.valid_sections = 0;
9622
9623 vsi->seid = ctxt.seid;
9624 vsi->id = ctxt.vsi_number;
9625
9626 enabled_tc = i40e_pf_get_tc_map(pf);
9627
9628 /* MFP mode setup queue map and update VSI */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00009629 if ((pf->flags & I40E_FLAG_MFP_ENABLED) &&
9630 !(pf->hw.func_caps.iscsi)) { /* NIC type PF */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009631 memset(&ctxt, 0, sizeof(ctxt));
9632 ctxt.seid = pf->main_vsi_seid;
9633 ctxt.pf_num = pf->hw.pf_id;
9634 ctxt.vf_num = 0;
9635 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
9636 ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
9637 if (ret) {
9638 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009639 "update vsi failed, err %s aq_err %s\n",
9640 i40e_stat_str(&pf->hw, ret),
9641 i40e_aq_str(&pf->hw,
9642 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009643 ret = -ENOENT;
9644 goto err;
9645 }
9646 /* update the local VSI info queue map */
9647 i40e_vsi_update_queue_map(vsi, &ctxt);
9648 vsi->info.valid_sections = 0;
9649 } else {
9650 /* Default/Main VSI is only enabled for TC0
9651 * reconfigure it to enable all TCs that are
9652 * available on the port in SFP mode.
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00009653 * For MFP case the iSCSI PF would use this
9654 * flow to enable LAN+iSCSI TC.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009655 */
9656 ret = i40e_vsi_config_tc(vsi, enabled_tc);
9657 if (ret) {
9658 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009659 "failed to configure TCs for main VSI tc_map 0x%08x, err %s aq_err %s\n",
9660 enabled_tc,
9661 i40e_stat_str(&pf->hw, ret),
9662 i40e_aq_str(&pf->hw,
9663 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009664 ret = -ENOENT;
9665 }
9666 }
9667 break;
9668
9669 case I40E_VSI_FDIR:
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009670 ctxt.pf_num = hw->pf_id;
9671 ctxt.vf_num = 0;
9672 ctxt.uplink_seid = vsi->uplink_seid;
Neerav Parikh2b18e592015-01-24 09:58:38 +00009673 ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009674 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07009675 if ((pf->flags & I40E_FLAG_VEB_MODE_ENABLED) &&
9676 (i40e_is_vsi_uplink_mode_veb(vsi))) {
Neerav Parikh51616012015-02-06 08:52:14 +00009677 ctxt.info.valid_sections |=
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07009678 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
Neerav Parikh51616012015-02-06 08:52:14 +00009679 ctxt.info.switch_id =
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07009680 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
Neerav Parikh51616012015-02-06 08:52:14 +00009681 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009682 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009683 break;
9684
9685 case I40E_VSI_VMDQ2:
9686 ctxt.pf_num = hw->pf_id;
9687 ctxt.vf_num = 0;
9688 ctxt.uplink_seid = vsi->uplink_seid;
Neerav Parikh2b18e592015-01-24 09:58:38 +00009689 ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009690 ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
9691
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009692 /* This VSI is connected to VEB so the switch_id
9693 * should be set to zero by default.
9694 */
Neerav Parikh51616012015-02-06 08:52:14 +00009695 if (i40e_is_vsi_uplink_mode_veb(vsi)) {
9696 ctxt.info.valid_sections |=
9697 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
9698 ctxt.info.switch_id =
9699 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
9700 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009701
9702 /* Setup the VSI tx/rx queue map for TC0 only for now */
9703 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
9704 break;
9705
9706 case I40E_VSI_SRIOV:
9707 ctxt.pf_num = hw->pf_id;
9708 ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
9709 ctxt.uplink_seid = vsi->uplink_seid;
Neerav Parikh2b18e592015-01-24 09:58:38 +00009710 ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009711 ctxt.flags = I40E_AQ_VSI_TYPE_VF;
9712
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009713 /* This VSI is connected to VEB so the switch_id
9714 * should be set to zero by default.
9715 */
Neerav Parikh51616012015-02-06 08:52:14 +00009716 if (i40e_is_vsi_uplink_mode_veb(vsi)) {
9717 ctxt.info.valid_sections |=
9718 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
9719 ctxt.info.switch_id =
9720 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
9721 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009722
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06009723 if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
9724 ctxt.info.valid_sections |=
9725 cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
9726 ctxt.info.queueing_opt_flags |=
Ashish Shah4b28cdb2016-05-03 15:13:17 -07009727 (I40E_AQ_VSI_QUE_OPT_TCP_ENA |
9728 I40E_AQ_VSI_QUE_OPT_RSS_LUT_VSI);
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06009729 }
9730
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009731 ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
9732 ctxt.info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_MODE_ALL;
Mitch Williamsc674d122014-05-20 08:01:40 +00009733 if (pf->vf[vsi->vf_id].spoofchk) {
9734 ctxt.info.valid_sections |=
9735 cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
9736 ctxt.info.sec_flags |=
9737 (I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
9738 I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
9739 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009740 /* Setup the VSI tx/rx queue map for TC0 only for now */
9741 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
9742 break;
9743
Vasu Dev38e00432014-08-01 13:27:03 -07009744#ifdef I40E_FCOE
9745 case I40E_VSI_FCOE:
9746 ret = i40e_fcoe_vsi_init(vsi, &ctxt);
9747 if (ret) {
9748 dev_info(&pf->pdev->dev, "failed to initialize FCoE VSI\n");
9749 return ret;
9750 }
9751 break;
9752
9753#endif /* I40E_FCOE */
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06009754 case I40E_VSI_IWARP:
9755 /* send down message to iWARP */
9756 break;
9757
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009758 default:
9759 return -ENODEV;
9760 }
9761
9762 if (vsi->type != I40E_VSI_MAIN) {
9763 ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
9764 if (ret) {
9765 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009766 "add vsi failed, err %s aq_err %s\n",
9767 i40e_stat_str(&pf->hw, ret),
9768 i40e_aq_str(&pf->hw,
9769 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009770 ret = -ENOENT;
9771 goto err;
9772 }
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07009773 vsi->info = ctxt.info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009774 vsi->info.valid_sections = 0;
9775 vsi->seid = ctxt.seid;
9776 vsi->id = ctxt.vsi_number;
9777 }
9778
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07009779 vsi->active_filters = 0;
9780 clear_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state);
Jacob Keller278e7d02016-10-05 09:30:37 -07009781 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009782 /* If macvlan filters already exist, force them to get loaded */
Jacob Keller278e7d02016-10-05 09:30:37 -07009783 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07009784 f->state = I40E_FILTER_NEW;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009785 f_count++;
9786 }
Jacob Keller278e7d02016-10-05 09:30:37 -07009787 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -04009788
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009789 if (f_count) {
9790 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
9791 pf->flags |= I40E_FLAG_FILTER_SYNC;
9792 }
9793
9794 /* Update VSI BW information */
9795 ret = i40e_vsi_get_bw_info(vsi);
9796 if (ret) {
9797 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009798 "couldn't get vsi bw info, err %s aq_err %s\n",
9799 i40e_stat_str(&pf->hw, ret),
9800 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009801 /* VSI is already added so not tearing that up */
9802 ret = 0;
9803 }
9804
9805err:
9806 return ret;
9807}
9808
9809/**
9810 * i40e_vsi_release - Delete a VSI and free its resources
9811 * @vsi: the VSI being removed
9812 *
9813 * Returns 0 on success or < 0 on error
9814 **/
9815int i40e_vsi_release(struct i40e_vsi *vsi)
9816{
Jacob Keller278e7d02016-10-05 09:30:37 -07009817 struct i40e_mac_filter *f;
9818 struct hlist_node *h;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009819 struct i40e_veb *veb = NULL;
9820 struct i40e_pf *pf;
9821 u16 uplink_seid;
Jacob Keller278e7d02016-10-05 09:30:37 -07009822 int i, n, bkt;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009823
9824 pf = vsi->back;
9825
9826 /* release of a VEB-owner or last VSI is not allowed */
9827 if (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) {
9828 dev_info(&pf->pdev->dev, "VSI %d has existing VEB %d\n",
9829 vsi->seid, vsi->uplink_seid);
9830 return -ENODEV;
9831 }
9832 if (vsi == pf->vsi[pf->lan_vsi] &&
9833 !test_bit(__I40E_DOWN, &pf->state)) {
9834 dev_info(&pf->pdev->dev, "Can't remove PF VSI\n");
9835 return -ENODEV;
9836 }
9837
9838 uplink_seid = vsi->uplink_seid;
9839 if (vsi->type != I40E_VSI_SRIOV) {
9840 if (vsi->netdev_registered) {
9841 vsi->netdev_registered = false;
9842 if (vsi->netdev) {
9843 /* results in a call to i40e_close() */
9844 unregister_netdev(vsi->netdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009845 }
9846 } else {
Shannon Nelson90ef8d42014-03-14 07:32:26 +00009847 i40e_vsi_close(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009848 }
9849 i40e_vsi_disable_irq(vsi);
9850 }
9851
Jacob Keller278e7d02016-10-05 09:30:37 -07009852 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller6622f5c2016-10-05 09:30:32 -07009853
9854 /* clear the sync flag on all filters */
9855 if (vsi->netdev) {
9856 __dev_uc_unsync(vsi->netdev, NULL);
9857 __dev_mc_unsync(vsi->netdev, NULL);
9858 }
9859
9860 /* make sure any remaining filters are marked for deletion */
Jacob Keller278e7d02016-10-05 09:30:37 -07009861 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
Jacob Keller290d2552016-10-05 09:30:36 -07009862 __i40e_del_filter(vsi, f);
Jacob Keller6622f5c2016-10-05 09:30:32 -07009863
Jacob Keller278e7d02016-10-05 09:30:37 -07009864 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -04009865
Jesse Brandeburg17652c62015-11-05 17:01:02 -08009866 i40e_sync_vsi_filters(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009867
9868 i40e_vsi_delete(vsi);
9869 i40e_vsi_free_q_vectors(vsi);
Shannon Nelsona4866592014-02-11 08:24:07 +00009870 if (vsi->netdev) {
9871 free_netdev(vsi->netdev);
9872 vsi->netdev = NULL;
9873 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009874 i40e_vsi_clear_rings(vsi);
9875 i40e_vsi_clear(vsi);
9876
9877 /* If this was the last thing on the VEB, except for the
9878 * controlling VSI, remove the VEB, which puts the controlling
9879 * VSI onto the next level down in the switch.
9880 *
9881 * Well, okay, there's one more exception here: don't remove
9882 * the orphan VEBs yet. We'll wait for an explicit remove request
9883 * from up the network stack.
9884 */
Mitch Williams505682c2014-05-20 08:01:37 +00009885 for (n = 0, i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009886 if (pf->vsi[i] &&
9887 pf->vsi[i]->uplink_seid == uplink_seid &&
9888 (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
9889 n++; /* count the VSIs */
9890 }
9891 }
9892 for (i = 0; i < I40E_MAX_VEB; i++) {
9893 if (!pf->veb[i])
9894 continue;
9895 if (pf->veb[i]->uplink_seid == uplink_seid)
9896 n++; /* count the VEBs */
9897 if (pf->veb[i]->seid == uplink_seid)
9898 veb = pf->veb[i];
9899 }
9900 if (n == 0 && veb && veb->uplink_seid != 0)
9901 i40e_veb_release(veb);
9902
9903 return 0;
9904}
9905
9906/**
9907 * i40e_vsi_setup_vectors - Set up the q_vectors for the given VSI
9908 * @vsi: ptr to the VSI
9909 *
9910 * This should only be called after i40e_vsi_mem_alloc() which allocates the
9911 * corresponding SW VSI structure and initializes num_queue_pairs for the
9912 * newly allocated VSI.
9913 *
9914 * Returns 0 on success or negative on failure
9915 **/
9916static int i40e_vsi_setup_vectors(struct i40e_vsi *vsi)
9917{
9918 int ret = -ENOENT;
9919 struct i40e_pf *pf = vsi->back;
9920
Alexander Duyck493fb302013-09-28 07:01:44 +00009921 if (vsi->q_vectors[0]) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009922 dev_info(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
9923 vsi->seid);
9924 return -EEXIST;
9925 }
9926
9927 if (vsi->base_vector) {
Jesse Brandeburgf29eaa32014-02-11 08:24:12 +00009928 dev_info(&pf->pdev->dev, "VSI %d has non-zero base vector %d\n",
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009929 vsi->seid, vsi->base_vector);
9930 return -EEXIST;
9931 }
9932
Greg Rose90e04072014-03-06 08:59:57 +00009933 ret = i40e_vsi_alloc_q_vectors(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009934 if (ret) {
9935 dev_info(&pf->pdev->dev,
9936 "failed to allocate %d q_vector for VSI %d, ret=%d\n",
9937 vsi->num_q_vectors, vsi->seid, ret);
9938 vsi->num_q_vectors = 0;
9939 goto vector_setup_out;
9940 }
9941
Anjali Singhai Jain26cdc442015-07-10 19:36:00 -04009942 /* In Legacy mode, we do not have to get any other vector since we
9943 * piggyback on the misc/ICR0 for queue interrupts.
9944 */
9945 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
9946 return ret;
Shannon Nelson958a3e32013-09-28 07:13:28 +00009947 if (vsi->num_q_vectors)
9948 vsi->base_vector = i40e_get_lump(pf, pf->irq_pile,
9949 vsi->num_q_vectors, vsi->idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009950 if (vsi->base_vector < 0) {
9951 dev_info(&pf->pdev->dev,
Shannon Nelson049a2be2014-10-17 03:14:50 +00009952 "failed to get tracking for %d vectors for VSI %d, err=%d\n",
9953 vsi->num_q_vectors, vsi->seid, vsi->base_vector);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009954 i40e_vsi_free_q_vectors(vsi);
9955 ret = -ENOENT;
9956 goto vector_setup_out;
9957 }
9958
9959vector_setup_out:
9960 return ret;
9961}
9962
9963/**
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00009964 * i40e_vsi_reinit_setup - return and reallocate resources for a VSI
9965 * @vsi: pointer to the vsi.
9966 *
9967 * This re-allocates a vsi's queue resources.
9968 *
9969 * Returns pointer to the successfully allocated and configured VSI sw struct
9970 * on success, otherwise returns NULL on failure.
9971 **/
9972static struct i40e_vsi *i40e_vsi_reinit_setup(struct i40e_vsi *vsi)
9973{
John Underwoodf5340392016-02-18 09:19:24 -08009974 struct i40e_pf *pf;
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00009975 u8 enabled_tc;
9976 int ret;
9977
John Underwoodf5340392016-02-18 09:19:24 -08009978 if (!vsi)
9979 return NULL;
9980
9981 pf = vsi->back;
9982
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00009983 i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
9984 i40e_vsi_clear_rings(vsi);
9985
9986 i40e_vsi_free_arrays(vsi, false);
9987 i40e_set_num_rings_in_vsi(vsi);
9988 ret = i40e_vsi_alloc_arrays(vsi, false);
9989 if (ret)
9990 goto err_vsi;
9991
9992 ret = i40e_get_lump(pf, pf->qp_pile, vsi->alloc_queue_pairs, vsi->idx);
9993 if (ret < 0) {
Shannon Nelson049a2be2014-10-17 03:14:50 +00009994 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009995 "failed to get tracking for %d queues for VSI %d err %d\n",
Shannon Nelson049a2be2014-10-17 03:14:50 +00009996 vsi->alloc_queue_pairs, vsi->seid, ret);
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00009997 goto err_vsi;
9998 }
9999 vsi->base_queue = ret;
10000
10001 /* Update the FW view of the VSI. Force a reset of TC and queue
10002 * layout configurations.
10003 */
10004 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
10005 pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
10006 pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
10007 i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
Jacob Keller1596b5d2016-11-08 13:05:15 -080010008 if (vsi->type == I40E_VSI_MAIN)
10009 i40e_rm_default_mac_filter(vsi, pf->hw.mac.perm_addr);
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010010
10011 /* assign it some queues */
10012 ret = i40e_alloc_rings(vsi);
10013 if (ret)
10014 goto err_rings;
10015
10016 /* map all of the rings to the q_vectors */
10017 i40e_vsi_map_rings_to_vectors(vsi);
10018 return vsi;
10019
10020err_rings:
10021 i40e_vsi_free_q_vectors(vsi);
10022 if (vsi->netdev_registered) {
10023 vsi->netdev_registered = false;
10024 unregister_netdev(vsi->netdev);
10025 free_netdev(vsi->netdev);
10026 vsi->netdev = NULL;
10027 }
10028 i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
10029err_vsi:
10030 i40e_vsi_clear(vsi);
10031 return NULL;
10032}
10033
10034/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010035 * i40e_vsi_setup - Set up a VSI by a given type
10036 * @pf: board private structure
10037 * @type: VSI type
10038 * @uplink_seid: the switch element to link to
10039 * @param1: usage depends upon VSI type. For VF types, indicates VF id
10040 *
10041 * This allocates the sw VSI structure and its queue resources, then add a VSI
10042 * to the identified VEB.
10043 *
10044 * Returns pointer to the successfully allocated and configure VSI sw struct on
10045 * success, otherwise returns NULL on failure.
10046 **/
10047struct i40e_vsi *i40e_vsi_setup(struct i40e_pf *pf, u8 type,
10048 u16 uplink_seid, u32 param1)
10049{
10050 struct i40e_vsi *vsi = NULL;
10051 struct i40e_veb *veb = NULL;
10052 int ret, i;
10053 int v_idx;
10054
10055 /* The requested uplink_seid must be either
10056 * - the PF's port seid
10057 * no VEB is needed because this is the PF
10058 * or this is a Flow Director special case VSI
10059 * - seid of an existing VEB
10060 * - seid of a VSI that owns an existing VEB
10061 * - seid of a VSI that doesn't own a VEB
10062 * a new VEB is created and the VSI becomes the owner
10063 * - seid of the PF VSI, which is what creates the first VEB
10064 * this is a special case of the previous
10065 *
10066 * Find which uplink_seid we were given and create a new VEB if needed
10067 */
10068 for (i = 0; i < I40E_MAX_VEB; i++) {
10069 if (pf->veb[i] && pf->veb[i]->seid == uplink_seid) {
10070 veb = pf->veb[i];
10071 break;
10072 }
10073 }
10074
10075 if (!veb && uplink_seid != pf->mac_seid) {
10076
Mitch Williams505682c2014-05-20 08:01:37 +000010077 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010078 if (pf->vsi[i] && pf->vsi[i]->seid == uplink_seid) {
10079 vsi = pf->vsi[i];
10080 break;
10081 }
10082 }
10083 if (!vsi) {
10084 dev_info(&pf->pdev->dev, "no such uplink_seid %d\n",
10085 uplink_seid);
10086 return NULL;
10087 }
10088
10089 if (vsi->uplink_seid == pf->mac_seid)
10090 veb = i40e_veb_setup(pf, 0, pf->mac_seid, vsi->seid,
10091 vsi->tc_config.enabled_tc);
10092 else if ((vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
10093 veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
10094 vsi->tc_config.enabled_tc);
Anjali Singhai Jain79c21a82014-11-13 03:06:14 +000010095 if (veb) {
10096 if (vsi->seid != pf->vsi[pf->lan_vsi]->seid) {
10097 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -040010098 "New VSI creation error, uplink seid of LAN VSI expected.\n");
Anjali Singhai Jain79c21a82014-11-13 03:06:14 +000010099 return NULL;
10100 }
Anjali Singhai Jainfa11cb32015-05-27 12:06:14 -040010101 /* We come up by default in VEPA mode if SRIOV is not
10102 * already enabled, in which case we can't force VEPA
10103 * mode.
10104 */
10105 if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
10106 veb->bridge_mode = BRIDGE_MODE_VEPA;
10107 pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
10108 }
Neerav Parikh51616012015-02-06 08:52:14 +000010109 i40e_config_bridge_mode(veb);
Anjali Singhai Jain79c21a82014-11-13 03:06:14 +000010110 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010111 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
10112 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
10113 veb = pf->veb[i];
10114 }
10115 if (!veb) {
10116 dev_info(&pf->pdev->dev, "couldn't add VEB\n");
10117 return NULL;
10118 }
10119
10120 vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
10121 uplink_seid = veb->seid;
10122 }
10123
10124 /* get vsi sw struct */
10125 v_idx = i40e_vsi_mem_alloc(pf, type);
10126 if (v_idx < 0)
10127 goto err_alloc;
10128 vsi = pf->vsi[v_idx];
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010129 if (!vsi)
10130 goto err_alloc;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010131 vsi->type = type;
10132 vsi->veb_idx = (veb ? veb->idx : I40E_NO_VEB);
10133
10134 if (type == I40E_VSI_MAIN)
10135 pf->lan_vsi = v_idx;
10136 else if (type == I40E_VSI_SRIOV)
10137 vsi->vf_id = param1;
10138 /* assign it some queues */
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010139 ret = i40e_get_lump(pf, pf->qp_pile, vsi->alloc_queue_pairs,
10140 vsi->idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010141 if (ret < 0) {
Shannon Nelson049a2be2014-10-17 03:14:50 +000010142 dev_info(&pf->pdev->dev,
10143 "failed to get tracking for %d queues for VSI %d err=%d\n",
10144 vsi->alloc_queue_pairs, vsi->seid, ret);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010145 goto err_vsi;
10146 }
10147 vsi->base_queue = ret;
10148
10149 /* get a VSI from the hardware */
10150 vsi->uplink_seid = uplink_seid;
10151 ret = i40e_add_vsi(vsi);
10152 if (ret)
10153 goto err_vsi;
10154
10155 switch (vsi->type) {
10156 /* setup the netdev if needed */
10157 case I40E_VSI_MAIN:
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050010158 /* Apply relevant filters if a platform-specific mac
10159 * address was selected.
10160 */
10161 if (!!(pf->flags & I40E_FLAG_PF_MAC)) {
10162 ret = i40e_macaddr_init(vsi, pf->hw.mac.addr);
10163 if (ret) {
10164 dev_warn(&pf->pdev->dev,
10165 "could not set up macaddr; err %d\n",
10166 ret);
10167 }
10168 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010169 case I40E_VSI_VMDQ2:
Vasu Dev38e00432014-08-01 13:27:03 -070010170 case I40E_VSI_FCOE:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010171 ret = i40e_config_netdev(vsi);
10172 if (ret)
10173 goto err_netdev;
10174 ret = register_netdev(vsi->netdev);
10175 if (ret)
10176 goto err_netdev;
10177 vsi->netdev_registered = true;
10178 netif_carrier_off(vsi->netdev);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -080010179#ifdef CONFIG_I40E_DCB
10180 /* Setup DCB netlink interface */
10181 i40e_dcbnl_setup(vsi);
10182#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010183 /* fall through */
10184
10185 case I40E_VSI_FDIR:
10186 /* set up vectors and rings if needed */
10187 ret = i40e_vsi_setup_vectors(vsi);
10188 if (ret)
10189 goto err_msix;
10190
10191 ret = i40e_alloc_rings(vsi);
10192 if (ret)
10193 goto err_rings;
10194
10195 /* map all of the rings to the q_vectors */
10196 i40e_vsi_map_rings_to_vectors(vsi);
10197
10198 i40e_vsi_reset_stats(vsi);
10199 break;
10200
10201 default:
10202 /* no netdev or rings for the other VSI types */
10203 break;
10204 }
10205
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -040010206 if ((pf->flags & I40E_FLAG_RSS_AQ_CAPABLE) &&
10207 (vsi->type == I40E_VSI_VMDQ2)) {
10208 ret = i40e_vsi_config_rss(vsi);
10209 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010210 return vsi;
10211
10212err_rings:
10213 i40e_vsi_free_q_vectors(vsi);
10214err_msix:
10215 if (vsi->netdev_registered) {
10216 vsi->netdev_registered = false;
10217 unregister_netdev(vsi->netdev);
10218 free_netdev(vsi->netdev);
10219 vsi->netdev = NULL;
10220 }
10221err_netdev:
10222 i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
10223err_vsi:
10224 i40e_vsi_clear(vsi);
10225err_alloc:
10226 return NULL;
10227}
10228
10229/**
10230 * i40e_veb_get_bw_info - Query VEB BW information
10231 * @veb: the veb to query
10232 *
10233 * Query the Tx scheduler BW configuration data for given VEB
10234 **/
10235static int i40e_veb_get_bw_info(struct i40e_veb *veb)
10236{
10237 struct i40e_aqc_query_switching_comp_ets_config_resp ets_data;
10238 struct i40e_aqc_query_switching_comp_bw_config_resp bw_data;
10239 struct i40e_pf *pf = veb->pf;
10240 struct i40e_hw *hw = &pf->hw;
10241 u32 tc_bw_max;
10242 int ret = 0;
10243 int i;
10244
10245 ret = i40e_aq_query_switch_comp_bw_config(hw, veb->seid,
10246 &bw_data, NULL);
10247 if (ret) {
10248 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010249 "query veb bw config failed, err %s aq_err %s\n",
10250 i40e_stat_str(&pf->hw, ret),
10251 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010252 goto out;
10253 }
10254
10255 ret = i40e_aq_query_switch_comp_ets_config(hw, veb->seid,
10256 &ets_data, NULL);
10257 if (ret) {
10258 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010259 "query veb bw ets config failed, err %s aq_err %s\n",
10260 i40e_stat_str(&pf->hw, ret),
10261 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010262 goto out;
10263 }
10264
10265 veb->bw_limit = le16_to_cpu(ets_data.port_bw_limit);
10266 veb->bw_max_quanta = ets_data.tc_bw_max;
10267 veb->is_abs_credits = bw_data.absolute_credits_enable;
Neerav Parikh23cd1f02014-11-12 00:18:41 +000010268 veb->enabled_tc = ets_data.tc_valid_bits;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010269 tc_bw_max = le16_to_cpu(bw_data.tc_bw_max[0]) |
10270 (le16_to_cpu(bw_data.tc_bw_max[1]) << 16);
10271 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
10272 veb->bw_tc_share_credits[i] = bw_data.tc_bw_share_credits[i];
10273 veb->bw_tc_limit_credits[i] =
10274 le16_to_cpu(bw_data.tc_bw_limits[i]);
10275 veb->bw_tc_max_quanta[i] = ((tc_bw_max >> (i*4)) & 0x7);
10276 }
10277
10278out:
10279 return ret;
10280}
10281
10282/**
10283 * i40e_veb_mem_alloc - Allocates the next available struct veb in the PF
10284 * @pf: board private structure
10285 *
10286 * On error: returns error code (negative)
10287 * On success: returns vsi index in PF (positive)
10288 **/
10289static int i40e_veb_mem_alloc(struct i40e_pf *pf)
10290{
10291 int ret = -ENOENT;
10292 struct i40e_veb *veb;
10293 int i;
10294
10295 /* Need to protect the allocation of switch elements at the PF level */
10296 mutex_lock(&pf->switch_mutex);
10297
10298 /* VEB list may be fragmented if VEB creation/destruction has
10299 * been happening. We can afford to do a quick scan to look
10300 * for any free slots in the list.
10301 *
10302 * find next empty veb slot, looping back around if necessary
10303 */
10304 i = 0;
10305 while ((i < I40E_MAX_VEB) && (pf->veb[i] != NULL))
10306 i++;
10307 if (i >= I40E_MAX_VEB) {
10308 ret = -ENOMEM;
10309 goto err_alloc_veb; /* out of VEB slots! */
10310 }
10311
10312 veb = kzalloc(sizeof(*veb), GFP_KERNEL);
10313 if (!veb) {
10314 ret = -ENOMEM;
10315 goto err_alloc_veb;
10316 }
10317 veb->pf = pf;
10318 veb->idx = i;
10319 veb->enabled_tc = 1;
10320
10321 pf->veb[i] = veb;
10322 ret = i;
10323err_alloc_veb:
10324 mutex_unlock(&pf->switch_mutex);
10325 return ret;
10326}
10327
10328/**
10329 * i40e_switch_branch_release - Delete a branch of the switch tree
10330 * @branch: where to start deleting
10331 *
10332 * This uses recursion to find the tips of the branch to be
10333 * removed, deleting until we get back to and can delete this VEB.
10334 **/
10335static void i40e_switch_branch_release(struct i40e_veb *branch)
10336{
10337 struct i40e_pf *pf = branch->pf;
10338 u16 branch_seid = branch->seid;
10339 u16 veb_idx = branch->idx;
10340 int i;
10341
10342 /* release any VEBs on this VEB - RECURSION */
10343 for (i = 0; i < I40E_MAX_VEB; i++) {
10344 if (!pf->veb[i])
10345 continue;
10346 if (pf->veb[i]->uplink_seid == branch->seid)
10347 i40e_switch_branch_release(pf->veb[i]);
10348 }
10349
10350 /* Release the VSIs on this VEB, but not the owner VSI.
10351 *
10352 * NOTE: Removing the last VSI on a VEB has the SIDE EFFECT of removing
10353 * the VEB itself, so don't use (*branch) after this loop.
10354 */
Mitch Williams505682c2014-05-20 08:01:37 +000010355 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010356 if (!pf->vsi[i])
10357 continue;
10358 if (pf->vsi[i]->uplink_seid == branch_seid &&
10359 (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
10360 i40e_vsi_release(pf->vsi[i]);
10361 }
10362 }
10363
10364 /* There's one corner case where the VEB might not have been
10365 * removed, so double check it here and remove it if needed.
10366 * This case happens if the veb was created from the debugfs
10367 * commands and no VSIs were added to it.
10368 */
10369 if (pf->veb[veb_idx])
10370 i40e_veb_release(pf->veb[veb_idx]);
10371}
10372
10373/**
10374 * i40e_veb_clear - remove veb struct
10375 * @veb: the veb to remove
10376 **/
10377static void i40e_veb_clear(struct i40e_veb *veb)
10378{
10379 if (!veb)
10380 return;
10381
10382 if (veb->pf) {
10383 struct i40e_pf *pf = veb->pf;
10384
10385 mutex_lock(&pf->switch_mutex);
10386 if (pf->veb[veb->idx] == veb)
10387 pf->veb[veb->idx] = NULL;
10388 mutex_unlock(&pf->switch_mutex);
10389 }
10390
10391 kfree(veb);
10392}
10393
10394/**
10395 * i40e_veb_release - Delete a VEB and free its resources
10396 * @veb: the VEB being removed
10397 **/
10398void i40e_veb_release(struct i40e_veb *veb)
10399{
10400 struct i40e_vsi *vsi = NULL;
10401 struct i40e_pf *pf;
10402 int i, n = 0;
10403
10404 pf = veb->pf;
10405
10406 /* find the remaining VSI and check for extras */
Mitch Williams505682c2014-05-20 08:01:37 +000010407 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010408 if (pf->vsi[i] && pf->vsi[i]->uplink_seid == veb->seid) {
10409 n++;
10410 vsi = pf->vsi[i];
10411 }
10412 }
10413 if (n != 1) {
10414 dev_info(&pf->pdev->dev,
10415 "can't remove VEB %d with %d VSIs left\n",
10416 veb->seid, n);
10417 return;
10418 }
10419
10420 /* move the remaining VSI to uplink veb */
10421 vsi->flags &= ~I40E_VSI_FLAG_VEB_OWNER;
10422 if (veb->uplink_seid) {
10423 vsi->uplink_seid = veb->uplink_seid;
10424 if (veb->uplink_seid == pf->mac_seid)
10425 vsi->veb_idx = I40E_NO_VEB;
10426 else
10427 vsi->veb_idx = veb->veb_idx;
10428 } else {
10429 /* floating VEB */
10430 vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
10431 vsi->veb_idx = pf->vsi[pf->lan_vsi]->veb_idx;
10432 }
10433
10434 i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
10435 i40e_veb_clear(veb);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010436}
10437
10438/**
10439 * i40e_add_veb - create the VEB in the switch
10440 * @veb: the VEB to be instantiated
10441 * @vsi: the controlling VSI
10442 **/
10443static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi)
10444{
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010445 struct i40e_pf *pf = veb->pf;
Shannon Nelson66fc3602016-01-13 16:51:42 -080010446 bool enable_stats = !!(pf->flags & I40E_FLAG_VEB_STATS_ENABLED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010447 int ret;
10448
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010449 ret = i40e_aq_add_veb(&pf->hw, veb->uplink_seid, vsi->seid,
Mitch Williams5bc16032016-05-16 10:26:43 -070010450 veb->enabled_tc, false,
Shannon Nelson66fc3602016-01-13 16:51:42 -080010451 &veb->seid, enable_stats, NULL);
Mitch Williams5bc16032016-05-16 10:26:43 -070010452
10453 /* get a VEB from the hardware */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010454 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010455 dev_info(&pf->pdev->dev,
10456 "couldn't add VEB, err %s aq_err %s\n",
10457 i40e_stat_str(&pf->hw, ret),
10458 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010459 return -EPERM;
10460 }
10461
10462 /* get statistics counter */
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010463 ret = i40e_aq_get_veb_parameters(&pf->hw, veb->seid, NULL, NULL,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010464 &veb->stats_idx, NULL, NULL, NULL);
10465 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010466 dev_info(&pf->pdev->dev,
10467 "couldn't get VEB statistics idx, err %s aq_err %s\n",
10468 i40e_stat_str(&pf->hw, ret),
10469 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010470 return -EPERM;
10471 }
10472 ret = i40e_veb_get_bw_info(veb);
10473 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010474 dev_info(&pf->pdev->dev,
10475 "couldn't get VEB bw info, err %s aq_err %s\n",
10476 i40e_stat_str(&pf->hw, ret),
10477 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10478 i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010479 return -ENOENT;
10480 }
10481
10482 vsi->uplink_seid = veb->seid;
10483 vsi->veb_idx = veb->idx;
10484 vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
10485
10486 return 0;
10487}
10488
10489/**
10490 * i40e_veb_setup - Set up a VEB
10491 * @pf: board private structure
10492 * @flags: VEB setup flags
10493 * @uplink_seid: the switch element to link to
10494 * @vsi_seid: the initial VSI seid
10495 * @enabled_tc: Enabled TC bit-map
10496 *
10497 * This allocates the sw VEB structure and links it into the switch
10498 * It is possible and legal for this to be a duplicate of an already
10499 * existing VEB. It is also possible for both uplink and vsi seids
10500 * to be zero, in order to create a floating VEB.
10501 *
10502 * Returns pointer to the successfully allocated VEB sw struct on
10503 * success, otherwise returns NULL on failure.
10504 **/
10505struct i40e_veb *i40e_veb_setup(struct i40e_pf *pf, u16 flags,
10506 u16 uplink_seid, u16 vsi_seid,
10507 u8 enabled_tc)
10508{
10509 struct i40e_veb *veb, *uplink_veb = NULL;
10510 int vsi_idx, veb_idx;
10511 int ret;
10512
10513 /* if one seid is 0, the other must be 0 to create a floating relay */
10514 if ((uplink_seid == 0 || vsi_seid == 0) &&
10515 (uplink_seid + vsi_seid != 0)) {
10516 dev_info(&pf->pdev->dev,
10517 "one, not both seid's are 0: uplink=%d vsi=%d\n",
10518 uplink_seid, vsi_seid);
10519 return NULL;
10520 }
10521
10522 /* make sure there is such a vsi and uplink */
Mitch Williams505682c2014-05-20 08:01:37 +000010523 for (vsi_idx = 0; vsi_idx < pf->num_alloc_vsi; vsi_idx++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010524 if (pf->vsi[vsi_idx] && pf->vsi[vsi_idx]->seid == vsi_seid)
10525 break;
Mitch Williams505682c2014-05-20 08:01:37 +000010526 if (vsi_idx >= pf->num_alloc_vsi && vsi_seid != 0) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010527 dev_info(&pf->pdev->dev, "vsi seid %d not found\n",
10528 vsi_seid);
10529 return NULL;
10530 }
10531
10532 if (uplink_seid && uplink_seid != pf->mac_seid) {
10533 for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
10534 if (pf->veb[veb_idx] &&
10535 pf->veb[veb_idx]->seid == uplink_seid) {
10536 uplink_veb = pf->veb[veb_idx];
10537 break;
10538 }
10539 }
10540 if (!uplink_veb) {
10541 dev_info(&pf->pdev->dev,
10542 "uplink seid %d not found\n", uplink_seid);
10543 return NULL;
10544 }
10545 }
10546
10547 /* get veb sw struct */
10548 veb_idx = i40e_veb_mem_alloc(pf);
10549 if (veb_idx < 0)
10550 goto err_alloc;
10551 veb = pf->veb[veb_idx];
10552 veb->flags = flags;
10553 veb->uplink_seid = uplink_seid;
10554 veb->veb_idx = (uplink_veb ? uplink_veb->idx : I40E_NO_VEB);
10555 veb->enabled_tc = (enabled_tc ? enabled_tc : 0x1);
10556
10557 /* create the VEB in the switch */
10558 ret = i40e_add_veb(veb, pf->vsi[vsi_idx]);
10559 if (ret)
10560 goto err_veb;
Shannon Nelson1bb8b932014-04-23 04:49:54 +000010561 if (vsi_idx == pf->lan_vsi)
10562 pf->lan_veb = veb->idx;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010563
10564 return veb;
10565
10566err_veb:
10567 i40e_veb_clear(veb);
10568err_alloc:
10569 return NULL;
10570}
10571
10572/**
Jeff Kirsherb40c82e2015-02-27 09:18:34 +000010573 * i40e_setup_pf_switch_element - set PF vars based on switch type
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010574 * @pf: board private structure
10575 * @ele: element we are building info from
10576 * @num_reported: total number of elements
10577 * @printconfig: should we print the contents
10578 *
10579 * helper function to assist in extracting a few useful SEID values.
10580 **/
10581static void i40e_setup_pf_switch_element(struct i40e_pf *pf,
10582 struct i40e_aqc_switch_config_element_resp *ele,
10583 u16 num_reported, bool printconfig)
10584{
10585 u16 downlink_seid = le16_to_cpu(ele->downlink_seid);
10586 u16 uplink_seid = le16_to_cpu(ele->uplink_seid);
10587 u8 element_type = ele->element_type;
10588 u16 seid = le16_to_cpu(ele->seid);
10589
10590 if (printconfig)
10591 dev_info(&pf->pdev->dev,
10592 "type=%d seid=%d uplink=%d downlink=%d\n",
10593 element_type, seid, uplink_seid, downlink_seid);
10594
10595 switch (element_type) {
10596 case I40E_SWITCH_ELEMENT_TYPE_MAC:
10597 pf->mac_seid = seid;
10598 break;
10599 case I40E_SWITCH_ELEMENT_TYPE_VEB:
10600 /* Main VEB? */
10601 if (uplink_seid != pf->mac_seid)
10602 break;
10603 if (pf->lan_veb == I40E_NO_VEB) {
10604 int v;
10605
10606 /* find existing or else empty VEB */
10607 for (v = 0; v < I40E_MAX_VEB; v++) {
10608 if (pf->veb[v] && (pf->veb[v]->seid == seid)) {
10609 pf->lan_veb = v;
10610 break;
10611 }
10612 }
10613 if (pf->lan_veb == I40E_NO_VEB) {
10614 v = i40e_veb_mem_alloc(pf);
10615 if (v < 0)
10616 break;
10617 pf->lan_veb = v;
10618 }
10619 }
10620
10621 pf->veb[pf->lan_veb]->seid = seid;
10622 pf->veb[pf->lan_veb]->uplink_seid = pf->mac_seid;
10623 pf->veb[pf->lan_veb]->pf = pf;
10624 pf->veb[pf->lan_veb]->veb_idx = I40E_NO_VEB;
10625 break;
10626 case I40E_SWITCH_ELEMENT_TYPE_VSI:
10627 if (num_reported != 1)
10628 break;
10629 /* This is immediately after a reset so we can assume this is
10630 * the PF's VSI
10631 */
10632 pf->mac_seid = uplink_seid;
10633 pf->pf_seid = downlink_seid;
10634 pf->main_vsi_seid = seid;
10635 if (printconfig)
10636 dev_info(&pf->pdev->dev,
10637 "pf_seid=%d main_vsi_seid=%d\n",
10638 pf->pf_seid, pf->main_vsi_seid);
10639 break;
10640 case I40E_SWITCH_ELEMENT_TYPE_PF:
10641 case I40E_SWITCH_ELEMENT_TYPE_VF:
10642 case I40E_SWITCH_ELEMENT_TYPE_EMP:
10643 case I40E_SWITCH_ELEMENT_TYPE_BMC:
10644 case I40E_SWITCH_ELEMENT_TYPE_PE:
10645 case I40E_SWITCH_ELEMENT_TYPE_PA:
10646 /* ignore these for now */
10647 break;
10648 default:
10649 dev_info(&pf->pdev->dev, "unknown element type=%d seid=%d\n",
10650 element_type, seid);
10651 break;
10652 }
10653}
10654
10655/**
10656 * i40e_fetch_switch_configuration - Get switch config from firmware
10657 * @pf: board private structure
10658 * @printconfig: should we print the contents
10659 *
10660 * Get the current switch configuration from the device and
10661 * extract a few useful SEID values.
10662 **/
10663int i40e_fetch_switch_configuration(struct i40e_pf *pf, bool printconfig)
10664{
10665 struct i40e_aqc_get_switch_config_resp *sw_config;
10666 u16 next_seid = 0;
10667 int ret = 0;
10668 u8 *aq_buf;
10669 int i;
10670
10671 aq_buf = kzalloc(I40E_AQ_LARGE_BUF, GFP_KERNEL);
10672 if (!aq_buf)
10673 return -ENOMEM;
10674
10675 sw_config = (struct i40e_aqc_get_switch_config_resp *)aq_buf;
10676 do {
10677 u16 num_reported, num_total;
10678
10679 ret = i40e_aq_get_switch_config(&pf->hw, sw_config,
10680 I40E_AQ_LARGE_BUF,
10681 &next_seid, NULL);
10682 if (ret) {
10683 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010684 "get switch config failed err %s aq_err %s\n",
10685 i40e_stat_str(&pf->hw, ret),
10686 i40e_aq_str(&pf->hw,
10687 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010688 kfree(aq_buf);
10689 return -ENOENT;
10690 }
10691
10692 num_reported = le16_to_cpu(sw_config->header.num_reported);
10693 num_total = le16_to_cpu(sw_config->header.num_total);
10694
10695 if (printconfig)
10696 dev_info(&pf->pdev->dev,
10697 "header: %d reported %d total\n",
10698 num_reported, num_total);
10699
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010700 for (i = 0; i < num_reported; i++) {
10701 struct i40e_aqc_switch_config_element_resp *ele =
10702 &sw_config->element[i];
10703
10704 i40e_setup_pf_switch_element(pf, ele, num_reported,
10705 printconfig);
10706 }
10707 } while (next_seid != 0);
10708
10709 kfree(aq_buf);
10710 return ret;
10711}
10712
10713/**
10714 * i40e_setup_pf_switch - Setup the HW switch on startup or after reset
10715 * @pf: board private structure
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010716 * @reinit: if the Main VSI needs to re-initialized.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010717 *
10718 * Returns 0 on success, negative value on failure
10719 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010720static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010721{
Anjali Singhai Jainb5569892016-05-03 15:13:12 -070010722 u16 flags = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010723 int ret;
10724
10725 /* find out what's out there already */
10726 ret = i40e_fetch_switch_configuration(pf, false);
10727 if (ret) {
10728 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010729 "couldn't fetch switch config, err %s aq_err %s\n",
10730 i40e_stat_str(&pf->hw, ret),
10731 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010732 return ret;
10733 }
10734 i40e_pf_reset_stats(pf);
10735
Anjali Singhai Jainb5569892016-05-03 15:13:12 -070010736 /* set the switch config bit for the whole device to
10737 * support limited promisc or true promisc
10738 * when user requests promisc. The default is limited
10739 * promisc.
10740 */
10741
10742 if ((pf->hw.pf_id == 0) &&
10743 !(pf->flags & I40E_FLAG_TRUE_PROMISC_SUPPORT))
10744 flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
10745
10746 if (pf->hw.pf_id == 0) {
10747 u16 valid_flags;
10748
10749 valid_flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
10750 ret = i40e_aq_set_switch_config(&pf->hw, flags, valid_flags,
10751 NULL);
10752 if (ret && pf->hw.aq.asq_last_status != I40E_AQ_RC_ESRCH) {
10753 dev_info(&pf->pdev->dev,
10754 "couldn't set switch config bits, err %s aq_err %s\n",
10755 i40e_stat_str(&pf->hw, ret),
10756 i40e_aq_str(&pf->hw,
10757 pf->hw.aq.asq_last_status));
10758 /* not a fatal problem, just keep going */
10759 }
10760 }
10761
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010762 /* first time setup */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010763 if (pf->lan_vsi == I40E_NO_VSI || reinit) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010764 struct i40e_vsi *vsi = NULL;
10765 u16 uplink_seid;
10766
10767 /* Set up the PF VSI associated with the PF's main VSI
10768 * that is already in the HW switch
10769 */
10770 if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
10771 uplink_seid = pf->veb[pf->lan_veb]->seid;
10772 else
10773 uplink_seid = pf->mac_seid;
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010774 if (pf->lan_vsi == I40E_NO_VSI)
10775 vsi = i40e_vsi_setup(pf, I40E_VSI_MAIN, uplink_seid, 0);
10776 else if (reinit)
10777 vsi = i40e_vsi_reinit_setup(pf->vsi[pf->lan_vsi]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010778 if (!vsi) {
10779 dev_info(&pf->pdev->dev, "setup of MAIN VSI failed\n");
10780 i40e_fdir_teardown(pf);
10781 return -EAGAIN;
10782 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010783 } else {
10784 /* force a reset of TC and queue layout configurations */
10785 u8 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
Jesse Brandeburg6995b362015-08-28 17:55:54 -040010786
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010787 pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
10788 pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
10789 i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
10790 }
10791 i40e_vlan_stripping_disable(pf->vsi[pf->lan_vsi]);
10792
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010793 i40e_fdir_sb_setup(pf);
10794
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010795 /* Setup static PF queue filter control settings */
10796 ret = i40e_setup_pf_filter_control(pf);
10797 if (ret) {
10798 dev_info(&pf->pdev->dev, "setup_pf_filter_control failed: %d\n",
10799 ret);
10800 /* Failure here should not stop continuing other steps */
10801 }
10802
10803 /* enable RSS in the HW, even for only one queue, as the stack can use
10804 * the hash
10805 */
10806 if ((pf->flags & I40E_FLAG_RSS_ENABLED))
Helin Zhang043dd652015-10-21 19:56:23 -040010807 i40e_pf_config_rss(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010808
10809 /* fill in link information and enable LSE reporting */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010810 i40e_link_event(pf);
10811
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +000010812 /* Initialize user-specific link properties */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010813 pf->fc_autoneg_status = ((pf->hw.phy.link_info.an_info &
10814 I40E_AQ_AN_COMPLETED) ? true : false);
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +000010815
Jacob Kellerbeb0dff2014-01-11 05:43:19 +000010816 i40e_ptp_init(pf);
10817
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010818 return ret;
10819}
10820
10821/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010822 * i40e_determine_queue_usage - Work out queue distribution
10823 * @pf: board private structure
10824 **/
10825static void i40e_determine_queue_usage(struct i40e_pf *pf)
10826{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010827 int queues_left;
10828
10829 pf->num_lan_qps = 0;
Vasu Dev38e00432014-08-01 13:27:03 -070010830#ifdef I40E_FCOE
10831 pf->num_fcoe_qps = 0;
10832#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010833
10834 /* Find the max queues to be put into basic use. We'll always be
10835 * using TC0, whether or not DCB is running, and TC0 will get the
10836 * big RSS set.
10837 */
10838 queues_left = pf->hw.func_caps.num_tx_qp;
10839
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010840 if ((queues_left == 1) ||
Frank Zhang9aa7e932014-05-20 08:01:42 +000010841 !(pf->flags & I40E_FLAG_MSIX_ENABLED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010842 /* one qp for PF, no queues for anything else */
10843 queues_left = 0;
Helin Zhangacd65442015-10-26 19:44:28 -040010844 pf->alloc_rss_size = pf->num_lan_qps = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010845
10846 /* make sure all the fancies are disabled */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080010847 pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060010848 I40E_FLAG_IWARP_ENABLED |
Vasu Dev38e00432014-08-01 13:27:03 -070010849#ifdef I40E_FCOE
10850 I40E_FLAG_FCOE_ENABLED |
10851#endif
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080010852 I40E_FLAG_FD_SB_ENABLED |
10853 I40E_FLAG_FD_ATR_ENABLED |
Neerav Parikh4d9b6042014-05-22 06:31:51 +000010854 I40E_FLAG_DCB_CAPABLE |
Dave Ertmana0362442016-08-29 17:38:26 -070010855 I40E_FLAG_DCB_ENABLED |
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080010856 I40E_FLAG_SRIOV_ENABLED |
10857 I40E_FLAG_VMDQ_ENABLED);
Frank Zhang9aa7e932014-05-20 08:01:42 +000010858 } else if (!(pf->flags & (I40E_FLAG_RSS_ENABLED |
10859 I40E_FLAG_FD_SB_ENABLED |
Anjali Singhai Jainbbe7d0e2014-05-20 08:01:44 +000010860 I40E_FLAG_FD_ATR_ENABLED |
Neerav Parikh4d9b6042014-05-22 06:31:51 +000010861 I40E_FLAG_DCB_CAPABLE))) {
Frank Zhang9aa7e932014-05-20 08:01:42 +000010862 /* one qp for PF */
Helin Zhangacd65442015-10-26 19:44:28 -040010863 pf->alloc_rss_size = pf->num_lan_qps = 1;
Frank Zhang9aa7e932014-05-20 08:01:42 +000010864 queues_left -= pf->num_lan_qps;
10865
10866 pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060010867 I40E_FLAG_IWARP_ENABLED |
Vasu Dev38e00432014-08-01 13:27:03 -070010868#ifdef I40E_FCOE
10869 I40E_FLAG_FCOE_ENABLED |
10870#endif
Frank Zhang9aa7e932014-05-20 08:01:42 +000010871 I40E_FLAG_FD_SB_ENABLED |
10872 I40E_FLAG_FD_ATR_ENABLED |
10873 I40E_FLAG_DCB_ENABLED |
10874 I40E_FLAG_VMDQ_ENABLED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010875 } else {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010876 /* Not enough queues for all TCs */
Neerav Parikh4d9b6042014-05-22 06:31:51 +000010877 if ((pf->flags & I40E_FLAG_DCB_CAPABLE) &&
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010878 (queues_left < I40E_MAX_TRAFFIC_CLASS)) {
Dave Ertmana0362442016-08-29 17:38:26 -070010879 pf->flags &= ~(I40E_FLAG_DCB_CAPABLE |
10880 I40E_FLAG_DCB_ENABLED);
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010881 dev_info(&pf->pdev->dev, "not enough queues for DCB. DCB is disabled.\n");
10882 }
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +000010883 pf->num_lan_qps = max_t(int, pf->rss_size_max,
10884 num_online_cpus());
10885 pf->num_lan_qps = min_t(int, pf->num_lan_qps,
10886 pf->hw.func_caps.num_tx_qp);
10887
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010888 queues_left -= pf->num_lan_qps;
10889 }
10890
Vasu Dev38e00432014-08-01 13:27:03 -070010891#ifdef I40E_FCOE
10892 if (pf->flags & I40E_FLAG_FCOE_ENABLED) {
10893 if (I40E_DEFAULT_FCOE <= queues_left) {
10894 pf->num_fcoe_qps = I40E_DEFAULT_FCOE;
10895 } else if (I40E_MINIMUM_FCOE <= queues_left) {
10896 pf->num_fcoe_qps = I40E_MINIMUM_FCOE;
10897 } else {
10898 pf->num_fcoe_qps = 0;
10899 pf->flags &= ~I40E_FLAG_FCOE_ENABLED;
10900 dev_info(&pf->pdev->dev, "not enough queues for FCoE. FCoE feature will be disabled\n");
10901 }
10902
10903 queues_left -= pf->num_fcoe_qps;
10904 }
10905
10906#endif
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010907 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
10908 if (queues_left > 1) {
10909 queues_left -= 1; /* save 1 queue for FD */
10910 } else {
10911 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
10912 dev_info(&pf->pdev->dev, "not enough queues for Flow Director. Flow Director feature is disabled\n");
10913 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010914 }
10915
10916 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
10917 pf->num_vf_qps && pf->num_req_vfs && queues_left) {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010918 pf->num_req_vfs = min_t(int, pf->num_req_vfs,
10919 (queues_left / pf->num_vf_qps));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010920 queues_left -= (pf->num_req_vfs * pf->num_vf_qps);
10921 }
10922
10923 if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
10924 pf->num_vmdq_vsis && pf->num_vmdq_qps && queues_left) {
10925 pf->num_vmdq_vsis = min_t(int, pf->num_vmdq_vsis,
10926 (queues_left / pf->num_vmdq_qps));
10927 queues_left -= (pf->num_vmdq_vsis * pf->num_vmdq_qps);
10928 }
10929
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +000010930 pf->queues_left = queues_left;
Neerav Parikh8279e492015-09-03 17:18:50 -040010931 dev_dbg(&pf->pdev->dev,
10932 "qs_avail=%d FD SB=%d lan_qs=%d lan_tc0=%d vf=%d*%d vmdq=%d*%d, remaining=%d\n",
10933 pf->hw.func_caps.num_tx_qp,
10934 !!(pf->flags & I40E_FLAG_FD_SB_ENABLED),
Helin Zhangacd65442015-10-26 19:44:28 -040010935 pf->num_lan_qps, pf->alloc_rss_size, pf->num_req_vfs,
10936 pf->num_vf_qps, pf->num_vmdq_vsis, pf->num_vmdq_qps,
10937 queues_left);
Vasu Dev38e00432014-08-01 13:27:03 -070010938#ifdef I40E_FCOE
Neerav Parikh8279e492015-09-03 17:18:50 -040010939 dev_dbg(&pf->pdev->dev, "fcoe queues = %d\n", pf->num_fcoe_qps);
Vasu Dev38e00432014-08-01 13:27:03 -070010940#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010941}
10942
10943/**
10944 * i40e_setup_pf_filter_control - Setup PF static filter control
10945 * @pf: PF to be setup
10946 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +000010947 * i40e_setup_pf_filter_control sets up a PF's initial filter control
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010948 * settings. If PE/FCoE are enabled then it will also set the per PF
10949 * based filter sizes required for them. It also enables Flow director,
10950 * ethertype and macvlan type filter settings for the pf.
10951 *
10952 * Returns 0 on success, negative on failure
10953 **/
10954static int i40e_setup_pf_filter_control(struct i40e_pf *pf)
10955{
10956 struct i40e_filter_control_settings *settings = &pf->filter_settings;
10957
10958 settings->hash_lut_size = I40E_HASH_LUT_SIZE_128;
10959
10960 /* Flow Director is enabled */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080010961 if (pf->flags & (I40E_FLAG_FD_SB_ENABLED | I40E_FLAG_FD_ATR_ENABLED))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010962 settings->enable_fdir = true;
10963
10964 /* Ethtype and MACVLAN filters enabled for PF */
10965 settings->enable_ethtype = true;
10966 settings->enable_macvlan = true;
10967
10968 if (i40e_set_filter_control(&pf->hw, settings))
10969 return -ENOENT;
10970
10971 return 0;
10972}
10973
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010974#define INFO_STRING_LEN 255
Shannon Nelson7fd89542015-10-21 19:47:04 -040010975#define REMAIN(__x) (INFO_STRING_LEN - (__x))
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010976static void i40e_print_features(struct i40e_pf *pf)
10977{
10978 struct i40e_hw *hw = &pf->hw;
Joe Perches3b195842015-12-03 04:20:57 -080010979 char *buf;
10980 int i;
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010981
Joe Perches3b195842015-12-03 04:20:57 -080010982 buf = kmalloc(INFO_STRING_LEN, GFP_KERNEL);
10983 if (!buf)
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010984 return;
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010985
Joe Perches3b195842015-12-03 04:20:57 -080010986 i = snprintf(buf, INFO_STRING_LEN, "Features: PF-id[%d]", hw->pf_id);
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010987#ifdef CONFIG_PCI_IOV
Joe Perches3b195842015-12-03 04:20:57 -080010988 i += snprintf(&buf[i], REMAIN(i), " VFs: %d", pf->num_req_vfs);
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010989#endif
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -070010990 i += snprintf(&buf[i], REMAIN(i), " VSIs: %d QP: %d",
Shannon Nelson7fd89542015-10-21 19:47:04 -040010991 pf->hw.func_caps.num_vsis,
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -070010992 pf->vsi[pf->lan_vsi]->num_queue_pairs);
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010993 if (pf->flags & I40E_FLAG_RSS_ENABLED)
Joe Perches3b195842015-12-03 04:20:57 -080010994 i += snprintf(&buf[i], REMAIN(i), " RSS");
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010995 if (pf->flags & I40E_FLAG_FD_ATR_ENABLED)
Joe Perches3b195842015-12-03 04:20:57 -080010996 i += snprintf(&buf[i], REMAIN(i), " FD_ATR");
Akeem G Abodunrinc6423ff2014-05-10 04:49:08 +000010997 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
Joe Perches3b195842015-12-03 04:20:57 -080010998 i += snprintf(&buf[i], REMAIN(i), " FD_SB");
10999 i += snprintf(&buf[i], REMAIN(i), " NTUPLE");
Akeem G Abodunrinc6423ff2014-05-10 04:49:08 +000011000 }
Neerav Parikh4d9b6042014-05-22 06:31:51 +000011001 if (pf->flags & I40E_FLAG_DCB_CAPABLE)
Joe Perches3b195842015-12-03 04:20:57 -080011002 i += snprintf(&buf[i], REMAIN(i), " DCB");
Joe Perches3b195842015-12-03 04:20:57 -080011003 i += snprintf(&buf[i], REMAIN(i), " VxLAN");
Singhai, Anjali6a899022015-12-14 12:21:18 -080011004 i += snprintf(&buf[i], REMAIN(i), " Geneve");
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000011005 if (pf->flags & I40E_FLAG_PTP)
Joe Perches3b195842015-12-03 04:20:57 -080011006 i += snprintf(&buf[i], REMAIN(i), " PTP");
Vasu Dev38e00432014-08-01 13:27:03 -070011007#ifdef I40E_FCOE
11008 if (pf->flags & I40E_FLAG_FCOE_ENABLED)
Joe Perches3b195842015-12-03 04:20:57 -080011009 i += snprintf(&buf[i], REMAIN(i), " FCOE");
Vasu Dev38e00432014-08-01 13:27:03 -070011010#endif
Shannon Nelson6dec1012015-09-28 14:12:30 -040011011 if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
Joe Perches3b195842015-12-03 04:20:57 -080011012 i += snprintf(&buf[i], REMAIN(i), " VEB");
Shannon Nelson6dec1012015-09-28 14:12:30 -040011013 else
Joe Perches3b195842015-12-03 04:20:57 -080011014 i += snprintf(&buf[i], REMAIN(i), " VEPA");
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000011015
Joe Perches3b195842015-12-03 04:20:57 -080011016 dev_info(&pf->pdev->dev, "%s\n", buf);
11017 kfree(buf);
Shannon Nelson7fd89542015-10-21 19:47:04 -040011018 WARN_ON(i > INFO_STRING_LEN);
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000011019}
11020
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011021/**
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050011022 * i40e_get_platform_mac_addr - get platform-specific MAC address
11023 *
11024 * @pdev: PCI device information struct
11025 * @pf: board private structure
11026 *
11027 * Look up the MAC address in Open Firmware on systems that support it,
11028 * and use IDPROM on SPARC if no OF address is found. On return, the
11029 * I40E_FLAG_PF_MAC will be wset in pf->flags if a platform-specific value
11030 * has been selected.
11031 **/
11032static void i40e_get_platform_mac_addr(struct pci_dev *pdev, struct i40e_pf *pf)
11033{
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050011034 pf->flags &= ~I40E_FLAG_PF_MAC;
Sowmini Varadhanba942722016-01-12 19:32:31 -080011035 if (!eth_platform_get_mac_address(&pdev->dev, pf->hw.mac.addr))
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050011036 pf->flags |= I40E_FLAG_PF_MAC;
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050011037}
11038
11039/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011040 * i40e_probe - Device initialization routine
11041 * @pdev: PCI device information struct
11042 * @ent: entry in i40e_pci_tbl
11043 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +000011044 * i40e_probe initializes a PF identified by a pci_dev structure.
11045 * The OS initialization, configuring of the PF private structure,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011046 * and a hardware reset occur.
11047 *
11048 * Returns 0 on success, negative on failure
11049 **/
11050static int i40e_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
11051{
Catherine Sullivane8278452015-02-06 08:52:08 +000011052 struct i40e_aq_get_phy_abilities_resp abilities;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011053 struct i40e_pf *pf;
11054 struct i40e_hw *hw;
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +000011055 static u16 pfs_found;
Shannon Nelson1d5109d2015-08-26 15:14:08 -040011056 u16 wol_nvm_bits;
Catherine Sullivand4dfb812013-11-28 06:39:21 +000011057 u16 link_status;
Jean Sacren6f66a482015-09-19 05:08:45 -060011058 int err;
Anjali Singhai Jain4f2f017c2015-10-21 19:47:07 -040011059 u32 val;
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +000011060 u32 i;
Helin Zhang58fc3262015-10-01 14:37:38 -040011061 u8 set_fc_aq_fail;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011062
11063 err = pci_enable_device_mem(pdev);
11064 if (err)
11065 return err;
11066
11067 /* set up for high or low dma */
Mitch Williams64942942014-02-11 08:26:33 +000011068 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
Mitch Williams64942942014-02-11 08:26:33 +000011069 if (err) {
Jean Sacrene3e3bfd2014-03-25 04:30:27 +000011070 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
11071 if (err) {
11072 dev_err(&pdev->dev,
11073 "DMA configuration failed: 0x%x\n", err);
11074 goto err_dma;
11075 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011076 }
11077
11078 /* set up pci connections */
Johannes Thumshirn56d766d2016-06-07 09:44:05 +020011079 err = pci_request_mem_regions(pdev, i40e_driver_name);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011080 if (err) {
11081 dev_info(&pdev->dev,
11082 "pci_request_selected_regions failed %d\n", err);
11083 goto err_pci_reg;
11084 }
11085
11086 pci_enable_pcie_error_reporting(pdev);
11087 pci_set_master(pdev);
11088
11089 /* Now that we have a PCI connection, we need to do the
11090 * low level device setup. This is primarily setting up
11091 * the Admin Queue structures and then querying for the
11092 * device's current profile information.
11093 */
11094 pf = kzalloc(sizeof(*pf), GFP_KERNEL);
11095 if (!pf) {
11096 err = -ENOMEM;
11097 goto err_pf_alloc;
11098 }
11099 pf->next_vsi = 0;
11100 pf->pdev = pdev;
11101 set_bit(__I40E_DOWN, &pf->state);
11102
11103 hw = &pf->hw;
11104 hw->back = pf;
Anjali Singhai232f4702015-02-26 16:15:39 +000011105
Shannon Nelson2ac8b672015-07-23 16:54:37 -040011106 pf->ioremap_len = min_t(int, pci_resource_len(pdev, 0),
11107 I40E_MAX_CSR_SPACE);
Anjali Singhai232f4702015-02-26 16:15:39 +000011108
Shannon Nelson2ac8b672015-07-23 16:54:37 -040011109 hw->hw_addr = ioremap(pci_resource_start(pdev, 0), pf->ioremap_len);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011110 if (!hw->hw_addr) {
11111 err = -EIO;
11112 dev_info(&pdev->dev, "ioremap(0x%04x, 0x%04x) failed: 0x%x\n",
11113 (unsigned int)pci_resource_start(pdev, 0),
Shannon Nelson2ac8b672015-07-23 16:54:37 -040011114 pf->ioremap_len, err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011115 goto err_ioremap;
11116 }
11117 hw->vendor_id = pdev->vendor;
11118 hw->device_id = pdev->device;
11119 pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
11120 hw->subsystem_vendor_id = pdev->subsystem_vendor;
11121 hw->subsystem_device_id = pdev->subsystem_device;
11122 hw->bus.device = PCI_SLOT(pdev->devfn);
11123 hw->bus.func = PCI_FUNC(pdev->devfn);
Sudheer Mogilappagarib3f028f2017-02-09 23:58:22 -080011124 hw->bus.bus_id = pdev->bus->number;
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +000011125 pf->instance = pfs_found;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011126
Shannon Nelsonde03d2b2016-03-10 14:59:44 -080011127 /* set up the locks for the AQ, do this only once in probe
11128 * and destroy them only once in remove
11129 */
11130 mutex_init(&hw->aq.asq_mutex);
11131 mutex_init(&hw->aq.arq_mutex);
11132
Alexander Duyck5d4ca232016-09-30 08:21:46 -040011133 pf->msg_enable = netif_msg_init(debug,
11134 NETIF_MSG_DRV |
11135 NETIF_MSG_PROBE |
11136 NETIF_MSG_LINK);
11137 if (debug < -1)
11138 pf->hw.debug_mask = debug;
Shannon Nelson5b5faa42014-10-17 03:14:51 +000011139
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +000011140 /* do a special CORER for clearing PXE mode once at init */
11141 if (hw->revision_id == 0 &&
11142 (rd32(hw, I40E_GLLAN_RCTL_0) & I40E_GLLAN_RCTL_0_PXE_MODE_MASK)) {
11143 wr32(hw, I40E_GLGEN_RTRIG, I40E_GLGEN_RTRIG_CORER_MASK);
11144 i40e_flush(hw);
11145 msleep(200);
11146 pf->corer_count++;
11147
11148 i40e_clear_pxe_mode(hw);
11149 }
11150
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011151 /* Reset here to make sure all is clean and to define PF 'n' */
Shannon Nelson838d41d2014-06-04 20:41:27 +000011152 i40e_clear_hw(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011153 err = i40e_pf_reset(hw);
11154 if (err) {
11155 dev_info(&pdev->dev, "Initial pf_reset failed: %d\n", err);
11156 goto err_pf_reset;
11157 }
11158 pf->pfr_count++;
11159
11160 hw->aq.num_arq_entries = I40E_AQ_LEN;
11161 hw->aq.num_asq_entries = I40E_AQ_LEN;
11162 hw->aq.arq_buf_size = I40E_MAX_AQ_BUF_SIZE;
11163 hw->aq.asq_buf_size = I40E_MAX_AQ_BUF_SIZE;
11164 pf->adminq_work_limit = I40E_AQ_WORK_LIMIT;
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +000011165
Carolyn Wybornyb294ac72014-12-11 07:06:39 +000011166 snprintf(pf->int_name, sizeof(pf->int_name) - 1,
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +000011167 "%s-%s:misc",
11168 dev_driver_string(&pf->pdev->dev), dev_name(&pdev->dev));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011169
11170 err = i40e_init_shared_code(hw);
11171 if (err) {
Anjali Singhai Jainb2a75c52015-04-27 14:57:20 -040011172 dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
11173 err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011174 goto err_pf_reset;
11175 }
11176
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +000011177 /* set up a default setting for link flow control */
11178 pf->hw.fc.requested_mode = I40E_FC_NONE;
11179
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011180 err = i40e_init_adminq(hw);
Carolyn Wyborny2b2426a762015-10-26 19:44:35 -040011181 if (err) {
11182 if (err == I40E_ERR_FIRMWARE_API_VERSION)
11183 dev_info(&pdev->dev,
11184 "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");
11185 else
11186 dev_info(&pdev->dev,
11187 "The driver for the device stopped because the device firmware failed to init. Try updating your NVM image.\n");
11188
11189 goto err_pf_reset;
11190 }
Carolyn Wybornyf0b44442015-08-31 19:54:49 -040011191
Shannon Nelson6dec1012015-09-28 14:12:30 -040011192 /* provide nvm, fw, api versions */
11193 dev_info(&pdev->dev, "fw %d.%d.%05d api %d.%d nvm %s\n",
11194 hw->aq.fw_maj_ver, hw->aq.fw_min_ver, hw->aq.fw_build,
11195 hw->aq.api_maj_ver, hw->aq.api_min_ver,
11196 i40e_nvm_version_str(hw));
Carolyn Wybornyf0b44442015-08-31 19:54:49 -040011197
Catherine Sullivan7aa67612014-07-09 07:46:17 +000011198 if (hw->aq.api_maj_ver == I40E_FW_API_VERSION_MAJOR &&
11199 hw->aq.api_min_ver > I40E_FW_API_VERSION_MINOR)
Shannon Nelson278b6f62014-06-04 01:41:03 +000011200 dev_info(&pdev->dev,
Catherine Sullivan7aa67612014-07-09 07:46:17 +000011201 "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");
11202 else if (hw->aq.api_maj_ver < I40E_FW_API_VERSION_MAJOR ||
11203 hw->aq.api_min_ver < (I40E_FW_API_VERSION_MINOR - 1))
Shannon Nelson278b6f62014-06-04 01:41:03 +000011204 dev_info(&pdev->dev,
Catherine Sullivan7aa67612014-07-09 07:46:17 +000011205 "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 +000011206
Shannon Nelson4eb3f762014-03-06 08:59:58 +000011207 i40e_verify_eeprom(pf);
11208
Jesse Brandeburg2c5fe332014-04-23 04:49:57 +000011209 /* Rev 0 hardware was never productized */
11210 if (hw->revision_id < 1)
11211 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");
11212
Shannon Nelson6ff4ef82013-12-21 05:44:49 +000011213 i40e_clear_pxe_mode(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011214 err = i40e_get_capabilities(pf);
11215 if (err)
11216 goto err_adminq_setup;
11217
11218 err = i40e_sw_init(pf);
11219 if (err) {
11220 dev_info(&pdev->dev, "sw_init failed: %d\n", err);
11221 goto err_sw_init;
11222 }
11223
11224 err = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
11225 hw->func_caps.num_rx_qp,
11226 pf->fcoe_hmc_cntx_num, pf->fcoe_hmc_filt_num);
11227 if (err) {
11228 dev_info(&pdev->dev, "init_lan_hmc failed: %d\n", err);
11229 goto err_init_lan_hmc;
11230 }
11231
11232 err = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
11233 if (err) {
11234 dev_info(&pdev->dev, "configure_lan_hmc failed: %d\n", err);
11235 err = -ENOENT;
11236 goto err_configure_lan_hmc;
11237 }
11238
Neerav Parikhb686ece2014-12-14 01:55:11 +000011239 /* Disable LLDP for NICs that have firmware versions lower than v4.3.
11240 * Ignore error return codes because if it was already disabled via
11241 * hardware settings this will fail
11242 */
Neerav Parikhf1bbad32016-01-13 16:51:39 -080011243 if (pf->flags & I40E_FLAG_STOP_FW_LLDP) {
Neerav Parikhb686ece2014-12-14 01:55:11 +000011244 dev_info(&pdev->dev, "Stopping firmware LLDP agent.\n");
11245 i40e_aq_stop_lldp(hw, true, NULL);
11246 }
11247
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011248 i40e_get_mac_addr(hw, hw->mac.addr);
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050011249 /* allow a platform config to override the HW addr */
11250 i40e_get_platform_mac_addr(pdev, pf);
Jesse Brandeburgf62b5062013-11-28 06:39:27 +000011251 if (!is_valid_ether_addr(hw->mac.addr)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011252 dev_info(&pdev->dev, "invalid MAC address %pM\n", hw->mac.addr);
11253 err = -EIO;
11254 goto err_mac_addr;
11255 }
11256 dev_info(&pdev->dev, "MAC address: %pM\n", hw->mac.addr);
Greg Rose9a173902014-05-22 06:32:02 +000011257 ether_addr_copy(hw->mac.perm_addr, hw->mac.addr);
Neerav Parikh1f224ad2014-02-12 01:45:31 +000011258 i40e_get_port_mac_addr(hw, hw->mac.port_addr);
11259 if (is_valid_ether_addr(hw->mac.port_addr))
11260 pf->flags |= I40E_FLAG_PORT_ID_VALID;
Vasu Dev38e00432014-08-01 13:27:03 -070011261#ifdef I40E_FCOE
11262 err = i40e_get_san_mac_addr(hw, hw->mac.san_addr);
11263 if (err)
11264 dev_info(&pdev->dev,
11265 "(non-fatal) SAN MAC retrieval failed: %d\n", err);
11266 if (!is_valid_ether_addr(hw->mac.san_addr)) {
11267 dev_warn(&pdev->dev, "invalid SAN MAC address %pM, falling back to LAN MAC\n",
11268 hw->mac.san_addr);
11269 ether_addr_copy(hw->mac.san_addr, hw->mac.addr);
11270 }
11271 dev_info(&pf->pdev->dev, "SAN MAC: %pM\n", hw->mac.san_addr);
11272#endif /* I40E_FCOE */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011273
11274 pci_set_drvdata(pdev, pf);
11275 pci_save_state(pdev);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -080011276#ifdef CONFIG_I40E_DCB
11277 err = i40e_init_pf_dcb(pf);
11278 if (err) {
Shannon Nelsonaebfc812014-12-11 07:06:38 +000011279 dev_info(&pdev->dev, "DCB init failed %d, disabled\n", err);
David Ertmanc17ef432016-09-30 01:36:21 -070011280 pf->flags &= ~(I40E_FLAG_DCB_CAPABLE | I40E_FLAG_DCB_ENABLED);
Neerav Parikh014269f2014-04-01 07:11:48 +000011281 /* Continue without DCB enabled */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -080011282 }
11283#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011284
11285 /* set up periodic task facility */
11286 setup_timer(&pf->service_timer, i40e_service_timer, (unsigned long)pf);
11287 pf->service_timer_period = HZ;
11288
11289 INIT_WORK(&pf->service_task, i40e_service_task);
11290 clear_bit(__I40E_SERVICE_SCHED, &pf->state);
11291 pf->flags |= I40E_FLAG_NEED_LINK_UPDATE;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011292
Shannon Nelson1d5109d2015-08-26 15:14:08 -040011293 /* NVM bit on means WoL disabled for the port */
11294 i40e_read_nvm_word(hw, I40E_SR_NVM_WAKE_ON_LAN, &wol_nvm_bits);
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -080011295 if (BIT (hw->port) & wol_nvm_bits || hw->partition_id != 1)
Shannon Nelson1d5109d2015-08-26 15:14:08 -040011296 pf->wol_en = false;
11297 else
11298 pf->wol_en = true;
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011299 device_set_wakeup_enable(&pf->pdev->dev, pf->wol_en);
11300
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011301 /* set up the main switch operations */
11302 i40e_determine_queue_usage(pf);
Jesse Brandeburgc11472802015-04-07 19:45:39 -040011303 err = i40e_init_interrupt_scheme(pf);
11304 if (err)
11305 goto err_switch_setup;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011306
Mitch Williams505682c2014-05-20 08:01:37 +000011307 /* The number of VSIs reported by the FW is the minimum guaranteed
11308 * to us; HW supports far more and we share the remaining pool with
11309 * the other PFs. We allocate space for more than the guarantee with
11310 * the understanding that we might not get them all later.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011311 */
Mitch Williams505682c2014-05-20 08:01:37 +000011312 if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
11313 pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
11314 else
11315 pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
11316
11317 /* Set up the *vsi struct and our local tracking of the MAIN PF vsi. */
Jesse Brandeburgd17038d2015-12-23 12:05:55 -080011318 pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
11319 GFP_KERNEL);
Wei Yongjuned87ac02013-09-24 05:17:25 +000011320 if (!pf->vsi) {
11321 err = -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011322 goto err_switch_setup;
Wei Yongjuned87ac02013-09-24 05:17:25 +000011323 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011324
Anjali Singhai Jainfa11cb32015-05-27 12:06:14 -040011325#ifdef CONFIG_PCI_IOV
11326 /* prep for VF support */
11327 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
11328 (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
11329 !test_bit(__I40E_BAD_EEPROM, &pf->state)) {
11330 if (pci_num_vf(pdev))
11331 pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
11332 }
11333#endif
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000011334 err = i40e_setup_pf_switch(pf, false);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011335 if (err) {
11336 dev_info(&pdev->dev, "setup_pf_switch failed: %d\n", err);
11337 goto err_vsis;
11338 }
Helin Zhang58fc3262015-10-01 14:37:38 -040011339
11340 /* Make sure flow control is set according to current settings */
11341 err = i40e_set_fc(hw, &set_fc_aq_fail, true);
11342 if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_GET)
11343 dev_dbg(&pf->pdev->dev,
11344 "Set fc with err %s aq_err %s on get_phy_cap\n",
11345 i40e_stat_str(hw, err),
11346 i40e_aq_str(hw, hw->aq.asq_last_status));
11347 if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_SET)
11348 dev_dbg(&pf->pdev->dev,
11349 "Set fc with err %s aq_err %s on set_phy_config\n",
11350 i40e_stat_str(hw, err),
11351 i40e_aq_str(hw, hw->aq.asq_last_status));
11352 if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_UPDATE)
11353 dev_dbg(&pf->pdev->dev,
11354 "Set fc with err %s aq_err %s on get_link_info\n",
11355 i40e_stat_str(hw, err),
11356 i40e_aq_str(hw, hw->aq.asq_last_status));
11357
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +000011358 /* if FDIR VSI was set up, start it now */
Mitch Williams505682c2014-05-20 08:01:37 +000011359 for (i = 0; i < pf->num_alloc_vsi; i++) {
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +000011360 if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
11361 i40e_vsi_open(pf->vsi[i]);
11362 break;
11363 }
11364 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011365
Shannon Nelson2f0aff42016-01-04 10:33:08 -080011366 /* The driver only wants link up/down and module qualification
11367 * reports from firmware. Note the negative logic.
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +000011368 */
11369 err = i40e_aq_set_phy_int_mask(&pf->hw,
Shannon Nelson2f0aff42016-01-04 10:33:08 -080011370 ~(I40E_AQ_EVENT_LINK_UPDOWN |
Shannon Nelson867a79e2016-03-18 12:18:15 -070011371 I40E_AQ_EVENT_MEDIA_NA |
Shannon Nelson2f0aff42016-01-04 10:33:08 -080011372 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +000011373 if (err)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040011374 dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
11375 i40e_stat_str(&pf->hw, err),
11376 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +000011377
Anjali Singhai Jain4f2f017c2015-10-21 19:47:07 -040011378 /* Reconfigure hardware for allowing smaller MSS in the case
11379 * of TSO, so that we avoid the MDD being fired and causing
11380 * a reset in the case of small MSS+TSO.
11381 */
11382 val = rd32(hw, I40E_REG_MSS);
11383 if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
11384 val &= ~I40E_REG_MSS_MIN_MASK;
11385 val |= I40E_64BYTE_MSS;
11386 wr32(hw, I40E_REG_MSS, val);
11387 }
11388
Anjali Singhai Jain8eed76f2015-12-09 15:50:31 -080011389 if (pf->flags & I40E_FLAG_RESTART_AUTONEG) {
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +000011390 msleep(75);
11391 err = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
11392 if (err)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040011393 dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
11394 i40e_stat_str(&pf->hw, err),
11395 i40e_aq_str(&pf->hw,
11396 pf->hw.aq.asq_last_status));
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +000011397 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011398 /* The main driver is (mostly) up and happy. We need to set this state
11399 * before setting up the misc vector or we get a race and the vector
11400 * ends up disabled forever.
11401 */
11402 clear_bit(__I40E_DOWN, &pf->state);
11403
11404 /* In case of MSIX we are going to setup the misc vector right here
11405 * to handle admin queue events etc. In case of legacy and MSI
11406 * the misc functionality and queue processing is combined in
11407 * the same vector and that gets setup at open.
11408 */
11409 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
11410 err = i40e_setup_misc_vector(pf);
11411 if (err) {
11412 dev_info(&pdev->dev,
11413 "setup of misc vector failed: %d\n", err);
11414 goto err_vsis;
11415 }
11416 }
11417
Greg Rosedf805f62014-04-04 04:43:16 +000011418#ifdef CONFIG_PCI_IOV
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011419 /* prep for VF support */
11420 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
Shannon Nelson4eb3f762014-03-06 08:59:58 +000011421 (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
11422 !test_bit(__I40E_BAD_EEPROM, &pf->state)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011423 /* disable link interrupts for VFs */
11424 val = rd32(hw, I40E_PFGEN_PORTMDIO_NUM);
11425 val &= ~I40E_PFGEN_PORTMDIO_NUM_VFLINK_STAT_ENA_MASK;
11426 wr32(hw, I40E_PFGEN_PORTMDIO_NUM, val);
11427 i40e_flush(hw);
Mitch Williams4aeec012014-02-13 03:48:47 -080011428
11429 if (pci_num_vf(pdev)) {
11430 dev_info(&pdev->dev,
11431 "Active VFs found, allocating resources.\n");
11432 err = i40e_alloc_vfs(pf, pci_num_vf(pdev));
11433 if (err)
11434 dev_info(&pdev->dev,
11435 "Error %d allocating resources for existing VFs\n",
11436 err);
11437 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011438 }
Greg Rosedf805f62014-04-04 04:43:16 +000011439#endif /* CONFIG_PCI_IOV */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011440
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060011441 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11442 pf->iwarp_base_vector = i40e_get_lump(pf, pf->irq_pile,
11443 pf->num_iwarp_msix,
11444 I40E_IWARP_IRQ_PILE_ID);
11445 if (pf->iwarp_base_vector < 0) {
11446 dev_info(&pdev->dev,
11447 "failed to get tracking for %d vectors for IWARP err=%d\n",
11448 pf->num_iwarp_msix, pf->iwarp_base_vector);
11449 pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
11450 }
11451 }
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +000011452
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011453 i40e_dbg_pf_init(pf);
11454
11455 /* tell the firmware that we're starting */
Jesse Brandeburg44033fa2014-04-23 04:50:15 +000011456 i40e_send_version(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011457
11458 /* since everything's happy, start the service_task timer */
11459 mod_timer(&pf->service_timer,
11460 round_jiffies(jiffies + pf->service_timer_period));
11461
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060011462 /* add this PF to client device list and launch a client service task */
11463 err = i40e_lan_add_device(pf);
11464 if (err)
11465 dev_info(&pdev->dev, "Failed to add PF to client API service list: %d\n",
11466 err);
11467
Vasu Dev38e00432014-08-01 13:27:03 -070011468#ifdef I40E_FCOE
11469 /* create FCoE interface */
11470 i40e_fcoe_vsi_setup(pf);
11471
11472#endif
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040011473#define PCI_SPEED_SIZE 8
11474#define PCI_WIDTH_SIZE 8
11475 /* Devices on the IOSF bus do not have this information
11476 * and will report PCI Gen 1 x 1 by default so don't bother
11477 * checking them.
11478 */
11479 if (!(pf->flags & I40E_FLAG_NO_PCI_LINK_CHECK)) {
11480 char speed[PCI_SPEED_SIZE] = "Unknown";
11481 char width[PCI_WIDTH_SIZE] = "Unknown";
Catherine Sullivand4dfb812013-11-28 06:39:21 +000011482
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040011483 /* Get the negotiated link width and speed from PCI config
11484 * space
11485 */
11486 pcie_capability_read_word(pf->pdev, PCI_EXP_LNKSTA,
11487 &link_status);
Catherine Sullivand4dfb812013-11-28 06:39:21 +000011488
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040011489 i40e_set_pci_config_data(hw, link_status);
Catherine Sullivand4dfb812013-11-28 06:39:21 +000011490
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040011491 switch (hw->bus.speed) {
11492 case i40e_bus_speed_8000:
11493 strncpy(speed, "8.0", PCI_SPEED_SIZE); break;
11494 case i40e_bus_speed_5000:
11495 strncpy(speed, "5.0", PCI_SPEED_SIZE); break;
11496 case i40e_bus_speed_2500:
11497 strncpy(speed, "2.5", PCI_SPEED_SIZE); break;
11498 default:
11499 break;
11500 }
11501 switch (hw->bus.width) {
11502 case i40e_bus_width_pcie_x8:
11503 strncpy(width, "8", PCI_WIDTH_SIZE); break;
11504 case i40e_bus_width_pcie_x4:
11505 strncpy(width, "4", PCI_WIDTH_SIZE); break;
11506 case i40e_bus_width_pcie_x2:
11507 strncpy(width, "2", PCI_WIDTH_SIZE); break;
11508 case i40e_bus_width_pcie_x1:
11509 strncpy(width, "1", PCI_WIDTH_SIZE); break;
11510 default:
11511 break;
11512 }
11513
11514 dev_info(&pdev->dev, "PCI-Express: Speed %sGT/s Width x%s\n",
11515 speed, width);
11516
11517 if (hw->bus.width < i40e_bus_width_pcie_x8 ||
11518 hw->bus.speed < i40e_bus_speed_8000) {
11519 dev_warn(&pdev->dev, "PCI-Express bandwidth available for this device may be insufficient for optimal performance.\n");
11520 dev_warn(&pdev->dev, "Please move the device to a different PCI-e link with more lanes and/or higher transfer rate.\n");
11521 }
Catherine Sullivand4dfb812013-11-28 06:39:21 +000011522 }
11523
Catherine Sullivane8278452015-02-06 08:52:08 +000011524 /* get the requested speeds from the fw */
11525 err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities, NULL);
11526 if (err)
Neerav Parikh8279e492015-09-03 17:18:50 -040011527 dev_dbg(&pf->pdev->dev, "get requested speeds ret = %s last_status = %s\n",
11528 i40e_stat_str(&pf->hw, err),
11529 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Catherine Sullivane8278452015-02-06 08:52:08 +000011530 pf->hw.phy.link_info.requested_speeds = abilities.link_speed;
11531
Catherine Sullivanfc72dbc2015-09-01 11:36:30 -040011532 /* get the supported phy types from the fw */
11533 err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities, NULL);
11534 if (err)
11535 dev_dbg(&pf->pdev->dev, "get supported phy types ret = %s last_status = %s\n",
11536 i40e_stat_str(&pf->hw, err),
11537 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Catherine Sullivanfc72dbc2015-09-01 11:36:30 -040011538
Anjali Singhai Jaine7358f52015-10-01 14:37:34 -040011539 /* Add a filter to drop all Flow control frames from any VSI from being
11540 * transmitted. By doing so we stop a malicious VF from sending out
11541 * PAUSE or PFC frames and potentially controlling traffic for other
11542 * PF/VF VSIs.
11543 * The FW can still send Flow control frames if enabled.
11544 */
11545 i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
11546 pf->main_vsi_seid);
11547
Carolyn Wyborny31b606d2016-02-17 16:12:12 -080011548 if ((pf->hw.device_id == I40E_DEV_ID_10G_BASE_T) ||
Henry Tieman4f9b4302016-11-08 13:05:18 -080011549 (pf->hw.device_id == I40E_DEV_ID_10G_BASE_T4))
11550 pf->flags |= I40E_FLAG_PHY_CONTROLS_LEDS;
Harshitha Ramamurthy4ad9f4f2016-11-08 13:05:09 -080011551 if (pf->hw.device_id == I40E_DEV_ID_SFP_I_X722)
11552 pf->flags |= I40E_FLAG_HAVE_CRT_RETIMER;
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000011553 /* print a string summarizing features */
11554 i40e_print_features(pf);
11555
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011556 return 0;
11557
11558 /* Unwind what we've done if something failed in the setup */
11559err_vsis:
11560 set_bit(__I40E_DOWN, &pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011561 i40e_clear_interrupt_scheme(pf);
11562 kfree(pf->vsi);
Shannon Nelson04b03012013-11-28 06:39:34 +000011563err_switch_setup:
11564 i40e_reset_interrupt_capability(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011565 del_timer_sync(&pf->service_timer);
11566err_mac_addr:
11567err_configure_lan_hmc:
11568 (void)i40e_shutdown_lan_hmc(hw);
11569err_init_lan_hmc:
11570 kfree(pf->qp_pile);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011571err_sw_init:
11572err_adminq_setup:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011573err_pf_reset:
11574 iounmap(hw->hw_addr);
11575err_ioremap:
11576 kfree(pf);
11577err_pf_alloc:
11578 pci_disable_pcie_error_reporting(pdev);
Johannes Thumshirn56d766d2016-06-07 09:44:05 +020011579 pci_release_mem_regions(pdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011580err_pci_reg:
11581err_dma:
11582 pci_disable_device(pdev);
11583 return err;
11584}
11585
11586/**
11587 * i40e_remove - Device removal routine
11588 * @pdev: PCI device information struct
11589 *
11590 * i40e_remove is called by the PCI subsystem to alert the driver
11591 * that is should release a PCI device. This could be caused by a
11592 * Hot-Plug event, or because the driver is going to be removed from
11593 * memory.
11594 **/
11595static void i40e_remove(struct pci_dev *pdev)
11596{
11597 struct i40e_pf *pf = pci_get_drvdata(pdev);
Carolyn Wybornybcab2db2015-09-28 14:16:55 -040011598 struct i40e_hw *hw = &pf->hw;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011599 i40e_status ret_code;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011600 int i;
11601
11602 i40e_dbg_pf_exit(pf);
11603
Jacob Kellerbeb0dff2014-01-11 05:43:19 +000011604 i40e_ptp_stop(pf);
11605
Carolyn Wybornybcab2db2015-09-28 14:16:55 -040011606 /* Disable RSS in hw */
Shannon Nelson272cdaf22016-02-17 16:12:21 -080011607 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), 0);
11608 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), 0);
Carolyn Wybornybcab2db2015-09-28 14:16:55 -040011609
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011610 /* no more scheduling of any task */
Pandi Kumar Maharajana4618ec2016-02-18 09:19:25 -080011611 set_bit(__I40E_SUSPENDED, &pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011612 set_bit(__I40E_DOWN, &pf->state);
Shannon Nelsonc99abb42016-03-10 14:59:45 -080011613 if (pf->service_timer.data)
11614 del_timer_sync(&pf->service_timer);
11615 if (pf->service_task.func)
11616 cancel_work_sync(&pf->service_task);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011617
Mitch Williamseb2d80b2014-02-13 03:48:48 -080011618 if (pf->flags & I40E_FLAG_SRIOV_ENABLED) {
11619 i40e_free_vfs(pf);
11620 pf->flags &= ~I40E_FLAG_SRIOV_ENABLED;
11621 }
11622
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011623 i40e_fdir_teardown(pf);
11624
11625 /* If there is a switch structure or any orphans, remove them.
11626 * This will leave only the PF's VSI remaining.
11627 */
11628 for (i = 0; i < I40E_MAX_VEB; i++) {
11629 if (!pf->veb[i])
11630 continue;
11631
11632 if (pf->veb[i]->uplink_seid == pf->mac_seid ||
11633 pf->veb[i]->uplink_seid == 0)
11634 i40e_switch_branch_release(pf->veb[i]);
11635 }
11636
11637 /* Now we can shutdown the PF's VSI, just before we kill
11638 * adminq and hmc.
11639 */
11640 if (pf->vsi[pf->lan_vsi])
11641 i40e_vsi_release(pf->vsi[pf->lan_vsi]);
11642
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060011643 /* remove attached clients */
11644 ret_code = i40e_lan_del_device(pf);
11645 if (ret_code) {
11646 dev_warn(&pdev->dev, "Failed to delete client device: %d\n",
11647 ret_code);
11648 }
11649
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011650 /* shutdown and destroy the HMC */
Jesse Brandeburgf734dff2016-01-15 14:33:11 -080011651 if (hw->hmc.hmc_obj) {
11652 ret_code = i40e_shutdown_lan_hmc(hw);
Shannon Nelson60442de2014-04-23 04:50:13 +000011653 if (ret_code)
11654 dev_warn(&pdev->dev,
11655 "Failed to destroy the HMC resources: %d\n",
11656 ret_code);
11657 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011658
11659 /* shutdown the adminq */
Henry Tiemanac9c5c62016-09-06 18:05:11 -070011660 i40e_shutdown_adminq(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011661
Jesse Brandeburg8ddb3322015-11-18 15:47:06 -080011662 /* destroy the locks only once, here */
11663 mutex_destroy(&hw->aq.arq_mutex);
11664 mutex_destroy(&hw->aq.asq_mutex);
11665
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011666 /* Clear all dynamic memory lists of rings, q_vectors, and VSIs */
11667 i40e_clear_interrupt_scheme(pf);
Mitch Williams505682c2014-05-20 08:01:37 +000011668 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011669 if (pf->vsi[i]) {
11670 i40e_vsi_clear_rings(pf->vsi[i]);
11671 i40e_vsi_clear(pf->vsi[i]);
11672 pf->vsi[i] = NULL;
11673 }
11674 }
11675
11676 for (i = 0; i < I40E_MAX_VEB; i++) {
11677 kfree(pf->veb[i]);
11678 pf->veb[i] = NULL;
11679 }
11680
11681 kfree(pf->qp_pile);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011682 kfree(pf->vsi);
11683
Jesse Brandeburgf734dff2016-01-15 14:33:11 -080011684 iounmap(hw->hw_addr);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011685 kfree(pf);
Johannes Thumshirn56d766d2016-06-07 09:44:05 +020011686 pci_release_mem_regions(pdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011687
11688 pci_disable_pcie_error_reporting(pdev);
11689 pci_disable_device(pdev);
11690}
11691
11692/**
11693 * i40e_pci_error_detected - warning that something funky happened in PCI land
11694 * @pdev: PCI device information struct
11695 *
11696 * Called to warn that something happened and the error handling steps
11697 * are in progress. Allows the driver to quiesce things, be ready for
11698 * remediation.
11699 **/
11700static pci_ers_result_t i40e_pci_error_detected(struct pci_dev *pdev,
11701 enum pci_channel_state error)
11702{
11703 struct i40e_pf *pf = pci_get_drvdata(pdev);
11704
11705 dev_info(&pdev->dev, "%s: error %d\n", __func__, error);
11706
Guilherme G Piccoliedfc23ee2016-10-03 00:31:12 -070011707 if (!pf) {
11708 dev_info(&pdev->dev,
11709 "Cannot recover - error happened during device probe\n");
11710 return PCI_ERS_RESULT_DISCONNECT;
11711 }
11712
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011713 /* shutdown all operations */
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011714 if (!test_bit(__I40E_SUSPENDED, &pf->state)) {
11715 rtnl_lock();
11716 i40e_prep_for_reset(pf);
11717 rtnl_unlock();
11718 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011719
11720 /* Request a slot reset */
11721 return PCI_ERS_RESULT_NEED_RESET;
11722}
11723
11724/**
11725 * i40e_pci_error_slot_reset - a PCI slot reset just happened
11726 * @pdev: PCI device information struct
11727 *
11728 * Called to find if the driver can work with the device now that
11729 * the pci slot has been reset. If a basic connection seems good
11730 * (registers are readable and have sane content) then return a
11731 * happy little PCI_ERS_RESULT_xxx.
11732 **/
11733static pci_ers_result_t i40e_pci_error_slot_reset(struct pci_dev *pdev)
11734{
11735 struct i40e_pf *pf = pci_get_drvdata(pdev);
11736 pci_ers_result_t result;
11737 int err;
11738 u32 reg;
11739
Shannon Nelsonfb43201f2015-08-26 15:14:17 -040011740 dev_dbg(&pdev->dev, "%s\n", __func__);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011741 if (pci_enable_device_mem(pdev)) {
11742 dev_info(&pdev->dev,
11743 "Cannot re-enable PCI device after reset.\n");
11744 result = PCI_ERS_RESULT_DISCONNECT;
11745 } else {
11746 pci_set_master(pdev);
11747 pci_restore_state(pdev);
11748 pci_save_state(pdev);
11749 pci_wake_from_d3(pdev, false);
11750
11751 reg = rd32(&pf->hw, I40E_GLGEN_RTRIG);
11752 if (reg == 0)
11753 result = PCI_ERS_RESULT_RECOVERED;
11754 else
11755 result = PCI_ERS_RESULT_DISCONNECT;
11756 }
11757
11758 err = pci_cleanup_aer_uncorrect_error_status(pdev);
11759 if (err) {
11760 dev_info(&pdev->dev,
11761 "pci_cleanup_aer_uncorrect_error_status failed 0x%0x\n",
11762 err);
11763 /* non-fatal, continue */
11764 }
11765
11766 return result;
11767}
11768
11769/**
11770 * i40e_pci_error_resume - restart operations after PCI error recovery
11771 * @pdev: PCI device information struct
11772 *
11773 * Called to allow the driver to bring things back up after PCI error
11774 * and/or reset recovery has finished.
11775 **/
11776static void i40e_pci_error_resume(struct pci_dev *pdev)
11777{
11778 struct i40e_pf *pf = pci_get_drvdata(pdev);
11779
Shannon Nelsonfb43201f2015-08-26 15:14:17 -040011780 dev_dbg(&pdev->dev, "%s\n", __func__);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011781 if (test_bit(__I40E_SUSPENDED, &pf->state))
11782 return;
11783
11784 rtnl_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011785 i40e_handle_reset_warning(pf);
Vasily Averin4c4935a2015-07-08 15:04:26 +030011786 rtnl_unlock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011787}
11788
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011789/**
Joshua Hay1d680052016-12-12 15:44:08 -080011790 * i40e_enable_mc_magic_wake - enable multicast magic packet wake up
11791 * using the mac_address_write admin q function
11792 * @pf: pointer to i40e_pf struct
11793 **/
11794static void i40e_enable_mc_magic_wake(struct i40e_pf *pf)
11795{
11796 struct i40e_hw *hw = &pf->hw;
11797 i40e_status ret;
11798 u8 mac_addr[6];
11799 u16 flags = 0;
11800
11801 /* Get current MAC address in case it's an LAA */
11802 if (pf->vsi[pf->lan_vsi] && pf->vsi[pf->lan_vsi]->netdev) {
11803 ether_addr_copy(mac_addr,
11804 pf->vsi[pf->lan_vsi]->netdev->dev_addr);
11805 } else {
11806 dev_err(&pf->pdev->dev,
11807 "Failed to retrieve MAC address; using default\n");
11808 ether_addr_copy(mac_addr, hw->mac.addr);
11809 }
11810
11811 /* The FW expects the mac address write cmd to first be called with
11812 * one of these flags before calling it again with the multicast
11813 * enable flags.
11814 */
11815 flags = I40E_AQC_WRITE_TYPE_LAA_WOL;
11816
11817 if (hw->func_caps.flex10_enable && hw->partition_id != 1)
11818 flags = I40E_AQC_WRITE_TYPE_LAA_ONLY;
11819
11820 ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
11821 if (ret) {
11822 dev_err(&pf->pdev->dev,
11823 "Failed to update MAC address registers; cannot enable Multicast Magic packet wake up");
11824 return;
11825 }
11826
11827 flags = I40E_AQC_MC_MAG_EN
11828 | I40E_AQC_WOL_PRESERVE_ON_PFR
11829 | I40E_AQC_WRITE_TYPE_UPDATE_MC_MAG;
11830 ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
11831 if (ret)
11832 dev_err(&pf->pdev->dev,
11833 "Failed to enable Multicast Magic Packet wake up\n");
11834}
11835
11836/**
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011837 * i40e_shutdown - PCI callback for shutting down
11838 * @pdev: PCI device information struct
11839 **/
11840static void i40e_shutdown(struct pci_dev *pdev)
11841{
11842 struct i40e_pf *pf = pci_get_drvdata(pdev);
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011843 struct i40e_hw *hw = &pf->hw;
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011844
11845 set_bit(__I40E_SUSPENDED, &pf->state);
11846 set_bit(__I40E_DOWN, &pf->state);
11847 rtnl_lock();
11848 i40e_prep_for_reset(pf);
11849 rtnl_unlock();
11850
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011851 wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
11852 wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
11853
Catherine Sullivan02b42492015-07-10 19:35:59 -040011854 del_timer_sync(&pf->service_timer);
11855 cancel_work_sync(&pf->service_task);
11856 i40e_fdir_teardown(pf);
11857
Joshua Hay1d680052016-12-12 15:44:08 -080011858 if (pf->wol_en && (pf->flags & I40E_FLAG_WOL_MC_MAGIC_PKT_WAKE))
11859 i40e_enable_mc_magic_wake(pf);
11860
Catherine Sullivan02b42492015-07-10 19:35:59 -040011861 rtnl_lock();
11862 i40e_prep_for_reset(pf);
11863 rtnl_unlock();
11864
11865 wr32(hw, I40E_PFPM_APM,
11866 (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
11867 wr32(hw, I40E_PFPM_WUFC,
11868 (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
11869
Shannon Nelsone1477582015-02-21 06:44:33 +000011870 i40e_clear_interrupt_scheme(pf);
11871
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011872 if (system_state == SYSTEM_POWER_OFF) {
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011873 pci_wake_from_d3(pdev, pf->wol_en);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011874 pci_set_power_state(pdev, PCI_D3hot);
11875 }
11876}
11877
11878#ifdef CONFIG_PM
11879/**
11880 * i40e_suspend - PCI callback for moving to D3
11881 * @pdev: PCI device information struct
11882 **/
11883static int i40e_suspend(struct pci_dev *pdev, pm_message_t state)
11884{
11885 struct i40e_pf *pf = pci_get_drvdata(pdev);
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011886 struct i40e_hw *hw = &pf->hw;
Greg Rose059ff692016-05-16 10:26:38 -070011887 int retval = 0;
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011888
11889 set_bit(__I40E_SUSPENDED, &pf->state);
11890 set_bit(__I40E_DOWN, &pf->state);
Mitch Williams3932dbf2015-03-31 00:45:04 -070011891
Joshua Hay1d680052016-12-12 15:44:08 -080011892 if (pf->wol_en && (pf->flags & I40E_FLAG_WOL_MC_MAGIC_PKT_WAKE))
11893 i40e_enable_mc_magic_wake(pf);
11894
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011895 rtnl_lock();
11896 i40e_prep_for_reset(pf);
11897 rtnl_unlock();
11898
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011899 wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
11900 wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
11901
Greg Roseb33d3b72016-05-16 10:26:37 -070011902 i40e_stop_misc_vector(pf);
11903
Greg Rose059ff692016-05-16 10:26:38 -070011904 retval = pci_save_state(pdev);
11905 if (retval)
11906 return retval;
11907
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011908 pci_wake_from_d3(pdev, pf->wol_en);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011909 pci_set_power_state(pdev, PCI_D3hot);
11910
Greg Rose059ff692016-05-16 10:26:38 -070011911 return retval;
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011912}
11913
11914/**
11915 * i40e_resume - PCI callback for waking up from D3
11916 * @pdev: PCI device information struct
11917 **/
11918static int i40e_resume(struct pci_dev *pdev)
11919{
11920 struct i40e_pf *pf = pci_get_drvdata(pdev);
11921 u32 err;
11922
11923 pci_set_power_state(pdev, PCI_D0);
11924 pci_restore_state(pdev);
11925 /* pci_restore_state() clears dev->state_saves, so
11926 * call pci_save_state() again to restore it.
11927 */
11928 pci_save_state(pdev);
11929
11930 err = pci_enable_device_mem(pdev);
11931 if (err) {
Shannon Nelsonfb43201f2015-08-26 15:14:17 -040011932 dev_err(&pdev->dev, "Cannot enable PCI device from suspend\n");
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011933 return err;
11934 }
11935 pci_set_master(pdev);
11936
11937 /* no wakeup events while running */
11938 pci_wake_from_d3(pdev, false);
11939
11940 /* handling the reset will rebuild the device state */
11941 if (test_and_clear_bit(__I40E_SUSPENDED, &pf->state)) {
11942 clear_bit(__I40E_DOWN, &pf->state);
11943 rtnl_lock();
11944 i40e_reset_and_rebuild(pf, false);
11945 rtnl_unlock();
11946 }
11947
11948 return 0;
11949}
11950
11951#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011952static const struct pci_error_handlers i40e_err_handler = {
11953 .error_detected = i40e_pci_error_detected,
11954 .slot_reset = i40e_pci_error_slot_reset,
11955 .resume = i40e_pci_error_resume,
11956};
11957
11958static struct pci_driver i40e_driver = {
11959 .name = i40e_driver_name,
11960 .id_table = i40e_pci_tbl,
11961 .probe = i40e_probe,
11962 .remove = i40e_remove,
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011963#ifdef CONFIG_PM
11964 .suspend = i40e_suspend,
11965 .resume = i40e_resume,
11966#endif
11967 .shutdown = i40e_shutdown,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011968 .err_handler = &i40e_err_handler,
11969 .sriov_configure = i40e_pci_sriov_configure,
11970};
11971
11972/**
11973 * i40e_init_module - Driver registration routine
11974 *
11975 * i40e_init_module is the first routine called when the driver is
11976 * loaded. All it does is register with the PCI subsystem.
11977 **/
11978static int __init i40e_init_module(void)
11979{
11980 pr_info("%s: %s - version %s\n", i40e_driver_name,
11981 i40e_driver_string, i40e_driver_version_str);
11982 pr_info("%s: %s\n", i40e_driver_name, i40e_copyright);
Greg Rose96664482015-02-06 08:52:13 +000011983
Jesse Brandeburg2803b162015-12-22 14:25:08 -080011984 /* we will see if single thread per module is enough for now,
11985 * it can't be any worse than using the system workqueue which
11986 * was already single threaded
11987 */
Jacob Keller6992a6c2016-08-04 11:37:01 -070011988 i40e_wq = alloc_workqueue("%s", WQ_UNBOUND | WQ_MEM_RECLAIM, 1,
11989 i40e_driver_name);
Jesse Brandeburg2803b162015-12-22 14:25:08 -080011990 if (!i40e_wq) {
11991 pr_err("%s: Failed to create workqueue\n", i40e_driver_name);
11992 return -ENOMEM;
11993 }
11994
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011995 i40e_dbg_init();
11996 return pci_register_driver(&i40e_driver);
11997}
11998module_init(i40e_init_module);
11999
12000/**
12001 * i40e_exit_module - Driver exit cleanup routine
12002 *
12003 * i40e_exit_module is called just before the driver is removed
12004 * from memory.
12005 **/
12006static void __exit i40e_exit_module(void)
12007{
12008 pci_unregister_driver(&i40e_driver);
Jesse Brandeburg2803b162015-12-22 14:25:08 -080012009 destroy_workqueue(i40e_wq);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012010 i40e_dbg_exit();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012011}
12012module_exit(i40e_exit_module);