blob: 638f5bad0bd7ac1933e9c32a2913b470a356816d [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/**
Mariusz Stachura0dc86922017-07-14 09:27:00 -0400603 * i40e_stat_update_and_clear32 - read and clear hw reg, update a 32 bit stat
604 * @hw: ptr to the hardware info
605 * @reg: the hw reg to read and clear
606 * @stat: ptr to the stat
607 **/
608static void i40e_stat_update_and_clear32(struct i40e_hw *hw, u32 reg, u64 *stat)
609{
610 u32 new_data = rd32(hw, reg);
611
612 wr32(hw, reg, 1); /* must write a nonzero value to clear register */
613 *stat += new_data;
614}
615
616/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000617 * i40e_update_eth_stats - Update VSI-specific ethernet statistics counters.
618 * @vsi: the VSI to be updated
619 **/
620void i40e_update_eth_stats(struct i40e_vsi *vsi)
621{
622 int stat_idx = le16_to_cpu(vsi->info.stat_counter_idx);
623 struct i40e_pf *pf = vsi->back;
624 struct i40e_hw *hw = &pf->hw;
625 struct i40e_eth_stats *oes;
626 struct i40e_eth_stats *es; /* device's eth stats */
627
628 es = &vsi->eth_stats;
629 oes = &vsi->eth_stats_offsets;
630
631 /* Gather up the stats that the hw collects */
632 i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
633 vsi->stat_offsets_loaded,
634 &oes->tx_errors, &es->tx_errors);
635 i40e_stat_update32(hw, I40E_GLV_RDPC(stat_idx),
636 vsi->stat_offsets_loaded,
637 &oes->rx_discards, &es->rx_discards);
Shannon Nelson41a9e552014-04-23 04:50:20 +0000638 i40e_stat_update32(hw, I40E_GLV_RUPP(stat_idx),
639 vsi->stat_offsets_loaded,
640 &oes->rx_unknown_protocol, &es->rx_unknown_protocol);
641 i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
642 vsi->stat_offsets_loaded,
643 &oes->tx_errors, &es->tx_errors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000644
645 i40e_stat_update48(hw, I40E_GLV_GORCH(stat_idx),
646 I40E_GLV_GORCL(stat_idx),
647 vsi->stat_offsets_loaded,
648 &oes->rx_bytes, &es->rx_bytes);
649 i40e_stat_update48(hw, I40E_GLV_UPRCH(stat_idx),
650 I40E_GLV_UPRCL(stat_idx),
651 vsi->stat_offsets_loaded,
652 &oes->rx_unicast, &es->rx_unicast);
653 i40e_stat_update48(hw, I40E_GLV_MPRCH(stat_idx),
654 I40E_GLV_MPRCL(stat_idx),
655 vsi->stat_offsets_loaded,
656 &oes->rx_multicast, &es->rx_multicast);
657 i40e_stat_update48(hw, I40E_GLV_BPRCH(stat_idx),
658 I40E_GLV_BPRCL(stat_idx),
659 vsi->stat_offsets_loaded,
660 &oes->rx_broadcast, &es->rx_broadcast);
661
662 i40e_stat_update48(hw, I40E_GLV_GOTCH(stat_idx),
663 I40E_GLV_GOTCL(stat_idx),
664 vsi->stat_offsets_loaded,
665 &oes->tx_bytes, &es->tx_bytes);
666 i40e_stat_update48(hw, I40E_GLV_UPTCH(stat_idx),
667 I40E_GLV_UPTCL(stat_idx),
668 vsi->stat_offsets_loaded,
669 &oes->tx_unicast, &es->tx_unicast);
670 i40e_stat_update48(hw, I40E_GLV_MPTCH(stat_idx),
671 I40E_GLV_MPTCL(stat_idx),
672 vsi->stat_offsets_loaded,
673 &oes->tx_multicast, &es->tx_multicast);
674 i40e_stat_update48(hw, I40E_GLV_BPTCH(stat_idx),
675 I40E_GLV_BPTCL(stat_idx),
676 vsi->stat_offsets_loaded,
677 &oes->tx_broadcast, &es->tx_broadcast);
678 vsi->stat_offsets_loaded = true;
679}
680
681/**
682 * i40e_update_veb_stats - Update Switch component statistics
683 * @veb: the VEB being updated
684 **/
685static void i40e_update_veb_stats(struct i40e_veb *veb)
686{
687 struct i40e_pf *pf = veb->pf;
688 struct i40e_hw *hw = &pf->hw;
689 struct i40e_eth_stats *oes;
690 struct i40e_eth_stats *es; /* device's eth stats */
Neerav Parikhfe860af2015-07-10 19:36:02 -0400691 struct i40e_veb_tc_stats *veb_oes;
692 struct i40e_veb_tc_stats *veb_es;
693 int i, idx = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000694
695 idx = veb->stats_idx;
696 es = &veb->stats;
697 oes = &veb->stats_offsets;
Neerav Parikhfe860af2015-07-10 19:36:02 -0400698 veb_es = &veb->tc_stats;
699 veb_oes = &veb->tc_stats_offsets;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000700
701 /* Gather up the stats that the hw collects */
702 i40e_stat_update32(hw, I40E_GLSW_TDPC(idx),
703 veb->stat_offsets_loaded,
704 &oes->tx_discards, &es->tx_discards);
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +0000705 if (hw->revision_id > 0)
706 i40e_stat_update32(hw, I40E_GLSW_RUPP(idx),
707 veb->stat_offsets_loaded,
708 &oes->rx_unknown_protocol,
709 &es->rx_unknown_protocol);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000710 i40e_stat_update48(hw, I40E_GLSW_GORCH(idx), I40E_GLSW_GORCL(idx),
711 veb->stat_offsets_loaded,
712 &oes->rx_bytes, &es->rx_bytes);
713 i40e_stat_update48(hw, I40E_GLSW_UPRCH(idx), I40E_GLSW_UPRCL(idx),
714 veb->stat_offsets_loaded,
715 &oes->rx_unicast, &es->rx_unicast);
716 i40e_stat_update48(hw, I40E_GLSW_MPRCH(idx), I40E_GLSW_MPRCL(idx),
717 veb->stat_offsets_loaded,
718 &oes->rx_multicast, &es->rx_multicast);
719 i40e_stat_update48(hw, I40E_GLSW_BPRCH(idx), I40E_GLSW_BPRCL(idx),
720 veb->stat_offsets_loaded,
721 &oes->rx_broadcast, &es->rx_broadcast);
722
723 i40e_stat_update48(hw, I40E_GLSW_GOTCH(idx), I40E_GLSW_GOTCL(idx),
724 veb->stat_offsets_loaded,
725 &oes->tx_bytes, &es->tx_bytes);
726 i40e_stat_update48(hw, I40E_GLSW_UPTCH(idx), I40E_GLSW_UPTCL(idx),
727 veb->stat_offsets_loaded,
728 &oes->tx_unicast, &es->tx_unicast);
729 i40e_stat_update48(hw, I40E_GLSW_MPTCH(idx), I40E_GLSW_MPTCL(idx),
730 veb->stat_offsets_loaded,
731 &oes->tx_multicast, &es->tx_multicast);
732 i40e_stat_update48(hw, I40E_GLSW_BPTCH(idx), I40E_GLSW_BPTCL(idx),
733 veb->stat_offsets_loaded,
734 &oes->tx_broadcast, &es->tx_broadcast);
Neerav Parikhfe860af2015-07-10 19:36:02 -0400735 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
736 i40e_stat_update48(hw, I40E_GLVEBTC_RPCH(i, idx),
737 I40E_GLVEBTC_RPCL(i, idx),
738 veb->stat_offsets_loaded,
739 &veb_oes->tc_rx_packets[i],
740 &veb_es->tc_rx_packets[i]);
741 i40e_stat_update48(hw, I40E_GLVEBTC_RBCH(i, idx),
742 I40E_GLVEBTC_RBCL(i, idx),
743 veb->stat_offsets_loaded,
744 &veb_oes->tc_rx_bytes[i],
745 &veb_es->tc_rx_bytes[i]);
746 i40e_stat_update48(hw, I40E_GLVEBTC_TPCH(i, idx),
747 I40E_GLVEBTC_TPCL(i, idx),
748 veb->stat_offsets_loaded,
749 &veb_oes->tc_tx_packets[i],
750 &veb_es->tc_tx_packets[i]);
751 i40e_stat_update48(hw, I40E_GLVEBTC_TBCH(i, idx),
752 I40E_GLVEBTC_TBCL(i, idx),
753 veb->stat_offsets_loaded,
754 &veb_oes->tc_tx_bytes[i],
755 &veb_es->tc_tx_bytes[i]);
756 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000757 veb->stat_offsets_loaded = true;
758}
759
760/**
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000761 * i40e_update_vsi_stats - Update the vsi statistics counters.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000762 * @vsi: the VSI to be updated
763 *
764 * There are a few instances where we store the same stat in a
765 * couple of different structs. This is partly because we have
766 * the netdev stats that need to be filled out, which is slightly
767 * different from the "eth_stats" defined by the chip and used in
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000768 * VF communications. We sort it out here.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000769 **/
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000770static void i40e_update_vsi_stats(struct i40e_vsi *vsi)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000771{
772 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000773 struct rtnl_link_stats64 *ons;
774 struct rtnl_link_stats64 *ns; /* netdev stats */
775 struct i40e_eth_stats *oes;
776 struct i40e_eth_stats *es; /* device's eth stats */
777 u32 tx_restart, tx_busy;
Akeem G Abodunrinbf00b372014-10-17 03:14:39 +0000778 struct i40e_ring *p;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000779 u32 rx_page, rx_buf;
Akeem G Abodunrinbf00b372014-10-17 03:14:39 +0000780 u64 bytes, packets;
781 unsigned int start;
Anjali Singhai Jain2fc3d712015-08-27 11:42:29 -0400782 u64 tx_linearize;
Anjali Singhai Jain164c9f52015-10-21 19:47:08 -0400783 u64 tx_force_wb;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000784 u64 rx_p, rx_b;
785 u64 tx_p, tx_b;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000786 u16 q;
787
Jacob Keller0da36b92017-04-19 09:25:55 -0400788 if (test_bit(__I40E_VSI_DOWN, vsi->state) ||
789 test_bit(__I40E_CONFIG_BUSY, pf->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000790 return;
791
792 ns = i40e_get_vsi_stats_struct(vsi);
793 ons = &vsi->net_stats_offsets;
794 es = &vsi->eth_stats;
795 oes = &vsi->eth_stats_offsets;
796
797 /* Gather up the netdev and vsi stats that the driver collects
798 * on the fly during packet processing
799 */
800 rx_b = rx_p = 0;
801 tx_b = tx_p = 0;
Anjali Singhai Jain164c9f52015-10-21 19:47:08 -0400802 tx_restart = tx_busy = tx_linearize = tx_force_wb = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000803 rx_page = 0;
804 rx_buf = 0;
Alexander Duyck980e9b12013-09-28 06:01:03 +0000805 rcu_read_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000806 for (q = 0; q < vsi->num_queue_pairs; q++) {
Alexander Duyck980e9b12013-09-28 06:01:03 +0000807 /* locate Tx ring */
808 p = ACCESS_ONCE(vsi->tx_rings[q]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000809
Alexander Duyck980e9b12013-09-28 06:01:03 +0000810 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 tx_b += bytes;
816 tx_p += packets;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000817 tx_restart += p->tx_stats.restart_queue;
818 tx_busy += p->tx_stats.tx_busy;
Anjali Singhai Jain2fc3d712015-08-27 11:42:29 -0400819 tx_linearize += p->tx_stats.tx_linearize;
Anjali Singhai Jain164c9f52015-10-21 19:47:08 -0400820 tx_force_wb += p->tx_stats.tx_force_wb;
Alexander Duyck980e9b12013-09-28 06:01:03 +0000821
822 /* Rx queue is part of the same block as Tx queue */
823 p = &p[1];
824 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700825 start = u64_stats_fetch_begin_irq(&p->syncp);
Alexander Duyck980e9b12013-09-28 06:01:03 +0000826 packets = p->stats.packets;
827 bytes = p->stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700828 } while (u64_stats_fetch_retry_irq(&p->syncp, start));
Alexander Duyck980e9b12013-09-28 06:01:03 +0000829 rx_b += bytes;
830 rx_p += packets;
Mitch Williams420136c2013-12-18 13:45:59 +0000831 rx_buf += p->rx_stats.alloc_buff_failed;
832 rx_page += p->rx_stats.alloc_page_failed;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000833 }
Alexander Duyck980e9b12013-09-28 06:01:03 +0000834 rcu_read_unlock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000835 vsi->tx_restart = tx_restart;
836 vsi->tx_busy = tx_busy;
Anjali Singhai Jain2fc3d712015-08-27 11:42:29 -0400837 vsi->tx_linearize = tx_linearize;
Anjali Singhai Jain164c9f52015-10-21 19:47:08 -0400838 vsi->tx_force_wb = tx_force_wb;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000839 vsi->rx_page_failed = rx_page;
840 vsi->rx_buf_failed = rx_buf;
841
842 ns->rx_packets = rx_p;
843 ns->rx_bytes = rx_b;
844 ns->tx_packets = tx_p;
845 ns->tx_bytes = tx_b;
846
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000847 /* update netdev stats from eth stats */
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000848 i40e_update_eth_stats(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000849 ons->tx_errors = oes->tx_errors;
850 ns->tx_errors = es->tx_errors;
851 ons->multicast = oes->rx_multicast;
852 ns->multicast = es->rx_multicast;
Shannon Nelson41a9e552014-04-23 04:50:20 +0000853 ons->rx_dropped = oes->rx_discards;
854 ns->rx_dropped = es->rx_discards;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000855 ons->tx_dropped = oes->tx_discards;
856 ns->tx_dropped = es->tx_discards;
857
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000858 /* pull in a couple PF stats if this is the main vsi */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000859 if (vsi == pf->vsi[pf->lan_vsi]) {
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000860 ns->rx_crc_errors = pf->stats.crc_errors;
861 ns->rx_errors = pf->stats.crc_errors + pf->stats.illegal_bytes;
862 ns->rx_length_errors = pf->stats.rx_length_errors;
863 }
864}
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000865
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000866/**
Jeff Kirsherb40c82e62015-02-27 09:18:34 +0000867 * i40e_update_pf_stats - Update the PF statistics counters.
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000868 * @pf: the PF to be updated
869 **/
870static void i40e_update_pf_stats(struct i40e_pf *pf)
871{
872 struct i40e_hw_port_stats *osd = &pf->stats_offsets;
873 struct i40e_hw_port_stats *nsd = &pf->stats;
874 struct i40e_hw *hw = &pf->hw;
875 u32 val;
876 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000877
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000878 i40e_stat_update48(hw, I40E_GLPRT_GORCH(hw->port),
879 I40E_GLPRT_GORCL(hw->port),
880 pf->stat_offsets_loaded,
881 &osd->eth.rx_bytes, &nsd->eth.rx_bytes);
882 i40e_stat_update48(hw, I40E_GLPRT_GOTCH(hw->port),
883 I40E_GLPRT_GOTCL(hw->port),
884 pf->stat_offsets_loaded,
885 &osd->eth.tx_bytes, &nsd->eth.tx_bytes);
886 i40e_stat_update32(hw, I40E_GLPRT_RDPC(hw->port),
887 pf->stat_offsets_loaded,
888 &osd->eth.rx_discards,
889 &nsd->eth.rx_discards);
Shannon Nelson532d2832014-04-23 04:50:09 +0000890 i40e_stat_update48(hw, I40E_GLPRT_UPRCH(hw->port),
891 I40E_GLPRT_UPRCL(hw->port),
892 pf->stat_offsets_loaded,
893 &osd->eth.rx_unicast,
894 &nsd->eth.rx_unicast);
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000895 i40e_stat_update48(hw, I40E_GLPRT_MPRCH(hw->port),
896 I40E_GLPRT_MPRCL(hw->port),
897 pf->stat_offsets_loaded,
898 &osd->eth.rx_multicast,
899 &nsd->eth.rx_multicast);
Shannon Nelson532d2832014-04-23 04:50:09 +0000900 i40e_stat_update48(hw, I40E_GLPRT_BPRCH(hw->port),
901 I40E_GLPRT_BPRCL(hw->port),
902 pf->stat_offsets_loaded,
903 &osd->eth.rx_broadcast,
904 &nsd->eth.rx_broadcast);
905 i40e_stat_update48(hw, I40E_GLPRT_UPTCH(hw->port),
906 I40E_GLPRT_UPTCL(hw->port),
907 pf->stat_offsets_loaded,
908 &osd->eth.tx_unicast,
909 &nsd->eth.tx_unicast);
910 i40e_stat_update48(hw, I40E_GLPRT_MPTCH(hw->port),
911 I40E_GLPRT_MPTCL(hw->port),
912 pf->stat_offsets_loaded,
913 &osd->eth.tx_multicast,
914 &nsd->eth.tx_multicast);
915 i40e_stat_update48(hw, I40E_GLPRT_BPTCH(hw->port),
916 I40E_GLPRT_BPTCL(hw->port),
917 pf->stat_offsets_loaded,
918 &osd->eth.tx_broadcast,
919 &nsd->eth.tx_broadcast);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000920
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000921 i40e_stat_update32(hw, I40E_GLPRT_TDOLD(hw->port),
922 pf->stat_offsets_loaded,
923 &osd->tx_dropped_link_down,
924 &nsd->tx_dropped_link_down);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000925
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000926 i40e_stat_update32(hw, I40E_GLPRT_CRCERRS(hw->port),
927 pf->stat_offsets_loaded,
928 &osd->crc_errors, &nsd->crc_errors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000929
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000930 i40e_stat_update32(hw, I40E_GLPRT_ILLERRC(hw->port),
931 pf->stat_offsets_loaded,
932 &osd->illegal_bytes, &nsd->illegal_bytes);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000933
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000934 i40e_stat_update32(hw, I40E_GLPRT_MLFC(hw->port),
935 pf->stat_offsets_loaded,
936 &osd->mac_local_faults,
937 &nsd->mac_local_faults);
938 i40e_stat_update32(hw, I40E_GLPRT_MRFC(hw->port),
939 pf->stat_offsets_loaded,
940 &osd->mac_remote_faults,
941 &nsd->mac_remote_faults);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000942
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000943 i40e_stat_update32(hw, I40E_GLPRT_RLEC(hw->port),
944 pf->stat_offsets_loaded,
945 &osd->rx_length_errors,
946 &nsd->rx_length_errors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000947
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000948 i40e_stat_update32(hw, I40E_GLPRT_LXONRXC(hw->port),
949 pf->stat_offsets_loaded,
950 &osd->link_xon_rx, &nsd->link_xon_rx);
951 i40e_stat_update32(hw, I40E_GLPRT_LXONTXC(hw->port),
952 pf->stat_offsets_loaded,
953 &osd->link_xon_tx, &nsd->link_xon_tx);
Neerav Parikh95db2392015-11-06 15:26:09 -0800954 i40e_stat_update32(hw, I40E_GLPRT_LXOFFRXC(hw->port),
955 pf->stat_offsets_loaded,
956 &osd->link_xoff_rx, &nsd->link_xoff_rx);
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000957 i40e_stat_update32(hw, I40E_GLPRT_LXOFFTXC(hw->port),
958 pf->stat_offsets_loaded,
959 &osd->link_xoff_tx, &nsd->link_xoff_tx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000960
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000961 for (i = 0; i < 8; i++) {
Neerav Parikh95db2392015-11-06 15:26:09 -0800962 i40e_stat_update32(hw, I40E_GLPRT_PXOFFRXC(hw->port, i),
963 pf->stat_offsets_loaded,
964 &osd->priority_xoff_rx[i],
965 &nsd->priority_xoff_rx[i]);
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000966 i40e_stat_update32(hw, I40E_GLPRT_PXONRXC(hw->port, i),
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000967 pf->stat_offsets_loaded,
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000968 &osd->priority_xon_rx[i],
969 &nsd->priority_xon_rx[i]);
970 i40e_stat_update32(hw, I40E_GLPRT_PXONTXC(hw->port, i),
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000971 pf->stat_offsets_loaded,
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000972 &osd->priority_xon_tx[i],
973 &nsd->priority_xon_tx[i]);
974 i40e_stat_update32(hw, I40E_GLPRT_PXOFFTXC(hw->port, i),
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000975 pf->stat_offsets_loaded,
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000976 &osd->priority_xoff_tx[i],
977 &nsd->priority_xoff_tx[i]);
978 i40e_stat_update32(hw,
979 I40E_GLPRT_RXON2OFFCNT(hw->port, i),
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000980 pf->stat_offsets_loaded,
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000981 &osd->priority_xon_2_xoff[i],
982 &nsd->priority_xon_2_xoff[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000983 }
984
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000985 i40e_stat_update48(hw, I40E_GLPRT_PRC64H(hw->port),
986 I40E_GLPRT_PRC64L(hw->port),
987 pf->stat_offsets_loaded,
988 &osd->rx_size_64, &nsd->rx_size_64);
989 i40e_stat_update48(hw, I40E_GLPRT_PRC127H(hw->port),
990 I40E_GLPRT_PRC127L(hw->port),
991 pf->stat_offsets_loaded,
992 &osd->rx_size_127, &nsd->rx_size_127);
993 i40e_stat_update48(hw, I40E_GLPRT_PRC255H(hw->port),
994 I40E_GLPRT_PRC255L(hw->port),
995 pf->stat_offsets_loaded,
996 &osd->rx_size_255, &nsd->rx_size_255);
997 i40e_stat_update48(hw, I40E_GLPRT_PRC511H(hw->port),
998 I40E_GLPRT_PRC511L(hw->port),
999 pf->stat_offsets_loaded,
1000 &osd->rx_size_511, &nsd->rx_size_511);
1001 i40e_stat_update48(hw, I40E_GLPRT_PRC1023H(hw->port),
1002 I40E_GLPRT_PRC1023L(hw->port),
1003 pf->stat_offsets_loaded,
1004 &osd->rx_size_1023, &nsd->rx_size_1023);
1005 i40e_stat_update48(hw, I40E_GLPRT_PRC1522H(hw->port),
1006 I40E_GLPRT_PRC1522L(hw->port),
1007 pf->stat_offsets_loaded,
1008 &osd->rx_size_1522, &nsd->rx_size_1522);
1009 i40e_stat_update48(hw, I40E_GLPRT_PRC9522H(hw->port),
1010 I40E_GLPRT_PRC9522L(hw->port),
1011 pf->stat_offsets_loaded,
1012 &osd->rx_size_big, &nsd->rx_size_big);
1013
1014 i40e_stat_update48(hw, I40E_GLPRT_PTC64H(hw->port),
1015 I40E_GLPRT_PTC64L(hw->port),
1016 pf->stat_offsets_loaded,
1017 &osd->tx_size_64, &nsd->tx_size_64);
1018 i40e_stat_update48(hw, I40E_GLPRT_PTC127H(hw->port),
1019 I40E_GLPRT_PTC127L(hw->port),
1020 pf->stat_offsets_loaded,
1021 &osd->tx_size_127, &nsd->tx_size_127);
1022 i40e_stat_update48(hw, I40E_GLPRT_PTC255H(hw->port),
1023 I40E_GLPRT_PTC255L(hw->port),
1024 pf->stat_offsets_loaded,
1025 &osd->tx_size_255, &nsd->tx_size_255);
1026 i40e_stat_update48(hw, I40E_GLPRT_PTC511H(hw->port),
1027 I40E_GLPRT_PTC511L(hw->port),
1028 pf->stat_offsets_loaded,
1029 &osd->tx_size_511, &nsd->tx_size_511);
1030 i40e_stat_update48(hw, I40E_GLPRT_PTC1023H(hw->port),
1031 I40E_GLPRT_PTC1023L(hw->port),
1032 pf->stat_offsets_loaded,
1033 &osd->tx_size_1023, &nsd->tx_size_1023);
1034 i40e_stat_update48(hw, I40E_GLPRT_PTC1522H(hw->port),
1035 I40E_GLPRT_PTC1522L(hw->port),
1036 pf->stat_offsets_loaded,
1037 &osd->tx_size_1522, &nsd->tx_size_1522);
1038 i40e_stat_update48(hw, I40E_GLPRT_PTC9522H(hw->port),
1039 I40E_GLPRT_PTC9522L(hw->port),
1040 pf->stat_offsets_loaded,
1041 &osd->tx_size_big, &nsd->tx_size_big);
1042
1043 i40e_stat_update32(hw, I40E_GLPRT_RUC(hw->port),
1044 pf->stat_offsets_loaded,
1045 &osd->rx_undersize, &nsd->rx_undersize);
1046 i40e_stat_update32(hw, I40E_GLPRT_RFC(hw->port),
1047 pf->stat_offsets_loaded,
1048 &osd->rx_fragments, &nsd->rx_fragments);
1049 i40e_stat_update32(hw, I40E_GLPRT_ROC(hw->port),
1050 pf->stat_offsets_loaded,
1051 &osd->rx_oversize, &nsd->rx_oversize);
1052 i40e_stat_update32(hw, I40E_GLPRT_RJC(hw->port),
1053 pf->stat_offsets_loaded,
1054 &osd->rx_jabber, &nsd->rx_jabber);
1055
Anjali Singhai Jain433c47d2014-05-22 06:32:17 +00001056 /* FDIR stats */
Mariusz Stachura0dc86922017-07-14 09:27:00 -04001057 i40e_stat_update_and_clear32(hw,
1058 I40E_GLQF_PCNT(I40E_FD_ATR_STAT_IDX(hw->pf_id)),
1059 &nsd->fd_atr_match);
1060 i40e_stat_update_and_clear32(hw,
1061 I40E_GLQF_PCNT(I40E_FD_SB_STAT_IDX(hw->pf_id)),
1062 &nsd->fd_sb_match);
1063 i40e_stat_update_and_clear32(hw,
1064 I40E_GLQF_PCNT(I40E_FD_ATR_TUNNEL_STAT_IDX(hw->pf_id)),
1065 &nsd->fd_atr_tunnel_match);
Anjali Singhai Jain433c47d2014-05-22 06:32:17 +00001066
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001067 val = rd32(hw, I40E_PRTPM_EEE_STAT);
1068 nsd->tx_lpi_status =
1069 (val & I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_MASK) >>
1070 I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_SHIFT;
1071 nsd->rx_lpi_status =
1072 (val & I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_MASK) >>
1073 I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_SHIFT;
1074 i40e_stat_update32(hw, I40E_PRTPM_TLPIC,
1075 pf->stat_offsets_loaded,
1076 &osd->tx_lpi_count, &nsd->tx_lpi_count);
1077 i40e_stat_update32(hw, I40E_PRTPM_RLPIC,
1078 pf->stat_offsets_loaded,
1079 &osd->rx_lpi_count, &nsd->rx_lpi_count);
1080
Anjali Singhai Jaind0389e52015-04-22 19:34:05 -04001081 if (pf->flags & I40E_FLAG_FD_SB_ENABLED &&
Jacob Keller47994c12017-04-19 09:25:57 -04001082 !(pf->flags & I40E_FLAG_FD_SB_AUTO_DISABLED))
Anjali Singhai Jaind0389e52015-04-22 19:34:05 -04001083 nsd->fd_sb_status = true;
1084 else
1085 nsd->fd_sb_status = false;
1086
1087 if (pf->flags & I40E_FLAG_FD_ATR_ENABLED &&
Jacob Keller47994c12017-04-19 09:25:57 -04001088 !(pf->flags & I40E_FLAG_FD_ATR_AUTO_DISABLED))
Anjali Singhai Jaind0389e52015-04-22 19:34:05 -04001089 nsd->fd_atr_status = true;
1090 else
1091 nsd->fd_atr_status = false;
1092
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001093 pf->stat_offsets_loaded = true;
1094}
1095
1096/**
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001097 * i40e_update_stats - Update the various statistics counters.
1098 * @vsi: the VSI to be updated
1099 *
1100 * Update the various stats for this VSI and its related entities.
1101 **/
1102void i40e_update_stats(struct i40e_vsi *vsi)
1103{
1104 struct i40e_pf *pf = vsi->back;
1105
1106 if (vsi == pf->vsi[pf->lan_vsi])
1107 i40e_update_pf_stats(pf);
1108
1109 i40e_update_vsi_stats(vsi);
1110}
1111
1112/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001113 * i40e_find_filter - Search VSI filter list for specific mac/vlan filter
1114 * @vsi: the VSI to be searched
1115 * @macaddr: the MAC address
1116 * @vlan: the vlan
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001117 *
1118 * Returns ptr to the filter object or NULL
1119 **/
1120static struct i40e_mac_filter *i40e_find_filter(struct i40e_vsi *vsi,
Jacob Keller6622f5c2016-10-05 09:30:32 -07001121 const u8 *macaddr, s16 vlan)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001122{
1123 struct i40e_mac_filter *f;
Jacob Keller278e7d02016-10-05 09:30:37 -07001124 u64 key;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001125
1126 if (!vsi || !macaddr)
1127 return NULL;
1128
Jacob Keller278e7d02016-10-05 09:30:37 -07001129 key = i40e_addr_to_hkey(macaddr);
1130 hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001131 if ((ether_addr_equal(macaddr, f->macaddr)) &&
Jacob Keller1bc87e82016-10-05 09:30:31 -07001132 (vlan == f->vlan))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001133 return f;
1134 }
1135 return NULL;
1136}
1137
1138/**
1139 * i40e_find_mac - Find a mac addr in the macvlan filters list
1140 * @vsi: the VSI to be searched
1141 * @macaddr: the MAC address we are searching for
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001142 *
1143 * Returns the first filter with the provided MAC address or NULL if
1144 * MAC address was not found
1145 **/
Jacob Keller6622f5c2016-10-05 09:30:32 -07001146struct i40e_mac_filter *i40e_find_mac(struct i40e_vsi *vsi, const u8 *macaddr)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001147{
1148 struct i40e_mac_filter *f;
Jacob Keller278e7d02016-10-05 09:30:37 -07001149 u64 key;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001150
1151 if (!vsi || !macaddr)
1152 return NULL;
1153
Jacob Keller278e7d02016-10-05 09:30:37 -07001154 key = i40e_addr_to_hkey(macaddr);
1155 hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
Jacob Keller1bc87e82016-10-05 09:30:31 -07001156 if ((ether_addr_equal(macaddr, f->macaddr)))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001157 return f;
1158 }
1159 return NULL;
1160}
1161
1162/**
1163 * i40e_is_vsi_in_vlan - Check if VSI is in vlan mode
1164 * @vsi: the VSI to be searched
1165 *
1166 * Returns true if VSI is in vlan mode or false otherwise
1167 **/
1168bool i40e_is_vsi_in_vlan(struct i40e_vsi *vsi)
1169{
Jacob Kellercbebb852016-10-05 09:30:40 -07001170 /* If we have a PVID, always operate in VLAN mode */
1171 if (vsi->info.pvid)
1172 return true;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001173
Jacob Kellercbebb852016-10-05 09:30:40 -07001174 /* We need to operate in VLAN mode whenever we have any filters with
1175 * a VLAN other than I40E_VLAN_ALL. We could check the table each
1176 * time, incurring search cost repeatedly. However, we can notice two
1177 * things:
1178 *
1179 * 1) the only place where we can gain a VLAN filter is in
1180 * i40e_add_filter.
1181 *
1182 * 2) the only place where filters are actually removed is in
Jacob Keller0b7c8b52016-10-25 16:08:52 -07001183 * i40e_sync_filters_subtask.
Jacob Kellercbebb852016-10-05 09:30:40 -07001184 *
1185 * Thus, we can simply use a boolean value, has_vlan_filters which we
1186 * will set to true when we add a VLAN filter in i40e_add_filter. Then
1187 * we have to perform the full search after deleting filters in
Jacob Keller0b7c8b52016-10-25 16:08:52 -07001188 * i40e_sync_filters_subtask, but we already have to search
Jacob Kellercbebb852016-10-05 09:30:40 -07001189 * filters here and can perform the check at the same time. This
1190 * results in avoiding embedding a loop for VLAN mode inside another
1191 * loop over all the filters, and should maintain correctness as noted
1192 * above.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001193 */
Jacob Kellercbebb852016-10-05 09:30:40 -07001194 return vsi->has_vlan_filter;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001195}
1196
1197/**
Jacob Keller489a3262016-11-11 12:39:31 -08001198 * i40e_correct_mac_vlan_filters - Correct non-VLAN filters if necessary
1199 * @vsi: the VSI to configure
1200 * @tmp_add_list: list of filters ready to be added
1201 * @tmp_del_list: list of filters ready to be deleted
1202 * @vlan_filters: the number of active VLAN filters
1203 *
1204 * Update VLAN=0 and VLAN=-1 (I40E_VLAN_ANY) filters properly so that they
1205 * behave as expected. If we have any active VLAN filters remaining or about
1206 * to be added then we need to update non-VLAN filters to be marked as VLAN=0
1207 * so that they only match against untagged traffic. If we no longer have any
1208 * active VLAN filters, we need to make all non-VLAN filters marked as VLAN=-1
1209 * so that they match against both tagged and untagged traffic. In this way,
1210 * we ensure that we correctly receive the desired traffic. This ensures that
1211 * when we have an active VLAN we will receive only untagged traffic and
1212 * traffic matching active VLANs. If we have no active VLANs then we will
1213 * operate in non-VLAN mode and receive all traffic, tagged or untagged.
1214 *
1215 * Finally, in a similar fashion, this function also corrects filters when
1216 * there is an active PVID assigned to this VSI.
1217 *
1218 * In case of memory allocation failure return -ENOMEM. Otherwise, return 0.
1219 *
1220 * This function is only expected to be called from within
1221 * i40e_sync_vsi_filters.
1222 *
1223 * NOTE: This function expects to be called while under the
1224 * mac_filter_hash_lock
1225 */
1226static int i40e_correct_mac_vlan_filters(struct i40e_vsi *vsi,
1227 struct hlist_head *tmp_add_list,
1228 struct hlist_head *tmp_del_list,
1229 int vlan_filters)
1230{
Jacob Keller5cb25902016-12-12 15:44:15 -08001231 s16 pvid = le16_to_cpu(vsi->info.pvid);
Jacob Keller489a3262016-11-11 12:39:31 -08001232 struct i40e_mac_filter *f, *add_head;
Jacob Keller671889e2016-12-02 12:33:00 -08001233 struct i40e_new_mac_filter *new;
Jacob Keller489a3262016-11-11 12:39:31 -08001234 struct hlist_node *h;
1235 int bkt, new_vlan;
1236
1237 /* To determine if a particular filter needs to be replaced we
1238 * have the three following conditions:
1239 *
1240 * a) if we have a PVID assigned, then all filters which are
1241 * not marked as VLAN=PVID must be replaced with filters that
1242 * are.
1243 * b) otherwise, if we have any active VLANS, all filters
1244 * which are marked as VLAN=-1 must be replaced with
1245 * filters marked as VLAN=0
1246 * c) finally, if we do not have any active VLANS, all filters
1247 * which are marked as VLAN=0 must be replaced with filters
1248 * marked as VLAN=-1
1249 */
1250
1251 /* Update the filters about to be added in place */
Jacob Keller671889e2016-12-02 12:33:00 -08001252 hlist_for_each_entry(new, tmp_add_list, hlist) {
Jacob Keller5cb25902016-12-12 15:44:15 -08001253 if (pvid && new->f->vlan != pvid)
1254 new->f->vlan = pvid;
Jacob Keller671889e2016-12-02 12:33:00 -08001255 else if (vlan_filters && new->f->vlan == I40E_VLAN_ANY)
1256 new->f->vlan = 0;
1257 else if (!vlan_filters && new->f->vlan == 0)
1258 new->f->vlan = I40E_VLAN_ANY;
Jacob Keller489a3262016-11-11 12:39:31 -08001259 }
1260
1261 /* Update the remaining active filters */
1262 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1263 /* Combine the checks for whether a filter needs to be changed
1264 * and then determine the new VLAN inside the if block, in
1265 * order to avoid duplicating code for adding the new filter
1266 * then deleting the old filter.
1267 */
Jacob Keller5cb25902016-12-12 15:44:15 -08001268 if ((pvid && f->vlan != pvid) ||
Jacob Keller489a3262016-11-11 12:39:31 -08001269 (vlan_filters && f->vlan == I40E_VLAN_ANY) ||
1270 (!vlan_filters && f->vlan == 0)) {
1271 /* Determine the new vlan we will be adding */
Jacob Keller5cb25902016-12-12 15:44:15 -08001272 if (pvid)
1273 new_vlan = pvid;
Jacob Keller489a3262016-11-11 12:39:31 -08001274 else if (vlan_filters)
1275 new_vlan = 0;
1276 else
1277 new_vlan = I40E_VLAN_ANY;
1278
1279 /* Create the new filter */
1280 add_head = i40e_add_filter(vsi, f->macaddr, new_vlan);
1281 if (!add_head)
1282 return -ENOMEM;
1283
Jacob Keller671889e2016-12-02 12:33:00 -08001284 /* Create a temporary i40e_new_mac_filter */
1285 new = kzalloc(sizeof(*new), GFP_ATOMIC);
1286 if (!new)
1287 return -ENOMEM;
1288
1289 new->f = add_head;
1290 new->state = add_head->state;
1291
1292 /* Add the new filter to the tmp list */
1293 hlist_add_head(&new->hlist, tmp_add_list);
Jacob Keller489a3262016-11-11 12:39:31 -08001294
1295 /* Put the original filter into the delete list */
1296 f->state = I40E_FILTER_REMOVE;
1297 hash_del(&f->hlist);
1298 hlist_add_head(&f->hlist, tmp_del_list);
1299 }
1300 }
1301
1302 vsi->has_vlan_filter = !!vlan_filters;
1303
1304 return 0;
1305}
1306
1307/**
Jacob Keller1596b5d2016-11-08 13:05:15 -08001308 * i40e_rm_default_mac_filter - Remove the default MAC filter set by NVM
1309 * @vsi: the PF Main VSI - inappropriate for any other VSI
1310 * @macaddr: the MAC address
1311 *
1312 * Remove whatever filter the firmware set up so the driver can manage
1313 * its own filtering intelligently.
1314 **/
1315static void i40e_rm_default_mac_filter(struct i40e_vsi *vsi, u8 *macaddr)
1316{
1317 struct i40e_aqc_remove_macvlan_element_data element;
1318 struct i40e_pf *pf = vsi->back;
1319
1320 /* Only appropriate for the PF main VSI */
1321 if (vsi->type != I40E_VSI_MAIN)
1322 return;
1323
1324 memset(&element, 0, sizeof(element));
1325 ether_addr_copy(element.mac_addr, macaddr);
1326 element.vlan_tag = 0;
1327 /* Ignore error returns, some firmware does it this way... */
1328 element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
1329 i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1330
1331 memset(&element, 0, sizeof(element));
1332 ether_addr_copy(element.mac_addr, macaddr);
1333 element.vlan_tag = 0;
1334 /* ...and some firmware does it this way. */
1335 element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH |
1336 I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
1337 i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1338}
1339
1340/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001341 * i40e_add_filter - Add a mac/vlan filter to the VSI
1342 * @vsi: the VSI to be searched
1343 * @macaddr: the MAC address
1344 * @vlan: the vlan
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001345 *
1346 * Returns ptr to the filter object or NULL when no memory available.
Kiran Patil21659032015-09-30 14:09:03 -04001347 *
Jacob Keller278e7d02016-10-05 09:30:37 -07001348 * NOTE: This function is expected to be called with mac_filter_hash_lock
Kiran Patil21659032015-09-30 14:09:03 -04001349 * being held.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001350 **/
1351struct i40e_mac_filter *i40e_add_filter(struct i40e_vsi *vsi,
Jacob Keller6622f5c2016-10-05 09:30:32 -07001352 const u8 *macaddr, s16 vlan)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001353{
1354 struct i40e_mac_filter *f;
Jacob Keller278e7d02016-10-05 09:30:37 -07001355 u64 key;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001356
1357 if (!vsi || !macaddr)
1358 return NULL;
1359
Jacob Keller1bc87e82016-10-05 09:30:31 -07001360 f = i40e_find_filter(vsi, macaddr, vlan);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001361 if (!f) {
1362 f = kzalloc(sizeof(*f), GFP_ATOMIC);
1363 if (!f)
Jacob Keller1bc87e82016-10-05 09:30:31 -07001364 return NULL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001365
Jacob Kellercbebb852016-10-05 09:30:40 -07001366 /* Update the boolean indicating if we need to function in
1367 * VLAN mode.
1368 */
1369 if (vlan >= 0)
1370 vsi->has_vlan_filter = true;
1371
Greg Rose9a173902014-05-22 06:32:02 +00001372 ether_addr_copy(f->macaddr, macaddr);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001373 f->vlan = vlan;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001374 /* If we're in overflow promisc mode, set the state directly
1375 * to failed, so we don't bother to try sending the filter
1376 * to the hardware.
1377 */
Jacob Keller0da36b92017-04-19 09:25:55 -04001378 if (test_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state))
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001379 f->state = I40E_FILTER_FAILED;
1380 else
1381 f->state = I40E_FILTER_NEW;
Jacob Keller278e7d02016-10-05 09:30:37 -07001382 INIT_HLIST_NODE(&f->hlist);
1383
1384 key = i40e_addr_to_hkey(macaddr);
1385 hash_add(vsi->mac_filter_hash, &f->hlist, key);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001386
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001387 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1388 vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
1389 }
1390
Jacob Keller1bc87e82016-10-05 09:30:31 -07001391 /* If we're asked to add a filter that has been marked for removal, it
1392 * is safe to simply restore it to active state. __i40e_del_filter
1393 * will have simply deleted any filters which were previously marked
1394 * NEW or FAILED, so if it is currently marked REMOVE it must have
1395 * previously been ACTIVE. Since we haven't yet run the sync filters
1396 * task, just restore this filter to the ACTIVE state so that the
1397 * sync task leaves it in place
1398 */
1399 if (f->state == I40E_FILTER_REMOVE)
1400 f->state = I40E_FILTER_ACTIVE;
1401
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001402 return f;
1403}
1404
1405/**
Jacob Keller290d2552016-10-05 09:30:36 -07001406 * __i40e_del_filter - Remove a specific filter from the VSI
1407 * @vsi: VSI to remove from
1408 * @f: the filter to remove from the list
1409 *
1410 * This function should be called instead of i40e_del_filter only if you know
1411 * the exact filter you will remove already, such as via i40e_find_filter or
1412 * i40e_find_mac.
Kiran Patil21659032015-09-30 14:09:03 -04001413 *
Jacob Keller278e7d02016-10-05 09:30:37 -07001414 * NOTE: This function is expected to be called with mac_filter_hash_lock
Kiran Patil21659032015-09-30 14:09:03 -04001415 * being held.
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001416 * ANOTHER NOTE: This function MUST be called from within the context of
1417 * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
1418 * instead of list_for_each_entry().
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001419 **/
Jacob Keller148141b2016-11-11 12:39:36 -08001420void __i40e_del_filter(struct i40e_vsi *vsi, struct i40e_mac_filter *f)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001421{
Jacob Keller1bc87e82016-10-05 09:30:31 -07001422 if (!f)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001423 return;
1424
Alan Bradya410c822016-12-12 15:44:07 -08001425 /* If the filter was never added to firmware then we can just delete it
1426 * directly and we don't want to set the status to remove or else an
1427 * admin queue command will unnecessarily fire.
1428 */
Jacob Keller1bc87e82016-10-05 09:30:31 -07001429 if ((f->state == I40E_FILTER_FAILED) ||
1430 (f->state == I40E_FILTER_NEW)) {
Jacob Keller278e7d02016-10-05 09:30:37 -07001431 hash_del(&f->hlist);
Jacob Keller1bc87e82016-10-05 09:30:31 -07001432 kfree(f);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001433 } else {
Jacob Keller1bc87e82016-10-05 09:30:31 -07001434 f->state = I40E_FILTER_REMOVE;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001435 }
Alan Bradya410c822016-12-12 15:44:07 -08001436
1437 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1438 vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001439}
1440
1441/**
Jacob Keller290d2552016-10-05 09:30:36 -07001442 * i40e_del_filter - Remove a MAC/VLAN filter from the VSI
1443 * @vsi: the VSI to be searched
1444 * @macaddr: the MAC address
1445 * @vlan: the VLAN
1446 *
Jacob Keller278e7d02016-10-05 09:30:37 -07001447 * NOTE: This function is expected to be called with mac_filter_hash_lock
Jacob Keller290d2552016-10-05 09:30:36 -07001448 * being held.
1449 * ANOTHER NOTE: This function MUST be called from within the context of
1450 * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
1451 * instead of list_for_each_entry().
1452 **/
1453void i40e_del_filter(struct i40e_vsi *vsi, const u8 *macaddr, s16 vlan)
1454{
1455 struct i40e_mac_filter *f;
1456
1457 if (!vsi || !macaddr)
1458 return;
1459
1460 f = i40e_find_filter(vsi, macaddr, vlan);
1461 __i40e_del_filter(vsi, f);
1462}
1463
1464/**
Jacob Kellerfeffdbe2016-11-11 12:39:35 -08001465 * i40e_add_mac_filter - Add a MAC filter for all active VLANs
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001466 * @vsi: the VSI to be searched
1467 * @macaddr: the mac address to be filtered
1468 *
Jacob Kellerfeffdbe2016-11-11 12:39:35 -08001469 * If we're not in VLAN mode, just add the filter to I40E_VLAN_ANY. Otherwise,
1470 * go through all the macvlan filters and add a macvlan filter for each
Jacob Keller5feb3d72016-10-05 09:30:34 -07001471 * unique vlan that already exists. If a PVID has been assigned, instead only
1472 * add the macaddr to that VLAN.
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001473 *
Jacob Keller5feb3d72016-10-05 09:30:34 -07001474 * Returns last filter added on success, else NULL
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001475 **/
Jacob Kellerfeffdbe2016-11-11 12:39:35 -08001476struct i40e_mac_filter *i40e_add_mac_filter(struct i40e_vsi *vsi,
1477 const u8 *macaddr)
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001478{
Jacob Keller5feb3d72016-10-05 09:30:34 -07001479 struct i40e_mac_filter *f, *add = NULL;
Jacob Keller278e7d02016-10-05 09:30:37 -07001480 struct hlist_node *h;
1481 int bkt;
Jacob Keller5feb3d72016-10-05 09:30:34 -07001482
1483 if (vsi->info.pvid)
1484 return i40e_add_filter(vsi, macaddr,
1485 le16_to_cpu(vsi->info.pvid));
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001486
Jacob Keller7aaf95362016-11-11 12:39:33 -08001487 if (!i40e_is_vsi_in_vlan(vsi))
1488 return i40e_add_filter(vsi, macaddr, I40E_VLAN_ANY);
1489
Jacob Keller278e7d02016-10-05 09:30:37 -07001490 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Jacob Keller57b341d2016-10-05 09:30:35 -07001491 if (f->state == I40E_FILTER_REMOVE)
1492 continue;
Jacob Keller5feb3d72016-10-05 09:30:34 -07001493 add = i40e_add_filter(vsi, macaddr, f->vlan);
1494 if (!add)
1495 return NULL;
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001496 }
1497
Jacob Keller5feb3d72016-10-05 09:30:34 -07001498 return add;
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001499}
1500
1501/**
Jacob Kellerfeffdbe2016-11-11 12:39:35 -08001502 * i40e_del_mac_filter - Remove a MAC filter from all VLANs
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001503 * @vsi: the VSI to be searched
1504 * @macaddr: the mac address to be removed
1505 *
Jacob Kellerfeffdbe2016-11-11 12:39:35 -08001506 * Removes a given MAC address from a VSI regardless of what VLAN it has been
1507 * associated with.
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001508 *
1509 * Returns 0 for success, or error
1510 **/
Jacob Kellerfeffdbe2016-11-11 12:39:35 -08001511int i40e_del_mac_filter(struct i40e_vsi *vsi, const u8 *macaddr)
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001512{
Jacob Keller278e7d02016-10-05 09:30:37 -07001513 struct i40e_mac_filter *f;
1514 struct hlist_node *h;
Jacob Keller290d2552016-10-05 09:30:36 -07001515 bool found = false;
Jacob Keller278e7d02016-10-05 09:30:37 -07001516 int bkt;
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001517
Jacob Keller278e7d02016-10-05 09:30:37 -07001518 WARN(!spin_is_locked(&vsi->mac_filter_hash_lock),
1519 "Missing mac_filter_hash_lock\n");
1520 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Jacob Keller290d2552016-10-05 09:30:36 -07001521 if (ether_addr_equal(macaddr, f->macaddr)) {
1522 __i40e_del_filter(vsi, f);
1523 found = true;
1524 }
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001525 }
Jacob Keller290d2552016-10-05 09:30:36 -07001526
1527 if (found)
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001528 return 0;
Jacob Keller290d2552016-10-05 09:30:36 -07001529 else
1530 return -ENOENT;
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001531}
1532
1533/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001534 * i40e_set_mac - NDO callback to set mac address
1535 * @netdev: network interface device structure
1536 * @p: pointer to an address structure
1537 *
1538 * Returns 0 on success, negative on failure
1539 **/
1540static int i40e_set_mac(struct net_device *netdev, void *p)
1541{
1542 struct i40e_netdev_priv *np = netdev_priv(netdev);
1543 struct i40e_vsi *vsi = np->vsi;
Shannon Nelson30650cc2014-07-29 04:01:50 +00001544 struct i40e_pf *pf = vsi->back;
1545 struct i40e_hw *hw = &pf->hw;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001546 struct sockaddr *addr = p;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001547
1548 if (!is_valid_ether_addr(addr->sa_data))
1549 return -EADDRNOTAVAIL;
1550
Shannon Nelson30650cc2014-07-29 04:01:50 +00001551 if (ether_addr_equal(netdev->dev_addr, addr->sa_data)) {
1552 netdev_info(netdev, "already using mac address %pM\n",
1553 addr->sa_data);
1554 return 0;
1555 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001556
Jacob Keller0da36b92017-04-19 09:25:55 -04001557 if (test_bit(__I40E_VSI_DOWN, vsi->back->state) ||
1558 test_bit(__I40E_RESET_RECOVERY_PENDING, vsi->back->state))
Anjali Singhai Jain80f64282013-11-28 06:39:47 +00001559 return -EADDRNOTAVAIL;
1560
Shannon Nelson30650cc2014-07-29 04:01:50 +00001561 if (ether_addr_equal(hw->mac.addr, addr->sa_data))
1562 netdev_info(netdev, "returning to hw mac address %pM\n",
1563 hw->mac.addr);
1564 else
1565 netdev_info(netdev, "set new mac address %pM\n", addr->sa_data);
1566
Jacob Keller278e7d02016-10-05 09:30:37 -07001567 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Kellerfeffdbe2016-11-11 12:39:35 -08001568 i40e_del_mac_filter(vsi, netdev->dev_addr);
1569 i40e_add_mac_filter(vsi, addr->sa_data);
Jacob Keller278e7d02016-10-05 09:30:37 -07001570 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001571 ether_addr_copy(netdev->dev_addr, addr->sa_data);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001572 if (vsi->type == I40E_VSI_MAIN) {
1573 i40e_status ret;
Jesse Brandeburg6995b362015-08-28 17:55:54 -04001574
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001575 ret = i40e_aq_mac_address_write(&vsi->back->hw,
Shannon Nelsoncc412222014-06-04 01:23:21 +00001576 I40E_AQC_WRITE_TYPE_LAA_WOL,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001577 addr->sa_data, NULL);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001578 if (ret)
1579 netdev_info(netdev, "Ignoring error from firmware on LAA update, status %s, AQ ret %s\n",
1580 i40e_stat_str(hw, ret),
1581 i40e_aq_str(hw, hw->aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001582 }
1583
Jesse Brandeburgc53934c2016-01-04 10:33:06 -08001584 /* schedule our worker thread which will take care of
1585 * applying the new filter changes
1586 */
1587 i40e_service_event_schedule(vsi->back);
1588 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001589}
1590
1591/**
1592 * i40e_vsi_setup_queue_map - Setup a VSI queue map based on enabled_tc
1593 * @vsi: the VSI being setup
1594 * @ctxt: VSI context structure
1595 * @enabled_tc: Enabled TCs bitmap
1596 * @is_add: True if called before Add VSI
1597 *
1598 * Setup VSI queue mapping for enabled traffic classes.
1599 **/
1600static void i40e_vsi_setup_queue_map(struct i40e_vsi *vsi,
1601 struct i40e_vsi_context *ctxt,
1602 u8 enabled_tc,
1603 bool is_add)
1604{
1605 struct i40e_pf *pf = vsi->back;
1606 u16 sections = 0;
1607 u8 netdev_tc = 0;
1608 u16 numtc = 0;
1609 u16 qcount;
1610 u8 offset;
1611 u16 qmap;
1612 int i;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001613 u16 num_tc_qps = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001614
1615 sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
1616 offset = 0;
1617
1618 if (enabled_tc && (vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
1619 /* Find numtc from enabled TC bitmap */
1620 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08001621 if (enabled_tc & BIT(i)) /* TC is enabled */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001622 numtc++;
1623 }
1624 if (!numtc) {
1625 dev_warn(&pf->pdev->dev, "DCB is enabled but no TC enabled, forcing TC0\n");
1626 numtc = 1;
1627 }
1628 } else {
1629 /* At least TC0 is enabled in case of non-DCB case */
1630 numtc = 1;
1631 }
1632
1633 vsi->tc_config.numtc = numtc;
1634 vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001635 /* Number of queues per enabled TC */
Catherine Sullivan7d644022016-05-16 10:26:41 -07001636 qcount = vsi->alloc_queue_pairs;
1637
Anjali Singhai7f9ff472015-02-21 06:43:19 +00001638 num_tc_qps = qcount / numtc;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001639 num_tc_qps = min_t(int, num_tc_qps, i40e_pf_get_max_q_per_tc(pf));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001640
1641 /* Setup queue offset/count for all TCs for given VSI */
1642 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1643 /* See if the given TC is enabled for the given VSI */
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08001644 if (vsi->tc_config.enabled_tc & BIT(i)) {
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04001645 /* TC is enabled */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001646 int pow, num_qps;
1647
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001648 switch (vsi->type) {
1649 case I40E_VSI_MAIN:
Helin Zhangacd65442015-10-26 19:44:28 -04001650 qcount = min_t(int, pf->alloc_rss_size,
1651 num_tc_qps);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001652 break;
1653 case I40E_VSI_FDIR:
1654 case I40E_VSI_SRIOV:
1655 case I40E_VSI_VMDQ2:
1656 default:
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001657 qcount = num_tc_qps;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001658 WARN_ON(i != 0);
1659 break;
1660 }
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001661 vsi->tc_config.tc_info[i].qoffset = offset;
1662 vsi->tc_config.tc_info[i].qcount = qcount;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001663
Shannon Nelson1e200e42015-02-27 09:15:24 +00001664 /* find the next higher power-of-2 of num queue pairs */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001665 num_qps = qcount;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001666 pow = 0;
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04001667 while (num_qps && (BIT_ULL(pow) < qcount)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001668 pow++;
1669 num_qps >>= 1;
1670 }
1671
1672 vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
1673 qmap =
1674 (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
1675 (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
1676
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001677 offset += qcount;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001678 } else {
1679 /* TC is not enabled so set the offset to
1680 * default queue and allocate one queue
1681 * for the given TC.
1682 */
1683 vsi->tc_config.tc_info[i].qoffset = 0;
1684 vsi->tc_config.tc_info[i].qcount = 1;
1685 vsi->tc_config.tc_info[i].netdev_tc = 0;
1686
1687 qmap = 0;
1688 }
1689 ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
1690 }
1691
1692 /* Set actual Tx/Rx queue pairs */
1693 vsi->num_queue_pairs = offset;
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00001694 if ((vsi->type == I40E_VSI_MAIN) && (numtc == 1)) {
1695 if (vsi->req_queue_pairs > 0)
1696 vsi->num_queue_pairs = vsi->req_queue_pairs;
Anjali Singhai Jain26cdc442015-07-10 19:36:00 -04001697 else if (pf->flags & I40E_FLAG_MSIX_ENABLED)
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00001698 vsi->num_queue_pairs = pf->num_lan_msix;
1699 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001700
1701 /* Scheduler section valid can only be set for ADD VSI */
1702 if (is_add) {
1703 sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
1704
1705 ctxt->info.up_enable_bits = enabled_tc;
1706 }
1707 if (vsi->type == I40E_VSI_SRIOV) {
1708 ctxt->info.mapping_flags |=
1709 cpu_to_le16(I40E_AQ_VSI_QUE_MAP_NONCONTIG);
1710 for (i = 0; i < vsi->num_queue_pairs; i++)
1711 ctxt->info.queue_mapping[i] =
1712 cpu_to_le16(vsi->base_queue + i);
1713 } else {
1714 ctxt->info.mapping_flags |=
1715 cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
1716 ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
1717 }
1718 ctxt->info.valid_sections |= cpu_to_le16(sections);
1719}
1720
1721/**
Jacob Keller6622f5c2016-10-05 09:30:32 -07001722 * i40e_addr_sync - Callback for dev_(mc|uc)_sync to add address
1723 * @netdev: the netdevice
1724 * @addr: address to add
1725 *
1726 * Called by __dev_(mc|uc)_sync when an address needs to be added. We call
1727 * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
1728 */
1729static int i40e_addr_sync(struct net_device *netdev, const u8 *addr)
1730{
1731 struct i40e_netdev_priv *np = netdev_priv(netdev);
1732 struct i40e_vsi *vsi = np->vsi;
Jacob Keller6622f5c2016-10-05 09:30:32 -07001733
Jacob Kellerfeffdbe2016-11-11 12:39:35 -08001734 if (i40e_add_mac_filter(vsi, addr))
Jacob Keller6622f5c2016-10-05 09:30:32 -07001735 return 0;
1736 else
1737 return -ENOMEM;
1738}
1739
1740/**
1741 * i40e_addr_unsync - Callback for dev_(mc|uc)_sync to remove address
1742 * @netdev: the netdevice
1743 * @addr: address to add
1744 *
1745 * Called by __dev_(mc|uc)_sync when an address needs to be removed. We call
1746 * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
1747 */
1748static int i40e_addr_unsync(struct net_device *netdev, const u8 *addr)
1749{
1750 struct i40e_netdev_priv *np = netdev_priv(netdev);
1751 struct i40e_vsi *vsi = np->vsi;
1752
Jacob Kellerfeffdbe2016-11-11 12:39:35 -08001753 i40e_del_mac_filter(vsi, addr);
Jacob Keller6622f5c2016-10-05 09:30:32 -07001754
1755 return 0;
1756}
1757
1758/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001759 * i40e_set_rx_mode - NDO callback to set the netdev filters
1760 * @netdev: network interface device structure
1761 **/
1762static void i40e_set_rx_mode(struct net_device *netdev)
1763{
1764 struct i40e_netdev_priv *np = netdev_priv(netdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001765 struct i40e_vsi *vsi = np->vsi;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001766
Jacob Keller278e7d02016-10-05 09:30:37 -07001767 spin_lock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -04001768
Jacob Keller6622f5c2016-10-05 09:30:32 -07001769 __dev_uc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
1770 __dev_mc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001771
Jacob Keller278e7d02016-10-05 09:30:37 -07001772 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001773
1774 /* check for other flag changes */
1775 if (vsi->current_netdev_flags != vsi->netdev->flags) {
1776 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1777 vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
1778 }
Jesse Brandeburgc53934c2016-01-04 10:33:06 -08001779
1780 /* schedule our worker thread which will take care of
1781 * applying the new filter changes
1782 */
1783 i40e_service_event_schedule(vsi->back);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001784}
1785
1786/**
Jacob Keller671889e2016-12-02 12:33:00 -08001787 * i40e_undo_del_filter_entries - Undo the changes made to MAC filter entries
Jacob Keller4a2ce272016-10-05 09:30:38 -07001788 * @vsi: Pointer to VSI struct
Kiran Patil21659032015-09-30 14:09:03 -04001789 * @from: Pointer to list which contains MAC filter entries - changes to
1790 * those entries needs to be undone.
1791 *
Jacob Keller671889e2016-12-02 12:33:00 -08001792 * MAC filter entries from this list were slated for deletion.
Kiran Patil21659032015-09-30 14:09:03 -04001793 **/
Jacob Keller671889e2016-12-02 12:33:00 -08001794static void i40e_undo_del_filter_entries(struct i40e_vsi *vsi,
1795 struct hlist_head *from)
Kiran Patil21659032015-09-30 14:09:03 -04001796{
Jacob Keller278e7d02016-10-05 09:30:37 -07001797 struct i40e_mac_filter *f;
1798 struct hlist_node *h;
Kiran Patil21659032015-09-30 14:09:03 -04001799
Jacob Keller278e7d02016-10-05 09:30:37 -07001800 hlist_for_each_entry_safe(f, h, from, hlist) {
1801 u64 key = i40e_addr_to_hkey(f->macaddr);
1802
Kiran Patil21659032015-09-30 14:09:03 -04001803 /* Move the element back into MAC filter list*/
Jacob Keller278e7d02016-10-05 09:30:37 -07001804 hlist_del(&f->hlist);
1805 hash_add(vsi->mac_filter_hash, &f->hlist, key);
Kiran Patil21659032015-09-30 14:09:03 -04001806 }
1807}
1808
1809/**
Jacob Keller671889e2016-12-02 12:33:00 -08001810 * i40e_undo_add_filter_entries - Undo the changes made to MAC filter entries
1811 * @vsi: Pointer to vsi struct
1812 * @from: Pointer to list which contains MAC filter entries - changes to
1813 * those entries needs to be undone.
1814 *
1815 * MAC filter entries from this list were slated for addition.
1816 **/
1817static void i40e_undo_add_filter_entries(struct i40e_vsi *vsi,
1818 struct hlist_head *from)
1819{
1820 struct i40e_new_mac_filter *new;
1821 struct hlist_node *h;
1822
1823 hlist_for_each_entry_safe(new, h, from, hlist) {
1824 /* We can simply free the wrapper structure */
1825 hlist_del(&new->hlist);
1826 kfree(new);
1827 }
1828}
1829
1830/**
Jacob Kellerd88d40b2016-12-02 12:32:59 -08001831 * i40e_next_entry - Get the next non-broadcast filter from a list
Jacob Keller671889e2016-12-02 12:33:00 -08001832 * @next: pointer to filter in list
Jacob Kellerd88d40b2016-12-02 12:32:59 -08001833 *
1834 * Returns the next non-broadcast filter in the list. Required so that we
1835 * ignore broadcast filters within the list, since these are not handled via
1836 * the normal firmware update path.
1837 */
Jacob Keller671889e2016-12-02 12:33:00 -08001838static
1839struct i40e_new_mac_filter *i40e_next_filter(struct i40e_new_mac_filter *next)
Jacob Kellerd88d40b2016-12-02 12:32:59 -08001840{
Jacob Keller584a8872017-03-07 15:17:52 -08001841 hlist_for_each_entry_continue(next, hlist) {
1842 if (!is_broadcast_ether_addr(next->f->macaddr))
1843 return next;
Jacob Kellerd88d40b2016-12-02 12:32:59 -08001844 }
1845
Jacob Keller584a8872017-03-07 15:17:52 -08001846 return NULL;
Jacob Kellerd88d40b2016-12-02 12:32:59 -08001847}
1848
1849/**
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001850 * i40e_update_filter_state - Update filter state based on return data
1851 * from firmware
1852 * @count: Number of filters added
1853 * @add_list: return data from fw
1854 * @head: pointer to first filter in current batch
Kiran Patil21659032015-09-30 14:09:03 -04001855 *
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001856 * MAC filter entries from list were slated to be added to device. Returns
1857 * number of successful filters. Note that 0 does NOT mean success!
Kiran Patil21659032015-09-30 14:09:03 -04001858 **/
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001859static int
1860i40e_update_filter_state(int count,
1861 struct i40e_aqc_add_macvlan_element_data *add_list,
Jacob Keller671889e2016-12-02 12:33:00 -08001862 struct i40e_new_mac_filter *add_head)
Kiran Patil21659032015-09-30 14:09:03 -04001863{
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001864 int retval = 0;
1865 int i;
Kiran Patil21659032015-09-30 14:09:03 -04001866
Jacob Kellerac9e2392016-11-11 12:39:27 -08001867 for (i = 0; i < count; i++) {
1868 /* Always check status of each filter. We don't need to check
1869 * the firmware return status because we pre-set the filter
1870 * status to I40E_AQC_MM_ERR_NO_RES when sending the filter
1871 * request to the adminq. Thus, if it no longer matches then
1872 * we know the filter is active.
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001873 */
Jacob Kellerac9e2392016-11-11 12:39:27 -08001874 if (add_list[i].match_method == I40E_AQC_MM_ERR_NO_RES) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001875 add_head->state = I40E_FILTER_FAILED;
Jacob Kellerac9e2392016-11-11 12:39:27 -08001876 } else {
1877 add_head->state = I40E_FILTER_ACTIVE;
1878 retval++;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001879 }
Jacob Kellerac9e2392016-11-11 12:39:27 -08001880
Jacob Kellerd88d40b2016-12-02 12:32:59 -08001881 add_head = i40e_next_filter(add_head);
1882 if (!add_head)
1883 break;
Kiran Patil21659032015-09-30 14:09:03 -04001884 }
Jacob Kellerac9e2392016-11-11 12:39:27 -08001885
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001886 return retval;
Kiran Patil21659032015-09-30 14:09:03 -04001887}
1888
1889/**
Jacob Keller00936312016-10-05 09:30:41 -07001890 * i40e_aqc_del_filters - Request firmware to delete a set of filters
1891 * @vsi: ptr to the VSI
1892 * @vsi_name: name to display in messages
1893 * @list: the list of filters to send to firmware
1894 * @num_del: the number of filters to delete
1895 * @retval: Set to -EIO on failure to delete
1896 *
1897 * Send a request to firmware via AdminQ to delete a set of filters. Uses
1898 * *retval instead of a return value so that success does not force ret_val to
1899 * be set to 0. This ensures that a sequence of calls to this function
1900 * preserve the previous value of *retval on successful delete.
1901 */
1902static
1903void i40e_aqc_del_filters(struct i40e_vsi *vsi, const char *vsi_name,
1904 struct i40e_aqc_remove_macvlan_element_data *list,
1905 int num_del, int *retval)
1906{
1907 struct i40e_hw *hw = &vsi->back->hw;
1908 i40e_status aq_ret;
1909 int aq_err;
1910
1911 aq_ret = i40e_aq_remove_macvlan(hw, vsi->seid, list, num_del, NULL);
1912 aq_err = hw->aq.asq_last_status;
1913
1914 /* Explicitly ignore and do not report when firmware returns ENOENT */
1915 if (aq_ret && !(aq_err == I40E_AQ_RC_ENOENT)) {
1916 *retval = -EIO;
1917 dev_info(&vsi->back->pdev->dev,
1918 "ignoring delete macvlan error on %s, err %s, aq_err %s\n",
1919 vsi_name, i40e_stat_str(hw, aq_ret),
1920 i40e_aq_str(hw, aq_err));
1921 }
1922}
1923
1924/**
1925 * i40e_aqc_add_filters - Request firmware to add a set of filters
1926 * @vsi: ptr to the VSI
1927 * @vsi_name: name to display in messages
1928 * @list: the list of filters to send to firmware
1929 * @add_head: Position in the add hlist
1930 * @num_add: the number of filters to add
1931 * @promisc_change: set to true on exit if promiscuous mode was forced on
1932 *
1933 * Send a request to firmware via AdminQ to add a chunk of filters. Will set
1934 * promisc_changed to true if the firmware has run out of space for more
1935 * filters.
1936 */
1937static
1938void i40e_aqc_add_filters(struct i40e_vsi *vsi, const char *vsi_name,
1939 struct i40e_aqc_add_macvlan_element_data *list,
Jacob Keller671889e2016-12-02 12:33:00 -08001940 struct i40e_new_mac_filter *add_head,
Jacob Keller00936312016-10-05 09:30:41 -07001941 int num_add, bool *promisc_changed)
1942{
1943 struct i40e_hw *hw = &vsi->back->hw;
Jacob Keller00936312016-10-05 09:30:41 -07001944 int aq_err, fcnt;
1945
Jacob Kellerac9e2392016-11-11 12:39:27 -08001946 i40e_aq_add_macvlan(hw, vsi->seid, list, num_add, NULL);
Jacob Keller00936312016-10-05 09:30:41 -07001947 aq_err = hw->aq.asq_last_status;
Jacob Kellerac9e2392016-11-11 12:39:27 -08001948 fcnt = i40e_update_filter_state(num_add, list, add_head);
Jacob Keller00936312016-10-05 09:30:41 -07001949
1950 if (fcnt != num_add) {
1951 *promisc_changed = true;
Jacob Keller0da36b92017-04-19 09:25:55 -04001952 set_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
Jacob Keller00936312016-10-05 09:30:41 -07001953 dev_warn(&vsi->back->pdev->dev,
1954 "Error %s adding RX filters on %s, promiscuous mode forced on\n",
1955 i40e_aq_str(hw, aq_err),
1956 vsi_name);
1957 }
1958}
1959
1960/**
Jacob Keller435c0842016-11-08 13:05:10 -08001961 * i40e_aqc_broadcast_filter - Set promiscuous broadcast flags
1962 * @vsi: pointer to the VSI
1963 * @f: filter data
1964 *
1965 * This function sets or clears the promiscuous broadcast flags for VLAN
1966 * filters in order to properly receive broadcast frames. Assumes that only
1967 * broadcast filters are passed.
Jacob Keller671889e2016-12-02 12:33:00 -08001968 *
1969 * Returns status indicating success or failure;
Jacob Keller435c0842016-11-08 13:05:10 -08001970 **/
Jacob Keller671889e2016-12-02 12:33:00 -08001971static i40e_status
1972i40e_aqc_broadcast_filter(struct i40e_vsi *vsi, const char *vsi_name,
1973 struct i40e_mac_filter *f)
Jacob Keller435c0842016-11-08 13:05:10 -08001974{
1975 bool enable = f->state == I40E_FILTER_NEW;
1976 struct i40e_hw *hw = &vsi->back->hw;
1977 i40e_status aq_ret;
1978
1979 if (f->vlan == I40E_VLAN_ANY) {
1980 aq_ret = i40e_aq_set_vsi_broadcast(hw,
1981 vsi->seid,
1982 enable,
1983 NULL);
1984 } else {
1985 aq_ret = i40e_aq_set_vsi_bc_promisc_on_vlan(hw,
1986 vsi->seid,
1987 enable,
1988 f->vlan,
1989 NULL);
1990 }
1991
Jacob Keller671889e2016-12-02 12:33:00 -08001992 if (aq_ret)
Jacob Keller435c0842016-11-08 13:05:10 -08001993 dev_warn(&vsi->back->pdev->dev,
1994 "Error %s setting broadcast promiscuous mode on %s\n",
1995 i40e_aq_str(hw, hw->aq.asq_last_status),
1996 vsi_name);
Jacob Keller671889e2016-12-02 12:33:00 -08001997
1998 return aq_ret;
Jacob Keller435c0842016-11-08 13:05:10 -08001999}
2000
2001/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002002 * i40e_sync_vsi_filters - Update the VSI filter list to the HW
2003 * @vsi: ptr to the VSI
2004 *
2005 * Push any outstanding VSI filter changes through the AdminQ.
2006 *
2007 * Returns 0 or error value
2008 **/
Jesse Brandeburg17652c62015-11-05 17:01:02 -08002009int i40e_sync_vsi_filters(struct i40e_vsi *vsi)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002010{
Jacob Keller278e7d02016-10-05 09:30:37 -07002011 struct hlist_head tmp_add_list, tmp_del_list;
Jacob Keller671889e2016-12-02 12:33:00 -08002012 struct i40e_mac_filter *f;
2013 struct i40e_new_mac_filter *new, *add_head = NULL;
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002014 struct i40e_hw *hw = &vsi->back->hw;
Jacob Keller38326212016-11-11 12:39:26 -08002015 unsigned int failed_filters = 0;
Alan Brady84f5ca62016-10-05 09:30:39 -07002016 unsigned int vlan_filters = 0;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002017 bool promisc_changed = false;
Shannon Nelson2d1de822016-05-16 10:26:44 -07002018 char vsi_name[16] = "PF";
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002019 int filter_list_len = 0;
Mitch Williamsea02e902015-11-09 15:35:50 -08002020 i40e_status aq_ret = 0;
Alan Brady84f5ca62016-10-05 09:30:39 -07002021 u32 changed_flags = 0;
Jacob Keller278e7d02016-10-05 09:30:37 -07002022 struct hlist_node *h;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002023 struct i40e_pf *pf;
2024 int num_add = 0;
2025 int num_del = 0;
Alan Brady84f5ca62016-10-05 09:30:39 -07002026 int retval = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002027 u16 cmd_flags;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002028 int list_size;
Jacob Keller278e7d02016-10-05 09:30:37 -07002029 int bkt;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002030
2031 /* empty array typed pointers, kcalloc later */
2032 struct i40e_aqc_add_macvlan_element_data *add_list;
2033 struct i40e_aqc_remove_macvlan_element_data *del_list;
2034
Jacob Keller0da36b92017-04-19 09:25:55 -04002035 while (test_and_set_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002036 usleep_range(1000, 2000);
2037 pf = vsi->back;
2038
2039 if (vsi->netdev) {
2040 changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
2041 vsi->current_netdev_flags = vsi->netdev->flags;
2042 }
2043
Jacob Keller278e7d02016-10-05 09:30:37 -07002044 INIT_HLIST_HEAD(&tmp_add_list);
2045 INIT_HLIST_HEAD(&tmp_del_list);
Kiran Patil21659032015-09-30 14:09:03 -04002046
Shannon Nelson2d1de822016-05-16 10:26:44 -07002047 if (vsi->type == I40E_VSI_SRIOV)
2048 snprintf(vsi_name, sizeof(vsi_name) - 1, "VF %d", vsi->vf_id);
2049 else if (vsi->type != I40E_VSI_MAIN)
2050 snprintf(vsi_name, sizeof(vsi_name) - 1, "vsi %d", vsi->seid);
2051
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002052 if (vsi->flags & I40E_VSI_FLAG_FILTER_CHANGED) {
2053 vsi->flags &= ~I40E_VSI_FLAG_FILTER_CHANGED;
2054
Jacob Keller278e7d02016-10-05 09:30:37 -07002055 spin_lock_bh(&vsi->mac_filter_hash_lock);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002056 /* Create a list of filters to delete. */
Jacob Keller278e7d02016-10-05 09:30:37 -07002057 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002058 if (f->state == I40E_FILTER_REMOVE) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002059 /* Move the element into temporary del_list */
Jacob Keller278e7d02016-10-05 09:30:37 -07002060 hash_del(&f->hlist);
2061 hlist_add_head(&f->hlist, &tmp_del_list);
Alan Brady84f5ca62016-10-05 09:30:39 -07002062
2063 /* Avoid counting removed filters */
2064 continue;
Kiran Patil21659032015-09-30 14:09:03 -04002065 }
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002066 if (f->state == I40E_FILTER_NEW) {
Jacob Keller671889e2016-12-02 12:33:00 -08002067 /* Create a temporary i40e_new_mac_filter */
2068 new = kzalloc(sizeof(*new), GFP_ATOMIC);
2069 if (!new)
2070 goto err_no_memory_locked;
2071
2072 /* Store pointer to the real filter */
2073 new->f = f;
2074 new->state = f->state;
2075
2076 /* Add it to the hash list */
2077 hlist_add_head(&new->hlist, &tmp_add_list);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002078 }
Alan Brady84f5ca62016-10-05 09:30:39 -07002079
Jacob Keller489a3262016-11-11 12:39:31 -08002080 /* Count the number of active (current and new) VLAN
2081 * filters we have now. Does not count filters which
2082 * are marked for deletion.
Alan Brady84f5ca62016-10-05 09:30:39 -07002083 */
2084 if (f->vlan > 0)
2085 vlan_filters++;
Alan Brady84f5ca62016-10-05 09:30:39 -07002086 }
2087
Jacob Keller489a3262016-11-11 12:39:31 -08002088 retval = i40e_correct_mac_vlan_filters(vsi,
2089 &tmp_add_list,
2090 &tmp_del_list,
2091 vlan_filters);
2092 if (retval)
2093 goto err_no_memory_locked;
Alan Brady84f5ca62016-10-05 09:30:39 -07002094
Jacob Keller278e7d02016-10-05 09:30:37 -07002095 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -04002096 }
2097
2098 /* Now process 'del_list' outside the lock */
Jacob Keller278e7d02016-10-05 09:30:37 -07002099 if (!hlist_empty(&tmp_del_list)) {
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002100 filter_list_len = hw->aq.asq_buf_size /
Kiran Patil21659032015-09-30 14:09:03 -04002101 sizeof(struct i40e_aqc_remove_macvlan_element_data);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002102 list_size = filter_list_len *
Shannon Nelsonf1199992015-11-19 11:34:23 -08002103 sizeof(struct i40e_aqc_remove_macvlan_element_data);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002104 del_list = kzalloc(list_size, GFP_ATOMIC);
Jacob Keller4a2ce272016-10-05 09:30:38 -07002105 if (!del_list)
2106 goto err_no_memory;
Kiran Patil21659032015-09-30 14:09:03 -04002107
Jacob Keller278e7d02016-10-05 09:30:37 -07002108 hlist_for_each_entry_safe(f, h, &tmp_del_list, hlist) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002109 cmd_flags = 0;
2110
Jacob Keller435c0842016-11-08 13:05:10 -08002111 /* handle broadcast filters by updating the broadcast
Jacob Kellerd88d40b2016-12-02 12:32:59 -08002112 * promiscuous flag and release filter list.
Jacob Keller435c0842016-11-08 13:05:10 -08002113 */
2114 if (is_broadcast_ether_addr(f->macaddr)) {
2115 i40e_aqc_broadcast_filter(vsi, vsi_name, f);
2116
2117 hlist_del(&f->hlist);
2118 kfree(f);
2119 continue;
2120 }
2121
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002122 /* add to delete list */
Greg Rose9a173902014-05-22 06:32:02 +00002123 ether_addr_copy(del_list[num_del].mac_addr, f->macaddr);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002124 if (f->vlan == I40E_VLAN_ANY) {
2125 del_list[num_del].vlan_tag = 0;
Alan Bradya6cb9142016-09-06 18:05:07 -07002126 cmd_flags |= I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002127 } else {
2128 del_list[num_del].vlan_tag =
2129 cpu_to_le16((u16)(f->vlan));
2130 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002131
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002132 cmd_flags |= I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
2133 del_list[num_del].flags = cmd_flags;
2134 num_del++;
2135
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002136 /* flush a full buffer */
2137 if (num_del == filter_list_len) {
Jacob Keller00936312016-10-05 09:30:41 -07002138 i40e_aqc_del_filters(vsi, vsi_name, del_list,
2139 num_del, &retval);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002140 memset(del_list, 0, list_size);
Jacob Keller00936312016-10-05 09:30:41 -07002141 num_del = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002142 }
Kiran Patil21659032015-09-30 14:09:03 -04002143 /* Release memory for MAC filter entries which were
2144 * synced up with HW.
2145 */
Jacob Keller278e7d02016-10-05 09:30:37 -07002146 hlist_del(&f->hlist);
Kiran Patil21659032015-09-30 14:09:03 -04002147 kfree(f);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002148 }
Kiran Patil21659032015-09-30 14:09:03 -04002149
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002150 if (num_del) {
Jacob Keller00936312016-10-05 09:30:41 -07002151 i40e_aqc_del_filters(vsi, vsi_name, del_list,
2152 num_del, &retval);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002153 }
2154
2155 kfree(del_list);
2156 del_list = NULL;
Kiran Patil21659032015-09-30 14:09:03 -04002157 }
2158
Jacob Keller278e7d02016-10-05 09:30:37 -07002159 if (!hlist_empty(&tmp_add_list)) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002160 /* Do all the adds now. */
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002161 filter_list_len = hw->aq.asq_buf_size /
Shannon Nelsonf1199992015-11-19 11:34:23 -08002162 sizeof(struct i40e_aqc_add_macvlan_element_data);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002163 list_size = filter_list_len *
2164 sizeof(struct i40e_aqc_add_macvlan_element_data);
2165 add_list = kzalloc(list_size, GFP_ATOMIC);
Jacob Keller4a2ce272016-10-05 09:30:38 -07002166 if (!add_list)
2167 goto err_no_memory;
2168
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002169 num_add = 0;
Jacob Keller671889e2016-12-02 12:33:00 -08002170 hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
Jacob Kellerd19cb642017-04-21 13:38:05 -07002171 if (test_bit(__I40E_VSI_OVERFLOW_PROMISC,
Jacob Keller0da36b92017-04-19 09:25:55 -04002172 vsi->state)) {
Jacob Keller671889e2016-12-02 12:33:00 -08002173 new->state = I40E_FILTER_FAILED;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002174 continue;
2175 }
Jacob Keller435c0842016-11-08 13:05:10 -08002176
2177 /* handle broadcast filters by updating the broadcast
2178 * promiscuous flag instead of adding a MAC filter.
2179 */
Jacob Keller671889e2016-12-02 12:33:00 -08002180 if (is_broadcast_ether_addr(new->f->macaddr)) {
2181 if (i40e_aqc_broadcast_filter(vsi, vsi_name,
2182 new->f))
2183 new->state = I40E_FILTER_FAILED;
2184 else
2185 new->state = I40E_FILTER_ACTIVE;
Jacob Keller435c0842016-11-08 13:05:10 -08002186 continue;
2187 }
2188
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002189 /* add to add array */
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002190 if (num_add == 0)
Jacob Keller671889e2016-12-02 12:33:00 -08002191 add_head = new;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002192 cmd_flags = 0;
Jacob Keller671889e2016-12-02 12:33:00 -08002193 ether_addr_copy(add_list[num_add].mac_addr,
2194 new->f->macaddr);
2195 if (new->f->vlan == I40E_VLAN_ANY) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002196 add_list[num_add].vlan_tag = 0;
2197 cmd_flags |= I40E_AQC_MACVLAN_ADD_IGNORE_VLAN;
2198 } else {
2199 add_list[num_add].vlan_tag =
Jacob Keller671889e2016-12-02 12:33:00 -08002200 cpu_to_le16((u16)(new->f->vlan));
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002201 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002202 add_list[num_add].queue_number = 0;
Jacob Kellerac9e2392016-11-11 12:39:27 -08002203 /* set invalid match method for later detection */
Keller, Jacob E0266ac42016-12-09 13:39:21 -08002204 add_list[num_add].match_method = I40E_AQC_MM_ERR_NO_RES;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002205 cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002206 add_list[num_add].flags = cpu_to_le16(cmd_flags);
2207 num_add++;
2208
2209 /* flush a full buffer */
2210 if (num_add == filter_list_len) {
Jacob Keller00936312016-10-05 09:30:41 -07002211 i40e_aqc_add_filters(vsi, vsi_name, add_list,
2212 add_head, num_add,
2213 &promisc_changed);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002214 memset(add_list, 0, list_size);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002215 num_add = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002216 }
2217 }
2218 if (num_add) {
Jacob Keller00936312016-10-05 09:30:41 -07002219 i40e_aqc_add_filters(vsi, vsi_name, add_list, add_head,
2220 num_add, &promisc_changed);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002221 }
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002222 /* Now move all of the filters from the temp add list back to
2223 * the VSI's list.
2224 */
Jacob Keller278e7d02016-10-05 09:30:37 -07002225 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller671889e2016-12-02 12:33:00 -08002226 hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
2227 /* Only update the state if we're still NEW */
2228 if (new->f->state == I40E_FILTER_NEW)
2229 new->f->state = new->state;
2230 hlist_del(&new->hlist);
2231 kfree(new);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002232 }
Jacob Keller278e7d02016-10-05 09:30:37 -07002233 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002234 kfree(add_list);
2235 add_list = NULL;
2236 }
2237
Jacob Keller38326212016-11-11 12:39:26 -08002238 /* Determine the number of active and failed filters. */
2239 spin_lock_bh(&vsi->mac_filter_hash_lock);
2240 vsi->active_filters = 0;
2241 hash_for_each(vsi->mac_filter_hash, bkt, f, hlist) {
2242 if (f->state == I40E_FILTER_ACTIVE)
2243 vsi->active_filters++;
2244 else if (f->state == I40E_FILTER_FAILED)
2245 failed_filters++;
2246 }
2247 spin_unlock_bh(&vsi->mac_filter_hash_lock);
2248
2249 /* If promiscuous mode has changed, we need to calculate a new
2250 * threshold for when we are safe to exit
2251 */
2252 if (promisc_changed)
2253 vsi->promisc_threshold = (vsi->active_filters * 3) / 4;
2254
2255 /* Check if we are able to exit overflow promiscuous mode. We can
2256 * safely exit if we didn't just enter, we no longer have any failed
2257 * filters, and we have reduced filters below the threshold value.
2258 */
Jacob Keller0da36b92017-04-19 09:25:55 -04002259 if (test_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state) &&
Jacob Keller38326212016-11-11 12:39:26 -08002260 !promisc_changed && !failed_filters &&
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002261 (vsi->active_filters < vsi->promisc_threshold)) {
Jacob Keller38326212016-11-11 12:39:26 -08002262 dev_info(&pf->pdev->dev,
2263 "filter logjam cleared on %s, leaving overflow promiscuous mode\n",
2264 vsi_name);
Jacob Keller0da36b92017-04-19 09:25:55 -04002265 clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
Jacob Keller38326212016-11-11 12:39:26 -08002266 promisc_changed = true;
2267 vsi->promisc_threshold = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002268 }
2269
Anjali Singhai Jaina856b5c2016-04-13 03:08:23 -07002270 /* if the VF is not trusted do not do promisc */
2271 if ((vsi->type == I40E_VSI_SRIOV) && !pf->vf[vsi->vf_id].trusted) {
Jacob Keller0da36b92017-04-19 09:25:55 -04002272 clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
Anjali Singhai Jaina856b5c2016-04-13 03:08:23 -07002273 goto out;
2274 }
2275
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002276 /* check for changes in promiscuous modes */
2277 if (changed_flags & IFF_ALLMULTI) {
2278 bool cur_multipromisc;
Jesse Brandeburg6995b362015-08-28 17:55:54 -04002279
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002280 cur_multipromisc = !!(vsi->current_netdev_flags & IFF_ALLMULTI);
Mitch Williamsea02e902015-11-09 15:35:50 -08002281 aq_ret = i40e_aq_set_vsi_multicast_promiscuous(&vsi->back->hw,
2282 vsi->seid,
2283 cur_multipromisc,
2284 NULL);
2285 if (aq_ret) {
2286 retval = i40e_aq_rc_to_posix(aq_ret,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002287 hw->aq.asq_last_status);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002288 dev_info(&pf->pdev->dev,
Shannon Nelson2d1de822016-05-16 10:26:44 -07002289 "set multi promisc failed on %s, err %s aq_err %s\n",
2290 vsi_name,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002291 i40e_stat_str(hw, aq_ret),
2292 i40e_aq_str(hw, hw->aq.asq_last_status));
Mitch Williamsea02e902015-11-09 15:35:50 -08002293 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002294 }
Alan Bradye5887232017-06-07 05:43:10 -04002295
2296 if ((changed_flags & IFF_PROMISC) || promisc_changed) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002297 bool cur_promisc;
Jesse Brandeburg6995b362015-08-28 17:55:54 -04002298
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002299 cur_promisc = (!!(vsi->current_netdev_flags & IFF_PROMISC) ||
Jacob Kellerd19cb642017-04-21 13:38:05 -07002300 test_bit(__I40E_VSI_OVERFLOW_PROMISC,
Jacob Keller0da36b92017-04-19 09:25:55 -04002301 vsi->state));
Anjali Singhai Jain6784ed52016-01-15 14:33:13 -08002302 if ((vsi->type == I40E_VSI_MAIN) &&
2303 (pf->lan_veb != I40E_NO_VEB) &&
2304 !(pf->flags & I40E_FLAG_MFP_ENABLED)) {
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002305 /* set defport ON for Main VSI instead of true promisc
2306 * this way we will get all unicast/multicast and VLAN
2307 * promisc behavior but will not get VF or VMDq traffic
2308 * replicated on the Main VSI.
2309 */
2310 if (pf->cur_promisc != cur_promisc) {
2311 pf->cur_promisc = cur_promisc;
Mitch Williams5bc16032016-05-16 10:26:43 -07002312 if (cur_promisc)
2313 aq_ret =
2314 i40e_aq_set_default_vsi(hw,
2315 vsi->seid,
2316 NULL);
2317 else
2318 aq_ret =
2319 i40e_aq_clear_default_vsi(hw,
2320 vsi->seid,
2321 NULL);
2322 if (aq_ret) {
2323 retval = i40e_aq_rc_to_posix(aq_ret,
2324 hw->aq.asq_last_status);
2325 dev_info(&pf->pdev->dev,
Shannon Nelson2d1de822016-05-16 10:26:44 -07002326 "Set default VSI failed on %s, err %s, aq_err %s\n",
2327 vsi_name,
Mitch Williams5bc16032016-05-16 10:26:43 -07002328 i40e_stat_str(hw, aq_ret),
2329 i40e_aq_str(hw,
2330 hw->aq.asq_last_status));
2331 }
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002332 }
2333 } else {
Mitch Williamsea02e902015-11-09 15:35:50 -08002334 aq_ret = i40e_aq_set_vsi_unicast_promiscuous(
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002335 hw,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002336 vsi->seid,
Anjali Singhai Jainb5569892016-05-03 15:13:12 -07002337 cur_promisc, NULL,
2338 true);
Mitch Williamsea02e902015-11-09 15:35:50 -08002339 if (aq_ret) {
2340 retval =
2341 i40e_aq_rc_to_posix(aq_ret,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002342 hw->aq.asq_last_status);
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002343 dev_info(&pf->pdev->dev,
Shannon Nelson2d1de822016-05-16 10:26:44 -07002344 "set unicast promisc failed on %s, err %s, aq_err %s\n",
2345 vsi_name,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002346 i40e_stat_str(hw, aq_ret),
2347 i40e_aq_str(hw,
2348 hw->aq.asq_last_status));
Mitch Williamsea02e902015-11-09 15:35:50 -08002349 }
2350 aq_ret = i40e_aq_set_vsi_multicast_promiscuous(
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002351 hw,
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002352 vsi->seid,
2353 cur_promisc, NULL);
Mitch Williamsea02e902015-11-09 15:35:50 -08002354 if (aq_ret) {
2355 retval =
2356 i40e_aq_rc_to_posix(aq_ret,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002357 hw->aq.asq_last_status);
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002358 dev_info(&pf->pdev->dev,
Shannon Nelson2d1de822016-05-16 10:26:44 -07002359 "set multicast promisc failed on %s, err %s, aq_err %s\n",
2360 vsi_name,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002361 i40e_stat_str(hw, aq_ret),
2362 i40e_aq_str(hw,
2363 hw->aq.asq_last_status));
Mitch Williamsea02e902015-11-09 15:35:50 -08002364 }
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002365 }
Mitch Williamsea02e902015-11-09 15:35:50 -08002366 aq_ret = i40e_aq_set_vsi_broadcast(&vsi->back->hw,
2367 vsi->seid,
2368 cur_promisc, NULL);
2369 if (aq_ret) {
2370 retval = i40e_aq_rc_to_posix(aq_ret,
2371 pf->hw.aq.asq_last_status);
Greg Rose1a103702013-11-28 06:42:39 +00002372 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002373 "set brdcast promisc failed, err %s, aq_err %s\n",
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002374 i40e_stat_str(hw, aq_ret),
2375 i40e_aq_str(hw,
2376 hw->aq.asq_last_status));
Mitch Williamsea02e902015-11-09 15:35:50 -08002377 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002378 }
Mitch Williamsea02e902015-11-09 15:35:50 -08002379out:
Jesse Brandeburg2818ccd2016-01-13 16:51:38 -08002380 /* if something went wrong then set the changed flag so we try again */
2381 if (retval)
2382 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2383
Jacob Keller0da36b92017-04-19 09:25:55 -04002384 clear_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state);
Mitch Williamsea02e902015-11-09 15:35:50 -08002385 return retval;
Jacob Keller4a2ce272016-10-05 09:30:38 -07002386
2387err_no_memory:
2388 /* Restore elements on the temporary add and delete lists */
2389 spin_lock_bh(&vsi->mac_filter_hash_lock);
Alan Brady84f5ca62016-10-05 09:30:39 -07002390err_no_memory_locked:
Jacob Keller671889e2016-12-02 12:33:00 -08002391 i40e_undo_del_filter_entries(vsi, &tmp_del_list);
2392 i40e_undo_add_filter_entries(vsi, &tmp_add_list);
Jacob Keller4a2ce272016-10-05 09:30:38 -07002393 spin_unlock_bh(&vsi->mac_filter_hash_lock);
2394
2395 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
Jacob Keller0da36b92017-04-19 09:25:55 -04002396 clear_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state);
Jacob Keller4a2ce272016-10-05 09:30:38 -07002397 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002398}
2399
2400/**
2401 * i40e_sync_filters_subtask - Sync the VSI filter list with HW
2402 * @pf: board private structure
2403 **/
2404static void i40e_sync_filters_subtask(struct i40e_pf *pf)
2405{
2406 int v;
2407
2408 if (!pf || !(pf->flags & I40E_FLAG_FILTER_SYNC))
2409 return;
2410 pf->flags &= ~I40E_FLAG_FILTER_SYNC;
2411
Mitch Williams505682c2014-05-20 08:01:37 +00002412 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002413 if (pf->vsi[v] &&
Jesse Brandeburg17652c62015-11-05 17:01:02 -08002414 (pf->vsi[v]->flags & I40E_VSI_FLAG_FILTER_CHANGED)) {
2415 int ret = i40e_sync_vsi_filters(pf->vsi[v]);
2416
2417 if (ret) {
2418 /* come back and try again later */
2419 pf->flags |= I40E_FLAG_FILTER_SYNC;
2420 break;
2421 }
2422 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002423 }
2424}
2425
2426/**
Björn Töpel0c8493d2017-05-24 07:55:34 +02002427 * i40e_max_xdp_frame_size - returns the maximum allowed frame size for XDP
2428 * @vsi: the vsi
2429 **/
2430static int i40e_max_xdp_frame_size(struct i40e_vsi *vsi)
2431{
2432 if (PAGE_SIZE >= 8192 || (vsi->back->flags & I40E_FLAG_LEGACY_RX))
2433 return I40E_RXBUFFER_2048;
2434 else
2435 return I40E_RXBUFFER_3072;
2436}
2437
2438/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002439 * i40e_change_mtu - NDO callback to change the Maximum Transfer Unit
2440 * @netdev: network interface device structure
2441 * @new_mtu: new value for maximum frame size
2442 *
2443 * Returns 0 on success, negative on failure
2444 **/
2445static int i40e_change_mtu(struct net_device *netdev, int new_mtu)
2446{
2447 struct i40e_netdev_priv *np = netdev_priv(netdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002448 struct i40e_vsi *vsi = np->vsi;
Mitch Williams0ef2d5a2017-01-24 10:24:00 -08002449 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002450
Björn Töpel0c8493d2017-05-24 07:55:34 +02002451 if (i40e_enabled_xdp_vsi(vsi)) {
2452 int frame_size = new_mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
2453
2454 if (frame_size > i40e_max_xdp_frame_size(vsi))
2455 return -EINVAL;
2456 }
2457
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002458 netdev_info(netdev, "changing MTU from %d to %d\n",
2459 netdev->mtu, new_mtu);
2460 netdev->mtu = new_mtu;
2461 if (netif_running(netdev))
2462 i40e_vsi_reinit_locked(vsi);
Mitch Williams0ef2d5a2017-01-24 10:24:00 -08002463 pf->flags |= (I40E_FLAG_SERVICE_CLIENT_REQUESTED |
2464 I40E_FLAG_CLIENT_L2_CHANGE);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002465 return 0;
2466}
2467
2468/**
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00002469 * i40e_ioctl - Access the hwtstamp interface
2470 * @netdev: network interface device structure
2471 * @ifr: interface request data
2472 * @cmd: ioctl command
2473 **/
2474int i40e_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
2475{
2476 struct i40e_netdev_priv *np = netdev_priv(netdev);
2477 struct i40e_pf *pf = np->vsi->back;
2478
2479 switch (cmd) {
2480 case SIOCGHWTSTAMP:
2481 return i40e_ptp_get_ts_config(pf, ifr);
2482 case SIOCSHWTSTAMP:
2483 return i40e_ptp_set_ts_config(pf, ifr);
2484 default:
2485 return -EOPNOTSUPP;
2486 }
2487}
2488
2489/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002490 * i40e_vlan_stripping_enable - Turn on vlan stripping for the VSI
2491 * @vsi: the vsi being adjusted
2492 **/
2493void i40e_vlan_stripping_enable(struct i40e_vsi *vsi)
2494{
2495 struct i40e_vsi_context ctxt;
2496 i40e_status ret;
2497
2498 if ((vsi->info.valid_sections &
2499 cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
2500 ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_MODE_MASK) == 0))
2501 return; /* already enabled */
2502
2503 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2504 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
2505 I40E_AQ_VSI_PVLAN_EMOD_STR_BOTH;
2506
2507 ctxt.seid = vsi->seid;
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07002508 ctxt.info = vsi->info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002509 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2510 if (ret) {
2511 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002512 "update vlan stripping failed, err %s aq_err %s\n",
2513 i40e_stat_str(&vsi->back->hw, ret),
2514 i40e_aq_str(&vsi->back->hw,
2515 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002516 }
2517}
2518
2519/**
2520 * i40e_vlan_stripping_disable - Turn off vlan stripping for the VSI
2521 * @vsi: the vsi being adjusted
2522 **/
2523void i40e_vlan_stripping_disable(struct i40e_vsi *vsi)
2524{
2525 struct i40e_vsi_context ctxt;
2526 i40e_status ret;
2527
2528 if ((vsi->info.valid_sections &
2529 cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
2530 ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_EMOD_MASK) ==
2531 I40E_AQ_VSI_PVLAN_EMOD_MASK))
2532 return; /* already disabled */
2533
2534 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2535 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
2536 I40E_AQ_VSI_PVLAN_EMOD_NOTHING;
2537
2538 ctxt.seid = vsi->seid;
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07002539 ctxt.info = vsi->info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002540 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2541 if (ret) {
2542 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002543 "update vlan stripping failed, err %s aq_err %s\n",
2544 i40e_stat_str(&vsi->back->hw, ret),
2545 i40e_aq_str(&vsi->back->hw,
2546 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002547 }
2548}
2549
2550/**
2551 * i40e_vlan_rx_register - Setup or shutdown vlan offload
2552 * @netdev: network interface to be adjusted
2553 * @features: netdev features to test if VLAN offload is enabled or not
2554 **/
2555static void i40e_vlan_rx_register(struct net_device *netdev, u32 features)
2556{
2557 struct i40e_netdev_priv *np = netdev_priv(netdev);
2558 struct i40e_vsi *vsi = np->vsi;
2559
2560 if (features & NETIF_F_HW_VLAN_CTAG_RX)
2561 i40e_vlan_stripping_enable(vsi);
2562 else
2563 i40e_vlan_stripping_disable(vsi);
2564}
2565
2566/**
Jacob Keller490a4ad2016-11-11 12:39:29 -08002567 * i40e_add_vlan_all_mac - Add a MAC/VLAN filter for each existing MAC address
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002568 * @vsi: the vsi being configured
2569 * @vid: vlan id to be added (0 = untagged only , -1 = any)
Jacob Keller490a4ad2016-11-11 12:39:29 -08002570 *
2571 * This is a helper function for adding a new MAC/VLAN filter with the
2572 * specified VLAN for each existing MAC address already in the hash table.
2573 * This function does *not* perform any accounting to update filters based on
2574 * VLAN mode.
2575 *
2576 * NOTE: this function expects to be called while under the
2577 * mac_filter_hash_lock
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002578 **/
Jacob Keller9af52f62016-11-11 12:39:30 -08002579int i40e_add_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002580{
Jacob Keller490a4ad2016-11-11 12:39:29 -08002581 struct i40e_mac_filter *f, *add_f;
Jacob Keller278e7d02016-10-05 09:30:37 -07002582 struct hlist_node *h;
2583 int bkt;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002584
Jacob Keller278e7d02016-10-05 09:30:37 -07002585 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Jacob Keller57b341d2016-10-05 09:30:35 -07002586 if (f->state == I40E_FILTER_REMOVE)
2587 continue;
Jacob Keller1bc87e82016-10-05 09:30:31 -07002588 add_f = i40e_add_filter(vsi, f->macaddr, vid);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002589 if (!add_f) {
2590 dev_info(&vsi->back->pdev->dev,
2591 "Could not add vlan filter %d for %pM\n",
2592 vid, f->macaddr);
2593 return -ENOMEM;
2594 }
2595 }
2596
Jacob Keller490a4ad2016-11-11 12:39:29 -08002597 return 0;
2598}
2599
2600/**
2601 * i40e_vsi_add_vlan - Add VSI membership for given VLAN
2602 * @vsi: the VSI being configured
Jacob Kellerf94484b2016-12-07 14:05:34 -08002603 * @vid: VLAN id to be added
Jacob Keller490a4ad2016-11-11 12:39:29 -08002604 **/
Jacob Kellerf94484b2016-12-07 14:05:34 -08002605int i40e_vsi_add_vlan(struct i40e_vsi *vsi, u16 vid)
Jacob Keller490a4ad2016-11-11 12:39:29 -08002606{
Jacob Keller489a3262016-11-11 12:39:31 -08002607 int err;
Jacob Keller490a4ad2016-11-11 12:39:29 -08002608
Jacob Kellerfcf6cfc2017-06-23 04:24:51 -04002609 if (vsi->info.pvid)
Jacob Kellerf94484b2016-12-07 14:05:34 -08002610 return -EINVAL;
2611
Jacob Kellerfcf6cfc2017-06-23 04:24:51 -04002612 /* The network stack will attempt to add VID=0, with the intention to
2613 * receive priority tagged packets with a VLAN of 0. Our HW receives
2614 * these packets by default when configured to receive untagged
2615 * packets, so we don't need to add a filter for this case.
2616 * Additionally, HW interprets adding a VID=0 filter as meaning to
2617 * receive *only* tagged traffic and stops receiving untagged traffic.
2618 * Thus, we do not want to actually add a filter for VID=0
2619 */
2620 if (!vid)
2621 return 0;
2622
Jacob Keller490a4ad2016-11-11 12:39:29 -08002623 /* Locked once because all functions invoked below iterates list*/
2624 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller490a4ad2016-11-11 12:39:29 -08002625 err = i40e_add_vlan_all_mac(vsi, vid);
Jacob Keller278e7d02016-10-05 09:30:37 -07002626 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller489a3262016-11-11 12:39:31 -08002627 if (err)
2628 return err;
Kiran Patil21659032015-09-30 14:09:03 -04002629
Jesse Brandeburg0e4425e2015-11-05 17:01:01 -08002630 /* schedule our worker thread which will take care of
2631 * applying the new filter changes
2632 */
2633 i40e_service_event_schedule(vsi->back);
2634 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002635}
2636
2637/**
Jacob Keller490a4ad2016-11-11 12:39:29 -08002638 * i40e_rm_vlan_all_mac - Remove MAC/VLAN pair for all MAC with the given VLAN
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002639 * @vsi: the vsi being configured
2640 * @vid: vlan id to be removed (0 = untagged only , -1 = any)
Jacob Keller490a4ad2016-11-11 12:39:29 -08002641 *
2642 * This function should be used to remove all VLAN filters which match the
2643 * given VID. It does not schedule the service event and does not take the
2644 * mac_filter_hash_lock so it may be combined with other operations under
2645 * a single invocation of the mac_filter_hash_lock.
2646 *
2647 * NOTE: this function expects to be called while under the
2648 * mac_filter_hash_lock
2649 */
Jacob Keller9af52f62016-11-11 12:39:30 -08002650void i40e_rm_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002651{
Alan Brady84f5ca62016-10-05 09:30:39 -07002652 struct i40e_mac_filter *f;
Jacob Keller278e7d02016-10-05 09:30:37 -07002653 struct hlist_node *h;
Jacob Keller278e7d02016-10-05 09:30:37 -07002654 int bkt;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002655
Jacob Keller278e7d02016-10-05 09:30:37 -07002656 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Jacob Keller290d2552016-10-05 09:30:36 -07002657 if (f->vlan == vid)
2658 __i40e_del_filter(vsi, f);
2659 }
Jacob Keller490a4ad2016-11-11 12:39:29 -08002660}
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002661
Jacob Keller490a4ad2016-11-11 12:39:29 -08002662/**
2663 * i40e_vsi_kill_vlan - Remove VSI membership for given VLAN
2664 * @vsi: the VSI being configured
Jacob Kellerf94484b2016-12-07 14:05:34 -08002665 * @vid: VLAN id to be removed
Jacob Keller490a4ad2016-11-11 12:39:29 -08002666 **/
Jacob Kellerf94484b2016-12-07 14:05:34 -08002667void i40e_vsi_kill_vlan(struct i40e_vsi *vsi, u16 vid)
Jacob Keller490a4ad2016-11-11 12:39:29 -08002668{
Jacob Kellerf94484b2016-12-07 14:05:34 -08002669 if (!vid || vsi->info.pvid)
2670 return;
2671
Jacob Keller490a4ad2016-11-11 12:39:29 -08002672 spin_lock_bh(&vsi->mac_filter_hash_lock);
2673 i40e_rm_vlan_all_mac(vsi, vid);
Jacob Keller278e7d02016-10-05 09:30:37 -07002674 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -04002675
Jesse Brandeburg0e4425e2015-11-05 17:01:01 -08002676 /* schedule our worker thread which will take care of
2677 * applying the new filter changes
2678 */
2679 i40e_service_event_schedule(vsi->back);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002680}
2681
2682/**
2683 * i40e_vlan_rx_add_vid - Add a vlan id filter to HW offload
2684 * @netdev: network interface to be adjusted
2685 * @vid: vlan id to be added
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002686 *
2687 * net_device_ops implementation for adding vlan ids
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002688 **/
2689static int i40e_vlan_rx_add_vid(struct net_device *netdev,
2690 __always_unused __be16 proto, u16 vid)
2691{
2692 struct i40e_netdev_priv *np = netdev_priv(netdev);
2693 struct i40e_vsi *vsi = np->vsi;
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002694 int ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002695
Jacob Keller6a1127852016-10-25 16:08:49 -07002696 if (vid >= VLAN_N_VID)
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002697 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002698
Jacob Kellerfcf6cfc2017-06-23 04:24:51 -04002699 ret = i40e_vsi_add_vlan(vsi, vid);
Jacob Keller6a1127852016-10-25 16:08:49 -07002700 if (!ret)
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002701 set_bit(vid, vsi->active_vlans);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002702
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002703 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002704}
2705
2706/**
2707 * i40e_vlan_rx_kill_vid - Remove a vlan id filter from HW offload
2708 * @netdev: network interface to be adjusted
2709 * @vid: vlan id to be removed
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002710 *
Akeem G Abodunrinfdfd9432014-02-11 08:24:15 +00002711 * net_device_ops implementation for removing vlan ids
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002712 **/
2713static int i40e_vlan_rx_kill_vid(struct net_device *netdev,
2714 __always_unused __be16 proto, u16 vid)
2715{
2716 struct i40e_netdev_priv *np = netdev_priv(netdev);
2717 struct i40e_vsi *vsi = np->vsi;
2718
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002719 /* return code is ignored as there is nothing a user
2720 * can do about failure to remove and a log message was
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002721 * already printed from the other function
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002722 */
2723 i40e_vsi_kill_vlan(vsi, vid);
2724
2725 clear_bit(vid, vsi->active_vlans);
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002726
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002727 return 0;
2728}
2729
2730/**
2731 * i40e_restore_vlan - Reinstate vlans when vsi/netdev comes back up
2732 * @vsi: the vsi being brought back up
2733 **/
2734static void i40e_restore_vlan(struct i40e_vsi *vsi)
2735{
2736 u16 vid;
2737
2738 if (!vsi->netdev)
2739 return;
2740
2741 i40e_vlan_rx_register(vsi->netdev, vsi->netdev->features);
2742
2743 for_each_set_bit(vid, vsi->active_vlans, VLAN_N_VID)
2744 i40e_vlan_rx_add_vid(vsi->netdev, htons(ETH_P_8021Q),
2745 vid);
2746}
2747
2748/**
2749 * i40e_vsi_add_pvid - Add pvid for the VSI
2750 * @vsi: the vsi being adjusted
2751 * @vid: the vlan id to set as a PVID
2752 **/
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002753int i40e_vsi_add_pvid(struct i40e_vsi *vsi, u16 vid)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002754{
2755 struct i40e_vsi_context ctxt;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002756 i40e_status ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002757
2758 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2759 vsi->info.pvid = cpu_to_le16(vid);
Greg Rose6c12fcb2013-11-28 06:39:34 +00002760 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_TAGGED |
2761 I40E_AQ_VSI_PVLAN_INSERT_PVID |
Greg Roseb774c7d2013-11-28 06:39:44 +00002762 I40E_AQ_VSI_PVLAN_EMOD_STR;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002763
2764 ctxt.seid = vsi->seid;
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07002765 ctxt.info = vsi->info;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002766 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2767 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002768 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002769 "add pvid failed, err %s aq_err %s\n",
2770 i40e_stat_str(&vsi->back->hw, ret),
2771 i40e_aq_str(&vsi->back->hw,
2772 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002773 return -ENOENT;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002774 }
2775
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002776 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002777}
2778
2779/**
2780 * i40e_vsi_remove_pvid - Remove the pvid from the VSI
2781 * @vsi: the vsi being adjusted
2782 *
2783 * Just use the vlan_rx_register() service to put it back to normal
2784 **/
2785void i40e_vsi_remove_pvid(struct i40e_vsi *vsi)
2786{
Greg Rose6c12fcb2013-11-28 06:39:34 +00002787 i40e_vlan_stripping_disable(vsi);
2788
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002789 vsi->info.pvid = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002790}
2791
2792/**
2793 * i40e_vsi_setup_tx_resources - Allocate VSI Tx queue resources
2794 * @vsi: ptr to the VSI
2795 *
2796 * If this function returns with an error, then it's possible one or
2797 * more of the rings is populated (while the rest are not). It is the
2798 * callers duty to clean those orphaned rings.
2799 *
2800 * Return 0 on success, negative on failure
2801 **/
2802static int i40e_vsi_setup_tx_resources(struct i40e_vsi *vsi)
2803{
2804 int i, err = 0;
2805
2806 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e152013-09-28 06:00:58 +00002807 err = i40e_setup_tx_descriptors(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002808
Björn Töpel74608d12017-05-24 07:55:35 +02002809 if (!i40e_enabled_xdp_vsi(vsi))
2810 return err;
2811
2812 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
2813 err = i40e_setup_tx_descriptors(vsi->xdp_rings[i]);
2814
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002815 return err;
2816}
2817
2818/**
2819 * i40e_vsi_free_tx_resources - Free Tx resources for VSI queues
2820 * @vsi: ptr to the VSI
2821 *
2822 * Free VSI's transmit software resources
2823 **/
2824static void i40e_vsi_free_tx_resources(struct i40e_vsi *vsi)
2825{
2826 int i;
2827
Björn Töpel74608d12017-05-24 07:55:35 +02002828 if (vsi->tx_rings) {
2829 for (i = 0; i < vsi->num_queue_pairs; i++)
2830 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
2831 i40e_free_tx_resources(vsi->tx_rings[i]);
2832 }
Greg Rose8e9dca52013-12-18 13:45:53 +00002833
Björn Töpel74608d12017-05-24 07:55:35 +02002834 if (vsi->xdp_rings) {
2835 for (i = 0; i < vsi->num_queue_pairs; i++)
2836 if (vsi->xdp_rings[i] && vsi->xdp_rings[i]->desc)
2837 i40e_free_tx_resources(vsi->xdp_rings[i]);
2838 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002839}
2840
2841/**
2842 * i40e_vsi_setup_rx_resources - Allocate VSI queues Rx resources
2843 * @vsi: ptr to the VSI
2844 *
2845 * If this function returns with an error, then it's possible one or
2846 * more of the rings is populated (while the rest are not). It is the
2847 * callers duty to clean those orphaned rings.
2848 *
2849 * Return 0 on success, negative on failure
2850 **/
2851static int i40e_vsi_setup_rx_resources(struct i40e_vsi *vsi)
2852{
2853 int i, err = 0;
2854
2855 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e152013-09-28 06:00:58 +00002856 err = i40e_setup_rx_descriptors(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002857 return err;
2858}
2859
2860/**
2861 * i40e_vsi_free_rx_resources - Free Rx Resources for VSI queues
2862 * @vsi: ptr to the VSI
2863 *
2864 * Free all receive software resources
2865 **/
2866static void i40e_vsi_free_rx_resources(struct i40e_vsi *vsi)
2867{
2868 int i;
2869
Greg Rose8e9dca52013-12-18 13:45:53 +00002870 if (!vsi->rx_rings)
2871 return;
2872
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002873 for (i = 0; i < vsi->num_queue_pairs; i++)
Greg Rose8e9dca52013-12-18 13:45:53 +00002874 if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
Alexander Duyck9f65e152013-09-28 06:00:58 +00002875 i40e_free_rx_resources(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002876}
2877
2878/**
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002879 * i40e_config_xps_tx_ring - Configure XPS for a Tx ring
2880 * @ring: The Tx ring to configure
2881 *
2882 * This enables/disables XPS for a given Tx descriptor ring
2883 * based on the TCs enabled for the VSI that ring belongs to.
2884 **/
2885static void i40e_config_xps_tx_ring(struct i40e_ring *ring)
2886{
2887 struct i40e_vsi *vsi = ring->vsi;
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002888
Jesse Brandeburg9a660ee2015-02-26 16:13:22 +00002889 if (!ring->q_vector || !ring->netdev)
2890 return;
2891
Jacob Kellerba4460d2017-07-14 09:10:08 -04002892 if ((vsi->tc_config.numtc <= 1) &&
2893 !test_and_set_bit(__I40E_TX_XPS_INIT_DONE, &ring->state)) {
2894 netif_set_xps_queue(ring->netdev,
Jacob Keller759dc4a2017-07-14 09:10:10 -04002895 get_cpu_mask(ring->q_vector->v_idx),
Jacob Kellerba4460d2017-07-14 09:10:08 -04002896 ring->queue_index);
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002897 }
Jesse Brandeburg0e4425e2015-11-05 17:01:01 -08002898
2899 /* schedule our worker thread which will take care of
2900 * applying the new filter changes
2901 */
2902 i40e_service_event_schedule(vsi->back);
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002903}
2904
2905/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002906 * i40e_configure_tx_ring - Configure a transmit ring context and rest
2907 * @ring: The Tx ring to configure
2908 *
2909 * Configure the Tx descriptor ring in the HMC context.
2910 **/
2911static int i40e_configure_tx_ring(struct i40e_ring *ring)
2912{
2913 struct i40e_vsi *vsi = ring->vsi;
2914 u16 pf_q = vsi->base_queue + ring->queue_index;
2915 struct i40e_hw *hw = &vsi->back->hw;
2916 struct i40e_hmc_obj_txq tx_ctx;
2917 i40e_status err = 0;
2918 u32 qtx_ctl = 0;
2919
2920 /* some ATR related tx ring init */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08002921 if (vsi->back->flags & I40E_FLAG_FD_ATR_ENABLED) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002922 ring->atr_sample_rate = vsi->back->atr_sample_rate;
2923 ring->atr_count = 0;
2924 } else {
2925 ring->atr_sample_rate = 0;
2926 }
2927
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002928 /* configure XPS */
2929 i40e_config_xps_tx_ring(ring);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002930
2931 /* clear the context structure first */
2932 memset(&tx_ctx, 0, sizeof(tx_ctx));
2933
2934 tx_ctx.new_context = 1;
2935 tx_ctx.base = (ring->dma / 128);
2936 tx_ctx.qlen = ring->count;
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08002937 tx_ctx.fd_ena = !!(vsi->back->flags & (I40E_FLAG_FD_SB_ENABLED |
2938 I40E_FLAG_FD_ATR_ENABLED));
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00002939 tx_ctx.timesync_ena = !!(vsi->back->flags & I40E_FLAG_PTP);
Jesse Brandeburg1943d8b2014-02-14 02:14:40 +00002940 /* FDIR VSI tx ring can still use RS bit and writebacks */
2941 if (vsi->type != I40E_VSI_FDIR)
2942 tx_ctx.head_wb_ena = 1;
2943 tx_ctx.head_wb_addr = ring->dma +
2944 (ring->count * sizeof(struct i40e_tx_desc));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002945
2946 /* As part of VSI creation/update, FW allocates certain
2947 * Tx arbitration queue sets for each TC enabled for
2948 * the VSI. The FW returns the handles to these queue
2949 * sets as part of the response buffer to Add VSI,
2950 * Update VSI, etc. AQ commands. It is expected that
2951 * these queue set handles be associated with the Tx
2952 * queues by the driver as part of the TX queue context
2953 * initialization. This has to be done regardless of
2954 * DCB as by default everything is mapped to TC0.
2955 */
2956 tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[ring->dcb_tc]);
2957 tx_ctx.rdylist_act = 0;
2958
2959 /* clear the context in the HMC */
2960 err = i40e_clear_lan_tx_queue_context(hw, pf_q);
2961 if (err) {
2962 dev_info(&vsi->back->pdev->dev,
2963 "Failed to clear LAN Tx queue context on Tx ring %d (pf_q %d), error: %d\n",
2964 ring->queue_index, pf_q, err);
2965 return -ENOMEM;
2966 }
2967
2968 /* set the context in the HMC */
2969 err = i40e_set_lan_tx_queue_context(hw, pf_q, &tx_ctx);
2970 if (err) {
2971 dev_info(&vsi->back->pdev->dev,
2972 "Failed to set LAN Tx queue context on Tx ring %d (pf_q %d, error: %d\n",
2973 ring->queue_index, pf_q, err);
2974 return -ENOMEM;
2975 }
2976
2977 /* Now associate this queue with this PCI function */
Mitch Williams7a28d882014-10-17 03:14:52 +00002978 if (vsi->type == I40E_VSI_VMDQ2) {
Shannon Nelson9d8bf542014-01-14 00:49:50 -08002979 qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
Mitch Williams7a28d882014-10-17 03:14:52 +00002980 qtx_ctl |= ((vsi->id) << I40E_QTX_CTL_VFVM_INDX_SHIFT) &
2981 I40E_QTX_CTL_VFVM_INDX_MASK;
2982 } else {
Shannon Nelson9d8bf542014-01-14 00:49:50 -08002983 qtx_ctl = I40E_QTX_CTL_PF_QUEUE;
Mitch Williams7a28d882014-10-17 03:14:52 +00002984 }
2985
Shannon Nelson13fd9772013-09-28 07:14:19 +00002986 qtx_ctl |= ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT) &
2987 I40E_QTX_CTL_PF_INDX_MASK);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002988 wr32(hw, I40E_QTX_CTL(pf_q), qtx_ctl);
2989 i40e_flush(hw);
2990
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002991 /* cache tail off for easier writes later */
2992 ring->tail = hw->hw_addr + I40E_QTX_TAIL(pf_q);
2993
2994 return 0;
2995}
2996
2997/**
2998 * i40e_configure_rx_ring - Configure a receive ring context
2999 * @ring: The Rx ring to configure
3000 *
3001 * Configure the Rx descriptor ring in the HMC context.
3002 **/
3003static int i40e_configure_rx_ring(struct i40e_ring *ring)
3004{
3005 struct i40e_vsi *vsi = ring->vsi;
3006 u32 chain_len = vsi->back->hw.func_caps.rx_buf_chain_len;
3007 u16 pf_q = vsi->base_queue + ring->queue_index;
3008 struct i40e_hw *hw = &vsi->back->hw;
3009 struct i40e_hmc_obj_rxq rx_ctx;
3010 i40e_status err = 0;
3011
3012 ring->state = 0;
3013
3014 /* clear the context structure first */
3015 memset(&rx_ctx, 0, sizeof(rx_ctx));
3016
3017 ring->rx_buf_len = vsi->rx_buf_len;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003018
Alexander Duyckdab86af2017-03-14 10:15:27 -07003019 rx_ctx.dbuff = DIV_ROUND_UP(ring->rx_buf_len,
3020 BIT_ULL(I40E_RXQ_CTX_DBUFF_SHIFT));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003021
3022 rx_ctx.base = (ring->dma / 128);
3023 rx_ctx.qlen = ring->count;
3024
Jesse Brandeburgbec60fc2016-04-18 11:33:47 -07003025 /* use 32 byte descriptors */
3026 rx_ctx.dsize = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003027
Jesse Brandeburgbec60fc2016-04-18 11:33:47 -07003028 /* descriptor type is always zero
3029 * rx_ctx.dtype = 0;
3030 */
Jesse Brandeburgb32bfa172016-04-18 11:33:42 -07003031 rx_ctx.hsplit_0 = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003032
Jesse Brandeburgb32bfa172016-04-18 11:33:42 -07003033 rx_ctx.rxmax = min_t(u16, vsi->max_frame, chain_len * ring->rx_buf_len);
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00003034 if (hw->revision_id == 0)
3035 rx_ctx.lrxqthresh = 0;
3036 else
3037 rx_ctx.lrxqthresh = 2;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003038 rx_ctx.crcstrip = 1;
3039 rx_ctx.l2tsel = 1;
Jesse Brandeburgc4bbac32015-09-28 11:21:48 -07003040 /* this controls whether VLAN is stripped from inner headers */
3041 rx_ctx.showiv = 0;
Catherine Sullivanacb36762014-03-06 09:02:30 +00003042 /* set the prefena field to 1 because the manual says to */
3043 rx_ctx.prefena = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003044
3045 /* clear the context in the HMC */
3046 err = i40e_clear_lan_rx_queue_context(hw, pf_q);
3047 if (err) {
3048 dev_info(&vsi->back->pdev->dev,
3049 "Failed to clear LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
3050 ring->queue_index, pf_q, err);
3051 return -ENOMEM;
3052 }
3053
3054 /* set the context in the HMC */
3055 err = i40e_set_lan_rx_queue_context(hw, pf_q, &rx_ctx);
3056 if (err) {
3057 dev_info(&vsi->back->pdev->dev,
3058 "Failed to set LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
3059 ring->queue_index, pf_q, err);
3060 return -ENOMEM;
3061 }
3062
Alexander Duyckca9ec082017-04-05 07:51:02 -04003063 /* configure Rx buffer alignment */
3064 if (!vsi->netdev || (vsi->back->flags & I40E_FLAG_LEGACY_RX))
3065 clear_ring_build_skb_enabled(ring);
3066 else
3067 set_ring_build_skb_enabled(ring);
3068
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003069 /* cache tail for quicker writes, and clear the reg before use */
3070 ring->tail = hw->hw_addr + I40E_QRX_TAIL(pf_q);
3071 writel(0, ring->tail);
3072
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07003073 i40e_alloc_rx_buffers(ring, I40E_DESC_UNUSED(ring));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003074
3075 return 0;
3076}
3077
3078/**
3079 * i40e_vsi_configure_tx - Configure the VSI for Tx
3080 * @vsi: VSI structure describing this set of rings and resources
3081 *
3082 * Configure the Tx VSI for operation.
3083 **/
3084static int i40e_vsi_configure_tx(struct i40e_vsi *vsi)
3085{
3086 int err = 0;
3087 u16 i;
3088
Alexander Duyck9f65e152013-09-28 06:00:58 +00003089 for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
3090 err = i40e_configure_tx_ring(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003091
Björn Töpel74608d12017-05-24 07:55:35 +02003092 if (!i40e_enabled_xdp_vsi(vsi))
3093 return err;
3094
3095 for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
3096 err = i40e_configure_tx_ring(vsi->xdp_rings[i]);
3097
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003098 return err;
3099}
3100
3101/**
3102 * i40e_vsi_configure_rx - Configure the VSI for Rx
3103 * @vsi: the VSI being configured
3104 *
3105 * Configure the Rx VSI for operation.
3106 **/
3107static int i40e_vsi_configure_rx(struct i40e_vsi *vsi)
3108{
3109 int err = 0;
3110 u16 i;
3111
Alexander Duyckdab86af2017-03-14 10:15:27 -07003112 if (!vsi->netdev || (vsi->back->flags & I40E_FLAG_LEGACY_RX)) {
3113 vsi->max_frame = I40E_MAX_RXBUFFER;
3114 vsi->rx_buf_len = I40E_RXBUFFER_2048;
3115#if (PAGE_SIZE < 8192)
Alexander Duyckca9ec082017-04-05 07:51:02 -04003116 } else if (!I40E_2K_TOO_SMALL_WITH_PADDING &&
3117 (vsi->netdev->mtu <= ETH_DATA_LEN)) {
Alexander Duyckdab86af2017-03-14 10:15:27 -07003118 vsi->max_frame = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
3119 vsi->rx_buf_len = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
3120#endif
3121 } else {
3122 vsi->max_frame = I40E_MAX_RXBUFFER;
Alexander Duyck98efd692017-04-05 07:51:01 -04003123 vsi->rx_buf_len = (PAGE_SIZE < 8192) ? I40E_RXBUFFER_3072 :
3124 I40E_RXBUFFER_2048;
Alexander Duyckdab86af2017-03-14 10:15:27 -07003125 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003126
3127 /* set up individual rings */
3128 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e152013-09-28 06:00:58 +00003129 err = i40e_configure_rx_ring(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003130
3131 return err;
3132}
3133
3134/**
3135 * i40e_vsi_config_dcb_rings - Update rings to reflect DCB TC
3136 * @vsi: ptr to the VSI
3137 **/
3138static void i40e_vsi_config_dcb_rings(struct i40e_vsi *vsi)
3139{
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +00003140 struct i40e_ring *tx_ring, *rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003141 u16 qoffset, qcount;
3142 int i, n;
3143
Parikh, Neeravcd238a32015-02-21 06:43:37 +00003144 if (!(vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
3145 /* Reset the TC information */
3146 for (i = 0; i < vsi->num_queue_pairs; i++) {
3147 rx_ring = vsi->rx_rings[i];
3148 tx_ring = vsi->tx_rings[i];
3149 rx_ring->dcb_tc = 0;
3150 tx_ring->dcb_tc = 0;
3151 }
3152 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003153
3154 for (n = 0; n < I40E_MAX_TRAFFIC_CLASS; n++) {
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04003155 if (!(vsi->tc_config.enabled_tc & BIT_ULL(n)))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003156 continue;
3157
3158 qoffset = vsi->tc_config.tc_info[n].qoffset;
3159 qcount = vsi->tc_config.tc_info[n].qcount;
3160 for (i = qoffset; i < (qoffset + qcount); i++) {
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +00003161 rx_ring = vsi->rx_rings[i];
3162 tx_ring = vsi->tx_rings[i];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003163 rx_ring->dcb_tc = n;
3164 tx_ring->dcb_tc = n;
3165 }
3166 }
3167}
3168
3169/**
3170 * i40e_set_vsi_rx_mode - Call set_rx_mode on a VSI
3171 * @vsi: ptr to the VSI
3172 **/
3173static void i40e_set_vsi_rx_mode(struct i40e_vsi *vsi)
3174{
3175 if (vsi->netdev)
3176 i40e_set_rx_mode(vsi->netdev);
3177}
3178
3179/**
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00003180 * i40e_fdir_filter_restore - Restore the Sideband Flow Director filters
3181 * @vsi: Pointer to the targeted VSI
3182 *
3183 * This function replays the hlist on the hw where all the SB Flow Director
3184 * filters were saved.
3185 **/
3186static void i40e_fdir_filter_restore(struct i40e_vsi *vsi)
3187{
3188 struct i40e_fdir_filter *filter;
3189 struct i40e_pf *pf = vsi->back;
3190 struct hlist_node *node;
3191
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00003192 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
3193 return;
3194
Jacob Keller6d069422017-02-06 14:38:44 -08003195 /* Reset FDir counters as we're replaying all existing filters */
Jacob Keller097dbf52017-02-06 14:38:46 -08003196 pf->fd_tcp4_filter_cnt = 0;
3197 pf->fd_udp4_filter_cnt = 0;
Jacob Kellerf223c872017-02-06 14:38:51 -08003198 pf->fd_sctp4_filter_cnt = 0;
Jacob Keller097dbf52017-02-06 14:38:46 -08003199 pf->fd_ip4_filter_cnt = 0;
Jacob Keller6d069422017-02-06 14:38:44 -08003200
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00003201 hlist_for_each_entry_safe(filter, node,
3202 &pf->fdir_filter_list, fdir_node) {
3203 i40e_add_del_fdir(vsi, filter, true);
3204 }
3205}
3206
3207/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003208 * i40e_vsi_configure - Set up the VSI for action
3209 * @vsi: the VSI being configured
3210 **/
3211static int i40e_vsi_configure(struct i40e_vsi *vsi)
3212{
3213 int err;
3214
3215 i40e_set_vsi_rx_mode(vsi);
3216 i40e_restore_vlan(vsi);
3217 i40e_vsi_config_dcb_rings(vsi);
3218 err = i40e_vsi_configure_tx(vsi);
3219 if (!err)
3220 err = i40e_vsi_configure_rx(vsi);
3221
3222 return err;
3223}
3224
3225/**
3226 * i40e_vsi_configure_msix - MSIX mode Interrupt Config in the HW
3227 * @vsi: the VSI being configured
3228 **/
3229static void i40e_vsi_configure_msix(struct i40e_vsi *vsi)
3230{
Björn Töpel74608d12017-05-24 07:55:35 +02003231 bool has_xdp = i40e_enabled_xdp_vsi(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003232 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003233 struct i40e_hw *hw = &pf->hw;
3234 u16 vector;
3235 int i, q;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003236 u32 qp;
3237
3238 /* The interrupt indexing is offset by 1 in the PFINT_ITRn
3239 * and PFINT_LNKLSTn registers, e.g.:
3240 * PFINT_ITRn[0..n-1] gets msix-1..msix-n (qpair interrupts)
3241 */
3242 qp = vsi->base_queue;
3243 vector = vsi->base_vector;
Alexander Duyck493fb302013-09-28 07:01:44 +00003244 for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
Jesse Brandeburgac26fc12015-09-28 14:12:37 -04003245 struct i40e_q_vector *q_vector = vsi->q_vectors[i];
3246
Jesse Brandeburgee2319c2015-09-28 14:16:54 -04003247 q_vector->itr_countdown = ITR_COUNTDOWN_START;
Kan Lianga75e8002016-02-19 09:24:04 -05003248 q_vector->rx.itr = ITR_TO_REG(vsi->rx_rings[i]->rx_itr_setting);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003249 q_vector->rx.latency_range = I40E_LOW_LATENCY;
3250 wr32(hw, I40E_PFINT_ITRN(I40E_RX_ITR, vector - 1),
3251 q_vector->rx.itr);
Kan Lianga75e8002016-02-19 09:24:04 -05003252 q_vector->tx.itr = ITR_TO_REG(vsi->tx_rings[i]->tx_itr_setting);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003253 q_vector->tx.latency_range = I40E_LOW_LATENCY;
3254 wr32(hw, I40E_PFINT_ITRN(I40E_TX_ITR, vector - 1),
3255 q_vector->tx.itr);
Jesse Brandeburgac26fc12015-09-28 14:12:37 -04003256 wr32(hw, I40E_PFINT_RATEN(vector - 1),
Alan Brady1c0e6a32016-11-28 16:06:02 -08003257 i40e_intrl_usec_to_reg(vsi->int_rate_limit));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003258
3259 /* Linked list for the queuepairs assigned to this vector */
3260 wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), qp);
3261 for (q = 0; q < q_vector->num_ringpairs; q++) {
Björn Töpel74608d12017-05-24 07:55:35 +02003262 u32 nextqp = has_xdp ? qp + vsi->alloc_queue_pairs : qp;
Jesse Brandeburgac26fc12015-09-28 14:12:37 -04003263 u32 val;
3264
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003265 val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
Björn Töpel74608d12017-05-24 07:55:35 +02003266 (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
3267 (vector << I40E_QINT_RQCTL_MSIX_INDX_SHIFT) |
3268 (nextqp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT) |
3269 (I40E_QUEUE_TYPE_TX <<
3270 I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003271
3272 wr32(hw, I40E_QINT_RQCTL(qp), val);
3273
Björn Töpel74608d12017-05-24 07:55:35 +02003274 if (has_xdp) {
3275 val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3276 (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3277 (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
3278 (qp << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) |
3279 (I40E_QUEUE_TYPE_TX <<
3280 I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3281
3282 wr32(hw, I40E_QINT_TQCTL(nextqp), val);
3283 }
3284
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003285 val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
Björn Töpel74608d12017-05-24 07:55:35 +02003286 (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3287 (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
3288 ((qp + 1) << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) |
3289 (I40E_QUEUE_TYPE_RX <<
3290 I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003291
3292 /* Terminate the linked list */
3293 if (q == (q_vector->num_ringpairs - 1))
Björn Töpel74608d12017-05-24 07:55:35 +02003294 val |= (I40E_QUEUE_END_OF_LIST <<
3295 I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003296
3297 wr32(hw, I40E_QINT_TQCTL(qp), val);
3298 qp++;
3299 }
3300 }
3301
3302 i40e_flush(hw);
3303}
3304
3305/**
3306 * i40e_enable_misc_int_causes - enable the non-queue interrupts
3307 * @hw: ptr to the hardware info
3308 **/
Jacob Kellerab437b52014-12-14 01:55:08 +00003309static void i40e_enable_misc_int_causes(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003310{
Jacob Kellerab437b52014-12-14 01:55:08 +00003311 struct i40e_hw *hw = &pf->hw;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003312 u32 val;
3313
3314 /* clear things first */
3315 wr32(hw, I40E_PFINT_ICR0_ENA, 0); /* disable all */
3316 rd32(hw, I40E_PFINT_ICR0); /* read to clear */
3317
3318 val = I40E_PFINT_ICR0_ENA_ECC_ERR_MASK |
3319 I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK |
3320 I40E_PFINT_ICR0_ENA_GRST_MASK |
3321 I40E_PFINT_ICR0_ENA_PCI_EXCEPTION_MASK |
3322 I40E_PFINT_ICR0_ENA_GPIO_MASK |
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003323 I40E_PFINT_ICR0_ENA_HMC_ERR_MASK |
3324 I40E_PFINT_ICR0_ENA_VFLR_MASK |
3325 I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
3326
Anjali Singhai Jain0d8e1432015-06-05 12:20:32 -04003327 if (pf->flags & I40E_FLAG_IWARP_ENABLED)
3328 val |= I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
3329
Jacob Kellerab437b52014-12-14 01:55:08 +00003330 if (pf->flags & I40E_FLAG_PTP)
3331 val |= I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
3332
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003333 wr32(hw, I40E_PFINT_ICR0_ENA, val);
3334
3335 /* SW_ITR_IDX = 0, but don't change INTENA */
Anjali Singhai Jain84ed40e2013-11-26 10:49:32 +00003336 wr32(hw, I40E_PFINT_DYN_CTL0, I40E_PFINT_DYN_CTL0_SW_ITR_INDX_MASK |
3337 I40E_PFINT_DYN_CTL0_INTENA_MSK_MASK);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003338
3339 /* OTHER_ITR_IDX = 0 */
3340 wr32(hw, I40E_PFINT_STAT_CTL0, 0);
3341}
3342
3343/**
3344 * i40e_configure_msi_and_legacy - Legacy mode interrupt config in the HW
3345 * @vsi: the VSI being configured
3346 **/
3347static void i40e_configure_msi_and_legacy(struct i40e_vsi *vsi)
3348{
Björn Töpel74608d12017-05-24 07:55:35 +02003349 u32 nextqp = i40e_enabled_xdp_vsi(vsi) ? vsi->alloc_queue_pairs : 0;
Alexander Duyck493fb302013-09-28 07:01:44 +00003350 struct i40e_q_vector *q_vector = vsi->q_vectors[0];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003351 struct i40e_pf *pf = vsi->back;
3352 struct i40e_hw *hw = &pf->hw;
3353 u32 val;
3354
3355 /* set the ITR configuration */
Jesse Brandeburgee2319c2015-09-28 14:16:54 -04003356 q_vector->itr_countdown = ITR_COUNTDOWN_START;
Kan Lianga75e8002016-02-19 09:24:04 -05003357 q_vector->rx.itr = ITR_TO_REG(vsi->rx_rings[0]->rx_itr_setting);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003358 q_vector->rx.latency_range = I40E_LOW_LATENCY;
3359 wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), q_vector->rx.itr);
Kan Lianga75e8002016-02-19 09:24:04 -05003360 q_vector->tx.itr = ITR_TO_REG(vsi->tx_rings[0]->tx_itr_setting);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003361 q_vector->tx.latency_range = I40E_LOW_LATENCY;
3362 wr32(hw, I40E_PFINT_ITR0(I40E_TX_ITR), q_vector->tx.itr);
3363
Jacob Kellerab437b52014-12-14 01:55:08 +00003364 i40e_enable_misc_int_causes(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003365
3366 /* FIRSTQ_INDX = 0, FIRSTQ_TYPE = 0 (rx) */
3367 wr32(hw, I40E_PFINT_LNKLST0, 0);
3368
Jesse Brandeburgf29eaa32014-02-11 08:24:12 +00003369 /* Associate the queue pair to the vector and enable the queue int */
Björn Töpel74608d12017-05-24 07:55:35 +02003370 val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3371 (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
3372 (nextqp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT)|
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003373 (I40E_QUEUE_TYPE_TX << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3374
3375 wr32(hw, I40E_QINT_RQCTL(0), val);
3376
Björn Töpel74608d12017-05-24 07:55:35 +02003377 if (i40e_enabled_xdp_vsi(vsi)) {
3378 val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3379 (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT)|
3380 (I40E_QUEUE_TYPE_TX
3381 << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3382
3383 wr32(hw, I40E_QINT_TQCTL(nextqp), val);
3384 }
3385
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003386 val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3387 (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3388 (I40E_QUEUE_END_OF_LIST << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
3389
3390 wr32(hw, I40E_QINT_TQCTL(0), val);
3391 i40e_flush(hw);
3392}
3393
3394/**
Mitch Williams2ef28cf2013-11-28 06:39:32 +00003395 * i40e_irq_dynamic_disable_icr0 - Disable default interrupt generation for icr0
3396 * @pf: board private structure
3397 **/
3398void i40e_irq_dynamic_disable_icr0(struct i40e_pf *pf)
3399{
3400 struct i40e_hw *hw = &pf->hw;
3401
3402 wr32(hw, I40E_PFINT_DYN_CTL0,
3403 I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
3404 i40e_flush(hw);
3405}
3406
3407/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003408 * i40e_irq_dynamic_enable_icr0 - Enable default interrupt generation for icr0
3409 * @pf: board private structure
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08003410 * @clearpba: true when all pending interrupt events should be cleared
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003411 **/
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08003412void i40e_irq_dynamic_enable_icr0(struct i40e_pf *pf, bool clearpba)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003413{
3414 struct i40e_hw *hw = &pf->hw;
3415 u32 val;
3416
3417 val = I40E_PFINT_DYN_CTL0_INTENA_MASK |
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08003418 (clearpba ? I40E_PFINT_DYN_CTL0_CLEARPBA_MASK : 0) |
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003419 (I40E_ITR_NONE << I40E_PFINT_DYN_CTL0_ITR_INDX_SHIFT);
3420
3421 wr32(hw, I40E_PFINT_DYN_CTL0, val);
3422 i40e_flush(hw);
3423}
3424
3425/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003426 * i40e_msix_clean_rings - MSIX mode Interrupt Handler
3427 * @irq: interrupt number
3428 * @data: pointer to a q_vector
3429 **/
3430static irqreturn_t i40e_msix_clean_rings(int irq, void *data)
3431{
3432 struct i40e_q_vector *q_vector = data;
3433
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003434 if (!q_vector->tx.ring && !q_vector->rx.ring)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003435 return IRQ_HANDLED;
3436
Alexander Duyck5d3465a2015-09-29 15:19:50 -07003437 napi_schedule_irqoff(&q_vector->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003438
3439 return IRQ_HANDLED;
3440}
3441
3442/**
Alan Brady96db7762016-09-14 16:24:38 -07003443 * i40e_irq_affinity_notify - Callback for affinity changes
3444 * @notify: context as to what irq was changed
3445 * @mask: the new affinity mask
3446 *
3447 * This is a callback function used by the irq_set_affinity_notifier function
3448 * so that we may register to receive changes to the irq affinity masks.
3449 **/
3450static void i40e_irq_affinity_notify(struct irq_affinity_notify *notify,
3451 const cpumask_t *mask)
3452{
3453 struct i40e_q_vector *q_vector =
3454 container_of(notify, struct i40e_q_vector, affinity_notify);
3455
Jacob Keller7e4d01e2017-07-12 05:46:05 -04003456 cpumask_copy(&q_vector->affinity_mask, mask);
Alan Brady96db7762016-09-14 16:24:38 -07003457}
3458
3459/**
3460 * i40e_irq_affinity_release - Callback for affinity notifier release
3461 * @ref: internal core kernel usage
3462 *
3463 * This is a callback function used by the irq_set_affinity_notifier function
3464 * to inform the current notification subscriber that they will no longer
3465 * receive notifications.
3466 **/
3467static void i40e_irq_affinity_release(struct kref *ref) {}
3468
3469/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003470 * i40e_vsi_request_irq_msix - Initialize MSI-X interrupts
3471 * @vsi: the VSI being configured
3472 * @basename: name for the vector
3473 *
3474 * Allocates MSI-X vectors and requests interrupts from the kernel.
3475 **/
3476static int i40e_vsi_request_irq_msix(struct i40e_vsi *vsi, char *basename)
3477{
3478 int q_vectors = vsi->num_q_vectors;
3479 struct i40e_pf *pf = vsi->back;
3480 int base = vsi->base_vector;
3481 int rx_int_idx = 0;
3482 int tx_int_idx = 0;
3483 int vector, err;
Alan Brady96db7762016-09-14 16:24:38 -07003484 int irq_num;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003485
3486 for (vector = 0; vector < q_vectors; vector++) {
Alexander Duyck493fb302013-09-28 07:01:44 +00003487 struct i40e_q_vector *q_vector = vsi->q_vectors[vector];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003488
Alan Brady96db7762016-09-14 16:24:38 -07003489 irq_num = pf->msix_entries[base + vector].vector;
3490
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003491 if (q_vector->tx.ring && 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, "TxRx", rx_int_idx++);
3494 tx_int_idx++;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003495 } else if (q_vector->rx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003496 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3497 "%s-%s-%d", basename, "rx", rx_int_idx++);
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003498 } else if (q_vector->tx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003499 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3500 "%s-%s-%d", basename, "tx", tx_int_idx++);
3501 } else {
3502 /* skip this unused q_vector */
3503 continue;
3504 }
Alan Brady96db7762016-09-14 16:24:38 -07003505 err = request_irq(irq_num,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003506 vsi->irq_handler,
3507 0,
3508 q_vector->name,
3509 q_vector);
3510 if (err) {
3511 dev_info(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04003512 "MSIX request_irq failed, error: %d\n", err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003513 goto free_queue_irqs;
3514 }
Alan Brady96db7762016-09-14 16:24:38 -07003515
3516 /* register for affinity change notifications */
3517 q_vector->affinity_notify.notify = i40e_irq_affinity_notify;
3518 q_vector->affinity_notify.release = i40e_irq_affinity_release;
3519 irq_set_affinity_notifier(irq_num, &q_vector->affinity_notify);
Jacob Keller759dc4a2017-07-14 09:10:10 -04003520 /* get_cpu_mask returns a static constant mask with
3521 * a permanent lifetime so it's ok to use here.
3522 */
3523 irq_set_affinity_hint(irq_num, get_cpu_mask(q_vector->v_idx));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003524 }
3525
Shannon Nelson63741842014-04-23 04:50:16 +00003526 vsi->irqs_ready = true;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003527 return 0;
3528
3529free_queue_irqs:
3530 while (vector) {
3531 vector--;
Alan Brady96db7762016-09-14 16:24:38 -07003532 irq_num = pf->msix_entries[base + vector].vector;
3533 irq_set_affinity_notifier(irq_num, NULL);
3534 irq_set_affinity_hint(irq_num, NULL);
3535 free_irq(irq_num, &vsi->q_vectors[vector]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003536 }
3537 return err;
3538}
3539
3540/**
3541 * i40e_vsi_disable_irq - Mask off queue interrupt generation on the VSI
3542 * @vsi: the VSI being un-configured
3543 **/
3544static void i40e_vsi_disable_irq(struct i40e_vsi *vsi)
3545{
3546 struct i40e_pf *pf = vsi->back;
3547 struct i40e_hw *hw = &pf->hw;
3548 int base = vsi->base_vector;
3549 int i;
3550
Shannon Nelson2e5c26ea2017-06-07 05:43:11 -04003551 /* disable interrupt causation from each queue */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003552 for (i = 0; i < vsi->num_queue_pairs; i++) {
Shannon Nelson2e5c26ea2017-06-07 05:43:11 -04003553 u32 val;
3554
3555 val = rd32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx));
3556 val &= ~I40E_QINT_TQCTL_CAUSE_ENA_MASK;
3557 wr32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx), val);
3558
3559 val = rd32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx));
3560 val &= ~I40E_QINT_RQCTL_CAUSE_ENA_MASK;
3561 wr32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx), val);
3562
Björn Töpel74608d12017-05-24 07:55:35 +02003563 if (!i40e_enabled_xdp_vsi(vsi))
3564 continue;
3565 wr32(hw, I40E_QINT_TQCTL(vsi->xdp_rings[i]->reg_idx), 0);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003566 }
3567
Shannon Nelson2e5c26ea2017-06-07 05:43:11 -04003568 /* disable each interrupt */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003569 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3570 for (i = vsi->base_vector;
3571 i < (vsi->num_q_vectors + vsi->base_vector); i++)
3572 wr32(hw, I40E_PFINT_DYN_CTLN(i - 1), 0);
3573
3574 i40e_flush(hw);
3575 for (i = 0; i < vsi->num_q_vectors; i++)
3576 synchronize_irq(pf->msix_entries[i + base].vector);
3577 } else {
3578 /* Legacy and MSI mode - this stops all interrupt handling */
3579 wr32(hw, I40E_PFINT_ICR0_ENA, 0);
3580 wr32(hw, I40E_PFINT_DYN_CTL0, 0);
3581 i40e_flush(hw);
3582 synchronize_irq(pf->pdev->irq);
3583 }
3584}
3585
3586/**
3587 * i40e_vsi_enable_irq - Enable IRQ for the given VSI
3588 * @vsi: the VSI being configured
3589 **/
3590static int i40e_vsi_enable_irq(struct i40e_vsi *vsi)
3591{
3592 struct i40e_pf *pf = vsi->back;
3593 int i;
3594
3595 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
Jesse Brandeburg78455482015-07-23 16:54:41 -04003596 for (i = 0; i < vsi->num_q_vectors; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003597 i40e_irq_dynamic_enable(vsi, i);
3598 } else {
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08003599 i40e_irq_dynamic_enable_icr0(pf, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003600 }
3601
Jesse Brandeburg1022cb62013-09-28 07:13:08 +00003602 i40e_flush(&pf->hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003603 return 0;
3604}
3605
3606/**
3607 * i40e_stop_misc_vector - Stop the vector that handles non-queue events
3608 * @pf: board private structure
3609 **/
3610static void i40e_stop_misc_vector(struct i40e_pf *pf)
3611{
3612 /* Disable ICR 0 */
3613 wr32(&pf->hw, I40E_PFINT_ICR0_ENA, 0);
3614 i40e_flush(&pf->hw);
3615}
3616
3617/**
3618 * i40e_intr - MSI/Legacy and non-queue interrupt handler
3619 * @irq: interrupt number
3620 * @data: pointer to a q_vector
3621 *
3622 * This is the handler used for all MSI/Legacy interrupts, and deals
3623 * with both queue and non-queue interrupts. This is also used in
3624 * MSIX mode to handle the non-queue interrupts.
3625 **/
3626static irqreturn_t i40e_intr(int irq, void *data)
3627{
3628 struct i40e_pf *pf = (struct i40e_pf *)data;
3629 struct i40e_hw *hw = &pf->hw;
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003630 irqreturn_t ret = IRQ_NONE;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003631 u32 icr0, icr0_remaining;
3632 u32 val, ena_mask;
3633
3634 icr0 = rd32(hw, I40E_PFINT_ICR0);
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003635 ena_mask = rd32(hw, I40E_PFINT_ICR0_ENA);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003636
Shannon Nelson116a57d2013-09-28 07:13:59 +00003637 /* if sharing a legacy IRQ, we might get called w/o an intr pending */
3638 if ((icr0 & I40E_PFINT_ICR0_INTEVENT_MASK) == 0)
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003639 goto enable_intr;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003640
Shannon Nelsoncd92e722013-11-16 10:00:44 +00003641 /* if interrupt but no bits showing, must be SWINT */
3642 if (((icr0 & ~I40E_PFINT_ICR0_INTEVENT_MASK) == 0) ||
3643 (icr0 & I40E_PFINT_ICR0_SWINT_MASK))
3644 pf->sw_int_count++;
3645
Anjali Singhai Jain0d8e1432015-06-05 12:20:32 -04003646 if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
Catherine Sullivan76429842017-06-07 05:43:12 -04003647 (icr0 & I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK)) {
Anjali Singhai Jain0d8e1432015-06-05 12:20:32 -04003648 ena_mask &= ~I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
Carolyn Wyborny23bb6dc2016-11-08 13:05:12 -08003649 dev_dbg(&pf->pdev->dev, "cleared PE_CRITERR\n");
Catherine Sullivan76429842017-06-07 05:43:12 -04003650 set_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
Anjali Singhai Jain0d8e1432015-06-05 12:20:32 -04003651 }
3652
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003653 /* only q0 is used in MSI/Legacy mode, and none are used in MSIX */
3654 if (icr0 & I40E_PFINT_ICR0_QUEUE_0_MASK) {
Alexander Duyck5d3465a2015-09-29 15:19:50 -07003655 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
3656 struct i40e_q_vector *q_vector = vsi->q_vectors[0];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003657
Anjali Singhai Jaina16ae2d2016-01-15 14:33:16 -08003658 /* We do not have a way to disarm Queue causes while leaving
3659 * interrupt enabled for all other causes, ideally
3660 * interrupt should be disabled while we are in NAPI but
3661 * this is not a performance path and napi_schedule()
3662 * can deal with rescheduling.
3663 */
Mauro S. M. Rodrigues9e6c9c02017-05-12 23:26:56 -03003664 if (!test_bit(__I40E_DOWN, pf->state))
Alexander Duyck5d3465a2015-09-29 15:19:50 -07003665 napi_schedule_irqoff(&q_vector->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003666 }
3667
3668 if (icr0 & I40E_PFINT_ICR0_ADMINQ_MASK) {
3669 ena_mask &= ~I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
Jacob Keller0da36b92017-04-19 09:25:55 -04003670 set_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state);
Shannon Nelson6e93d0c2016-01-15 14:33:18 -08003671 i40e_debug(&pf->hw, I40E_DEBUG_NVM, "AdminQ event\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003672 }
3673
3674 if (icr0 & I40E_PFINT_ICR0_MAL_DETECT_MASK) {
3675 ena_mask &= ~I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
Jacob Keller0da36b92017-04-19 09:25:55 -04003676 set_bit(__I40E_MDD_EVENT_PENDING, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003677 }
3678
3679 if (icr0 & I40E_PFINT_ICR0_VFLR_MASK) {
3680 ena_mask &= ~I40E_PFINT_ICR0_ENA_VFLR_MASK;
Jacob Keller0da36b92017-04-19 09:25:55 -04003681 set_bit(__I40E_VFLR_EVENT_PENDING, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003682 }
3683
3684 if (icr0 & I40E_PFINT_ICR0_GRST_MASK) {
Jacob Keller0da36b92017-04-19 09:25:55 -04003685 if (!test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
3686 set_bit(__I40E_RESET_INTR_RECEIVED, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003687 ena_mask &= ~I40E_PFINT_ICR0_ENA_GRST_MASK;
3688 val = rd32(hw, I40E_GLGEN_RSTAT);
3689 val = (val & I40E_GLGEN_RSTAT_RESET_TYPE_MASK)
3690 >> I40E_GLGEN_RSTAT_RESET_TYPE_SHIFT;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003691 if (val == I40E_RESET_CORER) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003692 pf->corer_count++;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003693 } else if (val == I40E_RESET_GLOBR) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003694 pf->globr_count++;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003695 } else if (val == I40E_RESET_EMPR) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003696 pf->empr_count++;
Jacob Keller0da36b92017-04-19 09:25:55 -04003697 set_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state);
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003698 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003699 }
3700
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003701 if (icr0 & I40E_PFINT_ICR0_HMC_ERR_MASK) {
3702 icr0 &= ~I40E_PFINT_ICR0_HMC_ERR_MASK;
3703 dev_info(&pf->pdev->dev, "HMC error interrupt\n");
Anjali Singhai Jain25fc0e62015-03-31 00:45:01 -07003704 dev_info(&pf->pdev->dev, "HMC error info 0x%x, HMC error data 0x%x\n",
3705 rd32(hw, I40E_PFHMC_ERRORINFO),
3706 rd32(hw, I40E_PFHMC_ERRORDATA));
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003707 }
3708
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003709 if (icr0 & I40E_PFINT_ICR0_TIMESYNC_MASK) {
3710 u32 prttsyn_stat = rd32(hw, I40E_PRTTSYN_STAT_0);
3711
3712 if (prttsyn_stat & I40E_PRTTSYN_STAT_0_TXTIME_MASK) {
Jacob Kellercafa1fc2014-04-24 18:05:03 -07003713 icr0 &= ~I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003714 i40e_ptp_tx_hwtstamp(pf);
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003715 }
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003716 }
3717
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003718 /* If a critical error is pending we have no choice but to reset the
3719 * device.
3720 * Report and mask out any remaining unexpected interrupts.
3721 */
3722 icr0_remaining = icr0 & ena_mask;
3723 if (icr0_remaining) {
3724 dev_info(&pf->pdev->dev, "unhandled interrupt icr0=0x%08x\n",
3725 icr0_remaining);
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003726 if ((icr0_remaining & I40E_PFINT_ICR0_PE_CRITERR_MASK) ||
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003727 (icr0_remaining & I40E_PFINT_ICR0_PCI_EXCEPTION_MASK) ||
Anjali Singhai Jainc0c28972014-02-12 01:45:34 +00003728 (icr0_remaining & I40E_PFINT_ICR0_ECC_ERR_MASK)) {
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003729 dev_info(&pf->pdev->dev, "device will be reset\n");
Jacob Keller0da36b92017-04-19 09:25:55 -04003730 set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003731 i40e_service_event_schedule(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003732 }
3733 ena_mask &= ~icr0_remaining;
3734 }
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003735 ret = IRQ_HANDLED;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003736
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003737enable_intr:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003738 /* re-enable interrupt causes */
3739 wr32(hw, I40E_PFINT_ICR0_ENA, ena_mask);
Mauro S. M. Rodrigues9e6c9c02017-05-12 23:26:56 -03003740 if (!test_bit(__I40E_DOWN, pf->state)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003741 i40e_service_event_schedule(pf);
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08003742 i40e_irq_dynamic_enable_icr0(pf, false);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003743 }
3744
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003745 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003746}
3747
3748/**
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003749 * i40e_clean_fdir_tx_irq - Reclaim resources after transmit completes
3750 * @tx_ring: tx ring to clean
3751 * @budget: how many cleans we're allowed
3752 *
3753 * Returns true if there's any budget left (e.g. the clean is finished)
3754 **/
3755static bool i40e_clean_fdir_tx_irq(struct i40e_ring *tx_ring, int budget)
3756{
3757 struct i40e_vsi *vsi = tx_ring->vsi;
3758 u16 i = tx_ring->next_to_clean;
3759 struct i40e_tx_buffer *tx_buf;
3760 struct i40e_tx_desc *tx_desc;
3761
3762 tx_buf = &tx_ring->tx_bi[i];
3763 tx_desc = I40E_TX_DESC(tx_ring, i);
3764 i -= tx_ring->count;
3765
3766 do {
3767 struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
3768
3769 /* if next_to_watch is not set then there is no work pending */
3770 if (!eop_desc)
3771 break;
3772
3773 /* prevent any other reads prior to eop_desc */
3774 read_barrier_depends();
3775
3776 /* if the descriptor isn't done, no work yet to do */
3777 if (!(eop_desc->cmd_type_offset_bsz &
3778 cpu_to_le64(I40E_TX_DESC_DTYPE_DESC_DONE)))
3779 break;
3780
3781 /* clear next_to_watch to prevent false hangs */
3782 tx_buf->next_to_watch = NULL;
3783
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003784 tx_desc->buffer_addr = 0;
3785 tx_desc->cmd_type_offset_bsz = 0;
3786 /* move past filter desc */
3787 tx_buf++;
3788 tx_desc++;
3789 i++;
3790 if (unlikely(!i)) {
3791 i -= tx_ring->count;
3792 tx_buf = tx_ring->tx_bi;
3793 tx_desc = I40E_TX_DESC(tx_ring, 0);
3794 }
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003795 /* unmap skb header data */
3796 dma_unmap_single(tx_ring->dev,
3797 dma_unmap_addr(tx_buf, dma),
3798 dma_unmap_len(tx_buf, len),
3799 DMA_TO_DEVICE);
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003800 if (tx_buf->tx_flags & I40E_TX_FLAGS_FD_SB)
3801 kfree(tx_buf->raw_buf);
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003802
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003803 tx_buf->raw_buf = NULL;
3804 tx_buf->tx_flags = 0;
3805 tx_buf->next_to_watch = NULL;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003806 dma_unmap_len_set(tx_buf, len, 0);
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003807 tx_desc->buffer_addr = 0;
3808 tx_desc->cmd_type_offset_bsz = 0;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003809
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003810 /* move us past the eop_desc for start of next FD desc */
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003811 tx_buf++;
3812 tx_desc++;
3813 i++;
3814 if (unlikely(!i)) {
3815 i -= tx_ring->count;
3816 tx_buf = tx_ring->tx_bi;
3817 tx_desc = I40E_TX_DESC(tx_ring, 0);
3818 }
3819
3820 /* update budget accounting */
3821 budget--;
3822 } while (likely(budget));
3823
3824 i += tx_ring->count;
3825 tx_ring->next_to_clean = i;
3826
Jesse Brandeburg6995b362015-08-28 17:55:54 -04003827 if (vsi->back->flags & I40E_FLAG_MSIX_ENABLED)
Jesse Brandeburg78455482015-07-23 16:54:41 -04003828 i40e_irq_dynamic_enable(vsi, tx_ring->q_vector->v_idx);
Jesse Brandeburg6995b362015-08-28 17:55:54 -04003829
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003830 return budget > 0;
3831}
3832
3833/**
3834 * i40e_fdir_clean_ring - Interrupt Handler for FDIR SB ring
3835 * @irq: interrupt number
3836 * @data: pointer to a q_vector
3837 **/
3838static irqreturn_t i40e_fdir_clean_ring(int irq, void *data)
3839{
3840 struct i40e_q_vector *q_vector = data;
3841 struct i40e_vsi *vsi;
3842
3843 if (!q_vector->tx.ring)
3844 return IRQ_HANDLED;
3845
3846 vsi = q_vector->tx.ring->vsi;
3847 i40e_clean_fdir_tx_irq(q_vector->tx.ring, vsi->work_limit);
3848
3849 return IRQ_HANDLED;
3850}
3851
3852/**
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003853 * i40e_map_vector_to_qp - Assigns the queue pair to the vector
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003854 * @vsi: the VSI being configured
3855 * @v_idx: vector index
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003856 * @qp_idx: queue pair index
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003857 **/
Anjali Singhai Jain26cdc442015-07-10 19:36:00 -04003858static void i40e_map_vector_to_qp(struct i40e_vsi *vsi, int v_idx, int qp_idx)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003859{
Alexander Duyck493fb302013-09-28 07:01:44 +00003860 struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
Alexander Duyck9f65e152013-09-28 06:00:58 +00003861 struct i40e_ring *tx_ring = vsi->tx_rings[qp_idx];
3862 struct i40e_ring *rx_ring = vsi->rx_rings[qp_idx];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003863
3864 tx_ring->q_vector = q_vector;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003865 tx_ring->next = q_vector->tx.ring;
3866 q_vector->tx.ring = tx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003867 q_vector->tx.count++;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003868
Björn Töpel74608d12017-05-24 07:55:35 +02003869 /* Place XDP Tx ring in the same q_vector ring list as regular Tx */
3870 if (i40e_enabled_xdp_vsi(vsi)) {
3871 struct i40e_ring *xdp_ring = vsi->xdp_rings[qp_idx];
3872
3873 xdp_ring->q_vector = q_vector;
3874 xdp_ring->next = q_vector->tx.ring;
3875 q_vector->tx.ring = xdp_ring;
3876 q_vector->tx.count++;
3877 }
3878
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003879 rx_ring->q_vector = q_vector;
3880 rx_ring->next = q_vector->rx.ring;
3881 q_vector->rx.ring = rx_ring;
3882 q_vector->rx.count++;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003883}
3884
3885/**
3886 * i40e_vsi_map_rings_to_vectors - Maps descriptor rings to vectors
3887 * @vsi: the VSI being configured
3888 *
3889 * This function maps descriptor rings to the queue-specific vectors
3890 * we were allotted through the MSI-X enabling code. Ideally, we'd have
3891 * one vector per queue pair, but on a constrained vector budget, we
3892 * group the queue pairs as "efficiently" as possible.
3893 **/
3894static void i40e_vsi_map_rings_to_vectors(struct i40e_vsi *vsi)
3895{
3896 int qp_remaining = vsi->num_queue_pairs;
3897 int q_vectors = vsi->num_q_vectors;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003898 int num_ringpairs;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003899 int v_start = 0;
3900 int qp_idx = 0;
3901
3902 /* If we don't have enough vectors for a 1-to-1 mapping, we'll have to
3903 * group them so there are multiple queues per vector.
Anjali Singhai Jain70114ec2014-06-03 23:50:14 +00003904 * It is also important to go through all the vectors available to be
3905 * sure that if we don't use all the vectors, that the remaining vectors
3906 * are cleared. This is especially important when decreasing the
3907 * number of queues in use.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003908 */
Anjali Singhai Jain70114ec2014-06-03 23:50:14 +00003909 for (; v_start < q_vectors; v_start++) {
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003910 struct i40e_q_vector *q_vector = vsi->q_vectors[v_start];
3911
3912 num_ringpairs = DIV_ROUND_UP(qp_remaining, q_vectors - v_start);
3913
3914 q_vector->num_ringpairs = num_ringpairs;
3915
3916 q_vector->rx.count = 0;
3917 q_vector->tx.count = 0;
3918 q_vector->rx.ring = NULL;
3919 q_vector->tx.ring = NULL;
3920
3921 while (num_ringpairs--) {
Anjali Singhai Jain26cdc442015-07-10 19:36:00 -04003922 i40e_map_vector_to_qp(vsi, v_start, qp_idx);
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003923 qp_idx++;
3924 qp_remaining--;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003925 }
3926 }
3927}
3928
3929/**
3930 * i40e_vsi_request_irq - Request IRQ from the OS
3931 * @vsi: the VSI being configured
3932 * @basename: name for the vector
3933 **/
3934static int i40e_vsi_request_irq(struct i40e_vsi *vsi, char *basename)
3935{
3936 struct i40e_pf *pf = vsi->back;
3937 int err;
3938
3939 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
3940 err = i40e_vsi_request_irq_msix(vsi, basename);
3941 else if (pf->flags & I40E_FLAG_MSI_ENABLED)
3942 err = request_irq(pf->pdev->irq, i40e_intr, 0,
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00003943 pf->int_name, pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003944 else
3945 err = request_irq(pf->pdev->irq, i40e_intr, IRQF_SHARED,
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00003946 pf->int_name, pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003947
3948 if (err)
3949 dev_info(&pf->pdev->dev, "request_irq failed, Error %d\n", err);
3950
3951 return err;
3952}
3953
3954#ifdef CONFIG_NET_POLL_CONTROLLER
3955/**
Jesse Brandeburgd89d9672016-01-04 10:33:02 -08003956 * i40e_netpoll - A Polling 'interrupt' handler
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003957 * @netdev: network interface device structure
3958 *
3959 * This is used by netconsole to send skbs without having to re-enable
3960 * interrupts. It's not called while the normal interrupt routine is executing.
3961 **/
3962static void i40e_netpoll(struct net_device *netdev)
3963{
3964 struct i40e_netdev_priv *np = netdev_priv(netdev);
3965 struct i40e_vsi *vsi = np->vsi;
3966 struct i40e_pf *pf = vsi->back;
3967 int i;
3968
3969 /* if interface is down do nothing */
Jacob Keller0da36b92017-04-19 09:25:55 -04003970 if (test_bit(__I40E_VSI_DOWN, vsi->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003971 return;
3972
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003973 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3974 for (i = 0; i < vsi->num_q_vectors; i++)
Alexander Duyck493fb302013-09-28 07:01:44 +00003975 i40e_msix_clean_rings(0, vsi->q_vectors[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003976 } else {
3977 i40e_intr(pf->pdev->irq, netdev);
3978 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003979}
3980#endif
3981
Jacob Kellerc768e492017-04-13 04:45:47 -04003982#define I40E_QTX_ENA_WAIT_COUNT 50
3983
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003984/**
Neerav Parikh23527302014-06-03 23:50:15 +00003985 * i40e_pf_txq_wait - Wait for a PF's Tx queue to be enabled or disabled
3986 * @pf: the PF being configured
3987 * @pf_q: the PF queue
3988 * @enable: enable or disable state of the queue
3989 *
3990 * This routine will wait for the given Tx queue of the PF to reach the
3991 * enabled or disabled state.
3992 * Returns -ETIMEDOUT in case of failing to reach the requested state after
3993 * multiple retries; else will return 0 in case of success.
3994 **/
3995static int i40e_pf_txq_wait(struct i40e_pf *pf, int pf_q, bool enable)
3996{
3997 int i;
3998 u32 tx_reg;
3999
4000 for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
4001 tx_reg = rd32(&pf->hw, I40E_QTX_ENA(pf_q));
4002 if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
4003 break;
4004
Neerav Parikhf98a2002014-09-13 07:40:44 +00004005 usleep_range(10, 20);
Neerav Parikh23527302014-06-03 23:50:15 +00004006 }
4007 if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
4008 return -ETIMEDOUT;
4009
4010 return 0;
4011}
4012
4013/**
Jacob Kellerc768e492017-04-13 04:45:47 -04004014 * i40e_control_tx_q - Start or stop a particular Tx queue
4015 * @pf: the PF structure
4016 * @pf_q: the PF queue to configure
4017 * @enable: start or stop the queue
4018 *
4019 * This function enables or disables a single queue. Note that any delay
4020 * required after the operation is expected to be handled by the caller of
4021 * this function.
4022 **/
4023static void i40e_control_tx_q(struct i40e_pf *pf, int pf_q, bool enable)
4024{
4025 struct i40e_hw *hw = &pf->hw;
4026 u32 tx_reg;
4027 int i;
4028
4029 /* warn the TX unit of coming changes */
4030 i40e_pre_tx_queue_cfg(&pf->hw, pf_q, enable);
4031 if (!enable)
4032 usleep_range(10, 20);
4033
4034 for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
4035 tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
4036 if (((tx_reg >> I40E_QTX_ENA_QENA_REQ_SHIFT) & 1) ==
4037 ((tx_reg >> I40E_QTX_ENA_QENA_STAT_SHIFT) & 1))
4038 break;
4039 usleep_range(1000, 2000);
4040 }
4041
4042 /* Skip if the queue is already in the requested state */
4043 if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
4044 return;
4045
4046 /* turn on/off the queue */
4047 if (enable) {
4048 wr32(hw, I40E_QTX_HEAD(pf_q), 0);
4049 tx_reg |= I40E_QTX_ENA_QENA_REQ_MASK;
4050 } else {
4051 tx_reg &= ~I40E_QTX_ENA_QENA_REQ_MASK;
4052 }
4053
4054 wr32(hw, I40E_QTX_ENA(pf_q), tx_reg);
4055}
4056
4057/**
Björn Töpel74608d12017-05-24 07:55:35 +02004058 * i40e_control_wait_tx_q - Start/stop Tx queue and wait for completion
4059 * @seid: VSI SEID
4060 * @pf: the PF structure
4061 * @pf_q: the PF queue to configure
4062 * @is_xdp: true if the queue is used for XDP
4063 * @enable: start or stop the queue
4064 **/
4065static int i40e_control_wait_tx_q(int seid, struct i40e_pf *pf, int pf_q,
4066 bool is_xdp, bool enable)
4067{
4068 int ret;
4069
4070 i40e_control_tx_q(pf, pf_q, enable);
4071
4072 /* wait for the change to finish */
4073 ret = i40e_pf_txq_wait(pf, pf_q, enable);
4074 if (ret) {
4075 dev_info(&pf->pdev->dev,
4076 "VSI seid %d %sTx ring %d %sable timeout\n",
4077 seid, (is_xdp ? "XDP " : ""), pf_q,
4078 (enable ? "en" : "dis"));
4079 }
4080
4081 return ret;
4082}
4083
4084/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004085 * i40e_vsi_control_tx - Start or stop a VSI's rings
4086 * @vsi: the VSI being configured
4087 * @enable: start or stop the rings
4088 **/
4089static int i40e_vsi_control_tx(struct i40e_vsi *vsi, bool enable)
4090{
4091 struct i40e_pf *pf = vsi->back;
Jacob Kellerc768e492017-04-13 04:45:47 -04004092 int i, pf_q, ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004093
4094 pf_q = vsi->base_queue;
4095 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
Björn Töpel74608d12017-05-24 07:55:35 +02004096 ret = i40e_control_wait_tx_q(vsi->seid, pf,
4097 pf_q,
4098 false /*is xdp*/, enable);
4099 if (ret)
Neerav Parikh23527302014-06-03 23:50:15 +00004100 break;
Björn Töpel74608d12017-05-24 07:55:35 +02004101
4102 if (!i40e_enabled_xdp_vsi(vsi))
4103 continue;
4104
4105 ret = i40e_control_wait_tx_q(vsi->seid, pf,
4106 pf_q + vsi->alloc_queue_pairs,
4107 true /*is xdp*/, enable);
4108 if (ret)
4109 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004110 }
4111
Neerav Parikh23527302014-06-03 23:50:15 +00004112 return ret;
4113}
4114
4115/**
4116 * i40e_pf_rxq_wait - Wait for a PF's Rx queue to be enabled or disabled
4117 * @pf: the PF being configured
4118 * @pf_q: the PF queue
4119 * @enable: enable or disable state of the queue
4120 *
4121 * This routine will wait for the given Rx queue of the PF to reach the
4122 * enabled or disabled state.
4123 * Returns -ETIMEDOUT in case of failing to reach the requested state after
4124 * multiple retries; else will return 0 in case of success.
4125 **/
4126static int i40e_pf_rxq_wait(struct i40e_pf *pf, int pf_q, bool enable)
4127{
4128 int i;
4129 u32 rx_reg;
4130
4131 for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
4132 rx_reg = rd32(&pf->hw, I40E_QRX_ENA(pf_q));
4133 if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4134 break;
4135
Neerav Parikhf98a2002014-09-13 07:40:44 +00004136 usleep_range(10, 20);
Neerav Parikh23527302014-06-03 23:50:15 +00004137 }
4138 if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
4139 return -ETIMEDOUT;
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00004140
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004141 return 0;
4142}
4143
4144/**
Jacob Kellerc768e492017-04-13 04:45:47 -04004145 * i40e_control_rx_q - Start or stop a particular Rx queue
4146 * @pf: the PF structure
4147 * @pf_q: the PF queue to configure
4148 * @enable: start or stop the queue
4149 *
4150 * This function enables or disables a single queue. Note that any delay
4151 * required after the operation is expected to be handled by the caller of
4152 * this function.
4153 **/
4154static void i40e_control_rx_q(struct i40e_pf *pf, int pf_q, bool enable)
4155{
4156 struct i40e_hw *hw = &pf->hw;
4157 u32 rx_reg;
4158 int i;
4159
4160 for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
4161 rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
4162 if (((rx_reg >> I40E_QRX_ENA_QENA_REQ_SHIFT) & 1) ==
4163 ((rx_reg >> I40E_QRX_ENA_QENA_STAT_SHIFT) & 1))
4164 break;
4165 usleep_range(1000, 2000);
4166 }
4167
4168 /* Skip if the queue is already in the requested state */
4169 if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4170 return;
4171
4172 /* turn on/off the queue */
4173 if (enable)
4174 rx_reg |= I40E_QRX_ENA_QENA_REQ_MASK;
4175 else
4176 rx_reg &= ~I40E_QRX_ENA_QENA_REQ_MASK;
4177
4178 wr32(hw, I40E_QRX_ENA(pf_q), rx_reg);
4179}
4180
4181/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004182 * i40e_vsi_control_rx - Start or stop a VSI's rings
4183 * @vsi: the VSI being configured
4184 * @enable: start or stop the rings
4185 **/
4186static int i40e_vsi_control_rx(struct i40e_vsi *vsi, bool enable)
4187{
4188 struct i40e_pf *pf = vsi->back;
Jacob Kellerc768e492017-04-13 04:45:47 -04004189 int i, pf_q, ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004190
4191 pf_q = vsi->base_queue;
4192 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
Jacob Kellerc768e492017-04-13 04:45:47 -04004193 i40e_control_rx_q(pf, pf_q, enable);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004194
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004195 /* wait for the change to finish */
Neerav Parikh23527302014-06-03 23:50:15 +00004196 ret = i40e_pf_rxq_wait(pf, pf_q, enable);
4197 if (ret) {
4198 dev_info(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04004199 "VSI seid %d Rx ring %d %sable timeout\n",
4200 vsi->seid, pf_q, (enable ? "en" : "dis"));
Neerav Parikh23527302014-06-03 23:50:15 +00004201 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004202 }
4203 }
4204
Wyborny, Carolynd08a9f62017-03-28 08:00:48 -07004205 /* Due to HW errata, on Rx disable only, the register can indicate done
4206 * before it really is. Needs 50ms to be sure
4207 */
4208 if (!enable)
4209 mdelay(50);
4210
Neerav Parikh23527302014-06-03 23:50:15 +00004211 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004212}
4213
4214/**
Filip Sadowski3aa7b742016-10-11 15:26:58 -07004215 * i40e_vsi_start_rings - Start a VSI's rings
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004216 * @vsi: the VSI being configured
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004217 **/
Filip Sadowski3aa7b742016-10-11 15:26:58 -07004218int i40e_vsi_start_rings(struct i40e_vsi *vsi)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004219{
Anjali Singhai Jain3b867b22013-12-21 05:44:44 +00004220 int ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004221
4222 /* do rx first for enable and last for disable */
Filip Sadowski3aa7b742016-10-11 15:26:58 -07004223 ret = i40e_vsi_control_rx(vsi, true);
4224 if (ret)
4225 return ret;
4226 ret = i40e_vsi_control_tx(vsi, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004227
4228 return ret;
4229}
4230
4231/**
Filip Sadowski3aa7b742016-10-11 15:26:58 -07004232 * i40e_vsi_stop_rings - Stop a VSI's rings
4233 * @vsi: the VSI being configured
4234 **/
4235void i40e_vsi_stop_rings(struct i40e_vsi *vsi)
4236{
Jacob Keller34807562017-04-13 04:45:53 -04004237 /* When port TX is suspended, don't wait */
Jacob Keller0da36b92017-04-19 09:25:55 -04004238 if (test_bit(__I40E_PORT_SUSPENDED, vsi->back->state))
Jacob Keller34807562017-04-13 04:45:53 -04004239 return i40e_vsi_stop_rings_no_wait(vsi);
4240
Filip Sadowski3aa7b742016-10-11 15:26:58 -07004241 /* do rx first for enable and last for disable
4242 * Ignore return value, we need to shutdown whatever we can
4243 */
4244 i40e_vsi_control_tx(vsi, false);
4245 i40e_vsi_control_rx(vsi, false);
4246}
4247
4248/**
Jacob Kellere4b433f2017-04-13 04:45:52 -04004249 * i40e_vsi_stop_rings_no_wait - Stop a VSI's rings and do not delay
4250 * @vsi: the VSI being shutdown
4251 *
4252 * This function stops all the rings for a VSI but does not delay to verify
4253 * that rings have been disabled. It is expected that the caller is shutting
4254 * down multiple VSIs at once and will delay together for all the VSIs after
4255 * initiating the shutdown. This is particularly useful for shutting down lots
4256 * of VFs together. Otherwise, a large delay can be incurred while configuring
4257 * each VSI in serial.
4258 **/
4259void i40e_vsi_stop_rings_no_wait(struct i40e_vsi *vsi)
4260{
4261 struct i40e_pf *pf = vsi->back;
4262 int i, pf_q;
4263
4264 pf_q = vsi->base_queue;
4265 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4266 i40e_control_tx_q(pf, pf_q, false);
4267 i40e_control_rx_q(pf, pf_q, false);
4268 }
4269}
4270
4271/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004272 * i40e_vsi_free_irq - Free the irq association with the OS
4273 * @vsi: the VSI being configured
4274 **/
4275static void i40e_vsi_free_irq(struct i40e_vsi *vsi)
4276{
4277 struct i40e_pf *pf = vsi->back;
4278 struct i40e_hw *hw = &pf->hw;
4279 int base = vsi->base_vector;
4280 u32 val, qp;
4281 int i;
4282
4283 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4284 if (!vsi->q_vectors)
4285 return;
4286
Shannon Nelson63741842014-04-23 04:50:16 +00004287 if (!vsi->irqs_ready)
4288 return;
4289
4290 vsi->irqs_ready = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004291 for (i = 0; i < vsi->num_q_vectors; i++) {
Alan Brady96db7762016-09-14 16:24:38 -07004292 int irq_num;
4293 u16 vector;
4294
4295 vector = i + base;
4296 irq_num = pf->msix_entries[vector].vector;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004297
4298 /* free only the irqs that were actually requested */
Shannon Nelson78681b12013-11-28 06:39:36 +00004299 if (!vsi->q_vectors[i] ||
4300 !vsi->q_vectors[i]->num_ringpairs)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004301 continue;
4302
Alan Brady96db7762016-09-14 16:24:38 -07004303 /* clear the affinity notifier in the IRQ descriptor */
4304 irq_set_affinity_notifier(irq_num, NULL);
Jacob Keller759dc4a2017-07-14 09:10:10 -04004305 /* remove our suggested affinity mask for this IRQ */
Alan Brady96db7762016-09-14 16:24:38 -07004306 irq_set_affinity_hint(irq_num, NULL);
4307 synchronize_irq(irq_num);
4308 free_irq(irq_num, vsi->q_vectors[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004309
4310 /* Tear down the interrupt queue link list
4311 *
4312 * We know that they come in pairs and always
4313 * the Rx first, then the Tx. To clear the
4314 * link list, stick the EOL value into the
4315 * next_q field of the registers.
4316 */
4317 val = rd32(hw, I40E_PFINT_LNKLSTN(vector - 1));
4318 qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
4319 >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4320 val |= I40E_QUEUE_END_OF_LIST
4321 << I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4322 wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), val);
4323
4324 while (qp != I40E_QUEUE_END_OF_LIST) {
4325 u32 next;
4326
4327 val = rd32(hw, I40E_QINT_RQCTL(qp));
4328
4329 val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
4330 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
4331 I40E_QINT_RQCTL_CAUSE_ENA_MASK |
4332 I40E_QINT_RQCTL_INTEVENT_MASK);
4333
4334 val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
4335 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
4336
4337 wr32(hw, I40E_QINT_RQCTL(qp), val);
4338
4339 val = rd32(hw, I40E_QINT_TQCTL(qp));
4340
4341 next = (val & I40E_QINT_TQCTL_NEXTQ_INDX_MASK)
4342 >> I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT;
4343
4344 val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
4345 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
4346 I40E_QINT_TQCTL_CAUSE_ENA_MASK |
4347 I40E_QINT_TQCTL_INTEVENT_MASK);
4348
4349 val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
4350 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
4351
4352 wr32(hw, I40E_QINT_TQCTL(qp), val);
4353 qp = next;
4354 }
4355 }
4356 } else {
4357 free_irq(pf->pdev->irq, pf);
4358
4359 val = rd32(hw, I40E_PFINT_LNKLST0);
4360 qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
4361 >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4362 val |= I40E_QUEUE_END_OF_LIST
4363 << I40E_PFINT_LNKLST0_FIRSTQ_INDX_SHIFT;
4364 wr32(hw, I40E_PFINT_LNKLST0, val);
4365
4366 val = rd32(hw, I40E_QINT_RQCTL(qp));
4367 val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
4368 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
4369 I40E_QINT_RQCTL_CAUSE_ENA_MASK |
4370 I40E_QINT_RQCTL_INTEVENT_MASK);
4371
4372 val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
4373 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
4374
4375 wr32(hw, I40E_QINT_RQCTL(qp), val);
4376
4377 val = rd32(hw, I40E_QINT_TQCTL(qp));
4378
4379 val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
4380 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
4381 I40E_QINT_TQCTL_CAUSE_ENA_MASK |
4382 I40E_QINT_TQCTL_INTEVENT_MASK);
4383
4384 val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
4385 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
4386
4387 wr32(hw, I40E_QINT_TQCTL(qp), val);
4388 }
4389}
4390
4391/**
Alexander Duyck493fb302013-09-28 07:01:44 +00004392 * i40e_free_q_vector - Free memory allocated for specific interrupt vector
4393 * @vsi: the VSI being configured
4394 * @v_idx: Index of vector to be freed
4395 *
4396 * This function frees the memory allocated to the q_vector. In addition if
4397 * NAPI is enabled it will delete any references to the NAPI struct prior
4398 * to freeing the q_vector.
4399 **/
4400static void i40e_free_q_vector(struct i40e_vsi *vsi, int v_idx)
4401{
4402 struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00004403 struct i40e_ring *ring;
Alexander Duyck493fb302013-09-28 07:01:44 +00004404
4405 if (!q_vector)
4406 return;
4407
4408 /* disassociate q_vector from rings */
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00004409 i40e_for_each_ring(ring, q_vector->tx)
4410 ring->q_vector = NULL;
4411
4412 i40e_for_each_ring(ring, q_vector->rx)
4413 ring->q_vector = NULL;
Alexander Duyck493fb302013-09-28 07:01:44 +00004414
4415 /* only VSI w/ an associated netdev is set up w/ NAPI */
4416 if (vsi->netdev)
4417 netif_napi_del(&q_vector->napi);
4418
4419 vsi->q_vectors[v_idx] = NULL;
4420
4421 kfree_rcu(q_vector, rcu);
4422}
4423
4424/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004425 * i40e_vsi_free_q_vectors - Free memory allocated for interrupt vectors
4426 * @vsi: the VSI being un-configured
4427 *
4428 * This frees the memory allocated to the q_vectors and
4429 * deletes references to the NAPI struct.
4430 **/
4431static void i40e_vsi_free_q_vectors(struct i40e_vsi *vsi)
4432{
4433 int v_idx;
4434
Alexander Duyck493fb302013-09-28 07:01:44 +00004435 for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++)
4436 i40e_free_q_vector(vsi, v_idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004437}
4438
4439/**
4440 * i40e_reset_interrupt_capability - Disable interrupt setup in OS
4441 * @pf: board private structure
4442 **/
4443static void i40e_reset_interrupt_capability(struct i40e_pf *pf)
4444{
4445 /* If we're in Legacy mode, the interrupt was cleaned in vsi_close */
4446 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4447 pci_disable_msix(pf->pdev);
4448 kfree(pf->msix_entries);
4449 pf->msix_entries = NULL;
Shannon Nelson3b444392015-02-26 16:15:57 +00004450 kfree(pf->irq_pile);
4451 pf->irq_pile = NULL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004452 } else if (pf->flags & I40E_FLAG_MSI_ENABLED) {
4453 pci_disable_msi(pf->pdev);
4454 }
4455 pf->flags &= ~(I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
4456}
4457
4458/**
4459 * i40e_clear_interrupt_scheme - Clear the current interrupt scheme settings
4460 * @pf: board private structure
4461 *
4462 * We go through and clear interrupt specific resources and reset the structure
4463 * to pre-load conditions
4464 **/
4465static void i40e_clear_interrupt_scheme(struct i40e_pf *pf)
4466{
4467 int i;
4468
Shannon Nelsone1477582015-02-21 06:44:33 +00004469 i40e_stop_misc_vector(pf);
Shannon Nelson69278392016-03-10 14:59:43 -08004470 if (pf->flags & I40E_FLAG_MSIX_ENABLED && pf->msix_entries) {
Shannon Nelsone1477582015-02-21 06:44:33 +00004471 synchronize_irq(pf->msix_entries[0].vector);
4472 free_irq(pf->msix_entries[0].vector, pf);
4473 }
4474
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06004475 i40e_put_lump(pf->irq_pile, pf->iwarp_base_vector,
4476 I40E_IWARP_IRQ_PILE_ID);
4477
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004478 i40e_put_lump(pf->irq_pile, 0, I40E_PILE_VALID_BIT-1);
Mitch Williams505682c2014-05-20 08:01:37 +00004479 for (i = 0; i < pf->num_alloc_vsi; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004480 if (pf->vsi[i])
4481 i40e_vsi_free_q_vectors(pf->vsi[i]);
4482 i40e_reset_interrupt_capability(pf);
4483}
4484
4485/**
4486 * i40e_napi_enable_all - Enable NAPI for all q_vectors in the VSI
4487 * @vsi: the VSI being configured
4488 **/
4489static void i40e_napi_enable_all(struct i40e_vsi *vsi)
4490{
4491 int q_idx;
4492
4493 if (!vsi->netdev)
4494 return;
4495
Alexander Duyck13a8cd12017-03-24 15:01:42 -07004496 for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
4497 struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
4498
4499 if (q_vector->rx.ring || q_vector->tx.ring)
4500 napi_enable(&q_vector->napi);
4501 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004502}
4503
4504/**
4505 * i40e_napi_disable_all - Disable NAPI for all q_vectors in the VSI
4506 * @vsi: the VSI being configured
4507 **/
4508static void i40e_napi_disable_all(struct i40e_vsi *vsi)
4509{
4510 int q_idx;
4511
4512 if (!vsi->netdev)
4513 return;
4514
Alexander Duyck13a8cd12017-03-24 15:01:42 -07004515 for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
4516 struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
4517
4518 if (q_vector->rx.ring || q_vector->tx.ring)
4519 napi_disable(&q_vector->napi);
4520 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004521}
4522
4523/**
Shannon Nelson90ef8d42014-03-14 07:32:26 +00004524 * i40e_vsi_close - Shut down a VSI
4525 * @vsi: the vsi to be quelled
4526 **/
4527static void i40e_vsi_close(struct i40e_vsi *vsi)
4528{
Mitch Williams0ef2d5a2017-01-24 10:24:00 -08004529 struct i40e_pf *pf = vsi->back;
Jacob Keller0da36b92017-04-19 09:25:55 -04004530 if (!test_and_set_bit(__I40E_VSI_DOWN, vsi->state))
Shannon Nelson90ef8d42014-03-14 07:32:26 +00004531 i40e_down(vsi);
4532 i40e_vsi_free_irq(vsi);
4533 i40e_vsi_free_tx_resources(vsi);
4534 i40e_vsi_free_rx_resources(vsi);
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04004535 vsi->current_netdev_flags = 0;
Mitch Williams0ef2d5a2017-01-24 10:24:00 -08004536 pf->flags |= I40E_FLAG_SERVICE_CLIENT_REQUESTED;
Jacob Keller0da36b92017-04-19 09:25:55 -04004537 if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
Mitch Williams0ef2d5a2017-01-24 10:24:00 -08004538 pf->flags |= I40E_FLAG_CLIENT_RESET;
Shannon Nelson90ef8d42014-03-14 07:32:26 +00004539}
4540
4541/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004542 * i40e_quiesce_vsi - Pause a given VSI
4543 * @vsi: the VSI being paused
4544 **/
4545static void i40e_quiesce_vsi(struct i40e_vsi *vsi)
4546{
Jacob Keller0da36b92017-04-19 09:25:55 -04004547 if (test_bit(__I40E_VSI_DOWN, vsi->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004548 return;
4549
Jacob Keller0da36b92017-04-19 09:25:55 -04004550 set_bit(__I40E_VSI_NEEDS_RESTART, vsi->state);
Jesse Brandeburg6995b362015-08-28 17:55:54 -04004551 if (vsi->netdev && netif_running(vsi->netdev))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004552 vsi->netdev->netdev_ops->ndo_stop(vsi->netdev);
Jesse Brandeburg6995b362015-08-28 17:55:54 -04004553 else
Shannon Nelson90ef8d42014-03-14 07:32:26 +00004554 i40e_vsi_close(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004555}
4556
4557/**
4558 * i40e_unquiesce_vsi - Resume a given VSI
4559 * @vsi: the VSI being resumed
4560 **/
4561static void i40e_unquiesce_vsi(struct i40e_vsi *vsi)
4562{
Jacob Keller0da36b92017-04-19 09:25:55 -04004563 if (!test_and_clear_bit(__I40E_VSI_NEEDS_RESTART, vsi->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004564 return;
4565
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004566 if (vsi->netdev && netif_running(vsi->netdev))
4567 vsi->netdev->netdev_ops->ndo_open(vsi->netdev);
4568 else
Shannon Nelson8276f752014-03-14 07:32:27 +00004569 i40e_vsi_open(vsi); /* this clears the DOWN bit */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004570}
4571
4572/**
4573 * i40e_pf_quiesce_all_vsi - Pause all VSIs on a PF
4574 * @pf: the PF
4575 **/
4576static void i40e_pf_quiesce_all_vsi(struct i40e_pf *pf)
4577{
4578 int v;
4579
Mitch Williams505682c2014-05-20 08:01:37 +00004580 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004581 if (pf->vsi[v])
4582 i40e_quiesce_vsi(pf->vsi[v]);
4583 }
4584}
4585
4586/**
4587 * i40e_pf_unquiesce_all_vsi - Resume all VSIs on a PF
4588 * @pf: the PF
4589 **/
4590static void i40e_pf_unquiesce_all_vsi(struct i40e_pf *pf)
4591{
4592 int v;
4593
Mitch Williams505682c2014-05-20 08:01:37 +00004594 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004595 if (pf->vsi[v])
4596 i40e_unquiesce_vsi(pf->vsi[v]);
4597 }
4598}
4599
Neerav Parikh69129dc2014-11-12 00:18:46 +00004600/**
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004601 * i40e_vsi_wait_queues_disabled - Wait for VSI's queues to be disabled
Neerav Parikh69129dc2014-11-12 00:18:46 +00004602 * @vsi: the VSI being configured
4603 *
Jacob Kelleraf26ce22017-04-05 07:50:58 -04004604 * Wait until all queues on a given VSI have been disabled.
Neerav Parikh69129dc2014-11-12 00:18:46 +00004605 **/
Jacob Kellere4b433f2017-04-13 04:45:52 -04004606int i40e_vsi_wait_queues_disabled(struct i40e_vsi *vsi)
Neerav Parikh69129dc2014-11-12 00:18:46 +00004607{
4608 struct i40e_pf *pf = vsi->back;
4609 int i, pf_q, ret;
4610
4611 pf_q = vsi->base_queue;
4612 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
Jacob Kelleraf26ce22017-04-05 07:50:58 -04004613 /* Check and wait for the Tx queue */
Neerav Parikh69129dc2014-11-12 00:18:46 +00004614 ret = i40e_pf_txq_wait(pf, pf_q, false);
4615 if (ret) {
4616 dev_info(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04004617 "VSI seid %d Tx ring %d disable timeout\n",
4618 vsi->seid, pf_q);
Neerav Parikh69129dc2014-11-12 00:18:46 +00004619 return ret;
4620 }
Björn Töpel74608d12017-05-24 07:55:35 +02004621
4622 if (!i40e_enabled_xdp_vsi(vsi))
4623 goto wait_rx;
4624
4625 /* Check and wait for the XDP Tx queue */
4626 ret = i40e_pf_txq_wait(pf, pf_q + vsi->alloc_queue_pairs,
4627 false);
4628 if (ret) {
4629 dev_info(&pf->pdev->dev,
4630 "VSI seid %d XDP Tx ring %d disable timeout\n",
4631 vsi->seid, pf_q);
4632 return ret;
4633 }
4634wait_rx:
4635 /* Check and wait for the Rx queue */
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004636 ret = i40e_pf_rxq_wait(pf, pf_q, false);
4637 if (ret) {
4638 dev_info(&pf->pdev->dev,
4639 "VSI seid %d Rx ring %d disable timeout\n",
4640 vsi->seid, pf_q);
4641 return ret;
4642 }
4643 }
4644
Neerav Parikh69129dc2014-11-12 00:18:46 +00004645 return 0;
4646}
4647
Jacob Kellere4b433f2017-04-13 04:45:52 -04004648#ifdef CONFIG_I40E_DCB
Neerav Parikh69129dc2014-11-12 00:18:46 +00004649/**
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004650 * i40e_pf_wait_queues_disabled - Wait for all queues of PF VSIs to be disabled
Neerav Parikh69129dc2014-11-12 00:18:46 +00004651 * @pf: the PF
4652 *
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004653 * This function waits for the queues to be in disabled state for all the
Neerav Parikh69129dc2014-11-12 00:18:46 +00004654 * VSIs that are managed by this PF.
4655 **/
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004656static int i40e_pf_wait_queues_disabled(struct i40e_pf *pf)
Neerav Parikh69129dc2014-11-12 00:18:46 +00004657{
4658 int v, ret = 0;
4659
4660 for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
Alexander Duyckc76cb6e2017-02-21 15:55:48 -08004661 if (pf->vsi[v]) {
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004662 ret = i40e_vsi_wait_queues_disabled(pf->vsi[v]);
Neerav Parikh69129dc2014-11-12 00:18:46 +00004663 if (ret)
4664 break;
4665 }
4666 }
4667
4668 return ret;
4669}
4670
4671#endif
Kiran Patilb03a8c12015-09-24 18:13:15 -04004672
4673/**
4674 * i40e_detect_recover_hung_queue - Function to detect and recover hung_queue
4675 * @q_idx: TX queue number
4676 * @vsi: Pointer to VSI struct
4677 *
4678 * This function checks specified queue for given VSI. Detects hung condition.
Alan Brady17daabb2017-04-05 07:50:56 -04004679 * We proactively detect hung TX queues by checking if interrupts are disabled
4680 * but there are pending descriptors. If it appears hung, attempt to recover
4681 * by triggering a SW interrupt.
Kiran Patilb03a8c12015-09-24 18:13:15 -04004682 **/
4683static void i40e_detect_recover_hung_queue(int q_idx, struct i40e_vsi *vsi)
4684{
4685 struct i40e_ring *tx_ring = NULL;
4686 struct i40e_pf *pf;
Alan Brady17daabb2017-04-05 07:50:56 -04004687 u32 val, tx_pending;
Kiran Patilb03a8c12015-09-24 18:13:15 -04004688 int i;
4689
4690 pf = vsi->back;
4691
4692 /* now that we have an index, find the tx_ring struct */
4693 for (i = 0; i < vsi->num_queue_pairs; i++) {
4694 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc) {
4695 if (q_idx == vsi->tx_rings[i]->queue_index) {
4696 tx_ring = vsi->tx_rings[i];
4697 break;
4698 }
4699 }
4700 }
4701
4702 if (!tx_ring)
4703 return;
4704
4705 /* Read interrupt register */
4706 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
4707 val = rd32(&pf->hw,
4708 I40E_PFINT_DYN_CTLN(tx_ring->q_vector->v_idx +
4709 tx_ring->vsi->base_vector - 1));
4710 else
4711 val = rd32(&pf->hw, I40E_PFINT_DYN_CTL0);
4712
Alan Brady17daabb2017-04-05 07:50:56 -04004713 tx_pending = i40e_get_tx_pending(tx_ring);
Kiran Patilb03a8c12015-09-24 18:13:15 -04004714
Alan Brady17daabb2017-04-05 07:50:56 -04004715 /* Interrupts are disabled and TX pending is non-zero,
4716 * trigger the SW interrupt (don't wait). Worst case
4717 * there will be one extra interrupt which may result
4718 * into not cleaning any queues because queues are cleaned.
Kiran Patilb03a8c12015-09-24 18:13:15 -04004719 */
Alan Brady17daabb2017-04-05 07:50:56 -04004720 if (tx_pending && (!(val & I40E_PFINT_DYN_CTLN_INTENA_MASK)))
4721 i40e_force_wb(vsi, tx_ring->q_vector);
Kiran Patilb03a8c12015-09-24 18:13:15 -04004722}
4723
4724/**
4725 * i40e_detect_recover_hung - Function to detect and recover hung_queues
4726 * @pf: pointer to PF struct
4727 *
4728 * LAN VSI has netdev and netdev has TX queues. This function is to check
4729 * each of those TX queues if they are hung, trigger recovery by issuing
4730 * SW interrupt.
4731 **/
4732static void i40e_detect_recover_hung(struct i40e_pf *pf)
4733{
4734 struct net_device *netdev;
4735 struct i40e_vsi *vsi;
Jesse Brandeburgb85c94b2017-06-20 15:16:59 -07004736 unsigned int i;
Kiran Patilb03a8c12015-09-24 18:13:15 -04004737
4738 /* Only for LAN VSI */
4739 vsi = pf->vsi[pf->lan_vsi];
4740
4741 if (!vsi)
4742 return;
4743
4744 /* Make sure, VSI state is not DOWN/RECOVERY_PENDING */
Jacob Keller0da36b92017-04-19 09:25:55 -04004745 if (test_bit(__I40E_VSI_DOWN, vsi->back->state) ||
4746 test_bit(__I40E_RESET_RECOVERY_PENDING, vsi->back->state))
Kiran Patilb03a8c12015-09-24 18:13:15 -04004747 return;
4748
4749 /* Make sure type is MAIN VSI */
4750 if (vsi->type != I40E_VSI_MAIN)
4751 return;
4752
4753 netdev = vsi->netdev;
4754 if (!netdev)
4755 return;
4756
4757 /* Bail out if netif_carrier is not OK */
4758 if (!netif_carrier_ok(netdev))
4759 return;
4760
4761 /* Go thru' TX queues for netdev */
4762 for (i = 0; i < netdev->num_tx_queues; i++) {
4763 struct netdev_queue *q;
4764
4765 q = netdev_get_tx_queue(netdev, i);
4766 if (q)
4767 i40e_detect_recover_hung_queue(i, vsi);
4768 }
4769}
4770
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004771/**
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004772 * i40e_get_iscsi_tc_map - Return TC map for iSCSI APP
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00004773 * @pf: pointer to PF
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004774 *
4775 * Get TC map for ISCSI PF type that will include iSCSI TC
4776 * and LAN TC.
4777 **/
4778static u8 i40e_get_iscsi_tc_map(struct i40e_pf *pf)
4779{
4780 struct i40e_dcb_app_priority_table app;
4781 struct i40e_hw *hw = &pf->hw;
4782 u8 enabled_tc = 1; /* TC0 is always enabled */
4783 u8 tc, i;
4784 /* Get the iSCSI APP TLV */
4785 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
4786
4787 for (i = 0; i < dcbcfg->numapps; i++) {
4788 app = dcbcfg->app[i];
4789 if (app.selector == I40E_APP_SEL_TCPIP &&
4790 app.protocolid == I40E_APP_PROTOID_ISCSI) {
4791 tc = dcbcfg->etscfg.prioritytable[app.priority];
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08004792 enabled_tc |= BIT(tc);
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004793 break;
4794 }
4795 }
4796
4797 return enabled_tc;
4798}
4799
4800/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004801 * i40e_dcb_get_num_tc - Get the number of TCs from DCBx config
4802 * @dcbcfg: the corresponding DCBx configuration structure
4803 *
4804 * Return the number of TCs from given DCBx configuration
4805 **/
4806static u8 i40e_dcb_get_num_tc(struct i40e_dcbx_config *dcbcfg)
4807{
Dave Ertmanfbfe12c2016-08-12 09:56:32 -07004808 int i, tc_unused = 0;
Jesse Brandeburg078b5872013-09-25 23:41:14 +00004809 u8 num_tc = 0;
Dave Ertmanfbfe12c2016-08-12 09:56:32 -07004810 u8 ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004811
4812 /* Scan the ETS Config Priority Table to find
4813 * traffic class enabled for a given priority
Dave Ertmanfbfe12c2016-08-12 09:56:32 -07004814 * and create a bitmask of enabled TCs
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004815 */
Dave Ertmanfbfe12c2016-08-12 09:56:32 -07004816 for (i = 0; i < I40E_MAX_USER_PRIORITY; i++)
4817 num_tc |= BIT(dcbcfg->etscfg.prioritytable[i]);
4818
4819 /* Now scan the bitmask to check for
4820 * contiguous TCs starting with TC0
4821 */
4822 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
4823 if (num_tc & BIT(i)) {
4824 if (!tc_unused) {
4825 ret++;
4826 } else {
4827 pr_err("Non-contiguous TC - Disabling DCB\n");
4828 return 1;
4829 }
4830 } else {
4831 tc_unused = 1;
4832 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004833 }
4834
Dave Ertmanfbfe12c2016-08-12 09:56:32 -07004835 /* There is always at least TC0 */
4836 if (!ret)
4837 ret = 1;
4838
4839 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004840}
4841
4842/**
4843 * i40e_dcb_get_enabled_tc - Get enabled traffic classes
4844 * @dcbcfg: the corresponding DCBx configuration structure
4845 *
4846 * Query the current DCB configuration and return the number of
4847 * traffic classes enabled from the given DCBX config
4848 **/
4849static u8 i40e_dcb_get_enabled_tc(struct i40e_dcbx_config *dcbcfg)
4850{
4851 u8 num_tc = i40e_dcb_get_num_tc(dcbcfg);
4852 u8 enabled_tc = 1;
4853 u8 i;
4854
4855 for (i = 0; i < num_tc; i++)
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04004856 enabled_tc |= BIT(i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004857
4858 return enabled_tc;
4859}
4860
4861/**
4862 * i40e_pf_get_num_tc - Get enabled traffic classes for PF
4863 * @pf: PF being queried
4864 *
4865 * Return number of traffic classes enabled for the given PF
4866 **/
4867static u8 i40e_pf_get_num_tc(struct i40e_pf *pf)
4868{
4869 struct i40e_hw *hw = &pf->hw;
Dave Ertman52a08ca2016-07-27 12:02:34 -07004870 u8 i, enabled_tc = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004871 u8 num_tc = 0;
4872 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
4873
4874 /* If DCB is not enabled then always in single TC */
4875 if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
4876 return 1;
4877
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004878 /* SFP mode will be enabled for all TCs on port */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004879 if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
4880 return i40e_dcb_get_num_tc(dcbcfg);
4881
4882 /* MFP mode return count of enabled TCs for this PF */
4883 if (pf->hw.func_caps.iscsi)
4884 enabled_tc = i40e_get_iscsi_tc_map(pf);
4885 else
Neerav Parikhfc51de92015-02-24 06:58:53 +00004886 return 1; /* Only TC0 */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004887
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004888 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08004889 if (enabled_tc & BIT(i))
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004890 num_tc++;
4891 }
4892 return num_tc;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004893}
4894
4895/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004896 * i40e_pf_get_pf_tc_map - Get bitmap for enabled traffic classes
4897 * @pf: PF being queried
4898 *
4899 * Return a bitmap for enabled traffic classes for this PF.
4900 **/
4901static u8 i40e_pf_get_tc_map(struct i40e_pf *pf)
4902{
4903 /* If DCB is not enabled for this PF then just return default TC */
4904 if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
David Ertmanea6acb72016-09-20 07:10:50 -07004905 return I40E_DEFAULT_TRAFFIC_CLASS;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004906
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004907 /* SFP mode we want PF to be enabled for all TCs */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004908 if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
4909 return i40e_dcb_get_enabled_tc(&pf->hw.local_dcbx_config);
4910
Neerav Parikhfc51de92015-02-24 06:58:53 +00004911 /* MFP enabled and iSCSI PF type */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004912 if (pf->hw.func_caps.iscsi)
4913 return i40e_get_iscsi_tc_map(pf);
4914 else
David Ertmanea6acb72016-09-20 07:10:50 -07004915 return I40E_DEFAULT_TRAFFIC_CLASS;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004916}
4917
4918/**
4919 * i40e_vsi_get_bw_info - Query VSI BW Information
4920 * @vsi: the VSI being queried
4921 *
4922 * Returns 0 on success, negative value on failure
4923 **/
4924static int i40e_vsi_get_bw_info(struct i40e_vsi *vsi)
4925{
4926 struct i40e_aqc_query_vsi_ets_sla_config_resp bw_ets_config = {0};
4927 struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
4928 struct i40e_pf *pf = vsi->back;
4929 struct i40e_hw *hw = &pf->hw;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004930 i40e_status ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004931 u32 tc_bw_max;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004932 int i;
4933
4934 /* Get the VSI level BW configuration */
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004935 ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid, &bw_config, NULL);
4936 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004937 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004938 "couldn't get PF vsi bw config, err %s aq_err %s\n",
4939 i40e_stat_str(&pf->hw, ret),
4940 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004941 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004942 }
4943
4944 /* Get the VSI level BW configuration per TC */
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004945 ret = i40e_aq_query_vsi_ets_sla_config(hw, vsi->seid, &bw_ets_config,
4946 NULL);
4947 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004948 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004949 "couldn't get PF vsi ets bw config, err %s aq_err %s\n",
4950 i40e_stat_str(&pf->hw, ret),
4951 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004952 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004953 }
4954
4955 if (bw_config.tc_valid_bits != bw_ets_config.tc_valid_bits) {
4956 dev_info(&pf->pdev->dev,
4957 "Enabled TCs mismatch from querying VSI BW info 0x%08x 0x%08x\n",
4958 bw_config.tc_valid_bits,
4959 bw_ets_config.tc_valid_bits);
4960 /* Still continuing */
4961 }
4962
4963 vsi->bw_limit = le16_to_cpu(bw_config.port_bw_limit);
4964 vsi->bw_max_quanta = bw_config.max_bw;
4965 tc_bw_max = le16_to_cpu(bw_ets_config.tc_bw_max[0]) |
4966 (le16_to_cpu(bw_ets_config.tc_bw_max[1]) << 16);
4967 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
4968 vsi->bw_ets_share_credits[i] = bw_ets_config.share_credits[i];
4969 vsi->bw_ets_limit_credits[i] =
4970 le16_to_cpu(bw_ets_config.credits[i]);
4971 /* 3 bits out of 4 for each TC */
4972 vsi->bw_ets_max_quanta[i] = (u8)((tc_bw_max >> (i*4)) & 0x7);
4973 }
Jesse Brandeburg078b5872013-09-25 23:41:14 +00004974
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004975 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004976}
4977
4978/**
4979 * i40e_vsi_configure_bw_alloc - Configure VSI BW allocation per TC
4980 * @vsi: the VSI being configured
4981 * @enabled_tc: TC bitmap
4982 * @bw_credits: BW shared credits per TC
4983 *
4984 * Returns 0 on success, negative value on failure
4985 **/
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004986static int i40e_vsi_configure_bw_alloc(struct i40e_vsi *vsi, u8 enabled_tc,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004987 u8 *bw_share)
4988{
4989 struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004990 i40e_status ret;
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004991 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004992
4993 bw_data.tc_valid_bits = enabled_tc;
4994 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
4995 bw_data.tc_bw_credits[i] = bw_share[i];
4996
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004997 ret = i40e_aq_config_vsi_tc_bw(&vsi->back->hw, vsi->seid, &bw_data,
4998 NULL);
4999 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005000 dev_info(&vsi->back->pdev->dev,
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005001 "AQ command Config VSI BW allocation per TC failed = %d\n",
5002 vsi->back->hw.aq.asq_last_status);
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00005003 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005004 }
5005
5006 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5007 vsi->info.qs_handle[i] = bw_data.qs_handles[i];
5008
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00005009 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005010}
5011
5012/**
5013 * i40e_vsi_config_netdev_tc - Setup the netdev TC configuration
5014 * @vsi: the VSI being configured
5015 * @enabled_tc: TC map to be enabled
5016 *
5017 **/
5018static void i40e_vsi_config_netdev_tc(struct i40e_vsi *vsi, u8 enabled_tc)
5019{
5020 struct net_device *netdev = vsi->netdev;
5021 struct i40e_pf *pf = vsi->back;
5022 struct i40e_hw *hw = &pf->hw;
5023 u8 netdev_tc = 0;
5024 int i;
5025 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5026
5027 if (!netdev)
5028 return;
5029
5030 if (!enabled_tc) {
5031 netdev_reset_tc(netdev);
5032 return;
5033 }
5034
5035 /* Set up actual enabled TCs on the VSI */
5036 if (netdev_set_num_tc(netdev, vsi->tc_config.numtc))
5037 return;
5038
5039 /* set per TC queues for the VSI */
5040 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5041 /* Only set TC queues for enabled tcs
5042 *
5043 * e.g. For a VSI that has TC0 and TC3 enabled the
5044 * enabled_tc bitmap would be 0x00001001; the driver
5045 * will set the numtc for netdev as 2 that will be
5046 * referenced by the netdev layer as TC 0 and 1.
5047 */
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08005048 if (vsi->tc_config.enabled_tc & BIT(i))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005049 netdev_set_tc_queue(netdev,
5050 vsi->tc_config.tc_info[i].netdev_tc,
5051 vsi->tc_config.tc_info[i].qcount,
5052 vsi->tc_config.tc_info[i].qoffset);
5053 }
5054
5055 /* Assign UP2TC map for the VSI */
5056 for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
5057 /* Get the actual TC# for the UP */
5058 u8 ets_tc = dcbcfg->etscfg.prioritytable[i];
5059 /* Get the mapped netdev TC# for the UP */
5060 netdev_tc = vsi->tc_config.tc_info[ets_tc].netdev_tc;
5061 netdev_set_prio_tc_map(netdev, i, netdev_tc);
5062 }
5063}
5064
5065/**
5066 * i40e_vsi_update_queue_map - Update our copy of VSi info with new queue map
5067 * @vsi: the VSI being configured
5068 * @ctxt: the ctxt buffer returned from AQ VSI update param command
5069 **/
5070static void i40e_vsi_update_queue_map(struct i40e_vsi *vsi,
5071 struct i40e_vsi_context *ctxt)
5072{
5073 /* copy just the sections touched not the entire info
5074 * since not all sections are valid as returned by
5075 * update vsi params
5076 */
5077 vsi->info.mapping_flags = ctxt->info.mapping_flags;
5078 memcpy(&vsi->info.queue_mapping,
5079 &ctxt->info.queue_mapping, sizeof(vsi->info.queue_mapping));
5080 memcpy(&vsi->info.tc_mapping, ctxt->info.tc_mapping,
5081 sizeof(vsi->info.tc_mapping));
5082}
5083
5084/**
5085 * i40e_vsi_config_tc - Configure VSI Tx Scheduler for given TC map
5086 * @vsi: VSI to be configured
5087 * @enabled_tc: TC bitmap
5088 *
5089 * This configures a particular VSI for TCs that are mapped to the
5090 * given TC bitmap. It uses default bandwidth share for TCs across
5091 * VSIs to configure TC for a particular VSI.
5092 *
5093 * NOTE:
5094 * It is expected that the VSI queues have been quisced before calling
5095 * this function.
5096 **/
5097static int i40e_vsi_config_tc(struct i40e_vsi *vsi, u8 enabled_tc)
5098{
5099 u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
5100 struct i40e_vsi_context ctxt;
5101 int ret = 0;
5102 int i;
5103
5104 /* Check if enabled_tc is same as existing or new TCs */
5105 if (vsi->tc_config.enabled_tc == enabled_tc)
5106 return ret;
5107
5108 /* Enable ETS TCs with equal BW Share for now across all VSIs */
5109 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08005110 if (enabled_tc & BIT(i))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005111 bw_share[i] = 1;
5112 }
5113
5114 ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
5115 if (ret) {
5116 dev_info(&vsi->back->pdev->dev,
5117 "Failed configuring TC map %d for VSI %d\n",
5118 enabled_tc, vsi->seid);
5119 goto out;
5120 }
5121
5122 /* Update Queue Pairs Mapping for currently enabled UPs */
5123 ctxt.seid = vsi->seid;
5124 ctxt.pf_num = vsi->back->hw.pf_id;
5125 ctxt.vf_num = 0;
5126 ctxt.uplink_seid = vsi->uplink_seid;
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07005127 ctxt.info = vsi->info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005128 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
5129
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06005130 if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
5131 ctxt.info.valid_sections |=
5132 cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
5133 ctxt.info.queueing_opt_flags |= I40E_AQ_VSI_QUE_OPT_TCP_ENA;
5134 }
5135
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005136 /* Update the VSI after updating the VSI queue-mapping information */
5137 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
5138 if (ret) {
5139 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005140 "Update vsi tc config failed, err %s aq_err %s\n",
5141 i40e_stat_str(&vsi->back->hw, ret),
5142 i40e_aq_str(&vsi->back->hw,
5143 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005144 goto out;
5145 }
5146 /* update the local VSI info with updated queue map */
5147 i40e_vsi_update_queue_map(vsi, &ctxt);
5148 vsi->info.valid_sections = 0;
5149
5150 /* Update current VSI BW information */
5151 ret = i40e_vsi_get_bw_info(vsi);
5152 if (ret) {
5153 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005154 "Failed updating vsi bw info, err %s aq_err %s\n",
5155 i40e_stat_str(&vsi->back->hw, ret),
5156 i40e_aq_str(&vsi->back->hw,
5157 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005158 goto out;
5159 }
5160
5161 /* Update the netdev TC setup */
5162 i40e_vsi_config_netdev_tc(vsi, enabled_tc);
5163out:
5164 return ret;
5165}
5166
5167/**
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005168 * i40e_veb_config_tc - Configure TCs for given VEB
5169 * @veb: given VEB
5170 * @enabled_tc: TC bitmap
5171 *
5172 * Configures given TC bitmap for VEB (switching) element
5173 **/
5174int i40e_veb_config_tc(struct i40e_veb *veb, u8 enabled_tc)
5175{
5176 struct i40e_aqc_configure_switching_comp_bw_config_data bw_data = {0};
5177 struct i40e_pf *pf = veb->pf;
5178 int ret = 0;
5179 int i;
5180
5181 /* No TCs or already enabled TCs just return */
5182 if (!enabled_tc || veb->enabled_tc == enabled_tc)
5183 return ret;
5184
5185 bw_data.tc_valid_bits = enabled_tc;
5186 /* bw_data.absolute_credits is not set (relative) */
5187
5188 /* Enable ETS TCs with equal BW Share for now */
5189 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08005190 if (enabled_tc & BIT(i))
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005191 bw_data.tc_bw_share_credits[i] = 1;
5192 }
5193
5194 ret = i40e_aq_config_switch_comp_bw_config(&pf->hw, veb->seid,
5195 &bw_data, NULL);
5196 if (ret) {
5197 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005198 "VEB bw config failed, err %s aq_err %s\n",
5199 i40e_stat_str(&pf->hw, ret),
5200 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005201 goto out;
5202 }
5203
5204 /* Update the BW information */
5205 ret = i40e_veb_get_bw_info(veb);
5206 if (ret) {
5207 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005208 "Failed getting veb bw config, err %s aq_err %s\n",
5209 i40e_stat_str(&pf->hw, ret),
5210 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005211 }
5212
5213out:
5214 return ret;
5215}
5216
5217#ifdef CONFIG_I40E_DCB
5218/**
5219 * i40e_dcb_reconfigure - Reconfigure all VEBs and VSIs
5220 * @pf: PF struct
5221 *
5222 * Reconfigure VEB/VSIs on a given PF; it is assumed that
5223 * the caller would've quiesce all the VSIs before calling
5224 * this function
5225 **/
5226static void i40e_dcb_reconfigure(struct i40e_pf *pf)
5227{
5228 u8 tc_map = 0;
5229 int ret;
5230 u8 v;
5231
5232 /* Enable the TCs available on PF to all VEBs */
5233 tc_map = i40e_pf_get_tc_map(pf);
5234 for (v = 0; v < I40E_MAX_VEB; v++) {
5235 if (!pf->veb[v])
5236 continue;
5237 ret = i40e_veb_config_tc(pf->veb[v], tc_map);
5238 if (ret) {
5239 dev_info(&pf->pdev->dev,
5240 "Failed configuring TC for VEB seid=%d\n",
5241 pf->veb[v]->seid);
5242 /* Will try to configure as many components */
5243 }
5244 }
5245
5246 /* Update each VSI */
Mitch Williams505682c2014-05-20 08:01:37 +00005247 for (v = 0; v < pf->num_alloc_vsi; v++) {
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005248 if (!pf->vsi[v])
5249 continue;
5250
5251 /* - Enable all TCs for the LAN VSI
5252 * - For all others keep them at TC0 for now
5253 */
5254 if (v == pf->lan_vsi)
5255 tc_map = i40e_pf_get_tc_map(pf);
5256 else
David Ertmanea6acb72016-09-20 07:10:50 -07005257 tc_map = I40E_DEFAULT_TRAFFIC_CLASS;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005258
5259 ret = i40e_vsi_config_tc(pf->vsi[v], tc_map);
5260 if (ret) {
5261 dev_info(&pf->pdev->dev,
5262 "Failed configuring TC for VSI seid=%d\n",
5263 pf->vsi[v]->seid);
5264 /* Will try to configure as many components */
5265 } else {
Neerav Parikh0672a092014-04-01 07:11:47 +00005266 /* Re-configure VSI vectors based on updated TC map */
5267 i40e_vsi_map_rings_to_vectors(pf->vsi[v]);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005268 if (pf->vsi[v]->netdev)
5269 i40e_dcbnl_set_all(pf->vsi[v]);
5270 }
5271 }
5272}
5273
5274/**
Neerav Parikh2fd75f32014-11-12 00:18:20 +00005275 * i40e_resume_port_tx - Resume port Tx
5276 * @pf: PF struct
5277 *
5278 * Resume a port's Tx and issue a PF reset in case of failure to
5279 * resume.
5280 **/
5281static int i40e_resume_port_tx(struct i40e_pf *pf)
5282{
5283 struct i40e_hw *hw = &pf->hw;
5284 int ret;
5285
5286 ret = i40e_aq_resume_port_tx(hw, NULL);
5287 if (ret) {
5288 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005289 "Resume Port Tx failed, err %s aq_err %s\n",
5290 i40e_stat_str(&pf->hw, ret),
5291 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh2fd75f32014-11-12 00:18:20 +00005292 /* Schedule PF reset to recover */
Jacob Keller0da36b92017-04-19 09:25:55 -04005293 set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
Neerav Parikh2fd75f32014-11-12 00:18:20 +00005294 i40e_service_event_schedule(pf);
5295 }
5296
5297 return ret;
5298}
5299
5300/**
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005301 * i40e_init_pf_dcb - Initialize DCB configuration
5302 * @pf: PF being configured
5303 *
5304 * Query the current DCB configuration and cache it
5305 * in the hardware structure
5306 **/
5307static int i40e_init_pf_dcb(struct i40e_pf *pf)
5308{
5309 struct i40e_hw *hw = &pf->hw;
5310 int err = 0;
5311
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +00005312 /* Do not enable DCB for SW1 and SW2 images even if the FW is capable */
Jacob Kellerd36e41d2017-06-23 04:24:46 -04005313 if (pf->hw_features & I40E_HW_NO_DCB_SUPPORT)
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +00005314 goto out;
5315
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005316 /* Get the initial DCB configuration */
5317 err = i40e_init_dcb(hw);
5318 if (!err) {
5319 /* Device/Function is not DCBX capable */
5320 if ((!hw->func_caps.dcb) ||
5321 (hw->dcbx_status == I40E_DCBX_STATUS_DISABLED)) {
5322 dev_info(&pf->pdev->dev,
5323 "DCBX offload is not supported or is disabled for this PF.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005324 } else {
5325 /* When status is not DISABLED then DCBX in FW */
5326 pf->dcbx_cap = DCB_CAP_DCBX_LLD_MANAGED |
5327 DCB_CAP_DCBX_VER_IEEE;
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005328
5329 pf->flags |= I40E_FLAG_DCB_CAPABLE;
Dave Ertmana0362442016-08-29 17:38:26 -07005330 /* Enable DCB tagging only when more than one TC
5331 * or explicitly disable if only one TC
5332 */
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005333 if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
5334 pf->flags |= I40E_FLAG_DCB_ENABLED;
Dave Ertmana0362442016-08-29 17:38:26 -07005335 else
5336 pf->flags &= ~I40E_FLAG_DCB_ENABLED;
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005337 dev_dbg(&pf->pdev->dev,
5338 "DCBX offload is supported for this PF.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005339 }
Neerav Parikh014269f2014-04-01 07:11:48 +00005340 } else {
Shannon Nelsonaebfc812014-12-11 07:06:38 +00005341 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005342 "Query for DCB configuration failed, err %s aq_err %s\n",
5343 i40e_stat_str(&pf->hw, err),
5344 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005345 }
5346
5347out:
5348 return err;
5349}
5350#endif /* CONFIG_I40E_DCB */
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005351#define SPEED_SIZE 14
5352#define FC_SIZE 8
5353/**
5354 * i40e_print_link_message - print link up or down
5355 * @vsi: the VSI for which link needs a message
5356 */
Matt Jaredc156f852015-08-27 11:42:39 -04005357void i40e_print_link_message(struct i40e_vsi *vsi, bool isup)
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005358{
Filip Sadowski7ec9ba12016-11-08 13:05:13 -08005359 enum i40e_aq_link_speed new_speed;
Sudheer Mogilappagari3fded462017-07-14 09:10:18 -04005360 struct i40e_pf *pf = vsi->back;
Shannon Nelsona9165492015-09-03 17:19:00 -04005361 char *speed = "Unknown";
5362 char *fc = "Unknown";
Henry Tieman3e03d7c2016-12-02 12:32:57 -08005363 char *fec = "";
Mariusz Stachura68e49702017-07-12 05:46:14 -04005364 char *req_fec = "";
Henry Tieman3e03d7c2016-12-02 12:32:57 -08005365 char *an = "";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005366
Sudheer Mogilappagari3fded462017-07-14 09:10:18 -04005367 new_speed = pf->hw.phy.link_info.link_speed;
Filip Sadowski7ec9ba12016-11-08 13:05:13 -08005368
5369 if ((vsi->current_isup == isup) && (vsi->current_speed == new_speed))
Matt Jaredc156f852015-08-27 11:42:39 -04005370 return;
5371 vsi->current_isup = isup;
Filip Sadowski7ec9ba12016-11-08 13:05:13 -08005372 vsi->current_speed = new_speed;
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005373 if (!isup) {
5374 netdev_info(vsi->netdev, "NIC Link is Down\n");
5375 return;
5376 }
5377
Greg Rose148c2d82014-12-11 07:06:27 +00005378 /* Warn user if link speed on NPAR enabled partition is not at
5379 * least 10GB
5380 */
Sudheer Mogilappagari3fded462017-07-14 09:10:18 -04005381 if (pf->hw.func_caps.npar_enable &&
5382 (pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_1GB ||
5383 pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_100MB))
Greg Rose148c2d82014-12-11 07:06:27 +00005384 netdev_warn(vsi->netdev,
5385 "The partition detected link speed that is less than 10Gbps\n");
5386
Sudheer Mogilappagari3fded462017-07-14 09:10:18 -04005387 switch (pf->hw.phy.link_info.link_speed) {
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005388 case I40E_LINK_SPEED_40GB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005389 speed = "40 G";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005390 break;
Jesse Brandeburgae24b402015-03-27 00:12:09 -07005391 case I40E_LINK_SPEED_20GB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005392 speed = "20 G";
Jesse Brandeburgae24b402015-03-27 00:12:09 -07005393 break;
Carolyn Wyborny31232372016-11-21 13:03:48 -08005394 case I40E_LINK_SPEED_25GB:
5395 speed = "25 G";
5396 break;
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005397 case I40E_LINK_SPEED_10GB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005398 speed = "10 G";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005399 break;
5400 case I40E_LINK_SPEED_1GB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005401 speed = "1000 M";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005402 break;
Mitch Williams5960d332014-09-13 07:40:47 +00005403 case I40E_LINK_SPEED_100MB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005404 speed = "100 M";
Mitch Williams5960d332014-09-13 07:40:47 +00005405 break;
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005406 default:
5407 break;
5408 }
5409
Sudheer Mogilappagari3fded462017-07-14 09:10:18 -04005410 switch (pf->hw.fc.current_mode) {
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005411 case I40E_FC_FULL:
Shannon Nelsona9165492015-09-03 17:19:00 -04005412 fc = "RX/TX";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005413 break;
5414 case I40E_FC_TX_PAUSE:
Shannon Nelsona9165492015-09-03 17:19:00 -04005415 fc = "TX";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005416 break;
5417 case I40E_FC_RX_PAUSE:
Shannon Nelsona9165492015-09-03 17:19:00 -04005418 fc = "RX";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005419 break;
5420 default:
Shannon Nelsona9165492015-09-03 17:19:00 -04005421 fc = "None";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005422 break;
5423 }
5424
Sudheer Mogilappagari3fded462017-07-14 09:10:18 -04005425 if (pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_25GB) {
Mariusz Stachura68e49702017-07-12 05:46:14 -04005426 req_fec = ", Requested FEC: None";
Henry Tieman3e03d7c2016-12-02 12:32:57 -08005427 fec = ", FEC: None";
5428 an = ", Autoneg: False";
5429
Sudheer Mogilappagari3fded462017-07-14 09:10:18 -04005430 if (pf->hw.phy.link_info.an_info & I40E_AQ_AN_COMPLETED)
Henry Tieman3e03d7c2016-12-02 12:32:57 -08005431 an = ", Autoneg: True";
5432
Sudheer Mogilappagari3fded462017-07-14 09:10:18 -04005433 if (pf->hw.phy.link_info.fec_info &
Henry Tieman3e03d7c2016-12-02 12:32:57 -08005434 I40E_AQ_CONFIG_FEC_KR_ENA)
5435 fec = ", FEC: CL74 FC-FEC/BASE-R";
Sudheer Mogilappagari3fded462017-07-14 09:10:18 -04005436 else if (pf->hw.phy.link_info.fec_info &
Henry Tieman3e03d7c2016-12-02 12:32:57 -08005437 I40E_AQ_CONFIG_FEC_RS_ENA)
5438 fec = ", FEC: CL108 RS-FEC";
Mariusz Stachura68e49702017-07-12 05:46:14 -04005439
5440 /* 'CL108 RS-FEC' should be displayed when RS is requested, or
5441 * both RS and FC are requested
5442 */
5443 if (vsi->back->hw.phy.link_info.req_fec_info &
5444 (I40E_AQ_REQUEST_FEC_KR | I40E_AQ_REQUEST_FEC_RS)) {
5445 if (vsi->back->hw.phy.link_info.req_fec_info &
5446 I40E_AQ_REQUEST_FEC_RS)
5447 req_fec = ", Requested FEC: CL108 RS-FEC";
5448 else
5449 req_fec = ", Requested FEC: CL74 FC-FEC/BASE-R";
5450 }
Henry Tieman3e03d7c2016-12-02 12:32:57 -08005451 }
5452
Mariusz Stachura68e49702017-07-12 05:46:14 -04005453 netdev_info(vsi->netdev, "NIC Link is Up, %sbps Full Duplex%s%s%s, Flow Control: %s\n",
5454 speed, req_fec, fec, an, fc);
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005455}
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005456
5457/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005458 * i40e_up_complete - Finish the last steps of bringing up a connection
5459 * @vsi: the VSI being configured
5460 **/
5461static int i40e_up_complete(struct i40e_vsi *vsi)
5462{
5463 struct i40e_pf *pf = vsi->back;
5464 int err;
5465
5466 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
5467 i40e_vsi_configure_msix(vsi);
5468 else
5469 i40e_configure_msi_and_legacy(vsi);
5470
5471 /* start rings */
Filip Sadowski3aa7b742016-10-11 15:26:58 -07005472 err = i40e_vsi_start_rings(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005473 if (err)
5474 return err;
5475
Jacob Keller0da36b92017-04-19 09:25:55 -04005476 clear_bit(__I40E_VSI_DOWN, vsi->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005477 i40e_napi_enable_all(vsi);
5478 i40e_vsi_enable_irq(vsi);
5479
5480 if ((pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP) &&
5481 (vsi->netdev)) {
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005482 i40e_print_link_message(vsi, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005483 netif_tx_start_all_queues(vsi->netdev);
5484 netif_carrier_on(vsi->netdev);
5485 }
Anjali Singhai Jainca64fa42014-02-11 08:26:30 +00005486
5487 /* replay FDIR SB filters */
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005488 if (vsi->type == I40E_VSI_FDIR) {
5489 /* reset fd counters */
Jacob Keller097dbf52017-02-06 14:38:46 -08005490 pf->fd_add_err = 0;
5491 pf->fd_atr_cnt = 0;
Anjali Singhai Jainca64fa42014-02-11 08:26:30 +00005492 i40e_fdir_filter_restore(vsi);
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005493 }
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06005494
5495 /* On the next run of the service_task, notify any clients of the new
5496 * opened netdev
5497 */
5498 pf->flags |= I40E_FLAG_SERVICE_CLIENT_REQUESTED;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005499 i40e_service_event_schedule(pf);
5500
5501 return 0;
5502}
5503
5504/**
5505 * i40e_vsi_reinit_locked - Reset the VSI
5506 * @vsi: the VSI being configured
5507 *
5508 * Rebuild the ring structs after some configuration
5509 * has changed, e.g. MTU size.
5510 **/
5511static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi)
5512{
5513 struct i40e_pf *pf = vsi->back;
5514
5515 WARN_ON(in_interrupt());
Jacob Keller0da36b92017-04-19 09:25:55 -04005516 while (test_and_set_bit(__I40E_CONFIG_BUSY, pf->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005517 usleep_range(1000, 2000);
5518 i40e_down(vsi);
5519
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005520 i40e_up(vsi);
Jacob Keller0da36b92017-04-19 09:25:55 -04005521 clear_bit(__I40E_CONFIG_BUSY, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005522}
5523
5524/**
5525 * i40e_up - Bring the connection back up after being down
5526 * @vsi: the VSI being configured
5527 **/
5528int i40e_up(struct i40e_vsi *vsi)
5529{
5530 int err;
5531
5532 err = i40e_vsi_configure(vsi);
5533 if (!err)
5534 err = i40e_up_complete(vsi);
5535
5536 return err;
5537}
5538
5539/**
5540 * i40e_down - Shutdown the connection processing
5541 * @vsi: the VSI being stopped
5542 **/
5543void i40e_down(struct i40e_vsi *vsi)
5544{
5545 int i;
5546
5547 /* It is assumed that the caller of this function
Jacob Kellerd19cb642017-04-21 13:38:05 -07005548 * sets the vsi->state __I40E_VSI_DOWN bit.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005549 */
5550 if (vsi->netdev) {
5551 netif_carrier_off(vsi->netdev);
5552 netif_tx_disable(vsi->netdev);
5553 }
5554 i40e_vsi_disable_irq(vsi);
Filip Sadowski3aa7b742016-10-11 15:26:58 -07005555 i40e_vsi_stop_rings(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005556 i40e_napi_disable_all(vsi);
5557
5558 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck9f65e152013-09-28 06:00:58 +00005559 i40e_clean_tx_ring(vsi->tx_rings[i]);
Björn Töpel74608d12017-05-24 07:55:35 +02005560 if (i40e_enabled_xdp_vsi(vsi))
5561 i40e_clean_tx_ring(vsi->xdp_rings[i]);
Alexander Duyck9f65e152013-09-28 06:00:58 +00005562 i40e_clean_rx_ring(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005563 }
Catherine Sullivanf980d442016-05-16 10:26:34 -07005564
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005565}
5566
5567/**
5568 * i40e_setup_tc - configure multiple traffic classes
5569 * @netdev: net device to configure
5570 * @tc: number of traffic classes to enable
5571 **/
5572static int i40e_setup_tc(struct net_device *netdev, u8 tc)
5573{
5574 struct i40e_netdev_priv *np = netdev_priv(netdev);
5575 struct i40e_vsi *vsi = np->vsi;
5576 struct i40e_pf *pf = vsi->back;
5577 u8 enabled_tc = 0;
5578 int ret = -EINVAL;
5579 int i;
5580
5581 /* Check if DCB enabled to continue */
5582 if (!(pf->flags & I40E_FLAG_DCB_ENABLED)) {
5583 netdev_info(netdev, "DCB is not enabled for adapter\n");
5584 goto exit;
5585 }
5586
5587 /* Check if MFP enabled */
5588 if (pf->flags & I40E_FLAG_MFP_ENABLED) {
5589 netdev_info(netdev, "Configuring TC not supported in MFP mode\n");
5590 goto exit;
5591 }
5592
5593 /* Check whether tc count is within enabled limit */
5594 if (tc > i40e_pf_get_num_tc(pf)) {
5595 netdev_info(netdev, "TC count greater than enabled on link for adapter\n");
5596 goto exit;
5597 }
5598
5599 /* Generate TC map for number of tc requested */
5600 for (i = 0; i < tc; i++)
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08005601 enabled_tc |= BIT(i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005602
5603 /* Requesting same TC configuration as already enabled */
5604 if (enabled_tc == vsi->tc_config.enabled_tc)
5605 return 0;
5606
5607 /* Quiesce VSI queues */
5608 i40e_quiesce_vsi(vsi);
5609
5610 /* Configure VSI for enabled TCs */
5611 ret = i40e_vsi_config_tc(vsi, enabled_tc);
5612 if (ret) {
5613 netdev_info(netdev, "Failed configuring TC for VSI seid=%d\n",
5614 vsi->seid);
5615 goto exit;
5616 }
5617
5618 /* Unquiesce VSI */
5619 i40e_unquiesce_vsi(vsi);
5620
5621exit:
5622 return ret;
5623}
5624
Jiri Pirko2572ac52017-08-07 10:15:17 +02005625static int __i40e_setup_tc(struct net_device *netdev, enum tc_setup_type type,
Jiri Pirkode4784c2017-08-07 10:15:32 +02005626 void *type_data)
John Fastabende4c67342016-02-16 21:16:15 -08005627{
Jiri Pirkode4784c2017-08-07 10:15:32 +02005628 struct tc_mqprio_qopt *mqprio = type_data;
5629
Jiri Pirko2572ac52017-08-07 10:15:17 +02005630 if (type != TC_SETUP_MQPRIO)
Jiri Pirko38cf0422017-08-07 10:15:31 +02005631 return -EOPNOTSUPP;
Amritha Nambiar56f36ac2017-03-15 10:39:25 -07005632
Jiri Pirkode4784c2017-08-07 10:15:32 +02005633 mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
Amritha Nambiar56f36ac2017-03-15 10:39:25 -07005634
Jiri Pirkode4784c2017-08-07 10:15:32 +02005635 return i40e_setup_tc(netdev, mqprio->num_tc);
John Fastabende4c67342016-02-16 21:16:15 -08005636}
5637
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005638/**
5639 * i40e_open - Called when a network interface is made active
5640 * @netdev: network interface device structure
5641 *
5642 * The open entry point is called when a network interface is made
5643 * active by the system (IFF_UP). At this point all resources needed
5644 * for transmit and receive operations are allocated, the interrupt
5645 * handler is registered with the OS, the netdev watchdog subtask is
5646 * enabled, and the stack is notified that the interface is ready.
5647 *
5648 * Returns 0 on success, negative value on failure
5649 **/
Vasu Dev38e00432014-08-01 13:27:03 -07005650int i40e_open(struct net_device *netdev)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005651{
5652 struct i40e_netdev_priv *np = netdev_priv(netdev);
5653 struct i40e_vsi *vsi = np->vsi;
5654 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005655 int err;
5656
Shannon Nelson4eb3f762014-03-06 08:59:58 +00005657 /* disallow open during test or if eeprom is broken */
Jacob Keller0da36b92017-04-19 09:25:55 -04005658 if (test_bit(__I40E_TESTING, pf->state) ||
5659 test_bit(__I40E_BAD_EEPROM, pf->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005660 return -EBUSY;
5661
5662 netif_carrier_off(netdev);
5663
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005664 err = i40e_vsi_open(vsi);
5665 if (err)
5666 return err;
5667
Jesse Brandeburg059dab62014-04-01 09:07:20 +00005668 /* configure global TSO hardware offload settings */
5669 wr32(&pf->hw, I40E_GLLAN_TSOMSK_F, be32_to_cpu(TCP_FLAG_PSH |
5670 TCP_FLAG_FIN) >> 16);
5671 wr32(&pf->hw, I40E_GLLAN_TSOMSK_M, be32_to_cpu(TCP_FLAG_PSH |
5672 TCP_FLAG_FIN |
5673 TCP_FLAG_CWR) >> 16);
5674 wr32(&pf->hw, I40E_GLLAN_TSOMSK_L, be32_to_cpu(TCP_FLAG_CWR) >> 16);
5675
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07005676 udp_tunnel_get_rx_info(netdev);
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06005677
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005678 return 0;
5679}
5680
5681/**
5682 * i40e_vsi_open -
5683 * @vsi: the VSI to open
5684 *
5685 * Finish initialization of the VSI.
5686 *
5687 * Returns 0 on success, negative value on failure
Maciej Sosin373149f2017-04-05 07:50:55 -04005688 *
5689 * Note: expects to be called while under rtnl_lock()
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005690 **/
5691int i40e_vsi_open(struct i40e_vsi *vsi)
5692{
5693 struct i40e_pf *pf = vsi->back;
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00005694 char int_name[I40E_INT_NAME_STR_LEN];
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005695 int err;
5696
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005697 /* allocate descriptors */
5698 err = i40e_vsi_setup_tx_resources(vsi);
5699 if (err)
5700 goto err_setup_tx;
5701 err = i40e_vsi_setup_rx_resources(vsi);
5702 if (err)
5703 goto err_setup_rx;
5704
5705 err = i40e_vsi_configure(vsi);
5706 if (err)
5707 goto err_setup_rx;
5708
Shannon Nelsonc22e3c62014-03-14 07:32:25 +00005709 if (vsi->netdev) {
5710 snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
5711 dev_driver_string(&pf->pdev->dev), vsi->netdev->name);
5712 err = i40e_vsi_request_irq(vsi, int_name);
5713 if (err)
5714 goto err_setup_rx;
5715
5716 /* Notify the stack of the actual queue counts. */
5717 err = netif_set_real_num_tx_queues(vsi->netdev,
5718 vsi->num_queue_pairs);
5719 if (err)
5720 goto err_set_queues;
5721
5722 err = netif_set_real_num_rx_queues(vsi->netdev,
5723 vsi->num_queue_pairs);
5724 if (err)
5725 goto err_set_queues;
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00005726
5727 } else if (vsi->type == I40E_VSI_FDIR) {
Carolyn Wybornye240f672014-12-11 07:06:37 +00005728 snprintf(int_name, sizeof(int_name) - 1, "%s-%s:fdir",
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +00005729 dev_driver_string(&pf->pdev->dev),
5730 dev_name(&pf->pdev->dev));
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00005731 err = i40e_vsi_request_irq(vsi, int_name);
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +00005732
Shannon Nelsonc22e3c62014-03-14 07:32:25 +00005733 } else {
Jean Sacrence9ccb12014-05-01 14:31:18 +00005734 err = -EINVAL;
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005735 goto err_setup_rx;
5736 }
Anjali Singhai Jain25946dd2013-11-26 10:49:14 +00005737
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005738 err = i40e_up_complete(vsi);
5739 if (err)
5740 goto err_up_complete;
5741
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005742 return 0;
5743
5744err_up_complete:
5745 i40e_down(vsi);
Anjali Singhai Jain25946dd2013-11-26 10:49:14 +00005746err_set_queues:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005747 i40e_vsi_free_irq(vsi);
5748err_setup_rx:
5749 i40e_vsi_free_rx_resources(vsi);
5750err_setup_tx:
5751 i40e_vsi_free_tx_resources(vsi);
5752 if (vsi == pf->vsi[pf->lan_vsi])
Maciej Sosin373149f2017-04-05 07:50:55 -04005753 i40e_do_reset(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED), true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005754
5755 return err;
5756}
5757
5758/**
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00005759 * i40e_fdir_filter_exit - Cleans up the Flow Director accounting
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00005760 * @pf: Pointer to PF
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00005761 *
5762 * This function destroys the hlist where all the Flow Director
5763 * filters were saved.
5764 **/
5765static void i40e_fdir_filter_exit(struct i40e_pf *pf)
5766{
5767 struct i40e_fdir_filter *filter;
Jacob Keller0e588de2017-02-06 14:38:50 -08005768 struct i40e_flex_pit *pit_entry, *tmp;
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00005769 struct hlist_node *node2;
5770
5771 hlist_for_each_entry_safe(filter, node2,
5772 &pf->fdir_filter_list, fdir_node) {
5773 hlist_del(&filter->fdir_node);
5774 kfree(filter);
5775 }
Jacob Keller097dbf52017-02-06 14:38:46 -08005776
Jacob Keller0e588de2017-02-06 14:38:50 -08005777 list_for_each_entry_safe(pit_entry, tmp, &pf->l3_flex_pit_list, list) {
5778 list_del(&pit_entry->list);
5779 kfree(pit_entry);
5780 }
5781 INIT_LIST_HEAD(&pf->l3_flex_pit_list);
5782
5783 list_for_each_entry_safe(pit_entry, tmp, &pf->l4_flex_pit_list, list) {
5784 list_del(&pit_entry->list);
5785 kfree(pit_entry);
5786 }
5787 INIT_LIST_HEAD(&pf->l4_flex_pit_list);
5788
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00005789 pf->fdir_pf_active_filters = 0;
Jacob Keller097dbf52017-02-06 14:38:46 -08005790 pf->fd_tcp4_filter_cnt = 0;
5791 pf->fd_udp4_filter_cnt = 0;
Jacob Kellerf223c872017-02-06 14:38:51 -08005792 pf->fd_sctp4_filter_cnt = 0;
Jacob Keller097dbf52017-02-06 14:38:46 -08005793 pf->fd_ip4_filter_cnt = 0;
Jacob Keller3bcee1e2017-02-06 14:38:46 -08005794
5795 /* Reprogram the default input set for TCP/IPv4 */
5796 i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_TCP,
5797 I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
5798 I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
5799
5800 /* Reprogram the default input set for UDP/IPv4 */
5801 i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_UDP,
5802 I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
5803 I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
5804
5805 /* Reprogram the default input set for SCTP/IPv4 */
5806 i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_SCTP,
5807 I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
5808 I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
5809
5810 /* Reprogram the default input set for Other/IPv4 */
5811 i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_OTHER,
5812 I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00005813}
5814
5815/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005816 * i40e_close - Disables a network interface
5817 * @netdev: network interface device structure
5818 *
5819 * The close entry point is called when an interface is de-activated
5820 * by the OS. The hardware is still under the driver's control, but
5821 * this netdev interface is disabled.
5822 *
5823 * Returns 0, this is not allowed to fail
5824 **/
Vasu Dev38e00432014-08-01 13:27:03 -07005825int i40e_close(struct net_device *netdev)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005826{
5827 struct i40e_netdev_priv *np = netdev_priv(netdev);
5828 struct i40e_vsi *vsi = np->vsi;
5829
Shannon Nelson90ef8d42014-03-14 07:32:26 +00005830 i40e_vsi_close(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005831
5832 return 0;
5833}
5834
5835/**
5836 * i40e_do_reset - Start a PF or Core Reset sequence
5837 * @pf: board private structure
5838 * @reset_flags: which reset is requested
Maciej Sosin373149f2017-04-05 07:50:55 -04005839 * @lock_acquired: indicates whether or not the lock has been acquired
5840 * before this function was called.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005841 *
5842 * The essential difference in resets is that the PF Reset
5843 * doesn't clear the packet buffers, doesn't reset the PE
5844 * firmware, and doesn't bother the other PFs on the chip.
5845 **/
Maciej Sosin373149f2017-04-05 07:50:55 -04005846void i40e_do_reset(struct i40e_pf *pf, u32 reset_flags, bool lock_acquired)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005847{
5848 u32 val;
5849
5850 WARN_ON(in_interrupt());
5851
Mitch Williams263fc482014-04-23 04:50:11 +00005852
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005853 /* do the biggest reset indicated */
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005854 if (reset_flags & BIT_ULL(__I40E_GLOBAL_RESET_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005855
5856 /* Request a Global Reset
5857 *
5858 * This will start the chip's countdown to the actual full
5859 * chip reset event, and a warning interrupt to be sent
5860 * to all PFs, including the requestor. Our handler
5861 * for the warning interrupt will deal with the shutdown
5862 * and recovery of the switch setup.
5863 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005864 dev_dbg(&pf->pdev->dev, "GlobalR requested\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005865 val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
5866 val |= I40E_GLGEN_RTRIG_GLOBR_MASK;
5867 wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
5868
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005869 } else if (reset_flags & BIT_ULL(__I40E_CORE_RESET_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005870
5871 /* Request a Core Reset
5872 *
5873 * Same as Global Reset, except does *not* include the MAC/PHY
5874 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005875 dev_dbg(&pf->pdev->dev, "CoreR requested\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005876 val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
5877 val |= I40E_GLGEN_RTRIG_CORER_MASK;
5878 wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
5879 i40e_flush(&pf->hw);
5880
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005881 } else if (reset_flags & BIT_ULL(__I40E_PF_RESET_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005882
5883 /* Request a PF Reset
5884 *
5885 * Resets only the PF-specific registers
5886 *
5887 * This goes directly to the tear-down and rebuild of
5888 * the switch, since we need to do all the recovery as
5889 * for the Core Reset.
5890 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005891 dev_dbg(&pf->pdev->dev, "PFR requested\n");
Maciej Sosin373149f2017-04-05 07:50:55 -04005892 i40e_handle_reset_warning(pf, lock_acquired);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005893
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005894 } else if (reset_flags & BIT_ULL(__I40E_REINIT_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005895 int v;
5896
5897 /* Find the VSI(s) that requested a re-init */
5898 dev_info(&pf->pdev->dev,
5899 "VSI reinit requested\n");
Mitch Williams505682c2014-05-20 08:01:37 +00005900 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005901 struct i40e_vsi *vsi = pf->vsi[v];
Jesse Brandeburg6995b362015-08-28 17:55:54 -04005902
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005903 if (vsi != NULL &&
Jacob Kellerd19cb642017-04-21 13:38:05 -07005904 test_and_clear_bit(__I40E_VSI_REINIT_REQUESTED,
Jacob Keller0da36b92017-04-19 09:25:55 -04005905 vsi->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005906 i40e_vsi_reinit_locked(pf->vsi[v]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005907 }
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005908 } else if (reset_flags & BIT_ULL(__I40E_DOWN_REQUESTED)) {
Neerav Parikhb5d06f02014-06-03 23:50:17 +00005909 int v;
5910
5911 /* Find the VSI(s) that needs to be brought down */
5912 dev_info(&pf->pdev->dev, "VSI down requested\n");
5913 for (v = 0; v < pf->num_alloc_vsi; v++) {
5914 struct i40e_vsi *vsi = pf->vsi[v];
Jesse Brandeburg6995b362015-08-28 17:55:54 -04005915
Neerav Parikhb5d06f02014-06-03 23:50:17 +00005916 if (vsi != NULL &&
Jacob Kellerd19cb642017-04-21 13:38:05 -07005917 test_and_clear_bit(__I40E_VSI_DOWN_REQUESTED,
Jacob Keller0da36b92017-04-19 09:25:55 -04005918 vsi->state)) {
5919 set_bit(__I40E_VSI_DOWN, vsi->state);
Neerav Parikhb5d06f02014-06-03 23:50:17 +00005920 i40e_down(vsi);
Neerav Parikhb5d06f02014-06-03 23:50:17 +00005921 }
5922 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005923 } else {
5924 dev_info(&pf->pdev->dev,
5925 "bad reset request 0x%08x\n", reset_flags);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005926 }
5927}
5928
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005929#ifdef CONFIG_I40E_DCB
5930/**
5931 * i40e_dcb_need_reconfig - Check if DCB needs reconfig
5932 * @pf: board private structure
5933 * @old_cfg: current DCB config
5934 * @new_cfg: new DCB config
5935 **/
5936bool i40e_dcb_need_reconfig(struct i40e_pf *pf,
5937 struct i40e_dcbx_config *old_cfg,
5938 struct i40e_dcbx_config *new_cfg)
5939{
5940 bool need_reconfig = false;
5941
5942 /* Check if ETS configuration has changed */
5943 if (memcmp(&new_cfg->etscfg,
5944 &old_cfg->etscfg,
5945 sizeof(new_cfg->etscfg))) {
5946 /* If Priority Table has changed reconfig is needed */
5947 if (memcmp(&new_cfg->etscfg.prioritytable,
5948 &old_cfg->etscfg.prioritytable,
5949 sizeof(new_cfg->etscfg.prioritytable))) {
5950 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005951 dev_dbg(&pf->pdev->dev, "ETS UP2TC changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005952 }
5953
5954 if (memcmp(&new_cfg->etscfg.tcbwtable,
5955 &old_cfg->etscfg.tcbwtable,
5956 sizeof(new_cfg->etscfg.tcbwtable)))
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005957 dev_dbg(&pf->pdev->dev, "ETS TC BW Table changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005958
5959 if (memcmp(&new_cfg->etscfg.tsatable,
5960 &old_cfg->etscfg.tsatable,
5961 sizeof(new_cfg->etscfg.tsatable)))
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005962 dev_dbg(&pf->pdev->dev, "ETS TSA Table changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005963 }
5964
5965 /* Check if PFC configuration has changed */
5966 if (memcmp(&new_cfg->pfc,
5967 &old_cfg->pfc,
5968 sizeof(new_cfg->pfc))) {
5969 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005970 dev_dbg(&pf->pdev->dev, "PFC config change detected.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005971 }
5972
5973 /* Check if APP Table has changed */
5974 if (memcmp(&new_cfg->app,
5975 &old_cfg->app,
Dave Jones3d9667a2014-01-27 23:11:09 -05005976 sizeof(new_cfg->app))) {
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005977 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005978 dev_dbg(&pf->pdev->dev, "APP Table change detected.\n");
Dave Jones3d9667a2014-01-27 23:11:09 -05005979 }
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005980
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04005981 dev_dbg(&pf->pdev->dev, "dcb need_reconfig=%d\n", need_reconfig);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005982 return need_reconfig;
5983}
5984
5985/**
5986 * i40e_handle_lldp_event - Handle LLDP Change MIB event
5987 * @pf: board private structure
5988 * @e: event info posted on ARQ
5989 **/
5990static int i40e_handle_lldp_event(struct i40e_pf *pf,
5991 struct i40e_arq_event_info *e)
5992{
5993 struct i40e_aqc_lldp_get_mib *mib =
5994 (struct i40e_aqc_lldp_get_mib *)&e->desc.params.raw;
5995 struct i40e_hw *hw = &pf->hw;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005996 struct i40e_dcbx_config tmp_dcbx_cfg;
5997 bool need_reconfig = false;
5998 int ret = 0;
5999 u8 type;
6000
Neerav Parikh4d9b6042014-05-22 06:31:51 +00006001 /* Not DCB capable or capability disabled */
David Ertmanea6acb72016-09-20 07:10:50 -07006002 if (!(pf->flags & I40E_FLAG_DCB_CAPABLE))
Neerav Parikh4d9b6042014-05-22 06:31:51 +00006003 return ret;
6004
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006005 /* Ignore if event is not for Nearest Bridge */
6006 type = ((mib->type >> I40E_AQ_LLDP_BRIDGE_TYPE_SHIFT)
6007 & I40E_AQ_LLDP_BRIDGE_TYPE_MASK);
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04006008 dev_dbg(&pf->pdev->dev, "LLDP event mib bridge type 0x%x\n", type);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006009 if (type != I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE)
6010 return ret;
6011
6012 /* Check MIB Type and return if event for Remote MIB update */
6013 type = mib->type & I40E_AQ_LLDP_MIB_TYPE_MASK;
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00006014 dev_dbg(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04006015 "LLDP event mib type %s\n", type ? "remote" : "local");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006016 if (type == I40E_AQ_LLDP_MIB_REMOTE) {
6017 /* Update the remote cached instance and return */
6018 ret = i40e_aq_get_dcb_config(hw, I40E_AQ_LLDP_MIB_REMOTE,
6019 I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE,
6020 &hw->remote_dcbx_config);
6021 goto exit;
6022 }
6023
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00006024 /* Store the old configuration */
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07006025 tmp_dcbx_cfg = hw->local_dcbx_config;
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00006026
Neerav Parikh750fcbc2015-02-24 06:58:47 +00006027 /* Reset the old DCBx configuration data */
6028 memset(&hw->local_dcbx_config, 0, sizeof(hw->local_dcbx_config));
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00006029 /* Get updated DCBX data from firmware */
6030 ret = i40e_get_dcb_config(&pf->hw);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006031 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006032 dev_info(&pf->pdev->dev,
6033 "Failed querying DCB configuration data from firmware, err %s aq_err %s\n",
6034 i40e_stat_str(&pf->hw, ret),
6035 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006036 goto exit;
6037 }
6038
6039 /* No change detected in DCBX configs */
Neerav Parikh750fcbc2015-02-24 06:58:47 +00006040 if (!memcmp(&tmp_dcbx_cfg, &hw->local_dcbx_config,
6041 sizeof(tmp_dcbx_cfg))) {
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006042 dev_dbg(&pf->pdev->dev, "No change detected in DCBX configuration.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006043 goto exit;
6044 }
6045
Neerav Parikh750fcbc2015-02-24 06:58:47 +00006046 need_reconfig = i40e_dcb_need_reconfig(pf, &tmp_dcbx_cfg,
6047 &hw->local_dcbx_config);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006048
Neerav Parikh750fcbc2015-02-24 06:58:47 +00006049 i40e_dcbnl_flush_apps(pf, &tmp_dcbx_cfg, &hw->local_dcbx_config);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006050
6051 if (!need_reconfig)
6052 goto exit;
6053
Neerav Parikh4d9b6042014-05-22 06:31:51 +00006054 /* Enable DCB tagging only when more than one TC */
Neerav Parikh750fcbc2015-02-24 06:58:47 +00006055 if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
Neerav Parikh4d9b6042014-05-22 06:31:51 +00006056 pf->flags |= I40E_FLAG_DCB_ENABLED;
6057 else
6058 pf->flags &= ~I40E_FLAG_DCB_ENABLED;
6059
Jacob Keller0da36b92017-04-19 09:25:55 -04006060 set_bit(__I40E_PORT_SUSPENDED, pf->state);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006061 /* Reconfiguration needed quiesce all VSIs */
6062 i40e_pf_quiesce_all_vsi(pf);
6063
6064 /* Changes in configuration update VEB/VSI */
6065 i40e_dcb_reconfigure(pf);
6066
Neerav Parikh2fd75f32014-11-12 00:18:20 +00006067 ret = i40e_resume_port_tx(pf);
6068
Jacob Keller0da36b92017-04-19 09:25:55 -04006069 clear_bit(__I40E_PORT_SUSPENDED, pf->state);
Neerav Parikh2fd75f32014-11-12 00:18:20 +00006070 /* In case of error no point in resuming VSIs */
Neerav Parikh69129dc2014-11-12 00:18:46 +00006071 if (ret)
6072 goto exit;
6073
Neerav Parikh3fe06f42016-02-17 16:12:15 -08006074 /* Wait for the PF's queues to be disabled */
6075 ret = i40e_pf_wait_queues_disabled(pf);
Parikh, Neerav11e47702015-02-21 06:43:55 +00006076 if (ret) {
6077 /* Schedule PF reset to recover */
Jacob Keller0da36b92017-04-19 09:25:55 -04006078 set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
Parikh, Neerav11e47702015-02-21 06:43:55 +00006079 i40e_service_event_schedule(pf);
6080 } else {
Neerav Parikh2fd75f32014-11-12 00:18:20 +00006081 i40e_pf_unquiesce_all_vsi(pf);
Mitch Williams0ef2d5a2017-01-24 10:24:00 -08006082 pf->flags |= (I40E_FLAG_SERVICE_CLIENT_REQUESTED |
6083 I40E_FLAG_CLIENT_L2_CHANGE);
Parikh, Neerav11e47702015-02-21 06:43:55 +00006084 }
6085
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006086exit:
6087 return ret;
6088}
6089#endif /* CONFIG_I40E_DCB */
6090
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006091/**
Anjali Singhai Jain233261862013-11-26 10:49:22 +00006092 * i40e_do_reset_safe - Protected reset path for userland calls.
6093 * @pf: board private structure
6094 * @reset_flags: which reset is requested
6095 *
6096 **/
6097void i40e_do_reset_safe(struct i40e_pf *pf, u32 reset_flags)
6098{
6099 rtnl_lock();
Maciej Sosin373149f2017-04-05 07:50:55 -04006100 i40e_do_reset(pf, reset_flags, true);
Anjali Singhai Jain233261862013-11-26 10:49:22 +00006101 rtnl_unlock();
6102}
6103
6104/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006105 * i40e_handle_lan_overflow_event - Handler for LAN queue overflow event
6106 * @pf: board private structure
6107 * @e: event info posted on ARQ
6108 *
6109 * Handler for LAN Queue Overflow Event generated by the firmware for PF
6110 * and VF queues
6111 **/
6112static void i40e_handle_lan_overflow_event(struct i40e_pf *pf,
6113 struct i40e_arq_event_info *e)
6114{
6115 struct i40e_aqc_lan_overflow *data =
6116 (struct i40e_aqc_lan_overflow *)&e->desc.params.raw;
6117 u32 queue = le32_to_cpu(data->prtdcb_rupto);
6118 u32 qtx_ctl = le32_to_cpu(data->otx_ctl);
6119 struct i40e_hw *hw = &pf->hw;
6120 struct i40e_vf *vf;
6121 u16 vf_id;
6122
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006123 dev_dbg(&pf->pdev->dev, "overflow Rx Queue Number = %d QTX_CTL=0x%08x\n",
6124 queue, qtx_ctl);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006125
6126 /* Queue belongs to VF, find the VF and issue VF reset */
6127 if (((qtx_ctl & I40E_QTX_CTL_PFVF_Q_MASK)
6128 >> I40E_QTX_CTL_PFVF_Q_SHIFT) == I40E_QTX_CTL_VF_QUEUE) {
6129 vf_id = (u16)((qtx_ctl & I40E_QTX_CTL_VFVM_INDX_MASK)
6130 >> I40E_QTX_CTL_VFVM_INDX_SHIFT);
6131 vf_id -= hw->func_caps.vf_base_id;
6132 vf = &pf->vf[vf_id];
6133 i40e_vc_notify_vf_reset(vf);
6134 /* Allow VF to process pending reset notification */
6135 msleep(20);
6136 i40e_reset_vf(vf, false);
6137 }
6138}
6139
6140/**
Anjali Singhai Jain12957382014-06-04 04:22:47 +00006141 * i40e_get_cur_guaranteed_fd_count - Get the consumed guaranteed FD filters
6142 * @pf: board private structure
6143 **/
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006144u32 i40e_get_cur_guaranteed_fd_count(struct i40e_pf *pf)
Anjali Singhai Jain12957382014-06-04 04:22:47 +00006145{
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006146 u32 val, fcnt_prog;
Anjali Singhai Jain12957382014-06-04 04:22:47 +00006147
6148 val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
6149 fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK);
6150 return fcnt_prog;
6151}
6152
6153/**
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006154 * i40e_get_current_fd_count - Get total FD filters programmed for this PF
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006155 * @pf: board private structure
6156 **/
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006157u32 i40e_get_current_fd_count(struct i40e_pf *pf)
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006158{
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006159 u32 val, fcnt_prog;
6160
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006161 val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
6162 fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK) +
6163 ((val & I40E_PFQF_FDSTAT_BEST_CNT_MASK) >>
6164 I40E_PFQF_FDSTAT_BEST_CNT_SHIFT);
6165 return fcnt_prog;
6166}
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006167
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006168/**
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006169 * i40e_get_global_fd_count - Get total FD filters programmed on device
6170 * @pf: board private structure
6171 **/
6172u32 i40e_get_global_fd_count(struct i40e_pf *pf)
6173{
6174 u32 val, fcnt_prog;
6175
6176 val = rd32(&pf->hw, I40E_GLQF_FDCNT_0);
6177 fcnt_prog = (val & I40E_GLQF_FDCNT_0_GUARANT_CNT_MASK) +
6178 ((val & I40E_GLQF_FDCNT_0_BESTCNT_MASK) >>
6179 I40E_GLQF_FDCNT_0_BESTCNT_SHIFT);
6180 return fcnt_prog;
6181}
6182
6183/**
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006184 * i40e_fdir_check_and_reenable - Function to reenabe FD ATR or SB if disabled
6185 * @pf: board private structure
6186 **/
6187void i40e_fdir_check_and_reenable(struct i40e_pf *pf)
6188{
Carolyn Wyborny3487b6c2015-08-27 11:42:38 -04006189 struct i40e_fdir_filter *filter;
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006190 u32 fcnt_prog, fcnt_avail;
Carolyn Wyborny3487b6c2015-08-27 11:42:38 -04006191 struct hlist_node *node;
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006192
Jacob Keller0da36b92017-04-19 09:25:55 -04006193 if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006194 return;
6195
Jacob Keller47994c12017-04-19 09:25:57 -04006196 /* Check if we have enough room to re-enable FDir SB capability. */
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006197 fcnt_prog = i40e_get_global_fd_count(pf);
Anjali Singhai Jain12957382014-06-04 04:22:47 +00006198 fcnt_avail = pf->fdir_pf_filter_count;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006199 if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM)) ||
6200 (pf->fd_add_err == 0) ||
6201 (i40e_get_current_atr_cnt(pf) < pf->fd_atr_cnt)) {
Jacob Keller47994c12017-04-19 09:25:57 -04006202 if (pf->flags & I40E_FLAG_FD_SB_AUTO_DISABLED) {
6203 pf->flags &= ~I40E_FLAG_FD_SB_AUTO_DISABLED;
6204 if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
6205 (I40E_DEBUG_FD & pf->hw.debug_mask))
Anjali Singhai Jain2e4875e2015-04-16 20:06:06 -04006206 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 +00006207 }
6208 }
Jacob Kellera3417d22016-09-06 18:05:10 -07006209
Jacob Keller47994c12017-04-19 09:25:57 -04006210 /* We should wait for even more space before re-enabling ATR.
6211 * Additionally, we cannot enable ATR as long as we still have TCP SB
6212 * rules active.
Jacob Kellera3417d22016-09-06 18:05:10 -07006213 */
Jacob Keller47994c12017-04-19 09:25:57 -04006214 if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) &&
6215 (pf->fd_tcp4_filter_cnt == 0)) {
6216 if (pf->flags & I40E_FLAG_FD_ATR_AUTO_DISABLED) {
6217 pf->flags &= ~I40E_FLAG_FD_ATR_AUTO_DISABLED;
6218 if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
6219 (I40E_DEBUG_FD & pf->hw.debug_mask))
Jacob Kellera3417d22016-09-06 18:05:10 -07006220 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 +00006221 }
6222 }
Carolyn Wyborny3487b6c2015-08-27 11:42:38 -04006223
6224 /* if hw had a problem adding a filter, delete it */
6225 if (pf->fd_inv > 0) {
6226 hlist_for_each_entry_safe(filter, node,
6227 &pf->fdir_filter_list, fdir_node) {
6228 if (filter->fd_id == pf->fd_inv) {
6229 hlist_del(&filter->fdir_node);
6230 kfree(filter);
6231 pf->fdir_pf_active_filters--;
6232 }
6233 }
6234 }
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006235}
6236
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006237#define I40E_MIN_FD_FLUSH_INTERVAL 10
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006238#define I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE 30
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006239/**
6240 * i40e_fdir_flush_and_replay - Function to flush all FD filters and replay SB
6241 * @pf: board private structure
6242 **/
6243static void i40e_fdir_flush_and_replay(struct i40e_pf *pf)
6244{
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006245 unsigned long min_flush_time;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006246 int flush_wait_retry = 50;
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006247 bool disable_atr = false;
6248 int fd_room;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006249 int reg;
6250
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04006251 if (!time_after(jiffies, pf->fd_flush_timestamp +
6252 (I40E_MIN_FD_FLUSH_INTERVAL * HZ)))
6253 return;
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006254
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04006255 /* If the flush is happening too quick and we have mostly SB rules we
6256 * should not re-enable ATR for some time.
6257 */
6258 min_flush_time = pf->fd_flush_timestamp +
6259 (I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE * HZ);
6260 fd_room = pf->fdir_pf_filter_count - pf->fdir_pf_active_filters;
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006261
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04006262 if (!(time_after(jiffies, min_flush_time)) &&
6263 (fd_room < I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) {
6264 if (I40E_DEBUG_FD & pf->hw.debug_mask)
6265 dev_info(&pf->pdev->dev, "ATR disabled, not enough FD filter space.\n");
6266 disable_atr = true;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006267 }
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04006268
6269 pf->fd_flush_timestamp = jiffies;
Jacob Keller47994c12017-04-19 09:25:57 -04006270 pf->flags |= I40E_FLAG_FD_ATR_AUTO_DISABLED;
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04006271 /* flush all filters */
6272 wr32(&pf->hw, I40E_PFQF_CTL_1,
6273 I40E_PFQF_CTL_1_CLEARFDTABLE_MASK);
6274 i40e_flush(&pf->hw);
6275 pf->fd_flush_cnt++;
6276 pf->fd_add_err = 0;
6277 do {
6278 /* Check FD flush status every 5-6msec */
6279 usleep_range(5000, 6000);
6280 reg = rd32(&pf->hw, I40E_PFQF_CTL_1);
6281 if (!(reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK))
6282 break;
6283 } while (flush_wait_retry--);
6284 if (reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK) {
6285 dev_warn(&pf->pdev->dev, "FD table did not flush, needs more time\n");
6286 } else {
6287 /* replay sideband filters */
6288 i40e_fdir_filter_restore(pf->vsi[pf->lan_vsi]);
Jacob Keller097dbf52017-02-06 14:38:46 -08006289 if (!disable_atr && !pf->fd_tcp4_filter_cnt)
Jacob Keller47994c12017-04-19 09:25:57 -04006290 pf->flags &= ~I40E_FLAG_FD_ATR_AUTO_DISABLED;
Jacob Keller0da36b92017-04-19 09:25:55 -04006291 clear_bit(__I40E_FD_FLUSH_REQUESTED, pf->state);
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04006292 if (I40E_DEBUG_FD & pf->hw.debug_mask)
6293 dev_info(&pf->pdev->dev, "FD Filter table flushed and FD-SB replayed.\n");
6294 }
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006295}
6296
6297/**
6298 * i40e_get_current_atr_count - Get the count of total FD ATR filters programmed
6299 * @pf: board private structure
6300 **/
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006301u32 i40e_get_current_atr_cnt(struct i40e_pf *pf)
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006302{
6303 return i40e_get_current_fd_count(pf) - pf->fdir_pf_active_filters;
6304}
6305
6306/* We can see up to 256 filter programming desc in transit if the filters are
6307 * being applied really fast; before we see the first
6308 * filter miss error on Rx queue 0. Accumulating enough error messages before
6309 * reacting will make sure we don't cause flush too often.
6310 */
6311#define I40E_MAX_FD_PROGRAM_ERROR 256
6312
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006313/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006314 * i40e_fdir_reinit_subtask - Worker thread to reinit FDIR filter table
6315 * @pf: board private structure
6316 **/
6317static void i40e_fdir_reinit_subtask(struct i40e_pf *pf)
6318{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006319
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006320 /* if interface is down do nothing */
Mauro S. M. Rodrigues9e6c9c02017-05-12 23:26:56 -03006321 if (test_bit(__I40E_DOWN, pf->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006322 return;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006323
Jacob Keller0da36b92017-04-19 09:25:55 -04006324 if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006325 i40e_fdir_flush_and_replay(pf);
6326
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006327 i40e_fdir_check_and_reenable(pf);
6328
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006329}
6330
6331/**
6332 * i40e_vsi_link_event - notify VSI of a link event
6333 * @vsi: vsi to be notified
6334 * @link_up: link up or down
6335 **/
6336static void i40e_vsi_link_event(struct i40e_vsi *vsi, bool link_up)
6337{
Jacob Keller0da36b92017-04-19 09:25:55 -04006338 if (!vsi || test_bit(__I40E_VSI_DOWN, vsi->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006339 return;
6340
6341 switch (vsi->type) {
6342 case I40E_VSI_MAIN:
6343 if (!vsi->netdev || !vsi->netdev_registered)
6344 break;
6345
6346 if (link_up) {
6347 netif_carrier_on(vsi->netdev);
6348 netif_tx_wake_all_queues(vsi->netdev);
6349 } else {
6350 netif_carrier_off(vsi->netdev);
6351 netif_tx_stop_all_queues(vsi->netdev);
6352 }
6353 break;
6354
6355 case I40E_VSI_SRIOV:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006356 case I40E_VSI_VMDQ2:
6357 case I40E_VSI_CTRL:
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06006358 case I40E_VSI_IWARP:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006359 case I40E_VSI_MIRROR:
6360 default:
6361 /* there is no notification for other VSIs */
6362 break;
6363 }
6364}
6365
6366/**
6367 * i40e_veb_link_event - notify elements on the veb of a link event
6368 * @veb: veb to be notified
6369 * @link_up: link up or down
6370 **/
6371static void i40e_veb_link_event(struct i40e_veb *veb, bool link_up)
6372{
6373 struct i40e_pf *pf;
6374 int i;
6375
6376 if (!veb || !veb->pf)
6377 return;
6378 pf = veb->pf;
6379
6380 /* depth first... */
6381 for (i = 0; i < I40E_MAX_VEB; i++)
6382 if (pf->veb[i] && (pf->veb[i]->uplink_seid == veb->seid))
6383 i40e_veb_link_event(pf->veb[i], link_up);
6384
6385 /* ... now the local VSIs */
Mitch Williams505682c2014-05-20 08:01:37 +00006386 for (i = 0; i < pf->num_alloc_vsi; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006387 if (pf->vsi[i] && (pf->vsi[i]->uplink_seid == veb->seid))
6388 i40e_vsi_link_event(pf->vsi[i], link_up);
6389}
6390
6391/**
6392 * i40e_link_event - Update netif_carrier status
6393 * @pf: board private structure
6394 **/
6395static void i40e_link_event(struct i40e_pf *pf)
6396{
Mitch Williams320684c2014-10-17 03:14:43 +00006397 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
Catherine Sullivanfef59dd2014-12-11 07:06:33 +00006398 u8 new_link_speed, old_link_speed;
Jesse Brandeburga72a5abc2015-08-26 15:14:19 -04006399 i40e_status status;
6400 bool new_link, old_link;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006401
Catherine Sullivan1f9610e2015-10-21 19:47:09 -04006402 /* save off old link status information */
6403 pf->hw.phy.link_info_old = pf->hw.phy.link_info;
6404
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00006405 /* set this to force the get_link_status call to refresh state */
6406 pf->hw.phy.get_link_info = true;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006407
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00006408 old_link = (pf->hw.phy.link_info_old.link_info & I40E_AQ_LINK_UP);
Jesse Brandeburga72a5abc2015-08-26 15:14:19 -04006409
6410 status = i40e_get_link_status(&pf->hw, &new_link);
Harshitha Ramamurthyae136702016-12-12 15:44:16 -08006411
6412 /* On success, disable temp link polling */
6413 if (status == I40E_SUCCESS) {
6414 if (pf->flags & I40E_FLAG_TEMP_LINK_POLLING)
6415 pf->flags &= ~I40E_FLAG_TEMP_LINK_POLLING;
6416 } else {
6417 /* Enable link polling temporarily until i40e_get_link_status
6418 * returns I40E_SUCCESS
6419 */
6420 pf->flags |= I40E_FLAG_TEMP_LINK_POLLING;
Jesse Brandeburga72a5abc2015-08-26 15:14:19 -04006421 dev_dbg(&pf->pdev->dev, "couldn't get link state, status: %d\n",
6422 status);
6423 return;
6424 }
6425
Catherine Sullivanfef59dd2014-12-11 07:06:33 +00006426 old_link_speed = pf->hw.phy.link_info_old.link_speed;
6427 new_link_speed = pf->hw.phy.link_info.link_speed;
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00006428
6429 if (new_link == old_link &&
Catherine Sullivanfef59dd2014-12-11 07:06:33 +00006430 new_link_speed == old_link_speed &&
Jacob Keller0da36b92017-04-19 09:25:55 -04006431 (test_bit(__I40E_VSI_DOWN, vsi->state) ||
Mitch Williams320684c2014-10-17 03:14:43 +00006432 new_link == netif_carrier_ok(vsi->netdev)))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006433 return;
Mitch Williams320684c2014-10-17 03:14:43 +00006434
Sudheer Mogilappagari9a034492017-07-14 09:10:16 -04006435 i40e_print_link_message(vsi, new_link);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006436
6437 /* Notify the base of the switch tree connected to
6438 * the link. Floating VEBs are not notified.
6439 */
6440 if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
6441 i40e_veb_link_event(pf->veb[pf->lan_veb], new_link);
6442 else
Mitch Williams320684c2014-10-17 03:14:43 +00006443 i40e_vsi_link_event(vsi, new_link);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006444
6445 if (pf->vf)
6446 i40e_vc_notify_link_state(pf);
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00006447
6448 if (pf->flags & I40E_FLAG_PTP)
6449 i40e_ptp_set_increment(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006450}
6451
6452/**
Shannon Nelson21536712014-10-25 10:35:25 +00006453 * i40e_watchdog_subtask - periodic checks not using event driven response
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006454 * @pf: board private structure
6455 **/
6456static void i40e_watchdog_subtask(struct i40e_pf *pf)
6457{
6458 int i;
6459
6460 /* if interface is down do nothing */
Mauro S. M. Rodrigues9e6c9c02017-05-12 23:26:56 -03006461 if (test_bit(__I40E_DOWN, pf->state) ||
Jacob Keller0da36b92017-04-19 09:25:55 -04006462 test_bit(__I40E_CONFIG_BUSY, pf->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006463 return;
6464
Shannon Nelson21536712014-10-25 10:35:25 +00006465 /* make sure we don't do these things too often */
6466 if (time_before(jiffies, (pf->service_timer_previous +
6467 pf->service_timer_period)))
6468 return;
6469 pf->service_timer_previous = jiffies;
6470
Harshitha Ramamurthyae136702016-12-12 15:44:16 -08006471 if ((pf->flags & I40E_FLAG_LINK_POLLING_ENABLED) ||
6472 (pf->flags & I40E_FLAG_TEMP_LINK_POLLING))
Shannon Nelson9ac77262015-08-27 11:42:40 -04006473 i40e_link_event(pf);
Shannon Nelson21536712014-10-25 10:35:25 +00006474
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006475 /* Update the stats for active netdevs so the network stack
6476 * can look at updated numbers whenever it cares to
6477 */
Mitch Williams505682c2014-05-20 08:01:37 +00006478 for (i = 0; i < pf->num_alloc_vsi; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006479 if (pf->vsi[i] && pf->vsi[i]->netdev)
6480 i40e_update_stats(pf->vsi[i]);
6481
Anjali Singhai Jaind1a8d272015-07-23 16:54:40 -04006482 if (pf->flags & I40E_FLAG_VEB_STATS_ENABLED) {
6483 /* Update the stats for the active switching components */
6484 for (i = 0; i < I40E_MAX_VEB; i++)
6485 if (pf->veb[i])
6486 i40e_update_veb_stats(pf->veb[i]);
6487 }
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00006488
Jacob Keller61189552017-05-03 10:29:01 -07006489 i40e_ptp_rx_hang(pf);
Jacob Keller0bc07062017-05-03 10:29:02 -07006490 i40e_ptp_tx_hang(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006491}
6492
6493/**
6494 * i40e_reset_subtask - Set up for resetting the device and driver
6495 * @pf: board private structure
6496 **/
6497static void i40e_reset_subtask(struct i40e_pf *pf)
6498{
6499 u32 reset_flags = 0;
6500
Jacob Keller0da36b92017-04-19 09:25:55 -04006501 if (test_bit(__I40E_REINIT_REQUESTED, pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006502 reset_flags |= BIT(__I40E_REINIT_REQUESTED);
Jacob Keller0da36b92017-04-19 09:25:55 -04006503 clear_bit(__I40E_REINIT_REQUESTED, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006504 }
Jacob Keller0da36b92017-04-19 09:25:55 -04006505 if (test_bit(__I40E_PF_RESET_REQUESTED, pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006506 reset_flags |= BIT(__I40E_PF_RESET_REQUESTED);
Jacob Keller0da36b92017-04-19 09:25:55 -04006507 clear_bit(__I40E_PF_RESET_REQUESTED, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006508 }
Jacob Keller0da36b92017-04-19 09:25:55 -04006509 if (test_bit(__I40E_CORE_RESET_REQUESTED, pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006510 reset_flags |= BIT(__I40E_CORE_RESET_REQUESTED);
Jacob Keller0da36b92017-04-19 09:25:55 -04006511 clear_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006512 }
Jacob Keller0da36b92017-04-19 09:25:55 -04006513 if (test_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006514 reset_flags |= BIT(__I40E_GLOBAL_RESET_REQUESTED);
Jacob Keller0da36b92017-04-19 09:25:55 -04006515 clear_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006516 }
Mauro S. M. Rodrigues9e6c9c02017-05-12 23:26:56 -03006517 if (test_bit(__I40E_DOWN_REQUESTED, pf->state)) {
6518 reset_flags |= BIT(__I40E_DOWN_REQUESTED);
6519 clear_bit(__I40E_DOWN_REQUESTED, pf->state);
Neerav Parikhb5d06f02014-06-03 23:50:17 +00006520 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006521
6522 /* If there's a recovery already waiting, it takes
6523 * precedence before starting a new reset sequence.
6524 */
Jacob Keller0da36b92017-04-19 09:25:55 -04006525 if (test_bit(__I40E_RESET_INTR_RECEIVED, pf->state)) {
Maciej Sosin373149f2017-04-05 07:50:55 -04006526 i40e_prep_for_reset(pf, false);
6527 i40e_reset(pf);
6528 i40e_rebuild(pf, false, false);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006529 }
6530
6531 /* If we're already down or resetting, just bail */
6532 if (reset_flags &&
Mauro S. M. Rodrigues9e6c9c02017-05-12 23:26:56 -03006533 !test_bit(__I40E_DOWN, pf->state) &&
Jacob Keller0da36b92017-04-19 09:25:55 -04006534 !test_bit(__I40E_CONFIG_BUSY, pf->state)) {
Jacob Kellerdfc4ff62017-06-07 05:43:13 -04006535 i40e_do_reset(pf, reset_flags, false);
Maciej Sosin373149f2017-04-05 07:50:55 -04006536 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006537}
6538
6539/**
6540 * i40e_handle_link_event - Handle link event
6541 * @pf: board private structure
6542 * @e: event info posted on ARQ
6543 **/
6544static void i40e_handle_link_event(struct i40e_pf *pf,
6545 struct i40e_arq_event_info *e)
6546{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006547 struct i40e_aqc_get_link_status *status =
6548 (struct i40e_aqc_get_link_status *)&e->desc.params.raw;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006549
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00006550 /* Do a new status request to re-enable LSE reporting
6551 * and load new status information into the hw struct
6552 * This completely ignores any state information
6553 * in the ARQ event info, instead choosing to always
6554 * issue the AQ update link status command.
6555 */
6556 i40e_link_event(pf);
6557
Carolyn Wyborny7b592f62014-07-10 07:58:19 +00006558 /* check for unqualified module, if link is down */
6559 if ((status->link_info & I40E_AQ_MEDIA_AVAILABLE) &&
6560 (!(status->an_info & I40E_AQ_QUALIFIED_MODULE)) &&
6561 (!(status->link_info & I40E_AQ_LINK_UP)))
6562 dev_err(&pf->pdev->dev,
6563 "The driver failed to link because an unqualified module was detected.\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006564}
6565
6566/**
6567 * i40e_clean_adminq_subtask - Clean the AdminQ rings
6568 * @pf: board private structure
6569 **/
6570static void i40e_clean_adminq_subtask(struct i40e_pf *pf)
6571{
6572 struct i40e_arq_event_info event;
6573 struct i40e_hw *hw = &pf->hw;
6574 u16 pending, i = 0;
6575 i40e_status ret;
6576 u16 opcode;
Shannon Nelson86df2422014-05-20 08:01:35 +00006577 u32 oldval;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006578 u32 val;
6579
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00006580 /* Do not run clean AQ when PF reset fails */
Jacob Keller0da36b92017-04-19 09:25:55 -04006581 if (test_bit(__I40E_RESET_FAILED, pf->state))
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00006582 return;
6583
Shannon Nelson86df2422014-05-20 08:01:35 +00006584 /* check for error indications */
6585 val = rd32(&pf->hw, pf->hw.aq.arq.len);
6586 oldval = val;
6587 if (val & I40E_PF_ARQLEN_ARQVFE_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006588 if (hw->debug_mask & I40E_DEBUG_AQ)
6589 dev_info(&pf->pdev->dev, "ARQ VF Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006590 val &= ~I40E_PF_ARQLEN_ARQVFE_MASK;
6591 }
6592 if (val & I40E_PF_ARQLEN_ARQOVFL_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006593 if (hw->debug_mask & I40E_DEBUG_AQ)
6594 dev_info(&pf->pdev->dev, "ARQ Overflow Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006595 val &= ~I40E_PF_ARQLEN_ARQOVFL_MASK;
Mitch Williams1d0a4ad2015-12-23 12:05:48 -08006596 pf->arq_overflows++;
Shannon Nelson86df2422014-05-20 08:01:35 +00006597 }
6598 if (val & I40E_PF_ARQLEN_ARQCRIT_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006599 if (hw->debug_mask & I40E_DEBUG_AQ)
6600 dev_info(&pf->pdev->dev, "ARQ Critical Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006601 val &= ~I40E_PF_ARQLEN_ARQCRIT_MASK;
6602 }
6603 if (oldval != val)
6604 wr32(&pf->hw, pf->hw.aq.arq.len, val);
6605
6606 val = rd32(&pf->hw, pf->hw.aq.asq.len);
6607 oldval = val;
6608 if (val & I40E_PF_ATQLEN_ATQVFE_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006609 if (pf->hw.debug_mask & I40E_DEBUG_AQ)
6610 dev_info(&pf->pdev->dev, "ASQ VF Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006611 val &= ~I40E_PF_ATQLEN_ATQVFE_MASK;
6612 }
6613 if (val & I40E_PF_ATQLEN_ATQOVFL_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006614 if (pf->hw.debug_mask & I40E_DEBUG_AQ)
6615 dev_info(&pf->pdev->dev, "ASQ Overflow Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006616 val &= ~I40E_PF_ATQLEN_ATQOVFL_MASK;
6617 }
6618 if (val & I40E_PF_ATQLEN_ATQCRIT_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006619 if (pf->hw.debug_mask & I40E_DEBUG_AQ)
6620 dev_info(&pf->pdev->dev, "ASQ Critical Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006621 val &= ~I40E_PF_ATQLEN_ATQCRIT_MASK;
6622 }
6623 if (oldval != val)
6624 wr32(&pf->hw, pf->hw.aq.asq.len, val);
6625
Mitch Williams1001dc32014-11-11 20:02:19 +00006626 event.buf_len = I40E_MAX_AQ_BUF_SIZE;
6627 event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006628 if (!event.msg_buf)
6629 return;
6630
6631 do {
6632 ret = i40e_clean_arq_element(hw, &event, &pending);
Mitch Williams56497972014-06-04 08:45:18 +00006633 if (ret == I40E_ERR_ADMIN_QUEUE_NO_WORK)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006634 break;
Mitch Williams56497972014-06-04 08:45:18 +00006635 else if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006636 dev_info(&pf->pdev->dev, "ARQ event error %d\n", ret);
6637 break;
6638 }
6639
6640 opcode = le16_to_cpu(event.desc.opcode);
6641 switch (opcode) {
6642
6643 case i40e_aqc_opc_get_link_status:
6644 i40e_handle_link_event(pf, &event);
6645 break;
6646 case i40e_aqc_opc_send_msg_to_pf:
6647 ret = i40e_vc_process_vf_msg(pf,
6648 le16_to_cpu(event.desc.retval),
6649 le32_to_cpu(event.desc.cookie_high),
6650 le32_to_cpu(event.desc.cookie_low),
6651 event.msg_buf,
Mitch Williams1001dc32014-11-11 20:02:19 +00006652 event.msg_len);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006653 break;
6654 case i40e_aqc_opc_lldp_update_mib:
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006655 dev_dbg(&pf->pdev->dev, "ARQ: Update LLDP MIB event received\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006656#ifdef CONFIG_I40E_DCB
6657 rtnl_lock();
6658 ret = i40e_handle_lldp_event(pf, &event);
6659 rtnl_unlock();
6660#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006661 break;
6662 case i40e_aqc_opc_event_lan_overflow:
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006663 dev_dbg(&pf->pdev->dev, "ARQ LAN queue overflow event received\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006664 i40e_handle_lan_overflow_event(pf, &event);
6665 break;
Shannon Nelson0467bc92013-12-18 13:45:58 +00006666 case i40e_aqc_opc_send_msg_to_peer:
6667 dev_info(&pf->pdev->dev, "ARQ: Msg from other pf\n");
6668 break;
Shannon Nelson91a0f932015-03-19 14:32:01 -07006669 case i40e_aqc_opc_nvm_erase:
6670 case i40e_aqc_opc_nvm_update:
Michal Kosiarz00ada502015-11-19 11:34:20 -08006671 case i40e_aqc_opc_oem_post_update:
Shannon Nelson6e93d0c2016-01-15 14:33:18 -08006672 i40e_debug(&pf->hw, I40E_DEBUG_NVM,
6673 "ARQ NVM operation 0x%04x completed\n",
6674 opcode);
Shannon Nelson91a0f932015-03-19 14:32:01 -07006675 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006676 default:
6677 dev_info(&pf->pdev->dev,
Shannon Nelson56e5ca62016-03-10 14:59:48 -08006678 "ARQ: Unknown event 0x%04x ignored\n",
Shannon Nelson0467bc92013-12-18 13:45:58 +00006679 opcode);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006680 break;
6681 }
Christopher N Bednarz1fca3262017-03-10 12:22:03 -08006682 } while (i++ < pf->adminq_work_limit);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006683
Christopher N Bednarz1fca3262017-03-10 12:22:03 -08006684 if (i < pf->adminq_work_limit)
Jacob Keller0da36b92017-04-19 09:25:55 -04006685 clear_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state);
Christopher N Bednarz1fca3262017-03-10 12:22:03 -08006686
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006687 /* re-enable Admin queue interrupt cause */
6688 val = rd32(hw, I40E_PFINT_ICR0_ENA);
6689 val |= I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
6690 wr32(hw, I40E_PFINT_ICR0_ENA, val);
6691 i40e_flush(hw);
6692
6693 kfree(event.msg_buf);
6694}
6695
6696/**
Shannon Nelson4eb3f762014-03-06 08:59:58 +00006697 * i40e_verify_eeprom - make sure eeprom is good to use
6698 * @pf: board private structure
6699 **/
6700static void i40e_verify_eeprom(struct i40e_pf *pf)
6701{
6702 int err;
6703
6704 err = i40e_diag_eeprom_test(&pf->hw);
6705 if (err) {
6706 /* retry in case of garbage read */
6707 err = i40e_diag_eeprom_test(&pf->hw);
6708 if (err) {
6709 dev_info(&pf->pdev->dev, "eeprom check failed (%d), Tx/Rx traffic disabled\n",
6710 err);
Jacob Keller0da36b92017-04-19 09:25:55 -04006711 set_bit(__I40E_BAD_EEPROM, pf->state);
Shannon Nelson4eb3f762014-03-06 08:59:58 +00006712 }
6713 }
6714
Jacob Keller0da36b92017-04-19 09:25:55 -04006715 if (!err && test_bit(__I40E_BAD_EEPROM, pf->state)) {
Shannon Nelson4eb3f762014-03-06 08:59:58 +00006716 dev_info(&pf->pdev->dev, "eeprom check passed, Tx/Rx traffic enabled\n");
Jacob Keller0da36b92017-04-19 09:25:55 -04006717 clear_bit(__I40E_BAD_EEPROM, pf->state);
Shannon Nelson4eb3f762014-03-06 08:59:58 +00006718 }
6719}
6720
6721/**
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006722 * i40e_enable_pf_switch_lb
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00006723 * @pf: pointer to the PF structure
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006724 *
6725 * enable switch loop back or die - no point in a return value
6726 **/
6727static void i40e_enable_pf_switch_lb(struct i40e_pf *pf)
6728{
6729 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
6730 struct i40e_vsi_context ctxt;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006731 int ret;
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006732
6733 ctxt.seid = pf->main_vsi_seid;
6734 ctxt.pf_num = pf->hw.pf_id;
6735 ctxt.vf_num = 0;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006736 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
6737 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006738 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006739 "couldn't get PF vsi config, err %s aq_err %s\n",
6740 i40e_stat_str(&pf->hw, ret),
6741 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006742 return;
6743 }
6744 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
6745 ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
6746 ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
6747
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006748 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
6749 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006750 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006751 "update vsi switch failed, err %s aq_err %s\n",
6752 i40e_stat_str(&pf->hw, ret),
6753 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006754 }
6755}
6756
6757/**
6758 * i40e_disable_pf_switch_lb
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00006759 * @pf: pointer to the PF structure
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006760 *
6761 * disable switch loop back or die - no point in a return value
6762 **/
6763static void i40e_disable_pf_switch_lb(struct i40e_pf *pf)
6764{
6765 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
6766 struct i40e_vsi_context ctxt;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006767 int ret;
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006768
6769 ctxt.seid = pf->main_vsi_seid;
6770 ctxt.pf_num = pf->hw.pf_id;
6771 ctxt.vf_num = 0;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006772 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
6773 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006774 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006775 "couldn't get PF vsi config, err %s aq_err %s\n",
6776 i40e_stat_str(&pf->hw, ret),
6777 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006778 return;
6779 }
6780 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
6781 ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
6782 ctxt.info.switch_id &= ~cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
6783
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006784 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
6785 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006786 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006787 "update vsi switch failed, err %s aq_err %s\n",
6788 i40e_stat_str(&pf->hw, ret),
6789 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006790 }
6791}
6792
6793/**
Neerav Parikh51616012015-02-06 08:52:14 +00006794 * i40e_config_bridge_mode - Configure the HW bridge mode
6795 * @veb: pointer to the bridge instance
6796 *
6797 * Configure the loop back mode for the LAN VSI that is downlink to the
6798 * specified HW bridge instance. It is expected this function is called
6799 * when a new HW bridge is instantiated.
6800 **/
6801static void i40e_config_bridge_mode(struct i40e_veb *veb)
6802{
6803 struct i40e_pf *pf = veb->pf;
6804
Shannon Nelson6dec1012015-09-28 14:12:30 -04006805 if (pf->hw.debug_mask & I40E_DEBUG_LAN)
6806 dev_info(&pf->pdev->dev, "enabling bridge mode: %s\n",
6807 veb->bridge_mode == BRIDGE_MODE_VEPA ? "VEPA" : "VEB");
Neerav Parikh51616012015-02-06 08:52:14 +00006808 if (veb->bridge_mode & BRIDGE_MODE_VEPA)
6809 i40e_disable_pf_switch_lb(pf);
6810 else
6811 i40e_enable_pf_switch_lb(pf);
6812}
6813
6814/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006815 * i40e_reconstitute_veb - rebuild the VEB and anything connected to it
6816 * @veb: pointer to the VEB instance
6817 *
6818 * This is a recursive function that first builds the attached VSIs then
6819 * recurses in to build the next layer of VEB. We track the connections
6820 * through our own index numbers because the seid's from the HW could
6821 * change across the reset.
6822 **/
6823static int i40e_reconstitute_veb(struct i40e_veb *veb)
6824{
6825 struct i40e_vsi *ctl_vsi = NULL;
6826 struct i40e_pf *pf = veb->pf;
6827 int v, veb_idx;
6828 int ret;
6829
6830 /* build VSI that owns this VEB, temporarily attached to base VEB */
Mitch Williams505682c2014-05-20 08:01:37 +00006831 for (v = 0; v < pf->num_alloc_vsi && !ctl_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006832 if (pf->vsi[v] &&
6833 pf->vsi[v]->veb_idx == veb->idx &&
6834 pf->vsi[v]->flags & I40E_VSI_FLAG_VEB_OWNER) {
6835 ctl_vsi = pf->vsi[v];
6836 break;
6837 }
6838 }
6839 if (!ctl_vsi) {
6840 dev_info(&pf->pdev->dev,
6841 "missing owner VSI for veb_idx %d\n", veb->idx);
6842 ret = -ENOENT;
6843 goto end_reconstitute;
6844 }
6845 if (ctl_vsi != pf->vsi[pf->lan_vsi])
6846 ctl_vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
6847 ret = i40e_add_vsi(ctl_vsi);
6848 if (ret) {
6849 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006850 "rebuild of veb_idx %d owner VSI failed: %d\n",
6851 veb->idx, ret);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006852 goto end_reconstitute;
6853 }
6854 i40e_vsi_reset_stats(ctl_vsi);
6855
6856 /* create the VEB in the switch and move the VSI onto the VEB */
6857 ret = i40e_add_veb(veb, ctl_vsi);
6858 if (ret)
6859 goto end_reconstitute;
6860
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07006861 if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
6862 veb->bridge_mode = BRIDGE_MODE_VEB;
6863 else
6864 veb->bridge_mode = BRIDGE_MODE_VEPA;
Neerav Parikh51616012015-02-06 08:52:14 +00006865 i40e_config_bridge_mode(veb);
Anjali Singhai Jainb64ba082014-11-13 03:06:15 +00006866
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006867 /* create the remaining VSIs attached to this VEB */
Mitch Williams505682c2014-05-20 08:01:37 +00006868 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006869 if (!pf->vsi[v] || pf->vsi[v] == ctl_vsi)
6870 continue;
6871
6872 if (pf->vsi[v]->veb_idx == veb->idx) {
6873 struct i40e_vsi *vsi = pf->vsi[v];
Jesse Brandeburg6995b362015-08-28 17:55:54 -04006874
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006875 vsi->uplink_seid = veb->seid;
6876 ret = i40e_add_vsi(vsi);
6877 if (ret) {
6878 dev_info(&pf->pdev->dev,
6879 "rebuild of vsi_idx %d failed: %d\n",
6880 v, ret);
6881 goto end_reconstitute;
6882 }
6883 i40e_vsi_reset_stats(vsi);
6884 }
6885 }
6886
6887 /* create any VEBs attached to this VEB - RECURSION */
6888 for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
6889 if (pf->veb[veb_idx] && pf->veb[veb_idx]->veb_idx == veb->idx) {
6890 pf->veb[veb_idx]->uplink_seid = veb->seid;
6891 ret = i40e_reconstitute_veb(pf->veb[veb_idx]);
6892 if (ret)
6893 break;
6894 }
6895 }
6896
6897end_reconstitute:
6898 return ret;
6899}
6900
6901/**
6902 * i40e_get_capabilities - get info about the HW
6903 * @pf: the PF struct
6904 **/
6905static int i40e_get_capabilities(struct i40e_pf *pf)
6906{
6907 struct i40e_aqc_list_capabilities_element_resp *cap_buf;
6908 u16 data_size;
6909 int buf_len;
6910 int err;
6911
6912 buf_len = 40 * sizeof(struct i40e_aqc_list_capabilities_element_resp);
6913 do {
6914 cap_buf = kzalloc(buf_len, GFP_KERNEL);
6915 if (!cap_buf)
6916 return -ENOMEM;
6917
6918 /* this loads the data into the hw struct for us */
6919 err = i40e_aq_discover_capabilities(&pf->hw, cap_buf, buf_len,
6920 &data_size,
6921 i40e_aqc_opc_list_func_capabilities,
6922 NULL);
6923 /* data loaded, buffer no longer needed */
6924 kfree(cap_buf);
6925
6926 if (pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOMEM) {
6927 /* retry with a larger buffer */
6928 buf_len = data_size;
6929 } else if (pf->hw.aq.asq_last_status != I40E_AQ_RC_OK) {
6930 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006931 "capability discovery failed, err %s aq_err %s\n",
6932 i40e_stat_str(&pf->hw, err),
6933 i40e_aq_str(&pf->hw,
6934 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006935 return -ENODEV;
6936 }
6937 } while (err);
6938
6939 if (pf->hw.debug_mask & I40E_DEBUG_USER)
6940 dev_info(&pf->pdev->dev,
6941 "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",
6942 pf->hw.pf_id, pf->hw.func_caps.num_vfs,
6943 pf->hw.func_caps.num_msix_vectors,
6944 pf->hw.func_caps.num_msix_vectors_vf,
6945 pf->hw.func_caps.fd_filters_guaranteed,
6946 pf->hw.func_caps.fd_filters_best_effort,
6947 pf->hw.func_caps.num_tx_qp,
6948 pf->hw.func_caps.num_vsis);
6949
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00006950#define DEF_NUM_VSI (1 + (pf->hw.func_caps.fcoe ? 1 : 0) \
6951 + pf->hw.func_caps.num_vfs)
6952 if (pf->hw.revision_id == 0 && (DEF_NUM_VSI > pf->hw.func_caps.num_vsis)) {
6953 dev_info(&pf->pdev->dev,
6954 "got num_vsis %d, setting num_vsis to %d\n",
6955 pf->hw.func_caps.num_vsis, DEF_NUM_VSI);
6956 pf->hw.func_caps.num_vsis = DEF_NUM_VSI;
6957 }
6958
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006959 return 0;
6960}
6961
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006962static int i40e_vsi_clear(struct i40e_vsi *vsi);
6963
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006964/**
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006965 * i40e_fdir_sb_setup - initialize the Flow Director resources for Sideband
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006966 * @pf: board private structure
6967 **/
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006968static void i40e_fdir_sb_setup(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006969{
6970 struct i40e_vsi *vsi;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006971
Jesse Brandeburg407e0632014-06-03 23:50:12 +00006972 /* quick workaround for an NVM issue that leaves a critical register
6973 * uninitialized
6974 */
6975 if (!rd32(&pf->hw, I40E_GLQF_HKEY(0))) {
6976 static const u32 hkey[] = {
6977 0xe640d33f, 0xcdfe98ab, 0x73fa7161, 0x0d7a7d36,
6978 0xeacb7d61, 0xaa4f05b6, 0x9c5c89ed, 0xfc425ddb,
6979 0xa4654832, 0xfc7461d4, 0x8f827619, 0xf5c63c21,
6980 0x95b3a76d};
Alexander Duyck4b816442016-10-11 15:26:53 -07006981 int i;
Jesse Brandeburg407e0632014-06-03 23:50:12 +00006982
6983 for (i = 0; i <= I40E_GLQF_HKEY_MAX_INDEX; i++)
6984 wr32(&pf->hw, I40E_GLQF_HKEY(i), hkey[i]);
6985 }
6986
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006987 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006988 return;
6989
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006990 /* find existing VSI and see if it needs configuring */
Alexander Duyck4b816442016-10-11 15:26:53 -07006991 vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006992
6993 /* create a new VSI if none exists */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006994 if (!vsi) {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006995 vsi = i40e_vsi_setup(pf, I40E_VSI_FDIR,
6996 pf->vsi[pf->lan_vsi]->seid, 0);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006997 if (!vsi) {
6998 dev_info(&pf->pdev->dev, "Couldn't create FDir VSI\n");
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00006999 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
7000 return;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007001 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007002 }
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00007003
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08007004 i40e_vsi_setup_irqhandler(vsi, i40e_fdir_clean_ring);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007005}
7006
7007/**
7008 * i40e_fdir_teardown - release the Flow Director resources
7009 * @pf: board private structure
7010 **/
7011static void i40e_fdir_teardown(struct i40e_pf *pf)
7012{
Alexander Duyck4b816442016-10-11 15:26:53 -07007013 struct i40e_vsi *vsi;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007014
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00007015 i40e_fdir_filter_exit(pf);
Alexander Duyck4b816442016-10-11 15:26:53 -07007016 vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
7017 if (vsi)
7018 i40e_vsi_release(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007019}
7020
7021/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007022 * i40e_prep_for_reset - prep for the core to reset
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007023 * @pf: board private structure
Maciej Sosin373149f2017-04-05 07:50:55 -04007024 * @lock_acquired: indicates whether or not the lock has been acquired
7025 * before this function was called.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007026 *
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00007027 * Close up the VFs and other things in prep for PF Reset.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007028 **/
Maciej Sosin373149f2017-04-05 07:50:55 -04007029static void i40e_prep_for_reset(struct i40e_pf *pf, bool lock_acquired)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007030{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007031 struct i40e_hw *hw = &pf->hw;
Shannon Nelson60442de2014-04-23 04:50:13 +00007032 i40e_status ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007033 u32 v;
7034
Jacob Keller0da36b92017-04-19 09:25:55 -04007035 clear_bit(__I40E_RESET_INTR_RECEIVED, pf->state);
7036 if (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
Shannon Nelson23cfbe02014-06-04 01:23:14 +00007037 return;
Mitch Williamsd3ce57342016-03-10 14:59:46 -08007038 if (i40e_check_asq_alive(&pf->hw))
7039 i40e_vc_notify_reset(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007040
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00007041 dev_dbg(&pf->pdev->dev, "Tearing down internal switch for reset\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007042
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007043 /* quiesce the VSIs and their queues that are not already DOWN */
Maciej Sosin373149f2017-04-05 07:50:55 -04007044 /* pf_quiesce_all_vsi modifies netdev structures -rtnl_lock needed */
7045 if (!lock_acquired)
7046 rtnl_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007047 i40e_pf_quiesce_all_vsi(pf);
Maciej Sosin373149f2017-04-05 07:50:55 -04007048 if (!lock_acquired)
7049 rtnl_unlock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007050
Mitch Williams505682c2014-05-20 08:01:37 +00007051 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007052 if (pf->vsi[v])
7053 pf->vsi[v]->seid = 0;
7054 }
7055
7056 i40e_shutdown_adminq(&pf->hw);
7057
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007058 /* call shutdown HMC */
Shannon Nelson60442de2014-04-23 04:50:13 +00007059 if (hw->hmc.hmc_obj) {
7060 ret = i40e_shutdown_lan_hmc(hw);
Shannon Nelson23cfbe02014-06-04 01:23:14 +00007061 if (ret)
Shannon Nelson60442de2014-04-23 04:50:13 +00007062 dev_warn(&pf->pdev->dev,
7063 "shutdown_lan_hmc failed: %d\n", ret);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007064 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007065}
7066
7067/**
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00007068 * i40e_send_version - update firmware with driver version
7069 * @pf: PF struct
7070 */
7071static void i40e_send_version(struct i40e_pf *pf)
7072{
7073 struct i40e_driver_version dv;
7074
7075 dv.major_version = DRV_VERSION_MAJOR;
7076 dv.minor_version = DRV_VERSION_MINOR;
7077 dv.build_version = DRV_VERSION_BUILD;
7078 dv.subbuild_version = 0;
Rickard Strandqvist35a7d802014-07-29 09:26:25 +00007079 strlcpy(dv.driver_string, DRV_VERSION, sizeof(dv.driver_string));
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00007080 i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
7081}
7082
7083/**
Filip Sadowski5bbb2e22017-06-07 05:43:09 -04007084 * i40e_get_oem_version - get OEM specific version information
7085 * @hw: pointer to the hardware structure
7086 **/
7087static void i40e_get_oem_version(struct i40e_hw *hw)
7088{
7089 u16 block_offset = 0xffff;
7090 u16 block_length = 0;
7091 u16 capabilities = 0;
7092 u16 gen_snap = 0;
7093 u16 release = 0;
7094
7095#define I40E_SR_NVM_OEM_VERSION_PTR 0x1B
7096#define I40E_NVM_OEM_LENGTH_OFFSET 0x00
7097#define I40E_NVM_OEM_CAPABILITIES_OFFSET 0x01
7098#define I40E_NVM_OEM_GEN_OFFSET 0x02
7099#define I40E_NVM_OEM_RELEASE_OFFSET 0x03
7100#define I40E_NVM_OEM_CAPABILITIES_MASK 0x000F
7101#define I40E_NVM_OEM_LENGTH 3
7102
7103 /* Check if pointer to OEM version block is valid. */
7104 i40e_read_nvm_word(hw, I40E_SR_NVM_OEM_VERSION_PTR, &block_offset);
7105 if (block_offset == 0xffff)
7106 return;
7107
7108 /* Check if OEM version block has correct length. */
7109 i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_LENGTH_OFFSET,
7110 &block_length);
7111 if (block_length < I40E_NVM_OEM_LENGTH)
7112 return;
7113
7114 /* Check if OEM version format is as expected. */
7115 i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_CAPABILITIES_OFFSET,
7116 &capabilities);
7117 if ((capabilities & I40E_NVM_OEM_CAPABILITIES_MASK) != 0)
7118 return;
7119
7120 i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_GEN_OFFSET,
7121 &gen_snap);
7122 i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_RELEASE_OFFSET,
7123 &release);
7124 hw->nvm.oem_ver = (gen_snap << I40E_OEM_SNAP_SHIFT) | release;
7125 hw->nvm.eetrack = I40E_OEM_EETRACK_ID;
7126}
7127
7128/**
Maciej Sosin373149f2017-04-05 07:50:55 -04007129 * i40e_reset - wait for core reset to finish reset, reset pf if corer not seen
7130 * @pf: board private structure
7131 **/
7132static int i40e_reset(struct i40e_pf *pf)
7133{
7134 struct i40e_hw *hw = &pf->hw;
7135 i40e_status ret;
7136
7137 ret = i40e_pf_reset(hw);
7138 if (ret) {
7139 dev_info(&pf->pdev->dev, "PF reset failed, %d\n", ret);
Jacob Keller0da36b92017-04-19 09:25:55 -04007140 set_bit(__I40E_RESET_FAILED, pf->state);
7141 clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
Maciej Sosin373149f2017-04-05 07:50:55 -04007142 } else {
7143 pf->pfr_count++;
7144 }
7145 return ret;
7146}
7147
7148/**
7149 * i40e_rebuild - rebuild using a saved config
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007150 * @pf: board private structure
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007151 * @reinit: if the Main VSI needs to re-initialized.
Maciej Sosin373149f2017-04-05 07:50:55 -04007152 * @lock_acquired: indicates whether or not the lock has been acquired
7153 * before this function was called.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007154 **/
Maciej Sosin373149f2017-04-05 07:50:55 -04007155static void i40e_rebuild(struct i40e_pf *pf, bool reinit, bool lock_acquired)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007156{
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007157 struct i40e_hw *hw = &pf->hw;
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00007158 u8 set_fc_aq_fail = 0;
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007159 i40e_status ret;
Anjali Singhai Jain4f2f017c2015-10-21 19:47:07 -04007160 u32 val;
Maciej Sosin373149f2017-04-05 07:50:55 -04007161 int v;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007162
Mauro S. M. Rodrigues9e6c9c02017-05-12 23:26:56 -03007163 if (test_bit(__I40E_DOWN, pf->state))
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00007164 goto clear_recovery;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00007165 dev_dbg(&pf->pdev->dev, "Rebuilding internal switch\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007166
7167 /* rebuild the basics for the AdminQ, HMC, and initial HW switch */
7168 ret = i40e_init_adminq(&pf->hw);
7169 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04007170 dev_info(&pf->pdev->dev, "Rebuild AdminQ failed, err %s aq_err %s\n",
7171 i40e_stat_str(&pf->hw, ret),
7172 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00007173 goto clear_recovery;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007174 }
Filip Sadowski5bbb2e22017-06-07 05:43:09 -04007175 i40e_get_oem_version(&pf->hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007176
Shannon Nelson4eb3f762014-03-06 08:59:58 +00007177 /* re-verify the eeprom if we just had an EMP reset */
Jacob Keller0da36b92017-04-19 09:25:55 -04007178 if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state))
Shannon Nelson4eb3f762014-03-06 08:59:58 +00007179 i40e_verify_eeprom(pf);
Shannon Nelson4eb3f762014-03-06 08:59:58 +00007180
Shannon Nelsone78ac4bf2014-05-10 04:49:09 +00007181 i40e_clear_pxe_mode(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007182 ret = i40e_get_capabilities(pf);
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04007183 if (ret)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007184 goto end_core_reset;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007185
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007186 ret = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
Alexander Duyckc76cb6e2017-02-21 15:55:48 -08007187 hw->func_caps.num_rx_qp, 0, 0);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007188 if (ret) {
7189 dev_info(&pf->pdev->dev, "init_lan_hmc failed: %d\n", ret);
7190 goto end_core_reset;
7191 }
7192 ret = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
7193 if (ret) {
7194 dev_info(&pf->pdev->dev, "configure_lan_hmc failed: %d\n", ret);
7195 goto end_core_reset;
7196 }
7197
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08007198#ifdef CONFIG_I40E_DCB
7199 ret = i40e_init_pf_dcb(pf);
7200 if (ret) {
Shannon Nelsonaebfc812014-12-11 07:06:38 +00007201 dev_info(&pf->pdev->dev, "DCB init failed %d, disabled\n", ret);
7202 pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
7203 /* Continue without DCB enabled */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08007204 }
7205#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007206 /* do basic switch setup */
Maciej Sosin373149f2017-04-05 07:50:55 -04007207 if (!lock_acquired)
7208 rtnl_lock();
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007209 ret = i40e_setup_pf_switch(pf, reinit);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007210 if (ret)
Maciej Sosin373149f2017-04-05 07:50:55 -04007211 goto end_unlock;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007212
Shannon Nelson2f0aff42016-01-04 10:33:08 -08007213 /* The driver only wants link up/down and module qualification
7214 * reports from firmware. Note the negative logic.
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +00007215 */
7216 ret = i40e_aq_set_phy_int_mask(&pf->hw,
Shannon Nelson2f0aff42016-01-04 10:33:08 -08007217 ~(I40E_AQ_EVENT_LINK_UPDOWN |
Shannon Nelson867a79e2016-03-18 12:18:15 -07007218 I40E_AQ_EVENT_MEDIA_NA |
Shannon Nelson2f0aff42016-01-04 10:33:08 -08007219 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +00007220 if (ret)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04007221 dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
7222 i40e_stat_str(&pf->hw, ret),
7223 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +00007224
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00007225 /* make sure our flow control settings are restored */
7226 ret = i40e_set_fc(&pf->hw, &set_fc_aq_fail, true);
7227 if (ret)
Neerav Parikh8279e492015-09-03 17:18:50 -04007228 dev_dbg(&pf->pdev->dev, "setting flow control: ret = %s last_status = %s\n",
7229 i40e_stat_str(&pf->hw, ret),
7230 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00007231
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007232 /* Rebuild the VSIs and VEBs that existed before reset.
7233 * They are still in our local switch element arrays, so only
7234 * need to rebuild the switch model in the HW.
7235 *
7236 * If there were VEBs but the reconstitution failed, we'll try
7237 * try to recover minimal use by getting the basic PF VSI working.
7238 */
7239 if (pf->vsi[pf->lan_vsi]->uplink_seid != pf->mac_seid) {
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00007240 dev_dbg(&pf->pdev->dev, "attempting to rebuild switch\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007241 /* find the one VEB connected to the MAC, and find orphans */
7242 for (v = 0; v < I40E_MAX_VEB; v++) {
7243 if (!pf->veb[v])
7244 continue;
7245
7246 if (pf->veb[v]->uplink_seid == pf->mac_seid ||
7247 pf->veb[v]->uplink_seid == 0) {
7248 ret = i40e_reconstitute_veb(pf->veb[v]);
7249
7250 if (!ret)
7251 continue;
7252
7253 /* If Main VEB failed, we're in deep doodoo,
7254 * so give up rebuilding the switch and set up
7255 * for minimal rebuild of PF VSI.
7256 * If orphan failed, we'll report the error
7257 * but try to keep going.
7258 */
7259 if (pf->veb[v]->uplink_seid == pf->mac_seid) {
7260 dev_info(&pf->pdev->dev,
7261 "rebuild of switch failed: %d, will try to set up simple PF connection\n",
7262 ret);
7263 pf->vsi[pf->lan_vsi]->uplink_seid
7264 = pf->mac_seid;
7265 break;
7266 } else if (pf->veb[v]->uplink_seid == 0) {
7267 dev_info(&pf->pdev->dev,
7268 "rebuild of orphan VEB failed: %d\n",
7269 ret);
7270 }
7271 }
7272 }
7273 }
7274
7275 if (pf->vsi[pf->lan_vsi]->uplink_seid == pf->mac_seid) {
Shannon Nelsoncde4cbc2014-06-04 01:23:17 +00007276 dev_dbg(&pf->pdev->dev, "attempting to rebuild PF VSI\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007277 /* no VEB, so rebuild only the Main VSI */
7278 ret = i40e_add_vsi(pf->vsi[pf->lan_vsi]);
7279 if (ret) {
7280 dev_info(&pf->pdev->dev,
7281 "rebuild of Main VSI failed: %d\n", ret);
Maciej Sosin373149f2017-04-05 07:50:55 -04007282 goto end_unlock;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007283 }
7284 }
7285
Anjali Singhai Jain4f2f017c2015-10-21 19:47:07 -04007286 /* Reconfigure hardware for allowing smaller MSS in the case
7287 * of TSO, so that we avoid the MDD being fired and causing
7288 * a reset in the case of small MSS+TSO.
7289 */
7290#define I40E_REG_MSS 0x000E64DC
7291#define I40E_REG_MSS_MIN_MASK 0x3FF0000
7292#define I40E_64BYTE_MSS 0x400000
7293 val = rd32(hw, I40E_REG_MSS);
7294 if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
7295 val &= ~I40E_REG_MSS_MIN_MASK;
7296 val |= I40E_64BYTE_MSS;
7297 wr32(hw, I40E_REG_MSS, val);
7298 }
7299
Jacob Kellerd36e41d2017-06-23 04:24:46 -04007300 if (pf->hw_features & I40E_HW_RESTART_AUTONEG) {
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +00007301 msleep(75);
7302 ret = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
7303 if (ret)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04007304 dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
7305 i40e_stat_str(&pf->hw, ret),
7306 i40e_aq_str(&pf->hw,
7307 pf->hw.aq.asq_last_status));
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00007308 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007309 /* reinit the misc interrupt */
7310 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
7311 ret = i40e_setup_misc_vector(pf);
7312
Anjali Singhai Jaine7358f52015-10-01 14:37:34 -04007313 /* Add a filter to drop all Flow control frames from any VSI from being
7314 * transmitted. By doing so we stop a malicious VF from sending out
7315 * PAUSE or PFC frames and potentially controlling traffic for other
7316 * PF/VF VSIs.
7317 * The FW can still send Flow control frames if enabled.
7318 */
7319 i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
7320 pf->main_vsi_seid);
7321
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007322 /* restart the VSIs that were rebuilt and running before the reset */
7323 i40e_pf_unquiesce_all_vsi(pf);
7324
Jacob Keller024b05f2017-04-13 04:45:46 -04007325 /* Release the RTNL lock before we start resetting VFs */
7326 if (!lock_acquired)
7327 rtnl_unlock();
7328
Jacob Kellere4b433f2017-04-13 04:45:52 -04007329 i40e_reset_all_vfs(pf, true);
Mitch Williams69f64b22014-02-13 03:48:46 -08007330
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007331 /* tell the firmware that we're starting */
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00007332 i40e_send_version(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007333
Jacob Keller024b05f2017-04-13 04:45:46 -04007334 /* We've already released the lock, so don't do it again */
7335 goto end_core_reset;
7336
Maciej Sosin373149f2017-04-05 07:50:55 -04007337end_unlock:
Jacob Keller024b05f2017-04-13 04:45:46 -04007338 if (!lock_acquired)
7339 rtnl_unlock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007340end_core_reset:
Jacob Keller0da36b92017-04-19 09:25:55 -04007341 clear_bit(__I40E_RESET_FAILED, pf->state);
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00007342clear_recovery:
Jacob Keller0da36b92017-04-19 09:25:55 -04007343 clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007344}
7345
7346/**
Maciej Sosin373149f2017-04-05 07:50:55 -04007347 * i40e_reset_and_rebuild - reset and rebuild using a saved config
7348 * @pf: board private structure
7349 * @reinit: if the Main VSI needs to re-initialized.
7350 * @lock_acquired: indicates whether or not the lock has been acquired
7351 * before this function was called.
7352 **/
7353static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit,
7354 bool lock_acquired)
7355{
7356 int ret;
7357 /* Now we wait for GRST to settle out.
7358 * We don't have to delete the VEBs or VSIs from the hw switch
7359 * because the reset will make them disappear.
7360 */
7361 ret = i40e_reset(pf);
7362 if (!ret)
7363 i40e_rebuild(pf, reinit, lock_acquired);
7364}
7365
7366/**
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00007367 * i40e_handle_reset_warning - prep for the PF to reset, reset and rebuild
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007368 * @pf: board private structure
7369 *
7370 * Close up the VFs and other things in prep for a Core Reset,
7371 * then get ready to rebuild the world.
Maciej Sosin373149f2017-04-05 07:50:55 -04007372 * @lock_acquired: indicates whether or not the lock has been acquired
7373 * before this function was called.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007374 **/
Maciej Sosin373149f2017-04-05 07:50:55 -04007375static void i40e_handle_reset_warning(struct i40e_pf *pf, bool lock_acquired)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007376{
Maciej Sosin373149f2017-04-05 07:50:55 -04007377 i40e_prep_for_reset(pf, lock_acquired);
7378 i40e_reset_and_rebuild(pf, false, lock_acquired);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007379}
7380
7381/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007382 * i40e_handle_mdd_event
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00007383 * @pf: pointer to the PF structure
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007384 *
7385 * Called from the MDD irq handler to identify possibly malicious vfs
7386 **/
7387static void i40e_handle_mdd_event(struct i40e_pf *pf)
7388{
7389 struct i40e_hw *hw = &pf->hw;
7390 bool mdd_detected = false;
Neerav Parikhdf430b12014-06-04 01:23:15 +00007391 bool pf_mdd_detected = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007392 struct i40e_vf *vf;
7393 u32 reg;
7394 int i;
7395
Jacob Keller0da36b92017-04-19 09:25:55 -04007396 if (!test_bit(__I40E_MDD_EVENT_PENDING, pf->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007397 return;
7398
7399 /* find what triggered the MDD event */
7400 reg = rd32(hw, I40E_GL_MDET_TX);
7401 if (reg & I40E_GL_MDET_TX_VALID_MASK) {
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00007402 u8 pf_num = (reg & I40E_GL_MDET_TX_PF_NUM_MASK) >>
7403 I40E_GL_MDET_TX_PF_NUM_SHIFT;
Mitch Williams2089ad02014-10-17 03:14:53 +00007404 u16 vf_num = (reg & I40E_GL_MDET_TX_VF_NUM_MASK) >>
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00007405 I40E_GL_MDET_TX_VF_NUM_SHIFT;
Dan Carpenter013f6572014-10-22 20:06:29 -07007406 u8 event = (reg & I40E_GL_MDET_TX_EVENT_MASK) >>
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00007407 I40E_GL_MDET_TX_EVENT_SHIFT;
Mitch Williams2089ad02014-10-17 03:14:53 +00007408 u16 queue = ((reg & I40E_GL_MDET_TX_QUEUE_MASK) >>
7409 I40E_GL_MDET_TX_QUEUE_SHIFT) -
7410 pf->hw.func_caps.base_queue;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007411 if (netif_msg_tx_err(pf))
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00007412 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 +00007413 event, queue, pf_num, vf_num);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007414 wr32(hw, I40E_GL_MDET_TX, 0xffffffff);
7415 mdd_detected = true;
7416 }
7417 reg = rd32(hw, I40E_GL_MDET_RX);
7418 if (reg & I40E_GL_MDET_RX_VALID_MASK) {
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00007419 u8 func = (reg & I40E_GL_MDET_RX_FUNCTION_MASK) >>
7420 I40E_GL_MDET_RX_FUNCTION_SHIFT;
Dan Carpenter013f6572014-10-22 20:06:29 -07007421 u8 event = (reg & I40E_GL_MDET_RX_EVENT_MASK) >>
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00007422 I40E_GL_MDET_RX_EVENT_SHIFT;
Mitch Williams2089ad02014-10-17 03:14:53 +00007423 u16 queue = ((reg & I40E_GL_MDET_RX_QUEUE_MASK) >>
7424 I40E_GL_MDET_RX_QUEUE_SHIFT) -
7425 pf->hw.func_caps.base_queue;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007426 if (netif_msg_rx_err(pf))
7427 dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on RX queue %d of function 0x%02x\n",
7428 event, queue, func);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007429 wr32(hw, I40E_GL_MDET_RX, 0xffffffff);
7430 mdd_detected = true;
7431 }
7432
Neerav Parikhdf430b12014-06-04 01:23:15 +00007433 if (mdd_detected) {
7434 reg = rd32(hw, I40E_PF_MDET_TX);
7435 if (reg & I40E_PF_MDET_TX_VALID_MASK) {
7436 wr32(hw, I40E_PF_MDET_TX, 0xFFFF);
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007437 dev_info(&pf->pdev->dev, "TX driver issue detected, PF reset issued\n");
Neerav Parikhdf430b12014-06-04 01:23:15 +00007438 pf_mdd_detected = true;
7439 }
7440 reg = rd32(hw, I40E_PF_MDET_RX);
7441 if (reg & I40E_PF_MDET_RX_VALID_MASK) {
7442 wr32(hw, I40E_PF_MDET_RX, 0xFFFF);
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007443 dev_info(&pf->pdev->dev, "RX driver issue detected, PF reset issued\n");
Neerav Parikhdf430b12014-06-04 01:23:15 +00007444 pf_mdd_detected = true;
7445 }
7446 /* Queue belongs to the PF, initiate a reset */
7447 if (pf_mdd_detected) {
Jacob Keller0da36b92017-04-19 09:25:55 -04007448 set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
Neerav Parikhdf430b12014-06-04 01:23:15 +00007449 i40e_service_event_schedule(pf);
7450 }
7451 }
7452
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007453 /* see if one of the VFs needs its hand slapped */
7454 for (i = 0; i < pf->num_alloc_vfs && mdd_detected; i++) {
7455 vf = &(pf->vf[i]);
7456 reg = rd32(hw, I40E_VP_MDET_TX(i));
7457 if (reg & I40E_VP_MDET_TX_VALID_MASK) {
7458 wr32(hw, I40E_VP_MDET_TX(i), 0xFFFF);
7459 vf->num_mdd_events++;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007460 dev_info(&pf->pdev->dev, "TX driver issue detected on VF %d\n",
7461 i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007462 }
7463
7464 reg = rd32(hw, I40E_VP_MDET_RX(i));
7465 if (reg & I40E_VP_MDET_RX_VALID_MASK) {
7466 wr32(hw, I40E_VP_MDET_RX(i), 0xFFFF);
7467 vf->num_mdd_events++;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007468 dev_info(&pf->pdev->dev, "RX driver issue detected on VF %d\n",
7469 i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007470 }
7471
7472 if (vf->num_mdd_events > I40E_DEFAULT_NUM_MDD_EVENTS_ALLOWED) {
7473 dev_info(&pf->pdev->dev,
7474 "Too many MDD events on VF %d, disabled\n", i);
7475 dev_info(&pf->pdev->dev,
7476 "Use PF Control I/F to re-enable the VF\n");
Jacob Keller6322e632017-04-13 04:45:54 -04007477 set_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007478 }
7479 }
7480
7481 /* re-enable mdd interrupt cause */
Jacob Keller0da36b92017-04-19 09:25:55 -04007482 clear_bit(__I40E_MDD_EVENT_PENDING, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007483 reg = rd32(hw, I40E_PFINT_ICR0_ENA);
7484 reg |= I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
7485 wr32(hw, I40E_PFINT_ICR0_ENA, reg);
7486 i40e_flush(hw);
7487}
7488
Jacob Kellerd8b2c702017-06-20 15:17:00 -07007489static const char *i40e_tunnel_name(struct i40e_udp_port_config *port)
7490{
7491 switch (port->type) {
7492 case UDP_TUNNEL_TYPE_VXLAN:
7493 return "vxlan";
7494 case UDP_TUNNEL_TYPE_GENEVE:
7495 return "geneve";
7496 default:
7497 return "unknown";
7498 }
7499}
7500
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007501/**
Alexander Duyck1f190d92017-04-19 09:25:51 -04007502 * i40e_sync_udp_filters - Trigger a sync event for existing UDP filters
7503 * @pf: board private structure
7504 **/
7505static void i40e_sync_udp_filters(struct i40e_pf *pf)
7506{
7507 int i;
7508
7509 /* loop through and set pending bit for all active UDP filters */
7510 for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
7511 if (pf->udp_ports[i].port)
7512 pf->pending_udp_bitmap |= BIT_ULL(i);
7513 }
7514
7515 pf->flags |= I40E_FLAG_UDP_FILTER_SYNC;
7516}
7517
7518/**
Singhai, Anjali6a899022015-12-14 12:21:18 -08007519 * i40e_sync_udp_filters_subtask - Sync the VSI filter list with HW
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007520 * @pf: board private structure
7521 **/
Singhai, Anjali6a899022015-12-14 12:21:18 -08007522static void i40e_sync_udp_filters_subtask(struct i40e_pf *pf)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007523{
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007524 struct i40e_hw *hw = &pf->hw;
7525 i40e_status ret;
Jacob Kellerfe0b0cd2017-02-06 14:38:38 -08007526 u16 port;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007527 int i;
7528
Singhai, Anjali6a899022015-12-14 12:21:18 -08007529 if (!(pf->flags & I40E_FLAG_UDP_FILTER_SYNC))
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007530 return;
7531
Singhai, Anjali6a899022015-12-14 12:21:18 -08007532 pf->flags &= ~I40E_FLAG_UDP_FILTER_SYNC;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007533
7534 for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
Singhai, Anjali6a899022015-12-14 12:21:18 -08007535 if (pf->pending_udp_bitmap & BIT_ULL(i)) {
7536 pf->pending_udp_bitmap &= ~BIT_ULL(i);
Jacob Keller27826fd2017-04-19 09:25:50 -04007537 port = pf->udp_ports[i].port;
Shannon Nelsonc22c06c2015-03-31 00:45:04 -07007538 if (port)
Carolyn Wybornyb3f5c7b2016-08-24 11:33:51 -07007539 ret = i40e_aq_add_udp_tunnel(hw, port,
7540 pf->udp_ports[i].type,
7541 NULL, NULL);
Shannon Nelsonc22c06c2015-03-31 00:45:04 -07007542 else
7543 ret = i40e_aq_del_udp_tunnel(hw, i, NULL);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007544
7545 if (ret) {
Jacob Kellerd8b2c702017-06-20 15:17:00 -07007546 dev_info(&pf->pdev->dev,
7547 "%s %s port %d, index %d failed, err %s aq_err %s\n",
7548 i40e_tunnel_name(&pf->udp_ports[i]),
7549 port ? "add" : "delete",
7550 port, i,
7551 i40e_stat_str(&pf->hw, ret),
7552 i40e_aq_str(&pf->hw,
7553 pf->hw.aq.asq_last_status));
Jacob Keller27826fd2017-04-19 09:25:50 -04007554 pf->udp_ports[i].port = 0;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007555 }
7556 }
7557 }
7558}
7559
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007560/**
7561 * i40e_service_task - Run the driver's async subtasks
7562 * @work: pointer to work_struct containing our data
7563 **/
7564static void i40e_service_task(struct work_struct *work)
7565{
7566 struct i40e_pf *pf = container_of(work,
7567 struct i40e_pf,
7568 service_task);
7569 unsigned long start_time = jiffies;
7570
Shannon Nelsone57a2fe2014-06-03 23:50:19 +00007571 /* don't bother with service tasks if a reset is in progress */
Jacob Keller0da36b92017-04-19 09:25:55 -04007572 if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
Shannon Nelsone57a2fe2014-06-03 23:50:19 +00007573 return;
Shannon Nelsone57a2fe2014-06-03 23:50:19 +00007574
Jacob Keller0da36b92017-04-19 09:25:55 -04007575 if (test_and_set_bit(__I40E_SERVICE_SCHED, pf->state))
Mitch Williams91089032016-11-21 13:03:51 -08007576 return;
7577
Kiran Patilb03a8c12015-09-24 18:13:15 -04007578 i40e_detect_recover_hung(pf);
Jesse Brandeburg2818ccd2016-01-13 16:51:38 -08007579 i40e_sync_filters_subtask(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007580 i40e_reset_subtask(pf);
7581 i40e_handle_mdd_event(pf);
7582 i40e_vc_process_vflr_event(pf);
7583 i40e_watchdog_subtask(pf);
7584 i40e_fdir_reinit_subtask(pf);
Mitch Williams0ef2d5a2017-01-24 10:24:00 -08007585 if (pf->flags & I40E_FLAG_CLIENT_RESET) {
7586 /* Client subtask will reopen next time through. */
7587 i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], true);
7588 pf->flags &= ~I40E_FLAG_CLIENT_RESET;
7589 } else {
7590 i40e_client_subtask(pf);
7591 if (pf->flags & I40E_FLAG_CLIENT_L2_CHANGE) {
7592 i40e_notify_client_of_l2_param_changes(
7593 pf->vsi[pf->lan_vsi]);
7594 pf->flags &= ~I40E_FLAG_CLIENT_L2_CHANGE;
7595 }
7596 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007597 i40e_sync_filters_subtask(pf);
Singhai, Anjali6a899022015-12-14 12:21:18 -08007598 i40e_sync_udp_filters_subtask(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007599 i40e_clean_adminq_subtask(pf);
7600
Mitch Williams91089032016-11-21 13:03:51 -08007601 /* flush memory to make sure state is correct before next watchdog */
7602 smp_mb__before_atomic();
Jacob Keller0da36b92017-04-19 09:25:55 -04007603 clear_bit(__I40E_SERVICE_SCHED, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007604
7605 /* If the tasks have taken longer than one timer cycle or there
7606 * is more work to be done, reschedule the service task now
7607 * rather than wait for the timer to tick again.
7608 */
7609 if (time_after(jiffies, (start_time + pf->service_timer_period)) ||
Jacob Keller0da36b92017-04-19 09:25:55 -04007610 test_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state) ||
7611 test_bit(__I40E_MDD_EVENT_PENDING, pf->state) ||
7612 test_bit(__I40E_VFLR_EVENT_PENDING, pf->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007613 i40e_service_event_schedule(pf);
7614}
7615
7616/**
7617 * i40e_service_timer - timer callback
7618 * @data: pointer to PF struct
7619 **/
7620static void i40e_service_timer(unsigned long data)
7621{
7622 struct i40e_pf *pf = (struct i40e_pf *)data;
7623
7624 mod_timer(&pf->service_timer,
7625 round_jiffies(jiffies + pf->service_timer_period));
7626 i40e_service_event_schedule(pf);
7627}
7628
7629/**
7630 * i40e_set_num_rings_in_vsi - Determine number of rings in the VSI
7631 * @vsi: the VSI being configured
7632 **/
7633static int i40e_set_num_rings_in_vsi(struct i40e_vsi *vsi)
7634{
7635 struct i40e_pf *pf = vsi->back;
7636
7637 switch (vsi->type) {
7638 case I40E_VSI_MAIN:
7639 vsi->alloc_queue_pairs = pf->num_lan_qps;
7640 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
7641 I40E_REQ_DESCRIPTOR_MULTIPLE);
7642 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
7643 vsi->num_q_vectors = pf->num_lan_msix;
7644 else
7645 vsi->num_q_vectors = 1;
7646
7647 break;
7648
7649 case I40E_VSI_FDIR:
7650 vsi->alloc_queue_pairs = 1;
7651 vsi->num_desc = ALIGN(I40E_FDIR_RING_COUNT,
7652 I40E_REQ_DESCRIPTOR_MULTIPLE);
Tushar Davea70e4072016-05-16 12:40:53 -07007653 vsi->num_q_vectors = pf->num_fdsb_msix;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007654 break;
7655
7656 case I40E_VSI_VMDQ2:
7657 vsi->alloc_queue_pairs = pf->num_vmdq_qps;
7658 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
7659 I40E_REQ_DESCRIPTOR_MULTIPLE);
7660 vsi->num_q_vectors = pf->num_vmdq_msix;
7661 break;
7662
7663 case I40E_VSI_SRIOV:
7664 vsi->alloc_queue_pairs = pf->num_vf_qps;
7665 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
7666 I40E_REQ_DESCRIPTOR_MULTIPLE);
7667 break;
7668
7669 default:
7670 WARN_ON(1);
7671 return -ENODATA;
7672 }
7673
7674 return 0;
7675}
7676
7677/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007678 * i40e_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the vsi
7679 * @type: VSI pointer
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007680 * @alloc_qvectors: a bool to specify if q_vectors need to be allocated.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007681 *
7682 * On error: returns error code (negative)
7683 * On success: returns 0
7684 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007685static int i40e_vsi_alloc_arrays(struct i40e_vsi *vsi, bool alloc_qvectors)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007686{
Björn Töpel74608d12017-05-24 07:55:35 +02007687 struct i40e_ring **next_rings;
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007688 int size;
7689 int ret = 0;
7690
Björn Töpel74608d12017-05-24 07:55:35 +02007691 /* allocate memory for both Tx, XDP Tx and Rx ring pointers */
7692 size = sizeof(struct i40e_ring *) * vsi->alloc_queue_pairs *
7693 (i40e_enabled_xdp_vsi(vsi) ? 3 : 2);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007694 vsi->tx_rings = kzalloc(size, GFP_KERNEL);
7695 if (!vsi->tx_rings)
7696 return -ENOMEM;
Björn Töpel74608d12017-05-24 07:55:35 +02007697 next_rings = vsi->tx_rings + vsi->alloc_queue_pairs;
7698 if (i40e_enabled_xdp_vsi(vsi)) {
7699 vsi->xdp_rings = next_rings;
7700 next_rings += vsi->alloc_queue_pairs;
7701 }
7702 vsi->rx_rings = next_rings;
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007703
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007704 if (alloc_qvectors) {
7705 /* allocate memory for q_vector pointers */
Julia Lawallf57e4fb2014-07-30 03:11:09 +00007706 size = sizeof(struct i40e_q_vector *) * vsi->num_q_vectors;
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007707 vsi->q_vectors = kzalloc(size, GFP_KERNEL);
7708 if (!vsi->q_vectors) {
7709 ret = -ENOMEM;
7710 goto err_vectors;
7711 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007712 }
7713 return ret;
7714
7715err_vectors:
7716 kfree(vsi->tx_rings);
7717 return ret;
7718}
7719
7720/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007721 * i40e_vsi_mem_alloc - Allocates the next available struct vsi in the PF
7722 * @pf: board private structure
7723 * @type: type of VSI
7724 *
7725 * On error: returns error code (negative)
7726 * On success: returns vsi index in PF (positive)
7727 **/
7728static int i40e_vsi_mem_alloc(struct i40e_pf *pf, enum i40e_vsi_type type)
7729{
7730 int ret = -ENODEV;
7731 struct i40e_vsi *vsi;
7732 int vsi_idx;
7733 int i;
7734
7735 /* Need to protect the allocation of the VSIs at the PF level */
7736 mutex_lock(&pf->switch_mutex);
7737
7738 /* VSI list may be fragmented if VSI creation/destruction has
7739 * been happening. We can afford to do a quick scan to look
7740 * for any free VSIs in the list.
7741 *
7742 * find next empty vsi slot, looping back around if necessary
7743 */
7744 i = pf->next_vsi;
Mitch Williams505682c2014-05-20 08:01:37 +00007745 while (i < pf->num_alloc_vsi && pf->vsi[i])
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007746 i++;
Mitch Williams505682c2014-05-20 08:01:37 +00007747 if (i >= pf->num_alloc_vsi) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007748 i = 0;
7749 while (i < pf->next_vsi && pf->vsi[i])
7750 i++;
7751 }
7752
Mitch Williams505682c2014-05-20 08:01:37 +00007753 if (i < pf->num_alloc_vsi && !pf->vsi[i]) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007754 vsi_idx = i; /* Found one! */
7755 } else {
7756 ret = -ENODEV;
Alexander Duyck493fb302013-09-28 07:01:44 +00007757 goto unlock_pf; /* out of VSI slots! */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007758 }
7759 pf->next_vsi = ++i;
7760
7761 vsi = kzalloc(sizeof(*vsi), GFP_KERNEL);
7762 if (!vsi) {
7763 ret = -ENOMEM;
Alexander Duyck493fb302013-09-28 07:01:44 +00007764 goto unlock_pf;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007765 }
7766 vsi->type = type;
7767 vsi->back = pf;
Jacob Keller0da36b92017-04-19 09:25:55 -04007768 set_bit(__I40E_VSI_DOWN, vsi->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007769 vsi->flags = 0;
7770 vsi->idx = vsi_idx;
Jesse Brandeburgac26fc12015-09-28 14:12:37 -04007771 vsi->int_rate_limit = 0;
Anjali Singhai Jain5db4cb52015-02-24 06:58:49 +00007772 vsi->rss_table_size = (vsi->type == I40E_VSI_MAIN) ?
7773 pf->rss_table_size : 64;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007774 vsi->netdev_registered = false;
7775 vsi->work_limit = I40E_DEFAULT_IRQ_WORK;
Jacob Keller278e7d02016-10-05 09:30:37 -07007776 hash_init(vsi->mac_filter_hash);
Shannon Nelson63741842014-04-23 04:50:16 +00007777 vsi->irqs_ready = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007778
Alexander Duyck9f65e152013-09-28 06:00:58 +00007779 ret = i40e_set_num_rings_in_vsi(vsi);
7780 if (ret)
7781 goto err_rings;
7782
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007783 ret = i40e_vsi_alloc_arrays(vsi, true);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007784 if (ret)
Alexander Duyck9f65e152013-09-28 06:00:58 +00007785 goto err_rings;
Alexander Duyck493fb302013-09-28 07:01:44 +00007786
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007787 /* Setup default MSIX irq handler for VSI */
7788 i40e_vsi_setup_irqhandler(vsi, i40e_msix_clean_rings);
7789
Kiran Patil21659032015-09-30 14:09:03 -04007790 /* Initialize VSI lock */
Jacob Keller278e7d02016-10-05 09:30:37 -07007791 spin_lock_init(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007792 pf->vsi[vsi_idx] = vsi;
7793 ret = vsi_idx;
Alexander Duyck493fb302013-09-28 07:01:44 +00007794 goto unlock_pf;
7795
Alexander Duyck9f65e152013-09-28 06:00:58 +00007796err_rings:
Alexander Duyck493fb302013-09-28 07:01:44 +00007797 pf->next_vsi = i - 1;
7798 kfree(vsi);
7799unlock_pf:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007800 mutex_unlock(&pf->switch_mutex);
7801 return ret;
7802}
7803
7804/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007805 * i40e_vsi_free_arrays - Free queue and vector pointer arrays for the VSI
7806 * @type: VSI pointer
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007807 * @free_qvectors: a bool to specify if q_vectors need to be freed.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007808 *
7809 * On error: returns error code (negative)
7810 * On success: returns 0
7811 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007812static void i40e_vsi_free_arrays(struct i40e_vsi *vsi, bool free_qvectors)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007813{
7814 /* free the ring and vector containers */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007815 if (free_qvectors) {
7816 kfree(vsi->q_vectors);
7817 vsi->q_vectors = NULL;
7818 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007819 kfree(vsi->tx_rings);
7820 vsi->tx_rings = NULL;
7821 vsi->rx_rings = NULL;
Björn Töpel74608d12017-05-24 07:55:35 +02007822 vsi->xdp_rings = NULL;
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007823}
7824
7825/**
Helin Zhang28c58692015-10-26 19:44:27 -04007826 * i40e_clear_rss_config_user - clear the user configured RSS hash keys
7827 * and lookup table
7828 * @vsi: Pointer to VSI structure
7829 */
7830static void i40e_clear_rss_config_user(struct i40e_vsi *vsi)
7831{
7832 if (!vsi)
7833 return;
7834
7835 kfree(vsi->rss_hkey_user);
7836 vsi->rss_hkey_user = NULL;
7837
7838 kfree(vsi->rss_lut_user);
7839 vsi->rss_lut_user = NULL;
7840}
7841
7842/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007843 * i40e_vsi_clear - Deallocate the VSI provided
7844 * @vsi: the VSI being un-configured
7845 **/
7846static int i40e_vsi_clear(struct i40e_vsi *vsi)
7847{
7848 struct i40e_pf *pf;
7849
7850 if (!vsi)
7851 return 0;
7852
7853 if (!vsi->back)
7854 goto free_vsi;
7855 pf = vsi->back;
7856
7857 mutex_lock(&pf->switch_mutex);
7858 if (!pf->vsi[vsi->idx]) {
7859 dev_err(&pf->pdev->dev, "pf->vsi[%d] is NULL, just free vsi[%d](%p,type %d)\n",
7860 vsi->idx, vsi->idx, vsi, vsi->type);
7861 goto unlock_vsi;
7862 }
7863
7864 if (pf->vsi[vsi->idx] != vsi) {
7865 dev_err(&pf->pdev->dev,
7866 "pf->vsi[%d](%p, type %d) != vsi[%d](%p,type %d): no free!\n",
7867 pf->vsi[vsi->idx]->idx,
7868 pf->vsi[vsi->idx],
7869 pf->vsi[vsi->idx]->type,
7870 vsi->idx, vsi, vsi->type);
7871 goto unlock_vsi;
7872 }
7873
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00007874 /* updates the PF for this cleared vsi */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007875 i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
7876 i40e_put_lump(pf->irq_pile, vsi->base_vector, vsi->idx);
7877
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007878 i40e_vsi_free_arrays(vsi, true);
Helin Zhang28c58692015-10-26 19:44:27 -04007879 i40e_clear_rss_config_user(vsi);
Alexander Duyck493fb302013-09-28 07:01:44 +00007880
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007881 pf->vsi[vsi->idx] = NULL;
7882 if (vsi->idx < pf->next_vsi)
7883 pf->next_vsi = vsi->idx;
7884
7885unlock_vsi:
7886 mutex_unlock(&pf->switch_mutex);
7887free_vsi:
7888 kfree(vsi);
7889
7890 return 0;
7891}
7892
7893/**
Alexander Duyck9f65e152013-09-28 06:00:58 +00007894 * i40e_vsi_clear_rings - Deallocates the Rx and Tx rings for the provided VSI
7895 * @vsi: the VSI being cleaned
7896 **/
Shannon Nelsonbe1d5ee2013-11-28 06:39:23 +00007897static void i40e_vsi_clear_rings(struct i40e_vsi *vsi)
Alexander Duyck9f65e152013-09-28 06:00:58 +00007898{
7899 int i;
7900
Greg Rose8e9dca52013-12-18 13:45:53 +00007901 if (vsi->tx_rings && vsi->tx_rings[0]) {
Neerav Parikhd7397642013-11-28 06:39:37 +00007902 for (i = 0; i < vsi->alloc_queue_pairs; i++) {
Mitch Williams00403f02013-09-28 07:13:13 +00007903 kfree_rcu(vsi->tx_rings[i], rcu);
7904 vsi->tx_rings[i] = NULL;
7905 vsi->rx_rings[i] = NULL;
Björn Töpel74608d12017-05-24 07:55:35 +02007906 if (vsi->xdp_rings)
7907 vsi->xdp_rings[i] = NULL;
Mitch Williams00403f02013-09-28 07:13:13 +00007908 }
Shannon Nelsonbe1d5ee2013-11-28 06:39:23 +00007909 }
Alexander Duyck9f65e152013-09-28 06:00:58 +00007910}
7911
7912/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007913 * i40e_alloc_rings - Allocates the Rx and Tx rings for the provided VSI
7914 * @vsi: the VSI being configured
7915 **/
7916static int i40e_alloc_rings(struct i40e_vsi *vsi)
7917{
Björn Töpel74608d12017-05-24 07:55:35 +02007918 int i, qpv = i40e_enabled_xdp_vsi(vsi) ? 3 : 2;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007919 struct i40e_pf *pf = vsi->back;
Björn Töpel74608d12017-05-24 07:55:35 +02007920 struct i40e_ring *ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007921
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007922 /* Set basic values in the rings to be used later during open() */
Neerav Parikhd7397642013-11-28 06:39:37 +00007923 for (i = 0; i < vsi->alloc_queue_pairs; i++) {
Shannon Nelsonac6c5e32013-11-20 10:02:57 +00007924 /* allocate space for both Tx and Rx in one shot */
Björn Töpel74608d12017-05-24 07:55:35 +02007925 ring = kcalloc(qpv, sizeof(struct i40e_ring), GFP_KERNEL);
7926 if (!ring)
Alexander Duyck9f65e152013-09-28 06:00:58 +00007927 goto err_out;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007928
Björn Töpel74608d12017-05-24 07:55:35 +02007929 ring->queue_index = i;
7930 ring->reg_idx = vsi->base_queue + i;
7931 ring->ring_active = false;
7932 ring->vsi = vsi;
7933 ring->netdev = vsi->netdev;
7934 ring->dev = &pf->pdev->dev;
7935 ring->count = vsi->num_desc;
7936 ring->size = 0;
7937 ring->dcb_tc = 0;
Jacob Kellerd36e41d2017-06-23 04:24:46 -04007938 if (vsi->back->hw_features & I40E_HW_WB_ON_ITR_CAPABLE)
Björn Töpel74608d12017-05-24 07:55:35 +02007939 ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
7940 ring->tx_itr_setting = pf->tx_itr_default;
7941 vsi->tx_rings[i] = ring++;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007942
Björn Töpel74608d12017-05-24 07:55:35 +02007943 if (!i40e_enabled_xdp_vsi(vsi))
7944 goto setup_rx;
7945
7946 ring->queue_index = vsi->alloc_queue_pairs + i;
7947 ring->reg_idx = vsi->base_queue + ring->queue_index;
7948 ring->ring_active = false;
7949 ring->vsi = vsi;
7950 ring->netdev = NULL;
7951 ring->dev = &pf->pdev->dev;
7952 ring->count = vsi->num_desc;
7953 ring->size = 0;
7954 ring->dcb_tc = 0;
Jacob Kellerd36e41d2017-06-23 04:24:46 -04007955 if (vsi->back->hw_features & I40E_HW_WB_ON_ITR_CAPABLE)
Björn Töpel74608d12017-05-24 07:55:35 +02007956 ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
7957 set_ring_xdp(ring);
7958 ring->tx_itr_setting = pf->tx_itr_default;
7959 vsi->xdp_rings[i] = ring++;
7960
7961setup_rx:
7962 ring->queue_index = i;
7963 ring->reg_idx = vsi->base_queue + i;
7964 ring->ring_active = false;
7965 ring->vsi = vsi;
7966 ring->netdev = vsi->netdev;
7967 ring->dev = &pf->pdev->dev;
7968 ring->count = vsi->num_desc;
7969 ring->size = 0;
7970 ring->dcb_tc = 0;
7971 ring->rx_itr_setting = pf->rx_itr_default;
7972 vsi->rx_rings[i] = ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007973 }
7974
7975 return 0;
Alexander Duyck9f65e152013-09-28 06:00:58 +00007976
7977err_out:
7978 i40e_vsi_clear_rings(vsi);
7979 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007980}
7981
7982/**
7983 * i40e_reserve_msix_vectors - Reserve MSI-X vectors in the kernel
7984 * @pf: board private structure
7985 * @vectors: the number of MSI-X vectors to request
7986 *
7987 * Returns the number of vectors reserved, or error
7988 **/
7989static int i40e_reserve_msix_vectors(struct i40e_pf *pf, int vectors)
7990{
Alexander Gordeev7b37f372014-02-18 11:11:42 +01007991 vectors = pci_enable_msix_range(pf->pdev, pf->msix_entries,
7992 I40E_MIN_MSIX, vectors);
7993 if (vectors < 0) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007994 dev_info(&pf->pdev->dev,
Alexander Gordeev7b37f372014-02-18 11:11:42 +01007995 "MSI-X vector reservation failed: %d\n", vectors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007996 vectors = 0;
7997 }
7998
7999 return vectors;
8000}
8001
8002/**
8003 * i40e_init_msix - Setup the MSIX capability
8004 * @pf: board private structure
8005 *
8006 * Work with the OS to set up the MSIX vectors needed.
8007 *
Shannon Nelson3b444392015-02-26 16:15:57 +00008008 * Returns the number of vectors reserved or negative on failure
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008009 **/
8010static int i40e_init_msix(struct i40e_pf *pf)
8011{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008012 struct i40e_hw *hw = &pf->hw;
Jacob Kellerc0cf70a2017-01-24 10:24:01 -08008013 int cpus, extra_vectors;
Shannon Nelson1e200e42015-02-27 09:15:24 +00008014 int vectors_left;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008015 int v_budget, i;
Shannon Nelson3b444392015-02-26 16:15:57 +00008016 int v_actual;
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06008017 int iwarp_requested = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008018
8019 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
8020 return -ENODEV;
8021
8022 /* The number of vectors we'll request will be comprised of:
8023 * - Add 1 for "other" cause for Admin Queue events, etc.
8024 * - The number of LAN queue pairs
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008025 * - Queues being used for RSS.
8026 * We don't need as many as max_rss_size vectors.
8027 * use rss_size instead in the calculation since that
8028 * is governed by number of cpus in the system.
8029 * - assumes symmetric Tx/Rx pairing
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008030 * - The number of VMDq pairs
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06008031 * - The CPU count within the NUMA node if iWARP is enabled
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008032 * Once we count this up, try the request.
8033 *
8034 * If we can't get what we want, we'll simplify to nearly nothing
8035 * and try again. If that still fails, we punt.
8036 */
Shannon Nelson1e200e42015-02-27 09:15:24 +00008037 vectors_left = hw->func_caps.num_msix_vectors;
8038 v_budget = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008039
Shannon Nelson1e200e42015-02-27 09:15:24 +00008040 /* reserve one vector for miscellaneous handler */
8041 if (vectors_left) {
8042 v_budget++;
8043 vectors_left--;
8044 }
8045
Jacob Kellerc0cf70a2017-01-24 10:24:01 -08008046 /* reserve some vectors for the main PF traffic queues. Initially we
8047 * only reserve at most 50% of the available vectors, in the case that
8048 * the number of online CPUs is large. This ensures that we can enable
8049 * extra features as well. Once we've enabled the other features, we
8050 * will use any remaining vectors to reach as close as we can to the
8051 * number of online CPUs.
8052 */
8053 cpus = num_online_cpus();
8054 pf->num_lan_msix = min_t(int, cpus, vectors_left / 2);
Shannon Nelson1e200e42015-02-27 09:15:24 +00008055 vectors_left -= pf->num_lan_msix;
Shannon Nelson1e200e42015-02-27 09:15:24 +00008056
8057 /* reserve one vector for sideband flow director */
8058 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
8059 if (vectors_left) {
Tushar Davea70e4072016-05-16 12:40:53 -07008060 pf->num_fdsb_msix = 1;
Shannon Nelson1e200e42015-02-27 09:15:24 +00008061 v_budget++;
8062 vectors_left--;
8063 } else {
Tushar Davea70e4072016-05-16 12:40:53 -07008064 pf->num_fdsb_msix = 0;
Shannon Nelson1e200e42015-02-27 09:15:24 +00008065 }
8066 }
John W Linville83840e42015-01-14 03:06:28 +00008067
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06008068 /* can we reserve enough for iWARP? */
8069 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02008070 iwarp_requested = pf->num_iwarp_msix;
8071
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06008072 if (!vectors_left)
8073 pf->num_iwarp_msix = 0;
8074 else if (vectors_left < pf->num_iwarp_msix)
8075 pf->num_iwarp_msix = 1;
8076 v_budget += pf->num_iwarp_msix;
8077 vectors_left -= pf->num_iwarp_msix;
8078 }
8079
Shannon Nelson1e200e42015-02-27 09:15:24 +00008080 /* any vectors left over go for VMDq support */
8081 if (pf->flags & I40E_FLAG_VMDQ_ENABLED) {
8082 int vmdq_vecs_wanted = pf->num_vmdq_vsis * pf->num_vmdq_qps;
8083 int vmdq_vecs = min_t(int, vectors_left, vmdq_vecs_wanted);
8084
Stefan Assmann9ca57e92016-09-19 13:37:49 +02008085 if (!vectors_left) {
8086 pf->num_vmdq_msix = 0;
8087 pf->num_vmdq_qps = 0;
8088 } else {
8089 /* if we're short on vectors for what's desired, we limit
8090 * the queues per vmdq. If this is still more than are
8091 * available, the user will need to change the number of
8092 * queues/vectors used by the PF later with the ethtool
8093 * channels command
8094 */
8095 if (vmdq_vecs < vmdq_vecs_wanted)
8096 pf->num_vmdq_qps = 1;
8097 pf->num_vmdq_msix = pf->num_vmdq_qps;
Shannon Nelson1e200e42015-02-27 09:15:24 +00008098
Stefan Assmann9ca57e92016-09-19 13:37:49 +02008099 v_budget += vmdq_vecs;
8100 vectors_left -= vmdq_vecs;
8101 }
Shannon Nelson1e200e42015-02-27 09:15:24 +00008102 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008103
Jacob Kellerc0cf70a2017-01-24 10:24:01 -08008104 /* On systems with a large number of SMP cores, we previously limited
8105 * the number of vectors for num_lan_msix to be at most 50% of the
8106 * available vectors, to allow for other features. Now, we add back
8107 * the remaining vectors. However, we ensure that the total
8108 * num_lan_msix will not exceed num_online_cpus(). To do this, we
8109 * calculate the number of vectors we can add without going over the
8110 * cap of CPUs. For systems with a small number of CPUs this will be
8111 * zero.
8112 */
8113 extra_vectors = min_t(int, cpus - pf->num_lan_msix, vectors_left);
8114 pf->num_lan_msix += extra_vectors;
8115 vectors_left -= extra_vectors;
8116
8117 WARN(vectors_left < 0,
8118 "Calculation of remaining vectors underflowed. This is an accounting bug when determining total MSI-X vectors.\n");
8119
8120 v_budget += pf->num_lan_msix;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008121 pf->msix_entries = kcalloc(v_budget, sizeof(struct msix_entry),
8122 GFP_KERNEL);
8123 if (!pf->msix_entries)
8124 return -ENOMEM;
8125
8126 for (i = 0; i < v_budget; i++)
8127 pf->msix_entries[i].entry = i;
Shannon Nelson3b444392015-02-26 16:15:57 +00008128 v_actual = i40e_reserve_msix_vectors(pf, v_budget);
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00008129
Shannon Nelson3b444392015-02-26 16:15:57 +00008130 if (v_actual < I40E_MIN_MSIX) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008131 pf->flags &= ~I40E_FLAG_MSIX_ENABLED;
8132 kfree(pf->msix_entries);
8133 pf->msix_entries = NULL;
Guilherme G Piccoli4c95aa52016-09-22 10:03:58 -03008134 pci_disable_msix(pf->pdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008135 return -ENODEV;
8136
Shannon Nelson3b444392015-02-26 16:15:57 +00008137 } else if (v_actual == I40E_MIN_MSIX) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008138 /* Adjust for minimal MSIX use */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008139 pf->num_vmdq_vsis = 0;
8140 pf->num_vmdq_qps = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008141 pf->num_lan_qps = 1;
8142 pf->num_lan_msix = 1;
8143
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02008144 } else if (!vectors_left) {
8145 /* If we have limited resources, we will start with no vectors
8146 * for the special features and then allocate vectors to some
8147 * of these features based on the policy and at the end disable
8148 * the features that did not get any vectors.
8149 */
Shannon Nelson3b444392015-02-26 16:15:57 +00008150 int vec;
8151
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02008152 dev_info(&pf->pdev->dev,
8153 "MSI-X vector limit reached, attempting to redistribute vectors\n");
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00008154 /* reserve the misc vector */
Shannon Nelson3b444392015-02-26 16:15:57 +00008155 vec = v_actual - 1;
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00008156
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008157 /* Scale vector usage down */
8158 pf->num_vmdq_msix = 1; /* force VMDqs to only one vector */
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00008159 pf->num_vmdq_vsis = 1;
Shannon Nelson1e200e42015-02-27 09:15:24 +00008160 pf->num_vmdq_qps = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008161
8162 /* partition out the remaining vectors */
8163 switch (vec) {
8164 case 2:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008165 pf->num_lan_msix = 1;
8166 break;
8167 case 3:
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06008168 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
8169 pf->num_lan_msix = 1;
8170 pf->num_iwarp_msix = 1;
8171 } else {
8172 pf->num_lan_msix = 2;
8173 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008174 break;
8175 default:
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06008176 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
8177 pf->num_iwarp_msix = min_t(int, (vec / 3),
8178 iwarp_requested);
8179 pf->num_vmdq_vsis = min_t(int, (vec / 3),
8180 I40E_DEFAULT_NUM_VMDQ_VSI);
8181 } else {
8182 pf->num_vmdq_vsis = min_t(int, (vec / 2),
8183 I40E_DEFAULT_NUM_VMDQ_VSI);
8184 }
Stefan Assmannabd97a92016-09-19 13:37:51 +02008185 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
8186 pf->num_fdsb_msix = 1;
8187 vec--;
8188 }
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06008189 pf->num_lan_msix = min_t(int,
8190 (vec - (pf->num_iwarp_msix + pf->num_vmdq_vsis)),
8191 pf->num_lan_msix);
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02008192 pf->num_lan_qps = pf->num_lan_msix;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008193 break;
8194 }
8195 }
8196
Stefan Assmannabd97a92016-09-19 13:37:51 +02008197 if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
8198 (pf->num_fdsb_msix == 0)) {
8199 dev_info(&pf->pdev->dev, "Sideband Flowdir disabled, not enough MSI-X vectors\n");
8200 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
8201 }
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00008202 if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
8203 (pf->num_vmdq_msix == 0)) {
8204 dev_info(&pf->pdev->dev, "VMDq disabled, not enough MSI-X vectors\n");
8205 pf->flags &= ~I40E_FLAG_VMDQ_ENABLED;
8206 }
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06008207
8208 if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
8209 (pf->num_iwarp_msix == 0)) {
8210 dev_info(&pf->pdev->dev, "IWARP disabled, not enough MSI-X vectors\n");
8211 pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
8212 }
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02008213 i40e_debug(&pf->hw, I40E_DEBUG_INIT,
8214 "MSI-X vector distribution: PF %d, VMDq %d, FDSB %d, iWARP %d\n",
8215 pf->num_lan_msix,
8216 pf->num_vmdq_msix * pf->num_vmdq_vsis,
8217 pf->num_fdsb_msix,
8218 pf->num_iwarp_msix);
8219
Shannon Nelson3b444392015-02-26 16:15:57 +00008220 return v_actual;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008221}
8222
8223/**
Greg Rose90e04072014-03-06 08:59:57 +00008224 * i40e_vsi_alloc_q_vector - Allocate memory for a single interrupt vector
Alexander Duyck493fb302013-09-28 07:01:44 +00008225 * @vsi: the VSI being configured
8226 * @v_idx: index of the vector in the vsi struct
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03008227 * @cpu: cpu to be used on affinity_mask
Alexander Duyck493fb302013-09-28 07:01:44 +00008228 *
8229 * We allocate one q_vector. If allocation fails we return -ENOMEM.
8230 **/
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03008231static int i40e_vsi_alloc_q_vector(struct i40e_vsi *vsi, int v_idx, int cpu)
Alexander Duyck493fb302013-09-28 07:01:44 +00008232{
8233 struct i40e_q_vector *q_vector;
8234
8235 /* allocate q_vector */
8236 q_vector = kzalloc(sizeof(struct i40e_q_vector), GFP_KERNEL);
8237 if (!q_vector)
8238 return -ENOMEM;
8239
8240 q_vector->vsi = vsi;
8241 q_vector->v_idx = v_idx;
Jacob Keller759dc4a2017-07-14 09:10:10 -04008242 cpumask_copy(&q_vector->affinity_mask, cpu_possible_mask);
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03008243
Alexander Duyck493fb302013-09-28 07:01:44 +00008244 if (vsi->netdev)
8245 netif_napi_add(vsi->netdev, &q_vector->napi,
Jesse Brandeburgeefeace2014-05-10 04:49:13 +00008246 i40e_napi_poll, NAPI_POLL_WEIGHT);
Alexander Duyck493fb302013-09-28 07:01:44 +00008247
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00008248 q_vector->rx.latency_range = I40E_LOW_LATENCY;
8249 q_vector->tx.latency_range = I40E_LOW_LATENCY;
8250
Alexander Duyck493fb302013-09-28 07:01:44 +00008251 /* tie q_vector and vsi together */
8252 vsi->q_vectors[v_idx] = q_vector;
8253
8254 return 0;
8255}
8256
8257/**
Greg Rose90e04072014-03-06 08:59:57 +00008258 * i40e_vsi_alloc_q_vectors - Allocate memory for interrupt vectors
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008259 * @vsi: the VSI being configured
8260 *
8261 * We allocate one q_vector per queue interrupt. If allocation fails we
8262 * return -ENOMEM.
8263 **/
Greg Rose90e04072014-03-06 08:59:57 +00008264static int i40e_vsi_alloc_q_vectors(struct i40e_vsi *vsi)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008265{
8266 struct i40e_pf *pf = vsi->back;
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03008267 int err, v_idx, num_q_vectors, current_cpu;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008268
8269 /* if not MSIX, give the one vector only to the LAN VSI */
8270 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
8271 num_q_vectors = vsi->num_q_vectors;
8272 else if (vsi == pf->vsi[pf->lan_vsi])
8273 num_q_vectors = 1;
8274 else
8275 return -EINVAL;
8276
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03008277 current_cpu = cpumask_first(cpu_online_mask);
8278
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008279 for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03008280 err = i40e_vsi_alloc_q_vector(vsi, v_idx, current_cpu);
Alexander Duyck493fb302013-09-28 07:01:44 +00008281 if (err)
8282 goto err_out;
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03008283 current_cpu = cpumask_next(current_cpu, cpu_online_mask);
8284 if (unlikely(current_cpu >= nr_cpu_ids))
8285 current_cpu = cpumask_first(cpu_online_mask);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008286 }
8287
8288 return 0;
Alexander Duyck493fb302013-09-28 07:01:44 +00008289
8290err_out:
8291 while (v_idx--)
8292 i40e_free_q_vector(vsi, v_idx);
8293
8294 return err;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008295}
8296
8297/**
8298 * i40e_init_interrupt_scheme - Determine proper interrupt scheme
8299 * @pf: board private structure to initialize
8300 **/
Jesse Brandeburgc11472802015-04-07 19:45:39 -04008301static int i40e_init_interrupt_scheme(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008302{
Shannon Nelson3b444392015-02-26 16:15:57 +00008303 int vectors = 0;
8304 ssize_t size;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008305
8306 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
Shannon Nelson3b444392015-02-26 16:15:57 +00008307 vectors = i40e_init_msix(pf);
8308 if (vectors < 0) {
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08008309 pf->flags &= ~(I40E_FLAG_MSIX_ENABLED |
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06008310 I40E_FLAG_IWARP_ENABLED |
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08008311 I40E_FLAG_RSS_ENABLED |
Neerav Parikh4d9b6042014-05-22 06:31:51 +00008312 I40E_FLAG_DCB_CAPABLE |
Dave Ertmana0362442016-08-29 17:38:26 -07008313 I40E_FLAG_DCB_ENABLED |
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08008314 I40E_FLAG_SRIOV_ENABLED |
8315 I40E_FLAG_FD_SB_ENABLED |
8316 I40E_FLAG_FD_ATR_ENABLED |
8317 I40E_FLAG_VMDQ_ENABLED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008318
8319 /* rework the queue expectations without MSIX */
8320 i40e_determine_queue_usage(pf);
8321 }
8322 }
8323
8324 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED) &&
8325 (pf->flags & I40E_FLAG_MSI_ENABLED)) {
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08008326 dev_info(&pf->pdev->dev, "MSI-X not available, trying MSI\n");
Shannon Nelson3b444392015-02-26 16:15:57 +00008327 vectors = pci_enable_msi(pf->pdev);
8328 if (vectors < 0) {
8329 dev_info(&pf->pdev->dev, "MSI init failed - %d\n",
8330 vectors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008331 pf->flags &= ~I40E_FLAG_MSI_ENABLED;
8332 }
Shannon Nelson3b444392015-02-26 16:15:57 +00008333 vectors = 1; /* one MSI or Legacy vector */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008334 }
8335
Shannon Nelson958a3e32013-09-28 07:13:28 +00008336 if (!(pf->flags & (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED)))
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08008337 dev_info(&pf->pdev->dev, "MSI-X and MSI not available, falling back to Legacy IRQ\n");
Shannon Nelson958a3e32013-09-28 07:13:28 +00008338
Shannon Nelson3b444392015-02-26 16:15:57 +00008339 /* set up vector assignment tracking */
8340 size = sizeof(struct i40e_lump_tracking) + (sizeof(u16) * vectors);
8341 pf->irq_pile = kzalloc(size, GFP_KERNEL);
Jesse Brandeburgc11472802015-04-07 19:45:39 -04008342 if (!pf->irq_pile) {
8343 dev_err(&pf->pdev->dev, "error allocating irq_pile memory\n");
8344 return -ENOMEM;
8345 }
Shannon Nelson3b444392015-02-26 16:15:57 +00008346 pf->irq_pile->num_entries = vectors;
8347 pf->irq_pile->search_hint = 0;
8348
Jesse Brandeburgc11472802015-04-07 19:45:39 -04008349 /* track first vector for misc interrupts, ignore return */
Shannon Nelson3b444392015-02-26 16:15:57 +00008350 (void)i40e_get_lump(pf, pf->irq_pile, 1, I40E_PILE_VALID_BIT - 1);
Jesse Brandeburgc11472802015-04-07 19:45:39 -04008351
8352 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008353}
8354
8355/**
8356 * i40e_setup_misc_vector - Setup the misc vector to handle non queue events
8357 * @pf: board private structure
8358 *
8359 * This sets up the handler for MSIX 0, which is used to manage the
8360 * non-queue interrupts, e.g. AdminQ and errors. This is not used
8361 * when in MSI or Legacy interrupt mode.
8362 **/
8363static int i40e_setup_misc_vector(struct i40e_pf *pf)
8364{
8365 struct i40e_hw *hw = &pf->hw;
8366 int err = 0;
8367
8368 /* Only request the irq if this is the first time through, and
8369 * not when we're rebuilding after a Reset
8370 */
Jacob Keller0da36b92017-04-19 09:25:55 -04008371 if (!test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008372 err = request_irq(pf->msix_entries[0].vector,
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00008373 i40e_intr, 0, pf->int_name, pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008374 if (err) {
8375 dev_info(&pf->pdev->dev,
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08008376 "request_irq for %s failed: %d\n",
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00008377 pf->int_name, err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008378 return -EFAULT;
8379 }
8380 }
8381
Jacob Kellerab437b52014-12-14 01:55:08 +00008382 i40e_enable_misc_int_causes(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008383
8384 /* associate no queues to the misc vector */
8385 wr32(hw, I40E_PFINT_LNKLST0, I40E_QUEUE_END_OF_LIST);
8386 wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), I40E_ITR_8K);
8387
8388 i40e_flush(hw);
8389
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08008390 i40e_irq_dynamic_enable_icr0(pf, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008391
8392 return err;
8393}
8394
8395/**
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008396 * i40e_config_rss_aq - Prepare for RSS using AQ commands
8397 * @vsi: vsi structure
8398 * @seed: RSS hash seed
8399 **/
Helin Zhange69ff812015-10-21 19:56:22 -04008400static int i40e_config_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
8401 u8 *lut, u16 lut_size)
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008402{
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008403 struct i40e_pf *pf = vsi->back;
8404 struct i40e_hw *hw = &pf->hw;
Jacob Keller776b2e12016-07-19 16:23:30 -07008405 int ret = 0;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008406
Jacob Keller776b2e12016-07-19 16:23:30 -07008407 if (seed) {
8408 struct i40e_aqc_get_set_rss_key_data *seed_dw =
8409 (struct i40e_aqc_get_set_rss_key_data *)seed;
8410 ret = i40e_aq_set_rss_key(hw, vsi->id, seed_dw);
8411 if (ret) {
8412 dev_info(&pf->pdev->dev,
8413 "Cannot set RSS key, err %s aq_err %s\n",
8414 i40e_stat_str(hw, ret),
8415 i40e_aq_str(hw, hw->aq.asq_last_status));
8416 return ret;
8417 }
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008418 }
Jacob Keller776b2e12016-07-19 16:23:30 -07008419 if (lut) {
8420 bool pf_lut = vsi->type == I40E_VSI_MAIN ? true : false;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008421
Jacob Keller776b2e12016-07-19 16:23:30 -07008422 ret = i40e_aq_set_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
8423 if (ret) {
8424 dev_info(&pf->pdev->dev,
8425 "Cannot set RSS lut, err %s aq_err %s\n",
8426 i40e_stat_str(hw, ret),
8427 i40e_aq_str(hw, hw->aq.asq_last_status));
8428 return ret;
8429 }
8430 }
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008431 return ret;
8432}
8433
8434/**
Anjali Singhai Jain95a73782015-12-22 14:25:04 -08008435 * i40e_get_rss_aq - Get RSS keys and lut by using AQ commands
8436 * @vsi: Pointer to vsi structure
8437 * @seed: Buffter to store the hash keys
8438 * @lut: Buffer to store the lookup table entries
8439 * @lut_size: Size of buffer to store the lookup table entries
8440 *
8441 * Return 0 on success, negative on failure
8442 */
8443static int i40e_get_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
8444 u8 *lut, u16 lut_size)
8445{
8446 struct i40e_pf *pf = vsi->back;
8447 struct i40e_hw *hw = &pf->hw;
8448 int ret = 0;
8449
8450 if (seed) {
8451 ret = i40e_aq_get_rss_key(hw, vsi->id,
8452 (struct i40e_aqc_get_set_rss_key_data *)seed);
8453 if (ret) {
8454 dev_info(&pf->pdev->dev,
8455 "Cannot get RSS key, err %s aq_err %s\n",
8456 i40e_stat_str(&pf->hw, ret),
8457 i40e_aq_str(&pf->hw,
8458 pf->hw.aq.asq_last_status));
8459 return ret;
8460 }
8461 }
8462
8463 if (lut) {
8464 bool pf_lut = vsi->type == I40E_VSI_MAIN ? true : false;
8465
8466 ret = i40e_aq_get_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
8467 if (ret) {
8468 dev_info(&pf->pdev->dev,
8469 "Cannot get RSS lut, err %s aq_err %s\n",
8470 i40e_stat_str(&pf->hw, ret),
8471 i40e_aq_str(&pf->hw,
8472 pf->hw.aq.asq_last_status));
8473 return ret;
8474 }
8475 }
8476
8477 return ret;
8478}
8479
8480/**
Jacob Keller0582b962016-07-19 16:23:29 -07008481 * i40e_vsi_config_rss - Prepare for VSI(VMDq) RSS if used
8482 * @vsi: VSI structure
8483 **/
8484static int i40e_vsi_config_rss(struct i40e_vsi *vsi)
8485{
8486 u8 seed[I40E_HKEY_ARRAY_SIZE];
8487 struct i40e_pf *pf = vsi->back;
8488 u8 *lut;
8489 int ret;
8490
Jacob Kellerd36e41d2017-06-23 04:24:46 -04008491 if (!(pf->hw_features & I40E_HW_RSS_AQ_CAPABLE))
Jacob Keller0582b962016-07-19 16:23:29 -07008492 return 0;
8493
Jacob Keller552b9962016-07-19 16:23:31 -07008494 if (!vsi->rss_size)
8495 vsi->rss_size = min_t(int, pf->alloc_rss_size,
8496 vsi->num_queue_pairs);
8497 if (!vsi->rss_size)
8498 return -EINVAL;
8499
Jacob Keller0582b962016-07-19 16:23:29 -07008500 lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
8501 if (!lut)
8502 return -ENOMEM;
Jacob Keller552b9962016-07-19 16:23:31 -07008503 /* Use the user configured hash keys and lookup table if there is one,
8504 * otherwise use default
8505 */
8506 if (vsi->rss_lut_user)
8507 memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
8508 else
8509 i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
8510 if (vsi->rss_hkey_user)
8511 memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
8512 else
8513 netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
Jacob Keller0582b962016-07-19 16:23:29 -07008514 ret = i40e_config_rss_aq(vsi, seed, lut, vsi->rss_table_size);
8515 kfree(lut);
8516
8517 return ret;
8518}
8519
8520/**
Helin Zhang043dd652015-10-21 19:56:23 -04008521 * i40e_config_rss_reg - Configure RSS keys and lut by writing registers
Helin Zhange69ff812015-10-21 19:56:22 -04008522 * @vsi: Pointer to vsi structure
8523 * @seed: RSS hash seed
8524 * @lut: Lookup table
8525 * @lut_size: Lookup table size
8526 *
8527 * Returns 0 on success, negative on failure
8528 **/
8529static int i40e_config_rss_reg(struct i40e_vsi *vsi, const u8 *seed,
8530 const u8 *lut, u16 lut_size)
8531{
8532 struct i40e_pf *pf = vsi->back;
8533 struct i40e_hw *hw = &pf->hw;
Mitch Williamsc4e18682016-04-12 08:30:40 -07008534 u16 vf_id = vsi->vf_id;
Helin Zhange69ff812015-10-21 19:56:22 -04008535 u8 i;
8536
8537 /* Fill out hash function seed */
8538 if (seed) {
8539 u32 *seed_dw = (u32 *)seed;
8540
Mitch Williamsc4e18682016-04-12 08:30:40 -07008541 if (vsi->type == I40E_VSI_MAIN) {
8542 for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
Lihong Yang26f77e52017-01-30 12:29:33 -08008543 wr32(hw, I40E_PFQF_HKEY(i), seed_dw[i]);
Mitch Williamsc4e18682016-04-12 08:30:40 -07008544 } else if (vsi->type == I40E_VSI_SRIOV) {
8545 for (i = 0; i <= I40E_VFQF_HKEY1_MAX_INDEX; i++)
Lihong Yang26f77e52017-01-30 12:29:33 -08008546 wr32(hw, I40E_VFQF_HKEY1(i, vf_id), seed_dw[i]);
Mitch Williamsc4e18682016-04-12 08:30:40 -07008547 } else {
8548 dev_err(&pf->pdev->dev, "Cannot set RSS seed - invalid VSI type\n");
8549 }
Helin Zhange69ff812015-10-21 19:56:22 -04008550 }
8551
8552 if (lut) {
8553 u32 *lut_dw = (u32 *)lut;
8554
Mitch Williamsc4e18682016-04-12 08:30:40 -07008555 if (vsi->type == I40E_VSI_MAIN) {
8556 if (lut_size != I40E_HLUT_ARRAY_SIZE)
8557 return -EINVAL;
8558 for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
8559 wr32(hw, I40E_PFQF_HLUT(i), lut_dw[i]);
8560 } else if (vsi->type == I40E_VSI_SRIOV) {
8561 if (lut_size != I40E_VF_HLUT_ARRAY_SIZE)
8562 return -EINVAL;
8563 for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
Lihong Yang26f77e52017-01-30 12:29:33 -08008564 wr32(hw, I40E_VFQF_HLUT1(i, vf_id), lut_dw[i]);
Mitch Williamsc4e18682016-04-12 08:30:40 -07008565 } else {
8566 dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
8567 }
Helin Zhange69ff812015-10-21 19:56:22 -04008568 }
8569 i40e_flush(hw);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008570
8571 return 0;
8572}
8573
8574/**
Helin Zhang043dd652015-10-21 19:56:23 -04008575 * i40e_get_rss_reg - Get the RSS keys and lut by reading registers
8576 * @vsi: Pointer to VSI structure
8577 * @seed: Buffer to store the keys
8578 * @lut: Buffer to store the lookup table entries
8579 * @lut_size: Size of buffer to store the lookup table entries
8580 *
8581 * Returns 0 on success, negative on failure
8582 */
8583static int i40e_get_rss_reg(struct i40e_vsi *vsi, u8 *seed,
8584 u8 *lut, u16 lut_size)
8585{
8586 struct i40e_pf *pf = vsi->back;
8587 struct i40e_hw *hw = &pf->hw;
8588 u16 i;
8589
8590 if (seed) {
8591 u32 *seed_dw = (u32 *)seed;
8592
8593 for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
Shannon Nelson272cdaf22016-02-17 16:12:21 -08008594 seed_dw[i] = i40e_read_rx_ctl(hw, I40E_PFQF_HKEY(i));
Helin Zhang043dd652015-10-21 19:56:23 -04008595 }
8596 if (lut) {
8597 u32 *lut_dw = (u32 *)lut;
8598
8599 if (lut_size != I40E_HLUT_ARRAY_SIZE)
8600 return -EINVAL;
8601 for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
8602 lut_dw[i] = rd32(hw, I40E_PFQF_HLUT(i));
8603 }
8604
8605 return 0;
8606}
8607
8608/**
8609 * i40e_config_rss - Configure RSS keys and lut
8610 * @vsi: Pointer to VSI structure
8611 * @seed: RSS hash seed
8612 * @lut: Lookup table
8613 * @lut_size: Lookup table size
8614 *
8615 * Returns 0 on success, negative on failure
8616 */
8617int i40e_config_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
8618{
8619 struct i40e_pf *pf = vsi->back;
8620
Jacob Kellerd36e41d2017-06-23 04:24:46 -04008621 if (pf->hw_features & I40E_HW_RSS_AQ_CAPABLE)
Helin Zhang043dd652015-10-21 19:56:23 -04008622 return i40e_config_rss_aq(vsi, seed, lut, lut_size);
8623 else
8624 return i40e_config_rss_reg(vsi, seed, lut, lut_size);
8625}
8626
8627/**
8628 * i40e_get_rss - Get RSS keys and lut
8629 * @vsi: Pointer to VSI structure
8630 * @seed: Buffer to store the keys
8631 * @lut: Buffer to store the lookup table entries
8632 * lut_size: Size of buffer to store the lookup table entries
8633 *
8634 * Returns 0 on success, negative on failure
8635 */
8636int i40e_get_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
8637{
Anjali Singhai Jain95a73782015-12-22 14:25:04 -08008638 struct i40e_pf *pf = vsi->back;
8639
Jacob Kellerd36e41d2017-06-23 04:24:46 -04008640 if (pf->hw_features & I40E_HW_RSS_AQ_CAPABLE)
Anjali Singhai Jain95a73782015-12-22 14:25:04 -08008641 return i40e_get_rss_aq(vsi, seed, lut, lut_size);
8642 else
8643 return i40e_get_rss_reg(vsi, seed, lut, lut_size);
Helin Zhang043dd652015-10-21 19:56:23 -04008644}
8645
8646/**
Helin Zhange69ff812015-10-21 19:56:22 -04008647 * i40e_fill_rss_lut - Fill the RSS lookup table with default values
8648 * @pf: Pointer to board private structure
8649 * @lut: Lookup table
8650 * @rss_table_size: Lookup table size
8651 * @rss_size: Range of queue number for hashing
8652 */
Alan Bradyf1582352016-08-24 11:33:46 -07008653void i40e_fill_rss_lut(struct i40e_pf *pf, u8 *lut,
8654 u16 rss_table_size, u16 rss_size)
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008655{
Helin Zhange69ff812015-10-21 19:56:22 -04008656 u16 i;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008657
Helin Zhange69ff812015-10-21 19:56:22 -04008658 for (i = 0; i < rss_table_size; i++)
8659 lut[i] = i % rss_size;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008660}
8661
8662/**
Helin Zhang043dd652015-10-21 19:56:23 -04008663 * i40e_pf_config_rss - Prepare for RSS if used
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008664 * @pf: board private structure
8665 **/
Helin Zhang043dd652015-10-21 19:56:23 -04008666static int i40e_pf_config_rss(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008667{
Anjali Singhai Jain66ddcff2015-02-24 06:58:50 +00008668 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008669 u8 seed[I40E_HKEY_ARRAY_SIZE];
Helin Zhange69ff812015-10-21 19:56:22 -04008670 u8 *lut;
Anjali Singhai Jain4617e8c2013-11-20 10:02:56 +00008671 struct i40e_hw *hw = &pf->hw;
Carolyn Wybornye157ea32014-06-03 23:50:22 +00008672 u32 reg_val;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008673 u64 hena;
Helin Zhange69ff812015-10-21 19:56:22 -04008674 int ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008675
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008676 /* By default we enable TCP/UDP with IPv4/IPv6 ptypes */
Shannon Nelson272cdaf22016-02-17 16:12:21 -08008677 hena = (u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(0)) |
8678 ((u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(1)) << 32);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008679 hena |= i40e_pf_get_default_rss_hena(pf);
8680
Shannon Nelson272cdaf22016-02-17 16:12:21 -08008681 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), (u32)hena);
8682 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008683
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008684 /* Determine the RSS table size based on the hardware capabilities */
Shannon Nelson272cdaf22016-02-17 16:12:21 -08008685 reg_val = i40e_read_rx_ctl(hw, I40E_PFQF_CTL_0);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008686 reg_val = (pf->rss_table_size == 512) ?
8687 (reg_val | I40E_PFQF_CTL_0_HASHLUTSIZE_512) :
8688 (reg_val & ~I40E_PFQF_CTL_0_HASHLUTSIZE_512);
Shannon Nelson272cdaf22016-02-17 16:12:21 -08008689 i40e_write_rx_ctl(hw, I40E_PFQF_CTL_0, reg_val);
Carolyn Wybornye157ea32014-06-03 23:50:22 +00008690
Helin Zhang28c58692015-10-26 19:44:27 -04008691 /* Determine the RSS size of the VSI */
Harshitha Ramamurthyf25571b2017-02-21 15:55:40 -08008692 if (!vsi->rss_size) {
8693 u16 qcount;
8694
8695 qcount = vsi->num_queue_pairs / vsi->tc_config.numtc;
8696 vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
8697 }
Mitch Williamsa4fa59c2016-09-12 14:18:43 -07008698 if (!vsi->rss_size)
8699 return -EINVAL;
Helin Zhang28c58692015-10-26 19:44:27 -04008700
Helin Zhange69ff812015-10-21 19:56:22 -04008701 lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
8702 if (!lut)
8703 return -ENOMEM;
8704
Helin Zhang28c58692015-10-26 19:44:27 -04008705 /* Use user configured lut if there is one, otherwise use default */
8706 if (vsi->rss_lut_user)
8707 memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
8708 else
8709 i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
Helin Zhange69ff812015-10-21 19:56:22 -04008710
Helin Zhang28c58692015-10-26 19:44:27 -04008711 /* Use user configured hash key if there is one, otherwise
8712 * use default.
8713 */
8714 if (vsi->rss_hkey_user)
8715 memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
8716 else
8717 netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
Helin Zhang043dd652015-10-21 19:56:23 -04008718 ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
Helin Zhange69ff812015-10-21 19:56:22 -04008719 kfree(lut);
8720
8721 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008722}
8723
8724/**
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008725 * i40e_reconfig_rss_queues - change number of queues for rss and rebuild
8726 * @pf: board private structure
8727 * @queue_count: the requested queue count for rss.
8728 *
8729 * returns 0 if rss is not enabled, if enabled returns the final rss queue
8730 * count which may be different from the requested queue count.
Maciej Sosin373149f2017-04-05 07:50:55 -04008731 * Note: expects to be called while under rtnl_lock()
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008732 **/
8733int i40e_reconfig_rss_queues(struct i40e_pf *pf, int queue_count)
8734{
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00008735 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
8736 int new_rss_size;
8737
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008738 if (!(pf->flags & I40E_FLAG_RSS_ENABLED))
8739 return 0;
8740
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00008741 new_rss_size = min_t(int, queue_count, pf->rss_size_max);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008742
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00008743 if (queue_count != vsi->num_queue_pairs) {
Harshitha Ramamurthyf25571b2017-02-21 15:55:40 -08008744 u16 qcount;
8745
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00008746 vsi->req_queue_pairs = queue_count;
Maciej Sosin373149f2017-04-05 07:50:55 -04008747 i40e_prep_for_reset(pf, true);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008748
Helin Zhangacd65442015-10-26 19:44:28 -04008749 pf->alloc_rss_size = new_rss_size;
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008750
Maciej Sosin373149f2017-04-05 07:50:55 -04008751 i40e_reset_and_rebuild(pf, true, true);
Helin Zhang28c58692015-10-26 19:44:27 -04008752
8753 /* Discard the user configured hash keys and lut, if less
8754 * queues are enabled.
8755 */
8756 if (queue_count < vsi->rss_size) {
8757 i40e_clear_rss_config_user(vsi);
8758 dev_dbg(&pf->pdev->dev,
8759 "discard user configured hash keys and lut\n");
8760 }
8761
8762 /* Reset vsi->rss_size, as number of enabled queues changed */
Harshitha Ramamurthyf25571b2017-02-21 15:55:40 -08008763 qcount = vsi->num_queue_pairs / vsi->tc_config.numtc;
8764 vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
Helin Zhang28c58692015-10-26 19:44:27 -04008765
Helin Zhang043dd652015-10-21 19:56:23 -04008766 i40e_pf_config_rss(pf);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008767 }
Lihong Yang12815052016-09-27 11:28:48 -07008768 dev_info(&pf->pdev->dev, "User requested queue count/HW max RSS count: %d/%d\n",
8769 vsi->req_queue_pairs, pf->rss_size_max);
Helin Zhangacd65442015-10-26 19:44:28 -04008770 return pf->alloc_rss_size;
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008771}
8772
8773/**
Shannon Nelson4fc8c672017-06-07 05:43:08 -04008774 * i40e_get_partition_bw_setting - Retrieve BW settings for this PF partition
Greg Rosef4492db2015-02-06 08:52:12 +00008775 * @pf: board private structure
8776 **/
Shannon Nelson4fc8c672017-06-07 05:43:08 -04008777i40e_status i40e_get_partition_bw_setting(struct i40e_pf *pf)
Greg Rosef4492db2015-02-06 08:52:12 +00008778{
8779 i40e_status status;
8780 bool min_valid, max_valid;
8781 u32 max_bw, min_bw;
8782
8783 status = i40e_read_bw_from_alt_ram(&pf->hw, &max_bw, &min_bw,
8784 &min_valid, &max_valid);
8785
8786 if (!status) {
8787 if (min_valid)
Shannon Nelson4fc8c672017-06-07 05:43:08 -04008788 pf->min_bw = min_bw;
Greg Rosef4492db2015-02-06 08:52:12 +00008789 if (max_valid)
Shannon Nelson4fc8c672017-06-07 05:43:08 -04008790 pf->max_bw = max_bw;
Greg Rosef4492db2015-02-06 08:52:12 +00008791 }
8792
8793 return status;
8794}
8795
8796/**
Shannon Nelson4fc8c672017-06-07 05:43:08 -04008797 * i40e_set_partition_bw_setting - Set BW settings for this PF partition
Greg Rosef4492db2015-02-06 08:52:12 +00008798 * @pf: board private structure
8799 **/
Shannon Nelson4fc8c672017-06-07 05:43:08 -04008800i40e_status i40e_set_partition_bw_setting(struct i40e_pf *pf)
Greg Rosef4492db2015-02-06 08:52:12 +00008801{
8802 struct i40e_aqc_configure_partition_bw_data bw_data;
8803 i40e_status status;
8804
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00008805 /* Set the valid bit for this PF */
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04008806 bw_data.pf_valid_bits = cpu_to_le16(BIT(pf->hw.pf_id));
Shannon Nelson4fc8c672017-06-07 05:43:08 -04008807 bw_data.max_bw[pf->hw.pf_id] = pf->max_bw & I40E_ALT_BW_VALUE_MASK;
8808 bw_data.min_bw[pf->hw.pf_id] = pf->min_bw & I40E_ALT_BW_VALUE_MASK;
Greg Rosef4492db2015-02-06 08:52:12 +00008809
8810 /* Set the new bandwidths */
8811 status = i40e_aq_configure_partition_bw(&pf->hw, &bw_data, NULL);
8812
8813 return status;
8814}
8815
8816/**
Shannon Nelson4fc8c672017-06-07 05:43:08 -04008817 * i40e_commit_partition_bw_setting - Commit BW settings for this PF partition
Greg Rosef4492db2015-02-06 08:52:12 +00008818 * @pf: board private structure
8819 **/
Shannon Nelson4fc8c672017-06-07 05:43:08 -04008820i40e_status i40e_commit_partition_bw_setting(struct i40e_pf *pf)
Greg Rosef4492db2015-02-06 08:52:12 +00008821{
8822 /* Commit temporary BW setting to permanent NVM image */
8823 enum i40e_admin_queue_err last_aq_status;
8824 i40e_status ret;
8825 u16 nvm_word;
8826
8827 if (pf->hw.partition_id != 1) {
8828 dev_info(&pf->pdev->dev,
8829 "Commit BW only works on partition 1! This is partition %d",
8830 pf->hw.partition_id);
8831 ret = I40E_NOT_SUPPORTED;
8832 goto bw_commit_out;
8833 }
8834
8835 /* Acquire NVM for read access */
8836 ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_READ);
8837 last_aq_status = pf->hw.aq.asq_last_status;
8838 if (ret) {
8839 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008840 "Cannot acquire NVM for read access, err %s aq_err %s\n",
8841 i40e_stat_str(&pf->hw, ret),
8842 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +00008843 goto bw_commit_out;
8844 }
8845
8846 /* Read word 0x10 of NVM - SW compatibility word 1 */
8847 ret = i40e_aq_read_nvm(&pf->hw,
8848 I40E_SR_NVM_CONTROL_WORD,
8849 0x10, sizeof(nvm_word), &nvm_word,
8850 false, NULL);
8851 /* Save off last admin queue command status before releasing
8852 * the NVM
8853 */
8854 last_aq_status = pf->hw.aq.asq_last_status;
8855 i40e_release_nvm(&pf->hw);
8856 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008857 dev_info(&pf->pdev->dev, "NVM read error, err %s aq_err %s\n",
8858 i40e_stat_str(&pf->hw, ret),
8859 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +00008860 goto bw_commit_out;
8861 }
8862
8863 /* Wait a bit for NVM release to complete */
8864 msleep(50);
8865
8866 /* Acquire NVM for write access */
8867 ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_WRITE);
8868 last_aq_status = pf->hw.aq.asq_last_status;
8869 if (ret) {
8870 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008871 "Cannot acquire NVM for write access, err %s aq_err %s\n",
8872 i40e_stat_str(&pf->hw, ret),
8873 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +00008874 goto bw_commit_out;
8875 }
8876 /* Write it back out unchanged to initiate update NVM,
8877 * which will force a write of the shadow (alt) RAM to
8878 * the NVM - thus storing the bandwidth values permanently.
8879 */
8880 ret = i40e_aq_update_nvm(&pf->hw,
8881 I40E_SR_NVM_CONTROL_WORD,
8882 0x10, sizeof(nvm_word),
8883 &nvm_word, true, NULL);
8884 /* Save off last admin queue command status before releasing
8885 * the NVM
8886 */
8887 last_aq_status = pf->hw.aq.asq_last_status;
8888 i40e_release_nvm(&pf->hw);
8889 if (ret)
8890 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008891 "BW settings NOT SAVED, err %s aq_err %s\n",
8892 i40e_stat_str(&pf->hw, ret),
8893 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +00008894bw_commit_out:
8895
8896 return ret;
8897}
8898
8899/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008900 * i40e_sw_init - Initialize general software structures (struct i40e_pf)
8901 * @pf: board private structure to initialize
8902 *
8903 * i40e_sw_init initializes the Adapter private data structure.
8904 * Fields are initialized based on PCI device information and
8905 * OS network device settings (MTU size).
8906 **/
8907static int i40e_sw_init(struct i40e_pf *pf)
8908{
8909 int err = 0;
8910 int size;
8911
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008912 /* Set default capability flags */
8913 pf->flags = I40E_FLAG_RX_CSUM_ENABLED |
8914 I40E_FLAG_MSI_ENABLED |
Mitch Williams2bc7ee82015-02-06 08:52:11 +00008915 I40E_FLAG_MSIX_ENABLED;
8916
Mitch Williamsca99eb92014-04-04 04:43:07 +00008917 /* Set default ITR */
8918 pf->rx_itr_default = I40E_ITR_DYNAMIC | I40E_ITR_RX_DEF;
8919 pf->tx_itr_default = I40E_ITR_DYNAMIC | I40E_ITR_TX_DEF;
8920
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00008921 /* Depending on PF configurations, it is possible that the RSS
8922 * maximum might end up larger than the available queues
8923 */
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04008924 pf->rss_size_max = BIT(pf->hw.func_caps.rss_table_entry_width);
Helin Zhangacd65442015-10-26 19:44:28 -04008925 pf->alloc_rss_size = 1;
Anjali Singhai Jain5db4cb52015-02-24 06:58:49 +00008926 pf->rss_table_size = pf->hw.func_caps.rss_table_size;
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00008927 pf->rss_size_max = min_t(int, pf->rss_size_max,
8928 pf->hw.func_caps.num_tx_qp);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008929 if (pf->hw.func_caps.rss) {
8930 pf->flags |= I40E_FLAG_RSS_ENABLED;
Helin Zhangacd65442015-10-26 19:44:28 -04008931 pf->alloc_rss_size = min_t(int, pf->rss_size_max,
8932 num_online_cpus());
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008933 }
8934
Catherine Sullivan2050bc62013-12-18 13:46:03 +00008935 /* MFP mode enabled */
Pawel Orlowskic78b9532015-04-22 19:34:06 -04008936 if (pf->hw.func_caps.npar_enable || pf->hw.func_caps.flex10_enable) {
Catherine Sullivan2050bc62013-12-18 13:46:03 +00008937 pf->flags |= I40E_FLAG_MFP_ENABLED;
8938 dev_info(&pf->pdev->dev, "MFP mode Enabled\n");
Shannon Nelson4fc8c672017-06-07 05:43:08 -04008939 if (i40e_get_partition_bw_setting(pf)) {
Greg Rosef4492db2015-02-06 08:52:12 +00008940 dev_warn(&pf->pdev->dev,
Shannon Nelson4fc8c672017-06-07 05:43:08 -04008941 "Could not get partition bw settings\n");
8942 } else {
Greg Rosef4492db2015-02-06 08:52:12 +00008943 dev_info(&pf->pdev->dev,
Shannon Nelson4fc8c672017-06-07 05:43:08 -04008944 "Partition BW Min = %8.8x, Max = %8.8x\n",
8945 pf->min_bw, pf->max_bw);
8946
8947 /* nudge the Tx scheduler */
8948 i40e_set_partition_bw_setting(pf);
8949 }
Catherine Sullivan2050bc62013-12-18 13:46:03 +00008950 }
8951
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008952 if ((pf->hw.func_caps.fd_filters_guaranteed > 0) ||
8953 (pf->hw.func_caps.fd_filters_best_effort > 0)) {
8954 pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
8955 pf->atr_sample_rate = I40E_DEFAULT_ATR_SAMPLE_RATE;
Shannon Nelson6eae9c62015-09-03 17:18:55 -04008956 if (pf->flags & I40E_FLAG_MFP_ENABLED &&
8957 pf->hw.num_partitions > 1)
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008958 dev_info(&pf->pdev->dev,
Anjali Singhai Jain0b675842014-03-06 08:59:51 +00008959 "Flow Director Sideband mode Disabled in MFP mode\n");
Shannon Nelson6eae9c62015-09-03 17:18:55 -04008960 else
8961 pf->flags |= I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008962 pf->fdir_pf_filter_count =
8963 pf->hw.func_caps.fd_filters_guaranteed;
8964 pf->hw.fdir_shared_filter_count =
8965 pf->hw.func_caps.fd_filters_best_effort;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008966 }
8967
Anjali Singhai Jain5a433192017-06-23 04:24:45 -04008968 if (pf->hw.mac.type == I40E_MAC_X722) {
Jacob Kellerd36e41d2017-06-23 04:24:46 -04008969 pf->hw_features |= (I40E_HW_RSS_AQ_CAPABLE |
8970 I40E_HW_128_QP_RSS_CAPABLE |
8971 I40E_HW_ATR_EVICT_CAPABLE |
8972 I40E_HW_WB_ON_ITR_CAPABLE |
8973 I40E_HW_MULTIPLE_TCP_UDP_RSS_PCTYPE |
8974 I40E_HW_NO_PCI_LINK_CHECK |
8975 I40E_HW_USE_SET_LLDP_MIB |
8976 I40E_HW_GENEVE_OFFLOAD_CAPABLE |
8977 I40E_HW_PTP_L4_CAPABLE |
8978 I40E_HW_WOL_MC_MAGIC_PKT_WAKE |
8979 I40E_HW_OUTER_UDP_CSUM_CAPABLE);
Anjali Singhai Jain10a955f2017-06-23 04:24:48 -04008980
8981#define I40E_FDEVICT_PCTYPE_DEFAULT 0xc03
8982 if (rd32(&pf->hw, I40E_GLQF_FDEVICTENA(1)) !=
8983 I40E_FDEVICT_PCTYPE_DEFAULT) {
8984 dev_warn(&pf->pdev->dev,
8985 "FD EVICT PCTYPES are not right, disable FD HW EVICT\n");
8986 pf->hw_features &= ~I40E_HW_ATR_EVICT_CAPABLE;
8987 }
Anjali Singhai Jain5a433192017-06-23 04:24:45 -04008988 } else if ((pf->hw.aq.api_maj_ver > 1) ||
8989 ((pf->hw.aq.api_maj_ver == 1) &&
8990 (pf->hw.aq.api_min_ver > 4))) {
8991 /* Supported in FW API version higher than 1.4 */
Jacob Kellerd36e41d2017-06-23 04:24:46 -04008992 pf->hw_features |= I40E_HW_GENEVE_OFFLOAD_CAPABLE;
Anjali Singhai Jain5a433192017-06-23 04:24:45 -04008993 }
8994
8995 /* Enable HW ATR eviction if possible */
Jacob Kellerd36e41d2017-06-23 04:24:46 -04008996 if (pf->hw_features & I40E_HW_ATR_EVICT_CAPABLE)
Anjali Singhai Jain5a433192017-06-23 04:24:45 -04008997 pf->flags |= I40E_FLAG_HW_ATR_EVICT_ENABLED;
8998
Bimmy Pujari6de432c2016-11-11 12:39:38 -08008999 if ((pf->hw.mac.type == I40E_MAC_XL710) &&
Anjali Singhai Jain8eed76f2015-12-09 15:50:31 -08009000 (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 33)) ||
Neerav Parikhf1bbad32016-01-13 16:51:39 -08009001 (pf->hw.aq.fw_maj_ver < 4))) {
Jacob Kellerd36e41d2017-06-23 04:24:46 -04009002 pf->hw_features |= I40E_HW_RESTART_AUTONEG;
Neerav Parikhf1bbad32016-01-13 16:51:39 -08009003 /* No DCB support for FW < v4.33 */
Jacob Kellerd36e41d2017-06-23 04:24:46 -04009004 pf->hw_features |= I40E_HW_NO_DCB_SUPPORT;
Neerav Parikhf1bbad32016-01-13 16:51:39 -08009005 }
9006
9007 /* Disable FW LLDP if FW < v4.3 */
Bimmy Pujari6de432c2016-11-11 12:39:38 -08009008 if ((pf->hw.mac.type == I40E_MAC_XL710) &&
Neerav Parikhf1bbad32016-01-13 16:51:39 -08009009 (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 3)) ||
9010 (pf->hw.aq.fw_maj_ver < 4)))
Jacob Kellerd36e41d2017-06-23 04:24:46 -04009011 pf->hw_features |= I40E_HW_STOP_FW_LLDP;
Neerav Parikhf1bbad32016-01-13 16:51:39 -08009012
9013 /* Use the FW Set LLDP MIB API if FW > v4.40 */
Bimmy Pujari6de432c2016-11-11 12:39:38 -08009014 if ((pf->hw.mac.type == I40E_MAC_XL710) &&
Neerav Parikhf1bbad32016-01-13 16:51:39 -08009015 (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver >= 40)) ||
9016 (pf->hw.aq.fw_maj_ver >= 5)))
Jacob Kellerd36e41d2017-06-23 04:24:46 -04009017 pf->hw_features |= I40E_HW_USE_SET_LLDP_MIB;
Anjali Singhai Jain8eed76f2015-12-09 15:50:31 -08009018
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009019 if (pf->hw.func_caps.vmdq) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009020 pf->num_vmdq_vsis = I40E_DEFAULT_NUM_VMDQ_VSI;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04009021 pf->flags |= I40E_FLAG_VMDQ_ENABLED;
Jesse Brandeburge9e53662015-10-02 17:57:21 -07009022 pf->num_vmdq_qps = i40e_default_queues_per_vmdq(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009023 }
9024
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06009025 if (pf->hw.func_caps.iwarp) {
9026 pf->flags |= I40E_FLAG_IWARP_ENABLED;
9027 /* IWARP needs one extra vector for CQP just like MISC.*/
9028 pf->num_iwarp_msix = (int)num_online_cpus() + 1;
9029 }
9030
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009031#ifdef CONFIG_PCI_IOV
Shannon Nelsonba252f132014-12-11 07:06:34 +00009032 if (pf->hw.func_caps.num_vfs && pf->hw.partition_id == 1) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009033 pf->num_vf_qps = I40E_DEFAULT_QUEUES_PER_VF;
9034 pf->flags |= I40E_FLAG_SRIOV_ENABLED;
9035 pf->num_req_vfs = min_t(int,
9036 pf->hw.func_caps.num_vfs,
9037 I40E_MAX_VF_COUNT);
9038 }
9039#endif /* CONFIG_PCI_IOV */
9040 pf->eeprom_version = 0xDEAD;
9041 pf->lan_veb = I40E_NO_VEB;
9042 pf->lan_vsi = I40E_NO_VSI;
9043
Anjali Singhai Jaind1a8d272015-07-23 16:54:40 -04009044 /* By default FW has this off for performance reasons */
9045 pf->flags &= ~I40E_FLAG_VEB_STATS_ENABLED;
9046
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009047 /* set up queue assignment tracking */
9048 size = sizeof(struct i40e_lump_tracking)
9049 + (sizeof(u16) * pf->hw.func_caps.num_tx_qp);
9050 pf->qp_pile = kzalloc(size, GFP_KERNEL);
9051 if (!pf->qp_pile) {
9052 err = -ENOMEM;
9053 goto sw_init_done;
9054 }
9055 pf->qp_pile->num_entries = pf->hw.func_caps.num_tx_qp;
9056 pf->qp_pile->search_hint = 0;
9057
Anjali Singhai Jain327fe042014-06-04 01:23:26 +00009058 pf->tx_timeout_recovery_level = 1;
9059
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009060 mutex_init(&pf->switch_mutex);
9061
9062sw_init_done:
9063 return err;
9064}
9065
9066/**
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00009067 * i40e_set_ntuple - set the ntuple feature flag and take action
9068 * @pf: board private structure to initialize
9069 * @features: the feature set that the stack is suggesting
9070 *
9071 * returns a bool to indicate if reset needs to happen
9072 **/
9073bool i40e_set_ntuple(struct i40e_pf *pf, netdev_features_t features)
9074{
9075 bool need_reset = false;
9076
9077 /* Check if Flow Director n-tuple support was enabled or disabled. If
9078 * the state changed, we need to reset.
9079 */
9080 if (features & NETIF_F_NTUPLE) {
9081 /* Enable filters and mark for reset */
9082 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
9083 need_reset = true;
Tushar Davea70e4072016-05-16 12:40:53 -07009084 /* enable FD_SB only if there is MSI-X vector */
9085 if (pf->num_fdsb_msix > 0)
9086 pf->flags |= I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00009087 } else {
9088 /* turn off filters, mark for reset and clear SW filter list */
9089 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
9090 need_reset = true;
9091 i40e_fdir_filter_exit(pf);
9092 }
Jacob Keller47994c12017-04-19 09:25:57 -04009093 pf->flags &= ~(I40E_FLAG_FD_SB_ENABLED |
9094 I40E_FLAG_FD_SB_AUTO_DISABLED);
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00009095 /* reset fd counters */
Jacob Keller097dbf52017-02-06 14:38:46 -08009096 pf->fd_add_err = 0;
9097 pf->fd_atr_cnt = 0;
Anjali Singhai Jain8a4f34f2014-06-04 08:45:20 +00009098 /* if ATR was auto disabled it can be re-enabled. */
Jacob Keller47994c12017-04-19 09:25:57 -04009099 if (pf->flags & I40E_FLAG_FD_ATR_AUTO_DISABLED) {
9100 pf->flags &= ~I40E_FLAG_FD_ATR_AUTO_DISABLED;
9101 if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
9102 (I40E_DEBUG_FD & pf->hw.debug_mask))
Jacob Keller234dc4e2016-09-06 18:05:09 -07009103 dev_info(&pf->pdev->dev, "ATR re-enabled.\n");
9104 }
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00009105 }
9106 return need_reset;
9107}
9108
9109/**
Alan Bradyd8ec9862016-07-27 12:02:38 -07009110 * i40e_clear_rss_lut - clear the rx hash lookup table
9111 * @vsi: the VSI being configured
9112 **/
9113static void i40e_clear_rss_lut(struct i40e_vsi *vsi)
9114{
9115 struct i40e_pf *pf = vsi->back;
9116 struct i40e_hw *hw = &pf->hw;
9117 u16 vf_id = vsi->vf_id;
9118 u8 i;
9119
9120 if (vsi->type == I40E_VSI_MAIN) {
9121 for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
9122 wr32(hw, I40E_PFQF_HLUT(i), 0);
9123 } else if (vsi->type == I40E_VSI_SRIOV) {
9124 for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
9125 i40e_write_rx_ctl(hw, I40E_VFQF_HLUT1(i, vf_id), 0);
9126 } else {
9127 dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
9128 }
9129}
9130
9131/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009132 * i40e_set_features - set the netdev feature flags
9133 * @netdev: ptr to the netdev being adjusted
9134 * @features: the feature set that the stack is suggesting
Maciej Sosin373149f2017-04-05 07:50:55 -04009135 * Note: expects to be called while under rtnl_lock()
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009136 **/
9137static int i40e_set_features(struct net_device *netdev,
9138 netdev_features_t features)
9139{
9140 struct i40e_netdev_priv *np = netdev_priv(netdev);
9141 struct i40e_vsi *vsi = np->vsi;
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00009142 struct i40e_pf *pf = vsi->back;
9143 bool need_reset;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009144
Alan Bradyd8ec9862016-07-27 12:02:38 -07009145 if (features & NETIF_F_RXHASH && !(netdev->features & NETIF_F_RXHASH))
9146 i40e_pf_config_rss(pf);
9147 else if (!(features & NETIF_F_RXHASH) &&
9148 netdev->features & NETIF_F_RXHASH)
9149 i40e_clear_rss_lut(vsi);
9150
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009151 if (features & NETIF_F_HW_VLAN_CTAG_RX)
9152 i40e_vlan_stripping_enable(vsi);
9153 else
9154 i40e_vlan_stripping_disable(vsi);
9155
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00009156 need_reset = i40e_set_ntuple(pf, features);
9157
9158 if (need_reset)
Maciej Sosin373149f2017-04-05 07:50:55 -04009159 i40e_do_reset(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED), true);
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00009160
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009161 return 0;
9162}
9163
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009164/**
Singhai, Anjali6a899022015-12-14 12:21:18 -08009165 * i40e_get_udp_port_idx - Lookup a possibly offloaded for Rx UDP port
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009166 * @pf: board private structure
9167 * @port: The UDP port to look up
9168 *
9169 * Returns the index number or I40E_MAX_PF_UDP_OFFLOAD_PORTS if port not found
9170 **/
Jacob Kellerfe0b0cd2017-02-06 14:38:38 -08009171static u8 i40e_get_udp_port_idx(struct i40e_pf *pf, u16 port)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009172{
9173 u8 i;
9174
9175 for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
Jacob Keller27826fd2017-04-19 09:25:50 -04009176 if (pf->udp_ports[i].port == port)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009177 return i;
9178 }
9179
9180 return i;
9181}
9182
9183/**
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009184 * i40e_udp_tunnel_add - Get notifications about UDP tunnel ports that come up
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009185 * @netdev: This physical port's netdev
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009186 * @ti: Tunnel endpoint information
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009187 **/
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009188static void i40e_udp_tunnel_add(struct net_device *netdev,
9189 struct udp_tunnel_info *ti)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009190{
9191 struct i40e_netdev_priv *np = netdev_priv(netdev);
9192 struct i40e_vsi *vsi = np->vsi;
9193 struct i40e_pf *pf = vsi->back;
Jacob Kellerfe0b0cd2017-02-06 14:38:38 -08009194 u16 port = ntohs(ti->port);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009195 u8 next_idx;
9196 u8 idx;
9197
Singhai, Anjali6a899022015-12-14 12:21:18 -08009198 idx = i40e_get_udp_port_idx(pf, port);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009199
9200 /* Check if port already exists */
9201 if (idx < I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
Jacob Kellerfe0b0cd2017-02-06 14:38:38 -08009202 netdev_info(netdev, "port %d already offloaded\n", port);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009203 return;
9204 }
9205
9206 /* Now check if there is space to add the new port */
Singhai, Anjali6a899022015-12-14 12:21:18 -08009207 next_idx = i40e_get_udp_port_idx(pf, 0);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009208
9209 if (next_idx == I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009210 netdev_info(netdev, "maximum number of offloaded UDP ports reached, not adding port %d\n",
Jacob Kellerfe0b0cd2017-02-06 14:38:38 -08009211 port);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009212 return;
9213 }
9214
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009215 switch (ti->type) {
9216 case UDP_TUNNEL_TYPE_VXLAN:
9217 pf->udp_ports[next_idx].type = I40E_AQC_TUNNEL_TYPE_VXLAN;
9218 break;
9219 case UDP_TUNNEL_TYPE_GENEVE:
Jacob Kellerd36e41d2017-06-23 04:24:46 -04009220 if (!(pf->hw_features & I40E_HW_GENEVE_OFFLOAD_CAPABLE))
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009221 return;
9222 pf->udp_ports[next_idx].type = I40E_AQC_TUNNEL_TYPE_NGE;
9223 break;
9224 default:
9225 return;
9226 }
9227
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009228 /* New port: add it and mark its index in the bitmap */
Jacob Keller27826fd2017-04-19 09:25:50 -04009229 pf->udp_ports[next_idx].port = port;
Singhai, Anjali6a899022015-12-14 12:21:18 -08009230 pf->pending_udp_bitmap |= BIT_ULL(next_idx);
9231 pf->flags |= I40E_FLAG_UDP_FILTER_SYNC;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009232}
9233
9234/**
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009235 * i40e_udp_tunnel_del - Get notifications about UDP tunnel ports that go away
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009236 * @netdev: This physical port's netdev
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009237 * @ti: Tunnel endpoint information
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009238 **/
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009239static void i40e_udp_tunnel_del(struct net_device *netdev,
9240 struct udp_tunnel_info *ti)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009241{
9242 struct i40e_netdev_priv *np = netdev_priv(netdev);
9243 struct i40e_vsi *vsi = np->vsi;
9244 struct i40e_pf *pf = vsi->back;
Jacob Kellerfe0b0cd2017-02-06 14:38:38 -08009245 u16 port = ntohs(ti->port);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009246 u8 idx;
9247
Singhai, Anjali6a899022015-12-14 12:21:18 -08009248 idx = i40e_get_udp_port_idx(pf, port);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009249
9250 /* Check if port already exists */
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009251 if (idx >= I40E_MAX_PF_UDP_OFFLOAD_PORTS)
9252 goto not_found;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009253
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009254 switch (ti->type) {
9255 case UDP_TUNNEL_TYPE_VXLAN:
9256 if (pf->udp_ports[idx].type != I40E_AQC_TUNNEL_TYPE_VXLAN)
9257 goto not_found;
9258 break;
9259 case UDP_TUNNEL_TYPE_GENEVE:
9260 if (pf->udp_ports[idx].type != I40E_AQC_TUNNEL_TYPE_NGE)
9261 goto not_found;
9262 break;
9263 default:
9264 goto not_found;
Singhai, Anjali6a899022015-12-14 12:21:18 -08009265 }
9266
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009267 /* if port exists, set it to 0 (mark for deletion)
9268 * and make it pending
9269 */
Jacob Keller27826fd2017-04-19 09:25:50 -04009270 pf->udp_ports[idx].port = 0;
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009271 pf->pending_udp_bitmap |= BIT_ULL(idx);
Singhai, Anjali6a899022015-12-14 12:21:18 -08009272 pf->flags |= I40E_FLAG_UDP_FILTER_SYNC;
9273
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009274 return;
9275not_found:
9276 netdev_warn(netdev, "UDP port %d was not found, not deleting\n",
Jacob Kellerfe0b0cd2017-02-06 14:38:38 -08009277 port);
Singhai, Anjali6a899022015-12-14 12:21:18 -08009278}
9279
Neerav Parikh1f224ad2014-02-12 01:45:31 +00009280static int i40e_get_phys_port_id(struct net_device *netdev,
Jiri Pirko02637fc2014-11-28 14:34:16 +01009281 struct netdev_phys_item_id *ppid)
Neerav Parikh1f224ad2014-02-12 01:45:31 +00009282{
9283 struct i40e_netdev_priv *np = netdev_priv(netdev);
9284 struct i40e_pf *pf = np->vsi->back;
9285 struct i40e_hw *hw = &pf->hw;
9286
Jacob Kellerd36e41d2017-06-23 04:24:46 -04009287 if (!(pf->hw_features & I40E_HW_PORT_ID_VALID))
Neerav Parikh1f224ad2014-02-12 01:45:31 +00009288 return -EOPNOTSUPP;
9289
9290 ppid->id_len = min_t(int, sizeof(hw->mac.port_addr), sizeof(ppid->id));
9291 memcpy(ppid->id, hw->mac.port_addr, ppid->id_len);
9292
9293 return 0;
9294}
9295
Jesse Brandeburg2f90ade2014-11-20 16:30:02 -08009296/**
9297 * i40e_ndo_fdb_add - add an entry to the hardware database
9298 * @ndm: the input from the stack
9299 * @tb: pointer to array of nladdr (unused)
9300 * @dev: the net device pointer
9301 * @addr: the MAC address entry being added
9302 * @flags: instructions from stack about fdb operation
9303 */
Greg Rose4ba0dea2014-03-06 08:59:55 +00009304static int i40e_ndo_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
9305 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +01009306 const unsigned char *addr, u16 vid,
Greg Rose4ba0dea2014-03-06 08:59:55 +00009307 u16 flags)
Greg Rose4ba0dea2014-03-06 08:59:55 +00009308{
9309 struct i40e_netdev_priv *np = netdev_priv(dev);
9310 struct i40e_pf *pf = np->vsi->back;
9311 int err = 0;
9312
9313 if (!(pf->flags & I40E_FLAG_SRIOV_ENABLED))
9314 return -EOPNOTSUPP;
9315
Or Gerlitz65891fe2014-12-14 18:19:05 +02009316 if (vid) {
9317 pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
9318 return -EINVAL;
9319 }
9320
Greg Rose4ba0dea2014-03-06 08:59:55 +00009321 /* Hardware does not support aging addresses so if a
9322 * ndm_state is given only allow permanent addresses
9323 */
9324 if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
9325 netdev_info(dev, "FDB only supports static addresses\n");
9326 return -EINVAL;
9327 }
9328
9329 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
9330 err = dev_uc_add_excl(dev, addr);
9331 else if (is_multicast_ether_addr(addr))
9332 err = dev_mc_add_excl(dev, addr);
9333 else
9334 err = -EINVAL;
9335
9336 /* Only return duplicate errors if NLM_F_EXCL is set */
9337 if (err == -EEXIST && !(flags & NLM_F_EXCL))
9338 err = 0;
9339
9340 return err;
9341}
9342
Neerav Parikh51616012015-02-06 08:52:14 +00009343/**
9344 * i40e_ndo_bridge_setlink - Set the hardware bridge mode
9345 * @dev: the netdev being configured
9346 * @nlh: RTNL message
9347 *
9348 * Inserts a new hardware bridge if not already created and
9349 * enables the bridging mode requested (VEB or VEPA). If the
9350 * hardware bridge has already been inserted and the request
9351 * is to change the mode then that requires a PF reset to
9352 * allow rebuild of the components with required hardware
9353 * bridge mode enabled.
Maciej Sosin373149f2017-04-05 07:50:55 -04009354 *
9355 * Note: expects to be called while under rtnl_lock()
Neerav Parikh51616012015-02-06 08:52:14 +00009356 **/
9357static int i40e_ndo_bridge_setlink(struct net_device *dev,
Carolyn Wyborny9df70b62015-04-27 14:57:11 -04009358 struct nlmsghdr *nlh,
9359 u16 flags)
Neerav Parikh51616012015-02-06 08:52:14 +00009360{
9361 struct i40e_netdev_priv *np = netdev_priv(dev);
9362 struct i40e_vsi *vsi = np->vsi;
9363 struct i40e_pf *pf = vsi->back;
9364 struct i40e_veb *veb = NULL;
9365 struct nlattr *attr, *br_spec;
9366 int i, rem;
9367
9368 /* Only for PF VSI for now */
9369 if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
9370 return -EOPNOTSUPP;
9371
9372 /* Find the HW bridge for PF VSI */
9373 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
9374 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
9375 veb = pf->veb[i];
9376 }
9377
9378 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
9379
9380 nla_for_each_nested(attr, br_spec, rem) {
9381 __u16 mode;
9382
9383 if (nla_type(attr) != IFLA_BRIDGE_MODE)
9384 continue;
9385
9386 mode = nla_get_u16(attr);
9387 if ((mode != BRIDGE_MODE_VEPA) &&
9388 (mode != BRIDGE_MODE_VEB))
9389 return -EINVAL;
9390
9391 /* Insert a new HW bridge */
9392 if (!veb) {
9393 veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
9394 vsi->tc_config.enabled_tc);
9395 if (veb) {
9396 veb->bridge_mode = mode;
9397 i40e_config_bridge_mode(veb);
9398 } else {
9399 /* No Bridge HW offload available */
9400 return -ENOENT;
9401 }
9402 break;
9403 } else if (mode != veb->bridge_mode) {
9404 /* Existing HW bridge but different mode needs reset */
9405 veb->bridge_mode = mode;
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07009406 /* TODO: If no VFs or VMDq VSIs, disallow VEB mode */
9407 if (mode == BRIDGE_MODE_VEB)
9408 pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
9409 else
9410 pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
Maciej Sosin373149f2017-04-05 07:50:55 -04009411 i40e_do_reset(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED),
9412 true);
Neerav Parikh51616012015-02-06 08:52:14 +00009413 break;
9414 }
9415 }
9416
9417 return 0;
9418}
9419
9420/**
9421 * i40e_ndo_bridge_getlink - Get the hardware bridge mode
9422 * @skb: skb buff
9423 * @pid: process id
9424 * @seq: RTNL message seq #
9425 * @dev: the netdev being configured
9426 * @filter_mask: unused
Jesse Brandeburgd4b2f9f2015-09-03 17:18:48 -04009427 * @nlflags: netlink flags passed in
Neerav Parikh51616012015-02-06 08:52:14 +00009428 *
9429 * Return the mode in which the hardware bridge is operating in
9430 * i.e VEB or VEPA.
9431 **/
Neerav Parikh51616012015-02-06 08:52:14 +00009432static int i40e_ndo_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
9433 struct net_device *dev,
Carolyn Wyborny9f4ffc42015-08-31 19:54:42 -04009434 u32 __always_unused filter_mask,
9435 int nlflags)
Neerav Parikh51616012015-02-06 08:52:14 +00009436{
9437 struct i40e_netdev_priv *np = netdev_priv(dev);
9438 struct i40e_vsi *vsi = np->vsi;
9439 struct i40e_pf *pf = vsi->back;
9440 struct i40e_veb *veb = NULL;
9441 int i;
9442
9443 /* Only for PF VSI for now */
9444 if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
9445 return -EOPNOTSUPP;
9446
9447 /* Find the HW bridge for the PF VSI */
9448 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
9449 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
9450 veb = pf->veb[i];
9451 }
9452
9453 if (!veb)
9454 return 0;
9455
Nicolas Dichtel46c264d2015-04-28 18:33:49 +02009456 return ndo_dflt_bridge_getlink(skb, pid, seq, dev, veb->bridge_mode,
Huaibin Wang599b0762016-09-26 09:51:18 +02009457 0, 0, nlflags, filter_mask, NULL);
Neerav Parikh51616012015-02-06 08:52:14 +00009458}
Neerav Parikh51616012015-02-06 08:52:14 +00009459
Joe Stringerf44a75e2015-04-14 17:09:14 -07009460/**
9461 * i40e_features_check - Validate encapsulated packet conforms to limits
9462 * @skb: skb buff
Jean Sacren2bc11c62015-09-19 05:08:43 -06009463 * @dev: This physical port's netdev
Joe Stringerf44a75e2015-04-14 17:09:14 -07009464 * @features: Offload features that the stack believes apply
9465 **/
9466static netdev_features_t i40e_features_check(struct sk_buff *skb,
9467 struct net_device *dev,
9468 netdev_features_t features)
9469{
Alexander Duyckf114dca2016-10-25 16:08:46 -07009470 size_t len;
9471
9472 /* No point in doing any of this if neither checksum nor GSO are
9473 * being requested for this frame. We can rule out both by just
9474 * checking for CHECKSUM_PARTIAL
9475 */
9476 if (skb->ip_summed != CHECKSUM_PARTIAL)
9477 return features;
9478
9479 /* We cannot support GSO if the MSS is going to be less than
9480 * 64 bytes. If it is then we need to drop support for GSO.
9481 */
9482 if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
9483 features &= ~NETIF_F_GSO_MASK;
9484
9485 /* MACLEN can support at most 63 words */
9486 len = skb_network_header(skb) - skb->data;
9487 if (len & ~(63 * 2))
9488 goto out_err;
9489
9490 /* IPLEN and EIPLEN can support at most 127 dwords */
9491 len = skb_transport_header(skb) - skb_network_header(skb);
9492 if (len & ~(127 * 4))
9493 goto out_err;
9494
9495 if (skb->encapsulation) {
9496 /* L4TUNLEN can support 127 words */
9497 len = skb_inner_network_header(skb) - skb_transport_header(skb);
9498 if (len & ~(127 * 2))
9499 goto out_err;
9500
9501 /* IPLEN can support at most 127 dwords */
9502 len = skb_inner_transport_header(skb) -
9503 skb_inner_network_header(skb);
9504 if (len & ~(127 * 4))
9505 goto out_err;
9506 }
9507
9508 /* No need to validate L4LEN as TCP is the only protocol with a
9509 * a flexible value and we support all possible values supported
9510 * by TCP, which is at most 15 dwords
9511 */
Joe Stringerf44a75e2015-04-14 17:09:14 -07009512
9513 return features;
Alexander Duyckf114dca2016-10-25 16:08:46 -07009514out_err:
9515 return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
Joe Stringerf44a75e2015-04-14 17:09:14 -07009516}
9517
Björn Töpel0c8493d2017-05-24 07:55:34 +02009518/**
9519 * i40e_xdp_setup - add/remove an XDP program
9520 * @vsi: VSI to changed
9521 * @prog: XDP program
9522 **/
9523static int i40e_xdp_setup(struct i40e_vsi *vsi,
9524 struct bpf_prog *prog)
9525{
9526 int frame_size = vsi->netdev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
9527 struct i40e_pf *pf = vsi->back;
9528 struct bpf_prog *old_prog;
9529 bool need_reset;
9530 int i;
9531
9532 /* Don't allow frames that span over multiple buffers */
9533 if (frame_size > vsi->rx_buf_len)
9534 return -EINVAL;
9535
9536 if (!i40e_enabled_xdp_vsi(vsi) && !prog)
9537 return 0;
9538
9539 /* When turning XDP on->off/off->on we reset and rebuild the rings. */
9540 need_reset = (i40e_enabled_xdp_vsi(vsi) != !!prog);
9541
9542 if (need_reset)
9543 i40e_prep_for_reset(pf, true);
9544
9545 old_prog = xchg(&vsi->xdp_prog, prog);
9546
9547 if (need_reset)
9548 i40e_reset_and_rebuild(pf, true, true);
9549
9550 for (i = 0; i < vsi->num_queue_pairs; i++)
9551 WRITE_ONCE(vsi->rx_rings[i]->xdp_prog, vsi->xdp_prog);
9552
9553 if (old_prog)
9554 bpf_prog_put(old_prog);
9555
9556 return 0;
9557}
9558
9559/**
9560 * i40e_xdp - implements ndo_xdp for i40e
9561 * @dev: netdevice
9562 * @xdp: XDP command
9563 **/
9564static int i40e_xdp(struct net_device *dev,
9565 struct netdev_xdp *xdp)
9566{
9567 struct i40e_netdev_priv *np = netdev_priv(dev);
9568 struct i40e_vsi *vsi = np->vsi;
9569
9570 if (vsi->type != I40E_VSI_MAIN)
9571 return -EINVAL;
9572
9573 switch (xdp->command) {
9574 case XDP_SETUP_PROG:
9575 return i40e_xdp_setup(vsi, xdp->prog);
9576 case XDP_QUERY_PROG:
9577 xdp->prog_attached = i40e_enabled_xdp_vsi(vsi);
Daniel Borkmanneb230392017-06-24 21:13:52 +02009578 xdp->prog_id = vsi->xdp_prog ? vsi->xdp_prog->aux->id : 0;
Björn Töpel0c8493d2017-05-24 07:55:34 +02009579 return 0;
9580 default:
9581 return -EINVAL;
9582 }
9583}
9584
Shannon Nelson37a29732015-02-27 09:15:19 +00009585static const struct net_device_ops i40e_netdev_ops = {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009586 .ndo_open = i40e_open,
9587 .ndo_stop = i40e_close,
9588 .ndo_start_xmit = i40e_lan_xmit_frame,
9589 .ndo_get_stats64 = i40e_get_netdev_stats_struct,
9590 .ndo_set_rx_mode = i40e_set_rx_mode,
9591 .ndo_validate_addr = eth_validate_addr,
9592 .ndo_set_mac_address = i40e_set_mac,
9593 .ndo_change_mtu = i40e_change_mtu,
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00009594 .ndo_do_ioctl = i40e_ioctl,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009595 .ndo_tx_timeout = i40e_tx_timeout,
9596 .ndo_vlan_rx_add_vid = i40e_vlan_rx_add_vid,
9597 .ndo_vlan_rx_kill_vid = i40e_vlan_rx_kill_vid,
9598#ifdef CONFIG_NET_POLL_CONTROLLER
9599 .ndo_poll_controller = i40e_netpoll,
9600#endif
John Fastabende4c67342016-02-16 21:16:15 -08009601 .ndo_setup_tc = __i40e_setup_tc,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009602 .ndo_set_features = i40e_set_features,
9603 .ndo_set_vf_mac = i40e_ndo_set_vf_mac,
9604 .ndo_set_vf_vlan = i40e_ndo_set_vf_port_vlan,
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04009605 .ndo_set_vf_rate = i40e_ndo_set_vf_bw,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009606 .ndo_get_vf_config = i40e_ndo_get_vf_config,
Mitch Williams588aefa2014-02-11 08:27:49 +00009607 .ndo_set_vf_link_state = i40e_ndo_set_vf_link_state,
Serey Konge6d90042014-07-12 07:28:14 +00009608 .ndo_set_vf_spoofchk = i40e_ndo_set_vf_spoofchk,
Anjali Singhai Jainc3bbbd22016-04-01 03:56:07 -07009609 .ndo_set_vf_trust = i40e_ndo_set_vf_trust,
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009610 .ndo_udp_tunnel_add = i40e_udp_tunnel_add,
9611 .ndo_udp_tunnel_del = i40e_udp_tunnel_del,
Neerav Parikh1f224ad2014-02-12 01:45:31 +00009612 .ndo_get_phys_port_id = i40e_get_phys_port_id,
Greg Rose4ba0dea2014-03-06 08:59:55 +00009613 .ndo_fdb_add = i40e_ndo_fdb_add,
Joe Stringerf44a75e2015-04-14 17:09:14 -07009614 .ndo_features_check = i40e_features_check,
Neerav Parikh51616012015-02-06 08:52:14 +00009615 .ndo_bridge_getlink = i40e_ndo_bridge_getlink,
9616 .ndo_bridge_setlink = i40e_ndo_bridge_setlink,
Björn Töpel0c8493d2017-05-24 07:55:34 +02009617 .ndo_xdp = i40e_xdp,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009618};
9619
9620/**
9621 * i40e_config_netdev - Setup the netdev flags
9622 * @vsi: the VSI being configured
9623 *
9624 * Returns 0 on success, negative value on failure
9625 **/
9626static int i40e_config_netdev(struct i40e_vsi *vsi)
9627{
9628 struct i40e_pf *pf = vsi->back;
9629 struct i40e_hw *hw = &pf->hw;
9630 struct i40e_netdev_priv *np;
9631 struct net_device *netdev;
Jacob Keller435c0842016-11-08 13:05:10 -08009632 u8 broadcast[ETH_ALEN];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009633 u8 mac_addr[ETH_ALEN];
9634 int etherdev_size;
Preethi Banalabacd75c2017-03-27 14:43:18 -07009635 netdev_features_t hw_enc_features;
9636 netdev_features_t hw_features;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009637
9638 etherdev_size = sizeof(struct i40e_netdev_priv);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00009639 netdev = alloc_etherdev_mq(etherdev_size, vsi->alloc_queue_pairs);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009640 if (!netdev)
9641 return -ENOMEM;
9642
9643 vsi->netdev = netdev;
9644 np = netdev_priv(netdev);
9645 np->vsi = vsi;
9646
Preethi Banalabacd75c2017-03-27 14:43:18 -07009647 hw_enc_features = NETIF_F_SG |
9648 NETIF_F_IP_CSUM |
9649 NETIF_F_IPV6_CSUM |
9650 NETIF_F_HIGHDMA |
9651 NETIF_F_SOFT_FEATURES |
9652 NETIF_F_TSO |
9653 NETIF_F_TSO_ECN |
9654 NETIF_F_TSO6 |
9655 NETIF_F_GSO_GRE |
9656 NETIF_F_GSO_GRE_CSUM |
9657 NETIF_F_GSO_PARTIAL |
9658 NETIF_F_GSO_UDP_TUNNEL |
9659 NETIF_F_GSO_UDP_TUNNEL_CSUM |
9660 NETIF_F_SCTP_CRC |
9661 NETIF_F_RXHASH |
9662 NETIF_F_RXCSUM |
9663 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009664
Jacob Kellerd36e41d2017-06-23 04:24:46 -04009665 if (!(pf->hw_features & I40E_HW_OUTER_UDP_CSUM_CAPABLE))
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04009666 netdev->gso_partial_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM;
9667
9668 netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009669
Preethi Banalabacd75c2017-03-27 14:43:18 -07009670 netdev->hw_enc_features |= hw_enc_features;
9671
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009672 /* record features VLANs can make use of */
Preethi Banalabacd75c2017-03-27 14:43:18 -07009673 netdev->vlan_features |= hw_enc_features | NETIF_F_TSO_MANGLEID;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009674
Anjali Singhai Jain2e86a0b2014-04-01 07:11:53 +00009675 if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009676 netdev->hw_features |= NETIF_F_NTUPLE;
Preethi Banalabacd75c2017-03-27 14:43:18 -07009677 hw_features = hw_enc_features |
9678 NETIF_F_HW_VLAN_CTAG_TX |
9679 NETIF_F_HW_VLAN_CTAG_RX;
Anjali Singhai Jain2e86a0b2014-04-01 07:11:53 +00009680
Preethi Banalabacd75c2017-03-27 14:43:18 -07009681 netdev->hw_features |= hw_features;
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009682
Preethi Banalabacd75c2017-03-27 14:43:18 -07009683 netdev->features |= hw_features | NETIF_F_HW_VLAN_CTAG_FILTER;
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04009684 netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009685
9686 if (vsi->type == I40E_VSI_MAIN) {
9687 SET_NETDEV_DEV(netdev, &pf->pdev->dev);
Greg Rose9a173902014-05-22 06:32:02 +00009688 ether_addr_copy(mac_addr, hw->mac.perm_addr);
Jacob Keller41c4c2b2017-04-05 07:50:57 -04009689 /* The following steps are necessary for two reasons. First,
9690 * some older NVM configurations load a default MAC-VLAN
9691 * filter that will accept any tagged packet, and we want to
9692 * replace this with a normal filter. Additionally, it is
9693 * possible our MAC address was provided by the platform using
9694 * Open Firmware or similar.
9695 *
9696 * Thus, we need to remove the default filter and install one
9697 * specific to the MAC address.
Jacob Keller1596b5d2016-11-08 13:05:15 -08009698 */
9699 i40e_rm_default_mac_filter(vsi, mac_addr);
Jacob Keller278e7d02016-10-05 09:30:37 -07009700 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller9569a9a2016-11-11 12:39:37 -08009701 i40e_add_mac_filter(vsi, mac_addr);
Jacob Keller278e7d02016-10-05 09:30:37 -07009702 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009703 } else {
Jacob Keller8c9eb352017-07-12 05:46:12 -04009704 /* Relate the VSI_VMDQ name to the VSI_MAIN name. Note that we
9705 * are still limited by IFNAMSIZ, but we're adding 'v%d\0' to
9706 * the end, which is 4 bytes long, so force truncation of the
9707 * original name by IFNAMSIZ - 4
9708 */
9709 snprintf(netdev->name, IFNAMSIZ, "%.*sv%%d",
9710 IFNAMSIZ - 4,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009711 pf->vsi[pf->lan_vsi]->netdev->name);
9712 random_ether_addr(mac_addr);
Kiran Patil21659032015-09-30 14:09:03 -04009713
Jacob Keller278e7d02016-10-05 09:30:37 -07009714 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller9569a9a2016-11-11 12:39:37 -08009715 i40e_add_mac_filter(vsi, mac_addr);
Jacob Keller278e7d02016-10-05 09:30:37 -07009716 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009717 }
Kiran Patil21659032015-09-30 14:09:03 -04009718
Jacob Keller435c0842016-11-08 13:05:10 -08009719 /* Add the broadcast filter so that we initially will receive
9720 * broadcast packets. Note that when a new VLAN is first added the
9721 * driver will convert all filters marked I40E_VLAN_ANY into VLAN
9722 * specific filters as part of transitioning into "vlan" operation.
9723 * When more VLANs are added, the driver will copy each existing MAC
9724 * filter and add it for the new VLAN.
9725 *
9726 * Broadcast filters are handled specially by
9727 * i40e_sync_filters_subtask, as the driver must to set the broadcast
9728 * promiscuous bit instead of adding this directly as a MAC/VLAN
9729 * filter. The subtask will update the correct broadcast promiscuous
9730 * bits as VLANs become active or inactive.
9731 */
9732 eth_broadcast_addr(broadcast);
9733 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller9569a9a2016-11-11 12:39:37 -08009734 i40e_add_mac_filter(vsi, broadcast);
Jacob Keller435c0842016-11-08 13:05:10 -08009735 spin_unlock_bh(&vsi->mac_filter_hash_lock);
9736
Greg Rose9a173902014-05-22 06:32:02 +00009737 ether_addr_copy(netdev->dev_addr, mac_addr);
9738 ether_addr_copy(netdev->perm_addr, mac_addr);
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009739
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009740 netdev->priv_flags |= IFF_UNICAST_FLT;
9741 netdev->priv_flags |= IFF_SUPP_NOFCS;
9742 /* Setup netdev TC information */
9743 i40e_vsi_config_netdev_tc(vsi, vsi->tc_config.enabled_tc);
9744
9745 netdev->netdev_ops = &i40e_netdev_ops;
9746 netdev->watchdog_timeo = 5 * HZ;
9747 i40e_set_ethtool_ops(netdev);
9748
Jarod Wilson91c527a2016-10-17 15:54:05 -04009749 /* MTU range: 68 - 9706 */
9750 netdev->min_mtu = ETH_MIN_MTU;
Mitch Williams1e3a5fd2017-06-23 04:24:43 -04009751 netdev->max_mtu = I40E_MAX_RXBUFFER - I40E_PACKET_HDR_PAD;
Jarod Wilson91c527a2016-10-17 15:54:05 -04009752
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009753 return 0;
9754}
9755
9756/**
9757 * i40e_vsi_delete - Delete a VSI from the switch
9758 * @vsi: the VSI being removed
9759 *
9760 * Returns 0 on success, negative value on failure
9761 **/
9762static void i40e_vsi_delete(struct i40e_vsi *vsi)
9763{
9764 /* remove default VSI is not allowed */
9765 if (vsi == vsi->back->vsi[vsi->back->lan_vsi])
9766 return;
9767
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009768 i40e_aq_delete_element(&vsi->back->hw, vsi->seid, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009769}
9770
9771/**
Neerav Parikh51616012015-02-06 08:52:14 +00009772 * i40e_is_vsi_uplink_mode_veb - Check if the VSI's uplink bridge mode is VEB
9773 * @vsi: the VSI being queried
9774 *
9775 * Returns 1 if HW bridge mode is VEB and return 0 in case of VEPA mode
9776 **/
9777int i40e_is_vsi_uplink_mode_veb(struct i40e_vsi *vsi)
9778{
9779 struct i40e_veb *veb;
9780 struct i40e_pf *pf = vsi->back;
9781
9782 /* Uplink is not a bridge so default to VEB */
9783 if (vsi->veb_idx == I40E_NO_VEB)
9784 return 1;
9785
9786 veb = pf->veb[vsi->veb_idx];
Akeem G Abodunrin09603ea2015-10-01 14:37:36 -04009787 if (!veb) {
9788 dev_info(&pf->pdev->dev,
9789 "There is no veb associated with the bridge\n");
9790 return -ENOENT;
9791 }
Neerav Parikh51616012015-02-06 08:52:14 +00009792
Akeem G Abodunrin09603ea2015-10-01 14:37:36 -04009793 /* Uplink is a bridge in VEPA mode */
9794 if (veb->bridge_mode & BRIDGE_MODE_VEPA) {
9795 return 0;
9796 } else {
9797 /* Uplink is a bridge in VEB mode */
9798 return 1;
9799 }
9800
9801 /* VEPA is now default bridge, so return 0 */
9802 return 0;
Neerav Parikh51616012015-02-06 08:52:14 +00009803}
9804
9805/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009806 * i40e_add_vsi - Add a VSI to the switch
9807 * @vsi: the VSI being configured
9808 *
9809 * This initializes a VSI context depending on the VSI type to be added and
9810 * passes it down to the add_vsi aq command.
9811 **/
9812static int i40e_add_vsi(struct i40e_vsi *vsi)
9813{
9814 int ret = -ENODEV;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009815 struct i40e_pf *pf = vsi->back;
9816 struct i40e_hw *hw = &pf->hw;
9817 struct i40e_vsi_context ctxt;
Jacob Keller278e7d02016-10-05 09:30:37 -07009818 struct i40e_mac_filter *f;
9819 struct hlist_node *h;
9820 int bkt;
Kiran Patil21659032015-09-30 14:09:03 -04009821
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009822 u8 enabled_tc = 0x1; /* TC0 enabled */
9823 int f_count = 0;
9824
9825 memset(&ctxt, 0, sizeof(ctxt));
9826 switch (vsi->type) {
9827 case I40E_VSI_MAIN:
9828 /* The PF's main VSI is already setup as part of the
9829 * device initialization, so we'll not bother with
9830 * the add_vsi call, but we will retrieve the current
9831 * VSI context.
9832 */
9833 ctxt.seid = pf->main_vsi_seid;
9834 ctxt.pf_num = pf->hw.pf_id;
9835 ctxt.vf_num = 0;
9836 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
9837 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
9838 if (ret) {
9839 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009840 "couldn't get PF vsi config, err %s aq_err %s\n",
9841 i40e_stat_str(&pf->hw, ret),
9842 i40e_aq_str(&pf->hw,
9843 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009844 return -ENOENT;
9845 }
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07009846 vsi->info = ctxt.info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009847 vsi->info.valid_sections = 0;
9848
9849 vsi->seid = ctxt.seid;
9850 vsi->id = ctxt.vsi_number;
9851
9852 enabled_tc = i40e_pf_get_tc_map(pf);
9853
9854 /* MFP mode setup queue map and update VSI */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00009855 if ((pf->flags & I40E_FLAG_MFP_ENABLED) &&
9856 !(pf->hw.func_caps.iscsi)) { /* NIC type PF */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009857 memset(&ctxt, 0, sizeof(ctxt));
9858 ctxt.seid = pf->main_vsi_seid;
9859 ctxt.pf_num = pf->hw.pf_id;
9860 ctxt.vf_num = 0;
9861 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
9862 ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
9863 if (ret) {
9864 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009865 "update vsi failed, err %s aq_err %s\n",
9866 i40e_stat_str(&pf->hw, ret),
9867 i40e_aq_str(&pf->hw,
9868 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009869 ret = -ENOENT;
9870 goto err;
9871 }
9872 /* update the local VSI info queue map */
9873 i40e_vsi_update_queue_map(vsi, &ctxt);
9874 vsi->info.valid_sections = 0;
9875 } else {
9876 /* Default/Main VSI is only enabled for TC0
9877 * reconfigure it to enable all TCs that are
9878 * available on the port in SFP mode.
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00009879 * For MFP case the iSCSI PF would use this
9880 * flow to enable LAN+iSCSI TC.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009881 */
9882 ret = i40e_vsi_config_tc(vsi, enabled_tc);
9883 if (ret) {
Carolyn Wyborny19279232017-07-14 09:10:07 -04009884 /* Single TC condition is not fatal,
9885 * message and continue
9886 */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009887 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009888 "failed to configure TCs for main VSI tc_map 0x%08x, err %s aq_err %s\n",
9889 enabled_tc,
9890 i40e_stat_str(&pf->hw, ret),
9891 i40e_aq_str(&pf->hw,
9892 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009893 }
9894 }
9895 break;
9896
9897 case I40E_VSI_FDIR:
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009898 ctxt.pf_num = hw->pf_id;
9899 ctxt.vf_num = 0;
9900 ctxt.uplink_seid = vsi->uplink_seid;
Neerav Parikh2b18e592015-01-24 09:58:38 +00009901 ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009902 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07009903 if ((pf->flags & I40E_FLAG_VEB_MODE_ENABLED) &&
9904 (i40e_is_vsi_uplink_mode_veb(vsi))) {
Neerav Parikh51616012015-02-06 08:52:14 +00009905 ctxt.info.valid_sections |=
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07009906 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
Neerav Parikh51616012015-02-06 08:52:14 +00009907 ctxt.info.switch_id =
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07009908 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
Neerav Parikh51616012015-02-06 08:52:14 +00009909 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009910 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009911 break;
9912
9913 case I40E_VSI_VMDQ2:
9914 ctxt.pf_num = hw->pf_id;
9915 ctxt.vf_num = 0;
9916 ctxt.uplink_seid = vsi->uplink_seid;
Neerav Parikh2b18e592015-01-24 09:58:38 +00009917 ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009918 ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
9919
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009920 /* This VSI is connected to VEB so the switch_id
9921 * should be set to zero by default.
9922 */
Neerav Parikh51616012015-02-06 08:52:14 +00009923 if (i40e_is_vsi_uplink_mode_veb(vsi)) {
9924 ctxt.info.valid_sections |=
9925 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
9926 ctxt.info.switch_id =
9927 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
9928 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009929
9930 /* Setup the VSI tx/rx queue map for TC0 only for now */
9931 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
9932 break;
9933
9934 case I40E_VSI_SRIOV:
9935 ctxt.pf_num = hw->pf_id;
9936 ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
9937 ctxt.uplink_seid = vsi->uplink_seid;
Neerav Parikh2b18e592015-01-24 09:58:38 +00009938 ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009939 ctxt.flags = I40E_AQ_VSI_TYPE_VF;
9940
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009941 /* This VSI is connected to VEB so the switch_id
9942 * should be set to zero by default.
9943 */
Neerav Parikh51616012015-02-06 08:52:14 +00009944 if (i40e_is_vsi_uplink_mode_veb(vsi)) {
9945 ctxt.info.valid_sections |=
9946 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
9947 ctxt.info.switch_id =
9948 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
9949 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009950
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06009951 if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
9952 ctxt.info.valid_sections |=
9953 cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
9954 ctxt.info.queueing_opt_flags |=
Ashish Shah4b28cdb2016-05-03 15:13:17 -07009955 (I40E_AQ_VSI_QUE_OPT_TCP_ENA |
9956 I40E_AQ_VSI_QUE_OPT_RSS_LUT_VSI);
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06009957 }
9958
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009959 ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
9960 ctxt.info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_MODE_ALL;
Mitch Williamsc674d122014-05-20 08:01:40 +00009961 if (pf->vf[vsi->vf_id].spoofchk) {
9962 ctxt.info.valid_sections |=
9963 cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
9964 ctxt.info.sec_flags |=
9965 (I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
9966 I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
9967 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009968 /* Setup the VSI tx/rx queue map for TC0 only for now */
9969 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
9970 break;
9971
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06009972 case I40E_VSI_IWARP:
9973 /* send down message to iWARP */
9974 break;
9975
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009976 default:
9977 return -ENODEV;
9978 }
9979
9980 if (vsi->type != I40E_VSI_MAIN) {
9981 ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
9982 if (ret) {
9983 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009984 "add vsi failed, err %s aq_err %s\n",
9985 i40e_stat_str(&pf->hw, ret),
9986 i40e_aq_str(&pf->hw,
9987 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009988 ret = -ENOENT;
9989 goto err;
9990 }
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07009991 vsi->info = ctxt.info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009992 vsi->info.valid_sections = 0;
9993 vsi->seid = ctxt.seid;
9994 vsi->id = ctxt.vsi_number;
9995 }
9996
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07009997 vsi->active_filters = 0;
Jacob Keller0da36b92017-04-19 09:25:55 -04009998 clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
Jacob Keller278e7d02016-10-05 09:30:37 -07009999 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010000 /* If macvlan filters already exist, force them to get loaded */
Jacob Keller278e7d02016-10-05 09:30:37 -070010001 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -070010002 f->state = I40E_FILTER_NEW;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010003 f_count++;
10004 }
Jacob Keller278e7d02016-10-05 09:30:37 -070010005 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -040010006
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010007 if (f_count) {
10008 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
10009 pf->flags |= I40E_FLAG_FILTER_SYNC;
10010 }
10011
10012 /* Update VSI BW information */
10013 ret = i40e_vsi_get_bw_info(vsi);
10014 if (ret) {
10015 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010016 "couldn't get vsi bw info, err %s aq_err %s\n",
10017 i40e_stat_str(&pf->hw, ret),
10018 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010019 /* VSI is already added so not tearing that up */
10020 ret = 0;
10021 }
10022
10023err:
10024 return ret;
10025}
10026
10027/**
10028 * i40e_vsi_release - Delete a VSI and free its resources
10029 * @vsi: the VSI being removed
10030 *
10031 * Returns 0 on success or < 0 on error
10032 **/
10033int i40e_vsi_release(struct i40e_vsi *vsi)
10034{
Jacob Keller278e7d02016-10-05 09:30:37 -070010035 struct i40e_mac_filter *f;
10036 struct hlist_node *h;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010037 struct i40e_veb *veb = NULL;
10038 struct i40e_pf *pf;
10039 u16 uplink_seid;
Jacob Keller278e7d02016-10-05 09:30:37 -070010040 int i, n, bkt;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010041
10042 pf = vsi->back;
10043
10044 /* release of a VEB-owner or last VSI is not allowed */
10045 if (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) {
10046 dev_info(&pf->pdev->dev, "VSI %d has existing VEB %d\n",
10047 vsi->seid, vsi->uplink_seid);
10048 return -ENODEV;
10049 }
10050 if (vsi == pf->vsi[pf->lan_vsi] &&
Mauro S. M. Rodrigues9e6c9c02017-05-12 23:26:56 -030010051 !test_bit(__I40E_DOWN, pf->state)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010052 dev_info(&pf->pdev->dev, "Can't remove PF VSI\n");
10053 return -ENODEV;
10054 }
10055
10056 uplink_seid = vsi->uplink_seid;
10057 if (vsi->type != I40E_VSI_SRIOV) {
10058 if (vsi->netdev_registered) {
10059 vsi->netdev_registered = false;
10060 if (vsi->netdev) {
10061 /* results in a call to i40e_close() */
10062 unregister_netdev(vsi->netdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010063 }
10064 } else {
Shannon Nelson90ef8d42014-03-14 07:32:26 +000010065 i40e_vsi_close(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010066 }
10067 i40e_vsi_disable_irq(vsi);
10068 }
10069
Jacob Keller278e7d02016-10-05 09:30:37 -070010070 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller6622f5c2016-10-05 09:30:32 -070010071
10072 /* clear the sync flag on all filters */
10073 if (vsi->netdev) {
10074 __dev_uc_unsync(vsi->netdev, NULL);
10075 __dev_mc_unsync(vsi->netdev, NULL);
10076 }
10077
10078 /* make sure any remaining filters are marked for deletion */
Jacob Keller278e7d02016-10-05 09:30:37 -070010079 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
Jacob Keller290d2552016-10-05 09:30:36 -070010080 __i40e_del_filter(vsi, f);
Jacob Keller6622f5c2016-10-05 09:30:32 -070010081
Jacob Keller278e7d02016-10-05 09:30:37 -070010082 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -040010083
Jesse Brandeburg17652c62015-11-05 17:01:02 -080010084 i40e_sync_vsi_filters(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010085
10086 i40e_vsi_delete(vsi);
10087 i40e_vsi_free_q_vectors(vsi);
Shannon Nelsona4866592014-02-11 08:24:07 +000010088 if (vsi->netdev) {
10089 free_netdev(vsi->netdev);
10090 vsi->netdev = NULL;
10091 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010092 i40e_vsi_clear_rings(vsi);
10093 i40e_vsi_clear(vsi);
10094
10095 /* If this was the last thing on the VEB, except for the
10096 * controlling VSI, remove the VEB, which puts the controlling
10097 * VSI onto the next level down in the switch.
10098 *
10099 * Well, okay, there's one more exception here: don't remove
10100 * the orphan VEBs yet. We'll wait for an explicit remove request
10101 * from up the network stack.
10102 */
Mitch Williams505682c2014-05-20 08:01:37 +000010103 for (n = 0, i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010104 if (pf->vsi[i] &&
10105 pf->vsi[i]->uplink_seid == uplink_seid &&
10106 (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
10107 n++; /* count the VSIs */
10108 }
10109 }
10110 for (i = 0; i < I40E_MAX_VEB; i++) {
10111 if (!pf->veb[i])
10112 continue;
10113 if (pf->veb[i]->uplink_seid == uplink_seid)
10114 n++; /* count the VEBs */
10115 if (pf->veb[i]->seid == uplink_seid)
10116 veb = pf->veb[i];
10117 }
10118 if (n == 0 && veb && veb->uplink_seid != 0)
10119 i40e_veb_release(veb);
10120
10121 return 0;
10122}
10123
10124/**
10125 * i40e_vsi_setup_vectors - Set up the q_vectors for the given VSI
10126 * @vsi: ptr to the VSI
10127 *
10128 * This should only be called after i40e_vsi_mem_alloc() which allocates the
10129 * corresponding SW VSI structure and initializes num_queue_pairs for the
10130 * newly allocated VSI.
10131 *
10132 * Returns 0 on success or negative on failure
10133 **/
10134static int i40e_vsi_setup_vectors(struct i40e_vsi *vsi)
10135{
10136 int ret = -ENOENT;
10137 struct i40e_pf *pf = vsi->back;
10138
Alexander Duyck493fb302013-09-28 07:01:44 +000010139 if (vsi->q_vectors[0]) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010140 dev_info(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
10141 vsi->seid);
10142 return -EEXIST;
10143 }
10144
10145 if (vsi->base_vector) {
Jesse Brandeburgf29eaa32014-02-11 08:24:12 +000010146 dev_info(&pf->pdev->dev, "VSI %d has non-zero base vector %d\n",
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010147 vsi->seid, vsi->base_vector);
10148 return -EEXIST;
10149 }
10150
Greg Rose90e04072014-03-06 08:59:57 +000010151 ret = i40e_vsi_alloc_q_vectors(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010152 if (ret) {
10153 dev_info(&pf->pdev->dev,
10154 "failed to allocate %d q_vector for VSI %d, ret=%d\n",
10155 vsi->num_q_vectors, vsi->seid, ret);
10156 vsi->num_q_vectors = 0;
10157 goto vector_setup_out;
10158 }
10159
Anjali Singhai Jain26cdc442015-07-10 19:36:00 -040010160 /* In Legacy mode, we do not have to get any other vector since we
10161 * piggyback on the misc/ICR0 for queue interrupts.
10162 */
10163 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
10164 return ret;
Shannon Nelson958a3e32013-09-28 07:13:28 +000010165 if (vsi->num_q_vectors)
10166 vsi->base_vector = i40e_get_lump(pf, pf->irq_pile,
10167 vsi->num_q_vectors, vsi->idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010168 if (vsi->base_vector < 0) {
10169 dev_info(&pf->pdev->dev,
Shannon Nelson049a2be2014-10-17 03:14:50 +000010170 "failed to get tracking for %d vectors for VSI %d, err=%d\n",
10171 vsi->num_q_vectors, vsi->seid, vsi->base_vector);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010172 i40e_vsi_free_q_vectors(vsi);
10173 ret = -ENOENT;
10174 goto vector_setup_out;
10175 }
10176
10177vector_setup_out:
10178 return ret;
10179}
10180
10181/**
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010182 * i40e_vsi_reinit_setup - return and reallocate resources for a VSI
10183 * @vsi: pointer to the vsi.
10184 *
10185 * This re-allocates a vsi's queue resources.
10186 *
10187 * Returns pointer to the successfully allocated and configured VSI sw struct
10188 * on success, otherwise returns NULL on failure.
10189 **/
10190static struct i40e_vsi *i40e_vsi_reinit_setup(struct i40e_vsi *vsi)
10191{
Björn Töpel74608d12017-05-24 07:55:35 +020010192 u16 alloc_queue_pairs;
John Underwoodf5340392016-02-18 09:19:24 -080010193 struct i40e_pf *pf;
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010194 u8 enabled_tc;
10195 int ret;
10196
John Underwoodf5340392016-02-18 09:19:24 -080010197 if (!vsi)
10198 return NULL;
10199
10200 pf = vsi->back;
10201
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010202 i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
10203 i40e_vsi_clear_rings(vsi);
10204
10205 i40e_vsi_free_arrays(vsi, false);
10206 i40e_set_num_rings_in_vsi(vsi);
10207 ret = i40e_vsi_alloc_arrays(vsi, false);
10208 if (ret)
10209 goto err_vsi;
10210
Björn Töpel74608d12017-05-24 07:55:35 +020010211 alloc_queue_pairs = vsi->alloc_queue_pairs *
10212 (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
10213
10214 ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010215 if (ret < 0) {
Shannon Nelson049a2be2014-10-17 03:14:50 +000010216 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010217 "failed to get tracking for %d queues for VSI %d err %d\n",
Björn Töpel74608d12017-05-24 07:55:35 +020010218 alloc_queue_pairs, vsi->seid, ret);
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010219 goto err_vsi;
10220 }
10221 vsi->base_queue = ret;
10222
10223 /* Update the FW view of the VSI. Force a reset of TC and queue
10224 * layout configurations.
10225 */
10226 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
10227 pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
10228 pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
10229 i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
Jacob Keller1596b5d2016-11-08 13:05:15 -080010230 if (vsi->type == I40E_VSI_MAIN)
10231 i40e_rm_default_mac_filter(vsi, pf->hw.mac.perm_addr);
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010232
10233 /* assign it some queues */
10234 ret = i40e_alloc_rings(vsi);
10235 if (ret)
10236 goto err_rings;
10237
10238 /* map all of the rings to the q_vectors */
10239 i40e_vsi_map_rings_to_vectors(vsi);
10240 return vsi;
10241
10242err_rings:
10243 i40e_vsi_free_q_vectors(vsi);
10244 if (vsi->netdev_registered) {
10245 vsi->netdev_registered = false;
10246 unregister_netdev(vsi->netdev);
10247 free_netdev(vsi->netdev);
10248 vsi->netdev = NULL;
10249 }
10250 i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
10251err_vsi:
10252 i40e_vsi_clear(vsi);
10253 return NULL;
10254}
10255
10256/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010257 * i40e_vsi_setup - Set up a VSI by a given type
10258 * @pf: board private structure
10259 * @type: VSI type
10260 * @uplink_seid: the switch element to link to
10261 * @param1: usage depends upon VSI type. For VF types, indicates VF id
10262 *
10263 * This allocates the sw VSI structure and its queue resources, then add a VSI
10264 * to the identified VEB.
10265 *
10266 * Returns pointer to the successfully allocated and configure VSI sw struct on
10267 * success, otherwise returns NULL on failure.
10268 **/
10269struct i40e_vsi *i40e_vsi_setup(struct i40e_pf *pf, u8 type,
10270 u16 uplink_seid, u32 param1)
10271{
10272 struct i40e_vsi *vsi = NULL;
10273 struct i40e_veb *veb = NULL;
Björn Töpel74608d12017-05-24 07:55:35 +020010274 u16 alloc_queue_pairs;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010275 int ret, i;
10276 int v_idx;
10277
10278 /* The requested uplink_seid must be either
10279 * - the PF's port seid
10280 * no VEB is needed because this is the PF
10281 * or this is a Flow Director special case VSI
10282 * - seid of an existing VEB
10283 * - seid of a VSI that owns an existing VEB
10284 * - seid of a VSI that doesn't own a VEB
10285 * a new VEB is created and the VSI becomes the owner
10286 * - seid of the PF VSI, which is what creates the first VEB
10287 * this is a special case of the previous
10288 *
10289 * Find which uplink_seid we were given and create a new VEB if needed
10290 */
10291 for (i = 0; i < I40E_MAX_VEB; i++) {
10292 if (pf->veb[i] && pf->veb[i]->seid == uplink_seid) {
10293 veb = pf->veb[i];
10294 break;
10295 }
10296 }
10297
10298 if (!veb && uplink_seid != pf->mac_seid) {
10299
Mitch Williams505682c2014-05-20 08:01:37 +000010300 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010301 if (pf->vsi[i] && pf->vsi[i]->seid == uplink_seid) {
10302 vsi = pf->vsi[i];
10303 break;
10304 }
10305 }
10306 if (!vsi) {
10307 dev_info(&pf->pdev->dev, "no such uplink_seid %d\n",
10308 uplink_seid);
10309 return NULL;
10310 }
10311
10312 if (vsi->uplink_seid == pf->mac_seid)
10313 veb = i40e_veb_setup(pf, 0, pf->mac_seid, vsi->seid,
10314 vsi->tc_config.enabled_tc);
10315 else if ((vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
10316 veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
10317 vsi->tc_config.enabled_tc);
Anjali Singhai Jain79c21a82014-11-13 03:06:14 +000010318 if (veb) {
10319 if (vsi->seid != pf->vsi[pf->lan_vsi]->seid) {
10320 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -040010321 "New VSI creation error, uplink seid of LAN VSI expected.\n");
Anjali Singhai Jain79c21a82014-11-13 03:06:14 +000010322 return NULL;
10323 }
Anjali Singhai Jainfa11cb32015-05-27 12:06:14 -040010324 /* We come up by default in VEPA mode if SRIOV is not
10325 * already enabled, in which case we can't force VEPA
10326 * mode.
10327 */
10328 if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
10329 veb->bridge_mode = BRIDGE_MODE_VEPA;
10330 pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
10331 }
Neerav Parikh51616012015-02-06 08:52:14 +000010332 i40e_config_bridge_mode(veb);
Anjali Singhai Jain79c21a82014-11-13 03:06:14 +000010333 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010334 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
10335 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
10336 veb = pf->veb[i];
10337 }
10338 if (!veb) {
10339 dev_info(&pf->pdev->dev, "couldn't add VEB\n");
10340 return NULL;
10341 }
10342
10343 vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
10344 uplink_seid = veb->seid;
10345 }
10346
10347 /* get vsi sw struct */
10348 v_idx = i40e_vsi_mem_alloc(pf, type);
10349 if (v_idx < 0)
10350 goto err_alloc;
10351 vsi = pf->vsi[v_idx];
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010352 if (!vsi)
10353 goto err_alloc;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010354 vsi->type = type;
10355 vsi->veb_idx = (veb ? veb->idx : I40E_NO_VEB);
10356
10357 if (type == I40E_VSI_MAIN)
10358 pf->lan_vsi = v_idx;
10359 else if (type == I40E_VSI_SRIOV)
10360 vsi->vf_id = param1;
10361 /* assign it some queues */
Björn Töpel74608d12017-05-24 07:55:35 +020010362 alloc_queue_pairs = vsi->alloc_queue_pairs *
10363 (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
10364
10365 ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010366 if (ret < 0) {
Shannon Nelson049a2be2014-10-17 03:14:50 +000010367 dev_info(&pf->pdev->dev,
10368 "failed to get tracking for %d queues for VSI %d err=%d\n",
Björn Töpel74608d12017-05-24 07:55:35 +020010369 alloc_queue_pairs, vsi->seid, ret);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010370 goto err_vsi;
10371 }
10372 vsi->base_queue = ret;
10373
10374 /* get a VSI from the hardware */
10375 vsi->uplink_seid = uplink_seid;
10376 ret = i40e_add_vsi(vsi);
10377 if (ret)
10378 goto err_vsi;
10379
10380 switch (vsi->type) {
10381 /* setup the netdev if needed */
10382 case I40E_VSI_MAIN:
10383 case I40E_VSI_VMDQ2:
10384 ret = i40e_config_netdev(vsi);
10385 if (ret)
10386 goto err_netdev;
10387 ret = register_netdev(vsi->netdev);
10388 if (ret)
10389 goto err_netdev;
10390 vsi->netdev_registered = true;
10391 netif_carrier_off(vsi->netdev);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -080010392#ifdef CONFIG_I40E_DCB
10393 /* Setup DCB netlink interface */
10394 i40e_dcbnl_setup(vsi);
10395#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010396 /* fall through */
10397
10398 case I40E_VSI_FDIR:
10399 /* set up vectors and rings if needed */
10400 ret = i40e_vsi_setup_vectors(vsi);
10401 if (ret)
10402 goto err_msix;
10403
10404 ret = i40e_alloc_rings(vsi);
10405 if (ret)
10406 goto err_rings;
10407
10408 /* map all of the rings to the q_vectors */
10409 i40e_vsi_map_rings_to_vectors(vsi);
10410
10411 i40e_vsi_reset_stats(vsi);
10412 break;
10413
10414 default:
10415 /* no netdev or rings for the other VSI types */
10416 break;
10417 }
10418
Jacob Kellerd36e41d2017-06-23 04:24:46 -040010419 if ((pf->hw_features & I40E_HW_RSS_AQ_CAPABLE) &&
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -040010420 (vsi->type == I40E_VSI_VMDQ2)) {
10421 ret = i40e_vsi_config_rss(vsi);
10422 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010423 return vsi;
10424
10425err_rings:
10426 i40e_vsi_free_q_vectors(vsi);
10427err_msix:
10428 if (vsi->netdev_registered) {
10429 vsi->netdev_registered = false;
10430 unregister_netdev(vsi->netdev);
10431 free_netdev(vsi->netdev);
10432 vsi->netdev = NULL;
10433 }
10434err_netdev:
10435 i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
10436err_vsi:
10437 i40e_vsi_clear(vsi);
10438err_alloc:
10439 return NULL;
10440}
10441
10442/**
10443 * i40e_veb_get_bw_info - Query VEB BW information
10444 * @veb: the veb to query
10445 *
10446 * Query the Tx scheduler BW configuration data for given VEB
10447 **/
10448static int i40e_veb_get_bw_info(struct i40e_veb *veb)
10449{
10450 struct i40e_aqc_query_switching_comp_ets_config_resp ets_data;
10451 struct i40e_aqc_query_switching_comp_bw_config_resp bw_data;
10452 struct i40e_pf *pf = veb->pf;
10453 struct i40e_hw *hw = &pf->hw;
10454 u32 tc_bw_max;
10455 int ret = 0;
10456 int i;
10457
10458 ret = i40e_aq_query_switch_comp_bw_config(hw, veb->seid,
10459 &bw_data, NULL);
10460 if (ret) {
10461 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010462 "query veb bw config failed, err %s aq_err %s\n",
10463 i40e_stat_str(&pf->hw, ret),
10464 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010465 goto out;
10466 }
10467
10468 ret = i40e_aq_query_switch_comp_ets_config(hw, veb->seid,
10469 &ets_data, NULL);
10470 if (ret) {
10471 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010472 "query veb bw ets config failed, err %s aq_err %s\n",
10473 i40e_stat_str(&pf->hw, ret),
10474 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010475 goto out;
10476 }
10477
10478 veb->bw_limit = le16_to_cpu(ets_data.port_bw_limit);
10479 veb->bw_max_quanta = ets_data.tc_bw_max;
10480 veb->is_abs_credits = bw_data.absolute_credits_enable;
Neerav Parikh23cd1f02014-11-12 00:18:41 +000010481 veb->enabled_tc = ets_data.tc_valid_bits;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010482 tc_bw_max = le16_to_cpu(bw_data.tc_bw_max[0]) |
10483 (le16_to_cpu(bw_data.tc_bw_max[1]) << 16);
10484 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
10485 veb->bw_tc_share_credits[i] = bw_data.tc_bw_share_credits[i];
10486 veb->bw_tc_limit_credits[i] =
10487 le16_to_cpu(bw_data.tc_bw_limits[i]);
10488 veb->bw_tc_max_quanta[i] = ((tc_bw_max >> (i*4)) & 0x7);
10489 }
10490
10491out:
10492 return ret;
10493}
10494
10495/**
10496 * i40e_veb_mem_alloc - Allocates the next available struct veb in the PF
10497 * @pf: board private structure
10498 *
10499 * On error: returns error code (negative)
10500 * On success: returns vsi index in PF (positive)
10501 **/
10502static int i40e_veb_mem_alloc(struct i40e_pf *pf)
10503{
10504 int ret = -ENOENT;
10505 struct i40e_veb *veb;
10506 int i;
10507
10508 /* Need to protect the allocation of switch elements at the PF level */
10509 mutex_lock(&pf->switch_mutex);
10510
10511 /* VEB list may be fragmented if VEB creation/destruction has
10512 * been happening. We can afford to do a quick scan to look
10513 * for any free slots in the list.
10514 *
10515 * find next empty veb slot, looping back around if necessary
10516 */
10517 i = 0;
10518 while ((i < I40E_MAX_VEB) && (pf->veb[i] != NULL))
10519 i++;
10520 if (i >= I40E_MAX_VEB) {
10521 ret = -ENOMEM;
10522 goto err_alloc_veb; /* out of VEB slots! */
10523 }
10524
10525 veb = kzalloc(sizeof(*veb), GFP_KERNEL);
10526 if (!veb) {
10527 ret = -ENOMEM;
10528 goto err_alloc_veb;
10529 }
10530 veb->pf = pf;
10531 veb->idx = i;
10532 veb->enabled_tc = 1;
10533
10534 pf->veb[i] = veb;
10535 ret = i;
10536err_alloc_veb:
10537 mutex_unlock(&pf->switch_mutex);
10538 return ret;
10539}
10540
10541/**
10542 * i40e_switch_branch_release - Delete a branch of the switch tree
10543 * @branch: where to start deleting
10544 *
10545 * This uses recursion to find the tips of the branch to be
10546 * removed, deleting until we get back to and can delete this VEB.
10547 **/
10548static void i40e_switch_branch_release(struct i40e_veb *branch)
10549{
10550 struct i40e_pf *pf = branch->pf;
10551 u16 branch_seid = branch->seid;
10552 u16 veb_idx = branch->idx;
10553 int i;
10554
10555 /* release any VEBs on this VEB - RECURSION */
10556 for (i = 0; i < I40E_MAX_VEB; i++) {
10557 if (!pf->veb[i])
10558 continue;
10559 if (pf->veb[i]->uplink_seid == branch->seid)
10560 i40e_switch_branch_release(pf->veb[i]);
10561 }
10562
10563 /* Release the VSIs on this VEB, but not the owner VSI.
10564 *
10565 * NOTE: Removing the last VSI on a VEB has the SIDE EFFECT of removing
10566 * the VEB itself, so don't use (*branch) after this loop.
10567 */
Mitch Williams505682c2014-05-20 08:01:37 +000010568 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010569 if (!pf->vsi[i])
10570 continue;
10571 if (pf->vsi[i]->uplink_seid == branch_seid &&
10572 (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
10573 i40e_vsi_release(pf->vsi[i]);
10574 }
10575 }
10576
10577 /* There's one corner case where the VEB might not have been
10578 * removed, so double check it here and remove it if needed.
10579 * This case happens if the veb was created from the debugfs
10580 * commands and no VSIs were added to it.
10581 */
10582 if (pf->veb[veb_idx])
10583 i40e_veb_release(pf->veb[veb_idx]);
10584}
10585
10586/**
10587 * i40e_veb_clear - remove veb struct
10588 * @veb: the veb to remove
10589 **/
10590static void i40e_veb_clear(struct i40e_veb *veb)
10591{
10592 if (!veb)
10593 return;
10594
10595 if (veb->pf) {
10596 struct i40e_pf *pf = veb->pf;
10597
10598 mutex_lock(&pf->switch_mutex);
10599 if (pf->veb[veb->idx] == veb)
10600 pf->veb[veb->idx] = NULL;
10601 mutex_unlock(&pf->switch_mutex);
10602 }
10603
10604 kfree(veb);
10605}
10606
10607/**
10608 * i40e_veb_release - Delete a VEB and free its resources
10609 * @veb: the VEB being removed
10610 **/
10611void i40e_veb_release(struct i40e_veb *veb)
10612{
10613 struct i40e_vsi *vsi = NULL;
10614 struct i40e_pf *pf;
10615 int i, n = 0;
10616
10617 pf = veb->pf;
10618
10619 /* find the remaining VSI and check for extras */
Mitch Williams505682c2014-05-20 08:01:37 +000010620 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010621 if (pf->vsi[i] && pf->vsi[i]->uplink_seid == veb->seid) {
10622 n++;
10623 vsi = pf->vsi[i];
10624 }
10625 }
10626 if (n != 1) {
10627 dev_info(&pf->pdev->dev,
10628 "can't remove VEB %d with %d VSIs left\n",
10629 veb->seid, n);
10630 return;
10631 }
10632
10633 /* move the remaining VSI to uplink veb */
10634 vsi->flags &= ~I40E_VSI_FLAG_VEB_OWNER;
10635 if (veb->uplink_seid) {
10636 vsi->uplink_seid = veb->uplink_seid;
10637 if (veb->uplink_seid == pf->mac_seid)
10638 vsi->veb_idx = I40E_NO_VEB;
10639 else
10640 vsi->veb_idx = veb->veb_idx;
10641 } else {
10642 /* floating VEB */
10643 vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
10644 vsi->veb_idx = pf->vsi[pf->lan_vsi]->veb_idx;
10645 }
10646
10647 i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
10648 i40e_veb_clear(veb);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010649}
10650
10651/**
10652 * i40e_add_veb - create the VEB in the switch
10653 * @veb: the VEB to be instantiated
10654 * @vsi: the controlling VSI
10655 **/
10656static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi)
10657{
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010658 struct i40e_pf *pf = veb->pf;
Shannon Nelson66fc3602016-01-13 16:51:42 -080010659 bool enable_stats = !!(pf->flags & I40E_FLAG_VEB_STATS_ENABLED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010660 int ret;
10661
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010662 ret = i40e_aq_add_veb(&pf->hw, veb->uplink_seid, vsi->seid,
Mitch Williams5bc16032016-05-16 10:26:43 -070010663 veb->enabled_tc, false,
Shannon Nelson66fc3602016-01-13 16:51:42 -080010664 &veb->seid, enable_stats, NULL);
Mitch Williams5bc16032016-05-16 10:26:43 -070010665
10666 /* get a VEB from the hardware */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010667 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010668 dev_info(&pf->pdev->dev,
10669 "couldn't add VEB, err %s aq_err %s\n",
10670 i40e_stat_str(&pf->hw, ret),
10671 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010672 return -EPERM;
10673 }
10674
10675 /* get statistics counter */
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010676 ret = i40e_aq_get_veb_parameters(&pf->hw, veb->seid, NULL, NULL,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010677 &veb->stats_idx, NULL, NULL, NULL);
10678 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010679 dev_info(&pf->pdev->dev,
10680 "couldn't get VEB statistics idx, err %s aq_err %s\n",
10681 i40e_stat_str(&pf->hw, ret),
10682 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010683 return -EPERM;
10684 }
10685 ret = i40e_veb_get_bw_info(veb);
10686 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010687 dev_info(&pf->pdev->dev,
10688 "couldn't get VEB bw info, err %s aq_err %s\n",
10689 i40e_stat_str(&pf->hw, ret),
10690 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10691 i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010692 return -ENOENT;
10693 }
10694
10695 vsi->uplink_seid = veb->seid;
10696 vsi->veb_idx = veb->idx;
10697 vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
10698
10699 return 0;
10700}
10701
10702/**
10703 * i40e_veb_setup - Set up a VEB
10704 * @pf: board private structure
10705 * @flags: VEB setup flags
10706 * @uplink_seid: the switch element to link to
10707 * @vsi_seid: the initial VSI seid
10708 * @enabled_tc: Enabled TC bit-map
10709 *
10710 * This allocates the sw VEB structure and links it into the switch
10711 * It is possible and legal for this to be a duplicate of an already
10712 * existing VEB. It is also possible for both uplink and vsi seids
10713 * to be zero, in order to create a floating VEB.
10714 *
10715 * Returns pointer to the successfully allocated VEB sw struct on
10716 * success, otherwise returns NULL on failure.
10717 **/
10718struct i40e_veb *i40e_veb_setup(struct i40e_pf *pf, u16 flags,
10719 u16 uplink_seid, u16 vsi_seid,
10720 u8 enabled_tc)
10721{
10722 struct i40e_veb *veb, *uplink_veb = NULL;
10723 int vsi_idx, veb_idx;
10724 int ret;
10725
10726 /* if one seid is 0, the other must be 0 to create a floating relay */
10727 if ((uplink_seid == 0 || vsi_seid == 0) &&
10728 (uplink_seid + vsi_seid != 0)) {
10729 dev_info(&pf->pdev->dev,
10730 "one, not both seid's are 0: uplink=%d vsi=%d\n",
10731 uplink_seid, vsi_seid);
10732 return NULL;
10733 }
10734
10735 /* make sure there is such a vsi and uplink */
Mitch Williams505682c2014-05-20 08:01:37 +000010736 for (vsi_idx = 0; vsi_idx < pf->num_alloc_vsi; vsi_idx++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010737 if (pf->vsi[vsi_idx] && pf->vsi[vsi_idx]->seid == vsi_seid)
10738 break;
Mitch Williams505682c2014-05-20 08:01:37 +000010739 if (vsi_idx >= pf->num_alloc_vsi && vsi_seid != 0) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010740 dev_info(&pf->pdev->dev, "vsi seid %d not found\n",
10741 vsi_seid);
10742 return NULL;
10743 }
10744
10745 if (uplink_seid && uplink_seid != pf->mac_seid) {
10746 for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
10747 if (pf->veb[veb_idx] &&
10748 pf->veb[veb_idx]->seid == uplink_seid) {
10749 uplink_veb = pf->veb[veb_idx];
10750 break;
10751 }
10752 }
10753 if (!uplink_veb) {
10754 dev_info(&pf->pdev->dev,
10755 "uplink seid %d not found\n", uplink_seid);
10756 return NULL;
10757 }
10758 }
10759
10760 /* get veb sw struct */
10761 veb_idx = i40e_veb_mem_alloc(pf);
10762 if (veb_idx < 0)
10763 goto err_alloc;
10764 veb = pf->veb[veb_idx];
10765 veb->flags = flags;
10766 veb->uplink_seid = uplink_seid;
10767 veb->veb_idx = (uplink_veb ? uplink_veb->idx : I40E_NO_VEB);
10768 veb->enabled_tc = (enabled_tc ? enabled_tc : 0x1);
10769
10770 /* create the VEB in the switch */
10771 ret = i40e_add_veb(veb, pf->vsi[vsi_idx]);
10772 if (ret)
10773 goto err_veb;
Shannon Nelson1bb8b932014-04-23 04:49:54 +000010774 if (vsi_idx == pf->lan_vsi)
10775 pf->lan_veb = veb->idx;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010776
10777 return veb;
10778
10779err_veb:
10780 i40e_veb_clear(veb);
10781err_alloc:
10782 return NULL;
10783}
10784
10785/**
Jeff Kirsherb40c82e62015-02-27 09:18:34 +000010786 * i40e_setup_pf_switch_element - set PF vars based on switch type
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010787 * @pf: board private structure
10788 * @ele: element we are building info from
10789 * @num_reported: total number of elements
10790 * @printconfig: should we print the contents
10791 *
10792 * helper function to assist in extracting a few useful SEID values.
10793 **/
10794static void i40e_setup_pf_switch_element(struct i40e_pf *pf,
10795 struct i40e_aqc_switch_config_element_resp *ele,
10796 u16 num_reported, bool printconfig)
10797{
10798 u16 downlink_seid = le16_to_cpu(ele->downlink_seid);
10799 u16 uplink_seid = le16_to_cpu(ele->uplink_seid);
10800 u8 element_type = ele->element_type;
10801 u16 seid = le16_to_cpu(ele->seid);
10802
10803 if (printconfig)
10804 dev_info(&pf->pdev->dev,
10805 "type=%d seid=%d uplink=%d downlink=%d\n",
10806 element_type, seid, uplink_seid, downlink_seid);
10807
10808 switch (element_type) {
10809 case I40E_SWITCH_ELEMENT_TYPE_MAC:
10810 pf->mac_seid = seid;
10811 break;
10812 case I40E_SWITCH_ELEMENT_TYPE_VEB:
10813 /* Main VEB? */
10814 if (uplink_seid != pf->mac_seid)
10815 break;
10816 if (pf->lan_veb == I40E_NO_VEB) {
10817 int v;
10818
10819 /* find existing or else empty VEB */
10820 for (v = 0; v < I40E_MAX_VEB; v++) {
10821 if (pf->veb[v] && (pf->veb[v]->seid == seid)) {
10822 pf->lan_veb = v;
10823 break;
10824 }
10825 }
10826 if (pf->lan_veb == I40E_NO_VEB) {
10827 v = i40e_veb_mem_alloc(pf);
10828 if (v < 0)
10829 break;
10830 pf->lan_veb = v;
10831 }
10832 }
10833
10834 pf->veb[pf->lan_veb]->seid = seid;
10835 pf->veb[pf->lan_veb]->uplink_seid = pf->mac_seid;
10836 pf->veb[pf->lan_veb]->pf = pf;
10837 pf->veb[pf->lan_veb]->veb_idx = I40E_NO_VEB;
10838 break;
10839 case I40E_SWITCH_ELEMENT_TYPE_VSI:
10840 if (num_reported != 1)
10841 break;
10842 /* This is immediately after a reset so we can assume this is
10843 * the PF's VSI
10844 */
10845 pf->mac_seid = uplink_seid;
10846 pf->pf_seid = downlink_seid;
10847 pf->main_vsi_seid = seid;
10848 if (printconfig)
10849 dev_info(&pf->pdev->dev,
10850 "pf_seid=%d main_vsi_seid=%d\n",
10851 pf->pf_seid, pf->main_vsi_seid);
10852 break;
10853 case I40E_SWITCH_ELEMENT_TYPE_PF:
10854 case I40E_SWITCH_ELEMENT_TYPE_VF:
10855 case I40E_SWITCH_ELEMENT_TYPE_EMP:
10856 case I40E_SWITCH_ELEMENT_TYPE_BMC:
10857 case I40E_SWITCH_ELEMENT_TYPE_PE:
10858 case I40E_SWITCH_ELEMENT_TYPE_PA:
10859 /* ignore these for now */
10860 break;
10861 default:
10862 dev_info(&pf->pdev->dev, "unknown element type=%d seid=%d\n",
10863 element_type, seid);
10864 break;
10865 }
10866}
10867
10868/**
10869 * i40e_fetch_switch_configuration - Get switch config from firmware
10870 * @pf: board private structure
10871 * @printconfig: should we print the contents
10872 *
10873 * Get the current switch configuration from the device and
10874 * extract a few useful SEID values.
10875 **/
10876int i40e_fetch_switch_configuration(struct i40e_pf *pf, bool printconfig)
10877{
10878 struct i40e_aqc_get_switch_config_resp *sw_config;
10879 u16 next_seid = 0;
10880 int ret = 0;
10881 u8 *aq_buf;
10882 int i;
10883
10884 aq_buf = kzalloc(I40E_AQ_LARGE_BUF, GFP_KERNEL);
10885 if (!aq_buf)
10886 return -ENOMEM;
10887
10888 sw_config = (struct i40e_aqc_get_switch_config_resp *)aq_buf;
10889 do {
10890 u16 num_reported, num_total;
10891
10892 ret = i40e_aq_get_switch_config(&pf->hw, sw_config,
10893 I40E_AQ_LARGE_BUF,
10894 &next_seid, NULL);
10895 if (ret) {
10896 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010897 "get switch config failed err %s aq_err %s\n",
10898 i40e_stat_str(&pf->hw, ret),
10899 i40e_aq_str(&pf->hw,
10900 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010901 kfree(aq_buf);
10902 return -ENOENT;
10903 }
10904
10905 num_reported = le16_to_cpu(sw_config->header.num_reported);
10906 num_total = le16_to_cpu(sw_config->header.num_total);
10907
10908 if (printconfig)
10909 dev_info(&pf->pdev->dev,
10910 "header: %d reported %d total\n",
10911 num_reported, num_total);
10912
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010913 for (i = 0; i < num_reported; i++) {
10914 struct i40e_aqc_switch_config_element_resp *ele =
10915 &sw_config->element[i];
10916
10917 i40e_setup_pf_switch_element(pf, ele, num_reported,
10918 printconfig);
10919 }
10920 } while (next_seid != 0);
10921
10922 kfree(aq_buf);
10923 return ret;
10924}
10925
10926/**
10927 * i40e_setup_pf_switch - Setup the HW switch on startup or after reset
10928 * @pf: board private structure
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010929 * @reinit: if the Main VSI needs to re-initialized.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010930 *
10931 * Returns 0 on success, negative value on failure
10932 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010933static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010934{
Anjali Singhai Jainb5569892016-05-03 15:13:12 -070010935 u16 flags = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010936 int ret;
10937
10938 /* find out what's out there already */
10939 ret = i40e_fetch_switch_configuration(pf, false);
10940 if (ret) {
10941 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010942 "couldn't fetch switch config, err %s aq_err %s\n",
10943 i40e_stat_str(&pf->hw, ret),
10944 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010945 return ret;
10946 }
10947 i40e_pf_reset_stats(pf);
10948
Anjali Singhai Jainb5569892016-05-03 15:13:12 -070010949 /* set the switch config bit for the whole device to
10950 * support limited promisc or true promisc
10951 * when user requests promisc. The default is limited
10952 * promisc.
10953 */
10954
10955 if ((pf->hw.pf_id == 0) &&
10956 !(pf->flags & I40E_FLAG_TRUE_PROMISC_SUPPORT))
10957 flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
10958
10959 if (pf->hw.pf_id == 0) {
10960 u16 valid_flags;
10961
10962 valid_flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
10963 ret = i40e_aq_set_switch_config(&pf->hw, flags, valid_flags,
10964 NULL);
10965 if (ret && pf->hw.aq.asq_last_status != I40E_AQ_RC_ESRCH) {
10966 dev_info(&pf->pdev->dev,
10967 "couldn't set switch config bits, err %s aq_err %s\n",
10968 i40e_stat_str(&pf->hw, ret),
10969 i40e_aq_str(&pf->hw,
10970 pf->hw.aq.asq_last_status));
10971 /* not a fatal problem, just keep going */
10972 }
10973 }
10974
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010975 /* first time setup */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010976 if (pf->lan_vsi == I40E_NO_VSI || reinit) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010977 struct i40e_vsi *vsi = NULL;
10978 u16 uplink_seid;
10979
10980 /* Set up the PF VSI associated with the PF's main VSI
10981 * that is already in the HW switch
10982 */
10983 if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
10984 uplink_seid = pf->veb[pf->lan_veb]->seid;
10985 else
10986 uplink_seid = pf->mac_seid;
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010987 if (pf->lan_vsi == I40E_NO_VSI)
10988 vsi = i40e_vsi_setup(pf, I40E_VSI_MAIN, uplink_seid, 0);
10989 else if (reinit)
10990 vsi = i40e_vsi_reinit_setup(pf->vsi[pf->lan_vsi]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010991 if (!vsi) {
10992 dev_info(&pf->pdev->dev, "setup of MAIN VSI failed\n");
10993 i40e_fdir_teardown(pf);
10994 return -EAGAIN;
10995 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010996 } else {
10997 /* force a reset of TC and queue layout configurations */
10998 u8 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
Jesse Brandeburg6995b362015-08-28 17:55:54 -040010999
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011000 pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
11001 pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
11002 i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
11003 }
11004 i40e_vlan_stripping_disable(pf->vsi[pf->lan_vsi]);
11005
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080011006 i40e_fdir_sb_setup(pf);
11007
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011008 /* Setup static PF queue filter control settings */
11009 ret = i40e_setup_pf_filter_control(pf);
11010 if (ret) {
11011 dev_info(&pf->pdev->dev, "setup_pf_filter_control failed: %d\n",
11012 ret);
11013 /* Failure here should not stop continuing other steps */
11014 }
11015
11016 /* enable RSS in the HW, even for only one queue, as the stack can use
11017 * the hash
11018 */
11019 if ((pf->flags & I40E_FLAG_RSS_ENABLED))
Helin Zhang043dd652015-10-21 19:56:23 -040011020 i40e_pf_config_rss(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011021
11022 /* fill in link information and enable LSE reporting */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011023 i40e_link_event(pf);
11024
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +000011025 /* Initialize user-specific link properties */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011026 pf->fc_autoneg_status = ((pf->hw.phy.link_info.an_info &
11027 I40E_AQ_AN_COMPLETED) ? true : false);
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +000011028
Jacob Kellerbeb0dff2014-01-11 05:43:19 +000011029 i40e_ptp_init(pf);
11030
Alexander Duyck1f190d92017-04-19 09:25:51 -040011031 /* repopulate tunnel port filters */
11032 i40e_sync_udp_filters(pf);
11033
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011034 return ret;
11035}
11036
11037/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011038 * i40e_determine_queue_usage - Work out queue distribution
11039 * @pf: board private structure
11040 **/
11041static void i40e_determine_queue_usage(struct i40e_pf *pf)
11042{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011043 int queues_left;
11044
11045 pf->num_lan_qps = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011046
11047 /* Find the max queues to be put into basic use. We'll always be
11048 * using TC0, whether or not DCB is running, and TC0 will get the
11049 * big RSS set.
11050 */
11051 queues_left = pf->hw.func_caps.num_tx_qp;
11052
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080011053 if ((queues_left == 1) ||
Frank Zhang9aa7e932014-05-20 08:01:42 +000011054 !(pf->flags & I40E_FLAG_MSIX_ENABLED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011055 /* one qp for PF, no queues for anything else */
11056 queues_left = 0;
Helin Zhangacd65442015-10-26 19:44:28 -040011057 pf->alloc_rss_size = pf->num_lan_qps = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011058
11059 /* make sure all the fancies are disabled */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080011060 pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060011061 I40E_FLAG_IWARP_ENABLED |
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080011062 I40E_FLAG_FD_SB_ENABLED |
11063 I40E_FLAG_FD_ATR_ENABLED |
Neerav Parikh4d9b6042014-05-22 06:31:51 +000011064 I40E_FLAG_DCB_CAPABLE |
Dave Ertmana0362442016-08-29 17:38:26 -070011065 I40E_FLAG_DCB_ENABLED |
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080011066 I40E_FLAG_SRIOV_ENABLED |
11067 I40E_FLAG_VMDQ_ENABLED);
Frank Zhang9aa7e932014-05-20 08:01:42 +000011068 } else if (!(pf->flags & (I40E_FLAG_RSS_ENABLED |
11069 I40E_FLAG_FD_SB_ENABLED |
Anjali Singhai Jainbbe7d0e2014-05-20 08:01:44 +000011070 I40E_FLAG_FD_ATR_ENABLED |
Neerav Parikh4d9b6042014-05-22 06:31:51 +000011071 I40E_FLAG_DCB_CAPABLE))) {
Frank Zhang9aa7e932014-05-20 08:01:42 +000011072 /* one qp for PF */
Helin Zhangacd65442015-10-26 19:44:28 -040011073 pf->alloc_rss_size = pf->num_lan_qps = 1;
Frank Zhang9aa7e932014-05-20 08:01:42 +000011074 queues_left -= pf->num_lan_qps;
11075
11076 pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060011077 I40E_FLAG_IWARP_ENABLED |
Frank Zhang9aa7e932014-05-20 08:01:42 +000011078 I40E_FLAG_FD_SB_ENABLED |
11079 I40E_FLAG_FD_ATR_ENABLED |
11080 I40E_FLAG_DCB_ENABLED |
11081 I40E_FLAG_VMDQ_ENABLED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011082 } else {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080011083 /* Not enough queues for all TCs */
Neerav Parikh4d9b6042014-05-22 06:31:51 +000011084 if ((pf->flags & I40E_FLAG_DCB_CAPABLE) &&
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080011085 (queues_left < I40E_MAX_TRAFFIC_CLASS)) {
Dave Ertmana0362442016-08-29 17:38:26 -070011086 pf->flags &= ~(I40E_FLAG_DCB_CAPABLE |
11087 I40E_FLAG_DCB_ENABLED);
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080011088 dev_info(&pf->pdev->dev, "not enough queues for DCB. DCB is disabled.\n");
11089 }
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +000011090 pf->num_lan_qps = max_t(int, pf->rss_size_max,
11091 num_online_cpus());
11092 pf->num_lan_qps = min_t(int, pf->num_lan_qps,
11093 pf->hw.func_caps.num_tx_qp);
11094
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080011095 queues_left -= pf->num_lan_qps;
11096 }
11097
11098 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
11099 if (queues_left > 1) {
11100 queues_left -= 1; /* save 1 queue for FD */
11101 } else {
11102 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
11103 dev_info(&pf->pdev->dev, "not enough queues for Flow Director. Flow Director feature is disabled\n");
11104 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011105 }
11106
11107 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
11108 pf->num_vf_qps && pf->num_req_vfs && queues_left) {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080011109 pf->num_req_vfs = min_t(int, pf->num_req_vfs,
11110 (queues_left / pf->num_vf_qps));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011111 queues_left -= (pf->num_req_vfs * pf->num_vf_qps);
11112 }
11113
11114 if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
11115 pf->num_vmdq_vsis && pf->num_vmdq_qps && queues_left) {
11116 pf->num_vmdq_vsis = min_t(int, pf->num_vmdq_vsis,
11117 (queues_left / pf->num_vmdq_qps));
11118 queues_left -= (pf->num_vmdq_vsis * pf->num_vmdq_qps);
11119 }
11120
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +000011121 pf->queues_left = queues_left;
Neerav Parikh8279e492015-09-03 17:18:50 -040011122 dev_dbg(&pf->pdev->dev,
11123 "qs_avail=%d FD SB=%d lan_qs=%d lan_tc0=%d vf=%d*%d vmdq=%d*%d, remaining=%d\n",
11124 pf->hw.func_caps.num_tx_qp,
11125 !!(pf->flags & I40E_FLAG_FD_SB_ENABLED),
Helin Zhangacd65442015-10-26 19:44:28 -040011126 pf->num_lan_qps, pf->alloc_rss_size, pf->num_req_vfs,
11127 pf->num_vf_qps, pf->num_vmdq_vsis, pf->num_vmdq_qps,
11128 queues_left);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011129}
11130
11131/**
11132 * i40e_setup_pf_filter_control - Setup PF static filter control
11133 * @pf: PF to be setup
11134 *
Jeff Kirsherb40c82e62015-02-27 09:18:34 +000011135 * i40e_setup_pf_filter_control sets up a PF's initial filter control
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011136 * settings. If PE/FCoE are enabled then it will also set the per PF
11137 * based filter sizes required for them. It also enables Flow director,
11138 * ethertype and macvlan type filter settings for the pf.
11139 *
11140 * Returns 0 on success, negative on failure
11141 **/
11142static int i40e_setup_pf_filter_control(struct i40e_pf *pf)
11143{
11144 struct i40e_filter_control_settings *settings = &pf->filter_settings;
11145
11146 settings->hash_lut_size = I40E_HASH_LUT_SIZE_128;
11147
11148 /* Flow Director is enabled */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080011149 if (pf->flags & (I40E_FLAG_FD_SB_ENABLED | I40E_FLAG_FD_ATR_ENABLED))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011150 settings->enable_fdir = true;
11151
11152 /* Ethtype and MACVLAN filters enabled for PF */
11153 settings->enable_ethtype = true;
11154 settings->enable_macvlan = true;
11155
11156 if (i40e_set_filter_control(&pf->hw, settings))
11157 return -ENOENT;
11158
11159 return 0;
11160}
11161
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000011162#define INFO_STRING_LEN 255
Shannon Nelson7fd89542015-10-21 19:47:04 -040011163#define REMAIN(__x) (INFO_STRING_LEN - (__x))
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000011164static void i40e_print_features(struct i40e_pf *pf)
11165{
11166 struct i40e_hw *hw = &pf->hw;
Joe Perches3b195842015-12-03 04:20:57 -080011167 char *buf;
11168 int i;
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000011169
Joe Perches3b195842015-12-03 04:20:57 -080011170 buf = kmalloc(INFO_STRING_LEN, GFP_KERNEL);
11171 if (!buf)
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000011172 return;
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000011173
Joe Perches3b195842015-12-03 04:20:57 -080011174 i = snprintf(buf, INFO_STRING_LEN, "Features: PF-id[%d]", hw->pf_id);
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000011175#ifdef CONFIG_PCI_IOV
Joe Perches3b195842015-12-03 04:20:57 -080011176 i += snprintf(&buf[i], REMAIN(i), " VFs: %d", pf->num_req_vfs);
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000011177#endif
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -070011178 i += snprintf(&buf[i], REMAIN(i), " VSIs: %d QP: %d",
Shannon Nelson7fd89542015-10-21 19:47:04 -040011179 pf->hw.func_caps.num_vsis,
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -070011180 pf->vsi[pf->lan_vsi]->num_queue_pairs);
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000011181 if (pf->flags & I40E_FLAG_RSS_ENABLED)
Joe Perches3b195842015-12-03 04:20:57 -080011182 i += snprintf(&buf[i], REMAIN(i), " RSS");
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000011183 if (pf->flags & I40E_FLAG_FD_ATR_ENABLED)
Joe Perches3b195842015-12-03 04:20:57 -080011184 i += snprintf(&buf[i], REMAIN(i), " FD_ATR");
Akeem G Abodunrinc6423ff2014-05-10 04:49:08 +000011185 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
Joe Perches3b195842015-12-03 04:20:57 -080011186 i += snprintf(&buf[i], REMAIN(i), " FD_SB");
11187 i += snprintf(&buf[i], REMAIN(i), " NTUPLE");
Akeem G Abodunrinc6423ff2014-05-10 04:49:08 +000011188 }
Neerav Parikh4d9b6042014-05-22 06:31:51 +000011189 if (pf->flags & I40E_FLAG_DCB_CAPABLE)
Joe Perches3b195842015-12-03 04:20:57 -080011190 i += snprintf(&buf[i], REMAIN(i), " DCB");
Joe Perches3b195842015-12-03 04:20:57 -080011191 i += snprintf(&buf[i], REMAIN(i), " VxLAN");
Singhai, Anjali6a899022015-12-14 12:21:18 -080011192 i += snprintf(&buf[i], REMAIN(i), " Geneve");
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000011193 if (pf->flags & I40E_FLAG_PTP)
Joe Perches3b195842015-12-03 04:20:57 -080011194 i += snprintf(&buf[i], REMAIN(i), " PTP");
Shannon Nelson6dec1012015-09-28 14:12:30 -040011195 if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
Joe Perches3b195842015-12-03 04:20:57 -080011196 i += snprintf(&buf[i], REMAIN(i), " VEB");
Shannon Nelson6dec1012015-09-28 14:12:30 -040011197 else
Joe Perches3b195842015-12-03 04:20:57 -080011198 i += snprintf(&buf[i], REMAIN(i), " VEPA");
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000011199
Joe Perches3b195842015-12-03 04:20:57 -080011200 dev_info(&pf->pdev->dev, "%s\n", buf);
11201 kfree(buf);
Shannon Nelson7fd89542015-10-21 19:47:04 -040011202 WARN_ON(i > INFO_STRING_LEN);
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000011203}
11204
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011205/**
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050011206 * i40e_get_platform_mac_addr - get platform-specific MAC address
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050011207 * @pdev: PCI device information struct
11208 * @pf: board private structure
11209 *
Jacob Keller41c4c2b2017-04-05 07:50:57 -040011210 * Look up the MAC address for the device. First we'll try
11211 * eth_platform_get_mac_address, which will check Open Firmware, or arch
11212 * specific fallback. Otherwise, we'll default to the stored value in
11213 * firmware.
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050011214 **/
11215static void i40e_get_platform_mac_addr(struct pci_dev *pdev, struct i40e_pf *pf)
11216{
Jacob Keller41c4c2b2017-04-05 07:50:57 -040011217 if (eth_platform_get_mac_address(&pdev->dev, pf->hw.mac.addr))
11218 i40e_get_mac_addr(&pf->hw, pf->hw.mac.addr);
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050011219}
11220
11221/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011222 * i40e_probe - Device initialization routine
11223 * @pdev: PCI device information struct
11224 * @ent: entry in i40e_pci_tbl
11225 *
Jeff Kirsherb40c82e62015-02-27 09:18:34 +000011226 * i40e_probe initializes a PF identified by a pci_dev structure.
11227 * The OS initialization, configuring of the PF private structure,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011228 * and a hardware reset occur.
11229 *
11230 * Returns 0 on success, negative on failure
11231 **/
11232static int i40e_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
11233{
Catherine Sullivane8278452015-02-06 08:52:08 +000011234 struct i40e_aq_get_phy_abilities_resp abilities;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011235 struct i40e_pf *pf;
11236 struct i40e_hw *hw;
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +000011237 static u16 pfs_found;
Shannon Nelson1d5109d2015-08-26 15:14:08 -040011238 u16 wol_nvm_bits;
Catherine Sullivand4dfb812013-11-28 06:39:21 +000011239 u16 link_status;
Jean Sacren6f66a482015-09-19 05:08:45 -060011240 int err;
Anjali Singhai Jain4f2f017c2015-10-21 19:47:07 -040011241 u32 val;
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +000011242 u32 i;
Helin Zhang58fc3262015-10-01 14:37:38 -040011243 u8 set_fc_aq_fail;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011244
11245 err = pci_enable_device_mem(pdev);
11246 if (err)
11247 return err;
11248
11249 /* set up for high or low dma */
Mitch Williams64942942014-02-11 08:26:33 +000011250 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
Mitch Williams64942942014-02-11 08:26:33 +000011251 if (err) {
Jean Sacrene3e3bfd2014-03-25 04:30:27 +000011252 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
11253 if (err) {
11254 dev_err(&pdev->dev,
11255 "DMA configuration failed: 0x%x\n", err);
11256 goto err_dma;
11257 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011258 }
11259
11260 /* set up pci connections */
Johannes Thumshirn56d766d2016-06-07 09:44:05 +020011261 err = pci_request_mem_regions(pdev, i40e_driver_name);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011262 if (err) {
11263 dev_info(&pdev->dev,
11264 "pci_request_selected_regions failed %d\n", err);
11265 goto err_pci_reg;
11266 }
11267
11268 pci_enable_pcie_error_reporting(pdev);
11269 pci_set_master(pdev);
11270
11271 /* Now that we have a PCI connection, we need to do the
11272 * low level device setup. This is primarily setting up
11273 * the Admin Queue structures and then querying for the
11274 * device's current profile information.
11275 */
11276 pf = kzalloc(sizeof(*pf), GFP_KERNEL);
11277 if (!pf) {
11278 err = -ENOMEM;
11279 goto err_pf_alloc;
11280 }
11281 pf->next_vsi = 0;
11282 pf->pdev = pdev;
Mauro S. M. Rodrigues9e6c9c02017-05-12 23:26:56 -030011283 set_bit(__I40E_DOWN, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011284
11285 hw = &pf->hw;
11286 hw->back = pf;
Anjali Singhai232f4702015-02-26 16:15:39 +000011287
Shannon Nelson2ac8b672015-07-23 16:54:37 -040011288 pf->ioremap_len = min_t(int, pci_resource_len(pdev, 0),
11289 I40E_MAX_CSR_SPACE);
Anjali Singhai232f4702015-02-26 16:15:39 +000011290
Shannon Nelson2ac8b672015-07-23 16:54:37 -040011291 hw->hw_addr = ioremap(pci_resource_start(pdev, 0), pf->ioremap_len);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011292 if (!hw->hw_addr) {
11293 err = -EIO;
11294 dev_info(&pdev->dev, "ioremap(0x%04x, 0x%04x) failed: 0x%x\n",
11295 (unsigned int)pci_resource_start(pdev, 0),
Shannon Nelson2ac8b672015-07-23 16:54:37 -040011296 pf->ioremap_len, err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011297 goto err_ioremap;
11298 }
11299 hw->vendor_id = pdev->vendor;
11300 hw->device_id = pdev->device;
11301 pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
11302 hw->subsystem_vendor_id = pdev->subsystem_vendor;
11303 hw->subsystem_device_id = pdev->subsystem_device;
11304 hw->bus.device = PCI_SLOT(pdev->devfn);
11305 hw->bus.func = PCI_FUNC(pdev->devfn);
Sudheer Mogilappagarib3f028f2017-02-09 23:58:22 -080011306 hw->bus.bus_id = pdev->bus->number;
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +000011307 pf->instance = pfs_found;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011308
Jacob Keller0e588de2017-02-06 14:38:50 -080011309 INIT_LIST_HEAD(&pf->l3_flex_pit_list);
11310 INIT_LIST_HEAD(&pf->l4_flex_pit_list);
11311
Shannon Nelsonde03d2b2016-03-10 14:59:44 -080011312 /* set up the locks for the AQ, do this only once in probe
11313 * and destroy them only once in remove
11314 */
11315 mutex_init(&hw->aq.asq_mutex);
11316 mutex_init(&hw->aq.arq_mutex);
11317
Alexander Duyck5d4ca232016-09-30 08:21:46 -040011318 pf->msg_enable = netif_msg_init(debug,
11319 NETIF_MSG_DRV |
11320 NETIF_MSG_PROBE |
11321 NETIF_MSG_LINK);
11322 if (debug < -1)
11323 pf->hw.debug_mask = debug;
Shannon Nelson5b5faa42014-10-17 03:14:51 +000011324
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +000011325 /* do a special CORER for clearing PXE mode once at init */
11326 if (hw->revision_id == 0 &&
11327 (rd32(hw, I40E_GLLAN_RCTL_0) & I40E_GLLAN_RCTL_0_PXE_MODE_MASK)) {
11328 wr32(hw, I40E_GLGEN_RTRIG, I40E_GLGEN_RTRIG_CORER_MASK);
11329 i40e_flush(hw);
11330 msleep(200);
11331 pf->corer_count++;
11332
11333 i40e_clear_pxe_mode(hw);
11334 }
11335
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011336 /* Reset here to make sure all is clean and to define PF 'n' */
Shannon Nelson838d41d2014-06-04 20:41:27 +000011337 i40e_clear_hw(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011338 err = i40e_pf_reset(hw);
11339 if (err) {
11340 dev_info(&pdev->dev, "Initial pf_reset failed: %d\n", err);
11341 goto err_pf_reset;
11342 }
11343 pf->pfr_count++;
11344
11345 hw->aq.num_arq_entries = I40E_AQ_LEN;
11346 hw->aq.num_asq_entries = I40E_AQ_LEN;
11347 hw->aq.arq_buf_size = I40E_MAX_AQ_BUF_SIZE;
11348 hw->aq.asq_buf_size = I40E_MAX_AQ_BUF_SIZE;
11349 pf->adminq_work_limit = I40E_AQ_WORK_LIMIT;
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +000011350
Carolyn Wybornyb294ac72014-12-11 07:06:39 +000011351 snprintf(pf->int_name, sizeof(pf->int_name) - 1,
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +000011352 "%s-%s:misc",
11353 dev_driver_string(&pf->pdev->dev), dev_name(&pdev->dev));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011354
11355 err = i40e_init_shared_code(hw);
11356 if (err) {
Anjali Singhai Jainb2a75c52015-04-27 14:57:20 -040011357 dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
11358 err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011359 goto err_pf_reset;
11360 }
11361
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +000011362 /* set up a default setting for link flow control */
11363 pf->hw.fc.requested_mode = I40E_FC_NONE;
11364
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011365 err = i40e_init_adminq(hw);
Carolyn Wyborny2b2426a762015-10-26 19:44:35 -040011366 if (err) {
11367 if (err == I40E_ERR_FIRMWARE_API_VERSION)
11368 dev_info(&pdev->dev,
11369 "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");
11370 else
11371 dev_info(&pdev->dev,
11372 "The driver for the device stopped because the device firmware failed to init. Try updating your NVM image.\n");
11373
11374 goto err_pf_reset;
11375 }
Filip Sadowski5bbb2e22017-06-07 05:43:09 -040011376 i40e_get_oem_version(hw);
Carolyn Wybornyf0b44442015-08-31 19:54:49 -040011377
Shannon Nelson6dec1012015-09-28 14:12:30 -040011378 /* provide nvm, fw, api versions */
11379 dev_info(&pdev->dev, "fw %d.%d.%05d api %d.%d nvm %s\n",
11380 hw->aq.fw_maj_ver, hw->aq.fw_min_ver, hw->aq.fw_build,
11381 hw->aq.api_maj_ver, hw->aq.api_min_ver,
11382 i40e_nvm_version_str(hw));
Carolyn Wybornyf0b44442015-08-31 19:54:49 -040011383
Catherine Sullivan7aa67612014-07-09 07:46:17 +000011384 if (hw->aq.api_maj_ver == I40E_FW_API_VERSION_MAJOR &&
11385 hw->aq.api_min_ver > I40E_FW_API_VERSION_MINOR)
Shannon Nelson278b6f62014-06-04 01:41:03 +000011386 dev_info(&pdev->dev,
Catherine Sullivan7aa67612014-07-09 07:46:17 +000011387 "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");
Mariusz Stachurae04ea002017-07-14 09:10:19 -040011388 else if (hw->aq.api_maj_ver == 1 && hw->aq.api_min_ver < 4)
Shannon Nelson278b6f62014-06-04 01:41:03 +000011389 dev_info(&pdev->dev,
Catherine Sullivan7aa67612014-07-09 07:46:17 +000011390 "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 +000011391
Shannon Nelson4eb3f762014-03-06 08:59:58 +000011392 i40e_verify_eeprom(pf);
11393
Jesse Brandeburg2c5fe332014-04-23 04:49:57 +000011394 /* Rev 0 hardware was never productized */
11395 if (hw->revision_id < 1)
11396 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");
11397
Shannon Nelson6ff4ef82013-12-21 05:44:49 +000011398 i40e_clear_pxe_mode(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011399 err = i40e_get_capabilities(pf);
11400 if (err)
11401 goto err_adminq_setup;
11402
11403 err = i40e_sw_init(pf);
11404 if (err) {
11405 dev_info(&pdev->dev, "sw_init failed: %d\n", err);
11406 goto err_sw_init;
11407 }
11408
11409 err = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
Alexander Duyckc76cb6e2017-02-21 15:55:48 -080011410 hw->func_caps.num_rx_qp, 0, 0);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011411 if (err) {
11412 dev_info(&pdev->dev, "init_lan_hmc failed: %d\n", err);
11413 goto err_init_lan_hmc;
11414 }
11415
11416 err = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
11417 if (err) {
11418 dev_info(&pdev->dev, "configure_lan_hmc failed: %d\n", err);
11419 err = -ENOENT;
11420 goto err_configure_lan_hmc;
11421 }
11422
Neerav Parikhb686ece2014-12-14 01:55:11 +000011423 /* Disable LLDP for NICs that have firmware versions lower than v4.3.
11424 * Ignore error return codes because if it was already disabled via
11425 * hardware settings this will fail
11426 */
Jacob Kellerd36e41d2017-06-23 04:24:46 -040011427 if (pf->hw_features & I40E_HW_STOP_FW_LLDP) {
Neerav Parikhb686ece2014-12-14 01:55:11 +000011428 dev_info(&pdev->dev, "Stopping firmware LLDP agent.\n");
11429 i40e_aq_stop_lldp(hw, true, NULL);
11430 }
11431
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050011432 /* allow a platform config to override the HW addr */
11433 i40e_get_platform_mac_addr(pdev, pf);
Jacob Keller41c4c2b2017-04-05 07:50:57 -040011434
Jesse Brandeburgf62b5062013-11-28 06:39:27 +000011435 if (!is_valid_ether_addr(hw->mac.addr)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011436 dev_info(&pdev->dev, "invalid MAC address %pM\n", hw->mac.addr);
11437 err = -EIO;
11438 goto err_mac_addr;
11439 }
11440 dev_info(&pdev->dev, "MAC address: %pM\n", hw->mac.addr);
Greg Rose9a173902014-05-22 06:32:02 +000011441 ether_addr_copy(hw->mac.perm_addr, hw->mac.addr);
Neerav Parikh1f224ad2014-02-12 01:45:31 +000011442 i40e_get_port_mac_addr(hw, hw->mac.port_addr);
11443 if (is_valid_ether_addr(hw->mac.port_addr))
Jacob Kellerd36e41d2017-06-23 04:24:46 -040011444 pf->hw_features |= I40E_HW_PORT_ID_VALID;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011445
11446 pci_set_drvdata(pdev, pf);
11447 pci_save_state(pdev);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -080011448#ifdef CONFIG_I40E_DCB
11449 err = i40e_init_pf_dcb(pf);
11450 if (err) {
Shannon Nelsonaebfc812014-12-11 07:06:38 +000011451 dev_info(&pdev->dev, "DCB init failed %d, disabled\n", err);
David Ertmanc17ef432016-09-30 01:36:21 -070011452 pf->flags &= ~(I40E_FLAG_DCB_CAPABLE | I40E_FLAG_DCB_ENABLED);
Neerav Parikh014269f2014-04-01 07:11:48 +000011453 /* Continue without DCB enabled */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -080011454 }
11455#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011456
11457 /* set up periodic task facility */
11458 setup_timer(&pf->service_timer, i40e_service_timer, (unsigned long)pf);
11459 pf->service_timer_period = HZ;
11460
11461 INIT_WORK(&pf->service_task, i40e_service_task);
Jacob Keller0da36b92017-04-19 09:25:55 -040011462 clear_bit(__I40E_SERVICE_SCHED, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011463
Shannon Nelson1d5109d2015-08-26 15:14:08 -040011464 /* NVM bit on means WoL disabled for the port */
11465 i40e_read_nvm_word(hw, I40E_SR_NVM_WAKE_ON_LAN, &wol_nvm_bits);
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -080011466 if (BIT (hw->port) & wol_nvm_bits || hw->partition_id != 1)
Shannon Nelson1d5109d2015-08-26 15:14:08 -040011467 pf->wol_en = false;
11468 else
11469 pf->wol_en = true;
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011470 device_set_wakeup_enable(&pf->pdev->dev, pf->wol_en);
11471
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011472 /* set up the main switch operations */
11473 i40e_determine_queue_usage(pf);
Jesse Brandeburgc11472802015-04-07 19:45:39 -040011474 err = i40e_init_interrupt_scheme(pf);
11475 if (err)
11476 goto err_switch_setup;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011477
Mitch Williams505682c2014-05-20 08:01:37 +000011478 /* The number of VSIs reported by the FW is the minimum guaranteed
11479 * to us; HW supports far more and we share the remaining pool with
11480 * the other PFs. We allocate space for more than the guarantee with
11481 * the understanding that we might not get them all later.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011482 */
Mitch Williams505682c2014-05-20 08:01:37 +000011483 if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
11484 pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
11485 else
11486 pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
11487
11488 /* Set up the *vsi struct and our local tracking of the MAIN PF vsi. */
Jesse Brandeburgd17038d2015-12-23 12:05:55 -080011489 pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
11490 GFP_KERNEL);
Wei Yongjuned87ac02013-09-24 05:17:25 +000011491 if (!pf->vsi) {
11492 err = -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011493 goto err_switch_setup;
Wei Yongjuned87ac02013-09-24 05:17:25 +000011494 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011495
Anjali Singhai Jainfa11cb32015-05-27 12:06:14 -040011496#ifdef CONFIG_PCI_IOV
11497 /* prep for VF support */
11498 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
11499 (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
Jacob Keller0da36b92017-04-19 09:25:55 -040011500 !test_bit(__I40E_BAD_EEPROM, pf->state)) {
Anjali Singhai Jainfa11cb32015-05-27 12:06:14 -040011501 if (pci_num_vf(pdev))
11502 pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
11503 }
11504#endif
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000011505 err = i40e_setup_pf_switch(pf, false);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011506 if (err) {
11507 dev_info(&pdev->dev, "setup_pf_switch failed: %d\n", err);
11508 goto err_vsis;
11509 }
Helin Zhang58fc3262015-10-01 14:37:38 -040011510
11511 /* Make sure flow control is set according to current settings */
11512 err = i40e_set_fc(hw, &set_fc_aq_fail, true);
11513 if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_GET)
11514 dev_dbg(&pf->pdev->dev,
11515 "Set fc with err %s aq_err %s on get_phy_cap\n",
11516 i40e_stat_str(hw, err),
11517 i40e_aq_str(hw, hw->aq.asq_last_status));
11518 if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_SET)
11519 dev_dbg(&pf->pdev->dev,
11520 "Set fc with err %s aq_err %s on set_phy_config\n",
11521 i40e_stat_str(hw, err),
11522 i40e_aq_str(hw, hw->aq.asq_last_status));
11523 if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_UPDATE)
11524 dev_dbg(&pf->pdev->dev,
11525 "Set fc with err %s aq_err %s on get_link_info\n",
11526 i40e_stat_str(hw, err),
11527 i40e_aq_str(hw, hw->aq.asq_last_status));
11528
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +000011529 /* if FDIR VSI was set up, start it now */
Mitch Williams505682c2014-05-20 08:01:37 +000011530 for (i = 0; i < pf->num_alloc_vsi; i++) {
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +000011531 if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
11532 i40e_vsi_open(pf->vsi[i]);
11533 break;
11534 }
11535 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011536
Shannon Nelson2f0aff42016-01-04 10:33:08 -080011537 /* The driver only wants link up/down and module qualification
11538 * reports from firmware. Note the negative logic.
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +000011539 */
11540 err = i40e_aq_set_phy_int_mask(&pf->hw,
Shannon Nelson2f0aff42016-01-04 10:33:08 -080011541 ~(I40E_AQ_EVENT_LINK_UPDOWN |
Shannon Nelson867a79e2016-03-18 12:18:15 -070011542 I40E_AQ_EVENT_MEDIA_NA |
Shannon Nelson2f0aff42016-01-04 10:33:08 -080011543 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +000011544 if (err)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040011545 dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
11546 i40e_stat_str(&pf->hw, err),
11547 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +000011548
Anjali Singhai Jain4f2f017c2015-10-21 19:47:07 -040011549 /* Reconfigure hardware for allowing smaller MSS in the case
11550 * of TSO, so that we avoid the MDD being fired and causing
11551 * a reset in the case of small MSS+TSO.
11552 */
11553 val = rd32(hw, I40E_REG_MSS);
11554 if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
11555 val &= ~I40E_REG_MSS_MIN_MASK;
11556 val |= I40E_64BYTE_MSS;
11557 wr32(hw, I40E_REG_MSS, val);
11558 }
11559
Jacob Kellerd36e41d2017-06-23 04:24:46 -040011560 if (pf->hw_features & I40E_HW_RESTART_AUTONEG) {
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +000011561 msleep(75);
11562 err = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
11563 if (err)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040011564 dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
11565 i40e_stat_str(&pf->hw, err),
11566 i40e_aq_str(&pf->hw,
11567 pf->hw.aq.asq_last_status));
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +000011568 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011569 /* The main driver is (mostly) up and happy. We need to set this state
11570 * before setting up the misc vector or we get a race and the vector
11571 * ends up disabled forever.
11572 */
Mauro S. M. Rodrigues9e6c9c02017-05-12 23:26:56 -030011573 clear_bit(__I40E_DOWN, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011574
11575 /* In case of MSIX we are going to setup the misc vector right here
11576 * to handle admin queue events etc. In case of legacy and MSI
11577 * the misc functionality and queue processing is combined in
11578 * the same vector and that gets setup at open.
11579 */
11580 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
11581 err = i40e_setup_misc_vector(pf);
11582 if (err) {
11583 dev_info(&pdev->dev,
11584 "setup of misc vector failed: %d\n", err);
11585 goto err_vsis;
11586 }
11587 }
11588
Greg Rosedf805f62014-04-04 04:43:16 +000011589#ifdef CONFIG_PCI_IOV
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011590 /* prep for VF support */
11591 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
Shannon Nelson4eb3f762014-03-06 08:59:58 +000011592 (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
Jacob Keller0da36b92017-04-19 09:25:55 -040011593 !test_bit(__I40E_BAD_EEPROM, pf->state)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011594 /* disable link interrupts for VFs */
11595 val = rd32(hw, I40E_PFGEN_PORTMDIO_NUM);
11596 val &= ~I40E_PFGEN_PORTMDIO_NUM_VFLINK_STAT_ENA_MASK;
11597 wr32(hw, I40E_PFGEN_PORTMDIO_NUM, val);
11598 i40e_flush(hw);
Mitch Williams4aeec012014-02-13 03:48:47 -080011599
11600 if (pci_num_vf(pdev)) {
11601 dev_info(&pdev->dev,
11602 "Active VFs found, allocating resources.\n");
11603 err = i40e_alloc_vfs(pf, pci_num_vf(pdev));
11604 if (err)
11605 dev_info(&pdev->dev,
11606 "Error %d allocating resources for existing VFs\n",
11607 err);
11608 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011609 }
Greg Rosedf805f62014-04-04 04:43:16 +000011610#endif /* CONFIG_PCI_IOV */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011611
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060011612 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11613 pf->iwarp_base_vector = i40e_get_lump(pf, pf->irq_pile,
11614 pf->num_iwarp_msix,
11615 I40E_IWARP_IRQ_PILE_ID);
11616 if (pf->iwarp_base_vector < 0) {
11617 dev_info(&pdev->dev,
11618 "failed to get tracking for %d vectors for IWARP err=%d\n",
11619 pf->num_iwarp_msix, pf->iwarp_base_vector);
11620 pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
11621 }
11622 }
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +000011623
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011624 i40e_dbg_pf_init(pf);
11625
11626 /* tell the firmware that we're starting */
Jesse Brandeburg44033fa2014-04-23 04:50:15 +000011627 i40e_send_version(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011628
11629 /* since everything's happy, start the service_task timer */
11630 mod_timer(&pf->service_timer,
11631 round_jiffies(jiffies + pf->service_timer_period));
11632
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060011633 /* add this PF to client device list and launch a client service task */
Mitch Williams004eb612017-04-04 12:40:16 -070011634 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11635 err = i40e_lan_add_device(pf);
11636 if (err)
11637 dev_info(&pdev->dev, "Failed to add PF to client API service list: %d\n",
11638 err);
11639 }
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060011640
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040011641#define PCI_SPEED_SIZE 8
11642#define PCI_WIDTH_SIZE 8
11643 /* Devices on the IOSF bus do not have this information
11644 * and will report PCI Gen 1 x 1 by default so don't bother
11645 * checking them.
11646 */
Jacob Kellerd36e41d2017-06-23 04:24:46 -040011647 if (!(pf->hw_features & I40E_HW_NO_PCI_LINK_CHECK)) {
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040011648 char speed[PCI_SPEED_SIZE] = "Unknown";
11649 char width[PCI_WIDTH_SIZE] = "Unknown";
Catherine Sullivand4dfb812013-11-28 06:39:21 +000011650
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040011651 /* Get the negotiated link width and speed from PCI config
11652 * space
11653 */
11654 pcie_capability_read_word(pf->pdev, PCI_EXP_LNKSTA,
11655 &link_status);
Catherine Sullivand4dfb812013-11-28 06:39:21 +000011656
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040011657 i40e_set_pci_config_data(hw, link_status);
Catherine Sullivand4dfb812013-11-28 06:39:21 +000011658
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040011659 switch (hw->bus.speed) {
11660 case i40e_bus_speed_8000:
11661 strncpy(speed, "8.0", PCI_SPEED_SIZE); break;
11662 case i40e_bus_speed_5000:
11663 strncpy(speed, "5.0", PCI_SPEED_SIZE); break;
11664 case i40e_bus_speed_2500:
11665 strncpy(speed, "2.5", PCI_SPEED_SIZE); break;
11666 default:
11667 break;
11668 }
11669 switch (hw->bus.width) {
11670 case i40e_bus_width_pcie_x8:
11671 strncpy(width, "8", PCI_WIDTH_SIZE); break;
11672 case i40e_bus_width_pcie_x4:
11673 strncpy(width, "4", PCI_WIDTH_SIZE); break;
11674 case i40e_bus_width_pcie_x2:
11675 strncpy(width, "2", PCI_WIDTH_SIZE); break;
11676 case i40e_bus_width_pcie_x1:
11677 strncpy(width, "1", PCI_WIDTH_SIZE); break;
11678 default:
11679 break;
11680 }
11681
11682 dev_info(&pdev->dev, "PCI-Express: Speed %sGT/s Width x%s\n",
11683 speed, width);
11684
11685 if (hw->bus.width < i40e_bus_width_pcie_x8 ||
11686 hw->bus.speed < i40e_bus_speed_8000) {
11687 dev_warn(&pdev->dev, "PCI-Express bandwidth available for this device may be insufficient for optimal performance.\n");
11688 dev_warn(&pdev->dev, "Please move the device to a different PCI-e link with more lanes and/or higher transfer rate.\n");
11689 }
Catherine Sullivand4dfb812013-11-28 06:39:21 +000011690 }
11691
Catherine Sullivane8278452015-02-06 08:52:08 +000011692 /* get the requested speeds from the fw */
11693 err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities, NULL);
11694 if (err)
Neerav Parikh8279e492015-09-03 17:18:50 -040011695 dev_dbg(&pf->pdev->dev, "get requested speeds ret = %s last_status = %s\n",
11696 i40e_stat_str(&pf->hw, err),
11697 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Catherine Sullivane8278452015-02-06 08:52:08 +000011698 pf->hw.phy.link_info.requested_speeds = abilities.link_speed;
11699
Catherine Sullivanfc72dbc2015-09-01 11:36:30 -040011700 /* get the supported phy types from the fw */
11701 err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities, NULL);
11702 if (err)
11703 dev_dbg(&pf->pdev->dev, "get supported phy types ret = %s last_status = %s\n",
11704 i40e_stat_str(&pf->hw, err),
11705 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Catherine Sullivanfc72dbc2015-09-01 11:36:30 -040011706
Anjali Singhai Jaine7358f52015-10-01 14:37:34 -040011707 /* Add a filter to drop all Flow control frames from any VSI from being
11708 * transmitted. By doing so we stop a malicious VF from sending out
11709 * PAUSE or PFC frames and potentially controlling traffic for other
11710 * PF/VF VSIs.
11711 * The FW can still send Flow control frames if enabled.
11712 */
11713 i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
11714 pf->main_vsi_seid);
11715
Carolyn Wyborny31b606d2016-02-17 16:12:12 -080011716 if ((pf->hw.device_id == I40E_DEV_ID_10G_BASE_T) ||
Henry Tieman4f9b4302016-11-08 13:05:18 -080011717 (pf->hw.device_id == I40E_DEV_ID_10G_BASE_T4))
Jacob Kellerd36e41d2017-06-23 04:24:46 -040011718 pf->hw_features |= I40E_HW_PHY_CONTROLS_LEDS;
Harshitha Ramamurthy4ad9f4f2016-11-08 13:05:09 -080011719 if (pf->hw.device_id == I40E_DEV_ID_SFP_I_X722)
Jacob Kellerd36e41d2017-06-23 04:24:46 -040011720 pf->hw_features |= I40E_HW_HAVE_CRT_RETIMER;
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000011721 /* print a string summarizing features */
11722 i40e_print_features(pf);
11723
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011724 return 0;
11725
11726 /* Unwind what we've done if something failed in the setup */
11727err_vsis:
Mauro S. M. Rodrigues9e6c9c02017-05-12 23:26:56 -030011728 set_bit(__I40E_DOWN, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011729 i40e_clear_interrupt_scheme(pf);
11730 kfree(pf->vsi);
Shannon Nelson04b03012013-11-28 06:39:34 +000011731err_switch_setup:
11732 i40e_reset_interrupt_capability(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011733 del_timer_sync(&pf->service_timer);
11734err_mac_addr:
11735err_configure_lan_hmc:
11736 (void)i40e_shutdown_lan_hmc(hw);
11737err_init_lan_hmc:
11738 kfree(pf->qp_pile);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011739err_sw_init:
11740err_adminq_setup:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011741err_pf_reset:
11742 iounmap(hw->hw_addr);
11743err_ioremap:
11744 kfree(pf);
11745err_pf_alloc:
11746 pci_disable_pcie_error_reporting(pdev);
Johannes Thumshirn56d766d2016-06-07 09:44:05 +020011747 pci_release_mem_regions(pdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011748err_pci_reg:
11749err_dma:
11750 pci_disable_device(pdev);
11751 return err;
11752}
11753
11754/**
11755 * i40e_remove - Device removal routine
11756 * @pdev: PCI device information struct
11757 *
11758 * i40e_remove is called by the PCI subsystem to alert the driver
11759 * that is should release a PCI device. This could be caused by a
11760 * Hot-Plug event, or because the driver is going to be removed from
11761 * memory.
11762 **/
11763static void i40e_remove(struct pci_dev *pdev)
11764{
11765 struct i40e_pf *pf = pci_get_drvdata(pdev);
Carolyn Wybornybcab2db2015-09-28 14:16:55 -040011766 struct i40e_hw *hw = &pf->hw;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011767 i40e_status ret_code;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011768 int i;
11769
11770 i40e_dbg_pf_exit(pf);
11771
Jacob Kellerbeb0dff2014-01-11 05:43:19 +000011772 i40e_ptp_stop(pf);
11773
Carolyn Wybornybcab2db2015-09-28 14:16:55 -040011774 /* Disable RSS in hw */
Shannon Nelson272cdaf22016-02-17 16:12:21 -080011775 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), 0);
11776 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), 0);
Carolyn Wybornybcab2db2015-09-28 14:16:55 -040011777
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011778 /* no more scheduling of any task */
Jacob Keller0da36b92017-04-19 09:25:55 -040011779 set_bit(__I40E_SUSPENDED, pf->state);
Mauro S. M. Rodrigues9e6c9c02017-05-12 23:26:56 -030011780 set_bit(__I40E_DOWN, pf->state);
Shannon Nelsonc99abb42016-03-10 14:59:45 -080011781 if (pf->service_timer.data)
11782 del_timer_sync(&pf->service_timer);
11783 if (pf->service_task.func)
11784 cancel_work_sync(&pf->service_task);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011785
Mitch Williams921c4672017-03-30 00:46:08 -070011786 /* Client close must be called explicitly here because the timer
11787 * has been stopped.
11788 */
11789 i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
11790
Mitch Williamseb2d80b2014-02-13 03:48:48 -080011791 if (pf->flags & I40E_FLAG_SRIOV_ENABLED) {
11792 i40e_free_vfs(pf);
11793 pf->flags &= ~I40E_FLAG_SRIOV_ENABLED;
11794 }
11795
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011796 i40e_fdir_teardown(pf);
11797
11798 /* If there is a switch structure or any orphans, remove them.
11799 * This will leave only the PF's VSI remaining.
11800 */
11801 for (i = 0; i < I40E_MAX_VEB; i++) {
11802 if (!pf->veb[i])
11803 continue;
11804
11805 if (pf->veb[i]->uplink_seid == pf->mac_seid ||
11806 pf->veb[i]->uplink_seid == 0)
11807 i40e_switch_branch_release(pf->veb[i]);
11808 }
11809
11810 /* Now we can shutdown the PF's VSI, just before we kill
11811 * adminq and hmc.
11812 */
11813 if (pf->vsi[pf->lan_vsi])
11814 i40e_vsi_release(pf->vsi[pf->lan_vsi]);
11815
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060011816 /* remove attached clients */
Mitch Williams004eb612017-04-04 12:40:16 -070011817 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11818 ret_code = i40e_lan_del_device(pf);
11819 if (ret_code)
11820 dev_warn(&pdev->dev, "Failed to delete client device: %d\n",
11821 ret_code);
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060011822 }
11823
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011824 /* shutdown and destroy the HMC */
Jesse Brandeburgf734dff2016-01-15 14:33:11 -080011825 if (hw->hmc.hmc_obj) {
11826 ret_code = i40e_shutdown_lan_hmc(hw);
Shannon Nelson60442de2014-04-23 04:50:13 +000011827 if (ret_code)
11828 dev_warn(&pdev->dev,
11829 "Failed to destroy the HMC resources: %d\n",
11830 ret_code);
11831 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011832
11833 /* shutdown the adminq */
Henry Tiemanac9c5c62016-09-06 18:05:11 -070011834 i40e_shutdown_adminq(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011835
Jesse Brandeburg8ddb3322015-11-18 15:47:06 -080011836 /* destroy the locks only once, here */
11837 mutex_destroy(&hw->aq.arq_mutex);
11838 mutex_destroy(&hw->aq.asq_mutex);
11839
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011840 /* Clear all dynamic memory lists of rings, q_vectors, and VSIs */
11841 i40e_clear_interrupt_scheme(pf);
Mitch Williams505682c2014-05-20 08:01:37 +000011842 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011843 if (pf->vsi[i]) {
11844 i40e_vsi_clear_rings(pf->vsi[i]);
11845 i40e_vsi_clear(pf->vsi[i]);
11846 pf->vsi[i] = NULL;
11847 }
11848 }
11849
11850 for (i = 0; i < I40E_MAX_VEB; i++) {
11851 kfree(pf->veb[i]);
11852 pf->veb[i] = NULL;
11853 }
11854
11855 kfree(pf->qp_pile);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011856 kfree(pf->vsi);
11857
Jesse Brandeburgf734dff2016-01-15 14:33:11 -080011858 iounmap(hw->hw_addr);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011859 kfree(pf);
Johannes Thumshirn56d766d2016-06-07 09:44:05 +020011860 pci_release_mem_regions(pdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011861
11862 pci_disable_pcie_error_reporting(pdev);
11863 pci_disable_device(pdev);
11864}
11865
11866/**
11867 * i40e_pci_error_detected - warning that something funky happened in PCI land
11868 * @pdev: PCI device information struct
11869 *
11870 * Called to warn that something happened and the error handling steps
11871 * are in progress. Allows the driver to quiesce things, be ready for
11872 * remediation.
11873 **/
11874static pci_ers_result_t i40e_pci_error_detected(struct pci_dev *pdev,
11875 enum pci_channel_state error)
11876{
11877 struct i40e_pf *pf = pci_get_drvdata(pdev);
11878
11879 dev_info(&pdev->dev, "%s: error %d\n", __func__, error);
11880
Guilherme G Piccoliedfc23ee2016-10-03 00:31:12 -070011881 if (!pf) {
11882 dev_info(&pdev->dev,
11883 "Cannot recover - error happened during device probe\n");
11884 return PCI_ERS_RESULT_DISCONNECT;
11885 }
11886
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011887 /* shutdown all operations */
Jacob Kellerdfc4ff62017-06-07 05:43:13 -040011888 if (!test_bit(__I40E_SUSPENDED, pf->state))
11889 i40e_prep_for_reset(pf, false);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011890
11891 /* Request a slot reset */
11892 return PCI_ERS_RESULT_NEED_RESET;
11893}
11894
11895/**
11896 * i40e_pci_error_slot_reset - a PCI slot reset just happened
11897 * @pdev: PCI device information struct
11898 *
11899 * Called to find if the driver can work with the device now that
11900 * the pci slot has been reset. If a basic connection seems good
11901 * (registers are readable and have sane content) then return a
11902 * happy little PCI_ERS_RESULT_xxx.
11903 **/
11904static pci_ers_result_t i40e_pci_error_slot_reset(struct pci_dev *pdev)
11905{
11906 struct i40e_pf *pf = pci_get_drvdata(pdev);
11907 pci_ers_result_t result;
11908 int err;
11909 u32 reg;
11910
Shannon Nelsonfb43201f2015-08-26 15:14:17 -040011911 dev_dbg(&pdev->dev, "%s\n", __func__);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011912 if (pci_enable_device_mem(pdev)) {
11913 dev_info(&pdev->dev,
11914 "Cannot re-enable PCI device after reset.\n");
11915 result = PCI_ERS_RESULT_DISCONNECT;
11916 } else {
11917 pci_set_master(pdev);
11918 pci_restore_state(pdev);
11919 pci_save_state(pdev);
11920 pci_wake_from_d3(pdev, false);
11921
11922 reg = rd32(&pf->hw, I40E_GLGEN_RTRIG);
11923 if (reg == 0)
11924 result = PCI_ERS_RESULT_RECOVERED;
11925 else
11926 result = PCI_ERS_RESULT_DISCONNECT;
11927 }
11928
11929 err = pci_cleanup_aer_uncorrect_error_status(pdev);
11930 if (err) {
11931 dev_info(&pdev->dev,
11932 "pci_cleanup_aer_uncorrect_error_status failed 0x%0x\n",
11933 err);
11934 /* non-fatal, continue */
11935 }
11936
11937 return result;
11938}
11939
11940/**
11941 * i40e_pci_error_resume - restart operations after PCI error recovery
11942 * @pdev: PCI device information struct
11943 *
11944 * Called to allow the driver to bring things back up after PCI error
11945 * and/or reset recovery has finished.
11946 **/
11947static void i40e_pci_error_resume(struct pci_dev *pdev)
11948{
11949 struct i40e_pf *pf = pci_get_drvdata(pdev);
11950
Shannon Nelsonfb43201f2015-08-26 15:14:17 -040011951 dev_dbg(&pdev->dev, "%s\n", __func__);
Jacob Keller0da36b92017-04-19 09:25:55 -040011952 if (test_bit(__I40E_SUSPENDED, pf->state))
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011953 return;
11954
Jacob Kellerdfc4ff62017-06-07 05:43:13 -040011955 i40e_handle_reset_warning(pf, false);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011956}
11957
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011958/**
Joshua Hay1d680052016-12-12 15:44:08 -080011959 * i40e_enable_mc_magic_wake - enable multicast magic packet wake up
11960 * using the mac_address_write admin q function
11961 * @pf: pointer to i40e_pf struct
11962 **/
11963static void i40e_enable_mc_magic_wake(struct i40e_pf *pf)
11964{
11965 struct i40e_hw *hw = &pf->hw;
11966 i40e_status ret;
11967 u8 mac_addr[6];
11968 u16 flags = 0;
11969
11970 /* Get current MAC address in case it's an LAA */
11971 if (pf->vsi[pf->lan_vsi] && pf->vsi[pf->lan_vsi]->netdev) {
11972 ether_addr_copy(mac_addr,
11973 pf->vsi[pf->lan_vsi]->netdev->dev_addr);
11974 } else {
11975 dev_err(&pf->pdev->dev,
11976 "Failed to retrieve MAC address; using default\n");
11977 ether_addr_copy(mac_addr, hw->mac.addr);
11978 }
11979
11980 /* The FW expects the mac address write cmd to first be called with
11981 * one of these flags before calling it again with the multicast
11982 * enable flags.
11983 */
11984 flags = I40E_AQC_WRITE_TYPE_LAA_WOL;
11985
11986 if (hw->func_caps.flex10_enable && hw->partition_id != 1)
11987 flags = I40E_AQC_WRITE_TYPE_LAA_ONLY;
11988
11989 ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
11990 if (ret) {
11991 dev_err(&pf->pdev->dev,
11992 "Failed to update MAC address registers; cannot enable Multicast Magic packet wake up");
11993 return;
11994 }
11995
11996 flags = I40E_AQC_MC_MAG_EN
11997 | I40E_AQC_WOL_PRESERVE_ON_PFR
11998 | I40E_AQC_WRITE_TYPE_UPDATE_MC_MAG;
11999 ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
12000 if (ret)
12001 dev_err(&pf->pdev->dev,
12002 "Failed to enable Multicast Magic Packet wake up\n");
12003}
12004
12005/**
Shannon Nelson9007bcc2013-11-26 10:49:23 +000012006 * i40e_shutdown - PCI callback for shutting down
12007 * @pdev: PCI device information struct
12008 **/
12009static void i40e_shutdown(struct pci_dev *pdev)
12010{
12011 struct i40e_pf *pf = pci_get_drvdata(pdev);
Shannon Nelson8e2773a2013-11-28 06:39:22 +000012012 struct i40e_hw *hw = &pf->hw;
Shannon Nelson9007bcc2013-11-26 10:49:23 +000012013
Jacob Keller0da36b92017-04-19 09:25:55 -040012014 set_bit(__I40E_SUSPENDED, pf->state);
Mauro S. M. Rodrigues9e6c9c02017-05-12 23:26:56 -030012015 set_bit(__I40E_DOWN, pf->state);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000012016 rtnl_lock();
Maciej Sosin373149f2017-04-05 07:50:55 -040012017 i40e_prep_for_reset(pf, true);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000012018 rtnl_unlock();
12019
Shannon Nelson8e2773a2013-11-28 06:39:22 +000012020 wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
12021 wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
12022
Catherine Sullivan02b42492015-07-10 19:35:59 -040012023 del_timer_sync(&pf->service_timer);
12024 cancel_work_sync(&pf->service_task);
12025 i40e_fdir_teardown(pf);
12026
Mitch Williams921c4672017-03-30 00:46:08 -070012027 /* Client close must be called explicitly here because the timer
12028 * has been stopped.
12029 */
12030 i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
12031
Jacob Kellerd36e41d2017-06-23 04:24:46 -040012032 if (pf->wol_en && (pf->hw_features & I40E_HW_WOL_MC_MAGIC_PKT_WAKE))
Joshua Hay1d680052016-12-12 15:44:08 -080012033 i40e_enable_mc_magic_wake(pf);
12034
Jacob Kellerdfc4ff62017-06-07 05:43:13 -040012035 i40e_prep_for_reset(pf, false);
Catherine Sullivan02b42492015-07-10 19:35:59 -040012036
12037 wr32(hw, I40E_PFPM_APM,
12038 (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
12039 wr32(hw, I40E_PFPM_WUFC,
12040 (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
12041
Shannon Nelsone1477582015-02-21 06:44:33 +000012042 i40e_clear_interrupt_scheme(pf);
12043
Shannon Nelson9007bcc2013-11-26 10:49:23 +000012044 if (system_state == SYSTEM_POWER_OFF) {
Shannon Nelson8e2773a2013-11-28 06:39:22 +000012045 pci_wake_from_d3(pdev, pf->wol_en);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000012046 pci_set_power_state(pdev, PCI_D3hot);
12047 }
12048}
12049
12050#ifdef CONFIG_PM
12051/**
12052 * i40e_suspend - PCI callback for moving to D3
12053 * @pdev: PCI device information struct
12054 **/
12055static int i40e_suspend(struct pci_dev *pdev, pm_message_t state)
12056{
12057 struct i40e_pf *pf = pci_get_drvdata(pdev);
Shannon Nelson8e2773a2013-11-28 06:39:22 +000012058 struct i40e_hw *hw = &pf->hw;
Greg Rose059ff692016-05-16 10:26:38 -070012059 int retval = 0;
Shannon Nelson9007bcc2013-11-26 10:49:23 +000012060
Jacob Keller0da36b92017-04-19 09:25:55 -040012061 set_bit(__I40E_SUSPENDED, pf->state);
Mauro S. M. Rodrigues9e6c9c02017-05-12 23:26:56 -030012062 set_bit(__I40E_DOWN, pf->state);
Mitch Williams3932dbf2015-03-31 00:45:04 -070012063
Jacob Kellerd36e41d2017-06-23 04:24:46 -040012064 if (pf->wol_en && (pf->hw_features & I40E_HW_WOL_MC_MAGIC_PKT_WAKE))
Joshua Hay1d680052016-12-12 15:44:08 -080012065 i40e_enable_mc_magic_wake(pf);
12066
Jacob Kellerdfc4ff62017-06-07 05:43:13 -040012067 i40e_prep_for_reset(pf, false);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000012068
Shannon Nelson8e2773a2013-11-28 06:39:22 +000012069 wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
12070 wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
12071
Greg Roseb33d3b72016-05-16 10:26:37 -070012072 i40e_stop_misc_vector(pf);
Carolyn Wyborny7c9ae7f2017-06-20 15:16:53 -070012073 if (pf->msix_entries) {
12074 synchronize_irq(pf->msix_entries[0].vector);
12075 free_irq(pf->msix_entries[0].vector, pf);
12076 }
Greg Rose059ff692016-05-16 10:26:38 -070012077 retval = pci_save_state(pdev);
12078 if (retval)
12079 return retval;
12080
Shannon Nelson8e2773a2013-11-28 06:39:22 +000012081 pci_wake_from_d3(pdev, pf->wol_en);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000012082 pci_set_power_state(pdev, PCI_D3hot);
12083
Greg Rose059ff692016-05-16 10:26:38 -070012084 return retval;
Shannon Nelson9007bcc2013-11-26 10:49:23 +000012085}
12086
12087/**
12088 * i40e_resume - PCI callback for waking up from D3
12089 * @pdev: PCI device information struct
12090 **/
12091static int i40e_resume(struct pci_dev *pdev)
12092{
12093 struct i40e_pf *pf = pci_get_drvdata(pdev);
12094 u32 err;
12095
12096 pci_set_power_state(pdev, PCI_D0);
12097 pci_restore_state(pdev);
12098 /* pci_restore_state() clears dev->state_saves, so
12099 * call pci_save_state() again to restore it.
12100 */
12101 pci_save_state(pdev);
12102
12103 err = pci_enable_device_mem(pdev);
12104 if (err) {
Shannon Nelsonfb43201f2015-08-26 15:14:17 -040012105 dev_err(&pdev->dev, "Cannot enable PCI device from suspend\n");
Shannon Nelson9007bcc2013-11-26 10:49:23 +000012106 return err;
12107 }
12108 pci_set_master(pdev);
12109
12110 /* no wakeup events while running */
12111 pci_wake_from_d3(pdev, false);
12112
12113 /* handling the reset will rebuild the device state */
Jacob Keller0da36b92017-04-19 09:25:55 -040012114 if (test_and_clear_bit(__I40E_SUSPENDED, pf->state)) {
Mauro S. M. Rodrigues9e6c9c02017-05-12 23:26:56 -030012115 clear_bit(__I40E_DOWN, pf->state);
Carolyn Wyborny7c9ae7f2017-06-20 15:16:53 -070012116 if (pf->msix_entries) {
12117 err = request_irq(pf->msix_entries[0].vector,
12118 i40e_intr, 0, pf->int_name, pf);
12119 if (err) {
12120 dev_err(&pf->pdev->dev,
12121 "request_irq for %s failed: %d\n",
12122 pf->int_name, err);
12123 }
12124 }
Jacob Kellerdfc4ff62017-06-07 05:43:13 -040012125 i40e_reset_and_rebuild(pf, false, false);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000012126 }
12127
12128 return 0;
12129}
12130
12131#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012132static const struct pci_error_handlers i40e_err_handler = {
12133 .error_detected = i40e_pci_error_detected,
12134 .slot_reset = i40e_pci_error_slot_reset,
12135 .resume = i40e_pci_error_resume,
12136};
12137
12138static struct pci_driver i40e_driver = {
12139 .name = i40e_driver_name,
12140 .id_table = i40e_pci_tbl,
12141 .probe = i40e_probe,
12142 .remove = i40e_remove,
Shannon Nelson9007bcc2013-11-26 10:49:23 +000012143#ifdef CONFIG_PM
12144 .suspend = i40e_suspend,
12145 .resume = i40e_resume,
12146#endif
12147 .shutdown = i40e_shutdown,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012148 .err_handler = &i40e_err_handler,
12149 .sriov_configure = i40e_pci_sriov_configure,
12150};
12151
12152/**
12153 * i40e_init_module - Driver registration routine
12154 *
12155 * i40e_init_module is the first routine called when the driver is
12156 * loaded. All it does is register with the PCI subsystem.
12157 **/
12158static int __init i40e_init_module(void)
12159{
12160 pr_info("%s: %s - version %s\n", i40e_driver_name,
12161 i40e_driver_string, i40e_driver_version_str);
12162 pr_info("%s: %s\n", i40e_driver_name, i40e_copyright);
Greg Rose96664482015-02-06 08:52:13 +000012163
Jacob Keller4d5957c2017-06-20 15:16:54 -070012164 /* There is no need to throttle the number of active tasks because
12165 * each device limits its own task using a state bit for scheduling
12166 * the service task, and the device tasks do not interfere with each
12167 * other, so we don't set a max task limit. We must set WQ_MEM_RECLAIM
12168 * since we need to be able to guarantee forward progress even under
12169 * memory pressure.
Jesse Brandeburg2803b162015-12-22 14:25:08 -080012170 */
Jacob Keller4d5957c2017-06-20 15:16:54 -070012171 i40e_wq = alloc_workqueue("%s", WQ_MEM_RECLAIM, 0, i40e_driver_name);
Jesse Brandeburg2803b162015-12-22 14:25:08 -080012172 if (!i40e_wq) {
12173 pr_err("%s: Failed to create workqueue\n", i40e_driver_name);
12174 return -ENOMEM;
12175 }
12176
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012177 i40e_dbg_init();
12178 return pci_register_driver(&i40e_driver);
12179}
12180module_init(i40e_init_module);
12181
12182/**
12183 * i40e_exit_module - Driver exit cleanup routine
12184 *
12185 * i40e_exit_module is called just before the driver is removed
12186 * from memory.
12187 **/
12188static void __exit i40e_exit_module(void)
12189{
12190 pci_unregister_driver(&i40e_driver);
Jesse Brandeburg2803b162015-12-22 14:25:08 -080012191 destroy_workqueue(i40e_wq);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012192 i40e_dbg_exit();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012193}
12194module_exit(i40e_exit_module);