blob: b0ccd3c2eec6582de4e3652c5bb4796699b8b312 [file] [log] [blame]
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001/*******************************************************************************
2 *
3 * Intel Ethernet Controller XL710 Family Linux Driver
Shannon Nelson4fc8c672017-06-07 05:43:08 -04004 * Copyright(c) 2013 - 2017 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>
Björn Töpel0c8493d2017-05-24 07:55:34 +020030#include <linux/bpf.h>
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050031
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000032/* Local includes */
33#include "i40e.h"
Shannon Nelson4eb3f762014-03-06 08:59:58 +000034#include "i40e_diag.h"
Alexander Duyck06a5f7f2016-06-16 12:22:06 -070035#include <net/udp_tunnel.h>
Scott Petersoned0980c2017-04-13 04:45:44 -040036/* All i40e tracepoints are defined by the include below, which
37 * must be included exactly once across the whole kernel with
38 * CREATE_TRACE_POINTS defined
39 */
40#define CREATE_TRACE_POINTS
41#include "i40e_trace.h"
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000042
43const char i40e_driver_name[] = "i40e";
44static const char i40e_driver_string[] =
45 "Intel(R) Ethernet Connection XL710 Network Driver";
46
47#define DRV_KERN "-k"
48
Bimmy Pujari15990832017-01-30 12:29:37 -080049#define DRV_VERSION_MAJOR 2
50#define DRV_VERSION_MINOR 1
Jacob Keller2318b402017-04-19 09:25:53 -040051#define DRV_VERSION_BUILD 14
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000052#define DRV_VERSION __stringify(DRV_VERSION_MAJOR) "." \
53 __stringify(DRV_VERSION_MINOR) "." \
54 __stringify(DRV_VERSION_BUILD) DRV_KERN
55const char i40e_driver_version_str[] = DRV_VERSION;
Jesse Brandeburg8fb905b2014-01-17 15:36:33 -080056static const char i40e_copyright[] = "Copyright (c) 2013 - 2014 Intel Corporation.";
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000057
58/* a bit of forward declarations */
59static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi);
Maciej Sosin373149f2017-04-05 07:50:55 -040060static void i40e_handle_reset_warning(struct i40e_pf *pf, bool lock_acquired);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000061static int i40e_add_vsi(struct i40e_vsi *vsi);
62static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi);
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000063static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000064static int i40e_setup_misc_vector(struct i40e_pf *pf);
65static void i40e_determine_queue_usage(struct i40e_pf *pf);
66static int i40e_setup_pf_filter_control(struct i40e_pf *pf);
Maciej Sosin373149f2017-04-05 07:50:55 -040067static void i40e_prep_for_reset(struct i40e_pf *pf, bool lock_acquired);
68static int i40e_reset(struct i40e_pf *pf);
69static void i40e_rebuild(struct i40e_pf *pf, bool reinit, bool lock_acquired);
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080070static void i40e_fdir_sb_setup(struct i40e_pf *pf);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -080071static int i40e_veb_get_bw_info(struct i40e_veb *veb);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000072
73/* i40e_pci_tbl - PCI Device ID Table
74 *
75 * Last entry must be all 0s
76 *
77 * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
78 * Class, Class Mask, private data (not used) }
79 */
Benoit Taine9baa3c32014-08-08 15:56:03 +020080static const struct pci_device_id i40e_pci_tbl[] = {
Shannon Nelsonab600852014-01-17 15:36:39 -080081 {PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_XL710), 0},
Shannon Nelsonab600852014-01-17 15:36:39 -080082 {PCI_VDEVICE(INTEL, I40E_DEV_ID_QEMU), 0},
Shannon Nelsonab600852014-01-17 15:36:39 -080083 {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_B), 0},
84 {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_C), 0},
Shannon Nelsonab600852014-01-17 15:36:39 -080085 {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_A), 0},
86 {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_B), 0},
87 {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_C), 0},
Mitch Williams5960d332014-09-13 07:40:47 +000088 {PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T), 0},
Shannon Nelsonbc5166b92015-08-26 15:14:10 -040089 {PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T4), 0},
Anjali Singhai Jain35dae512015-12-22 14:25:03 -080090 {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_X722), 0},
91 {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_X722), 0},
Anjali Singhai Jain87e6c1d2015-06-05 12:20:25 -040092 {PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_X722), 0},
93 {PCI_VDEVICE(INTEL, I40E_DEV_ID_1G_BASE_T_X722), 0},
94 {PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T_X722), 0},
Catherine Sullivand6bf58c2016-03-18 12:18:08 -070095 {PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_I_X722), 0},
Shannon Nelson48a3b512015-07-23 16:54:39 -040096 {PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2), 0},
97 {PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2_A), 0},
Carolyn Wyborny31232372016-11-21 13:03:48 -080098 {PCI_VDEVICE(INTEL, I40E_DEV_ID_25G_B), 0},
99 {PCI_VDEVICE(INTEL, I40E_DEV_ID_25G_SFP28), 0},
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000100 /* required last entry */
101 {0, }
102};
103MODULE_DEVICE_TABLE(pci, i40e_pci_tbl);
104
105#define I40E_MAX_VF_COUNT 128
106static int debug = -1;
Alexander Duyck5d4ca232016-09-30 08:21:46 -0400107module_param(debug, uint, 0);
108MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all), Debug mask (0x8XXXXXXX)");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000109
110MODULE_AUTHOR("Intel Corporation, <e1000-devel@lists.sourceforge.net>");
111MODULE_DESCRIPTION("Intel(R) Ethernet Connection XL710 Network Driver");
112MODULE_LICENSE("GPL");
113MODULE_VERSION(DRV_VERSION);
114
Jesse Brandeburg2803b162015-12-22 14:25:08 -0800115static struct workqueue_struct *i40e_wq;
116
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000117/**
118 * i40e_allocate_dma_mem_d - OS specific memory alloc for shared code
119 * @hw: pointer to the HW structure
120 * @mem: ptr to mem struct to fill out
121 * @size: size of memory requested
122 * @alignment: what to align the allocation to
123 **/
124int i40e_allocate_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem,
125 u64 size, u32 alignment)
126{
127 struct i40e_pf *pf = (struct i40e_pf *)hw->back;
128
129 mem->size = ALIGN(size, alignment);
130 mem->va = dma_zalloc_coherent(&pf->pdev->dev, mem->size,
131 &mem->pa, GFP_KERNEL);
Jesse Brandeburg93bc73b2013-09-13 08:23:18 +0000132 if (!mem->va)
133 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000134
Jesse Brandeburg93bc73b2013-09-13 08:23:18 +0000135 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000136}
137
138/**
139 * i40e_free_dma_mem_d - OS specific memory free for shared code
140 * @hw: pointer to the HW structure
141 * @mem: ptr to mem struct to free
142 **/
143int i40e_free_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem)
144{
145 struct i40e_pf *pf = (struct i40e_pf *)hw->back;
146
147 dma_free_coherent(&pf->pdev->dev, mem->size, mem->va, mem->pa);
148 mem->va = NULL;
149 mem->pa = 0;
150 mem->size = 0;
151
152 return 0;
153}
154
155/**
156 * i40e_allocate_virt_mem_d - OS specific memory alloc for shared code
157 * @hw: pointer to the HW structure
158 * @mem: ptr to mem struct to fill out
159 * @size: size of memory requested
160 **/
161int i40e_allocate_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem,
162 u32 size)
163{
164 mem->size = size;
165 mem->va = kzalloc(size, GFP_KERNEL);
166
Jesse Brandeburg93bc73b2013-09-13 08:23:18 +0000167 if (!mem->va)
168 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000169
Jesse Brandeburg93bc73b2013-09-13 08:23:18 +0000170 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000171}
172
173/**
174 * i40e_free_virt_mem_d - OS specific memory free for shared code
175 * @hw: pointer to the HW structure
176 * @mem: ptr to mem struct to free
177 **/
178int i40e_free_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem)
179{
180 /* it's ok to kfree a NULL pointer */
181 kfree(mem->va);
182 mem->va = NULL;
183 mem->size = 0;
184
185 return 0;
186}
187
188/**
189 * i40e_get_lump - find a lump of free generic resource
190 * @pf: board private structure
191 * @pile: the pile of resource to search
192 * @needed: the number of items needed
193 * @id: an owner id to stick on the items assigned
194 *
195 * Returns the base item index of the lump, or negative for error
196 *
197 * The search_hint trick and lack of advanced fit-finding only work
198 * because we're highly likely to have all the same size lump requests.
199 * Linear search time and any fragmentation should be minimal.
200 **/
201static int i40e_get_lump(struct i40e_pf *pf, struct i40e_lump_tracking *pile,
202 u16 needed, u16 id)
203{
204 int ret = -ENOMEM;
Jesse Brandeburgddf434a2013-09-13 08:23:19 +0000205 int i, j;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000206
207 if (!pile || needed == 0 || id >= I40E_PILE_VALID_BIT) {
208 dev_info(&pf->pdev->dev,
209 "param err: pile=%p needed=%d id=0x%04x\n",
210 pile, needed, id);
211 return -EINVAL;
212 }
213
214 /* start the linear search with an imperfect hint */
215 i = pile->search_hint;
Jesse Brandeburgddf434a2013-09-13 08:23:19 +0000216 while (i < pile->num_entries) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000217 /* skip already allocated entries */
218 if (pile->list[i] & I40E_PILE_VALID_BIT) {
219 i++;
220 continue;
221 }
222
223 /* do we have enough in this lump? */
224 for (j = 0; (j < needed) && ((i+j) < pile->num_entries); j++) {
225 if (pile->list[i+j] & I40E_PILE_VALID_BIT)
226 break;
227 }
228
229 if (j == needed) {
230 /* there was enough, so assign it to the requestor */
231 for (j = 0; j < needed; j++)
232 pile->list[i+j] = id | I40E_PILE_VALID_BIT;
233 ret = i;
234 pile->search_hint = i + j;
Jesse Brandeburgddf434a2013-09-13 08:23:19 +0000235 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000236 }
Jesse Brandeburg6995b362015-08-28 17:55:54 -0400237
238 /* not enough, so skip over it and continue looking */
239 i += j;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000240 }
241
242 return ret;
243}
244
245/**
246 * i40e_put_lump - return a lump of generic resource
247 * @pile: the pile of resource to search
248 * @index: the base item index
249 * @id: the owner id of the items assigned
250 *
251 * Returns the count of items in the lump
252 **/
253static int i40e_put_lump(struct i40e_lump_tracking *pile, u16 index, u16 id)
254{
255 int valid_id = (id | I40E_PILE_VALID_BIT);
256 int count = 0;
257 int i;
258
259 if (!pile || index >= pile->num_entries)
260 return -EINVAL;
261
262 for (i = index;
263 i < pile->num_entries && pile->list[i] == valid_id;
264 i++) {
265 pile->list[i] = 0;
266 count++;
267 }
268
269 if (count && index < pile->search_hint)
270 pile->search_hint = index;
271
272 return count;
273}
274
275/**
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700276 * i40e_find_vsi_from_id - searches for the vsi with the given id
277 * @pf - the pf structure to search for the vsi
278 * @id - id of the vsi it is searching for
279 **/
280struct i40e_vsi *i40e_find_vsi_from_id(struct i40e_pf *pf, u16 id)
281{
282 int i;
283
284 for (i = 0; i < pf->num_alloc_vsi; i++)
285 if (pf->vsi[i] && (pf->vsi[i]->id == id))
286 return pf->vsi[i];
287
288 return NULL;
289}
290
291/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000292 * i40e_service_event_schedule - Schedule the service task to wake up
293 * @pf: board private structure
294 *
295 * If not already scheduled, this puts the task into the work queue
296 **/
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -0600297void i40e_service_event_schedule(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000298{
Mauro S. M. Rodrigues9e6c9c02017-05-12 23:26:56 -0300299 if (!test_bit(__I40E_DOWN, pf->state) &&
Jacob Keller0da36b92017-04-19 09:25:55 -0400300 !test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
Jesse Brandeburg2803b162015-12-22 14:25:08 -0800301 queue_work(i40e_wq, &pf->service_task);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000302}
303
304/**
305 * i40e_tx_timeout - Respond to a Tx Hang
306 * @netdev: network interface device structure
307 *
308 * If any port has noticed a Tx timeout, it is likely that the whole
309 * device is munged, not just the one netdev port, so go for the full
310 * reset.
311 **/
312static void i40e_tx_timeout(struct net_device *netdev)
313{
314 struct i40e_netdev_priv *np = netdev_priv(netdev);
315 struct i40e_vsi *vsi = np->vsi;
316 struct i40e_pf *pf = vsi->back;
Kiran Patilb03a8c12015-09-24 18:13:15 -0400317 struct i40e_ring *tx_ring = NULL;
318 unsigned int i, hung_queue = 0;
319 u32 head, val;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000320
321 pf->tx_timeout_count++;
322
Kiran Patilb03a8c12015-09-24 18:13:15 -0400323 /* find the stopped queue the same way the stack does */
324 for (i = 0; i < netdev->num_tx_queues; i++) {
325 struct netdev_queue *q;
326 unsigned long trans_start;
327
328 q = netdev_get_tx_queue(netdev, i);
Florian Westphal9b366272016-05-03 16:33:14 +0200329 trans_start = q->trans_start;
Kiran Patilb03a8c12015-09-24 18:13:15 -0400330 if (netif_xmit_stopped(q) &&
331 time_after(jiffies,
332 (trans_start + netdev->watchdog_timeo))) {
333 hung_queue = i;
334 break;
335 }
336 }
337
338 if (i == netdev->num_tx_queues) {
339 netdev_info(netdev, "tx_timeout: no netdev hung queue found\n");
340 } else {
341 /* now that we have an index, find the tx_ring struct */
342 for (i = 0; i < vsi->num_queue_pairs; i++) {
343 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc) {
344 if (hung_queue ==
345 vsi->tx_rings[i]->queue_index) {
346 tx_ring = vsi->tx_rings[i];
347 break;
348 }
349 }
350 }
351 }
352
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000353 if (time_after(jiffies, (pf->tx_timeout_last_recovery + HZ*20)))
Kiran Patilb03a8c12015-09-24 18:13:15 -0400354 pf->tx_timeout_recovery_level = 1; /* reset after some time */
355 else if (time_before(jiffies,
356 (pf->tx_timeout_last_recovery + netdev->watchdog_timeo)))
357 return; /* don't do any new action before the next timeout */
358
359 if (tx_ring) {
360 head = i40e_get_head(tx_ring);
361 /* Read interrupt register */
362 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
363 val = rd32(&pf->hw,
364 I40E_PFINT_DYN_CTLN(tx_ring->q_vector->v_idx +
365 tx_ring->vsi->base_vector - 1));
366 else
367 val = rd32(&pf->hw, I40E_PFINT_DYN_CTL0);
368
369 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",
370 vsi->seid, hung_queue, tx_ring->next_to_clean,
371 head, tx_ring->next_to_use,
372 readl(tx_ring->tail), val);
373 }
374
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000375 pf->tx_timeout_last_recovery = jiffies;
Kiran Patilb03a8c12015-09-24 18:13:15 -0400376 netdev_info(netdev, "tx_timeout recovery level %d, hung_queue %d\n",
377 pf->tx_timeout_recovery_level, hung_queue);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000378
379 switch (pf->tx_timeout_recovery_level) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000380 case 1:
Jacob Keller0da36b92017-04-19 09:25:55 -0400381 set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000382 break;
383 case 2:
Jacob Keller0da36b92017-04-19 09:25:55 -0400384 set_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000385 break;
386 case 3:
Jacob Keller0da36b92017-04-19 09:25:55 -0400387 set_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000388 break;
389 default:
390 netdev_err(netdev, "tx_timeout recovery unsuccessful\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000391 break;
392 }
Kiran Patilb03a8c12015-09-24 18:13:15 -0400393
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000394 i40e_service_event_schedule(pf);
395 pf->tx_timeout_recovery_level++;
396}
397
398/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000399 * i40e_get_vsi_stats_struct - Get System Network Statistics
400 * @vsi: the VSI we care about
401 *
402 * Returns the address of the device statistics structure.
403 * The statistics are actually updated from the service task.
404 **/
405struct rtnl_link_stats64 *i40e_get_vsi_stats_struct(struct i40e_vsi *vsi)
406{
407 return &vsi->net_stats;
408}
409
410/**
Björn Töpel74608d12017-05-24 07:55:35 +0200411 * i40e_get_netdev_stats_struct_tx - populate stats from a Tx ring
412 * @ring: Tx ring to get statistics from
413 * @stats: statistics entry to be updated
414 **/
415static void i40e_get_netdev_stats_struct_tx(struct i40e_ring *ring,
416 struct rtnl_link_stats64 *stats)
417{
418 u64 bytes, packets;
419 unsigned int start;
420
421 do {
422 start = u64_stats_fetch_begin_irq(&ring->syncp);
423 packets = ring->stats.packets;
424 bytes = ring->stats.bytes;
425 } while (u64_stats_fetch_retry_irq(&ring->syncp, start));
426
427 stats->tx_packets += packets;
428 stats->tx_bytes += bytes;
429}
430
431/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000432 * i40e_get_netdev_stats_struct - Get statistics for netdev interface
433 * @netdev: network interface device structure
434 *
435 * Returns the address of the device statistics structure.
436 * The statistics are actually updated from the service task.
437 **/
Alexander Duyck9eed69a2017-02-21 15:55:47 -0800438static void i40e_get_netdev_stats_struct(struct net_device *netdev,
stephen hemmingerbc1f4472017-01-06 19:12:52 -0800439 struct rtnl_link_stats64 *stats)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000440{
441 struct i40e_netdev_priv *np = netdev_priv(netdev);
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +0000442 struct i40e_ring *tx_ring, *rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000443 struct i40e_vsi *vsi = np->vsi;
Alexander Duyck980e9b12013-09-28 06:01:03 +0000444 struct rtnl_link_stats64 *vsi_stats = i40e_get_vsi_stats_struct(vsi);
445 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000446
Jacob Keller0da36b92017-04-19 09:25:55 -0400447 if (test_bit(__I40E_VSI_DOWN, vsi->state))
stephen hemmingerbc1f4472017-01-06 19:12:52 -0800448 return;
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +0000449
Jesse Brandeburg3c325ce2013-12-14 03:26:45 -0800450 if (!vsi->tx_rings)
stephen hemmingerbc1f4472017-01-06 19:12:52 -0800451 return;
Jesse Brandeburg3c325ce2013-12-14 03:26:45 -0800452
Alexander Duyck980e9b12013-09-28 06:01:03 +0000453 rcu_read_lock();
454 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck980e9b12013-09-28 06:01:03 +0000455 u64 bytes, packets;
456 unsigned int start;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000457
Alexander Duyck980e9b12013-09-28 06:01:03 +0000458 tx_ring = ACCESS_ONCE(vsi->tx_rings[i]);
459 if (!tx_ring)
460 continue;
Björn Töpel74608d12017-05-24 07:55:35 +0200461 i40e_get_netdev_stats_struct_tx(tx_ring, stats);
Alexander Duyck980e9b12013-09-28 06:01:03 +0000462
Alexander Duyck980e9b12013-09-28 06:01:03 +0000463 rx_ring = &tx_ring[1];
464
465 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700466 start = u64_stats_fetch_begin_irq(&rx_ring->syncp);
Alexander Duyck980e9b12013-09-28 06:01:03 +0000467 packets = rx_ring->stats.packets;
468 bytes = rx_ring->stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700469 } while (u64_stats_fetch_retry_irq(&rx_ring->syncp, start));
Alexander Duyck980e9b12013-09-28 06:01:03 +0000470
471 stats->rx_packets += packets;
472 stats->rx_bytes += bytes;
Björn Töpel74608d12017-05-24 07:55:35 +0200473
474 if (i40e_enabled_xdp_vsi(vsi))
475 i40e_get_netdev_stats_struct_tx(&rx_ring[1], stats);
Alexander Duyck980e9b12013-09-28 06:01:03 +0000476 }
477 rcu_read_unlock();
478
Akeem G Abodunrina5282f42014-05-10 04:49:03 +0000479 /* following stats updated by i40e_watchdog_subtask() */
Alexander Duyck980e9b12013-09-28 06:01:03 +0000480 stats->multicast = vsi_stats->multicast;
481 stats->tx_errors = vsi_stats->tx_errors;
482 stats->tx_dropped = vsi_stats->tx_dropped;
483 stats->rx_errors = vsi_stats->rx_errors;
Jesse Brandeburgd8201e22015-07-23 16:54:35 -0400484 stats->rx_dropped = vsi_stats->rx_dropped;
Alexander Duyck980e9b12013-09-28 06:01:03 +0000485 stats->rx_crc_errors = vsi_stats->rx_crc_errors;
486 stats->rx_length_errors = vsi_stats->rx_length_errors;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000487}
488
489/**
490 * i40e_vsi_reset_stats - Resets all stats of the given vsi
491 * @vsi: the VSI to have its stats reset
492 **/
493void i40e_vsi_reset_stats(struct i40e_vsi *vsi)
494{
495 struct rtnl_link_stats64 *ns;
496 int i;
497
498 if (!vsi)
499 return;
500
501 ns = i40e_get_vsi_stats_struct(vsi);
502 memset(ns, 0, sizeof(*ns));
503 memset(&vsi->net_stats_offsets, 0, sizeof(vsi->net_stats_offsets));
504 memset(&vsi->eth_stats, 0, sizeof(vsi->eth_stats));
505 memset(&vsi->eth_stats_offsets, 0, sizeof(vsi->eth_stats_offsets));
Greg Rose8e9dca52013-12-18 13:45:53 +0000506 if (vsi->rx_rings && vsi->rx_rings[0]) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000507 for (i = 0; i < vsi->num_queue_pairs; i++) {
Jesse Brandeburg6995b362015-08-28 17:55:54 -0400508 memset(&vsi->rx_rings[i]->stats, 0,
Alexander Duyck9f65e152013-09-28 06:00:58 +0000509 sizeof(vsi->rx_rings[i]->stats));
Jesse Brandeburg6995b362015-08-28 17:55:54 -0400510 memset(&vsi->rx_rings[i]->rx_stats, 0,
Alexander Duyck9f65e152013-09-28 06:00:58 +0000511 sizeof(vsi->rx_rings[i]->rx_stats));
Jesse Brandeburg6995b362015-08-28 17:55:54 -0400512 memset(&vsi->tx_rings[i]->stats, 0,
Alexander Duyck9f65e152013-09-28 06:00:58 +0000513 sizeof(vsi->tx_rings[i]->stats));
514 memset(&vsi->tx_rings[i]->tx_stats, 0,
515 sizeof(vsi->tx_rings[i]->tx_stats));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000516 }
Greg Rose8e9dca52013-12-18 13:45:53 +0000517 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000518 vsi->stat_offsets_loaded = false;
519}
520
521/**
Jeff Kirsherb40c82e62015-02-27 09:18:34 +0000522 * i40e_pf_reset_stats - Reset all of the stats for the given PF
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000523 * @pf: the PF to be reset
524 **/
525void i40e_pf_reset_stats(struct i40e_pf *pf)
526{
Shannon Nelsone91fdf72014-06-03 23:50:18 +0000527 int i;
528
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000529 memset(&pf->stats, 0, sizeof(pf->stats));
530 memset(&pf->stats_offsets, 0, sizeof(pf->stats_offsets));
531 pf->stat_offsets_loaded = false;
Shannon Nelsone91fdf72014-06-03 23:50:18 +0000532
533 for (i = 0; i < I40E_MAX_VEB; i++) {
534 if (pf->veb[i]) {
535 memset(&pf->veb[i]->stats, 0,
536 sizeof(pf->veb[i]->stats));
537 memset(&pf->veb[i]->stats_offsets, 0,
538 sizeof(pf->veb[i]->stats_offsets));
539 pf->veb[i]->stat_offsets_loaded = false;
540 }
541 }
Catherine Sullivan42bce042016-07-27 12:02:32 -0700542 pf->hw_csum_rx_error = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000543}
544
545/**
546 * i40e_stat_update48 - read and update a 48 bit stat from the chip
547 * @hw: ptr to the hardware info
548 * @hireg: the high 32 bit reg to read
549 * @loreg: the low 32 bit reg to read
550 * @offset_loaded: has the initial offset been loaded yet
551 * @offset: ptr to current offset value
552 * @stat: ptr to the stat
553 *
554 * Since the device stats are not reset at PFReset, they likely will not
555 * be zeroed when the driver starts. We'll save the first values read
556 * and use them as offsets to be subtracted from the raw values in order
557 * to report stats that count from zero. In the process, we also manage
558 * the potential roll-over.
559 **/
560static void i40e_stat_update48(struct i40e_hw *hw, u32 hireg, u32 loreg,
561 bool offset_loaded, u64 *offset, u64 *stat)
562{
563 u64 new_data;
564
Shannon Nelsonab600852014-01-17 15:36:39 -0800565 if (hw->device_id == I40E_DEV_ID_QEMU) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000566 new_data = rd32(hw, loreg);
567 new_data |= ((u64)(rd32(hw, hireg) & 0xFFFF)) << 32;
568 } else {
569 new_data = rd64(hw, loreg);
570 }
571 if (!offset_loaded)
572 *offset = new_data;
573 if (likely(new_data >= *offset))
574 *stat = new_data - *offset;
575 else
Jesse Brandeburg41a1d042015-06-04 16:24:02 -0400576 *stat = (new_data + BIT_ULL(48)) - *offset;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000577 *stat &= 0xFFFFFFFFFFFFULL;
578}
579
580/**
581 * i40e_stat_update32 - read and update a 32 bit stat from the chip
582 * @hw: ptr to the hardware info
583 * @reg: the hw reg to read
584 * @offset_loaded: has the initial offset been loaded yet
585 * @offset: ptr to current offset value
586 * @stat: ptr to the stat
587 **/
588static void i40e_stat_update32(struct i40e_hw *hw, u32 reg,
589 bool offset_loaded, u64 *offset, u64 *stat)
590{
591 u32 new_data;
592
593 new_data = rd32(hw, reg);
594 if (!offset_loaded)
595 *offset = new_data;
596 if (likely(new_data >= *offset))
597 *stat = (u32)(new_data - *offset);
598 else
Jesse Brandeburg41a1d042015-06-04 16:24:02 -0400599 *stat = (u32)((new_data + BIT_ULL(32)) - *offset);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000600}
601
602/**
603 * i40e_update_eth_stats - Update VSI-specific ethernet statistics counters.
604 * @vsi: the VSI to be updated
605 **/
606void i40e_update_eth_stats(struct i40e_vsi *vsi)
607{
608 int stat_idx = le16_to_cpu(vsi->info.stat_counter_idx);
609 struct i40e_pf *pf = vsi->back;
610 struct i40e_hw *hw = &pf->hw;
611 struct i40e_eth_stats *oes;
612 struct i40e_eth_stats *es; /* device's eth stats */
613
614 es = &vsi->eth_stats;
615 oes = &vsi->eth_stats_offsets;
616
617 /* Gather up the stats that the hw collects */
618 i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
619 vsi->stat_offsets_loaded,
620 &oes->tx_errors, &es->tx_errors);
621 i40e_stat_update32(hw, I40E_GLV_RDPC(stat_idx),
622 vsi->stat_offsets_loaded,
623 &oes->rx_discards, &es->rx_discards);
Shannon Nelson41a9e552014-04-23 04:50:20 +0000624 i40e_stat_update32(hw, I40E_GLV_RUPP(stat_idx),
625 vsi->stat_offsets_loaded,
626 &oes->rx_unknown_protocol, &es->rx_unknown_protocol);
627 i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
628 vsi->stat_offsets_loaded,
629 &oes->tx_errors, &es->tx_errors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000630
631 i40e_stat_update48(hw, I40E_GLV_GORCH(stat_idx),
632 I40E_GLV_GORCL(stat_idx),
633 vsi->stat_offsets_loaded,
634 &oes->rx_bytes, &es->rx_bytes);
635 i40e_stat_update48(hw, I40E_GLV_UPRCH(stat_idx),
636 I40E_GLV_UPRCL(stat_idx),
637 vsi->stat_offsets_loaded,
638 &oes->rx_unicast, &es->rx_unicast);
639 i40e_stat_update48(hw, I40E_GLV_MPRCH(stat_idx),
640 I40E_GLV_MPRCL(stat_idx),
641 vsi->stat_offsets_loaded,
642 &oes->rx_multicast, &es->rx_multicast);
643 i40e_stat_update48(hw, I40E_GLV_BPRCH(stat_idx),
644 I40E_GLV_BPRCL(stat_idx),
645 vsi->stat_offsets_loaded,
646 &oes->rx_broadcast, &es->rx_broadcast);
647
648 i40e_stat_update48(hw, I40E_GLV_GOTCH(stat_idx),
649 I40E_GLV_GOTCL(stat_idx),
650 vsi->stat_offsets_loaded,
651 &oes->tx_bytes, &es->tx_bytes);
652 i40e_stat_update48(hw, I40E_GLV_UPTCH(stat_idx),
653 I40E_GLV_UPTCL(stat_idx),
654 vsi->stat_offsets_loaded,
655 &oes->tx_unicast, &es->tx_unicast);
656 i40e_stat_update48(hw, I40E_GLV_MPTCH(stat_idx),
657 I40E_GLV_MPTCL(stat_idx),
658 vsi->stat_offsets_loaded,
659 &oes->tx_multicast, &es->tx_multicast);
660 i40e_stat_update48(hw, I40E_GLV_BPTCH(stat_idx),
661 I40E_GLV_BPTCL(stat_idx),
662 vsi->stat_offsets_loaded,
663 &oes->tx_broadcast, &es->tx_broadcast);
664 vsi->stat_offsets_loaded = true;
665}
666
667/**
668 * i40e_update_veb_stats - Update Switch component statistics
669 * @veb: the VEB being updated
670 **/
671static void i40e_update_veb_stats(struct i40e_veb *veb)
672{
673 struct i40e_pf *pf = veb->pf;
674 struct i40e_hw *hw = &pf->hw;
675 struct i40e_eth_stats *oes;
676 struct i40e_eth_stats *es; /* device's eth stats */
Neerav Parikhfe860af2015-07-10 19:36:02 -0400677 struct i40e_veb_tc_stats *veb_oes;
678 struct i40e_veb_tc_stats *veb_es;
679 int i, idx = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000680
681 idx = veb->stats_idx;
682 es = &veb->stats;
683 oes = &veb->stats_offsets;
Neerav Parikhfe860af2015-07-10 19:36:02 -0400684 veb_es = &veb->tc_stats;
685 veb_oes = &veb->tc_stats_offsets;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000686
687 /* Gather up the stats that the hw collects */
688 i40e_stat_update32(hw, I40E_GLSW_TDPC(idx),
689 veb->stat_offsets_loaded,
690 &oes->tx_discards, &es->tx_discards);
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +0000691 if (hw->revision_id > 0)
692 i40e_stat_update32(hw, I40E_GLSW_RUPP(idx),
693 veb->stat_offsets_loaded,
694 &oes->rx_unknown_protocol,
695 &es->rx_unknown_protocol);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000696 i40e_stat_update48(hw, I40E_GLSW_GORCH(idx), I40E_GLSW_GORCL(idx),
697 veb->stat_offsets_loaded,
698 &oes->rx_bytes, &es->rx_bytes);
699 i40e_stat_update48(hw, I40E_GLSW_UPRCH(idx), I40E_GLSW_UPRCL(idx),
700 veb->stat_offsets_loaded,
701 &oes->rx_unicast, &es->rx_unicast);
702 i40e_stat_update48(hw, I40E_GLSW_MPRCH(idx), I40E_GLSW_MPRCL(idx),
703 veb->stat_offsets_loaded,
704 &oes->rx_multicast, &es->rx_multicast);
705 i40e_stat_update48(hw, I40E_GLSW_BPRCH(idx), I40E_GLSW_BPRCL(idx),
706 veb->stat_offsets_loaded,
707 &oes->rx_broadcast, &es->rx_broadcast);
708
709 i40e_stat_update48(hw, I40E_GLSW_GOTCH(idx), I40E_GLSW_GOTCL(idx),
710 veb->stat_offsets_loaded,
711 &oes->tx_bytes, &es->tx_bytes);
712 i40e_stat_update48(hw, I40E_GLSW_UPTCH(idx), I40E_GLSW_UPTCL(idx),
713 veb->stat_offsets_loaded,
714 &oes->tx_unicast, &es->tx_unicast);
715 i40e_stat_update48(hw, I40E_GLSW_MPTCH(idx), I40E_GLSW_MPTCL(idx),
716 veb->stat_offsets_loaded,
717 &oes->tx_multicast, &es->tx_multicast);
718 i40e_stat_update48(hw, I40E_GLSW_BPTCH(idx), I40E_GLSW_BPTCL(idx),
719 veb->stat_offsets_loaded,
720 &oes->tx_broadcast, &es->tx_broadcast);
Neerav Parikhfe860af2015-07-10 19:36:02 -0400721 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
722 i40e_stat_update48(hw, I40E_GLVEBTC_RPCH(i, idx),
723 I40E_GLVEBTC_RPCL(i, idx),
724 veb->stat_offsets_loaded,
725 &veb_oes->tc_rx_packets[i],
726 &veb_es->tc_rx_packets[i]);
727 i40e_stat_update48(hw, I40E_GLVEBTC_RBCH(i, idx),
728 I40E_GLVEBTC_RBCL(i, idx),
729 veb->stat_offsets_loaded,
730 &veb_oes->tc_rx_bytes[i],
731 &veb_es->tc_rx_bytes[i]);
732 i40e_stat_update48(hw, I40E_GLVEBTC_TPCH(i, idx),
733 I40E_GLVEBTC_TPCL(i, idx),
734 veb->stat_offsets_loaded,
735 &veb_oes->tc_tx_packets[i],
736 &veb_es->tc_tx_packets[i]);
737 i40e_stat_update48(hw, I40E_GLVEBTC_TBCH(i, idx),
738 I40E_GLVEBTC_TBCL(i, idx),
739 veb->stat_offsets_loaded,
740 &veb_oes->tc_tx_bytes[i],
741 &veb_es->tc_tx_bytes[i]);
742 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000743 veb->stat_offsets_loaded = true;
744}
745
746/**
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000747 * i40e_update_vsi_stats - Update the vsi statistics counters.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000748 * @vsi: the VSI to be updated
749 *
750 * There are a few instances where we store the same stat in a
751 * couple of different structs. This is partly because we have
752 * the netdev stats that need to be filled out, which is slightly
753 * different from the "eth_stats" defined by the chip and used in
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000754 * VF communications. We sort it out here.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000755 **/
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000756static void i40e_update_vsi_stats(struct i40e_vsi *vsi)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000757{
758 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000759 struct rtnl_link_stats64 *ons;
760 struct rtnl_link_stats64 *ns; /* netdev stats */
761 struct i40e_eth_stats *oes;
762 struct i40e_eth_stats *es; /* device's eth stats */
763 u32 tx_restart, tx_busy;
Akeem G Abodunrinbf00b372014-10-17 03:14:39 +0000764 struct i40e_ring *p;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000765 u32 rx_page, rx_buf;
Akeem G Abodunrinbf00b372014-10-17 03:14:39 +0000766 u64 bytes, packets;
767 unsigned int start;
Anjali Singhai Jain2fc3d712015-08-27 11:42:29 -0400768 u64 tx_linearize;
Anjali Singhai Jain164c9f52015-10-21 19:47:08 -0400769 u64 tx_force_wb;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000770 u64 rx_p, rx_b;
771 u64 tx_p, tx_b;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000772 u16 q;
773
Jacob Keller0da36b92017-04-19 09:25:55 -0400774 if (test_bit(__I40E_VSI_DOWN, vsi->state) ||
775 test_bit(__I40E_CONFIG_BUSY, pf->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000776 return;
777
778 ns = i40e_get_vsi_stats_struct(vsi);
779 ons = &vsi->net_stats_offsets;
780 es = &vsi->eth_stats;
781 oes = &vsi->eth_stats_offsets;
782
783 /* Gather up the netdev and vsi stats that the driver collects
784 * on the fly during packet processing
785 */
786 rx_b = rx_p = 0;
787 tx_b = tx_p = 0;
Anjali Singhai Jain164c9f52015-10-21 19:47:08 -0400788 tx_restart = tx_busy = tx_linearize = tx_force_wb = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000789 rx_page = 0;
790 rx_buf = 0;
Alexander Duyck980e9b12013-09-28 06:01:03 +0000791 rcu_read_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000792 for (q = 0; q < vsi->num_queue_pairs; q++) {
Alexander Duyck980e9b12013-09-28 06:01:03 +0000793 /* locate Tx ring */
794 p = ACCESS_ONCE(vsi->tx_rings[q]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000795
Alexander Duyck980e9b12013-09-28 06:01:03 +0000796 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700797 start = u64_stats_fetch_begin_irq(&p->syncp);
Alexander Duyck980e9b12013-09-28 06:01:03 +0000798 packets = p->stats.packets;
799 bytes = p->stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700800 } while (u64_stats_fetch_retry_irq(&p->syncp, start));
Alexander Duyck980e9b12013-09-28 06:01:03 +0000801 tx_b += bytes;
802 tx_p += packets;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000803 tx_restart += p->tx_stats.restart_queue;
804 tx_busy += p->tx_stats.tx_busy;
Anjali Singhai Jain2fc3d712015-08-27 11:42:29 -0400805 tx_linearize += p->tx_stats.tx_linearize;
Anjali Singhai Jain164c9f52015-10-21 19:47:08 -0400806 tx_force_wb += p->tx_stats.tx_force_wb;
Alexander Duyck980e9b12013-09-28 06:01:03 +0000807
808 /* Rx queue is part of the same block as Tx queue */
809 p = &p[1];
810 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700811 start = u64_stats_fetch_begin_irq(&p->syncp);
Alexander Duyck980e9b12013-09-28 06:01:03 +0000812 packets = p->stats.packets;
813 bytes = p->stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700814 } while (u64_stats_fetch_retry_irq(&p->syncp, start));
Alexander Duyck980e9b12013-09-28 06:01:03 +0000815 rx_b += bytes;
816 rx_p += packets;
Mitch Williams420136c2013-12-18 13:45:59 +0000817 rx_buf += p->rx_stats.alloc_buff_failed;
818 rx_page += p->rx_stats.alloc_page_failed;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000819 }
Alexander Duyck980e9b12013-09-28 06:01:03 +0000820 rcu_read_unlock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000821 vsi->tx_restart = tx_restart;
822 vsi->tx_busy = tx_busy;
Anjali Singhai Jain2fc3d712015-08-27 11:42:29 -0400823 vsi->tx_linearize = tx_linearize;
Anjali Singhai Jain164c9f52015-10-21 19:47:08 -0400824 vsi->tx_force_wb = tx_force_wb;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000825 vsi->rx_page_failed = rx_page;
826 vsi->rx_buf_failed = rx_buf;
827
828 ns->rx_packets = rx_p;
829 ns->rx_bytes = rx_b;
830 ns->tx_packets = tx_p;
831 ns->tx_bytes = tx_b;
832
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000833 /* update netdev stats from eth stats */
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000834 i40e_update_eth_stats(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000835 ons->tx_errors = oes->tx_errors;
836 ns->tx_errors = es->tx_errors;
837 ons->multicast = oes->rx_multicast;
838 ns->multicast = es->rx_multicast;
Shannon Nelson41a9e552014-04-23 04:50:20 +0000839 ons->rx_dropped = oes->rx_discards;
840 ns->rx_dropped = es->rx_discards;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000841 ons->tx_dropped = oes->tx_discards;
842 ns->tx_dropped = es->tx_discards;
843
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000844 /* pull in a couple PF stats if this is the main vsi */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000845 if (vsi == pf->vsi[pf->lan_vsi]) {
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000846 ns->rx_crc_errors = pf->stats.crc_errors;
847 ns->rx_errors = pf->stats.crc_errors + pf->stats.illegal_bytes;
848 ns->rx_length_errors = pf->stats.rx_length_errors;
849 }
850}
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000851
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000852/**
Jeff Kirsherb40c82e62015-02-27 09:18:34 +0000853 * i40e_update_pf_stats - Update the PF statistics counters.
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000854 * @pf: the PF to be updated
855 **/
856static void i40e_update_pf_stats(struct i40e_pf *pf)
857{
858 struct i40e_hw_port_stats *osd = &pf->stats_offsets;
859 struct i40e_hw_port_stats *nsd = &pf->stats;
860 struct i40e_hw *hw = &pf->hw;
861 u32 val;
862 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000863
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000864 i40e_stat_update48(hw, I40E_GLPRT_GORCH(hw->port),
865 I40E_GLPRT_GORCL(hw->port),
866 pf->stat_offsets_loaded,
867 &osd->eth.rx_bytes, &nsd->eth.rx_bytes);
868 i40e_stat_update48(hw, I40E_GLPRT_GOTCH(hw->port),
869 I40E_GLPRT_GOTCL(hw->port),
870 pf->stat_offsets_loaded,
871 &osd->eth.tx_bytes, &nsd->eth.tx_bytes);
872 i40e_stat_update32(hw, I40E_GLPRT_RDPC(hw->port),
873 pf->stat_offsets_loaded,
874 &osd->eth.rx_discards,
875 &nsd->eth.rx_discards);
Shannon Nelson532d2832014-04-23 04:50:09 +0000876 i40e_stat_update48(hw, I40E_GLPRT_UPRCH(hw->port),
877 I40E_GLPRT_UPRCL(hw->port),
878 pf->stat_offsets_loaded,
879 &osd->eth.rx_unicast,
880 &nsd->eth.rx_unicast);
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000881 i40e_stat_update48(hw, I40E_GLPRT_MPRCH(hw->port),
882 I40E_GLPRT_MPRCL(hw->port),
883 pf->stat_offsets_loaded,
884 &osd->eth.rx_multicast,
885 &nsd->eth.rx_multicast);
Shannon Nelson532d2832014-04-23 04:50:09 +0000886 i40e_stat_update48(hw, I40E_GLPRT_BPRCH(hw->port),
887 I40E_GLPRT_BPRCL(hw->port),
888 pf->stat_offsets_loaded,
889 &osd->eth.rx_broadcast,
890 &nsd->eth.rx_broadcast);
891 i40e_stat_update48(hw, I40E_GLPRT_UPTCH(hw->port),
892 I40E_GLPRT_UPTCL(hw->port),
893 pf->stat_offsets_loaded,
894 &osd->eth.tx_unicast,
895 &nsd->eth.tx_unicast);
896 i40e_stat_update48(hw, I40E_GLPRT_MPTCH(hw->port),
897 I40E_GLPRT_MPTCL(hw->port),
898 pf->stat_offsets_loaded,
899 &osd->eth.tx_multicast,
900 &nsd->eth.tx_multicast);
901 i40e_stat_update48(hw, I40E_GLPRT_BPTCH(hw->port),
902 I40E_GLPRT_BPTCL(hw->port),
903 pf->stat_offsets_loaded,
904 &osd->eth.tx_broadcast,
905 &nsd->eth.tx_broadcast);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000906
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000907 i40e_stat_update32(hw, I40E_GLPRT_TDOLD(hw->port),
908 pf->stat_offsets_loaded,
909 &osd->tx_dropped_link_down,
910 &nsd->tx_dropped_link_down);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000911
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000912 i40e_stat_update32(hw, I40E_GLPRT_CRCERRS(hw->port),
913 pf->stat_offsets_loaded,
914 &osd->crc_errors, &nsd->crc_errors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000915
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000916 i40e_stat_update32(hw, I40E_GLPRT_ILLERRC(hw->port),
917 pf->stat_offsets_loaded,
918 &osd->illegal_bytes, &nsd->illegal_bytes);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000919
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000920 i40e_stat_update32(hw, I40E_GLPRT_MLFC(hw->port),
921 pf->stat_offsets_loaded,
922 &osd->mac_local_faults,
923 &nsd->mac_local_faults);
924 i40e_stat_update32(hw, I40E_GLPRT_MRFC(hw->port),
925 pf->stat_offsets_loaded,
926 &osd->mac_remote_faults,
927 &nsd->mac_remote_faults);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000928
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000929 i40e_stat_update32(hw, I40E_GLPRT_RLEC(hw->port),
930 pf->stat_offsets_loaded,
931 &osd->rx_length_errors,
932 &nsd->rx_length_errors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000933
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000934 i40e_stat_update32(hw, I40E_GLPRT_LXONRXC(hw->port),
935 pf->stat_offsets_loaded,
936 &osd->link_xon_rx, &nsd->link_xon_rx);
937 i40e_stat_update32(hw, I40E_GLPRT_LXONTXC(hw->port),
938 pf->stat_offsets_loaded,
939 &osd->link_xon_tx, &nsd->link_xon_tx);
Neerav Parikh95db2392015-11-06 15:26:09 -0800940 i40e_stat_update32(hw, I40E_GLPRT_LXOFFRXC(hw->port),
941 pf->stat_offsets_loaded,
942 &osd->link_xoff_rx, &nsd->link_xoff_rx);
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000943 i40e_stat_update32(hw, I40E_GLPRT_LXOFFTXC(hw->port),
944 pf->stat_offsets_loaded,
945 &osd->link_xoff_tx, &nsd->link_xoff_tx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000946
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000947 for (i = 0; i < 8; i++) {
Neerav Parikh95db2392015-11-06 15:26:09 -0800948 i40e_stat_update32(hw, I40E_GLPRT_PXOFFRXC(hw->port, i),
949 pf->stat_offsets_loaded,
950 &osd->priority_xoff_rx[i],
951 &nsd->priority_xoff_rx[i]);
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000952 i40e_stat_update32(hw, I40E_GLPRT_PXONRXC(hw->port, i),
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000953 pf->stat_offsets_loaded,
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000954 &osd->priority_xon_rx[i],
955 &nsd->priority_xon_rx[i]);
956 i40e_stat_update32(hw, I40E_GLPRT_PXONTXC(hw->port, i),
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000957 pf->stat_offsets_loaded,
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000958 &osd->priority_xon_tx[i],
959 &nsd->priority_xon_tx[i]);
960 i40e_stat_update32(hw, I40E_GLPRT_PXOFFTXC(hw->port, i),
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000961 pf->stat_offsets_loaded,
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000962 &osd->priority_xoff_tx[i],
963 &nsd->priority_xoff_tx[i]);
964 i40e_stat_update32(hw,
965 I40E_GLPRT_RXON2OFFCNT(hw->port, i),
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000966 pf->stat_offsets_loaded,
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000967 &osd->priority_xon_2_xoff[i],
968 &nsd->priority_xon_2_xoff[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000969 }
970
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000971 i40e_stat_update48(hw, I40E_GLPRT_PRC64H(hw->port),
972 I40E_GLPRT_PRC64L(hw->port),
973 pf->stat_offsets_loaded,
974 &osd->rx_size_64, &nsd->rx_size_64);
975 i40e_stat_update48(hw, I40E_GLPRT_PRC127H(hw->port),
976 I40E_GLPRT_PRC127L(hw->port),
977 pf->stat_offsets_loaded,
978 &osd->rx_size_127, &nsd->rx_size_127);
979 i40e_stat_update48(hw, I40E_GLPRT_PRC255H(hw->port),
980 I40E_GLPRT_PRC255L(hw->port),
981 pf->stat_offsets_loaded,
982 &osd->rx_size_255, &nsd->rx_size_255);
983 i40e_stat_update48(hw, I40E_GLPRT_PRC511H(hw->port),
984 I40E_GLPRT_PRC511L(hw->port),
985 pf->stat_offsets_loaded,
986 &osd->rx_size_511, &nsd->rx_size_511);
987 i40e_stat_update48(hw, I40E_GLPRT_PRC1023H(hw->port),
988 I40E_GLPRT_PRC1023L(hw->port),
989 pf->stat_offsets_loaded,
990 &osd->rx_size_1023, &nsd->rx_size_1023);
991 i40e_stat_update48(hw, I40E_GLPRT_PRC1522H(hw->port),
992 I40E_GLPRT_PRC1522L(hw->port),
993 pf->stat_offsets_loaded,
994 &osd->rx_size_1522, &nsd->rx_size_1522);
995 i40e_stat_update48(hw, I40E_GLPRT_PRC9522H(hw->port),
996 I40E_GLPRT_PRC9522L(hw->port),
997 pf->stat_offsets_loaded,
998 &osd->rx_size_big, &nsd->rx_size_big);
999
1000 i40e_stat_update48(hw, I40E_GLPRT_PTC64H(hw->port),
1001 I40E_GLPRT_PTC64L(hw->port),
1002 pf->stat_offsets_loaded,
1003 &osd->tx_size_64, &nsd->tx_size_64);
1004 i40e_stat_update48(hw, I40E_GLPRT_PTC127H(hw->port),
1005 I40E_GLPRT_PTC127L(hw->port),
1006 pf->stat_offsets_loaded,
1007 &osd->tx_size_127, &nsd->tx_size_127);
1008 i40e_stat_update48(hw, I40E_GLPRT_PTC255H(hw->port),
1009 I40E_GLPRT_PTC255L(hw->port),
1010 pf->stat_offsets_loaded,
1011 &osd->tx_size_255, &nsd->tx_size_255);
1012 i40e_stat_update48(hw, I40E_GLPRT_PTC511H(hw->port),
1013 I40E_GLPRT_PTC511L(hw->port),
1014 pf->stat_offsets_loaded,
1015 &osd->tx_size_511, &nsd->tx_size_511);
1016 i40e_stat_update48(hw, I40E_GLPRT_PTC1023H(hw->port),
1017 I40E_GLPRT_PTC1023L(hw->port),
1018 pf->stat_offsets_loaded,
1019 &osd->tx_size_1023, &nsd->tx_size_1023);
1020 i40e_stat_update48(hw, I40E_GLPRT_PTC1522H(hw->port),
1021 I40E_GLPRT_PTC1522L(hw->port),
1022 pf->stat_offsets_loaded,
1023 &osd->tx_size_1522, &nsd->tx_size_1522);
1024 i40e_stat_update48(hw, I40E_GLPRT_PTC9522H(hw->port),
1025 I40E_GLPRT_PTC9522L(hw->port),
1026 pf->stat_offsets_loaded,
1027 &osd->tx_size_big, &nsd->tx_size_big);
1028
1029 i40e_stat_update32(hw, I40E_GLPRT_RUC(hw->port),
1030 pf->stat_offsets_loaded,
1031 &osd->rx_undersize, &nsd->rx_undersize);
1032 i40e_stat_update32(hw, I40E_GLPRT_RFC(hw->port),
1033 pf->stat_offsets_loaded,
1034 &osd->rx_fragments, &nsd->rx_fragments);
1035 i40e_stat_update32(hw, I40E_GLPRT_ROC(hw->port),
1036 pf->stat_offsets_loaded,
1037 &osd->rx_oversize, &nsd->rx_oversize);
1038 i40e_stat_update32(hw, I40E_GLPRT_RJC(hw->port),
1039 pf->stat_offsets_loaded,
1040 &osd->rx_jabber, &nsd->rx_jabber);
1041
Anjali Singhai Jain433c47d2014-05-22 06:32:17 +00001042 /* FDIR stats */
Anjali Singhai Jain0bf4b1b2015-04-16 20:06:02 -04001043 i40e_stat_update32(hw,
1044 I40E_GLQF_PCNT(I40E_FD_ATR_STAT_IDX(pf->hw.pf_id)),
Anjali Singhai Jain433c47d2014-05-22 06:32:17 +00001045 pf->stat_offsets_loaded,
1046 &osd->fd_atr_match, &nsd->fd_atr_match);
Anjali Singhai Jain0bf4b1b2015-04-16 20:06:02 -04001047 i40e_stat_update32(hw,
1048 I40E_GLQF_PCNT(I40E_FD_SB_STAT_IDX(pf->hw.pf_id)),
Anjali Singhai Jain433c47d2014-05-22 06:32:17 +00001049 pf->stat_offsets_loaded,
1050 &osd->fd_sb_match, &nsd->fd_sb_match);
Anjali Singhai Jain60ccd452015-04-16 20:06:01 -04001051 i40e_stat_update32(hw,
1052 I40E_GLQF_PCNT(I40E_FD_ATR_TUNNEL_STAT_IDX(pf->hw.pf_id)),
1053 pf->stat_offsets_loaded,
1054 &osd->fd_atr_tunnel_match, &nsd->fd_atr_tunnel_match);
Anjali Singhai Jain433c47d2014-05-22 06:32:17 +00001055
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001056 val = rd32(hw, I40E_PRTPM_EEE_STAT);
1057 nsd->tx_lpi_status =
1058 (val & I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_MASK) >>
1059 I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_SHIFT;
1060 nsd->rx_lpi_status =
1061 (val & I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_MASK) >>
1062 I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_SHIFT;
1063 i40e_stat_update32(hw, I40E_PRTPM_TLPIC,
1064 pf->stat_offsets_loaded,
1065 &osd->tx_lpi_count, &nsd->tx_lpi_count);
1066 i40e_stat_update32(hw, I40E_PRTPM_RLPIC,
1067 pf->stat_offsets_loaded,
1068 &osd->rx_lpi_count, &nsd->rx_lpi_count);
1069
Anjali Singhai Jaind0389e52015-04-22 19:34:05 -04001070 if (pf->flags & I40E_FLAG_FD_SB_ENABLED &&
Jacob Keller47994c12017-04-19 09:25:57 -04001071 !(pf->flags & I40E_FLAG_FD_SB_AUTO_DISABLED))
Anjali Singhai Jaind0389e52015-04-22 19:34:05 -04001072 nsd->fd_sb_status = true;
1073 else
1074 nsd->fd_sb_status = false;
1075
1076 if (pf->flags & I40E_FLAG_FD_ATR_ENABLED &&
Jacob Keller47994c12017-04-19 09:25:57 -04001077 !(pf->flags & I40E_FLAG_FD_ATR_AUTO_DISABLED))
Anjali Singhai Jaind0389e52015-04-22 19:34:05 -04001078 nsd->fd_atr_status = true;
1079 else
1080 nsd->fd_atr_status = false;
1081
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001082 pf->stat_offsets_loaded = true;
1083}
1084
1085/**
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001086 * i40e_update_stats - Update the various statistics counters.
1087 * @vsi: the VSI to be updated
1088 *
1089 * Update the various stats for this VSI and its related entities.
1090 **/
1091void i40e_update_stats(struct i40e_vsi *vsi)
1092{
1093 struct i40e_pf *pf = vsi->back;
1094
1095 if (vsi == pf->vsi[pf->lan_vsi])
1096 i40e_update_pf_stats(pf);
1097
1098 i40e_update_vsi_stats(vsi);
1099}
1100
1101/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001102 * i40e_find_filter - Search VSI filter list for specific mac/vlan filter
1103 * @vsi: the VSI to be searched
1104 * @macaddr: the MAC address
1105 * @vlan: the vlan
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001106 *
1107 * Returns ptr to the filter object or NULL
1108 **/
1109static struct i40e_mac_filter *i40e_find_filter(struct i40e_vsi *vsi,
Jacob Keller6622f5c2016-10-05 09:30:32 -07001110 const u8 *macaddr, s16 vlan)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001111{
1112 struct i40e_mac_filter *f;
Jacob Keller278e7d02016-10-05 09:30:37 -07001113 u64 key;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001114
1115 if (!vsi || !macaddr)
1116 return NULL;
1117
Jacob Keller278e7d02016-10-05 09:30:37 -07001118 key = i40e_addr_to_hkey(macaddr);
1119 hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001120 if ((ether_addr_equal(macaddr, f->macaddr)) &&
Jacob Keller1bc87e82016-10-05 09:30:31 -07001121 (vlan == f->vlan))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001122 return f;
1123 }
1124 return NULL;
1125}
1126
1127/**
1128 * i40e_find_mac - Find a mac addr in the macvlan filters list
1129 * @vsi: the VSI to be searched
1130 * @macaddr: the MAC address we are searching for
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001131 *
1132 * Returns the first filter with the provided MAC address or NULL if
1133 * MAC address was not found
1134 **/
Jacob Keller6622f5c2016-10-05 09:30:32 -07001135struct i40e_mac_filter *i40e_find_mac(struct i40e_vsi *vsi, const u8 *macaddr)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001136{
1137 struct i40e_mac_filter *f;
Jacob Keller278e7d02016-10-05 09:30:37 -07001138 u64 key;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001139
1140 if (!vsi || !macaddr)
1141 return NULL;
1142
Jacob Keller278e7d02016-10-05 09:30:37 -07001143 key = i40e_addr_to_hkey(macaddr);
1144 hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
Jacob Keller1bc87e82016-10-05 09:30:31 -07001145 if ((ether_addr_equal(macaddr, f->macaddr)))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001146 return f;
1147 }
1148 return NULL;
1149}
1150
1151/**
1152 * i40e_is_vsi_in_vlan - Check if VSI is in vlan mode
1153 * @vsi: the VSI to be searched
1154 *
1155 * Returns true if VSI is in vlan mode or false otherwise
1156 **/
1157bool i40e_is_vsi_in_vlan(struct i40e_vsi *vsi)
1158{
Jacob Kellercbebb852016-10-05 09:30:40 -07001159 /* If we have a PVID, always operate in VLAN mode */
1160 if (vsi->info.pvid)
1161 return true;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001162
Jacob Kellercbebb852016-10-05 09:30:40 -07001163 /* We need to operate in VLAN mode whenever we have any filters with
1164 * a VLAN other than I40E_VLAN_ALL. We could check the table each
1165 * time, incurring search cost repeatedly. However, we can notice two
1166 * things:
1167 *
1168 * 1) the only place where we can gain a VLAN filter is in
1169 * i40e_add_filter.
1170 *
1171 * 2) the only place where filters are actually removed is in
Jacob Keller0b7c8b52016-10-25 16:08:52 -07001172 * i40e_sync_filters_subtask.
Jacob Kellercbebb852016-10-05 09:30:40 -07001173 *
1174 * Thus, we can simply use a boolean value, has_vlan_filters which we
1175 * will set to true when we add a VLAN filter in i40e_add_filter. Then
1176 * we have to perform the full search after deleting filters in
Jacob Keller0b7c8b52016-10-25 16:08:52 -07001177 * i40e_sync_filters_subtask, but we already have to search
Jacob Kellercbebb852016-10-05 09:30:40 -07001178 * filters here and can perform the check at the same time. This
1179 * results in avoiding embedding a loop for VLAN mode inside another
1180 * loop over all the filters, and should maintain correctness as noted
1181 * above.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001182 */
Jacob Kellercbebb852016-10-05 09:30:40 -07001183 return vsi->has_vlan_filter;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001184}
1185
1186/**
Jacob Keller489a3262016-11-11 12:39:31 -08001187 * i40e_correct_mac_vlan_filters - Correct non-VLAN filters if necessary
1188 * @vsi: the VSI to configure
1189 * @tmp_add_list: list of filters ready to be added
1190 * @tmp_del_list: list of filters ready to be deleted
1191 * @vlan_filters: the number of active VLAN filters
1192 *
1193 * Update VLAN=0 and VLAN=-1 (I40E_VLAN_ANY) filters properly so that they
1194 * behave as expected. If we have any active VLAN filters remaining or about
1195 * to be added then we need to update non-VLAN filters to be marked as VLAN=0
1196 * so that they only match against untagged traffic. If we no longer have any
1197 * active VLAN filters, we need to make all non-VLAN filters marked as VLAN=-1
1198 * so that they match against both tagged and untagged traffic. In this way,
1199 * we ensure that we correctly receive the desired traffic. This ensures that
1200 * when we have an active VLAN we will receive only untagged traffic and
1201 * traffic matching active VLANs. If we have no active VLANs then we will
1202 * operate in non-VLAN mode and receive all traffic, tagged or untagged.
1203 *
1204 * Finally, in a similar fashion, this function also corrects filters when
1205 * there is an active PVID assigned to this VSI.
1206 *
1207 * In case of memory allocation failure return -ENOMEM. Otherwise, return 0.
1208 *
1209 * This function is only expected to be called from within
1210 * i40e_sync_vsi_filters.
1211 *
1212 * NOTE: This function expects to be called while under the
1213 * mac_filter_hash_lock
1214 */
1215static int i40e_correct_mac_vlan_filters(struct i40e_vsi *vsi,
1216 struct hlist_head *tmp_add_list,
1217 struct hlist_head *tmp_del_list,
1218 int vlan_filters)
1219{
Jacob Keller5cb25902016-12-12 15:44:15 -08001220 s16 pvid = le16_to_cpu(vsi->info.pvid);
Jacob Keller489a3262016-11-11 12:39:31 -08001221 struct i40e_mac_filter *f, *add_head;
Jacob Keller671889e2016-12-02 12:33:00 -08001222 struct i40e_new_mac_filter *new;
Jacob Keller489a3262016-11-11 12:39:31 -08001223 struct hlist_node *h;
1224 int bkt, new_vlan;
1225
1226 /* To determine if a particular filter needs to be replaced we
1227 * have the three following conditions:
1228 *
1229 * a) if we have a PVID assigned, then all filters which are
1230 * not marked as VLAN=PVID must be replaced with filters that
1231 * are.
1232 * b) otherwise, if we have any active VLANS, all filters
1233 * which are marked as VLAN=-1 must be replaced with
1234 * filters marked as VLAN=0
1235 * c) finally, if we do not have any active VLANS, all filters
1236 * which are marked as VLAN=0 must be replaced with filters
1237 * marked as VLAN=-1
1238 */
1239
1240 /* Update the filters about to be added in place */
Jacob Keller671889e2016-12-02 12:33:00 -08001241 hlist_for_each_entry(new, tmp_add_list, hlist) {
Jacob Keller5cb25902016-12-12 15:44:15 -08001242 if (pvid && new->f->vlan != pvid)
1243 new->f->vlan = pvid;
Jacob Keller671889e2016-12-02 12:33:00 -08001244 else if (vlan_filters && new->f->vlan == I40E_VLAN_ANY)
1245 new->f->vlan = 0;
1246 else if (!vlan_filters && new->f->vlan == 0)
1247 new->f->vlan = I40E_VLAN_ANY;
Jacob Keller489a3262016-11-11 12:39:31 -08001248 }
1249
1250 /* Update the remaining active filters */
1251 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1252 /* Combine the checks for whether a filter needs to be changed
1253 * and then determine the new VLAN inside the if block, in
1254 * order to avoid duplicating code for adding the new filter
1255 * then deleting the old filter.
1256 */
Jacob Keller5cb25902016-12-12 15:44:15 -08001257 if ((pvid && f->vlan != pvid) ||
Jacob Keller489a3262016-11-11 12:39:31 -08001258 (vlan_filters && f->vlan == I40E_VLAN_ANY) ||
1259 (!vlan_filters && f->vlan == 0)) {
1260 /* Determine the new vlan we will be adding */
Jacob Keller5cb25902016-12-12 15:44:15 -08001261 if (pvid)
1262 new_vlan = pvid;
Jacob Keller489a3262016-11-11 12:39:31 -08001263 else if (vlan_filters)
1264 new_vlan = 0;
1265 else
1266 new_vlan = I40E_VLAN_ANY;
1267
1268 /* Create the new filter */
1269 add_head = i40e_add_filter(vsi, f->macaddr, new_vlan);
1270 if (!add_head)
1271 return -ENOMEM;
1272
Jacob Keller671889e2016-12-02 12:33:00 -08001273 /* Create a temporary i40e_new_mac_filter */
1274 new = kzalloc(sizeof(*new), GFP_ATOMIC);
1275 if (!new)
1276 return -ENOMEM;
1277
1278 new->f = add_head;
1279 new->state = add_head->state;
1280
1281 /* Add the new filter to the tmp list */
1282 hlist_add_head(&new->hlist, tmp_add_list);
Jacob Keller489a3262016-11-11 12:39:31 -08001283
1284 /* Put the original filter into the delete list */
1285 f->state = I40E_FILTER_REMOVE;
1286 hash_del(&f->hlist);
1287 hlist_add_head(&f->hlist, tmp_del_list);
1288 }
1289 }
1290
1291 vsi->has_vlan_filter = !!vlan_filters;
1292
1293 return 0;
1294}
1295
1296/**
Jacob Keller1596b5d2016-11-08 13:05:15 -08001297 * i40e_rm_default_mac_filter - Remove the default MAC filter set by NVM
1298 * @vsi: the PF Main VSI - inappropriate for any other VSI
1299 * @macaddr: the MAC address
1300 *
1301 * Remove whatever filter the firmware set up so the driver can manage
1302 * its own filtering intelligently.
1303 **/
1304static void i40e_rm_default_mac_filter(struct i40e_vsi *vsi, u8 *macaddr)
1305{
1306 struct i40e_aqc_remove_macvlan_element_data element;
1307 struct i40e_pf *pf = vsi->back;
1308
1309 /* Only appropriate for the PF main VSI */
1310 if (vsi->type != I40E_VSI_MAIN)
1311 return;
1312
1313 memset(&element, 0, sizeof(element));
1314 ether_addr_copy(element.mac_addr, macaddr);
1315 element.vlan_tag = 0;
1316 /* Ignore error returns, some firmware does it this way... */
1317 element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
1318 i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1319
1320 memset(&element, 0, sizeof(element));
1321 ether_addr_copy(element.mac_addr, macaddr);
1322 element.vlan_tag = 0;
1323 /* ...and some firmware does it this way. */
1324 element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH |
1325 I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
1326 i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1327}
1328
1329/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001330 * i40e_add_filter - Add a mac/vlan filter to the VSI
1331 * @vsi: the VSI to be searched
1332 * @macaddr: the MAC address
1333 * @vlan: the vlan
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001334 *
1335 * Returns ptr to the filter object or NULL when no memory available.
Kiran Patil21659032015-09-30 14:09:03 -04001336 *
Jacob Keller278e7d02016-10-05 09:30:37 -07001337 * NOTE: This function is expected to be called with mac_filter_hash_lock
Kiran Patil21659032015-09-30 14:09:03 -04001338 * being held.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001339 **/
1340struct i40e_mac_filter *i40e_add_filter(struct i40e_vsi *vsi,
Jacob Keller6622f5c2016-10-05 09:30:32 -07001341 const u8 *macaddr, s16 vlan)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001342{
1343 struct i40e_mac_filter *f;
Jacob Keller278e7d02016-10-05 09:30:37 -07001344 u64 key;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001345
1346 if (!vsi || !macaddr)
1347 return NULL;
1348
Jacob Keller1bc87e82016-10-05 09:30:31 -07001349 f = i40e_find_filter(vsi, macaddr, vlan);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001350 if (!f) {
1351 f = kzalloc(sizeof(*f), GFP_ATOMIC);
1352 if (!f)
Jacob Keller1bc87e82016-10-05 09:30:31 -07001353 return NULL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001354
Jacob Kellercbebb852016-10-05 09:30:40 -07001355 /* Update the boolean indicating if we need to function in
1356 * VLAN mode.
1357 */
1358 if (vlan >= 0)
1359 vsi->has_vlan_filter = true;
1360
Greg Rose9a173902014-05-22 06:32:02 +00001361 ether_addr_copy(f->macaddr, macaddr);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001362 f->vlan = vlan;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001363 /* If we're in overflow promisc mode, set the state directly
1364 * to failed, so we don't bother to try sending the filter
1365 * to the hardware.
1366 */
Jacob Keller0da36b92017-04-19 09:25:55 -04001367 if (test_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state))
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001368 f->state = I40E_FILTER_FAILED;
1369 else
1370 f->state = I40E_FILTER_NEW;
Jacob Keller278e7d02016-10-05 09:30:37 -07001371 INIT_HLIST_NODE(&f->hlist);
1372
1373 key = i40e_addr_to_hkey(macaddr);
1374 hash_add(vsi->mac_filter_hash, &f->hlist, key);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001375
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001376 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1377 vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
1378 }
1379
Jacob Keller1bc87e82016-10-05 09:30:31 -07001380 /* If we're asked to add a filter that has been marked for removal, it
1381 * is safe to simply restore it to active state. __i40e_del_filter
1382 * will have simply deleted any filters which were previously marked
1383 * NEW or FAILED, so if it is currently marked REMOVE it must have
1384 * previously been ACTIVE. Since we haven't yet run the sync filters
1385 * task, just restore this filter to the ACTIVE state so that the
1386 * sync task leaves it in place
1387 */
1388 if (f->state == I40E_FILTER_REMOVE)
1389 f->state = I40E_FILTER_ACTIVE;
1390
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001391 return f;
1392}
1393
1394/**
Jacob Keller290d2552016-10-05 09:30:36 -07001395 * __i40e_del_filter - Remove a specific filter from the VSI
1396 * @vsi: VSI to remove from
1397 * @f: the filter to remove from the list
1398 *
1399 * This function should be called instead of i40e_del_filter only if you know
1400 * the exact filter you will remove already, such as via i40e_find_filter or
1401 * i40e_find_mac.
Kiran Patil21659032015-09-30 14:09:03 -04001402 *
Jacob Keller278e7d02016-10-05 09:30:37 -07001403 * NOTE: This function is expected to be called with mac_filter_hash_lock
Kiran Patil21659032015-09-30 14:09:03 -04001404 * being held.
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001405 * ANOTHER NOTE: This function MUST be called from within the context of
1406 * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
1407 * instead of list_for_each_entry().
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001408 **/
Jacob Keller148141b2016-11-11 12:39:36 -08001409void __i40e_del_filter(struct i40e_vsi *vsi, struct i40e_mac_filter *f)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001410{
Jacob Keller1bc87e82016-10-05 09:30:31 -07001411 if (!f)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001412 return;
1413
Alan Bradya410c822016-12-12 15:44:07 -08001414 /* If the filter was never added to firmware then we can just delete it
1415 * directly and we don't want to set the status to remove or else an
1416 * admin queue command will unnecessarily fire.
1417 */
Jacob Keller1bc87e82016-10-05 09:30:31 -07001418 if ((f->state == I40E_FILTER_FAILED) ||
1419 (f->state == I40E_FILTER_NEW)) {
Jacob Keller278e7d02016-10-05 09:30:37 -07001420 hash_del(&f->hlist);
Jacob Keller1bc87e82016-10-05 09:30:31 -07001421 kfree(f);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001422 } else {
Jacob Keller1bc87e82016-10-05 09:30:31 -07001423 f->state = I40E_FILTER_REMOVE;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001424 }
Alan Bradya410c822016-12-12 15:44:07 -08001425
1426 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1427 vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001428}
1429
1430/**
Jacob Keller290d2552016-10-05 09:30:36 -07001431 * i40e_del_filter - Remove a MAC/VLAN filter from the VSI
1432 * @vsi: the VSI to be searched
1433 * @macaddr: the MAC address
1434 * @vlan: the VLAN
1435 *
Jacob Keller278e7d02016-10-05 09:30:37 -07001436 * NOTE: This function is expected to be called with mac_filter_hash_lock
Jacob Keller290d2552016-10-05 09:30:36 -07001437 * being held.
1438 * ANOTHER NOTE: This function MUST be called from within the context of
1439 * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
1440 * instead of list_for_each_entry().
1441 **/
1442void i40e_del_filter(struct i40e_vsi *vsi, const u8 *macaddr, s16 vlan)
1443{
1444 struct i40e_mac_filter *f;
1445
1446 if (!vsi || !macaddr)
1447 return;
1448
1449 f = i40e_find_filter(vsi, macaddr, vlan);
1450 __i40e_del_filter(vsi, f);
1451}
1452
1453/**
Jacob Kellerfeffdbe2016-11-11 12:39:35 -08001454 * i40e_add_mac_filter - Add a MAC filter for all active VLANs
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001455 * @vsi: the VSI to be searched
1456 * @macaddr: the mac address to be filtered
1457 *
Jacob Kellerfeffdbe2016-11-11 12:39:35 -08001458 * If we're not in VLAN mode, just add the filter to I40E_VLAN_ANY. Otherwise,
1459 * go through all the macvlan filters and add a macvlan filter for each
Jacob Keller5feb3d72016-10-05 09:30:34 -07001460 * unique vlan that already exists. If a PVID has been assigned, instead only
1461 * add the macaddr to that VLAN.
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001462 *
Jacob Keller5feb3d72016-10-05 09:30:34 -07001463 * Returns last filter added on success, else NULL
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001464 **/
Jacob Kellerfeffdbe2016-11-11 12:39:35 -08001465struct i40e_mac_filter *i40e_add_mac_filter(struct i40e_vsi *vsi,
1466 const u8 *macaddr)
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001467{
Jacob Keller5feb3d72016-10-05 09:30:34 -07001468 struct i40e_mac_filter *f, *add = NULL;
Jacob Keller278e7d02016-10-05 09:30:37 -07001469 struct hlist_node *h;
1470 int bkt;
Jacob Keller5feb3d72016-10-05 09:30:34 -07001471
1472 if (vsi->info.pvid)
1473 return i40e_add_filter(vsi, macaddr,
1474 le16_to_cpu(vsi->info.pvid));
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001475
Jacob Keller7aaf95362016-11-11 12:39:33 -08001476 if (!i40e_is_vsi_in_vlan(vsi))
1477 return i40e_add_filter(vsi, macaddr, I40E_VLAN_ANY);
1478
Jacob Keller278e7d02016-10-05 09:30:37 -07001479 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Jacob Keller57b341d2016-10-05 09:30:35 -07001480 if (f->state == I40E_FILTER_REMOVE)
1481 continue;
Jacob Keller5feb3d72016-10-05 09:30:34 -07001482 add = i40e_add_filter(vsi, macaddr, f->vlan);
1483 if (!add)
1484 return NULL;
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001485 }
1486
Jacob Keller5feb3d72016-10-05 09:30:34 -07001487 return add;
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001488}
1489
1490/**
Jacob Kellerfeffdbe2016-11-11 12:39:35 -08001491 * i40e_del_mac_filter - Remove a MAC filter from all VLANs
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001492 * @vsi: the VSI to be searched
1493 * @macaddr: the mac address to be removed
1494 *
Jacob Kellerfeffdbe2016-11-11 12:39:35 -08001495 * Removes a given MAC address from a VSI regardless of what VLAN it has been
1496 * associated with.
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001497 *
1498 * Returns 0 for success, or error
1499 **/
Jacob Kellerfeffdbe2016-11-11 12:39:35 -08001500int i40e_del_mac_filter(struct i40e_vsi *vsi, const u8 *macaddr)
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001501{
Jacob Keller278e7d02016-10-05 09:30:37 -07001502 struct i40e_mac_filter *f;
1503 struct hlist_node *h;
Jacob Keller290d2552016-10-05 09:30:36 -07001504 bool found = false;
Jacob Keller278e7d02016-10-05 09:30:37 -07001505 int bkt;
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001506
Jacob Keller278e7d02016-10-05 09:30:37 -07001507 WARN(!spin_is_locked(&vsi->mac_filter_hash_lock),
1508 "Missing mac_filter_hash_lock\n");
1509 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Jacob Keller290d2552016-10-05 09:30:36 -07001510 if (ether_addr_equal(macaddr, f->macaddr)) {
1511 __i40e_del_filter(vsi, f);
1512 found = true;
1513 }
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001514 }
Jacob Keller290d2552016-10-05 09:30:36 -07001515
1516 if (found)
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001517 return 0;
Jacob Keller290d2552016-10-05 09:30:36 -07001518 else
1519 return -ENOENT;
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001520}
1521
1522/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001523 * i40e_set_mac - NDO callback to set mac address
1524 * @netdev: network interface device structure
1525 * @p: pointer to an address structure
1526 *
1527 * Returns 0 on success, negative on failure
1528 **/
1529static int i40e_set_mac(struct net_device *netdev, void *p)
1530{
1531 struct i40e_netdev_priv *np = netdev_priv(netdev);
1532 struct i40e_vsi *vsi = np->vsi;
Shannon Nelson30650cc2014-07-29 04:01:50 +00001533 struct i40e_pf *pf = vsi->back;
1534 struct i40e_hw *hw = &pf->hw;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001535 struct sockaddr *addr = p;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001536
1537 if (!is_valid_ether_addr(addr->sa_data))
1538 return -EADDRNOTAVAIL;
1539
Shannon Nelson30650cc2014-07-29 04:01:50 +00001540 if (ether_addr_equal(netdev->dev_addr, addr->sa_data)) {
1541 netdev_info(netdev, "already using mac address %pM\n",
1542 addr->sa_data);
1543 return 0;
1544 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001545
Jacob Keller0da36b92017-04-19 09:25:55 -04001546 if (test_bit(__I40E_VSI_DOWN, vsi->back->state) ||
1547 test_bit(__I40E_RESET_RECOVERY_PENDING, vsi->back->state))
Anjali Singhai Jain80f64282013-11-28 06:39:47 +00001548 return -EADDRNOTAVAIL;
1549
Shannon Nelson30650cc2014-07-29 04:01:50 +00001550 if (ether_addr_equal(hw->mac.addr, addr->sa_data))
1551 netdev_info(netdev, "returning to hw mac address %pM\n",
1552 hw->mac.addr);
1553 else
1554 netdev_info(netdev, "set new mac address %pM\n", addr->sa_data);
1555
Jacob Keller278e7d02016-10-05 09:30:37 -07001556 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Kellerfeffdbe2016-11-11 12:39:35 -08001557 i40e_del_mac_filter(vsi, netdev->dev_addr);
1558 i40e_add_mac_filter(vsi, addr->sa_data);
Jacob Keller278e7d02016-10-05 09:30:37 -07001559 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001560 ether_addr_copy(netdev->dev_addr, addr->sa_data);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001561 if (vsi->type == I40E_VSI_MAIN) {
1562 i40e_status ret;
Jesse Brandeburg6995b362015-08-28 17:55:54 -04001563
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001564 ret = i40e_aq_mac_address_write(&vsi->back->hw,
Shannon Nelsoncc412222014-06-04 01:23:21 +00001565 I40E_AQC_WRITE_TYPE_LAA_WOL,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001566 addr->sa_data, NULL);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001567 if (ret)
1568 netdev_info(netdev, "Ignoring error from firmware on LAA update, status %s, AQ ret %s\n",
1569 i40e_stat_str(hw, ret),
1570 i40e_aq_str(hw, hw->aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001571 }
1572
Jesse Brandeburgc53934c2016-01-04 10:33:06 -08001573 /* schedule our worker thread which will take care of
1574 * applying the new filter changes
1575 */
1576 i40e_service_event_schedule(vsi->back);
1577 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001578}
1579
1580/**
1581 * i40e_vsi_setup_queue_map - Setup a VSI queue map based on enabled_tc
1582 * @vsi: the VSI being setup
1583 * @ctxt: VSI context structure
1584 * @enabled_tc: Enabled TCs bitmap
1585 * @is_add: True if called before Add VSI
1586 *
1587 * Setup VSI queue mapping for enabled traffic classes.
1588 **/
1589static void i40e_vsi_setup_queue_map(struct i40e_vsi *vsi,
1590 struct i40e_vsi_context *ctxt,
1591 u8 enabled_tc,
1592 bool is_add)
1593{
1594 struct i40e_pf *pf = vsi->back;
1595 u16 sections = 0;
1596 u8 netdev_tc = 0;
1597 u16 numtc = 0;
1598 u16 qcount;
1599 u8 offset;
1600 u16 qmap;
1601 int i;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001602 u16 num_tc_qps = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001603
1604 sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
1605 offset = 0;
1606
1607 if (enabled_tc && (vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
1608 /* Find numtc from enabled TC bitmap */
1609 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08001610 if (enabled_tc & BIT(i)) /* TC is enabled */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001611 numtc++;
1612 }
1613 if (!numtc) {
1614 dev_warn(&pf->pdev->dev, "DCB is enabled but no TC enabled, forcing TC0\n");
1615 numtc = 1;
1616 }
1617 } else {
1618 /* At least TC0 is enabled in case of non-DCB case */
1619 numtc = 1;
1620 }
1621
1622 vsi->tc_config.numtc = numtc;
1623 vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001624 /* Number of queues per enabled TC */
Catherine Sullivan7d644022016-05-16 10:26:41 -07001625 qcount = vsi->alloc_queue_pairs;
1626
Anjali Singhai7f9ff472015-02-21 06:43:19 +00001627 num_tc_qps = qcount / numtc;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001628 num_tc_qps = min_t(int, num_tc_qps, i40e_pf_get_max_q_per_tc(pf));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001629
1630 /* Setup queue offset/count for all TCs for given VSI */
1631 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1632 /* See if the given TC is enabled for the given VSI */
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08001633 if (vsi->tc_config.enabled_tc & BIT(i)) {
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04001634 /* TC is enabled */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001635 int pow, num_qps;
1636
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001637 switch (vsi->type) {
1638 case I40E_VSI_MAIN:
Helin Zhangacd65442015-10-26 19:44:28 -04001639 qcount = min_t(int, pf->alloc_rss_size,
1640 num_tc_qps);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001641 break;
1642 case I40E_VSI_FDIR:
1643 case I40E_VSI_SRIOV:
1644 case I40E_VSI_VMDQ2:
1645 default:
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001646 qcount = num_tc_qps;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001647 WARN_ON(i != 0);
1648 break;
1649 }
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001650 vsi->tc_config.tc_info[i].qoffset = offset;
1651 vsi->tc_config.tc_info[i].qcount = qcount;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001652
Shannon Nelson1e200e42015-02-27 09:15:24 +00001653 /* find the next higher power-of-2 of num queue pairs */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001654 num_qps = qcount;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001655 pow = 0;
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04001656 while (num_qps && (BIT_ULL(pow) < qcount)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001657 pow++;
1658 num_qps >>= 1;
1659 }
1660
1661 vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
1662 qmap =
1663 (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
1664 (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
1665
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001666 offset += qcount;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001667 } else {
1668 /* TC is not enabled so set the offset to
1669 * default queue and allocate one queue
1670 * for the given TC.
1671 */
1672 vsi->tc_config.tc_info[i].qoffset = 0;
1673 vsi->tc_config.tc_info[i].qcount = 1;
1674 vsi->tc_config.tc_info[i].netdev_tc = 0;
1675
1676 qmap = 0;
1677 }
1678 ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
1679 }
1680
1681 /* Set actual Tx/Rx queue pairs */
1682 vsi->num_queue_pairs = offset;
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00001683 if ((vsi->type == I40E_VSI_MAIN) && (numtc == 1)) {
1684 if (vsi->req_queue_pairs > 0)
1685 vsi->num_queue_pairs = vsi->req_queue_pairs;
Anjali Singhai Jain26cdc442015-07-10 19:36:00 -04001686 else if (pf->flags & I40E_FLAG_MSIX_ENABLED)
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00001687 vsi->num_queue_pairs = pf->num_lan_msix;
1688 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001689
1690 /* Scheduler section valid can only be set for ADD VSI */
1691 if (is_add) {
1692 sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
1693
1694 ctxt->info.up_enable_bits = enabled_tc;
1695 }
1696 if (vsi->type == I40E_VSI_SRIOV) {
1697 ctxt->info.mapping_flags |=
1698 cpu_to_le16(I40E_AQ_VSI_QUE_MAP_NONCONTIG);
1699 for (i = 0; i < vsi->num_queue_pairs; i++)
1700 ctxt->info.queue_mapping[i] =
1701 cpu_to_le16(vsi->base_queue + i);
1702 } else {
1703 ctxt->info.mapping_flags |=
1704 cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
1705 ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
1706 }
1707 ctxt->info.valid_sections |= cpu_to_le16(sections);
1708}
1709
1710/**
Jacob Keller6622f5c2016-10-05 09:30:32 -07001711 * i40e_addr_sync - Callback for dev_(mc|uc)_sync to add address
1712 * @netdev: the netdevice
1713 * @addr: address to add
1714 *
1715 * Called by __dev_(mc|uc)_sync when an address needs to be added. We call
1716 * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
1717 */
1718static int i40e_addr_sync(struct net_device *netdev, const u8 *addr)
1719{
1720 struct i40e_netdev_priv *np = netdev_priv(netdev);
1721 struct i40e_vsi *vsi = np->vsi;
Jacob Keller6622f5c2016-10-05 09:30:32 -07001722
Jacob Kellerfeffdbe2016-11-11 12:39:35 -08001723 if (i40e_add_mac_filter(vsi, addr))
Jacob Keller6622f5c2016-10-05 09:30:32 -07001724 return 0;
1725 else
1726 return -ENOMEM;
1727}
1728
1729/**
1730 * i40e_addr_unsync - Callback for dev_(mc|uc)_sync to remove address
1731 * @netdev: the netdevice
1732 * @addr: address to add
1733 *
1734 * Called by __dev_(mc|uc)_sync when an address needs to be removed. We call
1735 * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
1736 */
1737static int i40e_addr_unsync(struct net_device *netdev, const u8 *addr)
1738{
1739 struct i40e_netdev_priv *np = netdev_priv(netdev);
1740 struct i40e_vsi *vsi = np->vsi;
1741
Jacob Kellerfeffdbe2016-11-11 12:39:35 -08001742 i40e_del_mac_filter(vsi, addr);
Jacob Keller6622f5c2016-10-05 09:30:32 -07001743
1744 return 0;
1745}
1746
1747/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001748 * i40e_set_rx_mode - NDO callback to set the netdev filters
1749 * @netdev: network interface device structure
1750 **/
1751static void i40e_set_rx_mode(struct net_device *netdev)
1752{
1753 struct i40e_netdev_priv *np = netdev_priv(netdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001754 struct i40e_vsi *vsi = np->vsi;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001755
Jacob Keller278e7d02016-10-05 09:30:37 -07001756 spin_lock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -04001757
Jacob Keller6622f5c2016-10-05 09:30:32 -07001758 __dev_uc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
1759 __dev_mc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001760
Jacob Keller278e7d02016-10-05 09:30:37 -07001761 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001762
1763 /* check for other flag changes */
1764 if (vsi->current_netdev_flags != vsi->netdev->flags) {
1765 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1766 vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
1767 }
Jesse Brandeburgc53934c2016-01-04 10:33:06 -08001768
1769 /* schedule our worker thread which will take care of
1770 * applying the new filter changes
1771 */
1772 i40e_service_event_schedule(vsi->back);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001773}
1774
1775/**
Jacob Keller671889e2016-12-02 12:33:00 -08001776 * i40e_undo_del_filter_entries - Undo the changes made to MAC filter entries
Jacob Keller4a2ce272016-10-05 09:30:38 -07001777 * @vsi: Pointer to VSI struct
Kiran Patil21659032015-09-30 14:09:03 -04001778 * @from: Pointer to list which contains MAC filter entries - changes to
1779 * those entries needs to be undone.
1780 *
Jacob Keller671889e2016-12-02 12:33:00 -08001781 * MAC filter entries from this list were slated for deletion.
Kiran Patil21659032015-09-30 14:09:03 -04001782 **/
Jacob Keller671889e2016-12-02 12:33:00 -08001783static void i40e_undo_del_filter_entries(struct i40e_vsi *vsi,
1784 struct hlist_head *from)
Kiran Patil21659032015-09-30 14:09:03 -04001785{
Jacob Keller278e7d02016-10-05 09:30:37 -07001786 struct i40e_mac_filter *f;
1787 struct hlist_node *h;
Kiran Patil21659032015-09-30 14:09:03 -04001788
Jacob Keller278e7d02016-10-05 09:30:37 -07001789 hlist_for_each_entry_safe(f, h, from, hlist) {
1790 u64 key = i40e_addr_to_hkey(f->macaddr);
1791
Kiran Patil21659032015-09-30 14:09:03 -04001792 /* Move the element back into MAC filter list*/
Jacob Keller278e7d02016-10-05 09:30:37 -07001793 hlist_del(&f->hlist);
1794 hash_add(vsi->mac_filter_hash, &f->hlist, key);
Kiran Patil21659032015-09-30 14:09:03 -04001795 }
1796}
1797
1798/**
Jacob Keller671889e2016-12-02 12:33:00 -08001799 * i40e_undo_add_filter_entries - Undo the changes made to MAC filter entries
1800 * @vsi: Pointer to vsi struct
1801 * @from: Pointer to list which contains MAC filter entries - changes to
1802 * those entries needs to be undone.
1803 *
1804 * MAC filter entries from this list were slated for addition.
1805 **/
1806static void i40e_undo_add_filter_entries(struct i40e_vsi *vsi,
1807 struct hlist_head *from)
1808{
1809 struct i40e_new_mac_filter *new;
1810 struct hlist_node *h;
1811
1812 hlist_for_each_entry_safe(new, h, from, hlist) {
1813 /* We can simply free the wrapper structure */
1814 hlist_del(&new->hlist);
1815 kfree(new);
1816 }
1817}
1818
1819/**
Jacob Kellerd88d40b2016-12-02 12:32:59 -08001820 * i40e_next_entry - Get the next non-broadcast filter from a list
Jacob Keller671889e2016-12-02 12:33:00 -08001821 * @next: pointer to filter in list
Jacob Kellerd88d40b2016-12-02 12:32:59 -08001822 *
1823 * Returns the next non-broadcast filter in the list. Required so that we
1824 * ignore broadcast filters within the list, since these are not handled via
1825 * the normal firmware update path.
1826 */
Jacob Keller671889e2016-12-02 12:33:00 -08001827static
1828struct i40e_new_mac_filter *i40e_next_filter(struct i40e_new_mac_filter *next)
Jacob Kellerd88d40b2016-12-02 12:32:59 -08001829{
Jacob Keller584a8872017-03-07 15:17:52 -08001830 hlist_for_each_entry_continue(next, hlist) {
1831 if (!is_broadcast_ether_addr(next->f->macaddr))
1832 return next;
Jacob Kellerd88d40b2016-12-02 12:32:59 -08001833 }
1834
Jacob Keller584a8872017-03-07 15:17:52 -08001835 return NULL;
Jacob Kellerd88d40b2016-12-02 12:32:59 -08001836}
1837
1838/**
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001839 * i40e_update_filter_state - Update filter state based on return data
1840 * from firmware
1841 * @count: Number of filters added
1842 * @add_list: return data from fw
1843 * @head: pointer to first filter in current batch
Kiran Patil21659032015-09-30 14:09:03 -04001844 *
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001845 * MAC filter entries from list were slated to be added to device. Returns
1846 * number of successful filters. Note that 0 does NOT mean success!
Kiran Patil21659032015-09-30 14:09:03 -04001847 **/
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001848static int
1849i40e_update_filter_state(int count,
1850 struct i40e_aqc_add_macvlan_element_data *add_list,
Jacob Keller671889e2016-12-02 12:33:00 -08001851 struct i40e_new_mac_filter *add_head)
Kiran Patil21659032015-09-30 14:09:03 -04001852{
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001853 int retval = 0;
1854 int i;
Kiran Patil21659032015-09-30 14:09:03 -04001855
Jacob Kellerac9e2392016-11-11 12:39:27 -08001856 for (i = 0; i < count; i++) {
1857 /* Always check status of each filter. We don't need to check
1858 * the firmware return status because we pre-set the filter
1859 * status to I40E_AQC_MM_ERR_NO_RES when sending the filter
1860 * request to the adminq. Thus, if it no longer matches then
1861 * we know the filter is active.
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001862 */
Jacob Kellerac9e2392016-11-11 12:39:27 -08001863 if (add_list[i].match_method == I40E_AQC_MM_ERR_NO_RES) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001864 add_head->state = I40E_FILTER_FAILED;
Jacob Kellerac9e2392016-11-11 12:39:27 -08001865 } else {
1866 add_head->state = I40E_FILTER_ACTIVE;
1867 retval++;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001868 }
Jacob Kellerac9e2392016-11-11 12:39:27 -08001869
Jacob Kellerd88d40b2016-12-02 12:32:59 -08001870 add_head = i40e_next_filter(add_head);
1871 if (!add_head)
1872 break;
Kiran Patil21659032015-09-30 14:09:03 -04001873 }
Jacob Kellerac9e2392016-11-11 12:39:27 -08001874
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001875 return retval;
Kiran Patil21659032015-09-30 14:09:03 -04001876}
1877
1878/**
Jacob Keller00936312016-10-05 09:30:41 -07001879 * i40e_aqc_del_filters - Request firmware to delete a set of filters
1880 * @vsi: ptr to the VSI
1881 * @vsi_name: name to display in messages
1882 * @list: the list of filters to send to firmware
1883 * @num_del: the number of filters to delete
1884 * @retval: Set to -EIO on failure to delete
1885 *
1886 * Send a request to firmware via AdminQ to delete a set of filters. Uses
1887 * *retval instead of a return value so that success does not force ret_val to
1888 * be set to 0. This ensures that a sequence of calls to this function
1889 * preserve the previous value of *retval on successful delete.
1890 */
1891static
1892void i40e_aqc_del_filters(struct i40e_vsi *vsi, const char *vsi_name,
1893 struct i40e_aqc_remove_macvlan_element_data *list,
1894 int num_del, int *retval)
1895{
1896 struct i40e_hw *hw = &vsi->back->hw;
1897 i40e_status aq_ret;
1898 int aq_err;
1899
1900 aq_ret = i40e_aq_remove_macvlan(hw, vsi->seid, list, num_del, NULL);
1901 aq_err = hw->aq.asq_last_status;
1902
1903 /* Explicitly ignore and do not report when firmware returns ENOENT */
1904 if (aq_ret && !(aq_err == I40E_AQ_RC_ENOENT)) {
1905 *retval = -EIO;
1906 dev_info(&vsi->back->pdev->dev,
1907 "ignoring delete macvlan error on %s, err %s, aq_err %s\n",
1908 vsi_name, i40e_stat_str(hw, aq_ret),
1909 i40e_aq_str(hw, aq_err));
1910 }
1911}
1912
1913/**
1914 * i40e_aqc_add_filters - Request firmware to add a set of filters
1915 * @vsi: ptr to the VSI
1916 * @vsi_name: name to display in messages
1917 * @list: the list of filters to send to firmware
1918 * @add_head: Position in the add hlist
1919 * @num_add: the number of filters to add
1920 * @promisc_change: set to true on exit if promiscuous mode was forced on
1921 *
1922 * Send a request to firmware via AdminQ to add a chunk of filters. Will set
1923 * promisc_changed to true if the firmware has run out of space for more
1924 * filters.
1925 */
1926static
1927void i40e_aqc_add_filters(struct i40e_vsi *vsi, const char *vsi_name,
1928 struct i40e_aqc_add_macvlan_element_data *list,
Jacob Keller671889e2016-12-02 12:33:00 -08001929 struct i40e_new_mac_filter *add_head,
Jacob Keller00936312016-10-05 09:30:41 -07001930 int num_add, bool *promisc_changed)
1931{
1932 struct i40e_hw *hw = &vsi->back->hw;
Jacob Keller00936312016-10-05 09:30:41 -07001933 int aq_err, fcnt;
1934
Jacob Kellerac9e2392016-11-11 12:39:27 -08001935 i40e_aq_add_macvlan(hw, vsi->seid, list, num_add, NULL);
Jacob Keller00936312016-10-05 09:30:41 -07001936 aq_err = hw->aq.asq_last_status;
Jacob Kellerac9e2392016-11-11 12:39:27 -08001937 fcnt = i40e_update_filter_state(num_add, list, add_head);
Jacob Keller00936312016-10-05 09:30:41 -07001938
1939 if (fcnt != num_add) {
1940 *promisc_changed = true;
Jacob Keller0da36b92017-04-19 09:25:55 -04001941 set_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
Jacob Keller00936312016-10-05 09:30:41 -07001942 dev_warn(&vsi->back->pdev->dev,
1943 "Error %s adding RX filters on %s, promiscuous mode forced on\n",
1944 i40e_aq_str(hw, aq_err),
1945 vsi_name);
1946 }
1947}
1948
1949/**
Jacob Keller435c0842016-11-08 13:05:10 -08001950 * i40e_aqc_broadcast_filter - Set promiscuous broadcast flags
1951 * @vsi: pointer to the VSI
1952 * @f: filter data
1953 *
1954 * This function sets or clears the promiscuous broadcast flags for VLAN
1955 * filters in order to properly receive broadcast frames. Assumes that only
1956 * broadcast filters are passed.
Jacob Keller671889e2016-12-02 12:33:00 -08001957 *
1958 * Returns status indicating success or failure;
Jacob Keller435c0842016-11-08 13:05:10 -08001959 **/
Jacob Keller671889e2016-12-02 12:33:00 -08001960static i40e_status
1961i40e_aqc_broadcast_filter(struct i40e_vsi *vsi, const char *vsi_name,
1962 struct i40e_mac_filter *f)
Jacob Keller435c0842016-11-08 13:05:10 -08001963{
1964 bool enable = f->state == I40E_FILTER_NEW;
1965 struct i40e_hw *hw = &vsi->back->hw;
1966 i40e_status aq_ret;
1967
1968 if (f->vlan == I40E_VLAN_ANY) {
1969 aq_ret = i40e_aq_set_vsi_broadcast(hw,
1970 vsi->seid,
1971 enable,
1972 NULL);
1973 } else {
1974 aq_ret = i40e_aq_set_vsi_bc_promisc_on_vlan(hw,
1975 vsi->seid,
1976 enable,
1977 f->vlan,
1978 NULL);
1979 }
1980
Jacob Keller671889e2016-12-02 12:33:00 -08001981 if (aq_ret)
Jacob Keller435c0842016-11-08 13:05:10 -08001982 dev_warn(&vsi->back->pdev->dev,
1983 "Error %s setting broadcast promiscuous mode on %s\n",
1984 i40e_aq_str(hw, hw->aq.asq_last_status),
1985 vsi_name);
Jacob Keller671889e2016-12-02 12:33:00 -08001986
1987 return aq_ret;
Jacob Keller435c0842016-11-08 13:05:10 -08001988}
1989
1990/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001991 * i40e_sync_vsi_filters - Update the VSI filter list to the HW
1992 * @vsi: ptr to the VSI
1993 *
1994 * Push any outstanding VSI filter changes through the AdminQ.
1995 *
1996 * Returns 0 or error value
1997 **/
Jesse Brandeburg17652c62015-11-05 17:01:02 -08001998int i40e_sync_vsi_filters(struct i40e_vsi *vsi)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001999{
Jacob Keller278e7d02016-10-05 09:30:37 -07002000 struct hlist_head tmp_add_list, tmp_del_list;
Jacob Keller671889e2016-12-02 12:33:00 -08002001 struct i40e_mac_filter *f;
2002 struct i40e_new_mac_filter *new, *add_head = NULL;
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002003 struct i40e_hw *hw = &vsi->back->hw;
Jacob Keller38326212016-11-11 12:39:26 -08002004 unsigned int failed_filters = 0;
Alan Brady84f5ca62016-10-05 09:30:39 -07002005 unsigned int vlan_filters = 0;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002006 bool promisc_changed = false;
Shannon Nelson2d1de822016-05-16 10:26:44 -07002007 char vsi_name[16] = "PF";
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002008 int filter_list_len = 0;
Mitch Williamsea02e902015-11-09 15:35:50 -08002009 i40e_status aq_ret = 0;
Alan Brady84f5ca62016-10-05 09:30:39 -07002010 u32 changed_flags = 0;
Jacob Keller278e7d02016-10-05 09:30:37 -07002011 struct hlist_node *h;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002012 struct i40e_pf *pf;
2013 int num_add = 0;
2014 int num_del = 0;
Alan Brady84f5ca62016-10-05 09:30:39 -07002015 int retval = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002016 u16 cmd_flags;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002017 int list_size;
Jacob Keller278e7d02016-10-05 09:30:37 -07002018 int bkt;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002019
2020 /* empty array typed pointers, kcalloc later */
2021 struct i40e_aqc_add_macvlan_element_data *add_list;
2022 struct i40e_aqc_remove_macvlan_element_data *del_list;
2023
Jacob Keller0da36b92017-04-19 09:25:55 -04002024 while (test_and_set_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002025 usleep_range(1000, 2000);
2026 pf = vsi->back;
2027
2028 if (vsi->netdev) {
2029 changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
2030 vsi->current_netdev_flags = vsi->netdev->flags;
2031 }
2032
Jacob Keller278e7d02016-10-05 09:30:37 -07002033 INIT_HLIST_HEAD(&tmp_add_list);
2034 INIT_HLIST_HEAD(&tmp_del_list);
Kiran Patil21659032015-09-30 14:09:03 -04002035
Shannon Nelson2d1de822016-05-16 10:26:44 -07002036 if (vsi->type == I40E_VSI_SRIOV)
2037 snprintf(vsi_name, sizeof(vsi_name) - 1, "VF %d", vsi->vf_id);
2038 else if (vsi->type != I40E_VSI_MAIN)
2039 snprintf(vsi_name, sizeof(vsi_name) - 1, "vsi %d", vsi->seid);
2040
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002041 if (vsi->flags & I40E_VSI_FLAG_FILTER_CHANGED) {
2042 vsi->flags &= ~I40E_VSI_FLAG_FILTER_CHANGED;
2043
Jacob Keller278e7d02016-10-05 09:30:37 -07002044 spin_lock_bh(&vsi->mac_filter_hash_lock);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002045 /* Create a list of filters to delete. */
Jacob Keller278e7d02016-10-05 09:30:37 -07002046 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002047 if (f->state == I40E_FILTER_REMOVE) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002048 /* Move the element into temporary del_list */
Jacob Keller278e7d02016-10-05 09:30:37 -07002049 hash_del(&f->hlist);
2050 hlist_add_head(&f->hlist, &tmp_del_list);
Alan Brady84f5ca62016-10-05 09:30:39 -07002051
2052 /* Avoid counting removed filters */
2053 continue;
Kiran Patil21659032015-09-30 14:09:03 -04002054 }
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002055 if (f->state == I40E_FILTER_NEW) {
Jacob Keller671889e2016-12-02 12:33:00 -08002056 /* Create a temporary i40e_new_mac_filter */
2057 new = kzalloc(sizeof(*new), GFP_ATOMIC);
2058 if (!new)
2059 goto err_no_memory_locked;
2060
2061 /* Store pointer to the real filter */
2062 new->f = f;
2063 new->state = f->state;
2064
2065 /* Add it to the hash list */
2066 hlist_add_head(&new->hlist, &tmp_add_list);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002067 }
Alan Brady84f5ca62016-10-05 09:30:39 -07002068
Jacob Keller489a3262016-11-11 12:39:31 -08002069 /* Count the number of active (current and new) VLAN
2070 * filters we have now. Does not count filters which
2071 * are marked for deletion.
Alan Brady84f5ca62016-10-05 09:30:39 -07002072 */
2073 if (f->vlan > 0)
2074 vlan_filters++;
Alan Brady84f5ca62016-10-05 09:30:39 -07002075 }
2076
Jacob Keller489a3262016-11-11 12:39:31 -08002077 retval = i40e_correct_mac_vlan_filters(vsi,
2078 &tmp_add_list,
2079 &tmp_del_list,
2080 vlan_filters);
2081 if (retval)
2082 goto err_no_memory_locked;
Alan Brady84f5ca62016-10-05 09:30:39 -07002083
Jacob Keller278e7d02016-10-05 09:30:37 -07002084 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -04002085 }
2086
2087 /* Now process 'del_list' outside the lock */
Jacob Keller278e7d02016-10-05 09:30:37 -07002088 if (!hlist_empty(&tmp_del_list)) {
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002089 filter_list_len = hw->aq.asq_buf_size /
Kiran Patil21659032015-09-30 14:09:03 -04002090 sizeof(struct i40e_aqc_remove_macvlan_element_data);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002091 list_size = filter_list_len *
Shannon Nelsonf1199992015-11-19 11:34:23 -08002092 sizeof(struct i40e_aqc_remove_macvlan_element_data);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002093 del_list = kzalloc(list_size, GFP_ATOMIC);
Jacob Keller4a2ce272016-10-05 09:30:38 -07002094 if (!del_list)
2095 goto err_no_memory;
Kiran Patil21659032015-09-30 14:09:03 -04002096
Jacob Keller278e7d02016-10-05 09:30:37 -07002097 hlist_for_each_entry_safe(f, h, &tmp_del_list, hlist) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002098 cmd_flags = 0;
2099
Jacob Keller435c0842016-11-08 13:05:10 -08002100 /* handle broadcast filters by updating the broadcast
Jacob Kellerd88d40b2016-12-02 12:32:59 -08002101 * promiscuous flag and release filter list.
Jacob Keller435c0842016-11-08 13:05:10 -08002102 */
2103 if (is_broadcast_ether_addr(f->macaddr)) {
2104 i40e_aqc_broadcast_filter(vsi, vsi_name, f);
2105
2106 hlist_del(&f->hlist);
2107 kfree(f);
2108 continue;
2109 }
2110
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002111 /* add to delete list */
Greg Rose9a173902014-05-22 06:32:02 +00002112 ether_addr_copy(del_list[num_del].mac_addr, f->macaddr);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002113 if (f->vlan == I40E_VLAN_ANY) {
2114 del_list[num_del].vlan_tag = 0;
Alan Bradya6cb9142016-09-06 18:05:07 -07002115 cmd_flags |= I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002116 } else {
2117 del_list[num_del].vlan_tag =
2118 cpu_to_le16((u16)(f->vlan));
2119 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002120
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002121 cmd_flags |= I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
2122 del_list[num_del].flags = cmd_flags;
2123 num_del++;
2124
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002125 /* flush a full buffer */
2126 if (num_del == filter_list_len) {
Jacob Keller00936312016-10-05 09:30:41 -07002127 i40e_aqc_del_filters(vsi, vsi_name, del_list,
2128 num_del, &retval);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002129 memset(del_list, 0, list_size);
Jacob Keller00936312016-10-05 09:30:41 -07002130 num_del = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002131 }
Kiran Patil21659032015-09-30 14:09:03 -04002132 /* Release memory for MAC filter entries which were
2133 * synced up with HW.
2134 */
Jacob Keller278e7d02016-10-05 09:30:37 -07002135 hlist_del(&f->hlist);
Kiran Patil21659032015-09-30 14:09:03 -04002136 kfree(f);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002137 }
Kiran Patil21659032015-09-30 14:09:03 -04002138
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002139 if (num_del) {
Jacob Keller00936312016-10-05 09:30:41 -07002140 i40e_aqc_del_filters(vsi, vsi_name, del_list,
2141 num_del, &retval);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002142 }
2143
2144 kfree(del_list);
2145 del_list = NULL;
Kiran Patil21659032015-09-30 14:09:03 -04002146 }
2147
Jacob Keller278e7d02016-10-05 09:30:37 -07002148 if (!hlist_empty(&tmp_add_list)) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002149 /* Do all the adds now. */
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002150 filter_list_len = hw->aq.asq_buf_size /
Shannon Nelsonf1199992015-11-19 11:34:23 -08002151 sizeof(struct i40e_aqc_add_macvlan_element_data);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002152 list_size = filter_list_len *
2153 sizeof(struct i40e_aqc_add_macvlan_element_data);
2154 add_list = kzalloc(list_size, GFP_ATOMIC);
Jacob Keller4a2ce272016-10-05 09:30:38 -07002155 if (!add_list)
2156 goto err_no_memory;
2157
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002158 num_add = 0;
Jacob Keller671889e2016-12-02 12:33:00 -08002159 hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
Jacob Kellerd19cb642017-04-21 13:38:05 -07002160 if (test_bit(__I40E_VSI_OVERFLOW_PROMISC,
Jacob Keller0da36b92017-04-19 09:25:55 -04002161 vsi->state)) {
Jacob Keller671889e2016-12-02 12:33:00 -08002162 new->state = I40E_FILTER_FAILED;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002163 continue;
2164 }
Jacob Keller435c0842016-11-08 13:05:10 -08002165
2166 /* handle broadcast filters by updating the broadcast
2167 * promiscuous flag instead of adding a MAC filter.
2168 */
Jacob Keller671889e2016-12-02 12:33:00 -08002169 if (is_broadcast_ether_addr(new->f->macaddr)) {
2170 if (i40e_aqc_broadcast_filter(vsi, vsi_name,
2171 new->f))
2172 new->state = I40E_FILTER_FAILED;
2173 else
2174 new->state = I40E_FILTER_ACTIVE;
Jacob Keller435c0842016-11-08 13:05:10 -08002175 continue;
2176 }
2177
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002178 /* add to add array */
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002179 if (num_add == 0)
Jacob Keller671889e2016-12-02 12:33:00 -08002180 add_head = new;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002181 cmd_flags = 0;
Jacob Keller671889e2016-12-02 12:33:00 -08002182 ether_addr_copy(add_list[num_add].mac_addr,
2183 new->f->macaddr);
2184 if (new->f->vlan == I40E_VLAN_ANY) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002185 add_list[num_add].vlan_tag = 0;
2186 cmd_flags |= I40E_AQC_MACVLAN_ADD_IGNORE_VLAN;
2187 } else {
2188 add_list[num_add].vlan_tag =
Jacob Keller671889e2016-12-02 12:33:00 -08002189 cpu_to_le16((u16)(new->f->vlan));
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002190 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002191 add_list[num_add].queue_number = 0;
Jacob Kellerac9e2392016-11-11 12:39:27 -08002192 /* set invalid match method for later detection */
Keller, Jacob E0266ac42016-12-09 13:39:21 -08002193 add_list[num_add].match_method = I40E_AQC_MM_ERR_NO_RES;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002194 cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002195 add_list[num_add].flags = cpu_to_le16(cmd_flags);
2196 num_add++;
2197
2198 /* flush a full buffer */
2199 if (num_add == filter_list_len) {
Jacob Keller00936312016-10-05 09:30:41 -07002200 i40e_aqc_add_filters(vsi, vsi_name, add_list,
2201 add_head, num_add,
2202 &promisc_changed);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002203 memset(add_list, 0, list_size);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002204 num_add = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002205 }
2206 }
2207 if (num_add) {
Jacob Keller00936312016-10-05 09:30:41 -07002208 i40e_aqc_add_filters(vsi, vsi_name, add_list, add_head,
2209 num_add, &promisc_changed);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002210 }
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002211 /* Now move all of the filters from the temp add list back to
2212 * the VSI's list.
2213 */
Jacob Keller278e7d02016-10-05 09:30:37 -07002214 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller671889e2016-12-02 12:33:00 -08002215 hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
2216 /* Only update the state if we're still NEW */
2217 if (new->f->state == I40E_FILTER_NEW)
2218 new->f->state = new->state;
2219 hlist_del(&new->hlist);
2220 kfree(new);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002221 }
Jacob Keller278e7d02016-10-05 09:30:37 -07002222 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002223 kfree(add_list);
2224 add_list = NULL;
2225 }
2226
Jacob Keller38326212016-11-11 12:39:26 -08002227 /* Determine the number of active and failed filters. */
2228 spin_lock_bh(&vsi->mac_filter_hash_lock);
2229 vsi->active_filters = 0;
2230 hash_for_each(vsi->mac_filter_hash, bkt, f, hlist) {
2231 if (f->state == I40E_FILTER_ACTIVE)
2232 vsi->active_filters++;
2233 else if (f->state == I40E_FILTER_FAILED)
2234 failed_filters++;
2235 }
2236 spin_unlock_bh(&vsi->mac_filter_hash_lock);
2237
2238 /* If promiscuous mode has changed, we need to calculate a new
2239 * threshold for when we are safe to exit
2240 */
2241 if (promisc_changed)
2242 vsi->promisc_threshold = (vsi->active_filters * 3) / 4;
2243
2244 /* Check if we are able to exit overflow promiscuous mode. We can
2245 * safely exit if we didn't just enter, we no longer have any failed
2246 * filters, and we have reduced filters below the threshold value.
2247 */
Jacob Keller0da36b92017-04-19 09:25:55 -04002248 if (test_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state) &&
Jacob Keller38326212016-11-11 12:39:26 -08002249 !promisc_changed && !failed_filters &&
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002250 (vsi->active_filters < vsi->promisc_threshold)) {
Jacob Keller38326212016-11-11 12:39:26 -08002251 dev_info(&pf->pdev->dev,
2252 "filter logjam cleared on %s, leaving overflow promiscuous mode\n",
2253 vsi_name);
Jacob Keller0da36b92017-04-19 09:25:55 -04002254 clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
Jacob Keller38326212016-11-11 12:39:26 -08002255 promisc_changed = true;
2256 vsi->promisc_threshold = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002257 }
2258
Anjali Singhai Jaina856b5c2016-04-13 03:08:23 -07002259 /* if the VF is not trusted do not do promisc */
2260 if ((vsi->type == I40E_VSI_SRIOV) && !pf->vf[vsi->vf_id].trusted) {
Jacob Keller0da36b92017-04-19 09:25:55 -04002261 clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
Anjali Singhai Jaina856b5c2016-04-13 03:08:23 -07002262 goto out;
2263 }
2264
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002265 /* check for changes in promiscuous modes */
2266 if (changed_flags & IFF_ALLMULTI) {
2267 bool cur_multipromisc;
Jesse Brandeburg6995b362015-08-28 17:55:54 -04002268
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002269 cur_multipromisc = !!(vsi->current_netdev_flags & IFF_ALLMULTI);
Mitch Williamsea02e902015-11-09 15:35:50 -08002270 aq_ret = i40e_aq_set_vsi_multicast_promiscuous(&vsi->back->hw,
2271 vsi->seid,
2272 cur_multipromisc,
2273 NULL);
2274 if (aq_ret) {
2275 retval = i40e_aq_rc_to_posix(aq_ret,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002276 hw->aq.asq_last_status);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002277 dev_info(&pf->pdev->dev,
Shannon Nelson2d1de822016-05-16 10:26:44 -07002278 "set multi promisc failed on %s, err %s aq_err %s\n",
2279 vsi_name,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002280 i40e_stat_str(hw, aq_ret),
2281 i40e_aq_str(hw, hw->aq.asq_last_status));
Mitch Williamsea02e902015-11-09 15:35:50 -08002282 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002283 }
Alan Bradye5887232017-06-07 05:43:10 -04002284
2285 if ((changed_flags & IFF_PROMISC) || promisc_changed) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002286 bool cur_promisc;
Jesse Brandeburg6995b362015-08-28 17:55:54 -04002287
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002288 cur_promisc = (!!(vsi->current_netdev_flags & IFF_PROMISC) ||
Jacob Kellerd19cb642017-04-21 13:38:05 -07002289 test_bit(__I40E_VSI_OVERFLOW_PROMISC,
Jacob Keller0da36b92017-04-19 09:25:55 -04002290 vsi->state));
Anjali Singhai Jain6784ed52016-01-15 14:33:13 -08002291 if ((vsi->type == I40E_VSI_MAIN) &&
2292 (pf->lan_veb != I40E_NO_VEB) &&
2293 !(pf->flags & I40E_FLAG_MFP_ENABLED)) {
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002294 /* set defport ON for Main VSI instead of true promisc
2295 * this way we will get all unicast/multicast and VLAN
2296 * promisc behavior but will not get VF or VMDq traffic
2297 * replicated on the Main VSI.
2298 */
2299 if (pf->cur_promisc != cur_promisc) {
2300 pf->cur_promisc = cur_promisc;
Mitch Williams5bc16032016-05-16 10:26:43 -07002301 if (cur_promisc)
2302 aq_ret =
2303 i40e_aq_set_default_vsi(hw,
2304 vsi->seid,
2305 NULL);
2306 else
2307 aq_ret =
2308 i40e_aq_clear_default_vsi(hw,
2309 vsi->seid,
2310 NULL);
2311 if (aq_ret) {
2312 retval = i40e_aq_rc_to_posix(aq_ret,
2313 hw->aq.asq_last_status);
2314 dev_info(&pf->pdev->dev,
Shannon Nelson2d1de822016-05-16 10:26:44 -07002315 "Set default VSI failed on %s, err %s, aq_err %s\n",
2316 vsi_name,
Mitch Williams5bc16032016-05-16 10:26:43 -07002317 i40e_stat_str(hw, aq_ret),
2318 i40e_aq_str(hw,
2319 hw->aq.asq_last_status));
2320 }
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002321 }
2322 } else {
Mitch Williamsea02e902015-11-09 15:35:50 -08002323 aq_ret = i40e_aq_set_vsi_unicast_promiscuous(
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002324 hw,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002325 vsi->seid,
Anjali Singhai Jainb5569892016-05-03 15:13:12 -07002326 cur_promisc, NULL,
2327 true);
Mitch Williamsea02e902015-11-09 15:35:50 -08002328 if (aq_ret) {
2329 retval =
2330 i40e_aq_rc_to_posix(aq_ret,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002331 hw->aq.asq_last_status);
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002332 dev_info(&pf->pdev->dev,
Shannon Nelson2d1de822016-05-16 10:26:44 -07002333 "set unicast promisc failed on %s, err %s, aq_err %s\n",
2334 vsi_name,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002335 i40e_stat_str(hw, aq_ret),
2336 i40e_aq_str(hw,
2337 hw->aq.asq_last_status));
Mitch Williamsea02e902015-11-09 15:35:50 -08002338 }
2339 aq_ret = i40e_aq_set_vsi_multicast_promiscuous(
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002340 hw,
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002341 vsi->seid,
2342 cur_promisc, NULL);
Mitch Williamsea02e902015-11-09 15:35:50 -08002343 if (aq_ret) {
2344 retval =
2345 i40e_aq_rc_to_posix(aq_ret,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002346 hw->aq.asq_last_status);
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002347 dev_info(&pf->pdev->dev,
Shannon Nelson2d1de822016-05-16 10:26:44 -07002348 "set multicast promisc failed on %s, err %s, aq_err %s\n",
2349 vsi_name,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002350 i40e_stat_str(hw, aq_ret),
2351 i40e_aq_str(hw,
2352 hw->aq.asq_last_status));
Mitch Williamsea02e902015-11-09 15:35:50 -08002353 }
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002354 }
Mitch Williamsea02e902015-11-09 15:35:50 -08002355 aq_ret = i40e_aq_set_vsi_broadcast(&vsi->back->hw,
2356 vsi->seid,
2357 cur_promisc, NULL);
2358 if (aq_ret) {
2359 retval = i40e_aq_rc_to_posix(aq_ret,
2360 pf->hw.aq.asq_last_status);
Greg Rose1a103702013-11-28 06:42:39 +00002361 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002362 "set brdcast promisc failed, err %s, aq_err %s\n",
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002363 i40e_stat_str(hw, aq_ret),
2364 i40e_aq_str(hw,
2365 hw->aq.asq_last_status));
Mitch Williamsea02e902015-11-09 15:35:50 -08002366 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002367 }
Mitch Williamsea02e902015-11-09 15:35:50 -08002368out:
Jesse Brandeburg2818ccd2016-01-13 16:51:38 -08002369 /* if something went wrong then set the changed flag so we try again */
2370 if (retval)
2371 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2372
Jacob Keller0da36b92017-04-19 09:25:55 -04002373 clear_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state);
Mitch Williamsea02e902015-11-09 15:35:50 -08002374 return retval;
Jacob Keller4a2ce272016-10-05 09:30:38 -07002375
2376err_no_memory:
2377 /* Restore elements on the temporary add and delete lists */
2378 spin_lock_bh(&vsi->mac_filter_hash_lock);
Alan Brady84f5ca62016-10-05 09:30:39 -07002379err_no_memory_locked:
Jacob Keller671889e2016-12-02 12:33:00 -08002380 i40e_undo_del_filter_entries(vsi, &tmp_del_list);
2381 i40e_undo_add_filter_entries(vsi, &tmp_add_list);
Jacob Keller4a2ce272016-10-05 09:30:38 -07002382 spin_unlock_bh(&vsi->mac_filter_hash_lock);
2383
2384 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
Jacob Keller0da36b92017-04-19 09:25:55 -04002385 clear_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state);
Jacob Keller4a2ce272016-10-05 09:30:38 -07002386 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002387}
2388
2389/**
2390 * i40e_sync_filters_subtask - Sync the VSI filter list with HW
2391 * @pf: board private structure
2392 **/
2393static void i40e_sync_filters_subtask(struct i40e_pf *pf)
2394{
2395 int v;
2396
2397 if (!pf || !(pf->flags & I40E_FLAG_FILTER_SYNC))
2398 return;
2399 pf->flags &= ~I40E_FLAG_FILTER_SYNC;
2400
Mitch Williams505682c2014-05-20 08:01:37 +00002401 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002402 if (pf->vsi[v] &&
Jesse Brandeburg17652c62015-11-05 17:01:02 -08002403 (pf->vsi[v]->flags & I40E_VSI_FLAG_FILTER_CHANGED)) {
2404 int ret = i40e_sync_vsi_filters(pf->vsi[v]);
2405
2406 if (ret) {
2407 /* come back and try again later */
2408 pf->flags |= I40E_FLAG_FILTER_SYNC;
2409 break;
2410 }
2411 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002412 }
2413}
2414
2415/**
Björn Töpel0c8493d2017-05-24 07:55:34 +02002416 * i40e_max_xdp_frame_size - returns the maximum allowed frame size for XDP
2417 * @vsi: the vsi
2418 **/
2419static int i40e_max_xdp_frame_size(struct i40e_vsi *vsi)
2420{
2421 if (PAGE_SIZE >= 8192 || (vsi->back->flags & I40E_FLAG_LEGACY_RX))
2422 return I40E_RXBUFFER_2048;
2423 else
2424 return I40E_RXBUFFER_3072;
2425}
2426
2427/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002428 * i40e_change_mtu - NDO callback to change the Maximum Transfer Unit
2429 * @netdev: network interface device structure
2430 * @new_mtu: new value for maximum frame size
2431 *
2432 * Returns 0 on success, negative on failure
2433 **/
2434static int i40e_change_mtu(struct net_device *netdev, int new_mtu)
2435{
2436 struct i40e_netdev_priv *np = netdev_priv(netdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002437 struct i40e_vsi *vsi = np->vsi;
Mitch Williams0ef2d5a2017-01-24 10:24:00 -08002438 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002439
Björn Töpel0c8493d2017-05-24 07:55:34 +02002440 if (i40e_enabled_xdp_vsi(vsi)) {
2441 int frame_size = new_mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
2442
2443 if (frame_size > i40e_max_xdp_frame_size(vsi))
2444 return -EINVAL;
2445 }
2446
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002447 netdev_info(netdev, "changing MTU from %d to %d\n",
2448 netdev->mtu, new_mtu);
2449 netdev->mtu = new_mtu;
2450 if (netif_running(netdev))
2451 i40e_vsi_reinit_locked(vsi);
Mitch Williams0ef2d5a2017-01-24 10:24:00 -08002452 pf->flags |= (I40E_FLAG_SERVICE_CLIENT_REQUESTED |
2453 I40E_FLAG_CLIENT_L2_CHANGE);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002454 return 0;
2455}
2456
2457/**
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00002458 * i40e_ioctl - Access the hwtstamp interface
2459 * @netdev: network interface device structure
2460 * @ifr: interface request data
2461 * @cmd: ioctl command
2462 **/
2463int i40e_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
2464{
2465 struct i40e_netdev_priv *np = netdev_priv(netdev);
2466 struct i40e_pf *pf = np->vsi->back;
2467
2468 switch (cmd) {
2469 case SIOCGHWTSTAMP:
2470 return i40e_ptp_get_ts_config(pf, ifr);
2471 case SIOCSHWTSTAMP:
2472 return i40e_ptp_set_ts_config(pf, ifr);
2473 default:
2474 return -EOPNOTSUPP;
2475 }
2476}
2477
2478/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002479 * i40e_vlan_stripping_enable - Turn on vlan stripping for the VSI
2480 * @vsi: the vsi being adjusted
2481 **/
2482void i40e_vlan_stripping_enable(struct i40e_vsi *vsi)
2483{
2484 struct i40e_vsi_context ctxt;
2485 i40e_status ret;
2486
2487 if ((vsi->info.valid_sections &
2488 cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
2489 ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_MODE_MASK) == 0))
2490 return; /* already enabled */
2491
2492 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2493 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
2494 I40E_AQ_VSI_PVLAN_EMOD_STR_BOTH;
2495
2496 ctxt.seid = vsi->seid;
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07002497 ctxt.info = vsi->info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002498 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2499 if (ret) {
2500 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002501 "update vlan stripping failed, err %s aq_err %s\n",
2502 i40e_stat_str(&vsi->back->hw, ret),
2503 i40e_aq_str(&vsi->back->hw,
2504 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002505 }
2506}
2507
2508/**
2509 * i40e_vlan_stripping_disable - Turn off vlan stripping for the VSI
2510 * @vsi: the vsi being adjusted
2511 **/
2512void i40e_vlan_stripping_disable(struct i40e_vsi *vsi)
2513{
2514 struct i40e_vsi_context ctxt;
2515 i40e_status ret;
2516
2517 if ((vsi->info.valid_sections &
2518 cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
2519 ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_EMOD_MASK) ==
2520 I40E_AQ_VSI_PVLAN_EMOD_MASK))
2521 return; /* already disabled */
2522
2523 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2524 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
2525 I40E_AQ_VSI_PVLAN_EMOD_NOTHING;
2526
2527 ctxt.seid = vsi->seid;
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07002528 ctxt.info = vsi->info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002529 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2530 if (ret) {
2531 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002532 "update vlan stripping failed, err %s aq_err %s\n",
2533 i40e_stat_str(&vsi->back->hw, ret),
2534 i40e_aq_str(&vsi->back->hw,
2535 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002536 }
2537}
2538
2539/**
2540 * i40e_vlan_rx_register - Setup or shutdown vlan offload
2541 * @netdev: network interface to be adjusted
2542 * @features: netdev features to test if VLAN offload is enabled or not
2543 **/
2544static void i40e_vlan_rx_register(struct net_device *netdev, u32 features)
2545{
2546 struct i40e_netdev_priv *np = netdev_priv(netdev);
2547 struct i40e_vsi *vsi = np->vsi;
2548
2549 if (features & NETIF_F_HW_VLAN_CTAG_RX)
2550 i40e_vlan_stripping_enable(vsi);
2551 else
2552 i40e_vlan_stripping_disable(vsi);
2553}
2554
2555/**
Jacob Keller490a4ad2016-11-11 12:39:29 -08002556 * i40e_add_vlan_all_mac - Add a MAC/VLAN filter for each existing MAC address
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002557 * @vsi: the vsi being configured
2558 * @vid: vlan id to be added (0 = untagged only , -1 = any)
Jacob Keller490a4ad2016-11-11 12:39:29 -08002559 *
2560 * This is a helper function for adding a new MAC/VLAN filter with the
2561 * specified VLAN for each existing MAC address already in the hash table.
2562 * This function does *not* perform any accounting to update filters based on
2563 * VLAN mode.
2564 *
2565 * NOTE: this function expects to be called while under the
2566 * mac_filter_hash_lock
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002567 **/
Jacob Keller9af52f62016-11-11 12:39:30 -08002568int i40e_add_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002569{
Jacob Keller490a4ad2016-11-11 12:39:29 -08002570 struct i40e_mac_filter *f, *add_f;
Jacob Keller278e7d02016-10-05 09:30:37 -07002571 struct hlist_node *h;
2572 int bkt;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002573
Jacob Keller278e7d02016-10-05 09:30:37 -07002574 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Jacob Keller57b341d2016-10-05 09:30:35 -07002575 if (f->state == I40E_FILTER_REMOVE)
2576 continue;
Jacob Keller1bc87e82016-10-05 09:30:31 -07002577 add_f = i40e_add_filter(vsi, f->macaddr, vid);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002578 if (!add_f) {
2579 dev_info(&vsi->back->pdev->dev,
2580 "Could not add vlan filter %d for %pM\n",
2581 vid, f->macaddr);
2582 return -ENOMEM;
2583 }
2584 }
2585
Jacob Keller490a4ad2016-11-11 12:39:29 -08002586 return 0;
2587}
2588
2589/**
2590 * i40e_vsi_add_vlan - Add VSI membership for given VLAN
2591 * @vsi: the VSI being configured
Jacob Kellerf94484b2016-12-07 14:05:34 -08002592 * @vid: VLAN id to be added
Jacob Keller490a4ad2016-11-11 12:39:29 -08002593 **/
Jacob Kellerf94484b2016-12-07 14:05:34 -08002594int i40e_vsi_add_vlan(struct i40e_vsi *vsi, u16 vid)
Jacob Keller490a4ad2016-11-11 12:39:29 -08002595{
Jacob Keller489a3262016-11-11 12:39:31 -08002596 int err;
Jacob Keller490a4ad2016-11-11 12:39:29 -08002597
Jacob Kellerfcf6cfc2017-06-23 04:24:51 -04002598 if (vsi->info.pvid)
Jacob Kellerf94484b2016-12-07 14:05:34 -08002599 return -EINVAL;
2600
Jacob Kellerfcf6cfc2017-06-23 04:24:51 -04002601 /* The network stack will attempt to add VID=0, with the intention to
2602 * receive priority tagged packets with a VLAN of 0. Our HW receives
2603 * these packets by default when configured to receive untagged
2604 * packets, so we don't need to add a filter for this case.
2605 * Additionally, HW interprets adding a VID=0 filter as meaning to
2606 * receive *only* tagged traffic and stops receiving untagged traffic.
2607 * Thus, we do not want to actually add a filter for VID=0
2608 */
2609 if (!vid)
2610 return 0;
2611
Jacob Keller490a4ad2016-11-11 12:39:29 -08002612 /* Locked once because all functions invoked below iterates list*/
2613 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller490a4ad2016-11-11 12:39:29 -08002614 err = i40e_add_vlan_all_mac(vsi, vid);
Jacob Keller278e7d02016-10-05 09:30:37 -07002615 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller489a3262016-11-11 12:39:31 -08002616 if (err)
2617 return err;
Kiran Patil21659032015-09-30 14:09:03 -04002618
Jesse Brandeburg0e4425e2015-11-05 17:01:01 -08002619 /* schedule our worker thread which will take care of
2620 * applying the new filter changes
2621 */
2622 i40e_service_event_schedule(vsi->back);
2623 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002624}
2625
2626/**
Jacob Keller490a4ad2016-11-11 12:39:29 -08002627 * i40e_rm_vlan_all_mac - Remove MAC/VLAN pair for all MAC with the given VLAN
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002628 * @vsi: the vsi being configured
2629 * @vid: vlan id to be removed (0 = untagged only , -1 = any)
Jacob Keller490a4ad2016-11-11 12:39:29 -08002630 *
2631 * This function should be used to remove all VLAN filters which match the
2632 * given VID. It does not schedule the service event and does not take the
2633 * mac_filter_hash_lock so it may be combined with other operations under
2634 * a single invocation of the mac_filter_hash_lock.
2635 *
2636 * NOTE: this function expects to be called while under the
2637 * mac_filter_hash_lock
2638 */
Jacob Keller9af52f62016-11-11 12:39:30 -08002639void i40e_rm_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002640{
Alan Brady84f5ca62016-10-05 09:30:39 -07002641 struct i40e_mac_filter *f;
Jacob Keller278e7d02016-10-05 09:30:37 -07002642 struct hlist_node *h;
Jacob Keller278e7d02016-10-05 09:30:37 -07002643 int bkt;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002644
Jacob Keller278e7d02016-10-05 09:30:37 -07002645 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Jacob Keller290d2552016-10-05 09:30:36 -07002646 if (f->vlan == vid)
2647 __i40e_del_filter(vsi, f);
2648 }
Jacob Keller490a4ad2016-11-11 12:39:29 -08002649}
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002650
Jacob Keller490a4ad2016-11-11 12:39:29 -08002651/**
2652 * i40e_vsi_kill_vlan - Remove VSI membership for given VLAN
2653 * @vsi: the VSI being configured
Jacob Kellerf94484b2016-12-07 14:05:34 -08002654 * @vid: VLAN id to be removed
Jacob Keller490a4ad2016-11-11 12:39:29 -08002655 **/
Jacob Kellerf94484b2016-12-07 14:05:34 -08002656void i40e_vsi_kill_vlan(struct i40e_vsi *vsi, u16 vid)
Jacob Keller490a4ad2016-11-11 12:39:29 -08002657{
Jacob Kellerf94484b2016-12-07 14:05:34 -08002658 if (!vid || vsi->info.pvid)
2659 return;
2660
Jacob Keller490a4ad2016-11-11 12:39:29 -08002661 spin_lock_bh(&vsi->mac_filter_hash_lock);
2662 i40e_rm_vlan_all_mac(vsi, vid);
Jacob Keller278e7d02016-10-05 09:30:37 -07002663 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -04002664
Jesse Brandeburg0e4425e2015-11-05 17:01:01 -08002665 /* schedule our worker thread which will take care of
2666 * applying the new filter changes
2667 */
2668 i40e_service_event_schedule(vsi->back);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002669}
2670
2671/**
2672 * i40e_vlan_rx_add_vid - Add a vlan id filter to HW offload
2673 * @netdev: network interface to be adjusted
2674 * @vid: vlan id to be added
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002675 *
2676 * net_device_ops implementation for adding vlan ids
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002677 **/
2678static int i40e_vlan_rx_add_vid(struct net_device *netdev,
2679 __always_unused __be16 proto, u16 vid)
2680{
2681 struct i40e_netdev_priv *np = netdev_priv(netdev);
2682 struct i40e_vsi *vsi = np->vsi;
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002683 int ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002684
Jacob Keller6a1127852016-10-25 16:08:49 -07002685 if (vid >= VLAN_N_VID)
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002686 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002687
Jacob Kellerfcf6cfc2017-06-23 04:24:51 -04002688 ret = i40e_vsi_add_vlan(vsi, vid);
Jacob Keller6a1127852016-10-25 16:08:49 -07002689 if (!ret)
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002690 set_bit(vid, vsi->active_vlans);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002691
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002692 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002693}
2694
2695/**
2696 * i40e_vlan_rx_kill_vid - Remove a vlan id filter from HW offload
2697 * @netdev: network interface to be adjusted
2698 * @vid: vlan id to be removed
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002699 *
Akeem G Abodunrinfdfd9432014-02-11 08:24:15 +00002700 * net_device_ops implementation for removing vlan ids
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002701 **/
2702static int i40e_vlan_rx_kill_vid(struct net_device *netdev,
2703 __always_unused __be16 proto, u16 vid)
2704{
2705 struct i40e_netdev_priv *np = netdev_priv(netdev);
2706 struct i40e_vsi *vsi = np->vsi;
2707
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002708 /* return code is ignored as there is nothing a user
2709 * can do about failure to remove and a log message was
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002710 * already printed from the other function
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002711 */
2712 i40e_vsi_kill_vlan(vsi, vid);
2713
2714 clear_bit(vid, vsi->active_vlans);
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002715
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002716 return 0;
2717}
2718
2719/**
2720 * i40e_restore_vlan - Reinstate vlans when vsi/netdev comes back up
2721 * @vsi: the vsi being brought back up
2722 **/
2723static void i40e_restore_vlan(struct i40e_vsi *vsi)
2724{
2725 u16 vid;
2726
2727 if (!vsi->netdev)
2728 return;
2729
2730 i40e_vlan_rx_register(vsi->netdev, vsi->netdev->features);
2731
2732 for_each_set_bit(vid, vsi->active_vlans, VLAN_N_VID)
2733 i40e_vlan_rx_add_vid(vsi->netdev, htons(ETH_P_8021Q),
2734 vid);
2735}
2736
2737/**
2738 * i40e_vsi_add_pvid - Add pvid for the VSI
2739 * @vsi: the vsi being adjusted
2740 * @vid: the vlan id to set as a PVID
2741 **/
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002742int i40e_vsi_add_pvid(struct i40e_vsi *vsi, u16 vid)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002743{
2744 struct i40e_vsi_context ctxt;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002745 i40e_status ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002746
2747 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2748 vsi->info.pvid = cpu_to_le16(vid);
Greg Rose6c12fcb2013-11-28 06:39:34 +00002749 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_TAGGED |
2750 I40E_AQ_VSI_PVLAN_INSERT_PVID |
Greg Roseb774c7d2013-11-28 06:39:44 +00002751 I40E_AQ_VSI_PVLAN_EMOD_STR;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002752
2753 ctxt.seid = vsi->seid;
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07002754 ctxt.info = vsi->info;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002755 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2756 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002757 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002758 "add pvid failed, err %s aq_err %s\n",
2759 i40e_stat_str(&vsi->back->hw, ret),
2760 i40e_aq_str(&vsi->back->hw,
2761 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002762 return -ENOENT;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002763 }
2764
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002765 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002766}
2767
2768/**
2769 * i40e_vsi_remove_pvid - Remove the pvid from the VSI
2770 * @vsi: the vsi being adjusted
2771 *
2772 * Just use the vlan_rx_register() service to put it back to normal
2773 **/
2774void i40e_vsi_remove_pvid(struct i40e_vsi *vsi)
2775{
Greg Rose6c12fcb2013-11-28 06:39:34 +00002776 i40e_vlan_stripping_disable(vsi);
2777
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002778 vsi->info.pvid = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002779}
2780
2781/**
2782 * i40e_vsi_setup_tx_resources - Allocate VSI Tx queue resources
2783 * @vsi: ptr to the VSI
2784 *
2785 * If this function returns with an error, then it's possible one or
2786 * more of the rings is populated (while the rest are not). It is the
2787 * callers duty to clean those orphaned rings.
2788 *
2789 * Return 0 on success, negative on failure
2790 **/
2791static int i40e_vsi_setup_tx_resources(struct i40e_vsi *vsi)
2792{
2793 int i, err = 0;
2794
2795 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e152013-09-28 06:00:58 +00002796 err = i40e_setup_tx_descriptors(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002797
Björn Töpel74608d12017-05-24 07:55:35 +02002798 if (!i40e_enabled_xdp_vsi(vsi))
2799 return err;
2800
2801 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
2802 err = i40e_setup_tx_descriptors(vsi->xdp_rings[i]);
2803
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002804 return err;
2805}
2806
2807/**
2808 * i40e_vsi_free_tx_resources - Free Tx resources for VSI queues
2809 * @vsi: ptr to the VSI
2810 *
2811 * Free VSI's transmit software resources
2812 **/
2813static void i40e_vsi_free_tx_resources(struct i40e_vsi *vsi)
2814{
2815 int i;
2816
Björn Töpel74608d12017-05-24 07:55:35 +02002817 if (vsi->tx_rings) {
2818 for (i = 0; i < vsi->num_queue_pairs; i++)
2819 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
2820 i40e_free_tx_resources(vsi->tx_rings[i]);
2821 }
Greg Rose8e9dca52013-12-18 13:45:53 +00002822
Björn Töpel74608d12017-05-24 07:55:35 +02002823 if (vsi->xdp_rings) {
2824 for (i = 0; i < vsi->num_queue_pairs; i++)
2825 if (vsi->xdp_rings[i] && vsi->xdp_rings[i]->desc)
2826 i40e_free_tx_resources(vsi->xdp_rings[i]);
2827 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002828}
2829
2830/**
2831 * i40e_vsi_setup_rx_resources - Allocate VSI queues Rx resources
2832 * @vsi: ptr to the VSI
2833 *
2834 * If this function returns with an error, then it's possible one or
2835 * more of the rings is populated (while the rest are not). It is the
2836 * callers duty to clean those orphaned rings.
2837 *
2838 * Return 0 on success, negative on failure
2839 **/
2840static int i40e_vsi_setup_rx_resources(struct i40e_vsi *vsi)
2841{
2842 int i, err = 0;
2843
2844 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e152013-09-28 06:00:58 +00002845 err = i40e_setup_rx_descriptors(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002846 return err;
2847}
2848
2849/**
2850 * i40e_vsi_free_rx_resources - Free Rx Resources for VSI queues
2851 * @vsi: ptr to the VSI
2852 *
2853 * Free all receive software resources
2854 **/
2855static void i40e_vsi_free_rx_resources(struct i40e_vsi *vsi)
2856{
2857 int i;
2858
Greg Rose8e9dca52013-12-18 13:45:53 +00002859 if (!vsi->rx_rings)
2860 return;
2861
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002862 for (i = 0; i < vsi->num_queue_pairs; i++)
Greg Rose8e9dca52013-12-18 13:45:53 +00002863 if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
Alexander Duyck9f65e152013-09-28 06:00:58 +00002864 i40e_free_rx_resources(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002865}
2866
2867/**
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002868 * i40e_config_xps_tx_ring - Configure XPS for a Tx ring
2869 * @ring: The Tx ring to configure
2870 *
2871 * This enables/disables XPS for a given Tx descriptor ring
2872 * based on the TCs enabled for the VSI that ring belongs to.
2873 **/
2874static void i40e_config_xps_tx_ring(struct i40e_ring *ring)
2875{
2876 struct i40e_vsi *vsi = ring->vsi;
2877 cpumask_var_t mask;
2878
Jesse Brandeburg9a660ee2015-02-26 16:13:22 +00002879 if (!ring->q_vector || !ring->netdev)
2880 return;
2881
2882 /* Single TC mode enable XPS */
2883 if (vsi->tc_config.numtc <= 1) {
2884 if (!test_and_set_bit(__I40E_TX_XPS_INIT_DONE, &ring->state))
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002885 netif_set_xps_queue(ring->netdev,
2886 &ring->q_vector->affinity_mask,
2887 ring->queue_index);
Jesse Brandeburg9a660ee2015-02-26 16:13:22 +00002888 } else if (alloc_cpumask_var(&mask, GFP_KERNEL)) {
2889 /* Disable XPS to allow selection based on TC */
2890 bitmap_zero(cpumask_bits(mask), nr_cpumask_bits);
2891 netif_set_xps_queue(ring->netdev, mask, ring->queue_index);
2892 free_cpumask_var(mask);
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002893 }
Jesse Brandeburg0e4425e2015-11-05 17:01:01 -08002894
2895 /* schedule our worker thread which will take care of
2896 * applying the new filter changes
2897 */
2898 i40e_service_event_schedule(vsi->back);
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002899}
2900
2901/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002902 * i40e_configure_tx_ring - Configure a transmit ring context and rest
2903 * @ring: The Tx ring to configure
2904 *
2905 * Configure the Tx descriptor ring in the HMC context.
2906 **/
2907static int i40e_configure_tx_ring(struct i40e_ring *ring)
2908{
2909 struct i40e_vsi *vsi = ring->vsi;
2910 u16 pf_q = vsi->base_queue + ring->queue_index;
2911 struct i40e_hw *hw = &vsi->back->hw;
2912 struct i40e_hmc_obj_txq tx_ctx;
2913 i40e_status err = 0;
2914 u32 qtx_ctl = 0;
2915
2916 /* some ATR related tx ring init */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08002917 if (vsi->back->flags & I40E_FLAG_FD_ATR_ENABLED) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002918 ring->atr_sample_rate = vsi->back->atr_sample_rate;
2919 ring->atr_count = 0;
2920 } else {
2921 ring->atr_sample_rate = 0;
2922 }
2923
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002924 /* configure XPS */
2925 i40e_config_xps_tx_ring(ring);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002926
2927 /* clear the context structure first */
2928 memset(&tx_ctx, 0, sizeof(tx_ctx));
2929
2930 tx_ctx.new_context = 1;
2931 tx_ctx.base = (ring->dma / 128);
2932 tx_ctx.qlen = ring->count;
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08002933 tx_ctx.fd_ena = !!(vsi->back->flags & (I40E_FLAG_FD_SB_ENABLED |
2934 I40E_FLAG_FD_ATR_ENABLED));
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00002935 tx_ctx.timesync_ena = !!(vsi->back->flags & I40E_FLAG_PTP);
Jesse Brandeburg1943d8b2014-02-14 02:14:40 +00002936 /* FDIR VSI tx ring can still use RS bit and writebacks */
2937 if (vsi->type != I40E_VSI_FDIR)
2938 tx_ctx.head_wb_ena = 1;
2939 tx_ctx.head_wb_addr = ring->dma +
2940 (ring->count * sizeof(struct i40e_tx_desc));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002941
2942 /* As part of VSI creation/update, FW allocates certain
2943 * Tx arbitration queue sets for each TC enabled for
2944 * the VSI. The FW returns the handles to these queue
2945 * sets as part of the response buffer to Add VSI,
2946 * Update VSI, etc. AQ commands. It is expected that
2947 * these queue set handles be associated with the Tx
2948 * queues by the driver as part of the TX queue context
2949 * initialization. This has to be done regardless of
2950 * DCB as by default everything is mapped to TC0.
2951 */
2952 tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[ring->dcb_tc]);
2953 tx_ctx.rdylist_act = 0;
2954
2955 /* clear the context in the HMC */
2956 err = i40e_clear_lan_tx_queue_context(hw, pf_q);
2957 if (err) {
2958 dev_info(&vsi->back->pdev->dev,
2959 "Failed to clear LAN Tx queue context on Tx ring %d (pf_q %d), error: %d\n",
2960 ring->queue_index, pf_q, err);
2961 return -ENOMEM;
2962 }
2963
2964 /* set the context in the HMC */
2965 err = i40e_set_lan_tx_queue_context(hw, pf_q, &tx_ctx);
2966 if (err) {
2967 dev_info(&vsi->back->pdev->dev,
2968 "Failed to set LAN Tx queue context on Tx ring %d (pf_q %d, error: %d\n",
2969 ring->queue_index, pf_q, err);
2970 return -ENOMEM;
2971 }
2972
2973 /* Now associate this queue with this PCI function */
Mitch Williams7a28d882014-10-17 03:14:52 +00002974 if (vsi->type == I40E_VSI_VMDQ2) {
Shannon Nelson9d8bf542014-01-14 00:49:50 -08002975 qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
Mitch Williams7a28d882014-10-17 03:14:52 +00002976 qtx_ctl |= ((vsi->id) << I40E_QTX_CTL_VFVM_INDX_SHIFT) &
2977 I40E_QTX_CTL_VFVM_INDX_MASK;
2978 } else {
Shannon Nelson9d8bf542014-01-14 00:49:50 -08002979 qtx_ctl = I40E_QTX_CTL_PF_QUEUE;
Mitch Williams7a28d882014-10-17 03:14:52 +00002980 }
2981
Shannon Nelson13fd9772013-09-28 07:14:19 +00002982 qtx_ctl |= ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT) &
2983 I40E_QTX_CTL_PF_INDX_MASK);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002984 wr32(hw, I40E_QTX_CTL(pf_q), qtx_ctl);
2985 i40e_flush(hw);
2986
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002987 /* cache tail off for easier writes later */
2988 ring->tail = hw->hw_addr + I40E_QTX_TAIL(pf_q);
2989
2990 return 0;
2991}
2992
2993/**
2994 * i40e_configure_rx_ring - Configure a receive ring context
2995 * @ring: The Rx ring to configure
2996 *
2997 * Configure the Rx descriptor ring in the HMC context.
2998 **/
2999static int i40e_configure_rx_ring(struct i40e_ring *ring)
3000{
3001 struct i40e_vsi *vsi = ring->vsi;
3002 u32 chain_len = vsi->back->hw.func_caps.rx_buf_chain_len;
3003 u16 pf_q = vsi->base_queue + ring->queue_index;
3004 struct i40e_hw *hw = &vsi->back->hw;
3005 struct i40e_hmc_obj_rxq rx_ctx;
3006 i40e_status err = 0;
3007
3008 ring->state = 0;
3009
3010 /* clear the context structure first */
3011 memset(&rx_ctx, 0, sizeof(rx_ctx));
3012
3013 ring->rx_buf_len = vsi->rx_buf_len;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003014
Alexander Duyckdab86af2017-03-14 10:15:27 -07003015 rx_ctx.dbuff = DIV_ROUND_UP(ring->rx_buf_len,
3016 BIT_ULL(I40E_RXQ_CTX_DBUFF_SHIFT));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003017
3018 rx_ctx.base = (ring->dma / 128);
3019 rx_ctx.qlen = ring->count;
3020
Jesse Brandeburgbec60fc2016-04-18 11:33:47 -07003021 /* use 32 byte descriptors */
3022 rx_ctx.dsize = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003023
Jesse Brandeburgbec60fc2016-04-18 11:33:47 -07003024 /* descriptor type is always zero
3025 * rx_ctx.dtype = 0;
3026 */
Jesse Brandeburgb32bfa172016-04-18 11:33:42 -07003027 rx_ctx.hsplit_0 = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003028
Jesse Brandeburgb32bfa172016-04-18 11:33:42 -07003029 rx_ctx.rxmax = min_t(u16, vsi->max_frame, chain_len * ring->rx_buf_len);
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00003030 if (hw->revision_id == 0)
3031 rx_ctx.lrxqthresh = 0;
3032 else
3033 rx_ctx.lrxqthresh = 2;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003034 rx_ctx.crcstrip = 1;
3035 rx_ctx.l2tsel = 1;
Jesse Brandeburgc4bbac32015-09-28 11:21:48 -07003036 /* this controls whether VLAN is stripped from inner headers */
3037 rx_ctx.showiv = 0;
Catherine Sullivanacb36762014-03-06 09:02:30 +00003038 /* set the prefena field to 1 because the manual says to */
3039 rx_ctx.prefena = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003040
3041 /* clear the context in the HMC */
3042 err = i40e_clear_lan_rx_queue_context(hw, pf_q);
3043 if (err) {
3044 dev_info(&vsi->back->pdev->dev,
3045 "Failed to clear LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
3046 ring->queue_index, pf_q, err);
3047 return -ENOMEM;
3048 }
3049
3050 /* set the context in the HMC */
3051 err = i40e_set_lan_rx_queue_context(hw, pf_q, &rx_ctx);
3052 if (err) {
3053 dev_info(&vsi->back->pdev->dev,
3054 "Failed to set LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
3055 ring->queue_index, pf_q, err);
3056 return -ENOMEM;
3057 }
3058
Alexander Duyckca9ec082017-04-05 07:51:02 -04003059 /* configure Rx buffer alignment */
3060 if (!vsi->netdev || (vsi->back->flags & I40E_FLAG_LEGACY_RX))
3061 clear_ring_build_skb_enabled(ring);
3062 else
3063 set_ring_build_skb_enabled(ring);
3064
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003065 /* cache tail for quicker writes, and clear the reg before use */
3066 ring->tail = hw->hw_addr + I40E_QRX_TAIL(pf_q);
3067 writel(0, ring->tail);
3068
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07003069 i40e_alloc_rx_buffers(ring, I40E_DESC_UNUSED(ring));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003070
3071 return 0;
3072}
3073
3074/**
3075 * i40e_vsi_configure_tx - Configure the VSI for Tx
3076 * @vsi: VSI structure describing this set of rings and resources
3077 *
3078 * Configure the Tx VSI for operation.
3079 **/
3080static int i40e_vsi_configure_tx(struct i40e_vsi *vsi)
3081{
3082 int err = 0;
3083 u16 i;
3084
Alexander Duyck9f65e152013-09-28 06:00:58 +00003085 for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
3086 err = i40e_configure_tx_ring(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003087
Björn Töpel74608d12017-05-24 07:55:35 +02003088 if (!i40e_enabled_xdp_vsi(vsi))
3089 return err;
3090
3091 for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
3092 err = i40e_configure_tx_ring(vsi->xdp_rings[i]);
3093
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003094 return err;
3095}
3096
3097/**
3098 * i40e_vsi_configure_rx - Configure the VSI for Rx
3099 * @vsi: the VSI being configured
3100 *
3101 * Configure the Rx VSI for operation.
3102 **/
3103static int i40e_vsi_configure_rx(struct i40e_vsi *vsi)
3104{
3105 int err = 0;
3106 u16 i;
3107
Alexander Duyckdab86af2017-03-14 10:15:27 -07003108 if (!vsi->netdev || (vsi->back->flags & I40E_FLAG_LEGACY_RX)) {
3109 vsi->max_frame = I40E_MAX_RXBUFFER;
3110 vsi->rx_buf_len = I40E_RXBUFFER_2048;
3111#if (PAGE_SIZE < 8192)
Alexander Duyckca9ec082017-04-05 07:51:02 -04003112 } else if (!I40E_2K_TOO_SMALL_WITH_PADDING &&
3113 (vsi->netdev->mtu <= ETH_DATA_LEN)) {
Alexander Duyckdab86af2017-03-14 10:15:27 -07003114 vsi->max_frame = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
3115 vsi->rx_buf_len = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
3116#endif
3117 } else {
3118 vsi->max_frame = I40E_MAX_RXBUFFER;
Alexander Duyck98efd692017-04-05 07:51:01 -04003119 vsi->rx_buf_len = (PAGE_SIZE < 8192) ? I40E_RXBUFFER_3072 :
3120 I40E_RXBUFFER_2048;
Alexander Duyckdab86af2017-03-14 10:15:27 -07003121 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003122
3123 /* set up individual rings */
3124 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e152013-09-28 06:00:58 +00003125 err = i40e_configure_rx_ring(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003126
3127 return err;
3128}
3129
3130/**
3131 * i40e_vsi_config_dcb_rings - Update rings to reflect DCB TC
3132 * @vsi: ptr to the VSI
3133 **/
3134static void i40e_vsi_config_dcb_rings(struct i40e_vsi *vsi)
3135{
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +00003136 struct i40e_ring *tx_ring, *rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003137 u16 qoffset, qcount;
3138 int i, n;
3139
Parikh, Neeravcd238a32015-02-21 06:43:37 +00003140 if (!(vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
3141 /* Reset the TC information */
3142 for (i = 0; i < vsi->num_queue_pairs; i++) {
3143 rx_ring = vsi->rx_rings[i];
3144 tx_ring = vsi->tx_rings[i];
3145 rx_ring->dcb_tc = 0;
3146 tx_ring->dcb_tc = 0;
3147 }
3148 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003149
3150 for (n = 0; n < I40E_MAX_TRAFFIC_CLASS; n++) {
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04003151 if (!(vsi->tc_config.enabled_tc & BIT_ULL(n)))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003152 continue;
3153
3154 qoffset = vsi->tc_config.tc_info[n].qoffset;
3155 qcount = vsi->tc_config.tc_info[n].qcount;
3156 for (i = qoffset; i < (qoffset + qcount); i++) {
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +00003157 rx_ring = vsi->rx_rings[i];
3158 tx_ring = vsi->tx_rings[i];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003159 rx_ring->dcb_tc = n;
3160 tx_ring->dcb_tc = n;
3161 }
3162 }
3163}
3164
3165/**
3166 * i40e_set_vsi_rx_mode - Call set_rx_mode on a VSI
3167 * @vsi: ptr to the VSI
3168 **/
3169static void i40e_set_vsi_rx_mode(struct i40e_vsi *vsi)
3170{
3171 if (vsi->netdev)
3172 i40e_set_rx_mode(vsi->netdev);
3173}
3174
3175/**
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00003176 * i40e_fdir_filter_restore - Restore the Sideband Flow Director filters
3177 * @vsi: Pointer to the targeted VSI
3178 *
3179 * This function replays the hlist on the hw where all the SB Flow Director
3180 * filters were saved.
3181 **/
3182static void i40e_fdir_filter_restore(struct i40e_vsi *vsi)
3183{
3184 struct i40e_fdir_filter *filter;
3185 struct i40e_pf *pf = vsi->back;
3186 struct hlist_node *node;
3187
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00003188 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
3189 return;
3190
Jacob Keller6d069422017-02-06 14:38:44 -08003191 /* Reset FDir counters as we're replaying all existing filters */
Jacob Keller097dbf52017-02-06 14:38:46 -08003192 pf->fd_tcp4_filter_cnt = 0;
3193 pf->fd_udp4_filter_cnt = 0;
Jacob Kellerf223c872017-02-06 14:38:51 -08003194 pf->fd_sctp4_filter_cnt = 0;
Jacob Keller097dbf52017-02-06 14:38:46 -08003195 pf->fd_ip4_filter_cnt = 0;
Jacob Keller6d069422017-02-06 14:38:44 -08003196
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00003197 hlist_for_each_entry_safe(filter, node,
3198 &pf->fdir_filter_list, fdir_node) {
3199 i40e_add_del_fdir(vsi, filter, true);
3200 }
3201}
3202
3203/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003204 * i40e_vsi_configure - Set up the VSI for action
3205 * @vsi: the VSI being configured
3206 **/
3207static int i40e_vsi_configure(struct i40e_vsi *vsi)
3208{
3209 int err;
3210
3211 i40e_set_vsi_rx_mode(vsi);
3212 i40e_restore_vlan(vsi);
3213 i40e_vsi_config_dcb_rings(vsi);
3214 err = i40e_vsi_configure_tx(vsi);
3215 if (!err)
3216 err = i40e_vsi_configure_rx(vsi);
3217
3218 return err;
3219}
3220
3221/**
3222 * i40e_vsi_configure_msix - MSIX mode Interrupt Config in the HW
3223 * @vsi: the VSI being configured
3224 **/
3225static void i40e_vsi_configure_msix(struct i40e_vsi *vsi)
3226{
Björn Töpel74608d12017-05-24 07:55:35 +02003227 bool has_xdp = i40e_enabled_xdp_vsi(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003228 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003229 struct i40e_hw *hw = &pf->hw;
3230 u16 vector;
3231 int i, q;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003232 u32 qp;
3233
3234 /* The interrupt indexing is offset by 1 in the PFINT_ITRn
3235 * and PFINT_LNKLSTn registers, e.g.:
3236 * PFINT_ITRn[0..n-1] gets msix-1..msix-n (qpair interrupts)
3237 */
3238 qp = vsi->base_queue;
3239 vector = vsi->base_vector;
Alexander Duyck493fb302013-09-28 07:01:44 +00003240 for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
Jesse Brandeburgac26fc12015-09-28 14:12:37 -04003241 struct i40e_q_vector *q_vector = vsi->q_vectors[i];
3242
Jesse Brandeburgee2319c2015-09-28 14:16:54 -04003243 q_vector->itr_countdown = ITR_COUNTDOWN_START;
Kan Lianga75e8002016-02-19 09:24:04 -05003244 q_vector->rx.itr = ITR_TO_REG(vsi->rx_rings[i]->rx_itr_setting);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003245 q_vector->rx.latency_range = I40E_LOW_LATENCY;
3246 wr32(hw, I40E_PFINT_ITRN(I40E_RX_ITR, vector - 1),
3247 q_vector->rx.itr);
Kan Lianga75e8002016-02-19 09:24:04 -05003248 q_vector->tx.itr = ITR_TO_REG(vsi->tx_rings[i]->tx_itr_setting);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003249 q_vector->tx.latency_range = I40E_LOW_LATENCY;
3250 wr32(hw, I40E_PFINT_ITRN(I40E_TX_ITR, vector - 1),
3251 q_vector->tx.itr);
Jesse Brandeburgac26fc12015-09-28 14:12:37 -04003252 wr32(hw, I40E_PFINT_RATEN(vector - 1),
Alan Brady1c0e6a32016-11-28 16:06:02 -08003253 i40e_intrl_usec_to_reg(vsi->int_rate_limit));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003254
3255 /* Linked list for the queuepairs assigned to this vector */
3256 wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), qp);
3257 for (q = 0; q < q_vector->num_ringpairs; q++) {
Björn Töpel74608d12017-05-24 07:55:35 +02003258 u32 nextqp = has_xdp ? qp + vsi->alloc_queue_pairs : qp;
Jesse Brandeburgac26fc12015-09-28 14:12:37 -04003259 u32 val;
3260
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003261 val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
Björn Töpel74608d12017-05-24 07:55:35 +02003262 (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
3263 (vector << I40E_QINT_RQCTL_MSIX_INDX_SHIFT) |
3264 (nextqp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT) |
3265 (I40E_QUEUE_TYPE_TX <<
3266 I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003267
3268 wr32(hw, I40E_QINT_RQCTL(qp), val);
3269
Björn Töpel74608d12017-05-24 07:55:35 +02003270 if (has_xdp) {
3271 val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3272 (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3273 (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
3274 (qp << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) |
3275 (I40E_QUEUE_TYPE_TX <<
3276 I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3277
3278 wr32(hw, I40E_QINT_TQCTL(nextqp), val);
3279 }
3280
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003281 val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
Björn Töpel74608d12017-05-24 07:55:35 +02003282 (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3283 (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
3284 ((qp + 1) << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) |
3285 (I40E_QUEUE_TYPE_RX <<
3286 I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003287
3288 /* Terminate the linked list */
3289 if (q == (q_vector->num_ringpairs - 1))
Björn Töpel74608d12017-05-24 07:55:35 +02003290 val |= (I40E_QUEUE_END_OF_LIST <<
3291 I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003292
3293 wr32(hw, I40E_QINT_TQCTL(qp), val);
3294 qp++;
3295 }
3296 }
3297
3298 i40e_flush(hw);
3299}
3300
3301/**
3302 * i40e_enable_misc_int_causes - enable the non-queue interrupts
3303 * @hw: ptr to the hardware info
3304 **/
Jacob Kellerab437b52014-12-14 01:55:08 +00003305static void i40e_enable_misc_int_causes(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003306{
Jacob Kellerab437b52014-12-14 01:55:08 +00003307 struct i40e_hw *hw = &pf->hw;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003308 u32 val;
3309
3310 /* clear things first */
3311 wr32(hw, I40E_PFINT_ICR0_ENA, 0); /* disable all */
3312 rd32(hw, I40E_PFINT_ICR0); /* read to clear */
3313
3314 val = I40E_PFINT_ICR0_ENA_ECC_ERR_MASK |
3315 I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK |
3316 I40E_PFINT_ICR0_ENA_GRST_MASK |
3317 I40E_PFINT_ICR0_ENA_PCI_EXCEPTION_MASK |
3318 I40E_PFINT_ICR0_ENA_GPIO_MASK |
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003319 I40E_PFINT_ICR0_ENA_HMC_ERR_MASK |
3320 I40E_PFINT_ICR0_ENA_VFLR_MASK |
3321 I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
3322
Anjali Singhai Jain0d8e1432015-06-05 12:20:32 -04003323 if (pf->flags & I40E_FLAG_IWARP_ENABLED)
3324 val |= I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
3325
Jacob Kellerab437b52014-12-14 01:55:08 +00003326 if (pf->flags & I40E_FLAG_PTP)
3327 val |= I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
3328
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003329 wr32(hw, I40E_PFINT_ICR0_ENA, val);
3330
3331 /* SW_ITR_IDX = 0, but don't change INTENA */
Anjali Singhai Jain84ed40e2013-11-26 10:49:32 +00003332 wr32(hw, I40E_PFINT_DYN_CTL0, I40E_PFINT_DYN_CTL0_SW_ITR_INDX_MASK |
3333 I40E_PFINT_DYN_CTL0_INTENA_MSK_MASK);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003334
3335 /* OTHER_ITR_IDX = 0 */
3336 wr32(hw, I40E_PFINT_STAT_CTL0, 0);
3337}
3338
3339/**
3340 * i40e_configure_msi_and_legacy - Legacy mode interrupt config in the HW
3341 * @vsi: the VSI being configured
3342 **/
3343static void i40e_configure_msi_and_legacy(struct i40e_vsi *vsi)
3344{
Björn Töpel74608d12017-05-24 07:55:35 +02003345 u32 nextqp = i40e_enabled_xdp_vsi(vsi) ? vsi->alloc_queue_pairs : 0;
Alexander Duyck493fb302013-09-28 07:01:44 +00003346 struct i40e_q_vector *q_vector = vsi->q_vectors[0];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003347 struct i40e_pf *pf = vsi->back;
3348 struct i40e_hw *hw = &pf->hw;
3349 u32 val;
3350
3351 /* set the ITR configuration */
Jesse Brandeburgee2319c2015-09-28 14:16:54 -04003352 q_vector->itr_countdown = ITR_COUNTDOWN_START;
Kan Lianga75e8002016-02-19 09:24:04 -05003353 q_vector->rx.itr = ITR_TO_REG(vsi->rx_rings[0]->rx_itr_setting);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003354 q_vector->rx.latency_range = I40E_LOW_LATENCY;
3355 wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), q_vector->rx.itr);
Kan Lianga75e8002016-02-19 09:24:04 -05003356 q_vector->tx.itr = ITR_TO_REG(vsi->tx_rings[0]->tx_itr_setting);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003357 q_vector->tx.latency_range = I40E_LOW_LATENCY;
3358 wr32(hw, I40E_PFINT_ITR0(I40E_TX_ITR), q_vector->tx.itr);
3359
Jacob Kellerab437b52014-12-14 01:55:08 +00003360 i40e_enable_misc_int_causes(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003361
3362 /* FIRSTQ_INDX = 0, FIRSTQ_TYPE = 0 (rx) */
3363 wr32(hw, I40E_PFINT_LNKLST0, 0);
3364
Jesse Brandeburgf29eaa32014-02-11 08:24:12 +00003365 /* Associate the queue pair to the vector and enable the queue int */
Björn Töpel74608d12017-05-24 07:55:35 +02003366 val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3367 (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
3368 (nextqp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT)|
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003369 (I40E_QUEUE_TYPE_TX << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3370
3371 wr32(hw, I40E_QINT_RQCTL(0), val);
3372
Björn Töpel74608d12017-05-24 07:55:35 +02003373 if (i40e_enabled_xdp_vsi(vsi)) {
3374 val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3375 (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT)|
3376 (I40E_QUEUE_TYPE_TX
3377 << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3378
3379 wr32(hw, I40E_QINT_TQCTL(nextqp), val);
3380 }
3381
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003382 val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3383 (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3384 (I40E_QUEUE_END_OF_LIST << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
3385
3386 wr32(hw, I40E_QINT_TQCTL(0), val);
3387 i40e_flush(hw);
3388}
3389
3390/**
Mitch Williams2ef28cf2013-11-28 06:39:32 +00003391 * i40e_irq_dynamic_disable_icr0 - Disable default interrupt generation for icr0
3392 * @pf: board private structure
3393 **/
3394void i40e_irq_dynamic_disable_icr0(struct i40e_pf *pf)
3395{
3396 struct i40e_hw *hw = &pf->hw;
3397
3398 wr32(hw, I40E_PFINT_DYN_CTL0,
3399 I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
3400 i40e_flush(hw);
3401}
3402
3403/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003404 * i40e_irq_dynamic_enable_icr0 - Enable default interrupt generation for icr0
3405 * @pf: board private structure
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08003406 * @clearpba: true when all pending interrupt events should be cleared
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003407 **/
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08003408void i40e_irq_dynamic_enable_icr0(struct i40e_pf *pf, bool clearpba)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003409{
3410 struct i40e_hw *hw = &pf->hw;
3411 u32 val;
3412
3413 val = I40E_PFINT_DYN_CTL0_INTENA_MASK |
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08003414 (clearpba ? I40E_PFINT_DYN_CTL0_CLEARPBA_MASK : 0) |
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003415 (I40E_ITR_NONE << I40E_PFINT_DYN_CTL0_ITR_INDX_SHIFT);
3416
3417 wr32(hw, I40E_PFINT_DYN_CTL0, val);
3418 i40e_flush(hw);
3419}
3420
3421/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003422 * i40e_msix_clean_rings - MSIX mode Interrupt Handler
3423 * @irq: interrupt number
3424 * @data: pointer to a q_vector
3425 **/
3426static irqreturn_t i40e_msix_clean_rings(int irq, void *data)
3427{
3428 struct i40e_q_vector *q_vector = data;
3429
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003430 if (!q_vector->tx.ring && !q_vector->rx.ring)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003431 return IRQ_HANDLED;
3432
Alexander Duyck5d3465a2015-09-29 15:19:50 -07003433 napi_schedule_irqoff(&q_vector->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003434
3435 return IRQ_HANDLED;
3436}
3437
3438/**
Alan Brady96db7762016-09-14 16:24:38 -07003439 * i40e_irq_affinity_notify - Callback for affinity changes
3440 * @notify: context as to what irq was changed
3441 * @mask: the new affinity mask
3442 *
3443 * This is a callback function used by the irq_set_affinity_notifier function
3444 * so that we may register to receive changes to the irq affinity masks.
3445 **/
3446static void i40e_irq_affinity_notify(struct irq_affinity_notify *notify,
3447 const cpumask_t *mask)
3448{
3449 struct i40e_q_vector *q_vector =
3450 container_of(notify, struct i40e_q_vector, affinity_notify);
3451
Jacob Keller7e4d01e2017-07-12 05:46:05 -04003452 cpumask_copy(&q_vector->affinity_mask, mask);
Alan Brady96db7762016-09-14 16:24:38 -07003453}
3454
3455/**
3456 * i40e_irq_affinity_release - Callback for affinity notifier release
3457 * @ref: internal core kernel usage
3458 *
3459 * This is a callback function used by the irq_set_affinity_notifier function
3460 * to inform the current notification subscriber that they will no longer
3461 * receive notifications.
3462 **/
3463static void i40e_irq_affinity_release(struct kref *ref) {}
3464
3465/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003466 * i40e_vsi_request_irq_msix - Initialize MSI-X interrupts
3467 * @vsi: the VSI being configured
3468 * @basename: name for the vector
3469 *
3470 * Allocates MSI-X vectors and requests interrupts from the kernel.
3471 **/
3472static int i40e_vsi_request_irq_msix(struct i40e_vsi *vsi, char *basename)
3473{
3474 int q_vectors = vsi->num_q_vectors;
3475 struct i40e_pf *pf = vsi->back;
3476 int base = vsi->base_vector;
3477 int rx_int_idx = 0;
3478 int tx_int_idx = 0;
3479 int vector, err;
Alan Brady96db7762016-09-14 16:24:38 -07003480 int irq_num;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003481
3482 for (vector = 0; vector < q_vectors; vector++) {
Alexander Duyck493fb302013-09-28 07:01:44 +00003483 struct i40e_q_vector *q_vector = vsi->q_vectors[vector];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003484
Alan Brady96db7762016-09-14 16:24:38 -07003485 irq_num = pf->msix_entries[base + vector].vector;
3486
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003487 if (q_vector->tx.ring && q_vector->rx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003488 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3489 "%s-%s-%d", basename, "TxRx", rx_int_idx++);
3490 tx_int_idx++;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003491 } else if (q_vector->rx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003492 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3493 "%s-%s-%d", basename, "rx", rx_int_idx++);
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003494 } else if (q_vector->tx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003495 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3496 "%s-%s-%d", basename, "tx", tx_int_idx++);
3497 } else {
3498 /* skip this unused q_vector */
3499 continue;
3500 }
Alan Brady96db7762016-09-14 16:24:38 -07003501 err = request_irq(irq_num,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003502 vsi->irq_handler,
3503 0,
3504 q_vector->name,
3505 q_vector);
3506 if (err) {
3507 dev_info(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04003508 "MSIX request_irq failed, error: %d\n", err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003509 goto free_queue_irqs;
3510 }
Alan Brady96db7762016-09-14 16:24:38 -07003511
3512 /* register for affinity change notifications */
3513 q_vector->affinity_notify.notify = i40e_irq_affinity_notify;
3514 q_vector->affinity_notify.release = i40e_irq_affinity_release;
3515 irq_set_affinity_notifier(irq_num, &q_vector->affinity_notify);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003516 /* assign the mask for this irq */
Alan Brady96db7762016-09-14 16:24:38 -07003517 irq_set_affinity_hint(irq_num, &q_vector->affinity_mask);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003518 }
3519
Shannon Nelson63741842014-04-23 04:50:16 +00003520 vsi->irqs_ready = true;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003521 return 0;
3522
3523free_queue_irqs:
3524 while (vector) {
3525 vector--;
Alan Brady96db7762016-09-14 16:24:38 -07003526 irq_num = pf->msix_entries[base + vector].vector;
3527 irq_set_affinity_notifier(irq_num, NULL);
3528 irq_set_affinity_hint(irq_num, NULL);
3529 free_irq(irq_num, &vsi->q_vectors[vector]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003530 }
3531 return err;
3532}
3533
3534/**
3535 * i40e_vsi_disable_irq - Mask off queue interrupt generation on the VSI
3536 * @vsi: the VSI being un-configured
3537 **/
3538static void i40e_vsi_disable_irq(struct i40e_vsi *vsi)
3539{
3540 struct i40e_pf *pf = vsi->back;
3541 struct i40e_hw *hw = &pf->hw;
3542 int base = vsi->base_vector;
3543 int i;
3544
Shannon Nelson2e5c26ea2017-06-07 05:43:11 -04003545 /* disable interrupt causation from each queue */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003546 for (i = 0; i < vsi->num_queue_pairs; i++) {
Shannon Nelson2e5c26ea2017-06-07 05:43:11 -04003547 u32 val;
3548
3549 val = rd32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx));
3550 val &= ~I40E_QINT_TQCTL_CAUSE_ENA_MASK;
3551 wr32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx), val);
3552
3553 val = rd32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx));
3554 val &= ~I40E_QINT_RQCTL_CAUSE_ENA_MASK;
3555 wr32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx), val);
3556
Björn Töpel74608d12017-05-24 07:55:35 +02003557 if (!i40e_enabled_xdp_vsi(vsi))
3558 continue;
3559 wr32(hw, I40E_QINT_TQCTL(vsi->xdp_rings[i]->reg_idx), 0);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003560 }
3561
Shannon Nelson2e5c26ea2017-06-07 05:43:11 -04003562 /* disable each interrupt */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003563 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3564 for (i = vsi->base_vector;
3565 i < (vsi->num_q_vectors + vsi->base_vector); i++)
3566 wr32(hw, I40E_PFINT_DYN_CTLN(i - 1), 0);
3567
3568 i40e_flush(hw);
3569 for (i = 0; i < vsi->num_q_vectors; i++)
3570 synchronize_irq(pf->msix_entries[i + base].vector);
3571 } else {
3572 /* Legacy and MSI mode - this stops all interrupt handling */
3573 wr32(hw, I40E_PFINT_ICR0_ENA, 0);
3574 wr32(hw, I40E_PFINT_DYN_CTL0, 0);
3575 i40e_flush(hw);
3576 synchronize_irq(pf->pdev->irq);
3577 }
3578}
3579
3580/**
3581 * i40e_vsi_enable_irq - Enable IRQ for the given VSI
3582 * @vsi: the VSI being configured
3583 **/
3584static int i40e_vsi_enable_irq(struct i40e_vsi *vsi)
3585{
3586 struct i40e_pf *pf = vsi->back;
3587 int i;
3588
3589 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
Jesse Brandeburg78455482015-07-23 16:54:41 -04003590 for (i = 0; i < vsi->num_q_vectors; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003591 i40e_irq_dynamic_enable(vsi, i);
3592 } else {
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08003593 i40e_irq_dynamic_enable_icr0(pf, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003594 }
3595
Jesse Brandeburg1022cb62013-09-28 07:13:08 +00003596 i40e_flush(&pf->hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003597 return 0;
3598}
3599
3600/**
3601 * i40e_stop_misc_vector - Stop the vector that handles non-queue events
3602 * @pf: board private structure
3603 **/
3604static void i40e_stop_misc_vector(struct i40e_pf *pf)
3605{
3606 /* Disable ICR 0 */
3607 wr32(&pf->hw, I40E_PFINT_ICR0_ENA, 0);
3608 i40e_flush(&pf->hw);
3609}
3610
3611/**
3612 * i40e_intr - MSI/Legacy and non-queue interrupt handler
3613 * @irq: interrupt number
3614 * @data: pointer to a q_vector
3615 *
3616 * This is the handler used for all MSI/Legacy interrupts, and deals
3617 * with both queue and non-queue interrupts. This is also used in
3618 * MSIX mode to handle the non-queue interrupts.
3619 **/
3620static irqreturn_t i40e_intr(int irq, void *data)
3621{
3622 struct i40e_pf *pf = (struct i40e_pf *)data;
3623 struct i40e_hw *hw = &pf->hw;
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003624 irqreturn_t ret = IRQ_NONE;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003625 u32 icr0, icr0_remaining;
3626 u32 val, ena_mask;
3627
3628 icr0 = rd32(hw, I40E_PFINT_ICR0);
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003629 ena_mask = rd32(hw, I40E_PFINT_ICR0_ENA);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003630
Shannon Nelson116a57d2013-09-28 07:13:59 +00003631 /* if sharing a legacy IRQ, we might get called w/o an intr pending */
3632 if ((icr0 & I40E_PFINT_ICR0_INTEVENT_MASK) == 0)
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003633 goto enable_intr;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003634
Shannon Nelsoncd92e722013-11-16 10:00:44 +00003635 /* if interrupt but no bits showing, must be SWINT */
3636 if (((icr0 & ~I40E_PFINT_ICR0_INTEVENT_MASK) == 0) ||
3637 (icr0 & I40E_PFINT_ICR0_SWINT_MASK))
3638 pf->sw_int_count++;
3639
Anjali Singhai Jain0d8e1432015-06-05 12:20:32 -04003640 if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
Catherine Sullivan76429842017-06-07 05:43:12 -04003641 (icr0 & I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK)) {
Anjali Singhai Jain0d8e1432015-06-05 12:20:32 -04003642 ena_mask &= ~I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
Carolyn Wyborny23bb6dc2016-11-08 13:05:12 -08003643 dev_dbg(&pf->pdev->dev, "cleared PE_CRITERR\n");
Catherine Sullivan76429842017-06-07 05:43:12 -04003644 set_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
Anjali Singhai Jain0d8e1432015-06-05 12:20:32 -04003645 }
3646
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003647 /* only q0 is used in MSI/Legacy mode, and none are used in MSIX */
3648 if (icr0 & I40E_PFINT_ICR0_QUEUE_0_MASK) {
Alexander Duyck5d3465a2015-09-29 15:19:50 -07003649 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
3650 struct i40e_q_vector *q_vector = vsi->q_vectors[0];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003651
Anjali Singhai Jaina16ae2d2016-01-15 14:33:16 -08003652 /* We do not have a way to disarm Queue causes while leaving
3653 * interrupt enabled for all other causes, ideally
3654 * interrupt should be disabled while we are in NAPI but
3655 * this is not a performance path and napi_schedule()
3656 * can deal with rescheduling.
3657 */
Mauro S. M. Rodrigues9e6c9c02017-05-12 23:26:56 -03003658 if (!test_bit(__I40E_DOWN, pf->state))
Alexander Duyck5d3465a2015-09-29 15:19:50 -07003659 napi_schedule_irqoff(&q_vector->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003660 }
3661
3662 if (icr0 & I40E_PFINT_ICR0_ADMINQ_MASK) {
3663 ena_mask &= ~I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
Jacob Keller0da36b92017-04-19 09:25:55 -04003664 set_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state);
Shannon Nelson6e93d0c2016-01-15 14:33:18 -08003665 i40e_debug(&pf->hw, I40E_DEBUG_NVM, "AdminQ event\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003666 }
3667
3668 if (icr0 & I40E_PFINT_ICR0_MAL_DETECT_MASK) {
3669 ena_mask &= ~I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
Jacob Keller0da36b92017-04-19 09:25:55 -04003670 set_bit(__I40E_MDD_EVENT_PENDING, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003671 }
3672
3673 if (icr0 & I40E_PFINT_ICR0_VFLR_MASK) {
3674 ena_mask &= ~I40E_PFINT_ICR0_ENA_VFLR_MASK;
Jacob Keller0da36b92017-04-19 09:25:55 -04003675 set_bit(__I40E_VFLR_EVENT_PENDING, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003676 }
3677
3678 if (icr0 & I40E_PFINT_ICR0_GRST_MASK) {
Jacob Keller0da36b92017-04-19 09:25:55 -04003679 if (!test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
3680 set_bit(__I40E_RESET_INTR_RECEIVED, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003681 ena_mask &= ~I40E_PFINT_ICR0_ENA_GRST_MASK;
3682 val = rd32(hw, I40E_GLGEN_RSTAT);
3683 val = (val & I40E_GLGEN_RSTAT_RESET_TYPE_MASK)
3684 >> I40E_GLGEN_RSTAT_RESET_TYPE_SHIFT;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003685 if (val == I40E_RESET_CORER) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003686 pf->corer_count++;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003687 } else if (val == I40E_RESET_GLOBR) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003688 pf->globr_count++;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003689 } else if (val == I40E_RESET_EMPR) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003690 pf->empr_count++;
Jacob Keller0da36b92017-04-19 09:25:55 -04003691 set_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state);
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003692 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003693 }
3694
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003695 if (icr0 & I40E_PFINT_ICR0_HMC_ERR_MASK) {
3696 icr0 &= ~I40E_PFINT_ICR0_HMC_ERR_MASK;
3697 dev_info(&pf->pdev->dev, "HMC error interrupt\n");
Anjali Singhai Jain25fc0e62015-03-31 00:45:01 -07003698 dev_info(&pf->pdev->dev, "HMC error info 0x%x, HMC error data 0x%x\n",
3699 rd32(hw, I40E_PFHMC_ERRORINFO),
3700 rd32(hw, I40E_PFHMC_ERRORDATA));
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003701 }
3702
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003703 if (icr0 & I40E_PFINT_ICR0_TIMESYNC_MASK) {
3704 u32 prttsyn_stat = rd32(hw, I40E_PRTTSYN_STAT_0);
3705
3706 if (prttsyn_stat & I40E_PRTTSYN_STAT_0_TXTIME_MASK) {
Jacob Kellercafa1fc2014-04-24 18:05:03 -07003707 icr0 &= ~I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003708 i40e_ptp_tx_hwtstamp(pf);
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003709 }
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003710 }
3711
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003712 /* If a critical error is pending we have no choice but to reset the
3713 * device.
3714 * Report and mask out any remaining unexpected interrupts.
3715 */
3716 icr0_remaining = icr0 & ena_mask;
3717 if (icr0_remaining) {
3718 dev_info(&pf->pdev->dev, "unhandled interrupt icr0=0x%08x\n",
3719 icr0_remaining);
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003720 if ((icr0_remaining & I40E_PFINT_ICR0_PE_CRITERR_MASK) ||
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003721 (icr0_remaining & I40E_PFINT_ICR0_PCI_EXCEPTION_MASK) ||
Anjali Singhai Jainc0c28972014-02-12 01:45:34 +00003722 (icr0_remaining & I40E_PFINT_ICR0_ECC_ERR_MASK)) {
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003723 dev_info(&pf->pdev->dev, "device will be reset\n");
Jacob Keller0da36b92017-04-19 09:25:55 -04003724 set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003725 i40e_service_event_schedule(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003726 }
3727 ena_mask &= ~icr0_remaining;
3728 }
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003729 ret = IRQ_HANDLED;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003730
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003731enable_intr:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003732 /* re-enable interrupt causes */
3733 wr32(hw, I40E_PFINT_ICR0_ENA, ena_mask);
Mauro S. M. Rodrigues9e6c9c02017-05-12 23:26:56 -03003734 if (!test_bit(__I40E_DOWN, pf->state)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003735 i40e_service_event_schedule(pf);
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08003736 i40e_irq_dynamic_enable_icr0(pf, false);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003737 }
3738
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003739 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003740}
3741
3742/**
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003743 * i40e_clean_fdir_tx_irq - Reclaim resources after transmit completes
3744 * @tx_ring: tx ring to clean
3745 * @budget: how many cleans we're allowed
3746 *
3747 * Returns true if there's any budget left (e.g. the clean is finished)
3748 **/
3749static bool i40e_clean_fdir_tx_irq(struct i40e_ring *tx_ring, int budget)
3750{
3751 struct i40e_vsi *vsi = tx_ring->vsi;
3752 u16 i = tx_ring->next_to_clean;
3753 struct i40e_tx_buffer *tx_buf;
3754 struct i40e_tx_desc *tx_desc;
3755
3756 tx_buf = &tx_ring->tx_bi[i];
3757 tx_desc = I40E_TX_DESC(tx_ring, i);
3758 i -= tx_ring->count;
3759
3760 do {
3761 struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
3762
3763 /* if next_to_watch is not set then there is no work pending */
3764 if (!eop_desc)
3765 break;
3766
3767 /* prevent any other reads prior to eop_desc */
3768 read_barrier_depends();
3769
3770 /* if the descriptor isn't done, no work yet to do */
3771 if (!(eop_desc->cmd_type_offset_bsz &
3772 cpu_to_le64(I40E_TX_DESC_DTYPE_DESC_DONE)))
3773 break;
3774
3775 /* clear next_to_watch to prevent false hangs */
3776 tx_buf->next_to_watch = NULL;
3777
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003778 tx_desc->buffer_addr = 0;
3779 tx_desc->cmd_type_offset_bsz = 0;
3780 /* move past filter desc */
3781 tx_buf++;
3782 tx_desc++;
3783 i++;
3784 if (unlikely(!i)) {
3785 i -= tx_ring->count;
3786 tx_buf = tx_ring->tx_bi;
3787 tx_desc = I40E_TX_DESC(tx_ring, 0);
3788 }
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003789 /* unmap skb header data */
3790 dma_unmap_single(tx_ring->dev,
3791 dma_unmap_addr(tx_buf, dma),
3792 dma_unmap_len(tx_buf, len),
3793 DMA_TO_DEVICE);
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003794 if (tx_buf->tx_flags & I40E_TX_FLAGS_FD_SB)
3795 kfree(tx_buf->raw_buf);
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003796
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003797 tx_buf->raw_buf = NULL;
3798 tx_buf->tx_flags = 0;
3799 tx_buf->next_to_watch = NULL;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003800 dma_unmap_len_set(tx_buf, len, 0);
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003801 tx_desc->buffer_addr = 0;
3802 tx_desc->cmd_type_offset_bsz = 0;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003803
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003804 /* move us past the eop_desc for start of next FD desc */
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003805 tx_buf++;
3806 tx_desc++;
3807 i++;
3808 if (unlikely(!i)) {
3809 i -= tx_ring->count;
3810 tx_buf = tx_ring->tx_bi;
3811 tx_desc = I40E_TX_DESC(tx_ring, 0);
3812 }
3813
3814 /* update budget accounting */
3815 budget--;
3816 } while (likely(budget));
3817
3818 i += tx_ring->count;
3819 tx_ring->next_to_clean = i;
3820
Jesse Brandeburg6995b362015-08-28 17:55:54 -04003821 if (vsi->back->flags & I40E_FLAG_MSIX_ENABLED)
Jesse Brandeburg78455482015-07-23 16:54:41 -04003822 i40e_irq_dynamic_enable(vsi, tx_ring->q_vector->v_idx);
Jesse Brandeburg6995b362015-08-28 17:55:54 -04003823
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003824 return budget > 0;
3825}
3826
3827/**
3828 * i40e_fdir_clean_ring - Interrupt Handler for FDIR SB ring
3829 * @irq: interrupt number
3830 * @data: pointer to a q_vector
3831 **/
3832static irqreturn_t i40e_fdir_clean_ring(int irq, void *data)
3833{
3834 struct i40e_q_vector *q_vector = data;
3835 struct i40e_vsi *vsi;
3836
3837 if (!q_vector->tx.ring)
3838 return IRQ_HANDLED;
3839
3840 vsi = q_vector->tx.ring->vsi;
3841 i40e_clean_fdir_tx_irq(q_vector->tx.ring, vsi->work_limit);
3842
3843 return IRQ_HANDLED;
3844}
3845
3846/**
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003847 * i40e_map_vector_to_qp - Assigns the queue pair to the vector
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003848 * @vsi: the VSI being configured
3849 * @v_idx: vector index
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003850 * @qp_idx: queue pair index
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003851 **/
Anjali Singhai Jain26cdc442015-07-10 19:36:00 -04003852static void i40e_map_vector_to_qp(struct i40e_vsi *vsi, int v_idx, int qp_idx)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003853{
Alexander Duyck493fb302013-09-28 07:01:44 +00003854 struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
Alexander Duyck9f65e152013-09-28 06:00:58 +00003855 struct i40e_ring *tx_ring = vsi->tx_rings[qp_idx];
3856 struct i40e_ring *rx_ring = vsi->rx_rings[qp_idx];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003857
3858 tx_ring->q_vector = q_vector;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003859 tx_ring->next = q_vector->tx.ring;
3860 q_vector->tx.ring = tx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003861 q_vector->tx.count++;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003862
Björn Töpel74608d12017-05-24 07:55:35 +02003863 /* Place XDP Tx ring in the same q_vector ring list as regular Tx */
3864 if (i40e_enabled_xdp_vsi(vsi)) {
3865 struct i40e_ring *xdp_ring = vsi->xdp_rings[qp_idx];
3866
3867 xdp_ring->q_vector = q_vector;
3868 xdp_ring->next = q_vector->tx.ring;
3869 q_vector->tx.ring = xdp_ring;
3870 q_vector->tx.count++;
3871 }
3872
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003873 rx_ring->q_vector = q_vector;
3874 rx_ring->next = q_vector->rx.ring;
3875 q_vector->rx.ring = rx_ring;
3876 q_vector->rx.count++;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003877}
3878
3879/**
3880 * i40e_vsi_map_rings_to_vectors - Maps descriptor rings to vectors
3881 * @vsi: the VSI being configured
3882 *
3883 * This function maps descriptor rings to the queue-specific vectors
3884 * we were allotted through the MSI-X enabling code. Ideally, we'd have
3885 * one vector per queue pair, but on a constrained vector budget, we
3886 * group the queue pairs as "efficiently" as possible.
3887 **/
3888static void i40e_vsi_map_rings_to_vectors(struct i40e_vsi *vsi)
3889{
3890 int qp_remaining = vsi->num_queue_pairs;
3891 int q_vectors = vsi->num_q_vectors;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003892 int num_ringpairs;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003893 int v_start = 0;
3894 int qp_idx = 0;
3895
3896 /* If we don't have enough vectors for a 1-to-1 mapping, we'll have to
3897 * group them so there are multiple queues per vector.
Anjali Singhai Jain70114ec2014-06-03 23:50:14 +00003898 * It is also important to go through all the vectors available to be
3899 * sure that if we don't use all the vectors, that the remaining vectors
3900 * are cleared. This is especially important when decreasing the
3901 * number of queues in use.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003902 */
Anjali Singhai Jain70114ec2014-06-03 23:50:14 +00003903 for (; v_start < q_vectors; v_start++) {
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003904 struct i40e_q_vector *q_vector = vsi->q_vectors[v_start];
3905
3906 num_ringpairs = DIV_ROUND_UP(qp_remaining, q_vectors - v_start);
3907
3908 q_vector->num_ringpairs = num_ringpairs;
3909
3910 q_vector->rx.count = 0;
3911 q_vector->tx.count = 0;
3912 q_vector->rx.ring = NULL;
3913 q_vector->tx.ring = NULL;
3914
3915 while (num_ringpairs--) {
Anjali Singhai Jain26cdc442015-07-10 19:36:00 -04003916 i40e_map_vector_to_qp(vsi, v_start, qp_idx);
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003917 qp_idx++;
3918 qp_remaining--;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003919 }
3920 }
3921}
3922
3923/**
3924 * i40e_vsi_request_irq - Request IRQ from the OS
3925 * @vsi: the VSI being configured
3926 * @basename: name for the vector
3927 **/
3928static int i40e_vsi_request_irq(struct i40e_vsi *vsi, char *basename)
3929{
3930 struct i40e_pf *pf = vsi->back;
3931 int err;
3932
3933 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
3934 err = i40e_vsi_request_irq_msix(vsi, basename);
3935 else if (pf->flags & I40E_FLAG_MSI_ENABLED)
3936 err = request_irq(pf->pdev->irq, i40e_intr, 0,
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00003937 pf->int_name, pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003938 else
3939 err = request_irq(pf->pdev->irq, i40e_intr, IRQF_SHARED,
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00003940 pf->int_name, pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003941
3942 if (err)
3943 dev_info(&pf->pdev->dev, "request_irq failed, Error %d\n", err);
3944
3945 return err;
3946}
3947
3948#ifdef CONFIG_NET_POLL_CONTROLLER
3949/**
Jesse Brandeburgd89d9672016-01-04 10:33:02 -08003950 * i40e_netpoll - A Polling 'interrupt' handler
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003951 * @netdev: network interface device structure
3952 *
3953 * This is used by netconsole to send skbs without having to re-enable
3954 * interrupts. It's not called while the normal interrupt routine is executing.
3955 **/
3956static void i40e_netpoll(struct net_device *netdev)
3957{
3958 struct i40e_netdev_priv *np = netdev_priv(netdev);
3959 struct i40e_vsi *vsi = np->vsi;
3960 struct i40e_pf *pf = vsi->back;
3961 int i;
3962
3963 /* if interface is down do nothing */
Jacob Keller0da36b92017-04-19 09:25:55 -04003964 if (test_bit(__I40E_VSI_DOWN, vsi->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003965 return;
3966
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003967 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3968 for (i = 0; i < vsi->num_q_vectors; i++)
Alexander Duyck493fb302013-09-28 07:01:44 +00003969 i40e_msix_clean_rings(0, vsi->q_vectors[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003970 } else {
3971 i40e_intr(pf->pdev->irq, netdev);
3972 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003973}
3974#endif
3975
Jacob Kellerc768e492017-04-13 04:45:47 -04003976#define I40E_QTX_ENA_WAIT_COUNT 50
3977
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003978/**
Neerav Parikh23527302014-06-03 23:50:15 +00003979 * i40e_pf_txq_wait - Wait for a PF's Tx queue to be enabled or disabled
3980 * @pf: the PF being configured
3981 * @pf_q: the PF queue
3982 * @enable: enable or disable state of the queue
3983 *
3984 * This routine will wait for the given Tx queue of the PF to reach the
3985 * enabled or disabled state.
3986 * Returns -ETIMEDOUT in case of failing to reach the requested state after
3987 * multiple retries; else will return 0 in case of success.
3988 **/
3989static int i40e_pf_txq_wait(struct i40e_pf *pf, int pf_q, bool enable)
3990{
3991 int i;
3992 u32 tx_reg;
3993
3994 for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
3995 tx_reg = rd32(&pf->hw, I40E_QTX_ENA(pf_q));
3996 if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
3997 break;
3998
Neerav Parikhf98a2002014-09-13 07:40:44 +00003999 usleep_range(10, 20);
Neerav Parikh23527302014-06-03 23:50:15 +00004000 }
4001 if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
4002 return -ETIMEDOUT;
4003
4004 return 0;
4005}
4006
4007/**
Jacob Kellerc768e492017-04-13 04:45:47 -04004008 * i40e_control_tx_q - Start or stop a particular Tx queue
4009 * @pf: the PF structure
4010 * @pf_q: the PF queue to configure
4011 * @enable: start or stop the queue
4012 *
4013 * This function enables or disables a single queue. Note that any delay
4014 * required after the operation is expected to be handled by the caller of
4015 * this function.
4016 **/
4017static void i40e_control_tx_q(struct i40e_pf *pf, int pf_q, bool enable)
4018{
4019 struct i40e_hw *hw = &pf->hw;
4020 u32 tx_reg;
4021 int i;
4022
4023 /* warn the TX unit of coming changes */
4024 i40e_pre_tx_queue_cfg(&pf->hw, pf_q, enable);
4025 if (!enable)
4026 usleep_range(10, 20);
4027
4028 for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
4029 tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
4030 if (((tx_reg >> I40E_QTX_ENA_QENA_REQ_SHIFT) & 1) ==
4031 ((tx_reg >> I40E_QTX_ENA_QENA_STAT_SHIFT) & 1))
4032 break;
4033 usleep_range(1000, 2000);
4034 }
4035
4036 /* Skip if the queue is already in the requested state */
4037 if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
4038 return;
4039
4040 /* turn on/off the queue */
4041 if (enable) {
4042 wr32(hw, I40E_QTX_HEAD(pf_q), 0);
4043 tx_reg |= I40E_QTX_ENA_QENA_REQ_MASK;
4044 } else {
4045 tx_reg &= ~I40E_QTX_ENA_QENA_REQ_MASK;
4046 }
4047
4048 wr32(hw, I40E_QTX_ENA(pf_q), tx_reg);
4049}
4050
4051/**
Björn Töpel74608d12017-05-24 07:55:35 +02004052 * i40e_control_wait_tx_q - Start/stop Tx queue and wait for completion
4053 * @seid: VSI SEID
4054 * @pf: the PF structure
4055 * @pf_q: the PF queue to configure
4056 * @is_xdp: true if the queue is used for XDP
4057 * @enable: start or stop the queue
4058 **/
4059static int i40e_control_wait_tx_q(int seid, struct i40e_pf *pf, int pf_q,
4060 bool is_xdp, bool enable)
4061{
4062 int ret;
4063
4064 i40e_control_tx_q(pf, pf_q, enable);
4065
4066 /* wait for the change to finish */
4067 ret = i40e_pf_txq_wait(pf, pf_q, enable);
4068 if (ret) {
4069 dev_info(&pf->pdev->dev,
4070 "VSI seid %d %sTx ring %d %sable timeout\n",
4071 seid, (is_xdp ? "XDP " : ""), pf_q,
4072 (enable ? "en" : "dis"));
4073 }
4074
4075 return ret;
4076}
4077
4078/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004079 * i40e_vsi_control_tx - Start or stop a VSI's rings
4080 * @vsi: the VSI being configured
4081 * @enable: start or stop the rings
4082 **/
4083static int i40e_vsi_control_tx(struct i40e_vsi *vsi, bool enable)
4084{
4085 struct i40e_pf *pf = vsi->back;
Jacob Kellerc768e492017-04-13 04:45:47 -04004086 int i, pf_q, ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004087
4088 pf_q = vsi->base_queue;
4089 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
Björn Töpel74608d12017-05-24 07:55:35 +02004090 ret = i40e_control_wait_tx_q(vsi->seid, pf,
4091 pf_q,
4092 false /*is xdp*/, enable);
4093 if (ret)
Neerav Parikh23527302014-06-03 23:50:15 +00004094 break;
Björn Töpel74608d12017-05-24 07:55:35 +02004095
4096 if (!i40e_enabled_xdp_vsi(vsi))
4097 continue;
4098
4099 ret = i40e_control_wait_tx_q(vsi->seid, pf,
4100 pf_q + vsi->alloc_queue_pairs,
4101 true /*is xdp*/, enable);
4102 if (ret)
4103 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004104 }
4105
Neerav Parikh23527302014-06-03 23:50:15 +00004106 return ret;
4107}
4108
4109/**
4110 * i40e_pf_rxq_wait - Wait for a PF's Rx queue to be enabled or disabled
4111 * @pf: the PF being configured
4112 * @pf_q: the PF queue
4113 * @enable: enable or disable state of the queue
4114 *
4115 * This routine will wait for the given Rx queue of the PF to reach the
4116 * enabled or disabled state.
4117 * Returns -ETIMEDOUT in case of failing to reach the requested state after
4118 * multiple retries; else will return 0 in case of success.
4119 **/
4120static int i40e_pf_rxq_wait(struct i40e_pf *pf, int pf_q, bool enable)
4121{
4122 int i;
4123 u32 rx_reg;
4124
4125 for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
4126 rx_reg = rd32(&pf->hw, I40E_QRX_ENA(pf_q));
4127 if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4128 break;
4129
Neerav Parikhf98a2002014-09-13 07:40:44 +00004130 usleep_range(10, 20);
Neerav Parikh23527302014-06-03 23:50:15 +00004131 }
4132 if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
4133 return -ETIMEDOUT;
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00004134
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004135 return 0;
4136}
4137
4138/**
Jacob Kellerc768e492017-04-13 04:45:47 -04004139 * i40e_control_rx_q - Start or stop a particular Rx queue
4140 * @pf: the PF structure
4141 * @pf_q: the PF queue to configure
4142 * @enable: start or stop the queue
4143 *
4144 * This function enables or disables a single queue. Note that any delay
4145 * required after the operation is expected to be handled by the caller of
4146 * this function.
4147 **/
4148static void i40e_control_rx_q(struct i40e_pf *pf, int pf_q, bool enable)
4149{
4150 struct i40e_hw *hw = &pf->hw;
4151 u32 rx_reg;
4152 int i;
4153
4154 for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
4155 rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
4156 if (((rx_reg >> I40E_QRX_ENA_QENA_REQ_SHIFT) & 1) ==
4157 ((rx_reg >> I40E_QRX_ENA_QENA_STAT_SHIFT) & 1))
4158 break;
4159 usleep_range(1000, 2000);
4160 }
4161
4162 /* Skip if the queue is already in the requested state */
4163 if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4164 return;
4165
4166 /* turn on/off the queue */
4167 if (enable)
4168 rx_reg |= I40E_QRX_ENA_QENA_REQ_MASK;
4169 else
4170 rx_reg &= ~I40E_QRX_ENA_QENA_REQ_MASK;
4171
4172 wr32(hw, I40E_QRX_ENA(pf_q), rx_reg);
4173}
4174
4175/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004176 * i40e_vsi_control_rx - Start or stop a VSI's rings
4177 * @vsi: the VSI being configured
4178 * @enable: start or stop the rings
4179 **/
4180static int i40e_vsi_control_rx(struct i40e_vsi *vsi, bool enable)
4181{
4182 struct i40e_pf *pf = vsi->back;
Jacob Kellerc768e492017-04-13 04:45:47 -04004183 int i, pf_q, ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004184
4185 pf_q = vsi->base_queue;
4186 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
Jacob Kellerc768e492017-04-13 04:45:47 -04004187 i40e_control_rx_q(pf, pf_q, enable);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004188
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004189 /* wait for the change to finish */
Neerav Parikh23527302014-06-03 23:50:15 +00004190 ret = i40e_pf_rxq_wait(pf, pf_q, enable);
4191 if (ret) {
4192 dev_info(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04004193 "VSI seid %d Rx ring %d %sable timeout\n",
4194 vsi->seid, pf_q, (enable ? "en" : "dis"));
Neerav Parikh23527302014-06-03 23:50:15 +00004195 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004196 }
4197 }
4198
Wyborny, Carolynd08a9f62017-03-28 08:00:48 -07004199 /* Due to HW errata, on Rx disable only, the register can indicate done
4200 * before it really is. Needs 50ms to be sure
4201 */
4202 if (!enable)
4203 mdelay(50);
4204
Neerav Parikh23527302014-06-03 23:50:15 +00004205 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004206}
4207
4208/**
Filip Sadowski3aa7b742016-10-11 15:26:58 -07004209 * i40e_vsi_start_rings - Start a VSI's rings
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004210 * @vsi: the VSI being configured
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004211 **/
Filip Sadowski3aa7b742016-10-11 15:26:58 -07004212int i40e_vsi_start_rings(struct i40e_vsi *vsi)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004213{
Anjali Singhai Jain3b867b22013-12-21 05:44:44 +00004214 int ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004215
4216 /* do rx first for enable and last for disable */
Filip Sadowski3aa7b742016-10-11 15:26:58 -07004217 ret = i40e_vsi_control_rx(vsi, true);
4218 if (ret)
4219 return ret;
4220 ret = i40e_vsi_control_tx(vsi, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004221
4222 return ret;
4223}
4224
4225/**
Filip Sadowski3aa7b742016-10-11 15:26:58 -07004226 * i40e_vsi_stop_rings - Stop a VSI's rings
4227 * @vsi: the VSI being configured
4228 **/
4229void i40e_vsi_stop_rings(struct i40e_vsi *vsi)
4230{
Jacob Keller34807562017-04-13 04:45:53 -04004231 /* When port TX is suspended, don't wait */
Jacob Keller0da36b92017-04-19 09:25:55 -04004232 if (test_bit(__I40E_PORT_SUSPENDED, vsi->back->state))
Jacob Keller34807562017-04-13 04:45:53 -04004233 return i40e_vsi_stop_rings_no_wait(vsi);
4234
Filip Sadowski3aa7b742016-10-11 15:26:58 -07004235 /* do rx first for enable and last for disable
4236 * Ignore return value, we need to shutdown whatever we can
4237 */
4238 i40e_vsi_control_tx(vsi, false);
4239 i40e_vsi_control_rx(vsi, false);
4240}
4241
4242/**
Jacob Kellere4b433f2017-04-13 04:45:52 -04004243 * i40e_vsi_stop_rings_no_wait - Stop a VSI's rings and do not delay
4244 * @vsi: the VSI being shutdown
4245 *
4246 * This function stops all the rings for a VSI but does not delay to verify
4247 * that rings have been disabled. It is expected that the caller is shutting
4248 * down multiple VSIs at once and will delay together for all the VSIs after
4249 * initiating the shutdown. This is particularly useful for shutting down lots
4250 * of VFs together. Otherwise, a large delay can be incurred while configuring
4251 * each VSI in serial.
4252 **/
4253void i40e_vsi_stop_rings_no_wait(struct i40e_vsi *vsi)
4254{
4255 struct i40e_pf *pf = vsi->back;
4256 int i, pf_q;
4257
4258 pf_q = vsi->base_queue;
4259 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4260 i40e_control_tx_q(pf, pf_q, false);
4261 i40e_control_rx_q(pf, pf_q, false);
4262 }
4263}
4264
4265/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004266 * i40e_vsi_free_irq - Free the irq association with the OS
4267 * @vsi: the VSI being configured
4268 **/
4269static void i40e_vsi_free_irq(struct i40e_vsi *vsi)
4270{
4271 struct i40e_pf *pf = vsi->back;
4272 struct i40e_hw *hw = &pf->hw;
4273 int base = vsi->base_vector;
4274 u32 val, qp;
4275 int i;
4276
4277 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4278 if (!vsi->q_vectors)
4279 return;
4280
Shannon Nelson63741842014-04-23 04:50:16 +00004281 if (!vsi->irqs_ready)
4282 return;
4283
4284 vsi->irqs_ready = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004285 for (i = 0; i < vsi->num_q_vectors; i++) {
Alan Brady96db7762016-09-14 16:24:38 -07004286 int irq_num;
4287 u16 vector;
4288
4289 vector = i + base;
4290 irq_num = pf->msix_entries[vector].vector;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004291
4292 /* free only the irqs that were actually requested */
Shannon Nelson78681b12013-11-28 06:39:36 +00004293 if (!vsi->q_vectors[i] ||
4294 !vsi->q_vectors[i]->num_ringpairs)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004295 continue;
4296
Alan Brady96db7762016-09-14 16:24:38 -07004297 /* clear the affinity notifier in the IRQ descriptor */
4298 irq_set_affinity_notifier(irq_num, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004299 /* clear the affinity_mask in the IRQ descriptor */
Alan Brady96db7762016-09-14 16:24:38 -07004300 irq_set_affinity_hint(irq_num, NULL);
4301 synchronize_irq(irq_num);
4302 free_irq(irq_num, vsi->q_vectors[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004303
4304 /* Tear down the interrupt queue link list
4305 *
4306 * We know that they come in pairs and always
4307 * the Rx first, then the Tx. To clear the
4308 * link list, stick the EOL value into the
4309 * next_q field of the registers.
4310 */
4311 val = rd32(hw, I40E_PFINT_LNKLSTN(vector - 1));
4312 qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
4313 >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4314 val |= I40E_QUEUE_END_OF_LIST
4315 << I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4316 wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), val);
4317
4318 while (qp != I40E_QUEUE_END_OF_LIST) {
4319 u32 next;
4320
4321 val = rd32(hw, I40E_QINT_RQCTL(qp));
4322
4323 val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
4324 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
4325 I40E_QINT_RQCTL_CAUSE_ENA_MASK |
4326 I40E_QINT_RQCTL_INTEVENT_MASK);
4327
4328 val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
4329 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
4330
4331 wr32(hw, I40E_QINT_RQCTL(qp), val);
4332
4333 val = rd32(hw, I40E_QINT_TQCTL(qp));
4334
4335 next = (val & I40E_QINT_TQCTL_NEXTQ_INDX_MASK)
4336 >> I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT;
4337
4338 val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
4339 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
4340 I40E_QINT_TQCTL_CAUSE_ENA_MASK |
4341 I40E_QINT_TQCTL_INTEVENT_MASK);
4342
4343 val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
4344 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
4345
4346 wr32(hw, I40E_QINT_TQCTL(qp), val);
4347 qp = next;
4348 }
4349 }
4350 } else {
4351 free_irq(pf->pdev->irq, pf);
4352
4353 val = rd32(hw, I40E_PFINT_LNKLST0);
4354 qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
4355 >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4356 val |= I40E_QUEUE_END_OF_LIST
4357 << I40E_PFINT_LNKLST0_FIRSTQ_INDX_SHIFT;
4358 wr32(hw, I40E_PFINT_LNKLST0, val);
4359
4360 val = rd32(hw, I40E_QINT_RQCTL(qp));
4361 val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
4362 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
4363 I40E_QINT_RQCTL_CAUSE_ENA_MASK |
4364 I40E_QINT_RQCTL_INTEVENT_MASK);
4365
4366 val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
4367 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
4368
4369 wr32(hw, I40E_QINT_RQCTL(qp), val);
4370
4371 val = rd32(hw, I40E_QINT_TQCTL(qp));
4372
4373 val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
4374 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
4375 I40E_QINT_TQCTL_CAUSE_ENA_MASK |
4376 I40E_QINT_TQCTL_INTEVENT_MASK);
4377
4378 val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
4379 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
4380
4381 wr32(hw, I40E_QINT_TQCTL(qp), val);
4382 }
4383}
4384
4385/**
Alexander Duyck493fb302013-09-28 07:01:44 +00004386 * i40e_free_q_vector - Free memory allocated for specific interrupt vector
4387 * @vsi: the VSI being configured
4388 * @v_idx: Index of vector to be freed
4389 *
4390 * This function frees the memory allocated to the q_vector. In addition if
4391 * NAPI is enabled it will delete any references to the NAPI struct prior
4392 * to freeing the q_vector.
4393 **/
4394static void i40e_free_q_vector(struct i40e_vsi *vsi, int v_idx)
4395{
4396 struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00004397 struct i40e_ring *ring;
Alexander Duyck493fb302013-09-28 07:01:44 +00004398
4399 if (!q_vector)
4400 return;
4401
4402 /* disassociate q_vector from rings */
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00004403 i40e_for_each_ring(ring, q_vector->tx)
4404 ring->q_vector = NULL;
4405
4406 i40e_for_each_ring(ring, q_vector->rx)
4407 ring->q_vector = NULL;
Alexander Duyck493fb302013-09-28 07:01:44 +00004408
4409 /* only VSI w/ an associated netdev is set up w/ NAPI */
4410 if (vsi->netdev)
4411 netif_napi_del(&q_vector->napi);
4412
4413 vsi->q_vectors[v_idx] = NULL;
4414
4415 kfree_rcu(q_vector, rcu);
4416}
4417
4418/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004419 * i40e_vsi_free_q_vectors - Free memory allocated for interrupt vectors
4420 * @vsi: the VSI being un-configured
4421 *
4422 * This frees the memory allocated to the q_vectors and
4423 * deletes references to the NAPI struct.
4424 **/
4425static void i40e_vsi_free_q_vectors(struct i40e_vsi *vsi)
4426{
4427 int v_idx;
4428
Alexander Duyck493fb302013-09-28 07:01:44 +00004429 for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++)
4430 i40e_free_q_vector(vsi, v_idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004431}
4432
4433/**
4434 * i40e_reset_interrupt_capability - Disable interrupt setup in OS
4435 * @pf: board private structure
4436 **/
4437static void i40e_reset_interrupt_capability(struct i40e_pf *pf)
4438{
4439 /* If we're in Legacy mode, the interrupt was cleaned in vsi_close */
4440 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4441 pci_disable_msix(pf->pdev);
4442 kfree(pf->msix_entries);
4443 pf->msix_entries = NULL;
Shannon Nelson3b444392015-02-26 16:15:57 +00004444 kfree(pf->irq_pile);
4445 pf->irq_pile = NULL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004446 } else if (pf->flags & I40E_FLAG_MSI_ENABLED) {
4447 pci_disable_msi(pf->pdev);
4448 }
4449 pf->flags &= ~(I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
4450}
4451
4452/**
4453 * i40e_clear_interrupt_scheme - Clear the current interrupt scheme settings
4454 * @pf: board private structure
4455 *
4456 * We go through and clear interrupt specific resources and reset the structure
4457 * to pre-load conditions
4458 **/
4459static void i40e_clear_interrupt_scheme(struct i40e_pf *pf)
4460{
4461 int i;
4462
Shannon Nelsone1477582015-02-21 06:44:33 +00004463 i40e_stop_misc_vector(pf);
Shannon Nelson69278392016-03-10 14:59:43 -08004464 if (pf->flags & I40E_FLAG_MSIX_ENABLED && pf->msix_entries) {
Shannon Nelsone1477582015-02-21 06:44:33 +00004465 synchronize_irq(pf->msix_entries[0].vector);
4466 free_irq(pf->msix_entries[0].vector, pf);
4467 }
4468
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06004469 i40e_put_lump(pf->irq_pile, pf->iwarp_base_vector,
4470 I40E_IWARP_IRQ_PILE_ID);
4471
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004472 i40e_put_lump(pf->irq_pile, 0, I40E_PILE_VALID_BIT-1);
Mitch Williams505682c2014-05-20 08:01:37 +00004473 for (i = 0; i < pf->num_alloc_vsi; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004474 if (pf->vsi[i])
4475 i40e_vsi_free_q_vectors(pf->vsi[i]);
4476 i40e_reset_interrupt_capability(pf);
4477}
4478
4479/**
4480 * i40e_napi_enable_all - Enable NAPI for all q_vectors in the VSI
4481 * @vsi: the VSI being configured
4482 **/
4483static void i40e_napi_enable_all(struct i40e_vsi *vsi)
4484{
4485 int q_idx;
4486
4487 if (!vsi->netdev)
4488 return;
4489
Alexander Duyck13a8cd12017-03-24 15:01:42 -07004490 for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
4491 struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
4492
4493 if (q_vector->rx.ring || q_vector->tx.ring)
4494 napi_enable(&q_vector->napi);
4495 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004496}
4497
4498/**
4499 * i40e_napi_disable_all - Disable NAPI for all q_vectors in the VSI
4500 * @vsi: the VSI being configured
4501 **/
4502static void i40e_napi_disable_all(struct i40e_vsi *vsi)
4503{
4504 int q_idx;
4505
4506 if (!vsi->netdev)
4507 return;
4508
Alexander Duyck13a8cd12017-03-24 15:01:42 -07004509 for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
4510 struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
4511
4512 if (q_vector->rx.ring || q_vector->tx.ring)
4513 napi_disable(&q_vector->napi);
4514 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004515}
4516
4517/**
Shannon Nelson90ef8d42014-03-14 07:32:26 +00004518 * i40e_vsi_close - Shut down a VSI
4519 * @vsi: the vsi to be quelled
4520 **/
4521static void i40e_vsi_close(struct i40e_vsi *vsi)
4522{
Mitch Williams0ef2d5a2017-01-24 10:24:00 -08004523 struct i40e_pf *pf = vsi->back;
Jacob Keller0da36b92017-04-19 09:25:55 -04004524 if (!test_and_set_bit(__I40E_VSI_DOWN, vsi->state))
Shannon Nelson90ef8d42014-03-14 07:32:26 +00004525 i40e_down(vsi);
4526 i40e_vsi_free_irq(vsi);
4527 i40e_vsi_free_tx_resources(vsi);
4528 i40e_vsi_free_rx_resources(vsi);
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04004529 vsi->current_netdev_flags = 0;
Mitch Williams0ef2d5a2017-01-24 10:24:00 -08004530 pf->flags |= I40E_FLAG_SERVICE_CLIENT_REQUESTED;
Jacob Keller0da36b92017-04-19 09:25:55 -04004531 if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
Mitch Williams0ef2d5a2017-01-24 10:24:00 -08004532 pf->flags |= I40E_FLAG_CLIENT_RESET;
Shannon Nelson90ef8d42014-03-14 07:32:26 +00004533}
4534
4535/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004536 * i40e_quiesce_vsi - Pause a given VSI
4537 * @vsi: the VSI being paused
4538 **/
4539static void i40e_quiesce_vsi(struct i40e_vsi *vsi)
4540{
Jacob Keller0da36b92017-04-19 09:25:55 -04004541 if (test_bit(__I40E_VSI_DOWN, vsi->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004542 return;
4543
Jacob Keller0da36b92017-04-19 09:25:55 -04004544 set_bit(__I40E_VSI_NEEDS_RESTART, vsi->state);
Jesse Brandeburg6995b362015-08-28 17:55:54 -04004545 if (vsi->netdev && netif_running(vsi->netdev))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004546 vsi->netdev->netdev_ops->ndo_stop(vsi->netdev);
Jesse Brandeburg6995b362015-08-28 17:55:54 -04004547 else
Shannon Nelson90ef8d42014-03-14 07:32:26 +00004548 i40e_vsi_close(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004549}
4550
4551/**
4552 * i40e_unquiesce_vsi - Resume a given VSI
4553 * @vsi: the VSI being resumed
4554 **/
4555static void i40e_unquiesce_vsi(struct i40e_vsi *vsi)
4556{
Jacob Keller0da36b92017-04-19 09:25:55 -04004557 if (!test_and_clear_bit(__I40E_VSI_NEEDS_RESTART, vsi->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004558 return;
4559
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004560 if (vsi->netdev && netif_running(vsi->netdev))
4561 vsi->netdev->netdev_ops->ndo_open(vsi->netdev);
4562 else
Shannon Nelson8276f752014-03-14 07:32:27 +00004563 i40e_vsi_open(vsi); /* this clears the DOWN bit */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004564}
4565
4566/**
4567 * i40e_pf_quiesce_all_vsi - Pause all VSIs on a PF
4568 * @pf: the PF
4569 **/
4570static void i40e_pf_quiesce_all_vsi(struct i40e_pf *pf)
4571{
4572 int v;
4573
Mitch Williams505682c2014-05-20 08:01:37 +00004574 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004575 if (pf->vsi[v])
4576 i40e_quiesce_vsi(pf->vsi[v]);
4577 }
4578}
4579
4580/**
4581 * i40e_pf_unquiesce_all_vsi - Resume all VSIs on a PF
4582 * @pf: the PF
4583 **/
4584static void i40e_pf_unquiesce_all_vsi(struct i40e_pf *pf)
4585{
4586 int v;
4587
Mitch Williams505682c2014-05-20 08:01:37 +00004588 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004589 if (pf->vsi[v])
4590 i40e_unquiesce_vsi(pf->vsi[v]);
4591 }
4592}
4593
Neerav Parikh69129dc2014-11-12 00:18:46 +00004594/**
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004595 * i40e_vsi_wait_queues_disabled - Wait for VSI's queues to be disabled
Neerav Parikh69129dc2014-11-12 00:18:46 +00004596 * @vsi: the VSI being configured
4597 *
Jacob Kelleraf26ce22017-04-05 07:50:58 -04004598 * Wait until all queues on a given VSI have been disabled.
Neerav Parikh69129dc2014-11-12 00:18:46 +00004599 **/
Jacob Kellere4b433f2017-04-13 04:45:52 -04004600int i40e_vsi_wait_queues_disabled(struct i40e_vsi *vsi)
Neerav Parikh69129dc2014-11-12 00:18:46 +00004601{
4602 struct i40e_pf *pf = vsi->back;
4603 int i, pf_q, ret;
4604
4605 pf_q = vsi->base_queue;
4606 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
Jacob Kelleraf26ce22017-04-05 07:50:58 -04004607 /* Check and wait for the Tx queue */
Neerav Parikh69129dc2014-11-12 00:18:46 +00004608 ret = i40e_pf_txq_wait(pf, pf_q, false);
4609 if (ret) {
4610 dev_info(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04004611 "VSI seid %d Tx ring %d disable timeout\n",
4612 vsi->seid, pf_q);
Neerav Parikh69129dc2014-11-12 00:18:46 +00004613 return ret;
4614 }
Björn Töpel74608d12017-05-24 07:55:35 +02004615
4616 if (!i40e_enabled_xdp_vsi(vsi))
4617 goto wait_rx;
4618
4619 /* Check and wait for the XDP Tx queue */
4620 ret = i40e_pf_txq_wait(pf, pf_q + vsi->alloc_queue_pairs,
4621 false);
4622 if (ret) {
4623 dev_info(&pf->pdev->dev,
4624 "VSI seid %d XDP Tx ring %d disable timeout\n",
4625 vsi->seid, pf_q);
4626 return ret;
4627 }
4628wait_rx:
4629 /* Check and wait for the Rx queue */
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004630 ret = i40e_pf_rxq_wait(pf, pf_q, false);
4631 if (ret) {
4632 dev_info(&pf->pdev->dev,
4633 "VSI seid %d Rx ring %d disable timeout\n",
4634 vsi->seid, pf_q);
4635 return ret;
4636 }
4637 }
4638
Neerav Parikh69129dc2014-11-12 00:18:46 +00004639 return 0;
4640}
4641
Jacob Kellere4b433f2017-04-13 04:45:52 -04004642#ifdef CONFIG_I40E_DCB
Neerav Parikh69129dc2014-11-12 00:18:46 +00004643/**
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004644 * i40e_pf_wait_queues_disabled - Wait for all queues of PF VSIs to be disabled
Neerav Parikh69129dc2014-11-12 00:18:46 +00004645 * @pf: the PF
4646 *
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004647 * This function waits for the queues to be in disabled state for all the
Neerav Parikh69129dc2014-11-12 00:18:46 +00004648 * VSIs that are managed by this PF.
4649 **/
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004650static int i40e_pf_wait_queues_disabled(struct i40e_pf *pf)
Neerav Parikh69129dc2014-11-12 00:18:46 +00004651{
4652 int v, ret = 0;
4653
4654 for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
Alexander Duyckc76cb6e2017-02-21 15:55:48 -08004655 if (pf->vsi[v]) {
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004656 ret = i40e_vsi_wait_queues_disabled(pf->vsi[v]);
Neerav Parikh69129dc2014-11-12 00:18:46 +00004657 if (ret)
4658 break;
4659 }
4660 }
4661
4662 return ret;
4663}
4664
4665#endif
Kiran Patilb03a8c12015-09-24 18:13:15 -04004666
4667/**
4668 * i40e_detect_recover_hung_queue - Function to detect and recover hung_queue
4669 * @q_idx: TX queue number
4670 * @vsi: Pointer to VSI struct
4671 *
4672 * This function checks specified queue for given VSI. Detects hung condition.
Alan Brady17daabb2017-04-05 07:50:56 -04004673 * We proactively detect hung TX queues by checking if interrupts are disabled
4674 * but there are pending descriptors. If it appears hung, attempt to recover
4675 * by triggering a SW interrupt.
Kiran Patilb03a8c12015-09-24 18:13:15 -04004676 **/
4677static void i40e_detect_recover_hung_queue(int q_idx, struct i40e_vsi *vsi)
4678{
4679 struct i40e_ring *tx_ring = NULL;
4680 struct i40e_pf *pf;
Alan Brady17daabb2017-04-05 07:50:56 -04004681 u32 val, tx_pending;
Kiran Patilb03a8c12015-09-24 18:13:15 -04004682 int i;
4683
4684 pf = vsi->back;
4685
4686 /* now that we have an index, find the tx_ring struct */
4687 for (i = 0; i < vsi->num_queue_pairs; i++) {
4688 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc) {
4689 if (q_idx == vsi->tx_rings[i]->queue_index) {
4690 tx_ring = vsi->tx_rings[i];
4691 break;
4692 }
4693 }
4694 }
4695
4696 if (!tx_ring)
4697 return;
4698
4699 /* Read interrupt register */
4700 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
4701 val = rd32(&pf->hw,
4702 I40E_PFINT_DYN_CTLN(tx_ring->q_vector->v_idx +
4703 tx_ring->vsi->base_vector - 1));
4704 else
4705 val = rd32(&pf->hw, I40E_PFINT_DYN_CTL0);
4706
Alan Brady17daabb2017-04-05 07:50:56 -04004707 tx_pending = i40e_get_tx_pending(tx_ring);
Kiran Patilb03a8c12015-09-24 18:13:15 -04004708
Alan Brady17daabb2017-04-05 07:50:56 -04004709 /* Interrupts are disabled and TX pending is non-zero,
4710 * trigger the SW interrupt (don't wait). Worst case
4711 * there will be one extra interrupt which may result
4712 * into not cleaning any queues because queues are cleaned.
Kiran Patilb03a8c12015-09-24 18:13:15 -04004713 */
Alan Brady17daabb2017-04-05 07:50:56 -04004714 if (tx_pending && (!(val & I40E_PFINT_DYN_CTLN_INTENA_MASK)))
4715 i40e_force_wb(vsi, tx_ring->q_vector);
Kiran Patilb03a8c12015-09-24 18:13:15 -04004716}
4717
4718/**
4719 * i40e_detect_recover_hung - Function to detect and recover hung_queues
4720 * @pf: pointer to PF struct
4721 *
4722 * LAN VSI has netdev and netdev has TX queues. This function is to check
4723 * each of those TX queues if they are hung, trigger recovery by issuing
4724 * SW interrupt.
4725 **/
4726static void i40e_detect_recover_hung(struct i40e_pf *pf)
4727{
4728 struct net_device *netdev;
4729 struct i40e_vsi *vsi;
Jesse Brandeburgb85c94b2017-06-20 15:16:59 -07004730 unsigned int i;
Kiran Patilb03a8c12015-09-24 18:13:15 -04004731
4732 /* Only for LAN VSI */
4733 vsi = pf->vsi[pf->lan_vsi];
4734
4735 if (!vsi)
4736 return;
4737
4738 /* Make sure, VSI state is not DOWN/RECOVERY_PENDING */
Jacob Keller0da36b92017-04-19 09:25:55 -04004739 if (test_bit(__I40E_VSI_DOWN, vsi->back->state) ||
4740 test_bit(__I40E_RESET_RECOVERY_PENDING, vsi->back->state))
Kiran Patilb03a8c12015-09-24 18:13:15 -04004741 return;
4742
4743 /* Make sure type is MAIN VSI */
4744 if (vsi->type != I40E_VSI_MAIN)
4745 return;
4746
4747 netdev = vsi->netdev;
4748 if (!netdev)
4749 return;
4750
4751 /* Bail out if netif_carrier is not OK */
4752 if (!netif_carrier_ok(netdev))
4753 return;
4754
4755 /* Go thru' TX queues for netdev */
4756 for (i = 0; i < netdev->num_tx_queues; i++) {
4757 struct netdev_queue *q;
4758
4759 q = netdev_get_tx_queue(netdev, i);
4760 if (q)
4761 i40e_detect_recover_hung_queue(i, vsi);
4762 }
4763}
4764
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004765/**
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004766 * i40e_get_iscsi_tc_map - Return TC map for iSCSI APP
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00004767 * @pf: pointer to PF
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004768 *
4769 * Get TC map for ISCSI PF type that will include iSCSI TC
4770 * and LAN TC.
4771 **/
4772static u8 i40e_get_iscsi_tc_map(struct i40e_pf *pf)
4773{
4774 struct i40e_dcb_app_priority_table app;
4775 struct i40e_hw *hw = &pf->hw;
4776 u8 enabled_tc = 1; /* TC0 is always enabled */
4777 u8 tc, i;
4778 /* Get the iSCSI APP TLV */
4779 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
4780
4781 for (i = 0; i < dcbcfg->numapps; i++) {
4782 app = dcbcfg->app[i];
4783 if (app.selector == I40E_APP_SEL_TCPIP &&
4784 app.protocolid == I40E_APP_PROTOID_ISCSI) {
4785 tc = dcbcfg->etscfg.prioritytable[app.priority];
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08004786 enabled_tc |= BIT(tc);
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004787 break;
4788 }
4789 }
4790
4791 return enabled_tc;
4792}
4793
4794/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004795 * i40e_dcb_get_num_tc - Get the number of TCs from DCBx config
4796 * @dcbcfg: the corresponding DCBx configuration structure
4797 *
4798 * Return the number of TCs from given DCBx configuration
4799 **/
4800static u8 i40e_dcb_get_num_tc(struct i40e_dcbx_config *dcbcfg)
4801{
Dave Ertmanfbfe12c2016-08-12 09:56:32 -07004802 int i, tc_unused = 0;
Jesse Brandeburg078b5872013-09-25 23:41:14 +00004803 u8 num_tc = 0;
Dave Ertmanfbfe12c2016-08-12 09:56:32 -07004804 u8 ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004805
4806 /* Scan the ETS Config Priority Table to find
4807 * traffic class enabled for a given priority
Dave Ertmanfbfe12c2016-08-12 09:56:32 -07004808 * and create a bitmask of enabled TCs
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004809 */
Dave Ertmanfbfe12c2016-08-12 09:56:32 -07004810 for (i = 0; i < I40E_MAX_USER_PRIORITY; i++)
4811 num_tc |= BIT(dcbcfg->etscfg.prioritytable[i]);
4812
4813 /* Now scan the bitmask to check for
4814 * contiguous TCs starting with TC0
4815 */
4816 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
4817 if (num_tc & BIT(i)) {
4818 if (!tc_unused) {
4819 ret++;
4820 } else {
4821 pr_err("Non-contiguous TC - Disabling DCB\n");
4822 return 1;
4823 }
4824 } else {
4825 tc_unused = 1;
4826 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004827 }
4828
Dave Ertmanfbfe12c2016-08-12 09:56:32 -07004829 /* There is always at least TC0 */
4830 if (!ret)
4831 ret = 1;
4832
4833 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004834}
4835
4836/**
4837 * i40e_dcb_get_enabled_tc - Get enabled traffic classes
4838 * @dcbcfg: the corresponding DCBx configuration structure
4839 *
4840 * Query the current DCB configuration and return the number of
4841 * traffic classes enabled from the given DCBX config
4842 **/
4843static u8 i40e_dcb_get_enabled_tc(struct i40e_dcbx_config *dcbcfg)
4844{
4845 u8 num_tc = i40e_dcb_get_num_tc(dcbcfg);
4846 u8 enabled_tc = 1;
4847 u8 i;
4848
4849 for (i = 0; i < num_tc; i++)
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04004850 enabled_tc |= BIT(i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004851
4852 return enabled_tc;
4853}
4854
4855/**
4856 * i40e_pf_get_num_tc - Get enabled traffic classes for PF
4857 * @pf: PF being queried
4858 *
4859 * Return number of traffic classes enabled for the given PF
4860 **/
4861static u8 i40e_pf_get_num_tc(struct i40e_pf *pf)
4862{
4863 struct i40e_hw *hw = &pf->hw;
Dave Ertman52a08ca2016-07-27 12:02:34 -07004864 u8 i, enabled_tc = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004865 u8 num_tc = 0;
4866 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
4867
4868 /* If DCB is not enabled then always in single TC */
4869 if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
4870 return 1;
4871
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004872 /* SFP mode will be enabled for all TCs on port */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004873 if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
4874 return i40e_dcb_get_num_tc(dcbcfg);
4875
4876 /* MFP mode return count of enabled TCs for this PF */
4877 if (pf->hw.func_caps.iscsi)
4878 enabled_tc = i40e_get_iscsi_tc_map(pf);
4879 else
Neerav Parikhfc51de92015-02-24 06:58:53 +00004880 return 1; /* Only TC0 */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004881
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004882 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08004883 if (enabled_tc & BIT(i))
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004884 num_tc++;
4885 }
4886 return num_tc;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004887}
4888
4889/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004890 * i40e_pf_get_pf_tc_map - Get bitmap for enabled traffic classes
4891 * @pf: PF being queried
4892 *
4893 * Return a bitmap for enabled traffic classes for this PF.
4894 **/
4895static u8 i40e_pf_get_tc_map(struct i40e_pf *pf)
4896{
4897 /* If DCB is not enabled for this PF then just return default TC */
4898 if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
David Ertmanea6acb72016-09-20 07:10:50 -07004899 return I40E_DEFAULT_TRAFFIC_CLASS;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004900
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004901 /* SFP mode we want PF to be enabled for all TCs */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004902 if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
4903 return i40e_dcb_get_enabled_tc(&pf->hw.local_dcbx_config);
4904
Neerav Parikhfc51de92015-02-24 06:58:53 +00004905 /* MFP enabled and iSCSI PF type */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004906 if (pf->hw.func_caps.iscsi)
4907 return i40e_get_iscsi_tc_map(pf);
4908 else
David Ertmanea6acb72016-09-20 07:10:50 -07004909 return I40E_DEFAULT_TRAFFIC_CLASS;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004910}
4911
4912/**
4913 * i40e_vsi_get_bw_info - Query VSI BW Information
4914 * @vsi: the VSI being queried
4915 *
4916 * Returns 0 on success, negative value on failure
4917 **/
4918static int i40e_vsi_get_bw_info(struct i40e_vsi *vsi)
4919{
4920 struct i40e_aqc_query_vsi_ets_sla_config_resp bw_ets_config = {0};
4921 struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
4922 struct i40e_pf *pf = vsi->back;
4923 struct i40e_hw *hw = &pf->hw;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004924 i40e_status ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004925 u32 tc_bw_max;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004926 int i;
4927
4928 /* Get the VSI level BW configuration */
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004929 ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid, &bw_config, NULL);
4930 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004931 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004932 "couldn't get PF vsi bw config, err %s aq_err %s\n",
4933 i40e_stat_str(&pf->hw, ret),
4934 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004935 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004936 }
4937
4938 /* Get the VSI level BW configuration per TC */
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004939 ret = i40e_aq_query_vsi_ets_sla_config(hw, vsi->seid, &bw_ets_config,
4940 NULL);
4941 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004942 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004943 "couldn't get PF vsi ets bw config, err %s aq_err %s\n",
4944 i40e_stat_str(&pf->hw, ret),
4945 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004946 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004947 }
4948
4949 if (bw_config.tc_valid_bits != bw_ets_config.tc_valid_bits) {
4950 dev_info(&pf->pdev->dev,
4951 "Enabled TCs mismatch from querying VSI BW info 0x%08x 0x%08x\n",
4952 bw_config.tc_valid_bits,
4953 bw_ets_config.tc_valid_bits);
4954 /* Still continuing */
4955 }
4956
4957 vsi->bw_limit = le16_to_cpu(bw_config.port_bw_limit);
4958 vsi->bw_max_quanta = bw_config.max_bw;
4959 tc_bw_max = le16_to_cpu(bw_ets_config.tc_bw_max[0]) |
4960 (le16_to_cpu(bw_ets_config.tc_bw_max[1]) << 16);
4961 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
4962 vsi->bw_ets_share_credits[i] = bw_ets_config.share_credits[i];
4963 vsi->bw_ets_limit_credits[i] =
4964 le16_to_cpu(bw_ets_config.credits[i]);
4965 /* 3 bits out of 4 for each TC */
4966 vsi->bw_ets_max_quanta[i] = (u8)((tc_bw_max >> (i*4)) & 0x7);
4967 }
Jesse Brandeburg078b5872013-09-25 23:41:14 +00004968
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004969 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004970}
4971
4972/**
4973 * i40e_vsi_configure_bw_alloc - Configure VSI BW allocation per TC
4974 * @vsi: the VSI being configured
4975 * @enabled_tc: TC bitmap
4976 * @bw_credits: BW shared credits per TC
4977 *
4978 * Returns 0 on success, negative value on failure
4979 **/
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004980static int i40e_vsi_configure_bw_alloc(struct i40e_vsi *vsi, u8 enabled_tc,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004981 u8 *bw_share)
4982{
4983 struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004984 i40e_status ret;
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004985 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004986
4987 bw_data.tc_valid_bits = enabled_tc;
4988 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
4989 bw_data.tc_bw_credits[i] = bw_share[i];
4990
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004991 ret = i40e_aq_config_vsi_tc_bw(&vsi->back->hw, vsi->seid, &bw_data,
4992 NULL);
4993 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004994 dev_info(&vsi->back->pdev->dev,
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004995 "AQ command Config VSI BW allocation per TC failed = %d\n",
4996 vsi->back->hw.aq.asq_last_status);
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004997 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004998 }
4999
5000 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5001 vsi->info.qs_handle[i] = bw_data.qs_handles[i];
5002
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00005003 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005004}
5005
5006/**
5007 * i40e_vsi_config_netdev_tc - Setup the netdev TC configuration
5008 * @vsi: the VSI being configured
5009 * @enabled_tc: TC map to be enabled
5010 *
5011 **/
5012static void i40e_vsi_config_netdev_tc(struct i40e_vsi *vsi, u8 enabled_tc)
5013{
5014 struct net_device *netdev = vsi->netdev;
5015 struct i40e_pf *pf = vsi->back;
5016 struct i40e_hw *hw = &pf->hw;
5017 u8 netdev_tc = 0;
5018 int i;
5019 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5020
5021 if (!netdev)
5022 return;
5023
5024 if (!enabled_tc) {
5025 netdev_reset_tc(netdev);
5026 return;
5027 }
5028
5029 /* Set up actual enabled TCs on the VSI */
5030 if (netdev_set_num_tc(netdev, vsi->tc_config.numtc))
5031 return;
5032
5033 /* set per TC queues for the VSI */
5034 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5035 /* Only set TC queues for enabled tcs
5036 *
5037 * e.g. For a VSI that has TC0 and TC3 enabled the
5038 * enabled_tc bitmap would be 0x00001001; the driver
5039 * will set the numtc for netdev as 2 that will be
5040 * referenced by the netdev layer as TC 0 and 1.
5041 */
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08005042 if (vsi->tc_config.enabled_tc & BIT(i))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005043 netdev_set_tc_queue(netdev,
5044 vsi->tc_config.tc_info[i].netdev_tc,
5045 vsi->tc_config.tc_info[i].qcount,
5046 vsi->tc_config.tc_info[i].qoffset);
5047 }
5048
5049 /* Assign UP2TC map for the VSI */
5050 for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
5051 /* Get the actual TC# for the UP */
5052 u8 ets_tc = dcbcfg->etscfg.prioritytable[i];
5053 /* Get the mapped netdev TC# for the UP */
5054 netdev_tc = vsi->tc_config.tc_info[ets_tc].netdev_tc;
5055 netdev_set_prio_tc_map(netdev, i, netdev_tc);
5056 }
5057}
5058
5059/**
5060 * i40e_vsi_update_queue_map - Update our copy of VSi info with new queue map
5061 * @vsi: the VSI being configured
5062 * @ctxt: the ctxt buffer returned from AQ VSI update param command
5063 **/
5064static void i40e_vsi_update_queue_map(struct i40e_vsi *vsi,
5065 struct i40e_vsi_context *ctxt)
5066{
5067 /* copy just the sections touched not the entire info
5068 * since not all sections are valid as returned by
5069 * update vsi params
5070 */
5071 vsi->info.mapping_flags = ctxt->info.mapping_flags;
5072 memcpy(&vsi->info.queue_mapping,
5073 &ctxt->info.queue_mapping, sizeof(vsi->info.queue_mapping));
5074 memcpy(&vsi->info.tc_mapping, ctxt->info.tc_mapping,
5075 sizeof(vsi->info.tc_mapping));
5076}
5077
5078/**
5079 * i40e_vsi_config_tc - Configure VSI Tx Scheduler for given TC map
5080 * @vsi: VSI to be configured
5081 * @enabled_tc: TC bitmap
5082 *
5083 * This configures a particular VSI for TCs that are mapped to the
5084 * given TC bitmap. It uses default bandwidth share for TCs across
5085 * VSIs to configure TC for a particular VSI.
5086 *
5087 * NOTE:
5088 * It is expected that the VSI queues have been quisced before calling
5089 * this function.
5090 **/
5091static int i40e_vsi_config_tc(struct i40e_vsi *vsi, u8 enabled_tc)
5092{
5093 u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
5094 struct i40e_vsi_context ctxt;
5095 int ret = 0;
5096 int i;
5097
5098 /* Check if enabled_tc is same as existing or new TCs */
5099 if (vsi->tc_config.enabled_tc == enabled_tc)
5100 return ret;
5101
5102 /* Enable ETS TCs with equal BW Share for now across all VSIs */
5103 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08005104 if (enabled_tc & BIT(i))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005105 bw_share[i] = 1;
5106 }
5107
5108 ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
5109 if (ret) {
5110 dev_info(&vsi->back->pdev->dev,
5111 "Failed configuring TC map %d for VSI %d\n",
5112 enabled_tc, vsi->seid);
5113 goto out;
5114 }
5115
5116 /* Update Queue Pairs Mapping for currently enabled UPs */
5117 ctxt.seid = vsi->seid;
5118 ctxt.pf_num = vsi->back->hw.pf_id;
5119 ctxt.vf_num = 0;
5120 ctxt.uplink_seid = vsi->uplink_seid;
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07005121 ctxt.info = vsi->info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005122 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
5123
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06005124 if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
5125 ctxt.info.valid_sections |=
5126 cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
5127 ctxt.info.queueing_opt_flags |= I40E_AQ_VSI_QUE_OPT_TCP_ENA;
5128 }
5129
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005130 /* Update the VSI after updating the VSI queue-mapping information */
5131 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
5132 if (ret) {
5133 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005134 "Update vsi tc config failed, err %s aq_err %s\n",
5135 i40e_stat_str(&vsi->back->hw, ret),
5136 i40e_aq_str(&vsi->back->hw,
5137 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005138 goto out;
5139 }
5140 /* update the local VSI info with updated queue map */
5141 i40e_vsi_update_queue_map(vsi, &ctxt);
5142 vsi->info.valid_sections = 0;
5143
5144 /* Update current VSI BW information */
5145 ret = i40e_vsi_get_bw_info(vsi);
5146 if (ret) {
5147 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005148 "Failed updating vsi bw info, err %s aq_err %s\n",
5149 i40e_stat_str(&vsi->back->hw, ret),
5150 i40e_aq_str(&vsi->back->hw,
5151 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005152 goto out;
5153 }
5154
5155 /* Update the netdev TC setup */
5156 i40e_vsi_config_netdev_tc(vsi, enabled_tc);
5157out:
5158 return ret;
5159}
5160
5161/**
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005162 * i40e_veb_config_tc - Configure TCs for given VEB
5163 * @veb: given VEB
5164 * @enabled_tc: TC bitmap
5165 *
5166 * Configures given TC bitmap for VEB (switching) element
5167 **/
5168int i40e_veb_config_tc(struct i40e_veb *veb, u8 enabled_tc)
5169{
5170 struct i40e_aqc_configure_switching_comp_bw_config_data bw_data = {0};
5171 struct i40e_pf *pf = veb->pf;
5172 int ret = 0;
5173 int i;
5174
5175 /* No TCs or already enabled TCs just return */
5176 if (!enabled_tc || veb->enabled_tc == enabled_tc)
5177 return ret;
5178
5179 bw_data.tc_valid_bits = enabled_tc;
5180 /* bw_data.absolute_credits is not set (relative) */
5181
5182 /* Enable ETS TCs with equal BW Share for now */
5183 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08005184 if (enabled_tc & BIT(i))
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005185 bw_data.tc_bw_share_credits[i] = 1;
5186 }
5187
5188 ret = i40e_aq_config_switch_comp_bw_config(&pf->hw, veb->seid,
5189 &bw_data, NULL);
5190 if (ret) {
5191 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005192 "VEB bw config failed, err %s aq_err %s\n",
5193 i40e_stat_str(&pf->hw, ret),
5194 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005195 goto out;
5196 }
5197
5198 /* Update the BW information */
5199 ret = i40e_veb_get_bw_info(veb);
5200 if (ret) {
5201 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005202 "Failed getting veb bw config, err %s aq_err %s\n",
5203 i40e_stat_str(&pf->hw, ret),
5204 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005205 }
5206
5207out:
5208 return ret;
5209}
5210
5211#ifdef CONFIG_I40E_DCB
5212/**
5213 * i40e_dcb_reconfigure - Reconfigure all VEBs and VSIs
5214 * @pf: PF struct
5215 *
5216 * Reconfigure VEB/VSIs on a given PF; it is assumed that
5217 * the caller would've quiesce all the VSIs before calling
5218 * this function
5219 **/
5220static void i40e_dcb_reconfigure(struct i40e_pf *pf)
5221{
5222 u8 tc_map = 0;
5223 int ret;
5224 u8 v;
5225
5226 /* Enable the TCs available on PF to all VEBs */
5227 tc_map = i40e_pf_get_tc_map(pf);
5228 for (v = 0; v < I40E_MAX_VEB; v++) {
5229 if (!pf->veb[v])
5230 continue;
5231 ret = i40e_veb_config_tc(pf->veb[v], tc_map);
5232 if (ret) {
5233 dev_info(&pf->pdev->dev,
5234 "Failed configuring TC for VEB seid=%d\n",
5235 pf->veb[v]->seid);
5236 /* Will try to configure as many components */
5237 }
5238 }
5239
5240 /* Update each VSI */
Mitch Williams505682c2014-05-20 08:01:37 +00005241 for (v = 0; v < pf->num_alloc_vsi; v++) {
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005242 if (!pf->vsi[v])
5243 continue;
5244
5245 /* - Enable all TCs for the LAN VSI
5246 * - For all others keep them at TC0 for now
5247 */
5248 if (v == pf->lan_vsi)
5249 tc_map = i40e_pf_get_tc_map(pf);
5250 else
David Ertmanea6acb72016-09-20 07:10:50 -07005251 tc_map = I40E_DEFAULT_TRAFFIC_CLASS;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005252
5253 ret = i40e_vsi_config_tc(pf->vsi[v], tc_map);
5254 if (ret) {
5255 dev_info(&pf->pdev->dev,
5256 "Failed configuring TC for VSI seid=%d\n",
5257 pf->vsi[v]->seid);
5258 /* Will try to configure as many components */
5259 } else {
Neerav Parikh0672a092014-04-01 07:11:47 +00005260 /* Re-configure VSI vectors based on updated TC map */
5261 i40e_vsi_map_rings_to_vectors(pf->vsi[v]);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005262 if (pf->vsi[v]->netdev)
5263 i40e_dcbnl_set_all(pf->vsi[v]);
5264 }
5265 }
5266}
5267
5268/**
Neerav Parikh2fd75f32014-11-12 00:18:20 +00005269 * i40e_resume_port_tx - Resume port Tx
5270 * @pf: PF struct
5271 *
5272 * Resume a port's Tx and issue a PF reset in case of failure to
5273 * resume.
5274 **/
5275static int i40e_resume_port_tx(struct i40e_pf *pf)
5276{
5277 struct i40e_hw *hw = &pf->hw;
5278 int ret;
5279
5280 ret = i40e_aq_resume_port_tx(hw, NULL);
5281 if (ret) {
5282 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005283 "Resume Port Tx failed, err %s aq_err %s\n",
5284 i40e_stat_str(&pf->hw, ret),
5285 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh2fd75f32014-11-12 00:18:20 +00005286 /* Schedule PF reset to recover */
Jacob Keller0da36b92017-04-19 09:25:55 -04005287 set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
Neerav Parikh2fd75f32014-11-12 00:18:20 +00005288 i40e_service_event_schedule(pf);
5289 }
5290
5291 return ret;
5292}
5293
5294/**
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005295 * i40e_init_pf_dcb - Initialize DCB configuration
5296 * @pf: PF being configured
5297 *
5298 * Query the current DCB configuration and cache it
5299 * in the hardware structure
5300 **/
5301static int i40e_init_pf_dcb(struct i40e_pf *pf)
5302{
5303 struct i40e_hw *hw = &pf->hw;
5304 int err = 0;
5305
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +00005306 /* Do not enable DCB for SW1 and SW2 images even if the FW is capable */
Jacob Kellerd36e41d2017-06-23 04:24:46 -04005307 if (pf->hw_features & I40E_HW_NO_DCB_SUPPORT)
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +00005308 goto out;
5309
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005310 /* Get the initial DCB configuration */
5311 err = i40e_init_dcb(hw);
5312 if (!err) {
5313 /* Device/Function is not DCBX capable */
5314 if ((!hw->func_caps.dcb) ||
5315 (hw->dcbx_status == I40E_DCBX_STATUS_DISABLED)) {
5316 dev_info(&pf->pdev->dev,
5317 "DCBX offload is not supported or is disabled for this PF.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005318 } else {
5319 /* When status is not DISABLED then DCBX in FW */
5320 pf->dcbx_cap = DCB_CAP_DCBX_LLD_MANAGED |
5321 DCB_CAP_DCBX_VER_IEEE;
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005322
5323 pf->flags |= I40E_FLAG_DCB_CAPABLE;
Dave Ertmana0362442016-08-29 17:38:26 -07005324 /* Enable DCB tagging only when more than one TC
5325 * or explicitly disable if only one TC
5326 */
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005327 if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
5328 pf->flags |= I40E_FLAG_DCB_ENABLED;
Dave Ertmana0362442016-08-29 17:38:26 -07005329 else
5330 pf->flags &= ~I40E_FLAG_DCB_ENABLED;
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005331 dev_dbg(&pf->pdev->dev,
5332 "DCBX offload is supported for this PF.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005333 }
Neerav Parikh014269f2014-04-01 07:11:48 +00005334 } else {
Shannon Nelsonaebfc812014-12-11 07:06:38 +00005335 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005336 "Query for DCB configuration failed, err %s aq_err %s\n",
5337 i40e_stat_str(&pf->hw, err),
5338 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005339 }
5340
5341out:
5342 return err;
5343}
5344#endif /* CONFIG_I40E_DCB */
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005345#define SPEED_SIZE 14
5346#define FC_SIZE 8
5347/**
5348 * i40e_print_link_message - print link up or down
5349 * @vsi: the VSI for which link needs a message
5350 */
Matt Jaredc156f852015-08-27 11:42:39 -04005351void i40e_print_link_message(struct i40e_vsi *vsi, bool isup)
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005352{
Filip Sadowski7ec9ba12016-11-08 13:05:13 -08005353 enum i40e_aq_link_speed new_speed;
Shannon Nelsona9165492015-09-03 17:19:00 -04005354 char *speed = "Unknown";
5355 char *fc = "Unknown";
Henry Tieman3e03d7c2016-12-02 12:32:57 -08005356 char *fec = "";
5357 char *an = "";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005358
Filip Sadowski7ec9ba12016-11-08 13:05:13 -08005359 new_speed = vsi->back->hw.phy.link_info.link_speed;
5360
5361 if ((vsi->current_isup == isup) && (vsi->current_speed == new_speed))
Matt Jaredc156f852015-08-27 11:42:39 -04005362 return;
5363 vsi->current_isup = isup;
Filip Sadowski7ec9ba12016-11-08 13:05:13 -08005364 vsi->current_speed = new_speed;
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005365 if (!isup) {
5366 netdev_info(vsi->netdev, "NIC Link is Down\n");
5367 return;
5368 }
5369
Greg Rose148c2d82014-12-11 07:06:27 +00005370 /* Warn user if link speed on NPAR enabled partition is not at
5371 * least 10GB
5372 */
5373 if (vsi->back->hw.func_caps.npar_enable &&
5374 (vsi->back->hw.phy.link_info.link_speed == I40E_LINK_SPEED_1GB ||
5375 vsi->back->hw.phy.link_info.link_speed == I40E_LINK_SPEED_100MB))
5376 netdev_warn(vsi->netdev,
5377 "The partition detected link speed that is less than 10Gbps\n");
5378
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005379 switch (vsi->back->hw.phy.link_info.link_speed) {
5380 case I40E_LINK_SPEED_40GB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005381 speed = "40 G";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005382 break;
Jesse Brandeburgae24b402015-03-27 00:12:09 -07005383 case I40E_LINK_SPEED_20GB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005384 speed = "20 G";
Jesse Brandeburgae24b402015-03-27 00:12:09 -07005385 break;
Carolyn Wyborny31232372016-11-21 13:03:48 -08005386 case I40E_LINK_SPEED_25GB:
5387 speed = "25 G";
5388 break;
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005389 case I40E_LINK_SPEED_10GB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005390 speed = "10 G";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005391 break;
5392 case I40E_LINK_SPEED_1GB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005393 speed = "1000 M";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005394 break;
Mitch Williams5960d332014-09-13 07:40:47 +00005395 case I40E_LINK_SPEED_100MB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005396 speed = "100 M";
Mitch Williams5960d332014-09-13 07:40:47 +00005397 break;
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005398 default:
5399 break;
5400 }
5401
5402 switch (vsi->back->hw.fc.current_mode) {
5403 case I40E_FC_FULL:
Shannon Nelsona9165492015-09-03 17:19:00 -04005404 fc = "RX/TX";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005405 break;
5406 case I40E_FC_TX_PAUSE:
Shannon Nelsona9165492015-09-03 17:19:00 -04005407 fc = "TX";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005408 break;
5409 case I40E_FC_RX_PAUSE:
Shannon Nelsona9165492015-09-03 17:19:00 -04005410 fc = "RX";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005411 break;
5412 default:
Shannon Nelsona9165492015-09-03 17:19:00 -04005413 fc = "None";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005414 break;
5415 }
5416
Henry Tieman3e03d7c2016-12-02 12:32:57 -08005417 if (vsi->back->hw.phy.link_info.link_speed == I40E_LINK_SPEED_25GB) {
5418 fec = ", FEC: None";
5419 an = ", Autoneg: False";
5420
5421 if (vsi->back->hw.phy.link_info.an_info & I40E_AQ_AN_COMPLETED)
5422 an = ", Autoneg: True";
5423
5424 if (vsi->back->hw.phy.link_info.fec_info &
5425 I40E_AQ_CONFIG_FEC_KR_ENA)
5426 fec = ", FEC: CL74 FC-FEC/BASE-R";
5427 else if (vsi->back->hw.phy.link_info.fec_info &
5428 I40E_AQ_CONFIG_FEC_RS_ENA)
5429 fec = ", FEC: CL108 RS-FEC";
5430 }
5431
5432 netdev_info(vsi->netdev, "NIC Link is Up, %sbps Full Duplex%s%s, Flow Control: %s\n",
5433 speed, fec, an, fc);
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005434}
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005435
5436/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005437 * i40e_up_complete - Finish the last steps of bringing up a connection
5438 * @vsi: the VSI being configured
5439 **/
5440static int i40e_up_complete(struct i40e_vsi *vsi)
5441{
5442 struct i40e_pf *pf = vsi->back;
5443 int err;
5444
5445 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
5446 i40e_vsi_configure_msix(vsi);
5447 else
5448 i40e_configure_msi_and_legacy(vsi);
5449
5450 /* start rings */
Filip Sadowski3aa7b742016-10-11 15:26:58 -07005451 err = i40e_vsi_start_rings(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005452 if (err)
5453 return err;
5454
Jacob Keller0da36b92017-04-19 09:25:55 -04005455 clear_bit(__I40E_VSI_DOWN, vsi->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005456 i40e_napi_enable_all(vsi);
5457 i40e_vsi_enable_irq(vsi);
5458
5459 if ((pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP) &&
5460 (vsi->netdev)) {
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005461 i40e_print_link_message(vsi, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005462 netif_tx_start_all_queues(vsi->netdev);
5463 netif_carrier_on(vsi->netdev);
Anjali Singhai6d779b42013-09-28 06:00:02 +00005464 } else if (vsi->netdev) {
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005465 i40e_print_link_message(vsi, false);
Carolyn Wyborny7b592f62014-07-10 07:58:19 +00005466 /* need to check for qualified module here*/
5467 if ((pf->hw.phy.link_info.link_info &
5468 I40E_AQ_MEDIA_AVAILABLE) &&
5469 (!(pf->hw.phy.link_info.an_info &
5470 I40E_AQ_QUALIFIED_MODULE)))
5471 netdev_err(vsi->netdev,
5472 "the driver failed to link because an unqualified module was detected.");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005473 }
Anjali Singhai Jainca64fa42014-02-11 08:26:30 +00005474
5475 /* replay FDIR SB filters */
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005476 if (vsi->type == I40E_VSI_FDIR) {
5477 /* reset fd counters */
Jacob Keller097dbf52017-02-06 14:38:46 -08005478 pf->fd_add_err = 0;
5479 pf->fd_atr_cnt = 0;
Anjali Singhai Jainca64fa42014-02-11 08:26:30 +00005480 i40e_fdir_filter_restore(vsi);
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005481 }
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06005482
5483 /* On the next run of the service_task, notify any clients of the new
5484 * opened netdev
5485 */
5486 pf->flags |= I40E_FLAG_SERVICE_CLIENT_REQUESTED;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005487 i40e_service_event_schedule(pf);
5488
5489 return 0;
5490}
5491
5492/**
5493 * i40e_vsi_reinit_locked - Reset the VSI
5494 * @vsi: the VSI being configured
5495 *
5496 * Rebuild the ring structs after some configuration
5497 * has changed, e.g. MTU size.
5498 **/
5499static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi)
5500{
5501 struct i40e_pf *pf = vsi->back;
5502
5503 WARN_ON(in_interrupt());
Jacob Keller0da36b92017-04-19 09:25:55 -04005504 while (test_and_set_bit(__I40E_CONFIG_BUSY, pf->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005505 usleep_range(1000, 2000);
5506 i40e_down(vsi);
5507
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005508 i40e_up(vsi);
Jacob Keller0da36b92017-04-19 09:25:55 -04005509 clear_bit(__I40E_CONFIG_BUSY, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005510}
5511
5512/**
5513 * i40e_up - Bring the connection back up after being down
5514 * @vsi: the VSI being configured
5515 **/
5516int i40e_up(struct i40e_vsi *vsi)
5517{
5518 int err;
5519
5520 err = i40e_vsi_configure(vsi);
5521 if (!err)
5522 err = i40e_up_complete(vsi);
5523
5524 return err;
5525}
5526
5527/**
5528 * i40e_down - Shutdown the connection processing
5529 * @vsi: the VSI being stopped
5530 **/
5531void i40e_down(struct i40e_vsi *vsi)
5532{
5533 int i;
5534
5535 /* It is assumed that the caller of this function
Jacob Kellerd19cb642017-04-21 13:38:05 -07005536 * sets the vsi->state __I40E_VSI_DOWN bit.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005537 */
5538 if (vsi->netdev) {
5539 netif_carrier_off(vsi->netdev);
5540 netif_tx_disable(vsi->netdev);
5541 }
5542 i40e_vsi_disable_irq(vsi);
Filip Sadowski3aa7b742016-10-11 15:26:58 -07005543 i40e_vsi_stop_rings(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005544 i40e_napi_disable_all(vsi);
5545
5546 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck9f65e152013-09-28 06:00:58 +00005547 i40e_clean_tx_ring(vsi->tx_rings[i]);
Björn Töpel74608d12017-05-24 07:55:35 +02005548 if (i40e_enabled_xdp_vsi(vsi))
5549 i40e_clean_tx_ring(vsi->xdp_rings[i]);
Alexander Duyck9f65e152013-09-28 06:00:58 +00005550 i40e_clean_rx_ring(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005551 }
Catherine Sullivanf980d442016-05-16 10:26:34 -07005552
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005553}
5554
5555/**
5556 * i40e_setup_tc - configure multiple traffic classes
5557 * @netdev: net device to configure
5558 * @tc: number of traffic classes to enable
5559 **/
5560static int i40e_setup_tc(struct net_device *netdev, u8 tc)
5561{
5562 struct i40e_netdev_priv *np = netdev_priv(netdev);
5563 struct i40e_vsi *vsi = np->vsi;
5564 struct i40e_pf *pf = vsi->back;
5565 u8 enabled_tc = 0;
5566 int ret = -EINVAL;
5567 int i;
5568
5569 /* Check if DCB enabled to continue */
5570 if (!(pf->flags & I40E_FLAG_DCB_ENABLED)) {
5571 netdev_info(netdev, "DCB is not enabled for adapter\n");
5572 goto exit;
5573 }
5574
5575 /* Check if MFP enabled */
5576 if (pf->flags & I40E_FLAG_MFP_ENABLED) {
5577 netdev_info(netdev, "Configuring TC not supported in MFP mode\n");
5578 goto exit;
5579 }
5580
5581 /* Check whether tc count is within enabled limit */
5582 if (tc > i40e_pf_get_num_tc(pf)) {
5583 netdev_info(netdev, "TC count greater than enabled on link for adapter\n");
5584 goto exit;
5585 }
5586
5587 /* Generate TC map for number of tc requested */
5588 for (i = 0; i < tc; i++)
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08005589 enabled_tc |= BIT(i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005590
5591 /* Requesting same TC configuration as already enabled */
5592 if (enabled_tc == vsi->tc_config.enabled_tc)
5593 return 0;
5594
5595 /* Quiesce VSI queues */
5596 i40e_quiesce_vsi(vsi);
5597
5598 /* Configure VSI for enabled TCs */
5599 ret = i40e_vsi_config_tc(vsi, enabled_tc);
5600 if (ret) {
5601 netdev_info(netdev, "Failed configuring TC for VSI seid=%d\n",
5602 vsi->seid);
5603 goto exit;
5604 }
5605
5606 /* Unquiesce VSI */
5607 i40e_unquiesce_vsi(vsi);
5608
5609exit:
5610 return ret;
5611}
5612
Jiri Pirko2572ac52017-08-07 10:15:17 +02005613static int __i40e_setup_tc(struct net_device *netdev, enum tc_setup_type type,
Jiri Pirkode4784c2017-08-07 10:15:32 +02005614 void *type_data)
John Fastabende4c67342016-02-16 21:16:15 -08005615{
Jiri Pirkode4784c2017-08-07 10:15:32 +02005616 struct tc_mqprio_qopt *mqprio = type_data;
5617
Jiri Pirko2572ac52017-08-07 10:15:17 +02005618 if (type != TC_SETUP_MQPRIO)
Jiri Pirko38cf0422017-08-07 10:15:31 +02005619 return -EOPNOTSUPP;
Amritha Nambiar56f36ac2017-03-15 10:39:25 -07005620
Jiri Pirkode4784c2017-08-07 10:15:32 +02005621 mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
Amritha Nambiar56f36ac2017-03-15 10:39:25 -07005622
Jiri Pirkode4784c2017-08-07 10:15:32 +02005623 return i40e_setup_tc(netdev, mqprio->num_tc);
John Fastabende4c67342016-02-16 21:16:15 -08005624}
5625
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005626/**
5627 * i40e_open - Called when a network interface is made active
5628 * @netdev: network interface device structure
5629 *
5630 * The open entry point is called when a network interface is made
5631 * active by the system (IFF_UP). At this point all resources needed
5632 * for transmit and receive operations are allocated, the interrupt
5633 * handler is registered with the OS, the netdev watchdog subtask is
5634 * enabled, and the stack is notified that the interface is ready.
5635 *
5636 * Returns 0 on success, negative value on failure
5637 **/
Vasu Dev38e00432014-08-01 13:27:03 -07005638int i40e_open(struct net_device *netdev)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005639{
5640 struct i40e_netdev_priv *np = netdev_priv(netdev);
5641 struct i40e_vsi *vsi = np->vsi;
5642 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005643 int err;
5644
Shannon Nelson4eb3f762014-03-06 08:59:58 +00005645 /* disallow open during test or if eeprom is broken */
Jacob Keller0da36b92017-04-19 09:25:55 -04005646 if (test_bit(__I40E_TESTING, pf->state) ||
5647 test_bit(__I40E_BAD_EEPROM, pf->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005648 return -EBUSY;
5649
5650 netif_carrier_off(netdev);
5651
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005652 err = i40e_vsi_open(vsi);
5653 if (err)
5654 return err;
5655
Jesse Brandeburg059dab62014-04-01 09:07:20 +00005656 /* configure global TSO hardware offload settings */
5657 wr32(&pf->hw, I40E_GLLAN_TSOMSK_F, be32_to_cpu(TCP_FLAG_PSH |
5658 TCP_FLAG_FIN) >> 16);
5659 wr32(&pf->hw, I40E_GLLAN_TSOMSK_M, be32_to_cpu(TCP_FLAG_PSH |
5660 TCP_FLAG_FIN |
5661 TCP_FLAG_CWR) >> 16);
5662 wr32(&pf->hw, I40E_GLLAN_TSOMSK_L, be32_to_cpu(TCP_FLAG_CWR) >> 16);
5663
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07005664 udp_tunnel_get_rx_info(netdev);
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06005665
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005666 return 0;
5667}
5668
5669/**
5670 * i40e_vsi_open -
5671 * @vsi: the VSI to open
5672 *
5673 * Finish initialization of the VSI.
5674 *
5675 * Returns 0 on success, negative value on failure
Maciej Sosin373149f2017-04-05 07:50:55 -04005676 *
5677 * Note: expects to be called while under rtnl_lock()
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005678 **/
5679int i40e_vsi_open(struct i40e_vsi *vsi)
5680{
5681 struct i40e_pf *pf = vsi->back;
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00005682 char int_name[I40E_INT_NAME_STR_LEN];
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005683 int err;
5684
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005685 /* allocate descriptors */
5686 err = i40e_vsi_setup_tx_resources(vsi);
5687 if (err)
5688 goto err_setup_tx;
5689 err = i40e_vsi_setup_rx_resources(vsi);
5690 if (err)
5691 goto err_setup_rx;
5692
5693 err = i40e_vsi_configure(vsi);
5694 if (err)
5695 goto err_setup_rx;
5696
Shannon Nelsonc22e3c62014-03-14 07:32:25 +00005697 if (vsi->netdev) {
5698 snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
5699 dev_driver_string(&pf->pdev->dev), vsi->netdev->name);
5700 err = i40e_vsi_request_irq(vsi, int_name);
5701 if (err)
5702 goto err_setup_rx;
5703
5704 /* Notify the stack of the actual queue counts. */
5705 err = netif_set_real_num_tx_queues(vsi->netdev,
5706 vsi->num_queue_pairs);
5707 if (err)
5708 goto err_set_queues;
5709
5710 err = netif_set_real_num_rx_queues(vsi->netdev,
5711 vsi->num_queue_pairs);
5712 if (err)
5713 goto err_set_queues;
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00005714
5715 } else if (vsi->type == I40E_VSI_FDIR) {
Carolyn Wybornye240f672014-12-11 07:06:37 +00005716 snprintf(int_name, sizeof(int_name) - 1, "%s-%s:fdir",
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +00005717 dev_driver_string(&pf->pdev->dev),
5718 dev_name(&pf->pdev->dev));
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00005719 err = i40e_vsi_request_irq(vsi, int_name);
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +00005720
Shannon Nelsonc22e3c62014-03-14 07:32:25 +00005721 } else {
Jean Sacrence9ccb12014-05-01 14:31:18 +00005722 err = -EINVAL;
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005723 goto err_setup_rx;
5724 }
Anjali Singhai Jain25946dd2013-11-26 10:49:14 +00005725
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005726 err = i40e_up_complete(vsi);
5727 if (err)
5728 goto err_up_complete;
5729
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005730 return 0;
5731
5732err_up_complete:
5733 i40e_down(vsi);
Anjali Singhai Jain25946dd2013-11-26 10:49:14 +00005734err_set_queues:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005735 i40e_vsi_free_irq(vsi);
5736err_setup_rx:
5737 i40e_vsi_free_rx_resources(vsi);
5738err_setup_tx:
5739 i40e_vsi_free_tx_resources(vsi);
5740 if (vsi == pf->vsi[pf->lan_vsi])
Maciej Sosin373149f2017-04-05 07:50:55 -04005741 i40e_do_reset(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED), true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005742
5743 return err;
5744}
5745
5746/**
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00005747 * i40e_fdir_filter_exit - Cleans up the Flow Director accounting
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00005748 * @pf: Pointer to PF
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00005749 *
5750 * This function destroys the hlist where all the Flow Director
5751 * filters were saved.
5752 **/
5753static void i40e_fdir_filter_exit(struct i40e_pf *pf)
5754{
5755 struct i40e_fdir_filter *filter;
Jacob Keller0e588de2017-02-06 14:38:50 -08005756 struct i40e_flex_pit *pit_entry, *tmp;
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00005757 struct hlist_node *node2;
5758
5759 hlist_for_each_entry_safe(filter, node2,
5760 &pf->fdir_filter_list, fdir_node) {
5761 hlist_del(&filter->fdir_node);
5762 kfree(filter);
5763 }
Jacob Keller097dbf52017-02-06 14:38:46 -08005764
Jacob Keller0e588de2017-02-06 14:38:50 -08005765 list_for_each_entry_safe(pit_entry, tmp, &pf->l3_flex_pit_list, list) {
5766 list_del(&pit_entry->list);
5767 kfree(pit_entry);
5768 }
5769 INIT_LIST_HEAD(&pf->l3_flex_pit_list);
5770
5771 list_for_each_entry_safe(pit_entry, tmp, &pf->l4_flex_pit_list, list) {
5772 list_del(&pit_entry->list);
5773 kfree(pit_entry);
5774 }
5775 INIT_LIST_HEAD(&pf->l4_flex_pit_list);
5776
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00005777 pf->fdir_pf_active_filters = 0;
Jacob Keller097dbf52017-02-06 14:38:46 -08005778 pf->fd_tcp4_filter_cnt = 0;
5779 pf->fd_udp4_filter_cnt = 0;
Jacob Kellerf223c872017-02-06 14:38:51 -08005780 pf->fd_sctp4_filter_cnt = 0;
Jacob Keller097dbf52017-02-06 14:38:46 -08005781 pf->fd_ip4_filter_cnt = 0;
Jacob Keller3bcee1e2017-02-06 14:38:46 -08005782
5783 /* Reprogram the default input set for TCP/IPv4 */
5784 i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_TCP,
5785 I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
5786 I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
5787
5788 /* Reprogram the default input set for UDP/IPv4 */
5789 i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_UDP,
5790 I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
5791 I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
5792
5793 /* Reprogram the default input set for SCTP/IPv4 */
5794 i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_SCTP,
5795 I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
5796 I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
5797
5798 /* Reprogram the default input set for Other/IPv4 */
5799 i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_OTHER,
5800 I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00005801}
5802
5803/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005804 * i40e_close - Disables a network interface
5805 * @netdev: network interface device structure
5806 *
5807 * The close entry point is called when an interface is de-activated
5808 * by the OS. The hardware is still under the driver's control, but
5809 * this netdev interface is disabled.
5810 *
5811 * Returns 0, this is not allowed to fail
5812 **/
Vasu Dev38e00432014-08-01 13:27:03 -07005813int i40e_close(struct net_device *netdev)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005814{
5815 struct i40e_netdev_priv *np = netdev_priv(netdev);
5816 struct i40e_vsi *vsi = np->vsi;
5817
Shannon Nelson90ef8d42014-03-14 07:32:26 +00005818 i40e_vsi_close(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005819
5820 return 0;
5821}
5822
5823/**
5824 * i40e_do_reset - Start a PF or Core Reset sequence
5825 * @pf: board private structure
5826 * @reset_flags: which reset is requested
Maciej Sosin373149f2017-04-05 07:50:55 -04005827 * @lock_acquired: indicates whether or not the lock has been acquired
5828 * before this function was called.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005829 *
5830 * The essential difference in resets is that the PF Reset
5831 * doesn't clear the packet buffers, doesn't reset the PE
5832 * firmware, and doesn't bother the other PFs on the chip.
5833 **/
Maciej Sosin373149f2017-04-05 07:50:55 -04005834void i40e_do_reset(struct i40e_pf *pf, u32 reset_flags, bool lock_acquired)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005835{
5836 u32 val;
5837
5838 WARN_ON(in_interrupt());
5839
Mitch Williams263fc482014-04-23 04:50:11 +00005840
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005841 /* do the biggest reset indicated */
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005842 if (reset_flags & BIT_ULL(__I40E_GLOBAL_RESET_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005843
5844 /* Request a Global Reset
5845 *
5846 * This will start the chip's countdown to the actual full
5847 * chip reset event, and a warning interrupt to be sent
5848 * to all PFs, including the requestor. Our handler
5849 * for the warning interrupt will deal with the shutdown
5850 * and recovery of the switch setup.
5851 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005852 dev_dbg(&pf->pdev->dev, "GlobalR requested\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005853 val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
5854 val |= I40E_GLGEN_RTRIG_GLOBR_MASK;
5855 wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
5856
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005857 } else if (reset_flags & BIT_ULL(__I40E_CORE_RESET_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005858
5859 /* Request a Core Reset
5860 *
5861 * Same as Global Reset, except does *not* include the MAC/PHY
5862 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005863 dev_dbg(&pf->pdev->dev, "CoreR requested\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005864 val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
5865 val |= I40E_GLGEN_RTRIG_CORER_MASK;
5866 wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
5867 i40e_flush(&pf->hw);
5868
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005869 } else if (reset_flags & BIT_ULL(__I40E_PF_RESET_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005870
5871 /* Request a PF Reset
5872 *
5873 * Resets only the PF-specific registers
5874 *
5875 * This goes directly to the tear-down and rebuild of
5876 * the switch, since we need to do all the recovery as
5877 * for the Core Reset.
5878 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005879 dev_dbg(&pf->pdev->dev, "PFR requested\n");
Maciej Sosin373149f2017-04-05 07:50:55 -04005880 i40e_handle_reset_warning(pf, lock_acquired);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005881
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005882 } else if (reset_flags & BIT_ULL(__I40E_REINIT_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005883 int v;
5884
5885 /* Find the VSI(s) that requested a re-init */
5886 dev_info(&pf->pdev->dev,
5887 "VSI reinit requested\n");
Mitch Williams505682c2014-05-20 08:01:37 +00005888 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005889 struct i40e_vsi *vsi = pf->vsi[v];
Jesse Brandeburg6995b362015-08-28 17:55:54 -04005890
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005891 if (vsi != NULL &&
Jacob Kellerd19cb642017-04-21 13:38:05 -07005892 test_and_clear_bit(__I40E_VSI_REINIT_REQUESTED,
Jacob Keller0da36b92017-04-19 09:25:55 -04005893 vsi->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005894 i40e_vsi_reinit_locked(pf->vsi[v]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005895 }
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005896 } else if (reset_flags & BIT_ULL(__I40E_DOWN_REQUESTED)) {
Neerav Parikhb5d06f02014-06-03 23:50:17 +00005897 int v;
5898
5899 /* Find the VSI(s) that needs to be brought down */
5900 dev_info(&pf->pdev->dev, "VSI down requested\n");
5901 for (v = 0; v < pf->num_alloc_vsi; v++) {
5902 struct i40e_vsi *vsi = pf->vsi[v];
Jesse Brandeburg6995b362015-08-28 17:55:54 -04005903
Neerav Parikhb5d06f02014-06-03 23:50:17 +00005904 if (vsi != NULL &&
Jacob Kellerd19cb642017-04-21 13:38:05 -07005905 test_and_clear_bit(__I40E_VSI_DOWN_REQUESTED,
Jacob Keller0da36b92017-04-19 09:25:55 -04005906 vsi->state)) {
5907 set_bit(__I40E_VSI_DOWN, vsi->state);
Neerav Parikhb5d06f02014-06-03 23:50:17 +00005908 i40e_down(vsi);
Neerav Parikhb5d06f02014-06-03 23:50:17 +00005909 }
5910 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005911 } else {
5912 dev_info(&pf->pdev->dev,
5913 "bad reset request 0x%08x\n", reset_flags);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005914 }
5915}
5916
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005917#ifdef CONFIG_I40E_DCB
5918/**
5919 * i40e_dcb_need_reconfig - Check if DCB needs reconfig
5920 * @pf: board private structure
5921 * @old_cfg: current DCB config
5922 * @new_cfg: new DCB config
5923 **/
5924bool i40e_dcb_need_reconfig(struct i40e_pf *pf,
5925 struct i40e_dcbx_config *old_cfg,
5926 struct i40e_dcbx_config *new_cfg)
5927{
5928 bool need_reconfig = false;
5929
5930 /* Check if ETS configuration has changed */
5931 if (memcmp(&new_cfg->etscfg,
5932 &old_cfg->etscfg,
5933 sizeof(new_cfg->etscfg))) {
5934 /* If Priority Table has changed reconfig is needed */
5935 if (memcmp(&new_cfg->etscfg.prioritytable,
5936 &old_cfg->etscfg.prioritytable,
5937 sizeof(new_cfg->etscfg.prioritytable))) {
5938 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005939 dev_dbg(&pf->pdev->dev, "ETS UP2TC changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005940 }
5941
5942 if (memcmp(&new_cfg->etscfg.tcbwtable,
5943 &old_cfg->etscfg.tcbwtable,
5944 sizeof(new_cfg->etscfg.tcbwtable)))
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005945 dev_dbg(&pf->pdev->dev, "ETS TC BW Table changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005946
5947 if (memcmp(&new_cfg->etscfg.tsatable,
5948 &old_cfg->etscfg.tsatable,
5949 sizeof(new_cfg->etscfg.tsatable)))
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005950 dev_dbg(&pf->pdev->dev, "ETS TSA Table changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005951 }
5952
5953 /* Check if PFC configuration has changed */
5954 if (memcmp(&new_cfg->pfc,
5955 &old_cfg->pfc,
5956 sizeof(new_cfg->pfc))) {
5957 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005958 dev_dbg(&pf->pdev->dev, "PFC config change detected.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005959 }
5960
5961 /* Check if APP Table has changed */
5962 if (memcmp(&new_cfg->app,
5963 &old_cfg->app,
Dave Jones3d9667a2014-01-27 23:11:09 -05005964 sizeof(new_cfg->app))) {
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005965 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005966 dev_dbg(&pf->pdev->dev, "APP Table change detected.\n");
Dave Jones3d9667a2014-01-27 23:11:09 -05005967 }
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005968
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04005969 dev_dbg(&pf->pdev->dev, "dcb need_reconfig=%d\n", need_reconfig);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005970 return need_reconfig;
5971}
5972
5973/**
5974 * i40e_handle_lldp_event - Handle LLDP Change MIB event
5975 * @pf: board private structure
5976 * @e: event info posted on ARQ
5977 **/
5978static int i40e_handle_lldp_event(struct i40e_pf *pf,
5979 struct i40e_arq_event_info *e)
5980{
5981 struct i40e_aqc_lldp_get_mib *mib =
5982 (struct i40e_aqc_lldp_get_mib *)&e->desc.params.raw;
5983 struct i40e_hw *hw = &pf->hw;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005984 struct i40e_dcbx_config tmp_dcbx_cfg;
5985 bool need_reconfig = false;
5986 int ret = 0;
5987 u8 type;
5988
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005989 /* Not DCB capable or capability disabled */
David Ertmanea6acb72016-09-20 07:10:50 -07005990 if (!(pf->flags & I40E_FLAG_DCB_CAPABLE))
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005991 return ret;
5992
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005993 /* Ignore if event is not for Nearest Bridge */
5994 type = ((mib->type >> I40E_AQ_LLDP_BRIDGE_TYPE_SHIFT)
5995 & I40E_AQ_LLDP_BRIDGE_TYPE_MASK);
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04005996 dev_dbg(&pf->pdev->dev, "LLDP event mib bridge type 0x%x\n", type);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005997 if (type != I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE)
5998 return ret;
5999
6000 /* Check MIB Type and return if event for Remote MIB update */
6001 type = mib->type & I40E_AQ_LLDP_MIB_TYPE_MASK;
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00006002 dev_dbg(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04006003 "LLDP event mib type %s\n", type ? "remote" : "local");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006004 if (type == I40E_AQ_LLDP_MIB_REMOTE) {
6005 /* Update the remote cached instance and return */
6006 ret = i40e_aq_get_dcb_config(hw, I40E_AQ_LLDP_MIB_REMOTE,
6007 I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE,
6008 &hw->remote_dcbx_config);
6009 goto exit;
6010 }
6011
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00006012 /* Store the old configuration */
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07006013 tmp_dcbx_cfg = hw->local_dcbx_config;
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00006014
Neerav Parikh750fcbc2015-02-24 06:58:47 +00006015 /* Reset the old DCBx configuration data */
6016 memset(&hw->local_dcbx_config, 0, sizeof(hw->local_dcbx_config));
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00006017 /* Get updated DCBX data from firmware */
6018 ret = i40e_get_dcb_config(&pf->hw);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006019 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006020 dev_info(&pf->pdev->dev,
6021 "Failed querying DCB configuration data from firmware, err %s aq_err %s\n",
6022 i40e_stat_str(&pf->hw, ret),
6023 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006024 goto exit;
6025 }
6026
6027 /* No change detected in DCBX configs */
Neerav Parikh750fcbc2015-02-24 06:58:47 +00006028 if (!memcmp(&tmp_dcbx_cfg, &hw->local_dcbx_config,
6029 sizeof(tmp_dcbx_cfg))) {
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006030 dev_dbg(&pf->pdev->dev, "No change detected in DCBX configuration.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006031 goto exit;
6032 }
6033
Neerav Parikh750fcbc2015-02-24 06:58:47 +00006034 need_reconfig = i40e_dcb_need_reconfig(pf, &tmp_dcbx_cfg,
6035 &hw->local_dcbx_config);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006036
Neerav Parikh750fcbc2015-02-24 06:58:47 +00006037 i40e_dcbnl_flush_apps(pf, &tmp_dcbx_cfg, &hw->local_dcbx_config);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006038
6039 if (!need_reconfig)
6040 goto exit;
6041
Neerav Parikh4d9b6042014-05-22 06:31:51 +00006042 /* Enable DCB tagging only when more than one TC */
Neerav Parikh750fcbc2015-02-24 06:58:47 +00006043 if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
Neerav Parikh4d9b6042014-05-22 06:31:51 +00006044 pf->flags |= I40E_FLAG_DCB_ENABLED;
6045 else
6046 pf->flags &= ~I40E_FLAG_DCB_ENABLED;
6047
Jacob Keller0da36b92017-04-19 09:25:55 -04006048 set_bit(__I40E_PORT_SUSPENDED, pf->state);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006049 /* Reconfiguration needed quiesce all VSIs */
6050 i40e_pf_quiesce_all_vsi(pf);
6051
6052 /* Changes in configuration update VEB/VSI */
6053 i40e_dcb_reconfigure(pf);
6054
Neerav Parikh2fd75f32014-11-12 00:18:20 +00006055 ret = i40e_resume_port_tx(pf);
6056
Jacob Keller0da36b92017-04-19 09:25:55 -04006057 clear_bit(__I40E_PORT_SUSPENDED, pf->state);
Neerav Parikh2fd75f32014-11-12 00:18:20 +00006058 /* In case of error no point in resuming VSIs */
Neerav Parikh69129dc2014-11-12 00:18:46 +00006059 if (ret)
6060 goto exit;
6061
Neerav Parikh3fe06f42016-02-17 16:12:15 -08006062 /* Wait for the PF's queues to be disabled */
6063 ret = i40e_pf_wait_queues_disabled(pf);
Parikh, Neerav11e47702015-02-21 06:43:55 +00006064 if (ret) {
6065 /* Schedule PF reset to recover */
Jacob Keller0da36b92017-04-19 09:25:55 -04006066 set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
Parikh, Neerav11e47702015-02-21 06:43:55 +00006067 i40e_service_event_schedule(pf);
6068 } else {
Neerav Parikh2fd75f32014-11-12 00:18:20 +00006069 i40e_pf_unquiesce_all_vsi(pf);
Mitch Williams0ef2d5a2017-01-24 10:24:00 -08006070 pf->flags |= (I40E_FLAG_SERVICE_CLIENT_REQUESTED |
6071 I40E_FLAG_CLIENT_L2_CHANGE);
Parikh, Neerav11e47702015-02-21 06:43:55 +00006072 }
6073
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006074exit:
6075 return ret;
6076}
6077#endif /* CONFIG_I40E_DCB */
6078
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006079/**
Anjali Singhai Jain233261862013-11-26 10:49:22 +00006080 * i40e_do_reset_safe - Protected reset path for userland calls.
6081 * @pf: board private structure
6082 * @reset_flags: which reset is requested
6083 *
6084 **/
6085void i40e_do_reset_safe(struct i40e_pf *pf, u32 reset_flags)
6086{
6087 rtnl_lock();
Maciej Sosin373149f2017-04-05 07:50:55 -04006088 i40e_do_reset(pf, reset_flags, true);
Anjali Singhai Jain233261862013-11-26 10:49:22 +00006089 rtnl_unlock();
6090}
6091
6092/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006093 * i40e_handle_lan_overflow_event - Handler for LAN queue overflow event
6094 * @pf: board private structure
6095 * @e: event info posted on ARQ
6096 *
6097 * Handler for LAN Queue Overflow Event generated by the firmware for PF
6098 * and VF queues
6099 **/
6100static void i40e_handle_lan_overflow_event(struct i40e_pf *pf,
6101 struct i40e_arq_event_info *e)
6102{
6103 struct i40e_aqc_lan_overflow *data =
6104 (struct i40e_aqc_lan_overflow *)&e->desc.params.raw;
6105 u32 queue = le32_to_cpu(data->prtdcb_rupto);
6106 u32 qtx_ctl = le32_to_cpu(data->otx_ctl);
6107 struct i40e_hw *hw = &pf->hw;
6108 struct i40e_vf *vf;
6109 u16 vf_id;
6110
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006111 dev_dbg(&pf->pdev->dev, "overflow Rx Queue Number = %d QTX_CTL=0x%08x\n",
6112 queue, qtx_ctl);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006113
6114 /* Queue belongs to VF, find the VF and issue VF reset */
6115 if (((qtx_ctl & I40E_QTX_CTL_PFVF_Q_MASK)
6116 >> I40E_QTX_CTL_PFVF_Q_SHIFT) == I40E_QTX_CTL_VF_QUEUE) {
6117 vf_id = (u16)((qtx_ctl & I40E_QTX_CTL_VFVM_INDX_MASK)
6118 >> I40E_QTX_CTL_VFVM_INDX_SHIFT);
6119 vf_id -= hw->func_caps.vf_base_id;
6120 vf = &pf->vf[vf_id];
6121 i40e_vc_notify_vf_reset(vf);
6122 /* Allow VF to process pending reset notification */
6123 msleep(20);
6124 i40e_reset_vf(vf, false);
6125 }
6126}
6127
6128/**
Anjali Singhai Jain12957382014-06-04 04:22:47 +00006129 * i40e_get_cur_guaranteed_fd_count - Get the consumed guaranteed FD filters
6130 * @pf: board private structure
6131 **/
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006132u32 i40e_get_cur_guaranteed_fd_count(struct i40e_pf *pf)
Anjali Singhai Jain12957382014-06-04 04:22:47 +00006133{
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006134 u32 val, fcnt_prog;
Anjali Singhai Jain12957382014-06-04 04:22:47 +00006135
6136 val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
6137 fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK);
6138 return fcnt_prog;
6139}
6140
6141/**
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006142 * i40e_get_current_fd_count - Get total FD filters programmed for this PF
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006143 * @pf: board private structure
6144 **/
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006145u32 i40e_get_current_fd_count(struct i40e_pf *pf)
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006146{
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006147 u32 val, fcnt_prog;
6148
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006149 val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
6150 fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK) +
6151 ((val & I40E_PFQF_FDSTAT_BEST_CNT_MASK) >>
6152 I40E_PFQF_FDSTAT_BEST_CNT_SHIFT);
6153 return fcnt_prog;
6154}
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006155
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006156/**
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006157 * i40e_get_global_fd_count - Get total FD filters programmed on device
6158 * @pf: board private structure
6159 **/
6160u32 i40e_get_global_fd_count(struct i40e_pf *pf)
6161{
6162 u32 val, fcnt_prog;
6163
6164 val = rd32(&pf->hw, I40E_GLQF_FDCNT_0);
6165 fcnt_prog = (val & I40E_GLQF_FDCNT_0_GUARANT_CNT_MASK) +
6166 ((val & I40E_GLQF_FDCNT_0_BESTCNT_MASK) >>
6167 I40E_GLQF_FDCNT_0_BESTCNT_SHIFT);
6168 return fcnt_prog;
6169}
6170
6171/**
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006172 * i40e_fdir_check_and_reenable - Function to reenabe FD ATR or SB if disabled
6173 * @pf: board private structure
6174 **/
6175void i40e_fdir_check_and_reenable(struct i40e_pf *pf)
6176{
Carolyn Wyborny3487b6c2015-08-27 11:42:38 -04006177 struct i40e_fdir_filter *filter;
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006178 u32 fcnt_prog, fcnt_avail;
Carolyn Wyborny3487b6c2015-08-27 11:42:38 -04006179 struct hlist_node *node;
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006180
Jacob Keller0da36b92017-04-19 09:25:55 -04006181 if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006182 return;
6183
Jacob Keller47994c12017-04-19 09:25:57 -04006184 /* Check if we have enough room to re-enable FDir SB capability. */
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006185 fcnt_prog = i40e_get_global_fd_count(pf);
Anjali Singhai Jain12957382014-06-04 04:22:47 +00006186 fcnt_avail = pf->fdir_pf_filter_count;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006187 if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM)) ||
6188 (pf->fd_add_err == 0) ||
6189 (i40e_get_current_atr_cnt(pf) < pf->fd_atr_cnt)) {
Jacob Keller47994c12017-04-19 09:25:57 -04006190 if (pf->flags & I40E_FLAG_FD_SB_AUTO_DISABLED) {
6191 pf->flags &= ~I40E_FLAG_FD_SB_AUTO_DISABLED;
6192 if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
6193 (I40E_DEBUG_FD & pf->hw.debug_mask))
Anjali Singhai Jain2e4875e2015-04-16 20:06:06 -04006194 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 +00006195 }
6196 }
Jacob Kellera3417d22016-09-06 18:05:10 -07006197
Jacob Keller47994c12017-04-19 09:25:57 -04006198 /* We should wait for even more space before re-enabling ATR.
6199 * Additionally, we cannot enable ATR as long as we still have TCP SB
6200 * rules active.
Jacob Kellera3417d22016-09-06 18:05:10 -07006201 */
Jacob Keller47994c12017-04-19 09:25:57 -04006202 if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) &&
6203 (pf->fd_tcp4_filter_cnt == 0)) {
6204 if (pf->flags & I40E_FLAG_FD_ATR_AUTO_DISABLED) {
6205 pf->flags &= ~I40E_FLAG_FD_ATR_AUTO_DISABLED;
6206 if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
6207 (I40E_DEBUG_FD & pf->hw.debug_mask))
Jacob Kellera3417d22016-09-06 18:05:10 -07006208 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 +00006209 }
6210 }
Carolyn Wyborny3487b6c2015-08-27 11:42:38 -04006211
6212 /* if hw had a problem adding a filter, delete it */
6213 if (pf->fd_inv > 0) {
6214 hlist_for_each_entry_safe(filter, node,
6215 &pf->fdir_filter_list, fdir_node) {
6216 if (filter->fd_id == pf->fd_inv) {
6217 hlist_del(&filter->fdir_node);
6218 kfree(filter);
6219 pf->fdir_pf_active_filters--;
6220 }
6221 }
6222 }
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006223}
6224
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006225#define I40E_MIN_FD_FLUSH_INTERVAL 10
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006226#define I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE 30
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006227/**
6228 * i40e_fdir_flush_and_replay - Function to flush all FD filters and replay SB
6229 * @pf: board private structure
6230 **/
6231static void i40e_fdir_flush_and_replay(struct i40e_pf *pf)
6232{
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006233 unsigned long min_flush_time;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006234 int flush_wait_retry = 50;
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006235 bool disable_atr = false;
6236 int fd_room;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006237 int reg;
6238
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04006239 if (!time_after(jiffies, pf->fd_flush_timestamp +
6240 (I40E_MIN_FD_FLUSH_INTERVAL * HZ)))
6241 return;
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006242
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04006243 /* If the flush is happening too quick and we have mostly SB rules we
6244 * should not re-enable ATR for some time.
6245 */
6246 min_flush_time = pf->fd_flush_timestamp +
6247 (I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE * HZ);
6248 fd_room = pf->fdir_pf_filter_count - pf->fdir_pf_active_filters;
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006249
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04006250 if (!(time_after(jiffies, min_flush_time)) &&
6251 (fd_room < I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) {
6252 if (I40E_DEBUG_FD & pf->hw.debug_mask)
6253 dev_info(&pf->pdev->dev, "ATR disabled, not enough FD filter space.\n");
6254 disable_atr = true;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006255 }
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04006256
6257 pf->fd_flush_timestamp = jiffies;
Jacob Keller47994c12017-04-19 09:25:57 -04006258 pf->flags |= I40E_FLAG_FD_ATR_AUTO_DISABLED;
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04006259 /* flush all filters */
6260 wr32(&pf->hw, I40E_PFQF_CTL_1,
6261 I40E_PFQF_CTL_1_CLEARFDTABLE_MASK);
6262 i40e_flush(&pf->hw);
6263 pf->fd_flush_cnt++;
6264 pf->fd_add_err = 0;
6265 do {
6266 /* Check FD flush status every 5-6msec */
6267 usleep_range(5000, 6000);
6268 reg = rd32(&pf->hw, I40E_PFQF_CTL_1);
6269 if (!(reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK))
6270 break;
6271 } while (flush_wait_retry--);
6272 if (reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK) {
6273 dev_warn(&pf->pdev->dev, "FD table did not flush, needs more time\n");
6274 } else {
6275 /* replay sideband filters */
6276 i40e_fdir_filter_restore(pf->vsi[pf->lan_vsi]);
Jacob Keller097dbf52017-02-06 14:38:46 -08006277 if (!disable_atr && !pf->fd_tcp4_filter_cnt)
Jacob Keller47994c12017-04-19 09:25:57 -04006278 pf->flags &= ~I40E_FLAG_FD_ATR_AUTO_DISABLED;
Jacob Keller0da36b92017-04-19 09:25:55 -04006279 clear_bit(__I40E_FD_FLUSH_REQUESTED, pf->state);
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04006280 if (I40E_DEBUG_FD & pf->hw.debug_mask)
6281 dev_info(&pf->pdev->dev, "FD Filter table flushed and FD-SB replayed.\n");
6282 }
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006283}
6284
6285/**
6286 * i40e_get_current_atr_count - Get the count of total FD ATR filters programmed
6287 * @pf: board private structure
6288 **/
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006289u32 i40e_get_current_atr_cnt(struct i40e_pf *pf)
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006290{
6291 return i40e_get_current_fd_count(pf) - pf->fdir_pf_active_filters;
6292}
6293
6294/* We can see up to 256 filter programming desc in transit if the filters are
6295 * being applied really fast; before we see the first
6296 * filter miss error on Rx queue 0. Accumulating enough error messages before
6297 * reacting will make sure we don't cause flush too often.
6298 */
6299#define I40E_MAX_FD_PROGRAM_ERROR 256
6300
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006301/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006302 * i40e_fdir_reinit_subtask - Worker thread to reinit FDIR filter table
6303 * @pf: board private structure
6304 **/
6305static void i40e_fdir_reinit_subtask(struct i40e_pf *pf)
6306{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006307
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006308 /* if interface is down do nothing */
Mauro S. M. Rodrigues9e6c9c02017-05-12 23:26:56 -03006309 if (test_bit(__I40E_DOWN, pf->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006310 return;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006311
Jacob Keller0da36b92017-04-19 09:25:55 -04006312 if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006313 i40e_fdir_flush_and_replay(pf);
6314
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006315 i40e_fdir_check_and_reenable(pf);
6316
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006317}
6318
6319/**
6320 * i40e_vsi_link_event - notify VSI of a link event
6321 * @vsi: vsi to be notified
6322 * @link_up: link up or down
6323 **/
6324static void i40e_vsi_link_event(struct i40e_vsi *vsi, bool link_up)
6325{
Jacob Keller0da36b92017-04-19 09:25:55 -04006326 if (!vsi || test_bit(__I40E_VSI_DOWN, vsi->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006327 return;
6328
6329 switch (vsi->type) {
6330 case I40E_VSI_MAIN:
6331 if (!vsi->netdev || !vsi->netdev_registered)
6332 break;
6333
6334 if (link_up) {
6335 netif_carrier_on(vsi->netdev);
6336 netif_tx_wake_all_queues(vsi->netdev);
6337 } else {
6338 netif_carrier_off(vsi->netdev);
6339 netif_tx_stop_all_queues(vsi->netdev);
6340 }
6341 break;
6342
6343 case I40E_VSI_SRIOV:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006344 case I40E_VSI_VMDQ2:
6345 case I40E_VSI_CTRL:
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06006346 case I40E_VSI_IWARP:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006347 case I40E_VSI_MIRROR:
6348 default:
6349 /* there is no notification for other VSIs */
6350 break;
6351 }
6352}
6353
6354/**
6355 * i40e_veb_link_event - notify elements on the veb of a link event
6356 * @veb: veb to be notified
6357 * @link_up: link up or down
6358 **/
6359static void i40e_veb_link_event(struct i40e_veb *veb, bool link_up)
6360{
6361 struct i40e_pf *pf;
6362 int i;
6363
6364 if (!veb || !veb->pf)
6365 return;
6366 pf = veb->pf;
6367
6368 /* depth first... */
6369 for (i = 0; i < I40E_MAX_VEB; i++)
6370 if (pf->veb[i] && (pf->veb[i]->uplink_seid == veb->seid))
6371 i40e_veb_link_event(pf->veb[i], link_up);
6372
6373 /* ... now the local VSIs */
Mitch Williams505682c2014-05-20 08:01:37 +00006374 for (i = 0; i < pf->num_alloc_vsi; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006375 if (pf->vsi[i] && (pf->vsi[i]->uplink_seid == veb->seid))
6376 i40e_vsi_link_event(pf->vsi[i], link_up);
6377}
6378
6379/**
6380 * i40e_link_event - Update netif_carrier status
6381 * @pf: board private structure
6382 **/
6383static void i40e_link_event(struct i40e_pf *pf)
6384{
Mitch Williams320684c2014-10-17 03:14:43 +00006385 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
Catherine Sullivanfef59dd2014-12-11 07:06:33 +00006386 u8 new_link_speed, old_link_speed;
Jesse Brandeburga72a5abc2015-08-26 15:14:19 -04006387 i40e_status status;
6388 bool new_link, old_link;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006389
Catherine Sullivan1f9610e2015-10-21 19:47:09 -04006390 /* save off old link status information */
6391 pf->hw.phy.link_info_old = pf->hw.phy.link_info;
6392
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00006393 /* set this to force the get_link_status call to refresh state */
6394 pf->hw.phy.get_link_info = true;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006395
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00006396 old_link = (pf->hw.phy.link_info_old.link_info & I40E_AQ_LINK_UP);
Jesse Brandeburga72a5abc2015-08-26 15:14:19 -04006397
6398 status = i40e_get_link_status(&pf->hw, &new_link);
Harshitha Ramamurthyae136702016-12-12 15:44:16 -08006399
6400 /* On success, disable temp link polling */
6401 if (status == I40E_SUCCESS) {
6402 if (pf->flags & I40E_FLAG_TEMP_LINK_POLLING)
6403 pf->flags &= ~I40E_FLAG_TEMP_LINK_POLLING;
6404 } else {
6405 /* Enable link polling temporarily until i40e_get_link_status
6406 * returns I40E_SUCCESS
6407 */
6408 pf->flags |= I40E_FLAG_TEMP_LINK_POLLING;
Jesse Brandeburga72a5abc2015-08-26 15:14:19 -04006409 dev_dbg(&pf->pdev->dev, "couldn't get link state, status: %d\n",
6410 status);
6411 return;
6412 }
6413
Catherine Sullivanfef59dd2014-12-11 07:06:33 +00006414 old_link_speed = pf->hw.phy.link_info_old.link_speed;
6415 new_link_speed = pf->hw.phy.link_info.link_speed;
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00006416
6417 if (new_link == old_link &&
Catherine Sullivanfef59dd2014-12-11 07:06:33 +00006418 new_link_speed == old_link_speed &&
Jacob Keller0da36b92017-04-19 09:25:55 -04006419 (test_bit(__I40E_VSI_DOWN, vsi->state) ||
Mitch Williams320684c2014-10-17 03:14:43 +00006420 new_link == netif_carrier_ok(vsi->netdev)))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006421 return;
Mitch Williams320684c2014-10-17 03:14:43 +00006422
Jacob Keller0da36b92017-04-19 09:25:55 -04006423 if (!test_bit(__I40E_VSI_DOWN, vsi->state))
Mitch Williams320684c2014-10-17 03:14:43 +00006424 i40e_print_link_message(vsi, new_link);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006425
6426 /* Notify the base of the switch tree connected to
6427 * the link. Floating VEBs are not notified.
6428 */
6429 if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
6430 i40e_veb_link_event(pf->veb[pf->lan_veb], new_link);
6431 else
Mitch Williams320684c2014-10-17 03:14:43 +00006432 i40e_vsi_link_event(vsi, new_link);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006433
6434 if (pf->vf)
6435 i40e_vc_notify_link_state(pf);
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00006436
6437 if (pf->flags & I40E_FLAG_PTP)
6438 i40e_ptp_set_increment(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006439}
6440
6441/**
Shannon Nelson21536712014-10-25 10:35:25 +00006442 * i40e_watchdog_subtask - periodic checks not using event driven response
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006443 * @pf: board private structure
6444 **/
6445static void i40e_watchdog_subtask(struct i40e_pf *pf)
6446{
6447 int i;
6448
6449 /* if interface is down do nothing */
Mauro S. M. Rodrigues9e6c9c02017-05-12 23:26:56 -03006450 if (test_bit(__I40E_DOWN, pf->state) ||
Jacob Keller0da36b92017-04-19 09:25:55 -04006451 test_bit(__I40E_CONFIG_BUSY, pf->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006452 return;
6453
Shannon Nelson21536712014-10-25 10:35:25 +00006454 /* make sure we don't do these things too often */
6455 if (time_before(jiffies, (pf->service_timer_previous +
6456 pf->service_timer_period)))
6457 return;
6458 pf->service_timer_previous = jiffies;
6459
Harshitha Ramamurthyae136702016-12-12 15:44:16 -08006460 if ((pf->flags & I40E_FLAG_LINK_POLLING_ENABLED) ||
6461 (pf->flags & I40E_FLAG_TEMP_LINK_POLLING))
Shannon Nelson9ac77262015-08-27 11:42:40 -04006462 i40e_link_event(pf);
Shannon Nelson21536712014-10-25 10:35:25 +00006463
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006464 /* Update the stats for active netdevs so the network stack
6465 * can look at updated numbers whenever it cares to
6466 */
Mitch Williams505682c2014-05-20 08:01:37 +00006467 for (i = 0; i < pf->num_alloc_vsi; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006468 if (pf->vsi[i] && pf->vsi[i]->netdev)
6469 i40e_update_stats(pf->vsi[i]);
6470
Anjali Singhai Jaind1a8d272015-07-23 16:54:40 -04006471 if (pf->flags & I40E_FLAG_VEB_STATS_ENABLED) {
6472 /* Update the stats for the active switching components */
6473 for (i = 0; i < I40E_MAX_VEB; i++)
6474 if (pf->veb[i])
6475 i40e_update_veb_stats(pf->veb[i]);
6476 }
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00006477
Jacob Keller61189552017-05-03 10:29:01 -07006478 i40e_ptp_rx_hang(pf);
Jacob Keller0bc07062017-05-03 10:29:02 -07006479 i40e_ptp_tx_hang(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006480}
6481
6482/**
6483 * i40e_reset_subtask - Set up for resetting the device and driver
6484 * @pf: board private structure
6485 **/
6486static void i40e_reset_subtask(struct i40e_pf *pf)
6487{
6488 u32 reset_flags = 0;
6489
Jacob Keller0da36b92017-04-19 09:25:55 -04006490 if (test_bit(__I40E_REINIT_REQUESTED, pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006491 reset_flags |= BIT(__I40E_REINIT_REQUESTED);
Jacob Keller0da36b92017-04-19 09:25:55 -04006492 clear_bit(__I40E_REINIT_REQUESTED, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006493 }
Jacob Keller0da36b92017-04-19 09:25:55 -04006494 if (test_bit(__I40E_PF_RESET_REQUESTED, pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006495 reset_flags |= BIT(__I40E_PF_RESET_REQUESTED);
Jacob Keller0da36b92017-04-19 09:25:55 -04006496 clear_bit(__I40E_PF_RESET_REQUESTED, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006497 }
Jacob Keller0da36b92017-04-19 09:25:55 -04006498 if (test_bit(__I40E_CORE_RESET_REQUESTED, pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006499 reset_flags |= BIT(__I40E_CORE_RESET_REQUESTED);
Jacob Keller0da36b92017-04-19 09:25:55 -04006500 clear_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006501 }
Jacob Keller0da36b92017-04-19 09:25:55 -04006502 if (test_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006503 reset_flags |= BIT(__I40E_GLOBAL_RESET_REQUESTED);
Jacob Keller0da36b92017-04-19 09:25:55 -04006504 clear_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006505 }
Mauro S. M. Rodrigues9e6c9c02017-05-12 23:26:56 -03006506 if (test_bit(__I40E_DOWN_REQUESTED, pf->state)) {
6507 reset_flags |= BIT(__I40E_DOWN_REQUESTED);
6508 clear_bit(__I40E_DOWN_REQUESTED, pf->state);
Neerav Parikhb5d06f02014-06-03 23:50:17 +00006509 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006510
6511 /* If there's a recovery already waiting, it takes
6512 * precedence before starting a new reset sequence.
6513 */
Jacob Keller0da36b92017-04-19 09:25:55 -04006514 if (test_bit(__I40E_RESET_INTR_RECEIVED, pf->state)) {
Maciej Sosin373149f2017-04-05 07:50:55 -04006515 i40e_prep_for_reset(pf, false);
6516 i40e_reset(pf);
6517 i40e_rebuild(pf, false, false);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006518 }
6519
6520 /* If we're already down or resetting, just bail */
6521 if (reset_flags &&
Mauro S. M. Rodrigues9e6c9c02017-05-12 23:26:56 -03006522 !test_bit(__I40E_DOWN, pf->state) &&
Jacob Keller0da36b92017-04-19 09:25:55 -04006523 !test_bit(__I40E_CONFIG_BUSY, pf->state)) {
Jacob Kellerdfc4ff62017-06-07 05:43:13 -04006524 i40e_do_reset(pf, reset_flags, false);
Maciej Sosin373149f2017-04-05 07:50:55 -04006525 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006526}
6527
6528/**
6529 * i40e_handle_link_event - Handle link event
6530 * @pf: board private structure
6531 * @e: event info posted on ARQ
6532 **/
6533static void i40e_handle_link_event(struct i40e_pf *pf,
6534 struct i40e_arq_event_info *e)
6535{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006536 struct i40e_aqc_get_link_status *status =
6537 (struct i40e_aqc_get_link_status *)&e->desc.params.raw;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006538
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00006539 /* Do a new status request to re-enable LSE reporting
6540 * and load new status information into the hw struct
6541 * This completely ignores any state information
6542 * in the ARQ event info, instead choosing to always
6543 * issue the AQ update link status command.
6544 */
6545 i40e_link_event(pf);
6546
Carolyn Wyborny7b592f62014-07-10 07:58:19 +00006547 /* check for unqualified module, if link is down */
6548 if ((status->link_info & I40E_AQ_MEDIA_AVAILABLE) &&
6549 (!(status->an_info & I40E_AQ_QUALIFIED_MODULE)) &&
6550 (!(status->link_info & I40E_AQ_LINK_UP)))
6551 dev_err(&pf->pdev->dev,
6552 "The driver failed to link because an unqualified module was detected.\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006553}
6554
6555/**
6556 * i40e_clean_adminq_subtask - Clean the AdminQ rings
6557 * @pf: board private structure
6558 **/
6559static void i40e_clean_adminq_subtask(struct i40e_pf *pf)
6560{
6561 struct i40e_arq_event_info event;
6562 struct i40e_hw *hw = &pf->hw;
6563 u16 pending, i = 0;
6564 i40e_status ret;
6565 u16 opcode;
Shannon Nelson86df2422014-05-20 08:01:35 +00006566 u32 oldval;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006567 u32 val;
6568
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00006569 /* Do not run clean AQ when PF reset fails */
Jacob Keller0da36b92017-04-19 09:25:55 -04006570 if (test_bit(__I40E_RESET_FAILED, pf->state))
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00006571 return;
6572
Shannon Nelson86df2422014-05-20 08:01:35 +00006573 /* check for error indications */
6574 val = rd32(&pf->hw, pf->hw.aq.arq.len);
6575 oldval = val;
6576 if (val & I40E_PF_ARQLEN_ARQVFE_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006577 if (hw->debug_mask & I40E_DEBUG_AQ)
6578 dev_info(&pf->pdev->dev, "ARQ VF Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006579 val &= ~I40E_PF_ARQLEN_ARQVFE_MASK;
6580 }
6581 if (val & I40E_PF_ARQLEN_ARQOVFL_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006582 if (hw->debug_mask & I40E_DEBUG_AQ)
6583 dev_info(&pf->pdev->dev, "ARQ Overflow Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006584 val &= ~I40E_PF_ARQLEN_ARQOVFL_MASK;
Mitch Williams1d0a4ad2015-12-23 12:05:48 -08006585 pf->arq_overflows++;
Shannon Nelson86df2422014-05-20 08:01:35 +00006586 }
6587 if (val & I40E_PF_ARQLEN_ARQCRIT_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006588 if (hw->debug_mask & I40E_DEBUG_AQ)
6589 dev_info(&pf->pdev->dev, "ARQ Critical Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006590 val &= ~I40E_PF_ARQLEN_ARQCRIT_MASK;
6591 }
6592 if (oldval != val)
6593 wr32(&pf->hw, pf->hw.aq.arq.len, val);
6594
6595 val = rd32(&pf->hw, pf->hw.aq.asq.len);
6596 oldval = val;
6597 if (val & I40E_PF_ATQLEN_ATQVFE_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006598 if (pf->hw.debug_mask & I40E_DEBUG_AQ)
6599 dev_info(&pf->pdev->dev, "ASQ VF Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006600 val &= ~I40E_PF_ATQLEN_ATQVFE_MASK;
6601 }
6602 if (val & I40E_PF_ATQLEN_ATQOVFL_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006603 if (pf->hw.debug_mask & I40E_DEBUG_AQ)
6604 dev_info(&pf->pdev->dev, "ASQ Overflow Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006605 val &= ~I40E_PF_ATQLEN_ATQOVFL_MASK;
6606 }
6607 if (val & I40E_PF_ATQLEN_ATQCRIT_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006608 if (pf->hw.debug_mask & I40E_DEBUG_AQ)
6609 dev_info(&pf->pdev->dev, "ASQ Critical Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006610 val &= ~I40E_PF_ATQLEN_ATQCRIT_MASK;
6611 }
6612 if (oldval != val)
6613 wr32(&pf->hw, pf->hw.aq.asq.len, val);
6614
Mitch Williams1001dc32014-11-11 20:02:19 +00006615 event.buf_len = I40E_MAX_AQ_BUF_SIZE;
6616 event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006617 if (!event.msg_buf)
6618 return;
6619
6620 do {
6621 ret = i40e_clean_arq_element(hw, &event, &pending);
Mitch Williams56497972014-06-04 08:45:18 +00006622 if (ret == I40E_ERR_ADMIN_QUEUE_NO_WORK)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006623 break;
Mitch Williams56497972014-06-04 08:45:18 +00006624 else if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006625 dev_info(&pf->pdev->dev, "ARQ event error %d\n", ret);
6626 break;
6627 }
6628
6629 opcode = le16_to_cpu(event.desc.opcode);
6630 switch (opcode) {
6631
6632 case i40e_aqc_opc_get_link_status:
6633 i40e_handle_link_event(pf, &event);
6634 break;
6635 case i40e_aqc_opc_send_msg_to_pf:
6636 ret = i40e_vc_process_vf_msg(pf,
6637 le16_to_cpu(event.desc.retval),
6638 le32_to_cpu(event.desc.cookie_high),
6639 le32_to_cpu(event.desc.cookie_low),
6640 event.msg_buf,
Mitch Williams1001dc32014-11-11 20:02:19 +00006641 event.msg_len);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006642 break;
6643 case i40e_aqc_opc_lldp_update_mib:
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006644 dev_dbg(&pf->pdev->dev, "ARQ: Update LLDP MIB event received\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006645#ifdef CONFIG_I40E_DCB
6646 rtnl_lock();
6647 ret = i40e_handle_lldp_event(pf, &event);
6648 rtnl_unlock();
6649#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006650 break;
6651 case i40e_aqc_opc_event_lan_overflow:
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006652 dev_dbg(&pf->pdev->dev, "ARQ LAN queue overflow event received\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006653 i40e_handle_lan_overflow_event(pf, &event);
6654 break;
Shannon Nelson0467bc92013-12-18 13:45:58 +00006655 case i40e_aqc_opc_send_msg_to_peer:
6656 dev_info(&pf->pdev->dev, "ARQ: Msg from other pf\n");
6657 break;
Shannon Nelson91a0f932015-03-19 14:32:01 -07006658 case i40e_aqc_opc_nvm_erase:
6659 case i40e_aqc_opc_nvm_update:
Michal Kosiarz00ada502015-11-19 11:34:20 -08006660 case i40e_aqc_opc_oem_post_update:
Shannon Nelson6e93d0c2016-01-15 14:33:18 -08006661 i40e_debug(&pf->hw, I40E_DEBUG_NVM,
6662 "ARQ NVM operation 0x%04x completed\n",
6663 opcode);
Shannon Nelson91a0f932015-03-19 14:32:01 -07006664 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006665 default:
6666 dev_info(&pf->pdev->dev,
Shannon Nelson56e5ca62016-03-10 14:59:48 -08006667 "ARQ: Unknown event 0x%04x ignored\n",
Shannon Nelson0467bc92013-12-18 13:45:58 +00006668 opcode);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006669 break;
6670 }
Christopher N Bednarz1fca3262017-03-10 12:22:03 -08006671 } while (i++ < pf->adminq_work_limit);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006672
Christopher N Bednarz1fca3262017-03-10 12:22:03 -08006673 if (i < pf->adminq_work_limit)
Jacob Keller0da36b92017-04-19 09:25:55 -04006674 clear_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state);
Christopher N Bednarz1fca3262017-03-10 12:22:03 -08006675
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006676 /* re-enable Admin queue interrupt cause */
6677 val = rd32(hw, I40E_PFINT_ICR0_ENA);
6678 val |= I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
6679 wr32(hw, I40E_PFINT_ICR0_ENA, val);
6680 i40e_flush(hw);
6681
6682 kfree(event.msg_buf);
6683}
6684
6685/**
Shannon Nelson4eb3f762014-03-06 08:59:58 +00006686 * i40e_verify_eeprom - make sure eeprom is good to use
6687 * @pf: board private structure
6688 **/
6689static void i40e_verify_eeprom(struct i40e_pf *pf)
6690{
6691 int err;
6692
6693 err = i40e_diag_eeprom_test(&pf->hw);
6694 if (err) {
6695 /* retry in case of garbage read */
6696 err = i40e_diag_eeprom_test(&pf->hw);
6697 if (err) {
6698 dev_info(&pf->pdev->dev, "eeprom check failed (%d), Tx/Rx traffic disabled\n",
6699 err);
Jacob Keller0da36b92017-04-19 09:25:55 -04006700 set_bit(__I40E_BAD_EEPROM, pf->state);
Shannon Nelson4eb3f762014-03-06 08:59:58 +00006701 }
6702 }
6703
Jacob Keller0da36b92017-04-19 09:25:55 -04006704 if (!err && test_bit(__I40E_BAD_EEPROM, pf->state)) {
Shannon Nelson4eb3f762014-03-06 08:59:58 +00006705 dev_info(&pf->pdev->dev, "eeprom check passed, Tx/Rx traffic enabled\n");
Jacob Keller0da36b92017-04-19 09:25:55 -04006706 clear_bit(__I40E_BAD_EEPROM, pf->state);
Shannon Nelson4eb3f762014-03-06 08:59:58 +00006707 }
6708}
6709
6710/**
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006711 * i40e_enable_pf_switch_lb
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00006712 * @pf: pointer to the PF structure
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006713 *
6714 * enable switch loop back or die - no point in a return value
6715 **/
6716static void i40e_enable_pf_switch_lb(struct i40e_pf *pf)
6717{
6718 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
6719 struct i40e_vsi_context ctxt;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006720 int ret;
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006721
6722 ctxt.seid = pf->main_vsi_seid;
6723 ctxt.pf_num = pf->hw.pf_id;
6724 ctxt.vf_num = 0;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006725 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
6726 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006727 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006728 "couldn't get PF vsi config, err %s aq_err %s\n",
6729 i40e_stat_str(&pf->hw, ret),
6730 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006731 return;
6732 }
6733 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
6734 ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
6735 ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
6736
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006737 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
6738 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006739 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006740 "update vsi switch failed, err %s aq_err %s\n",
6741 i40e_stat_str(&pf->hw, ret),
6742 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006743 }
6744}
6745
6746/**
6747 * i40e_disable_pf_switch_lb
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00006748 * @pf: pointer to the PF structure
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006749 *
6750 * disable switch loop back or die - no point in a return value
6751 **/
6752static void i40e_disable_pf_switch_lb(struct i40e_pf *pf)
6753{
6754 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
6755 struct i40e_vsi_context ctxt;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006756 int ret;
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006757
6758 ctxt.seid = pf->main_vsi_seid;
6759 ctxt.pf_num = pf->hw.pf_id;
6760 ctxt.vf_num = 0;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006761 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
6762 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006763 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006764 "couldn't get PF vsi config, err %s aq_err %s\n",
6765 i40e_stat_str(&pf->hw, ret),
6766 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006767 return;
6768 }
6769 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
6770 ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
6771 ctxt.info.switch_id &= ~cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
6772
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006773 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
6774 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006775 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006776 "update vsi switch failed, err %s aq_err %s\n",
6777 i40e_stat_str(&pf->hw, ret),
6778 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006779 }
6780}
6781
6782/**
Neerav Parikh51616012015-02-06 08:52:14 +00006783 * i40e_config_bridge_mode - Configure the HW bridge mode
6784 * @veb: pointer to the bridge instance
6785 *
6786 * Configure the loop back mode for the LAN VSI that is downlink to the
6787 * specified HW bridge instance. It is expected this function is called
6788 * when a new HW bridge is instantiated.
6789 **/
6790static void i40e_config_bridge_mode(struct i40e_veb *veb)
6791{
6792 struct i40e_pf *pf = veb->pf;
6793
Shannon Nelson6dec1012015-09-28 14:12:30 -04006794 if (pf->hw.debug_mask & I40E_DEBUG_LAN)
6795 dev_info(&pf->pdev->dev, "enabling bridge mode: %s\n",
6796 veb->bridge_mode == BRIDGE_MODE_VEPA ? "VEPA" : "VEB");
Neerav Parikh51616012015-02-06 08:52:14 +00006797 if (veb->bridge_mode & BRIDGE_MODE_VEPA)
6798 i40e_disable_pf_switch_lb(pf);
6799 else
6800 i40e_enable_pf_switch_lb(pf);
6801}
6802
6803/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006804 * i40e_reconstitute_veb - rebuild the VEB and anything connected to it
6805 * @veb: pointer to the VEB instance
6806 *
6807 * This is a recursive function that first builds the attached VSIs then
6808 * recurses in to build the next layer of VEB. We track the connections
6809 * through our own index numbers because the seid's from the HW could
6810 * change across the reset.
6811 **/
6812static int i40e_reconstitute_veb(struct i40e_veb *veb)
6813{
6814 struct i40e_vsi *ctl_vsi = NULL;
6815 struct i40e_pf *pf = veb->pf;
6816 int v, veb_idx;
6817 int ret;
6818
6819 /* build VSI that owns this VEB, temporarily attached to base VEB */
Mitch Williams505682c2014-05-20 08:01:37 +00006820 for (v = 0; v < pf->num_alloc_vsi && !ctl_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006821 if (pf->vsi[v] &&
6822 pf->vsi[v]->veb_idx == veb->idx &&
6823 pf->vsi[v]->flags & I40E_VSI_FLAG_VEB_OWNER) {
6824 ctl_vsi = pf->vsi[v];
6825 break;
6826 }
6827 }
6828 if (!ctl_vsi) {
6829 dev_info(&pf->pdev->dev,
6830 "missing owner VSI for veb_idx %d\n", veb->idx);
6831 ret = -ENOENT;
6832 goto end_reconstitute;
6833 }
6834 if (ctl_vsi != pf->vsi[pf->lan_vsi])
6835 ctl_vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
6836 ret = i40e_add_vsi(ctl_vsi);
6837 if (ret) {
6838 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006839 "rebuild of veb_idx %d owner VSI failed: %d\n",
6840 veb->idx, ret);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006841 goto end_reconstitute;
6842 }
6843 i40e_vsi_reset_stats(ctl_vsi);
6844
6845 /* create the VEB in the switch and move the VSI onto the VEB */
6846 ret = i40e_add_veb(veb, ctl_vsi);
6847 if (ret)
6848 goto end_reconstitute;
6849
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07006850 if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
6851 veb->bridge_mode = BRIDGE_MODE_VEB;
6852 else
6853 veb->bridge_mode = BRIDGE_MODE_VEPA;
Neerav Parikh51616012015-02-06 08:52:14 +00006854 i40e_config_bridge_mode(veb);
Anjali Singhai Jainb64ba082014-11-13 03:06:15 +00006855
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006856 /* create the remaining VSIs attached to this VEB */
Mitch Williams505682c2014-05-20 08:01:37 +00006857 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006858 if (!pf->vsi[v] || pf->vsi[v] == ctl_vsi)
6859 continue;
6860
6861 if (pf->vsi[v]->veb_idx == veb->idx) {
6862 struct i40e_vsi *vsi = pf->vsi[v];
Jesse Brandeburg6995b362015-08-28 17:55:54 -04006863
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006864 vsi->uplink_seid = veb->seid;
6865 ret = i40e_add_vsi(vsi);
6866 if (ret) {
6867 dev_info(&pf->pdev->dev,
6868 "rebuild of vsi_idx %d failed: %d\n",
6869 v, ret);
6870 goto end_reconstitute;
6871 }
6872 i40e_vsi_reset_stats(vsi);
6873 }
6874 }
6875
6876 /* create any VEBs attached to this VEB - RECURSION */
6877 for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
6878 if (pf->veb[veb_idx] && pf->veb[veb_idx]->veb_idx == veb->idx) {
6879 pf->veb[veb_idx]->uplink_seid = veb->seid;
6880 ret = i40e_reconstitute_veb(pf->veb[veb_idx]);
6881 if (ret)
6882 break;
6883 }
6884 }
6885
6886end_reconstitute:
6887 return ret;
6888}
6889
6890/**
6891 * i40e_get_capabilities - get info about the HW
6892 * @pf: the PF struct
6893 **/
6894static int i40e_get_capabilities(struct i40e_pf *pf)
6895{
6896 struct i40e_aqc_list_capabilities_element_resp *cap_buf;
6897 u16 data_size;
6898 int buf_len;
6899 int err;
6900
6901 buf_len = 40 * sizeof(struct i40e_aqc_list_capabilities_element_resp);
6902 do {
6903 cap_buf = kzalloc(buf_len, GFP_KERNEL);
6904 if (!cap_buf)
6905 return -ENOMEM;
6906
6907 /* this loads the data into the hw struct for us */
6908 err = i40e_aq_discover_capabilities(&pf->hw, cap_buf, buf_len,
6909 &data_size,
6910 i40e_aqc_opc_list_func_capabilities,
6911 NULL);
6912 /* data loaded, buffer no longer needed */
6913 kfree(cap_buf);
6914
6915 if (pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOMEM) {
6916 /* retry with a larger buffer */
6917 buf_len = data_size;
6918 } else if (pf->hw.aq.asq_last_status != I40E_AQ_RC_OK) {
6919 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006920 "capability discovery failed, err %s aq_err %s\n",
6921 i40e_stat_str(&pf->hw, err),
6922 i40e_aq_str(&pf->hw,
6923 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006924 return -ENODEV;
6925 }
6926 } while (err);
6927
6928 if (pf->hw.debug_mask & I40E_DEBUG_USER)
6929 dev_info(&pf->pdev->dev,
6930 "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",
6931 pf->hw.pf_id, pf->hw.func_caps.num_vfs,
6932 pf->hw.func_caps.num_msix_vectors,
6933 pf->hw.func_caps.num_msix_vectors_vf,
6934 pf->hw.func_caps.fd_filters_guaranteed,
6935 pf->hw.func_caps.fd_filters_best_effort,
6936 pf->hw.func_caps.num_tx_qp,
6937 pf->hw.func_caps.num_vsis);
6938
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00006939#define DEF_NUM_VSI (1 + (pf->hw.func_caps.fcoe ? 1 : 0) \
6940 + pf->hw.func_caps.num_vfs)
6941 if (pf->hw.revision_id == 0 && (DEF_NUM_VSI > pf->hw.func_caps.num_vsis)) {
6942 dev_info(&pf->pdev->dev,
6943 "got num_vsis %d, setting num_vsis to %d\n",
6944 pf->hw.func_caps.num_vsis, DEF_NUM_VSI);
6945 pf->hw.func_caps.num_vsis = DEF_NUM_VSI;
6946 }
6947
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006948 return 0;
6949}
6950
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006951static int i40e_vsi_clear(struct i40e_vsi *vsi);
6952
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006953/**
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006954 * i40e_fdir_sb_setup - initialize the Flow Director resources for Sideband
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006955 * @pf: board private structure
6956 **/
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006957static void i40e_fdir_sb_setup(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006958{
6959 struct i40e_vsi *vsi;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006960
Jesse Brandeburg407e0632014-06-03 23:50:12 +00006961 /* quick workaround for an NVM issue that leaves a critical register
6962 * uninitialized
6963 */
6964 if (!rd32(&pf->hw, I40E_GLQF_HKEY(0))) {
6965 static const u32 hkey[] = {
6966 0xe640d33f, 0xcdfe98ab, 0x73fa7161, 0x0d7a7d36,
6967 0xeacb7d61, 0xaa4f05b6, 0x9c5c89ed, 0xfc425ddb,
6968 0xa4654832, 0xfc7461d4, 0x8f827619, 0xf5c63c21,
6969 0x95b3a76d};
Alexander Duyck4b816442016-10-11 15:26:53 -07006970 int i;
Jesse Brandeburg407e0632014-06-03 23:50:12 +00006971
6972 for (i = 0; i <= I40E_GLQF_HKEY_MAX_INDEX; i++)
6973 wr32(&pf->hw, I40E_GLQF_HKEY(i), hkey[i]);
6974 }
6975
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006976 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006977 return;
6978
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006979 /* find existing VSI and see if it needs configuring */
Alexander Duyck4b816442016-10-11 15:26:53 -07006980 vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006981
6982 /* create a new VSI if none exists */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006983 if (!vsi) {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006984 vsi = i40e_vsi_setup(pf, I40E_VSI_FDIR,
6985 pf->vsi[pf->lan_vsi]->seid, 0);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006986 if (!vsi) {
6987 dev_info(&pf->pdev->dev, "Couldn't create FDir VSI\n");
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00006988 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
6989 return;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006990 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006991 }
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00006992
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006993 i40e_vsi_setup_irqhandler(vsi, i40e_fdir_clean_ring);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006994}
6995
6996/**
6997 * i40e_fdir_teardown - release the Flow Director resources
6998 * @pf: board private structure
6999 **/
7000static void i40e_fdir_teardown(struct i40e_pf *pf)
7001{
Alexander Duyck4b816442016-10-11 15:26:53 -07007002 struct i40e_vsi *vsi;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007003
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00007004 i40e_fdir_filter_exit(pf);
Alexander Duyck4b816442016-10-11 15:26:53 -07007005 vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
7006 if (vsi)
7007 i40e_vsi_release(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007008}
7009
7010/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007011 * i40e_prep_for_reset - prep for the core to reset
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007012 * @pf: board private structure
Maciej Sosin373149f2017-04-05 07:50:55 -04007013 * @lock_acquired: indicates whether or not the lock has been acquired
7014 * before this function was called.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007015 *
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00007016 * Close up the VFs and other things in prep for PF Reset.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007017 **/
Maciej Sosin373149f2017-04-05 07:50:55 -04007018static void i40e_prep_for_reset(struct i40e_pf *pf, bool lock_acquired)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007019{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007020 struct i40e_hw *hw = &pf->hw;
Shannon Nelson60442de2014-04-23 04:50:13 +00007021 i40e_status ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007022 u32 v;
7023
Jacob Keller0da36b92017-04-19 09:25:55 -04007024 clear_bit(__I40E_RESET_INTR_RECEIVED, pf->state);
7025 if (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
Shannon Nelson23cfbe02014-06-04 01:23:14 +00007026 return;
Mitch Williamsd3ce57342016-03-10 14:59:46 -08007027 if (i40e_check_asq_alive(&pf->hw))
7028 i40e_vc_notify_reset(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007029
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00007030 dev_dbg(&pf->pdev->dev, "Tearing down internal switch for reset\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007031
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007032 /* quiesce the VSIs and their queues that are not already DOWN */
Maciej Sosin373149f2017-04-05 07:50:55 -04007033 /* pf_quiesce_all_vsi modifies netdev structures -rtnl_lock needed */
7034 if (!lock_acquired)
7035 rtnl_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007036 i40e_pf_quiesce_all_vsi(pf);
Maciej Sosin373149f2017-04-05 07:50:55 -04007037 if (!lock_acquired)
7038 rtnl_unlock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007039
Mitch Williams505682c2014-05-20 08:01:37 +00007040 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007041 if (pf->vsi[v])
7042 pf->vsi[v]->seid = 0;
7043 }
7044
7045 i40e_shutdown_adminq(&pf->hw);
7046
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007047 /* call shutdown HMC */
Shannon Nelson60442de2014-04-23 04:50:13 +00007048 if (hw->hmc.hmc_obj) {
7049 ret = i40e_shutdown_lan_hmc(hw);
Shannon Nelson23cfbe02014-06-04 01:23:14 +00007050 if (ret)
Shannon Nelson60442de2014-04-23 04:50:13 +00007051 dev_warn(&pf->pdev->dev,
7052 "shutdown_lan_hmc failed: %d\n", ret);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007053 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007054}
7055
7056/**
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00007057 * i40e_send_version - update firmware with driver version
7058 * @pf: PF struct
7059 */
7060static void i40e_send_version(struct i40e_pf *pf)
7061{
7062 struct i40e_driver_version dv;
7063
7064 dv.major_version = DRV_VERSION_MAJOR;
7065 dv.minor_version = DRV_VERSION_MINOR;
7066 dv.build_version = DRV_VERSION_BUILD;
7067 dv.subbuild_version = 0;
Rickard Strandqvist35a7d802014-07-29 09:26:25 +00007068 strlcpy(dv.driver_string, DRV_VERSION, sizeof(dv.driver_string));
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00007069 i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
7070}
7071
7072/**
Filip Sadowski5bbb2e22017-06-07 05:43:09 -04007073 * i40e_get_oem_version - get OEM specific version information
7074 * @hw: pointer to the hardware structure
7075 **/
7076static void i40e_get_oem_version(struct i40e_hw *hw)
7077{
7078 u16 block_offset = 0xffff;
7079 u16 block_length = 0;
7080 u16 capabilities = 0;
7081 u16 gen_snap = 0;
7082 u16 release = 0;
7083
7084#define I40E_SR_NVM_OEM_VERSION_PTR 0x1B
7085#define I40E_NVM_OEM_LENGTH_OFFSET 0x00
7086#define I40E_NVM_OEM_CAPABILITIES_OFFSET 0x01
7087#define I40E_NVM_OEM_GEN_OFFSET 0x02
7088#define I40E_NVM_OEM_RELEASE_OFFSET 0x03
7089#define I40E_NVM_OEM_CAPABILITIES_MASK 0x000F
7090#define I40E_NVM_OEM_LENGTH 3
7091
7092 /* Check if pointer to OEM version block is valid. */
7093 i40e_read_nvm_word(hw, I40E_SR_NVM_OEM_VERSION_PTR, &block_offset);
7094 if (block_offset == 0xffff)
7095 return;
7096
7097 /* Check if OEM version block has correct length. */
7098 i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_LENGTH_OFFSET,
7099 &block_length);
7100 if (block_length < I40E_NVM_OEM_LENGTH)
7101 return;
7102
7103 /* Check if OEM version format is as expected. */
7104 i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_CAPABILITIES_OFFSET,
7105 &capabilities);
7106 if ((capabilities & I40E_NVM_OEM_CAPABILITIES_MASK) != 0)
7107 return;
7108
7109 i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_GEN_OFFSET,
7110 &gen_snap);
7111 i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_RELEASE_OFFSET,
7112 &release);
7113 hw->nvm.oem_ver = (gen_snap << I40E_OEM_SNAP_SHIFT) | release;
7114 hw->nvm.eetrack = I40E_OEM_EETRACK_ID;
7115}
7116
7117/**
Maciej Sosin373149f2017-04-05 07:50:55 -04007118 * i40e_reset - wait for core reset to finish reset, reset pf if corer not seen
7119 * @pf: board private structure
7120 **/
7121static int i40e_reset(struct i40e_pf *pf)
7122{
7123 struct i40e_hw *hw = &pf->hw;
7124 i40e_status ret;
7125
7126 ret = i40e_pf_reset(hw);
7127 if (ret) {
7128 dev_info(&pf->pdev->dev, "PF reset failed, %d\n", ret);
Jacob Keller0da36b92017-04-19 09:25:55 -04007129 set_bit(__I40E_RESET_FAILED, pf->state);
7130 clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
Maciej Sosin373149f2017-04-05 07:50:55 -04007131 } else {
7132 pf->pfr_count++;
7133 }
7134 return ret;
7135}
7136
7137/**
7138 * i40e_rebuild - rebuild using a saved config
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007139 * @pf: board private structure
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007140 * @reinit: if the Main VSI needs to re-initialized.
Maciej Sosin373149f2017-04-05 07:50:55 -04007141 * @lock_acquired: indicates whether or not the lock has been acquired
7142 * before this function was called.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007143 **/
Maciej Sosin373149f2017-04-05 07:50:55 -04007144static void i40e_rebuild(struct i40e_pf *pf, bool reinit, bool lock_acquired)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007145{
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007146 struct i40e_hw *hw = &pf->hw;
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00007147 u8 set_fc_aq_fail = 0;
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007148 i40e_status ret;
Anjali Singhai Jain4f2f017c2015-10-21 19:47:07 -04007149 u32 val;
Maciej Sosin373149f2017-04-05 07:50:55 -04007150 int v;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007151
Mauro S. M. Rodrigues9e6c9c02017-05-12 23:26:56 -03007152 if (test_bit(__I40E_DOWN, pf->state))
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00007153 goto clear_recovery;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00007154 dev_dbg(&pf->pdev->dev, "Rebuilding internal switch\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007155
7156 /* rebuild the basics for the AdminQ, HMC, and initial HW switch */
7157 ret = i40e_init_adminq(&pf->hw);
7158 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04007159 dev_info(&pf->pdev->dev, "Rebuild AdminQ failed, err %s aq_err %s\n",
7160 i40e_stat_str(&pf->hw, ret),
7161 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00007162 goto clear_recovery;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007163 }
Filip Sadowski5bbb2e22017-06-07 05:43:09 -04007164 i40e_get_oem_version(&pf->hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007165
Shannon Nelson4eb3f762014-03-06 08:59:58 +00007166 /* re-verify the eeprom if we just had an EMP reset */
Jacob Keller0da36b92017-04-19 09:25:55 -04007167 if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state))
Shannon Nelson4eb3f762014-03-06 08:59:58 +00007168 i40e_verify_eeprom(pf);
Shannon Nelson4eb3f762014-03-06 08:59:58 +00007169
Shannon Nelsone78ac4bf2014-05-10 04:49:09 +00007170 i40e_clear_pxe_mode(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007171 ret = i40e_get_capabilities(pf);
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04007172 if (ret)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007173 goto end_core_reset;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007174
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007175 ret = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
Alexander Duyckc76cb6e2017-02-21 15:55:48 -08007176 hw->func_caps.num_rx_qp, 0, 0);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007177 if (ret) {
7178 dev_info(&pf->pdev->dev, "init_lan_hmc failed: %d\n", ret);
7179 goto end_core_reset;
7180 }
7181 ret = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
7182 if (ret) {
7183 dev_info(&pf->pdev->dev, "configure_lan_hmc failed: %d\n", ret);
7184 goto end_core_reset;
7185 }
7186
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08007187#ifdef CONFIG_I40E_DCB
7188 ret = i40e_init_pf_dcb(pf);
7189 if (ret) {
Shannon Nelsonaebfc812014-12-11 07:06:38 +00007190 dev_info(&pf->pdev->dev, "DCB init failed %d, disabled\n", ret);
7191 pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
7192 /* Continue without DCB enabled */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08007193 }
7194#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007195 /* do basic switch setup */
Maciej Sosin373149f2017-04-05 07:50:55 -04007196 if (!lock_acquired)
7197 rtnl_lock();
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007198 ret = i40e_setup_pf_switch(pf, reinit);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007199 if (ret)
Maciej Sosin373149f2017-04-05 07:50:55 -04007200 goto end_unlock;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007201
Shannon Nelson2f0aff42016-01-04 10:33:08 -08007202 /* The driver only wants link up/down and module qualification
7203 * reports from firmware. Note the negative logic.
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +00007204 */
7205 ret = i40e_aq_set_phy_int_mask(&pf->hw,
Shannon Nelson2f0aff42016-01-04 10:33:08 -08007206 ~(I40E_AQ_EVENT_LINK_UPDOWN |
Shannon Nelson867a79e2016-03-18 12:18:15 -07007207 I40E_AQ_EVENT_MEDIA_NA |
Shannon Nelson2f0aff42016-01-04 10:33:08 -08007208 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +00007209 if (ret)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04007210 dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
7211 i40e_stat_str(&pf->hw, ret),
7212 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +00007213
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00007214 /* make sure our flow control settings are restored */
7215 ret = i40e_set_fc(&pf->hw, &set_fc_aq_fail, true);
7216 if (ret)
Neerav Parikh8279e492015-09-03 17:18:50 -04007217 dev_dbg(&pf->pdev->dev, "setting flow control: ret = %s last_status = %s\n",
7218 i40e_stat_str(&pf->hw, ret),
7219 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00007220
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007221 /* Rebuild the VSIs and VEBs that existed before reset.
7222 * They are still in our local switch element arrays, so only
7223 * need to rebuild the switch model in the HW.
7224 *
7225 * If there were VEBs but the reconstitution failed, we'll try
7226 * try to recover minimal use by getting the basic PF VSI working.
7227 */
7228 if (pf->vsi[pf->lan_vsi]->uplink_seid != pf->mac_seid) {
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00007229 dev_dbg(&pf->pdev->dev, "attempting to rebuild switch\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007230 /* find the one VEB connected to the MAC, and find orphans */
7231 for (v = 0; v < I40E_MAX_VEB; v++) {
7232 if (!pf->veb[v])
7233 continue;
7234
7235 if (pf->veb[v]->uplink_seid == pf->mac_seid ||
7236 pf->veb[v]->uplink_seid == 0) {
7237 ret = i40e_reconstitute_veb(pf->veb[v]);
7238
7239 if (!ret)
7240 continue;
7241
7242 /* If Main VEB failed, we're in deep doodoo,
7243 * so give up rebuilding the switch and set up
7244 * for minimal rebuild of PF VSI.
7245 * If orphan failed, we'll report the error
7246 * but try to keep going.
7247 */
7248 if (pf->veb[v]->uplink_seid == pf->mac_seid) {
7249 dev_info(&pf->pdev->dev,
7250 "rebuild of switch failed: %d, will try to set up simple PF connection\n",
7251 ret);
7252 pf->vsi[pf->lan_vsi]->uplink_seid
7253 = pf->mac_seid;
7254 break;
7255 } else if (pf->veb[v]->uplink_seid == 0) {
7256 dev_info(&pf->pdev->dev,
7257 "rebuild of orphan VEB failed: %d\n",
7258 ret);
7259 }
7260 }
7261 }
7262 }
7263
7264 if (pf->vsi[pf->lan_vsi]->uplink_seid == pf->mac_seid) {
Shannon Nelsoncde4cbc2014-06-04 01:23:17 +00007265 dev_dbg(&pf->pdev->dev, "attempting to rebuild PF VSI\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007266 /* no VEB, so rebuild only the Main VSI */
7267 ret = i40e_add_vsi(pf->vsi[pf->lan_vsi]);
7268 if (ret) {
7269 dev_info(&pf->pdev->dev,
7270 "rebuild of Main VSI failed: %d\n", ret);
Maciej Sosin373149f2017-04-05 07:50:55 -04007271 goto end_unlock;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007272 }
7273 }
7274
Anjali Singhai Jain4f2f017c2015-10-21 19:47:07 -04007275 /* Reconfigure hardware for allowing smaller MSS in the case
7276 * of TSO, so that we avoid the MDD being fired and causing
7277 * a reset in the case of small MSS+TSO.
7278 */
7279#define I40E_REG_MSS 0x000E64DC
7280#define I40E_REG_MSS_MIN_MASK 0x3FF0000
7281#define I40E_64BYTE_MSS 0x400000
7282 val = rd32(hw, I40E_REG_MSS);
7283 if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
7284 val &= ~I40E_REG_MSS_MIN_MASK;
7285 val |= I40E_64BYTE_MSS;
7286 wr32(hw, I40E_REG_MSS, val);
7287 }
7288
Jacob Kellerd36e41d2017-06-23 04:24:46 -04007289 if (pf->hw_features & I40E_HW_RESTART_AUTONEG) {
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +00007290 msleep(75);
7291 ret = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
7292 if (ret)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04007293 dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
7294 i40e_stat_str(&pf->hw, ret),
7295 i40e_aq_str(&pf->hw,
7296 pf->hw.aq.asq_last_status));
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00007297 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007298 /* reinit the misc interrupt */
7299 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
7300 ret = i40e_setup_misc_vector(pf);
7301
Anjali Singhai Jaine7358f52015-10-01 14:37:34 -04007302 /* Add a filter to drop all Flow control frames from any VSI from being
7303 * transmitted. By doing so we stop a malicious VF from sending out
7304 * PAUSE or PFC frames and potentially controlling traffic for other
7305 * PF/VF VSIs.
7306 * The FW can still send Flow control frames if enabled.
7307 */
7308 i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
7309 pf->main_vsi_seid);
7310
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007311 /* restart the VSIs that were rebuilt and running before the reset */
7312 i40e_pf_unquiesce_all_vsi(pf);
7313
Jacob Keller024b05f2017-04-13 04:45:46 -04007314 /* Release the RTNL lock before we start resetting VFs */
7315 if (!lock_acquired)
7316 rtnl_unlock();
7317
Jacob Kellere4b433f2017-04-13 04:45:52 -04007318 i40e_reset_all_vfs(pf, true);
Mitch Williams69f64b22014-02-13 03:48:46 -08007319
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007320 /* tell the firmware that we're starting */
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00007321 i40e_send_version(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007322
Jacob Keller024b05f2017-04-13 04:45:46 -04007323 /* We've already released the lock, so don't do it again */
7324 goto end_core_reset;
7325
Maciej Sosin373149f2017-04-05 07:50:55 -04007326end_unlock:
Jacob Keller024b05f2017-04-13 04:45:46 -04007327 if (!lock_acquired)
7328 rtnl_unlock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007329end_core_reset:
Jacob Keller0da36b92017-04-19 09:25:55 -04007330 clear_bit(__I40E_RESET_FAILED, pf->state);
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00007331clear_recovery:
Jacob Keller0da36b92017-04-19 09:25:55 -04007332 clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007333}
7334
7335/**
Maciej Sosin373149f2017-04-05 07:50:55 -04007336 * i40e_reset_and_rebuild - reset and rebuild using a saved config
7337 * @pf: board private structure
7338 * @reinit: if the Main VSI needs to re-initialized.
7339 * @lock_acquired: indicates whether or not the lock has been acquired
7340 * before this function was called.
7341 **/
7342static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit,
7343 bool lock_acquired)
7344{
7345 int ret;
7346 /* Now we wait for GRST to settle out.
7347 * We don't have to delete the VEBs or VSIs from the hw switch
7348 * because the reset will make them disappear.
7349 */
7350 ret = i40e_reset(pf);
7351 if (!ret)
7352 i40e_rebuild(pf, reinit, lock_acquired);
7353}
7354
7355/**
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00007356 * i40e_handle_reset_warning - prep for the PF to reset, reset and rebuild
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007357 * @pf: board private structure
7358 *
7359 * Close up the VFs and other things in prep for a Core Reset,
7360 * then get ready to rebuild the world.
Maciej Sosin373149f2017-04-05 07:50:55 -04007361 * @lock_acquired: indicates whether or not the lock has been acquired
7362 * before this function was called.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007363 **/
Maciej Sosin373149f2017-04-05 07:50:55 -04007364static void i40e_handle_reset_warning(struct i40e_pf *pf, bool lock_acquired)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007365{
Maciej Sosin373149f2017-04-05 07:50:55 -04007366 i40e_prep_for_reset(pf, lock_acquired);
7367 i40e_reset_and_rebuild(pf, false, lock_acquired);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007368}
7369
7370/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007371 * i40e_handle_mdd_event
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00007372 * @pf: pointer to the PF structure
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007373 *
7374 * Called from the MDD irq handler to identify possibly malicious vfs
7375 **/
7376static void i40e_handle_mdd_event(struct i40e_pf *pf)
7377{
7378 struct i40e_hw *hw = &pf->hw;
7379 bool mdd_detected = false;
Neerav Parikhdf430b12014-06-04 01:23:15 +00007380 bool pf_mdd_detected = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007381 struct i40e_vf *vf;
7382 u32 reg;
7383 int i;
7384
Jacob Keller0da36b92017-04-19 09:25:55 -04007385 if (!test_bit(__I40E_MDD_EVENT_PENDING, pf->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007386 return;
7387
7388 /* find what triggered the MDD event */
7389 reg = rd32(hw, I40E_GL_MDET_TX);
7390 if (reg & I40E_GL_MDET_TX_VALID_MASK) {
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00007391 u8 pf_num = (reg & I40E_GL_MDET_TX_PF_NUM_MASK) >>
7392 I40E_GL_MDET_TX_PF_NUM_SHIFT;
Mitch Williams2089ad02014-10-17 03:14:53 +00007393 u16 vf_num = (reg & I40E_GL_MDET_TX_VF_NUM_MASK) >>
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00007394 I40E_GL_MDET_TX_VF_NUM_SHIFT;
Dan Carpenter013f6572014-10-22 20:06:29 -07007395 u8 event = (reg & I40E_GL_MDET_TX_EVENT_MASK) >>
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00007396 I40E_GL_MDET_TX_EVENT_SHIFT;
Mitch Williams2089ad02014-10-17 03:14:53 +00007397 u16 queue = ((reg & I40E_GL_MDET_TX_QUEUE_MASK) >>
7398 I40E_GL_MDET_TX_QUEUE_SHIFT) -
7399 pf->hw.func_caps.base_queue;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007400 if (netif_msg_tx_err(pf))
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00007401 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 +00007402 event, queue, pf_num, vf_num);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007403 wr32(hw, I40E_GL_MDET_TX, 0xffffffff);
7404 mdd_detected = true;
7405 }
7406 reg = rd32(hw, I40E_GL_MDET_RX);
7407 if (reg & I40E_GL_MDET_RX_VALID_MASK) {
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00007408 u8 func = (reg & I40E_GL_MDET_RX_FUNCTION_MASK) >>
7409 I40E_GL_MDET_RX_FUNCTION_SHIFT;
Dan Carpenter013f6572014-10-22 20:06:29 -07007410 u8 event = (reg & I40E_GL_MDET_RX_EVENT_MASK) >>
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00007411 I40E_GL_MDET_RX_EVENT_SHIFT;
Mitch Williams2089ad02014-10-17 03:14:53 +00007412 u16 queue = ((reg & I40E_GL_MDET_RX_QUEUE_MASK) >>
7413 I40E_GL_MDET_RX_QUEUE_SHIFT) -
7414 pf->hw.func_caps.base_queue;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007415 if (netif_msg_rx_err(pf))
7416 dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on RX queue %d of function 0x%02x\n",
7417 event, queue, func);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007418 wr32(hw, I40E_GL_MDET_RX, 0xffffffff);
7419 mdd_detected = true;
7420 }
7421
Neerav Parikhdf430b12014-06-04 01:23:15 +00007422 if (mdd_detected) {
7423 reg = rd32(hw, I40E_PF_MDET_TX);
7424 if (reg & I40E_PF_MDET_TX_VALID_MASK) {
7425 wr32(hw, I40E_PF_MDET_TX, 0xFFFF);
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007426 dev_info(&pf->pdev->dev, "TX driver issue detected, PF reset issued\n");
Neerav Parikhdf430b12014-06-04 01:23:15 +00007427 pf_mdd_detected = true;
7428 }
7429 reg = rd32(hw, I40E_PF_MDET_RX);
7430 if (reg & I40E_PF_MDET_RX_VALID_MASK) {
7431 wr32(hw, I40E_PF_MDET_RX, 0xFFFF);
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007432 dev_info(&pf->pdev->dev, "RX driver issue detected, PF reset issued\n");
Neerav Parikhdf430b12014-06-04 01:23:15 +00007433 pf_mdd_detected = true;
7434 }
7435 /* Queue belongs to the PF, initiate a reset */
7436 if (pf_mdd_detected) {
Jacob Keller0da36b92017-04-19 09:25:55 -04007437 set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
Neerav Parikhdf430b12014-06-04 01:23:15 +00007438 i40e_service_event_schedule(pf);
7439 }
7440 }
7441
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007442 /* see if one of the VFs needs its hand slapped */
7443 for (i = 0; i < pf->num_alloc_vfs && mdd_detected; i++) {
7444 vf = &(pf->vf[i]);
7445 reg = rd32(hw, I40E_VP_MDET_TX(i));
7446 if (reg & I40E_VP_MDET_TX_VALID_MASK) {
7447 wr32(hw, I40E_VP_MDET_TX(i), 0xFFFF);
7448 vf->num_mdd_events++;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007449 dev_info(&pf->pdev->dev, "TX driver issue detected on VF %d\n",
7450 i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007451 }
7452
7453 reg = rd32(hw, I40E_VP_MDET_RX(i));
7454 if (reg & I40E_VP_MDET_RX_VALID_MASK) {
7455 wr32(hw, I40E_VP_MDET_RX(i), 0xFFFF);
7456 vf->num_mdd_events++;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007457 dev_info(&pf->pdev->dev, "RX driver issue detected on VF %d\n",
7458 i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007459 }
7460
7461 if (vf->num_mdd_events > I40E_DEFAULT_NUM_MDD_EVENTS_ALLOWED) {
7462 dev_info(&pf->pdev->dev,
7463 "Too many MDD events on VF %d, disabled\n", i);
7464 dev_info(&pf->pdev->dev,
7465 "Use PF Control I/F to re-enable the VF\n");
Jacob Keller6322e632017-04-13 04:45:54 -04007466 set_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007467 }
7468 }
7469
7470 /* re-enable mdd interrupt cause */
Jacob Keller0da36b92017-04-19 09:25:55 -04007471 clear_bit(__I40E_MDD_EVENT_PENDING, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007472 reg = rd32(hw, I40E_PFINT_ICR0_ENA);
7473 reg |= I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
7474 wr32(hw, I40E_PFINT_ICR0_ENA, reg);
7475 i40e_flush(hw);
7476}
7477
Jacob Kellerd8b2c702017-06-20 15:17:00 -07007478static const char *i40e_tunnel_name(struct i40e_udp_port_config *port)
7479{
7480 switch (port->type) {
7481 case UDP_TUNNEL_TYPE_VXLAN:
7482 return "vxlan";
7483 case UDP_TUNNEL_TYPE_GENEVE:
7484 return "geneve";
7485 default:
7486 return "unknown";
7487 }
7488}
7489
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007490/**
Alexander Duyck1f190d92017-04-19 09:25:51 -04007491 * i40e_sync_udp_filters - Trigger a sync event for existing UDP filters
7492 * @pf: board private structure
7493 **/
7494static void i40e_sync_udp_filters(struct i40e_pf *pf)
7495{
7496 int i;
7497
7498 /* loop through and set pending bit for all active UDP filters */
7499 for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
7500 if (pf->udp_ports[i].port)
7501 pf->pending_udp_bitmap |= BIT_ULL(i);
7502 }
7503
7504 pf->flags |= I40E_FLAG_UDP_FILTER_SYNC;
7505}
7506
7507/**
Singhai, Anjali6a899022015-12-14 12:21:18 -08007508 * i40e_sync_udp_filters_subtask - Sync the VSI filter list with HW
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007509 * @pf: board private structure
7510 **/
Singhai, Anjali6a899022015-12-14 12:21:18 -08007511static void i40e_sync_udp_filters_subtask(struct i40e_pf *pf)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007512{
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007513 struct i40e_hw *hw = &pf->hw;
7514 i40e_status ret;
Jacob Kellerfe0b0cd2017-02-06 14:38:38 -08007515 u16 port;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007516 int i;
7517
Singhai, Anjali6a899022015-12-14 12:21:18 -08007518 if (!(pf->flags & I40E_FLAG_UDP_FILTER_SYNC))
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007519 return;
7520
Singhai, Anjali6a899022015-12-14 12:21:18 -08007521 pf->flags &= ~I40E_FLAG_UDP_FILTER_SYNC;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007522
7523 for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
Singhai, Anjali6a899022015-12-14 12:21:18 -08007524 if (pf->pending_udp_bitmap & BIT_ULL(i)) {
7525 pf->pending_udp_bitmap &= ~BIT_ULL(i);
Jacob Keller27826fd2017-04-19 09:25:50 -04007526 port = pf->udp_ports[i].port;
Shannon Nelsonc22c06c2015-03-31 00:45:04 -07007527 if (port)
Carolyn Wybornyb3f5c7b2016-08-24 11:33:51 -07007528 ret = i40e_aq_add_udp_tunnel(hw, port,
7529 pf->udp_ports[i].type,
7530 NULL, NULL);
Shannon Nelsonc22c06c2015-03-31 00:45:04 -07007531 else
7532 ret = i40e_aq_del_udp_tunnel(hw, i, NULL);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007533
7534 if (ret) {
Jacob Kellerd8b2c702017-06-20 15:17:00 -07007535 dev_info(&pf->pdev->dev,
7536 "%s %s port %d, index %d failed, err %s aq_err %s\n",
7537 i40e_tunnel_name(&pf->udp_ports[i]),
7538 port ? "add" : "delete",
7539 port, i,
7540 i40e_stat_str(&pf->hw, ret),
7541 i40e_aq_str(&pf->hw,
7542 pf->hw.aq.asq_last_status));
Jacob Keller27826fd2017-04-19 09:25:50 -04007543 pf->udp_ports[i].port = 0;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007544 }
7545 }
7546 }
7547}
7548
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007549/**
7550 * i40e_service_task - Run the driver's async subtasks
7551 * @work: pointer to work_struct containing our data
7552 **/
7553static void i40e_service_task(struct work_struct *work)
7554{
7555 struct i40e_pf *pf = container_of(work,
7556 struct i40e_pf,
7557 service_task);
7558 unsigned long start_time = jiffies;
7559
Shannon Nelsone57a2fe2014-06-03 23:50:19 +00007560 /* don't bother with service tasks if a reset is in progress */
Jacob Keller0da36b92017-04-19 09:25:55 -04007561 if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
Shannon Nelsone57a2fe2014-06-03 23:50:19 +00007562 return;
Shannon Nelsone57a2fe2014-06-03 23:50:19 +00007563
Jacob Keller0da36b92017-04-19 09:25:55 -04007564 if (test_and_set_bit(__I40E_SERVICE_SCHED, pf->state))
Mitch Williams91089032016-11-21 13:03:51 -08007565 return;
7566
Kiran Patilb03a8c12015-09-24 18:13:15 -04007567 i40e_detect_recover_hung(pf);
Jesse Brandeburg2818ccd2016-01-13 16:51:38 -08007568 i40e_sync_filters_subtask(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007569 i40e_reset_subtask(pf);
7570 i40e_handle_mdd_event(pf);
7571 i40e_vc_process_vflr_event(pf);
7572 i40e_watchdog_subtask(pf);
7573 i40e_fdir_reinit_subtask(pf);
Mitch Williams0ef2d5a2017-01-24 10:24:00 -08007574 if (pf->flags & I40E_FLAG_CLIENT_RESET) {
7575 /* Client subtask will reopen next time through. */
7576 i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], true);
7577 pf->flags &= ~I40E_FLAG_CLIENT_RESET;
7578 } else {
7579 i40e_client_subtask(pf);
7580 if (pf->flags & I40E_FLAG_CLIENT_L2_CHANGE) {
7581 i40e_notify_client_of_l2_param_changes(
7582 pf->vsi[pf->lan_vsi]);
7583 pf->flags &= ~I40E_FLAG_CLIENT_L2_CHANGE;
7584 }
7585 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007586 i40e_sync_filters_subtask(pf);
Singhai, Anjali6a899022015-12-14 12:21:18 -08007587 i40e_sync_udp_filters_subtask(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007588 i40e_clean_adminq_subtask(pf);
7589
Mitch Williams91089032016-11-21 13:03:51 -08007590 /* flush memory to make sure state is correct before next watchdog */
7591 smp_mb__before_atomic();
Jacob Keller0da36b92017-04-19 09:25:55 -04007592 clear_bit(__I40E_SERVICE_SCHED, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007593
7594 /* If the tasks have taken longer than one timer cycle or there
7595 * is more work to be done, reschedule the service task now
7596 * rather than wait for the timer to tick again.
7597 */
7598 if (time_after(jiffies, (start_time + pf->service_timer_period)) ||
Jacob Keller0da36b92017-04-19 09:25:55 -04007599 test_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state) ||
7600 test_bit(__I40E_MDD_EVENT_PENDING, pf->state) ||
7601 test_bit(__I40E_VFLR_EVENT_PENDING, pf->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007602 i40e_service_event_schedule(pf);
7603}
7604
7605/**
7606 * i40e_service_timer - timer callback
7607 * @data: pointer to PF struct
7608 **/
7609static void i40e_service_timer(unsigned long data)
7610{
7611 struct i40e_pf *pf = (struct i40e_pf *)data;
7612
7613 mod_timer(&pf->service_timer,
7614 round_jiffies(jiffies + pf->service_timer_period));
7615 i40e_service_event_schedule(pf);
7616}
7617
7618/**
7619 * i40e_set_num_rings_in_vsi - Determine number of rings in the VSI
7620 * @vsi: the VSI being configured
7621 **/
7622static int i40e_set_num_rings_in_vsi(struct i40e_vsi *vsi)
7623{
7624 struct i40e_pf *pf = vsi->back;
7625
7626 switch (vsi->type) {
7627 case I40E_VSI_MAIN:
7628 vsi->alloc_queue_pairs = pf->num_lan_qps;
7629 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
7630 I40E_REQ_DESCRIPTOR_MULTIPLE);
7631 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
7632 vsi->num_q_vectors = pf->num_lan_msix;
7633 else
7634 vsi->num_q_vectors = 1;
7635
7636 break;
7637
7638 case I40E_VSI_FDIR:
7639 vsi->alloc_queue_pairs = 1;
7640 vsi->num_desc = ALIGN(I40E_FDIR_RING_COUNT,
7641 I40E_REQ_DESCRIPTOR_MULTIPLE);
Tushar Davea70e4072016-05-16 12:40:53 -07007642 vsi->num_q_vectors = pf->num_fdsb_msix;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007643 break;
7644
7645 case I40E_VSI_VMDQ2:
7646 vsi->alloc_queue_pairs = pf->num_vmdq_qps;
7647 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
7648 I40E_REQ_DESCRIPTOR_MULTIPLE);
7649 vsi->num_q_vectors = pf->num_vmdq_msix;
7650 break;
7651
7652 case I40E_VSI_SRIOV:
7653 vsi->alloc_queue_pairs = pf->num_vf_qps;
7654 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
7655 I40E_REQ_DESCRIPTOR_MULTIPLE);
7656 break;
7657
7658 default:
7659 WARN_ON(1);
7660 return -ENODATA;
7661 }
7662
7663 return 0;
7664}
7665
7666/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007667 * i40e_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the vsi
7668 * @type: VSI pointer
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007669 * @alloc_qvectors: a bool to specify if q_vectors need to be allocated.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007670 *
7671 * On error: returns error code (negative)
7672 * On success: returns 0
7673 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007674static int i40e_vsi_alloc_arrays(struct i40e_vsi *vsi, bool alloc_qvectors)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007675{
Björn Töpel74608d12017-05-24 07:55:35 +02007676 struct i40e_ring **next_rings;
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007677 int size;
7678 int ret = 0;
7679
Björn Töpel74608d12017-05-24 07:55:35 +02007680 /* allocate memory for both Tx, XDP Tx and Rx ring pointers */
7681 size = sizeof(struct i40e_ring *) * vsi->alloc_queue_pairs *
7682 (i40e_enabled_xdp_vsi(vsi) ? 3 : 2);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007683 vsi->tx_rings = kzalloc(size, GFP_KERNEL);
7684 if (!vsi->tx_rings)
7685 return -ENOMEM;
Björn Töpel74608d12017-05-24 07:55:35 +02007686 next_rings = vsi->tx_rings + vsi->alloc_queue_pairs;
7687 if (i40e_enabled_xdp_vsi(vsi)) {
7688 vsi->xdp_rings = next_rings;
7689 next_rings += vsi->alloc_queue_pairs;
7690 }
7691 vsi->rx_rings = next_rings;
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007692
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007693 if (alloc_qvectors) {
7694 /* allocate memory for q_vector pointers */
Julia Lawallf57e4fb2014-07-30 03:11:09 +00007695 size = sizeof(struct i40e_q_vector *) * vsi->num_q_vectors;
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007696 vsi->q_vectors = kzalloc(size, GFP_KERNEL);
7697 if (!vsi->q_vectors) {
7698 ret = -ENOMEM;
7699 goto err_vectors;
7700 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007701 }
7702 return ret;
7703
7704err_vectors:
7705 kfree(vsi->tx_rings);
7706 return ret;
7707}
7708
7709/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007710 * i40e_vsi_mem_alloc - Allocates the next available struct vsi in the PF
7711 * @pf: board private structure
7712 * @type: type of VSI
7713 *
7714 * On error: returns error code (negative)
7715 * On success: returns vsi index in PF (positive)
7716 **/
7717static int i40e_vsi_mem_alloc(struct i40e_pf *pf, enum i40e_vsi_type type)
7718{
7719 int ret = -ENODEV;
7720 struct i40e_vsi *vsi;
7721 int vsi_idx;
7722 int i;
7723
7724 /* Need to protect the allocation of the VSIs at the PF level */
7725 mutex_lock(&pf->switch_mutex);
7726
7727 /* VSI list may be fragmented if VSI creation/destruction has
7728 * been happening. We can afford to do a quick scan to look
7729 * for any free VSIs in the list.
7730 *
7731 * find next empty vsi slot, looping back around if necessary
7732 */
7733 i = pf->next_vsi;
Mitch Williams505682c2014-05-20 08:01:37 +00007734 while (i < pf->num_alloc_vsi && pf->vsi[i])
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007735 i++;
Mitch Williams505682c2014-05-20 08:01:37 +00007736 if (i >= pf->num_alloc_vsi) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007737 i = 0;
7738 while (i < pf->next_vsi && pf->vsi[i])
7739 i++;
7740 }
7741
Mitch Williams505682c2014-05-20 08:01:37 +00007742 if (i < pf->num_alloc_vsi && !pf->vsi[i]) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007743 vsi_idx = i; /* Found one! */
7744 } else {
7745 ret = -ENODEV;
Alexander Duyck493fb302013-09-28 07:01:44 +00007746 goto unlock_pf; /* out of VSI slots! */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007747 }
7748 pf->next_vsi = ++i;
7749
7750 vsi = kzalloc(sizeof(*vsi), GFP_KERNEL);
7751 if (!vsi) {
7752 ret = -ENOMEM;
Alexander Duyck493fb302013-09-28 07:01:44 +00007753 goto unlock_pf;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007754 }
7755 vsi->type = type;
7756 vsi->back = pf;
Jacob Keller0da36b92017-04-19 09:25:55 -04007757 set_bit(__I40E_VSI_DOWN, vsi->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007758 vsi->flags = 0;
7759 vsi->idx = vsi_idx;
Jesse Brandeburgac26fc12015-09-28 14:12:37 -04007760 vsi->int_rate_limit = 0;
Anjali Singhai Jain5db4cb52015-02-24 06:58:49 +00007761 vsi->rss_table_size = (vsi->type == I40E_VSI_MAIN) ?
7762 pf->rss_table_size : 64;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007763 vsi->netdev_registered = false;
7764 vsi->work_limit = I40E_DEFAULT_IRQ_WORK;
Jacob Keller278e7d02016-10-05 09:30:37 -07007765 hash_init(vsi->mac_filter_hash);
Shannon Nelson63741842014-04-23 04:50:16 +00007766 vsi->irqs_ready = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007767
Alexander Duyck9f65e152013-09-28 06:00:58 +00007768 ret = i40e_set_num_rings_in_vsi(vsi);
7769 if (ret)
7770 goto err_rings;
7771
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007772 ret = i40e_vsi_alloc_arrays(vsi, true);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007773 if (ret)
Alexander Duyck9f65e152013-09-28 06:00:58 +00007774 goto err_rings;
Alexander Duyck493fb302013-09-28 07:01:44 +00007775
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007776 /* Setup default MSIX irq handler for VSI */
7777 i40e_vsi_setup_irqhandler(vsi, i40e_msix_clean_rings);
7778
Kiran Patil21659032015-09-30 14:09:03 -04007779 /* Initialize VSI lock */
Jacob Keller278e7d02016-10-05 09:30:37 -07007780 spin_lock_init(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007781 pf->vsi[vsi_idx] = vsi;
7782 ret = vsi_idx;
Alexander Duyck493fb302013-09-28 07:01:44 +00007783 goto unlock_pf;
7784
Alexander Duyck9f65e152013-09-28 06:00:58 +00007785err_rings:
Alexander Duyck493fb302013-09-28 07:01:44 +00007786 pf->next_vsi = i - 1;
7787 kfree(vsi);
7788unlock_pf:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007789 mutex_unlock(&pf->switch_mutex);
7790 return ret;
7791}
7792
7793/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007794 * i40e_vsi_free_arrays - Free queue and vector pointer arrays for the VSI
7795 * @type: VSI pointer
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007796 * @free_qvectors: a bool to specify if q_vectors need to be freed.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007797 *
7798 * On error: returns error code (negative)
7799 * On success: returns 0
7800 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007801static void i40e_vsi_free_arrays(struct i40e_vsi *vsi, bool free_qvectors)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007802{
7803 /* free the ring and vector containers */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007804 if (free_qvectors) {
7805 kfree(vsi->q_vectors);
7806 vsi->q_vectors = NULL;
7807 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007808 kfree(vsi->tx_rings);
7809 vsi->tx_rings = NULL;
7810 vsi->rx_rings = NULL;
Björn Töpel74608d12017-05-24 07:55:35 +02007811 vsi->xdp_rings = NULL;
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007812}
7813
7814/**
Helin Zhang28c58692015-10-26 19:44:27 -04007815 * i40e_clear_rss_config_user - clear the user configured RSS hash keys
7816 * and lookup table
7817 * @vsi: Pointer to VSI structure
7818 */
7819static void i40e_clear_rss_config_user(struct i40e_vsi *vsi)
7820{
7821 if (!vsi)
7822 return;
7823
7824 kfree(vsi->rss_hkey_user);
7825 vsi->rss_hkey_user = NULL;
7826
7827 kfree(vsi->rss_lut_user);
7828 vsi->rss_lut_user = NULL;
7829}
7830
7831/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007832 * i40e_vsi_clear - Deallocate the VSI provided
7833 * @vsi: the VSI being un-configured
7834 **/
7835static int i40e_vsi_clear(struct i40e_vsi *vsi)
7836{
7837 struct i40e_pf *pf;
7838
7839 if (!vsi)
7840 return 0;
7841
7842 if (!vsi->back)
7843 goto free_vsi;
7844 pf = vsi->back;
7845
7846 mutex_lock(&pf->switch_mutex);
7847 if (!pf->vsi[vsi->idx]) {
7848 dev_err(&pf->pdev->dev, "pf->vsi[%d] is NULL, just free vsi[%d](%p,type %d)\n",
7849 vsi->idx, vsi->idx, vsi, vsi->type);
7850 goto unlock_vsi;
7851 }
7852
7853 if (pf->vsi[vsi->idx] != vsi) {
7854 dev_err(&pf->pdev->dev,
7855 "pf->vsi[%d](%p, type %d) != vsi[%d](%p,type %d): no free!\n",
7856 pf->vsi[vsi->idx]->idx,
7857 pf->vsi[vsi->idx],
7858 pf->vsi[vsi->idx]->type,
7859 vsi->idx, vsi, vsi->type);
7860 goto unlock_vsi;
7861 }
7862
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00007863 /* updates the PF for this cleared vsi */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007864 i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
7865 i40e_put_lump(pf->irq_pile, vsi->base_vector, vsi->idx);
7866
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007867 i40e_vsi_free_arrays(vsi, true);
Helin Zhang28c58692015-10-26 19:44:27 -04007868 i40e_clear_rss_config_user(vsi);
Alexander Duyck493fb302013-09-28 07:01:44 +00007869
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007870 pf->vsi[vsi->idx] = NULL;
7871 if (vsi->idx < pf->next_vsi)
7872 pf->next_vsi = vsi->idx;
7873
7874unlock_vsi:
7875 mutex_unlock(&pf->switch_mutex);
7876free_vsi:
7877 kfree(vsi);
7878
7879 return 0;
7880}
7881
7882/**
Alexander Duyck9f65e152013-09-28 06:00:58 +00007883 * i40e_vsi_clear_rings - Deallocates the Rx and Tx rings for the provided VSI
7884 * @vsi: the VSI being cleaned
7885 **/
Shannon Nelsonbe1d5ee2013-11-28 06:39:23 +00007886static void i40e_vsi_clear_rings(struct i40e_vsi *vsi)
Alexander Duyck9f65e152013-09-28 06:00:58 +00007887{
7888 int i;
7889
Greg Rose8e9dca52013-12-18 13:45:53 +00007890 if (vsi->tx_rings && vsi->tx_rings[0]) {
Neerav Parikhd7397642013-11-28 06:39:37 +00007891 for (i = 0; i < vsi->alloc_queue_pairs; i++) {
Mitch Williams00403f02013-09-28 07:13:13 +00007892 kfree_rcu(vsi->tx_rings[i], rcu);
7893 vsi->tx_rings[i] = NULL;
7894 vsi->rx_rings[i] = NULL;
Björn Töpel74608d12017-05-24 07:55:35 +02007895 if (vsi->xdp_rings)
7896 vsi->xdp_rings[i] = NULL;
Mitch Williams00403f02013-09-28 07:13:13 +00007897 }
Shannon Nelsonbe1d5ee2013-11-28 06:39:23 +00007898 }
Alexander Duyck9f65e152013-09-28 06:00:58 +00007899}
7900
7901/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007902 * i40e_alloc_rings - Allocates the Rx and Tx rings for the provided VSI
7903 * @vsi: the VSI being configured
7904 **/
7905static int i40e_alloc_rings(struct i40e_vsi *vsi)
7906{
Björn Töpel74608d12017-05-24 07:55:35 +02007907 int i, qpv = i40e_enabled_xdp_vsi(vsi) ? 3 : 2;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007908 struct i40e_pf *pf = vsi->back;
Björn Töpel74608d12017-05-24 07:55:35 +02007909 struct i40e_ring *ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007910
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007911 /* Set basic values in the rings to be used later during open() */
Neerav Parikhd7397642013-11-28 06:39:37 +00007912 for (i = 0; i < vsi->alloc_queue_pairs; i++) {
Shannon Nelsonac6c5e32013-11-20 10:02:57 +00007913 /* allocate space for both Tx and Rx in one shot */
Björn Töpel74608d12017-05-24 07:55:35 +02007914 ring = kcalloc(qpv, sizeof(struct i40e_ring), GFP_KERNEL);
7915 if (!ring)
Alexander Duyck9f65e152013-09-28 06:00:58 +00007916 goto err_out;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007917
Björn Töpel74608d12017-05-24 07:55:35 +02007918 ring->queue_index = i;
7919 ring->reg_idx = vsi->base_queue + i;
7920 ring->ring_active = false;
7921 ring->vsi = vsi;
7922 ring->netdev = vsi->netdev;
7923 ring->dev = &pf->pdev->dev;
7924 ring->count = vsi->num_desc;
7925 ring->size = 0;
7926 ring->dcb_tc = 0;
Jacob Kellerd36e41d2017-06-23 04:24:46 -04007927 if (vsi->back->hw_features & I40E_HW_WB_ON_ITR_CAPABLE)
Björn Töpel74608d12017-05-24 07:55:35 +02007928 ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
7929 ring->tx_itr_setting = pf->tx_itr_default;
7930 vsi->tx_rings[i] = ring++;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007931
Björn Töpel74608d12017-05-24 07:55:35 +02007932 if (!i40e_enabled_xdp_vsi(vsi))
7933 goto setup_rx;
7934
7935 ring->queue_index = vsi->alloc_queue_pairs + i;
7936 ring->reg_idx = vsi->base_queue + ring->queue_index;
7937 ring->ring_active = false;
7938 ring->vsi = vsi;
7939 ring->netdev = NULL;
7940 ring->dev = &pf->pdev->dev;
7941 ring->count = vsi->num_desc;
7942 ring->size = 0;
7943 ring->dcb_tc = 0;
Jacob Kellerd36e41d2017-06-23 04:24:46 -04007944 if (vsi->back->hw_features & I40E_HW_WB_ON_ITR_CAPABLE)
Björn Töpel74608d12017-05-24 07:55:35 +02007945 ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
7946 set_ring_xdp(ring);
7947 ring->tx_itr_setting = pf->tx_itr_default;
7948 vsi->xdp_rings[i] = ring++;
7949
7950setup_rx:
7951 ring->queue_index = i;
7952 ring->reg_idx = vsi->base_queue + i;
7953 ring->ring_active = false;
7954 ring->vsi = vsi;
7955 ring->netdev = vsi->netdev;
7956 ring->dev = &pf->pdev->dev;
7957 ring->count = vsi->num_desc;
7958 ring->size = 0;
7959 ring->dcb_tc = 0;
7960 ring->rx_itr_setting = pf->rx_itr_default;
7961 vsi->rx_rings[i] = ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007962 }
7963
7964 return 0;
Alexander Duyck9f65e152013-09-28 06:00:58 +00007965
7966err_out:
7967 i40e_vsi_clear_rings(vsi);
7968 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007969}
7970
7971/**
7972 * i40e_reserve_msix_vectors - Reserve MSI-X vectors in the kernel
7973 * @pf: board private structure
7974 * @vectors: the number of MSI-X vectors to request
7975 *
7976 * Returns the number of vectors reserved, or error
7977 **/
7978static int i40e_reserve_msix_vectors(struct i40e_pf *pf, int vectors)
7979{
Alexander Gordeev7b37f372014-02-18 11:11:42 +01007980 vectors = pci_enable_msix_range(pf->pdev, pf->msix_entries,
7981 I40E_MIN_MSIX, vectors);
7982 if (vectors < 0) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007983 dev_info(&pf->pdev->dev,
Alexander Gordeev7b37f372014-02-18 11:11:42 +01007984 "MSI-X vector reservation failed: %d\n", vectors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007985 vectors = 0;
7986 }
7987
7988 return vectors;
7989}
7990
7991/**
7992 * i40e_init_msix - Setup the MSIX capability
7993 * @pf: board private structure
7994 *
7995 * Work with the OS to set up the MSIX vectors needed.
7996 *
Shannon Nelson3b444392015-02-26 16:15:57 +00007997 * Returns the number of vectors reserved or negative on failure
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007998 **/
7999static int i40e_init_msix(struct i40e_pf *pf)
8000{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008001 struct i40e_hw *hw = &pf->hw;
Jacob Kellerc0cf70a2017-01-24 10:24:01 -08008002 int cpus, extra_vectors;
Shannon Nelson1e200e42015-02-27 09:15:24 +00008003 int vectors_left;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008004 int v_budget, i;
Shannon Nelson3b444392015-02-26 16:15:57 +00008005 int v_actual;
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06008006 int iwarp_requested = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008007
8008 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
8009 return -ENODEV;
8010
8011 /* The number of vectors we'll request will be comprised of:
8012 * - Add 1 for "other" cause for Admin Queue events, etc.
8013 * - The number of LAN queue pairs
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008014 * - Queues being used for RSS.
8015 * We don't need as many as max_rss_size vectors.
8016 * use rss_size instead in the calculation since that
8017 * is governed by number of cpus in the system.
8018 * - assumes symmetric Tx/Rx pairing
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008019 * - The number of VMDq pairs
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06008020 * - The CPU count within the NUMA node if iWARP is enabled
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008021 * Once we count this up, try the request.
8022 *
8023 * If we can't get what we want, we'll simplify to nearly nothing
8024 * and try again. If that still fails, we punt.
8025 */
Shannon Nelson1e200e42015-02-27 09:15:24 +00008026 vectors_left = hw->func_caps.num_msix_vectors;
8027 v_budget = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008028
Shannon Nelson1e200e42015-02-27 09:15:24 +00008029 /* reserve one vector for miscellaneous handler */
8030 if (vectors_left) {
8031 v_budget++;
8032 vectors_left--;
8033 }
8034
Jacob Kellerc0cf70a2017-01-24 10:24:01 -08008035 /* reserve some vectors for the main PF traffic queues. Initially we
8036 * only reserve at most 50% of the available vectors, in the case that
8037 * the number of online CPUs is large. This ensures that we can enable
8038 * extra features as well. Once we've enabled the other features, we
8039 * will use any remaining vectors to reach as close as we can to the
8040 * number of online CPUs.
8041 */
8042 cpus = num_online_cpus();
8043 pf->num_lan_msix = min_t(int, cpus, vectors_left / 2);
Shannon Nelson1e200e42015-02-27 09:15:24 +00008044 vectors_left -= pf->num_lan_msix;
Shannon Nelson1e200e42015-02-27 09:15:24 +00008045
8046 /* reserve one vector for sideband flow director */
8047 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
8048 if (vectors_left) {
Tushar Davea70e4072016-05-16 12:40:53 -07008049 pf->num_fdsb_msix = 1;
Shannon Nelson1e200e42015-02-27 09:15:24 +00008050 v_budget++;
8051 vectors_left--;
8052 } else {
Tushar Davea70e4072016-05-16 12:40:53 -07008053 pf->num_fdsb_msix = 0;
Shannon Nelson1e200e42015-02-27 09:15:24 +00008054 }
8055 }
John W Linville83840e42015-01-14 03:06:28 +00008056
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06008057 /* can we reserve enough for iWARP? */
8058 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02008059 iwarp_requested = pf->num_iwarp_msix;
8060
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06008061 if (!vectors_left)
8062 pf->num_iwarp_msix = 0;
8063 else if (vectors_left < pf->num_iwarp_msix)
8064 pf->num_iwarp_msix = 1;
8065 v_budget += pf->num_iwarp_msix;
8066 vectors_left -= pf->num_iwarp_msix;
8067 }
8068
Shannon Nelson1e200e42015-02-27 09:15:24 +00008069 /* any vectors left over go for VMDq support */
8070 if (pf->flags & I40E_FLAG_VMDQ_ENABLED) {
8071 int vmdq_vecs_wanted = pf->num_vmdq_vsis * pf->num_vmdq_qps;
8072 int vmdq_vecs = min_t(int, vectors_left, vmdq_vecs_wanted);
8073
Stefan Assmann9ca57e92016-09-19 13:37:49 +02008074 if (!vectors_left) {
8075 pf->num_vmdq_msix = 0;
8076 pf->num_vmdq_qps = 0;
8077 } else {
8078 /* if we're short on vectors for what's desired, we limit
8079 * the queues per vmdq. If this is still more than are
8080 * available, the user will need to change the number of
8081 * queues/vectors used by the PF later with the ethtool
8082 * channels command
8083 */
8084 if (vmdq_vecs < vmdq_vecs_wanted)
8085 pf->num_vmdq_qps = 1;
8086 pf->num_vmdq_msix = pf->num_vmdq_qps;
Shannon Nelson1e200e42015-02-27 09:15:24 +00008087
Stefan Assmann9ca57e92016-09-19 13:37:49 +02008088 v_budget += vmdq_vecs;
8089 vectors_left -= vmdq_vecs;
8090 }
Shannon Nelson1e200e42015-02-27 09:15:24 +00008091 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008092
Jacob Kellerc0cf70a2017-01-24 10:24:01 -08008093 /* On systems with a large number of SMP cores, we previously limited
8094 * the number of vectors for num_lan_msix to be at most 50% of the
8095 * available vectors, to allow for other features. Now, we add back
8096 * the remaining vectors. However, we ensure that the total
8097 * num_lan_msix will not exceed num_online_cpus(). To do this, we
8098 * calculate the number of vectors we can add without going over the
8099 * cap of CPUs. For systems with a small number of CPUs this will be
8100 * zero.
8101 */
8102 extra_vectors = min_t(int, cpus - pf->num_lan_msix, vectors_left);
8103 pf->num_lan_msix += extra_vectors;
8104 vectors_left -= extra_vectors;
8105
8106 WARN(vectors_left < 0,
8107 "Calculation of remaining vectors underflowed. This is an accounting bug when determining total MSI-X vectors.\n");
8108
8109 v_budget += pf->num_lan_msix;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008110 pf->msix_entries = kcalloc(v_budget, sizeof(struct msix_entry),
8111 GFP_KERNEL);
8112 if (!pf->msix_entries)
8113 return -ENOMEM;
8114
8115 for (i = 0; i < v_budget; i++)
8116 pf->msix_entries[i].entry = i;
Shannon Nelson3b444392015-02-26 16:15:57 +00008117 v_actual = i40e_reserve_msix_vectors(pf, v_budget);
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00008118
Shannon Nelson3b444392015-02-26 16:15:57 +00008119 if (v_actual < I40E_MIN_MSIX) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008120 pf->flags &= ~I40E_FLAG_MSIX_ENABLED;
8121 kfree(pf->msix_entries);
8122 pf->msix_entries = NULL;
Guilherme G Piccoli4c95aa52016-09-22 10:03:58 -03008123 pci_disable_msix(pf->pdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008124 return -ENODEV;
8125
Shannon Nelson3b444392015-02-26 16:15:57 +00008126 } else if (v_actual == I40E_MIN_MSIX) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008127 /* Adjust for minimal MSIX use */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008128 pf->num_vmdq_vsis = 0;
8129 pf->num_vmdq_qps = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008130 pf->num_lan_qps = 1;
8131 pf->num_lan_msix = 1;
8132
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02008133 } else if (!vectors_left) {
8134 /* If we have limited resources, we will start with no vectors
8135 * for the special features and then allocate vectors to some
8136 * of these features based on the policy and at the end disable
8137 * the features that did not get any vectors.
8138 */
Shannon Nelson3b444392015-02-26 16:15:57 +00008139 int vec;
8140
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02008141 dev_info(&pf->pdev->dev,
8142 "MSI-X vector limit reached, attempting to redistribute vectors\n");
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00008143 /* reserve the misc vector */
Shannon Nelson3b444392015-02-26 16:15:57 +00008144 vec = v_actual - 1;
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00008145
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008146 /* Scale vector usage down */
8147 pf->num_vmdq_msix = 1; /* force VMDqs to only one vector */
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00008148 pf->num_vmdq_vsis = 1;
Shannon Nelson1e200e42015-02-27 09:15:24 +00008149 pf->num_vmdq_qps = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008150
8151 /* partition out the remaining vectors */
8152 switch (vec) {
8153 case 2:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008154 pf->num_lan_msix = 1;
8155 break;
8156 case 3:
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06008157 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
8158 pf->num_lan_msix = 1;
8159 pf->num_iwarp_msix = 1;
8160 } else {
8161 pf->num_lan_msix = 2;
8162 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008163 break;
8164 default:
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06008165 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
8166 pf->num_iwarp_msix = min_t(int, (vec / 3),
8167 iwarp_requested);
8168 pf->num_vmdq_vsis = min_t(int, (vec / 3),
8169 I40E_DEFAULT_NUM_VMDQ_VSI);
8170 } else {
8171 pf->num_vmdq_vsis = min_t(int, (vec / 2),
8172 I40E_DEFAULT_NUM_VMDQ_VSI);
8173 }
Stefan Assmannabd97a92016-09-19 13:37:51 +02008174 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
8175 pf->num_fdsb_msix = 1;
8176 vec--;
8177 }
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06008178 pf->num_lan_msix = min_t(int,
8179 (vec - (pf->num_iwarp_msix + pf->num_vmdq_vsis)),
8180 pf->num_lan_msix);
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02008181 pf->num_lan_qps = pf->num_lan_msix;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008182 break;
8183 }
8184 }
8185
Stefan Assmannabd97a92016-09-19 13:37:51 +02008186 if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
8187 (pf->num_fdsb_msix == 0)) {
8188 dev_info(&pf->pdev->dev, "Sideband Flowdir disabled, not enough MSI-X vectors\n");
8189 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
8190 }
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00008191 if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
8192 (pf->num_vmdq_msix == 0)) {
8193 dev_info(&pf->pdev->dev, "VMDq disabled, not enough MSI-X vectors\n");
8194 pf->flags &= ~I40E_FLAG_VMDQ_ENABLED;
8195 }
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06008196
8197 if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
8198 (pf->num_iwarp_msix == 0)) {
8199 dev_info(&pf->pdev->dev, "IWARP disabled, not enough MSI-X vectors\n");
8200 pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
8201 }
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02008202 i40e_debug(&pf->hw, I40E_DEBUG_INIT,
8203 "MSI-X vector distribution: PF %d, VMDq %d, FDSB %d, iWARP %d\n",
8204 pf->num_lan_msix,
8205 pf->num_vmdq_msix * pf->num_vmdq_vsis,
8206 pf->num_fdsb_msix,
8207 pf->num_iwarp_msix);
8208
Shannon Nelson3b444392015-02-26 16:15:57 +00008209 return v_actual;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008210}
8211
8212/**
Greg Rose90e04072014-03-06 08:59:57 +00008213 * i40e_vsi_alloc_q_vector - Allocate memory for a single interrupt vector
Alexander Duyck493fb302013-09-28 07:01:44 +00008214 * @vsi: the VSI being configured
8215 * @v_idx: index of the vector in the vsi struct
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03008216 * @cpu: cpu to be used on affinity_mask
Alexander Duyck493fb302013-09-28 07:01:44 +00008217 *
8218 * We allocate one q_vector. If allocation fails we return -ENOMEM.
8219 **/
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03008220static int i40e_vsi_alloc_q_vector(struct i40e_vsi *vsi, int v_idx, int cpu)
Alexander Duyck493fb302013-09-28 07:01:44 +00008221{
8222 struct i40e_q_vector *q_vector;
8223
8224 /* allocate q_vector */
8225 q_vector = kzalloc(sizeof(struct i40e_q_vector), GFP_KERNEL);
8226 if (!q_vector)
8227 return -ENOMEM;
8228
8229 q_vector->vsi = vsi;
8230 q_vector->v_idx = v_idx;
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03008231 cpumask_set_cpu(cpu, &q_vector->affinity_mask);
8232
Alexander Duyck493fb302013-09-28 07:01:44 +00008233 if (vsi->netdev)
8234 netif_napi_add(vsi->netdev, &q_vector->napi,
Jesse Brandeburgeefeace2014-05-10 04:49:13 +00008235 i40e_napi_poll, NAPI_POLL_WEIGHT);
Alexander Duyck493fb302013-09-28 07:01:44 +00008236
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00008237 q_vector->rx.latency_range = I40E_LOW_LATENCY;
8238 q_vector->tx.latency_range = I40E_LOW_LATENCY;
8239
Alexander Duyck493fb302013-09-28 07:01:44 +00008240 /* tie q_vector and vsi together */
8241 vsi->q_vectors[v_idx] = q_vector;
8242
8243 return 0;
8244}
8245
8246/**
Greg Rose90e04072014-03-06 08:59:57 +00008247 * i40e_vsi_alloc_q_vectors - Allocate memory for interrupt vectors
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008248 * @vsi: the VSI being configured
8249 *
8250 * We allocate one q_vector per queue interrupt. If allocation fails we
8251 * return -ENOMEM.
8252 **/
Greg Rose90e04072014-03-06 08:59:57 +00008253static int i40e_vsi_alloc_q_vectors(struct i40e_vsi *vsi)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008254{
8255 struct i40e_pf *pf = vsi->back;
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03008256 int err, v_idx, num_q_vectors, current_cpu;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008257
8258 /* if not MSIX, give the one vector only to the LAN VSI */
8259 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
8260 num_q_vectors = vsi->num_q_vectors;
8261 else if (vsi == pf->vsi[pf->lan_vsi])
8262 num_q_vectors = 1;
8263 else
8264 return -EINVAL;
8265
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03008266 current_cpu = cpumask_first(cpu_online_mask);
8267
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008268 for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03008269 err = i40e_vsi_alloc_q_vector(vsi, v_idx, current_cpu);
Alexander Duyck493fb302013-09-28 07:01:44 +00008270 if (err)
8271 goto err_out;
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03008272 current_cpu = cpumask_next(current_cpu, cpu_online_mask);
8273 if (unlikely(current_cpu >= nr_cpu_ids))
8274 current_cpu = cpumask_first(cpu_online_mask);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008275 }
8276
8277 return 0;
Alexander Duyck493fb302013-09-28 07:01:44 +00008278
8279err_out:
8280 while (v_idx--)
8281 i40e_free_q_vector(vsi, v_idx);
8282
8283 return err;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008284}
8285
8286/**
8287 * i40e_init_interrupt_scheme - Determine proper interrupt scheme
8288 * @pf: board private structure to initialize
8289 **/
Jesse Brandeburgc11472802015-04-07 19:45:39 -04008290static int i40e_init_interrupt_scheme(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008291{
Shannon Nelson3b444392015-02-26 16:15:57 +00008292 int vectors = 0;
8293 ssize_t size;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008294
8295 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
Shannon Nelson3b444392015-02-26 16:15:57 +00008296 vectors = i40e_init_msix(pf);
8297 if (vectors < 0) {
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08008298 pf->flags &= ~(I40E_FLAG_MSIX_ENABLED |
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06008299 I40E_FLAG_IWARP_ENABLED |
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08008300 I40E_FLAG_RSS_ENABLED |
Neerav Parikh4d9b6042014-05-22 06:31:51 +00008301 I40E_FLAG_DCB_CAPABLE |
Dave Ertmana0362442016-08-29 17:38:26 -07008302 I40E_FLAG_DCB_ENABLED |
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08008303 I40E_FLAG_SRIOV_ENABLED |
8304 I40E_FLAG_FD_SB_ENABLED |
8305 I40E_FLAG_FD_ATR_ENABLED |
8306 I40E_FLAG_VMDQ_ENABLED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008307
8308 /* rework the queue expectations without MSIX */
8309 i40e_determine_queue_usage(pf);
8310 }
8311 }
8312
8313 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED) &&
8314 (pf->flags & I40E_FLAG_MSI_ENABLED)) {
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08008315 dev_info(&pf->pdev->dev, "MSI-X not available, trying MSI\n");
Shannon Nelson3b444392015-02-26 16:15:57 +00008316 vectors = pci_enable_msi(pf->pdev);
8317 if (vectors < 0) {
8318 dev_info(&pf->pdev->dev, "MSI init failed - %d\n",
8319 vectors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008320 pf->flags &= ~I40E_FLAG_MSI_ENABLED;
8321 }
Shannon Nelson3b444392015-02-26 16:15:57 +00008322 vectors = 1; /* one MSI or Legacy vector */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008323 }
8324
Shannon Nelson958a3e32013-09-28 07:13:28 +00008325 if (!(pf->flags & (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED)))
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08008326 dev_info(&pf->pdev->dev, "MSI-X and MSI not available, falling back to Legacy IRQ\n");
Shannon Nelson958a3e32013-09-28 07:13:28 +00008327
Shannon Nelson3b444392015-02-26 16:15:57 +00008328 /* set up vector assignment tracking */
8329 size = sizeof(struct i40e_lump_tracking) + (sizeof(u16) * vectors);
8330 pf->irq_pile = kzalloc(size, GFP_KERNEL);
Jesse Brandeburgc11472802015-04-07 19:45:39 -04008331 if (!pf->irq_pile) {
8332 dev_err(&pf->pdev->dev, "error allocating irq_pile memory\n");
8333 return -ENOMEM;
8334 }
Shannon Nelson3b444392015-02-26 16:15:57 +00008335 pf->irq_pile->num_entries = vectors;
8336 pf->irq_pile->search_hint = 0;
8337
Jesse Brandeburgc11472802015-04-07 19:45:39 -04008338 /* track first vector for misc interrupts, ignore return */
Shannon Nelson3b444392015-02-26 16:15:57 +00008339 (void)i40e_get_lump(pf, pf->irq_pile, 1, I40E_PILE_VALID_BIT - 1);
Jesse Brandeburgc11472802015-04-07 19:45:39 -04008340
8341 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008342}
8343
8344/**
8345 * i40e_setup_misc_vector - Setup the misc vector to handle non queue events
8346 * @pf: board private structure
8347 *
8348 * This sets up the handler for MSIX 0, which is used to manage the
8349 * non-queue interrupts, e.g. AdminQ and errors. This is not used
8350 * when in MSI or Legacy interrupt mode.
8351 **/
8352static int i40e_setup_misc_vector(struct i40e_pf *pf)
8353{
8354 struct i40e_hw *hw = &pf->hw;
8355 int err = 0;
8356
8357 /* Only request the irq if this is the first time through, and
8358 * not when we're rebuilding after a Reset
8359 */
Jacob Keller0da36b92017-04-19 09:25:55 -04008360 if (!test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008361 err = request_irq(pf->msix_entries[0].vector,
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00008362 i40e_intr, 0, pf->int_name, pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008363 if (err) {
8364 dev_info(&pf->pdev->dev,
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08008365 "request_irq for %s failed: %d\n",
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00008366 pf->int_name, err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008367 return -EFAULT;
8368 }
8369 }
8370
Jacob Kellerab437b52014-12-14 01:55:08 +00008371 i40e_enable_misc_int_causes(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008372
8373 /* associate no queues to the misc vector */
8374 wr32(hw, I40E_PFINT_LNKLST0, I40E_QUEUE_END_OF_LIST);
8375 wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), I40E_ITR_8K);
8376
8377 i40e_flush(hw);
8378
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08008379 i40e_irq_dynamic_enable_icr0(pf, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008380
8381 return err;
8382}
8383
8384/**
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008385 * i40e_config_rss_aq - Prepare for RSS using AQ commands
8386 * @vsi: vsi structure
8387 * @seed: RSS hash seed
8388 **/
Helin Zhange69ff812015-10-21 19:56:22 -04008389static int i40e_config_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
8390 u8 *lut, u16 lut_size)
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008391{
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008392 struct i40e_pf *pf = vsi->back;
8393 struct i40e_hw *hw = &pf->hw;
Jacob Keller776b2e12016-07-19 16:23:30 -07008394 int ret = 0;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008395
Jacob Keller776b2e12016-07-19 16:23:30 -07008396 if (seed) {
8397 struct i40e_aqc_get_set_rss_key_data *seed_dw =
8398 (struct i40e_aqc_get_set_rss_key_data *)seed;
8399 ret = i40e_aq_set_rss_key(hw, vsi->id, seed_dw);
8400 if (ret) {
8401 dev_info(&pf->pdev->dev,
8402 "Cannot set RSS key, err %s aq_err %s\n",
8403 i40e_stat_str(hw, ret),
8404 i40e_aq_str(hw, hw->aq.asq_last_status));
8405 return ret;
8406 }
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008407 }
Jacob Keller776b2e12016-07-19 16:23:30 -07008408 if (lut) {
8409 bool pf_lut = vsi->type == I40E_VSI_MAIN ? true : false;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008410
Jacob Keller776b2e12016-07-19 16:23:30 -07008411 ret = i40e_aq_set_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
8412 if (ret) {
8413 dev_info(&pf->pdev->dev,
8414 "Cannot set RSS lut, err %s aq_err %s\n",
8415 i40e_stat_str(hw, ret),
8416 i40e_aq_str(hw, hw->aq.asq_last_status));
8417 return ret;
8418 }
8419 }
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008420 return ret;
8421}
8422
8423/**
Anjali Singhai Jain95a73782015-12-22 14:25:04 -08008424 * i40e_get_rss_aq - Get RSS keys and lut by using AQ commands
8425 * @vsi: Pointer to vsi structure
8426 * @seed: Buffter to store the hash keys
8427 * @lut: Buffer to store the lookup table entries
8428 * @lut_size: Size of buffer to store the lookup table entries
8429 *
8430 * Return 0 on success, negative on failure
8431 */
8432static int i40e_get_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
8433 u8 *lut, u16 lut_size)
8434{
8435 struct i40e_pf *pf = vsi->back;
8436 struct i40e_hw *hw = &pf->hw;
8437 int ret = 0;
8438
8439 if (seed) {
8440 ret = i40e_aq_get_rss_key(hw, vsi->id,
8441 (struct i40e_aqc_get_set_rss_key_data *)seed);
8442 if (ret) {
8443 dev_info(&pf->pdev->dev,
8444 "Cannot get RSS key, err %s aq_err %s\n",
8445 i40e_stat_str(&pf->hw, ret),
8446 i40e_aq_str(&pf->hw,
8447 pf->hw.aq.asq_last_status));
8448 return ret;
8449 }
8450 }
8451
8452 if (lut) {
8453 bool pf_lut = vsi->type == I40E_VSI_MAIN ? true : false;
8454
8455 ret = i40e_aq_get_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
8456 if (ret) {
8457 dev_info(&pf->pdev->dev,
8458 "Cannot get RSS lut, err %s aq_err %s\n",
8459 i40e_stat_str(&pf->hw, ret),
8460 i40e_aq_str(&pf->hw,
8461 pf->hw.aq.asq_last_status));
8462 return ret;
8463 }
8464 }
8465
8466 return ret;
8467}
8468
8469/**
Jacob Keller0582b962016-07-19 16:23:29 -07008470 * i40e_vsi_config_rss - Prepare for VSI(VMDq) RSS if used
8471 * @vsi: VSI structure
8472 **/
8473static int i40e_vsi_config_rss(struct i40e_vsi *vsi)
8474{
8475 u8 seed[I40E_HKEY_ARRAY_SIZE];
8476 struct i40e_pf *pf = vsi->back;
8477 u8 *lut;
8478 int ret;
8479
Jacob Kellerd36e41d2017-06-23 04:24:46 -04008480 if (!(pf->hw_features & I40E_HW_RSS_AQ_CAPABLE))
Jacob Keller0582b962016-07-19 16:23:29 -07008481 return 0;
8482
Jacob Keller552b9962016-07-19 16:23:31 -07008483 if (!vsi->rss_size)
8484 vsi->rss_size = min_t(int, pf->alloc_rss_size,
8485 vsi->num_queue_pairs);
8486 if (!vsi->rss_size)
8487 return -EINVAL;
8488
Jacob Keller0582b962016-07-19 16:23:29 -07008489 lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
8490 if (!lut)
8491 return -ENOMEM;
Jacob Keller552b9962016-07-19 16:23:31 -07008492 /* Use the user configured hash keys and lookup table if there is one,
8493 * otherwise use default
8494 */
8495 if (vsi->rss_lut_user)
8496 memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
8497 else
8498 i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
8499 if (vsi->rss_hkey_user)
8500 memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
8501 else
8502 netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
Jacob Keller0582b962016-07-19 16:23:29 -07008503 ret = i40e_config_rss_aq(vsi, seed, lut, vsi->rss_table_size);
8504 kfree(lut);
8505
8506 return ret;
8507}
8508
8509/**
Helin Zhang043dd652015-10-21 19:56:23 -04008510 * i40e_config_rss_reg - Configure RSS keys and lut by writing registers
Helin Zhange69ff812015-10-21 19:56:22 -04008511 * @vsi: Pointer to vsi structure
8512 * @seed: RSS hash seed
8513 * @lut: Lookup table
8514 * @lut_size: Lookup table size
8515 *
8516 * Returns 0 on success, negative on failure
8517 **/
8518static int i40e_config_rss_reg(struct i40e_vsi *vsi, const u8 *seed,
8519 const u8 *lut, u16 lut_size)
8520{
8521 struct i40e_pf *pf = vsi->back;
8522 struct i40e_hw *hw = &pf->hw;
Mitch Williamsc4e18682016-04-12 08:30:40 -07008523 u16 vf_id = vsi->vf_id;
Helin Zhange69ff812015-10-21 19:56:22 -04008524 u8 i;
8525
8526 /* Fill out hash function seed */
8527 if (seed) {
8528 u32 *seed_dw = (u32 *)seed;
8529
Mitch Williamsc4e18682016-04-12 08:30:40 -07008530 if (vsi->type == I40E_VSI_MAIN) {
8531 for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
Lihong Yang26f77e52017-01-30 12:29:33 -08008532 wr32(hw, I40E_PFQF_HKEY(i), seed_dw[i]);
Mitch Williamsc4e18682016-04-12 08:30:40 -07008533 } else if (vsi->type == I40E_VSI_SRIOV) {
8534 for (i = 0; i <= I40E_VFQF_HKEY1_MAX_INDEX; i++)
Lihong Yang26f77e52017-01-30 12:29:33 -08008535 wr32(hw, I40E_VFQF_HKEY1(i, vf_id), seed_dw[i]);
Mitch Williamsc4e18682016-04-12 08:30:40 -07008536 } else {
8537 dev_err(&pf->pdev->dev, "Cannot set RSS seed - invalid VSI type\n");
8538 }
Helin Zhange69ff812015-10-21 19:56:22 -04008539 }
8540
8541 if (lut) {
8542 u32 *lut_dw = (u32 *)lut;
8543
Mitch Williamsc4e18682016-04-12 08:30:40 -07008544 if (vsi->type == I40E_VSI_MAIN) {
8545 if (lut_size != I40E_HLUT_ARRAY_SIZE)
8546 return -EINVAL;
8547 for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
8548 wr32(hw, I40E_PFQF_HLUT(i), lut_dw[i]);
8549 } else if (vsi->type == I40E_VSI_SRIOV) {
8550 if (lut_size != I40E_VF_HLUT_ARRAY_SIZE)
8551 return -EINVAL;
8552 for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
Lihong Yang26f77e52017-01-30 12:29:33 -08008553 wr32(hw, I40E_VFQF_HLUT1(i, vf_id), lut_dw[i]);
Mitch Williamsc4e18682016-04-12 08:30:40 -07008554 } else {
8555 dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
8556 }
Helin Zhange69ff812015-10-21 19:56:22 -04008557 }
8558 i40e_flush(hw);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008559
8560 return 0;
8561}
8562
8563/**
Helin Zhang043dd652015-10-21 19:56:23 -04008564 * i40e_get_rss_reg - Get the RSS keys and lut by reading registers
8565 * @vsi: Pointer to VSI structure
8566 * @seed: Buffer to store the keys
8567 * @lut: Buffer to store the lookup table entries
8568 * @lut_size: Size of buffer to store the lookup table entries
8569 *
8570 * Returns 0 on success, negative on failure
8571 */
8572static int i40e_get_rss_reg(struct i40e_vsi *vsi, u8 *seed,
8573 u8 *lut, u16 lut_size)
8574{
8575 struct i40e_pf *pf = vsi->back;
8576 struct i40e_hw *hw = &pf->hw;
8577 u16 i;
8578
8579 if (seed) {
8580 u32 *seed_dw = (u32 *)seed;
8581
8582 for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
Shannon Nelson272cdaf22016-02-17 16:12:21 -08008583 seed_dw[i] = i40e_read_rx_ctl(hw, I40E_PFQF_HKEY(i));
Helin Zhang043dd652015-10-21 19:56:23 -04008584 }
8585 if (lut) {
8586 u32 *lut_dw = (u32 *)lut;
8587
8588 if (lut_size != I40E_HLUT_ARRAY_SIZE)
8589 return -EINVAL;
8590 for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
8591 lut_dw[i] = rd32(hw, I40E_PFQF_HLUT(i));
8592 }
8593
8594 return 0;
8595}
8596
8597/**
8598 * i40e_config_rss - Configure RSS keys and lut
8599 * @vsi: Pointer to VSI structure
8600 * @seed: RSS hash seed
8601 * @lut: Lookup table
8602 * @lut_size: Lookup table size
8603 *
8604 * Returns 0 on success, negative on failure
8605 */
8606int i40e_config_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
8607{
8608 struct i40e_pf *pf = vsi->back;
8609
Jacob Kellerd36e41d2017-06-23 04:24:46 -04008610 if (pf->hw_features & I40E_HW_RSS_AQ_CAPABLE)
Helin Zhang043dd652015-10-21 19:56:23 -04008611 return i40e_config_rss_aq(vsi, seed, lut, lut_size);
8612 else
8613 return i40e_config_rss_reg(vsi, seed, lut, lut_size);
8614}
8615
8616/**
8617 * i40e_get_rss - Get RSS keys and lut
8618 * @vsi: Pointer to VSI structure
8619 * @seed: Buffer to store the keys
8620 * @lut: Buffer to store the lookup table entries
8621 * lut_size: Size of buffer to store the lookup table entries
8622 *
8623 * Returns 0 on success, negative on failure
8624 */
8625int i40e_get_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
8626{
Anjali Singhai Jain95a73782015-12-22 14:25:04 -08008627 struct i40e_pf *pf = vsi->back;
8628
Jacob Kellerd36e41d2017-06-23 04:24:46 -04008629 if (pf->hw_features & I40E_HW_RSS_AQ_CAPABLE)
Anjali Singhai Jain95a73782015-12-22 14:25:04 -08008630 return i40e_get_rss_aq(vsi, seed, lut, lut_size);
8631 else
8632 return i40e_get_rss_reg(vsi, seed, lut, lut_size);
Helin Zhang043dd652015-10-21 19:56:23 -04008633}
8634
8635/**
Helin Zhange69ff812015-10-21 19:56:22 -04008636 * i40e_fill_rss_lut - Fill the RSS lookup table with default values
8637 * @pf: Pointer to board private structure
8638 * @lut: Lookup table
8639 * @rss_table_size: Lookup table size
8640 * @rss_size: Range of queue number for hashing
8641 */
Alan Bradyf1582352016-08-24 11:33:46 -07008642void i40e_fill_rss_lut(struct i40e_pf *pf, u8 *lut,
8643 u16 rss_table_size, u16 rss_size)
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008644{
Helin Zhange69ff812015-10-21 19:56:22 -04008645 u16 i;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008646
Helin Zhange69ff812015-10-21 19:56:22 -04008647 for (i = 0; i < rss_table_size; i++)
8648 lut[i] = i % rss_size;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008649}
8650
8651/**
Helin Zhang043dd652015-10-21 19:56:23 -04008652 * i40e_pf_config_rss - Prepare for RSS if used
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008653 * @pf: board private structure
8654 **/
Helin Zhang043dd652015-10-21 19:56:23 -04008655static int i40e_pf_config_rss(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008656{
Anjali Singhai Jain66ddcff2015-02-24 06:58:50 +00008657 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008658 u8 seed[I40E_HKEY_ARRAY_SIZE];
Helin Zhange69ff812015-10-21 19:56:22 -04008659 u8 *lut;
Anjali Singhai Jain4617e8c2013-11-20 10:02:56 +00008660 struct i40e_hw *hw = &pf->hw;
Carolyn Wybornye157ea32014-06-03 23:50:22 +00008661 u32 reg_val;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008662 u64 hena;
Helin Zhange69ff812015-10-21 19:56:22 -04008663 int ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008664
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008665 /* By default we enable TCP/UDP with IPv4/IPv6 ptypes */
Shannon Nelson272cdaf22016-02-17 16:12:21 -08008666 hena = (u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(0)) |
8667 ((u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(1)) << 32);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008668 hena |= i40e_pf_get_default_rss_hena(pf);
8669
Shannon Nelson272cdaf22016-02-17 16:12:21 -08008670 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), (u32)hena);
8671 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008672
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008673 /* Determine the RSS table size based on the hardware capabilities */
Shannon Nelson272cdaf22016-02-17 16:12:21 -08008674 reg_val = i40e_read_rx_ctl(hw, I40E_PFQF_CTL_0);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008675 reg_val = (pf->rss_table_size == 512) ?
8676 (reg_val | I40E_PFQF_CTL_0_HASHLUTSIZE_512) :
8677 (reg_val & ~I40E_PFQF_CTL_0_HASHLUTSIZE_512);
Shannon Nelson272cdaf22016-02-17 16:12:21 -08008678 i40e_write_rx_ctl(hw, I40E_PFQF_CTL_0, reg_val);
Carolyn Wybornye157ea32014-06-03 23:50:22 +00008679
Helin Zhang28c58692015-10-26 19:44:27 -04008680 /* Determine the RSS size of the VSI */
Harshitha Ramamurthyf25571b2017-02-21 15:55:40 -08008681 if (!vsi->rss_size) {
8682 u16 qcount;
8683
8684 qcount = vsi->num_queue_pairs / vsi->tc_config.numtc;
8685 vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
8686 }
Mitch Williamsa4fa59c2016-09-12 14:18:43 -07008687 if (!vsi->rss_size)
8688 return -EINVAL;
Helin Zhang28c58692015-10-26 19:44:27 -04008689
Helin Zhange69ff812015-10-21 19:56:22 -04008690 lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
8691 if (!lut)
8692 return -ENOMEM;
8693
Helin Zhang28c58692015-10-26 19:44:27 -04008694 /* Use user configured lut if there is one, otherwise use default */
8695 if (vsi->rss_lut_user)
8696 memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
8697 else
8698 i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
Helin Zhange69ff812015-10-21 19:56:22 -04008699
Helin Zhang28c58692015-10-26 19:44:27 -04008700 /* Use user configured hash key if there is one, otherwise
8701 * use default.
8702 */
8703 if (vsi->rss_hkey_user)
8704 memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
8705 else
8706 netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
Helin Zhang043dd652015-10-21 19:56:23 -04008707 ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
Helin Zhange69ff812015-10-21 19:56:22 -04008708 kfree(lut);
8709
8710 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008711}
8712
8713/**
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008714 * i40e_reconfig_rss_queues - change number of queues for rss and rebuild
8715 * @pf: board private structure
8716 * @queue_count: the requested queue count for rss.
8717 *
8718 * returns 0 if rss is not enabled, if enabled returns the final rss queue
8719 * count which may be different from the requested queue count.
Maciej Sosin373149f2017-04-05 07:50:55 -04008720 * Note: expects to be called while under rtnl_lock()
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008721 **/
8722int i40e_reconfig_rss_queues(struct i40e_pf *pf, int queue_count)
8723{
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00008724 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
8725 int new_rss_size;
8726
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008727 if (!(pf->flags & I40E_FLAG_RSS_ENABLED))
8728 return 0;
8729
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00008730 new_rss_size = min_t(int, queue_count, pf->rss_size_max);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008731
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00008732 if (queue_count != vsi->num_queue_pairs) {
Harshitha Ramamurthyf25571b2017-02-21 15:55:40 -08008733 u16 qcount;
8734
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00008735 vsi->req_queue_pairs = queue_count;
Maciej Sosin373149f2017-04-05 07:50:55 -04008736 i40e_prep_for_reset(pf, true);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008737
Helin Zhangacd65442015-10-26 19:44:28 -04008738 pf->alloc_rss_size = new_rss_size;
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008739
Maciej Sosin373149f2017-04-05 07:50:55 -04008740 i40e_reset_and_rebuild(pf, true, true);
Helin Zhang28c58692015-10-26 19:44:27 -04008741
8742 /* Discard the user configured hash keys and lut, if less
8743 * queues are enabled.
8744 */
8745 if (queue_count < vsi->rss_size) {
8746 i40e_clear_rss_config_user(vsi);
8747 dev_dbg(&pf->pdev->dev,
8748 "discard user configured hash keys and lut\n");
8749 }
8750
8751 /* Reset vsi->rss_size, as number of enabled queues changed */
Harshitha Ramamurthyf25571b2017-02-21 15:55:40 -08008752 qcount = vsi->num_queue_pairs / vsi->tc_config.numtc;
8753 vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
Helin Zhang28c58692015-10-26 19:44:27 -04008754
Helin Zhang043dd652015-10-21 19:56:23 -04008755 i40e_pf_config_rss(pf);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008756 }
Lihong Yang12815052016-09-27 11:28:48 -07008757 dev_info(&pf->pdev->dev, "User requested queue count/HW max RSS count: %d/%d\n",
8758 vsi->req_queue_pairs, pf->rss_size_max);
Helin Zhangacd65442015-10-26 19:44:28 -04008759 return pf->alloc_rss_size;
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008760}
8761
8762/**
Shannon Nelson4fc8c672017-06-07 05:43:08 -04008763 * i40e_get_partition_bw_setting - Retrieve BW settings for this PF partition
Greg Rosef4492db2015-02-06 08:52:12 +00008764 * @pf: board private structure
8765 **/
Shannon Nelson4fc8c672017-06-07 05:43:08 -04008766i40e_status i40e_get_partition_bw_setting(struct i40e_pf *pf)
Greg Rosef4492db2015-02-06 08:52:12 +00008767{
8768 i40e_status status;
8769 bool min_valid, max_valid;
8770 u32 max_bw, min_bw;
8771
8772 status = i40e_read_bw_from_alt_ram(&pf->hw, &max_bw, &min_bw,
8773 &min_valid, &max_valid);
8774
8775 if (!status) {
8776 if (min_valid)
Shannon Nelson4fc8c672017-06-07 05:43:08 -04008777 pf->min_bw = min_bw;
Greg Rosef4492db2015-02-06 08:52:12 +00008778 if (max_valid)
Shannon Nelson4fc8c672017-06-07 05:43:08 -04008779 pf->max_bw = max_bw;
Greg Rosef4492db2015-02-06 08:52:12 +00008780 }
8781
8782 return status;
8783}
8784
8785/**
Shannon Nelson4fc8c672017-06-07 05:43:08 -04008786 * i40e_set_partition_bw_setting - Set BW settings for this PF partition
Greg Rosef4492db2015-02-06 08:52:12 +00008787 * @pf: board private structure
8788 **/
Shannon Nelson4fc8c672017-06-07 05:43:08 -04008789i40e_status i40e_set_partition_bw_setting(struct i40e_pf *pf)
Greg Rosef4492db2015-02-06 08:52:12 +00008790{
8791 struct i40e_aqc_configure_partition_bw_data bw_data;
8792 i40e_status status;
8793
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00008794 /* Set the valid bit for this PF */
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04008795 bw_data.pf_valid_bits = cpu_to_le16(BIT(pf->hw.pf_id));
Shannon Nelson4fc8c672017-06-07 05:43:08 -04008796 bw_data.max_bw[pf->hw.pf_id] = pf->max_bw & I40E_ALT_BW_VALUE_MASK;
8797 bw_data.min_bw[pf->hw.pf_id] = pf->min_bw & I40E_ALT_BW_VALUE_MASK;
Greg Rosef4492db2015-02-06 08:52:12 +00008798
8799 /* Set the new bandwidths */
8800 status = i40e_aq_configure_partition_bw(&pf->hw, &bw_data, NULL);
8801
8802 return status;
8803}
8804
8805/**
Shannon Nelson4fc8c672017-06-07 05:43:08 -04008806 * i40e_commit_partition_bw_setting - Commit BW settings for this PF partition
Greg Rosef4492db2015-02-06 08:52:12 +00008807 * @pf: board private structure
8808 **/
Shannon Nelson4fc8c672017-06-07 05:43:08 -04008809i40e_status i40e_commit_partition_bw_setting(struct i40e_pf *pf)
Greg Rosef4492db2015-02-06 08:52:12 +00008810{
8811 /* Commit temporary BW setting to permanent NVM image */
8812 enum i40e_admin_queue_err last_aq_status;
8813 i40e_status ret;
8814 u16 nvm_word;
8815
8816 if (pf->hw.partition_id != 1) {
8817 dev_info(&pf->pdev->dev,
8818 "Commit BW only works on partition 1! This is partition %d",
8819 pf->hw.partition_id);
8820 ret = I40E_NOT_SUPPORTED;
8821 goto bw_commit_out;
8822 }
8823
8824 /* Acquire NVM for read access */
8825 ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_READ);
8826 last_aq_status = pf->hw.aq.asq_last_status;
8827 if (ret) {
8828 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008829 "Cannot acquire NVM for read access, err %s aq_err %s\n",
8830 i40e_stat_str(&pf->hw, ret),
8831 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +00008832 goto bw_commit_out;
8833 }
8834
8835 /* Read word 0x10 of NVM - SW compatibility word 1 */
8836 ret = i40e_aq_read_nvm(&pf->hw,
8837 I40E_SR_NVM_CONTROL_WORD,
8838 0x10, sizeof(nvm_word), &nvm_word,
8839 false, NULL);
8840 /* Save off last admin queue command status before releasing
8841 * the NVM
8842 */
8843 last_aq_status = pf->hw.aq.asq_last_status;
8844 i40e_release_nvm(&pf->hw);
8845 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008846 dev_info(&pf->pdev->dev, "NVM read error, err %s aq_err %s\n",
8847 i40e_stat_str(&pf->hw, ret),
8848 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +00008849 goto bw_commit_out;
8850 }
8851
8852 /* Wait a bit for NVM release to complete */
8853 msleep(50);
8854
8855 /* Acquire NVM for write access */
8856 ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_WRITE);
8857 last_aq_status = pf->hw.aq.asq_last_status;
8858 if (ret) {
8859 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008860 "Cannot acquire NVM for write access, err %s aq_err %s\n",
8861 i40e_stat_str(&pf->hw, ret),
8862 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +00008863 goto bw_commit_out;
8864 }
8865 /* Write it back out unchanged to initiate update NVM,
8866 * which will force a write of the shadow (alt) RAM to
8867 * the NVM - thus storing the bandwidth values permanently.
8868 */
8869 ret = i40e_aq_update_nvm(&pf->hw,
8870 I40E_SR_NVM_CONTROL_WORD,
8871 0x10, sizeof(nvm_word),
8872 &nvm_word, true, NULL);
8873 /* Save off last admin queue command status before releasing
8874 * the NVM
8875 */
8876 last_aq_status = pf->hw.aq.asq_last_status;
8877 i40e_release_nvm(&pf->hw);
8878 if (ret)
8879 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008880 "BW settings NOT SAVED, err %s aq_err %s\n",
8881 i40e_stat_str(&pf->hw, ret),
8882 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +00008883bw_commit_out:
8884
8885 return ret;
8886}
8887
8888/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008889 * i40e_sw_init - Initialize general software structures (struct i40e_pf)
8890 * @pf: board private structure to initialize
8891 *
8892 * i40e_sw_init initializes the Adapter private data structure.
8893 * Fields are initialized based on PCI device information and
8894 * OS network device settings (MTU size).
8895 **/
8896static int i40e_sw_init(struct i40e_pf *pf)
8897{
8898 int err = 0;
8899 int size;
8900
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008901 /* Set default capability flags */
8902 pf->flags = I40E_FLAG_RX_CSUM_ENABLED |
8903 I40E_FLAG_MSI_ENABLED |
Mitch Williams2bc7ee82015-02-06 08:52:11 +00008904 I40E_FLAG_MSIX_ENABLED;
8905
Mitch Williamsca99eb92014-04-04 04:43:07 +00008906 /* Set default ITR */
8907 pf->rx_itr_default = I40E_ITR_DYNAMIC | I40E_ITR_RX_DEF;
8908 pf->tx_itr_default = I40E_ITR_DYNAMIC | I40E_ITR_TX_DEF;
8909
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00008910 /* Depending on PF configurations, it is possible that the RSS
8911 * maximum might end up larger than the available queues
8912 */
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04008913 pf->rss_size_max = BIT(pf->hw.func_caps.rss_table_entry_width);
Helin Zhangacd65442015-10-26 19:44:28 -04008914 pf->alloc_rss_size = 1;
Anjali Singhai Jain5db4cb52015-02-24 06:58:49 +00008915 pf->rss_table_size = pf->hw.func_caps.rss_table_size;
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00008916 pf->rss_size_max = min_t(int, pf->rss_size_max,
8917 pf->hw.func_caps.num_tx_qp);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008918 if (pf->hw.func_caps.rss) {
8919 pf->flags |= I40E_FLAG_RSS_ENABLED;
Helin Zhangacd65442015-10-26 19:44:28 -04008920 pf->alloc_rss_size = min_t(int, pf->rss_size_max,
8921 num_online_cpus());
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008922 }
8923
Catherine Sullivan2050bc62013-12-18 13:46:03 +00008924 /* MFP mode enabled */
Pawel Orlowskic78b9532015-04-22 19:34:06 -04008925 if (pf->hw.func_caps.npar_enable || pf->hw.func_caps.flex10_enable) {
Catherine Sullivan2050bc62013-12-18 13:46:03 +00008926 pf->flags |= I40E_FLAG_MFP_ENABLED;
8927 dev_info(&pf->pdev->dev, "MFP mode Enabled\n");
Shannon Nelson4fc8c672017-06-07 05:43:08 -04008928 if (i40e_get_partition_bw_setting(pf)) {
Greg Rosef4492db2015-02-06 08:52:12 +00008929 dev_warn(&pf->pdev->dev,
Shannon Nelson4fc8c672017-06-07 05:43:08 -04008930 "Could not get partition bw settings\n");
8931 } else {
Greg Rosef4492db2015-02-06 08:52:12 +00008932 dev_info(&pf->pdev->dev,
Shannon Nelson4fc8c672017-06-07 05:43:08 -04008933 "Partition BW Min = %8.8x, Max = %8.8x\n",
8934 pf->min_bw, pf->max_bw);
8935
8936 /* nudge the Tx scheduler */
8937 i40e_set_partition_bw_setting(pf);
8938 }
Catherine Sullivan2050bc62013-12-18 13:46:03 +00008939 }
8940
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008941 if ((pf->hw.func_caps.fd_filters_guaranteed > 0) ||
8942 (pf->hw.func_caps.fd_filters_best_effort > 0)) {
8943 pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
8944 pf->atr_sample_rate = I40E_DEFAULT_ATR_SAMPLE_RATE;
Shannon Nelson6eae9c62015-09-03 17:18:55 -04008945 if (pf->flags & I40E_FLAG_MFP_ENABLED &&
8946 pf->hw.num_partitions > 1)
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008947 dev_info(&pf->pdev->dev,
Anjali Singhai Jain0b675842014-03-06 08:59:51 +00008948 "Flow Director Sideband mode Disabled in MFP mode\n");
Shannon Nelson6eae9c62015-09-03 17:18:55 -04008949 else
8950 pf->flags |= I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008951 pf->fdir_pf_filter_count =
8952 pf->hw.func_caps.fd_filters_guaranteed;
8953 pf->hw.fdir_shared_filter_count =
8954 pf->hw.func_caps.fd_filters_best_effort;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008955 }
8956
Anjali Singhai Jain5a433192017-06-23 04:24:45 -04008957 if (pf->hw.mac.type == I40E_MAC_X722) {
Jacob Kellerd36e41d2017-06-23 04:24:46 -04008958 pf->hw_features |= (I40E_HW_RSS_AQ_CAPABLE |
8959 I40E_HW_128_QP_RSS_CAPABLE |
8960 I40E_HW_ATR_EVICT_CAPABLE |
8961 I40E_HW_WB_ON_ITR_CAPABLE |
8962 I40E_HW_MULTIPLE_TCP_UDP_RSS_PCTYPE |
8963 I40E_HW_NO_PCI_LINK_CHECK |
8964 I40E_HW_USE_SET_LLDP_MIB |
8965 I40E_HW_GENEVE_OFFLOAD_CAPABLE |
8966 I40E_HW_PTP_L4_CAPABLE |
8967 I40E_HW_WOL_MC_MAGIC_PKT_WAKE |
8968 I40E_HW_OUTER_UDP_CSUM_CAPABLE);
Anjali Singhai Jain10a955f2017-06-23 04:24:48 -04008969
8970#define I40E_FDEVICT_PCTYPE_DEFAULT 0xc03
8971 if (rd32(&pf->hw, I40E_GLQF_FDEVICTENA(1)) !=
8972 I40E_FDEVICT_PCTYPE_DEFAULT) {
8973 dev_warn(&pf->pdev->dev,
8974 "FD EVICT PCTYPES are not right, disable FD HW EVICT\n");
8975 pf->hw_features &= ~I40E_HW_ATR_EVICT_CAPABLE;
8976 }
Anjali Singhai Jain5a433192017-06-23 04:24:45 -04008977 } else if ((pf->hw.aq.api_maj_ver > 1) ||
8978 ((pf->hw.aq.api_maj_ver == 1) &&
8979 (pf->hw.aq.api_min_ver > 4))) {
8980 /* Supported in FW API version higher than 1.4 */
Jacob Kellerd36e41d2017-06-23 04:24:46 -04008981 pf->hw_features |= I40E_HW_GENEVE_OFFLOAD_CAPABLE;
Anjali Singhai Jain5a433192017-06-23 04:24:45 -04008982 }
8983
8984 /* Enable HW ATR eviction if possible */
Jacob Kellerd36e41d2017-06-23 04:24:46 -04008985 if (pf->hw_features & I40E_HW_ATR_EVICT_CAPABLE)
Anjali Singhai Jain5a433192017-06-23 04:24:45 -04008986 pf->flags |= I40E_FLAG_HW_ATR_EVICT_ENABLED;
8987
Bimmy Pujari6de432c2016-11-11 12:39:38 -08008988 if ((pf->hw.mac.type == I40E_MAC_XL710) &&
Anjali Singhai Jain8eed76f2015-12-09 15:50:31 -08008989 (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 33)) ||
Neerav Parikhf1bbad32016-01-13 16:51:39 -08008990 (pf->hw.aq.fw_maj_ver < 4))) {
Jacob Kellerd36e41d2017-06-23 04:24:46 -04008991 pf->hw_features |= I40E_HW_RESTART_AUTONEG;
Neerav Parikhf1bbad32016-01-13 16:51:39 -08008992 /* No DCB support for FW < v4.33 */
Jacob Kellerd36e41d2017-06-23 04:24:46 -04008993 pf->hw_features |= I40E_HW_NO_DCB_SUPPORT;
Neerav Parikhf1bbad32016-01-13 16:51:39 -08008994 }
8995
8996 /* Disable FW LLDP if FW < v4.3 */
Bimmy Pujari6de432c2016-11-11 12:39:38 -08008997 if ((pf->hw.mac.type == I40E_MAC_XL710) &&
Neerav Parikhf1bbad32016-01-13 16:51:39 -08008998 (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 3)) ||
8999 (pf->hw.aq.fw_maj_ver < 4)))
Jacob Kellerd36e41d2017-06-23 04:24:46 -04009000 pf->hw_features |= I40E_HW_STOP_FW_LLDP;
Neerav Parikhf1bbad32016-01-13 16:51:39 -08009001
9002 /* Use the FW Set LLDP MIB API if FW > v4.40 */
Bimmy Pujari6de432c2016-11-11 12:39:38 -08009003 if ((pf->hw.mac.type == I40E_MAC_XL710) &&
Neerav Parikhf1bbad32016-01-13 16:51:39 -08009004 (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver >= 40)) ||
9005 (pf->hw.aq.fw_maj_ver >= 5)))
Jacob Kellerd36e41d2017-06-23 04:24:46 -04009006 pf->hw_features |= I40E_HW_USE_SET_LLDP_MIB;
Anjali Singhai Jain8eed76f2015-12-09 15:50:31 -08009007
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009008 if (pf->hw.func_caps.vmdq) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009009 pf->num_vmdq_vsis = I40E_DEFAULT_NUM_VMDQ_VSI;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04009010 pf->flags |= I40E_FLAG_VMDQ_ENABLED;
Jesse Brandeburge9e53662015-10-02 17:57:21 -07009011 pf->num_vmdq_qps = i40e_default_queues_per_vmdq(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009012 }
9013
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06009014 if (pf->hw.func_caps.iwarp) {
9015 pf->flags |= I40E_FLAG_IWARP_ENABLED;
9016 /* IWARP needs one extra vector for CQP just like MISC.*/
9017 pf->num_iwarp_msix = (int)num_online_cpus() + 1;
9018 }
9019
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009020#ifdef CONFIG_PCI_IOV
Shannon Nelsonba252f132014-12-11 07:06:34 +00009021 if (pf->hw.func_caps.num_vfs && pf->hw.partition_id == 1) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009022 pf->num_vf_qps = I40E_DEFAULT_QUEUES_PER_VF;
9023 pf->flags |= I40E_FLAG_SRIOV_ENABLED;
9024 pf->num_req_vfs = min_t(int,
9025 pf->hw.func_caps.num_vfs,
9026 I40E_MAX_VF_COUNT);
9027 }
9028#endif /* CONFIG_PCI_IOV */
9029 pf->eeprom_version = 0xDEAD;
9030 pf->lan_veb = I40E_NO_VEB;
9031 pf->lan_vsi = I40E_NO_VSI;
9032
Anjali Singhai Jaind1a8d272015-07-23 16:54:40 -04009033 /* By default FW has this off for performance reasons */
9034 pf->flags &= ~I40E_FLAG_VEB_STATS_ENABLED;
9035
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009036 /* set up queue assignment tracking */
9037 size = sizeof(struct i40e_lump_tracking)
9038 + (sizeof(u16) * pf->hw.func_caps.num_tx_qp);
9039 pf->qp_pile = kzalloc(size, GFP_KERNEL);
9040 if (!pf->qp_pile) {
9041 err = -ENOMEM;
9042 goto sw_init_done;
9043 }
9044 pf->qp_pile->num_entries = pf->hw.func_caps.num_tx_qp;
9045 pf->qp_pile->search_hint = 0;
9046
Anjali Singhai Jain327fe042014-06-04 01:23:26 +00009047 pf->tx_timeout_recovery_level = 1;
9048
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009049 mutex_init(&pf->switch_mutex);
9050
9051sw_init_done:
9052 return err;
9053}
9054
9055/**
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00009056 * i40e_set_ntuple - set the ntuple feature flag and take action
9057 * @pf: board private structure to initialize
9058 * @features: the feature set that the stack is suggesting
9059 *
9060 * returns a bool to indicate if reset needs to happen
9061 **/
9062bool i40e_set_ntuple(struct i40e_pf *pf, netdev_features_t features)
9063{
9064 bool need_reset = false;
9065
9066 /* Check if Flow Director n-tuple support was enabled or disabled. If
9067 * the state changed, we need to reset.
9068 */
9069 if (features & NETIF_F_NTUPLE) {
9070 /* Enable filters and mark for reset */
9071 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
9072 need_reset = true;
Tushar Davea70e4072016-05-16 12:40:53 -07009073 /* enable FD_SB only if there is MSI-X vector */
9074 if (pf->num_fdsb_msix > 0)
9075 pf->flags |= I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00009076 } else {
9077 /* turn off filters, mark for reset and clear SW filter list */
9078 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
9079 need_reset = true;
9080 i40e_fdir_filter_exit(pf);
9081 }
Jacob Keller47994c12017-04-19 09:25:57 -04009082 pf->flags &= ~(I40E_FLAG_FD_SB_ENABLED |
9083 I40E_FLAG_FD_SB_AUTO_DISABLED);
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00009084 /* reset fd counters */
Jacob Keller097dbf52017-02-06 14:38:46 -08009085 pf->fd_add_err = 0;
9086 pf->fd_atr_cnt = 0;
Anjali Singhai Jain8a4f34f2014-06-04 08:45:20 +00009087 /* if ATR was auto disabled it can be re-enabled. */
Jacob Keller47994c12017-04-19 09:25:57 -04009088 if (pf->flags & I40E_FLAG_FD_ATR_AUTO_DISABLED) {
9089 pf->flags &= ~I40E_FLAG_FD_ATR_AUTO_DISABLED;
9090 if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
9091 (I40E_DEBUG_FD & pf->hw.debug_mask))
Jacob Keller234dc4e2016-09-06 18:05:09 -07009092 dev_info(&pf->pdev->dev, "ATR re-enabled.\n");
9093 }
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00009094 }
9095 return need_reset;
9096}
9097
9098/**
Alan Bradyd8ec9862016-07-27 12:02:38 -07009099 * i40e_clear_rss_lut - clear the rx hash lookup table
9100 * @vsi: the VSI being configured
9101 **/
9102static void i40e_clear_rss_lut(struct i40e_vsi *vsi)
9103{
9104 struct i40e_pf *pf = vsi->back;
9105 struct i40e_hw *hw = &pf->hw;
9106 u16 vf_id = vsi->vf_id;
9107 u8 i;
9108
9109 if (vsi->type == I40E_VSI_MAIN) {
9110 for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
9111 wr32(hw, I40E_PFQF_HLUT(i), 0);
9112 } else if (vsi->type == I40E_VSI_SRIOV) {
9113 for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
9114 i40e_write_rx_ctl(hw, I40E_VFQF_HLUT1(i, vf_id), 0);
9115 } else {
9116 dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
9117 }
9118}
9119
9120/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009121 * i40e_set_features - set the netdev feature flags
9122 * @netdev: ptr to the netdev being adjusted
9123 * @features: the feature set that the stack is suggesting
Maciej Sosin373149f2017-04-05 07:50:55 -04009124 * Note: expects to be called while under rtnl_lock()
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009125 **/
9126static int i40e_set_features(struct net_device *netdev,
9127 netdev_features_t features)
9128{
9129 struct i40e_netdev_priv *np = netdev_priv(netdev);
9130 struct i40e_vsi *vsi = np->vsi;
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00009131 struct i40e_pf *pf = vsi->back;
9132 bool need_reset;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009133
Alan Bradyd8ec9862016-07-27 12:02:38 -07009134 if (features & NETIF_F_RXHASH && !(netdev->features & NETIF_F_RXHASH))
9135 i40e_pf_config_rss(pf);
9136 else if (!(features & NETIF_F_RXHASH) &&
9137 netdev->features & NETIF_F_RXHASH)
9138 i40e_clear_rss_lut(vsi);
9139
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009140 if (features & NETIF_F_HW_VLAN_CTAG_RX)
9141 i40e_vlan_stripping_enable(vsi);
9142 else
9143 i40e_vlan_stripping_disable(vsi);
9144
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00009145 need_reset = i40e_set_ntuple(pf, features);
9146
9147 if (need_reset)
Maciej Sosin373149f2017-04-05 07:50:55 -04009148 i40e_do_reset(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED), true);
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00009149
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009150 return 0;
9151}
9152
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009153/**
Singhai, Anjali6a899022015-12-14 12:21:18 -08009154 * i40e_get_udp_port_idx - Lookup a possibly offloaded for Rx UDP port
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009155 * @pf: board private structure
9156 * @port: The UDP port to look up
9157 *
9158 * Returns the index number or I40E_MAX_PF_UDP_OFFLOAD_PORTS if port not found
9159 **/
Jacob Kellerfe0b0cd2017-02-06 14:38:38 -08009160static u8 i40e_get_udp_port_idx(struct i40e_pf *pf, u16 port)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009161{
9162 u8 i;
9163
9164 for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
Jacob Keller27826fd2017-04-19 09:25:50 -04009165 if (pf->udp_ports[i].port == port)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009166 return i;
9167 }
9168
9169 return i;
9170}
9171
9172/**
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009173 * i40e_udp_tunnel_add - Get notifications about UDP tunnel ports that come up
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009174 * @netdev: This physical port's netdev
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009175 * @ti: Tunnel endpoint information
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009176 **/
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009177static void i40e_udp_tunnel_add(struct net_device *netdev,
9178 struct udp_tunnel_info *ti)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009179{
9180 struct i40e_netdev_priv *np = netdev_priv(netdev);
9181 struct i40e_vsi *vsi = np->vsi;
9182 struct i40e_pf *pf = vsi->back;
Jacob Kellerfe0b0cd2017-02-06 14:38:38 -08009183 u16 port = ntohs(ti->port);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009184 u8 next_idx;
9185 u8 idx;
9186
Singhai, Anjali6a899022015-12-14 12:21:18 -08009187 idx = i40e_get_udp_port_idx(pf, port);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009188
9189 /* Check if port already exists */
9190 if (idx < I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
Jacob Kellerfe0b0cd2017-02-06 14:38:38 -08009191 netdev_info(netdev, "port %d already offloaded\n", port);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009192 return;
9193 }
9194
9195 /* Now check if there is space to add the new port */
Singhai, Anjali6a899022015-12-14 12:21:18 -08009196 next_idx = i40e_get_udp_port_idx(pf, 0);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009197
9198 if (next_idx == I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009199 netdev_info(netdev, "maximum number of offloaded UDP ports reached, not adding port %d\n",
Jacob Kellerfe0b0cd2017-02-06 14:38:38 -08009200 port);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009201 return;
9202 }
9203
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009204 switch (ti->type) {
9205 case UDP_TUNNEL_TYPE_VXLAN:
9206 pf->udp_ports[next_idx].type = I40E_AQC_TUNNEL_TYPE_VXLAN;
9207 break;
9208 case UDP_TUNNEL_TYPE_GENEVE:
Jacob Kellerd36e41d2017-06-23 04:24:46 -04009209 if (!(pf->hw_features & I40E_HW_GENEVE_OFFLOAD_CAPABLE))
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009210 return;
9211 pf->udp_ports[next_idx].type = I40E_AQC_TUNNEL_TYPE_NGE;
9212 break;
9213 default:
9214 return;
9215 }
9216
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009217 /* New port: add it and mark its index in the bitmap */
Jacob Keller27826fd2017-04-19 09:25:50 -04009218 pf->udp_ports[next_idx].port = port;
Singhai, Anjali6a899022015-12-14 12:21:18 -08009219 pf->pending_udp_bitmap |= BIT_ULL(next_idx);
9220 pf->flags |= I40E_FLAG_UDP_FILTER_SYNC;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009221}
9222
9223/**
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009224 * i40e_udp_tunnel_del - Get notifications about UDP tunnel ports that go away
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009225 * @netdev: This physical port's netdev
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009226 * @ti: Tunnel endpoint information
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009227 **/
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009228static void i40e_udp_tunnel_del(struct net_device *netdev,
9229 struct udp_tunnel_info *ti)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009230{
9231 struct i40e_netdev_priv *np = netdev_priv(netdev);
9232 struct i40e_vsi *vsi = np->vsi;
9233 struct i40e_pf *pf = vsi->back;
Jacob Kellerfe0b0cd2017-02-06 14:38:38 -08009234 u16 port = ntohs(ti->port);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009235 u8 idx;
9236
Singhai, Anjali6a899022015-12-14 12:21:18 -08009237 idx = i40e_get_udp_port_idx(pf, port);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009238
9239 /* Check if port already exists */
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009240 if (idx >= I40E_MAX_PF_UDP_OFFLOAD_PORTS)
9241 goto not_found;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009242
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009243 switch (ti->type) {
9244 case UDP_TUNNEL_TYPE_VXLAN:
9245 if (pf->udp_ports[idx].type != I40E_AQC_TUNNEL_TYPE_VXLAN)
9246 goto not_found;
9247 break;
9248 case UDP_TUNNEL_TYPE_GENEVE:
9249 if (pf->udp_ports[idx].type != I40E_AQC_TUNNEL_TYPE_NGE)
9250 goto not_found;
9251 break;
9252 default:
9253 goto not_found;
Singhai, Anjali6a899022015-12-14 12:21:18 -08009254 }
9255
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009256 /* if port exists, set it to 0 (mark for deletion)
9257 * and make it pending
9258 */
Jacob Keller27826fd2017-04-19 09:25:50 -04009259 pf->udp_ports[idx].port = 0;
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009260 pf->pending_udp_bitmap |= BIT_ULL(idx);
Singhai, Anjali6a899022015-12-14 12:21:18 -08009261 pf->flags |= I40E_FLAG_UDP_FILTER_SYNC;
9262
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009263 return;
9264not_found:
9265 netdev_warn(netdev, "UDP port %d was not found, not deleting\n",
Jacob Kellerfe0b0cd2017-02-06 14:38:38 -08009266 port);
Singhai, Anjali6a899022015-12-14 12:21:18 -08009267}
9268
Neerav Parikh1f224ad2014-02-12 01:45:31 +00009269static int i40e_get_phys_port_id(struct net_device *netdev,
Jiri Pirko02637fc2014-11-28 14:34:16 +01009270 struct netdev_phys_item_id *ppid)
Neerav Parikh1f224ad2014-02-12 01:45:31 +00009271{
9272 struct i40e_netdev_priv *np = netdev_priv(netdev);
9273 struct i40e_pf *pf = np->vsi->back;
9274 struct i40e_hw *hw = &pf->hw;
9275
Jacob Kellerd36e41d2017-06-23 04:24:46 -04009276 if (!(pf->hw_features & I40E_HW_PORT_ID_VALID))
Neerav Parikh1f224ad2014-02-12 01:45:31 +00009277 return -EOPNOTSUPP;
9278
9279 ppid->id_len = min_t(int, sizeof(hw->mac.port_addr), sizeof(ppid->id));
9280 memcpy(ppid->id, hw->mac.port_addr, ppid->id_len);
9281
9282 return 0;
9283}
9284
Jesse Brandeburg2f90ade2014-11-20 16:30:02 -08009285/**
9286 * i40e_ndo_fdb_add - add an entry to the hardware database
9287 * @ndm: the input from the stack
9288 * @tb: pointer to array of nladdr (unused)
9289 * @dev: the net device pointer
9290 * @addr: the MAC address entry being added
9291 * @flags: instructions from stack about fdb operation
9292 */
Greg Rose4ba0dea2014-03-06 08:59:55 +00009293static int i40e_ndo_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
9294 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +01009295 const unsigned char *addr, u16 vid,
Greg Rose4ba0dea2014-03-06 08:59:55 +00009296 u16 flags)
Greg Rose4ba0dea2014-03-06 08:59:55 +00009297{
9298 struct i40e_netdev_priv *np = netdev_priv(dev);
9299 struct i40e_pf *pf = np->vsi->back;
9300 int err = 0;
9301
9302 if (!(pf->flags & I40E_FLAG_SRIOV_ENABLED))
9303 return -EOPNOTSUPP;
9304
Or Gerlitz65891fe2014-12-14 18:19:05 +02009305 if (vid) {
9306 pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
9307 return -EINVAL;
9308 }
9309
Greg Rose4ba0dea2014-03-06 08:59:55 +00009310 /* Hardware does not support aging addresses so if a
9311 * ndm_state is given only allow permanent addresses
9312 */
9313 if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
9314 netdev_info(dev, "FDB only supports static addresses\n");
9315 return -EINVAL;
9316 }
9317
9318 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
9319 err = dev_uc_add_excl(dev, addr);
9320 else if (is_multicast_ether_addr(addr))
9321 err = dev_mc_add_excl(dev, addr);
9322 else
9323 err = -EINVAL;
9324
9325 /* Only return duplicate errors if NLM_F_EXCL is set */
9326 if (err == -EEXIST && !(flags & NLM_F_EXCL))
9327 err = 0;
9328
9329 return err;
9330}
9331
Neerav Parikh51616012015-02-06 08:52:14 +00009332/**
9333 * i40e_ndo_bridge_setlink - Set the hardware bridge mode
9334 * @dev: the netdev being configured
9335 * @nlh: RTNL message
9336 *
9337 * Inserts a new hardware bridge if not already created and
9338 * enables the bridging mode requested (VEB or VEPA). If the
9339 * hardware bridge has already been inserted and the request
9340 * is to change the mode then that requires a PF reset to
9341 * allow rebuild of the components with required hardware
9342 * bridge mode enabled.
Maciej Sosin373149f2017-04-05 07:50:55 -04009343 *
9344 * Note: expects to be called while under rtnl_lock()
Neerav Parikh51616012015-02-06 08:52:14 +00009345 **/
9346static int i40e_ndo_bridge_setlink(struct net_device *dev,
Carolyn Wyborny9df70b62015-04-27 14:57:11 -04009347 struct nlmsghdr *nlh,
9348 u16 flags)
Neerav Parikh51616012015-02-06 08:52:14 +00009349{
9350 struct i40e_netdev_priv *np = netdev_priv(dev);
9351 struct i40e_vsi *vsi = np->vsi;
9352 struct i40e_pf *pf = vsi->back;
9353 struct i40e_veb *veb = NULL;
9354 struct nlattr *attr, *br_spec;
9355 int i, rem;
9356
9357 /* Only for PF VSI for now */
9358 if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
9359 return -EOPNOTSUPP;
9360
9361 /* Find the HW bridge for PF VSI */
9362 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
9363 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
9364 veb = pf->veb[i];
9365 }
9366
9367 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
9368
9369 nla_for_each_nested(attr, br_spec, rem) {
9370 __u16 mode;
9371
9372 if (nla_type(attr) != IFLA_BRIDGE_MODE)
9373 continue;
9374
9375 mode = nla_get_u16(attr);
9376 if ((mode != BRIDGE_MODE_VEPA) &&
9377 (mode != BRIDGE_MODE_VEB))
9378 return -EINVAL;
9379
9380 /* Insert a new HW bridge */
9381 if (!veb) {
9382 veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
9383 vsi->tc_config.enabled_tc);
9384 if (veb) {
9385 veb->bridge_mode = mode;
9386 i40e_config_bridge_mode(veb);
9387 } else {
9388 /* No Bridge HW offload available */
9389 return -ENOENT;
9390 }
9391 break;
9392 } else if (mode != veb->bridge_mode) {
9393 /* Existing HW bridge but different mode needs reset */
9394 veb->bridge_mode = mode;
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07009395 /* TODO: If no VFs or VMDq VSIs, disallow VEB mode */
9396 if (mode == BRIDGE_MODE_VEB)
9397 pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
9398 else
9399 pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
Maciej Sosin373149f2017-04-05 07:50:55 -04009400 i40e_do_reset(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED),
9401 true);
Neerav Parikh51616012015-02-06 08:52:14 +00009402 break;
9403 }
9404 }
9405
9406 return 0;
9407}
9408
9409/**
9410 * i40e_ndo_bridge_getlink - Get the hardware bridge mode
9411 * @skb: skb buff
9412 * @pid: process id
9413 * @seq: RTNL message seq #
9414 * @dev: the netdev being configured
9415 * @filter_mask: unused
Jesse Brandeburgd4b2f9f2015-09-03 17:18:48 -04009416 * @nlflags: netlink flags passed in
Neerav Parikh51616012015-02-06 08:52:14 +00009417 *
9418 * Return the mode in which the hardware bridge is operating in
9419 * i.e VEB or VEPA.
9420 **/
Neerav Parikh51616012015-02-06 08:52:14 +00009421static int i40e_ndo_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
9422 struct net_device *dev,
Carolyn Wyborny9f4ffc42015-08-31 19:54:42 -04009423 u32 __always_unused filter_mask,
9424 int nlflags)
Neerav Parikh51616012015-02-06 08:52:14 +00009425{
9426 struct i40e_netdev_priv *np = netdev_priv(dev);
9427 struct i40e_vsi *vsi = np->vsi;
9428 struct i40e_pf *pf = vsi->back;
9429 struct i40e_veb *veb = NULL;
9430 int i;
9431
9432 /* Only for PF VSI for now */
9433 if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
9434 return -EOPNOTSUPP;
9435
9436 /* Find the HW bridge for the PF VSI */
9437 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
9438 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
9439 veb = pf->veb[i];
9440 }
9441
9442 if (!veb)
9443 return 0;
9444
Nicolas Dichtel46c264d2015-04-28 18:33:49 +02009445 return ndo_dflt_bridge_getlink(skb, pid, seq, dev, veb->bridge_mode,
Huaibin Wang599b0762016-09-26 09:51:18 +02009446 0, 0, nlflags, filter_mask, NULL);
Neerav Parikh51616012015-02-06 08:52:14 +00009447}
Neerav Parikh51616012015-02-06 08:52:14 +00009448
Joe Stringerf44a75e2015-04-14 17:09:14 -07009449/**
9450 * i40e_features_check - Validate encapsulated packet conforms to limits
9451 * @skb: skb buff
Jean Sacren2bc11c62015-09-19 05:08:43 -06009452 * @dev: This physical port's netdev
Joe Stringerf44a75e2015-04-14 17:09:14 -07009453 * @features: Offload features that the stack believes apply
9454 **/
9455static netdev_features_t i40e_features_check(struct sk_buff *skb,
9456 struct net_device *dev,
9457 netdev_features_t features)
9458{
Alexander Duyckf114dca2016-10-25 16:08:46 -07009459 size_t len;
9460
9461 /* No point in doing any of this if neither checksum nor GSO are
9462 * being requested for this frame. We can rule out both by just
9463 * checking for CHECKSUM_PARTIAL
9464 */
9465 if (skb->ip_summed != CHECKSUM_PARTIAL)
9466 return features;
9467
9468 /* We cannot support GSO if the MSS is going to be less than
9469 * 64 bytes. If it is then we need to drop support for GSO.
9470 */
9471 if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
9472 features &= ~NETIF_F_GSO_MASK;
9473
9474 /* MACLEN can support at most 63 words */
9475 len = skb_network_header(skb) - skb->data;
9476 if (len & ~(63 * 2))
9477 goto out_err;
9478
9479 /* IPLEN and EIPLEN can support at most 127 dwords */
9480 len = skb_transport_header(skb) - skb_network_header(skb);
9481 if (len & ~(127 * 4))
9482 goto out_err;
9483
9484 if (skb->encapsulation) {
9485 /* L4TUNLEN can support 127 words */
9486 len = skb_inner_network_header(skb) - skb_transport_header(skb);
9487 if (len & ~(127 * 2))
9488 goto out_err;
9489
9490 /* IPLEN can support at most 127 dwords */
9491 len = skb_inner_transport_header(skb) -
9492 skb_inner_network_header(skb);
9493 if (len & ~(127 * 4))
9494 goto out_err;
9495 }
9496
9497 /* No need to validate L4LEN as TCP is the only protocol with a
9498 * a flexible value and we support all possible values supported
9499 * by TCP, which is at most 15 dwords
9500 */
Joe Stringerf44a75e2015-04-14 17:09:14 -07009501
9502 return features;
Alexander Duyckf114dca2016-10-25 16:08:46 -07009503out_err:
9504 return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
Joe Stringerf44a75e2015-04-14 17:09:14 -07009505}
9506
Björn Töpel0c8493d2017-05-24 07:55:34 +02009507/**
9508 * i40e_xdp_setup - add/remove an XDP program
9509 * @vsi: VSI to changed
9510 * @prog: XDP program
9511 **/
9512static int i40e_xdp_setup(struct i40e_vsi *vsi,
9513 struct bpf_prog *prog)
9514{
9515 int frame_size = vsi->netdev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
9516 struct i40e_pf *pf = vsi->back;
9517 struct bpf_prog *old_prog;
9518 bool need_reset;
9519 int i;
9520
9521 /* Don't allow frames that span over multiple buffers */
9522 if (frame_size > vsi->rx_buf_len)
9523 return -EINVAL;
9524
9525 if (!i40e_enabled_xdp_vsi(vsi) && !prog)
9526 return 0;
9527
9528 /* When turning XDP on->off/off->on we reset and rebuild the rings. */
9529 need_reset = (i40e_enabled_xdp_vsi(vsi) != !!prog);
9530
9531 if (need_reset)
9532 i40e_prep_for_reset(pf, true);
9533
9534 old_prog = xchg(&vsi->xdp_prog, prog);
9535
9536 if (need_reset)
9537 i40e_reset_and_rebuild(pf, true, true);
9538
9539 for (i = 0; i < vsi->num_queue_pairs; i++)
9540 WRITE_ONCE(vsi->rx_rings[i]->xdp_prog, vsi->xdp_prog);
9541
9542 if (old_prog)
9543 bpf_prog_put(old_prog);
9544
9545 return 0;
9546}
9547
9548/**
9549 * i40e_xdp - implements ndo_xdp for i40e
9550 * @dev: netdevice
9551 * @xdp: XDP command
9552 **/
9553static int i40e_xdp(struct net_device *dev,
9554 struct netdev_xdp *xdp)
9555{
9556 struct i40e_netdev_priv *np = netdev_priv(dev);
9557 struct i40e_vsi *vsi = np->vsi;
9558
9559 if (vsi->type != I40E_VSI_MAIN)
9560 return -EINVAL;
9561
9562 switch (xdp->command) {
9563 case XDP_SETUP_PROG:
9564 return i40e_xdp_setup(vsi, xdp->prog);
9565 case XDP_QUERY_PROG:
9566 xdp->prog_attached = i40e_enabled_xdp_vsi(vsi);
Daniel Borkmanneb230392017-06-24 21:13:52 +02009567 xdp->prog_id = vsi->xdp_prog ? vsi->xdp_prog->aux->id : 0;
Björn Töpel0c8493d2017-05-24 07:55:34 +02009568 return 0;
9569 default:
9570 return -EINVAL;
9571 }
9572}
9573
Shannon Nelson37a29732015-02-27 09:15:19 +00009574static const struct net_device_ops i40e_netdev_ops = {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009575 .ndo_open = i40e_open,
9576 .ndo_stop = i40e_close,
9577 .ndo_start_xmit = i40e_lan_xmit_frame,
9578 .ndo_get_stats64 = i40e_get_netdev_stats_struct,
9579 .ndo_set_rx_mode = i40e_set_rx_mode,
9580 .ndo_validate_addr = eth_validate_addr,
9581 .ndo_set_mac_address = i40e_set_mac,
9582 .ndo_change_mtu = i40e_change_mtu,
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00009583 .ndo_do_ioctl = i40e_ioctl,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009584 .ndo_tx_timeout = i40e_tx_timeout,
9585 .ndo_vlan_rx_add_vid = i40e_vlan_rx_add_vid,
9586 .ndo_vlan_rx_kill_vid = i40e_vlan_rx_kill_vid,
9587#ifdef CONFIG_NET_POLL_CONTROLLER
9588 .ndo_poll_controller = i40e_netpoll,
9589#endif
John Fastabende4c67342016-02-16 21:16:15 -08009590 .ndo_setup_tc = __i40e_setup_tc,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009591 .ndo_set_features = i40e_set_features,
9592 .ndo_set_vf_mac = i40e_ndo_set_vf_mac,
9593 .ndo_set_vf_vlan = i40e_ndo_set_vf_port_vlan,
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04009594 .ndo_set_vf_rate = i40e_ndo_set_vf_bw,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009595 .ndo_get_vf_config = i40e_ndo_get_vf_config,
Mitch Williams588aefa2014-02-11 08:27:49 +00009596 .ndo_set_vf_link_state = i40e_ndo_set_vf_link_state,
Serey Konge6d90042014-07-12 07:28:14 +00009597 .ndo_set_vf_spoofchk = i40e_ndo_set_vf_spoofchk,
Anjali Singhai Jainc3bbbd22016-04-01 03:56:07 -07009598 .ndo_set_vf_trust = i40e_ndo_set_vf_trust,
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009599 .ndo_udp_tunnel_add = i40e_udp_tunnel_add,
9600 .ndo_udp_tunnel_del = i40e_udp_tunnel_del,
Neerav Parikh1f224ad2014-02-12 01:45:31 +00009601 .ndo_get_phys_port_id = i40e_get_phys_port_id,
Greg Rose4ba0dea2014-03-06 08:59:55 +00009602 .ndo_fdb_add = i40e_ndo_fdb_add,
Joe Stringerf44a75e2015-04-14 17:09:14 -07009603 .ndo_features_check = i40e_features_check,
Neerav Parikh51616012015-02-06 08:52:14 +00009604 .ndo_bridge_getlink = i40e_ndo_bridge_getlink,
9605 .ndo_bridge_setlink = i40e_ndo_bridge_setlink,
Björn Töpel0c8493d2017-05-24 07:55:34 +02009606 .ndo_xdp = i40e_xdp,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009607};
9608
9609/**
9610 * i40e_config_netdev - Setup the netdev flags
9611 * @vsi: the VSI being configured
9612 *
9613 * Returns 0 on success, negative value on failure
9614 **/
9615static int i40e_config_netdev(struct i40e_vsi *vsi)
9616{
9617 struct i40e_pf *pf = vsi->back;
9618 struct i40e_hw *hw = &pf->hw;
9619 struct i40e_netdev_priv *np;
9620 struct net_device *netdev;
Jacob Keller435c0842016-11-08 13:05:10 -08009621 u8 broadcast[ETH_ALEN];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009622 u8 mac_addr[ETH_ALEN];
9623 int etherdev_size;
Preethi Banalabacd75c2017-03-27 14:43:18 -07009624 netdev_features_t hw_enc_features;
9625 netdev_features_t hw_features;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009626
9627 etherdev_size = sizeof(struct i40e_netdev_priv);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00009628 netdev = alloc_etherdev_mq(etherdev_size, vsi->alloc_queue_pairs);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009629 if (!netdev)
9630 return -ENOMEM;
9631
9632 vsi->netdev = netdev;
9633 np = netdev_priv(netdev);
9634 np->vsi = vsi;
9635
Preethi Banalabacd75c2017-03-27 14:43:18 -07009636 hw_enc_features = NETIF_F_SG |
9637 NETIF_F_IP_CSUM |
9638 NETIF_F_IPV6_CSUM |
9639 NETIF_F_HIGHDMA |
9640 NETIF_F_SOFT_FEATURES |
9641 NETIF_F_TSO |
9642 NETIF_F_TSO_ECN |
9643 NETIF_F_TSO6 |
9644 NETIF_F_GSO_GRE |
9645 NETIF_F_GSO_GRE_CSUM |
9646 NETIF_F_GSO_PARTIAL |
9647 NETIF_F_GSO_UDP_TUNNEL |
9648 NETIF_F_GSO_UDP_TUNNEL_CSUM |
9649 NETIF_F_SCTP_CRC |
9650 NETIF_F_RXHASH |
9651 NETIF_F_RXCSUM |
9652 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009653
Jacob Kellerd36e41d2017-06-23 04:24:46 -04009654 if (!(pf->hw_features & I40E_HW_OUTER_UDP_CSUM_CAPABLE))
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04009655 netdev->gso_partial_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM;
9656
9657 netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009658
Preethi Banalabacd75c2017-03-27 14:43:18 -07009659 netdev->hw_enc_features |= hw_enc_features;
9660
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009661 /* record features VLANs can make use of */
Preethi Banalabacd75c2017-03-27 14:43:18 -07009662 netdev->vlan_features |= hw_enc_features | NETIF_F_TSO_MANGLEID;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009663
Anjali Singhai Jain2e86a0b2014-04-01 07:11:53 +00009664 if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009665 netdev->hw_features |= NETIF_F_NTUPLE;
Preethi Banalabacd75c2017-03-27 14:43:18 -07009666 hw_features = hw_enc_features |
9667 NETIF_F_HW_VLAN_CTAG_TX |
9668 NETIF_F_HW_VLAN_CTAG_RX;
Anjali Singhai Jain2e86a0b2014-04-01 07:11:53 +00009669
Preethi Banalabacd75c2017-03-27 14:43:18 -07009670 netdev->hw_features |= hw_features;
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009671
Preethi Banalabacd75c2017-03-27 14:43:18 -07009672 netdev->features |= hw_features | NETIF_F_HW_VLAN_CTAG_FILTER;
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04009673 netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009674
9675 if (vsi->type == I40E_VSI_MAIN) {
9676 SET_NETDEV_DEV(netdev, &pf->pdev->dev);
Greg Rose9a173902014-05-22 06:32:02 +00009677 ether_addr_copy(mac_addr, hw->mac.perm_addr);
Jacob Keller41c4c2b2017-04-05 07:50:57 -04009678 /* The following steps are necessary for two reasons. First,
9679 * some older NVM configurations load a default MAC-VLAN
9680 * filter that will accept any tagged packet, and we want to
9681 * replace this with a normal filter. Additionally, it is
9682 * possible our MAC address was provided by the platform using
9683 * Open Firmware or similar.
9684 *
9685 * Thus, we need to remove the default filter and install one
9686 * specific to the MAC address.
Jacob Keller1596b5d2016-11-08 13:05:15 -08009687 */
9688 i40e_rm_default_mac_filter(vsi, mac_addr);
Jacob Keller278e7d02016-10-05 09:30:37 -07009689 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller9569a9a2016-11-11 12:39:37 -08009690 i40e_add_mac_filter(vsi, mac_addr);
Jacob Keller278e7d02016-10-05 09:30:37 -07009691 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009692 } else {
9693 /* relate the VSI_VMDQ name to the VSI_MAIN name */
9694 snprintf(netdev->name, IFNAMSIZ, "%sv%%d",
9695 pf->vsi[pf->lan_vsi]->netdev->name);
9696 random_ether_addr(mac_addr);
Kiran Patil21659032015-09-30 14:09:03 -04009697
Jacob Keller278e7d02016-10-05 09:30:37 -07009698 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller9569a9a2016-11-11 12:39:37 -08009699 i40e_add_mac_filter(vsi, mac_addr);
Jacob Keller278e7d02016-10-05 09:30:37 -07009700 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009701 }
Kiran Patil21659032015-09-30 14:09:03 -04009702
Jacob Keller435c0842016-11-08 13:05:10 -08009703 /* Add the broadcast filter so that we initially will receive
9704 * broadcast packets. Note that when a new VLAN is first added the
9705 * driver will convert all filters marked I40E_VLAN_ANY into VLAN
9706 * specific filters as part of transitioning into "vlan" operation.
9707 * When more VLANs are added, the driver will copy each existing MAC
9708 * filter and add it for the new VLAN.
9709 *
9710 * Broadcast filters are handled specially by
9711 * i40e_sync_filters_subtask, as the driver must to set the broadcast
9712 * promiscuous bit instead of adding this directly as a MAC/VLAN
9713 * filter. The subtask will update the correct broadcast promiscuous
9714 * bits as VLANs become active or inactive.
9715 */
9716 eth_broadcast_addr(broadcast);
9717 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller9569a9a2016-11-11 12:39:37 -08009718 i40e_add_mac_filter(vsi, broadcast);
Jacob Keller435c0842016-11-08 13:05:10 -08009719 spin_unlock_bh(&vsi->mac_filter_hash_lock);
9720
Greg Rose9a173902014-05-22 06:32:02 +00009721 ether_addr_copy(netdev->dev_addr, mac_addr);
9722 ether_addr_copy(netdev->perm_addr, mac_addr);
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009723
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009724 netdev->priv_flags |= IFF_UNICAST_FLT;
9725 netdev->priv_flags |= IFF_SUPP_NOFCS;
9726 /* Setup netdev TC information */
9727 i40e_vsi_config_netdev_tc(vsi, vsi->tc_config.enabled_tc);
9728
9729 netdev->netdev_ops = &i40e_netdev_ops;
9730 netdev->watchdog_timeo = 5 * HZ;
9731 i40e_set_ethtool_ops(netdev);
9732
Jarod Wilson91c527a2016-10-17 15:54:05 -04009733 /* MTU range: 68 - 9706 */
9734 netdev->min_mtu = ETH_MIN_MTU;
Mitch Williams1e3a5fd2017-06-23 04:24:43 -04009735 netdev->max_mtu = I40E_MAX_RXBUFFER - I40E_PACKET_HDR_PAD;
Jarod Wilson91c527a2016-10-17 15:54:05 -04009736
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009737 return 0;
9738}
9739
9740/**
9741 * i40e_vsi_delete - Delete a VSI from the switch
9742 * @vsi: the VSI being removed
9743 *
9744 * Returns 0 on success, negative value on failure
9745 **/
9746static void i40e_vsi_delete(struct i40e_vsi *vsi)
9747{
9748 /* remove default VSI is not allowed */
9749 if (vsi == vsi->back->vsi[vsi->back->lan_vsi])
9750 return;
9751
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009752 i40e_aq_delete_element(&vsi->back->hw, vsi->seid, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009753}
9754
9755/**
Neerav Parikh51616012015-02-06 08:52:14 +00009756 * i40e_is_vsi_uplink_mode_veb - Check if the VSI's uplink bridge mode is VEB
9757 * @vsi: the VSI being queried
9758 *
9759 * Returns 1 if HW bridge mode is VEB and return 0 in case of VEPA mode
9760 **/
9761int i40e_is_vsi_uplink_mode_veb(struct i40e_vsi *vsi)
9762{
9763 struct i40e_veb *veb;
9764 struct i40e_pf *pf = vsi->back;
9765
9766 /* Uplink is not a bridge so default to VEB */
9767 if (vsi->veb_idx == I40E_NO_VEB)
9768 return 1;
9769
9770 veb = pf->veb[vsi->veb_idx];
Akeem G Abodunrin09603ea2015-10-01 14:37:36 -04009771 if (!veb) {
9772 dev_info(&pf->pdev->dev,
9773 "There is no veb associated with the bridge\n");
9774 return -ENOENT;
9775 }
Neerav Parikh51616012015-02-06 08:52:14 +00009776
Akeem G Abodunrin09603ea2015-10-01 14:37:36 -04009777 /* Uplink is a bridge in VEPA mode */
9778 if (veb->bridge_mode & BRIDGE_MODE_VEPA) {
9779 return 0;
9780 } else {
9781 /* Uplink is a bridge in VEB mode */
9782 return 1;
9783 }
9784
9785 /* VEPA is now default bridge, so return 0 */
9786 return 0;
Neerav Parikh51616012015-02-06 08:52:14 +00009787}
9788
9789/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009790 * i40e_add_vsi - Add a VSI to the switch
9791 * @vsi: the VSI being configured
9792 *
9793 * This initializes a VSI context depending on the VSI type to be added and
9794 * passes it down to the add_vsi aq command.
9795 **/
9796static int i40e_add_vsi(struct i40e_vsi *vsi)
9797{
9798 int ret = -ENODEV;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009799 struct i40e_pf *pf = vsi->back;
9800 struct i40e_hw *hw = &pf->hw;
9801 struct i40e_vsi_context ctxt;
Jacob Keller278e7d02016-10-05 09:30:37 -07009802 struct i40e_mac_filter *f;
9803 struct hlist_node *h;
9804 int bkt;
Kiran Patil21659032015-09-30 14:09:03 -04009805
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009806 u8 enabled_tc = 0x1; /* TC0 enabled */
9807 int f_count = 0;
9808
9809 memset(&ctxt, 0, sizeof(ctxt));
9810 switch (vsi->type) {
9811 case I40E_VSI_MAIN:
9812 /* The PF's main VSI is already setup as part of the
9813 * device initialization, so we'll not bother with
9814 * the add_vsi call, but we will retrieve the current
9815 * VSI context.
9816 */
9817 ctxt.seid = pf->main_vsi_seid;
9818 ctxt.pf_num = pf->hw.pf_id;
9819 ctxt.vf_num = 0;
9820 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
9821 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
9822 if (ret) {
9823 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009824 "couldn't get PF vsi config, err %s aq_err %s\n",
9825 i40e_stat_str(&pf->hw, ret),
9826 i40e_aq_str(&pf->hw,
9827 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009828 return -ENOENT;
9829 }
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07009830 vsi->info = ctxt.info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009831 vsi->info.valid_sections = 0;
9832
9833 vsi->seid = ctxt.seid;
9834 vsi->id = ctxt.vsi_number;
9835
9836 enabled_tc = i40e_pf_get_tc_map(pf);
9837
9838 /* MFP mode setup queue map and update VSI */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00009839 if ((pf->flags & I40E_FLAG_MFP_ENABLED) &&
9840 !(pf->hw.func_caps.iscsi)) { /* NIC type PF */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009841 memset(&ctxt, 0, sizeof(ctxt));
9842 ctxt.seid = pf->main_vsi_seid;
9843 ctxt.pf_num = pf->hw.pf_id;
9844 ctxt.vf_num = 0;
9845 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
9846 ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
9847 if (ret) {
9848 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009849 "update vsi failed, err %s aq_err %s\n",
9850 i40e_stat_str(&pf->hw, ret),
9851 i40e_aq_str(&pf->hw,
9852 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009853 ret = -ENOENT;
9854 goto err;
9855 }
9856 /* update the local VSI info queue map */
9857 i40e_vsi_update_queue_map(vsi, &ctxt);
9858 vsi->info.valid_sections = 0;
9859 } else {
9860 /* Default/Main VSI is only enabled for TC0
9861 * reconfigure it to enable all TCs that are
9862 * available on the port in SFP mode.
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00009863 * For MFP case the iSCSI PF would use this
9864 * flow to enable LAN+iSCSI TC.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009865 */
9866 ret = i40e_vsi_config_tc(vsi, enabled_tc);
9867 if (ret) {
9868 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009869 "failed to configure TCs for main VSI tc_map 0x%08x, err %s aq_err %s\n",
9870 enabled_tc,
9871 i40e_stat_str(&pf->hw, ret),
9872 i40e_aq_str(&pf->hw,
9873 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009874 ret = -ENOENT;
9875 }
9876 }
9877 break;
9878
9879 case I40E_VSI_FDIR:
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009880 ctxt.pf_num = hw->pf_id;
9881 ctxt.vf_num = 0;
9882 ctxt.uplink_seid = vsi->uplink_seid;
Neerav Parikh2b18e592015-01-24 09:58:38 +00009883 ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009884 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07009885 if ((pf->flags & I40E_FLAG_VEB_MODE_ENABLED) &&
9886 (i40e_is_vsi_uplink_mode_veb(vsi))) {
Neerav Parikh51616012015-02-06 08:52:14 +00009887 ctxt.info.valid_sections |=
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07009888 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
Neerav Parikh51616012015-02-06 08:52:14 +00009889 ctxt.info.switch_id =
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07009890 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
Neerav Parikh51616012015-02-06 08:52:14 +00009891 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009892 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009893 break;
9894
9895 case I40E_VSI_VMDQ2:
9896 ctxt.pf_num = hw->pf_id;
9897 ctxt.vf_num = 0;
9898 ctxt.uplink_seid = vsi->uplink_seid;
Neerav Parikh2b18e592015-01-24 09:58:38 +00009899 ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009900 ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
9901
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009902 /* This VSI is connected to VEB so the switch_id
9903 * should be set to zero by default.
9904 */
Neerav Parikh51616012015-02-06 08:52:14 +00009905 if (i40e_is_vsi_uplink_mode_veb(vsi)) {
9906 ctxt.info.valid_sections |=
9907 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
9908 ctxt.info.switch_id =
9909 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
9910 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009911
9912 /* Setup the VSI tx/rx queue map for TC0 only for now */
9913 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
9914 break;
9915
9916 case I40E_VSI_SRIOV:
9917 ctxt.pf_num = hw->pf_id;
9918 ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
9919 ctxt.uplink_seid = vsi->uplink_seid;
Neerav Parikh2b18e592015-01-24 09:58:38 +00009920 ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009921 ctxt.flags = I40E_AQ_VSI_TYPE_VF;
9922
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009923 /* This VSI is connected to VEB so the switch_id
9924 * should be set to zero by default.
9925 */
Neerav Parikh51616012015-02-06 08:52:14 +00009926 if (i40e_is_vsi_uplink_mode_veb(vsi)) {
9927 ctxt.info.valid_sections |=
9928 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
9929 ctxt.info.switch_id =
9930 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
9931 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009932
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06009933 if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
9934 ctxt.info.valid_sections |=
9935 cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
9936 ctxt.info.queueing_opt_flags |=
Ashish Shah4b28cdb2016-05-03 15:13:17 -07009937 (I40E_AQ_VSI_QUE_OPT_TCP_ENA |
9938 I40E_AQ_VSI_QUE_OPT_RSS_LUT_VSI);
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06009939 }
9940
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009941 ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
9942 ctxt.info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_MODE_ALL;
Mitch Williamsc674d122014-05-20 08:01:40 +00009943 if (pf->vf[vsi->vf_id].spoofchk) {
9944 ctxt.info.valid_sections |=
9945 cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
9946 ctxt.info.sec_flags |=
9947 (I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
9948 I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
9949 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009950 /* Setup the VSI tx/rx queue map for TC0 only for now */
9951 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
9952 break;
9953
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06009954 case I40E_VSI_IWARP:
9955 /* send down message to iWARP */
9956 break;
9957
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009958 default:
9959 return -ENODEV;
9960 }
9961
9962 if (vsi->type != I40E_VSI_MAIN) {
9963 ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
9964 if (ret) {
9965 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009966 "add vsi failed, err %s aq_err %s\n",
9967 i40e_stat_str(&pf->hw, ret),
9968 i40e_aq_str(&pf->hw,
9969 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009970 ret = -ENOENT;
9971 goto err;
9972 }
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07009973 vsi->info = ctxt.info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009974 vsi->info.valid_sections = 0;
9975 vsi->seid = ctxt.seid;
9976 vsi->id = ctxt.vsi_number;
9977 }
9978
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07009979 vsi->active_filters = 0;
Jacob Keller0da36b92017-04-19 09:25:55 -04009980 clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
Jacob Keller278e7d02016-10-05 09:30:37 -07009981 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009982 /* If macvlan filters already exist, force them to get loaded */
Jacob Keller278e7d02016-10-05 09:30:37 -07009983 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07009984 f->state = I40E_FILTER_NEW;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009985 f_count++;
9986 }
Jacob Keller278e7d02016-10-05 09:30:37 -07009987 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -04009988
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009989 if (f_count) {
9990 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
9991 pf->flags |= I40E_FLAG_FILTER_SYNC;
9992 }
9993
9994 /* Update VSI BW information */
9995 ret = i40e_vsi_get_bw_info(vsi);
9996 if (ret) {
9997 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009998 "couldn't get vsi bw info, err %s aq_err %s\n",
9999 i40e_stat_str(&pf->hw, ret),
10000 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010001 /* VSI is already added so not tearing that up */
10002 ret = 0;
10003 }
10004
10005err:
10006 return ret;
10007}
10008
10009/**
10010 * i40e_vsi_release - Delete a VSI and free its resources
10011 * @vsi: the VSI being removed
10012 *
10013 * Returns 0 on success or < 0 on error
10014 **/
10015int i40e_vsi_release(struct i40e_vsi *vsi)
10016{
Jacob Keller278e7d02016-10-05 09:30:37 -070010017 struct i40e_mac_filter *f;
10018 struct hlist_node *h;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010019 struct i40e_veb *veb = NULL;
10020 struct i40e_pf *pf;
10021 u16 uplink_seid;
Jacob Keller278e7d02016-10-05 09:30:37 -070010022 int i, n, bkt;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010023
10024 pf = vsi->back;
10025
10026 /* release of a VEB-owner or last VSI is not allowed */
10027 if (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) {
10028 dev_info(&pf->pdev->dev, "VSI %d has existing VEB %d\n",
10029 vsi->seid, vsi->uplink_seid);
10030 return -ENODEV;
10031 }
10032 if (vsi == pf->vsi[pf->lan_vsi] &&
Mauro S. M. Rodrigues9e6c9c02017-05-12 23:26:56 -030010033 !test_bit(__I40E_DOWN, pf->state)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010034 dev_info(&pf->pdev->dev, "Can't remove PF VSI\n");
10035 return -ENODEV;
10036 }
10037
10038 uplink_seid = vsi->uplink_seid;
10039 if (vsi->type != I40E_VSI_SRIOV) {
10040 if (vsi->netdev_registered) {
10041 vsi->netdev_registered = false;
10042 if (vsi->netdev) {
10043 /* results in a call to i40e_close() */
10044 unregister_netdev(vsi->netdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010045 }
10046 } else {
Shannon Nelson90ef8d42014-03-14 07:32:26 +000010047 i40e_vsi_close(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010048 }
10049 i40e_vsi_disable_irq(vsi);
10050 }
10051
Jacob Keller278e7d02016-10-05 09:30:37 -070010052 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller6622f5c2016-10-05 09:30:32 -070010053
10054 /* clear the sync flag on all filters */
10055 if (vsi->netdev) {
10056 __dev_uc_unsync(vsi->netdev, NULL);
10057 __dev_mc_unsync(vsi->netdev, NULL);
10058 }
10059
10060 /* make sure any remaining filters are marked for deletion */
Jacob Keller278e7d02016-10-05 09:30:37 -070010061 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
Jacob Keller290d2552016-10-05 09:30:36 -070010062 __i40e_del_filter(vsi, f);
Jacob Keller6622f5c2016-10-05 09:30:32 -070010063
Jacob Keller278e7d02016-10-05 09:30:37 -070010064 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -040010065
Jesse Brandeburg17652c62015-11-05 17:01:02 -080010066 i40e_sync_vsi_filters(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010067
10068 i40e_vsi_delete(vsi);
10069 i40e_vsi_free_q_vectors(vsi);
Shannon Nelsona4866592014-02-11 08:24:07 +000010070 if (vsi->netdev) {
10071 free_netdev(vsi->netdev);
10072 vsi->netdev = NULL;
10073 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010074 i40e_vsi_clear_rings(vsi);
10075 i40e_vsi_clear(vsi);
10076
10077 /* If this was the last thing on the VEB, except for the
10078 * controlling VSI, remove the VEB, which puts the controlling
10079 * VSI onto the next level down in the switch.
10080 *
10081 * Well, okay, there's one more exception here: don't remove
10082 * the orphan VEBs yet. We'll wait for an explicit remove request
10083 * from up the network stack.
10084 */
Mitch Williams505682c2014-05-20 08:01:37 +000010085 for (n = 0, i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010086 if (pf->vsi[i] &&
10087 pf->vsi[i]->uplink_seid == uplink_seid &&
10088 (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
10089 n++; /* count the VSIs */
10090 }
10091 }
10092 for (i = 0; i < I40E_MAX_VEB; i++) {
10093 if (!pf->veb[i])
10094 continue;
10095 if (pf->veb[i]->uplink_seid == uplink_seid)
10096 n++; /* count the VEBs */
10097 if (pf->veb[i]->seid == uplink_seid)
10098 veb = pf->veb[i];
10099 }
10100 if (n == 0 && veb && veb->uplink_seid != 0)
10101 i40e_veb_release(veb);
10102
10103 return 0;
10104}
10105
10106/**
10107 * i40e_vsi_setup_vectors - Set up the q_vectors for the given VSI
10108 * @vsi: ptr to the VSI
10109 *
10110 * This should only be called after i40e_vsi_mem_alloc() which allocates the
10111 * corresponding SW VSI structure and initializes num_queue_pairs for the
10112 * newly allocated VSI.
10113 *
10114 * Returns 0 on success or negative on failure
10115 **/
10116static int i40e_vsi_setup_vectors(struct i40e_vsi *vsi)
10117{
10118 int ret = -ENOENT;
10119 struct i40e_pf *pf = vsi->back;
10120
Alexander Duyck493fb302013-09-28 07:01:44 +000010121 if (vsi->q_vectors[0]) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010122 dev_info(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
10123 vsi->seid);
10124 return -EEXIST;
10125 }
10126
10127 if (vsi->base_vector) {
Jesse Brandeburgf29eaa32014-02-11 08:24:12 +000010128 dev_info(&pf->pdev->dev, "VSI %d has non-zero base vector %d\n",
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010129 vsi->seid, vsi->base_vector);
10130 return -EEXIST;
10131 }
10132
Greg Rose90e04072014-03-06 08:59:57 +000010133 ret = i40e_vsi_alloc_q_vectors(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010134 if (ret) {
10135 dev_info(&pf->pdev->dev,
10136 "failed to allocate %d q_vector for VSI %d, ret=%d\n",
10137 vsi->num_q_vectors, vsi->seid, ret);
10138 vsi->num_q_vectors = 0;
10139 goto vector_setup_out;
10140 }
10141
Anjali Singhai Jain26cdc442015-07-10 19:36:00 -040010142 /* In Legacy mode, we do not have to get any other vector since we
10143 * piggyback on the misc/ICR0 for queue interrupts.
10144 */
10145 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
10146 return ret;
Shannon Nelson958a3e32013-09-28 07:13:28 +000010147 if (vsi->num_q_vectors)
10148 vsi->base_vector = i40e_get_lump(pf, pf->irq_pile,
10149 vsi->num_q_vectors, vsi->idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010150 if (vsi->base_vector < 0) {
10151 dev_info(&pf->pdev->dev,
Shannon Nelson049a2be2014-10-17 03:14:50 +000010152 "failed to get tracking for %d vectors for VSI %d, err=%d\n",
10153 vsi->num_q_vectors, vsi->seid, vsi->base_vector);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010154 i40e_vsi_free_q_vectors(vsi);
10155 ret = -ENOENT;
10156 goto vector_setup_out;
10157 }
10158
10159vector_setup_out:
10160 return ret;
10161}
10162
10163/**
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010164 * i40e_vsi_reinit_setup - return and reallocate resources for a VSI
10165 * @vsi: pointer to the vsi.
10166 *
10167 * This re-allocates a vsi's queue resources.
10168 *
10169 * Returns pointer to the successfully allocated and configured VSI sw struct
10170 * on success, otherwise returns NULL on failure.
10171 **/
10172static struct i40e_vsi *i40e_vsi_reinit_setup(struct i40e_vsi *vsi)
10173{
Björn Töpel74608d12017-05-24 07:55:35 +020010174 u16 alloc_queue_pairs;
John Underwoodf5340392016-02-18 09:19:24 -080010175 struct i40e_pf *pf;
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010176 u8 enabled_tc;
10177 int ret;
10178
John Underwoodf5340392016-02-18 09:19:24 -080010179 if (!vsi)
10180 return NULL;
10181
10182 pf = vsi->back;
10183
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010184 i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
10185 i40e_vsi_clear_rings(vsi);
10186
10187 i40e_vsi_free_arrays(vsi, false);
10188 i40e_set_num_rings_in_vsi(vsi);
10189 ret = i40e_vsi_alloc_arrays(vsi, false);
10190 if (ret)
10191 goto err_vsi;
10192
Björn Töpel74608d12017-05-24 07:55:35 +020010193 alloc_queue_pairs = vsi->alloc_queue_pairs *
10194 (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
10195
10196 ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010197 if (ret < 0) {
Shannon Nelson049a2be2014-10-17 03:14:50 +000010198 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010199 "failed to get tracking for %d queues for VSI %d err %d\n",
Björn Töpel74608d12017-05-24 07:55:35 +020010200 alloc_queue_pairs, vsi->seid, ret);
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010201 goto err_vsi;
10202 }
10203 vsi->base_queue = ret;
10204
10205 /* Update the FW view of the VSI. Force a reset of TC and queue
10206 * layout configurations.
10207 */
10208 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
10209 pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
10210 pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
10211 i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
Jacob Keller1596b5d2016-11-08 13:05:15 -080010212 if (vsi->type == I40E_VSI_MAIN)
10213 i40e_rm_default_mac_filter(vsi, pf->hw.mac.perm_addr);
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010214
10215 /* assign it some queues */
10216 ret = i40e_alloc_rings(vsi);
10217 if (ret)
10218 goto err_rings;
10219
10220 /* map all of the rings to the q_vectors */
10221 i40e_vsi_map_rings_to_vectors(vsi);
10222 return vsi;
10223
10224err_rings:
10225 i40e_vsi_free_q_vectors(vsi);
10226 if (vsi->netdev_registered) {
10227 vsi->netdev_registered = false;
10228 unregister_netdev(vsi->netdev);
10229 free_netdev(vsi->netdev);
10230 vsi->netdev = NULL;
10231 }
10232 i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
10233err_vsi:
10234 i40e_vsi_clear(vsi);
10235 return NULL;
10236}
10237
10238/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010239 * i40e_vsi_setup - Set up a VSI by a given type
10240 * @pf: board private structure
10241 * @type: VSI type
10242 * @uplink_seid: the switch element to link to
10243 * @param1: usage depends upon VSI type. For VF types, indicates VF id
10244 *
10245 * This allocates the sw VSI structure and its queue resources, then add a VSI
10246 * to the identified VEB.
10247 *
10248 * Returns pointer to the successfully allocated and configure VSI sw struct on
10249 * success, otherwise returns NULL on failure.
10250 **/
10251struct i40e_vsi *i40e_vsi_setup(struct i40e_pf *pf, u8 type,
10252 u16 uplink_seid, u32 param1)
10253{
10254 struct i40e_vsi *vsi = NULL;
10255 struct i40e_veb *veb = NULL;
Björn Töpel74608d12017-05-24 07:55:35 +020010256 u16 alloc_queue_pairs;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010257 int ret, i;
10258 int v_idx;
10259
10260 /* The requested uplink_seid must be either
10261 * - the PF's port seid
10262 * no VEB is needed because this is the PF
10263 * or this is a Flow Director special case VSI
10264 * - seid of an existing VEB
10265 * - seid of a VSI that owns an existing VEB
10266 * - seid of a VSI that doesn't own a VEB
10267 * a new VEB is created and the VSI becomes the owner
10268 * - seid of the PF VSI, which is what creates the first VEB
10269 * this is a special case of the previous
10270 *
10271 * Find which uplink_seid we were given and create a new VEB if needed
10272 */
10273 for (i = 0; i < I40E_MAX_VEB; i++) {
10274 if (pf->veb[i] && pf->veb[i]->seid == uplink_seid) {
10275 veb = pf->veb[i];
10276 break;
10277 }
10278 }
10279
10280 if (!veb && uplink_seid != pf->mac_seid) {
10281
Mitch Williams505682c2014-05-20 08:01:37 +000010282 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010283 if (pf->vsi[i] && pf->vsi[i]->seid == uplink_seid) {
10284 vsi = pf->vsi[i];
10285 break;
10286 }
10287 }
10288 if (!vsi) {
10289 dev_info(&pf->pdev->dev, "no such uplink_seid %d\n",
10290 uplink_seid);
10291 return NULL;
10292 }
10293
10294 if (vsi->uplink_seid == pf->mac_seid)
10295 veb = i40e_veb_setup(pf, 0, pf->mac_seid, vsi->seid,
10296 vsi->tc_config.enabled_tc);
10297 else if ((vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
10298 veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
10299 vsi->tc_config.enabled_tc);
Anjali Singhai Jain79c21a82014-11-13 03:06:14 +000010300 if (veb) {
10301 if (vsi->seid != pf->vsi[pf->lan_vsi]->seid) {
10302 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -040010303 "New VSI creation error, uplink seid of LAN VSI expected.\n");
Anjali Singhai Jain79c21a82014-11-13 03:06:14 +000010304 return NULL;
10305 }
Anjali Singhai Jainfa11cb32015-05-27 12:06:14 -040010306 /* We come up by default in VEPA mode if SRIOV is not
10307 * already enabled, in which case we can't force VEPA
10308 * mode.
10309 */
10310 if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
10311 veb->bridge_mode = BRIDGE_MODE_VEPA;
10312 pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
10313 }
Neerav Parikh51616012015-02-06 08:52:14 +000010314 i40e_config_bridge_mode(veb);
Anjali Singhai Jain79c21a82014-11-13 03:06:14 +000010315 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010316 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
10317 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
10318 veb = pf->veb[i];
10319 }
10320 if (!veb) {
10321 dev_info(&pf->pdev->dev, "couldn't add VEB\n");
10322 return NULL;
10323 }
10324
10325 vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
10326 uplink_seid = veb->seid;
10327 }
10328
10329 /* get vsi sw struct */
10330 v_idx = i40e_vsi_mem_alloc(pf, type);
10331 if (v_idx < 0)
10332 goto err_alloc;
10333 vsi = pf->vsi[v_idx];
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010334 if (!vsi)
10335 goto err_alloc;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010336 vsi->type = type;
10337 vsi->veb_idx = (veb ? veb->idx : I40E_NO_VEB);
10338
10339 if (type == I40E_VSI_MAIN)
10340 pf->lan_vsi = v_idx;
10341 else if (type == I40E_VSI_SRIOV)
10342 vsi->vf_id = param1;
10343 /* assign it some queues */
Björn Töpel74608d12017-05-24 07:55:35 +020010344 alloc_queue_pairs = vsi->alloc_queue_pairs *
10345 (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
10346
10347 ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010348 if (ret < 0) {
Shannon Nelson049a2be2014-10-17 03:14:50 +000010349 dev_info(&pf->pdev->dev,
10350 "failed to get tracking for %d queues for VSI %d err=%d\n",
Björn Töpel74608d12017-05-24 07:55:35 +020010351 alloc_queue_pairs, vsi->seid, ret);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010352 goto err_vsi;
10353 }
10354 vsi->base_queue = ret;
10355
10356 /* get a VSI from the hardware */
10357 vsi->uplink_seid = uplink_seid;
10358 ret = i40e_add_vsi(vsi);
10359 if (ret)
10360 goto err_vsi;
10361
10362 switch (vsi->type) {
10363 /* setup the netdev if needed */
10364 case I40E_VSI_MAIN:
10365 case I40E_VSI_VMDQ2:
10366 ret = i40e_config_netdev(vsi);
10367 if (ret)
10368 goto err_netdev;
10369 ret = register_netdev(vsi->netdev);
10370 if (ret)
10371 goto err_netdev;
10372 vsi->netdev_registered = true;
10373 netif_carrier_off(vsi->netdev);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -080010374#ifdef CONFIG_I40E_DCB
10375 /* Setup DCB netlink interface */
10376 i40e_dcbnl_setup(vsi);
10377#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010378 /* fall through */
10379
10380 case I40E_VSI_FDIR:
10381 /* set up vectors and rings if needed */
10382 ret = i40e_vsi_setup_vectors(vsi);
10383 if (ret)
10384 goto err_msix;
10385
10386 ret = i40e_alloc_rings(vsi);
10387 if (ret)
10388 goto err_rings;
10389
10390 /* map all of the rings to the q_vectors */
10391 i40e_vsi_map_rings_to_vectors(vsi);
10392
10393 i40e_vsi_reset_stats(vsi);
10394 break;
10395
10396 default:
10397 /* no netdev or rings for the other VSI types */
10398 break;
10399 }
10400
Jacob Kellerd36e41d2017-06-23 04:24:46 -040010401 if ((pf->hw_features & I40E_HW_RSS_AQ_CAPABLE) &&
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -040010402 (vsi->type == I40E_VSI_VMDQ2)) {
10403 ret = i40e_vsi_config_rss(vsi);
10404 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010405 return vsi;
10406
10407err_rings:
10408 i40e_vsi_free_q_vectors(vsi);
10409err_msix:
10410 if (vsi->netdev_registered) {
10411 vsi->netdev_registered = false;
10412 unregister_netdev(vsi->netdev);
10413 free_netdev(vsi->netdev);
10414 vsi->netdev = NULL;
10415 }
10416err_netdev:
10417 i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
10418err_vsi:
10419 i40e_vsi_clear(vsi);
10420err_alloc:
10421 return NULL;
10422}
10423
10424/**
10425 * i40e_veb_get_bw_info - Query VEB BW information
10426 * @veb: the veb to query
10427 *
10428 * Query the Tx scheduler BW configuration data for given VEB
10429 **/
10430static int i40e_veb_get_bw_info(struct i40e_veb *veb)
10431{
10432 struct i40e_aqc_query_switching_comp_ets_config_resp ets_data;
10433 struct i40e_aqc_query_switching_comp_bw_config_resp bw_data;
10434 struct i40e_pf *pf = veb->pf;
10435 struct i40e_hw *hw = &pf->hw;
10436 u32 tc_bw_max;
10437 int ret = 0;
10438 int i;
10439
10440 ret = i40e_aq_query_switch_comp_bw_config(hw, veb->seid,
10441 &bw_data, NULL);
10442 if (ret) {
10443 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010444 "query veb bw config failed, err %s aq_err %s\n",
10445 i40e_stat_str(&pf->hw, ret),
10446 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010447 goto out;
10448 }
10449
10450 ret = i40e_aq_query_switch_comp_ets_config(hw, veb->seid,
10451 &ets_data, NULL);
10452 if (ret) {
10453 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010454 "query veb bw ets config failed, err %s aq_err %s\n",
10455 i40e_stat_str(&pf->hw, ret),
10456 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010457 goto out;
10458 }
10459
10460 veb->bw_limit = le16_to_cpu(ets_data.port_bw_limit);
10461 veb->bw_max_quanta = ets_data.tc_bw_max;
10462 veb->is_abs_credits = bw_data.absolute_credits_enable;
Neerav Parikh23cd1f02014-11-12 00:18:41 +000010463 veb->enabled_tc = ets_data.tc_valid_bits;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010464 tc_bw_max = le16_to_cpu(bw_data.tc_bw_max[0]) |
10465 (le16_to_cpu(bw_data.tc_bw_max[1]) << 16);
10466 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
10467 veb->bw_tc_share_credits[i] = bw_data.tc_bw_share_credits[i];
10468 veb->bw_tc_limit_credits[i] =
10469 le16_to_cpu(bw_data.tc_bw_limits[i]);
10470 veb->bw_tc_max_quanta[i] = ((tc_bw_max >> (i*4)) & 0x7);
10471 }
10472
10473out:
10474 return ret;
10475}
10476
10477/**
10478 * i40e_veb_mem_alloc - Allocates the next available struct veb in the PF
10479 * @pf: board private structure
10480 *
10481 * On error: returns error code (negative)
10482 * On success: returns vsi index in PF (positive)
10483 **/
10484static int i40e_veb_mem_alloc(struct i40e_pf *pf)
10485{
10486 int ret = -ENOENT;
10487 struct i40e_veb *veb;
10488 int i;
10489
10490 /* Need to protect the allocation of switch elements at the PF level */
10491 mutex_lock(&pf->switch_mutex);
10492
10493 /* VEB list may be fragmented if VEB creation/destruction has
10494 * been happening. We can afford to do a quick scan to look
10495 * for any free slots in the list.
10496 *
10497 * find next empty veb slot, looping back around if necessary
10498 */
10499 i = 0;
10500 while ((i < I40E_MAX_VEB) && (pf->veb[i] != NULL))
10501 i++;
10502 if (i >= I40E_MAX_VEB) {
10503 ret = -ENOMEM;
10504 goto err_alloc_veb; /* out of VEB slots! */
10505 }
10506
10507 veb = kzalloc(sizeof(*veb), GFP_KERNEL);
10508 if (!veb) {
10509 ret = -ENOMEM;
10510 goto err_alloc_veb;
10511 }
10512 veb->pf = pf;
10513 veb->idx = i;
10514 veb->enabled_tc = 1;
10515
10516 pf->veb[i] = veb;
10517 ret = i;
10518err_alloc_veb:
10519 mutex_unlock(&pf->switch_mutex);
10520 return ret;
10521}
10522
10523/**
10524 * i40e_switch_branch_release - Delete a branch of the switch tree
10525 * @branch: where to start deleting
10526 *
10527 * This uses recursion to find the tips of the branch to be
10528 * removed, deleting until we get back to and can delete this VEB.
10529 **/
10530static void i40e_switch_branch_release(struct i40e_veb *branch)
10531{
10532 struct i40e_pf *pf = branch->pf;
10533 u16 branch_seid = branch->seid;
10534 u16 veb_idx = branch->idx;
10535 int i;
10536
10537 /* release any VEBs on this VEB - RECURSION */
10538 for (i = 0; i < I40E_MAX_VEB; i++) {
10539 if (!pf->veb[i])
10540 continue;
10541 if (pf->veb[i]->uplink_seid == branch->seid)
10542 i40e_switch_branch_release(pf->veb[i]);
10543 }
10544
10545 /* Release the VSIs on this VEB, but not the owner VSI.
10546 *
10547 * NOTE: Removing the last VSI on a VEB has the SIDE EFFECT of removing
10548 * the VEB itself, so don't use (*branch) after this loop.
10549 */
Mitch Williams505682c2014-05-20 08:01:37 +000010550 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010551 if (!pf->vsi[i])
10552 continue;
10553 if (pf->vsi[i]->uplink_seid == branch_seid &&
10554 (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
10555 i40e_vsi_release(pf->vsi[i]);
10556 }
10557 }
10558
10559 /* There's one corner case where the VEB might not have been
10560 * removed, so double check it here and remove it if needed.
10561 * This case happens if the veb was created from the debugfs
10562 * commands and no VSIs were added to it.
10563 */
10564 if (pf->veb[veb_idx])
10565 i40e_veb_release(pf->veb[veb_idx]);
10566}
10567
10568/**
10569 * i40e_veb_clear - remove veb struct
10570 * @veb: the veb to remove
10571 **/
10572static void i40e_veb_clear(struct i40e_veb *veb)
10573{
10574 if (!veb)
10575 return;
10576
10577 if (veb->pf) {
10578 struct i40e_pf *pf = veb->pf;
10579
10580 mutex_lock(&pf->switch_mutex);
10581 if (pf->veb[veb->idx] == veb)
10582 pf->veb[veb->idx] = NULL;
10583 mutex_unlock(&pf->switch_mutex);
10584 }
10585
10586 kfree(veb);
10587}
10588
10589/**
10590 * i40e_veb_release - Delete a VEB and free its resources
10591 * @veb: the VEB being removed
10592 **/
10593void i40e_veb_release(struct i40e_veb *veb)
10594{
10595 struct i40e_vsi *vsi = NULL;
10596 struct i40e_pf *pf;
10597 int i, n = 0;
10598
10599 pf = veb->pf;
10600
10601 /* find the remaining VSI and check for extras */
Mitch Williams505682c2014-05-20 08:01:37 +000010602 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010603 if (pf->vsi[i] && pf->vsi[i]->uplink_seid == veb->seid) {
10604 n++;
10605 vsi = pf->vsi[i];
10606 }
10607 }
10608 if (n != 1) {
10609 dev_info(&pf->pdev->dev,
10610 "can't remove VEB %d with %d VSIs left\n",
10611 veb->seid, n);
10612 return;
10613 }
10614
10615 /* move the remaining VSI to uplink veb */
10616 vsi->flags &= ~I40E_VSI_FLAG_VEB_OWNER;
10617 if (veb->uplink_seid) {
10618 vsi->uplink_seid = veb->uplink_seid;
10619 if (veb->uplink_seid == pf->mac_seid)
10620 vsi->veb_idx = I40E_NO_VEB;
10621 else
10622 vsi->veb_idx = veb->veb_idx;
10623 } else {
10624 /* floating VEB */
10625 vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
10626 vsi->veb_idx = pf->vsi[pf->lan_vsi]->veb_idx;
10627 }
10628
10629 i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
10630 i40e_veb_clear(veb);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010631}
10632
10633/**
10634 * i40e_add_veb - create the VEB in the switch
10635 * @veb: the VEB to be instantiated
10636 * @vsi: the controlling VSI
10637 **/
10638static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi)
10639{
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010640 struct i40e_pf *pf = veb->pf;
Shannon Nelson66fc3602016-01-13 16:51:42 -080010641 bool enable_stats = !!(pf->flags & I40E_FLAG_VEB_STATS_ENABLED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010642 int ret;
10643
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010644 ret = i40e_aq_add_veb(&pf->hw, veb->uplink_seid, vsi->seid,
Mitch Williams5bc16032016-05-16 10:26:43 -070010645 veb->enabled_tc, false,
Shannon Nelson66fc3602016-01-13 16:51:42 -080010646 &veb->seid, enable_stats, NULL);
Mitch Williams5bc16032016-05-16 10:26:43 -070010647
10648 /* get a VEB from the hardware */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010649 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010650 dev_info(&pf->pdev->dev,
10651 "couldn't add VEB, err %s aq_err %s\n",
10652 i40e_stat_str(&pf->hw, ret),
10653 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010654 return -EPERM;
10655 }
10656
10657 /* get statistics counter */
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010658 ret = i40e_aq_get_veb_parameters(&pf->hw, veb->seid, NULL, NULL,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010659 &veb->stats_idx, NULL, NULL, NULL);
10660 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010661 dev_info(&pf->pdev->dev,
10662 "couldn't get VEB statistics idx, err %s aq_err %s\n",
10663 i40e_stat_str(&pf->hw, ret),
10664 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010665 return -EPERM;
10666 }
10667 ret = i40e_veb_get_bw_info(veb);
10668 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010669 dev_info(&pf->pdev->dev,
10670 "couldn't get VEB bw info, err %s aq_err %s\n",
10671 i40e_stat_str(&pf->hw, ret),
10672 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10673 i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010674 return -ENOENT;
10675 }
10676
10677 vsi->uplink_seid = veb->seid;
10678 vsi->veb_idx = veb->idx;
10679 vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
10680
10681 return 0;
10682}
10683
10684/**
10685 * i40e_veb_setup - Set up a VEB
10686 * @pf: board private structure
10687 * @flags: VEB setup flags
10688 * @uplink_seid: the switch element to link to
10689 * @vsi_seid: the initial VSI seid
10690 * @enabled_tc: Enabled TC bit-map
10691 *
10692 * This allocates the sw VEB structure and links it into the switch
10693 * It is possible and legal for this to be a duplicate of an already
10694 * existing VEB. It is also possible for both uplink and vsi seids
10695 * to be zero, in order to create a floating VEB.
10696 *
10697 * Returns pointer to the successfully allocated VEB sw struct on
10698 * success, otherwise returns NULL on failure.
10699 **/
10700struct i40e_veb *i40e_veb_setup(struct i40e_pf *pf, u16 flags,
10701 u16 uplink_seid, u16 vsi_seid,
10702 u8 enabled_tc)
10703{
10704 struct i40e_veb *veb, *uplink_veb = NULL;
10705 int vsi_idx, veb_idx;
10706 int ret;
10707
10708 /* if one seid is 0, the other must be 0 to create a floating relay */
10709 if ((uplink_seid == 0 || vsi_seid == 0) &&
10710 (uplink_seid + vsi_seid != 0)) {
10711 dev_info(&pf->pdev->dev,
10712 "one, not both seid's are 0: uplink=%d vsi=%d\n",
10713 uplink_seid, vsi_seid);
10714 return NULL;
10715 }
10716
10717 /* make sure there is such a vsi and uplink */
Mitch Williams505682c2014-05-20 08:01:37 +000010718 for (vsi_idx = 0; vsi_idx < pf->num_alloc_vsi; vsi_idx++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010719 if (pf->vsi[vsi_idx] && pf->vsi[vsi_idx]->seid == vsi_seid)
10720 break;
Mitch Williams505682c2014-05-20 08:01:37 +000010721 if (vsi_idx >= pf->num_alloc_vsi && vsi_seid != 0) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010722 dev_info(&pf->pdev->dev, "vsi seid %d not found\n",
10723 vsi_seid);
10724 return NULL;
10725 }
10726
10727 if (uplink_seid && uplink_seid != pf->mac_seid) {
10728 for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
10729 if (pf->veb[veb_idx] &&
10730 pf->veb[veb_idx]->seid == uplink_seid) {
10731 uplink_veb = pf->veb[veb_idx];
10732 break;
10733 }
10734 }
10735 if (!uplink_veb) {
10736 dev_info(&pf->pdev->dev,
10737 "uplink seid %d not found\n", uplink_seid);
10738 return NULL;
10739 }
10740 }
10741
10742 /* get veb sw struct */
10743 veb_idx = i40e_veb_mem_alloc(pf);
10744 if (veb_idx < 0)
10745 goto err_alloc;
10746 veb = pf->veb[veb_idx];
10747 veb->flags = flags;
10748 veb->uplink_seid = uplink_seid;
10749 veb->veb_idx = (uplink_veb ? uplink_veb->idx : I40E_NO_VEB);
10750 veb->enabled_tc = (enabled_tc ? enabled_tc : 0x1);
10751
10752 /* create the VEB in the switch */
10753 ret = i40e_add_veb(veb, pf->vsi[vsi_idx]);
10754 if (ret)
10755 goto err_veb;
Shannon Nelson1bb8b932014-04-23 04:49:54 +000010756 if (vsi_idx == pf->lan_vsi)
10757 pf->lan_veb = veb->idx;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010758
10759 return veb;
10760
10761err_veb:
10762 i40e_veb_clear(veb);
10763err_alloc:
10764 return NULL;
10765}
10766
10767/**
Jeff Kirsherb40c82e62015-02-27 09:18:34 +000010768 * i40e_setup_pf_switch_element - set PF vars based on switch type
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010769 * @pf: board private structure
10770 * @ele: element we are building info from
10771 * @num_reported: total number of elements
10772 * @printconfig: should we print the contents
10773 *
10774 * helper function to assist in extracting a few useful SEID values.
10775 **/
10776static void i40e_setup_pf_switch_element(struct i40e_pf *pf,
10777 struct i40e_aqc_switch_config_element_resp *ele,
10778 u16 num_reported, bool printconfig)
10779{
10780 u16 downlink_seid = le16_to_cpu(ele->downlink_seid);
10781 u16 uplink_seid = le16_to_cpu(ele->uplink_seid);
10782 u8 element_type = ele->element_type;
10783 u16 seid = le16_to_cpu(ele->seid);
10784
10785 if (printconfig)
10786 dev_info(&pf->pdev->dev,
10787 "type=%d seid=%d uplink=%d downlink=%d\n",
10788 element_type, seid, uplink_seid, downlink_seid);
10789
10790 switch (element_type) {
10791 case I40E_SWITCH_ELEMENT_TYPE_MAC:
10792 pf->mac_seid = seid;
10793 break;
10794 case I40E_SWITCH_ELEMENT_TYPE_VEB:
10795 /* Main VEB? */
10796 if (uplink_seid != pf->mac_seid)
10797 break;
10798 if (pf->lan_veb == I40E_NO_VEB) {
10799 int v;
10800
10801 /* find existing or else empty VEB */
10802 for (v = 0; v < I40E_MAX_VEB; v++) {
10803 if (pf->veb[v] && (pf->veb[v]->seid == seid)) {
10804 pf->lan_veb = v;
10805 break;
10806 }
10807 }
10808 if (pf->lan_veb == I40E_NO_VEB) {
10809 v = i40e_veb_mem_alloc(pf);
10810 if (v < 0)
10811 break;
10812 pf->lan_veb = v;
10813 }
10814 }
10815
10816 pf->veb[pf->lan_veb]->seid = seid;
10817 pf->veb[pf->lan_veb]->uplink_seid = pf->mac_seid;
10818 pf->veb[pf->lan_veb]->pf = pf;
10819 pf->veb[pf->lan_veb]->veb_idx = I40E_NO_VEB;
10820 break;
10821 case I40E_SWITCH_ELEMENT_TYPE_VSI:
10822 if (num_reported != 1)
10823 break;
10824 /* This is immediately after a reset so we can assume this is
10825 * the PF's VSI
10826 */
10827 pf->mac_seid = uplink_seid;
10828 pf->pf_seid = downlink_seid;
10829 pf->main_vsi_seid = seid;
10830 if (printconfig)
10831 dev_info(&pf->pdev->dev,
10832 "pf_seid=%d main_vsi_seid=%d\n",
10833 pf->pf_seid, pf->main_vsi_seid);
10834 break;
10835 case I40E_SWITCH_ELEMENT_TYPE_PF:
10836 case I40E_SWITCH_ELEMENT_TYPE_VF:
10837 case I40E_SWITCH_ELEMENT_TYPE_EMP:
10838 case I40E_SWITCH_ELEMENT_TYPE_BMC:
10839 case I40E_SWITCH_ELEMENT_TYPE_PE:
10840 case I40E_SWITCH_ELEMENT_TYPE_PA:
10841 /* ignore these for now */
10842 break;
10843 default:
10844 dev_info(&pf->pdev->dev, "unknown element type=%d seid=%d\n",
10845 element_type, seid);
10846 break;
10847 }
10848}
10849
10850/**
10851 * i40e_fetch_switch_configuration - Get switch config from firmware
10852 * @pf: board private structure
10853 * @printconfig: should we print the contents
10854 *
10855 * Get the current switch configuration from the device and
10856 * extract a few useful SEID values.
10857 **/
10858int i40e_fetch_switch_configuration(struct i40e_pf *pf, bool printconfig)
10859{
10860 struct i40e_aqc_get_switch_config_resp *sw_config;
10861 u16 next_seid = 0;
10862 int ret = 0;
10863 u8 *aq_buf;
10864 int i;
10865
10866 aq_buf = kzalloc(I40E_AQ_LARGE_BUF, GFP_KERNEL);
10867 if (!aq_buf)
10868 return -ENOMEM;
10869
10870 sw_config = (struct i40e_aqc_get_switch_config_resp *)aq_buf;
10871 do {
10872 u16 num_reported, num_total;
10873
10874 ret = i40e_aq_get_switch_config(&pf->hw, sw_config,
10875 I40E_AQ_LARGE_BUF,
10876 &next_seid, NULL);
10877 if (ret) {
10878 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010879 "get switch config failed err %s aq_err %s\n",
10880 i40e_stat_str(&pf->hw, ret),
10881 i40e_aq_str(&pf->hw,
10882 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010883 kfree(aq_buf);
10884 return -ENOENT;
10885 }
10886
10887 num_reported = le16_to_cpu(sw_config->header.num_reported);
10888 num_total = le16_to_cpu(sw_config->header.num_total);
10889
10890 if (printconfig)
10891 dev_info(&pf->pdev->dev,
10892 "header: %d reported %d total\n",
10893 num_reported, num_total);
10894
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010895 for (i = 0; i < num_reported; i++) {
10896 struct i40e_aqc_switch_config_element_resp *ele =
10897 &sw_config->element[i];
10898
10899 i40e_setup_pf_switch_element(pf, ele, num_reported,
10900 printconfig);
10901 }
10902 } while (next_seid != 0);
10903
10904 kfree(aq_buf);
10905 return ret;
10906}
10907
10908/**
10909 * i40e_setup_pf_switch - Setup the HW switch on startup or after reset
10910 * @pf: board private structure
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010911 * @reinit: if the Main VSI needs to re-initialized.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010912 *
10913 * Returns 0 on success, negative value on failure
10914 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010915static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010916{
Anjali Singhai Jainb5569892016-05-03 15:13:12 -070010917 u16 flags = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010918 int ret;
10919
10920 /* find out what's out there already */
10921 ret = i40e_fetch_switch_configuration(pf, false);
10922 if (ret) {
10923 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010924 "couldn't fetch switch config, err %s aq_err %s\n",
10925 i40e_stat_str(&pf->hw, ret),
10926 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010927 return ret;
10928 }
10929 i40e_pf_reset_stats(pf);
10930
Anjali Singhai Jainb5569892016-05-03 15:13:12 -070010931 /* set the switch config bit for the whole device to
10932 * support limited promisc or true promisc
10933 * when user requests promisc. The default is limited
10934 * promisc.
10935 */
10936
10937 if ((pf->hw.pf_id == 0) &&
10938 !(pf->flags & I40E_FLAG_TRUE_PROMISC_SUPPORT))
10939 flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
10940
10941 if (pf->hw.pf_id == 0) {
10942 u16 valid_flags;
10943
10944 valid_flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
10945 ret = i40e_aq_set_switch_config(&pf->hw, flags, valid_flags,
10946 NULL);
10947 if (ret && pf->hw.aq.asq_last_status != I40E_AQ_RC_ESRCH) {
10948 dev_info(&pf->pdev->dev,
10949 "couldn't set switch config bits, err %s aq_err %s\n",
10950 i40e_stat_str(&pf->hw, ret),
10951 i40e_aq_str(&pf->hw,
10952 pf->hw.aq.asq_last_status));
10953 /* not a fatal problem, just keep going */
10954 }
10955 }
10956
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010957 /* first time setup */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010958 if (pf->lan_vsi == I40E_NO_VSI || reinit) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010959 struct i40e_vsi *vsi = NULL;
10960 u16 uplink_seid;
10961
10962 /* Set up the PF VSI associated with the PF's main VSI
10963 * that is already in the HW switch
10964 */
10965 if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
10966 uplink_seid = pf->veb[pf->lan_veb]->seid;
10967 else
10968 uplink_seid = pf->mac_seid;
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010969 if (pf->lan_vsi == I40E_NO_VSI)
10970 vsi = i40e_vsi_setup(pf, I40E_VSI_MAIN, uplink_seid, 0);
10971 else if (reinit)
10972 vsi = i40e_vsi_reinit_setup(pf->vsi[pf->lan_vsi]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010973 if (!vsi) {
10974 dev_info(&pf->pdev->dev, "setup of MAIN VSI failed\n");
10975 i40e_fdir_teardown(pf);
10976 return -EAGAIN;
10977 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010978 } else {
10979 /* force a reset of TC and queue layout configurations */
10980 u8 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
Jesse Brandeburg6995b362015-08-28 17:55:54 -040010981
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010982 pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
10983 pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
10984 i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
10985 }
10986 i40e_vlan_stripping_disable(pf->vsi[pf->lan_vsi]);
10987
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010988 i40e_fdir_sb_setup(pf);
10989
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010990 /* Setup static PF queue filter control settings */
10991 ret = i40e_setup_pf_filter_control(pf);
10992 if (ret) {
10993 dev_info(&pf->pdev->dev, "setup_pf_filter_control failed: %d\n",
10994 ret);
10995 /* Failure here should not stop continuing other steps */
10996 }
10997
10998 /* enable RSS in the HW, even for only one queue, as the stack can use
10999 * the hash
11000 */
11001 if ((pf->flags & I40E_FLAG_RSS_ENABLED))
Helin Zhang043dd652015-10-21 19:56:23 -040011002 i40e_pf_config_rss(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011003
11004 /* fill in link information and enable LSE reporting */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011005 i40e_link_event(pf);
11006
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +000011007 /* Initialize user-specific link properties */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011008 pf->fc_autoneg_status = ((pf->hw.phy.link_info.an_info &
11009 I40E_AQ_AN_COMPLETED) ? true : false);
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +000011010
Jacob Kellerbeb0dff2014-01-11 05:43:19 +000011011 i40e_ptp_init(pf);
11012
Alexander Duyck1f190d92017-04-19 09:25:51 -040011013 /* repopulate tunnel port filters */
11014 i40e_sync_udp_filters(pf);
11015
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011016 return ret;
11017}
11018
11019/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011020 * i40e_determine_queue_usage - Work out queue distribution
11021 * @pf: board private structure
11022 **/
11023static void i40e_determine_queue_usage(struct i40e_pf *pf)
11024{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011025 int queues_left;
11026
11027 pf->num_lan_qps = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011028
11029 /* Find the max queues to be put into basic use. We'll always be
11030 * using TC0, whether or not DCB is running, and TC0 will get the
11031 * big RSS set.
11032 */
11033 queues_left = pf->hw.func_caps.num_tx_qp;
11034
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080011035 if ((queues_left == 1) ||
Frank Zhang9aa7e932014-05-20 08:01:42 +000011036 !(pf->flags & I40E_FLAG_MSIX_ENABLED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011037 /* one qp for PF, no queues for anything else */
11038 queues_left = 0;
Helin Zhangacd65442015-10-26 19:44:28 -040011039 pf->alloc_rss_size = pf->num_lan_qps = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011040
11041 /* make sure all the fancies are disabled */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080011042 pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060011043 I40E_FLAG_IWARP_ENABLED |
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080011044 I40E_FLAG_FD_SB_ENABLED |
11045 I40E_FLAG_FD_ATR_ENABLED |
Neerav Parikh4d9b6042014-05-22 06:31:51 +000011046 I40E_FLAG_DCB_CAPABLE |
Dave Ertmana0362442016-08-29 17:38:26 -070011047 I40E_FLAG_DCB_ENABLED |
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080011048 I40E_FLAG_SRIOV_ENABLED |
11049 I40E_FLAG_VMDQ_ENABLED);
Frank Zhang9aa7e932014-05-20 08:01:42 +000011050 } else if (!(pf->flags & (I40E_FLAG_RSS_ENABLED |
11051 I40E_FLAG_FD_SB_ENABLED |
Anjali Singhai Jainbbe7d0e2014-05-20 08:01:44 +000011052 I40E_FLAG_FD_ATR_ENABLED |
Neerav Parikh4d9b6042014-05-22 06:31:51 +000011053 I40E_FLAG_DCB_CAPABLE))) {
Frank Zhang9aa7e932014-05-20 08:01:42 +000011054 /* one qp for PF */
Helin Zhangacd65442015-10-26 19:44:28 -040011055 pf->alloc_rss_size = pf->num_lan_qps = 1;
Frank Zhang9aa7e932014-05-20 08:01:42 +000011056 queues_left -= pf->num_lan_qps;
11057
11058 pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060011059 I40E_FLAG_IWARP_ENABLED |
Frank Zhang9aa7e932014-05-20 08:01:42 +000011060 I40E_FLAG_FD_SB_ENABLED |
11061 I40E_FLAG_FD_ATR_ENABLED |
11062 I40E_FLAG_DCB_ENABLED |
11063 I40E_FLAG_VMDQ_ENABLED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011064 } else {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080011065 /* Not enough queues for all TCs */
Neerav Parikh4d9b6042014-05-22 06:31:51 +000011066 if ((pf->flags & I40E_FLAG_DCB_CAPABLE) &&
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080011067 (queues_left < I40E_MAX_TRAFFIC_CLASS)) {
Dave Ertmana0362442016-08-29 17:38:26 -070011068 pf->flags &= ~(I40E_FLAG_DCB_CAPABLE |
11069 I40E_FLAG_DCB_ENABLED);
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080011070 dev_info(&pf->pdev->dev, "not enough queues for DCB. DCB is disabled.\n");
11071 }
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +000011072 pf->num_lan_qps = max_t(int, pf->rss_size_max,
11073 num_online_cpus());
11074 pf->num_lan_qps = min_t(int, pf->num_lan_qps,
11075 pf->hw.func_caps.num_tx_qp);
11076
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080011077 queues_left -= pf->num_lan_qps;
11078 }
11079
11080 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
11081 if (queues_left > 1) {
11082 queues_left -= 1; /* save 1 queue for FD */
11083 } else {
11084 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
11085 dev_info(&pf->pdev->dev, "not enough queues for Flow Director. Flow Director feature is disabled\n");
11086 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011087 }
11088
11089 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
11090 pf->num_vf_qps && pf->num_req_vfs && queues_left) {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080011091 pf->num_req_vfs = min_t(int, pf->num_req_vfs,
11092 (queues_left / pf->num_vf_qps));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011093 queues_left -= (pf->num_req_vfs * pf->num_vf_qps);
11094 }
11095
11096 if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
11097 pf->num_vmdq_vsis && pf->num_vmdq_qps && queues_left) {
11098 pf->num_vmdq_vsis = min_t(int, pf->num_vmdq_vsis,
11099 (queues_left / pf->num_vmdq_qps));
11100 queues_left -= (pf->num_vmdq_vsis * pf->num_vmdq_qps);
11101 }
11102
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +000011103 pf->queues_left = queues_left;
Neerav Parikh8279e492015-09-03 17:18:50 -040011104 dev_dbg(&pf->pdev->dev,
11105 "qs_avail=%d FD SB=%d lan_qs=%d lan_tc0=%d vf=%d*%d vmdq=%d*%d, remaining=%d\n",
11106 pf->hw.func_caps.num_tx_qp,
11107 !!(pf->flags & I40E_FLAG_FD_SB_ENABLED),
Helin Zhangacd65442015-10-26 19:44:28 -040011108 pf->num_lan_qps, pf->alloc_rss_size, pf->num_req_vfs,
11109 pf->num_vf_qps, pf->num_vmdq_vsis, pf->num_vmdq_qps,
11110 queues_left);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011111}
11112
11113/**
11114 * i40e_setup_pf_filter_control - Setup PF static filter control
11115 * @pf: PF to be setup
11116 *
Jeff Kirsherb40c82e62015-02-27 09:18:34 +000011117 * i40e_setup_pf_filter_control sets up a PF's initial filter control
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011118 * settings. If PE/FCoE are enabled then it will also set the per PF
11119 * based filter sizes required for them. It also enables Flow director,
11120 * ethertype and macvlan type filter settings for the pf.
11121 *
11122 * Returns 0 on success, negative on failure
11123 **/
11124static int i40e_setup_pf_filter_control(struct i40e_pf *pf)
11125{
11126 struct i40e_filter_control_settings *settings = &pf->filter_settings;
11127
11128 settings->hash_lut_size = I40E_HASH_LUT_SIZE_128;
11129
11130 /* Flow Director is enabled */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080011131 if (pf->flags & (I40E_FLAG_FD_SB_ENABLED | I40E_FLAG_FD_ATR_ENABLED))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011132 settings->enable_fdir = true;
11133
11134 /* Ethtype and MACVLAN filters enabled for PF */
11135 settings->enable_ethtype = true;
11136 settings->enable_macvlan = true;
11137
11138 if (i40e_set_filter_control(&pf->hw, settings))
11139 return -ENOENT;
11140
11141 return 0;
11142}
11143
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000011144#define INFO_STRING_LEN 255
Shannon Nelson7fd89542015-10-21 19:47:04 -040011145#define REMAIN(__x) (INFO_STRING_LEN - (__x))
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000011146static void i40e_print_features(struct i40e_pf *pf)
11147{
11148 struct i40e_hw *hw = &pf->hw;
Joe Perches3b195842015-12-03 04:20:57 -080011149 char *buf;
11150 int i;
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000011151
Joe Perches3b195842015-12-03 04:20:57 -080011152 buf = kmalloc(INFO_STRING_LEN, GFP_KERNEL);
11153 if (!buf)
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000011154 return;
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000011155
Joe Perches3b195842015-12-03 04:20:57 -080011156 i = snprintf(buf, INFO_STRING_LEN, "Features: PF-id[%d]", hw->pf_id);
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000011157#ifdef CONFIG_PCI_IOV
Joe Perches3b195842015-12-03 04:20:57 -080011158 i += snprintf(&buf[i], REMAIN(i), " VFs: %d", pf->num_req_vfs);
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000011159#endif
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -070011160 i += snprintf(&buf[i], REMAIN(i), " VSIs: %d QP: %d",
Shannon Nelson7fd89542015-10-21 19:47:04 -040011161 pf->hw.func_caps.num_vsis,
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -070011162 pf->vsi[pf->lan_vsi]->num_queue_pairs);
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000011163 if (pf->flags & I40E_FLAG_RSS_ENABLED)
Joe Perches3b195842015-12-03 04:20:57 -080011164 i += snprintf(&buf[i], REMAIN(i), " RSS");
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000011165 if (pf->flags & I40E_FLAG_FD_ATR_ENABLED)
Joe Perches3b195842015-12-03 04:20:57 -080011166 i += snprintf(&buf[i], REMAIN(i), " FD_ATR");
Akeem G Abodunrinc6423ff2014-05-10 04:49:08 +000011167 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
Joe Perches3b195842015-12-03 04:20:57 -080011168 i += snprintf(&buf[i], REMAIN(i), " FD_SB");
11169 i += snprintf(&buf[i], REMAIN(i), " NTUPLE");
Akeem G Abodunrinc6423ff2014-05-10 04:49:08 +000011170 }
Neerav Parikh4d9b6042014-05-22 06:31:51 +000011171 if (pf->flags & I40E_FLAG_DCB_CAPABLE)
Joe Perches3b195842015-12-03 04:20:57 -080011172 i += snprintf(&buf[i], REMAIN(i), " DCB");
Joe Perches3b195842015-12-03 04:20:57 -080011173 i += snprintf(&buf[i], REMAIN(i), " VxLAN");
Singhai, Anjali6a899022015-12-14 12:21:18 -080011174 i += snprintf(&buf[i], REMAIN(i), " Geneve");
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000011175 if (pf->flags & I40E_FLAG_PTP)
Joe Perches3b195842015-12-03 04:20:57 -080011176 i += snprintf(&buf[i], REMAIN(i), " PTP");
Shannon Nelson6dec1012015-09-28 14:12:30 -040011177 if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
Joe Perches3b195842015-12-03 04:20:57 -080011178 i += snprintf(&buf[i], REMAIN(i), " VEB");
Shannon Nelson6dec1012015-09-28 14:12:30 -040011179 else
Joe Perches3b195842015-12-03 04:20:57 -080011180 i += snprintf(&buf[i], REMAIN(i), " VEPA");
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000011181
Joe Perches3b195842015-12-03 04:20:57 -080011182 dev_info(&pf->pdev->dev, "%s\n", buf);
11183 kfree(buf);
Shannon Nelson7fd89542015-10-21 19:47:04 -040011184 WARN_ON(i > INFO_STRING_LEN);
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000011185}
11186
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011187/**
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050011188 * i40e_get_platform_mac_addr - get platform-specific MAC address
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050011189 * @pdev: PCI device information struct
11190 * @pf: board private structure
11191 *
Jacob Keller41c4c2b2017-04-05 07:50:57 -040011192 * Look up the MAC address for the device. First we'll try
11193 * eth_platform_get_mac_address, which will check Open Firmware, or arch
11194 * specific fallback. Otherwise, we'll default to the stored value in
11195 * firmware.
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050011196 **/
11197static void i40e_get_platform_mac_addr(struct pci_dev *pdev, struct i40e_pf *pf)
11198{
Jacob Keller41c4c2b2017-04-05 07:50:57 -040011199 if (eth_platform_get_mac_address(&pdev->dev, pf->hw.mac.addr))
11200 i40e_get_mac_addr(&pf->hw, pf->hw.mac.addr);
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050011201}
11202
11203/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011204 * i40e_probe - Device initialization routine
11205 * @pdev: PCI device information struct
11206 * @ent: entry in i40e_pci_tbl
11207 *
Jeff Kirsherb40c82e62015-02-27 09:18:34 +000011208 * i40e_probe initializes a PF identified by a pci_dev structure.
11209 * The OS initialization, configuring of the PF private structure,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011210 * and a hardware reset occur.
11211 *
11212 * Returns 0 on success, negative on failure
11213 **/
11214static int i40e_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
11215{
Catherine Sullivane8278452015-02-06 08:52:08 +000011216 struct i40e_aq_get_phy_abilities_resp abilities;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011217 struct i40e_pf *pf;
11218 struct i40e_hw *hw;
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +000011219 static u16 pfs_found;
Shannon Nelson1d5109d2015-08-26 15:14:08 -040011220 u16 wol_nvm_bits;
Catherine Sullivand4dfb812013-11-28 06:39:21 +000011221 u16 link_status;
Jean Sacren6f66a482015-09-19 05:08:45 -060011222 int err;
Anjali Singhai Jain4f2f017c2015-10-21 19:47:07 -040011223 u32 val;
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +000011224 u32 i;
Helin Zhang58fc3262015-10-01 14:37:38 -040011225 u8 set_fc_aq_fail;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011226
11227 err = pci_enable_device_mem(pdev);
11228 if (err)
11229 return err;
11230
11231 /* set up for high or low dma */
Mitch Williams64942942014-02-11 08:26:33 +000011232 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
Mitch Williams64942942014-02-11 08:26:33 +000011233 if (err) {
Jean Sacrene3e3bfd2014-03-25 04:30:27 +000011234 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
11235 if (err) {
11236 dev_err(&pdev->dev,
11237 "DMA configuration failed: 0x%x\n", err);
11238 goto err_dma;
11239 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011240 }
11241
11242 /* set up pci connections */
Johannes Thumshirn56d766d2016-06-07 09:44:05 +020011243 err = pci_request_mem_regions(pdev, i40e_driver_name);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011244 if (err) {
11245 dev_info(&pdev->dev,
11246 "pci_request_selected_regions failed %d\n", err);
11247 goto err_pci_reg;
11248 }
11249
11250 pci_enable_pcie_error_reporting(pdev);
11251 pci_set_master(pdev);
11252
11253 /* Now that we have a PCI connection, we need to do the
11254 * low level device setup. This is primarily setting up
11255 * the Admin Queue structures and then querying for the
11256 * device's current profile information.
11257 */
11258 pf = kzalloc(sizeof(*pf), GFP_KERNEL);
11259 if (!pf) {
11260 err = -ENOMEM;
11261 goto err_pf_alloc;
11262 }
11263 pf->next_vsi = 0;
11264 pf->pdev = pdev;
Mauro S. M. Rodrigues9e6c9c02017-05-12 23:26:56 -030011265 set_bit(__I40E_DOWN, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011266
11267 hw = &pf->hw;
11268 hw->back = pf;
Anjali Singhai232f4702015-02-26 16:15:39 +000011269
Shannon Nelson2ac8b672015-07-23 16:54:37 -040011270 pf->ioremap_len = min_t(int, pci_resource_len(pdev, 0),
11271 I40E_MAX_CSR_SPACE);
Anjali Singhai232f4702015-02-26 16:15:39 +000011272
Shannon Nelson2ac8b672015-07-23 16:54:37 -040011273 hw->hw_addr = ioremap(pci_resource_start(pdev, 0), pf->ioremap_len);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011274 if (!hw->hw_addr) {
11275 err = -EIO;
11276 dev_info(&pdev->dev, "ioremap(0x%04x, 0x%04x) failed: 0x%x\n",
11277 (unsigned int)pci_resource_start(pdev, 0),
Shannon Nelson2ac8b672015-07-23 16:54:37 -040011278 pf->ioremap_len, err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011279 goto err_ioremap;
11280 }
11281 hw->vendor_id = pdev->vendor;
11282 hw->device_id = pdev->device;
11283 pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
11284 hw->subsystem_vendor_id = pdev->subsystem_vendor;
11285 hw->subsystem_device_id = pdev->subsystem_device;
11286 hw->bus.device = PCI_SLOT(pdev->devfn);
11287 hw->bus.func = PCI_FUNC(pdev->devfn);
Sudheer Mogilappagarib3f028f2017-02-09 23:58:22 -080011288 hw->bus.bus_id = pdev->bus->number;
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +000011289 pf->instance = pfs_found;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011290
Jacob Keller0e588de2017-02-06 14:38:50 -080011291 INIT_LIST_HEAD(&pf->l3_flex_pit_list);
11292 INIT_LIST_HEAD(&pf->l4_flex_pit_list);
11293
Shannon Nelsonde03d2b2016-03-10 14:59:44 -080011294 /* set up the locks for the AQ, do this only once in probe
11295 * and destroy them only once in remove
11296 */
11297 mutex_init(&hw->aq.asq_mutex);
11298 mutex_init(&hw->aq.arq_mutex);
11299
Alexander Duyck5d4ca232016-09-30 08:21:46 -040011300 pf->msg_enable = netif_msg_init(debug,
11301 NETIF_MSG_DRV |
11302 NETIF_MSG_PROBE |
11303 NETIF_MSG_LINK);
11304 if (debug < -1)
11305 pf->hw.debug_mask = debug;
Shannon Nelson5b5faa42014-10-17 03:14:51 +000011306
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +000011307 /* do a special CORER for clearing PXE mode once at init */
11308 if (hw->revision_id == 0 &&
11309 (rd32(hw, I40E_GLLAN_RCTL_0) & I40E_GLLAN_RCTL_0_PXE_MODE_MASK)) {
11310 wr32(hw, I40E_GLGEN_RTRIG, I40E_GLGEN_RTRIG_CORER_MASK);
11311 i40e_flush(hw);
11312 msleep(200);
11313 pf->corer_count++;
11314
11315 i40e_clear_pxe_mode(hw);
11316 }
11317
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011318 /* Reset here to make sure all is clean and to define PF 'n' */
Shannon Nelson838d41d2014-06-04 20:41:27 +000011319 i40e_clear_hw(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011320 err = i40e_pf_reset(hw);
11321 if (err) {
11322 dev_info(&pdev->dev, "Initial pf_reset failed: %d\n", err);
11323 goto err_pf_reset;
11324 }
11325 pf->pfr_count++;
11326
11327 hw->aq.num_arq_entries = I40E_AQ_LEN;
11328 hw->aq.num_asq_entries = I40E_AQ_LEN;
11329 hw->aq.arq_buf_size = I40E_MAX_AQ_BUF_SIZE;
11330 hw->aq.asq_buf_size = I40E_MAX_AQ_BUF_SIZE;
11331 pf->adminq_work_limit = I40E_AQ_WORK_LIMIT;
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +000011332
Carolyn Wybornyb294ac72014-12-11 07:06:39 +000011333 snprintf(pf->int_name, sizeof(pf->int_name) - 1,
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +000011334 "%s-%s:misc",
11335 dev_driver_string(&pf->pdev->dev), dev_name(&pdev->dev));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011336
11337 err = i40e_init_shared_code(hw);
11338 if (err) {
Anjali Singhai Jainb2a75c52015-04-27 14:57:20 -040011339 dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
11340 err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011341 goto err_pf_reset;
11342 }
11343
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +000011344 /* set up a default setting for link flow control */
11345 pf->hw.fc.requested_mode = I40E_FC_NONE;
11346
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011347 err = i40e_init_adminq(hw);
Carolyn Wyborny2b2426a762015-10-26 19:44:35 -040011348 if (err) {
11349 if (err == I40E_ERR_FIRMWARE_API_VERSION)
11350 dev_info(&pdev->dev,
11351 "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");
11352 else
11353 dev_info(&pdev->dev,
11354 "The driver for the device stopped because the device firmware failed to init. Try updating your NVM image.\n");
11355
11356 goto err_pf_reset;
11357 }
Filip Sadowski5bbb2e22017-06-07 05:43:09 -040011358 i40e_get_oem_version(hw);
Carolyn Wybornyf0b44442015-08-31 19:54:49 -040011359
Shannon Nelson6dec1012015-09-28 14:12:30 -040011360 /* provide nvm, fw, api versions */
11361 dev_info(&pdev->dev, "fw %d.%d.%05d api %d.%d nvm %s\n",
11362 hw->aq.fw_maj_ver, hw->aq.fw_min_ver, hw->aq.fw_build,
11363 hw->aq.api_maj_ver, hw->aq.api_min_ver,
11364 i40e_nvm_version_str(hw));
Carolyn Wybornyf0b44442015-08-31 19:54:49 -040011365
Catherine Sullivan7aa67612014-07-09 07:46:17 +000011366 if (hw->aq.api_maj_ver == I40E_FW_API_VERSION_MAJOR &&
11367 hw->aq.api_min_ver > I40E_FW_API_VERSION_MINOR)
Shannon Nelson278b6f62014-06-04 01:41:03 +000011368 dev_info(&pdev->dev,
Catherine Sullivan7aa67612014-07-09 07:46:17 +000011369 "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");
11370 else if (hw->aq.api_maj_ver < I40E_FW_API_VERSION_MAJOR ||
11371 hw->aq.api_min_ver < (I40E_FW_API_VERSION_MINOR - 1))
Shannon Nelson278b6f62014-06-04 01:41:03 +000011372 dev_info(&pdev->dev,
Catherine Sullivan7aa67612014-07-09 07:46:17 +000011373 "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 +000011374
Shannon Nelson4eb3f762014-03-06 08:59:58 +000011375 i40e_verify_eeprom(pf);
11376
Jesse Brandeburg2c5fe332014-04-23 04:49:57 +000011377 /* Rev 0 hardware was never productized */
11378 if (hw->revision_id < 1)
11379 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");
11380
Shannon Nelson6ff4ef82013-12-21 05:44:49 +000011381 i40e_clear_pxe_mode(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011382 err = i40e_get_capabilities(pf);
11383 if (err)
11384 goto err_adminq_setup;
11385
11386 err = i40e_sw_init(pf);
11387 if (err) {
11388 dev_info(&pdev->dev, "sw_init failed: %d\n", err);
11389 goto err_sw_init;
11390 }
11391
11392 err = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
Alexander Duyckc76cb6e2017-02-21 15:55:48 -080011393 hw->func_caps.num_rx_qp, 0, 0);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011394 if (err) {
11395 dev_info(&pdev->dev, "init_lan_hmc failed: %d\n", err);
11396 goto err_init_lan_hmc;
11397 }
11398
11399 err = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
11400 if (err) {
11401 dev_info(&pdev->dev, "configure_lan_hmc failed: %d\n", err);
11402 err = -ENOENT;
11403 goto err_configure_lan_hmc;
11404 }
11405
Neerav Parikhb686ece2014-12-14 01:55:11 +000011406 /* Disable LLDP for NICs that have firmware versions lower than v4.3.
11407 * Ignore error return codes because if it was already disabled via
11408 * hardware settings this will fail
11409 */
Jacob Kellerd36e41d2017-06-23 04:24:46 -040011410 if (pf->hw_features & I40E_HW_STOP_FW_LLDP) {
Neerav Parikhb686ece2014-12-14 01:55:11 +000011411 dev_info(&pdev->dev, "Stopping firmware LLDP agent.\n");
11412 i40e_aq_stop_lldp(hw, true, NULL);
11413 }
11414
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050011415 /* allow a platform config to override the HW addr */
11416 i40e_get_platform_mac_addr(pdev, pf);
Jacob Keller41c4c2b2017-04-05 07:50:57 -040011417
Jesse Brandeburgf62b5062013-11-28 06:39:27 +000011418 if (!is_valid_ether_addr(hw->mac.addr)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011419 dev_info(&pdev->dev, "invalid MAC address %pM\n", hw->mac.addr);
11420 err = -EIO;
11421 goto err_mac_addr;
11422 }
11423 dev_info(&pdev->dev, "MAC address: %pM\n", hw->mac.addr);
Greg Rose9a173902014-05-22 06:32:02 +000011424 ether_addr_copy(hw->mac.perm_addr, hw->mac.addr);
Neerav Parikh1f224ad2014-02-12 01:45:31 +000011425 i40e_get_port_mac_addr(hw, hw->mac.port_addr);
11426 if (is_valid_ether_addr(hw->mac.port_addr))
Jacob Kellerd36e41d2017-06-23 04:24:46 -040011427 pf->hw_features |= I40E_HW_PORT_ID_VALID;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011428
11429 pci_set_drvdata(pdev, pf);
11430 pci_save_state(pdev);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -080011431#ifdef CONFIG_I40E_DCB
11432 err = i40e_init_pf_dcb(pf);
11433 if (err) {
Shannon Nelsonaebfc812014-12-11 07:06:38 +000011434 dev_info(&pdev->dev, "DCB init failed %d, disabled\n", err);
David Ertmanc17ef432016-09-30 01:36:21 -070011435 pf->flags &= ~(I40E_FLAG_DCB_CAPABLE | I40E_FLAG_DCB_ENABLED);
Neerav Parikh014269f2014-04-01 07:11:48 +000011436 /* Continue without DCB enabled */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -080011437 }
11438#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011439
11440 /* set up periodic task facility */
11441 setup_timer(&pf->service_timer, i40e_service_timer, (unsigned long)pf);
11442 pf->service_timer_period = HZ;
11443
11444 INIT_WORK(&pf->service_task, i40e_service_task);
Jacob Keller0da36b92017-04-19 09:25:55 -040011445 clear_bit(__I40E_SERVICE_SCHED, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011446
Shannon Nelson1d5109d2015-08-26 15:14:08 -040011447 /* NVM bit on means WoL disabled for the port */
11448 i40e_read_nvm_word(hw, I40E_SR_NVM_WAKE_ON_LAN, &wol_nvm_bits);
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -080011449 if (BIT (hw->port) & wol_nvm_bits || hw->partition_id != 1)
Shannon Nelson1d5109d2015-08-26 15:14:08 -040011450 pf->wol_en = false;
11451 else
11452 pf->wol_en = true;
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011453 device_set_wakeup_enable(&pf->pdev->dev, pf->wol_en);
11454
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011455 /* set up the main switch operations */
11456 i40e_determine_queue_usage(pf);
Jesse Brandeburgc11472802015-04-07 19:45:39 -040011457 err = i40e_init_interrupt_scheme(pf);
11458 if (err)
11459 goto err_switch_setup;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011460
Mitch Williams505682c2014-05-20 08:01:37 +000011461 /* The number of VSIs reported by the FW is the minimum guaranteed
11462 * to us; HW supports far more and we share the remaining pool with
11463 * the other PFs. We allocate space for more than the guarantee with
11464 * the understanding that we might not get them all later.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011465 */
Mitch Williams505682c2014-05-20 08:01:37 +000011466 if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
11467 pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
11468 else
11469 pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
11470
11471 /* Set up the *vsi struct and our local tracking of the MAIN PF vsi. */
Jesse Brandeburgd17038d2015-12-23 12:05:55 -080011472 pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
11473 GFP_KERNEL);
Wei Yongjuned87ac02013-09-24 05:17:25 +000011474 if (!pf->vsi) {
11475 err = -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011476 goto err_switch_setup;
Wei Yongjuned87ac02013-09-24 05:17:25 +000011477 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011478
Anjali Singhai Jainfa11cb32015-05-27 12:06:14 -040011479#ifdef CONFIG_PCI_IOV
11480 /* prep for VF support */
11481 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
11482 (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
Jacob Keller0da36b92017-04-19 09:25:55 -040011483 !test_bit(__I40E_BAD_EEPROM, pf->state)) {
Anjali Singhai Jainfa11cb32015-05-27 12:06:14 -040011484 if (pci_num_vf(pdev))
11485 pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
11486 }
11487#endif
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000011488 err = i40e_setup_pf_switch(pf, false);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011489 if (err) {
11490 dev_info(&pdev->dev, "setup_pf_switch failed: %d\n", err);
11491 goto err_vsis;
11492 }
Helin Zhang58fc3262015-10-01 14:37:38 -040011493
11494 /* Make sure flow control is set according to current settings */
11495 err = i40e_set_fc(hw, &set_fc_aq_fail, true);
11496 if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_GET)
11497 dev_dbg(&pf->pdev->dev,
11498 "Set fc with err %s aq_err %s on get_phy_cap\n",
11499 i40e_stat_str(hw, err),
11500 i40e_aq_str(hw, hw->aq.asq_last_status));
11501 if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_SET)
11502 dev_dbg(&pf->pdev->dev,
11503 "Set fc with err %s aq_err %s on set_phy_config\n",
11504 i40e_stat_str(hw, err),
11505 i40e_aq_str(hw, hw->aq.asq_last_status));
11506 if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_UPDATE)
11507 dev_dbg(&pf->pdev->dev,
11508 "Set fc with err %s aq_err %s on get_link_info\n",
11509 i40e_stat_str(hw, err),
11510 i40e_aq_str(hw, hw->aq.asq_last_status));
11511
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +000011512 /* if FDIR VSI was set up, start it now */
Mitch Williams505682c2014-05-20 08:01:37 +000011513 for (i = 0; i < pf->num_alloc_vsi; i++) {
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +000011514 if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
11515 i40e_vsi_open(pf->vsi[i]);
11516 break;
11517 }
11518 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011519
Shannon Nelson2f0aff42016-01-04 10:33:08 -080011520 /* The driver only wants link up/down and module qualification
11521 * reports from firmware. Note the negative logic.
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +000011522 */
11523 err = i40e_aq_set_phy_int_mask(&pf->hw,
Shannon Nelson2f0aff42016-01-04 10:33:08 -080011524 ~(I40E_AQ_EVENT_LINK_UPDOWN |
Shannon Nelson867a79e2016-03-18 12:18:15 -070011525 I40E_AQ_EVENT_MEDIA_NA |
Shannon Nelson2f0aff42016-01-04 10:33:08 -080011526 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +000011527 if (err)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040011528 dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
11529 i40e_stat_str(&pf->hw, err),
11530 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +000011531
Anjali Singhai Jain4f2f017c2015-10-21 19:47:07 -040011532 /* Reconfigure hardware for allowing smaller MSS in the case
11533 * of TSO, so that we avoid the MDD being fired and causing
11534 * a reset in the case of small MSS+TSO.
11535 */
11536 val = rd32(hw, I40E_REG_MSS);
11537 if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
11538 val &= ~I40E_REG_MSS_MIN_MASK;
11539 val |= I40E_64BYTE_MSS;
11540 wr32(hw, I40E_REG_MSS, val);
11541 }
11542
Jacob Kellerd36e41d2017-06-23 04:24:46 -040011543 if (pf->hw_features & I40E_HW_RESTART_AUTONEG) {
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +000011544 msleep(75);
11545 err = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
11546 if (err)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040011547 dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
11548 i40e_stat_str(&pf->hw, err),
11549 i40e_aq_str(&pf->hw,
11550 pf->hw.aq.asq_last_status));
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +000011551 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011552 /* The main driver is (mostly) up and happy. We need to set this state
11553 * before setting up the misc vector or we get a race and the vector
11554 * ends up disabled forever.
11555 */
Mauro S. M. Rodrigues9e6c9c02017-05-12 23:26:56 -030011556 clear_bit(__I40E_DOWN, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011557
11558 /* In case of MSIX we are going to setup the misc vector right here
11559 * to handle admin queue events etc. In case of legacy and MSI
11560 * the misc functionality and queue processing is combined in
11561 * the same vector and that gets setup at open.
11562 */
11563 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
11564 err = i40e_setup_misc_vector(pf);
11565 if (err) {
11566 dev_info(&pdev->dev,
11567 "setup of misc vector failed: %d\n", err);
11568 goto err_vsis;
11569 }
11570 }
11571
Greg Rosedf805f62014-04-04 04:43:16 +000011572#ifdef CONFIG_PCI_IOV
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011573 /* prep for VF support */
11574 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
Shannon Nelson4eb3f762014-03-06 08:59:58 +000011575 (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
Jacob Keller0da36b92017-04-19 09:25:55 -040011576 !test_bit(__I40E_BAD_EEPROM, pf->state)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011577 /* disable link interrupts for VFs */
11578 val = rd32(hw, I40E_PFGEN_PORTMDIO_NUM);
11579 val &= ~I40E_PFGEN_PORTMDIO_NUM_VFLINK_STAT_ENA_MASK;
11580 wr32(hw, I40E_PFGEN_PORTMDIO_NUM, val);
11581 i40e_flush(hw);
Mitch Williams4aeec012014-02-13 03:48:47 -080011582
11583 if (pci_num_vf(pdev)) {
11584 dev_info(&pdev->dev,
11585 "Active VFs found, allocating resources.\n");
11586 err = i40e_alloc_vfs(pf, pci_num_vf(pdev));
11587 if (err)
11588 dev_info(&pdev->dev,
11589 "Error %d allocating resources for existing VFs\n",
11590 err);
11591 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011592 }
Greg Rosedf805f62014-04-04 04:43:16 +000011593#endif /* CONFIG_PCI_IOV */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011594
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060011595 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11596 pf->iwarp_base_vector = i40e_get_lump(pf, pf->irq_pile,
11597 pf->num_iwarp_msix,
11598 I40E_IWARP_IRQ_PILE_ID);
11599 if (pf->iwarp_base_vector < 0) {
11600 dev_info(&pdev->dev,
11601 "failed to get tracking for %d vectors for IWARP err=%d\n",
11602 pf->num_iwarp_msix, pf->iwarp_base_vector);
11603 pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
11604 }
11605 }
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +000011606
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011607 i40e_dbg_pf_init(pf);
11608
11609 /* tell the firmware that we're starting */
Jesse Brandeburg44033fa2014-04-23 04:50:15 +000011610 i40e_send_version(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011611
11612 /* since everything's happy, start the service_task timer */
11613 mod_timer(&pf->service_timer,
11614 round_jiffies(jiffies + pf->service_timer_period));
11615
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060011616 /* add this PF to client device list and launch a client service task */
Mitch Williams004eb612017-04-04 12:40:16 -070011617 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11618 err = i40e_lan_add_device(pf);
11619 if (err)
11620 dev_info(&pdev->dev, "Failed to add PF to client API service list: %d\n",
11621 err);
11622 }
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060011623
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040011624#define PCI_SPEED_SIZE 8
11625#define PCI_WIDTH_SIZE 8
11626 /* Devices on the IOSF bus do not have this information
11627 * and will report PCI Gen 1 x 1 by default so don't bother
11628 * checking them.
11629 */
Jacob Kellerd36e41d2017-06-23 04:24:46 -040011630 if (!(pf->hw_features & I40E_HW_NO_PCI_LINK_CHECK)) {
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040011631 char speed[PCI_SPEED_SIZE] = "Unknown";
11632 char width[PCI_WIDTH_SIZE] = "Unknown";
Catherine Sullivand4dfb812013-11-28 06:39:21 +000011633
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040011634 /* Get the negotiated link width and speed from PCI config
11635 * space
11636 */
11637 pcie_capability_read_word(pf->pdev, PCI_EXP_LNKSTA,
11638 &link_status);
Catherine Sullivand4dfb812013-11-28 06:39:21 +000011639
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040011640 i40e_set_pci_config_data(hw, link_status);
Catherine Sullivand4dfb812013-11-28 06:39:21 +000011641
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040011642 switch (hw->bus.speed) {
11643 case i40e_bus_speed_8000:
11644 strncpy(speed, "8.0", PCI_SPEED_SIZE); break;
11645 case i40e_bus_speed_5000:
11646 strncpy(speed, "5.0", PCI_SPEED_SIZE); break;
11647 case i40e_bus_speed_2500:
11648 strncpy(speed, "2.5", PCI_SPEED_SIZE); break;
11649 default:
11650 break;
11651 }
11652 switch (hw->bus.width) {
11653 case i40e_bus_width_pcie_x8:
11654 strncpy(width, "8", PCI_WIDTH_SIZE); break;
11655 case i40e_bus_width_pcie_x4:
11656 strncpy(width, "4", PCI_WIDTH_SIZE); break;
11657 case i40e_bus_width_pcie_x2:
11658 strncpy(width, "2", PCI_WIDTH_SIZE); break;
11659 case i40e_bus_width_pcie_x1:
11660 strncpy(width, "1", PCI_WIDTH_SIZE); break;
11661 default:
11662 break;
11663 }
11664
11665 dev_info(&pdev->dev, "PCI-Express: Speed %sGT/s Width x%s\n",
11666 speed, width);
11667
11668 if (hw->bus.width < i40e_bus_width_pcie_x8 ||
11669 hw->bus.speed < i40e_bus_speed_8000) {
11670 dev_warn(&pdev->dev, "PCI-Express bandwidth available for this device may be insufficient for optimal performance.\n");
11671 dev_warn(&pdev->dev, "Please move the device to a different PCI-e link with more lanes and/or higher transfer rate.\n");
11672 }
Catherine Sullivand4dfb812013-11-28 06:39:21 +000011673 }
11674
Catherine Sullivane8278452015-02-06 08:52:08 +000011675 /* get the requested speeds from the fw */
11676 err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities, NULL);
11677 if (err)
Neerav Parikh8279e492015-09-03 17:18:50 -040011678 dev_dbg(&pf->pdev->dev, "get requested speeds ret = %s last_status = %s\n",
11679 i40e_stat_str(&pf->hw, err),
11680 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Catherine Sullivane8278452015-02-06 08:52:08 +000011681 pf->hw.phy.link_info.requested_speeds = abilities.link_speed;
11682
Catherine Sullivanfc72dbc2015-09-01 11:36:30 -040011683 /* get the supported phy types from the fw */
11684 err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities, NULL);
11685 if (err)
11686 dev_dbg(&pf->pdev->dev, "get supported phy types ret = %s last_status = %s\n",
11687 i40e_stat_str(&pf->hw, err),
11688 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Catherine Sullivanfc72dbc2015-09-01 11:36:30 -040011689
Anjali Singhai Jaine7358f52015-10-01 14:37:34 -040011690 /* Add a filter to drop all Flow control frames from any VSI from being
11691 * transmitted. By doing so we stop a malicious VF from sending out
11692 * PAUSE or PFC frames and potentially controlling traffic for other
11693 * PF/VF VSIs.
11694 * The FW can still send Flow control frames if enabled.
11695 */
11696 i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
11697 pf->main_vsi_seid);
11698
Carolyn Wyborny31b606d2016-02-17 16:12:12 -080011699 if ((pf->hw.device_id == I40E_DEV_ID_10G_BASE_T) ||
Henry Tieman4f9b4302016-11-08 13:05:18 -080011700 (pf->hw.device_id == I40E_DEV_ID_10G_BASE_T4))
Jacob Kellerd36e41d2017-06-23 04:24:46 -040011701 pf->hw_features |= I40E_HW_PHY_CONTROLS_LEDS;
Harshitha Ramamurthy4ad9f4f2016-11-08 13:05:09 -080011702 if (pf->hw.device_id == I40E_DEV_ID_SFP_I_X722)
Jacob Kellerd36e41d2017-06-23 04:24:46 -040011703 pf->hw_features |= I40E_HW_HAVE_CRT_RETIMER;
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000011704 /* print a string summarizing features */
11705 i40e_print_features(pf);
11706
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011707 return 0;
11708
11709 /* Unwind what we've done if something failed in the setup */
11710err_vsis:
Mauro S. M. Rodrigues9e6c9c02017-05-12 23:26:56 -030011711 set_bit(__I40E_DOWN, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011712 i40e_clear_interrupt_scheme(pf);
11713 kfree(pf->vsi);
Shannon Nelson04b03012013-11-28 06:39:34 +000011714err_switch_setup:
11715 i40e_reset_interrupt_capability(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011716 del_timer_sync(&pf->service_timer);
11717err_mac_addr:
11718err_configure_lan_hmc:
11719 (void)i40e_shutdown_lan_hmc(hw);
11720err_init_lan_hmc:
11721 kfree(pf->qp_pile);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011722err_sw_init:
11723err_adminq_setup:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011724err_pf_reset:
11725 iounmap(hw->hw_addr);
11726err_ioremap:
11727 kfree(pf);
11728err_pf_alloc:
11729 pci_disable_pcie_error_reporting(pdev);
Johannes Thumshirn56d766d2016-06-07 09:44:05 +020011730 pci_release_mem_regions(pdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011731err_pci_reg:
11732err_dma:
11733 pci_disable_device(pdev);
11734 return err;
11735}
11736
11737/**
11738 * i40e_remove - Device removal routine
11739 * @pdev: PCI device information struct
11740 *
11741 * i40e_remove is called by the PCI subsystem to alert the driver
11742 * that is should release a PCI device. This could be caused by a
11743 * Hot-Plug event, or because the driver is going to be removed from
11744 * memory.
11745 **/
11746static void i40e_remove(struct pci_dev *pdev)
11747{
11748 struct i40e_pf *pf = pci_get_drvdata(pdev);
Carolyn Wybornybcab2db2015-09-28 14:16:55 -040011749 struct i40e_hw *hw = &pf->hw;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011750 i40e_status ret_code;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011751 int i;
11752
11753 i40e_dbg_pf_exit(pf);
11754
Jacob Kellerbeb0dff2014-01-11 05:43:19 +000011755 i40e_ptp_stop(pf);
11756
Carolyn Wybornybcab2db2015-09-28 14:16:55 -040011757 /* Disable RSS in hw */
Shannon Nelson272cdaf22016-02-17 16:12:21 -080011758 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), 0);
11759 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), 0);
Carolyn Wybornybcab2db2015-09-28 14:16:55 -040011760
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011761 /* no more scheduling of any task */
Jacob Keller0da36b92017-04-19 09:25:55 -040011762 set_bit(__I40E_SUSPENDED, pf->state);
Mauro S. M. Rodrigues9e6c9c02017-05-12 23:26:56 -030011763 set_bit(__I40E_DOWN, pf->state);
Shannon Nelsonc99abb42016-03-10 14:59:45 -080011764 if (pf->service_timer.data)
11765 del_timer_sync(&pf->service_timer);
11766 if (pf->service_task.func)
11767 cancel_work_sync(&pf->service_task);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011768
Mitch Williams921c4672017-03-30 00:46:08 -070011769 /* Client close must be called explicitly here because the timer
11770 * has been stopped.
11771 */
11772 i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
11773
Mitch Williamseb2d80b2014-02-13 03:48:48 -080011774 if (pf->flags & I40E_FLAG_SRIOV_ENABLED) {
11775 i40e_free_vfs(pf);
11776 pf->flags &= ~I40E_FLAG_SRIOV_ENABLED;
11777 }
11778
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011779 i40e_fdir_teardown(pf);
11780
11781 /* If there is a switch structure or any orphans, remove them.
11782 * This will leave only the PF's VSI remaining.
11783 */
11784 for (i = 0; i < I40E_MAX_VEB; i++) {
11785 if (!pf->veb[i])
11786 continue;
11787
11788 if (pf->veb[i]->uplink_seid == pf->mac_seid ||
11789 pf->veb[i]->uplink_seid == 0)
11790 i40e_switch_branch_release(pf->veb[i]);
11791 }
11792
11793 /* Now we can shutdown the PF's VSI, just before we kill
11794 * adminq and hmc.
11795 */
11796 if (pf->vsi[pf->lan_vsi])
11797 i40e_vsi_release(pf->vsi[pf->lan_vsi]);
11798
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060011799 /* remove attached clients */
Mitch Williams004eb612017-04-04 12:40:16 -070011800 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11801 ret_code = i40e_lan_del_device(pf);
11802 if (ret_code)
11803 dev_warn(&pdev->dev, "Failed to delete client device: %d\n",
11804 ret_code);
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060011805 }
11806
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011807 /* shutdown and destroy the HMC */
Jesse Brandeburgf734dff2016-01-15 14:33:11 -080011808 if (hw->hmc.hmc_obj) {
11809 ret_code = i40e_shutdown_lan_hmc(hw);
Shannon Nelson60442de2014-04-23 04:50:13 +000011810 if (ret_code)
11811 dev_warn(&pdev->dev,
11812 "Failed to destroy the HMC resources: %d\n",
11813 ret_code);
11814 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011815
11816 /* shutdown the adminq */
Henry Tiemanac9c5c62016-09-06 18:05:11 -070011817 i40e_shutdown_adminq(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011818
Jesse Brandeburg8ddb3322015-11-18 15:47:06 -080011819 /* destroy the locks only once, here */
11820 mutex_destroy(&hw->aq.arq_mutex);
11821 mutex_destroy(&hw->aq.asq_mutex);
11822
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011823 /* Clear all dynamic memory lists of rings, q_vectors, and VSIs */
11824 i40e_clear_interrupt_scheme(pf);
Mitch Williams505682c2014-05-20 08:01:37 +000011825 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011826 if (pf->vsi[i]) {
11827 i40e_vsi_clear_rings(pf->vsi[i]);
11828 i40e_vsi_clear(pf->vsi[i]);
11829 pf->vsi[i] = NULL;
11830 }
11831 }
11832
11833 for (i = 0; i < I40E_MAX_VEB; i++) {
11834 kfree(pf->veb[i]);
11835 pf->veb[i] = NULL;
11836 }
11837
11838 kfree(pf->qp_pile);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011839 kfree(pf->vsi);
11840
Jesse Brandeburgf734dff2016-01-15 14:33:11 -080011841 iounmap(hw->hw_addr);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011842 kfree(pf);
Johannes Thumshirn56d766d2016-06-07 09:44:05 +020011843 pci_release_mem_regions(pdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011844
11845 pci_disable_pcie_error_reporting(pdev);
11846 pci_disable_device(pdev);
11847}
11848
11849/**
11850 * i40e_pci_error_detected - warning that something funky happened in PCI land
11851 * @pdev: PCI device information struct
11852 *
11853 * Called to warn that something happened and the error handling steps
11854 * are in progress. Allows the driver to quiesce things, be ready for
11855 * remediation.
11856 **/
11857static pci_ers_result_t i40e_pci_error_detected(struct pci_dev *pdev,
11858 enum pci_channel_state error)
11859{
11860 struct i40e_pf *pf = pci_get_drvdata(pdev);
11861
11862 dev_info(&pdev->dev, "%s: error %d\n", __func__, error);
11863
Guilherme G Piccoliedfc23ee2016-10-03 00:31:12 -070011864 if (!pf) {
11865 dev_info(&pdev->dev,
11866 "Cannot recover - error happened during device probe\n");
11867 return PCI_ERS_RESULT_DISCONNECT;
11868 }
11869
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011870 /* shutdown all operations */
Jacob Kellerdfc4ff62017-06-07 05:43:13 -040011871 if (!test_bit(__I40E_SUSPENDED, pf->state))
11872 i40e_prep_for_reset(pf, false);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011873
11874 /* Request a slot reset */
11875 return PCI_ERS_RESULT_NEED_RESET;
11876}
11877
11878/**
11879 * i40e_pci_error_slot_reset - a PCI slot reset just happened
11880 * @pdev: PCI device information struct
11881 *
11882 * Called to find if the driver can work with the device now that
11883 * the pci slot has been reset. If a basic connection seems good
11884 * (registers are readable and have sane content) then return a
11885 * happy little PCI_ERS_RESULT_xxx.
11886 **/
11887static pci_ers_result_t i40e_pci_error_slot_reset(struct pci_dev *pdev)
11888{
11889 struct i40e_pf *pf = pci_get_drvdata(pdev);
11890 pci_ers_result_t result;
11891 int err;
11892 u32 reg;
11893
Shannon Nelsonfb43201f2015-08-26 15:14:17 -040011894 dev_dbg(&pdev->dev, "%s\n", __func__);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011895 if (pci_enable_device_mem(pdev)) {
11896 dev_info(&pdev->dev,
11897 "Cannot re-enable PCI device after reset.\n");
11898 result = PCI_ERS_RESULT_DISCONNECT;
11899 } else {
11900 pci_set_master(pdev);
11901 pci_restore_state(pdev);
11902 pci_save_state(pdev);
11903 pci_wake_from_d3(pdev, false);
11904
11905 reg = rd32(&pf->hw, I40E_GLGEN_RTRIG);
11906 if (reg == 0)
11907 result = PCI_ERS_RESULT_RECOVERED;
11908 else
11909 result = PCI_ERS_RESULT_DISCONNECT;
11910 }
11911
11912 err = pci_cleanup_aer_uncorrect_error_status(pdev);
11913 if (err) {
11914 dev_info(&pdev->dev,
11915 "pci_cleanup_aer_uncorrect_error_status failed 0x%0x\n",
11916 err);
11917 /* non-fatal, continue */
11918 }
11919
11920 return result;
11921}
11922
11923/**
11924 * i40e_pci_error_resume - restart operations after PCI error recovery
11925 * @pdev: PCI device information struct
11926 *
11927 * Called to allow the driver to bring things back up after PCI error
11928 * and/or reset recovery has finished.
11929 **/
11930static void i40e_pci_error_resume(struct pci_dev *pdev)
11931{
11932 struct i40e_pf *pf = pci_get_drvdata(pdev);
11933
Shannon Nelsonfb43201f2015-08-26 15:14:17 -040011934 dev_dbg(&pdev->dev, "%s\n", __func__);
Jacob Keller0da36b92017-04-19 09:25:55 -040011935 if (test_bit(__I40E_SUSPENDED, pf->state))
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011936 return;
11937
Jacob Kellerdfc4ff62017-06-07 05:43:13 -040011938 i40e_handle_reset_warning(pf, false);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011939}
11940
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011941/**
Joshua Hay1d680052016-12-12 15:44:08 -080011942 * i40e_enable_mc_magic_wake - enable multicast magic packet wake up
11943 * using the mac_address_write admin q function
11944 * @pf: pointer to i40e_pf struct
11945 **/
11946static void i40e_enable_mc_magic_wake(struct i40e_pf *pf)
11947{
11948 struct i40e_hw *hw = &pf->hw;
11949 i40e_status ret;
11950 u8 mac_addr[6];
11951 u16 flags = 0;
11952
11953 /* Get current MAC address in case it's an LAA */
11954 if (pf->vsi[pf->lan_vsi] && pf->vsi[pf->lan_vsi]->netdev) {
11955 ether_addr_copy(mac_addr,
11956 pf->vsi[pf->lan_vsi]->netdev->dev_addr);
11957 } else {
11958 dev_err(&pf->pdev->dev,
11959 "Failed to retrieve MAC address; using default\n");
11960 ether_addr_copy(mac_addr, hw->mac.addr);
11961 }
11962
11963 /* The FW expects the mac address write cmd to first be called with
11964 * one of these flags before calling it again with the multicast
11965 * enable flags.
11966 */
11967 flags = I40E_AQC_WRITE_TYPE_LAA_WOL;
11968
11969 if (hw->func_caps.flex10_enable && hw->partition_id != 1)
11970 flags = I40E_AQC_WRITE_TYPE_LAA_ONLY;
11971
11972 ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
11973 if (ret) {
11974 dev_err(&pf->pdev->dev,
11975 "Failed to update MAC address registers; cannot enable Multicast Magic packet wake up");
11976 return;
11977 }
11978
11979 flags = I40E_AQC_MC_MAG_EN
11980 | I40E_AQC_WOL_PRESERVE_ON_PFR
11981 | I40E_AQC_WRITE_TYPE_UPDATE_MC_MAG;
11982 ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
11983 if (ret)
11984 dev_err(&pf->pdev->dev,
11985 "Failed to enable Multicast Magic Packet wake up\n");
11986}
11987
11988/**
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011989 * i40e_shutdown - PCI callback for shutting down
11990 * @pdev: PCI device information struct
11991 **/
11992static void i40e_shutdown(struct pci_dev *pdev)
11993{
11994 struct i40e_pf *pf = pci_get_drvdata(pdev);
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011995 struct i40e_hw *hw = &pf->hw;
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011996
Jacob Keller0da36b92017-04-19 09:25:55 -040011997 set_bit(__I40E_SUSPENDED, pf->state);
Mauro S. M. Rodrigues9e6c9c02017-05-12 23:26:56 -030011998 set_bit(__I40E_DOWN, pf->state);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011999 rtnl_lock();
Maciej Sosin373149f2017-04-05 07:50:55 -040012000 i40e_prep_for_reset(pf, true);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000012001 rtnl_unlock();
12002
Shannon Nelson8e2773a2013-11-28 06:39:22 +000012003 wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
12004 wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
12005
Catherine Sullivan02b42492015-07-10 19:35:59 -040012006 del_timer_sync(&pf->service_timer);
12007 cancel_work_sync(&pf->service_task);
12008 i40e_fdir_teardown(pf);
12009
Mitch Williams921c4672017-03-30 00:46:08 -070012010 /* Client close must be called explicitly here because the timer
12011 * has been stopped.
12012 */
12013 i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
12014
Jacob Kellerd36e41d2017-06-23 04:24:46 -040012015 if (pf->wol_en && (pf->hw_features & I40E_HW_WOL_MC_MAGIC_PKT_WAKE))
Joshua Hay1d680052016-12-12 15:44:08 -080012016 i40e_enable_mc_magic_wake(pf);
12017
Jacob Kellerdfc4ff62017-06-07 05:43:13 -040012018 i40e_prep_for_reset(pf, false);
Catherine Sullivan02b42492015-07-10 19:35:59 -040012019
12020 wr32(hw, I40E_PFPM_APM,
12021 (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
12022 wr32(hw, I40E_PFPM_WUFC,
12023 (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
12024
Shannon Nelsone1477582015-02-21 06:44:33 +000012025 i40e_clear_interrupt_scheme(pf);
12026
Shannon Nelson9007bcc2013-11-26 10:49:23 +000012027 if (system_state == SYSTEM_POWER_OFF) {
Shannon Nelson8e2773a2013-11-28 06:39:22 +000012028 pci_wake_from_d3(pdev, pf->wol_en);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000012029 pci_set_power_state(pdev, PCI_D3hot);
12030 }
12031}
12032
12033#ifdef CONFIG_PM
12034/**
12035 * i40e_suspend - PCI callback for moving to D3
12036 * @pdev: PCI device information struct
12037 **/
12038static int i40e_suspend(struct pci_dev *pdev, pm_message_t state)
12039{
12040 struct i40e_pf *pf = pci_get_drvdata(pdev);
Shannon Nelson8e2773a2013-11-28 06:39:22 +000012041 struct i40e_hw *hw = &pf->hw;
Greg Rose059ff692016-05-16 10:26:38 -070012042 int retval = 0;
Shannon Nelson9007bcc2013-11-26 10:49:23 +000012043
Jacob Keller0da36b92017-04-19 09:25:55 -040012044 set_bit(__I40E_SUSPENDED, pf->state);
Mauro S. M. Rodrigues9e6c9c02017-05-12 23:26:56 -030012045 set_bit(__I40E_DOWN, pf->state);
Mitch Williams3932dbf2015-03-31 00:45:04 -070012046
Jacob Kellerd36e41d2017-06-23 04:24:46 -040012047 if (pf->wol_en && (pf->hw_features & I40E_HW_WOL_MC_MAGIC_PKT_WAKE))
Joshua Hay1d680052016-12-12 15:44:08 -080012048 i40e_enable_mc_magic_wake(pf);
12049
Jacob Kellerdfc4ff62017-06-07 05:43:13 -040012050 i40e_prep_for_reset(pf, false);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000012051
Shannon Nelson8e2773a2013-11-28 06:39:22 +000012052 wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
12053 wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
12054
Greg Roseb33d3b72016-05-16 10:26:37 -070012055 i40e_stop_misc_vector(pf);
Carolyn Wyborny7c9ae7f2017-06-20 15:16:53 -070012056 if (pf->msix_entries) {
12057 synchronize_irq(pf->msix_entries[0].vector);
12058 free_irq(pf->msix_entries[0].vector, pf);
12059 }
Greg Rose059ff692016-05-16 10:26:38 -070012060 retval = pci_save_state(pdev);
12061 if (retval)
12062 return retval;
12063
Shannon Nelson8e2773a2013-11-28 06:39:22 +000012064 pci_wake_from_d3(pdev, pf->wol_en);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000012065 pci_set_power_state(pdev, PCI_D3hot);
12066
Greg Rose059ff692016-05-16 10:26:38 -070012067 return retval;
Shannon Nelson9007bcc2013-11-26 10:49:23 +000012068}
12069
12070/**
12071 * i40e_resume - PCI callback for waking up from D3
12072 * @pdev: PCI device information struct
12073 **/
12074static int i40e_resume(struct pci_dev *pdev)
12075{
12076 struct i40e_pf *pf = pci_get_drvdata(pdev);
12077 u32 err;
12078
12079 pci_set_power_state(pdev, PCI_D0);
12080 pci_restore_state(pdev);
12081 /* pci_restore_state() clears dev->state_saves, so
12082 * call pci_save_state() again to restore it.
12083 */
12084 pci_save_state(pdev);
12085
12086 err = pci_enable_device_mem(pdev);
12087 if (err) {
Shannon Nelsonfb43201f2015-08-26 15:14:17 -040012088 dev_err(&pdev->dev, "Cannot enable PCI device from suspend\n");
Shannon Nelson9007bcc2013-11-26 10:49:23 +000012089 return err;
12090 }
12091 pci_set_master(pdev);
12092
12093 /* no wakeup events while running */
12094 pci_wake_from_d3(pdev, false);
12095
12096 /* handling the reset will rebuild the device state */
Jacob Keller0da36b92017-04-19 09:25:55 -040012097 if (test_and_clear_bit(__I40E_SUSPENDED, pf->state)) {
Mauro S. M. Rodrigues9e6c9c02017-05-12 23:26:56 -030012098 clear_bit(__I40E_DOWN, pf->state);
Carolyn Wyborny7c9ae7f2017-06-20 15:16:53 -070012099 if (pf->msix_entries) {
12100 err = request_irq(pf->msix_entries[0].vector,
12101 i40e_intr, 0, pf->int_name, pf);
12102 if (err) {
12103 dev_err(&pf->pdev->dev,
12104 "request_irq for %s failed: %d\n",
12105 pf->int_name, err);
12106 }
12107 }
Jacob Kellerdfc4ff62017-06-07 05:43:13 -040012108 i40e_reset_and_rebuild(pf, false, false);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000012109 }
12110
12111 return 0;
12112}
12113
12114#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012115static const struct pci_error_handlers i40e_err_handler = {
12116 .error_detected = i40e_pci_error_detected,
12117 .slot_reset = i40e_pci_error_slot_reset,
12118 .resume = i40e_pci_error_resume,
12119};
12120
12121static struct pci_driver i40e_driver = {
12122 .name = i40e_driver_name,
12123 .id_table = i40e_pci_tbl,
12124 .probe = i40e_probe,
12125 .remove = i40e_remove,
Shannon Nelson9007bcc2013-11-26 10:49:23 +000012126#ifdef CONFIG_PM
12127 .suspend = i40e_suspend,
12128 .resume = i40e_resume,
12129#endif
12130 .shutdown = i40e_shutdown,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012131 .err_handler = &i40e_err_handler,
12132 .sriov_configure = i40e_pci_sriov_configure,
12133};
12134
12135/**
12136 * i40e_init_module - Driver registration routine
12137 *
12138 * i40e_init_module is the first routine called when the driver is
12139 * loaded. All it does is register with the PCI subsystem.
12140 **/
12141static int __init i40e_init_module(void)
12142{
12143 pr_info("%s: %s - version %s\n", i40e_driver_name,
12144 i40e_driver_string, i40e_driver_version_str);
12145 pr_info("%s: %s\n", i40e_driver_name, i40e_copyright);
Greg Rose96664482015-02-06 08:52:13 +000012146
Jacob Keller4d5957c2017-06-20 15:16:54 -070012147 /* There is no need to throttle the number of active tasks because
12148 * each device limits its own task using a state bit for scheduling
12149 * the service task, and the device tasks do not interfere with each
12150 * other, so we don't set a max task limit. We must set WQ_MEM_RECLAIM
12151 * since we need to be able to guarantee forward progress even under
12152 * memory pressure.
Jesse Brandeburg2803b162015-12-22 14:25:08 -080012153 */
Jacob Keller4d5957c2017-06-20 15:16:54 -070012154 i40e_wq = alloc_workqueue("%s", WQ_MEM_RECLAIM, 0, i40e_driver_name);
Jesse Brandeburg2803b162015-12-22 14:25:08 -080012155 if (!i40e_wq) {
12156 pr_err("%s: Failed to create workqueue\n", i40e_driver_name);
12157 return -ENOMEM;
12158 }
12159
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012160 i40e_dbg_init();
12161 return pci_register_driver(&i40e_driver);
12162}
12163module_init(i40e_init_module);
12164
12165/**
12166 * i40e_exit_module - Driver exit cleanup routine
12167 *
12168 * i40e_exit_module is called just before the driver is removed
12169 * from memory.
12170 **/
12171static void __exit i40e_exit_module(void)
12172{
12173 pci_unregister_driver(&i40e_driver);
Jesse Brandeburg2803b162015-12-22 14:25:08 -080012174 destroy_workqueue(i40e_wq);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012175 i40e_dbg_exit();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012176}
12177module_exit(i40e_exit_module);