blob: 4104944ea36709120709a6e347f2af1abcdd20ff [file] [log] [blame]
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001/*******************************************************************************
2 *
3 * Intel Ethernet Controller XL710 Family Linux Driver
Shannon Nelson4fc8c672017-06-07 05:43:08 -04004 * Copyright(c) 2013 - 2017 Intel Corporation.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
Greg Rosedc641b72013-12-18 13:45:51 +000015 * You should have received a copy of the GNU General Public License along
16 * with this program. If not, see <http://www.gnu.org/licenses/>.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000017 *
18 * The full GNU General Public License is included in this distribution in
19 * the file called "COPYING".
20 *
21 * Contact Information:
22 * e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
23 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
24 *
25 ******************************************************************************/
26
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050027#include <linux/etherdevice.h>
28#include <linux/of_net.h>
29#include <linux/pci.h>
Björn Töpel0c8493d2017-05-24 07:55:34 +020030#include <linux/bpf.h>
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050031
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000032/* Local includes */
33#include "i40e.h"
Shannon Nelson4eb3f762014-03-06 08:59:58 +000034#include "i40e_diag.h"
Alexander Duyck06a5f7f2016-06-16 12:22:06 -070035#include <net/udp_tunnel.h>
Scott Petersoned0980c2017-04-13 04:45:44 -040036/* All i40e tracepoints are defined by the include below, which
37 * must be included exactly once across the whole kernel with
38 * CREATE_TRACE_POINTS defined
39 */
40#define CREATE_TRACE_POINTS
41#include "i40e_trace.h"
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000042
43const char i40e_driver_name[] = "i40e";
44static const char i40e_driver_string[] =
45 "Intel(R) Ethernet Connection XL710 Network Driver";
46
47#define DRV_KERN "-k"
48
Bimmy Pujari15990832017-01-30 12:29:37 -080049#define DRV_VERSION_MAJOR 2
50#define DRV_VERSION_MINOR 1
Jacob Keller2318b402017-04-19 09:25:53 -040051#define DRV_VERSION_BUILD 14
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000052#define DRV_VERSION __stringify(DRV_VERSION_MAJOR) "." \
53 __stringify(DRV_VERSION_MINOR) "." \
54 __stringify(DRV_VERSION_BUILD) DRV_KERN
55const char i40e_driver_version_str[] = DRV_VERSION;
Jesse Brandeburg8fb905b2014-01-17 15:36:33 -080056static const char i40e_copyright[] = "Copyright (c) 2013 - 2014 Intel Corporation.";
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000057
58/* a bit of forward declarations */
59static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi);
Maciej Sosin373149f2017-04-05 07:50:55 -040060static void i40e_handle_reset_warning(struct i40e_pf *pf, bool lock_acquired);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000061static int i40e_add_vsi(struct i40e_vsi *vsi);
62static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi);
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000063static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000064static int i40e_setup_misc_vector(struct i40e_pf *pf);
65static void i40e_determine_queue_usage(struct i40e_pf *pf);
66static int i40e_setup_pf_filter_control(struct i40e_pf *pf);
Maciej Sosin373149f2017-04-05 07:50:55 -040067static void i40e_prep_for_reset(struct i40e_pf *pf, bool lock_acquired);
68static int i40e_reset(struct i40e_pf *pf);
69static void i40e_rebuild(struct i40e_pf *pf, bool reinit, bool lock_acquired);
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080070static void i40e_fdir_sb_setup(struct i40e_pf *pf);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -080071static int i40e_veb_get_bw_info(struct i40e_veb *veb);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000072
73/* i40e_pci_tbl - PCI Device ID Table
74 *
75 * Last entry must be all 0s
76 *
77 * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
78 * Class, Class Mask, private data (not used) }
79 */
Benoit Taine9baa3c32014-08-08 15:56:03 +020080static const struct pci_device_id i40e_pci_tbl[] = {
Shannon Nelsonab600852014-01-17 15:36:39 -080081 {PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_XL710), 0},
Shannon Nelsonab600852014-01-17 15:36:39 -080082 {PCI_VDEVICE(INTEL, I40E_DEV_ID_QEMU), 0},
Shannon Nelsonab600852014-01-17 15:36:39 -080083 {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_B), 0},
84 {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_C), 0},
Shannon Nelsonab600852014-01-17 15:36:39 -080085 {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_A), 0},
86 {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_B), 0},
87 {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_C), 0},
Mitch Williams5960d332014-09-13 07:40:47 +000088 {PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T), 0},
Shannon Nelsonbc5166b92015-08-26 15:14:10 -040089 {PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T4), 0},
Anjali Singhai Jain35dae512015-12-22 14:25:03 -080090 {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_X722), 0},
91 {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_X722), 0},
Anjali Singhai Jain87e6c1d2015-06-05 12:20:25 -040092 {PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_X722), 0},
93 {PCI_VDEVICE(INTEL, I40E_DEV_ID_1G_BASE_T_X722), 0},
94 {PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T_X722), 0},
Catherine Sullivand6bf58c2016-03-18 12:18:08 -070095 {PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_I_X722), 0},
Shannon Nelson48a3b512015-07-23 16:54:39 -040096 {PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2), 0},
97 {PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2_A), 0},
Carolyn Wyborny31232372016-11-21 13:03:48 -080098 {PCI_VDEVICE(INTEL, I40E_DEV_ID_25G_B), 0},
99 {PCI_VDEVICE(INTEL, I40E_DEV_ID_25G_SFP28), 0},
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000100 /* required last entry */
101 {0, }
102};
103MODULE_DEVICE_TABLE(pci, i40e_pci_tbl);
104
105#define I40E_MAX_VF_COUNT 128
106static int debug = -1;
Alexander Duyck5d4ca232016-09-30 08:21:46 -0400107module_param(debug, uint, 0);
108MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all), Debug mask (0x8XXXXXXX)");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000109
110MODULE_AUTHOR("Intel Corporation, <e1000-devel@lists.sourceforge.net>");
111MODULE_DESCRIPTION("Intel(R) Ethernet Connection XL710 Network Driver");
112MODULE_LICENSE("GPL");
113MODULE_VERSION(DRV_VERSION);
114
Jesse Brandeburg2803b162015-12-22 14:25:08 -0800115static struct workqueue_struct *i40e_wq;
116
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000117/**
118 * i40e_allocate_dma_mem_d - OS specific memory alloc for shared code
119 * @hw: pointer to the HW structure
120 * @mem: ptr to mem struct to fill out
121 * @size: size of memory requested
122 * @alignment: what to align the allocation to
123 **/
124int i40e_allocate_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem,
125 u64 size, u32 alignment)
126{
127 struct i40e_pf *pf = (struct i40e_pf *)hw->back;
128
129 mem->size = ALIGN(size, alignment);
130 mem->va = dma_zalloc_coherent(&pf->pdev->dev, mem->size,
131 &mem->pa, GFP_KERNEL);
Jesse Brandeburg93bc73b2013-09-13 08:23:18 +0000132 if (!mem->va)
133 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000134
Jesse Brandeburg93bc73b2013-09-13 08:23:18 +0000135 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000136}
137
138/**
139 * i40e_free_dma_mem_d - OS specific memory free for shared code
140 * @hw: pointer to the HW structure
141 * @mem: ptr to mem struct to free
142 **/
143int i40e_free_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem)
144{
145 struct i40e_pf *pf = (struct i40e_pf *)hw->back;
146
147 dma_free_coherent(&pf->pdev->dev, mem->size, mem->va, mem->pa);
148 mem->va = NULL;
149 mem->pa = 0;
150 mem->size = 0;
151
152 return 0;
153}
154
155/**
156 * i40e_allocate_virt_mem_d - OS specific memory alloc for shared code
157 * @hw: pointer to the HW structure
158 * @mem: ptr to mem struct to fill out
159 * @size: size of memory requested
160 **/
161int i40e_allocate_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem,
162 u32 size)
163{
164 mem->size = size;
165 mem->va = kzalloc(size, GFP_KERNEL);
166
Jesse Brandeburg93bc73b2013-09-13 08:23:18 +0000167 if (!mem->va)
168 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000169
Jesse Brandeburg93bc73b2013-09-13 08:23:18 +0000170 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000171}
172
173/**
174 * i40e_free_virt_mem_d - OS specific memory free for shared code
175 * @hw: pointer to the HW structure
176 * @mem: ptr to mem struct to free
177 **/
178int i40e_free_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem)
179{
180 /* it's ok to kfree a NULL pointer */
181 kfree(mem->va);
182 mem->va = NULL;
183 mem->size = 0;
184
185 return 0;
186}
187
188/**
189 * i40e_get_lump - find a lump of free generic resource
190 * @pf: board private structure
191 * @pile: the pile of resource to search
192 * @needed: the number of items needed
193 * @id: an owner id to stick on the items assigned
194 *
195 * Returns the base item index of the lump, or negative for error
196 *
197 * The search_hint trick and lack of advanced fit-finding only work
198 * because we're highly likely to have all the same size lump requests.
199 * Linear search time and any fragmentation should be minimal.
200 **/
201static int i40e_get_lump(struct i40e_pf *pf, struct i40e_lump_tracking *pile,
202 u16 needed, u16 id)
203{
204 int ret = -ENOMEM;
Jesse Brandeburgddf434a2013-09-13 08:23:19 +0000205 int i, j;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000206
207 if (!pile || needed == 0 || id >= I40E_PILE_VALID_BIT) {
208 dev_info(&pf->pdev->dev,
209 "param err: pile=%p needed=%d id=0x%04x\n",
210 pile, needed, id);
211 return -EINVAL;
212 }
213
214 /* start the linear search with an imperfect hint */
215 i = pile->search_hint;
Jesse Brandeburgddf434a2013-09-13 08:23:19 +0000216 while (i < pile->num_entries) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000217 /* skip already allocated entries */
218 if (pile->list[i] & I40E_PILE_VALID_BIT) {
219 i++;
220 continue;
221 }
222
223 /* do we have enough in this lump? */
224 for (j = 0; (j < needed) && ((i+j) < pile->num_entries); j++) {
225 if (pile->list[i+j] & I40E_PILE_VALID_BIT)
226 break;
227 }
228
229 if (j == needed) {
230 /* there was enough, so assign it to the requestor */
231 for (j = 0; j < needed; j++)
232 pile->list[i+j] = id | I40E_PILE_VALID_BIT;
233 ret = i;
234 pile->search_hint = i + j;
Jesse Brandeburgddf434a2013-09-13 08:23:19 +0000235 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000236 }
Jesse Brandeburg6995b362015-08-28 17:55:54 -0400237
238 /* not enough, so skip over it and continue looking */
239 i += j;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000240 }
241
242 return ret;
243}
244
245/**
246 * i40e_put_lump - return a lump of generic resource
247 * @pile: the pile of resource to search
248 * @index: the base item index
249 * @id: the owner id of the items assigned
250 *
251 * Returns the count of items in the lump
252 **/
253static int i40e_put_lump(struct i40e_lump_tracking *pile, u16 index, u16 id)
254{
255 int valid_id = (id | I40E_PILE_VALID_BIT);
256 int count = 0;
257 int i;
258
259 if (!pile || index >= pile->num_entries)
260 return -EINVAL;
261
262 for (i = index;
263 i < pile->num_entries && pile->list[i] == valid_id;
264 i++) {
265 pile->list[i] = 0;
266 count++;
267 }
268
269 if (count && index < pile->search_hint)
270 pile->search_hint = index;
271
272 return count;
273}
274
275/**
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700276 * i40e_find_vsi_from_id - searches for the vsi with the given id
277 * @pf - the pf structure to search for the vsi
278 * @id - id of the vsi it is searching for
279 **/
280struct i40e_vsi *i40e_find_vsi_from_id(struct i40e_pf *pf, u16 id)
281{
282 int i;
283
284 for (i = 0; i < pf->num_alloc_vsi; i++)
285 if (pf->vsi[i] && (pf->vsi[i]->id == id))
286 return pf->vsi[i];
287
288 return NULL;
289}
290
291/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000292 * i40e_service_event_schedule - Schedule the service task to wake up
293 * @pf: board private structure
294 *
295 * If not already scheduled, this puts the task into the work queue
296 **/
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -0600297void i40e_service_event_schedule(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000298{
Mauro S. M. Rodrigues9e6c9c02017-05-12 23:26:56 -0300299 if (!test_bit(__I40E_DOWN, pf->state) &&
Jacob Keller0da36b92017-04-19 09:25:55 -0400300 !test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
Jesse Brandeburg2803b162015-12-22 14:25:08 -0800301 queue_work(i40e_wq, &pf->service_task);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000302}
303
304/**
305 * i40e_tx_timeout - Respond to a Tx Hang
306 * @netdev: network interface device structure
307 *
308 * If any port has noticed a Tx timeout, it is likely that the whole
309 * device is munged, not just the one netdev port, so go for the full
310 * reset.
311 **/
312static void i40e_tx_timeout(struct net_device *netdev)
313{
314 struct i40e_netdev_priv *np = netdev_priv(netdev);
315 struct i40e_vsi *vsi = np->vsi;
316 struct i40e_pf *pf = vsi->back;
Kiran Patilb03a8c12015-09-24 18:13:15 -0400317 struct i40e_ring *tx_ring = NULL;
318 unsigned int i, hung_queue = 0;
319 u32 head, val;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000320
321 pf->tx_timeout_count++;
322
Kiran Patilb03a8c12015-09-24 18:13:15 -0400323 /* find the stopped queue the same way the stack does */
324 for (i = 0; i < netdev->num_tx_queues; i++) {
325 struct netdev_queue *q;
326 unsigned long trans_start;
327
328 q = netdev_get_tx_queue(netdev, i);
Florian Westphal9b366272016-05-03 16:33:14 +0200329 trans_start = q->trans_start;
Kiran Patilb03a8c12015-09-24 18:13:15 -0400330 if (netif_xmit_stopped(q) &&
331 time_after(jiffies,
332 (trans_start + netdev->watchdog_timeo))) {
333 hung_queue = i;
334 break;
335 }
336 }
337
338 if (i == netdev->num_tx_queues) {
339 netdev_info(netdev, "tx_timeout: no netdev hung queue found\n");
340 } else {
341 /* now that we have an index, find the tx_ring struct */
342 for (i = 0; i < vsi->num_queue_pairs; i++) {
343 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc) {
344 if (hung_queue ==
345 vsi->tx_rings[i]->queue_index) {
346 tx_ring = vsi->tx_rings[i];
347 break;
348 }
349 }
350 }
351 }
352
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000353 if (time_after(jiffies, (pf->tx_timeout_last_recovery + HZ*20)))
Kiran Patilb03a8c12015-09-24 18:13:15 -0400354 pf->tx_timeout_recovery_level = 1; /* reset after some time */
355 else if (time_before(jiffies,
356 (pf->tx_timeout_last_recovery + netdev->watchdog_timeo)))
357 return; /* don't do any new action before the next timeout */
358
359 if (tx_ring) {
360 head = i40e_get_head(tx_ring);
361 /* Read interrupt register */
362 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
363 val = rd32(&pf->hw,
364 I40E_PFINT_DYN_CTLN(tx_ring->q_vector->v_idx +
365 tx_ring->vsi->base_vector - 1));
366 else
367 val = rd32(&pf->hw, I40E_PFINT_DYN_CTL0);
368
369 netdev_info(netdev, "tx_timeout: VSI_seid: %d, Q %d, NTC: 0x%x, HWB: 0x%x, NTU: 0x%x, TAIL: 0x%x, INT: 0x%x\n",
370 vsi->seid, hung_queue, tx_ring->next_to_clean,
371 head, tx_ring->next_to_use,
372 readl(tx_ring->tail), val);
373 }
374
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000375 pf->tx_timeout_last_recovery = jiffies;
Kiran Patilb03a8c12015-09-24 18:13:15 -0400376 netdev_info(netdev, "tx_timeout recovery level %d, hung_queue %d\n",
377 pf->tx_timeout_recovery_level, hung_queue);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000378
379 switch (pf->tx_timeout_recovery_level) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000380 case 1:
Jacob Keller0da36b92017-04-19 09:25:55 -0400381 set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000382 break;
383 case 2:
Jacob Keller0da36b92017-04-19 09:25:55 -0400384 set_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000385 break;
386 case 3:
Jacob Keller0da36b92017-04-19 09:25:55 -0400387 set_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000388 break;
389 default:
390 netdev_err(netdev, "tx_timeout recovery unsuccessful\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000391 break;
392 }
Kiran Patilb03a8c12015-09-24 18:13:15 -0400393
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000394 i40e_service_event_schedule(pf);
395 pf->tx_timeout_recovery_level++;
396}
397
398/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000399 * i40e_get_vsi_stats_struct - Get System Network Statistics
400 * @vsi: the VSI we care about
401 *
402 * Returns the address of the device statistics structure.
403 * The statistics are actually updated from the service task.
404 **/
405struct rtnl_link_stats64 *i40e_get_vsi_stats_struct(struct i40e_vsi *vsi)
406{
407 return &vsi->net_stats;
408}
409
410/**
Björn Töpel74608d12017-05-24 07:55:35 +0200411 * i40e_get_netdev_stats_struct_tx - populate stats from a Tx ring
412 * @ring: Tx ring to get statistics from
413 * @stats: statistics entry to be updated
414 **/
415static void i40e_get_netdev_stats_struct_tx(struct i40e_ring *ring,
416 struct rtnl_link_stats64 *stats)
417{
418 u64 bytes, packets;
419 unsigned int start;
420
421 do {
422 start = u64_stats_fetch_begin_irq(&ring->syncp);
423 packets = ring->stats.packets;
424 bytes = ring->stats.bytes;
425 } while (u64_stats_fetch_retry_irq(&ring->syncp, start));
426
427 stats->tx_packets += packets;
428 stats->tx_bytes += bytes;
429}
430
431/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000432 * i40e_get_netdev_stats_struct - Get statistics for netdev interface
433 * @netdev: network interface device structure
434 *
435 * Returns the address of the device statistics structure.
436 * The statistics are actually updated from the service task.
437 **/
Alexander Duyck9eed69a2017-02-21 15:55:47 -0800438static void i40e_get_netdev_stats_struct(struct net_device *netdev,
stephen hemmingerbc1f4472017-01-06 19:12:52 -0800439 struct rtnl_link_stats64 *stats)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000440{
441 struct i40e_netdev_priv *np = netdev_priv(netdev);
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +0000442 struct i40e_ring *tx_ring, *rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000443 struct i40e_vsi *vsi = np->vsi;
Alexander Duyck980e9b12013-09-28 06:01:03 +0000444 struct rtnl_link_stats64 *vsi_stats = i40e_get_vsi_stats_struct(vsi);
445 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000446
Jacob Keller0da36b92017-04-19 09:25:55 -0400447 if (test_bit(__I40E_VSI_DOWN, vsi->state))
stephen hemmingerbc1f4472017-01-06 19:12:52 -0800448 return;
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +0000449
Jesse Brandeburg3c325ce2013-12-14 03:26:45 -0800450 if (!vsi->tx_rings)
stephen hemmingerbc1f4472017-01-06 19:12:52 -0800451 return;
Jesse Brandeburg3c325ce2013-12-14 03:26:45 -0800452
Alexander Duyck980e9b12013-09-28 06:01:03 +0000453 rcu_read_lock();
454 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck980e9b12013-09-28 06:01:03 +0000455 u64 bytes, packets;
456 unsigned int start;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000457
Alexander Duyck980e9b12013-09-28 06:01:03 +0000458 tx_ring = ACCESS_ONCE(vsi->tx_rings[i]);
459 if (!tx_ring)
460 continue;
Björn Töpel74608d12017-05-24 07:55:35 +0200461 i40e_get_netdev_stats_struct_tx(tx_ring, stats);
Alexander Duyck980e9b12013-09-28 06:01:03 +0000462
Alexander Duyck980e9b12013-09-28 06:01:03 +0000463 rx_ring = &tx_ring[1];
464
465 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700466 start = u64_stats_fetch_begin_irq(&rx_ring->syncp);
Alexander Duyck980e9b12013-09-28 06:01:03 +0000467 packets = rx_ring->stats.packets;
468 bytes = rx_ring->stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700469 } while (u64_stats_fetch_retry_irq(&rx_ring->syncp, start));
Alexander Duyck980e9b12013-09-28 06:01:03 +0000470
471 stats->rx_packets += packets;
472 stats->rx_bytes += bytes;
Björn Töpel74608d12017-05-24 07:55:35 +0200473
474 if (i40e_enabled_xdp_vsi(vsi))
475 i40e_get_netdev_stats_struct_tx(&rx_ring[1], stats);
Alexander Duyck980e9b12013-09-28 06:01:03 +0000476 }
477 rcu_read_unlock();
478
Akeem G Abodunrina5282f42014-05-10 04:49:03 +0000479 /* following stats updated by i40e_watchdog_subtask() */
Alexander Duyck980e9b12013-09-28 06:01:03 +0000480 stats->multicast = vsi_stats->multicast;
481 stats->tx_errors = vsi_stats->tx_errors;
482 stats->tx_dropped = vsi_stats->tx_dropped;
483 stats->rx_errors = vsi_stats->rx_errors;
Jesse Brandeburgd8201e22015-07-23 16:54:35 -0400484 stats->rx_dropped = vsi_stats->rx_dropped;
Alexander Duyck980e9b12013-09-28 06:01:03 +0000485 stats->rx_crc_errors = vsi_stats->rx_crc_errors;
486 stats->rx_length_errors = vsi_stats->rx_length_errors;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000487}
488
489/**
490 * i40e_vsi_reset_stats - Resets all stats of the given vsi
491 * @vsi: the VSI to have its stats reset
492 **/
493void i40e_vsi_reset_stats(struct i40e_vsi *vsi)
494{
495 struct rtnl_link_stats64 *ns;
496 int i;
497
498 if (!vsi)
499 return;
500
501 ns = i40e_get_vsi_stats_struct(vsi);
502 memset(ns, 0, sizeof(*ns));
503 memset(&vsi->net_stats_offsets, 0, sizeof(vsi->net_stats_offsets));
504 memset(&vsi->eth_stats, 0, sizeof(vsi->eth_stats));
505 memset(&vsi->eth_stats_offsets, 0, sizeof(vsi->eth_stats_offsets));
Greg Rose8e9dca52013-12-18 13:45:53 +0000506 if (vsi->rx_rings && vsi->rx_rings[0]) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000507 for (i = 0; i < vsi->num_queue_pairs; i++) {
Jesse Brandeburg6995b362015-08-28 17:55:54 -0400508 memset(&vsi->rx_rings[i]->stats, 0,
Alexander Duyck9f65e152013-09-28 06:00:58 +0000509 sizeof(vsi->rx_rings[i]->stats));
Jesse Brandeburg6995b362015-08-28 17:55:54 -0400510 memset(&vsi->rx_rings[i]->rx_stats, 0,
Alexander Duyck9f65e152013-09-28 06:00:58 +0000511 sizeof(vsi->rx_rings[i]->rx_stats));
Jesse Brandeburg6995b362015-08-28 17:55:54 -0400512 memset(&vsi->tx_rings[i]->stats, 0,
Alexander Duyck9f65e152013-09-28 06:00:58 +0000513 sizeof(vsi->tx_rings[i]->stats));
514 memset(&vsi->tx_rings[i]->tx_stats, 0,
515 sizeof(vsi->tx_rings[i]->tx_stats));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000516 }
Greg Rose8e9dca52013-12-18 13:45:53 +0000517 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000518 vsi->stat_offsets_loaded = false;
519}
520
521/**
Jeff Kirsherb40c82e62015-02-27 09:18:34 +0000522 * i40e_pf_reset_stats - Reset all of the stats for the given PF
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000523 * @pf: the PF to be reset
524 **/
525void i40e_pf_reset_stats(struct i40e_pf *pf)
526{
Shannon Nelsone91fdf72014-06-03 23:50:18 +0000527 int i;
528
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000529 memset(&pf->stats, 0, sizeof(pf->stats));
530 memset(&pf->stats_offsets, 0, sizeof(pf->stats_offsets));
531 pf->stat_offsets_loaded = false;
Shannon Nelsone91fdf72014-06-03 23:50:18 +0000532
533 for (i = 0; i < I40E_MAX_VEB; i++) {
534 if (pf->veb[i]) {
535 memset(&pf->veb[i]->stats, 0,
536 sizeof(pf->veb[i]->stats));
537 memset(&pf->veb[i]->stats_offsets, 0,
538 sizeof(pf->veb[i]->stats_offsets));
539 pf->veb[i]->stat_offsets_loaded = false;
540 }
541 }
Catherine Sullivan42bce042016-07-27 12:02:32 -0700542 pf->hw_csum_rx_error = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000543}
544
545/**
546 * i40e_stat_update48 - read and update a 48 bit stat from the chip
547 * @hw: ptr to the hardware info
548 * @hireg: the high 32 bit reg to read
549 * @loreg: the low 32 bit reg to read
550 * @offset_loaded: has the initial offset been loaded yet
551 * @offset: ptr to current offset value
552 * @stat: ptr to the stat
553 *
554 * Since the device stats are not reset at PFReset, they likely will not
555 * be zeroed when the driver starts. We'll save the first values read
556 * and use them as offsets to be subtracted from the raw values in order
557 * to report stats that count from zero. In the process, we also manage
558 * the potential roll-over.
559 **/
560static void i40e_stat_update48(struct i40e_hw *hw, u32 hireg, u32 loreg,
561 bool offset_loaded, u64 *offset, u64 *stat)
562{
563 u64 new_data;
564
Shannon Nelsonab600852014-01-17 15:36:39 -0800565 if (hw->device_id == I40E_DEV_ID_QEMU) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000566 new_data = rd32(hw, loreg);
567 new_data |= ((u64)(rd32(hw, hireg) & 0xFFFF)) << 32;
568 } else {
569 new_data = rd64(hw, loreg);
570 }
571 if (!offset_loaded)
572 *offset = new_data;
573 if (likely(new_data >= *offset))
574 *stat = new_data - *offset;
575 else
Jesse Brandeburg41a1d042015-06-04 16:24:02 -0400576 *stat = (new_data + BIT_ULL(48)) - *offset;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000577 *stat &= 0xFFFFFFFFFFFFULL;
578}
579
580/**
581 * i40e_stat_update32 - read and update a 32 bit stat from the chip
582 * @hw: ptr to the hardware info
583 * @reg: the hw reg to read
584 * @offset_loaded: has the initial offset been loaded yet
585 * @offset: ptr to current offset value
586 * @stat: ptr to the stat
587 **/
588static void i40e_stat_update32(struct i40e_hw *hw, u32 reg,
589 bool offset_loaded, u64 *offset, u64 *stat)
590{
591 u32 new_data;
592
593 new_data = rd32(hw, reg);
594 if (!offset_loaded)
595 *offset = new_data;
596 if (likely(new_data >= *offset))
597 *stat = (u32)(new_data - *offset);
598 else
Jesse Brandeburg41a1d042015-06-04 16:24:02 -0400599 *stat = (u32)((new_data + BIT_ULL(32)) - *offset);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000600}
601
602/**
603 * i40e_update_eth_stats - Update VSI-specific ethernet statistics counters.
604 * @vsi: the VSI to be updated
605 **/
606void i40e_update_eth_stats(struct i40e_vsi *vsi)
607{
608 int stat_idx = le16_to_cpu(vsi->info.stat_counter_idx);
609 struct i40e_pf *pf = vsi->back;
610 struct i40e_hw *hw = &pf->hw;
611 struct i40e_eth_stats *oes;
612 struct i40e_eth_stats *es; /* device's eth stats */
613
614 es = &vsi->eth_stats;
615 oes = &vsi->eth_stats_offsets;
616
617 /* Gather up the stats that the hw collects */
618 i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
619 vsi->stat_offsets_loaded,
620 &oes->tx_errors, &es->tx_errors);
621 i40e_stat_update32(hw, I40E_GLV_RDPC(stat_idx),
622 vsi->stat_offsets_loaded,
623 &oes->rx_discards, &es->rx_discards);
Shannon Nelson41a9e552014-04-23 04:50:20 +0000624 i40e_stat_update32(hw, I40E_GLV_RUPP(stat_idx),
625 vsi->stat_offsets_loaded,
626 &oes->rx_unknown_protocol, &es->rx_unknown_protocol);
627 i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
628 vsi->stat_offsets_loaded,
629 &oes->tx_errors, &es->tx_errors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000630
631 i40e_stat_update48(hw, I40E_GLV_GORCH(stat_idx),
632 I40E_GLV_GORCL(stat_idx),
633 vsi->stat_offsets_loaded,
634 &oes->rx_bytes, &es->rx_bytes);
635 i40e_stat_update48(hw, I40E_GLV_UPRCH(stat_idx),
636 I40E_GLV_UPRCL(stat_idx),
637 vsi->stat_offsets_loaded,
638 &oes->rx_unicast, &es->rx_unicast);
639 i40e_stat_update48(hw, I40E_GLV_MPRCH(stat_idx),
640 I40E_GLV_MPRCL(stat_idx),
641 vsi->stat_offsets_loaded,
642 &oes->rx_multicast, &es->rx_multicast);
643 i40e_stat_update48(hw, I40E_GLV_BPRCH(stat_idx),
644 I40E_GLV_BPRCL(stat_idx),
645 vsi->stat_offsets_loaded,
646 &oes->rx_broadcast, &es->rx_broadcast);
647
648 i40e_stat_update48(hw, I40E_GLV_GOTCH(stat_idx),
649 I40E_GLV_GOTCL(stat_idx),
650 vsi->stat_offsets_loaded,
651 &oes->tx_bytes, &es->tx_bytes);
652 i40e_stat_update48(hw, I40E_GLV_UPTCH(stat_idx),
653 I40E_GLV_UPTCL(stat_idx),
654 vsi->stat_offsets_loaded,
655 &oes->tx_unicast, &es->tx_unicast);
656 i40e_stat_update48(hw, I40E_GLV_MPTCH(stat_idx),
657 I40E_GLV_MPTCL(stat_idx),
658 vsi->stat_offsets_loaded,
659 &oes->tx_multicast, &es->tx_multicast);
660 i40e_stat_update48(hw, I40E_GLV_BPTCH(stat_idx),
661 I40E_GLV_BPTCL(stat_idx),
662 vsi->stat_offsets_loaded,
663 &oes->tx_broadcast, &es->tx_broadcast);
664 vsi->stat_offsets_loaded = true;
665}
666
667/**
668 * i40e_update_veb_stats - Update Switch component statistics
669 * @veb: the VEB being updated
670 **/
671static void i40e_update_veb_stats(struct i40e_veb *veb)
672{
673 struct i40e_pf *pf = veb->pf;
674 struct i40e_hw *hw = &pf->hw;
675 struct i40e_eth_stats *oes;
676 struct i40e_eth_stats *es; /* device's eth stats */
Neerav Parikhfe860af2015-07-10 19:36:02 -0400677 struct i40e_veb_tc_stats *veb_oes;
678 struct i40e_veb_tc_stats *veb_es;
679 int i, idx = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000680
681 idx = veb->stats_idx;
682 es = &veb->stats;
683 oes = &veb->stats_offsets;
Neerav Parikhfe860af2015-07-10 19:36:02 -0400684 veb_es = &veb->tc_stats;
685 veb_oes = &veb->tc_stats_offsets;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000686
687 /* Gather up the stats that the hw collects */
688 i40e_stat_update32(hw, I40E_GLSW_TDPC(idx),
689 veb->stat_offsets_loaded,
690 &oes->tx_discards, &es->tx_discards);
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +0000691 if (hw->revision_id > 0)
692 i40e_stat_update32(hw, I40E_GLSW_RUPP(idx),
693 veb->stat_offsets_loaded,
694 &oes->rx_unknown_protocol,
695 &es->rx_unknown_protocol);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000696 i40e_stat_update48(hw, I40E_GLSW_GORCH(idx), I40E_GLSW_GORCL(idx),
697 veb->stat_offsets_loaded,
698 &oes->rx_bytes, &es->rx_bytes);
699 i40e_stat_update48(hw, I40E_GLSW_UPRCH(idx), I40E_GLSW_UPRCL(idx),
700 veb->stat_offsets_loaded,
701 &oes->rx_unicast, &es->rx_unicast);
702 i40e_stat_update48(hw, I40E_GLSW_MPRCH(idx), I40E_GLSW_MPRCL(idx),
703 veb->stat_offsets_loaded,
704 &oes->rx_multicast, &es->rx_multicast);
705 i40e_stat_update48(hw, I40E_GLSW_BPRCH(idx), I40E_GLSW_BPRCL(idx),
706 veb->stat_offsets_loaded,
707 &oes->rx_broadcast, &es->rx_broadcast);
708
709 i40e_stat_update48(hw, I40E_GLSW_GOTCH(idx), I40E_GLSW_GOTCL(idx),
710 veb->stat_offsets_loaded,
711 &oes->tx_bytes, &es->tx_bytes);
712 i40e_stat_update48(hw, I40E_GLSW_UPTCH(idx), I40E_GLSW_UPTCL(idx),
713 veb->stat_offsets_loaded,
714 &oes->tx_unicast, &es->tx_unicast);
715 i40e_stat_update48(hw, I40E_GLSW_MPTCH(idx), I40E_GLSW_MPTCL(idx),
716 veb->stat_offsets_loaded,
717 &oes->tx_multicast, &es->tx_multicast);
718 i40e_stat_update48(hw, I40E_GLSW_BPTCH(idx), I40E_GLSW_BPTCL(idx),
719 veb->stat_offsets_loaded,
720 &oes->tx_broadcast, &es->tx_broadcast);
Neerav Parikhfe860af2015-07-10 19:36:02 -0400721 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
722 i40e_stat_update48(hw, I40E_GLVEBTC_RPCH(i, idx),
723 I40E_GLVEBTC_RPCL(i, idx),
724 veb->stat_offsets_loaded,
725 &veb_oes->tc_rx_packets[i],
726 &veb_es->tc_rx_packets[i]);
727 i40e_stat_update48(hw, I40E_GLVEBTC_RBCH(i, idx),
728 I40E_GLVEBTC_RBCL(i, idx),
729 veb->stat_offsets_loaded,
730 &veb_oes->tc_rx_bytes[i],
731 &veb_es->tc_rx_bytes[i]);
732 i40e_stat_update48(hw, I40E_GLVEBTC_TPCH(i, idx),
733 I40E_GLVEBTC_TPCL(i, idx),
734 veb->stat_offsets_loaded,
735 &veb_oes->tc_tx_packets[i],
736 &veb_es->tc_tx_packets[i]);
737 i40e_stat_update48(hw, I40E_GLVEBTC_TBCH(i, idx),
738 I40E_GLVEBTC_TBCL(i, idx),
739 veb->stat_offsets_loaded,
740 &veb_oes->tc_tx_bytes[i],
741 &veb_es->tc_tx_bytes[i]);
742 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000743 veb->stat_offsets_loaded = true;
744}
745
746/**
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000747 * i40e_update_vsi_stats - Update the vsi statistics counters.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000748 * @vsi: the VSI to be updated
749 *
750 * There are a few instances where we store the same stat in a
751 * couple of different structs. This is partly because we have
752 * the netdev stats that need to be filled out, which is slightly
753 * different from the "eth_stats" defined by the chip and used in
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000754 * VF communications. We sort it out here.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000755 **/
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000756static void i40e_update_vsi_stats(struct i40e_vsi *vsi)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000757{
758 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000759 struct rtnl_link_stats64 *ons;
760 struct rtnl_link_stats64 *ns; /* netdev stats */
761 struct i40e_eth_stats *oes;
762 struct i40e_eth_stats *es; /* device's eth stats */
763 u32 tx_restart, tx_busy;
Akeem G Abodunrinbf00b372014-10-17 03:14:39 +0000764 struct i40e_ring *p;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000765 u32 rx_page, rx_buf;
Akeem G Abodunrinbf00b372014-10-17 03:14:39 +0000766 u64 bytes, packets;
767 unsigned int start;
Anjali Singhai Jain2fc3d712015-08-27 11:42:29 -0400768 u64 tx_linearize;
Anjali Singhai Jain164c9f52015-10-21 19:47:08 -0400769 u64 tx_force_wb;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000770 u64 rx_p, rx_b;
771 u64 tx_p, tx_b;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000772 u16 q;
773
Jacob Keller0da36b92017-04-19 09:25:55 -0400774 if (test_bit(__I40E_VSI_DOWN, vsi->state) ||
775 test_bit(__I40E_CONFIG_BUSY, pf->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000776 return;
777
778 ns = i40e_get_vsi_stats_struct(vsi);
779 ons = &vsi->net_stats_offsets;
780 es = &vsi->eth_stats;
781 oes = &vsi->eth_stats_offsets;
782
783 /* Gather up the netdev and vsi stats that the driver collects
784 * on the fly during packet processing
785 */
786 rx_b = rx_p = 0;
787 tx_b = tx_p = 0;
Anjali Singhai Jain164c9f52015-10-21 19:47:08 -0400788 tx_restart = tx_busy = tx_linearize = tx_force_wb = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000789 rx_page = 0;
790 rx_buf = 0;
Alexander Duyck980e9b12013-09-28 06:01:03 +0000791 rcu_read_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000792 for (q = 0; q < vsi->num_queue_pairs; q++) {
Alexander Duyck980e9b12013-09-28 06:01:03 +0000793 /* locate Tx ring */
794 p = ACCESS_ONCE(vsi->tx_rings[q]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000795
Alexander Duyck980e9b12013-09-28 06:01:03 +0000796 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700797 start = u64_stats_fetch_begin_irq(&p->syncp);
Alexander Duyck980e9b12013-09-28 06:01:03 +0000798 packets = p->stats.packets;
799 bytes = p->stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700800 } while (u64_stats_fetch_retry_irq(&p->syncp, start));
Alexander Duyck980e9b12013-09-28 06:01:03 +0000801 tx_b += bytes;
802 tx_p += packets;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000803 tx_restart += p->tx_stats.restart_queue;
804 tx_busy += p->tx_stats.tx_busy;
Anjali Singhai Jain2fc3d712015-08-27 11:42:29 -0400805 tx_linearize += p->tx_stats.tx_linearize;
Anjali Singhai Jain164c9f52015-10-21 19:47:08 -0400806 tx_force_wb += p->tx_stats.tx_force_wb;
Alexander Duyck980e9b12013-09-28 06:01:03 +0000807
808 /* Rx queue is part of the same block as Tx queue */
809 p = &p[1];
810 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700811 start = u64_stats_fetch_begin_irq(&p->syncp);
Alexander Duyck980e9b12013-09-28 06:01:03 +0000812 packets = p->stats.packets;
813 bytes = p->stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700814 } while (u64_stats_fetch_retry_irq(&p->syncp, start));
Alexander Duyck980e9b12013-09-28 06:01:03 +0000815 rx_b += bytes;
816 rx_p += packets;
Mitch Williams420136c2013-12-18 13:45:59 +0000817 rx_buf += p->rx_stats.alloc_buff_failed;
818 rx_page += p->rx_stats.alloc_page_failed;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000819 }
Alexander Duyck980e9b12013-09-28 06:01:03 +0000820 rcu_read_unlock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000821 vsi->tx_restart = tx_restart;
822 vsi->tx_busy = tx_busy;
Anjali Singhai Jain2fc3d712015-08-27 11:42:29 -0400823 vsi->tx_linearize = tx_linearize;
Anjali Singhai Jain164c9f52015-10-21 19:47:08 -0400824 vsi->tx_force_wb = tx_force_wb;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000825 vsi->rx_page_failed = rx_page;
826 vsi->rx_buf_failed = rx_buf;
827
828 ns->rx_packets = rx_p;
829 ns->rx_bytes = rx_b;
830 ns->tx_packets = tx_p;
831 ns->tx_bytes = tx_b;
832
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000833 /* update netdev stats from eth stats */
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000834 i40e_update_eth_stats(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000835 ons->tx_errors = oes->tx_errors;
836 ns->tx_errors = es->tx_errors;
837 ons->multicast = oes->rx_multicast;
838 ns->multicast = es->rx_multicast;
Shannon Nelson41a9e552014-04-23 04:50:20 +0000839 ons->rx_dropped = oes->rx_discards;
840 ns->rx_dropped = es->rx_discards;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000841 ons->tx_dropped = oes->tx_discards;
842 ns->tx_dropped = es->tx_discards;
843
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000844 /* pull in a couple PF stats if this is the main vsi */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000845 if (vsi == pf->vsi[pf->lan_vsi]) {
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000846 ns->rx_crc_errors = pf->stats.crc_errors;
847 ns->rx_errors = pf->stats.crc_errors + pf->stats.illegal_bytes;
848 ns->rx_length_errors = pf->stats.rx_length_errors;
849 }
850}
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000851
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000852/**
Jeff Kirsherb40c82e62015-02-27 09:18:34 +0000853 * i40e_update_pf_stats - Update the PF statistics counters.
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000854 * @pf: the PF to be updated
855 **/
856static void i40e_update_pf_stats(struct i40e_pf *pf)
857{
858 struct i40e_hw_port_stats *osd = &pf->stats_offsets;
859 struct i40e_hw_port_stats *nsd = &pf->stats;
860 struct i40e_hw *hw = &pf->hw;
861 u32 val;
862 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000863
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000864 i40e_stat_update48(hw, I40E_GLPRT_GORCH(hw->port),
865 I40E_GLPRT_GORCL(hw->port),
866 pf->stat_offsets_loaded,
867 &osd->eth.rx_bytes, &nsd->eth.rx_bytes);
868 i40e_stat_update48(hw, I40E_GLPRT_GOTCH(hw->port),
869 I40E_GLPRT_GOTCL(hw->port),
870 pf->stat_offsets_loaded,
871 &osd->eth.tx_bytes, &nsd->eth.tx_bytes);
872 i40e_stat_update32(hw, I40E_GLPRT_RDPC(hw->port),
873 pf->stat_offsets_loaded,
874 &osd->eth.rx_discards,
875 &nsd->eth.rx_discards);
Shannon Nelson532d2832014-04-23 04:50:09 +0000876 i40e_stat_update48(hw, I40E_GLPRT_UPRCH(hw->port),
877 I40E_GLPRT_UPRCL(hw->port),
878 pf->stat_offsets_loaded,
879 &osd->eth.rx_unicast,
880 &nsd->eth.rx_unicast);
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000881 i40e_stat_update48(hw, I40E_GLPRT_MPRCH(hw->port),
882 I40E_GLPRT_MPRCL(hw->port),
883 pf->stat_offsets_loaded,
884 &osd->eth.rx_multicast,
885 &nsd->eth.rx_multicast);
Shannon Nelson532d2832014-04-23 04:50:09 +0000886 i40e_stat_update48(hw, I40E_GLPRT_BPRCH(hw->port),
887 I40E_GLPRT_BPRCL(hw->port),
888 pf->stat_offsets_loaded,
889 &osd->eth.rx_broadcast,
890 &nsd->eth.rx_broadcast);
891 i40e_stat_update48(hw, I40E_GLPRT_UPTCH(hw->port),
892 I40E_GLPRT_UPTCL(hw->port),
893 pf->stat_offsets_loaded,
894 &osd->eth.tx_unicast,
895 &nsd->eth.tx_unicast);
896 i40e_stat_update48(hw, I40E_GLPRT_MPTCH(hw->port),
897 I40E_GLPRT_MPTCL(hw->port),
898 pf->stat_offsets_loaded,
899 &osd->eth.tx_multicast,
900 &nsd->eth.tx_multicast);
901 i40e_stat_update48(hw, I40E_GLPRT_BPTCH(hw->port),
902 I40E_GLPRT_BPTCL(hw->port),
903 pf->stat_offsets_loaded,
904 &osd->eth.tx_broadcast,
905 &nsd->eth.tx_broadcast);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000906
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000907 i40e_stat_update32(hw, I40E_GLPRT_TDOLD(hw->port),
908 pf->stat_offsets_loaded,
909 &osd->tx_dropped_link_down,
910 &nsd->tx_dropped_link_down);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000911
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000912 i40e_stat_update32(hw, I40E_GLPRT_CRCERRS(hw->port),
913 pf->stat_offsets_loaded,
914 &osd->crc_errors, &nsd->crc_errors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000915
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000916 i40e_stat_update32(hw, I40E_GLPRT_ILLERRC(hw->port),
917 pf->stat_offsets_loaded,
918 &osd->illegal_bytes, &nsd->illegal_bytes);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000919
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000920 i40e_stat_update32(hw, I40E_GLPRT_MLFC(hw->port),
921 pf->stat_offsets_loaded,
922 &osd->mac_local_faults,
923 &nsd->mac_local_faults);
924 i40e_stat_update32(hw, I40E_GLPRT_MRFC(hw->port),
925 pf->stat_offsets_loaded,
926 &osd->mac_remote_faults,
927 &nsd->mac_remote_faults);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000928
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000929 i40e_stat_update32(hw, I40E_GLPRT_RLEC(hw->port),
930 pf->stat_offsets_loaded,
931 &osd->rx_length_errors,
932 &nsd->rx_length_errors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000933
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000934 i40e_stat_update32(hw, I40E_GLPRT_LXONRXC(hw->port),
935 pf->stat_offsets_loaded,
936 &osd->link_xon_rx, &nsd->link_xon_rx);
937 i40e_stat_update32(hw, I40E_GLPRT_LXONTXC(hw->port),
938 pf->stat_offsets_loaded,
939 &osd->link_xon_tx, &nsd->link_xon_tx);
Neerav Parikh95db2392015-11-06 15:26:09 -0800940 i40e_stat_update32(hw, I40E_GLPRT_LXOFFRXC(hw->port),
941 pf->stat_offsets_loaded,
942 &osd->link_xoff_rx, &nsd->link_xoff_rx);
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000943 i40e_stat_update32(hw, I40E_GLPRT_LXOFFTXC(hw->port),
944 pf->stat_offsets_loaded,
945 &osd->link_xoff_tx, &nsd->link_xoff_tx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000946
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000947 for (i = 0; i < 8; i++) {
Neerav Parikh95db2392015-11-06 15:26:09 -0800948 i40e_stat_update32(hw, I40E_GLPRT_PXOFFRXC(hw->port, i),
949 pf->stat_offsets_loaded,
950 &osd->priority_xoff_rx[i],
951 &nsd->priority_xoff_rx[i]);
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000952 i40e_stat_update32(hw, I40E_GLPRT_PXONRXC(hw->port, i),
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000953 pf->stat_offsets_loaded,
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000954 &osd->priority_xon_rx[i],
955 &nsd->priority_xon_rx[i]);
956 i40e_stat_update32(hw, I40E_GLPRT_PXONTXC(hw->port, i),
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000957 pf->stat_offsets_loaded,
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000958 &osd->priority_xon_tx[i],
959 &nsd->priority_xon_tx[i]);
960 i40e_stat_update32(hw, I40E_GLPRT_PXOFFTXC(hw->port, i),
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000961 pf->stat_offsets_loaded,
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000962 &osd->priority_xoff_tx[i],
963 &nsd->priority_xoff_tx[i]);
964 i40e_stat_update32(hw,
965 I40E_GLPRT_RXON2OFFCNT(hw->port, i),
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000966 pf->stat_offsets_loaded,
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000967 &osd->priority_xon_2_xoff[i],
968 &nsd->priority_xon_2_xoff[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000969 }
970
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000971 i40e_stat_update48(hw, I40E_GLPRT_PRC64H(hw->port),
972 I40E_GLPRT_PRC64L(hw->port),
973 pf->stat_offsets_loaded,
974 &osd->rx_size_64, &nsd->rx_size_64);
975 i40e_stat_update48(hw, I40E_GLPRT_PRC127H(hw->port),
976 I40E_GLPRT_PRC127L(hw->port),
977 pf->stat_offsets_loaded,
978 &osd->rx_size_127, &nsd->rx_size_127);
979 i40e_stat_update48(hw, I40E_GLPRT_PRC255H(hw->port),
980 I40E_GLPRT_PRC255L(hw->port),
981 pf->stat_offsets_loaded,
982 &osd->rx_size_255, &nsd->rx_size_255);
983 i40e_stat_update48(hw, I40E_GLPRT_PRC511H(hw->port),
984 I40E_GLPRT_PRC511L(hw->port),
985 pf->stat_offsets_loaded,
986 &osd->rx_size_511, &nsd->rx_size_511);
987 i40e_stat_update48(hw, I40E_GLPRT_PRC1023H(hw->port),
988 I40E_GLPRT_PRC1023L(hw->port),
989 pf->stat_offsets_loaded,
990 &osd->rx_size_1023, &nsd->rx_size_1023);
991 i40e_stat_update48(hw, I40E_GLPRT_PRC1522H(hw->port),
992 I40E_GLPRT_PRC1522L(hw->port),
993 pf->stat_offsets_loaded,
994 &osd->rx_size_1522, &nsd->rx_size_1522);
995 i40e_stat_update48(hw, I40E_GLPRT_PRC9522H(hw->port),
996 I40E_GLPRT_PRC9522L(hw->port),
997 pf->stat_offsets_loaded,
998 &osd->rx_size_big, &nsd->rx_size_big);
999
1000 i40e_stat_update48(hw, I40E_GLPRT_PTC64H(hw->port),
1001 I40E_GLPRT_PTC64L(hw->port),
1002 pf->stat_offsets_loaded,
1003 &osd->tx_size_64, &nsd->tx_size_64);
1004 i40e_stat_update48(hw, I40E_GLPRT_PTC127H(hw->port),
1005 I40E_GLPRT_PTC127L(hw->port),
1006 pf->stat_offsets_loaded,
1007 &osd->tx_size_127, &nsd->tx_size_127);
1008 i40e_stat_update48(hw, I40E_GLPRT_PTC255H(hw->port),
1009 I40E_GLPRT_PTC255L(hw->port),
1010 pf->stat_offsets_loaded,
1011 &osd->tx_size_255, &nsd->tx_size_255);
1012 i40e_stat_update48(hw, I40E_GLPRT_PTC511H(hw->port),
1013 I40E_GLPRT_PTC511L(hw->port),
1014 pf->stat_offsets_loaded,
1015 &osd->tx_size_511, &nsd->tx_size_511);
1016 i40e_stat_update48(hw, I40E_GLPRT_PTC1023H(hw->port),
1017 I40E_GLPRT_PTC1023L(hw->port),
1018 pf->stat_offsets_loaded,
1019 &osd->tx_size_1023, &nsd->tx_size_1023);
1020 i40e_stat_update48(hw, I40E_GLPRT_PTC1522H(hw->port),
1021 I40E_GLPRT_PTC1522L(hw->port),
1022 pf->stat_offsets_loaded,
1023 &osd->tx_size_1522, &nsd->tx_size_1522);
1024 i40e_stat_update48(hw, I40E_GLPRT_PTC9522H(hw->port),
1025 I40E_GLPRT_PTC9522L(hw->port),
1026 pf->stat_offsets_loaded,
1027 &osd->tx_size_big, &nsd->tx_size_big);
1028
1029 i40e_stat_update32(hw, I40E_GLPRT_RUC(hw->port),
1030 pf->stat_offsets_loaded,
1031 &osd->rx_undersize, &nsd->rx_undersize);
1032 i40e_stat_update32(hw, I40E_GLPRT_RFC(hw->port),
1033 pf->stat_offsets_loaded,
1034 &osd->rx_fragments, &nsd->rx_fragments);
1035 i40e_stat_update32(hw, I40E_GLPRT_ROC(hw->port),
1036 pf->stat_offsets_loaded,
1037 &osd->rx_oversize, &nsd->rx_oversize);
1038 i40e_stat_update32(hw, I40E_GLPRT_RJC(hw->port),
1039 pf->stat_offsets_loaded,
1040 &osd->rx_jabber, &nsd->rx_jabber);
1041
Anjali Singhai Jain433c47d2014-05-22 06:32:17 +00001042 /* FDIR stats */
Anjali Singhai Jain0bf4b1b2015-04-16 20:06:02 -04001043 i40e_stat_update32(hw,
1044 I40E_GLQF_PCNT(I40E_FD_ATR_STAT_IDX(pf->hw.pf_id)),
Anjali Singhai Jain433c47d2014-05-22 06:32:17 +00001045 pf->stat_offsets_loaded,
1046 &osd->fd_atr_match, &nsd->fd_atr_match);
Anjali Singhai Jain0bf4b1b2015-04-16 20:06:02 -04001047 i40e_stat_update32(hw,
1048 I40E_GLQF_PCNT(I40E_FD_SB_STAT_IDX(pf->hw.pf_id)),
Anjali Singhai Jain433c47d2014-05-22 06:32:17 +00001049 pf->stat_offsets_loaded,
1050 &osd->fd_sb_match, &nsd->fd_sb_match);
Anjali Singhai Jain60ccd452015-04-16 20:06:01 -04001051 i40e_stat_update32(hw,
1052 I40E_GLQF_PCNT(I40E_FD_ATR_TUNNEL_STAT_IDX(pf->hw.pf_id)),
1053 pf->stat_offsets_loaded,
1054 &osd->fd_atr_tunnel_match, &nsd->fd_atr_tunnel_match);
Anjali Singhai Jain433c47d2014-05-22 06:32:17 +00001055
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001056 val = rd32(hw, I40E_PRTPM_EEE_STAT);
1057 nsd->tx_lpi_status =
1058 (val & I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_MASK) >>
1059 I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_SHIFT;
1060 nsd->rx_lpi_status =
1061 (val & I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_MASK) >>
1062 I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_SHIFT;
1063 i40e_stat_update32(hw, I40E_PRTPM_TLPIC,
1064 pf->stat_offsets_loaded,
1065 &osd->tx_lpi_count, &nsd->tx_lpi_count);
1066 i40e_stat_update32(hw, I40E_PRTPM_RLPIC,
1067 pf->stat_offsets_loaded,
1068 &osd->rx_lpi_count, &nsd->rx_lpi_count);
1069
Anjali Singhai Jaind0389e52015-04-22 19:34:05 -04001070 if (pf->flags & I40E_FLAG_FD_SB_ENABLED &&
Jacob Keller47994c12017-04-19 09:25:57 -04001071 !(pf->flags & I40E_FLAG_FD_SB_AUTO_DISABLED))
Anjali Singhai Jaind0389e52015-04-22 19:34:05 -04001072 nsd->fd_sb_status = true;
1073 else
1074 nsd->fd_sb_status = false;
1075
1076 if (pf->flags & I40E_FLAG_FD_ATR_ENABLED &&
Jacob Keller47994c12017-04-19 09:25:57 -04001077 !(pf->flags & I40E_FLAG_FD_ATR_AUTO_DISABLED))
Anjali Singhai Jaind0389e52015-04-22 19:34:05 -04001078 nsd->fd_atr_status = true;
1079 else
1080 nsd->fd_atr_status = false;
1081
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001082 pf->stat_offsets_loaded = true;
1083}
1084
1085/**
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001086 * i40e_update_stats - Update the various statistics counters.
1087 * @vsi: the VSI to be updated
1088 *
1089 * Update the various stats for this VSI and its related entities.
1090 **/
1091void i40e_update_stats(struct i40e_vsi *vsi)
1092{
1093 struct i40e_pf *pf = vsi->back;
1094
1095 if (vsi == pf->vsi[pf->lan_vsi])
1096 i40e_update_pf_stats(pf);
1097
1098 i40e_update_vsi_stats(vsi);
1099}
1100
1101/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001102 * i40e_find_filter - Search VSI filter list for specific mac/vlan filter
1103 * @vsi: the VSI to be searched
1104 * @macaddr: the MAC address
1105 * @vlan: the vlan
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001106 *
1107 * Returns ptr to the filter object or NULL
1108 **/
1109static struct i40e_mac_filter *i40e_find_filter(struct i40e_vsi *vsi,
Jacob Keller6622f5c2016-10-05 09:30:32 -07001110 const u8 *macaddr, s16 vlan)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001111{
1112 struct i40e_mac_filter *f;
Jacob Keller278e7d02016-10-05 09:30:37 -07001113 u64 key;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001114
1115 if (!vsi || !macaddr)
1116 return NULL;
1117
Jacob Keller278e7d02016-10-05 09:30:37 -07001118 key = i40e_addr_to_hkey(macaddr);
1119 hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001120 if ((ether_addr_equal(macaddr, f->macaddr)) &&
Jacob Keller1bc87e82016-10-05 09:30:31 -07001121 (vlan == f->vlan))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001122 return f;
1123 }
1124 return NULL;
1125}
1126
1127/**
1128 * i40e_find_mac - Find a mac addr in the macvlan filters list
1129 * @vsi: the VSI to be searched
1130 * @macaddr: the MAC address we are searching for
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001131 *
1132 * Returns the first filter with the provided MAC address or NULL if
1133 * MAC address was not found
1134 **/
Jacob Keller6622f5c2016-10-05 09:30:32 -07001135struct i40e_mac_filter *i40e_find_mac(struct i40e_vsi *vsi, const u8 *macaddr)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001136{
1137 struct i40e_mac_filter *f;
Jacob Keller278e7d02016-10-05 09:30:37 -07001138 u64 key;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001139
1140 if (!vsi || !macaddr)
1141 return NULL;
1142
Jacob Keller278e7d02016-10-05 09:30:37 -07001143 key = i40e_addr_to_hkey(macaddr);
1144 hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
Jacob Keller1bc87e82016-10-05 09:30:31 -07001145 if ((ether_addr_equal(macaddr, f->macaddr)))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001146 return f;
1147 }
1148 return NULL;
1149}
1150
1151/**
1152 * i40e_is_vsi_in_vlan - Check if VSI is in vlan mode
1153 * @vsi: the VSI to be searched
1154 *
1155 * Returns true if VSI is in vlan mode or false otherwise
1156 **/
1157bool i40e_is_vsi_in_vlan(struct i40e_vsi *vsi)
1158{
Jacob Kellercbebb852016-10-05 09:30:40 -07001159 /* If we have a PVID, always operate in VLAN mode */
1160 if (vsi->info.pvid)
1161 return true;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001162
Jacob Kellercbebb852016-10-05 09:30:40 -07001163 /* We need to operate in VLAN mode whenever we have any filters with
1164 * a VLAN other than I40E_VLAN_ALL. We could check the table each
1165 * time, incurring search cost repeatedly. However, we can notice two
1166 * things:
1167 *
1168 * 1) the only place where we can gain a VLAN filter is in
1169 * i40e_add_filter.
1170 *
1171 * 2) the only place where filters are actually removed is in
Jacob Keller0b7c8b52016-10-25 16:08:52 -07001172 * i40e_sync_filters_subtask.
Jacob Kellercbebb852016-10-05 09:30:40 -07001173 *
1174 * Thus, we can simply use a boolean value, has_vlan_filters which we
1175 * will set to true when we add a VLAN filter in i40e_add_filter. Then
1176 * we have to perform the full search after deleting filters in
Jacob Keller0b7c8b52016-10-25 16:08:52 -07001177 * i40e_sync_filters_subtask, but we already have to search
Jacob Kellercbebb852016-10-05 09:30:40 -07001178 * filters here and can perform the check at the same time. This
1179 * results in avoiding embedding a loop for VLAN mode inside another
1180 * loop over all the filters, and should maintain correctness as noted
1181 * above.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001182 */
Jacob Kellercbebb852016-10-05 09:30:40 -07001183 return vsi->has_vlan_filter;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001184}
1185
1186/**
Jacob Keller489a3262016-11-11 12:39:31 -08001187 * i40e_correct_mac_vlan_filters - Correct non-VLAN filters if necessary
1188 * @vsi: the VSI to configure
1189 * @tmp_add_list: list of filters ready to be added
1190 * @tmp_del_list: list of filters ready to be deleted
1191 * @vlan_filters: the number of active VLAN filters
1192 *
1193 * Update VLAN=0 and VLAN=-1 (I40E_VLAN_ANY) filters properly so that they
1194 * behave as expected. If we have any active VLAN filters remaining or about
1195 * to be added then we need to update non-VLAN filters to be marked as VLAN=0
1196 * so that they only match against untagged traffic. If we no longer have any
1197 * active VLAN filters, we need to make all non-VLAN filters marked as VLAN=-1
1198 * so that they match against both tagged and untagged traffic. In this way,
1199 * we ensure that we correctly receive the desired traffic. This ensures that
1200 * when we have an active VLAN we will receive only untagged traffic and
1201 * traffic matching active VLANs. If we have no active VLANs then we will
1202 * operate in non-VLAN mode and receive all traffic, tagged or untagged.
1203 *
1204 * Finally, in a similar fashion, this function also corrects filters when
1205 * there is an active PVID assigned to this VSI.
1206 *
1207 * In case of memory allocation failure return -ENOMEM. Otherwise, return 0.
1208 *
1209 * This function is only expected to be called from within
1210 * i40e_sync_vsi_filters.
1211 *
1212 * NOTE: This function expects to be called while under the
1213 * mac_filter_hash_lock
1214 */
1215static int i40e_correct_mac_vlan_filters(struct i40e_vsi *vsi,
1216 struct hlist_head *tmp_add_list,
1217 struct hlist_head *tmp_del_list,
1218 int vlan_filters)
1219{
Jacob Keller5cb25902016-12-12 15:44:15 -08001220 s16 pvid = le16_to_cpu(vsi->info.pvid);
Jacob Keller489a3262016-11-11 12:39:31 -08001221 struct i40e_mac_filter *f, *add_head;
Jacob Keller671889e2016-12-02 12:33:00 -08001222 struct i40e_new_mac_filter *new;
Jacob Keller489a3262016-11-11 12:39:31 -08001223 struct hlist_node *h;
1224 int bkt, new_vlan;
1225
1226 /* To determine if a particular filter needs to be replaced we
1227 * have the three following conditions:
1228 *
1229 * a) if we have a PVID assigned, then all filters which are
1230 * not marked as VLAN=PVID must be replaced with filters that
1231 * are.
1232 * b) otherwise, if we have any active VLANS, all filters
1233 * which are marked as VLAN=-1 must be replaced with
1234 * filters marked as VLAN=0
1235 * c) finally, if we do not have any active VLANS, all filters
1236 * which are marked as VLAN=0 must be replaced with filters
1237 * marked as VLAN=-1
1238 */
1239
1240 /* Update the filters about to be added in place */
Jacob Keller671889e2016-12-02 12:33:00 -08001241 hlist_for_each_entry(new, tmp_add_list, hlist) {
Jacob Keller5cb25902016-12-12 15:44:15 -08001242 if (pvid && new->f->vlan != pvid)
1243 new->f->vlan = pvid;
Jacob Keller671889e2016-12-02 12:33:00 -08001244 else if (vlan_filters && new->f->vlan == I40E_VLAN_ANY)
1245 new->f->vlan = 0;
1246 else if (!vlan_filters && new->f->vlan == 0)
1247 new->f->vlan = I40E_VLAN_ANY;
Jacob Keller489a3262016-11-11 12:39:31 -08001248 }
1249
1250 /* Update the remaining active filters */
1251 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1252 /* Combine the checks for whether a filter needs to be changed
1253 * and then determine the new VLAN inside the if block, in
1254 * order to avoid duplicating code for adding the new filter
1255 * then deleting the old filter.
1256 */
Jacob Keller5cb25902016-12-12 15:44:15 -08001257 if ((pvid && f->vlan != pvid) ||
Jacob Keller489a3262016-11-11 12:39:31 -08001258 (vlan_filters && f->vlan == I40E_VLAN_ANY) ||
1259 (!vlan_filters && f->vlan == 0)) {
1260 /* Determine the new vlan we will be adding */
Jacob Keller5cb25902016-12-12 15:44:15 -08001261 if (pvid)
1262 new_vlan = pvid;
Jacob Keller489a3262016-11-11 12:39:31 -08001263 else if (vlan_filters)
1264 new_vlan = 0;
1265 else
1266 new_vlan = I40E_VLAN_ANY;
1267
1268 /* Create the new filter */
1269 add_head = i40e_add_filter(vsi, f->macaddr, new_vlan);
1270 if (!add_head)
1271 return -ENOMEM;
1272
Jacob Keller671889e2016-12-02 12:33:00 -08001273 /* Create a temporary i40e_new_mac_filter */
1274 new = kzalloc(sizeof(*new), GFP_ATOMIC);
1275 if (!new)
1276 return -ENOMEM;
1277
1278 new->f = add_head;
1279 new->state = add_head->state;
1280
1281 /* Add the new filter to the tmp list */
1282 hlist_add_head(&new->hlist, tmp_add_list);
Jacob Keller489a3262016-11-11 12:39:31 -08001283
1284 /* Put the original filter into the delete list */
1285 f->state = I40E_FILTER_REMOVE;
1286 hash_del(&f->hlist);
1287 hlist_add_head(&f->hlist, tmp_del_list);
1288 }
1289 }
1290
1291 vsi->has_vlan_filter = !!vlan_filters;
1292
1293 return 0;
1294}
1295
1296/**
Jacob Keller1596b5d2016-11-08 13:05:15 -08001297 * i40e_rm_default_mac_filter - Remove the default MAC filter set by NVM
1298 * @vsi: the PF Main VSI - inappropriate for any other VSI
1299 * @macaddr: the MAC address
1300 *
1301 * Remove whatever filter the firmware set up so the driver can manage
1302 * its own filtering intelligently.
1303 **/
1304static void i40e_rm_default_mac_filter(struct i40e_vsi *vsi, u8 *macaddr)
1305{
1306 struct i40e_aqc_remove_macvlan_element_data element;
1307 struct i40e_pf *pf = vsi->back;
1308
1309 /* Only appropriate for the PF main VSI */
1310 if (vsi->type != I40E_VSI_MAIN)
1311 return;
1312
1313 memset(&element, 0, sizeof(element));
1314 ether_addr_copy(element.mac_addr, macaddr);
1315 element.vlan_tag = 0;
1316 /* Ignore error returns, some firmware does it this way... */
1317 element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
1318 i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1319
1320 memset(&element, 0, sizeof(element));
1321 ether_addr_copy(element.mac_addr, macaddr);
1322 element.vlan_tag = 0;
1323 /* ...and some firmware does it this way. */
1324 element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH |
1325 I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
1326 i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1327}
1328
1329/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001330 * i40e_add_filter - Add a mac/vlan filter to the VSI
1331 * @vsi: the VSI to be searched
1332 * @macaddr: the MAC address
1333 * @vlan: the vlan
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001334 *
1335 * Returns ptr to the filter object or NULL when no memory available.
Kiran Patil21659032015-09-30 14:09:03 -04001336 *
Jacob Keller278e7d02016-10-05 09:30:37 -07001337 * NOTE: This function is expected to be called with mac_filter_hash_lock
Kiran Patil21659032015-09-30 14:09:03 -04001338 * being held.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001339 **/
1340struct i40e_mac_filter *i40e_add_filter(struct i40e_vsi *vsi,
Jacob Keller6622f5c2016-10-05 09:30:32 -07001341 const u8 *macaddr, s16 vlan)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001342{
1343 struct i40e_mac_filter *f;
Jacob Keller278e7d02016-10-05 09:30:37 -07001344 u64 key;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001345
1346 if (!vsi || !macaddr)
1347 return NULL;
1348
Jacob Keller1bc87e82016-10-05 09:30:31 -07001349 f = i40e_find_filter(vsi, macaddr, vlan);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001350 if (!f) {
1351 f = kzalloc(sizeof(*f), GFP_ATOMIC);
1352 if (!f)
Jacob Keller1bc87e82016-10-05 09:30:31 -07001353 return NULL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001354
Jacob Kellercbebb852016-10-05 09:30:40 -07001355 /* Update the boolean indicating if we need to function in
1356 * VLAN mode.
1357 */
1358 if (vlan >= 0)
1359 vsi->has_vlan_filter = true;
1360
Greg Rose9a173902014-05-22 06:32:02 +00001361 ether_addr_copy(f->macaddr, macaddr);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001362 f->vlan = vlan;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001363 /* If we're in overflow promisc mode, set the state directly
1364 * to failed, so we don't bother to try sending the filter
1365 * to the hardware.
1366 */
Jacob Keller0da36b92017-04-19 09:25:55 -04001367 if (test_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state))
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001368 f->state = I40E_FILTER_FAILED;
1369 else
1370 f->state = I40E_FILTER_NEW;
Jacob Keller278e7d02016-10-05 09:30:37 -07001371 INIT_HLIST_NODE(&f->hlist);
1372
1373 key = i40e_addr_to_hkey(macaddr);
1374 hash_add(vsi->mac_filter_hash, &f->hlist, key);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001375
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001376 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1377 vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
1378 }
1379
Jacob Keller1bc87e82016-10-05 09:30:31 -07001380 /* If we're asked to add a filter that has been marked for removal, it
1381 * is safe to simply restore it to active state. __i40e_del_filter
1382 * will have simply deleted any filters which were previously marked
1383 * NEW or FAILED, so if it is currently marked REMOVE it must have
1384 * previously been ACTIVE. Since we haven't yet run the sync filters
1385 * task, just restore this filter to the ACTIVE state so that the
1386 * sync task leaves it in place
1387 */
1388 if (f->state == I40E_FILTER_REMOVE)
1389 f->state = I40E_FILTER_ACTIVE;
1390
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001391 return f;
1392}
1393
1394/**
Jacob Keller290d2552016-10-05 09:30:36 -07001395 * __i40e_del_filter - Remove a specific filter from the VSI
1396 * @vsi: VSI to remove from
1397 * @f: the filter to remove from the list
1398 *
1399 * This function should be called instead of i40e_del_filter only if you know
1400 * the exact filter you will remove already, such as via i40e_find_filter or
1401 * i40e_find_mac.
Kiran Patil21659032015-09-30 14:09:03 -04001402 *
Jacob Keller278e7d02016-10-05 09:30:37 -07001403 * NOTE: This function is expected to be called with mac_filter_hash_lock
Kiran Patil21659032015-09-30 14:09:03 -04001404 * being held.
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001405 * ANOTHER NOTE: This function MUST be called from within the context of
1406 * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
1407 * instead of list_for_each_entry().
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001408 **/
Jacob Keller148141b2016-11-11 12:39:36 -08001409void __i40e_del_filter(struct i40e_vsi *vsi, struct i40e_mac_filter *f)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001410{
Jacob Keller1bc87e82016-10-05 09:30:31 -07001411 if (!f)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001412 return;
1413
Alan Bradya410c822016-12-12 15:44:07 -08001414 /* If the filter was never added to firmware then we can just delete it
1415 * directly and we don't want to set the status to remove or else an
1416 * admin queue command will unnecessarily fire.
1417 */
Jacob Keller1bc87e82016-10-05 09:30:31 -07001418 if ((f->state == I40E_FILTER_FAILED) ||
1419 (f->state == I40E_FILTER_NEW)) {
Jacob Keller278e7d02016-10-05 09:30:37 -07001420 hash_del(&f->hlist);
Jacob Keller1bc87e82016-10-05 09:30:31 -07001421 kfree(f);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001422 } else {
Jacob Keller1bc87e82016-10-05 09:30:31 -07001423 f->state = I40E_FILTER_REMOVE;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001424 }
Alan Bradya410c822016-12-12 15:44:07 -08001425
1426 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1427 vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001428}
1429
1430/**
Jacob Keller290d2552016-10-05 09:30:36 -07001431 * i40e_del_filter - Remove a MAC/VLAN filter from the VSI
1432 * @vsi: the VSI to be searched
1433 * @macaddr: the MAC address
1434 * @vlan: the VLAN
1435 *
Jacob Keller278e7d02016-10-05 09:30:37 -07001436 * NOTE: This function is expected to be called with mac_filter_hash_lock
Jacob Keller290d2552016-10-05 09:30:36 -07001437 * being held.
1438 * ANOTHER NOTE: This function MUST be called from within the context of
1439 * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
1440 * instead of list_for_each_entry().
1441 **/
1442void i40e_del_filter(struct i40e_vsi *vsi, const u8 *macaddr, s16 vlan)
1443{
1444 struct i40e_mac_filter *f;
1445
1446 if (!vsi || !macaddr)
1447 return;
1448
1449 f = i40e_find_filter(vsi, macaddr, vlan);
1450 __i40e_del_filter(vsi, f);
1451}
1452
1453/**
Jacob Kellerfeffdbe2016-11-11 12:39:35 -08001454 * i40e_add_mac_filter - Add a MAC filter for all active VLANs
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001455 * @vsi: the VSI to be searched
1456 * @macaddr: the mac address to be filtered
1457 *
Jacob Kellerfeffdbe2016-11-11 12:39:35 -08001458 * If we're not in VLAN mode, just add the filter to I40E_VLAN_ANY. Otherwise,
1459 * go through all the macvlan filters and add a macvlan filter for each
Jacob Keller5feb3d72016-10-05 09:30:34 -07001460 * unique vlan that already exists. If a PVID has been assigned, instead only
1461 * add the macaddr to that VLAN.
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001462 *
Jacob Keller5feb3d72016-10-05 09:30:34 -07001463 * Returns last filter added on success, else NULL
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001464 **/
Jacob Kellerfeffdbe2016-11-11 12:39:35 -08001465struct i40e_mac_filter *i40e_add_mac_filter(struct i40e_vsi *vsi,
1466 const u8 *macaddr)
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001467{
Jacob Keller5feb3d72016-10-05 09:30:34 -07001468 struct i40e_mac_filter *f, *add = NULL;
Jacob Keller278e7d02016-10-05 09:30:37 -07001469 struct hlist_node *h;
1470 int bkt;
Jacob Keller5feb3d72016-10-05 09:30:34 -07001471
1472 if (vsi->info.pvid)
1473 return i40e_add_filter(vsi, macaddr,
1474 le16_to_cpu(vsi->info.pvid));
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001475
Jacob Keller7aaf95362016-11-11 12:39:33 -08001476 if (!i40e_is_vsi_in_vlan(vsi))
1477 return i40e_add_filter(vsi, macaddr, I40E_VLAN_ANY);
1478
Jacob Keller278e7d02016-10-05 09:30:37 -07001479 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Jacob Keller57b341d2016-10-05 09:30:35 -07001480 if (f->state == I40E_FILTER_REMOVE)
1481 continue;
Jacob Keller5feb3d72016-10-05 09:30:34 -07001482 add = i40e_add_filter(vsi, macaddr, f->vlan);
1483 if (!add)
1484 return NULL;
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001485 }
1486
Jacob Keller5feb3d72016-10-05 09:30:34 -07001487 return add;
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001488}
1489
1490/**
Jacob Kellerfeffdbe2016-11-11 12:39:35 -08001491 * i40e_del_mac_filter - Remove a MAC filter from all VLANs
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001492 * @vsi: the VSI to be searched
1493 * @macaddr: the mac address to be removed
1494 *
Jacob Kellerfeffdbe2016-11-11 12:39:35 -08001495 * Removes a given MAC address from a VSI regardless of what VLAN it has been
1496 * associated with.
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001497 *
1498 * Returns 0 for success, or error
1499 **/
Jacob Kellerfeffdbe2016-11-11 12:39:35 -08001500int i40e_del_mac_filter(struct i40e_vsi *vsi, const u8 *macaddr)
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001501{
Jacob Keller278e7d02016-10-05 09:30:37 -07001502 struct i40e_mac_filter *f;
1503 struct hlist_node *h;
Jacob Keller290d2552016-10-05 09:30:36 -07001504 bool found = false;
Jacob Keller278e7d02016-10-05 09:30:37 -07001505 int bkt;
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001506
Jacob Keller278e7d02016-10-05 09:30:37 -07001507 WARN(!spin_is_locked(&vsi->mac_filter_hash_lock),
1508 "Missing mac_filter_hash_lock\n");
1509 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Jacob Keller290d2552016-10-05 09:30:36 -07001510 if (ether_addr_equal(macaddr, f->macaddr)) {
1511 __i40e_del_filter(vsi, f);
1512 found = true;
1513 }
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001514 }
Jacob Keller290d2552016-10-05 09:30:36 -07001515
1516 if (found)
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001517 return 0;
Jacob Keller290d2552016-10-05 09:30:36 -07001518 else
1519 return -ENOENT;
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001520}
1521
1522/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001523 * i40e_set_mac - NDO callback to set mac address
1524 * @netdev: network interface device structure
1525 * @p: pointer to an address structure
1526 *
1527 * Returns 0 on success, negative on failure
1528 **/
1529static int i40e_set_mac(struct net_device *netdev, void *p)
1530{
1531 struct i40e_netdev_priv *np = netdev_priv(netdev);
1532 struct i40e_vsi *vsi = np->vsi;
Shannon Nelson30650cc2014-07-29 04:01:50 +00001533 struct i40e_pf *pf = vsi->back;
1534 struct i40e_hw *hw = &pf->hw;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001535 struct sockaddr *addr = p;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001536
1537 if (!is_valid_ether_addr(addr->sa_data))
1538 return -EADDRNOTAVAIL;
1539
Shannon Nelson30650cc2014-07-29 04:01:50 +00001540 if (ether_addr_equal(netdev->dev_addr, addr->sa_data)) {
1541 netdev_info(netdev, "already using mac address %pM\n",
1542 addr->sa_data);
1543 return 0;
1544 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001545
Jacob Keller0da36b92017-04-19 09:25:55 -04001546 if (test_bit(__I40E_VSI_DOWN, vsi->back->state) ||
1547 test_bit(__I40E_RESET_RECOVERY_PENDING, vsi->back->state))
Anjali Singhai Jain80f64282013-11-28 06:39:47 +00001548 return -EADDRNOTAVAIL;
1549
Shannon Nelson30650cc2014-07-29 04:01:50 +00001550 if (ether_addr_equal(hw->mac.addr, addr->sa_data))
1551 netdev_info(netdev, "returning to hw mac address %pM\n",
1552 hw->mac.addr);
1553 else
1554 netdev_info(netdev, "set new mac address %pM\n", addr->sa_data);
1555
Jacob Keller278e7d02016-10-05 09:30:37 -07001556 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Kellerfeffdbe2016-11-11 12:39:35 -08001557 i40e_del_mac_filter(vsi, netdev->dev_addr);
1558 i40e_add_mac_filter(vsi, addr->sa_data);
Jacob Keller278e7d02016-10-05 09:30:37 -07001559 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001560 ether_addr_copy(netdev->dev_addr, addr->sa_data);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001561 if (vsi->type == I40E_VSI_MAIN) {
1562 i40e_status ret;
Jesse Brandeburg6995b362015-08-28 17:55:54 -04001563
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001564 ret = i40e_aq_mac_address_write(&vsi->back->hw,
Shannon Nelsoncc412222014-06-04 01:23:21 +00001565 I40E_AQC_WRITE_TYPE_LAA_WOL,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001566 addr->sa_data, NULL);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001567 if (ret)
1568 netdev_info(netdev, "Ignoring error from firmware on LAA update, status %s, AQ ret %s\n",
1569 i40e_stat_str(hw, ret),
1570 i40e_aq_str(hw, hw->aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001571 }
1572
Jesse Brandeburgc53934c2016-01-04 10:33:06 -08001573 /* schedule our worker thread which will take care of
1574 * applying the new filter changes
1575 */
1576 i40e_service_event_schedule(vsi->back);
1577 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001578}
1579
1580/**
1581 * i40e_vsi_setup_queue_map - Setup a VSI queue map based on enabled_tc
1582 * @vsi: the VSI being setup
1583 * @ctxt: VSI context structure
1584 * @enabled_tc: Enabled TCs bitmap
1585 * @is_add: True if called before Add VSI
1586 *
1587 * Setup VSI queue mapping for enabled traffic classes.
1588 **/
1589static void i40e_vsi_setup_queue_map(struct i40e_vsi *vsi,
1590 struct i40e_vsi_context *ctxt,
1591 u8 enabled_tc,
1592 bool is_add)
1593{
1594 struct i40e_pf *pf = vsi->back;
1595 u16 sections = 0;
1596 u8 netdev_tc = 0;
1597 u16 numtc = 0;
1598 u16 qcount;
1599 u8 offset;
1600 u16 qmap;
1601 int i;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001602 u16 num_tc_qps = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001603
1604 sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
1605 offset = 0;
1606
1607 if (enabled_tc && (vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
1608 /* Find numtc from enabled TC bitmap */
1609 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08001610 if (enabled_tc & BIT(i)) /* TC is enabled */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001611 numtc++;
1612 }
1613 if (!numtc) {
1614 dev_warn(&pf->pdev->dev, "DCB is enabled but no TC enabled, forcing TC0\n");
1615 numtc = 1;
1616 }
1617 } else {
1618 /* At least TC0 is enabled in case of non-DCB case */
1619 numtc = 1;
1620 }
1621
1622 vsi->tc_config.numtc = numtc;
1623 vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001624 /* Number of queues per enabled TC */
Catherine Sullivan7d644022016-05-16 10:26:41 -07001625 qcount = vsi->alloc_queue_pairs;
1626
Anjali Singhai7f9ff472015-02-21 06:43:19 +00001627 num_tc_qps = qcount / numtc;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001628 num_tc_qps = min_t(int, num_tc_qps, i40e_pf_get_max_q_per_tc(pf));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001629
1630 /* Setup queue offset/count for all TCs for given VSI */
1631 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1632 /* See if the given TC is enabled for the given VSI */
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08001633 if (vsi->tc_config.enabled_tc & BIT(i)) {
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04001634 /* TC is enabled */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001635 int pow, num_qps;
1636
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001637 switch (vsi->type) {
1638 case I40E_VSI_MAIN:
Helin Zhangacd65442015-10-26 19:44:28 -04001639 qcount = min_t(int, pf->alloc_rss_size,
1640 num_tc_qps);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001641 break;
1642 case I40E_VSI_FDIR:
1643 case I40E_VSI_SRIOV:
1644 case I40E_VSI_VMDQ2:
1645 default:
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001646 qcount = num_tc_qps;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001647 WARN_ON(i != 0);
1648 break;
1649 }
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001650 vsi->tc_config.tc_info[i].qoffset = offset;
1651 vsi->tc_config.tc_info[i].qcount = qcount;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001652
Shannon Nelson1e200e42015-02-27 09:15:24 +00001653 /* find the next higher power-of-2 of num queue pairs */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001654 num_qps = qcount;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001655 pow = 0;
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04001656 while (num_qps && (BIT_ULL(pow) < qcount)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001657 pow++;
1658 num_qps >>= 1;
1659 }
1660
1661 vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
1662 qmap =
1663 (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
1664 (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
1665
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001666 offset += qcount;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001667 } else {
1668 /* TC is not enabled so set the offset to
1669 * default queue and allocate one queue
1670 * for the given TC.
1671 */
1672 vsi->tc_config.tc_info[i].qoffset = 0;
1673 vsi->tc_config.tc_info[i].qcount = 1;
1674 vsi->tc_config.tc_info[i].netdev_tc = 0;
1675
1676 qmap = 0;
1677 }
1678 ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
1679 }
1680
1681 /* Set actual Tx/Rx queue pairs */
1682 vsi->num_queue_pairs = offset;
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00001683 if ((vsi->type == I40E_VSI_MAIN) && (numtc == 1)) {
1684 if (vsi->req_queue_pairs > 0)
1685 vsi->num_queue_pairs = vsi->req_queue_pairs;
Anjali Singhai Jain26cdc442015-07-10 19:36:00 -04001686 else if (pf->flags & I40E_FLAG_MSIX_ENABLED)
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00001687 vsi->num_queue_pairs = pf->num_lan_msix;
1688 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001689
1690 /* Scheduler section valid can only be set for ADD VSI */
1691 if (is_add) {
1692 sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
1693
1694 ctxt->info.up_enable_bits = enabled_tc;
1695 }
1696 if (vsi->type == I40E_VSI_SRIOV) {
1697 ctxt->info.mapping_flags |=
1698 cpu_to_le16(I40E_AQ_VSI_QUE_MAP_NONCONTIG);
1699 for (i = 0; i < vsi->num_queue_pairs; i++)
1700 ctxt->info.queue_mapping[i] =
1701 cpu_to_le16(vsi->base_queue + i);
1702 } else {
1703 ctxt->info.mapping_flags |=
1704 cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
1705 ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
1706 }
1707 ctxt->info.valid_sections |= cpu_to_le16(sections);
1708}
1709
1710/**
Jacob Keller6622f5c2016-10-05 09:30:32 -07001711 * i40e_addr_sync - Callback for dev_(mc|uc)_sync to add address
1712 * @netdev: the netdevice
1713 * @addr: address to add
1714 *
1715 * Called by __dev_(mc|uc)_sync when an address needs to be added. We call
1716 * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
1717 */
1718static int i40e_addr_sync(struct net_device *netdev, const u8 *addr)
1719{
1720 struct i40e_netdev_priv *np = netdev_priv(netdev);
1721 struct i40e_vsi *vsi = np->vsi;
Jacob Keller6622f5c2016-10-05 09:30:32 -07001722
Jacob Kellerfeffdbe2016-11-11 12:39:35 -08001723 if (i40e_add_mac_filter(vsi, addr))
Jacob Keller6622f5c2016-10-05 09:30:32 -07001724 return 0;
1725 else
1726 return -ENOMEM;
1727}
1728
1729/**
1730 * i40e_addr_unsync - Callback for dev_(mc|uc)_sync to remove address
1731 * @netdev: the netdevice
1732 * @addr: address to add
1733 *
1734 * Called by __dev_(mc|uc)_sync when an address needs to be removed. We call
1735 * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
1736 */
1737static int i40e_addr_unsync(struct net_device *netdev, const u8 *addr)
1738{
1739 struct i40e_netdev_priv *np = netdev_priv(netdev);
1740 struct i40e_vsi *vsi = np->vsi;
1741
Jacob Kellerfeffdbe2016-11-11 12:39:35 -08001742 i40e_del_mac_filter(vsi, addr);
Jacob Keller6622f5c2016-10-05 09:30:32 -07001743
1744 return 0;
1745}
1746
1747/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001748 * i40e_set_rx_mode - NDO callback to set the netdev filters
1749 * @netdev: network interface device structure
1750 **/
1751static void i40e_set_rx_mode(struct net_device *netdev)
1752{
1753 struct i40e_netdev_priv *np = netdev_priv(netdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001754 struct i40e_vsi *vsi = np->vsi;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001755
Jacob Keller278e7d02016-10-05 09:30:37 -07001756 spin_lock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -04001757
Jacob Keller6622f5c2016-10-05 09:30:32 -07001758 __dev_uc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
1759 __dev_mc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001760
Jacob Keller278e7d02016-10-05 09:30:37 -07001761 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001762
1763 /* check for other flag changes */
1764 if (vsi->current_netdev_flags != vsi->netdev->flags) {
1765 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1766 vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
1767 }
Jesse Brandeburgc53934c2016-01-04 10:33:06 -08001768
1769 /* schedule our worker thread which will take care of
1770 * applying the new filter changes
1771 */
1772 i40e_service_event_schedule(vsi->back);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001773}
1774
1775/**
Jacob Keller671889e2016-12-02 12:33:00 -08001776 * i40e_undo_del_filter_entries - Undo the changes made to MAC filter entries
Jacob Keller4a2ce272016-10-05 09:30:38 -07001777 * @vsi: Pointer to VSI struct
Kiran Patil21659032015-09-30 14:09:03 -04001778 * @from: Pointer to list which contains MAC filter entries - changes to
1779 * those entries needs to be undone.
1780 *
Jacob Keller671889e2016-12-02 12:33:00 -08001781 * MAC filter entries from this list were slated for deletion.
Kiran Patil21659032015-09-30 14:09:03 -04001782 **/
Jacob Keller671889e2016-12-02 12:33:00 -08001783static void i40e_undo_del_filter_entries(struct i40e_vsi *vsi,
1784 struct hlist_head *from)
Kiran Patil21659032015-09-30 14:09:03 -04001785{
Jacob Keller278e7d02016-10-05 09:30:37 -07001786 struct i40e_mac_filter *f;
1787 struct hlist_node *h;
Kiran Patil21659032015-09-30 14:09:03 -04001788
Jacob Keller278e7d02016-10-05 09:30:37 -07001789 hlist_for_each_entry_safe(f, h, from, hlist) {
1790 u64 key = i40e_addr_to_hkey(f->macaddr);
1791
Kiran Patil21659032015-09-30 14:09:03 -04001792 /* Move the element back into MAC filter list*/
Jacob Keller278e7d02016-10-05 09:30:37 -07001793 hlist_del(&f->hlist);
1794 hash_add(vsi->mac_filter_hash, &f->hlist, key);
Kiran Patil21659032015-09-30 14:09:03 -04001795 }
1796}
1797
1798/**
Jacob Keller671889e2016-12-02 12:33:00 -08001799 * i40e_undo_add_filter_entries - Undo the changes made to MAC filter entries
1800 * @vsi: Pointer to vsi struct
1801 * @from: Pointer to list which contains MAC filter entries - changes to
1802 * those entries needs to be undone.
1803 *
1804 * MAC filter entries from this list were slated for addition.
1805 **/
1806static void i40e_undo_add_filter_entries(struct i40e_vsi *vsi,
1807 struct hlist_head *from)
1808{
1809 struct i40e_new_mac_filter *new;
1810 struct hlist_node *h;
1811
1812 hlist_for_each_entry_safe(new, h, from, hlist) {
1813 /* We can simply free the wrapper structure */
1814 hlist_del(&new->hlist);
1815 kfree(new);
1816 }
1817}
1818
1819/**
Jacob Kellerd88d40b2016-12-02 12:32:59 -08001820 * i40e_next_entry - Get the next non-broadcast filter from a list
Jacob Keller671889e2016-12-02 12:33:00 -08001821 * @next: pointer to filter in list
Jacob Kellerd88d40b2016-12-02 12:32:59 -08001822 *
1823 * Returns the next non-broadcast filter in the list. Required so that we
1824 * ignore broadcast filters within the list, since these are not handled via
1825 * the normal firmware update path.
1826 */
Jacob Keller671889e2016-12-02 12:33:00 -08001827static
1828struct i40e_new_mac_filter *i40e_next_filter(struct i40e_new_mac_filter *next)
Jacob Kellerd88d40b2016-12-02 12:32:59 -08001829{
Jacob Keller584a8872017-03-07 15:17:52 -08001830 hlist_for_each_entry_continue(next, hlist) {
1831 if (!is_broadcast_ether_addr(next->f->macaddr))
1832 return next;
Jacob Kellerd88d40b2016-12-02 12:32:59 -08001833 }
1834
Jacob Keller584a8872017-03-07 15:17:52 -08001835 return NULL;
Jacob Kellerd88d40b2016-12-02 12:32:59 -08001836}
1837
1838/**
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001839 * i40e_update_filter_state - Update filter state based on return data
1840 * from firmware
1841 * @count: Number of filters added
1842 * @add_list: return data from fw
1843 * @head: pointer to first filter in current batch
Kiran Patil21659032015-09-30 14:09:03 -04001844 *
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001845 * MAC filter entries from list were slated to be added to device. Returns
1846 * number of successful filters. Note that 0 does NOT mean success!
Kiran Patil21659032015-09-30 14:09:03 -04001847 **/
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001848static int
1849i40e_update_filter_state(int count,
1850 struct i40e_aqc_add_macvlan_element_data *add_list,
Jacob Keller671889e2016-12-02 12:33:00 -08001851 struct i40e_new_mac_filter *add_head)
Kiran Patil21659032015-09-30 14:09:03 -04001852{
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001853 int retval = 0;
1854 int i;
Kiran Patil21659032015-09-30 14:09:03 -04001855
Jacob Kellerac9e2392016-11-11 12:39:27 -08001856 for (i = 0; i < count; i++) {
1857 /* Always check status of each filter. We don't need to check
1858 * the firmware return status because we pre-set the filter
1859 * status to I40E_AQC_MM_ERR_NO_RES when sending the filter
1860 * request to the adminq. Thus, if it no longer matches then
1861 * we know the filter is active.
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001862 */
Jacob Kellerac9e2392016-11-11 12:39:27 -08001863 if (add_list[i].match_method == I40E_AQC_MM_ERR_NO_RES) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001864 add_head->state = I40E_FILTER_FAILED;
Jacob Kellerac9e2392016-11-11 12:39:27 -08001865 } else {
1866 add_head->state = I40E_FILTER_ACTIVE;
1867 retval++;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001868 }
Jacob Kellerac9e2392016-11-11 12:39:27 -08001869
Jacob Kellerd88d40b2016-12-02 12:32:59 -08001870 add_head = i40e_next_filter(add_head);
1871 if (!add_head)
1872 break;
Kiran Patil21659032015-09-30 14:09:03 -04001873 }
Jacob Kellerac9e2392016-11-11 12:39:27 -08001874
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001875 return retval;
Kiran Patil21659032015-09-30 14:09:03 -04001876}
1877
1878/**
Jacob Keller00936312016-10-05 09:30:41 -07001879 * i40e_aqc_del_filters - Request firmware to delete a set of filters
1880 * @vsi: ptr to the VSI
1881 * @vsi_name: name to display in messages
1882 * @list: the list of filters to send to firmware
1883 * @num_del: the number of filters to delete
1884 * @retval: Set to -EIO on failure to delete
1885 *
1886 * Send a request to firmware via AdminQ to delete a set of filters. Uses
1887 * *retval instead of a return value so that success does not force ret_val to
1888 * be set to 0. This ensures that a sequence of calls to this function
1889 * preserve the previous value of *retval on successful delete.
1890 */
1891static
1892void i40e_aqc_del_filters(struct i40e_vsi *vsi, const char *vsi_name,
1893 struct i40e_aqc_remove_macvlan_element_data *list,
1894 int num_del, int *retval)
1895{
1896 struct i40e_hw *hw = &vsi->back->hw;
1897 i40e_status aq_ret;
1898 int aq_err;
1899
1900 aq_ret = i40e_aq_remove_macvlan(hw, vsi->seid, list, num_del, NULL);
1901 aq_err = hw->aq.asq_last_status;
1902
1903 /* Explicitly ignore and do not report when firmware returns ENOENT */
1904 if (aq_ret && !(aq_err == I40E_AQ_RC_ENOENT)) {
1905 *retval = -EIO;
1906 dev_info(&vsi->back->pdev->dev,
1907 "ignoring delete macvlan error on %s, err %s, aq_err %s\n",
1908 vsi_name, i40e_stat_str(hw, aq_ret),
1909 i40e_aq_str(hw, aq_err));
1910 }
1911}
1912
1913/**
1914 * i40e_aqc_add_filters - Request firmware to add a set of filters
1915 * @vsi: ptr to the VSI
1916 * @vsi_name: name to display in messages
1917 * @list: the list of filters to send to firmware
1918 * @add_head: Position in the add hlist
1919 * @num_add: the number of filters to add
1920 * @promisc_change: set to true on exit if promiscuous mode was forced on
1921 *
1922 * Send a request to firmware via AdminQ to add a chunk of filters. Will set
1923 * promisc_changed to true if the firmware has run out of space for more
1924 * filters.
1925 */
1926static
1927void i40e_aqc_add_filters(struct i40e_vsi *vsi, const char *vsi_name,
1928 struct i40e_aqc_add_macvlan_element_data *list,
Jacob Keller671889e2016-12-02 12:33:00 -08001929 struct i40e_new_mac_filter *add_head,
Jacob Keller00936312016-10-05 09:30:41 -07001930 int num_add, bool *promisc_changed)
1931{
1932 struct i40e_hw *hw = &vsi->back->hw;
Jacob Keller00936312016-10-05 09:30:41 -07001933 int aq_err, fcnt;
1934
Jacob Kellerac9e2392016-11-11 12:39:27 -08001935 i40e_aq_add_macvlan(hw, vsi->seid, list, num_add, NULL);
Jacob Keller00936312016-10-05 09:30:41 -07001936 aq_err = hw->aq.asq_last_status;
Jacob Kellerac9e2392016-11-11 12:39:27 -08001937 fcnt = i40e_update_filter_state(num_add, list, add_head);
Jacob Keller00936312016-10-05 09:30:41 -07001938
1939 if (fcnt != num_add) {
1940 *promisc_changed = true;
Jacob Keller0da36b92017-04-19 09:25:55 -04001941 set_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
Jacob Keller00936312016-10-05 09:30:41 -07001942 dev_warn(&vsi->back->pdev->dev,
1943 "Error %s adding RX filters on %s, promiscuous mode forced on\n",
1944 i40e_aq_str(hw, aq_err),
1945 vsi_name);
1946 }
1947}
1948
1949/**
Jacob Keller435c0842016-11-08 13:05:10 -08001950 * i40e_aqc_broadcast_filter - Set promiscuous broadcast flags
1951 * @vsi: pointer to the VSI
1952 * @f: filter data
1953 *
1954 * This function sets or clears the promiscuous broadcast flags for VLAN
1955 * filters in order to properly receive broadcast frames. Assumes that only
1956 * broadcast filters are passed.
Jacob Keller671889e2016-12-02 12:33:00 -08001957 *
1958 * Returns status indicating success or failure;
Jacob Keller435c0842016-11-08 13:05:10 -08001959 **/
Jacob Keller671889e2016-12-02 12:33:00 -08001960static i40e_status
1961i40e_aqc_broadcast_filter(struct i40e_vsi *vsi, const char *vsi_name,
1962 struct i40e_mac_filter *f)
Jacob Keller435c0842016-11-08 13:05:10 -08001963{
1964 bool enable = f->state == I40E_FILTER_NEW;
1965 struct i40e_hw *hw = &vsi->back->hw;
1966 i40e_status aq_ret;
1967
1968 if (f->vlan == I40E_VLAN_ANY) {
1969 aq_ret = i40e_aq_set_vsi_broadcast(hw,
1970 vsi->seid,
1971 enable,
1972 NULL);
1973 } else {
1974 aq_ret = i40e_aq_set_vsi_bc_promisc_on_vlan(hw,
1975 vsi->seid,
1976 enable,
1977 f->vlan,
1978 NULL);
1979 }
1980
Jacob Keller671889e2016-12-02 12:33:00 -08001981 if (aq_ret)
Jacob Keller435c0842016-11-08 13:05:10 -08001982 dev_warn(&vsi->back->pdev->dev,
1983 "Error %s setting broadcast promiscuous mode on %s\n",
1984 i40e_aq_str(hw, hw->aq.asq_last_status),
1985 vsi_name);
Jacob Keller671889e2016-12-02 12:33:00 -08001986
1987 return aq_ret;
Jacob Keller435c0842016-11-08 13:05:10 -08001988}
1989
1990/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001991 * i40e_sync_vsi_filters - Update the VSI filter list to the HW
1992 * @vsi: ptr to the VSI
1993 *
1994 * Push any outstanding VSI filter changes through the AdminQ.
1995 *
1996 * Returns 0 or error value
1997 **/
Jesse Brandeburg17652c62015-11-05 17:01:02 -08001998int i40e_sync_vsi_filters(struct i40e_vsi *vsi)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001999{
Jacob Keller278e7d02016-10-05 09:30:37 -07002000 struct hlist_head tmp_add_list, tmp_del_list;
Jacob Keller671889e2016-12-02 12:33:00 -08002001 struct i40e_mac_filter *f;
2002 struct i40e_new_mac_filter *new, *add_head = NULL;
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002003 struct i40e_hw *hw = &vsi->back->hw;
Jacob Keller38326212016-11-11 12:39:26 -08002004 unsigned int failed_filters = 0;
Alan Brady84f5ca62016-10-05 09:30:39 -07002005 unsigned int vlan_filters = 0;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002006 bool promisc_changed = false;
Shannon Nelson2d1de822016-05-16 10:26:44 -07002007 char vsi_name[16] = "PF";
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002008 int filter_list_len = 0;
Mitch Williamsea02e902015-11-09 15:35:50 -08002009 i40e_status aq_ret = 0;
Alan Brady84f5ca62016-10-05 09:30:39 -07002010 u32 changed_flags = 0;
Jacob Keller278e7d02016-10-05 09:30:37 -07002011 struct hlist_node *h;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002012 struct i40e_pf *pf;
2013 int num_add = 0;
2014 int num_del = 0;
Alan Brady84f5ca62016-10-05 09:30:39 -07002015 int retval = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002016 u16 cmd_flags;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002017 int list_size;
Jacob Keller278e7d02016-10-05 09:30:37 -07002018 int bkt;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002019
2020 /* empty array typed pointers, kcalloc later */
2021 struct i40e_aqc_add_macvlan_element_data *add_list;
2022 struct i40e_aqc_remove_macvlan_element_data *del_list;
2023
Jacob Keller0da36b92017-04-19 09:25:55 -04002024 while (test_and_set_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002025 usleep_range(1000, 2000);
2026 pf = vsi->back;
2027
2028 if (vsi->netdev) {
2029 changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
2030 vsi->current_netdev_flags = vsi->netdev->flags;
2031 }
2032
Jacob Keller278e7d02016-10-05 09:30:37 -07002033 INIT_HLIST_HEAD(&tmp_add_list);
2034 INIT_HLIST_HEAD(&tmp_del_list);
Kiran Patil21659032015-09-30 14:09:03 -04002035
Shannon Nelson2d1de822016-05-16 10:26:44 -07002036 if (vsi->type == I40E_VSI_SRIOV)
2037 snprintf(vsi_name, sizeof(vsi_name) - 1, "VF %d", vsi->vf_id);
2038 else if (vsi->type != I40E_VSI_MAIN)
2039 snprintf(vsi_name, sizeof(vsi_name) - 1, "vsi %d", vsi->seid);
2040
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002041 if (vsi->flags & I40E_VSI_FLAG_FILTER_CHANGED) {
2042 vsi->flags &= ~I40E_VSI_FLAG_FILTER_CHANGED;
2043
Jacob Keller278e7d02016-10-05 09:30:37 -07002044 spin_lock_bh(&vsi->mac_filter_hash_lock);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002045 /* Create a list of filters to delete. */
Jacob Keller278e7d02016-10-05 09:30:37 -07002046 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002047 if (f->state == I40E_FILTER_REMOVE) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002048 /* Move the element into temporary del_list */
Jacob Keller278e7d02016-10-05 09:30:37 -07002049 hash_del(&f->hlist);
2050 hlist_add_head(&f->hlist, &tmp_del_list);
Alan Brady84f5ca62016-10-05 09:30:39 -07002051
2052 /* Avoid counting removed filters */
2053 continue;
Kiran Patil21659032015-09-30 14:09:03 -04002054 }
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002055 if (f->state == I40E_FILTER_NEW) {
Jacob Keller671889e2016-12-02 12:33:00 -08002056 /* Create a temporary i40e_new_mac_filter */
2057 new = kzalloc(sizeof(*new), GFP_ATOMIC);
2058 if (!new)
2059 goto err_no_memory_locked;
2060
2061 /* Store pointer to the real filter */
2062 new->f = f;
2063 new->state = f->state;
2064
2065 /* Add it to the hash list */
2066 hlist_add_head(&new->hlist, &tmp_add_list);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002067 }
Alan Brady84f5ca62016-10-05 09:30:39 -07002068
Jacob Keller489a3262016-11-11 12:39:31 -08002069 /* Count the number of active (current and new) VLAN
2070 * filters we have now. Does not count filters which
2071 * are marked for deletion.
Alan Brady84f5ca62016-10-05 09:30:39 -07002072 */
2073 if (f->vlan > 0)
2074 vlan_filters++;
Alan Brady84f5ca62016-10-05 09:30:39 -07002075 }
2076
Jacob Keller489a3262016-11-11 12:39:31 -08002077 retval = i40e_correct_mac_vlan_filters(vsi,
2078 &tmp_add_list,
2079 &tmp_del_list,
2080 vlan_filters);
2081 if (retval)
2082 goto err_no_memory_locked;
Alan Brady84f5ca62016-10-05 09:30:39 -07002083
Jacob Keller278e7d02016-10-05 09:30:37 -07002084 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -04002085 }
2086
2087 /* Now process 'del_list' outside the lock */
Jacob Keller278e7d02016-10-05 09:30:37 -07002088 if (!hlist_empty(&tmp_del_list)) {
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002089 filter_list_len = hw->aq.asq_buf_size /
Kiran Patil21659032015-09-30 14:09:03 -04002090 sizeof(struct i40e_aqc_remove_macvlan_element_data);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002091 list_size = filter_list_len *
Shannon Nelsonf1199992015-11-19 11:34:23 -08002092 sizeof(struct i40e_aqc_remove_macvlan_element_data);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002093 del_list = kzalloc(list_size, GFP_ATOMIC);
Jacob Keller4a2ce272016-10-05 09:30:38 -07002094 if (!del_list)
2095 goto err_no_memory;
Kiran Patil21659032015-09-30 14:09:03 -04002096
Jacob Keller278e7d02016-10-05 09:30:37 -07002097 hlist_for_each_entry_safe(f, h, &tmp_del_list, hlist) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002098 cmd_flags = 0;
2099
Jacob Keller435c0842016-11-08 13:05:10 -08002100 /* handle broadcast filters by updating the broadcast
Jacob Kellerd88d40b2016-12-02 12:32:59 -08002101 * promiscuous flag and release filter list.
Jacob Keller435c0842016-11-08 13:05:10 -08002102 */
2103 if (is_broadcast_ether_addr(f->macaddr)) {
2104 i40e_aqc_broadcast_filter(vsi, vsi_name, f);
2105
2106 hlist_del(&f->hlist);
2107 kfree(f);
2108 continue;
2109 }
2110
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002111 /* add to delete list */
Greg Rose9a173902014-05-22 06:32:02 +00002112 ether_addr_copy(del_list[num_del].mac_addr, f->macaddr);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002113 if (f->vlan == I40E_VLAN_ANY) {
2114 del_list[num_del].vlan_tag = 0;
Alan Bradya6cb9142016-09-06 18:05:07 -07002115 cmd_flags |= I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002116 } else {
2117 del_list[num_del].vlan_tag =
2118 cpu_to_le16((u16)(f->vlan));
2119 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002120
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002121 cmd_flags |= I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
2122 del_list[num_del].flags = cmd_flags;
2123 num_del++;
2124
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002125 /* flush a full buffer */
2126 if (num_del == filter_list_len) {
Jacob Keller00936312016-10-05 09:30:41 -07002127 i40e_aqc_del_filters(vsi, vsi_name, del_list,
2128 num_del, &retval);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002129 memset(del_list, 0, list_size);
Jacob Keller00936312016-10-05 09:30:41 -07002130 num_del = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002131 }
Kiran Patil21659032015-09-30 14:09:03 -04002132 /* Release memory for MAC filter entries which were
2133 * synced up with HW.
2134 */
Jacob Keller278e7d02016-10-05 09:30:37 -07002135 hlist_del(&f->hlist);
Kiran Patil21659032015-09-30 14:09:03 -04002136 kfree(f);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002137 }
Kiran Patil21659032015-09-30 14:09:03 -04002138
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002139 if (num_del) {
Jacob Keller00936312016-10-05 09:30:41 -07002140 i40e_aqc_del_filters(vsi, vsi_name, del_list,
2141 num_del, &retval);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002142 }
2143
2144 kfree(del_list);
2145 del_list = NULL;
Kiran Patil21659032015-09-30 14:09:03 -04002146 }
2147
Jacob Keller278e7d02016-10-05 09:30:37 -07002148 if (!hlist_empty(&tmp_add_list)) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002149 /* Do all the adds now. */
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002150 filter_list_len = hw->aq.asq_buf_size /
Shannon Nelsonf1199992015-11-19 11:34:23 -08002151 sizeof(struct i40e_aqc_add_macvlan_element_data);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002152 list_size = filter_list_len *
2153 sizeof(struct i40e_aqc_add_macvlan_element_data);
2154 add_list = kzalloc(list_size, GFP_ATOMIC);
Jacob Keller4a2ce272016-10-05 09:30:38 -07002155 if (!add_list)
2156 goto err_no_memory;
2157
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002158 num_add = 0;
Jacob Keller671889e2016-12-02 12:33:00 -08002159 hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
Jacob Kellerd19cb642017-04-21 13:38:05 -07002160 if (test_bit(__I40E_VSI_OVERFLOW_PROMISC,
Jacob Keller0da36b92017-04-19 09:25:55 -04002161 vsi->state)) {
Jacob Keller671889e2016-12-02 12:33:00 -08002162 new->state = I40E_FILTER_FAILED;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002163 continue;
2164 }
Jacob Keller435c0842016-11-08 13:05:10 -08002165
2166 /* handle broadcast filters by updating the broadcast
2167 * promiscuous flag instead of adding a MAC filter.
2168 */
Jacob Keller671889e2016-12-02 12:33:00 -08002169 if (is_broadcast_ether_addr(new->f->macaddr)) {
2170 if (i40e_aqc_broadcast_filter(vsi, vsi_name,
2171 new->f))
2172 new->state = I40E_FILTER_FAILED;
2173 else
2174 new->state = I40E_FILTER_ACTIVE;
Jacob Keller435c0842016-11-08 13:05:10 -08002175 continue;
2176 }
2177
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002178 /* add to add array */
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002179 if (num_add == 0)
Jacob Keller671889e2016-12-02 12:33:00 -08002180 add_head = new;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002181 cmd_flags = 0;
Jacob Keller671889e2016-12-02 12:33:00 -08002182 ether_addr_copy(add_list[num_add].mac_addr,
2183 new->f->macaddr);
2184 if (new->f->vlan == I40E_VLAN_ANY) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002185 add_list[num_add].vlan_tag = 0;
2186 cmd_flags |= I40E_AQC_MACVLAN_ADD_IGNORE_VLAN;
2187 } else {
2188 add_list[num_add].vlan_tag =
Jacob Keller671889e2016-12-02 12:33:00 -08002189 cpu_to_le16((u16)(new->f->vlan));
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002190 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002191 add_list[num_add].queue_number = 0;
Jacob Kellerac9e2392016-11-11 12:39:27 -08002192 /* set invalid match method for later detection */
Keller, Jacob E0266ac42016-12-09 13:39:21 -08002193 add_list[num_add].match_method = I40E_AQC_MM_ERR_NO_RES;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002194 cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002195 add_list[num_add].flags = cpu_to_le16(cmd_flags);
2196 num_add++;
2197
2198 /* flush a full buffer */
2199 if (num_add == filter_list_len) {
Jacob Keller00936312016-10-05 09:30:41 -07002200 i40e_aqc_add_filters(vsi, vsi_name, add_list,
2201 add_head, num_add,
2202 &promisc_changed);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002203 memset(add_list, 0, list_size);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002204 num_add = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002205 }
2206 }
2207 if (num_add) {
Jacob Keller00936312016-10-05 09:30:41 -07002208 i40e_aqc_add_filters(vsi, vsi_name, add_list, add_head,
2209 num_add, &promisc_changed);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002210 }
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002211 /* Now move all of the filters from the temp add list back to
2212 * the VSI's list.
2213 */
Jacob Keller278e7d02016-10-05 09:30:37 -07002214 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller671889e2016-12-02 12:33:00 -08002215 hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
2216 /* Only update the state if we're still NEW */
2217 if (new->f->state == I40E_FILTER_NEW)
2218 new->f->state = new->state;
2219 hlist_del(&new->hlist);
2220 kfree(new);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002221 }
Jacob Keller278e7d02016-10-05 09:30:37 -07002222 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002223 kfree(add_list);
2224 add_list = NULL;
2225 }
2226
Jacob Keller38326212016-11-11 12:39:26 -08002227 /* Determine the number of active and failed filters. */
2228 spin_lock_bh(&vsi->mac_filter_hash_lock);
2229 vsi->active_filters = 0;
2230 hash_for_each(vsi->mac_filter_hash, bkt, f, hlist) {
2231 if (f->state == I40E_FILTER_ACTIVE)
2232 vsi->active_filters++;
2233 else if (f->state == I40E_FILTER_FAILED)
2234 failed_filters++;
2235 }
2236 spin_unlock_bh(&vsi->mac_filter_hash_lock);
2237
2238 /* If promiscuous mode has changed, we need to calculate a new
2239 * threshold for when we are safe to exit
2240 */
2241 if (promisc_changed)
2242 vsi->promisc_threshold = (vsi->active_filters * 3) / 4;
2243
2244 /* Check if we are able to exit overflow promiscuous mode. We can
2245 * safely exit if we didn't just enter, we no longer have any failed
2246 * filters, and we have reduced filters below the threshold value.
2247 */
Jacob Keller0da36b92017-04-19 09:25:55 -04002248 if (test_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state) &&
Jacob Keller38326212016-11-11 12:39:26 -08002249 !promisc_changed && !failed_filters &&
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002250 (vsi->active_filters < vsi->promisc_threshold)) {
Jacob Keller38326212016-11-11 12:39:26 -08002251 dev_info(&pf->pdev->dev,
2252 "filter logjam cleared on %s, leaving overflow promiscuous mode\n",
2253 vsi_name);
Jacob Keller0da36b92017-04-19 09:25:55 -04002254 clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
Jacob Keller38326212016-11-11 12:39:26 -08002255 promisc_changed = true;
2256 vsi->promisc_threshold = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002257 }
2258
Anjali Singhai Jaina856b5c2016-04-13 03:08:23 -07002259 /* if the VF is not trusted do not do promisc */
2260 if ((vsi->type == I40E_VSI_SRIOV) && !pf->vf[vsi->vf_id].trusted) {
Jacob Keller0da36b92017-04-19 09:25:55 -04002261 clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
Anjali Singhai Jaina856b5c2016-04-13 03:08:23 -07002262 goto out;
2263 }
2264
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002265 /* check for changes in promiscuous modes */
2266 if (changed_flags & IFF_ALLMULTI) {
2267 bool cur_multipromisc;
Jesse Brandeburg6995b362015-08-28 17:55:54 -04002268
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002269 cur_multipromisc = !!(vsi->current_netdev_flags & IFF_ALLMULTI);
Mitch Williamsea02e902015-11-09 15:35:50 -08002270 aq_ret = i40e_aq_set_vsi_multicast_promiscuous(&vsi->back->hw,
2271 vsi->seid,
2272 cur_multipromisc,
2273 NULL);
2274 if (aq_ret) {
2275 retval = i40e_aq_rc_to_posix(aq_ret,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002276 hw->aq.asq_last_status);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002277 dev_info(&pf->pdev->dev,
Shannon Nelson2d1de822016-05-16 10:26:44 -07002278 "set multi promisc failed on %s, err %s aq_err %s\n",
2279 vsi_name,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002280 i40e_stat_str(hw, aq_ret),
2281 i40e_aq_str(hw, hw->aq.asq_last_status));
Mitch Williamsea02e902015-11-09 15:35:50 -08002282 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002283 }
Alan Bradye5887232017-06-07 05:43:10 -04002284
2285 if ((changed_flags & IFF_PROMISC) || promisc_changed) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002286 bool cur_promisc;
Jesse Brandeburg6995b362015-08-28 17:55:54 -04002287
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002288 cur_promisc = (!!(vsi->current_netdev_flags & IFF_PROMISC) ||
Jacob Kellerd19cb642017-04-21 13:38:05 -07002289 test_bit(__I40E_VSI_OVERFLOW_PROMISC,
Jacob Keller0da36b92017-04-19 09:25:55 -04002290 vsi->state));
Anjali Singhai Jain6784ed52016-01-15 14:33:13 -08002291 if ((vsi->type == I40E_VSI_MAIN) &&
2292 (pf->lan_veb != I40E_NO_VEB) &&
2293 !(pf->flags & I40E_FLAG_MFP_ENABLED)) {
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002294 /* set defport ON for Main VSI instead of true promisc
2295 * this way we will get all unicast/multicast and VLAN
2296 * promisc behavior but will not get VF or VMDq traffic
2297 * replicated on the Main VSI.
2298 */
2299 if (pf->cur_promisc != cur_promisc) {
2300 pf->cur_promisc = cur_promisc;
Mitch Williams5bc16032016-05-16 10:26:43 -07002301 if (cur_promisc)
2302 aq_ret =
2303 i40e_aq_set_default_vsi(hw,
2304 vsi->seid,
2305 NULL);
2306 else
2307 aq_ret =
2308 i40e_aq_clear_default_vsi(hw,
2309 vsi->seid,
2310 NULL);
2311 if (aq_ret) {
2312 retval = i40e_aq_rc_to_posix(aq_ret,
2313 hw->aq.asq_last_status);
2314 dev_info(&pf->pdev->dev,
Shannon Nelson2d1de822016-05-16 10:26:44 -07002315 "Set default VSI failed on %s, err %s, aq_err %s\n",
2316 vsi_name,
Mitch Williams5bc16032016-05-16 10:26:43 -07002317 i40e_stat_str(hw, aq_ret),
2318 i40e_aq_str(hw,
2319 hw->aq.asq_last_status));
2320 }
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002321 }
2322 } else {
Mitch Williamsea02e902015-11-09 15:35:50 -08002323 aq_ret = i40e_aq_set_vsi_unicast_promiscuous(
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002324 hw,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002325 vsi->seid,
Anjali Singhai Jainb5569892016-05-03 15:13:12 -07002326 cur_promisc, NULL,
2327 true);
Mitch Williamsea02e902015-11-09 15:35:50 -08002328 if (aq_ret) {
2329 retval =
2330 i40e_aq_rc_to_posix(aq_ret,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002331 hw->aq.asq_last_status);
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002332 dev_info(&pf->pdev->dev,
Shannon Nelson2d1de822016-05-16 10:26:44 -07002333 "set unicast promisc failed on %s, err %s, aq_err %s\n",
2334 vsi_name,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002335 i40e_stat_str(hw, aq_ret),
2336 i40e_aq_str(hw,
2337 hw->aq.asq_last_status));
Mitch Williamsea02e902015-11-09 15:35:50 -08002338 }
2339 aq_ret = i40e_aq_set_vsi_multicast_promiscuous(
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002340 hw,
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002341 vsi->seid,
2342 cur_promisc, NULL);
Mitch Williamsea02e902015-11-09 15:35:50 -08002343 if (aq_ret) {
2344 retval =
2345 i40e_aq_rc_to_posix(aq_ret,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002346 hw->aq.asq_last_status);
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002347 dev_info(&pf->pdev->dev,
Shannon Nelson2d1de822016-05-16 10:26:44 -07002348 "set multicast promisc failed on %s, err %s, aq_err %s\n",
2349 vsi_name,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002350 i40e_stat_str(hw, aq_ret),
2351 i40e_aq_str(hw,
2352 hw->aq.asq_last_status));
Mitch Williamsea02e902015-11-09 15:35:50 -08002353 }
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002354 }
Mitch Williamsea02e902015-11-09 15:35:50 -08002355 aq_ret = i40e_aq_set_vsi_broadcast(&vsi->back->hw,
2356 vsi->seid,
2357 cur_promisc, NULL);
2358 if (aq_ret) {
2359 retval = i40e_aq_rc_to_posix(aq_ret,
2360 pf->hw.aq.asq_last_status);
Greg Rose1a103702013-11-28 06:42:39 +00002361 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002362 "set brdcast promisc failed, err %s, aq_err %s\n",
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002363 i40e_stat_str(hw, aq_ret),
2364 i40e_aq_str(hw,
2365 hw->aq.asq_last_status));
Mitch Williamsea02e902015-11-09 15:35:50 -08002366 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002367 }
Mitch Williamsea02e902015-11-09 15:35:50 -08002368out:
Jesse Brandeburg2818ccd2016-01-13 16:51:38 -08002369 /* if something went wrong then set the changed flag so we try again */
2370 if (retval)
2371 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2372
Jacob Keller0da36b92017-04-19 09:25:55 -04002373 clear_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state);
Mitch Williamsea02e902015-11-09 15:35:50 -08002374 return retval;
Jacob Keller4a2ce272016-10-05 09:30:38 -07002375
2376err_no_memory:
2377 /* Restore elements on the temporary add and delete lists */
2378 spin_lock_bh(&vsi->mac_filter_hash_lock);
Alan Brady84f5ca62016-10-05 09:30:39 -07002379err_no_memory_locked:
Jacob Keller671889e2016-12-02 12:33:00 -08002380 i40e_undo_del_filter_entries(vsi, &tmp_del_list);
2381 i40e_undo_add_filter_entries(vsi, &tmp_add_list);
Jacob Keller4a2ce272016-10-05 09:30:38 -07002382 spin_unlock_bh(&vsi->mac_filter_hash_lock);
2383
2384 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
Jacob Keller0da36b92017-04-19 09:25:55 -04002385 clear_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state);
Jacob Keller4a2ce272016-10-05 09:30:38 -07002386 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002387}
2388
2389/**
2390 * i40e_sync_filters_subtask - Sync the VSI filter list with HW
2391 * @pf: board private structure
2392 **/
2393static void i40e_sync_filters_subtask(struct i40e_pf *pf)
2394{
2395 int v;
2396
2397 if (!pf || !(pf->flags & I40E_FLAG_FILTER_SYNC))
2398 return;
2399 pf->flags &= ~I40E_FLAG_FILTER_SYNC;
2400
Mitch Williams505682c2014-05-20 08:01:37 +00002401 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002402 if (pf->vsi[v] &&
Jesse Brandeburg17652c62015-11-05 17:01:02 -08002403 (pf->vsi[v]->flags & I40E_VSI_FLAG_FILTER_CHANGED)) {
2404 int ret = i40e_sync_vsi_filters(pf->vsi[v]);
2405
2406 if (ret) {
2407 /* come back and try again later */
2408 pf->flags |= I40E_FLAG_FILTER_SYNC;
2409 break;
2410 }
2411 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002412 }
2413}
2414
2415/**
Björn Töpel0c8493d2017-05-24 07:55:34 +02002416 * i40e_max_xdp_frame_size - returns the maximum allowed frame size for XDP
2417 * @vsi: the vsi
2418 **/
2419static int i40e_max_xdp_frame_size(struct i40e_vsi *vsi)
2420{
2421 if (PAGE_SIZE >= 8192 || (vsi->back->flags & I40E_FLAG_LEGACY_RX))
2422 return I40E_RXBUFFER_2048;
2423 else
2424 return I40E_RXBUFFER_3072;
2425}
2426
2427/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002428 * i40e_change_mtu - NDO callback to change the Maximum Transfer Unit
2429 * @netdev: network interface device structure
2430 * @new_mtu: new value for maximum frame size
2431 *
2432 * Returns 0 on success, negative on failure
2433 **/
2434static int i40e_change_mtu(struct net_device *netdev, int new_mtu)
2435{
2436 struct i40e_netdev_priv *np = netdev_priv(netdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002437 struct i40e_vsi *vsi = np->vsi;
Mitch Williams0ef2d5a2017-01-24 10:24:00 -08002438 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002439
Björn Töpel0c8493d2017-05-24 07:55:34 +02002440 if (i40e_enabled_xdp_vsi(vsi)) {
2441 int frame_size = new_mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
2442
2443 if (frame_size > i40e_max_xdp_frame_size(vsi))
2444 return -EINVAL;
2445 }
2446
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002447 netdev_info(netdev, "changing MTU from %d to %d\n",
2448 netdev->mtu, new_mtu);
2449 netdev->mtu = new_mtu;
2450 if (netif_running(netdev))
2451 i40e_vsi_reinit_locked(vsi);
Mitch Williams0ef2d5a2017-01-24 10:24:00 -08002452 pf->flags |= (I40E_FLAG_SERVICE_CLIENT_REQUESTED |
2453 I40E_FLAG_CLIENT_L2_CHANGE);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002454 return 0;
2455}
2456
2457/**
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00002458 * i40e_ioctl - Access the hwtstamp interface
2459 * @netdev: network interface device structure
2460 * @ifr: interface request data
2461 * @cmd: ioctl command
2462 **/
2463int i40e_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
2464{
2465 struct i40e_netdev_priv *np = netdev_priv(netdev);
2466 struct i40e_pf *pf = np->vsi->back;
2467
2468 switch (cmd) {
2469 case SIOCGHWTSTAMP:
2470 return i40e_ptp_get_ts_config(pf, ifr);
2471 case SIOCSHWTSTAMP:
2472 return i40e_ptp_set_ts_config(pf, ifr);
2473 default:
2474 return -EOPNOTSUPP;
2475 }
2476}
2477
2478/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002479 * i40e_vlan_stripping_enable - Turn on vlan stripping for the VSI
2480 * @vsi: the vsi being adjusted
2481 **/
2482void i40e_vlan_stripping_enable(struct i40e_vsi *vsi)
2483{
2484 struct i40e_vsi_context ctxt;
2485 i40e_status ret;
2486
2487 if ((vsi->info.valid_sections &
2488 cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
2489 ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_MODE_MASK) == 0))
2490 return; /* already enabled */
2491
2492 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2493 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
2494 I40E_AQ_VSI_PVLAN_EMOD_STR_BOTH;
2495
2496 ctxt.seid = vsi->seid;
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07002497 ctxt.info = vsi->info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002498 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2499 if (ret) {
2500 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002501 "update vlan stripping failed, err %s aq_err %s\n",
2502 i40e_stat_str(&vsi->back->hw, ret),
2503 i40e_aq_str(&vsi->back->hw,
2504 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002505 }
2506}
2507
2508/**
2509 * i40e_vlan_stripping_disable - Turn off vlan stripping for the VSI
2510 * @vsi: the vsi being adjusted
2511 **/
2512void i40e_vlan_stripping_disable(struct i40e_vsi *vsi)
2513{
2514 struct i40e_vsi_context ctxt;
2515 i40e_status ret;
2516
2517 if ((vsi->info.valid_sections &
2518 cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
2519 ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_EMOD_MASK) ==
2520 I40E_AQ_VSI_PVLAN_EMOD_MASK))
2521 return; /* already disabled */
2522
2523 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2524 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
2525 I40E_AQ_VSI_PVLAN_EMOD_NOTHING;
2526
2527 ctxt.seid = vsi->seid;
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07002528 ctxt.info = vsi->info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002529 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2530 if (ret) {
2531 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002532 "update vlan stripping failed, err %s aq_err %s\n",
2533 i40e_stat_str(&vsi->back->hw, ret),
2534 i40e_aq_str(&vsi->back->hw,
2535 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002536 }
2537}
2538
2539/**
2540 * i40e_vlan_rx_register - Setup or shutdown vlan offload
2541 * @netdev: network interface to be adjusted
2542 * @features: netdev features to test if VLAN offload is enabled or not
2543 **/
2544static void i40e_vlan_rx_register(struct net_device *netdev, u32 features)
2545{
2546 struct i40e_netdev_priv *np = netdev_priv(netdev);
2547 struct i40e_vsi *vsi = np->vsi;
2548
2549 if (features & NETIF_F_HW_VLAN_CTAG_RX)
2550 i40e_vlan_stripping_enable(vsi);
2551 else
2552 i40e_vlan_stripping_disable(vsi);
2553}
2554
2555/**
Jacob Keller490a4ad2016-11-11 12:39:29 -08002556 * i40e_add_vlan_all_mac - Add a MAC/VLAN filter for each existing MAC address
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002557 * @vsi: the vsi being configured
2558 * @vid: vlan id to be added (0 = untagged only , -1 = any)
Jacob Keller490a4ad2016-11-11 12:39:29 -08002559 *
2560 * This is a helper function for adding a new MAC/VLAN filter with the
2561 * specified VLAN for each existing MAC address already in the hash table.
2562 * This function does *not* perform any accounting to update filters based on
2563 * VLAN mode.
2564 *
2565 * NOTE: this function expects to be called while under the
2566 * mac_filter_hash_lock
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002567 **/
Jacob Keller9af52f62016-11-11 12:39:30 -08002568int i40e_add_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002569{
Jacob Keller490a4ad2016-11-11 12:39:29 -08002570 struct i40e_mac_filter *f, *add_f;
Jacob Keller278e7d02016-10-05 09:30:37 -07002571 struct hlist_node *h;
2572 int bkt;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002573
Jacob Keller278e7d02016-10-05 09:30:37 -07002574 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Jacob Keller57b341d2016-10-05 09:30:35 -07002575 if (f->state == I40E_FILTER_REMOVE)
2576 continue;
Jacob Keller1bc87e82016-10-05 09:30:31 -07002577 add_f = i40e_add_filter(vsi, f->macaddr, vid);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002578 if (!add_f) {
2579 dev_info(&vsi->back->pdev->dev,
2580 "Could not add vlan filter %d for %pM\n",
2581 vid, f->macaddr);
2582 return -ENOMEM;
2583 }
2584 }
2585
Jacob Keller490a4ad2016-11-11 12:39:29 -08002586 return 0;
2587}
2588
2589/**
2590 * i40e_vsi_add_vlan - Add VSI membership for given VLAN
2591 * @vsi: the VSI being configured
Jacob Kellerf94484b2016-12-07 14:05:34 -08002592 * @vid: VLAN id to be added
Jacob Keller490a4ad2016-11-11 12:39:29 -08002593 **/
Jacob Kellerf94484b2016-12-07 14:05:34 -08002594int i40e_vsi_add_vlan(struct i40e_vsi *vsi, u16 vid)
Jacob Keller490a4ad2016-11-11 12:39:29 -08002595{
Jacob Keller489a3262016-11-11 12:39:31 -08002596 int err;
Jacob Keller490a4ad2016-11-11 12:39:29 -08002597
Jacob Kellerf94484b2016-12-07 14:05:34 -08002598 if (!vid || vsi->info.pvid)
2599 return -EINVAL;
2600
Jacob Keller490a4ad2016-11-11 12:39:29 -08002601 /* Locked once because all functions invoked below iterates list*/
2602 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller490a4ad2016-11-11 12:39:29 -08002603 err = i40e_add_vlan_all_mac(vsi, vid);
Jacob Keller278e7d02016-10-05 09:30:37 -07002604 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller489a3262016-11-11 12:39:31 -08002605 if (err)
2606 return err;
Kiran Patil21659032015-09-30 14:09:03 -04002607
Jesse Brandeburg0e4425e2015-11-05 17:01:01 -08002608 /* schedule our worker thread which will take care of
2609 * applying the new filter changes
2610 */
2611 i40e_service_event_schedule(vsi->back);
2612 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002613}
2614
2615/**
Jacob Keller490a4ad2016-11-11 12:39:29 -08002616 * i40e_rm_vlan_all_mac - Remove MAC/VLAN pair for all MAC with the given VLAN
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002617 * @vsi: the vsi being configured
2618 * @vid: vlan id to be removed (0 = untagged only , -1 = any)
Jacob Keller490a4ad2016-11-11 12:39:29 -08002619 *
2620 * This function should be used to remove all VLAN filters which match the
2621 * given VID. It does not schedule the service event and does not take the
2622 * mac_filter_hash_lock so it may be combined with other operations under
2623 * a single invocation of the mac_filter_hash_lock.
2624 *
2625 * NOTE: this function expects to be called while under the
2626 * mac_filter_hash_lock
2627 */
Jacob Keller9af52f62016-11-11 12:39:30 -08002628void i40e_rm_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002629{
Alan Brady84f5ca62016-10-05 09:30:39 -07002630 struct i40e_mac_filter *f;
Jacob Keller278e7d02016-10-05 09:30:37 -07002631 struct hlist_node *h;
Jacob Keller278e7d02016-10-05 09:30:37 -07002632 int bkt;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002633
Jacob Keller278e7d02016-10-05 09:30:37 -07002634 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Jacob Keller290d2552016-10-05 09:30:36 -07002635 if (f->vlan == vid)
2636 __i40e_del_filter(vsi, f);
2637 }
Jacob Keller490a4ad2016-11-11 12:39:29 -08002638}
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002639
Jacob Keller490a4ad2016-11-11 12:39:29 -08002640/**
2641 * i40e_vsi_kill_vlan - Remove VSI membership for given VLAN
2642 * @vsi: the VSI being configured
Jacob Kellerf94484b2016-12-07 14:05:34 -08002643 * @vid: VLAN id to be removed
Jacob Keller490a4ad2016-11-11 12:39:29 -08002644 **/
Jacob Kellerf94484b2016-12-07 14:05:34 -08002645void i40e_vsi_kill_vlan(struct i40e_vsi *vsi, u16 vid)
Jacob Keller490a4ad2016-11-11 12:39:29 -08002646{
Jacob Kellerf94484b2016-12-07 14:05:34 -08002647 if (!vid || vsi->info.pvid)
2648 return;
2649
Jacob Keller490a4ad2016-11-11 12:39:29 -08002650 spin_lock_bh(&vsi->mac_filter_hash_lock);
2651 i40e_rm_vlan_all_mac(vsi, vid);
Jacob Keller278e7d02016-10-05 09:30:37 -07002652 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -04002653
Jesse Brandeburg0e4425e2015-11-05 17:01:01 -08002654 /* schedule our worker thread which will take care of
2655 * applying the new filter changes
2656 */
2657 i40e_service_event_schedule(vsi->back);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002658}
2659
2660/**
2661 * i40e_vlan_rx_add_vid - Add a vlan id filter to HW offload
2662 * @netdev: network interface to be adjusted
2663 * @vid: vlan id to be added
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002664 *
2665 * net_device_ops implementation for adding vlan ids
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002666 **/
2667static int i40e_vlan_rx_add_vid(struct net_device *netdev,
2668 __always_unused __be16 proto, u16 vid)
2669{
2670 struct i40e_netdev_priv *np = netdev_priv(netdev);
2671 struct i40e_vsi *vsi = np->vsi;
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002672 int ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002673
Jacob Keller6a1127852016-10-25 16:08:49 -07002674 if (vid >= VLAN_N_VID)
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002675 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002676
Anjali Singhai Jain6982d422014-02-06 05:51:10 +00002677 /* If the network stack called us with vid = 0 then
2678 * it is asking to receive priority tagged packets with
2679 * vlan id 0. Our HW receives them by default when configured
2680 * to receive untagged packets so there is no need to add an
2681 * extra filter for vlan 0 tagged packets.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002682 */
Anjali Singhai Jain6982d422014-02-06 05:51:10 +00002683 if (vid)
2684 ret = i40e_vsi_add_vlan(vsi, vid);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002685
Jacob Keller6a1127852016-10-25 16:08:49 -07002686 if (!ret)
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002687 set_bit(vid, vsi->active_vlans);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002688
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002689 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002690}
2691
2692/**
2693 * i40e_vlan_rx_kill_vid - Remove a vlan id filter from HW offload
2694 * @netdev: network interface to be adjusted
2695 * @vid: vlan id to be removed
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002696 *
Akeem G Abodunrinfdfd9432014-02-11 08:24:15 +00002697 * net_device_ops implementation for removing vlan ids
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002698 **/
2699static int i40e_vlan_rx_kill_vid(struct net_device *netdev,
2700 __always_unused __be16 proto, u16 vid)
2701{
2702 struct i40e_netdev_priv *np = netdev_priv(netdev);
2703 struct i40e_vsi *vsi = np->vsi;
2704
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002705 /* return code is ignored as there is nothing a user
2706 * can do about failure to remove and a log message was
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002707 * already printed from the other function
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002708 */
2709 i40e_vsi_kill_vlan(vsi, vid);
2710
2711 clear_bit(vid, vsi->active_vlans);
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002712
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002713 return 0;
2714}
2715
2716/**
Tushar Daveb1b15df2016-07-01 10:11:20 -07002717 * i40e_macaddr_init - explicitly write the mac address filters
2718 *
2719 * @vsi: pointer to the vsi
2720 * @macaddr: the MAC address
2721 *
2722 * This is needed when the macaddr has been obtained by other
2723 * means than the default, e.g., from Open Firmware or IDPROM.
2724 * Returns 0 on success, negative on failure
2725 **/
2726static int i40e_macaddr_init(struct i40e_vsi *vsi, u8 *macaddr)
2727{
2728 int ret;
2729 struct i40e_aqc_add_macvlan_element_data element;
2730
2731 ret = i40e_aq_mac_address_write(&vsi->back->hw,
2732 I40E_AQC_WRITE_TYPE_LAA_WOL,
2733 macaddr, NULL);
2734 if (ret) {
2735 dev_info(&vsi->back->pdev->dev,
2736 "Addr change for VSI failed: %d\n", ret);
2737 return -EADDRNOTAVAIL;
2738 }
2739
2740 memset(&element, 0, sizeof(element));
2741 ether_addr_copy(element.mac_addr, macaddr);
2742 element.flags = cpu_to_le16(I40E_AQC_MACVLAN_ADD_PERFECT_MATCH);
2743 ret = i40e_aq_add_macvlan(&vsi->back->hw, vsi->seid, &element, 1, NULL);
2744 if (ret) {
2745 dev_info(&vsi->back->pdev->dev,
2746 "add filter failed err %s aq_err %s\n",
2747 i40e_stat_str(&vsi->back->hw, ret),
2748 i40e_aq_str(&vsi->back->hw,
2749 vsi->back->hw.aq.asq_last_status));
2750 }
2751 return ret;
2752}
2753
2754/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002755 * i40e_restore_vlan - Reinstate vlans when vsi/netdev comes back up
2756 * @vsi: the vsi being brought back up
2757 **/
2758static void i40e_restore_vlan(struct i40e_vsi *vsi)
2759{
2760 u16 vid;
2761
2762 if (!vsi->netdev)
2763 return;
2764
2765 i40e_vlan_rx_register(vsi->netdev, vsi->netdev->features);
2766
2767 for_each_set_bit(vid, vsi->active_vlans, VLAN_N_VID)
2768 i40e_vlan_rx_add_vid(vsi->netdev, htons(ETH_P_8021Q),
2769 vid);
2770}
2771
2772/**
2773 * i40e_vsi_add_pvid - Add pvid for the VSI
2774 * @vsi: the vsi being adjusted
2775 * @vid: the vlan id to set as a PVID
2776 **/
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002777int i40e_vsi_add_pvid(struct i40e_vsi *vsi, u16 vid)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002778{
2779 struct i40e_vsi_context ctxt;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002780 i40e_status ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002781
2782 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2783 vsi->info.pvid = cpu_to_le16(vid);
Greg Rose6c12fcb2013-11-28 06:39:34 +00002784 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_TAGGED |
2785 I40E_AQ_VSI_PVLAN_INSERT_PVID |
Greg Roseb774c7d2013-11-28 06:39:44 +00002786 I40E_AQ_VSI_PVLAN_EMOD_STR;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002787
2788 ctxt.seid = vsi->seid;
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07002789 ctxt.info = vsi->info;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002790 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2791 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002792 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002793 "add pvid failed, err %s aq_err %s\n",
2794 i40e_stat_str(&vsi->back->hw, ret),
2795 i40e_aq_str(&vsi->back->hw,
2796 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002797 return -ENOENT;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002798 }
2799
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002800 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002801}
2802
2803/**
2804 * i40e_vsi_remove_pvid - Remove the pvid from the VSI
2805 * @vsi: the vsi being adjusted
2806 *
2807 * Just use the vlan_rx_register() service to put it back to normal
2808 **/
2809void i40e_vsi_remove_pvid(struct i40e_vsi *vsi)
2810{
Greg Rose6c12fcb2013-11-28 06:39:34 +00002811 i40e_vlan_stripping_disable(vsi);
2812
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002813 vsi->info.pvid = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002814}
2815
2816/**
2817 * i40e_vsi_setup_tx_resources - Allocate VSI Tx queue resources
2818 * @vsi: ptr to the VSI
2819 *
2820 * If this function returns with an error, then it's possible one or
2821 * more of the rings is populated (while the rest are not). It is the
2822 * callers duty to clean those orphaned rings.
2823 *
2824 * Return 0 on success, negative on failure
2825 **/
2826static int i40e_vsi_setup_tx_resources(struct i40e_vsi *vsi)
2827{
2828 int i, err = 0;
2829
2830 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e152013-09-28 06:00:58 +00002831 err = i40e_setup_tx_descriptors(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002832
Björn Töpel74608d12017-05-24 07:55:35 +02002833 if (!i40e_enabled_xdp_vsi(vsi))
2834 return err;
2835
2836 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
2837 err = i40e_setup_tx_descriptors(vsi->xdp_rings[i]);
2838
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002839 return err;
2840}
2841
2842/**
2843 * i40e_vsi_free_tx_resources - Free Tx resources for VSI queues
2844 * @vsi: ptr to the VSI
2845 *
2846 * Free VSI's transmit software resources
2847 **/
2848static void i40e_vsi_free_tx_resources(struct i40e_vsi *vsi)
2849{
2850 int i;
2851
Björn Töpel74608d12017-05-24 07:55:35 +02002852 if (vsi->tx_rings) {
2853 for (i = 0; i < vsi->num_queue_pairs; i++)
2854 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
2855 i40e_free_tx_resources(vsi->tx_rings[i]);
2856 }
Greg Rose8e9dca52013-12-18 13:45:53 +00002857
Björn Töpel74608d12017-05-24 07:55:35 +02002858 if (vsi->xdp_rings) {
2859 for (i = 0; i < vsi->num_queue_pairs; i++)
2860 if (vsi->xdp_rings[i] && vsi->xdp_rings[i]->desc)
2861 i40e_free_tx_resources(vsi->xdp_rings[i]);
2862 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002863}
2864
2865/**
2866 * i40e_vsi_setup_rx_resources - Allocate VSI queues Rx resources
2867 * @vsi: ptr to the VSI
2868 *
2869 * If this function returns with an error, then it's possible one or
2870 * more of the rings is populated (while the rest are not). It is the
2871 * callers duty to clean those orphaned rings.
2872 *
2873 * Return 0 on success, negative on failure
2874 **/
2875static int i40e_vsi_setup_rx_resources(struct i40e_vsi *vsi)
2876{
2877 int i, err = 0;
2878
2879 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e152013-09-28 06:00:58 +00002880 err = i40e_setup_rx_descriptors(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002881 return err;
2882}
2883
2884/**
2885 * i40e_vsi_free_rx_resources - Free Rx Resources for VSI queues
2886 * @vsi: ptr to the VSI
2887 *
2888 * Free all receive software resources
2889 **/
2890static void i40e_vsi_free_rx_resources(struct i40e_vsi *vsi)
2891{
2892 int i;
2893
Greg Rose8e9dca52013-12-18 13:45:53 +00002894 if (!vsi->rx_rings)
2895 return;
2896
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002897 for (i = 0; i < vsi->num_queue_pairs; i++)
Greg Rose8e9dca52013-12-18 13:45:53 +00002898 if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
Alexander Duyck9f65e152013-09-28 06:00:58 +00002899 i40e_free_rx_resources(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002900}
2901
2902/**
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002903 * i40e_config_xps_tx_ring - Configure XPS for a Tx ring
2904 * @ring: The Tx ring to configure
2905 *
2906 * This enables/disables XPS for a given Tx descriptor ring
2907 * based on the TCs enabled for the VSI that ring belongs to.
2908 **/
2909static void i40e_config_xps_tx_ring(struct i40e_ring *ring)
2910{
2911 struct i40e_vsi *vsi = ring->vsi;
2912 cpumask_var_t mask;
2913
Jesse Brandeburg9a660ee2015-02-26 16:13:22 +00002914 if (!ring->q_vector || !ring->netdev)
2915 return;
2916
2917 /* Single TC mode enable XPS */
2918 if (vsi->tc_config.numtc <= 1) {
2919 if (!test_and_set_bit(__I40E_TX_XPS_INIT_DONE, &ring->state))
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002920 netif_set_xps_queue(ring->netdev,
2921 &ring->q_vector->affinity_mask,
2922 ring->queue_index);
Jesse Brandeburg9a660ee2015-02-26 16:13:22 +00002923 } else if (alloc_cpumask_var(&mask, GFP_KERNEL)) {
2924 /* Disable XPS to allow selection based on TC */
2925 bitmap_zero(cpumask_bits(mask), nr_cpumask_bits);
2926 netif_set_xps_queue(ring->netdev, mask, ring->queue_index);
2927 free_cpumask_var(mask);
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002928 }
Jesse Brandeburg0e4425e2015-11-05 17:01:01 -08002929
2930 /* schedule our worker thread which will take care of
2931 * applying the new filter changes
2932 */
2933 i40e_service_event_schedule(vsi->back);
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002934}
2935
2936/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002937 * i40e_configure_tx_ring - Configure a transmit ring context and rest
2938 * @ring: The Tx ring to configure
2939 *
2940 * Configure the Tx descriptor ring in the HMC context.
2941 **/
2942static int i40e_configure_tx_ring(struct i40e_ring *ring)
2943{
2944 struct i40e_vsi *vsi = ring->vsi;
2945 u16 pf_q = vsi->base_queue + ring->queue_index;
2946 struct i40e_hw *hw = &vsi->back->hw;
2947 struct i40e_hmc_obj_txq tx_ctx;
2948 i40e_status err = 0;
2949 u32 qtx_ctl = 0;
2950
2951 /* some ATR related tx ring init */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08002952 if (vsi->back->flags & I40E_FLAG_FD_ATR_ENABLED) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002953 ring->atr_sample_rate = vsi->back->atr_sample_rate;
2954 ring->atr_count = 0;
2955 } else {
2956 ring->atr_sample_rate = 0;
2957 }
2958
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002959 /* configure XPS */
2960 i40e_config_xps_tx_ring(ring);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002961
2962 /* clear the context structure first */
2963 memset(&tx_ctx, 0, sizeof(tx_ctx));
2964
2965 tx_ctx.new_context = 1;
2966 tx_ctx.base = (ring->dma / 128);
2967 tx_ctx.qlen = ring->count;
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08002968 tx_ctx.fd_ena = !!(vsi->back->flags & (I40E_FLAG_FD_SB_ENABLED |
2969 I40E_FLAG_FD_ATR_ENABLED));
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00002970 tx_ctx.timesync_ena = !!(vsi->back->flags & I40E_FLAG_PTP);
Jesse Brandeburg1943d8b2014-02-14 02:14:40 +00002971 /* FDIR VSI tx ring can still use RS bit and writebacks */
2972 if (vsi->type != I40E_VSI_FDIR)
2973 tx_ctx.head_wb_ena = 1;
2974 tx_ctx.head_wb_addr = ring->dma +
2975 (ring->count * sizeof(struct i40e_tx_desc));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002976
2977 /* As part of VSI creation/update, FW allocates certain
2978 * Tx arbitration queue sets for each TC enabled for
2979 * the VSI. The FW returns the handles to these queue
2980 * sets as part of the response buffer to Add VSI,
2981 * Update VSI, etc. AQ commands. It is expected that
2982 * these queue set handles be associated with the Tx
2983 * queues by the driver as part of the TX queue context
2984 * initialization. This has to be done regardless of
2985 * DCB as by default everything is mapped to TC0.
2986 */
2987 tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[ring->dcb_tc]);
2988 tx_ctx.rdylist_act = 0;
2989
2990 /* clear the context in the HMC */
2991 err = i40e_clear_lan_tx_queue_context(hw, pf_q);
2992 if (err) {
2993 dev_info(&vsi->back->pdev->dev,
2994 "Failed to clear LAN Tx queue context on Tx ring %d (pf_q %d), error: %d\n",
2995 ring->queue_index, pf_q, err);
2996 return -ENOMEM;
2997 }
2998
2999 /* set the context in the HMC */
3000 err = i40e_set_lan_tx_queue_context(hw, pf_q, &tx_ctx);
3001 if (err) {
3002 dev_info(&vsi->back->pdev->dev,
3003 "Failed to set LAN Tx queue context on Tx ring %d (pf_q %d, error: %d\n",
3004 ring->queue_index, pf_q, err);
3005 return -ENOMEM;
3006 }
3007
3008 /* Now associate this queue with this PCI function */
Mitch Williams7a28d882014-10-17 03:14:52 +00003009 if (vsi->type == I40E_VSI_VMDQ2) {
Shannon Nelson9d8bf542014-01-14 00:49:50 -08003010 qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
Mitch Williams7a28d882014-10-17 03:14:52 +00003011 qtx_ctl |= ((vsi->id) << I40E_QTX_CTL_VFVM_INDX_SHIFT) &
3012 I40E_QTX_CTL_VFVM_INDX_MASK;
3013 } else {
Shannon Nelson9d8bf542014-01-14 00:49:50 -08003014 qtx_ctl = I40E_QTX_CTL_PF_QUEUE;
Mitch Williams7a28d882014-10-17 03:14:52 +00003015 }
3016
Shannon Nelson13fd9772013-09-28 07:14:19 +00003017 qtx_ctl |= ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT) &
3018 I40E_QTX_CTL_PF_INDX_MASK);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003019 wr32(hw, I40E_QTX_CTL(pf_q), qtx_ctl);
3020 i40e_flush(hw);
3021
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003022 /* cache tail off for easier writes later */
3023 ring->tail = hw->hw_addr + I40E_QTX_TAIL(pf_q);
3024
3025 return 0;
3026}
3027
3028/**
3029 * i40e_configure_rx_ring - Configure a receive ring context
3030 * @ring: The Rx ring to configure
3031 *
3032 * Configure the Rx descriptor ring in the HMC context.
3033 **/
3034static int i40e_configure_rx_ring(struct i40e_ring *ring)
3035{
3036 struct i40e_vsi *vsi = ring->vsi;
3037 u32 chain_len = vsi->back->hw.func_caps.rx_buf_chain_len;
3038 u16 pf_q = vsi->base_queue + ring->queue_index;
3039 struct i40e_hw *hw = &vsi->back->hw;
3040 struct i40e_hmc_obj_rxq rx_ctx;
3041 i40e_status err = 0;
3042
3043 ring->state = 0;
3044
3045 /* clear the context structure first */
3046 memset(&rx_ctx, 0, sizeof(rx_ctx));
3047
3048 ring->rx_buf_len = vsi->rx_buf_len;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003049
Alexander Duyckdab86af2017-03-14 10:15:27 -07003050 rx_ctx.dbuff = DIV_ROUND_UP(ring->rx_buf_len,
3051 BIT_ULL(I40E_RXQ_CTX_DBUFF_SHIFT));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003052
3053 rx_ctx.base = (ring->dma / 128);
3054 rx_ctx.qlen = ring->count;
3055
Jesse Brandeburgbec60fc2016-04-18 11:33:47 -07003056 /* use 32 byte descriptors */
3057 rx_ctx.dsize = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003058
Jesse Brandeburgbec60fc2016-04-18 11:33:47 -07003059 /* descriptor type is always zero
3060 * rx_ctx.dtype = 0;
3061 */
Jesse Brandeburgb32bfa172016-04-18 11:33:42 -07003062 rx_ctx.hsplit_0 = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003063
Jesse Brandeburgb32bfa172016-04-18 11:33:42 -07003064 rx_ctx.rxmax = min_t(u16, vsi->max_frame, chain_len * ring->rx_buf_len);
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00003065 if (hw->revision_id == 0)
3066 rx_ctx.lrxqthresh = 0;
3067 else
3068 rx_ctx.lrxqthresh = 2;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003069 rx_ctx.crcstrip = 1;
3070 rx_ctx.l2tsel = 1;
Jesse Brandeburgc4bbac32015-09-28 11:21:48 -07003071 /* this controls whether VLAN is stripped from inner headers */
3072 rx_ctx.showiv = 0;
Catherine Sullivanacb36762014-03-06 09:02:30 +00003073 /* set the prefena field to 1 because the manual says to */
3074 rx_ctx.prefena = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003075
3076 /* clear the context in the HMC */
3077 err = i40e_clear_lan_rx_queue_context(hw, pf_q);
3078 if (err) {
3079 dev_info(&vsi->back->pdev->dev,
3080 "Failed to clear LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
3081 ring->queue_index, pf_q, err);
3082 return -ENOMEM;
3083 }
3084
3085 /* set the context in the HMC */
3086 err = i40e_set_lan_rx_queue_context(hw, pf_q, &rx_ctx);
3087 if (err) {
3088 dev_info(&vsi->back->pdev->dev,
3089 "Failed to set LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
3090 ring->queue_index, pf_q, err);
3091 return -ENOMEM;
3092 }
3093
Alexander Duyckca9ec082017-04-05 07:51:02 -04003094 /* configure Rx buffer alignment */
3095 if (!vsi->netdev || (vsi->back->flags & I40E_FLAG_LEGACY_RX))
3096 clear_ring_build_skb_enabled(ring);
3097 else
3098 set_ring_build_skb_enabled(ring);
3099
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003100 /* cache tail for quicker writes, and clear the reg before use */
3101 ring->tail = hw->hw_addr + I40E_QRX_TAIL(pf_q);
3102 writel(0, ring->tail);
3103
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07003104 i40e_alloc_rx_buffers(ring, I40E_DESC_UNUSED(ring));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003105
3106 return 0;
3107}
3108
3109/**
3110 * i40e_vsi_configure_tx - Configure the VSI for Tx
3111 * @vsi: VSI structure describing this set of rings and resources
3112 *
3113 * Configure the Tx VSI for operation.
3114 **/
3115static int i40e_vsi_configure_tx(struct i40e_vsi *vsi)
3116{
3117 int err = 0;
3118 u16 i;
3119
Alexander Duyck9f65e152013-09-28 06:00:58 +00003120 for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
3121 err = i40e_configure_tx_ring(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003122
Björn Töpel74608d12017-05-24 07:55:35 +02003123 if (!i40e_enabled_xdp_vsi(vsi))
3124 return err;
3125
3126 for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
3127 err = i40e_configure_tx_ring(vsi->xdp_rings[i]);
3128
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003129 return err;
3130}
3131
3132/**
3133 * i40e_vsi_configure_rx - Configure the VSI for Rx
3134 * @vsi: the VSI being configured
3135 *
3136 * Configure the Rx VSI for operation.
3137 **/
3138static int i40e_vsi_configure_rx(struct i40e_vsi *vsi)
3139{
3140 int err = 0;
3141 u16 i;
3142
Alexander Duyckdab86af2017-03-14 10:15:27 -07003143 if (!vsi->netdev || (vsi->back->flags & I40E_FLAG_LEGACY_RX)) {
3144 vsi->max_frame = I40E_MAX_RXBUFFER;
3145 vsi->rx_buf_len = I40E_RXBUFFER_2048;
3146#if (PAGE_SIZE < 8192)
Alexander Duyckca9ec082017-04-05 07:51:02 -04003147 } else if (!I40E_2K_TOO_SMALL_WITH_PADDING &&
3148 (vsi->netdev->mtu <= ETH_DATA_LEN)) {
Alexander Duyckdab86af2017-03-14 10:15:27 -07003149 vsi->max_frame = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
3150 vsi->rx_buf_len = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
3151#endif
3152 } else {
3153 vsi->max_frame = I40E_MAX_RXBUFFER;
Alexander Duyck98efd692017-04-05 07:51:01 -04003154 vsi->rx_buf_len = (PAGE_SIZE < 8192) ? I40E_RXBUFFER_3072 :
3155 I40E_RXBUFFER_2048;
Alexander Duyckdab86af2017-03-14 10:15:27 -07003156 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003157
3158 /* set up individual rings */
3159 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e152013-09-28 06:00:58 +00003160 err = i40e_configure_rx_ring(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003161
3162 return err;
3163}
3164
3165/**
3166 * i40e_vsi_config_dcb_rings - Update rings to reflect DCB TC
3167 * @vsi: ptr to the VSI
3168 **/
3169static void i40e_vsi_config_dcb_rings(struct i40e_vsi *vsi)
3170{
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +00003171 struct i40e_ring *tx_ring, *rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003172 u16 qoffset, qcount;
3173 int i, n;
3174
Parikh, Neeravcd238a32015-02-21 06:43:37 +00003175 if (!(vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
3176 /* Reset the TC information */
3177 for (i = 0; i < vsi->num_queue_pairs; i++) {
3178 rx_ring = vsi->rx_rings[i];
3179 tx_ring = vsi->tx_rings[i];
3180 rx_ring->dcb_tc = 0;
3181 tx_ring->dcb_tc = 0;
3182 }
3183 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003184
3185 for (n = 0; n < I40E_MAX_TRAFFIC_CLASS; n++) {
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04003186 if (!(vsi->tc_config.enabled_tc & BIT_ULL(n)))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003187 continue;
3188
3189 qoffset = vsi->tc_config.tc_info[n].qoffset;
3190 qcount = vsi->tc_config.tc_info[n].qcount;
3191 for (i = qoffset; i < (qoffset + qcount); i++) {
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +00003192 rx_ring = vsi->rx_rings[i];
3193 tx_ring = vsi->tx_rings[i];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003194 rx_ring->dcb_tc = n;
3195 tx_ring->dcb_tc = n;
3196 }
3197 }
3198}
3199
3200/**
3201 * i40e_set_vsi_rx_mode - Call set_rx_mode on a VSI
3202 * @vsi: ptr to the VSI
3203 **/
3204static void i40e_set_vsi_rx_mode(struct i40e_vsi *vsi)
3205{
Tushar Daveb1b15df2016-07-01 10:11:20 -07003206 struct i40e_pf *pf = vsi->back;
3207 int err;
3208
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003209 if (vsi->netdev)
3210 i40e_set_rx_mode(vsi->netdev);
Tushar Daveb1b15df2016-07-01 10:11:20 -07003211
3212 if (!!(pf->flags & I40E_FLAG_PF_MAC)) {
3213 err = i40e_macaddr_init(vsi, pf->hw.mac.addr);
3214 if (err) {
3215 dev_warn(&pf->pdev->dev,
3216 "could not set up macaddr; err %d\n", err);
3217 }
3218 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003219}
3220
3221/**
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00003222 * i40e_fdir_filter_restore - Restore the Sideband Flow Director filters
3223 * @vsi: Pointer to the targeted VSI
3224 *
3225 * This function replays the hlist on the hw where all the SB Flow Director
3226 * filters were saved.
3227 **/
3228static void i40e_fdir_filter_restore(struct i40e_vsi *vsi)
3229{
3230 struct i40e_fdir_filter *filter;
3231 struct i40e_pf *pf = vsi->back;
3232 struct hlist_node *node;
3233
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00003234 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
3235 return;
3236
Jacob Keller6d069422017-02-06 14:38:44 -08003237 /* Reset FDir counters as we're replaying all existing filters */
Jacob Keller097dbf52017-02-06 14:38:46 -08003238 pf->fd_tcp4_filter_cnt = 0;
3239 pf->fd_udp4_filter_cnt = 0;
Jacob Kellerf223c872017-02-06 14:38:51 -08003240 pf->fd_sctp4_filter_cnt = 0;
Jacob Keller097dbf52017-02-06 14:38:46 -08003241 pf->fd_ip4_filter_cnt = 0;
Jacob Keller6d069422017-02-06 14:38:44 -08003242
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00003243 hlist_for_each_entry_safe(filter, node,
3244 &pf->fdir_filter_list, fdir_node) {
3245 i40e_add_del_fdir(vsi, filter, true);
3246 }
3247}
3248
3249/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003250 * i40e_vsi_configure - Set up the VSI for action
3251 * @vsi: the VSI being configured
3252 **/
3253static int i40e_vsi_configure(struct i40e_vsi *vsi)
3254{
3255 int err;
3256
3257 i40e_set_vsi_rx_mode(vsi);
3258 i40e_restore_vlan(vsi);
3259 i40e_vsi_config_dcb_rings(vsi);
3260 err = i40e_vsi_configure_tx(vsi);
3261 if (!err)
3262 err = i40e_vsi_configure_rx(vsi);
3263
3264 return err;
3265}
3266
3267/**
3268 * i40e_vsi_configure_msix - MSIX mode Interrupt Config in the HW
3269 * @vsi: the VSI being configured
3270 **/
3271static void i40e_vsi_configure_msix(struct i40e_vsi *vsi)
3272{
Björn Töpel74608d12017-05-24 07:55:35 +02003273 bool has_xdp = i40e_enabled_xdp_vsi(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003274 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003275 struct i40e_hw *hw = &pf->hw;
3276 u16 vector;
3277 int i, q;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003278 u32 qp;
3279
3280 /* The interrupt indexing is offset by 1 in the PFINT_ITRn
3281 * and PFINT_LNKLSTn registers, e.g.:
3282 * PFINT_ITRn[0..n-1] gets msix-1..msix-n (qpair interrupts)
3283 */
3284 qp = vsi->base_queue;
3285 vector = vsi->base_vector;
Alexander Duyck493fb302013-09-28 07:01:44 +00003286 for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
Jesse Brandeburgac26fc12015-09-28 14:12:37 -04003287 struct i40e_q_vector *q_vector = vsi->q_vectors[i];
3288
Jesse Brandeburgee2319c2015-09-28 14:16:54 -04003289 q_vector->itr_countdown = ITR_COUNTDOWN_START;
Kan Lianga75e8002016-02-19 09:24:04 -05003290 q_vector->rx.itr = ITR_TO_REG(vsi->rx_rings[i]->rx_itr_setting);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003291 q_vector->rx.latency_range = I40E_LOW_LATENCY;
3292 wr32(hw, I40E_PFINT_ITRN(I40E_RX_ITR, vector - 1),
3293 q_vector->rx.itr);
Kan Lianga75e8002016-02-19 09:24:04 -05003294 q_vector->tx.itr = ITR_TO_REG(vsi->tx_rings[i]->tx_itr_setting);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003295 q_vector->tx.latency_range = I40E_LOW_LATENCY;
3296 wr32(hw, I40E_PFINT_ITRN(I40E_TX_ITR, vector - 1),
3297 q_vector->tx.itr);
Jesse Brandeburgac26fc12015-09-28 14:12:37 -04003298 wr32(hw, I40E_PFINT_RATEN(vector - 1),
Alan Brady1c0e6a32016-11-28 16:06:02 -08003299 i40e_intrl_usec_to_reg(vsi->int_rate_limit));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003300
3301 /* Linked list for the queuepairs assigned to this vector */
3302 wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), qp);
3303 for (q = 0; q < q_vector->num_ringpairs; q++) {
Björn Töpel74608d12017-05-24 07:55:35 +02003304 u32 nextqp = has_xdp ? qp + vsi->alloc_queue_pairs : qp;
Jesse Brandeburgac26fc12015-09-28 14:12:37 -04003305 u32 val;
3306
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003307 val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
Björn Töpel74608d12017-05-24 07:55:35 +02003308 (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
3309 (vector << I40E_QINT_RQCTL_MSIX_INDX_SHIFT) |
3310 (nextqp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT) |
3311 (I40E_QUEUE_TYPE_TX <<
3312 I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003313
3314 wr32(hw, I40E_QINT_RQCTL(qp), val);
3315
Björn Töpel74608d12017-05-24 07:55:35 +02003316 if (has_xdp) {
3317 val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3318 (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3319 (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
3320 (qp << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) |
3321 (I40E_QUEUE_TYPE_TX <<
3322 I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3323
3324 wr32(hw, I40E_QINT_TQCTL(nextqp), val);
3325 }
3326
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003327 val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
Björn Töpel74608d12017-05-24 07:55:35 +02003328 (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3329 (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
3330 ((qp + 1) << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) |
3331 (I40E_QUEUE_TYPE_RX <<
3332 I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003333
3334 /* Terminate the linked list */
3335 if (q == (q_vector->num_ringpairs - 1))
Björn Töpel74608d12017-05-24 07:55:35 +02003336 val |= (I40E_QUEUE_END_OF_LIST <<
3337 I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003338
3339 wr32(hw, I40E_QINT_TQCTL(qp), val);
3340 qp++;
3341 }
3342 }
3343
3344 i40e_flush(hw);
3345}
3346
3347/**
3348 * i40e_enable_misc_int_causes - enable the non-queue interrupts
3349 * @hw: ptr to the hardware info
3350 **/
Jacob Kellerab437b52014-12-14 01:55:08 +00003351static void i40e_enable_misc_int_causes(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003352{
Jacob Kellerab437b52014-12-14 01:55:08 +00003353 struct i40e_hw *hw = &pf->hw;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003354 u32 val;
3355
3356 /* clear things first */
3357 wr32(hw, I40E_PFINT_ICR0_ENA, 0); /* disable all */
3358 rd32(hw, I40E_PFINT_ICR0); /* read to clear */
3359
3360 val = I40E_PFINT_ICR0_ENA_ECC_ERR_MASK |
3361 I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK |
3362 I40E_PFINT_ICR0_ENA_GRST_MASK |
3363 I40E_PFINT_ICR0_ENA_PCI_EXCEPTION_MASK |
3364 I40E_PFINT_ICR0_ENA_GPIO_MASK |
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003365 I40E_PFINT_ICR0_ENA_HMC_ERR_MASK |
3366 I40E_PFINT_ICR0_ENA_VFLR_MASK |
3367 I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
3368
Anjali Singhai Jain0d8e1432015-06-05 12:20:32 -04003369 if (pf->flags & I40E_FLAG_IWARP_ENABLED)
3370 val |= I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
3371
Jacob Kellerab437b52014-12-14 01:55:08 +00003372 if (pf->flags & I40E_FLAG_PTP)
3373 val |= I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
3374
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003375 wr32(hw, I40E_PFINT_ICR0_ENA, val);
3376
3377 /* SW_ITR_IDX = 0, but don't change INTENA */
Anjali Singhai Jain84ed40e2013-11-26 10:49:32 +00003378 wr32(hw, I40E_PFINT_DYN_CTL0, I40E_PFINT_DYN_CTL0_SW_ITR_INDX_MASK |
3379 I40E_PFINT_DYN_CTL0_INTENA_MSK_MASK);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003380
3381 /* OTHER_ITR_IDX = 0 */
3382 wr32(hw, I40E_PFINT_STAT_CTL0, 0);
3383}
3384
3385/**
3386 * i40e_configure_msi_and_legacy - Legacy mode interrupt config in the HW
3387 * @vsi: the VSI being configured
3388 **/
3389static void i40e_configure_msi_and_legacy(struct i40e_vsi *vsi)
3390{
Björn Töpel74608d12017-05-24 07:55:35 +02003391 u32 nextqp = i40e_enabled_xdp_vsi(vsi) ? vsi->alloc_queue_pairs : 0;
Alexander Duyck493fb302013-09-28 07:01:44 +00003392 struct i40e_q_vector *q_vector = vsi->q_vectors[0];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003393 struct i40e_pf *pf = vsi->back;
3394 struct i40e_hw *hw = &pf->hw;
3395 u32 val;
3396
3397 /* set the ITR configuration */
Jesse Brandeburgee2319c2015-09-28 14:16:54 -04003398 q_vector->itr_countdown = ITR_COUNTDOWN_START;
Kan Lianga75e8002016-02-19 09:24:04 -05003399 q_vector->rx.itr = ITR_TO_REG(vsi->rx_rings[0]->rx_itr_setting);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003400 q_vector->rx.latency_range = I40E_LOW_LATENCY;
3401 wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), q_vector->rx.itr);
Kan Lianga75e8002016-02-19 09:24:04 -05003402 q_vector->tx.itr = ITR_TO_REG(vsi->tx_rings[0]->tx_itr_setting);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003403 q_vector->tx.latency_range = I40E_LOW_LATENCY;
3404 wr32(hw, I40E_PFINT_ITR0(I40E_TX_ITR), q_vector->tx.itr);
3405
Jacob Kellerab437b52014-12-14 01:55:08 +00003406 i40e_enable_misc_int_causes(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003407
3408 /* FIRSTQ_INDX = 0, FIRSTQ_TYPE = 0 (rx) */
3409 wr32(hw, I40E_PFINT_LNKLST0, 0);
3410
Jesse Brandeburgf29eaa32014-02-11 08:24:12 +00003411 /* Associate the queue pair to the vector and enable the queue int */
Björn Töpel74608d12017-05-24 07:55:35 +02003412 val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3413 (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
3414 (nextqp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT)|
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003415 (I40E_QUEUE_TYPE_TX << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3416
3417 wr32(hw, I40E_QINT_RQCTL(0), val);
3418
Björn Töpel74608d12017-05-24 07:55:35 +02003419 if (i40e_enabled_xdp_vsi(vsi)) {
3420 val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3421 (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT)|
3422 (I40E_QUEUE_TYPE_TX
3423 << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3424
3425 wr32(hw, I40E_QINT_TQCTL(nextqp), val);
3426 }
3427
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003428 val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3429 (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3430 (I40E_QUEUE_END_OF_LIST << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
3431
3432 wr32(hw, I40E_QINT_TQCTL(0), val);
3433 i40e_flush(hw);
3434}
3435
3436/**
Mitch Williams2ef28cf2013-11-28 06:39:32 +00003437 * i40e_irq_dynamic_disable_icr0 - Disable default interrupt generation for icr0
3438 * @pf: board private structure
3439 **/
3440void i40e_irq_dynamic_disable_icr0(struct i40e_pf *pf)
3441{
3442 struct i40e_hw *hw = &pf->hw;
3443
3444 wr32(hw, I40E_PFINT_DYN_CTL0,
3445 I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
3446 i40e_flush(hw);
3447}
3448
3449/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003450 * i40e_irq_dynamic_enable_icr0 - Enable default interrupt generation for icr0
3451 * @pf: board private structure
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08003452 * @clearpba: true when all pending interrupt events should be cleared
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003453 **/
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08003454void i40e_irq_dynamic_enable_icr0(struct i40e_pf *pf, bool clearpba)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003455{
3456 struct i40e_hw *hw = &pf->hw;
3457 u32 val;
3458
3459 val = I40E_PFINT_DYN_CTL0_INTENA_MASK |
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08003460 (clearpba ? I40E_PFINT_DYN_CTL0_CLEARPBA_MASK : 0) |
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003461 (I40E_ITR_NONE << I40E_PFINT_DYN_CTL0_ITR_INDX_SHIFT);
3462
3463 wr32(hw, I40E_PFINT_DYN_CTL0, val);
3464 i40e_flush(hw);
3465}
3466
3467/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003468 * i40e_msix_clean_rings - MSIX mode Interrupt Handler
3469 * @irq: interrupt number
3470 * @data: pointer to a q_vector
3471 **/
3472static irqreturn_t i40e_msix_clean_rings(int irq, void *data)
3473{
3474 struct i40e_q_vector *q_vector = data;
3475
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003476 if (!q_vector->tx.ring && !q_vector->rx.ring)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003477 return IRQ_HANDLED;
3478
Alexander Duyck5d3465a2015-09-29 15:19:50 -07003479 napi_schedule_irqoff(&q_vector->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003480
3481 return IRQ_HANDLED;
3482}
3483
3484/**
Alan Brady96db7762016-09-14 16:24:38 -07003485 * i40e_irq_affinity_notify - Callback for affinity changes
3486 * @notify: context as to what irq was changed
3487 * @mask: the new affinity mask
3488 *
3489 * This is a callback function used by the irq_set_affinity_notifier function
3490 * so that we may register to receive changes to the irq affinity masks.
3491 **/
3492static void i40e_irq_affinity_notify(struct irq_affinity_notify *notify,
3493 const cpumask_t *mask)
3494{
3495 struct i40e_q_vector *q_vector =
3496 container_of(notify, struct i40e_q_vector, affinity_notify);
3497
3498 q_vector->affinity_mask = *mask;
3499}
3500
3501/**
3502 * i40e_irq_affinity_release - Callback for affinity notifier release
3503 * @ref: internal core kernel usage
3504 *
3505 * This is a callback function used by the irq_set_affinity_notifier function
3506 * to inform the current notification subscriber that they will no longer
3507 * receive notifications.
3508 **/
3509static void i40e_irq_affinity_release(struct kref *ref) {}
3510
3511/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003512 * i40e_vsi_request_irq_msix - Initialize MSI-X interrupts
3513 * @vsi: the VSI being configured
3514 * @basename: name for the vector
3515 *
3516 * Allocates MSI-X vectors and requests interrupts from the kernel.
3517 **/
3518static int i40e_vsi_request_irq_msix(struct i40e_vsi *vsi, char *basename)
3519{
3520 int q_vectors = vsi->num_q_vectors;
3521 struct i40e_pf *pf = vsi->back;
3522 int base = vsi->base_vector;
3523 int rx_int_idx = 0;
3524 int tx_int_idx = 0;
3525 int vector, err;
Alan Brady96db7762016-09-14 16:24:38 -07003526 int irq_num;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003527
3528 for (vector = 0; vector < q_vectors; vector++) {
Alexander Duyck493fb302013-09-28 07:01:44 +00003529 struct i40e_q_vector *q_vector = vsi->q_vectors[vector];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003530
Alan Brady96db7762016-09-14 16:24:38 -07003531 irq_num = pf->msix_entries[base + vector].vector;
3532
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003533 if (q_vector->tx.ring && q_vector->rx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003534 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3535 "%s-%s-%d", basename, "TxRx", rx_int_idx++);
3536 tx_int_idx++;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003537 } else if (q_vector->rx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003538 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3539 "%s-%s-%d", basename, "rx", rx_int_idx++);
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003540 } else if (q_vector->tx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003541 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3542 "%s-%s-%d", basename, "tx", tx_int_idx++);
3543 } else {
3544 /* skip this unused q_vector */
3545 continue;
3546 }
Alan Brady96db7762016-09-14 16:24:38 -07003547 err = request_irq(irq_num,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003548 vsi->irq_handler,
3549 0,
3550 q_vector->name,
3551 q_vector);
3552 if (err) {
3553 dev_info(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04003554 "MSIX request_irq failed, error: %d\n", err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003555 goto free_queue_irqs;
3556 }
Alan Brady96db7762016-09-14 16:24:38 -07003557
3558 /* register for affinity change notifications */
3559 q_vector->affinity_notify.notify = i40e_irq_affinity_notify;
3560 q_vector->affinity_notify.release = i40e_irq_affinity_release;
3561 irq_set_affinity_notifier(irq_num, &q_vector->affinity_notify);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003562 /* assign the mask for this irq */
Alan Brady96db7762016-09-14 16:24:38 -07003563 irq_set_affinity_hint(irq_num, &q_vector->affinity_mask);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003564 }
3565
Shannon Nelson63741842014-04-23 04:50:16 +00003566 vsi->irqs_ready = true;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003567 return 0;
3568
3569free_queue_irqs:
3570 while (vector) {
3571 vector--;
Alan Brady96db7762016-09-14 16:24:38 -07003572 irq_num = pf->msix_entries[base + vector].vector;
3573 irq_set_affinity_notifier(irq_num, NULL);
3574 irq_set_affinity_hint(irq_num, NULL);
3575 free_irq(irq_num, &vsi->q_vectors[vector]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003576 }
3577 return err;
3578}
3579
3580/**
3581 * i40e_vsi_disable_irq - Mask off queue interrupt generation on the VSI
3582 * @vsi: the VSI being un-configured
3583 **/
3584static void i40e_vsi_disable_irq(struct i40e_vsi *vsi)
3585{
3586 struct i40e_pf *pf = vsi->back;
3587 struct i40e_hw *hw = &pf->hw;
3588 int base = vsi->base_vector;
3589 int i;
3590
Shannon Nelson2e5c26ea2017-06-07 05:43:11 -04003591 /* disable interrupt causation from each queue */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003592 for (i = 0; i < vsi->num_queue_pairs; i++) {
Shannon Nelson2e5c26ea2017-06-07 05:43:11 -04003593 u32 val;
3594
3595 val = rd32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx));
3596 val &= ~I40E_QINT_TQCTL_CAUSE_ENA_MASK;
3597 wr32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx), val);
3598
3599 val = rd32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx));
3600 val &= ~I40E_QINT_RQCTL_CAUSE_ENA_MASK;
3601 wr32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx), val);
3602
Björn Töpel74608d12017-05-24 07:55:35 +02003603 if (!i40e_enabled_xdp_vsi(vsi))
3604 continue;
3605 wr32(hw, I40E_QINT_TQCTL(vsi->xdp_rings[i]->reg_idx), 0);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003606 }
3607
Shannon Nelson2e5c26ea2017-06-07 05:43:11 -04003608 /* disable each interrupt */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003609 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3610 for (i = vsi->base_vector;
3611 i < (vsi->num_q_vectors + vsi->base_vector); i++)
3612 wr32(hw, I40E_PFINT_DYN_CTLN(i - 1), 0);
3613
3614 i40e_flush(hw);
3615 for (i = 0; i < vsi->num_q_vectors; i++)
3616 synchronize_irq(pf->msix_entries[i + base].vector);
3617 } else {
3618 /* Legacy and MSI mode - this stops all interrupt handling */
3619 wr32(hw, I40E_PFINT_ICR0_ENA, 0);
3620 wr32(hw, I40E_PFINT_DYN_CTL0, 0);
3621 i40e_flush(hw);
3622 synchronize_irq(pf->pdev->irq);
3623 }
3624}
3625
3626/**
3627 * i40e_vsi_enable_irq - Enable IRQ for the given VSI
3628 * @vsi: the VSI being configured
3629 **/
3630static int i40e_vsi_enable_irq(struct i40e_vsi *vsi)
3631{
3632 struct i40e_pf *pf = vsi->back;
3633 int i;
3634
3635 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
Jesse Brandeburg78455482015-07-23 16:54:41 -04003636 for (i = 0; i < vsi->num_q_vectors; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003637 i40e_irq_dynamic_enable(vsi, i);
3638 } else {
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08003639 i40e_irq_dynamic_enable_icr0(pf, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003640 }
3641
Jesse Brandeburg1022cb62013-09-28 07:13:08 +00003642 i40e_flush(&pf->hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003643 return 0;
3644}
3645
3646/**
3647 * i40e_stop_misc_vector - Stop the vector that handles non-queue events
3648 * @pf: board private structure
3649 **/
3650static void i40e_stop_misc_vector(struct i40e_pf *pf)
3651{
3652 /* Disable ICR 0 */
3653 wr32(&pf->hw, I40E_PFINT_ICR0_ENA, 0);
3654 i40e_flush(&pf->hw);
3655}
3656
3657/**
3658 * i40e_intr - MSI/Legacy and non-queue interrupt handler
3659 * @irq: interrupt number
3660 * @data: pointer to a q_vector
3661 *
3662 * This is the handler used for all MSI/Legacy interrupts, and deals
3663 * with both queue and non-queue interrupts. This is also used in
3664 * MSIX mode to handle the non-queue interrupts.
3665 **/
3666static irqreturn_t i40e_intr(int irq, void *data)
3667{
3668 struct i40e_pf *pf = (struct i40e_pf *)data;
3669 struct i40e_hw *hw = &pf->hw;
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003670 irqreturn_t ret = IRQ_NONE;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003671 u32 icr0, icr0_remaining;
3672 u32 val, ena_mask;
3673
3674 icr0 = rd32(hw, I40E_PFINT_ICR0);
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003675 ena_mask = rd32(hw, I40E_PFINT_ICR0_ENA);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003676
Shannon Nelson116a57d2013-09-28 07:13:59 +00003677 /* if sharing a legacy IRQ, we might get called w/o an intr pending */
3678 if ((icr0 & I40E_PFINT_ICR0_INTEVENT_MASK) == 0)
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003679 goto enable_intr;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003680
Shannon Nelsoncd92e722013-11-16 10:00:44 +00003681 /* if interrupt but no bits showing, must be SWINT */
3682 if (((icr0 & ~I40E_PFINT_ICR0_INTEVENT_MASK) == 0) ||
3683 (icr0 & I40E_PFINT_ICR0_SWINT_MASK))
3684 pf->sw_int_count++;
3685
Anjali Singhai Jain0d8e1432015-06-05 12:20:32 -04003686 if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
Catherine Sullivan76429842017-06-07 05:43:12 -04003687 (icr0 & I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK)) {
Anjali Singhai Jain0d8e1432015-06-05 12:20:32 -04003688 ena_mask &= ~I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
Carolyn Wyborny23bb6dc2016-11-08 13:05:12 -08003689 dev_dbg(&pf->pdev->dev, "cleared PE_CRITERR\n");
Catherine Sullivan76429842017-06-07 05:43:12 -04003690 set_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
Anjali Singhai Jain0d8e1432015-06-05 12:20:32 -04003691 }
3692
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003693 /* only q0 is used in MSI/Legacy mode, and none are used in MSIX */
3694 if (icr0 & I40E_PFINT_ICR0_QUEUE_0_MASK) {
Alexander Duyck5d3465a2015-09-29 15:19:50 -07003695 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
3696 struct i40e_q_vector *q_vector = vsi->q_vectors[0];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003697
Anjali Singhai Jaina16ae2d2016-01-15 14:33:16 -08003698 /* We do not have a way to disarm Queue causes while leaving
3699 * interrupt enabled for all other causes, ideally
3700 * interrupt should be disabled while we are in NAPI but
3701 * this is not a performance path and napi_schedule()
3702 * can deal with rescheduling.
3703 */
Mauro S. M. Rodrigues9e6c9c02017-05-12 23:26:56 -03003704 if (!test_bit(__I40E_DOWN, pf->state))
Alexander Duyck5d3465a2015-09-29 15:19:50 -07003705 napi_schedule_irqoff(&q_vector->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003706 }
3707
3708 if (icr0 & I40E_PFINT_ICR0_ADMINQ_MASK) {
3709 ena_mask &= ~I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
Jacob Keller0da36b92017-04-19 09:25:55 -04003710 set_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state);
Shannon Nelson6e93d0c2016-01-15 14:33:18 -08003711 i40e_debug(&pf->hw, I40E_DEBUG_NVM, "AdminQ event\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003712 }
3713
3714 if (icr0 & I40E_PFINT_ICR0_MAL_DETECT_MASK) {
3715 ena_mask &= ~I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
Jacob Keller0da36b92017-04-19 09:25:55 -04003716 set_bit(__I40E_MDD_EVENT_PENDING, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003717 }
3718
3719 if (icr0 & I40E_PFINT_ICR0_VFLR_MASK) {
3720 ena_mask &= ~I40E_PFINT_ICR0_ENA_VFLR_MASK;
Jacob Keller0da36b92017-04-19 09:25:55 -04003721 set_bit(__I40E_VFLR_EVENT_PENDING, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003722 }
3723
3724 if (icr0 & I40E_PFINT_ICR0_GRST_MASK) {
Jacob Keller0da36b92017-04-19 09:25:55 -04003725 if (!test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
3726 set_bit(__I40E_RESET_INTR_RECEIVED, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003727 ena_mask &= ~I40E_PFINT_ICR0_ENA_GRST_MASK;
3728 val = rd32(hw, I40E_GLGEN_RSTAT);
3729 val = (val & I40E_GLGEN_RSTAT_RESET_TYPE_MASK)
3730 >> I40E_GLGEN_RSTAT_RESET_TYPE_SHIFT;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003731 if (val == I40E_RESET_CORER) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003732 pf->corer_count++;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003733 } else if (val == I40E_RESET_GLOBR) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003734 pf->globr_count++;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003735 } else if (val == I40E_RESET_EMPR) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003736 pf->empr_count++;
Jacob Keller0da36b92017-04-19 09:25:55 -04003737 set_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state);
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003738 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003739 }
3740
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003741 if (icr0 & I40E_PFINT_ICR0_HMC_ERR_MASK) {
3742 icr0 &= ~I40E_PFINT_ICR0_HMC_ERR_MASK;
3743 dev_info(&pf->pdev->dev, "HMC error interrupt\n");
Anjali Singhai Jain25fc0e62015-03-31 00:45:01 -07003744 dev_info(&pf->pdev->dev, "HMC error info 0x%x, HMC error data 0x%x\n",
3745 rd32(hw, I40E_PFHMC_ERRORINFO),
3746 rd32(hw, I40E_PFHMC_ERRORDATA));
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003747 }
3748
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003749 if (icr0 & I40E_PFINT_ICR0_TIMESYNC_MASK) {
3750 u32 prttsyn_stat = rd32(hw, I40E_PRTTSYN_STAT_0);
3751
3752 if (prttsyn_stat & I40E_PRTTSYN_STAT_0_TXTIME_MASK) {
Jacob Kellercafa1fc2014-04-24 18:05:03 -07003753 icr0 &= ~I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003754 i40e_ptp_tx_hwtstamp(pf);
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003755 }
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003756 }
3757
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003758 /* If a critical error is pending we have no choice but to reset the
3759 * device.
3760 * Report and mask out any remaining unexpected interrupts.
3761 */
3762 icr0_remaining = icr0 & ena_mask;
3763 if (icr0_remaining) {
3764 dev_info(&pf->pdev->dev, "unhandled interrupt icr0=0x%08x\n",
3765 icr0_remaining);
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003766 if ((icr0_remaining & I40E_PFINT_ICR0_PE_CRITERR_MASK) ||
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003767 (icr0_remaining & I40E_PFINT_ICR0_PCI_EXCEPTION_MASK) ||
Anjali Singhai Jainc0c28972014-02-12 01:45:34 +00003768 (icr0_remaining & I40E_PFINT_ICR0_ECC_ERR_MASK)) {
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003769 dev_info(&pf->pdev->dev, "device will be reset\n");
Jacob Keller0da36b92017-04-19 09:25:55 -04003770 set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003771 i40e_service_event_schedule(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003772 }
3773 ena_mask &= ~icr0_remaining;
3774 }
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003775 ret = IRQ_HANDLED;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003776
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003777enable_intr:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003778 /* re-enable interrupt causes */
3779 wr32(hw, I40E_PFINT_ICR0_ENA, ena_mask);
Mauro S. M. Rodrigues9e6c9c02017-05-12 23:26:56 -03003780 if (!test_bit(__I40E_DOWN, pf->state)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003781 i40e_service_event_schedule(pf);
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08003782 i40e_irq_dynamic_enable_icr0(pf, false);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003783 }
3784
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003785 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003786}
3787
3788/**
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003789 * i40e_clean_fdir_tx_irq - Reclaim resources after transmit completes
3790 * @tx_ring: tx ring to clean
3791 * @budget: how many cleans we're allowed
3792 *
3793 * Returns true if there's any budget left (e.g. the clean is finished)
3794 **/
3795static bool i40e_clean_fdir_tx_irq(struct i40e_ring *tx_ring, int budget)
3796{
3797 struct i40e_vsi *vsi = tx_ring->vsi;
3798 u16 i = tx_ring->next_to_clean;
3799 struct i40e_tx_buffer *tx_buf;
3800 struct i40e_tx_desc *tx_desc;
3801
3802 tx_buf = &tx_ring->tx_bi[i];
3803 tx_desc = I40E_TX_DESC(tx_ring, i);
3804 i -= tx_ring->count;
3805
3806 do {
3807 struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
3808
3809 /* if next_to_watch is not set then there is no work pending */
3810 if (!eop_desc)
3811 break;
3812
3813 /* prevent any other reads prior to eop_desc */
3814 read_barrier_depends();
3815
3816 /* if the descriptor isn't done, no work yet to do */
3817 if (!(eop_desc->cmd_type_offset_bsz &
3818 cpu_to_le64(I40E_TX_DESC_DTYPE_DESC_DONE)))
3819 break;
3820
3821 /* clear next_to_watch to prevent false hangs */
3822 tx_buf->next_to_watch = NULL;
3823
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003824 tx_desc->buffer_addr = 0;
3825 tx_desc->cmd_type_offset_bsz = 0;
3826 /* move past filter desc */
3827 tx_buf++;
3828 tx_desc++;
3829 i++;
3830 if (unlikely(!i)) {
3831 i -= tx_ring->count;
3832 tx_buf = tx_ring->tx_bi;
3833 tx_desc = I40E_TX_DESC(tx_ring, 0);
3834 }
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003835 /* unmap skb header data */
3836 dma_unmap_single(tx_ring->dev,
3837 dma_unmap_addr(tx_buf, dma),
3838 dma_unmap_len(tx_buf, len),
3839 DMA_TO_DEVICE);
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003840 if (tx_buf->tx_flags & I40E_TX_FLAGS_FD_SB)
3841 kfree(tx_buf->raw_buf);
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003842
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003843 tx_buf->raw_buf = NULL;
3844 tx_buf->tx_flags = 0;
3845 tx_buf->next_to_watch = NULL;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003846 dma_unmap_len_set(tx_buf, len, 0);
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003847 tx_desc->buffer_addr = 0;
3848 tx_desc->cmd_type_offset_bsz = 0;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003849
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003850 /* move us past the eop_desc for start of next FD desc */
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003851 tx_buf++;
3852 tx_desc++;
3853 i++;
3854 if (unlikely(!i)) {
3855 i -= tx_ring->count;
3856 tx_buf = tx_ring->tx_bi;
3857 tx_desc = I40E_TX_DESC(tx_ring, 0);
3858 }
3859
3860 /* update budget accounting */
3861 budget--;
3862 } while (likely(budget));
3863
3864 i += tx_ring->count;
3865 tx_ring->next_to_clean = i;
3866
Jesse Brandeburg6995b362015-08-28 17:55:54 -04003867 if (vsi->back->flags & I40E_FLAG_MSIX_ENABLED)
Jesse Brandeburg78455482015-07-23 16:54:41 -04003868 i40e_irq_dynamic_enable(vsi, tx_ring->q_vector->v_idx);
Jesse Brandeburg6995b362015-08-28 17:55:54 -04003869
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003870 return budget > 0;
3871}
3872
3873/**
3874 * i40e_fdir_clean_ring - Interrupt Handler for FDIR SB ring
3875 * @irq: interrupt number
3876 * @data: pointer to a q_vector
3877 **/
3878static irqreturn_t i40e_fdir_clean_ring(int irq, void *data)
3879{
3880 struct i40e_q_vector *q_vector = data;
3881 struct i40e_vsi *vsi;
3882
3883 if (!q_vector->tx.ring)
3884 return IRQ_HANDLED;
3885
3886 vsi = q_vector->tx.ring->vsi;
3887 i40e_clean_fdir_tx_irq(q_vector->tx.ring, vsi->work_limit);
3888
3889 return IRQ_HANDLED;
3890}
3891
3892/**
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003893 * i40e_map_vector_to_qp - Assigns the queue pair to the vector
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003894 * @vsi: the VSI being configured
3895 * @v_idx: vector index
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003896 * @qp_idx: queue pair index
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003897 **/
Anjali Singhai Jain26cdc442015-07-10 19:36:00 -04003898static void i40e_map_vector_to_qp(struct i40e_vsi *vsi, int v_idx, int qp_idx)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003899{
Alexander Duyck493fb302013-09-28 07:01:44 +00003900 struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
Alexander Duyck9f65e152013-09-28 06:00:58 +00003901 struct i40e_ring *tx_ring = vsi->tx_rings[qp_idx];
3902 struct i40e_ring *rx_ring = vsi->rx_rings[qp_idx];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003903
3904 tx_ring->q_vector = q_vector;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003905 tx_ring->next = q_vector->tx.ring;
3906 q_vector->tx.ring = tx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003907 q_vector->tx.count++;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003908
Björn Töpel74608d12017-05-24 07:55:35 +02003909 /* Place XDP Tx ring in the same q_vector ring list as regular Tx */
3910 if (i40e_enabled_xdp_vsi(vsi)) {
3911 struct i40e_ring *xdp_ring = vsi->xdp_rings[qp_idx];
3912
3913 xdp_ring->q_vector = q_vector;
3914 xdp_ring->next = q_vector->tx.ring;
3915 q_vector->tx.ring = xdp_ring;
3916 q_vector->tx.count++;
3917 }
3918
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003919 rx_ring->q_vector = q_vector;
3920 rx_ring->next = q_vector->rx.ring;
3921 q_vector->rx.ring = rx_ring;
3922 q_vector->rx.count++;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003923}
3924
3925/**
3926 * i40e_vsi_map_rings_to_vectors - Maps descriptor rings to vectors
3927 * @vsi: the VSI being configured
3928 *
3929 * This function maps descriptor rings to the queue-specific vectors
3930 * we were allotted through the MSI-X enabling code. Ideally, we'd have
3931 * one vector per queue pair, but on a constrained vector budget, we
3932 * group the queue pairs as "efficiently" as possible.
3933 **/
3934static void i40e_vsi_map_rings_to_vectors(struct i40e_vsi *vsi)
3935{
3936 int qp_remaining = vsi->num_queue_pairs;
3937 int q_vectors = vsi->num_q_vectors;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003938 int num_ringpairs;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003939 int v_start = 0;
3940 int qp_idx = 0;
3941
3942 /* If we don't have enough vectors for a 1-to-1 mapping, we'll have to
3943 * group them so there are multiple queues per vector.
Anjali Singhai Jain70114ec2014-06-03 23:50:14 +00003944 * It is also important to go through all the vectors available to be
3945 * sure that if we don't use all the vectors, that the remaining vectors
3946 * are cleared. This is especially important when decreasing the
3947 * number of queues in use.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003948 */
Anjali Singhai Jain70114ec2014-06-03 23:50:14 +00003949 for (; v_start < q_vectors; v_start++) {
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003950 struct i40e_q_vector *q_vector = vsi->q_vectors[v_start];
3951
3952 num_ringpairs = DIV_ROUND_UP(qp_remaining, q_vectors - v_start);
3953
3954 q_vector->num_ringpairs = num_ringpairs;
3955
3956 q_vector->rx.count = 0;
3957 q_vector->tx.count = 0;
3958 q_vector->rx.ring = NULL;
3959 q_vector->tx.ring = NULL;
3960
3961 while (num_ringpairs--) {
Anjali Singhai Jain26cdc442015-07-10 19:36:00 -04003962 i40e_map_vector_to_qp(vsi, v_start, qp_idx);
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003963 qp_idx++;
3964 qp_remaining--;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003965 }
3966 }
3967}
3968
3969/**
3970 * i40e_vsi_request_irq - Request IRQ from the OS
3971 * @vsi: the VSI being configured
3972 * @basename: name for the vector
3973 **/
3974static int i40e_vsi_request_irq(struct i40e_vsi *vsi, char *basename)
3975{
3976 struct i40e_pf *pf = vsi->back;
3977 int err;
3978
3979 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
3980 err = i40e_vsi_request_irq_msix(vsi, basename);
3981 else if (pf->flags & I40E_FLAG_MSI_ENABLED)
3982 err = request_irq(pf->pdev->irq, i40e_intr, 0,
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00003983 pf->int_name, pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003984 else
3985 err = request_irq(pf->pdev->irq, i40e_intr, IRQF_SHARED,
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00003986 pf->int_name, pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003987
3988 if (err)
3989 dev_info(&pf->pdev->dev, "request_irq failed, Error %d\n", err);
3990
3991 return err;
3992}
3993
3994#ifdef CONFIG_NET_POLL_CONTROLLER
3995/**
Jesse Brandeburgd89d9672016-01-04 10:33:02 -08003996 * i40e_netpoll - A Polling 'interrupt' handler
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003997 * @netdev: network interface device structure
3998 *
3999 * This is used by netconsole to send skbs without having to re-enable
4000 * interrupts. It's not called while the normal interrupt routine is executing.
4001 **/
4002static void i40e_netpoll(struct net_device *netdev)
4003{
4004 struct i40e_netdev_priv *np = netdev_priv(netdev);
4005 struct i40e_vsi *vsi = np->vsi;
4006 struct i40e_pf *pf = vsi->back;
4007 int i;
4008
4009 /* if interface is down do nothing */
Jacob Keller0da36b92017-04-19 09:25:55 -04004010 if (test_bit(__I40E_VSI_DOWN, vsi->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004011 return;
4012
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004013 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4014 for (i = 0; i < vsi->num_q_vectors; i++)
Alexander Duyck493fb302013-09-28 07:01:44 +00004015 i40e_msix_clean_rings(0, vsi->q_vectors[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004016 } else {
4017 i40e_intr(pf->pdev->irq, netdev);
4018 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004019}
4020#endif
4021
Jacob Kellerc768e492017-04-13 04:45:47 -04004022#define I40E_QTX_ENA_WAIT_COUNT 50
4023
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004024/**
Neerav Parikh23527302014-06-03 23:50:15 +00004025 * i40e_pf_txq_wait - Wait for a PF's Tx queue to be enabled or disabled
4026 * @pf: the PF being configured
4027 * @pf_q: the PF queue
4028 * @enable: enable or disable state of the queue
4029 *
4030 * This routine will wait for the given Tx queue of the PF to reach the
4031 * enabled or disabled state.
4032 * Returns -ETIMEDOUT in case of failing to reach the requested state after
4033 * multiple retries; else will return 0 in case of success.
4034 **/
4035static int i40e_pf_txq_wait(struct i40e_pf *pf, int pf_q, bool enable)
4036{
4037 int i;
4038 u32 tx_reg;
4039
4040 for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
4041 tx_reg = rd32(&pf->hw, I40E_QTX_ENA(pf_q));
4042 if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
4043 break;
4044
Neerav Parikhf98a2002014-09-13 07:40:44 +00004045 usleep_range(10, 20);
Neerav Parikh23527302014-06-03 23:50:15 +00004046 }
4047 if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
4048 return -ETIMEDOUT;
4049
4050 return 0;
4051}
4052
4053/**
Jacob Kellerc768e492017-04-13 04:45:47 -04004054 * i40e_control_tx_q - Start or stop a particular Tx queue
4055 * @pf: the PF structure
4056 * @pf_q: the PF queue to configure
4057 * @enable: start or stop the queue
4058 *
4059 * This function enables or disables a single queue. Note that any delay
4060 * required after the operation is expected to be handled by the caller of
4061 * this function.
4062 **/
4063static void i40e_control_tx_q(struct i40e_pf *pf, int pf_q, bool enable)
4064{
4065 struct i40e_hw *hw = &pf->hw;
4066 u32 tx_reg;
4067 int i;
4068
4069 /* warn the TX unit of coming changes */
4070 i40e_pre_tx_queue_cfg(&pf->hw, pf_q, enable);
4071 if (!enable)
4072 usleep_range(10, 20);
4073
4074 for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
4075 tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
4076 if (((tx_reg >> I40E_QTX_ENA_QENA_REQ_SHIFT) & 1) ==
4077 ((tx_reg >> I40E_QTX_ENA_QENA_STAT_SHIFT) & 1))
4078 break;
4079 usleep_range(1000, 2000);
4080 }
4081
4082 /* Skip if the queue is already in the requested state */
4083 if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
4084 return;
4085
4086 /* turn on/off the queue */
4087 if (enable) {
4088 wr32(hw, I40E_QTX_HEAD(pf_q), 0);
4089 tx_reg |= I40E_QTX_ENA_QENA_REQ_MASK;
4090 } else {
4091 tx_reg &= ~I40E_QTX_ENA_QENA_REQ_MASK;
4092 }
4093
4094 wr32(hw, I40E_QTX_ENA(pf_q), tx_reg);
4095}
4096
4097/**
Björn Töpel74608d12017-05-24 07:55:35 +02004098 * i40e_control_wait_tx_q - Start/stop Tx queue and wait for completion
4099 * @seid: VSI SEID
4100 * @pf: the PF structure
4101 * @pf_q: the PF queue to configure
4102 * @is_xdp: true if the queue is used for XDP
4103 * @enable: start or stop the queue
4104 **/
4105static int i40e_control_wait_tx_q(int seid, struct i40e_pf *pf, int pf_q,
4106 bool is_xdp, bool enable)
4107{
4108 int ret;
4109
4110 i40e_control_tx_q(pf, pf_q, enable);
4111
4112 /* wait for the change to finish */
4113 ret = i40e_pf_txq_wait(pf, pf_q, enable);
4114 if (ret) {
4115 dev_info(&pf->pdev->dev,
4116 "VSI seid %d %sTx ring %d %sable timeout\n",
4117 seid, (is_xdp ? "XDP " : ""), pf_q,
4118 (enable ? "en" : "dis"));
4119 }
4120
4121 return ret;
4122}
4123
4124/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004125 * i40e_vsi_control_tx - Start or stop a VSI's rings
4126 * @vsi: the VSI being configured
4127 * @enable: start or stop the rings
4128 **/
4129static int i40e_vsi_control_tx(struct i40e_vsi *vsi, bool enable)
4130{
4131 struct i40e_pf *pf = vsi->back;
Jacob Kellerc768e492017-04-13 04:45:47 -04004132 int i, pf_q, ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004133
4134 pf_q = vsi->base_queue;
4135 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
Björn Töpel74608d12017-05-24 07:55:35 +02004136 ret = i40e_control_wait_tx_q(vsi->seid, pf,
4137 pf_q,
4138 false /*is xdp*/, enable);
4139 if (ret)
Neerav Parikh23527302014-06-03 23:50:15 +00004140 break;
Björn Töpel74608d12017-05-24 07:55:35 +02004141
4142 if (!i40e_enabled_xdp_vsi(vsi))
4143 continue;
4144
4145 ret = i40e_control_wait_tx_q(vsi->seid, pf,
4146 pf_q + vsi->alloc_queue_pairs,
4147 true /*is xdp*/, enable);
4148 if (ret)
4149 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004150 }
4151
Neerav Parikh23527302014-06-03 23:50:15 +00004152 return ret;
4153}
4154
4155/**
4156 * i40e_pf_rxq_wait - Wait for a PF's Rx queue to be enabled or disabled
4157 * @pf: the PF being configured
4158 * @pf_q: the PF queue
4159 * @enable: enable or disable state of the queue
4160 *
4161 * This routine will wait for the given Rx queue of the PF to reach the
4162 * enabled or disabled state.
4163 * Returns -ETIMEDOUT in case of failing to reach the requested state after
4164 * multiple retries; else will return 0 in case of success.
4165 **/
4166static int i40e_pf_rxq_wait(struct i40e_pf *pf, int pf_q, bool enable)
4167{
4168 int i;
4169 u32 rx_reg;
4170
4171 for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
4172 rx_reg = rd32(&pf->hw, I40E_QRX_ENA(pf_q));
4173 if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4174 break;
4175
Neerav Parikhf98a2002014-09-13 07:40:44 +00004176 usleep_range(10, 20);
Neerav Parikh23527302014-06-03 23:50:15 +00004177 }
4178 if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
4179 return -ETIMEDOUT;
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00004180
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004181 return 0;
4182}
4183
4184/**
Jacob Kellerc768e492017-04-13 04:45:47 -04004185 * i40e_control_rx_q - Start or stop a particular Rx queue
4186 * @pf: the PF structure
4187 * @pf_q: the PF queue to configure
4188 * @enable: start or stop the queue
4189 *
4190 * This function enables or disables a single queue. Note that any delay
4191 * required after the operation is expected to be handled by the caller of
4192 * this function.
4193 **/
4194static void i40e_control_rx_q(struct i40e_pf *pf, int pf_q, bool enable)
4195{
4196 struct i40e_hw *hw = &pf->hw;
4197 u32 rx_reg;
4198 int i;
4199
4200 for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
4201 rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
4202 if (((rx_reg >> I40E_QRX_ENA_QENA_REQ_SHIFT) & 1) ==
4203 ((rx_reg >> I40E_QRX_ENA_QENA_STAT_SHIFT) & 1))
4204 break;
4205 usleep_range(1000, 2000);
4206 }
4207
4208 /* Skip if the queue is already in the requested state */
4209 if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4210 return;
4211
4212 /* turn on/off the queue */
4213 if (enable)
4214 rx_reg |= I40E_QRX_ENA_QENA_REQ_MASK;
4215 else
4216 rx_reg &= ~I40E_QRX_ENA_QENA_REQ_MASK;
4217
4218 wr32(hw, I40E_QRX_ENA(pf_q), rx_reg);
4219}
4220
4221/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004222 * i40e_vsi_control_rx - Start or stop a VSI's rings
4223 * @vsi: the VSI being configured
4224 * @enable: start or stop the rings
4225 **/
4226static int i40e_vsi_control_rx(struct i40e_vsi *vsi, bool enable)
4227{
4228 struct i40e_pf *pf = vsi->back;
Jacob Kellerc768e492017-04-13 04:45:47 -04004229 int i, pf_q, ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004230
4231 pf_q = vsi->base_queue;
4232 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
Jacob Kellerc768e492017-04-13 04:45:47 -04004233 i40e_control_rx_q(pf, pf_q, enable);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004234
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004235 /* wait for the change to finish */
Neerav Parikh23527302014-06-03 23:50:15 +00004236 ret = i40e_pf_rxq_wait(pf, pf_q, enable);
4237 if (ret) {
4238 dev_info(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04004239 "VSI seid %d Rx ring %d %sable timeout\n",
4240 vsi->seid, pf_q, (enable ? "en" : "dis"));
Neerav Parikh23527302014-06-03 23:50:15 +00004241 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004242 }
4243 }
4244
Wyborny, Carolynd08a9f62017-03-28 08:00:48 -07004245 /* Due to HW errata, on Rx disable only, the register can indicate done
4246 * before it really is. Needs 50ms to be sure
4247 */
4248 if (!enable)
4249 mdelay(50);
4250
Neerav Parikh23527302014-06-03 23:50:15 +00004251 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004252}
4253
4254/**
Filip Sadowski3aa7b742016-10-11 15:26:58 -07004255 * i40e_vsi_start_rings - Start a VSI's rings
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004256 * @vsi: the VSI being configured
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004257 **/
Filip Sadowski3aa7b742016-10-11 15:26:58 -07004258int i40e_vsi_start_rings(struct i40e_vsi *vsi)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004259{
Anjali Singhai Jain3b867b22013-12-21 05:44:44 +00004260 int ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004261
4262 /* do rx first for enable and last for disable */
Filip Sadowski3aa7b742016-10-11 15:26:58 -07004263 ret = i40e_vsi_control_rx(vsi, true);
4264 if (ret)
4265 return ret;
4266 ret = i40e_vsi_control_tx(vsi, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004267
4268 return ret;
4269}
4270
4271/**
Filip Sadowski3aa7b742016-10-11 15:26:58 -07004272 * i40e_vsi_stop_rings - Stop a VSI's rings
4273 * @vsi: the VSI being configured
4274 **/
4275void i40e_vsi_stop_rings(struct i40e_vsi *vsi)
4276{
Jacob Keller34807562017-04-13 04:45:53 -04004277 /* When port TX is suspended, don't wait */
Jacob Keller0da36b92017-04-19 09:25:55 -04004278 if (test_bit(__I40E_PORT_SUSPENDED, vsi->back->state))
Jacob Keller34807562017-04-13 04:45:53 -04004279 return i40e_vsi_stop_rings_no_wait(vsi);
4280
Filip Sadowski3aa7b742016-10-11 15:26:58 -07004281 /* do rx first for enable and last for disable
4282 * Ignore return value, we need to shutdown whatever we can
4283 */
4284 i40e_vsi_control_tx(vsi, false);
4285 i40e_vsi_control_rx(vsi, false);
4286}
4287
4288/**
Jacob Kellere4b433f2017-04-13 04:45:52 -04004289 * i40e_vsi_stop_rings_no_wait - Stop a VSI's rings and do not delay
4290 * @vsi: the VSI being shutdown
4291 *
4292 * This function stops all the rings for a VSI but does not delay to verify
4293 * that rings have been disabled. It is expected that the caller is shutting
4294 * down multiple VSIs at once and will delay together for all the VSIs after
4295 * initiating the shutdown. This is particularly useful for shutting down lots
4296 * of VFs together. Otherwise, a large delay can be incurred while configuring
4297 * each VSI in serial.
4298 **/
4299void i40e_vsi_stop_rings_no_wait(struct i40e_vsi *vsi)
4300{
4301 struct i40e_pf *pf = vsi->back;
4302 int i, pf_q;
4303
4304 pf_q = vsi->base_queue;
4305 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4306 i40e_control_tx_q(pf, pf_q, false);
4307 i40e_control_rx_q(pf, pf_q, false);
4308 }
4309}
4310
4311/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004312 * i40e_vsi_free_irq - Free the irq association with the OS
4313 * @vsi: the VSI being configured
4314 **/
4315static void i40e_vsi_free_irq(struct i40e_vsi *vsi)
4316{
4317 struct i40e_pf *pf = vsi->back;
4318 struct i40e_hw *hw = &pf->hw;
4319 int base = vsi->base_vector;
4320 u32 val, qp;
4321 int i;
4322
4323 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4324 if (!vsi->q_vectors)
4325 return;
4326
Shannon Nelson63741842014-04-23 04:50:16 +00004327 if (!vsi->irqs_ready)
4328 return;
4329
4330 vsi->irqs_ready = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004331 for (i = 0; i < vsi->num_q_vectors; i++) {
Alan Brady96db7762016-09-14 16:24:38 -07004332 int irq_num;
4333 u16 vector;
4334
4335 vector = i + base;
4336 irq_num = pf->msix_entries[vector].vector;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004337
4338 /* free only the irqs that were actually requested */
Shannon Nelson78681b12013-11-28 06:39:36 +00004339 if (!vsi->q_vectors[i] ||
4340 !vsi->q_vectors[i]->num_ringpairs)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004341 continue;
4342
Alan Brady96db7762016-09-14 16:24:38 -07004343 /* clear the affinity notifier in the IRQ descriptor */
4344 irq_set_affinity_notifier(irq_num, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004345 /* clear the affinity_mask in the IRQ descriptor */
Alan Brady96db7762016-09-14 16:24:38 -07004346 irq_set_affinity_hint(irq_num, NULL);
4347 synchronize_irq(irq_num);
4348 free_irq(irq_num, vsi->q_vectors[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004349
4350 /* Tear down the interrupt queue link list
4351 *
4352 * We know that they come in pairs and always
4353 * the Rx first, then the Tx. To clear the
4354 * link list, stick the EOL value into the
4355 * next_q field of the registers.
4356 */
4357 val = rd32(hw, I40E_PFINT_LNKLSTN(vector - 1));
4358 qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
4359 >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4360 val |= I40E_QUEUE_END_OF_LIST
4361 << I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4362 wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), val);
4363
4364 while (qp != I40E_QUEUE_END_OF_LIST) {
4365 u32 next;
4366
4367 val = rd32(hw, I40E_QINT_RQCTL(qp));
4368
4369 val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
4370 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
4371 I40E_QINT_RQCTL_CAUSE_ENA_MASK |
4372 I40E_QINT_RQCTL_INTEVENT_MASK);
4373
4374 val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
4375 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
4376
4377 wr32(hw, I40E_QINT_RQCTL(qp), val);
4378
4379 val = rd32(hw, I40E_QINT_TQCTL(qp));
4380
4381 next = (val & I40E_QINT_TQCTL_NEXTQ_INDX_MASK)
4382 >> I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT;
4383
4384 val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
4385 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
4386 I40E_QINT_TQCTL_CAUSE_ENA_MASK |
4387 I40E_QINT_TQCTL_INTEVENT_MASK);
4388
4389 val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
4390 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
4391
4392 wr32(hw, I40E_QINT_TQCTL(qp), val);
4393 qp = next;
4394 }
4395 }
4396 } else {
4397 free_irq(pf->pdev->irq, pf);
4398
4399 val = rd32(hw, I40E_PFINT_LNKLST0);
4400 qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
4401 >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4402 val |= I40E_QUEUE_END_OF_LIST
4403 << I40E_PFINT_LNKLST0_FIRSTQ_INDX_SHIFT;
4404 wr32(hw, I40E_PFINT_LNKLST0, val);
4405
4406 val = rd32(hw, I40E_QINT_RQCTL(qp));
4407 val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
4408 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
4409 I40E_QINT_RQCTL_CAUSE_ENA_MASK |
4410 I40E_QINT_RQCTL_INTEVENT_MASK);
4411
4412 val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
4413 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
4414
4415 wr32(hw, I40E_QINT_RQCTL(qp), val);
4416
4417 val = rd32(hw, I40E_QINT_TQCTL(qp));
4418
4419 val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
4420 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
4421 I40E_QINT_TQCTL_CAUSE_ENA_MASK |
4422 I40E_QINT_TQCTL_INTEVENT_MASK);
4423
4424 val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
4425 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
4426
4427 wr32(hw, I40E_QINT_TQCTL(qp), val);
4428 }
4429}
4430
4431/**
Alexander Duyck493fb302013-09-28 07:01:44 +00004432 * i40e_free_q_vector - Free memory allocated for specific interrupt vector
4433 * @vsi: the VSI being configured
4434 * @v_idx: Index of vector to be freed
4435 *
4436 * This function frees the memory allocated to the q_vector. In addition if
4437 * NAPI is enabled it will delete any references to the NAPI struct prior
4438 * to freeing the q_vector.
4439 **/
4440static void i40e_free_q_vector(struct i40e_vsi *vsi, int v_idx)
4441{
4442 struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00004443 struct i40e_ring *ring;
Alexander Duyck493fb302013-09-28 07:01:44 +00004444
4445 if (!q_vector)
4446 return;
4447
4448 /* disassociate q_vector from rings */
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00004449 i40e_for_each_ring(ring, q_vector->tx)
4450 ring->q_vector = NULL;
4451
4452 i40e_for_each_ring(ring, q_vector->rx)
4453 ring->q_vector = NULL;
Alexander Duyck493fb302013-09-28 07:01:44 +00004454
4455 /* only VSI w/ an associated netdev is set up w/ NAPI */
4456 if (vsi->netdev)
4457 netif_napi_del(&q_vector->napi);
4458
4459 vsi->q_vectors[v_idx] = NULL;
4460
4461 kfree_rcu(q_vector, rcu);
4462}
4463
4464/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004465 * i40e_vsi_free_q_vectors - Free memory allocated for interrupt vectors
4466 * @vsi: the VSI being un-configured
4467 *
4468 * This frees the memory allocated to the q_vectors and
4469 * deletes references to the NAPI struct.
4470 **/
4471static void i40e_vsi_free_q_vectors(struct i40e_vsi *vsi)
4472{
4473 int v_idx;
4474
Alexander Duyck493fb302013-09-28 07:01:44 +00004475 for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++)
4476 i40e_free_q_vector(vsi, v_idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004477}
4478
4479/**
4480 * i40e_reset_interrupt_capability - Disable interrupt setup in OS
4481 * @pf: board private structure
4482 **/
4483static void i40e_reset_interrupt_capability(struct i40e_pf *pf)
4484{
4485 /* If we're in Legacy mode, the interrupt was cleaned in vsi_close */
4486 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4487 pci_disable_msix(pf->pdev);
4488 kfree(pf->msix_entries);
4489 pf->msix_entries = NULL;
Shannon Nelson3b444392015-02-26 16:15:57 +00004490 kfree(pf->irq_pile);
4491 pf->irq_pile = NULL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004492 } else if (pf->flags & I40E_FLAG_MSI_ENABLED) {
4493 pci_disable_msi(pf->pdev);
4494 }
4495 pf->flags &= ~(I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
4496}
4497
4498/**
4499 * i40e_clear_interrupt_scheme - Clear the current interrupt scheme settings
4500 * @pf: board private structure
4501 *
4502 * We go through and clear interrupt specific resources and reset the structure
4503 * to pre-load conditions
4504 **/
4505static void i40e_clear_interrupt_scheme(struct i40e_pf *pf)
4506{
4507 int i;
4508
Shannon Nelsone1477582015-02-21 06:44:33 +00004509 i40e_stop_misc_vector(pf);
Shannon Nelson69278392016-03-10 14:59:43 -08004510 if (pf->flags & I40E_FLAG_MSIX_ENABLED && pf->msix_entries) {
Shannon Nelsone1477582015-02-21 06:44:33 +00004511 synchronize_irq(pf->msix_entries[0].vector);
4512 free_irq(pf->msix_entries[0].vector, pf);
4513 }
4514
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06004515 i40e_put_lump(pf->irq_pile, pf->iwarp_base_vector,
4516 I40E_IWARP_IRQ_PILE_ID);
4517
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004518 i40e_put_lump(pf->irq_pile, 0, I40E_PILE_VALID_BIT-1);
Mitch Williams505682c2014-05-20 08:01:37 +00004519 for (i = 0; i < pf->num_alloc_vsi; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004520 if (pf->vsi[i])
4521 i40e_vsi_free_q_vectors(pf->vsi[i]);
4522 i40e_reset_interrupt_capability(pf);
4523}
4524
4525/**
4526 * i40e_napi_enable_all - Enable NAPI for all q_vectors in the VSI
4527 * @vsi: the VSI being configured
4528 **/
4529static void i40e_napi_enable_all(struct i40e_vsi *vsi)
4530{
4531 int q_idx;
4532
4533 if (!vsi->netdev)
4534 return;
4535
Alexander Duyck13a8cd12017-03-24 15:01:42 -07004536 for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
4537 struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
4538
4539 if (q_vector->rx.ring || q_vector->tx.ring)
4540 napi_enable(&q_vector->napi);
4541 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004542}
4543
4544/**
4545 * i40e_napi_disable_all - Disable NAPI for all q_vectors in the VSI
4546 * @vsi: the VSI being configured
4547 **/
4548static void i40e_napi_disable_all(struct i40e_vsi *vsi)
4549{
4550 int q_idx;
4551
4552 if (!vsi->netdev)
4553 return;
4554
Alexander Duyck13a8cd12017-03-24 15:01:42 -07004555 for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
4556 struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
4557
4558 if (q_vector->rx.ring || q_vector->tx.ring)
4559 napi_disable(&q_vector->napi);
4560 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004561}
4562
4563/**
Shannon Nelson90ef8d42014-03-14 07:32:26 +00004564 * i40e_vsi_close - Shut down a VSI
4565 * @vsi: the vsi to be quelled
4566 **/
4567static void i40e_vsi_close(struct i40e_vsi *vsi)
4568{
Mitch Williams0ef2d5a2017-01-24 10:24:00 -08004569 struct i40e_pf *pf = vsi->back;
Jacob Keller0da36b92017-04-19 09:25:55 -04004570 if (!test_and_set_bit(__I40E_VSI_DOWN, vsi->state))
Shannon Nelson90ef8d42014-03-14 07:32:26 +00004571 i40e_down(vsi);
4572 i40e_vsi_free_irq(vsi);
4573 i40e_vsi_free_tx_resources(vsi);
4574 i40e_vsi_free_rx_resources(vsi);
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04004575 vsi->current_netdev_flags = 0;
Mitch Williams0ef2d5a2017-01-24 10:24:00 -08004576 pf->flags |= I40E_FLAG_SERVICE_CLIENT_REQUESTED;
Jacob Keller0da36b92017-04-19 09:25:55 -04004577 if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
Mitch Williams0ef2d5a2017-01-24 10:24:00 -08004578 pf->flags |= I40E_FLAG_CLIENT_RESET;
Shannon Nelson90ef8d42014-03-14 07:32:26 +00004579}
4580
4581/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004582 * i40e_quiesce_vsi - Pause a given VSI
4583 * @vsi: the VSI being paused
4584 **/
4585static void i40e_quiesce_vsi(struct i40e_vsi *vsi)
4586{
Jacob Keller0da36b92017-04-19 09:25:55 -04004587 if (test_bit(__I40E_VSI_DOWN, vsi->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004588 return;
4589
Jacob Keller0da36b92017-04-19 09:25:55 -04004590 set_bit(__I40E_VSI_NEEDS_RESTART, vsi->state);
Jesse Brandeburg6995b362015-08-28 17:55:54 -04004591 if (vsi->netdev && netif_running(vsi->netdev))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004592 vsi->netdev->netdev_ops->ndo_stop(vsi->netdev);
Jesse Brandeburg6995b362015-08-28 17:55:54 -04004593 else
Shannon Nelson90ef8d42014-03-14 07:32:26 +00004594 i40e_vsi_close(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004595}
4596
4597/**
4598 * i40e_unquiesce_vsi - Resume a given VSI
4599 * @vsi: the VSI being resumed
4600 **/
4601static void i40e_unquiesce_vsi(struct i40e_vsi *vsi)
4602{
Jacob Keller0da36b92017-04-19 09:25:55 -04004603 if (!test_and_clear_bit(__I40E_VSI_NEEDS_RESTART, vsi->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004604 return;
4605
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004606 if (vsi->netdev && netif_running(vsi->netdev))
4607 vsi->netdev->netdev_ops->ndo_open(vsi->netdev);
4608 else
Shannon Nelson8276f752014-03-14 07:32:27 +00004609 i40e_vsi_open(vsi); /* this clears the DOWN bit */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004610}
4611
4612/**
4613 * i40e_pf_quiesce_all_vsi - Pause all VSIs on a PF
4614 * @pf: the PF
4615 **/
4616static void i40e_pf_quiesce_all_vsi(struct i40e_pf *pf)
4617{
4618 int v;
4619
Mitch Williams505682c2014-05-20 08:01:37 +00004620 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004621 if (pf->vsi[v])
4622 i40e_quiesce_vsi(pf->vsi[v]);
4623 }
4624}
4625
4626/**
4627 * i40e_pf_unquiesce_all_vsi - Resume all VSIs on a PF
4628 * @pf: the PF
4629 **/
4630static void i40e_pf_unquiesce_all_vsi(struct i40e_pf *pf)
4631{
4632 int v;
4633
Mitch Williams505682c2014-05-20 08:01:37 +00004634 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004635 if (pf->vsi[v])
4636 i40e_unquiesce_vsi(pf->vsi[v]);
4637 }
4638}
4639
Neerav Parikh69129dc2014-11-12 00:18:46 +00004640/**
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004641 * i40e_vsi_wait_queues_disabled - Wait for VSI's queues to be disabled
Neerav Parikh69129dc2014-11-12 00:18:46 +00004642 * @vsi: the VSI being configured
4643 *
Jacob Kelleraf26ce22017-04-05 07:50:58 -04004644 * Wait until all queues on a given VSI have been disabled.
Neerav Parikh69129dc2014-11-12 00:18:46 +00004645 **/
Jacob Kellere4b433f2017-04-13 04:45:52 -04004646int i40e_vsi_wait_queues_disabled(struct i40e_vsi *vsi)
Neerav Parikh69129dc2014-11-12 00:18:46 +00004647{
4648 struct i40e_pf *pf = vsi->back;
4649 int i, pf_q, ret;
4650
4651 pf_q = vsi->base_queue;
4652 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
Jacob Kelleraf26ce22017-04-05 07:50:58 -04004653 /* Check and wait for the Tx queue */
Neerav Parikh69129dc2014-11-12 00:18:46 +00004654 ret = i40e_pf_txq_wait(pf, pf_q, false);
4655 if (ret) {
4656 dev_info(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04004657 "VSI seid %d Tx ring %d disable timeout\n",
4658 vsi->seid, pf_q);
Neerav Parikh69129dc2014-11-12 00:18:46 +00004659 return ret;
4660 }
Björn Töpel74608d12017-05-24 07:55:35 +02004661
4662 if (!i40e_enabled_xdp_vsi(vsi))
4663 goto wait_rx;
4664
4665 /* Check and wait for the XDP Tx queue */
4666 ret = i40e_pf_txq_wait(pf, pf_q + vsi->alloc_queue_pairs,
4667 false);
4668 if (ret) {
4669 dev_info(&pf->pdev->dev,
4670 "VSI seid %d XDP Tx ring %d disable timeout\n",
4671 vsi->seid, pf_q);
4672 return ret;
4673 }
4674wait_rx:
4675 /* Check and wait for the Rx queue */
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004676 ret = i40e_pf_rxq_wait(pf, pf_q, false);
4677 if (ret) {
4678 dev_info(&pf->pdev->dev,
4679 "VSI seid %d Rx ring %d disable timeout\n",
4680 vsi->seid, pf_q);
4681 return ret;
4682 }
4683 }
4684
Neerav Parikh69129dc2014-11-12 00:18:46 +00004685 return 0;
4686}
4687
Jacob Kellere4b433f2017-04-13 04:45:52 -04004688#ifdef CONFIG_I40E_DCB
Neerav Parikh69129dc2014-11-12 00:18:46 +00004689/**
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004690 * i40e_pf_wait_queues_disabled - Wait for all queues of PF VSIs to be disabled
Neerav Parikh69129dc2014-11-12 00:18:46 +00004691 * @pf: the PF
4692 *
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004693 * This function waits for the queues to be in disabled state for all the
Neerav Parikh69129dc2014-11-12 00:18:46 +00004694 * VSIs that are managed by this PF.
4695 **/
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004696static int i40e_pf_wait_queues_disabled(struct i40e_pf *pf)
Neerav Parikh69129dc2014-11-12 00:18:46 +00004697{
4698 int v, ret = 0;
4699
4700 for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
Alexander Duyckc76cb6e2017-02-21 15:55:48 -08004701 if (pf->vsi[v]) {
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004702 ret = i40e_vsi_wait_queues_disabled(pf->vsi[v]);
Neerav Parikh69129dc2014-11-12 00:18:46 +00004703 if (ret)
4704 break;
4705 }
4706 }
4707
4708 return ret;
4709}
4710
4711#endif
Kiran Patilb03a8c12015-09-24 18:13:15 -04004712
4713/**
4714 * i40e_detect_recover_hung_queue - Function to detect and recover hung_queue
4715 * @q_idx: TX queue number
4716 * @vsi: Pointer to VSI struct
4717 *
4718 * This function checks specified queue for given VSI. Detects hung condition.
Alan Brady17daabb2017-04-05 07:50:56 -04004719 * We proactively detect hung TX queues by checking if interrupts are disabled
4720 * but there are pending descriptors. If it appears hung, attempt to recover
4721 * by triggering a SW interrupt.
Kiran Patilb03a8c12015-09-24 18:13:15 -04004722 **/
4723static void i40e_detect_recover_hung_queue(int q_idx, struct i40e_vsi *vsi)
4724{
4725 struct i40e_ring *tx_ring = NULL;
4726 struct i40e_pf *pf;
Alan Brady17daabb2017-04-05 07:50:56 -04004727 u32 val, tx_pending;
Kiran Patilb03a8c12015-09-24 18:13:15 -04004728 int i;
4729
4730 pf = vsi->back;
4731
4732 /* now that we have an index, find the tx_ring struct */
4733 for (i = 0; i < vsi->num_queue_pairs; i++) {
4734 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc) {
4735 if (q_idx == vsi->tx_rings[i]->queue_index) {
4736 tx_ring = vsi->tx_rings[i];
4737 break;
4738 }
4739 }
4740 }
4741
4742 if (!tx_ring)
4743 return;
4744
4745 /* Read interrupt register */
4746 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
4747 val = rd32(&pf->hw,
4748 I40E_PFINT_DYN_CTLN(tx_ring->q_vector->v_idx +
4749 tx_ring->vsi->base_vector - 1));
4750 else
4751 val = rd32(&pf->hw, I40E_PFINT_DYN_CTL0);
4752
Alan Brady17daabb2017-04-05 07:50:56 -04004753 tx_pending = i40e_get_tx_pending(tx_ring);
Kiran Patilb03a8c12015-09-24 18:13:15 -04004754
Alan Brady17daabb2017-04-05 07:50:56 -04004755 /* Interrupts are disabled and TX pending is non-zero,
4756 * trigger the SW interrupt (don't wait). Worst case
4757 * there will be one extra interrupt which may result
4758 * into not cleaning any queues because queues are cleaned.
Kiran Patilb03a8c12015-09-24 18:13:15 -04004759 */
Alan Brady17daabb2017-04-05 07:50:56 -04004760 if (tx_pending && (!(val & I40E_PFINT_DYN_CTLN_INTENA_MASK)))
4761 i40e_force_wb(vsi, tx_ring->q_vector);
Kiran Patilb03a8c12015-09-24 18:13:15 -04004762}
4763
4764/**
4765 * i40e_detect_recover_hung - Function to detect and recover hung_queues
4766 * @pf: pointer to PF struct
4767 *
4768 * LAN VSI has netdev and netdev has TX queues. This function is to check
4769 * each of those TX queues if they are hung, trigger recovery by issuing
4770 * SW interrupt.
4771 **/
4772static void i40e_detect_recover_hung(struct i40e_pf *pf)
4773{
4774 struct net_device *netdev;
4775 struct i40e_vsi *vsi;
Jesse Brandeburgb85c94b2017-06-20 15:16:59 -07004776 unsigned int i;
Kiran Patilb03a8c12015-09-24 18:13:15 -04004777
4778 /* Only for LAN VSI */
4779 vsi = pf->vsi[pf->lan_vsi];
4780
4781 if (!vsi)
4782 return;
4783
4784 /* Make sure, VSI state is not DOWN/RECOVERY_PENDING */
Jacob Keller0da36b92017-04-19 09:25:55 -04004785 if (test_bit(__I40E_VSI_DOWN, vsi->back->state) ||
4786 test_bit(__I40E_RESET_RECOVERY_PENDING, vsi->back->state))
Kiran Patilb03a8c12015-09-24 18:13:15 -04004787 return;
4788
4789 /* Make sure type is MAIN VSI */
4790 if (vsi->type != I40E_VSI_MAIN)
4791 return;
4792
4793 netdev = vsi->netdev;
4794 if (!netdev)
4795 return;
4796
4797 /* Bail out if netif_carrier is not OK */
4798 if (!netif_carrier_ok(netdev))
4799 return;
4800
4801 /* Go thru' TX queues for netdev */
4802 for (i = 0; i < netdev->num_tx_queues; i++) {
4803 struct netdev_queue *q;
4804
4805 q = netdev_get_tx_queue(netdev, i);
4806 if (q)
4807 i40e_detect_recover_hung_queue(i, vsi);
4808 }
4809}
4810
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004811/**
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004812 * i40e_get_iscsi_tc_map - Return TC map for iSCSI APP
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00004813 * @pf: pointer to PF
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004814 *
4815 * Get TC map for ISCSI PF type that will include iSCSI TC
4816 * and LAN TC.
4817 **/
4818static u8 i40e_get_iscsi_tc_map(struct i40e_pf *pf)
4819{
4820 struct i40e_dcb_app_priority_table app;
4821 struct i40e_hw *hw = &pf->hw;
4822 u8 enabled_tc = 1; /* TC0 is always enabled */
4823 u8 tc, i;
4824 /* Get the iSCSI APP TLV */
4825 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
4826
4827 for (i = 0; i < dcbcfg->numapps; i++) {
4828 app = dcbcfg->app[i];
4829 if (app.selector == I40E_APP_SEL_TCPIP &&
4830 app.protocolid == I40E_APP_PROTOID_ISCSI) {
4831 tc = dcbcfg->etscfg.prioritytable[app.priority];
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08004832 enabled_tc |= BIT(tc);
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004833 break;
4834 }
4835 }
4836
4837 return enabled_tc;
4838}
4839
4840/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004841 * i40e_dcb_get_num_tc - Get the number of TCs from DCBx config
4842 * @dcbcfg: the corresponding DCBx configuration structure
4843 *
4844 * Return the number of TCs from given DCBx configuration
4845 **/
4846static u8 i40e_dcb_get_num_tc(struct i40e_dcbx_config *dcbcfg)
4847{
Dave Ertmanfbfe12c2016-08-12 09:56:32 -07004848 int i, tc_unused = 0;
Jesse Brandeburg078b5872013-09-25 23:41:14 +00004849 u8 num_tc = 0;
Dave Ertmanfbfe12c2016-08-12 09:56:32 -07004850 u8 ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004851
4852 /* Scan the ETS Config Priority Table to find
4853 * traffic class enabled for a given priority
Dave Ertmanfbfe12c2016-08-12 09:56:32 -07004854 * and create a bitmask of enabled TCs
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004855 */
Dave Ertmanfbfe12c2016-08-12 09:56:32 -07004856 for (i = 0; i < I40E_MAX_USER_PRIORITY; i++)
4857 num_tc |= BIT(dcbcfg->etscfg.prioritytable[i]);
4858
4859 /* Now scan the bitmask to check for
4860 * contiguous TCs starting with TC0
4861 */
4862 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
4863 if (num_tc & BIT(i)) {
4864 if (!tc_unused) {
4865 ret++;
4866 } else {
4867 pr_err("Non-contiguous TC - Disabling DCB\n");
4868 return 1;
4869 }
4870 } else {
4871 tc_unused = 1;
4872 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004873 }
4874
Dave Ertmanfbfe12c2016-08-12 09:56:32 -07004875 /* There is always at least TC0 */
4876 if (!ret)
4877 ret = 1;
4878
4879 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004880}
4881
4882/**
4883 * i40e_dcb_get_enabled_tc - Get enabled traffic classes
4884 * @dcbcfg: the corresponding DCBx configuration structure
4885 *
4886 * Query the current DCB configuration and return the number of
4887 * traffic classes enabled from the given DCBX config
4888 **/
4889static u8 i40e_dcb_get_enabled_tc(struct i40e_dcbx_config *dcbcfg)
4890{
4891 u8 num_tc = i40e_dcb_get_num_tc(dcbcfg);
4892 u8 enabled_tc = 1;
4893 u8 i;
4894
4895 for (i = 0; i < num_tc; i++)
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04004896 enabled_tc |= BIT(i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004897
4898 return enabled_tc;
4899}
4900
4901/**
4902 * i40e_pf_get_num_tc - Get enabled traffic classes for PF
4903 * @pf: PF being queried
4904 *
4905 * Return number of traffic classes enabled for the given PF
4906 **/
4907static u8 i40e_pf_get_num_tc(struct i40e_pf *pf)
4908{
4909 struct i40e_hw *hw = &pf->hw;
Dave Ertman52a08ca2016-07-27 12:02:34 -07004910 u8 i, enabled_tc = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004911 u8 num_tc = 0;
4912 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
4913
4914 /* If DCB is not enabled then always in single TC */
4915 if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
4916 return 1;
4917
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004918 /* SFP mode will be enabled for all TCs on port */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004919 if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
4920 return i40e_dcb_get_num_tc(dcbcfg);
4921
4922 /* MFP mode return count of enabled TCs for this PF */
4923 if (pf->hw.func_caps.iscsi)
4924 enabled_tc = i40e_get_iscsi_tc_map(pf);
4925 else
Neerav Parikhfc51de92015-02-24 06:58:53 +00004926 return 1; /* Only TC0 */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004927
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004928 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08004929 if (enabled_tc & BIT(i))
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004930 num_tc++;
4931 }
4932 return num_tc;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004933}
4934
4935/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004936 * i40e_pf_get_pf_tc_map - Get bitmap for enabled traffic classes
4937 * @pf: PF being queried
4938 *
4939 * Return a bitmap for enabled traffic classes for this PF.
4940 **/
4941static u8 i40e_pf_get_tc_map(struct i40e_pf *pf)
4942{
4943 /* If DCB is not enabled for this PF then just return default TC */
4944 if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
David Ertmanea6acb72016-09-20 07:10:50 -07004945 return I40E_DEFAULT_TRAFFIC_CLASS;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004946
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004947 /* SFP mode we want PF to be enabled for all TCs */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004948 if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
4949 return i40e_dcb_get_enabled_tc(&pf->hw.local_dcbx_config);
4950
Neerav Parikhfc51de92015-02-24 06:58:53 +00004951 /* MFP enabled and iSCSI PF type */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004952 if (pf->hw.func_caps.iscsi)
4953 return i40e_get_iscsi_tc_map(pf);
4954 else
David Ertmanea6acb72016-09-20 07:10:50 -07004955 return I40E_DEFAULT_TRAFFIC_CLASS;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004956}
4957
4958/**
4959 * i40e_vsi_get_bw_info - Query VSI BW Information
4960 * @vsi: the VSI being queried
4961 *
4962 * Returns 0 on success, negative value on failure
4963 **/
4964static int i40e_vsi_get_bw_info(struct i40e_vsi *vsi)
4965{
4966 struct i40e_aqc_query_vsi_ets_sla_config_resp bw_ets_config = {0};
4967 struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
4968 struct i40e_pf *pf = vsi->back;
4969 struct i40e_hw *hw = &pf->hw;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004970 i40e_status ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004971 u32 tc_bw_max;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004972 int i;
4973
4974 /* Get the VSI level BW configuration */
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004975 ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid, &bw_config, NULL);
4976 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004977 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004978 "couldn't get PF vsi bw config, err %s aq_err %s\n",
4979 i40e_stat_str(&pf->hw, ret),
4980 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004981 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004982 }
4983
4984 /* Get the VSI level BW configuration per TC */
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004985 ret = i40e_aq_query_vsi_ets_sla_config(hw, vsi->seid, &bw_ets_config,
4986 NULL);
4987 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004988 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004989 "couldn't get PF vsi ets bw config, err %s aq_err %s\n",
4990 i40e_stat_str(&pf->hw, ret),
4991 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004992 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004993 }
4994
4995 if (bw_config.tc_valid_bits != bw_ets_config.tc_valid_bits) {
4996 dev_info(&pf->pdev->dev,
4997 "Enabled TCs mismatch from querying VSI BW info 0x%08x 0x%08x\n",
4998 bw_config.tc_valid_bits,
4999 bw_ets_config.tc_valid_bits);
5000 /* Still continuing */
5001 }
5002
5003 vsi->bw_limit = le16_to_cpu(bw_config.port_bw_limit);
5004 vsi->bw_max_quanta = bw_config.max_bw;
5005 tc_bw_max = le16_to_cpu(bw_ets_config.tc_bw_max[0]) |
5006 (le16_to_cpu(bw_ets_config.tc_bw_max[1]) << 16);
5007 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5008 vsi->bw_ets_share_credits[i] = bw_ets_config.share_credits[i];
5009 vsi->bw_ets_limit_credits[i] =
5010 le16_to_cpu(bw_ets_config.credits[i]);
5011 /* 3 bits out of 4 for each TC */
5012 vsi->bw_ets_max_quanta[i] = (u8)((tc_bw_max >> (i*4)) & 0x7);
5013 }
Jesse Brandeburg078b5872013-09-25 23:41:14 +00005014
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00005015 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005016}
5017
5018/**
5019 * i40e_vsi_configure_bw_alloc - Configure VSI BW allocation per TC
5020 * @vsi: the VSI being configured
5021 * @enabled_tc: TC bitmap
5022 * @bw_credits: BW shared credits per TC
5023 *
5024 * Returns 0 on success, negative value on failure
5025 **/
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00005026static int i40e_vsi_configure_bw_alloc(struct i40e_vsi *vsi, u8 enabled_tc,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005027 u8 *bw_share)
5028{
5029 struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005030 i40e_status ret;
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00005031 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005032
5033 bw_data.tc_valid_bits = enabled_tc;
5034 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5035 bw_data.tc_bw_credits[i] = bw_share[i];
5036
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005037 ret = i40e_aq_config_vsi_tc_bw(&vsi->back->hw, vsi->seid, &bw_data,
5038 NULL);
5039 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005040 dev_info(&vsi->back->pdev->dev,
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005041 "AQ command Config VSI BW allocation per TC failed = %d\n",
5042 vsi->back->hw.aq.asq_last_status);
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00005043 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005044 }
5045
5046 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5047 vsi->info.qs_handle[i] = bw_data.qs_handles[i];
5048
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00005049 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005050}
5051
5052/**
5053 * i40e_vsi_config_netdev_tc - Setup the netdev TC configuration
5054 * @vsi: the VSI being configured
5055 * @enabled_tc: TC map to be enabled
5056 *
5057 **/
5058static void i40e_vsi_config_netdev_tc(struct i40e_vsi *vsi, u8 enabled_tc)
5059{
5060 struct net_device *netdev = vsi->netdev;
5061 struct i40e_pf *pf = vsi->back;
5062 struct i40e_hw *hw = &pf->hw;
5063 u8 netdev_tc = 0;
5064 int i;
5065 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5066
5067 if (!netdev)
5068 return;
5069
5070 if (!enabled_tc) {
5071 netdev_reset_tc(netdev);
5072 return;
5073 }
5074
5075 /* Set up actual enabled TCs on the VSI */
5076 if (netdev_set_num_tc(netdev, vsi->tc_config.numtc))
5077 return;
5078
5079 /* set per TC queues for the VSI */
5080 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5081 /* Only set TC queues for enabled tcs
5082 *
5083 * e.g. For a VSI that has TC0 and TC3 enabled the
5084 * enabled_tc bitmap would be 0x00001001; the driver
5085 * will set the numtc for netdev as 2 that will be
5086 * referenced by the netdev layer as TC 0 and 1.
5087 */
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08005088 if (vsi->tc_config.enabled_tc & BIT(i))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005089 netdev_set_tc_queue(netdev,
5090 vsi->tc_config.tc_info[i].netdev_tc,
5091 vsi->tc_config.tc_info[i].qcount,
5092 vsi->tc_config.tc_info[i].qoffset);
5093 }
5094
5095 /* Assign UP2TC map for the VSI */
5096 for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
5097 /* Get the actual TC# for the UP */
5098 u8 ets_tc = dcbcfg->etscfg.prioritytable[i];
5099 /* Get the mapped netdev TC# for the UP */
5100 netdev_tc = vsi->tc_config.tc_info[ets_tc].netdev_tc;
5101 netdev_set_prio_tc_map(netdev, i, netdev_tc);
5102 }
5103}
5104
5105/**
5106 * i40e_vsi_update_queue_map - Update our copy of VSi info with new queue map
5107 * @vsi: the VSI being configured
5108 * @ctxt: the ctxt buffer returned from AQ VSI update param command
5109 **/
5110static void i40e_vsi_update_queue_map(struct i40e_vsi *vsi,
5111 struct i40e_vsi_context *ctxt)
5112{
5113 /* copy just the sections touched not the entire info
5114 * since not all sections are valid as returned by
5115 * update vsi params
5116 */
5117 vsi->info.mapping_flags = ctxt->info.mapping_flags;
5118 memcpy(&vsi->info.queue_mapping,
5119 &ctxt->info.queue_mapping, sizeof(vsi->info.queue_mapping));
5120 memcpy(&vsi->info.tc_mapping, ctxt->info.tc_mapping,
5121 sizeof(vsi->info.tc_mapping));
5122}
5123
5124/**
5125 * i40e_vsi_config_tc - Configure VSI Tx Scheduler for given TC map
5126 * @vsi: VSI to be configured
5127 * @enabled_tc: TC bitmap
5128 *
5129 * This configures a particular VSI for TCs that are mapped to the
5130 * given TC bitmap. It uses default bandwidth share for TCs across
5131 * VSIs to configure TC for a particular VSI.
5132 *
5133 * NOTE:
5134 * It is expected that the VSI queues have been quisced before calling
5135 * this function.
5136 **/
5137static int i40e_vsi_config_tc(struct i40e_vsi *vsi, u8 enabled_tc)
5138{
5139 u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
5140 struct i40e_vsi_context ctxt;
5141 int ret = 0;
5142 int i;
5143
5144 /* Check if enabled_tc is same as existing or new TCs */
5145 if (vsi->tc_config.enabled_tc == enabled_tc)
5146 return ret;
5147
5148 /* Enable ETS TCs with equal BW Share for now across all VSIs */
5149 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08005150 if (enabled_tc & BIT(i))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005151 bw_share[i] = 1;
5152 }
5153
5154 ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
5155 if (ret) {
5156 dev_info(&vsi->back->pdev->dev,
5157 "Failed configuring TC map %d for VSI %d\n",
5158 enabled_tc, vsi->seid);
5159 goto out;
5160 }
5161
5162 /* Update Queue Pairs Mapping for currently enabled UPs */
5163 ctxt.seid = vsi->seid;
5164 ctxt.pf_num = vsi->back->hw.pf_id;
5165 ctxt.vf_num = 0;
5166 ctxt.uplink_seid = vsi->uplink_seid;
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07005167 ctxt.info = vsi->info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005168 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
5169
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06005170 if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
5171 ctxt.info.valid_sections |=
5172 cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
5173 ctxt.info.queueing_opt_flags |= I40E_AQ_VSI_QUE_OPT_TCP_ENA;
5174 }
5175
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005176 /* Update the VSI after updating the VSI queue-mapping information */
5177 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
5178 if (ret) {
5179 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005180 "Update vsi tc config failed, err %s aq_err %s\n",
5181 i40e_stat_str(&vsi->back->hw, ret),
5182 i40e_aq_str(&vsi->back->hw,
5183 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005184 goto out;
5185 }
5186 /* update the local VSI info with updated queue map */
5187 i40e_vsi_update_queue_map(vsi, &ctxt);
5188 vsi->info.valid_sections = 0;
5189
5190 /* Update current VSI BW information */
5191 ret = i40e_vsi_get_bw_info(vsi);
5192 if (ret) {
5193 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005194 "Failed updating vsi bw info, err %s aq_err %s\n",
5195 i40e_stat_str(&vsi->back->hw, ret),
5196 i40e_aq_str(&vsi->back->hw,
5197 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005198 goto out;
5199 }
5200
5201 /* Update the netdev TC setup */
5202 i40e_vsi_config_netdev_tc(vsi, enabled_tc);
5203out:
5204 return ret;
5205}
5206
5207/**
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005208 * i40e_veb_config_tc - Configure TCs for given VEB
5209 * @veb: given VEB
5210 * @enabled_tc: TC bitmap
5211 *
5212 * Configures given TC bitmap for VEB (switching) element
5213 **/
5214int i40e_veb_config_tc(struct i40e_veb *veb, u8 enabled_tc)
5215{
5216 struct i40e_aqc_configure_switching_comp_bw_config_data bw_data = {0};
5217 struct i40e_pf *pf = veb->pf;
5218 int ret = 0;
5219 int i;
5220
5221 /* No TCs or already enabled TCs just return */
5222 if (!enabled_tc || veb->enabled_tc == enabled_tc)
5223 return ret;
5224
5225 bw_data.tc_valid_bits = enabled_tc;
5226 /* bw_data.absolute_credits is not set (relative) */
5227
5228 /* Enable ETS TCs with equal BW Share for now */
5229 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08005230 if (enabled_tc & BIT(i))
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005231 bw_data.tc_bw_share_credits[i] = 1;
5232 }
5233
5234 ret = i40e_aq_config_switch_comp_bw_config(&pf->hw, veb->seid,
5235 &bw_data, NULL);
5236 if (ret) {
5237 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005238 "VEB bw config failed, err %s aq_err %s\n",
5239 i40e_stat_str(&pf->hw, ret),
5240 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005241 goto out;
5242 }
5243
5244 /* Update the BW information */
5245 ret = i40e_veb_get_bw_info(veb);
5246 if (ret) {
5247 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005248 "Failed getting veb bw config, err %s aq_err %s\n",
5249 i40e_stat_str(&pf->hw, ret),
5250 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005251 }
5252
5253out:
5254 return ret;
5255}
5256
5257#ifdef CONFIG_I40E_DCB
5258/**
5259 * i40e_dcb_reconfigure - Reconfigure all VEBs and VSIs
5260 * @pf: PF struct
5261 *
5262 * Reconfigure VEB/VSIs on a given PF; it is assumed that
5263 * the caller would've quiesce all the VSIs before calling
5264 * this function
5265 **/
5266static void i40e_dcb_reconfigure(struct i40e_pf *pf)
5267{
5268 u8 tc_map = 0;
5269 int ret;
5270 u8 v;
5271
5272 /* Enable the TCs available on PF to all VEBs */
5273 tc_map = i40e_pf_get_tc_map(pf);
5274 for (v = 0; v < I40E_MAX_VEB; v++) {
5275 if (!pf->veb[v])
5276 continue;
5277 ret = i40e_veb_config_tc(pf->veb[v], tc_map);
5278 if (ret) {
5279 dev_info(&pf->pdev->dev,
5280 "Failed configuring TC for VEB seid=%d\n",
5281 pf->veb[v]->seid);
5282 /* Will try to configure as many components */
5283 }
5284 }
5285
5286 /* Update each VSI */
Mitch Williams505682c2014-05-20 08:01:37 +00005287 for (v = 0; v < pf->num_alloc_vsi; v++) {
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005288 if (!pf->vsi[v])
5289 continue;
5290
5291 /* - Enable all TCs for the LAN VSI
5292 * - For all others keep them at TC0 for now
5293 */
5294 if (v == pf->lan_vsi)
5295 tc_map = i40e_pf_get_tc_map(pf);
5296 else
David Ertmanea6acb72016-09-20 07:10:50 -07005297 tc_map = I40E_DEFAULT_TRAFFIC_CLASS;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005298
5299 ret = i40e_vsi_config_tc(pf->vsi[v], tc_map);
5300 if (ret) {
5301 dev_info(&pf->pdev->dev,
5302 "Failed configuring TC for VSI seid=%d\n",
5303 pf->vsi[v]->seid);
5304 /* Will try to configure as many components */
5305 } else {
Neerav Parikh0672a092014-04-01 07:11:47 +00005306 /* Re-configure VSI vectors based on updated TC map */
5307 i40e_vsi_map_rings_to_vectors(pf->vsi[v]);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005308 if (pf->vsi[v]->netdev)
5309 i40e_dcbnl_set_all(pf->vsi[v]);
5310 }
5311 }
5312}
5313
5314/**
Neerav Parikh2fd75f32014-11-12 00:18:20 +00005315 * i40e_resume_port_tx - Resume port Tx
5316 * @pf: PF struct
5317 *
5318 * Resume a port's Tx and issue a PF reset in case of failure to
5319 * resume.
5320 **/
5321static int i40e_resume_port_tx(struct i40e_pf *pf)
5322{
5323 struct i40e_hw *hw = &pf->hw;
5324 int ret;
5325
5326 ret = i40e_aq_resume_port_tx(hw, NULL);
5327 if (ret) {
5328 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005329 "Resume Port Tx failed, err %s aq_err %s\n",
5330 i40e_stat_str(&pf->hw, ret),
5331 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh2fd75f32014-11-12 00:18:20 +00005332 /* Schedule PF reset to recover */
Jacob Keller0da36b92017-04-19 09:25:55 -04005333 set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
Neerav Parikh2fd75f32014-11-12 00:18:20 +00005334 i40e_service_event_schedule(pf);
5335 }
5336
5337 return ret;
5338}
5339
5340/**
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005341 * i40e_init_pf_dcb - Initialize DCB configuration
5342 * @pf: PF being configured
5343 *
5344 * Query the current DCB configuration and cache it
5345 * in the hardware structure
5346 **/
5347static int i40e_init_pf_dcb(struct i40e_pf *pf)
5348{
5349 struct i40e_hw *hw = &pf->hw;
5350 int err = 0;
5351
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +00005352 /* Do not enable DCB for SW1 and SW2 images even if the FW is capable */
Neerav Parikhf1bbad32016-01-13 16:51:39 -08005353 if (pf->flags & I40E_FLAG_NO_DCB_SUPPORT)
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +00005354 goto out;
5355
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005356 /* Get the initial DCB configuration */
5357 err = i40e_init_dcb(hw);
5358 if (!err) {
5359 /* Device/Function is not DCBX capable */
5360 if ((!hw->func_caps.dcb) ||
5361 (hw->dcbx_status == I40E_DCBX_STATUS_DISABLED)) {
5362 dev_info(&pf->pdev->dev,
5363 "DCBX offload is not supported or is disabled for this PF.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005364 } else {
5365 /* When status is not DISABLED then DCBX in FW */
5366 pf->dcbx_cap = DCB_CAP_DCBX_LLD_MANAGED |
5367 DCB_CAP_DCBX_VER_IEEE;
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005368
5369 pf->flags |= I40E_FLAG_DCB_CAPABLE;
Dave Ertmana0362442016-08-29 17:38:26 -07005370 /* Enable DCB tagging only when more than one TC
5371 * or explicitly disable if only one TC
5372 */
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005373 if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
5374 pf->flags |= I40E_FLAG_DCB_ENABLED;
Dave Ertmana0362442016-08-29 17:38:26 -07005375 else
5376 pf->flags &= ~I40E_FLAG_DCB_ENABLED;
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005377 dev_dbg(&pf->pdev->dev,
5378 "DCBX offload is supported for this PF.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005379 }
Neerav Parikh014269f2014-04-01 07:11:48 +00005380 } else {
Shannon Nelsonaebfc812014-12-11 07:06:38 +00005381 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005382 "Query for DCB configuration failed, err %s aq_err %s\n",
5383 i40e_stat_str(&pf->hw, err),
5384 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005385 }
5386
5387out:
5388 return err;
5389}
5390#endif /* CONFIG_I40E_DCB */
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005391#define SPEED_SIZE 14
5392#define FC_SIZE 8
5393/**
5394 * i40e_print_link_message - print link up or down
5395 * @vsi: the VSI for which link needs a message
5396 */
Matt Jaredc156f852015-08-27 11:42:39 -04005397void i40e_print_link_message(struct i40e_vsi *vsi, bool isup)
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005398{
Filip Sadowski7ec9ba12016-11-08 13:05:13 -08005399 enum i40e_aq_link_speed new_speed;
Shannon Nelsona9165492015-09-03 17:19:00 -04005400 char *speed = "Unknown";
5401 char *fc = "Unknown";
Henry Tieman3e03d7c2016-12-02 12:32:57 -08005402 char *fec = "";
5403 char *an = "";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005404
Filip Sadowski7ec9ba12016-11-08 13:05:13 -08005405 new_speed = vsi->back->hw.phy.link_info.link_speed;
5406
5407 if ((vsi->current_isup == isup) && (vsi->current_speed == new_speed))
Matt Jaredc156f852015-08-27 11:42:39 -04005408 return;
5409 vsi->current_isup = isup;
Filip Sadowski7ec9ba12016-11-08 13:05:13 -08005410 vsi->current_speed = new_speed;
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005411 if (!isup) {
5412 netdev_info(vsi->netdev, "NIC Link is Down\n");
5413 return;
5414 }
5415
Greg Rose148c2d82014-12-11 07:06:27 +00005416 /* Warn user if link speed on NPAR enabled partition is not at
5417 * least 10GB
5418 */
5419 if (vsi->back->hw.func_caps.npar_enable &&
5420 (vsi->back->hw.phy.link_info.link_speed == I40E_LINK_SPEED_1GB ||
5421 vsi->back->hw.phy.link_info.link_speed == I40E_LINK_SPEED_100MB))
5422 netdev_warn(vsi->netdev,
5423 "The partition detected link speed that is less than 10Gbps\n");
5424
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005425 switch (vsi->back->hw.phy.link_info.link_speed) {
5426 case I40E_LINK_SPEED_40GB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005427 speed = "40 G";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005428 break;
Jesse Brandeburgae24b402015-03-27 00:12:09 -07005429 case I40E_LINK_SPEED_20GB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005430 speed = "20 G";
Jesse Brandeburgae24b402015-03-27 00:12:09 -07005431 break;
Carolyn Wyborny31232372016-11-21 13:03:48 -08005432 case I40E_LINK_SPEED_25GB:
5433 speed = "25 G";
5434 break;
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005435 case I40E_LINK_SPEED_10GB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005436 speed = "10 G";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005437 break;
5438 case I40E_LINK_SPEED_1GB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005439 speed = "1000 M";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005440 break;
Mitch Williams5960d332014-09-13 07:40:47 +00005441 case I40E_LINK_SPEED_100MB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005442 speed = "100 M";
Mitch Williams5960d332014-09-13 07:40:47 +00005443 break;
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005444 default:
5445 break;
5446 }
5447
5448 switch (vsi->back->hw.fc.current_mode) {
5449 case I40E_FC_FULL:
Shannon Nelsona9165492015-09-03 17:19:00 -04005450 fc = "RX/TX";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005451 break;
5452 case I40E_FC_TX_PAUSE:
Shannon Nelsona9165492015-09-03 17:19:00 -04005453 fc = "TX";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005454 break;
5455 case I40E_FC_RX_PAUSE:
Shannon Nelsona9165492015-09-03 17:19:00 -04005456 fc = "RX";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005457 break;
5458 default:
Shannon Nelsona9165492015-09-03 17:19:00 -04005459 fc = "None";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005460 break;
5461 }
5462
Henry Tieman3e03d7c2016-12-02 12:32:57 -08005463 if (vsi->back->hw.phy.link_info.link_speed == I40E_LINK_SPEED_25GB) {
5464 fec = ", FEC: None";
5465 an = ", Autoneg: False";
5466
5467 if (vsi->back->hw.phy.link_info.an_info & I40E_AQ_AN_COMPLETED)
5468 an = ", Autoneg: True";
5469
5470 if (vsi->back->hw.phy.link_info.fec_info &
5471 I40E_AQ_CONFIG_FEC_KR_ENA)
5472 fec = ", FEC: CL74 FC-FEC/BASE-R";
5473 else if (vsi->back->hw.phy.link_info.fec_info &
5474 I40E_AQ_CONFIG_FEC_RS_ENA)
5475 fec = ", FEC: CL108 RS-FEC";
5476 }
5477
5478 netdev_info(vsi->netdev, "NIC Link is Up, %sbps Full Duplex%s%s, Flow Control: %s\n",
5479 speed, fec, an, fc);
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005480}
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005481
5482/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005483 * i40e_up_complete - Finish the last steps of bringing up a connection
5484 * @vsi: the VSI being configured
5485 **/
5486static int i40e_up_complete(struct i40e_vsi *vsi)
5487{
5488 struct i40e_pf *pf = vsi->back;
5489 int err;
5490
5491 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
5492 i40e_vsi_configure_msix(vsi);
5493 else
5494 i40e_configure_msi_and_legacy(vsi);
5495
5496 /* start rings */
Filip Sadowski3aa7b742016-10-11 15:26:58 -07005497 err = i40e_vsi_start_rings(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005498 if (err)
5499 return err;
5500
Jacob Keller0da36b92017-04-19 09:25:55 -04005501 clear_bit(__I40E_VSI_DOWN, vsi->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005502 i40e_napi_enable_all(vsi);
5503 i40e_vsi_enable_irq(vsi);
5504
5505 if ((pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP) &&
5506 (vsi->netdev)) {
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005507 i40e_print_link_message(vsi, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005508 netif_tx_start_all_queues(vsi->netdev);
5509 netif_carrier_on(vsi->netdev);
Anjali Singhai6d779b42013-09-28 06:00:02 +00005510 } else if (vsi->netdev) {
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005511 i40e_print_link_message(vsi, false);
Carolyn Wyborny7b592f62014-07-10 07:58:19 +00005512 /* need to check for qualified module here*/
5513 if ((pf->hw.phy.link_info.link_info &
5514 I40E_AQ_MEDIA_AVAILABLE) &&
5515 (!(pf->hw.phy.link_info.an_info &
5516 I40E_AQ_QUALIFIED_MODULE)))
5517 netdev_err(vsi->netdev,
5518 "the driver failed to link because an unqualified module was detected.");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005519 }
Anjali Singhai Jainca64fa42014-02-11 08:26:30 +00005520
5521 /* replay FDIR SB filters */
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005522 if (vsi->type == I40E_VSI_FDIR) {
5523 /* reset fd counters */
Jacob Keller097dbf52017-02-06 14:38:46 -08005524 pf->fd_add_err = 0;
5525 pf->fd_atr_cnt = 0;
Anjali Singhai Jainca64fa42014-02-11 08:26:30 +00005526 i40e_fdir_filter_restore(vsi);
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005527 }
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06005528
5529 /* On the next run of the service_task, notify any clients of the new
5530 * opened netdev
5531 */
5532 pf->flags |= I40E_FLAG_SERVICE_CLIENT_REQUESTED;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005533 i40e_service_event_schedule(pf);
5534
5535 return 0;
5536}
5537
5538/**
5539 * i40e_vsi_reinit_locked - Reset the VSI
5540 * @vsi: the VSI being configured
5541 *
5542 * Rebuild the ring structs after some configuration
5543 * has changed, e.g. MTU size.
5544 **/
5545static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi)
5546{
5547 struct i40e_pf *pf = vsi->back;
5548
5549 WARN_ON(in_interrupt());
Jacob Keller0da36b92017-04-19 09:25:55 -04005550 while (test_and_set_bit(__I40E_CONFIG_BUSY, pf->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005551 usleep_range(1000, 2000);
5552 i40e_down(vsi);
5553
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005554 i40e_up(vsi);
Jacob Keller0da36b92017-04-19 09:25:55 -04005555 clear_bit(__I40E_CONFIG_BUSY, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005556}
5557
5558/**
5559 * i40e_up - Bring the connection back up after being down
5560 * @vsi: the VSI being configured
5561 **/
5562int i40e_up(struct i40e_vsi *vsi)
5563{
5564 int err;
5565
5566 err = i40e_vsi_configure(vsi);
5567 if (!err)
5568 err = i40e_up_complete(vsi);
5569
5570 return err;
5571}
5572
5573/**
5574 * i40e_down - Shutdown the connection processing
5575 * @vsi: the VSI being stopped
5576 **/
5577void i40e_down(struct i40e_vsi *vsi)
5578{
5579 int i;
5580
5581 /* It is assumed that the caller of this function
Jacob Kellerd19cb642017-04-21 13:38:05 -07005582 * sets the vsi->state __I40E_VSI_DOWN bit.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005583 */
5584 if (vsi->netdev) {
5585 netif_carrier_off(vsi->netdev);
5586 netif_tx_disable(vsi->netdev);
5587 }
5588 i40e_vsi_disable_irq(vsi);
Filip Sadowski3aa7b742016-10-11 15:26:58 -07005589 i40e_vsi_stop_rings(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005590 i40e_napi_disable_all(vsi);
5591
5592 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck9f65e152013-09-28 06:00:58 +00005593 i40e_clean_tx_ring(vsi->tx_rings[i]);
Björn Töpel74608d12017-05-24 07:55:35 +02005594 if (i40e_enabled_xdp_vsi(vsi))
5595 i40e_clean_tx_ring(vsi->xdp_rings[i]);
Alexander Duyck9f65e152013-09-28 06:00:58 +00005596 i40e_clean_rx_ring(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005597 }
Catherine Sullivanf980d442016-05-16 10:26:34 -07005598
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005599}
5600
5601/**
5602 * i40e_setup_tc - configure multiple traffic classes
5603 * @netdev: net device to configure
5604 * @tc: number of traffic classes to enable
5605 **/
5606static int i40e_setup_tc(struct net_device *netdev, u8 tc)
5607{
5608 struct i40e_netdev_priv *np = netdev_priv(netdev);
5609 struct i40e_vsi *vsi = np->vsi;
5610 struct i40e_pf *pf = vsi->back;
5611 u8 enabled_tc = 0;
5612 int ret = -EINVAL;
5613 int i;
5614
5615 /* Check if DCB enabled to continue */
5616 if (!(pf->flags & I40E_FLAG_DCB_ENABLED)) {
5617 netdev_info(netdev, "DCB is not enabled for adapter\n");
5618 goto exit;
5619 }
5620
5621 /* Check if MFP enabled */
5622 if (pf->flags & I40E_FLAG_MFP_ENABLED) {
5623 netdev_info(netdev, "Configuring TC not supported in MFP mode\n");
5624 goto exit;
5625 }
5626
5627 /* Check whether tc count is within enabled limit */
5628 if (tc > i40e_pf_get_num_tc(pf)) {
5629 netdev_info(netdev, "TC count greater than enabled on link for adapter\n");
5630 goto exit;
5631 }
5632
5633 /* Generate TC map for number of tc requested */
5634 for (i = 0; i < tc; i++)
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08005635 enabled_tc |= BIT(i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005636
5637 /* Requesting same TC configuration as already enabled */
5638 if (enabled_tc == vsi->tc_config.enabled_tc)
5639 return 0;
5640
5641 /* Quiesce VSI queues */
5642 i40e_quiesce_vsi(vsi);
5643
5644 /* Configure VSI for enabled TCs */
5645 ret = i40e_vsi_config_tc(vsi, enabled_tc);
5646 if (ret) {
5647 netdev_info(netdev, "Failed configuring TC for VSI seid=%d\n",
5648 vsi->seid);
5649 goto exit;
5650 }
5651
5652 /* Unquiesce VSI */
5653 i40e_unquiesce_vsi(vsi);
5654
5655exit:
5656 return ret;
5657}
5658
Jiri Pirkoa5fcf8a2017-06-06 17:00:16 +02005659static int __i40e_setup_tc(struct net_device *netdev, u32 handle,
5660 u32 chain_index, __be16 proto,
John Fastabend16e5cc62016-02-16 21:16:43 -08005661 struct tc_to_netdev *tc)
John Fastabende4c67342016-02-16 21:16:15 -08005662{
Amritha Nambiar56f36ac2017-03-15 10:39:25 -07005663 if (tc->type != TC_SETUP_MQPRIO)
John Fastabende4c67342016-02-16 21:16:15 -08005664 return -EINVAL;
Amritha Nambiar56f36ac2017-03-15 10:39:25 -07005665
5666 tc->mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
5667
5668 return i40e_setup_tc(netdev, tc->mqprio->num_tc);
John Fastabende4c67342016-02-16 21:16:15 -08005669}
5670
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005671/**
5672 * i40e_open - Called when a network interface is made active
5673 * @netdev: network interface device structure
5674 *
5675 * The open entry point is called when a network interface is made
5676 * active by the system (IFF_UP). At this point all resources needed
5677 * for transmit and receive operations are allocated, the interrupt
5678 * handler is registered with the OS, the netdev watchdog subtask is
5679 * enabled, and the stack is notified that the interface is ready.
5680 *
5681 * Returns 0 on success, negative value on failure
5682 **/
Vasu Dev38e00432014-08-01 13:27:03 -07005683int i40e_open(struct net_device *netdev)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005684{
5685 struct i40e_netdev_priv *np = netdev_priv(netdev);
5686 struct i40e_vsi *vsi = np->vsi;
5687 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005688 int err;
5689
Shannon Nelson4eb3f762014-03-06 08:59:58 +00005690 /* disallow open during test or if eeprom is broken */
Jacob Keller0da36b92017-04-19 09:25:55 -04005691 if (test_bit(__I40E_TESTING, pf->state) ||
5692 test_bit(__I40E_BAD_EEPROM, pf->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005693 return -EBUSY;
5694
5695 netif_carrier_off(netdev);
5696
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005697 err = i40e_vsi_open(vsi);
5698 if (err)
5699 return err;
5700
Jesse Brandeburg059dab62014-04-01 09:07:20 +00005701 /* configure global TSO hardware offload settings */
5702 wr32(&pf->hw, I40E_GLLAN_TSOMSK_F, be32_to_cpu(TCP_FLAG_PSH |
5703 TCP_FLAG_FIN) >> 16);
5704 wr32(&pf->hw, I40E_GLLAN_TSOMSK_M, be32_to_cpu(TCP_FLAG_PSH |
5705 TCP_FLAG_FIN |
5706 TCP_FLAG_CWR) >> 16);
5707 wr32(&pf->hw, I40E_GLLAN_TSOMSK_L, be32_to_cpu(TCP_FLAG_CWR) >> 16);
5708
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07005709 udp_tunnel_get_rx_info(netdev);
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06005710
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005711 return 0;
5712}
5713
5714/**
5715 * i40e_vsi_open -
5716 * @vsi: the VSI to open
5717 *
5718 * Finish initialization of the VSI.
5719 *
5720 * Returns 0 on success, negative value on failure
Maciej Sosin373149f2017-04-05 07:50:55 -04005721 *
5722 * Note: expects to be called while under rtnl_lock()
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005723 **/
5724int i40e_vsi_open(struct i40e_vsi *vsi)
5725{
5726 struct i40e_pf *pf = vsi->back;
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00005727 char int_name[I40E_INT_NAME_STR_LEN];
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005728 int err;
5729
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005730 /* allocate descriptors */
5731 err = i40e_vsi_setup_tx_resources(vsi);
5732 if (err)
5733 goto err_setup_tx;
5734 err = i40e_vsi_setup_rx_resources(vsi);
5735 if (err)
5736 goto err_setup_rx;
5737
5738 err = i40e_vsi_configure(vsi);
5739 if (err)
5740 goto err_setup_rx;
5741
Shannon Nelsonc22e3c62014-03-14 07:32:25 +00005742 if (vsi->netdev) {
5743 snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
5744 dev_driver_string(&pf->pdev->dev), vsi->netdev->name);
5745 err = i40e_vsi_request_irq(vsi, int_name);
5746 if (err)
5747 goto err_setup_rx;
5748
5749 /* Notify the stack of the actual queue counts. */
5750 err = netif_set_real_num_tx_queues(vsi->netdev,
5751 vsi->num_queue_pairs);
5752 if (err)
5753 goto err_set_queues;
5754
5755 err = netif_set_real_num_rx_queues(vsi->netdev,
5756 vsi->num_queue_pairs);
5757 if (err)
5758 goto err_set_queues;
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00005759
5760 } else if (vsi->type == I40E_VSI_FDIR) {
Carolyn Wybornye240f672014-12-11 07:06:37 +00005761 snprintf(int_name, sizeof(int_name) - 1, "%s-%s:fdir",
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +00005762 dev_driver_string(&pf->pdev->dev),
5763 dev_name(&pf->pdev->dev));
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00005764 err = i40e_vsi_request_irq(vsi, int_name);
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +00005765
Shannon Nelsonc22e3c62014-03-14 07:32:25 +00005766 } else {
Jean Sacrence9ccb12014-05-01 14:31:18 +00005767 err = -EINVAL;
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005768 goto err_setup_rx;
5769 }
Anjali Singhai Jain25946dd2013-11-26 10:49:14 +00005770
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005771 err = i40e_up_complete(vsi);
5772 if (err)
5773 goto err_up_complete;
5774
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005775 return 0;
5776
5777err_up_complete:
5778 i40e_down(vsi);
Anjali Singhai Jain25946dd2013-11-26 10:49:14 +00005779err_set_queues:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005780 i40e_vsi_free_irq(vsi);
5781err_setup_rx:
5782 i40e_vsi_free_rx_resources(vsi);
5783err_setup_tx:
5784 i40e_vsi_free_tx_resources(vsi);
5785 if (vsi == pf->vsi[pf->lan_vsi])
Maciej Sosin373149f2017-04-05 07:50:55 -04005786 i40e_do_reset(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED), true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005787
5788 return err;
5789}
5790
5791/**
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00005792 * i40e_fdir_filter_exit - Cleans up the Flow Director accounting
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00005793 * @pf: Pointer to PF
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00005794 *
5795 * This function destroys the hlist where all the Flow Director
5796 * filters were saved.
5797 **/
5798static void i40e_fdir_filter_exit(struct i40e_pf *pf)
5799{
5800 struct i40e_fdir_filter *filter;
Jacob Keller0e588de2017-02-06 14:38:50 -08005801 struct i40e_flex_pit *pit_entry, *tmp;
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00005802 struct hlist_node *node2;
5803
5804 hlist_for_each_entry_safe(filter, node2,
5805 &pf->fdir_filter_list, fdir_node) {
5806 hlist_del(&filter->fdir_node);
5807 kfree(filter);
5808 }
Jacob Keller097dbf52017-02-06 14:38:46 -08005809
Jacob Keller0e588de2017-02-06 14:38:50 -08005810 list_for_each_entry_safe(pit_entry, tmp, &pf->l3_flex_pit_list, list) {
5811 list_del(&pit_entry->list);
5812 kfree(pit_entry);
5813 }
5814 INIT_LIST_HEAD(&pf->l3_flex_pit_list);
5815
5816 list_for_each_entry_safe(pit_entry, tmp, &pf->l4_flex_pit_list, list) {
5817 list_del(&pit_entry->list);
5818 kfree(pit_entry);
5819 }
5820 INIT_LIST_HEAD(&pf->l4_flex_pit_list);
5821
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00005822 pf->fdir_pf_active_filters = 0;
Jacob Keller097dbf52017-02-06 14:38:46 -08005823 pf->fd_tcp4_filter_cnt = 0;
5824 pf->fd_udp4_filter_cnt = 0;
Jacob Kellerf223c872017-02-06 14:38:51 -08005825 pf->fd_sctp4_filter_cnt = 0;
Jacob Keller097dbf52017-02-06 14:38:46 -08005826 pf->fd_ip4_filter_cnt = 0;
Jacob Keller3bcee1e2017-02-06 14:38:46 -08005827
5828 /* Reprogram the default input set for TCP/IPv4 */
5829 i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_TCP,
5830 I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
5831 I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
5832
5833 /* Reprogram the default input set for UDP/IPv4 */
5834 i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_UDP,
5835 I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
5836 I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
5837
5838 /* Reprogram the default input set for SCTP/IPv4 */
5839 i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_SCTP,
5840 I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
5841 I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
5842
5843 /* Reprogram the default input set for Other/IPv4 */
5844 i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_OTHER,
5845 I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00005846}
5847
5848/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005849 * i40e_close - Disables a network interface
5850 * @netdev: network interface device structure
5851 *
5852 * The close entry point is called when an interface is de-activated
5853 * by the OS. The hardware is still under the driver's control, but
5854 * this netdev interface is disabled.
5855 *
5856 * Returns 0, this is not allowed to fail
5857 **/
Vasu Dev38e00432014-08-01 13:27:03 -07005858int i40e_close(struct net_device *netdev)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005859{
5860 struct i40e_netdev_priv *np = netdev_priv(netdev);
5861 struct i40e_vsi *vsi = np->vsi;
5862
Shannon Nelson90ef8d42014-03-14 07:32:26 +00005863 i40e_vsi_close(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005864
5865 return 0;
5866}
5867
5868/**
5869 * i40e_do_reset - Start a PF or Core Reset sequence
5870 * @pf: board private structure
5871 * @reset_flags: which reset is requested
Maciej Sosin373149f2017-04-05 07:50:55 -04005872 * @lock_acquired: indicates whether or not the lock has been acquired
5873 * before this function was called.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005874 *
5875 * The essential difference in resets is that the PF Reset
5876 * doesn't clear the packet buffers, doesn't reset the PE
5877 * firmware, and doesn't bother the other PFs on the chip.
5878 **/
Maciej Sosin373149f2017-04-05 07:50:55 -04005879void i40e_do_reset(struct i40e_pf *pf, u32 reset_flags, bool lock_acquired)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005880{
5881 u32 val;
5882
5883 WARN_ON(in_interrupt());
5884
Mitch Williams263fc482014-04-23 04:50:11 +00005885
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005886 /* do the biggest reset indicated */
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005887 if (reset_flags & BIT_ULL(__I40E_GLOBAL_RESET_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005888
5889 /* Request a Global Reset
5890 *
5891 * This will start the chip's countdown to the actual full
5892 * chip reset event, and a warning interrupt to be sent
5893 * to all PFs, including the requestor. Our handler
5894 * for the warning interrupt will deal with the shutdown
5895 * and recovery of the switch setup.
5896 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005897 dev_dbg(&pf->pdev->dev, "GlobalR requested\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005898 val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
5899 val |= I40E_GLGEN_RTRIG_GLOBR_MASK;
5900 wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
5901
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005902 } else if (reset_flags & BIT_ULL(__I40E_CORE_RESET_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005903
5904 /* Request a Core Reset
5905 *
5906 * Same as Global Reset, except does *not* include the MAC/PHY
5907 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005908 dev_dbg(&pf->pdev->dev, "CoreR requested\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005909 val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
5910 val |= I40E_GLGEN_RTRIG_CORER_MASK;
5911 wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
5912 i40e_flush(&pf->hw);
5913
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005914 } else if (reset_flags & BIT_ULL(__I40E_PF_RESET_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005915
5916 /* Request a PF Reset
5917 *
5918 * Resets only the PF-specific registers
5919 *
5920 * This goes directly to the tear-down and rebuild of
5921 * the switch, since we need to do all the recovery as
5922 * for the Core Reset.
5923 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005924 dev_dbg(&pf->pdev->dev, "PFR requested\n");
Maciej Sosin373149f2017-04-05 07:50:55 -04005925 i40e_handle_reset_warning(pf, lock_acquired);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005926
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005927 } else if (reset_flags & BIT_ULL(__I40E_REINIT_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005928 int v;
5929
5930 /* Find the VSI(s) that requested a re-init */
5931 dev_info(&pf->pdev->dev,
5932 "VSI reinit requested\n");
Mitch Williams505682c2014-05-20 08:01:37 +00005933 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005934 struct i40e_vsi *vsi = pf->vsi[v];
Jesse Brandeburg6995b362015-08-28 17:55:54 -04005935
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005936 if (vsi != NULL &&
Jacob Kellerd19cb642017-04-21 13:38:05 -07005937 test_and_clear_bit(__I40E_VSI_REINIT_REQUESTED,
Jacob Keller0da36b92017-04-19 09:25:55 -04005938 vsi->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005939 i40e_vsi_reinit_locked(pf->vsi[v]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005940 }
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005941 } else if (reset_flags & BIT_ULL(__I40E_DOWN_REQUESTED)) {
Neerav Parikhb5d06f02014-06-03 23:50:17 +00005942 int v;
5943
5944 /* Find the VSI(s) that needs to be brought down */
5945 dev_info(&pf->pdev->dev, "VSI down requested\n");
5946 for (v = 0; v < pf->num_alloc_vsi; v++) {
5947 struct i40e_vsi *vsi = pf->vsi[v];
Jesse Brandeburg6995b362015-08-28 17:55:54 -04005948
Neerav Parikhb5d06f02014-06-03 23:50:17 +00005949 if (vsi != NULL &&
Jacob Kellerd19cb642017-04-21 13:38:05 -07005950 test_and_clear_bit(__I40E_VSI_DOWN_REQUESTED,
Jacob Keller0da36b92017-04-19 09:25:55 -04005951 vsi->state)) {
5952 set_bit(__I40E_VSI_DOWN, vsi->state);
Neerav Parikhb5d06f02014-06-03 23:50:17 +00005953 i40e_down(vsi);
Neerav Parikhb5d06f02014-06-03 23:50:17 +00005954 }
5955 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005956 } else {
5957 dev_info(&pf->pdev->dev,
5958 "bad reset request 0x%08x\n", reset_flags);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005959 }
5960}
5961
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005962#ifdef CONFIG_I40E_DCB
5963/**
5964 * i40e_dcb_need_reconfig - Check if DCB needs reconfig
5965 * @pf: board private structure
5966 * @old_cfg: current DCB config
5967 * @new_cfg: new DCB config
5968 **/
5969bool i40e_dcb_need_reconfig(struct i40e_pf *pf,
5970 struct i40e_dcbx_config *old_cfg,
5971 struct i40e_dcbx_config *new_cfg)
5972{
5973 bool need_reconfig = false;
5974
5975 /* Check if ETS configuration has changed */
5976 if (memcmp(&new_cfg->etscfg,
5977 &old_cfg->etscfg,
5978 sizeof(new_cfg->etscfg))) {
5979 /* If Priority Table has changed reconfig is needed */
5980 if (memcmp(&new_cfg->etscfg.prioritytable,
5981 &old_cfg->etscfg.prioritytable,
5982 sizeof(new_cfg->etscfg.prioritytable))) {
5983 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005984 dev_dbg(&pf->pdev->dev, "ETS UP2TC changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005985 }
5986
5987 if (memcmp(&new_cfg->etscfg.tcbwtable,
5988 &old_cfg->etscfg.tcbwtable,
5989 sizeof(new_cfg->etscfg.tcbwtable)))
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005990 dev_dbg(&pf->pdev->dev, "ETS TC BW Table changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005991
5992 if (memcmp(&new_cfg->etscfg.tsatable,
5993 &old_cfg->etscfg.tsatable,
5994 sizeof(new_cfg->etscfg.tsatable)))
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005995 dev_dbg(&pf->pdev->dev, "ETS TSA Table changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005996 }
5997
5998 /* Check if PFC configuration has changed */
5999 if (memcmp(&new_cfg->pfc,
6000 &old_cfg->pfc,
6001 sizeof(new_cfg->pfc))) {
6002 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006003 dev_dbg(&pf->pdev->dev, "PFC config change detected.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006004 }
6005
6006 /* Check if APP Table has changed */
6007 if (memcmp(&new_cfg->app,
6008 &old_cfg->app,
Dave Jones3d9667a2014-01-27 23:11:09 -05006009 sizeof(new_cfg->app))) {
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006010 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006011 dev_dbg(&pf->pdev->dev, "APP Table change detected.\n");
Dave Jones3d9667a2014-01-27 23:11:09 -05006012 }
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006013
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04006014 dev_dbg(&pf->pdev->dev, "dcb need_reconfig=%d\n", need_reconfig);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006015 return need_reconfig;
6016}
6017
6018/**
6019 * i40e_handle_lldp_event - Handle LLDP Change MIB event
6020 * @pf: board private structure
6021 * @e: event info posted on ARQ
6022 **/
6023static int i40e_handle_lldp_event(struct i40e_pf *pf,
6024 struct i40e_arq_event_info *e)
6025{
6026 struct i40e_aqc_lldp_get_mib *mib =
6027 (struct i40e_aqc_lldp_get_mib *)&e->desc.params.raw;
6028 struct i40e_hw *hw = &pf->hw;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006029 struct i40e_dcbx_config tmp_dcbx_cfg;
6030 bool need_reconfig = false;
6031 int ret = 0;
6032 u8 type;
6033
Neerav Parikh4d9b6042014-05-22 06:31:51 +00006034 /* Not DCB capable or capability disabled */
David Ertmanea6acb72016-09-20 07:10:50 -07006035 if (!(pf->flags & I40E_FLAG_DCB_CAPABLE))
Neerav Parikh4d9b6042014-05-22 06:31:51 +00006036 return ret;
6037
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006038 /* Ignore if event is not for Nearest Bridge */
6039 type = ((mib->type >> I40E_AQ_LLDP_BRIDGE_TYPE_SHIFT)
6040 & I40E_AQ_LLDP_BRIDGE_TYPE_MASK);
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04006041 dev_dbg(&pf->pdev->dev, "LLDP event mib bridge type 0x%x\n", type);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006042 if (type != I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE)
6043 return ret;
6044
6045 /* Check MIB Type and return if event for Remote MIB update */
6046 type = mib->type & I40E_AQ_LLDP_MIB_TYPE_MASK;
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00006047 dev_dbg(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04006048 "LLDP event mib type %s\n", type ? "remote" : "local");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006049 if (type == I40E_AQ_LLDP_MIB_REMOTE) {
6050 /* Update the remote cached instance and return */
6051 ret = i40e_aq_get_dcb_config(hw, I40E_AQ_LLDP_MIB_REMOTE,
6052 I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE,
6053 &hw->remote_dcbx_config);
6054 goto exit;
6055 }
6056
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00006057 /* Store the old configuration */
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07006058 tmp_dcbx_cfg = hw->local_dcbx_config;
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00006059
Neerav Parikh750fcbc2015-02-24 06:58:47 +00006060 /* Reset the old DCBx configuration data */
6061 memset(&hw->local_dcbx_config, 0, sizeof(hw->local_dcbx_config));
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00006062 /* Get updated DCBX data from firmware */
6063 ret = i40e_get_dcb_config(&pf->hw);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006064 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006065 dev_info(&pf->pdev->dev,
6066 "Failed querying DCB configuration data from firmware, err %s aq_err %s\n",
6067 i40e_stat_str(&pf->hw, ret),
6068 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006069 goto exit;
6070 }
6071
6072 /* No change detected in DCBX configs */
Neerav Parikh750fcbc2015-02-24 06:58:47 +00006073 if (!memcmp(&tmp_dcbx_cfg, &hw->local_dcbx_config,
6074 sizeof(tmp_dcbx_cfg))) {
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006075 dev_dbg(&pf->pdev->dev, "No change detected in DCBX configuration.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006076 goto exit;
6077 }
6078
Neerav Parikh750fcbc2015-02-24 06:58:47 +00006079 need_reconfig = i40e_dcb_need_reconfig(pf, &tmp_dcbx_cfg,
6080 &hw->local_dcbx_config);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006081
Neerav Parikh750fcbc2015-02-24 06:58:47 +00006082 i40e_dcbnl_flush_apps(pf, &tmp_dcbx_cfg, &hw->local_dcbx_config);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006083
6084 if (!need_reconfig)
6085 goto exit;
6086
Neerav Parikh4d9b6042014-05-22 06:31:51 +00006087 /* Enable DCB tagging only when more than one TC */
Neerav Parikh750fcbc2015-02-24 06:58:47 +00006088 if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
Neerav Parikh4d9b6042014-05-22 06:31:51 +00006089 pf->flags |= I40E_FLAG_DCB_ENABLED;
6090 else
6091 pf->flags &= ~I40E_FLAG_DCB_ENABLED;
6092
Jacob Keller0da36b92017-04-19 09:25:55 -04006093 set_bit(__I40E_PORT_SUSPENDED, pf->state);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006094 /* Reconfiguration needed quiesce all VSIs */
6095 i40e_pf_quiesce_all_vsi(pf);
6096
6097 /* Changes in configuration update VEB/VSI */
6098 i40e_dcb_reconfigure(pf);
6099
Neerav Parikh2fd75f32014-11-12 00:18:20 +00006100 ret = i40e_resume_port_tx(pf);
6101
Jacob Keller0da36b92017-04-19 09:25:55 -04006102 clear_bit(__I40E_PORT_SUSPENDED, pf->state);
Neerav Parikh2fd75f32014-11-12 00:18:20 +00006103 /* In case of error no point in resuming VSIs */
Neerav Parikh69129dc2014-11-12 00:18:46 +00006104 if (ret)
6105 goto exit;
6106
Neerav Parikh3fe06f42016-02-17 16:12:15 -08006107 /* Wait for the PF's queues to be disabled */
6108 ret = i40e_pf_wait_queues_disabled(pf);
Parikh, Neerav11e47702015-02-21 06:43:55 +00006109 if (ret) {
6110 /* Schedule PF reset to recover */
Jacob Keller0da36b92017-04-19 09:25:55 -04006111 set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
Parikh, Neerav11e47702015-02-21 06:43:55 +00006112 i40e_service_event_schedule(pf);
6113 } else {
Neerav Parikh2fd75f32014-11-12 00:18:20 +00006114 i40e_pf_unquiesce_all_vsi(pf);
Mitch Williams0ef2d5a2017-01-24 10:24:00 -08006115 pf->flags |= (I40E_FLAG_SERVICE_CLIENT_REQUESTED |
6116 I40E_FLAG_CLIENT_L2_CHANGE);
Parikh, Neerav11e47702015-02-21 06:43:55 +00006117 }
6118
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006119exit:
6120 return ret;
6121}
6122#endif /* CONFIG_I40E_DCB */
6123
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006124/**
Anjali Singhai Jain233261862013-11-26 10:49:22 +00006125 * i40e_do_reset_safe - Protected reset path for userland calls.
6126 * @pf: board private structure
6127 * @reset_flags: which reset is requested
6128 *
6129 **/
6130void i40e_do_reset_safe(struct i40e_pf *pf, u32 reset_flags)
6131{
6132 rtnl_lock();
Maciej Sosin373149f2017-04-05 07:50:55 -04006133 i40e_do_reset(pf, reset_flags, true);
Anjali Singhai Jain233261862013-11-26 10:49:22 +00006134 rtnl_unlock();
6135}
6136
6137/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006138 * i40e_handle_lan_overflow_event - Handler for LAN queue overflow event
6139 * @pf: board private structure
6140 * @e: event info posted on ARQ
6141 *
6142 * Handler for LAN Queue Overflow Event generated by the firmware for PF
6143 * and VF queues
6144 **/
6145static void i40e_handle_lan_overflow_event(struct i40e_pf *pf,
6146 struct i40e_arq_event_info *e)
6147{
6148 struct i40e_aqc_lan_overflow *data =
6149 (struct i40e_aqc_lan_overflow *)&e->desc.params.raw;
6150 u32 queue = le32_to_cpu(data->prtdcb_rupto);
6151 u32 qtx_ctl = le32_to_cpu(data->otx_ctl);
6152 struct i40e_hw *hw = &pf->hw;
6153 struct i40e_vf *vf;
6154 u16 vf_id;
6155
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006156 dev_dbg(&pf->pdev->dev, "overflow Rx Queue Number = %d QTX_CTL=0x%08x\n",
6157 queue, qtx_ctl);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006158
6159 /* Queue belongs to VF, find the VF and issue VF reset */
6160 if (((qtx_ctl & I40E_QTX_CTL_PFVF_Q_MASK)
6161 >> I40E_QTX_CTL_PFVF_Q_SHIFT) == I40E_QTX_CTL_VF_QUEUE) {
6162 vf_id = (u16)((qtx_ctl & I40E_QTX_CTL_VFVM_INDX_MASK)
6163 >> I40E_QTX_CTL_VFVM_INDX_SHIFT);
6164 vf_id -= hw->func_caps.vf_base_id;
6165 vf = &pf->vf[vf_id];
6166 i40e_vc_notify_vf_reset(vf);
6167 /* Allow VF to process pending reset notification */
6168 msleep(20);
6169 i40e_reset_vf(vf, false);
6170 }
6171}
6172
6173/**
Anjali Singhai Jain12957382014-06-04 04:22:47 +00006174 * i40e_get_cur_guaranteed_fd_count - Get the consumed guaranteed FD filters
6175 * @pf: board private structure
6176 **/
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006177u32 i40e_get_cur_guaranteed_fd_count(struct i40e_pf *pf)
Anjali Singhai Jain12957382014-06-04 04:22:47 +00006178{
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006179 u32 val, fcnt_prog;
Anjali Singhai Jain12957382014-06-04 04:22:47 +00006180
6181 val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
6182 fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK);
6183 return fcnt_prog;
6184}
6185
6186/**
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006187 * i40e_get_current_fd_count - Get total FD filters programmed for this PF
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006188 * @pf: board private structure
6189 **/
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006190u32 i40e_get_current_fd_count(struct i40e_pf *pf)
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006191{
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006192 u32 val, fcnt_prog;
6193
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006194 val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
6195 fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK) +
6196 ((val & I40E_PFQF_FDSTAT_BEST_CNT_MASK) >>
6197 I40E_PFQF_FDSTAT_BEST_CNT_SHIFT);
6198 return fcnt_prog;
6199}
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006200
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006201/**
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006202 * i40e_get_global_fd_count - Get total FD filters programmed on device
6203 * @pf: board private structure
6204 **/
6205u32 i40e_get_global_fd_count(struct i40e_pf *pf)
6206{
6207 u32 val, fcnt_prog;
6208
6209 val = rd32(&pf->hw, I40E_GLQF_FDCNT_0);
6210 fcnt_prog = (val & I40E_GLQF_FDCNT_0_GUARANT_CNT_MASK) +
6211 ((val & I40E_GLQF_FDCNT_0_BESTCNT_MASK) >>
6212 I40E_GLQF_FDCNT_0_BESTCNT_SHIFT);
6213 return fcnt_prog;
6214}
6215
6216/**
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006217 * i40e_fdir_check_and_reenable - Function to reenabe FD ATR or SB if disabled
6218 * @pf: board private structure
6219 **/
6220void i40e_fdir_check_and_reenable(struct i40e_pf *pf)
6221{
Carolyn Wyborny3487b6c2015-08-27 11:42:38 -04006222 struct i40e_fdir_filter *filter;
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006223 u32 fcnt_prog, fcnt_avail;
Carolyn Wyborny3487b6c2015-08-27 11:42:38 -04006224 struct hlist_node *node;
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006225
Jacob Keller0da36b92017-04-19 09:25:55 -04006226 if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006227 return;
6228
Jacob Keller47994c12017-04-19 09:25:57 -04006229 /* Check if we have enough room to re-enable FDir SB capability. */
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006230 fcnt_prog = i40e_get_global_fd_count(pf);
Anjali Singhai Jain12957382014-06-04 04:22:47 +00006231 fcnt_avail = pf->fdir_pf_filter_count;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006232 if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM)) ||
6233 (pf->fd_add_err == 0) ||
6234 (i40e_get_current_atr_cnt(pf) < pf->fd_atr_cnt)) {
Jacob Keller47994c12017-04-19 09:25:57 -04006235 if (pf->flags & I40E_FLAG_FD_SB_AUTO_DISABLED) {
6236 pf->flags &= ~I40E_FLAG_FD_SB_AUTO_DISABLED;
6237 if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
6238 (I40E_DEBUG_FD & pf->hw.debug_mask))
Anjali Singhai Jain2e4875e2015-04-16 20:06:06 -04006239 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 +00006240 }
6241 }
Jacob Kellera3417d22016-09-06 18:05:10 -07006242
Jacob Keller47994c12017-04-19 09:25:57 -04006243 /* We should wait for even more space before re-enabling ATR.
6244 * Additionally, we cannot enable ATR as long as we still have TCP SB
6245 * rules active.
Jacob Kellera3417d22016-09-06 18:05:10 -07006246 */
Jacob Keller47994c12017-04-19 09:25:57 -04006247 if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) &&
6248 (pf->fd_tcp4_filter_cnt == 0)) {
6249 if (pf->flags & I40E_FLAG_FD_ATR_AUTO_DISABLED) {
6250 pf->flags &= ~I40E_FLAG_FD_ATR_AUTO_DISABLED;
6251 if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
6252 (I40E_DEBUG_FD & pf->hw.debug_mask))
Jacob Kellera3417d22016-09-06 18:05:10 -07006253 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 +00006254 }
6255 }
Carolyn Wyborny3487b6c2015-08-27 11:42:38 -04006256
6257 /* if hw had a problem adding a filter, delete it */
6258 if (pf->fd_inv > 0) {
6259 hlist_for_each_entry_safe(filter, node,
6260 &pf->fdir_filter_list, fdir_node) {
6261 if (filter->fd_id == pf->fd_inv) {
6262 hlist_del(&filter->fdir_node);
6263 kfree(filter);
6264 pf->fdir_pf_active_filters--;
6265 }
6266 }
6267 }
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006268}
6269
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006270#define I40E_MIN_FD_FLUSH_INTERVAL 10
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006271#define I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE 30
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006272/**
6273 * i40e_fdir_flush_and_replay - Function to flush all FD filters and replay SB
6274 * @pf: board private structure
6275 **/
6276static void i40e_fdir_flush_and_replay(struct i40e_pf *pf)
6277{
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006278 unsigned long min_flush_time;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006279 int flush_wait_retry = 50;
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006280 bool disable_atr = false;
6281 int fd_room;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006282 int reg;
6283
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04006284 if (!time_after(jiffies, pf->fd_flush_timestamp +
6285 (I40E_MIN_FD_FLUSH_INTERVAL * HZ)))
6286 return;
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006287
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04006288 /* If the flush is happening too quick and we have mostly SB rules we
6289 * should not re-enable ATR for some time.
6290 */
6291 min_flush_time = pf->fd_flush_timestamp +
6292 (I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE * HZ);
6293 fd_room = pf->fdir_pf_filter_count - pf->fdir_pf_active_filters;
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006294
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04006295 if (!(time_after(jiffies, min_flush_time)) &&
6296 (fd_room < I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) {
6297 if (I40E_DEBUG_FD & pf->hw.debug_mask)
6298 dev_info(&pf->pdev->dev, "ATR disabled, not enough FD filter space.\n");
6299 disable_atr = true;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006300 }
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04006301
6302 pf->fd_flush_timestamp = jiffies;
Jacob Keller47994c12017-04-19 09:25:57 -04006303 pf->flags |= I40E_FLAG_FD_ATR_AUTO_DISABLED;
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04006304 /* flush all filters */
6305 wr32(&pf->hw, I40E_PFQF_CTL_1,
6306 I40E_PFQF_CTL_1_CLEARFDTABLE_MASK);
6307 i40e_flush(&pf->hw);
6308 pf->fd_flush_cnt++;
6309 pf->fd_add_err = 0;
6310 do {
6311 /* Check FD flush status every 5-6msec */
6312 usleep_range(5000, 6000);
6313 reg = rd32(&pf->hw, I40E_PFQF_CTL_1);
6314 if (!(reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK))
6315 break;
6316 } while (flush_wait_retry--);
6317 if (reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK) {
6318 dev_warn(&pf->pdev->dev, "FD table did not flush, needs more time\n");
6319 } else {
6320 /* replay sideband filters */
6321 i40e_fdir_filter_restore(pf->vsi[pf->lan_vsi]);
Jacob Keller097dbf52017-02-06 14:38:46 -08006322 if (!disable_atr && !pf->fd_tcp4_filter_cnt)
Jacob Keller47994c12017-04-19 09:25:57 -04006323 pf->flags &= ~I40E_FLAG_FD_ATR_AUTO_DISABLED;
Jacob Keller0da36b92017-04-19 09:25:55 -04006324 clear_bit(__I40E_FD_FLUSH_REQUESTED, pf->state);
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04006325 if (I40E_DEBUG_FD & pf->hw.debug_mask)
6326 dev_info(&pf->pdev->dev, "FD Filter table flushed and FD-SB replayed.\n");
6327 }
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006328}
6329
6330/**
6331 * i40e_get_current_atr_count - Get the count of total FD ATR filters programmed
6332 * @pf: board private structure
6333 **/
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006334u32 i40e_get_current_atr_cnt(struct i40e_pf *pf)
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006335{
6336 return i40e_get_current_fd_count(pf) - pf->fdir_pf_active_filters;
6337}
6338
6339/* We can see up to 256 filter programming desc in transit if the filters are
6340 * being applied really fast; before we see the first
6341 * filter miss error on Rx queue 0. Accumulating enough error messages before
6342 * reacting will make sure we don't cause flush too often.
6343 */
6344#define I40E_MAX_FD_PROGRAM_ERROR 256
6345
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006346/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006347 * i40e_fdir_reinit_subtask - Worker thread to reinit FDIR filter table
6348 * @pf: board private structure
6349 **/
6350static void i40e_fdir_reinit_subtask(struct i40e_pf *pf)
6351{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006352
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006353 /* if interface is down do nothing */
Mauro S. M. Rodrigues9e6c9c02017-05-12 23:26:56 -03006354 if (test_bit(__I40E_DOWN, pf->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006355 return;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006356
Jacob Keller0da36b92017-04-19 09:25:55 -04006357 if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006358 i40e_fdir_flush_and_replay(pf);
6359
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006360 i40e_fdir_check_and_reenable(pf);
6361
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006362}
6363
6364/**
6365 * i40e_vsi_link_event - notify VSI of a link event
6366 * @vsi: vsi to be notified
6367 * @link_up: link up or down
6368 **/
6369static void i40e_vsi_link_event(struct i40e_vsi *vsi, bool link_up)
6370{
Jacob Keller0da36b92017-04-19 09:25:55 -04006371 if (!vsi || test_bit(__I40E_VSI_DOWN, vsi->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006372 return;
6373
6374 switch (vsi->type) {
6375 case I40E_VSI_MAIN:
6376 if (!vsi->netdev || !vsi->netdev_registered)
6377 break;
6378
6379 if (link_up) {
6380 netif_carrier_on(vsi->netdev);
6381 netif_tx_wake_all_queues(vsi->netdev);
6382 } else {
6383 netif_carrier_off(vsi->netdev);
6384 netif_tx_stop_all_queues(vsi->netdev);
6385 }
6386 break;
6387
6388 case I40E_VSI_SRIOV:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006389 case I40E_VSI_VMDQ2:
6390 case I40E_VSI_CTRL:
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06006391 case I40E_VSI_IWARP:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006392 case I40E_VSI_MIRROR:
6393 default:
6394 /* there is no notification for other VSIs */
6395 break;
6396 }
6397}
6398
6399/**
6400 * i40e_veb_link_event - notify elements on the veb of a link event
6401 * @veb: veb to be notified
6402 * @link_up: link up or down
6403 **/
6404static void i40e_veb_link_event(struct i40e_veb *veb, bool link_up)
6405{
6406 struct i40e_pf *pf;
6407 int i;
6408
6409 if (!veb || !veb->pf)
6410 return;
6411 pf = veb->pf;
6412
6413 /* depth first... */
6414 for (i = 0; i < I40E_MAX_VEB; i++)
6415 if (pf->veb[i] && (pf->veb[i]->uplink_seid == veb->seid))
6416 i40e_veb_link_event(pf->veb[i], link_up);
6417
6418 /* ... now the local VSIs */
Mitch Williams505682c2014-05-20 08:01:37 +00006419 for (i = 0; i < pf->num_alloc_vsi; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006420 if (pf->vsi[i] && (pf->vsi[i]->uplink_seid == veb->seid))
6421 i40e_vsi_link_event(pf->vsi[i], link_up);
6422}
6423
6424/**
6425 * i40e_link_event - Update netif_carrier status
6426 * @pf: board private structure
6427 **/
6428static void i40e_link_event(struct i40e_pf *pf)
6429{
Mitch Williams320684c2014-10-17 03:14:43 +00006430 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
Catherine Sullivanfef59dd2014-12-11 07:06:33 +00006431 u8 new_link_speed, old_link_speed;
Jesse Brandeburga72a5abc2015-08-26 15:14:19 -04006432 i40e_status status;
6433 bool new_link, old_link;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006434
Catherine Sullivan1f9610e2015-10-21 19:47:09 -04006435 /* save off old link status information */
6436 pf->hw.phy.link_info_old = pf->hw.phy.link_info;
6437
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00006438 /* set this to force the get_link_status call to refresh state */
6439 pf->hw.phy.get_link_info = true;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006440
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00006441 old_link = (pf->hw.phy.link_info_old.link_info & I40E_AQ_LINK_UP);
Jesse Brandeburga72a5abc2015-08-26 15:14:19 -04006442
6443 status = i40e_get_link_status(&pf->hw, &new_link);
Harshitha Ramamurthyae136702016-12-12 15:44:16 -08006444
6445 /* On success, disable temp link polling */
6446 if (status == I40E_SUCCESS) {
6447 if (pf->flags & I40E_FLAG_TEMP_LINK_POLLING)
6448 pf->flags &= ~I40E_FLAG_TEMP_LINK_POLLING;
6449 } else {
6450 /* Enable link polling temporarily until i40e_get_link_status
6451 * returns I40E_SUCCESS
6452 */
6453 pf->flags |= I40E_FLAG_TEMP_LINK_POLLING;
Jesse Brandeburga72a5abc2015-08-26 15:14:19 -04006454 dev_dbg(&pf->pdev->dev, "couldn't get link state, status: %d\n",
6455 status);
6456 return;
6457 }
6458
Catherine Sullivanfef59dd2014-12-11 07:06:33 +00006459 old_link_speed = pf->hw.phy.link_info_old.link_speed;
6460 new_link_speed = pf->hw.phy.link_info.link_speed;
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00006461
6462 if (new_link == old_link &&
Catherine Sullivanfef59dd2014-12-11 07:06:33 +00006463 new_link_speed == old_link_speed &&
Jacob Keller0da36b92017-04-19 09:25:55 -04006464 (test_bit(__I40E_VSI_DOWN, vsi->state) ||
Mitch Williams320684c2014-10-17 03:14:43 +00006465 new_link == netif_carrier_ok(vsi->netdev)))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006466 return;
Mitch Williams320684c2014-10-17 03:14:43 +00006467
Jacob Keller0da36b92017-04-19 09:25:55 -04006468 if (!test_bit(__I40E_VSI_DOWN, vsi->state))
Mitch Williams320684c2014-10-17 03:14:43 +00006469 i40e_print_link_message(vsi, new_link);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006470
6471 /* Notify the base of the switch tree connected to
6472 * the link. Floating VEBs are not notified.
6473 */
6474 if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
6475 i40e_veb_link_event(pf->veb[pf->lan_veb], new_link);
6476 else
Mitch Williams320684c2014-10-17 03:14:43 +00006477 i40e_vsi_link_event(vsi, new_link);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006478
6479 if (pf->vf)
6480 i40e_vc_notify_link_state(pf);
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00006481
6482 if (pf->flags & I40E_FLAG_PTP)
6483 i40e_ptp_set_increment(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006484}
6485
6486/**
Shannon Nelson21536712014-10-25 10:35:25 +00006487 * i40e_watchdog_subtask - periodic checks not using event driven response
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006488 * @pf: board private structure
6489 **/
6490static void i40e_watchdog_subtask(struct i40e_pf *pf)
6491{
6492 int i;
6493
6494 /* if interface is down do nothing */
Mauro S. M. Rodrigues9e6c9c02017-05-12 23:26:56 -03006495 if (test_bit(__I40E_DOWN, pf->state) ||
Jacob Keller0da36b92017-04-19 09:25:55 -04006496 test_bit(__I40E_CONFIG_BUSY, pf->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006497 return;
6498
Shannon Nelson21536712014-10-25 10:35:25 +00006499 /* make sure we don't do these things too often */
6500 if (time_before(jiffies, (pf->service_timer_previous +
6501 pf->service_timer_period)))
6502 return;
6503 pf->service_timer_previous = jiffies;
6504
Harshitha Ramamurthyae136702016-12-12 15:44:16 -08006505 if ((pf->flags & I40E_FLAG_LINK_POLLING_ENABLED) ||
6506 (pf->flags & I40E_FLAG_TEMP_LINK_POLLING))
Shannon Nelson9ac77262015-08-27 11:42:40 -04006507 i40e_link_event(pf);
Shannon Nelson21536712014-10-25 10:35:25 +00006508
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006509 /* Update the stats for active netdevs so the network stack
6510 * can look at updated numbers whenever it cares to
6511 */
Mitch Williams505682c2014-05-20 08:01:37 +00006512 for (i = 0; i < pf->num_alloc_vsi; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006513 if (pf->vsi[i] && pf->vsi[i]->netdev)
6514 i40e_update_stats(pf->vsi[i]);
6515
Anjali Singhai Jaind1a8d272015-07-23 16:54:40 -04006516 if (pf->flags & I40E_FLAG_VEB_STATS_ENABLED) {
6517 /* Update the stats for the active switching components */
6518 for (i = 0; i < I40E_MAX_VEB; i++)
6519 if (pf->veb[i])
6520 i40e_update_veb_stats(pf->veb[i]);
6521 }
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00006522
Jacob Keller61189552017-05-03 10:29:01 -07006523 i40e_ptp_rx_hang(pf);
Jacob Keller0bc07062017-05-03 10:29:02 -07006524 i40e_ptp_tx_hang(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006525}
6526
6527/**
6528 * i40e_reset_subtask - Set up for resetting the device and driver
6529 * @pf: board private structure
6530 **/
6531static void i40e_reset_subtask(struct i40e_pf *pf)
6532{
6533 u32 reset_flags = 0;
6534
Jacob Keller0da36b92017-04-19 09:25:55 -04006535 if (test_bit(__I40E_REINIT_REQUESTED, pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006536 reset_flags |= BIT(__I40E_REINIT_REQUESTED);
Jacob Keller0da36b92017-04-19 09:25:55 -04006537 clear_bit(__I40E_REINIT_REQUESTED, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006538 }
Jacob Keller0da36b92017-04-19 09:25:55 -04006539 if (test_bit(__I40E_PF_RESET_REQUESTED, pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006540 reset_flags |= BIT(__I40E_PF_RESET_REQUESTED);
Jacob Keller0da36b92017-04-19 09:25:55 -04006541 clear_bit(__I40E_PF_RESET_REQUESTED, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006542 }
Jacob Keller0da36b92017-04-19 09:25:55 -04006543 if (test_bit(__I40E_CORE_RESET_REQUESTED, pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006544 reset_flags |= BIT(__I40E_CORE_RESET_REQUESTED);
Jacob Keller0da36b92017-04-19 09:25:55 -04006545 clear_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006546 }
Jacob Keller0da36b92017-04-19 09:25:55 -04006547 if (test_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006548 reset_flags |= BIT(__I40E_GLOBAL_RESET_REQUESTED);
Jacob Keller0da36b92017-04-19 09:25:55 -04006549 clear_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006550 }
Mauro S. M. Rodrigues9e6c9c02017-05-12 23:26:56 -03006551 if (test_bit(__I40E_DOWN_REQUESTED, pf->state)) {
6552 reset_flags |= BIT(__I40E_DOWN_REQUESTED);
6553 clear_bit(__I40E_DOWN_REQUESTED, pf->state);
Neerav Parikhb5d06f02014-06-03 23:50:17 +00006554 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006555
6556 /* If there's a recovery already waiting, it takes
6557 * precedence before starting a new reset sequence.
6558 */
Jacob Keller0da36b92017-04-19 09:25:55 -04006559 if (test_bit(__I40E_RESET_INTR_RECEIVED, pf->state)) {
Maciej Sosin373149f2017-04-05 07:50:55 -04006560 i40e_prep_for_reset(pf, false);
6561 i40e_reset(pf);
6562 i40e_rebuild(pf, false, false);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006563 }
6564
6565 /* If we're already down or resetting, just bail */
6566 if (reset_flags &&
Mauro S. M. Rodrigues9e6c9c02017-05-12 23:26:56 -03006567 !test_bit(__I40E_DOWN, pf->state) &&
Jacob Keller0da36b92017-04-19 09:25:55 -04006568 !test_bit(__I40E_CONFIG_BUSY, pf->state)) {
Jacob Kellerdfc4ff62017-06-07 05:43:13 -04006569 i40e_do_reset(pf, reset_flags, false);
Maciej Sosin373149f2017-04-05 07:50:55 -04006570 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006571}
6572
6573/**
6574 * i40e_handle_link_event - Handle link event
6575 * @pf: board private structure
6576 * @e: event info posted on ARQ
6577 **/
6578static void i40e_handle_link_event(struct i40e_pf *pf,
6579 struct i40e_arq_event_info *e)
6580{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006581 struct i40e_aqc_get_link_status *status =
6582 (struct i40e_aqc_get_link_status *)&e->desc.params.raw;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006583
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00006584 /* Do a new status request to re-enable LSE reporting
6585 * and load new status information into the hw struct
6586 * This completely ignores any state information
6587 * in the ARQ event info, instead choosing to always
6588 * issue the AQ update link status command.
6589 */
6590 i40e_link_event(pf);
6591
Carolyn Wyborny7b592f62014-07-10 07:58:19 +00006592 /* check for unqualified module, if link is down */
6593 if ((status->link_info & I40E_AQ_MEDIA_AVAILABLE) &&
6594 (!(status->an_info & I40E_AQ_QUALIFIED_MODULE)) &&
6595 (!(status->link_info & I40E_AQ_LINK_UP)))
6596 dev_err(&pf->pdev->dev,
6597 "The driver failed to link because an unqualified module was detected.\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006598}
6599
6600/**
6601 * i40e_clean_adminq_subtask - Clean the AdminQ rings
6602 * @pf: board private structure
6603 **/
6604static void i40e_clean_adminq_subtask(struct i40e_pf *pf)
6605{
6606 struct i40e_arq_event_info event;
6607 struct i40e_hw *hw = &pf->hw;
6608 u16 pending, i = 0;
6609 i40e_status ret;
6610 u16 opcode;
Shannon Nelson86df2422014-05-20 08:01:35 +00006611 u32 oldval;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006612 u32 val;
6613
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00006614 /* Do not run clean AQ when PF reset fails */
Jacob Keller0da36b92017-04-19 09:25:55 -04006615 if (test_bit(__I40E_RESET_FAILED, pf->state))
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00006616 return;
6617
Shannon Nelson86df2422014-05-20 08:01:35 +00006618 /* check for error indications */
6619 val = rd32(&pf->hw, pf->hw.aq.arq.len);
6620 oldval = val;
6621 if (val & I40E_PF_ARQLEN_ARQVFE_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006622 if (hw->debug_mask & I40E_DEBUG_AQ)
6623 dev_info(&pf->pdev->dev, "ARQ VF Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006624 val &= ~I40E_PF_ARQLEN_ARQVFE_MASK;
6625 }
6626 if (val & I40E_PF_ARQLEN_ARQOVFL_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006627 if (hw->debug_mask & I40E_DEBUG_AQ)
6628 dev_info(&pf->pdev->dev, "ARQ Overflow Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006629 val &= ~I40E_PF_ARQLEN_ARQOVFL_MASK;
Mitch Williams1d0a4ad2015-12-23 12:05:48 -08006630 pf->arq_overflows++;
Shannon Nelson86df2422014-05-20 08:01:35 +00006631 }
6632 if (val & I40E_PF_ARQLEN_ARQCRIT_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006633 if (hw->debug_mask & I40E_DEBUG_AQ)
6634 dev_info(&pf->pdev->dev, "ARQ Critical Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006635 val &= ~I40E_PF_ARQLEN_ARQCRIT_MASK;
6636 }
6637 if (oldval != val)
6638 wr32(&pf->hw, pf->hw.aq.arq.len, val);
6639
6640 val = rd32(&pf->hw, pf->hw.aq.asq.len);
6641 oldval = val;
6642 if (val & I40E_PF_ATQLEN_ATQVFE_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006643 if (pf->hw.debug_mask & I40E_DEBUG_AQ)
6644 dev_info(&pf->pdev->dev, "ASQ VF Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006645 val &= ~I40E_PF_ATQLEN_ATQVFE_MASK;
6646 }
6647 if (val & I40E_PF_ATQLEN_ATQOVFL_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006648 if (pf->hw.debug_mask & I40E_DEBUG_AQ)
6649 dev_info(&pf->pdev->dev, "ASQ Overflow Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006650 val &= ~I40E_PF_ATQLEN_ATQOVFL_MASK;
6651 }
6652 if (val & I40E_PF_ATQLEN_ATQCRIT_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006653 if (pf->hw.debug_mask & I40E_DEBUG_AQ)
6654 dev_info(&pf->pdev->dev, "ASQ Critical Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006655 val &= ~I40E_PF_ATQLEN_ATQCRIT_MASK;
6656 }
6657 if (oldval != val)
6658 wr32(&pf->hw, pf->hw.aq.asq.len, val);
6659
Mitch Williams1001dc32014-11-11 20:02:19 +00006660 event.buf_len = I40E_MAX_AQ_BUF_SIZE;
6661 event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006662 if (!event.msg_buf)
6663 return;
6664
6665 do {
6666 ret = i40e_clean_arq_element(hw, &event, &pending);
Mitch Williams56497972014-06-04 08:45:18 +00006667 if (ret == I40E_ERR_ADMIN_QUEUE_NO_WORK)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006668 break;
Mitch Williams56497972014-06-04 08:45:18 +00006669 else if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006670 dev_info(&pf->pdev->dev, "ARQ event error %d\n", ret);
6671 break;
6672 }
6673
6674 opcode = le16_to_cpu(event.desc.opcode);
6675 switch (opcode) {
6676
6677 case i40e_aqc_opc_get_link_status:
6678 i40e_handle_link_event(pf, &event);
6679 break;
6680 case i40e_aqc_opc_send_msg_to_pf:
6681 ret = i40e_vc_process_vf_msg(pf,
6682 le16_to_cpu(event.desc.retval),
6683 le32_to_cpu(event.desc.cookie_high),
6684 le32_to_cpu(event.desc.cookie_low),
6685 event.msg_buf,
Mitch Williams1001dc32014-11-11 20:02:19 +00006686 event.msg_len);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006687 break;
6688 case i40e_aqc_opc_lldp_update_mib:
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006689 dev_dbg(&pf->pdev->dev, "ARQ: Update LLDP MIB event received\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006690#ifdef CONFIG_I40E_DCB
6691 rtnl_lock();
6692 ret = i40e_handle_lldp_event(pf, &event);
6693 rtnl_unlock();
6694#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006695 break;
6696 case i40e_aqc_opc_event_lan_overflow:
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006697 dev_dbg(&pf->pdev->dev, "ARQ LAN queue overflow event received\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006698 i40e_handle_lan_overflow_event(pf, &event);
6699 break;
Shannon Nelson0467bc92013-12-18 13:45:58 +00006700 case i40e_aqc_opc_send_msg_to_peer:
6701 dev_info(&pf->pdev->dev, "ARQ: Msg from other pf\n");
6702 break;
Shannon Nelson91a0f932015-03-19 14:32:01 -07006703 case i40e_aqc_opc_nvm_erase:
6704 case i40e_aqc_opc_nvm_update:
Michal Kosiarz00ada502015-11-19 11:34:20 -08006705 case i40e_aqc_opc_oem_post_update:
Shannon Nelson6e93d0c2016-01-15 14:33:18 -08006706 i40e_debug(&pf->hw, I40E_DEBUG_NVM,
6707 "ARQ NVM operation 0x%04x completed\n",
6708 opcode);
Shannon Nelson91a0f932015-03-19 14:32:01 -07006709 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006710 default:
6711 dev_info(&pf->pdev->dev,
Shannon Nelson56e5ca62016-03-10 14:59:48 -08006712 "ARQ: Unknown event 0x%04x ignored\n",
Shannon Nelson0467bc92013-12-18 13:45:58 +00006713 opcode);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006714 break;
6715 }
Christopher N Bednarz1fca3262017-03-10 12:22:03 -08006716 } while (i++ < pf->adminq_work_limit);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006717
Christopher N Bednarz1fca3262017-03-10 12:22:03 -08006718 if (i < pf->adminq_work_limit)
Jacob Keller0da36b92017-04-19 09:25:55 -04006719 clear_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state);
Christopher N Bednarz1fca3262017-03-10 12:22:03 -08006720
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006721 /* re-enable Admin queue interrupt cause */
6722 val = rd32(hw, I40E_PFINT_ICR0_ENA);
6723 val |= I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
6724 wr32(hw, I40E_PFINT_ICR0_ENA, val);
6725 i40e_flush(hw);
6726
6727 kfree(event.msg_buf);
6728}
6729
6730/**
Shannon Nelson4eb3f762014-03-06 08:59:58 +00006731 * i40e_verify_eeprom - make sure eeprom is good to use
6732 * @pf: board private structure
6733 **/
6734static void i40e_verify_eeprom(struct i40e_pf *pf)
6735{
6736 int err;
6737
6738 err = i40e_diag_eeprom_test(&pf->hw);
6739 if (err) {
6740 /* retry in case of garbage read */
6741 err = i40e_diag_eeprom_test(&pf->hw);
6742 if (err) {
6743 dev_info(&pf->pdev->dev, "eeprom check failed (%d), Tx/Rx traffic disabled\n",
6744 err);
Jacob Keller0da36b92017-04-19 09:25:55 -04006745 set_bit(__I40E_BAD_EEPROM, pf->state);
Shannon Nelson4eb3f762014-03-06 08:59:58 +00006746 }
6747 }
6748
Jacob Keller0da36b92017-04-19 09:25:55 -04006749 if (!err && test_bit(__I40E_BAD_EEPROM, pf->state)) {
Shannon Nelson4eb3f762014-03-06 08:59:58 +00006750 dev_info(&pf->pdev->dev, "eeprom check passed, Tx/Rx traffic enabled\n");
Jacob Keller0da36b92017-04-19 09:25:55 -04006751 clear_bit(__I40E_BAD_EEPROM, pf->state);
Shannon Nelson4eb3f762014-03-06 08:59:58 +00006752 }
6753}
6754
6755/**
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006756 * i40e_enable_pf_switch_lb
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00006757 * @pf: pointer to the PF structure
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006758 *
6759 * enable switch loop back or die - no point in a return value
6760 **/
6761static void i40e_enable_pf_switch_lb(struct i40e_pf *pf)
6762{
6763 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
6764 struct i40e_vsi_context ctxt;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006765 int ret;
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006766
6767 ctxt.seid = pf->main_vsi_seid;
6768 ctxt.pf_num = pf->hw.pf_id;
6769 ctxt.vf_num = 0;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006770 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
6771 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006772 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006773 "couldn't get PF vsi config, err %s aq_err %s\n",
6774 i40e_stat_str(&pf->hw, ret),
6775 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006776 return;
6777 }
6778 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
6779 ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
6780 ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
6781
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006782 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
6783 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006784 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006785 "update vsi switch failed, err %s aq_err %s\n",
6786 i40e_stat_str(&pf->hw, ret),
6787 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006788 }
6789}
6790
6791/**
6792 * i40e_disable_pf_switch_lb
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00006793 * @pf: pointer to the PF structure
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006794 *
6795 * disable switch loop back or die - no point in a return value
6796 **/
6797static void i40e_disable_pf_switch_lb(struct i40e_pf *pf)
6798{
6799 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
6800 struct i40e_vsi_context ctxt;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006801 int ret;
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006802
6803 ctxt.seid = pf->main_vsi_seid;
6804 ctxt.pf_num = pf->hw.pf_id;
6805 ctxt.vf_num = 0;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006806 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
6807 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006808 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006809 "couldn't get PF vsi config, err %s aq_err %s\n",
6810 i40e_stat_str(&pf->hw, ret),
6811 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006812 return;
6813 }
6814 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
6815 ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
6816 ctxt.info.switch_id &= ~cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
6817
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006818 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
6819 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006820 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006821 "update vsi switch failed, err %s aq_err %s\n",
6822 i40e_stat_str(&pf->hw, ret),
6823 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006824 }
6825}
6826
6827/**
Neerav Parikh51616012015-02-06 08:52:14 +00006828 * i40e_config_bridge_mode - Configure the HW bridge mode
6829 * @veb: pointer to the bridge instance
6830 *
6831 * Configure the loop back mode for the LAN VSI that is downlink to the
6832 * specified HW bridge instance. It is expected this function is called
6833 * when a new HW bridge is instantiated.
6834 **/
6835static void i40e_config_bridge_mode(struct i40e_veb *veb)
6836{
6837 struct i40e_pf *pf = veb->pf;
6838
Shannon Nelson6dec1012015-09-28 14:12:30 -04006839 if (pf->hw.debug_mask & I40E_DEBUG_LAN)
6840 dev_info(&pf->pdev->dev, "enabling bridge mode: %s\n",
6841 veb->bridge_mode == BRIDGE_MODE_VEPA ? "VEPA" : "VEB");
Neerav Parikh51616012015-02-06 08:52:14 +00006842 if (veb->bridge_mode & BRIDGE_MODE_VEPA)
6843 i40e_disable_pf_switch_lb(pf);
6844 else
6845 i40e_enable_pf_switch_lb(pf);
6846}
6847
6848/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006849 * i40e_reconstitute_veb - rebuild the VEB and anything connected to it
6850 * @veb: pointer to the VEB instance
6851 *
6852 * This is a recursive function that first builds the attached VSIs then
6853 * recurses in to build the next layer of VEB. We track the connections
6854 * through our own index numbers because the seid's from the HW could
6855 * change across the reset.
6856 **/
6857static int i40e_reconstitute_veb(struct i40e_veb *veb)
6858{
6859 struct i40e_vsi *ctl_vsi = NULL;
6860 struct i40e_pf *pf = veb->pf;
6861 int v, veb_idx;
6862 int ret;
6863
6864 /* build VSI that owns this VEB, temporarily attached to base VEB */
Mitch Williams505682c2014-05-20 08:01:37 +00006865 for (v = 0; v < pf->num_alloc_vsi && !ctl_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006866 if (pf->vsi[v] &&
6867 pf->vsi[v]->veb_idx == veb->idx &&
6868 pf->vsi[v]->flags & I40E_VSI_FLAG_VEB_OWNER) {
6869 ctl_vsi = pf->vsi[v];
6870 break;
6871 }
6872 }
6873 if (!ctl_vsi) {
6874 dev_info(&pf->pdev->dev,
6875 "missing owner VSI for veb_idx %d\n", veb->idx);
6876 ret = -ENOENT;
6877 goto end_reconstitute;
6878 }
6879 if (ctl_vsi != pf->vsi[pf->lan_vsi])
6880 ctl_vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
6881 ret = i40e_add_vsi(ctl_vsi);
6882 if (ret) {
6883 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006884 "rebuild of veb_idx %d owner VSI failed: %d\n",
6885 veb->idx, ret);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006886 goto end_reconstitute;
6887 }
6888 i40e_vsi_reset_stats(ctl_vsi);
6889
6890 /* create the VEB in the switch and move the VSI onto the VEB */
6891 ret = i40e_add_veb(veb, ctl_vsi);
6892 if (ret)
6893 goto end_reconstitute;
6894
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07006895 if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
6896 veb->bridge_mode = BRIDGE_MODE_VEB;
6897 else
6898 veb->bridge_mode = BRIDGE_MODE_VEPA;
Neerav Parikh51616012015-02-06 08:52:14 +00006899 i40e_config_bridge_mode(veb);
Anjali Singhai Jainb64ba082014-11-13 03:06:15 +00006900
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006901 /* create the remaining VSIs attached to this VEB */
Mitch Williams505682c2014-05-20 08:01:37 +00006902 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006903 if (!pf->vsi[v] || pf->vsi[v] == ctl_vsi)
6904 continue;
6905
6906 if (pf->vsi[v]->veb_idx == veb->idx) {
6907 struct i40e_vsi *vsi = pf->vsi[v];
Jesse Brandeburg6995b362015-08-28 17:55:54 -04006908
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006909 vsi->uplink_seid = veb->seid;
6910 ret = i40e_add_vsi(vsi);
6911 if (ret) {
6912 dev_info(&pf->pdev->dev,
6913 "rebuild of vsi_idx %d failed: %d\n",
6914 v, ret);
6915 goto end_reconstitute;
6916 }
6917 i40e_vsi_reset_stats(vsi);
6918 }
6919 }
6920
6921 /* create any VEBs attached to this VEB - RECURSION */
6922 for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
6923 if (pf->veb[veb_idx] && pf->veb[veb_idx]->veb_idx == veb->idx) {
6924 pf->veb[veb_idx]->uplink_seid = veb->seid;
6925 ret = i40e_reconstitute_veb(pf->veb[veb_idx]);
6926 if (ret)
6927 break;
6928 }
6929 }
6930
6931end_reconstitute:
6932 return ret;
6933}
6934
6935/**
6936 * i40e_get_capabilities - get info about the HW
6937 * @pf: the PF struct
6938 **/
6939static int i40e_get_capabilities(struct i40e_pf *pf)
6940{
6941 struct i40e_aqc_list_capabilities_element_resp *cap_buf;
6942 u16 data_size;
6943 int buf_len;
6944 int err;
6945
6946 buf_len = 40 * sizeof(struct i40e_aqc_list_capabilities_element_resp);
6947 do {
6948 cap_buf = kzalloc(buf_len, GFP_KERNEL);
6949 if (!cap_buf)
6950 return -ENOMEM;
6951
6952 /* this loads the data into the hw struct for us */
6953 err = i40e_aq_discover_capabilities(&pf->hw, cap_buf, buf_len,
6954 &data_size,
6955 i40e_aqc_opc_list_func_capabilities,
6956 NULL);
6957 /* data loaded, buffer no longer needed */
6958 kfree(cap_buf);
6959
6960 if (pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOMEM) {
6961 /* retry with a larger buffer */
6962 buf_len = data_size;
6963 } else if (pf->hw.aq.asq_last_status != I40E_AQ_RC_OK) {
6964 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006965 "capability discovery failed, err %s aq_err %s\n",
6966 i40e_stat_str(&pf->hw, err),
6967 i40e_aq_str(&pf->hw,
6968 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006969 return -ENODEV;
6970 }
6971 } while (err);
6972
6973 if (pf->hw.debug_mask & I40E_DEBUG_USER)
6974 dev_info(&pf->pdev->dev,
6975 "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",
6976 pf->hw.pf_id, pf->hw.func_caps.num_vfs,
6977 pf->hw.func_caps.num_msix_vectors,
6978 pf->hw.func_caps.num_msix_vectors_vf,
6979 pf->hw.func_caps.fd_filters_guaranteed,
6980 pf->hw.func_caps.fd_filters_best_effort,
6981 pf->hw.func_caps.num_tx_qp,
6982 pf->hw.func_caps.num_vsis);
6983
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00006984#define DEF_NUM_VSI (1 + (pf->hw.func_caps.fcoe ? 1 : 0) \
6985 + pf->hw.func_caps.num_vfs)
6986 if (pf->hw.revision_id == 0 && (DEF_NUM_VSI > pf->hw.func_caps.num_vsis)) {
6987 dev_info(&pf->pdev->dev,
6988 "got num_vsis %d, setting num_vsis to %d\n",
6989 pf->hw.func_caps.num_vsis, DEF_NUM_VSI);
6990 pf->hw.func_caps.num_vsis = DEF_NUM_VSI;
6991 }
6992
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006993 return 0;
6994}
6995
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006996static int i40e_vsi_clear(struct i40e_vsi *vsi);
6997
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006998/**
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006999 * i40e_fdir_sb_setup - initialize the Flow Director resources for Sideband
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007000 * @pf: board private structure
7001 **/
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08007002static void i40e_fdir_sb_setup(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007003{
7004 struct i40e_vsi *vsi;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007005
Jesse Brandeburg407e0632014-06-03 23:50:12 +00007006 /* quick workaround for an NVM issue that leaves a critical register
7007 * uninitialized
7008 */
7009 if (!rd32(&pf->hw, I40E_GLQF_HKEY(0))) {
7010 static const u32 hkey[] = {
7011 0xe640d33f, 0xcdfe98ab, 0x73fa7161, 0x0d7a7d36,
7012 0xeacb7d61, 0xaa4f05b6, 0x9c5c89ed, 0xfc425ddb,
7013 0xa4654832, 0xfc7461d4, 0x8f827619, 0xf5c63c21,
7014 0x95b3a76d};
Alexander Duyck4b816442016-10-11 15:26:53 -07007015 int i;
Jesse Brandeburg407e0632014-06-03 23:50:12 +00007016
7017 for (i = 0; i <= I40E_GLQF_HKEY_MAX_INDEX; i++)
7018 wr32(&pf->hw, I40E_GLQF_HKEY(i), hkey[i]);
7019 }
7020
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08007021 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007022 return;
7023
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08007024 /* find existing VSI and see if it needs configuring */
Alexander Duyck4b816442016-10-11 15:26:53 -07007025 vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08007026
7027 /* create a new VSI if none exists */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007028 if (!vsi) {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08007029 vsi = i40e_vsi_setup(pf, I40E_VSI_FDIR,
7030 pf->vsi[pf->lan_vsi]->seid, 0);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007031 if (!vsi) {
7032 dev_info(&pf->pdev->dev, "Couldn't create FDir VSI\n");
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00007033 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
7034 return;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007035 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007036 }
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00007037
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08007038 i40e_vsi_setup_irqhandler(vsi, i40e_fdir_clean_ring);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007039}
7040
7041/**
7042 * i40e_fdir_teardown - release the Flow Director resources
7043 * @pf: board private structure
7044 **/
7045static void i40e_fdir_teardown(struct i40e_pf *pf)
7046{
Alexander Duyck4b816442016-10-11 15:26:53 -07007047 struct i40e_vsi *vsi;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007048
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00007049 i40e_fdir_filter_exit(pf);
Alexander Duyck4b816442016-10-11 15:26:53 -07007050 vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
7051 if (vsi)
7052 i40e_vsi_release(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007053}
7054
7055/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007056 * i40e_prep_for_reset - prep for the core to reset
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007057 * @pf: board private structure
Maciej Sosin373149f2017-04-05 07:50:55 -04007058 * @lock_acquired: indicates whether or not the lock has been acquired
7059 * before this function was called.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007060 *
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00007061 * Close up the VFs and other things in prep for PF Reset.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007062 **/
Maciej Sosin373149f2017-04-05 07:50:55 -04007063static void i40e_prep_for_reset(struct i40e_pf *pf, bool lock_acquired)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007064{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007065 struct i40e_hw *hw = &pf->hw;
Shannon Nelson60442de2014-04-23 04:50:13 +00007066 i40e_status ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007067 u32 v;
7068
Jacob Keller0da36b92017-04-19 09:25:55 -04007069 clear_bit(__I40E_RESET_INTR_RECEIVED, pf->state);
7070 if (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
Shannon Nelson23cfbe02014-06-04 01:23:14 +00007071 return;
Mitch Williamsd3ce57342016-03-10 14:59:46 -08007072 if (i40e_check_asq_alive(&pf->hw))
7073 i40e_vc_notify_reset(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007074
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00007075 dev_dbg(&pf->pdev->dev, "Tearing down internal switch for reset\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007076
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007077 /* quiesce the VSIs and their queues that are not already DOWN */
Maciej Sosin373149f2017-04-05 07:50:55 -04007078 /* pf_quiesce_all_vsi modifies netdev structures -rtnl_lock needed */
7079 if (!lock_acquired)
7080 rtnl_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007081 i40e_pf_quiesce_all_vsi(pf);
Maciej Sosin373149f2017-04-05 07:50:55 -04007082 if (!lock_acquired)
7083 rtnl_unlock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007084
Mitch Williams505682c2014-05-20 08:01:37 +00007085 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007086 if (pf->vsi[v])
7087 pf->vsi[v]->seid = 0;
7088 }
7089
7090 i40e_shutdown_adminq(&pf->hw);
7091
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007092 /* call shutdown HMC */
Shannon Nelson60442de2014-04-23 04:50:13 +00007093 if (hw->hmc.hmc_obj) {
7094 ret = i40e_shutdown_lan_hmc(hw);
Shannon Nelson23cfbe02014-06-04 01:23:14 +00007095 if (ret)
Shannon Nelson60442de2014-04-23 04:50:13 +00007096 dev_warn(&pf->pdev->dev,
7097 "shutdown_lan_hmc failed: %d\n", ret);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007098 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007099}
7100
7101/**
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00007102 * i40e_send_version - update firmware with driver version
7103 * @pf: PF struct
7104 */
7105static void i40e_send_version(struct i40e_pf *pf)
7106{
7107 struct i40e_driver_version dv;
7108
7109 dv.major_version = DRV_VERSION_MAJOR;
7110 dv.minor_version = DRV_VERSION_MINOR;
7111 dv.build_version = DRV_VERSION_BUILD;
7112 dv.subbuild_version = 0;
Rickard Strandqvist35a7d802014-07-29 09:26:25 +00007113 strlcpy(dv.driver_string, DRV_VERSION, sizeof(dv.driver_string));
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00007114 i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
7115}
7116
7117/**
Filip Sadowski5bbb2e22017-06-07 05:43:09 -04007118 * i40e_get_oem_version - get OEM specific version information
7119 * @hw: pointer to the hardware structure
7120 **/
7121static void i40e_get_oem_version(struct i40e_hw *hw)
7122{
7123 u16 block_offset = 0xffff;
7124 u16 block_length = 0;
7125 u16 capabilities = 0;
7126 u16 gen_snap = 0;
7127 u16 release = 0;
7128
7129#define I40E_SR_NVM_OEM_VERSION_PTR 0x1B
7130#define I40E_NVM_OEM_LENGTH_OFFSET 0x00
7131#define I40E_NVM_OEM_CAPABILITIES_OFFSET 0x01
7132#define I40E_NVM_OEM_GEN_OFFSET 0x02
7133#define I40E_NVM_OEM_RELEASE_OFFSET 0x03
7134#define I40E_NVM_OEM_CAPABILITIES_MASK 0x000F
7135#define I40E_NVM_OEM_LENGTH 3
7136
7137 /* Check if pointer to OEM version block is valid. */
7138 i40e_read_nvm_word(hw, I40E_SR_NVM_OEM_VERSION_PTR, &block_offset);
7139 if (block_offset == 0xffff)
7140 return;
7141
7142 /* Check if OEM version block has correct length. */
7143 i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_LENGTH_OFFSET,
7144 &block_length);
7145 if (block_length < I40E_NVM_OEM_LENGTH)
7146 return;
7147
7148 /* Check if OEM version format is as expected. */
7149 i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_CAPABILITIES_OFFSET,
7150 &capabilities);
7151 if ((capabilities & I40E_NVM_OEM_CAPABILITIES_MASK) != 0)
7152 return;
7153
7154 i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_GEN_OFFSET,
7155 &gen_snap);
7156 i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_RELEASE_OFFSET,
7157 &release);
7158 hw->nvm.oem_ver = (gen_snap << I40E_OEM_SNAP_SHIFT) | release;
7159 hw->nvm.eetrack = I40E_OEM_EETRACK_ID;
7160}
7161
7162/**
Maciej Sosin373149f2017-04-05 07:50:55 -04007163 * i40e_reset - wait for core reset to finish reset, reset pf if corer not seen
7164 * @pf: board private structure
7165 **/
7166static int i40e_reset(struct i40e_pf *pf)
7167{
7168 struct i40e_hw *hw = &pf->hw;
7169 i40e_status ret;
7170
7171 ret = i40e_pf_reset(hw);
7172 if (ret) {
7173 dev_info(&pf->pdev->dev, "PF reset failed, %d\n", ret);
Jacob Keller0da36b92017-04-19 09:25:55 -04007174 set_bit(__I40E_RESET_FAILED, pf->state);
7175 clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
Maciej Sosin373149f2017-04-05 07:50:55 -04007176 } else {
7177 pf->pfr_count++;
7178 }
7179 return ret;
7180}
7181
7182/**
7183 * i40e_rebuild - rebuild using a saved config
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007184 * @pf: board private structure
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007185 * @reinit: if the Main VSI needs to re-initialized.
Maciej Sosin373149f2017-04-05 07:50:55 -04007186 * @lock_acquired: indicates whether or not the lock has been acquired
7187 * before this function was called.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007188 **/
Maciej Sosin373149f2017-04-05 07:50:55 -04007189static void i40e_rebuild(struct i40e_pf *pf, bool reinit, bool lock_acquired)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007190{
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007191 struct i40e_hw *hw = &pf->hw;
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00007192 u8 set_fc_aq_fail = 0;
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007193 i40e_status ret;
Anjali Singhai Jain4f2f017c2015-10-21 19:47:07 -04007194 u32 val;
Maciej Sosin373149f2017-04-05 07:50:55 -04007195 int v;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007196
Mauro S. M. Rodrigues9e6c9c02017-05-12 23:26:56 -03007197 if (test_bit(__I40E_DOWN, pf->state))
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00007198 goto clear_recovery;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00007199 dev_dbg(&pf->pdev->dev, "Rebuilding internal switch\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007200
7201 /* rebuild the basics for the AdminQ, HMC, and initial HW switch */
7202 ret = i40e_init_adminq(&pf->hw);
7203 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04007204 dev_info(&pf->pdev->dev, "Rebuild AdminQ failed, err %s aq_err %s\n",
7205 i40e_stat_str(&pf->hw, ret),
7206 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00007207 goto clear_recovery;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007208 }
Filip Sadowski5bbb2e22017-06-07 05:43:09 -04007209 i40e_get_oem_version(&pf->hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007210
Shannon Nelson4eb3f762014-03-06 08:59:58 +00007211 /* re-verify the eeprom if we just had an EMP reset */
Jacob Keller0da36b92017-04-19 09:25:55 -04007212 if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state))
Shannon Nelson4eb3f762014-03-06 08:59:58 +00007213 i40e_verify_eeprom(pf);
Shannon Nelson4eb3f762014-03-06 08:59:58 +00007214
Shannon Nelsone78ac4bf2014-05-10 04:49:09 +00007215 i40e_clear_pxe_mode(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007216 ret = i40e_get_capabilities(pf);
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04007217 if (ret)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007218 goto end_core_reset;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007219
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007220 ret = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
Alexander Duyckc76cb6e2017-02-21 15:55:48 -08007221 hw->func_caps.num_rx_qp, 0, 0);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007222 if (ret) {
7223 dev_info(&pf->pdev->dev, "init_lan_hmc failed: %d\n", ret);
7224 goto end_core_reset;
7225 }
7226 ret = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
7227 if (ret) {
7228 dev_info(&pf->pdev->dev, "configure_lan_hmc failed: %d\n", ret);
7229 goto end_core_reset;
7230 }
7231
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08007232#ifdef CONFIG_I40E_DCB
7233 ret = i40e_init_pf_dcb(pf);
7234 if (ret) {
Shannon Nelsonaebfc812014-12-11 07:06:38 +00007235 dev_info(&pf->pdev->dev, "DCB init failed %d, disabled\n", ret);
7236 pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
7237 /* Continue without DCB enabled */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08007238 }
7239#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007240 /* do basic switch setup */
Maciej Sosin373149f2017-04-05 07:50:55 -04007241 if (!lock_acquired)
7242 rtnl_lock();
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007243 ret = i40e_setup_pf_switch(pf, reinit);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007244 if (ret)
Maciej Sosin373149f2017-04-05 07:50:55 -04007245 goto end_unlock;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007246
Shannon Nelson2f0aff42016-01-04 10:33:08 -08007247 /* The driver only wants link up/down and module qualification
7248 * reports from firmware. Note the negative logic.
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +00007249 */
7250 ret = i40e_aq_set_phy_int_mask(&pf->hw,
Shannon Nelson2f0aff42016-01-04 10:33:08 -08007251 ~(I40E_AQ_EVENT_LINK_UPDOWN |
Shannon Nelson867a79e2016-03-18 12:18:15 -07007252 I40E_AQ_EVENT_MEDIA_NA |
Shannon Nelson2f0aff42016-01-04 10:33:08 -08007253 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +00007254 if (ret)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04007255 dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
7256 i40e_stat_str(&pf->hw, ret),
7257 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +00007258
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00007259 /* make sure our flow control settings are restored */
7260 ret = i40e_set_fc(&pf->hw, &set_fc_aq_fail, true);
7261 if (ret)
Neerav Parikh8279e492015-09-03 17:18:50 -04007262 dev_dbg(&pf->pdev->dev, "setting flow control: ret = %s last_status = %s\n",
7263 i40e_stat_str(&pf->hw, ret),
7264 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00007265
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007266 /* Rebuild the VSIs and VEBs that existed before reset.
7267 * They are still in our local switch element arrays, so only
7268 * need to rebuild the switch model in the HW.
7269 *
7270 * If there were VEBs but the reconstitution failed, we'll try
7271 * try to recover minimal use by getting the basic PF VSI working.
7272 */
7273 if (pf->vsi[pf->lan_vsi]->uplink_seid != pf->mac_seid) {
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00007274 dev_dbg(&pf->pdev->dev, "attempting to rebuild switch\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007275 /* find the one VEB connected to the MAC, and find orphans */
7276 for (v = 0; v < I40E_MAX_VEB; v++) {
7277 if (!pf->veb[v])
7278 continue;
7279
7280 if (pf->veb[v]->uplink_seid == pf->mac_seid ||
7281 pf->veb[v]->uplink_seid == 0) {
7282 ret = i40e_reconstitute_veb(pf->veb[v]);
7283
7284 if (!ret)
7285 continue;
7286
7287 /* If Main VEB failed, we're in deep doodoo,
7288 * so give up rebuilding the switch and set up
7289 * for minimal rebuild of PF VSI.
7290 * If orphan failed, we'll report the error
7291 * but try to keep going.
7292 */
7293 if (pf->veb[v]->uplink_seid == pf->mac_seid) {
7294 dev_info(&pf->pdev->dev,
7295 "rebuild of switch failed: %d, will try to set up simple PF connection\n",
7296 ret);
7297 pf->vsi[pf->lan_vsi]->uplink_seid
7298 = pf->mac_seid;
7299 break;
7300 } else if (pf->veb[v]->uplink_seid == 0) {
7301 dev_info(&pf->pdev->dev,
7302 "rebuild of orphan VEB failed: %d\n",
7303 ret);
7304 }
7305 }
7306 }
7307 }
7308
7309 if (pf->vsi[pf->lan_vsi]->uplink_seid == pf->mac_seid) {
Shannon Nelsoncde4cbc2014-06-04 01:23:17 +00007310 dev_dbg(&pf->pdev->dev, "attempting to rebuild PF VSI\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007311 /* no VEB, so rebuild only the Main VSI */
7312 ret = i40e_add_vsi(pf->vsi[pf->lan_vsi]);
7313 if (ret) {
7314 dev_info(&pf->pdev->dev,
7315 "rebuild of Main VSI failed: %d\n", ret);
Maciej Sosin373149f2017-04-05 07:50:55 -04007316 goto end_unlock;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007317 }
7318 }
7319
Anjali Singhai Jain4f2f017c2015-10-21 19:47:07 -04007320 /* Reconfigure hardware for allowing smaller MSS in the case
7321 * of TSO, so that we avoid the MDD being fired and causing
7322 * a reset in the case of small MSS+TSO.
7323 */
7324#define I40E_REG_MSS 0x000E64DC
7325#define I40E_REG_MSS_MIN_MASK 0x3FF0000
7326#define I40E_64BYTE_MSS 0x400000
7327 val = rd32(hw, I40E_REG_MSS);
7328 if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
7329 val &= ~I40E_REG_MSS_MIN_MASK;
7330 val |= I40E_64BYTE_MSS;
7331 wr32(hw, I40E_REG_MSS, val);
7332 }
7333
Anjali Singhai Jain8eed76f2015-12-09 15:50:31 -08007334 if (pf->flags & I40E_FLAG_RESTART_AUTONEG) {
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +00007335 msleep(75);
7336 ret = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
7337 if (ret)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04007338 dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
7339 i40e_stat_str(&pf->hw, ret),
7340 i40e_aq_str(&pf->hw,
7341 pf->hw.aq.asq_last_status));
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00007342 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007343 /* reinit the misc interrupt */
7344 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
7345 ret = i40e_setup_misc_vector(pf);
7346
Anjali Singhai Jaine7358f52015-10-01 14:37:34 -04007347 /* Add a filter to drop all Flow control frames from any VSI from being
7348 * transmitted. By doing so we stop a malicious VF from sending out
7349 * PAUSE or PFC frames and potentially controlling traffic for other
7350 * PF/VF VSIs.
7351 * The FW can still send Flow control frames if enabled.
7352 */
7353 i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
7354 pf->main_vsi_seid);
7355
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007356 /* restart the VSIs that were rebuilt and running before the reset */
7357 i40e_pf_unquiesce_all_vsi(pf);
7358
Jacob Keller024b05f2017-04-13 04:45:46 -04007359 /* Release the RTNL lock before we start resetting VFs */
7360 if (!lock_acquired)
7361 rtnl_unlock();
7362
Jacob Kellere4b433f2017-04-13 04:45:52 -04007363 i40e_reset_all_vfs(pf, true);
Mitch Williams69f64b22014-02-13 03:48:46 -08007364
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007365 /* tell the firmware that we're starting */
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00007366 i40e_send_version(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007367
Jacob Keller024b05f2017-04-13 04:45:46 -04007368 /* We've already released the lock, so don't do it again */
7369 goto end_core_reset;
7370
Maciej Sosin373149f2017-04-05 07:50:55 -04007371end_unlock:
Jacob Keller024b05f2017-04-13 04:45:46 -04007372 if (!lock_acquired)
7373 rtnl_unlock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007374end_core_reset:
Jacob Keller0da36b92017-04-19 09:25:55 -04007375 clear_bit(__I40E_RESET_FAILED, pf->state);
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00007376clear_recovery:
Jacob Keller0da36b92017-04-19 09:25:55 -04007377 clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007378}
7379
7380/**
Maciej Sosin373149f2017-04-05 07:50:55 -04007381 * i40e_reset_and_rebuild - reset and rebuild using a saved config
7382 * @pf: board private structure
7383 * @reinit: if the Main VSI needs to re-initialized.
7384 * @lock_acquired: indicates whether or not the lock has been acquired
7385 * before this function was called.
7386 **/
7387static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit,
7388 bool lock_acquired)
7389{
7390 int ret;
7391 /* Now we wait for GRST to settle out.
7392 * We don't have to delete the VEBs or VSIs from the hw switch
7393 * because the reset will make them disappear.
7394 */
7395 ret = i40e_reset(pf);
7396 if (!ret)
7397 i40e_rebuild(pf, reinit, lock_acquired);
7398}
7399
7400/**
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00007401 * i40e_handle_reset_warning - prep for the PF to reset, reset and rebuild
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007402 * @pf: board private structure
7403 *
7404 * Close up the VFs and other things in prep for a Core Reset,
7405 * then get ready to rebuild the world.
Maciej Sosin373149f2017-04-05 07:50:55 -04007406 * @lock_acquired: indicates whether or not the lock has been acquired
7407 * before this function was called.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007408 **/
Maciej Sosin373149f2017-04-05 07:50:55 -04007409static void i40e_handle_reset_warning(struct i40e_pf *pf, bool lock_acquired)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007410{
Maciej Sosin373149f2017-04-05 07:50:55 -04007411 i40e_prep_for_reset(pf, lock_acquired);
7412 i40e_reset_and_rebuild(pf, false, lock_acquired);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007413}
7414
7415/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007416 * i40e_handle_mdd_event
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00007417 * @pf: pointer to the PF structure
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007418 *
7419 * Called from the MDD irq handler to identify possibly malicious vfs
7420 **/
7421static void i40e_handle_mdd_event(struct i40e_pf *pf)
7422{
7423 struct i40e_hw *hw = &pf->hw;
7424 bool mdd_detected = false;
Neerav Parikhdf430b12014-06-04 01:23:15 +00007425 bool pf_mdd_detected = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007426 struct i40e_vf *vf;
7427 u32 reg;
7428 int i;
7429
Jacob Keller0da36b92017-04-19 09:25:55 -04007430 if (!test_bit(__I40E_MDD_EVENT_PENDING, pf->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007431 return;
7432
7433 /* find what triggered the MDD event */
7434 reg = rd32(hw, I40E_GL_MDET_TX);
7435 if (reg & I40E_GL_MDET_TX_VALID_MASK) {
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00007436 u8 pf_num = (reg & I40E_GL_MDET_TX_PF_NUM_MASK) >>
7437 I40E_GL_MDET_TX_PF_NUM_SHIFT;
Mitch Williams2089ad02014-10-17 03:14:53 +00007438 u16 vf_num = (reg & I40E_GL_MDET_TX_VF_NUM_MASK) >>
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00007439 I40E_GL_MDET_TX_VF_NUM_SHIFT;
Dan Carpenter013f6572014-10-22 20:06:29 -07007440 u8 event = (reg & I40E_GL_MDET_TX_EVENT_MASK) >>
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00007441 I40E_GL_MDET_TX_EVENT_SHIFT;
Mitch Williams2089ad02014-10-17 03:14:53 +00007442 u16 queue = ((reg & I40E_GL_MDET_TX_QUEUE_MASK) >>
7443 I40E_GL_MDET_TX_QUEUE_SHIFT) -
7444 pf->hw.func_caps.base_queue;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007445 if (netif_msg_tx_err(pf))
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00007446 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 +00007447 event, queue, pf_num, vf_num);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007448 wr32(hw, I40E_GL_MDET_TX, 0xffffffff);
7449 mdd_detected = true;
7450 }
7451 reg = rd32(hw, I40E_GL_MDET_RX);
7452 if (reg & I40E_GL_MDET_RX_VALID_MASK) {
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00007453 u8 func = (reg & I40E_GL_MDET_RX_FUNCTION_MASK) >>
7454 I40E_GL_MDET_RX_FUNCTION_SHIFT;
Dan Carpenter013f6572014-10-22 20:06:29 -07007455 u8 event = (reg & I40E_GL_MDET_RX_EVENT_MASK) >>
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00007456 I40E_GL_MDET_RX_EVENT_SHIFT;
Mitch Williams2089ad02014-10-17 03:14:53 +00007457 u16 queue = ((reg & I40E_GL_MDET_RX_QUEUE_MASK) >>
7458 I40E_GL_MDET_RX_QUEUE_SHIFT) -
7459 pf->hw.func_caps.base_queue;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007460 if (netif_msg_rx_err(pf))
7461 dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on RX queue %d of function 0x%02x\n",
7462 event, queue, func);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007463 wr32(hw, I40E_GL_MDET_RX, 0xffffffff);
7464 mdd_detected = true;
7465 }
7466
Neerav Parikhdf430b12014-06-04 01:23:15 +00007467 if (mdd_detected) {
7468 reg = rd32(hw, I40E_PF_MDET_TX);
7469 if (reg & I40E_PF_MDET_TX_VALID_MASK) {
7470 wr32(hw, I40E_PF_MDET_TX, 0xFFFF);
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007471 dev_info(&pf->pdev->dev, "TX driver issue detected, PF reset issued\n");
Neerav Parikhdf430b12014-06-04 01:23:15 +00007472 pf_mdd_detected = true;
7473 }
7474 reg = rd32(hw, I40E_PF_MDET_RX);
7475 if (reg & I40E_PF_MDET_RX_VALID_MASK) {
7476 wr32(hw, I40E_PF_MDET_RX, 0xFFFF);
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007477 dev_info(&pf->pdev->dev, "RX driver issue detected, PF reset issued\n");
Neerav Parikhdf430b12014-06-04 01:23:15 +00007478 pf_mdd_detected = true;
7479 }
7480 /* Queue belongs to the PF, initiate a reset */
7481 if (pf_mdd_detected) {
Jacob Keller0da36b92017-04-19 09:25:55 -04007482 set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
Neerav Parikhdf430b12014-06-04 01:23:15 +00007483 i40e_service_event_schedule(pf);
7484 }
7485 }
7486
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007487 /* see if one of the VFs needs its hand slapped */
7488 for (i = 0; i < pf->num_alloc_vfs && mdd_detected; i++) {
7489 vf = &(pf->vf[i]);
7490 reg = rd32(hw, I40E_VP_MDET_TX(i));
7491 if (reg & I40E_VP_MDET_TX_VALID_MASK) {
7492 wr32(hw, I40E_VP_MDET_TX(i), 0xFFFF);
7493 vf->num_mdd_events++;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007494 dev_info(&pf->pdev->dev, "TX driver issue detected on VF %d\n",
7495 i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007496 }
7497
7498 reg = rd32(hw, I40E_VP_MDET_RX(i));
7499 if (reg & I40E_VP_MDET_RX_VALID_MASK) {
7500 wr32(hw, I40E_VP_MDET_RX(i), 0xFFFF);
7501 vf->num_mdd_events++;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007502 dev_info(&pf->pdev->dev, "RX driver issue detected on VF %d\n",
7503 i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007504 }
7505
7506 if (vf->num_mdd_events > I40E_DEFAULT_NUM_MDD_EVENTS_ALLOWED) {
7507 dev_info(&pf->pdev->dev,
7508 "Too many MDD events on VF %d, disabled\n", i);
7509 dev_info(&pf->pdev->dev,
7510 "Use PF Control I/F to re-enable the VF\n");
Jacob Keller6322e632017-04-13 04:45:54 -04007511 set_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007512 }
7513 }
7514
7515 /* re-enable mdd interrupt cause */
Jacob Keller0da36b92017-04-19 09:25:55 -04007516 clear_bit(__I40E_MDD_EVENT_PENDING, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007517 reg = rd32(hw, I40E_PFINT_ICR0_ENA);
7518 reg |= I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
7519 wr32(hw, I40E_PFINT_ICR0_ENA, reg);
7520 i40e_flush(hw);
7521}
7522
Jacob Kellerd8b2c702017-06-20 15:17:00 -07007523static const char *i40e_tunnel_name(struct i40e_udp_port_config *port)
7524{
7525 switch (port->type) {
7526 case UDP_TUNNEL_TYPE_VXLAN:
7527 return "vxlan";
7528 case UDP_TUNNEL_TYPE_GENEVE:
7529 return "geneve";
7530 default:
7531 return "unknown";
7532 }
7533}
7534
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007535/**
Alexander Duyck1f190d92017-04-19 09:25:51 -04007536 * i40e_sync_udp_filters - Trigger a sync event for existing UDP filters
7537 * @pf: board private structure
7538 **/
7539static void i40e_sync_udp_filters(struct i40e_pf *pf)
7540{
7541 int i;
7542
7543 /* loop through and set pending bit for all active UDP filters */
7544 for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
7545 if (pf->udp_ports[i].port)
7546 pf->pending_udp_bitmap |= BIT_ULL(i);
7547 }
7548
7549 pf->flags |= I40E_FLAG_UDP_FILTER_SYNC;
7550}
7551
7552/**
Singhai, Anjali6a899022015-12-14 12:21:18 -08007553 * i40e_sync_udp_filters_subtask - Sync the VSI filter list with HW
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007554 * @pf: board private structure
7555 **/
Singhai, Anjali6a899022015-12-14 12:21:18 -08007556static void i40e_sync_udp_filters_subtask(struct i40e_pf *pf)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007557{
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007558 struct i40e_hw *hw = &pf->hw;
7559 i40e_status ret;
Jacob Kellerfe0b0cd2017-02-06 14:38:38 -08007560 u16 port;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007561 int i;
7562
Singhai, Anjali6a899022015-12-14 12:21:18 -08007563 if (!(pf->flags & I40E_FLAG_UDP_FILTER_SYNC))
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007564 return;
7565
Singhai, Anjali6a899022015-12-14 12:21:18 -08007566 pf->flags &= ~I40E_FLAG_UDP_FILTER_SYNC;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007567
7568 for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
Singhai, Anjali6a899022015-12-14 12:21:18 -08007569 if (pf->pending_udp_bitmap & BIT_ULL(i)) {
7570 pf->pending_udp_bitmap &= ~BIT_ULL(i);
Jacob Keller27826fd2017-04-19 09:25:50 -04007571 port = pf->udp_ports[i].port;
Shannon Nelsonc22c06c2015-03-31 00:45:04 -07007572 if (port)
Carolyn Wybornyb3f5c7b2016-08-24 11:33:51 -07007573 ret = i40e_aq_add_udp_tunnel(hw, port,
7574 pf->udp_ports[i].type,
7575 NULL, NULL);
Shannon Nelsonc22c06c2015-03-31 00:45:04 -07007576 else
7577 ret = i40e_aq_del_udp_tunnel(hw, i, NULL);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007578
7579 if (ret) {
Jacob Kellerd8b2c702017-06-20 15:17:00 -07007580 dev_info(&pf->pdev->dev,
7581 "%s %s port %d, index %d failed, err %s aq_err %s\n",
7582 i40e_tunnel_name(&pf->udp_ports[i]),
7583 port ? "add" : "delete",
7584 port, i,
7585 i40e_stat_str(&pf->hw, ret),
7586 i40e_aq_str(&pf->hw,
7587 pf->hw.aq.asq_last_status));
Jacob Keller27826fd2017-04-19 09:25:50 -04007588 pf->udp_ports[i].port = 0;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007589 }
7590 }
7591 }
7592}
7593
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007594/**
7595 * i40e_service_task - Run the driver's async subtasks
7596 * @work: pointer to work_struct containing our data
7597 **/
7598static void i40e_service_task(struct work_struct *work)
7599{
7600 struct i40e_pf *pf = container_of(work,
7601 struct i40e_pf,
7602 service_task);
7603 unsigned long start_time = jiffies;
7604
Shannon Nelsone57a2fe2014-06-03 23:50:19 +00007605 /* don't bother with service tasks if a reset is in progress */
Jacob Keller0da36b92017-04-19 09:25:55 -04007606 if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
Shannon Nelsone57a2fe2014-06-03 23:50:19 +00007607 return;
Shannon Nelsone57a2fe2014-06-03 23:50:19 +00007608
Jacob Keller0da36b92017-04-19 09:25:55 -04007609 if (test_and_set_bit(__I40E_SERVICE_SCHED, pf->state))
Mitch Williams91089032016-11-21 13:03:51 -08007610 return;
7611
Kiran Patilb03a8c12015-09-24 18:13:15 -04007612 i40e_detect_recover_hung(pf);
Jesse Brandeburg2818ccd2016-01-13 16:51:38 -08007613 i40e_sync_filters_subtask(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007614 i40e_reset_subtask(pf);
7615 i40e_handle_mdd_event(pf);
7616 i40e_vc_process_vflr_event(pf);
7617 i40e_watchdog_subtask(pf);
7618 i40e_fdir_reinit_subtask(pf);
Mitch Williams0ef2d5a2017-01-24 10:24:00 -08007619 if (pf->flags & I40E_FLAG_CLIENT_RESET) {
7620 /* Client subtask will reopen next time through. */
7621 i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], true);
7622 pf->flags &= ~I40E_FLAG_CLIENT_RESET;
7623 } else {
7624 i40e_client_subtask(pf);
7625 if (pf->flags & I40E_FLAG_CLIENT_L2_CHANGE) {
7626 i40e_notify_client_of_l2_param_changes(
7627 pf->vsi[pf->lan_vsi]);
7628 pf->flags &= ~I40E_FLAG_CLIENT_L2_CHANGE;
7629 }
7630 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007631 i40e_sync_filters_subtask(pf);
Singhai, Anjali6a899022015-12-14 12:21:18 -08007632 i40e_sync_udp_filters_subtask(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007633 i40e_clean_adminq_subtask(pf);
7634
Mitch Williams91089032016-11-21 13:03:51 -08007635 /* flush memory to make sure state is correct before next watchdog */
7636 smp_mb__before_atomic();
Jacob Keller0da36b92017-04-19 09:25:55 -04007637 clear_bit(__I40E_SERVICE_SCHED, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007638
7639 /* If the tasks have taken longer than one timer cycle or there
7640 * is more work to be done, reschedule the service task now
7641 * rather than wait for the timer to tick again.
7642 */
7643 if (time_after(jiffies, (start_time + pf->service_timer_period)) ||
Jacob Keller0da36b92017-04-19 09:25:55 -04007644 test_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state) ||
7645 test_bit(__I40E_MDD_EVENT_PENDING, pf->state) ||
7646 test_bit(__I40E_VFLR_EVENT_PENDING, pf->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007647 i40e_service_event_schedule(pf);
7648}
7649
7650/**
7651 * i40e_service_timer - timer callback
7652 * @data: pointer to PF struct
7653 **/
7654static void i40e_service_timer(unsigned long data)
7655{
7656 struct i40e_pf *pf = (struct i40e_pf *)data;
7657
7658 mod_timer(&pf->service_timer,
7659 round_jiffies(jiffies + pf->service_timer_period));
7660 i40e_service_event_schedule(pf);
7661}
7662
7663/**
7664 * i40e_set_num_rings_in_vsi - Determine number of rings in the VSI
7665 * @vsi: the VSI being configured
7666 **/
7667static int i40e_set_num_rings_in_vsi(struct i40e_vsi *vsi)
7668{
7669 struct i40e_pf *pf = vsi->back;
7670
7671 switch (vsi->type) {
7672 case I40E_VSI_MAIN:
7673 vsi->alloc_queue_pairs = pf->num_lan_qps;
7674 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
7675 I40E_REQ_DESCRIPTOR_MULTIPLE);
7676 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
7677 vsi->num_q_vectors = pf->num_lan_msix;
7678 else
7679 vsi->num_q_vectors = 1;
7680
7681 break;
7682
7683 case I40E_VSI_FDIR:
7684 vsi->alloc_queue_pairs = 1;
7685 vsi->num_desc = ALIGN(I40E_FDIR_RING_COUNT,
7686 I40E_REQ_DESCRIPTOR_MULTIPLE);
Tushar Davea70e4072016-05-16 12:40:53 -07007687 vsi->num_q_vectors = pf->num_fdsb_msix;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007688 break;
7689
7690 case I40E_VSI_VMDQ2:
7691 vsi->alloc_queue_pairs = pf->num_vmdq_qps;
7692 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
7693 I40E_REQ_DESCRIPTOR_MULTIPLE);
7694 vsi->num_q_vectors = pf->num_vmdq_msix;
7695 break;
7696
7697 case I40E_VSI_SRIOV:
7698 vsi->alloc_queue_pairs = pf->num_vf_qps;
7699 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
7700 I40E_REQ_DESCRIPTOR_MULTIPLE);
7701 break;
7702
7703 default:
7704 WARN_ON(1);
7705 return -ENODATA;
7706 }
7707
7708 return 0;
7709}
7710
7711/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007712 * i40e_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the vsi
7713 * @type: VSI pointer
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007714 * @alloc_qvectors: a bool to specify if q_vectors need to be allocated.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007715 *
7716 * On error: returns error code (negative)
7717 * On success: returns 0
7718 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007719static int i40e_vsi_alloc_arrays(struct i40e_vsi *vsi, bool alloc_qvectors)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007720{
Björn Töpel74608d12017-05-24 07:55:35 +02007721 struct i40e_ring **next_rings;
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007722 int size;
7723 int ret = 0;
7724
Björn Töpel74608d12017-05-24 07:55:35 +02007725 /* allocate memory for both Tx, XDP Tx and Rx ring pointers */
7726 size = sizeof(struct i40e_ring *) * vsi->alloc_queue_pairs *
7727 (i40e_enabled_xdp_vsi(vsi) ? 3 : 2);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007728 vsi->tx_rings = kzalloc(size, GFP_KERNEL);
7729 if (!vsi->tx_rings)
7730 return -ENOMEM;
Björn Töpel74608d12017-05-24 07:55:35 +02007731 next_rings = vsi->tx_rings + vsi->alloc_queue_pairs;
7732 if (i40e_enabled_xdp_vsi(vsi)) {
7733 vsi->xdp_rings = next_rings;
7734 next_rings += vsi->alloc_queue_pairs;
7735 }
7736 vsi->rx_rings = next_rings;
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007737
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007738 if (alloc_qvectors) {
7739 /* allocate memory for q_vector pointers */
Julia Lawallf57e4fb2014-07-30 03:11:09 +00007740 size = sizeof(struct i40e_q_vector *) * vsi->num_q_vectors;
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007741 vsi->q_vectors = kzalloc(size, GFP_KERNEL);
7742 if (!vsi->q_vectors) {
7743 ret = -ENOMEM;
7744 goto err_vectors;
7745 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007746 }
7747 return ret;
7748
7749err_vectors:
7750 kfree(vsi->tx_rings);
7751 return ret;
7752}
7753
7754/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007755 * i40e_vsi_mem_alloc - Allocates the next available struct vsi in the PF
7756 * @pf: board private structure
7757 * @type: type of VSI
7758 *
7759 * On error: returns error code (negative)
7760 * On success: returns vsi index in PF (positive)
7761 **/
7762static int i40e_vsi_mem_alloc(struct i40e_pf *pf, enum i40e_vsi_type type)
7763{
7764 int ret = -ENODEV;
7765 struct i40e_vsi *vsi;
7766 int vsi_idx;
7767 int i;
7768
7769 /* Need to protect the allocation of the VSIs at the PF level */
7770 mutex_lock(&pf->switch_mutex);
7771
7772 /* VSI list may be fragmented if VSI creation/destruction has
7773 * been happening. We can afford to do a quick scan to look
7774 * for any free VSIs in the list.
7775 *
7776 * find next empty vsi slot, looping back around if necessary
7777 */
7778 i = pf->next_vsi;
Mitch Williams505682c2014-05-20 08:01:37 +00007779 while (i < pf->num_alloc_vsi && pf->vsi[i])
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007780 i++;
Mitch Williams505682c2014-05-20 08:01:37 +00007781 if (i >= pf->num_alloc_vsi) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007782 i = 0;
7783 while (i < pf->next_vsi && pf->vsi[i])
7784 i++;
7785 }
7786
Mitch Williams505682c2014-05-20 08:01:37 +00007787 if (i < pf->num_alloc_vsi && !pf->vsi[i]) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007788 vsi_idx = i; /* Found one! */
7789 } else {
7790 ret = -ENODEV;
Alexander Duyck493fb302013-09-28 07:01:44 +00007791 goto unlock_pf; /* out of VSI slots! */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007792 }
7793 pf->next_vsi = ++i;
7794
7795 vsi = kzalloc(sizeof(*vsi), GFP_KERNEL);
7796 if (!vsi) {
7797 ret = -ENOMEM;
Alexander Duyck493fb302013-09-28 07:01:44 +00007798 goto unlock_pf;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007799 }
7800 vsi->type = type;
7801 vsi->back = pf;
Jacob Keller0da36b92017-04-19 09:25:55 -04007802 set_bit(__I40E_VSI_DOWN, vsi->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007803 vsi->flags = 0;
7804 vsi->idx = vsi_idx;
Jesse Brandeburgac26fc12015-09-28 14:12:37 -04007805 vsi->int_rate_limit = 0;
Anjali Singhai Jain5db4cb52015-02-24 06:58:49 +00007806 vsi->rss_table_size = (vsi->type == I40E_VSI_MAIN) ?
7807 pf->rss_table_size : 64;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007808 vsi->netdev_registered = false;
7809 vsi->work_limit = I40E_DEFAULT_IRQ_WORK;
Jacob Keller278e7d02016-10-05 09:30:37 -07007810 hash_init(vsi->mac_filter_hash);
Shannon Nelson63741842014-04-23 04:50:16 +00007811 vsi->irqs_ready = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007812
Alexander Duyck9f65e152013-09-28 06:00:58 +00007813 ret = i40e_set_num_rings_in_vsi(vsi);
7814 if (ret)
7815 goto err_rings;
7816
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007817 ret = i40e_vsi_alloc_arrays(vsi, true);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007818 if (ret)
Alexander Duyck9f65e152013-09-28 06:00:58 +00007819 goto err_rings;
Alexander Duyck493fb302013-09-28 07:01:44 +00007820
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007821 /* Setup default MSIX irq handler for VSI */
7822 i40e_vsi_setup_irqhandler(vsi, i40e_msix_clean_rings);
7823
Kiran Patil21659032015-09-30 14:09:03 -04007824 /* Initialize VSI lock */
Jacob Keller278e7d02016-10-05 09:30:37 -07007825 spin_lock_init(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007826 pf->vsi[vsi_idx] = vsi;
7827 ret = vsi_idx;
Alexander Duyck493fb302013-09-28 07:01:44 +00007828 goto unlock_pf;
7829
Alexander Duyck9f65e152013-09-28 06:00:58 +00007830err_rings:
Alexander Duyck493fb302013-09-28 07:01:44 +00007831 pf->next_vsi = i - 1;
7832 kfree(vsi);
7833unlock_pf:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007834 mutex_unlock(&pf->switch_mutex);
7835 return ret;
7836}
7837
7838/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007839 * i40e_vsi_free_arrays - Free queue and vector pointer arrays for the VSI
7840 * @type: VSI pointer
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007841 * @free_qvectors: a bool to specify if q_vectors need to be freed.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007842 *
7843 * On error: returns error code (negative)
7844 * On success: returns 0
7845 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007846static void i40e_vsi_free_arrays(struct i40e_vsi *vsi, bool free_qvectors)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007847{
7848 /* free the ring and vector containers */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007849 if (free_qvectors) {
7850 kfree(vsi->q_vectors);
7851 vsi->q_vectors = NULL;
7852 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007853 kfree(vsi->tx_rings);
7854 vsi->tx_rings = NULL;
7855 vsi->rx_rings = NULL;
Björn Töpel74608d12017-05-24 07:55:35 +02007856 vsi->xdp_rings = NULL;
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007857}
7858
7859/**
Helin Zhang28c58692015-10-26 19:44:27 -04007860 * i40e_clear_rss_config_user - clear the user configured RSS hash keys
7861 * and lookup table
7862 * @vsi: Pointer to VSI structure
7863 */
7864static void i40e_clear_rss_config_user(struct i40e_vsi *vsi)
7865{
7866 if (!vsi)
7867 return;
7868
7869 kfree(vsi->rss_hkey_user);
7870 vsi->rss_hkey_user = NULL;
7871
7872 kfree(vsi->rss_lut_user);
7873 vsi->rss_lut_user = NULL;
7874}
7875
7876/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007877 * i40e_vsi_clear - Deallocate the VSI provided
7878 * @vsi: the VSI being un-configured
7879 **/
7880static int i40e_vsi_clear(struct i40e_vsi *vsi)
7881{
7882 struct i40e_pf *pf;
7883
7884 if (!vsi)
7885 return 0;
7886
7887 if (!vsi->back)
7888 goto free_vsi;
7889 pf = vsi->back;
7890
7891 mutex_lock(&pf->switch_mutex);
7892 if (!pf->vsi[vsi->idx]) {
7893 dev_err(&pf->pdev->dev, "pf->vsi[%d] is NULL, just free vsi[%d](%p,type %d)\n",
7894 vsi->idx, vsi->idx, vsi, vsi->type);
7895 goto unlock_vsi;
7896 }
7897
7898 if (pf->vsi[vsi->idx] != vsi) {
7899 dev_err(&pf->pdev->dev,
7900 "pf->vsi[%d](%p, type %d) != vsi[%d](%p,type %d): no free!\n",
7901 pf->vsi[vsi->idx]->idx,
7902 pf->vsi[vsi->idx],
7903 pf->vsi[vsi->idx]->type,
7904 vsi->idx, vsi, vsi->type);
7905 goto unlock_vsi;
7906 }
7907
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00007908 /* updates the PF for this cleared vsi */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007909 i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
7910 i40e_put_lump(pf->irq_pile, vsi->base_vector, vsi->idx);
7911
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007912 i40e_vsi_free_arrays(vsi, true);
Helin Zhang28c58692015-10-26 19:44:27 -04007913 i40e_clear_rss_config_user(vsi);
Alexander Duyck493fb302013-09-28 07:01:44 +00007914
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007915 pf->vsi[vsi->idx] = NULL;
7916 if (vsi->idx < pf->next_vsi)
7917 pf->next_vsi = vsi->idx;
7918
7919unlock_vsi:
7920 mutex_unlock(&pf->switch_mutex);
7921free_vsi:
7922 kfree(vsi);
7923
7924 return 0;
7925}
7926
7927/**
Alexander Duyck9f65e152013-09-28 06:00:58 +00007928 * i40e_vsi_clear_rings - Deallocates the Rx and Tx rings for the provided VSI
7929 * @vsi: the VSI being cleaned
7930 **/
Shannon Nelsonbe1d5ee2013-11-28 06:39:23 +00007931static void i40e_vsi_clear_rings(struct i40e_vsi *vsi)
Alexander Duyck9f65e152013-09-28 06:00:58 +00007932{
7933 int i;
7934
Greg Rose8e9dca52013-12-18 13:45:53 +00007935 if (vsi->tx_rings && vsi->tx_rings[0]) {
Neerav Parikhd7397642013-11-28 06:39:37 +00007936 for (i = 0; i < vsi->alloc_queue_pairs; i++) {
Mitch Williams00403f02013-09-28 07:13:13 +00007937 kfree_rcu(vsi->tx_rings[i], rcu);
7938 vsi->tx_rings[i] = NULL;
7939 vsi->rx_rings[i] = NULL;
Björn Töpel74608d12017-05-24 07:55:35 +02007940 if (vsi->xdp_rings)
7941 vsi->xdp_rings[i] = NULL;
Mitch Williams00403f02013-09-28 07:13:13 +00007942 }
Shannon Nelsonbe1d5ee2013-11-28 06:39:23 +00007943 }
Alexander Duyck9f65e152013-09-28 06:00:58 +00007944}
7945
7946/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007947 * i40e_alloc_rings - Allocates the Rx and Tx rings for the provided VSI
7948 * @vsi: the VSI being configured
7949 **/
7950static int i40e_alloc_rings(struct i40e_vsi *vsi)
7951{
Björn Töpel74608d12017-05-24 07:55:35 +02007952 int i, qpv = i40e_enabled_xdp_vsi(vsi) ? 3 : 2;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007953 struct i40e_pf *pf = vsi->back;
Björn Töpel74608d12017-05-24 07:55:35 +02007954 struct i40e_ring *ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007955
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007956 /* Set basic values in the rings to be used later during open() */
Neerav Parikhd7397642013-11-28 06:39:37 +00007957 for (i = 0; i < vsi->alloc_queue_pairs; i++) {
Shannon Nelsonac6c5e32013-11-20 10:02:57 +00007958 /* allocate space for both Tx and Rx in one shot */
Björn Töpel74608d12017-05-24 07:55:35 +02007959 ring = kcalloc(qpv, sizeof(struct i40e_ring), GFP_KERNEL);
7960 if (!ring)
Alexander Duyck9f65e152013-09-28 06:00:58 +00007961 goto err_out;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007962
Björn Töpel74608d12017-05-24 07:55:35 +02007963 ring->queue_index = i;
7964 ring->reg_idx = vsi->base_queue + i;
7965 ring->ring_active = false;
7966 ring->vsi = vsi;
7967 ring->netdev = vsi->netdev;
7968 ring->dev = &pf->pdev->dev;
7969 ring->count = vsi->num_desc;
7970 ring->size = 0;
7971 ring->dcb_tc = 0;
Anjali Singhai Jain8e0764b2015-06-05 12:20:30 -04007972 if (vsi->back->flags & I40E_FLAG_WB_ON_ITR_CAPABLE)
Björn Töpel74608d12017-05-24 07:55:35 +02007973 ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
7974 ring->tx_itr_setting = pf->tx_itr_default;
7975 vsi->tx_rings[i] = ring++;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007976
Björn Töpel74608d12017-05-24 07:55:35 +02007977 if (!i40e_enabled_xdp_vsi(vsi))
7978 goto setup_rx;
7979
7980 ring->queue_index = vsi->alloc_queue_pairs + i;
7981 ring->reg_idx = vsi->base_queue + ring->queue_index;
7982 ring->ring_active = false;
7983 ring->vsi = vsi;
7984 ring->netdev = NULL;
7985 ring->dev = &pf->pdev->dev;
7986 ring->count = vsi->num_desc;
7987 ring->size = 0;
7988 ring->dcb_tc = 0;
7989 if (vsi->back->flags & I40E_FLAG_WB_ON_ITR_CAPABLE)
7990 ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
7991 set_ring_xdp(ring);
7992 ring->tx_itr_setting = pf->tx_itr_default;
7993 vsi->xdp_rings[i] = ring++;
7994
7995setup_rx:
7996 ring->queue_index = i;
7997 ring->reg_idx = vsi->base_queue + i;
7998 ring->ring_active = false;
7999 ring->vsi = vsi;
8000 ring->netdev = vsi->netdev;
8001 ring->dev = &pf->pdev->dev;
8002 ring->count = vsi->num_desc;
8003 ring->size = 0;
8004 ring->dcb_tc = 0;
8005 ring->rx_itr_setting = pf->rx_itr_default;
8006 vsi->rx_rings[i] = ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008007 }
8008
8009 return 0;
Alexander Duyck9f65e152013-09-28 06:00:58 +00008010
8011err_out:
8012 i40e_vsi_clear_rings(vsi);
8013 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008014}
8015
8016/**
8017 * i40e_reserve_msix_vectors - Reserve MSI-X vectors in the kernel
8018 * @pf: board private structure
8019 * @vectors: the number of MSI-X vectors to request
8020 *
8021 * Returns the number of vectors reserved, or error
8022 **/
8023static int i40e_reserve_msix_vectors(struct i40e_pf *pf, int vectors)
8024{
Alexander Gordeev7b37f372014-02-18 11:11:42 +01008025 vectors = pci_enable_msix_range(pf->pdev, pf->msix_entries,
8026 I40E_MIN_MSIX, vectors);
8027 if (vectors < 0) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008028 dev_info(&pf->pdev->dev,
Alexander Gordeev7b37f372014-02-18 11:11:42 +01008029 "MSI-X vector reservation failed: %d\n", vectors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008030 vectors = 0;
8031 }
8032
8033 return vectors;
8034}
8035
8036/**
8037 * i40e_init_msix - Setup the MSIX capability
8038 * @pf: board private structure
8039 *
8040 * Work with the OS to set up the MSIX vectors needed.
8041 *
Shannon Nelson3b444392015-02-26 16:15:57 +00008042 * Returns the number of vectors reserved or negative on failure
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008043 **/
8044static int i40e_init_msix(struct i40e_pf *pf)
8045{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008046 struct i40e_hw *hw = &pf->hw;
Jacob Kellerc0cf70a2017-01-24 10:24:01 -08008047 int cpus, extra_vectors;
Shannon Nelson1e200e42015-02-27 09:15:24 +00008048 int vectors_left;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008049 int v_budget, i;
Shannon Nelson3b444392015-02-26 16:15:57 +00008050 int v_actual;
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06008051 int iwarp_requested = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008052
8053 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
8054 return -ENODEV;
8055
8056 /* The number of vectors we'll request will be comprised of:
8057 * - Add 1 for "other" cause for Admin Queue events, etc.
8058 * - The number of LAN queue pairs
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008059 * - Queues being used for RSS.
8060 * We don't need as many as max_rss_size vectors.
8061 * use rss_size instead in the calculation since that
8062 * is governed by number of cpus in the system.
8063 * - assumes symmetric Tx/Rx pairing
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008064 * - The number of VMDq pairs
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06008065 * - The CPU count within the NUMA node if iWARP is enabled
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008066 * Once we count this up, try the request.
8067 *
8068 * If we can't get what we want, we'll simplify to nearly nothing
8069 * and try again. If that still fails, we punt.
8070 */
Shannon Nelson1e200e42015-02-27 09:15:24 +00008071 vectors_left = hw->func_caps.num_msix_vectors;
8072 v_budget = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008073
Shannon Nelson1e200e42015-02-27 09:15:24 +00008074 /* reserve one vector for miscellaneous handler */
8075 if (vectors_left) {
8076 v_budget++;
8077 vectors_left--;
8078 }
8079
Jacob Kellerc0cf70a2017-01-24 10:24:01 -08008080 /* reserve some vectors for the main PF traffic queues. Initially we
8081 * only reserve at most 50% of the available vectors, in the case that
8082 * the number of online CPUs is large. This ensures that we can enable
8083 * extra features as well. Once we've enabled the other features, we
8084 * will use any remaining vectors to reach as close as we can to the
8085 * number of online CPUs.
8086 */
8087 cpus = num_online_cpus();
8088 pf->num_lan_msix = min_t(int, cpus, vectors_left / 2);
Shannon Nelson1e200e42015-02-27 09:15:24 +00008089 vectors_left -= pf->num_lan_msix;
Shannon Nelson1e200e42015-02-27 09:15:24 +00008090
8091 /* reserve one vector for sideband flow director */
8092 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
8093 if (vectors_left) {
Tushar Davea70e4072016-05-16 12:40:53 -07008094 pf->num_fdsb_msix = 1;
Shannon Nelson1e200e42015-02-27 09:15:24 +00008095 v_budget++;
8096 vectors_left--;
8097 } else {
Tushar Davea70e4072016-05-16 12:40:53 -07008098 pf->num_fdsb_msix = 0;
Shannon Nelson1e200e42015-02-27 09:15:24 +00008099 }
8100 }
John W Linville83840e42015-01-14 03:06:28 +00008101
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06008102 /* can we reserve enough for iWARP? */
8103 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02008104 iwarp_requested = pf->num_iwarp_msix;
8105
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06008106 if (!vectors_left)
8107 pf->num_iwarp_msix = 0;
8108 else if (vectors_left < pf->num_iwarp_msix)
8109 pf->num_iwarp_msix = 1;
8110 v_budget += pf->num_iwarp_msix;
8111 vectors_left -= pf->num_iwarp_msix;
8112 }
8113
Shannon Nelson1e200e42015-02-27 09:15:24 +00008114 /* any vectors left over go for VMDq support */
8115 if (pf->flags & I40E_FLAG_VMDQ_ENABLED) {
8116 int vmdq_vecs_wanted = pf->num_vmdq_vsis * pf->num_vmdq_qps;
8117 int vmdq_vecs = min_t(int, vectors_left, vmdq_vecs_wanted);
8118
Stefan Assmann9ca57e92016-09-19 13:37:49 +02008119 if (!vectors_left) {
8120 pf->num_vmdq_msix = 0;
8121 pf->num_vmdq_qps = 0;
8122 } else {
8123 /* if we're short on vectors for what's desired, we limit
8124 * the queues per vmdq. If this is still more than are
8125 * available, the user will need to change the number of
8126 * queues/vectors used by the PF later with the ethtool
8127 * channels command
8128 */
8129 if (vmdq_vecs < vmdq_vecs_wanted)
8130 pf->num_vmdq_qps = 1;
8131 pf->num_vmdq_msix = pf->num_vmdq_qps;
Shannon Nelson1e200e42015-02-27 09:15:24 +00008132
Stefan Assmann9ca57e92016-09-19 13:37:49 +02008133 v_budget += vmdq_vecs;
8134 vectors_left -= vmdq_vecs;
8135 }
Shannon Nelson1e200e42015-02-27 09:15:24 +00008136 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008137
Jacob Kellerc0cf70a2017-01-24 10:24:01 -08008138 /* On systems with a large number of SMP cores, we previously limited
8139 * the number of vectors for num_lan_msix to be at most 50% of the
8140 * available vectors, to allow for other features. Now, we add back
8141 * the remaining vectors. However, we ensure that the total
8142 * num_lan_msix will not exceed num_online_cpus(). To do this, we
8143 * calculate the number of vectors we can add without going over the
8144 * cap of CPUs. For systems with a small number of CPUs this will be
8145 * zero.
8146 */
8147 extra_vectors = min_t(int, cpus - pf->num_lan_msix, vectors_left);
8148 pf->num_lan_msix += extra_vectors;
8149 vectors_left -= extra_vectors;
8150
8151 WARN(vectors_left < 0,
8152 "Calculation of remaining vectors underflowed. This is an accounting bug when determining total MSI-X vectors.\n");
8153
8154 v_budget += pf->num_lan_msix;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008155 pf->msix_entries = kcalloc(v_budget, sizeof(struct msix_entry),
8156 GFP_KERNEL);
8157 if (!pf->msix_entries)
8158 return -ENOMEM;
8159
8160 for (i = 0; i < v_budget; i++)
8161 pf->msix_entries[i].entry = i;
Shannon Nelson3b444392015-02-26 16:15:57 +00008162 v_actual = i40e_reserve_msix_vectors(pf, v_budget);
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00008163
Shannon Nelson3b444392015-02-26 16:15:57 +00008164 if (v_actual < I40E_MIN_MSIX) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008165 pf->flags &= ~I40E_FLAG_MSIX_ENABLED;
8166 kfree(pf->msix_entries);
8167 pf->msix_entries = NULL;
Guilherme G Piccoli4c95aa52016-09-22 10:03:58 -03008168 pci_disable_msix(pf->pdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008169 return -ENODEV;
8170
Shannon Nelson3b444392015-02-26 16:15:57 +00008171 } else if (v_actual == I40E_MIN_MSIX) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008172 /* Adjust for minimal MSIX use */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008173 pf->num_vmdq_vsis = 0;
8174 pf->num_vmdq_qps = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008175 pf->num_lan_qps = 1;
8176 pf->num_lan_msix = 1;
8177
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02008178 } else if (!vectors_left) {
8179 /* If we have limited resources, we will start with no vectors
8180 * for the special features and then allocate vectors to some
8181 * of these features based on the policy and at the end disable
8182 * the features that did not get any vectors.
8183 */
Shannon Nelson3b444392015-02-26 16:15:57 +00008184 int vec;
8185
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02008186 dev_info(&pf->pdev->dev,
8187 "MSI-X vector limit reached, attempting to redistribute vectors\n");
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00008188 /* reserve the misc vector */
Shannon Nelson3b444392015-02-26 16:15:57 +00008189 vec = v_actual - 1;
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00008190
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008191 /* Scale vector usage down */
8192 pf->num_vmdq_msix = 1; /* force VMDqs to only one vector */
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00008193 pf->num_vmdq_vsis = 1;
Shannon Nelson1e200e42015-02-27 09:15:24 +00008194 pf->num_vmdq_qps = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008195
8196 /* partition out the remaining vectors */
8197 switch (vec) {
8198 case 2:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008199 pf->num_lan_msix = 1;
8200 break;
8201 case 3:
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06008202 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
8203 pf->num_lan_msix = 1;
8204 pf->num_iwarp_msix = 1;
8205 } else {
8206 pf->num_lan_msix = 2;
8207 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008208 break;
8209 default:
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06008210 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
8211 pf->num_iwarp_msix = min_t(int, (vec / 3),
8212 iwarp_requested);
8213 pf->num_vmdq_vsis = min_t(int, (vec / 3),
8214 I40E_DEFAULT_NUM_VMDQ_VSI);
8215 } else {
8216 pf->num_vmdq_vsis = min_t(int, (vec / 2),
8217 I40E_DEFAULT_NUM_VMDQ_VSI);
8218 }
Stefan Assmannabd97a92016-09-19 13:37:51 +02008219 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
8220 pf->num_fdsb_msix = 1;
8221 vec--;
8222 }
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06008223 pf->num_lan_msix = min_t(int,
8224 (vec - (pf->num_iwarp_msix + pf->num_vmdq_vsis)),
8225 pf->num_lan_msix);
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02008226 pf->num_lan_qps = pf->num_lan_msix;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008227 break;
8228 }
8229 }
8230
Stefan Assmannabd97a92016-09-19 13:37:51 +02008231 if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
8232 (pf->num_fdsb_msix == 0)) {
8233 dev_info(&pf->pdev->dev, "Sideband Flowdir disabled, not enough MSI-X vectors\n");
8234 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
8235 }
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00008236 if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
8237 (pf->num_vmdq_msix == 0)) {
8238 dev_info(&pf->pdev->dev, "VMDq disabled, not enough MSI-X vectors\n");
8239 pf->flags &= ~I40E_FLAG_VMDQ_ENABLED;
8240 }
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06008241
8242 if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
8243 (pf->num_iwarp_msix == 0)) {
8244 dev_info(&pf->pdev->dev, "IWARP disabled, not enough MSI-X vectors\n");
8245 pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
8246 }
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02008247 i40e_debug(&pf->hw, I40E_DEBUG_INIT,
8248 "MSI-X vector distribution: PF %d, VMDq %d, FDSB %d, iWARP %d\n",
8249 pf->num_lan_msix,
8250 pf->num_vmdq_msix * pf->num_vmdq_vsis,
8251 pf->num_fdsb_msix,
8252 pf->num_iwarp_msix);
8253
Shannon Nelson3b444392015-02-26 16:15:57 +00008254 return v_actual;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008255}
8256
8257/**
Greg Rose90e04072014-03-06 08:59:57 +00008258 * i40e_vsi_alloc_q_vector - Allocate memory for a single interrupt vector
Alexander Duyck493fb302013-09-28 07:01:44 +00008259 * @vsi: the VSI being configured
8260 * @v_idx: index of the vector in the vsi struct
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03008261 * @cpu: cpu to be used on affinity_mask
Alexander Duyck493fb302013-09-28 07:01:44 +00008262 *
8263 * We allocate one q_vector. If allocation fails we return -ENOMEM.
8264 **/
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03008265static int i40e_vsi_alloc_q_vector(struct i40e_vsi *vsi, int v_idx, int cpu)
Alexander Duyck493fb302013-09-28 07:01:44 +00008266{
8267 struct i40e_q_vector *q_vector;
8268
8269 /* allocate q_vector */
8270 q_vector = kzalloc(sizeof(struct i40e_q_vector), GFP_KERNEL);
8271 if (!q_vector)
8272 return -ENOMEM;
8273
8274 q_vector->vsi = vsi;
8275 q_vector->v_idx = v_idx;
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03008276 cpumask_set_cpu(cpu, &q_vector->affinity_mask);
8277
Alexander Duyck493fb302013-09-28 07:01:44 +00008278 if (vsi->netdev)
8279 netif_napi_add(vsi->netdev, &q_vector->napi,
Jesse Brandeburgeefeace2014-05-10 04:49:13 +00008280 i40e_napi_poll, NAPI_POLL_WEIGHT);
Alexander Duyck493fb302013-09-28 07:01:44 +00008281
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00008282 q_vector->rx.latency_range = I40E_LOW_LATENCY;
8283 q_vector->tx.latency_range = I40E_LOW_LATENCY;
8284
Alexander Duyck493fb302013-09-28 07:01:44 +00008285 /* tie q_vector and vsi together */
8286 vsi->q_vectors[v_idx] = q_vector;
8287
8288 return 0;
8289}
8290
8291/**
Greg Rose90e04072014-03-06 08:59:57 +00008292 * i40e_vsi_alloc_q_vectors - Allocate memory for interrupt vectors
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008293 * @vsi: the VSI being configured
8294 *
8295 * We allocate one q_vector per queue interrupt. If allocation fails we
8296 * return -ENOMEM.
8297 **/
Greg Rose90e04072014-03-06 08:59:57 +00008298static int i40e_vsi_alloc_q_vectors(struct i40e_vsi *vsi)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008299{
8300 struct i40e_pf *pf = vsi->back;
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03008301 int err, v_idx, num_q_vectors, current_cpu;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008302
8303 /* if not MSIX, give the one vector only to the LAN VSI */
8304 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
8305 num_q_vectors = vsi->num_q_vectors;
8306 else if (vsi == pf->vsi[pf->lan_vsi])
8307 num_q_vectors = 1;
8308 else
8309 return -EINVAL;
8310
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03008311 current_cpu = cpumask_first(cpu_online_mask);
8312
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008313 for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03008314 err = i40e_vsi_alloc_q_vector(vsi, v_idx, current_cpu);
Alexander Duyck493fb302013-09-28 07:01:44 +00008315 if (err)
8316 goto err_out;
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03008317 current_cpu = cpumask_next(current_cpu, cpu_online_mask);
8318 if (unlikely(current_cpu >= nr_cpu_ids))
8319 current_cpu = cpumask_first(cpu_online_mask);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008320 }
8321
8322 return 0;
Alexander Duyck493fb302013-09-28 07:01:44 +00008323
8324err_out:
8325 while (v_idx--)
8326 i40e_free_q_vector(vsi, v_idx);
8327
8328 return err;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008329}
8330
8331/**
8332 * i40e_init_interrupt_scheme - Determine proper interrupt scheme
8333 * @pf: board private structure to initialize
8334 **/
Jesse Brandeburgc11472802015-04-07 19:45:39 -04008335static int i40e_init_interrupt_scheme(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008336{
Shannon Nelson3b444392015-02-26 16:15:57 +00008337 int vectors = 0;
8338 ssize_t size;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008339
8340 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
Shannon Nelson3b444392015-02-26 16:15:57 +00008341 vectors = i40e_init_msix(pf);
8342 if (vectors < 0) {
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08008343 pf->flags &= ~(I40E_FLAG_MSIX_ENABLED |
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06008344 I40E_FLAG_IWARP_ENABLED |
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08008345 I40E_FLAG_RSS_ENABLED |
Neerav Parikh4d9b6042014-05-22 06:31:51 +00008346 I40E_FLAG_DCB_CAPABLE |
Dave Ertmana0362442016-08-29 17:38:26 -07008347 I40E_FLAG_DCB_ENABLED |
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08008348 I40E_FLAG_SRIOV_ENABLED |
8349 I40E_FLAG_FD_SB_ENABLED |
8350 I40E_FLAG_FD_ATR_ENABLED |
8351 I40E_FLAG_VMDQ_ENABLED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008352
8353 /* rework the queue expectations without MSIX */
8354 i40e_determine_queue_usage(pf);
8355 }
8356 }
8357
8358 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED) &&
8359 (pf->flags & I40E_FLAG_MSI_ENABLED)) {
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08008360 dev_info(&pf->pdev->dev, "MSI-X not available, trying MSI\n");
Shannon Nelson3b444392015-02-26 16:15:57 +00008361 vectors = pci_enable_msi(pf->pdev);
8362 if (vectors < 0) {
8363 dev_info(&pf->pdev->dev, "MSI init failed - %d\n",
8364 vectors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008365 pf->flags &= ~I40E_FLAG_MSI_ENABLED;
8366 }
Shannon Nelson3b444392015-02-26 16:15:57 +00008367 vectors = 1; /* one MSI or Legacy vector */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008368 }
8369
Shannon Nelson958a3e32013-09-28 07:13:28 +00008370 if (!(pf->flags & (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED)))
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08008371 dev_info(&pf->pdev->dev, "MSI-X and MSI not available, falling back to Legacy IRQ\n");
Shannon Nelson958a3e32013-09-28 07:13:28 +00008372
Shannon Nelson3b444392015-02-26 16:15:57 +00008373 /* set up vector assignment tracking */
8374 size = sizeof(struct i40e_lump_tracking) + (sizeof(u16) * vectors);
8375 pf->irq_pile = kzalloc(size, GFP_KERNEL);
Jesse Brandeburgc11472802015-04-07 19:45:39 -04008376 if (!pf->irq_pile) {
8377 dev_err(&pf->pdev->dev, "error allocating irq_pile memory\n");
8378 return -ENOMEM;
8379 }
Shannon Nelson3b444392015-02-26 16:15:57 +00008380 pf->irq_pile->num_entries = vectors;
8381 pf->irq_pile->search_hint = 0;
8382
Jesse Brandeburgc11472802015-04-07 19:45:39 -04008383 /* track first vector for misc interrupts, ignore return */
Shannon Nelson3b444392015-02-26 16:15:57 +00008384 (void)i40e_get_lump(pf, pf->irq_pile, 1, I40E_PILE_VALID_BIT - 1);
Jesse Brandeburgc11472802015-04-07 19:45:39 -04008385
8386 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008387}
8388
8389/**
8390 * i40e_setup_misc_vector - Setup the misc vector to handle non queue events
8391 * @pf: board private structure
8392 *
8393 * This sets up the handler for MSIX 0, which is used to manage the
8394 * non-queue interrupts, e.g. AdminQ and errors. This is not used
8395 * when in MSI or Legacy interrupt mode.
8396 **/
8397static int i40e_setup_misc_vector(struct i40e_pf *pf)
8398{
8399 struct i40e_hw *hw = &pf->hw;
8400 int err = 0;
8401
8402 /* Only request the irq if this is the first time through, and
8403 * not when we're rebuilding after a Reset
8404 */
Jacob Keller0da36b92017-04-19 09:25:55 -04008405 if (!test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008406 err = request_irq(pf->msix_entries[0].vector,
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00008407 i40e_intr, 0, pf->int_name, pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008408 if (err) {
8409 dev_info(&pf->pdev->dev,
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08008410 "request_irq for %s failed: %d\n",
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00008411 pf->int_name, err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008412 return -EFAULT;
8413 }
8414 }
8415
Jacob Kellerab437b52014-12-14 01:55:08 +00008416 i40e_enable_misc_int_causes(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008417
8418 /* associate no queues to the misc vector */
8419 wr32(hw, I40E_PFINT_LNKLST0, I40E_QUEUE_END_OF_LIST);
8420 wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), I40E_ITR_8K);
8421
8422 i40e_flush(hw);
8423
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08008424 i40e_irq_dynamic_enable_icr0(pf, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008425
8426 return err;
8427}
8428
8429/**
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008430 * i40e_config_rss_aq - Prepare for RSS using AQ commands
8431 * @vsi: vsi structure
8432 * @seed: RSS hash seed
8433 **/
Helin Zhange69ff812015-10-21 19:56:22 -04008434static int i40e_config_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
8435 u8 *lut, u16 lut_size)
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008436{
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008437 struct i40e_pf *pf = vsi->back;
8438 struct i40e_hw *hw = &pf->hw;
Jacob Keller776b2e12016-07-19 16:23:30 -07008439 int ret = 0;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008440
Jacob Keller776b2e12016-07-19 16:23:30 -07008441 if (seed) {
8442 struct i40e_aqc_get_set_rss_key_data *seed_dw =
8443 (struct i40e_aqc_get_set_rss_key_data *)seed;
8444 ret = i40e_aq_set_rss_key(hw, vsi->id, seed_dw);
8445 if (ret) {
8446 dev_info(&pf->pdev->dev,
8447 "Cannot set RSS key, err %s aq_err %s\n",
8448 i40e_stat_str(hw, ret),
8449 i40e_aq_str(hw, hw->aq.asq_last_status));
8450 return ret;
8451 }
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008452 }
Jacob Keller776b2e12016-07-19 16:23:30 -07008453 if (lut) {
8454 bool pf_lut = vsi->type == I40E_VSI_MAIN ? true : false;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008455
Jacob Keller776b2e12016-07-19 16:23:30 -07008456 ret = i40e_aq_set_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
8457 if (ret) {
8458 dev_info(&pf->pdev->dev,
8459 "Cannot set RSS lut, err %s aq_err %s\n",
8460 i40e_stat_str(hw, ret),
8461 i40e_aq_str(hw, hw->aq.asq_last_status));
8462 return ret;
8463 }
8464 }
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008465 return ret;
8466}
8467
8468/**
Anjali Singhai Jain95a73782015-12-22 14:25:04 -08008469 * i40e_get_rss_aq - Get RSS keys and lut by using AQ commands
8470 * @vsi: Pointer to vsi structure
8471 * @seed: Buffter to store the hash keys
8472 * @lut: Buffer to store the lookup table entries
8473 * @lut_size: Size of buffer to store the lookup table entries
8474 *
8475 * Return 0 on success, negative on failure
8476 */
8477static int i40e_get_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
8478 u8 *lut, u16 lut_size)
8479{
8480 struct i40e_pf *pf = vsi->back;
8481 struct i40e_hw *hw = &pf->hw;
8482 int ret = 0;
8483
8484 if (seed) {
8485 ret = i40e_aq_get_rss_key(hw, vsi->id,
8486 (struct i40e_aqc_get_set_rss_key_data *)seed);
8487 if (ret) {
8488 dev_info(&pf->pdev->dev,
8489 "Cannot get RSS key, err %s aq_err %s\n",
8490 i40e_stat_str(&pf->hw, ret),
8491 i40e_aq_str(&pf->hw,
8492 pf->hw.aq.asq_last_status));
8493 return ret;
8494 }
8495 }
8496
8497 if (lut) {
8498 bool pf_lut = vsi->type == I40E_VSI_MAIN ? true : false;
8499
8500 ret = i40e_aq_get_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
8501 if (ret) {
8502 dev_info(&pf->pdev->dev,
8503 "Cannot get RSS lut, err %s aq_err %s\n",
8504 i40e_stat_str(&pf->hw, ret),
8505 i40e_aq_str(&pf->hw,
8506 pf->hw.aq.asq_last_status));
8507 return ret;
8508 }
8509 }
8510
8511 return ret;
8512}
8513
8514/**
Jacob Keller0582b962016-07-19 16:23:29 -07008515 * i40e_vsi_config_rss - Prepare for VSI(VMDq) RSS if used
8516 * @vsi: VSI structure
8517 **/
8518static int i40e_vsi_config_rss(struct i40e_vsi *vsi)
8519{
8520 u8 seed[I40E_HKEY_ARRAY_SIZE];
8521 struct i40e_pf *pf = vsi->back;
8522 u8 *lut;
8523 int ret;
8524
8525 if (!(pf->flags & I40E_FLAG_RSS_AQ_CAPABLE))
8526 return 0;
8527
Jacob Keller552b9962016-07-19 16:23:31 -07008528 if (!vsi->rss_size)
8529 vsi->rss_size = min_t(int, pf->alloc_rss_size,
8530 vsi->num_queue_pairs);
8531 if (!vsi->rss_size)
8532 return -EINVAL;
8533
Jacob Keller0582b962016-07-19 16:23:29 -07008534 lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
8535 if (!lut)
8536 return -ENOMEM;
Jacob Keller552b9962016-07-19 16:23:31 -07008537 /* Use the user configured hash keys and lookup table if there is one,
8538 * otherwise use default
8539 */
8540 if (vsi->rss_lut_user)
8541 memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
8542 else
8543 i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
8544 if (vsi->rss_hkey_user)
8545 memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
8546 else
8547 netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
Jacob Keller0582b962016-07-19 16:23:29 -07008548 ret = i40e_config_rss_aq(vsi, seed, lut, vsi->rss_table_size);
8549 kfree(lut);
8550
8551 return ret;
8552}
8553
8554/**
Helin Zhang043dd652015-10-21 19:56:23 -04008555 * i40e_config_rss_reg - Configure RSS keys and lut by writing registers
Helin Zhange69ff812015-10-21 19:56:22 -04008556 * @vsi: Pointer to vsi structure
8557 * @seed: RSS hash seed
8558 * @lut: Lookup table
8559 * @lut_size: Lookup table size
8560 *
8561 * Returns 0 on success, negative on failure
8562 **/
8563static int i40e_config_rss_reg(struct i40e_vsi *vsi, const u8 *seed,
8564 const u8 *lut, u16 lut_size)
8565{
8566 struct i40e_pf *pf = vsi->back;
8567 struct i40e_hw *hw = &pf->hw;
Mitch Williamsc4e18682016-04-12 08:30:40 -07008568 u16 vf_id = vsi->vf_id;
Helin Zhange69ff812015-10-21 19:56:22 -04008569 u8 i;
8570
8571 /* Fill out hash function seed */
8572 if (seed) {
8573 u32 *seed_dw = (u32 *)seed;
8574
Mitch Williamsc4e18682016-04-12 08:30:40 -07008575 if (vsi->type == I40E_VSI_MAIN) {
8576 for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
Lihong Yang26f77e52017-01-30 12:29:33 -08008577 wr32(hw, I40E_PFQF_HKEY(i), seed_dw[i]);
Mitch Williamsc4e18682016-04-12 08:30:40 -07008578 } else if (vsi->type == I40E_VSI_SRIOV) {
8579 for (i = 0; i <= I40E_VFQF_HKEY1_MAX_INDEX; i++)
Lihong Yang26f77e52017-01-30 12:29:33 -08008580 wr32(hw, I40E_VFQF_HKEY1(i, vf_id), seed_dw[i]);
Mitch Williamsc4e18682016-04-12 08:30:40 -07008581 } else {
8582 dev_err(&pf->pdev->dev, "Cannot set RSS seed - invalid VSI type\n");
8583 }
Helin Zhange69ff812015-10-21 19:56:22 -04008584 }
8585
8586 if (lut) {
8587 u32 *lut_dw = (u32 *)lut;
8588
Mitch Williamsc4e18682016-04-12 08:30:40 -07008589 if (vsi->type == I40E_VSI_MAIN) {
8590 if (lut_size != I40E_HLUT_ARRAY_SIZE)
8591 return -EINVAL;
8592 for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
8593 wr32(hw, I40E_PFQF_HLUT(i), lut_dw[i]);
8594 } else if (vsi->type == I40E_VSI_SRIOV) {
8595 if (lut_size != I40E_VF_HLUT_ARRAY_SIZE)
8596 return -EINVAL;
8597 for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
Lihong Yang26f77e52017-01-30 12:29:33 -08008598 wr32(hw, I40E_VFQF_HLUT1(i, vf_id), lut_dw[i]);
Mitch Williamsc4e18682016-04-12 08:30:40 -07008599 } else {
8600 dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
8601 }
Helin Zhange69ff812015-10-21 19:56:22 -04008602 }
8603 i40e_flush(hw);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008604
8605 return 0;
8606}
8607
8608/**
Helin Zhang043dd652015-10-21 19:56:23 -04008609 * i40e_get_rss_reg - Get the RSS keys and lut by reading registers
8610 * @vsi: Pointer to VSI structure
8611 * @seed: Buffer to store the keys
8612 * @lut: Buffer to store the lookup table entries
8613 * @lut_size: Size of buffer to store the lookup table entries
8614 *
8615 * Returns 0 on success, negative on failure
8616 */
8617static int i40e_get_rss_reg(struct i40e_vsi *vsi, u8 *seed,
8618 u8 *lut, u16 lut_size)
8619{
8620 struct i40e_pf *pf = vsi->back;
8621 struct i40e_hw *hw = &pf->hw;
8622 u16 i;
8623
8624 if (seed) {
8625 u32 *seed_dw = (u32 *)seed;
8626
8627 for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
Shannon Nelson272cdaf22016-02-17 16:12:21 -08008628 seed_dw[i] = i40e_read_rx_ctl(hw, I40E_PFQF_HKEY(i));
Helin Zhang043dd652015-10-21 19:56:23 -04008629 }
8630 if (lut) {
8631 u32 *lut_dw = (u32 *)lut;
8632
8633 if (lut_size != I40E_HLUT_ARRAY_SIZE)
8634 return -EINVAL;
8635 for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
8636 lut_dw[i] = rd32(hw, I40E_PFQF_HLUT(i));
8637 }
8638
8639 return 0;
8640}
8641
8642/**
8643 * i40e_config_rss - Configure RSS keys and lut
8644 * @vsi: Pointer to VSI structure
8645 * @seed: RSS hash seed
8646 * @lut: Lookup table
8647 * @lut_size: Lookup table size
8648 *
8649 * Returns 0 on success, negative on failure
8650 */
8651int i40e_config_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
8652{
8653 struct i40e_pf *pf = vsi->back;
8654
8655 if (pf->flags & I40E_FLAG_RSS_AQ_CAPABLE)
8656 return i40e_config_rss_aq(vsi, seed, lut, lut_size);
8657 else
8658 return i40e_config_rss_reg(vsi, seed, lut, lut_size);
8659}
8660
8661/**
8662 * i40e_get_rss - Get RSS keys and lut
8663 * @vsi: Pointer to VSI structure
8664 * @seed: Buffer to store the keys
8665 * @lut: Buffer to store the lookup table entries
8666 * lut_size: Size of buffer to store the lookup table entries
8667 *
8668 * Returns 0 on success, negative on failure
8669 */
8670int i40e_get_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
8671{
Anjali Singhai Jain95a73782015-12-22 14:25:04 -08008672 struct i40e_pf *pf = vsi->back;
8673
8674 if (pf->flags & I40E_FLAG_RSS_AQ_CAPABLE)
8675 return i40e_get_rss_aq(vsi, seed, lut, lut_size);
8676 else
8677 return i40e_get_rss_reg(vsi, seed, lut, lut_size);
Helin Zhang043dd652015-10-21 19:56:23 -04008678}
8679
8680/**
Helin Zhange69ff812015-10-21 19:56:22 -04008681 * i40e_fill_rss_lut - Fill the RSS lookup table with default values
8682 * @pf: Pointer to board private structure
8683 * @lut: Lookup table
8684 * @rss_table_size: Lookup table size
8685 * @rss_size: Range of queue number for hashing
8686 */
Alan Bradyf1582352016-08-24 11:33:46 -07008687void i40e_fill_rss_lut(struct i40e_pf *pf, u8 *lut,
8688 u16 rss_table_size, u16 rss_size)
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008689{
Helin Zhange69ff812015-10-21 19:56:22 -04008690 u16 i;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008691
Helin Zhange69ff812015-10-21 19:56:22 -04008692 for (i = 0; i < rss_table_size; i++)
8693 lut[i] = i % rss_size;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008694}
8695
8696/**
Helin Zhang043dd652015-10-21 19:56:23 -04008697 * i40e_pf_config_rss - Prepare for RSS if used
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008698 * @pf: board private structure
8699 **/
Helin Zhang043dd652015-10-21 19:56:23 -04008700static int i40e_pf_config_rss(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008701{
Anjali Singhai Jain66ddcff2015-02-24 06:58:50 +00008702 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008703 u8 seed[I40E_HKEY_ARRAY_SIZE];
Helin Zhange69ff812015-10-21 19:56:22 -04008704 u8 *lut;
Anjali Singhai Jain4617e8c2013-11-20 10:02:56 +00008705 struct i40e_hw *hw = &pf->hw;
Carolyn Wybornye157ea32014-06-03 23:50:22 +00008706 u32 reg_val;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008707 u64 hena;
Helin Zhange69ff812015-10-21 19:56:22 -04008708 int ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008709
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008710 /* By default we enable TCP/UDP with IPv4/IPv6 ptypes */
Shannon Nelson272cdaf22016-02-17 16:12:21 -08008711 hena = (u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(0)) |
8712 ((u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(1)) << 32);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008713 hena |= i40e_pf_get_default_rss_hena(pf);
8714
Shannon Nelson272cdaf22016-02-17 16:12:21 -08008715 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), (u32)hena);
8716 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008717
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008718 /* Determine the RSS table size based on the hardware capabilities */
Shannon Nelson272cdaf22016-02-17 16:12:21 -08008719 reg_val = i40e_read_rx_ctl(hw, I40E_PFQF_CTL_0);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008720 reg_val = (pf->rss_table_size == 512) ?
8721 (reg_val | I40E_PFQF_CTL_0_HASHLUTSIZE_512) :
8722 (reg_val & ~I40E_PFQF_CTL_0_HASHLUTSIZE_512);
Shannon Nelson272cdaf22016-02-17 16:12:21 -08008723 i40e_write_rx_ctl(hw, I40E_PFQF_CTL_0, reg_val);
Carolyn Wybornye157ea32014-06-03 23:50:22 +00008724
Helin Zhang28c58692015-10-26 19:44:27 -04008725 /* Determine the RSS size of the VSI */
Harshitha Ramamurthyf25571b2017-02-21 15:55:40 -08008726 if (!vsi->rss_size) {
8727 u16 qcount;
8728
8729 qcount = vsi->num_queue_pairs / vsi->tc_config.numtc;
8730 vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
8731 }
Mitch Williamsa4fa59c2016-09-12 14:18:43 -07008732 if (!vsi->rss_size)
8733 return -EINVAL;
Helin Zhang28c58692015-10-26 19:44:27 -04008734
Helin Zhange69ff812015-10-21 19:56:22 -04008735 lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
8736 if (!lut)
8737 return -ENOMEM;
8738
Helin Zhang28c58692015-10-26 19:44:27 -04008739 /* Use user configured lut if there is one, otherwise use default */
8740 if (vsi->rss_lut_user)
8741 memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
8742 else
8743 i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
Helin Zhange69ff812015-10-21 19:56:22 -04008744
Helin Zhang28c58692015-10-26 19:44:27 -04008745 /* Use user configured hash key if there is one, otherwise
8746 * use default.
8747 */
8748 if (vsi->rss_hkey_user)
8749 memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
8750 else
8751 netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
Helin Zhang043dd652015-10-21 19:56:23 -04008752 ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
Helin Zhange69ff812015-10-21 19:56:22 -04008753 kfree(lut);
8754
8755 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008756}
8757
8758/**
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008759 * i40e_reconfig_rss_queues - change number of queues for rss and rebuild
8760 * @pf: board private structure
8761 * @queue_count: the requested queue count for rss.
8762 *
8763 * returns 0 if rss is not enabled, if enabled returns the final rss queue
8764 * count which may be different from the requested queue count.
Maciej Sosin373149f2017-04-05 07:50:55 -04008765 * Note: expects to be called while under rtnl_lock()
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008766 **/
8767int i40e_reconfig_rss_queues(struct i40e_pf *pf, int queue_count)
8768{
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00008769 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
8770 int new_rss_size;
8771
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008772 if (!(pf->flags & I40E_FLAG_RSS_ENABLED))
8773 return 0;
8774
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00008775 new_rss_size = min_t(int, queue_count, pf->rss_size_max);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008776
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00008777 if (queue_count != vsi->num_queue_pairs) {
Harshitha Ramamurthyf25571b2017-02-21 15:55:40 -08008778 u16 qcount;
8779
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00008780 vsi->req_queue_pairs = queue_count;
Maciej Sosin373149f2017-04-05 07:50:55 -04008781 i40e_prep_for_reset(pf, true);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008782
Helin Zhangacd65442015-10-26 19:44:28 -04008783 pf->alloc_rss_size = new_rss_size;
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008784
Maciej Sosin373149f2017-04-05 07:50:55 -04008785 i40e_reset_and_rebuild(pf, true, true);
Helin Zhang28c58692015-10-26 19:44:27 -04008786
8787 /* Discard the user configured hash keys and lut, if less
8788 * queues are enabled.
8789 */
8790 if (queue_count < vsi->rss_size) {
8791 i40e_clear_rss_config_user(vsi);
8792 dev_dbg(&pf->pdev->dev,
8793 "discard user configured hash keys and lut\n");
8794 }
8795
8796 /* Reset vsi->rss_size, as number of enabled queues changed */
Harshitha Ramamurthyf25571b2017-02-21 15:55:40 -08008797 qcount = vsi->num_queue_pairs / vsi->tc_config.numtc;
8798 vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
Helin Zhang28c58692015-10-26 19:44:27 -04008799
Helin Zhang043dd652015-10-21 19:56:23 -04008800 i40e_pf_config_rss(pf);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008801 }
Lihong Yang12815052016-09-27 11:28:48 -07008802 dev_info(&pf->pdev->dev, "User requested queue count/HW max RSS count: %d/%d\n",
8803 vsi->req_queue_pairs, pf->rss_size_max);
Helin Zhangacd65442015-10-26 19:44:28 -04008804 return pf->alloc_rss_size;
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008805}
8806
8807/**
Shannon Nelson4fc8c672017-06-07 05:43:08 -04008808 * i40e_get_partition_bw_setting - Retrieve BW settings for this PF partition
Greg Rosef4492db2015-02-06 08:52:12 +00008809 * @pf: board private structure
8810 **/
Shannon Nelson4fc8c672017-06-07 05:43:08 -04008811i40e_status i40e_get_partition_bw_setting(struct i40e_pf *pf)
Greg Rosef4492db2015-02-06 08:52:12 +00008812{
8813 i40e_status status;
8814 bool min_valid, max_valid;
8815 u32 max_bw, min_bw;
8816
8817 status = i40e_read_bw_from_alt_ram(&pf->hw, &max_bw, &min_bw,
8818 &min_valid, &max_valid);
8819
8820 if (!status) {
8821 if (min_valid)
Shannon Nelson4fc8c672017-06-07 05:43:08 -04008822 pf->min_bw = min_bw;
Greg Rosef4492db2015-02-06 08:52:12 +00008823 if (max_valid)
Shannon Nelson4fc8c672017-06-07 05:43:08 -04008824 pf->max_bw = max_bw;
Greg Rosef4492db2015-02-06 08:52:12 +00008825 }
8826
8827 return status;
8828}
8829
8830/**
Shannon Nelson4fc8c672017-06-07 05:43:08 -04008831 * i40e_set_partition_bw_setting - Set BW settings for this PF partition
Greg Rosef4492db2015-02-06 08:52:12 +00008832 * @pf: board private structure
8833 **/
Shannon Nelson4fc8c672017-06-07 05:43:08 -04008834i40e_status i40e_set_partition_bw_setting(struct i40e_pf *pf)
Greg Rosef4492db2015-02-06 08:52:12 +00008835{
8836 struct i40e_aqc_configure_partition_bw_data bw_data;
8837 i40e_status status;
8838
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00008839 /* Set the valid bit for this PF */
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04008840 bw_data.pf_valid_bits = cpu_to_le16(BIT(pf->hw.pf_id));
Shannon Nelson4fc8c672017-06-07 05:43:08 -04008841 bw_data.max_bw[pf->hw.pf_id] = pf->max_bw & I40E_ALT_BW_VALUE_MASK;
8842 bw_data.min_bw[pf->hw.pf_id] = pf->min_bw & I40E_ALT_BW_VALUE_MASK;
Greg Rosef4492db2015-02-06 08:52:12 +00008843
8844 /* Set the new bandwidths */
8845 status = i40e_aq_configure_partition_bw(&pf->hw, &bw_data, NULL);
8846
8847 return status;
8848}
8849
8850/**
Shannon Nelson4fc8c672017-06-07 05:43:08 -04008851 * i40e_commit_partition_bw_setting - Commit BW settings for this PF partition
Greg Rosef4492db2015-02-06 08:52:12 +00008852 * @pf: board private structure
8853 **/
Shannon Nelson4fc8c672017-06-07 05:43:08 -04008854i40e_status i40e_commit_partition_bw_setting(struct i40e_pf *pf)
Greg Rosef4492db2015-02-06 08:52:12 +00008855{
8856 /* Commit temporary BW setting to permanent NVM image */
8857 enum i40e_admin_queue_err last_aq_status;
8858 i40e_status ret;
8859 u16 nvm_word;
8860
8861 if (pf->hw.partition_id != 1) {
8862 dev_info(&pf->pdev->dev,
8863 "Commit BW only works on partition 1! This is partition %d",
8864 pf->hw.partition_id);
8865 ret = I40E_NOT_SUPPORTED;
8866 goto bw_commit_out;
8867 }
8868
8869 /* Acquire NVM for read access */
8870 ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_READ);
8871 last_aq_status = pf->hw.aq.asq_last_status;
8872 if (ret) {
8873 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008874 "Cannot acquire NVM for read access, err %s aq_err %s\n",
8875 i40e_stat_str(&pf->hw, ret),
8876 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +00008877 goto bw_commit_out;
8878 }
8879
8880 /* Read word 0x10 of NVM - SW compatibility word 1 */
8881 ret = i40e_aq_read_nvm(&pf->hw,
8882 I40E_SR_NVM_CONTROL_WORD,
8883 0x10, sizeof(nvm_word), &nvm_word,
8884 false, NULL);
8885 /* Save off last admin queue command status before releasing
8886 * the NVM
8887 */
8888 last_aq_status = pf->hw.aq.asq_last_status;
8889 i40e_release_nvm(&pf->hw);
8890 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008891 dev_info(&pf->pdev->dev, "NVM read error, 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 +00008894 goto bw_commit_out;
8895 }
8896
8897 /* Wait a bit for NVM release to complete */
8898 msleep(50);
8899
8900 /* Acquire NVM for write access */
8901 ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_WRITE);
8902 last_aq_status = pf->hw.aq.asq_last_status;
8903 if (ret) {
8904 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008905 "Cannot acquire NVM for write access, err %s aq_err %s\n",
8906 i40e_stat_str(&pf->hw, ret),
8907 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +00008908 goto bw_commit_out;
8909 }
8910 /* Write it back out unchanged to initiate update NVM,
8911 * which will force a write of the shadow (alt) RAM to
8912 * the NVM - thus storing the bandwidth values permanently.
8913 */
8914 ret = i40e_aq_update_nvm(&pf->hw,
8915 I40E_SR_NVM_CONTROL_WORD,
8916 0x10, sizeof(nvm_word),
8917 &nvm_word, true, NULL);
8918 /* Save off last admin queue command status before releasing
8919 * the NVM
8920 */
8921 last_aq_status = pf->hw.aq.asq_last_status;
8922 i40e_release_nvm(&pf->hw);
8923 if (ret)
8924 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008925 "BW settings NOT SAVED, err %s aq_err %s\n",
8926 i40e_stat_str(&pf->hw, ret),
8927 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +00008928bw_commit_out:
8929
8930 return ret;
8931}
8932
8933/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008934 * i40e_sw_init - Initialize general software structures (struct i40e_pf)
8935 * @pf: board private structure to initialize
8936 *
8937 * i40e_sw_init initializes the Adapter private data structure.
8938 * Fields are initialized based on PCI device information and
8939 * OS network device settings (MTU size).
8940 **/
8941static int i40e_sw_init(struct i40e_pf *pf)
8942{
8943 int err = 0;
8944 int size;
8945
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008946 /* Set default capability flags */
8947 pf->flags = I40E_FLAG_RX_CSUM_ENABLED |
8948 I40E_FLAG_MSI_ENABLED |
Mitch Williams2bc7ee82015-02-06 08:52:11 +00008949 I40E_FLAG_MSIX_ENABLED;
8950
Mitch Williamsca99eb92014-04-04 04:43:07 +00008951 /* Set default ITR */
8952 pf->rx_itr_default = I40E_ITR_DYNAMIC | I40E_ITR_RX_DEF;
8953 pf->tx_itr_default = I40E_ITR_DYNAMIC | I40E_ITR_TX_DEF;
8954
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00008955 /* Depending on PF configurations, it is possible that the RSS
8956 * maximum might end up larger than the available queues
8957 */
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04008958 pf->rss_size_max = BIT(pf->hw.func_caps.rss_table_entry_width);
Helin Zhangacd65442015-10-26 19:44:28 -04008959 pf->alloc_rss_size = 1;
Anjali Singhai Jain5db4cb52015-02-24 06:58:49 +00008960 pf->rss_table_size = pf->hw.func_caps.rss_table_size;
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00008961 pf->rss_size_max = min_t(int, pf->rss_size_max,
8962 pf->hw.func_caps.num_tx_qp);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008963 if (pf->hw.func_caps.rss) {
8964 pf->flags |= I40E_FLAG_RSS_ENABLED;
Helin Zhangacd65442015-10-26 19:44:28 -04008965 pf->alloc_rss_size = min_t(int, pf->rss_size_max,
8966 num_online_cpus());
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008967 }
8968
Catherine Sullivan2050bc62013-12-18 13:46:03 +00008969 /* MFP mode enabled */
Pawel Orlowskic78b9532015-04-22 19:34:06 -04008970 if (pf->hw.func_caps.npar_enable || pf->hw.func_caps.flex10_enable) {
Catherine Sullivan2050bc62013-12-18 13:46:03 +00008971 pf->flags |= I40E_FLAG_MFP_ENABLED;
8972 dev_info(&pf->pdev->dev, "MFP mode Enabled\n");
Shannon Nelson4fc8c672017-06-07 05:43:08 -04008973 if (i40e_get_partition_bw_setting(pf)) {
Greg Rosef4492db2015-02-06 08:52:12 +00008974 dev_warn(&pf->pdev->dev,
Shannon Nelson4fc8c672017-06-07 05:43:08 -04008975 "Could not get partition bw settings\n");
8976 } else {
Greg Rosef4492db2015-02-06 08:52:12 +00008977 dev_info(&pf->pdev->dev,
Shannon Nelson4fc8c672017-06-07 05:43:08 -04008978 "Partition BW Min = %8.8x, Max = %8.8x\n",
8979 pf->min_bw, pf->max_bw);
8980
8981 /* nudge the Tx scheduler */
8982 i40e_set_partition_bw_setting(pf);
8983 }
Catherine Sullivan2050bc62013-12-18 13:46:03 +00008984 }
8985
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008986 if ((pf->hw.func_caps.fd_filters_guaranteed > 0) ||
8987 (pf->hw.func_caps.fd_filters_best_effort > 0)) {
8988 pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
8989 pf->atr_sample_rate = I40E_DEFAULT_ATR_SAMPLE_RATE;
Shannon Nelson6eae9c62015-09-03 17:18:55 -04008990 if (pf->flags & I40E_FLAG_MFP_ENABLED &&
8991 pf->hw.num_partitions > 1)
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008992 dev_info(&pf->pdev->dev,
Anjali Singhai Jain0b675842014-03-06 08:59:51 +00008993 "Flow Director Sideband mode Disabled in MFP mode\n");
Shannon Nelson6eae9c62015-09-03 17:18:55 -04008994 else
8995 pf->flags |= I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008996 pf->fdir_pf_filter_count =
8997 pf->hw.func_caps.fd_filters_guaranteed;
8998 pf->hw.fdir_shared_filter_count =
8999 pf->hw.func_caps.fd_filters_best_effort;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009000 }
9001
Bimmy Pujari6de432c2016-11-11 12:39:38 -08009002 if ((pf->hw.mac.type == I40E_MAC_XL710) &&
Anjali Singhai Jain8eed76f2015-12-09 15:50:31 -08009003 (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 33)) ||
Neerav Parikhf1bbad32016-01-13 16:51:39 -08009004 (pf->hw.aq.fw_maj_ver < 4))) {
Anjali Singhai Jain8eed76f2015-12-09 15:50:31 -08009005 pf->flags |= I40E_FLAG_RESTART_AUTONEG;
Neerav Parikhf1bbad32016-01-13 16:51:39 -08009006 /* No DCB support for FW < v4.33 */
9007 pf->flags |= I40E_FLAG_NO_DCB_SUPPORT;
9008 }
9009
9010 /* Disable FW LLDP if FW < v4.3 */
Bimmy Pujari6de432c2016-11-11 12:39:38 -08009011 if ((pf->hw.mac.type == I40E_MAC_XL710) &&
Neerav Parikhf1bbad32016-01-13 16:51:39 -08009012 (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 3)) ||
9013 (pf->hw.aq.fw_maj_ver < 4)))
9014 pf->flags |= I40E_FLAG_STOP_FW_LLDP;
9015
9016 /* Use the FW Set LLDP MIB API if FW > v4.40 */
Bimmy Pujari6de432c2016-11-11 12:39:38 -08009017 if ((pf->hw.mac.type == I40E_MAC_XL710) &&
Neerav Parikhf1bbad32016-01-13 16:51:39 -08009018 (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver >= 40)) ||
9019 (pf->hw.aq.fw_maj_ver >= 5)))
9020 pf->flags |= I40E_FLAG_USE_SET_LLDP_MIB;
Anjali Singhai Jain8eed76f2015-12-09 15:50:31 -08009021
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009022 if (pf->hw.func_caps.vmdq) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009023 pf->num_vmdq_vsis = I40E_DEFAULT_NUM_VMDQ_VSI;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04009024 pf->flags |= I40E_FLAG_VMDQ_ENABLED;
Jesse Brandeburge9e53662015-10-02 17:57:21 -07009025 pf->num_vmdq_qps = i40e_default_queues_per_vmdq(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009026 }
9027
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06009028 if (pf->hw.func_caps.iwarp) {
9029 pf->flags |= I40E_FLAG_IWARP_ENABLED;
9030 /* IWARP needs one extra vector for CQP just like MISC.*/
9031 pf->num_iwarp_msix = (int)num_online_cpus() + 1;
9032 }
9033
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009034#ifdef CONFIG_PCI_IOV
Shannon Nelsonba252f132014-12-11 07:06:34 +00009035 if (pf->hw.func_caps.num_vfs && pf->hw.partition_id == 1) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009036 pf->num_vf_qps = I40E_DEFAULT_QUEUES_PER_VF;
9037 pf->flags |= I40E_FLAG_SRIOV_ENABLED;
9038 pf->num_req_vfs = min_t(int,
9039 pf->hw.func_caps.num_vfs,
9040 I40E_MAX_VF_COUNT);
9041 }
9042#endif /* CONFIG_PCI_IOV */
Anjali Singhai Jaind502ce02015-06-05 12:20:26 -04009043 if (pf->hw.mac.type == I40E_MAC_X722) {
Joshua Hay1d680052016-12-12 15:44:08 -08009044 pf->flags |= I40E_FLAG_RSS_AQ_CAPABLE
9045 | I40E_FLAG_128_QP_RSS_CAPABLE
9046 | I40E_FLAG_HW_ATR_EVICT_CAPABLE
9047 | I40E_FLAG_OUTER_UDP_CSUM_CAPABLE
9048 | I40E_FLAG_WB_ON_ITR_CAPABLE
9049 | I40E_FLAG_MULTIPLE_TCP_UDP_RSS_PCTYPE
9050 | I40E_FLAG_NO_PCI_LINK_CHECK
9051 | I40E_FLAG_USE_SET_LLDP_MIB
9052 | I40E_FLAG_GENEVE_OFFLOAD_CAPABLE
9053 | I40E_FLAG_PTP_L4_CAPABLE
9054 | I40E_FLAG_WOL_MC_MAGIC_PKT_WAKE;
Anjali Singhaia340c782016-01-06 11:49:28 -08009055 } else if ((pf->hw.aq.api_maj_ver > 1) ||
9056 ((pf->hw.aq.api_maj_ver == 1) &&
9057 (pf->hw.aq.api_min_ver > 4))) {
9058 /* Supported in FW API version higher than 1.4 */
9059 pf->flags |= I40E_FLAG_GENEVE_OFFLOAD_CAPABLE;
Anjali Singhai Jaind502ce02015-06-05 12:20:26 -04009060 }
Anjali Singhaia340c782016-01-06 11:49:28 -08009061
Jacob Keller6964e532017-06-12 15:38:36 -07009062 /* Enable HW ATR eviction if possible */
9063 if (pf->flags & I40E_FLAG_HW_ATR_EVICT_CAPABLE)
9064 pf->flags |= I40E_FLAG_HW_ATR_EVICT_ENABLED;
9065
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009066 pf->eeprom_version = 0xDEAD;
9067 pf->lan_veb = I40E_NO_VEB;
9068 pf->lan_vsi = I40E_NO_VSI;
9069
Anjali Singhai Jaind1a8d272015-07-23 16:54:40 -04009070 /* By default FW has this off for performance reasons */
9071 pf->flags &= ~I40E_FLAG_VEB_STATS_ENABLED;
9072
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009073 /* set up queue assignment tracking */
9074 size = sizeof(struct i40e_lump_tracking)
9075 + (sizeof(u16) * pf->hw.func_caps.num_tx_qp);
9076 pf->qp_pile = kzalloc(size, GFP_KERNEL);
9077 if (!pf->qp_pile) {
9078 err = -ENOMEM;
9079 goto sw_init_done;
9080 }
9081 pf->qp_pile->num_entries = pf->hw.func_caps.num_tx_qp;
9082 pf->qp_pile->search_hint = 0;
9083
Anjali Singhai Jain327fe042014-06-04 01:23:26 +00009084 pf->tx_timeout_recovery_level = 1;
9085
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009086 mutex_init(&pf->switch_mutex);
9087
9088sw_init_done:
9089 return err;
9090}
9091
9092/**
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00009093 * i40e_set_ntuple - set the ntuple feature flag and take action
9094 * @pf: board private structure to initialize
9095 * @features: the feature set that the stack is suggesting
9096 *
9097 * returns a bool to indicate if reset needs to happen
9098 **/
9099bool i40e_set_ntuple(struct i40e_pf *pf, netdev_features_t features)
9100{
9101 bool need_reset = false;
9102
9103 /* Check if Flow Director n-tuple support was enabled or disabled. If
9104 * the state changed, we need to reset.
9105 */
9106 if (features & NETIF_F_NTUPLE) {
9107 /* Enable filters and mark for reset */
9108 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
9109 need_reset = true;
Tushar Davea70e4072016-05-16 12:40:53 -07009110 /* enable FD_SB only if there is MSI-X vector */
9111 if (pf->num_fdsb_msix > 0)
9112 pf->flags |= I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00009113 } else {
9114 /* turn off filters, mark for reset and clear SW filter list */
9115 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
9116 need_reset = true;
9117 i40e_fdir_filter_exit(pf);
9118 }
Jacob Keller47994c12017-04-19 09:25:57 -04009119 pf->flags &= ~(I40E_FLAG_FD_SB_ENABLED |
9120 I40E_FLAG_FD_SB_AUTO_DISABLED);
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00009121 /* reset fd counters */
Jacob Keller097dbf52017-02-06 14:38:46 -08009122 pf->fd_add_err = 0;
9123 pf->fd_atr_cnt = 0;
Anjali Singhai Jain8a4f34f2014-06-04 08:45:20 +00009124 /* if ATR was auto disabled it can be re-enabled. */
Jacob Keller47994c12017-04-19 09:25:57 -04009125 if (pf->flags & I40E_FLAG_FD_ATR_AUTO_DISABLED) {
9126 pf->flags &= ~I40E_FLAG_FD_ATR_AUTO_DISABLED;
9127 if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
9128 (I40E_DEBUG_FD & pf->hw.debug_mask))
Jacob Keller234dc4e2016-09-06 18:05:09 -07009129 dev_info(&pf->pdev->dev, "ATR re-enabled.\n");
9130 }
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00009131 }
9132 return need_reset;
9133}
9134
9135/**
Alan Bradyd8ec9862016-07-27 12:02:38 -07009136 * i40e_clear_rss_lut - clear the rx hash lookup table
9137 * @vsi: the VSI being configured
9138 **/
9139static void i40e_clear_rss_lut(struct i40e_vsi *vsi)
9140{
9141 struct i40e_pf *pf = vsi->back;
9142 struct i40e_hw *hw = &pf->hw;
9143 u16 vf_id = vsi->vf_id;
9144 u8 i;
9145
9146 if (vsi->type == I40E_VSI_MAIN) {
9147 for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
9148 wr32(hw, I40E_PFQF_HLUT(i), 0);
9149 } else if (vsi->type == I40E_VSI_SRIOV) {
9150 for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
9151 i40e_write_rx_ctl(hw, I40E_VFQF_HLUT1(i, vf_id), 0);
9152 } else {
9153 dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
9154 }
9155}
9156
9157/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009158 * i40e_set_features - set the netdev feature flags
9159 * @netdev: ptr to the netdev being adjusted
9160 * @features: the feature set that the stack is suggesting
Maciej Sosin373149f2017-04-05 07:50:55 -04009161 * Note: expects to be called while under rtnl_lock()
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009162 **/
9163static int i40e_set_features(struct net_device *netdev,
9164 netdev_features_t features)
9165{
9166 struct i40e_netdev_priv *np = netdev_priv(netdev);
9167 struct i40e_vsi *vsi = np->vsi;
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00009168 struct i40e_pf *pf = vsi->back;
9169 bool need_reset;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009170
Alan Bradyd8ec9862016-07-27 12:02:38 -07009171 if (features & NETIF_F_RXHASH && !(netdev->features & NETIF_F_RXHASH))
9172 i40e_pf_config_rss(pf);
9173 else if (!(features & NETIF_F_RXHASH) &&
9174 netdev->features & NETIF_F_RXHASH)
9175 i40e_clear_rss_lut(vsi);
9176
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009177 if (features & NETIF_F_HW_VLAN_CTAG_RX)
9178 i40e_vlan_stripping_enable(vsi);
9179 else
9180 i40e_vlan_stripping_disable(vsi);
9181
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00009182 need_reset = i40e_set_ntuple(pf, features);
9183
9184 if (need_reset)
Maciej Sosin373149f2017-04-05 07:50:55 -04009185 i40e_do_reset(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED), true);
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00009186
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009187 return 0;
9188}
9189
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009190/**
Singhai, Anjali6a899022015-12-14 12:21:18 -08009191 * i40e_get_udp_port_idx - Lookup a possibly offloaded for Rx UDP port
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009192 * @pf: board private structure
9193 * @port: The UDP port to look up
9194 *
9195 * Returns the index number or I40E_MAX_PF_UDP_OFFLOAD_PORTS if port not found
9196 **/
Jacob Kellerfe0b0cd2017-02-06 14:38:38 -08009197static u8 i40e_get_udp_port_idx(struct i40e_pf *pf, u16 port)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009198{
9199 u8 i;
9200
9201 for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
Jacob Keller27826fd2017-04-19 09:25:50 -04009202 if (pf->udp_ports[i].port == port)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009203 return i;
9204 }
9205
9206 return i;
9207}
9208
9209/**
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009210 * i40e_udp_tunnel_add - Get notifications about UDP tunnel ports that come up
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009211 * @netdev: This physical port's netdev
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009212 * @ti: Tunnel endpoint information
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009213 **/
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009214static void i40e_udp_tunnel_add(struct net_device *netdev,
9215 struct udp_tunnel_info *ti)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009216{
9217 struct i40e_netdev_priv *np = netdev_priv(netdev);
9218 struct i40e_vsi *vsi = np->vsi;
9219 struct i40e_pf *pf = vsi->back;
Jacob Kellerfe0b0cd2017-02-06 14:38:38 -08009220 u16 port = ntohs(ti->port);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009221 u8 next_idx;
9222 u8 idx;
9223
Singhai, Anjali6a899022015-12-14 12:21:18 -08009224 idx = i40e_get_udp_port_idx(pf, port);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009225
9226 /* Check if port already exists */
9227 if (idx < I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
Jacob Kellerfe0b0cd2017-02-06 14:38:38 -08009228 netdev_info(netdev, "port %d already offloaded\n", port);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009229 return;
9230 }
9231
9232 /* Now check if there is space to add the new port */
Singhai, Anjali6a899022015-12-14 12:21:18 -08009233 next_idx = i40e_get_udp_port_idx(pf, 0);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009234
9235 if (next_idx == I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009236 netdev_info(netdev, "maximum number of offloaded UDP ports reached, not adding port %d\n",
Jacob Kellerfe0b0cd2017-02-06 14:38:38 -08009237 port);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009238 return;
9239 }
9240
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009241 switch (ti->type) {
9242 case UDP_TUNNEL_TYPE_VXLAN:
9243 pf->udp_ports[next_idx].type = I40E_AQC_TUNNEL_TYPE_VXLAN;
9244 break;
9245 case UDP_TUNNEL_TYPE_GENEVE:
9246 if (!(pf->flags & I40E_FLAG_GENEVE_OFFLOAD_CAPABLE))
9247 return;
9248 pf->udp_ports[next_idx].type = I40E_AQC_TUNNEL_TYPE_NGE;
9249 break;
9250 default:
9251 return;
9252 }
9253
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009254 /* New port: add it and mark its index in the bitmap */
Jacob Keller27826fd2017-04-19 09:25:50 -04009255 pf->udp_ports[next_idx].port = port;
Singhai, Anjali6a899022015-12-14 12:21:18 -08009256 pf->pending_udp_bitmap |= BIT_ULL(next_idx);
9257 pf->flags |= I40E_FLAG_UDP_FILTER_SYNC;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009258}
9259
9260/**
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009261 * i40e_udp_tunnel_del - Get notifications about UDP tunnel ports that go away
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009262 * @netdev: This physical port's netdev
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009263 * @ti: Tunnel endpoint information
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009264 **/
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009265static void i40e_udp_tunnel_del(struct net_device *netdev,
9266 struct udp_tunnel_info *ti)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009267{
9268 struct i40e_netdev_priv *np = netdev_priv(netdev);
9269 struct i40e_vsi *vsi = np->vsi;
9270 struct i40e_pf *pf = vsi->back;
Jacob Kellerfe0b0cd2017-02-06 14:38:38 -08009271 u16 port = ntohs(ti->port);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009272 u8 idx;
9273
Singhai, Anjali6a899022015-12-14 12:21:18 -08009274 idx = i40e_get_udp_port_idx(pf, port);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009275
9276 /* Check if port already exists */
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009277 if (idx >= I40E_MAX_PF_UDP_OFFLOAD_PORTS)
9278 goto not_found;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009279
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009280 switch (ti->type) {
9281 case UDP_TUNNEL_TYPE_VXLAN:
9282 if (pf->udp_ports[idx].type != I40E_AQC_TUNNEL_TYPE_VXLAN)
9283 goto not_found;
9284 break;
9285 case UDP_TUNNEL_TYPE_GENEVE:
9286 if (pf->udp_ports[idx].type != I40E_AQC_TUNNEL_TYPE_NGE)
9287 goto not_found;
9288 break;
9289 default:
9290 goto not_found;
Singhai, Anjali6a899022015-12-14 12:21:18 -08009291 }
9292
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009293 /* if port exists, set it to 0 (mark for deletion)
9294 * and make it pending
9295 */
Jacob Keller27826fd2017-04-19 09:25:50 -04009296 pf->udp_ports[idx].port = 0;
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009297 pf->pending_udp_bitmap |= BIT_ULL(idx);
Singhai, Anjali6a899022015-12-14 12:21:18 -08009298 pf->flags |= I40E_FLAG_UDP_FILTER_SYNC;
9299
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009300 return;
9301not_found:
9302 netdev_warn(netdev, "UDP port %d was not found, not deleting\n",
Jacob Kellerfe0b0cd2017-02-06 14:38:38 -08009303 port);
Singhai, Anjali6a899022015-12-14 12:21:18 -08009304}
9305
Neerav Parikh1f224ad2014-02-12 01:45:31 +00009306static int i40e_get_phys_port_id(struct net_device *netdev,
Jiri Pirko02637fc2014-11-28 14:34:16 +01009307 struct netdev_phys_item_id *ppid)
Neerav Parikh1f224ad2014-02-12 01:45:31 +00009308{
9309 struct i40e_netdev_priv *np = netdev_priv(netdev);
9310 struct i40e_pf *pf = np->vsi->back;
9311 struct i40e_hw *hw = &pf->hw;
9312
9313 if (!(pf->flags & I40E_FLAG_PORT_ID_VALID))
9314 return -EOPNOTSUPP;
9315
9316 ppid->id_len = min_t(int, sizeof(hw->mac.port_addr), sizeof(ppid->id));
9317 memcpy(ppid->id, hw->mac.port_addr, ppid->id_len);
9318
9319 return 0;
9320}
9321
Jesse Brandeburg2f90ade2014-11-20 16:30:02 -08009322/**
9323 * i40e_ndo_fdb_add - add an entry to the hardware database
9324 * @ndm: the input from the stack
9325 * @tb: pointer to array of nladdr (unused)
9326 * @dev: the net device pointer
9327 * @addr: the MAC address entry being added
9328 * @flags: instructions from stack about fdb operation
9329 */
Greg Rose4ba0dea2014-03-06 08:59:55 +00009330static int i40e_ndo_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
9331 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +01009332 const unsigned char *addr, u16 vid,
Greg Rose4ba0dea2014-03-06 08:59:55 +00009333 u16 flags)
Greg Rose4ba0dea2014-03-06 08:59:55 +00009334{
9335 struct i40e_netdev_priv *np = netdev_priv(dev);
9336 struct i40e_pf *pf = np->vsi->back;
9337 int err = 0;
9338
9339 if (!(pf->flags & I40E_FLAG_SRIOV_ENABLED))
9340 return -EOPNOTSUPP;
9341
Or Gerlitz65891fe2014-12-14 18:19:05 +02009342 if (vid) {
9343 pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
9344 return -EINVAL;
9345 }
9346
Greg Rose4ba0dea2014-03-06 08:59:55 +00009347 /* Hardware does not support aging addresses so if a
9348 * ndm_state is given only allow permanent addresses
9349 */
9350 if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
9351 netdev_info(dev, "FDB only supports static addresses\n");
9352 return -EINVAL;
9353 }
9354
9355 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
9356 err = dev_uc_add_excl(dev, addr);
9357 else if (is_multicast_ether_addr(addr))
9358 err = dev_mc_add_excl(dev, addr);
9359 else
9360 err = -EINVAL;
9361
9362 /* Only return duplicate errors if NLM_F_EXCL is set */
9363 if (err == -EEXIST && !(flags & NLM_F_EXCL))
9364 err = 0;
9365
9366 return err;
9367}
9368
Neerav Parikh51616012015-02-06 08:52:14 +00009369/**
9370 * i40e_ndo_bridge_setlink - Set the hardware bridge mode
9371 * @dev: the netdev being configured
9372 * @nlh: RTNL message
9373 *
9374 * Inserts a new hardware bridge if not already created and
9375 * enables the bridging mode requested (VEB or VEPA). If the
9376 * hardware bridge has already been inserted and the request
9377 * is to change the mode then that requires a PF reset to
9378 * allow rebuild of the components with required hardware
9379 * bridge mode enabled.
Maciej Sosin373149f2017-04-05 07:50:55 -04009380 *
9381 * Note: expects to be called while under rtnl_lock()
Neerav Parikh51616012015-02-06 08:52:14 +00009382 **/
9383static int i40e_ndo_bridge_setlink(struct net_device *dev,
Carolyn Wyborny9df70b62015-04-27 14:57:11 -04009384 struct nlmsghdr *nlh,
9385 u16 flags)
Neerav Parikh51616012015-02-06 08:52:14 +00009386{
9387 struct i40e_netdev_priv *np = netdev_priv(dev);
9388 struct i40e_vsi *vsi = np->vsi;
9389 struct i40e_pf *pf = vsi->back;
9390 struct i40e_veb *veb = NULL;
9391 struct nlattr *attr, *br_spec;
9392 int i, rem;
9393
9394 /* Only for PF VSI for now */
9395 if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
9396 return -EOPNOTSUPP;
9397
9398 /* Find the HW bridge for PF VSI */
9399 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
9400 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
9401 veb = pf->veb[i];
9402 }
9403
9404 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
9405
9406 nla_for_each_nested(attr, br_spec, rem) {
9407 __u16 mode;
9408
9409 if (nla_type(attr) != IFLA_BRIDGE_MODE)
9410 continue;
9411
9412 mode = nla_get_u16(attr);
9413 if ((mode != BRIDGE_MODE_VEPA) &&
9414 (mode != BRIDGE_MODE_VEB))
9415 return -EINVAL;
9416
9417 /* Insert a new HW bridge */
9418 if (!veb) {
9419 veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
9420 vsi->tc_config.enabled_tc);
9421 if (veb) {
9422 veb->bridge_mode = mode;
9423 i40e_config_bridge_mode(veb);
9424 } else {
9425 /* No Bridge HW offload available */
9426 return -ENOENT;
9427 }
9428 break;
9429 } else if (mode != veb->bridge_mode) {
9430 /* Existing HW bridge but different mode needs reset */
9431 veb->bridge_mode = mode;
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07009432 /* TODO: If no VFs or VMDq VSIs, disallow VEB mode */
9433 if (mode == BRIDGE_MODE_VEB)
9434 pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
9435 else
9436 pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
Maciej Sosin373149f2017-04-05 07:50:55 -04009437 i40e_do_reset(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED),
9438 true);
Neerav Parikh51616012015-02-06 08:52:14 +00009439 break;
9440 }
9441 }
9442
9443 return 0;
9444}
9445
9446/**
9447 * i40e_ndo_bridge_getlink - Get the hardware bridge mode
9448 * @skb: skb buff
9449 * @pid: process id
9450 * @seq: RTNL message seq #
9451 * @dev: the netdev being configured
9452 * @filter_mask: unused
Jesse Brandeburgd4b2f9f2015-09-03 17:18:48 -04009453 * @nlflags: netlink flags passed in
Neerav Parikh51616012015-02-06 08:52:14 +00009454 *
9455 * Return the mode in which the hardware bridge is operating in
9456 * i.e VEB or VEPA.
9457 **/
Neerav Parikh51616012015-02-06 08:52:14 +00009458static int i40e_ndo_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
9459 struct net_device *dev,
Carolyn Wyborny9f4ffc42015-08-31 19:54:42 -04009460 u32 __always_unused filter_mask,
9461 int nlflags)
Neerav Parikh51616012015-02-06 08:52:14 +00009462{
9463 struct i40e_netdev_priv *np = netdev_priv(dev);
9464 struct i40e_vsi *vsi = np->vsi;
9465 struct i40e_pf *pf = vsi->back;
9466 struct i40e_veb *veb = NULL;
9467 int i;
9468
9469 /* Only for PF VSI for now */
9470 if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
9471 return -EOPNOTSUPP;
9472
9473 /* Find the HW bridge for the PF VSI */
9474 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
9475 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
9476 veb = pf->veb[i];
9477 }
9478
9479 if (!veb)
9480 return 0;
9481
Nicolas Dichtel46c264d2015-04-28 18:33:49 +02009482 return ndo_dflt_bridge_getlink(skb, pid, seq, dev, veb->bridge_mode,
Huaibin Wang599b0762016-09-26 09:51:18 +02009483 0, 0, nlflags, filter_mask, NULL);
Neerav Parikh51616012015-02-06 08:52:14 +00009484}
Neerav Parikh51616012015-02-06 08:52:14 +00009485
Joe Stringerf44a75e2015-04-14 17:09:14 -07009486/**
9487 * i40e_features_check - Validate encapsulated packet conforms to limits
9488 * @skb: skb buff
Jean Sacren2bc11c62015-09-19 05:08:43 -06009489 * @dev: This physical port's netdev
Joe Stringerf44a75e2015-04-14 17:09:14 -07009490 * @features: Offload features that the stack believes apply
9491 **/
9492static netdev_features_t i40e_features_check(struct sk_buff *skb,
9493 struct net_device *dev,
9494 netdev_features_t features)
9495{
Alexander Duyckf114dca2016-10-25 16:08:46 -07009496 size_t len;
9497
9498 /* No point in doing any of this if neither checksum nor GSO are
9499 * being requested for this frame. We can rule out both by just
9500 * checking for CHECKSUM_PARTIAL
9501 */
9502 if (skb->ip_summed != CHECKSUM_PARTIAL)
9503 return features;
9504
9505 /* We cannot support GSO if the MSS is going to be less than
9506 * 64 bytes. If it is then we need to drop support for GSO.
9507 */
9508 if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
9509 features &= ~NETIF_F_GSO_MASK;
9510
9511 /* MACLEN can support at most 63 words */
9512 len = skb_network_header(skb) - skb->data;
9513 if (len & ~(63 * 2))
9514 goto out_err;
9515
9516 /* IPLEN and EIPLEN can support at most 127 dwords */
9517 len = skb_transport_header(skb) - skb_network_header(skb);
9518 if (len & ~(127 * 4))
9519 goto out_err;
9520
9521 if (skb->encapsulation) {
9522 /* L4TUNLEN can support 127 words */
9523 len = skb_inner_network_header(skb) - skb_transport_header(skb);
9524 if (len & ~(127 * 2))
9525 goto out_err;
9526
9527 /* IPLEN can support at most 127 dwords */
9528 len = skb_inner_transport_header(skb) -
9529 skb_inner_network_header(skb);
9530 if (len & ~(127 * 4))
9531 goto out_err;
9532 }
9533
9534 /* No need to validate L4LEN as TCP is the only protocol with a
9535 * a flexible value and we support all possible values supported
9536 * by TCP, which is at most 15 dwords
9537 */
Joe Stringerf44a75e2015-04-14 17:09:14 -07009538
9539 return features;
Alexander Duyckf114dca2016-10-25 16:08:46 -07009540out_err:
9541 return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
Joe Stringerf44a75e2015-04-14 17:09:14 -07009542}
9543
Björn Töpel0c8493d2017-05-24 07:55:34 +02009544/**
9545 * i40e_xdp_setup - add/remove an XDP program
9546 * @vsi: VSI to changed
9547 * @prog: XDP program
9548 **/
9549static int i40e_xdp_setup(struct i40e_vsi *vsi,
9550 struct bpf_prog *prog)
9551{
9552 int frame_size = vsi->netdev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
9553 struct i40e_pf *pf = vsi->back;
9554 struct bpf_prog *old_prog;
9555 bool need_reset;
9556 int i;
9557
9558 /* Don't allow frames that span over multiple buffers */
9559 if (frame_size > vsi->rx_buf_len)
9560 return -EINVAL;
9561
9562 if (!i40e_enabled_xdp_vsi(vsi) && !prog)
9563 return 0;
9564
9565 /* When turning XDP on->off/off->on we reset and rebuild the rings. */
9566 need_reset = (i40e_enabled_xdp_vsi(vsi) != !!prog);
9567
9568 if (need_reset)
9569 i40e_prep_for_reset(pf, true);
9570
9571 old_prog = xchg(&vsi->xdp_prog, prog);
9572
9573 if (need_reset)
9574 i40e_reset_and_rebuild(pf, true, true);
9575
9576 for (i = 0; i < vsi->num_queue_pairs; i++)
9577 WRITE_ONCE(vsi->rx_rings[i]->xdp_prog, vsi->xdp_prog);
9578
9579 if (old_prog)
9580 bpf_prog_put(old_prog);
9581
9582 return 0;
9583}
9584
9585/**
9586 * i40e_xdp - implements ndo_xdp for i40e
9587 * @dev: netdevice
9588 * @xdp: XDP command
9589 **/
9590static int i40e_xdp(struct net_device *dev,
9591 struct netdev_xdp *xdp)
9592{
9593 struct i40e_netdev_priv *np = netdev_priv(dev);
9594 struct i40e_vsi *vsi = np->vsi;
9595
9596 if (vsi->type != I40E_VSI_MAIN)
9597 return -EINVAL;
9598
9599 switch (xdp->command) {
9600 case XDP_SETUP_PROG:
9601 return i40e_xdp_setup(vsi, xdp->prog);
9602 case XDP_QUERY_PROG:
9603 xdp->prog_attached = i40e_enabled_xdp_vsi(vsi);
Daniel Borkmanneb230392017-06-24 21:13:52 +02009604 xdp->prog_id = vsi->xdp_prog ? vsi->xdp_prog->aux->id : 0;
Björn Töpel0c8493d2017-05-24 07:55:34 +02009605 return 0;
9606 default:
9607 return -EINVAL;
9608 }
9609}
9610
Shannon Nelson37a29732015-02-27 09:15:19 +00009611static const struct net_device_ops i40e_netdev_ops = {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009612 .ndo_open = i40e_open,
9613 .ndo_stop = i40e_close,
9614 .ndo_start_xmit = i40e_lan_xmit_frame,
9615 .ndo_get_stats64 = i40e_get_netdev_stats_struct,
9616 .ndo_set_rx_mode = i40e_set_rx_mode,
9617 .ndo_validate_addr = eth_validate_addr,
9618 .ndo_set_mac_address = i40e_set_mac,
9619 .ndo_change_mtu = i40e_change_mtu,
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00009620 .ndo_do_ioctl = i40e_ioctl,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009621 .ndo_tx_timeout = i40e_tx_timeout,
9622 .ndo_vlan_rx_add_vid = i40e_vlan_rx_add_vid,
9623 .ndo_vlan_rx_kill_vid = i40e_vlan_rx_kill_vid,
9624#ifdef CONFIG_NET_POLL_CONTROLLER
9625 .ndo_poll_controller = i40e_netpoll,
9626#endif
John Fastabende4c67342016-02-16 21:16:15 -08009627 .ndo_setup_tc = __i40e_setup_tc,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009628 .ndo_set_features = i40e_set_features,
9629 .ndo_set_vf_mac = i40e_ndo_set_vf_mac,
9630 .ndo_set_vf_vlan = i40e_ndo_set_vf_port_vlan,
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04009631 .ndo_set_vf_rate = i40e_ndo_set_vf_bw,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009632 .ndo_get_vf_config = i40e_ndo_get_vf_config,
Mitch Williams588aefa2014-02-11 08:27:49 +00009633 .ndo_set_vf_link_state = i40e_ndo_set_vf_link_state,
Serey Konge6d90042014-07-12 07:28:14 +00009634 .ndo_set_vf_spoofchk = i40e_ndo_set_vf_spoofchk,
Anjali Singhai Jainc3bbbd22016-04-01 03:56:07 -07009635 .ndo_set_vf_trust = i40e_ndo_set_vf_trust,
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009636 .ndo_udp_tunnel_add = i40e_udp_tunnel_add,
9637 .ndo_udp_tunnel_del = i40e_udp_tunnel_del,
Neerav Parikh1f224ad2014-02-12 01:45:31 +00009638 .ndo_get_phys_port_id = i40e_get_phys_port_id,
Greg Rose4ba0dea2014-03-06 08:59:55 +00009639 .ndo_fdb_add = i40e_ndo_fdb_add,
Joe Stringerf44a75e2015-04-14 17:09:14 -07009640 .ndo_features_check = i40e_features_check,
Neerav Parikh51616012015-02-06 08:52:14 +00009641 .ndo_bridge_getlink = i40e_ndo_bridge_getlink,
9642 .ndo_bridge_setlink = i40e_ndo_bridge_setlink,
Björn Töpel0c8493d2017-05-24 07:55:34 +02009643 .ndo_xdp = i40e_xdp,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009644};
9645
9646/**
9647 * i40e_config_netdev - Setup the netdev flags
9648 * @vsi: the VSI being configured
9649 *
9650 * Returns 0 on success, negative value on failure
9651 **/
9652static int i40e_config_netdev(struct i40e_vsi *vsi)
9653{
9654 struct i40e_pf *pf = vsi->back;
9655 struct i40e_hw *hw = &pf->hw;
9656 struct i40e_netdev_priv *np;
9657 struct net_device *netdev;
Jacob Keller435c0842016-11-08 13:05:10 -08009658 u8 broadcast[ETH_ALEN];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009659 u8 mac_addr[ETH_ALEN];
9660 int etherdev_size;
Preethi Banalabacd75c2017-03-27 14:43:18 -07009661 netdev_features_t hw_enc_features;
9662 netdev_features_t hw_features;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009663
9664 etherdev_size = sizeof(struct i40e_netdev_priv);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00009665 netdev = alloc_etherdev_mq(etherdev_size, vsi->alloc_queue_pairs);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009666 if (!netdev)
9667 return -ENOMEM;
9668
9669 vsi->netdev = netdev;
9670 np = netdev_priv(netdev);
9671 np->vsi = vsi;
9672
Preethi Banalabacd75c2017-03-27 14:43:18 -07009673 hw_enc_features = NETIF_F_SG |
9674 NETIF_F_IP_CSUM |
9675 NETIF_F_IPV6_CSUM |
9676 NETIF_F_HIGHDMA |
9677 NETIF_F_SOFT_FEATURES |
9678 NETIF_F_TSO |
9679 NETIF_F_TSO_ECN |
9680 NETIF_F_TSO6 |
9681 NETIF_F_GSO_GRE |
9682 NETIF_F_GSO_GRE_CSUM |
9683 NETIF_F_GSO_PARTIAL |
9684 NETIF_F_GSO_UDP_TUNNEL |
9685 NETIF_F_GSO_UDP_TUNNEL_CSUM |
9686 NETIF_F_SCTP_CRC |
9687 NETIF_F_RXHASH |
9688 NETIF_F_RXCSUM |
9689 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009690
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009691 if (!(pf->flags & I40E_FLAG_OUTER_UDP_CSUM_CAPABLE))
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04009692 netdev->gso_partial_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM;
9693
9694 netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009695
Preethi Banalabacd75c2017-03-27 14:43:18 -07009696 netdev->hw_enc_features |= hw_enc_features;
9697
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009698 /* record features VLANs can make use of */
Preethi Banalabacd75c2017-03-27 14:43:18 -07009699 netdev->vlan_features |= hw_enc_features | NETIF_F_TSO_MANGLEID;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009700
Anjali Singhai Jain2e86a0b2014-04-01 07:11:53 +00009701 if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009702 netdev->hw_features |= NETIF_F_NTUPLE;
Preethi Banalabacd75c2017-03-27 14:43:18 -07009703 hw_features = hw_enc_features |
9704 NETIF_F_HW_VLAN_CTAG_TX |
9705 NETIF_F_HW_VLAN_CTAG_RX;
Anjali Singhai Jain2e86a0b2014-04-01 07:11:53 +00009706
Preethi Banalabacd75c2017-03-27 14:43:18 -07009707 netdev->hw_features |= hw_features;
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009708
Preethi Banalabacd75c2017-03-27 14:43:18 -07009709 netdev->features |= hw_features | NETIF_F_HW_VLAN_CTAG_FILTER;
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04009710 netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009711
9712 if (vsi->type == I40E_VSI_MAIN) {
9713 SET_NETDEV_DEV(netdev, &pf->pdev->dev);
Greg Rose9a173902014-05-22 06:32:02 +00009714 ether_addr_copy(mac_addr, hw->mac.perm_addr);
Jacob Keller41c4c2b2017-04-05 07:50:57 -04009715 /* The following steps are necessary for two reasons. First,
9716 * some older NVM configurations load a default MAC-VLAN
9717 * filter that will accept any tagged packet, and we want to
9718 * replace this with a normal filter. Additionally, it is
9719 * possible our MAC address was provided by the platform using
9720 * Open Firmware or similar.
9721 *
9722 * Thus, we need to remove the default filter and install one
9723 * specific to the MAC address.
Jacob Keller1596b5d2016-11-08 13:05:15 -08009724 */
9725 i40e_rm_default_mac_filter(vsi, mac_addr);
Jacob Keller278e7d02016-10-05 09:30:37 -07009726 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller9569a9a2016-11-11 12:39:37 -08009727 i40e_add_mac_filter(vsi, mac_addr);
Jacob Keller278e7d02016-10-05 09:30:37 -07009728 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009729 } else {
9730 /* relate the VSI_VMDQ name to the VSI_MAIN name */
9731 snprintf(netdev->name, IFNAMSIZ, "%sv%%d",
9732 pf->vsi[pf->lan_vsi]->netdev->name);
9733 random_ether_addr(mac_addr);
Kiran Patil21659032015-09-30 14:09:03 -04009734
Jacob Keller278e7d02016-10-05 09:30:37 -07009735 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller9569a9a2016-11-11 12:39:37 -08009736 i40e_add_mac_filter(vsi, mac_addr);
Jacob Keller278e7d02016-10-05 09:30:37 -07009737 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009738 }
Kiran Patil21659032015-09-30 14:09:03 -04009739
Jacob Keller435c0842016-11-08 13:05:10 -08009740 /* Add the broadcast filter so that we initially will receive
9741 * broadcast packets. Note that when a new VLAN is first added the
9742 * driver will convert all filters marked I40E_VLAN_ANY into VLAN
9743 * specific filters as part of transitioning into "vlan" operation.
9744 * When more VLANs are added, the driver will copy each existing MAC
9745 * filter and add it for the new VLAN.
9746 *
9747 * Broadcast filters are handled specially by
9748 * i40e_sync_filters_subtask, as the driver must to set the broadcast
9749 * promiscuous bit instead of adding this directly as a MAC/VLAN
9750 * filter. The subtask will update the correct broadcast promiscuous
9751 * bits as VLANs become active or inactive.
9752 */
9753 eth_broadcast_addr(broadcast);
9754 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller9569a9a2016-11-11 12:39:37 -08009755 i40e_add_mac_filter(vsi, broadcast);
Jacob Keller435c0842016-11-08 13:05:10 -08009756 spin_unlock_bh(&vsi->mac_filter_hash_lock);
9757
Greg Rose9a173902014-05-22 06:32:02 +00009758 ether_addr_copy(netdev->dev_addr, mac_addr);
9759 ether_addr_copy(netdev->perm_addr, mac_addr);
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009760
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009761 netdev->priv_flags |= IFF_UNICAST_FLT;
9762 netdev->priv_flags |= IFF_SUPP_NOFCS;
9763 /* Setup netdev TC information */
9764 i40e_vsi_config_netdev_tc(vsi, vsi->tc_config.enabled_tc);
9765
9766 netdev->netdev_ops = &i40e_netdev_ops;
9767 netdev->watchdog_timeo = 5 * HZ;
9768 i40e_set_ethtool_ops(netdev);
9769
Jarod Wilson91c527a2016-10-17 15:54:05 -04009770 /* MTU range: 68 - 9706 */
9771 netdev->min_mtu = ETH_MIN_MTU;
9772 netdev->max_mtu = I40E_MAX_RXBUFFER -
9773 (ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN);
9774
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009775 return 0;
9776}
9777
9778/**
9779 * i40e_vsi_delete - Delete a VSI from the switch
9780 * @vsi: the VSI being removed
9781 *
9782 * Returns 0 on success, negative value on failure
9783 **/
9784static void i40e_vsi_delete(struct i40e_vsi *vsi)
9785{
9786 /* remove default VSI is not allowed */
9787 if (vsi == vsi->back->vsi[vsi->back->lan_vsi])
9788 return;
9789
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009790 i40e_aq_delete_element(&vsi->back->hw, vsi->seid, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009791}
9792
9793/**
Neerav Parikh51616012015-02-06 08:52:14 +00009794 * i40e_is_vsi_uplink_mode_veb - Check if the VSI's uplink bridge mode is VEB
9795 * @vsi: the VSI being queried
9796 *
9797 * Returns 1 if HW bridge mode is VEB and return 0 in case of VEPA mode
9798 **/
9799int i40e_is_vsi_uplink_mode_veb(struct i40e_vsi *vsi)
9800{
9801 struct i40e_veb *veb;
9802 struct i40e_pf *pf = vsi->back;
9803
9804 /* Uplink is not a bridge so default to VEB */
9805 if (vsi->veb_idx == I40E_NO_VEB)
9806 return 1;
9807
9808 veb = pf->veb[vsi->veb_idx];
Akeem G Abodunrin09603ea2015-10-01 14:37:36 -04009809 if (!veb) {
9810 dev_info(&pf->pdev->dev,
9811 "There is no veb associated with the bridge\n");
9812 return -ENOENT;
9813 }
Neerav Parikh51616012015-02-06 08:52:14 +00009814
Akeem G Abodunrin09603ea2015-10-01 14:37:36 -04009815 /* Uplink is a bridge in VEPA mode */
9816 if (veb->bridge_mode & BRIDGE_MODE_VEPA) {
9817 return 0;
9818 } else {
9819 /* Uplink is a bridge in VEB mode */
9820 return 1;
9821 }
9822
9823 /* VEPA is now default bridge, so return 0 */
9824 return 0;
Neerav Parikh51616012015-02-06 08:52:14 +00009825}
9826
9827/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009828 * i40e_add_vsi - Add a VSI to the switch
9829 * @vsi: the VSI being configured
9830 *
9831 * This initializes a VSI context depending on the VSI type to be added and
9832 * passes it down to the add_vsi aq command.
9833 **/
9834static int i40e_add_vsi(struct i40e_vsi *vsi)
9835{
9836 int ret = -ENODEV;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009837 struct i40e_pf *pf = vsi->back;
9838 struct i40e_hw *hw = &pf->hw;
9839 struct i40e_vsi_context ctxt;
Jacob Keller278e7d02016-10-05 09:30:37 -07009840 struct i40e_mac_filter *f;
9841 struct hlist_node *h;
9842 int bkt;
Kiran Patil21659032015-09-30 14:09:03 -04009843
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009844 u8 enabled_tc = 0x1; /* TC0 enabled */
9845 int f_count = 0;
9846
9847 memset(&ctxt, 0, sizeof(ctxt));
9848 switch (vsi->type) {
9849 case I40E_VSI_MAIN:
9850 /* The PF's main VSI is already setup as part of the
9851 * device initialization, so we'll not bother with
9852 * the add_vsi call, but we will retrieve the current
9853 * VSI context.
9854 */
9855 ctxt.seid = pf->main_vsi_seid;
9856 ctxt.pf_num = pf->hw.pf_id;
9857 ctxt.vf_num = 0;
9858 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
9859 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
9860 if (ret) {
9861 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009862 "couldn't get PF vsi config, err %s aq_err %s\n",
9863 i40e_stat_str(&pf->hw, ret),
9864 i40e_aq_str(&pf->hw,
9865 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009866 return -ENOENT;
9867 }
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07009868 vsi->info = ctxt.info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009869 vsi->info.valid_sections = 0;
9870
9871 vsi->seid = ctxt.seid;
9872 vsi->id = ctxt.vsi_number;
9873
9874 enabled_tc = i40e_pf_get_tc_map(pf);
9875
9876 /* MFP mode setup queue map and update VSI */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00009877 if ((pf->flags & I40E_FLAG_MFP_ENABLED) &&
9878 !(pf->hw.func_caps.iscsi)) { /* NIC type PF */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009879 memset(&ctxt, 0, sizeof(ctxt));
9880 ctxt.seid = pf->main_vsi_seid;
9881 ctxt.pf_num = pf->hw.pf_id;
9882 ctxt.vf_num = 0;
9883 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
9884 ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
9885 if (ret) {
9886 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009887 "update vsi failed, err %s aq_err %s\n",
9888 i40e_stat_str(&pf->hw, ret),
9889 i40e_aq_str(&pf->hw,
9890 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009891 ret = -ENOENT;
9892 goto err;
9893 }
9894 /* update the local VSI info queue map */
9895 i40e_vsi_update_queue_map(vsi, &ctxt);
9896 vsi->info.valid_sections = 0;
9897 } else {
9898 /* Default/Main VSI is only enabled for TC0
9899 * reconfigure it to enable all TCs that are
9900 * available on the port in SFP mode.
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00009901 * For MFP case the iSCSI PF would use this
9902 * flow to enable LAN+iSCSI TC.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009903 */
9904 ret = i40e_vsi_config_tc(vsi, enabled_tc);
9905 if (ret) {
9906 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009907 "failed to configure TCs for main VSI tc_map 0x%08x, err %s aq_err %s\n",
9908 enabled_tc,
9909 i40e_stat_str(&pf->hw, ret),
9910 i40e_aq_str(&pf->hw,
9911 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009912 ret = -ENOENT;
9913 }
9914 }
9915 break;
9916
9917 case I40E_VSI_FDIR:
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009918 ctxt.pf_num = hw->pf_id;
9919 ctxt.vf_num = 0;
9920 ctxt.uplink_seid = vsi->uplink_seid;
Neerav Parikh2b18e592015-01-24 09:58:38 +00009921 ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009922 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07009923 if ((pf->flags & I40E_FLAG_VEB_MODE_ENABLED) &&
9924 (i40e_is_vsi_uplink_mode_veb(vsi))) {
Neerav Parikh51616012015-02-06 08:52:14 +00009925 ctxt.info.valid_sections |=
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07009926 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
Neerav Parikh51616012015-02-06 08:52:14 +00009927 ctxt.info.switch_id =
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07009928 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
Neerav Parikh51616012015-02-06 08:52:14 +00009929 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009930 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009931 break;
9932
9933 case I40E_VSI_VMDQ2:
9934 ctxt.pf_num = hw->pf_id;
9935 ctxt.vf_num = 0;
9936 ctxt.uplink_seid = vsi->uplink_seid;
Neerav Parikh2b18e592015-01-24 09:58:38 +00009937 ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009938 ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
9939
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009940 /* This VSI is connected to VEB so the switch_id
9941 * should be set to zero by default.
9942 */
Neerav Parikh51616012015-02-06 08:52:14 +00009943 if (i40e_is_vsi_uplink_mode_veb(vsi)) {
9944 ctxt.info.valid_sections |=
9945 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
9946 ctxt.info.switch_id =
9947 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
9948 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009949
9950 /* Setup the VSI tx/rx queue map for TC0 only for now */
9951 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
9952 break;
9953
9954 case I40E_VSI_SRIOV:
9955 ctxt.pf_num = hw->pf_id;
9956 ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
9957 ctxt.uplink_seid = vsi->uplink_seid;
Neerav Parikh2b18e592015-01-24 09:58:38 +00009958 ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009959 ctxt.flags = I40E_AQ_VSI_TYPE_VF;
9960
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009961 /* This VSI is connected to VEB so the switch_id
9962 * should be set to zero by default.
9963 */
Neerav Parikh51616012015-02-06 08:52:14 +00009964 if (i40e_is_vsi_uplink_mode_veb(vsi)) {
9965 ctxt.info.valid_sections |=
9966 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
9967 ctxt.info.switch_id =
9968 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
9969 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009970
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06009971 if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
9972 ctxt.info.valid_sections |=
9973 cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
9974 ctxt.info.queueing_opt_flags |=
Ashish Shah4b28cdb2016-05-03 15:13:17 -07009975 (I40E_AQ_VSI_QUE_OPT_TCP_ENA |
9976 I40E_AQ_VSI_QUE_OPT_RSS_LUT_VSI);
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06009977 }
9978
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009979 ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
9980 ctxt.info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_MODE_ALL;
Mitch Williamsc674d122014-05-20 08:01:40 +00009981 if (pf->vf[vsi->vf_id].spoofchk) {
9982 ctxt.info.valid_sections |=
9983 cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
9984 ctxt.info.sec_flags |=
9985 (I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
9986 I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
9987 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009988 /* Setup the VSI tx/rx queue map for TC0 only for now */
9989 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
9990 break;
9991
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06009992 case I40E_VSI_IWARP:
9993 /* send down message to iWARP */
9994 break;
9995
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009996 default:
9997 return -ENODEV;
9998 }
9999
10000 if (vsi->type != I40E_VSI_MAIN) {
10001 ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
10002 if (ret) {
10003 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010004 "add vsi failed, err %s aq_err %s\n",
10005 i40e_stat_str(&pf->hw, ret),
10006 i40e_aq_str(&pf->hw,
10007 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010008 ret = -ENOENT;
10009 goto err;
10010 }
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -070010011 vsi->info = ctxt.info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010012 vsi->info.valid_sections = 0;
10013 vsi->seid = ctxt.seid;
10014 vsi->id = ctxt.vsi_number;
10015 }
10016
Mitch Williamsc3c7ea22016-06-20 09:10:38 -070010017 vsi->active_filters = 0;
Jacob Keller0da36b92017-04-19 09:25:55 -040010018 clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
Jacob Keller278e7d02016-10-05 09:30:37 -070010019 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010020 /* If macvlan filters already exist, force them to get loaded */
Jacob Keller278e7d02016-10-05 09:30:37 -070010021 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -070010022 f->state = I40E_FILTER_NEW;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010023 f_count++;
10024 }
Jacob Keller278e7d02016-10-05 09:30:37 -070010025 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -040010026
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010027 if (f_count) {
10028 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
10029 pf->flags |= I40E_FLAG_FILTER_SYNC;
10030 }
10031
10032 /* Update VSI BW information */
10033 ret = i40e_vsi_get_bw_info(vsi);
10034 if (ret) {
10035 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010036 "couldn't get vsi bw info, err %s aq_err %s\n",
10037 i40e_stat_str(&pf->hw, ret),
10038 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010039 /* VSI is already added so not tearing that up */
10040 ret = 0;
10041 }
10042
10043err:
10044 return ret;
10045}
10046
10047/**
10048 * i40e_vsi_release - Delete a VSI and free its resources
10049 * @vsi: the VSI being removed
10050 *
10051 * Returns 0 on success or < 0 on error
10052 **/
10053int i40e_vsi_release(struct i40e_vsi *vsi)
10054{
Jacob Keller278e7d02016-10-05 09:30:37 -070010055 struct i40e_mac_filter *f;
10056 struct hlist_node *h;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010057 struct i40e_veb *veb = NULL;
10058 struct i40e_pf *pf;
10059 u16 uplink_seid;
Jacob Keller278e7d02016-10-05 09:30:37 -070010060 int i, n, bkt;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010061
10062 pf = vsi->back;
10063
10064 /* release of a VEB-owner or last VSI is not allowed */
10065 if (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) {
10066 dev_info(&pf->pdev->dev, "VSI %d has existing VEB %d\n",
10067 vsi->seid, vsi->uplink_seid);
10068 return -ENODEV;
10069 }
10070 if (vsi == pf->vsi[pf->lan_vsi] &&
Mauro S. M. Rodrigues9e6c9c02017-05-12 23:26:56 -030010071 !test_bit(__I40E_DOWN, pf->state)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010072 dev_info(&pf->pdev->dev, "Can't remove PF VSI\n");
10073 return -ENODEV;
10074 }
10075
10076 uplink_seid = vsi->uplink_seid;
10077 if (vsi->type != I40E_VSI_SRIOV) {
10078 if (vsi->netdev_registered) {
10079 vsi->netdev_registered = false;
10080 if (vsi->netdev) {
10081 /* results in a call to i40e_close() */
10082 unregister_netdev(vsi->netdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010083 }
10084 } else {
Shannon Nelson90ef8d42014-03-14 07:32:26 +000010085 i40e_vsi_close(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010086 }
10087 i40e_vsi_disable_irq(vsi);
10088 }
10089
Jacob Keller278e7d02016-10-05 09:30:37 -070010090 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller6622f5c2016-10-05 09:30:32 -070010091
10092 /* clear the sync flag on all filters */
10093 if (vsi->netdev) {
10094 __dev_uc_unsync(vsi->netdev, NULL);
10095 __dev_mc_unsync(vsi->netdev, NULL);
10096 }
10097
10098 /* make sure any remaining filters are marked for deletion */
Jacob Keller278e7d02016-10-05 09:30:37 -070010099 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
Jacob Keller290d2552016-10-05 09:30:36 -070010100 __i40e_del_filter(vsi, f);
Jacob Keller6622f5c2016-10-05 09:30:32 -070010101
Jacob Keller278e7d02016-10-05 09:30:37 -070010102 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -040010103
Jesse Brandeburg17652c62015-11-05 17:01:02 -080010104 i40e_sync_vsi_filters(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010105
10106 i40e_vsi_delete(vsi);
10107 i40e_vsi_free_q_vectors(vsi);
Shannon Nelsona4866592014-02-11 08:24:07 +000010108 if (vsi->netdev) {
10109 free_netdev(vsi->netdev);
10110 vsi->netdev = NULL;
10111 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010112 i40e_vsi_clear_rings(vsi);
10113 i40e_vsi_clear(vsi);
10114
10115 /* If this was the last thing on the VEB, except for the
10116 * controlling VSI, remove the VEB, which puts the controlling
10117 * VSI onto the next level down in the switch.
10118 *
10119 * Well, okay, there's one more exception here: don't remove
10120 * the orphan VEBs yet. We'll wait for an explicit remove request
10121 * from up the network stack.
10122 */
Mitch Williams505682c2014-05-20 08:01:37 +000010123 for (n = 0, i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010124 if (pf->vsi[i] &&
10125 pf->vsi[i]->uplink_seid == uplink_seid &&
10126 (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
10127 n++; /* count the VSIs */
10128 }
10129 }
10130 for (i = 0; i < I40E_MAX_VEB; i++) {
10131 if (!pf->veb[i])
10132 continue;
10133 if (pf->veb[i]->uplink_seid == uplink_seid)
10134 n++; /* count the VEBs */
10135 if (pf->veb[i]->seid == uplink_seid)
10136 veb = pf->veb[i];
10137 }
10138 if (n == 0 && veb && veb->uplink_seid != 0)
10139 i40e_veb_release(veb);
10140
10141 return 0;
10142}
10143
10144/**
10145 * i40e_vsi_setup_vectors - Set up the q_vectors for the given VSI
10146 * @vsi: ptr to the VSI
10147 *
10148 * This should only be called after i40e_vsi_mem_alloc() which allocates the
10149 * corresponding SW VSI structure and initializes num_queue_pairs for the
10150 * newly allocated VSI.
10151 *
10152 * Returns 0 on success or negative on failure
10153 **/
10154static int i40e_vsi_setup_vectors(struct i40e_vsi *vsi)
10155{
10156 int ret = -ENOENT;
10157 struct i40e_pf *pf = vsi->back;
10158
Alexander Duyck493fb302013-09-28 07:01:44 +000010159 if (vsi->q_vectors[0]) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010160 dev_info(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
10161 vsi->seid);
10162 return -EEXIST;
10163 }
10164
10165 if (vsi->base_vector) {
Jesse Brandeburgf29eaa32014-02-11 08:24:12 +000010166 dev_info(&pf->pdev->dev, "VSI %d has non-zero base vector %d\n",
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010167 vsi->seid, vsi->base_vector);
10168 return -EEXIST;
10169 }
10170
Greg Rose90e04072014-03-06 08:59:57 +000010171 ret = i40e_vsi_alloc_q_vectors(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010172 if (ret) {
10173 dev_info(&pf->pdev->dev,
10174 "failed to allocate %d q_vector for VSI %d, ret=%d\n",
10175 vsi->num_q_vectors, vsi->seid, ret);
10176 vsi->num_q_vectors = 0;
10177 goto vector_setup_out;
10178 }
10179
Anjali Singhai Jain26cdc442015-07-10 19:36:00 -040010180 /* In Legacy mode, we do not have to get any other vector since we
10181 * piggyback on the misc/ICR0 for queue interrupts.
10182 */
10183 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
10184 return ret;
Shannon Nelson958a3e32013-09-28 07:13:28 +000010185 if (vsi->num_q_vectors)
10186 vsi->base_vector = i40e_get_lump(pf, pf->irq_pile,
10187 vsi->num_q_vectors, vsi->idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010188 if (vsi->base_vector < 0) {
10189 dev_info(&pf->pdev->dev,
Shannon Nelson049a2be2014-10-17 03:14:50 +000010190 "failed to get tracking for %d vectors for VSI %d, err=%d\n",
10191 vsi->num_q_vectors, vsi->seid, vsi->base_vector);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010192 i40e_vsi_free_q_vectors(vsi);
10193 ret = -ENOENT;
10194 goto vector_setup_out;
10195 }
10196
10197vector_setup_out:
10198 return ret;
10199}
10200
10201/**
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010202 * i40e_vsi_reinit_setup - return and reallocate resources for a VSI
10203 * @vsi: pointer to the vsi.
10204 *
10205 * This re-allocates a vsi's queue resources.
10206 *
10207 * Returns pointer to the successfully allocated and configured VSI sw struct
10208 * on success, otherwise returns NULL on failure.
10209 **/
10210static struct i40e_vsi *i40e_vsi_reinit_setup(struct i40e_vsi *vsi)
10211{
Björn Töpel74608d12017-05-24 07:55:35 +020010212 u16 alloc_queue_pairs;
John Underwoodf5340392016-02-18 09:19:24 -080010213 struct i40e_pf *pf;
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010214 u8 enabled_tc;
10215 int ret;
10216
John Underwoodf5340392016-02-18 09:19:24 -080010217 if (!vsi)
10218 return NULL;
10219
10220 pf = vsi->back;
10221
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010222 i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
10223 i40e_vsi_clear_rings(vsi);
10224
10225 i40e_vsi_free_arrays(vsi, false);
10226 i40e_set_num_rings_in_vsi(vsi);
10227 ret = i40e_vsi_alloc_arrays(vsi, false);
10228 if (ret)
10229 goto err_vsi;
10230
Björn Töpel74608d12017-05-24 07:55:35 +020010231 alloc_queue_pairs = vsi->alloc_queue_pairs *
10232 (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
10233
10234 ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010235 if (ret < 0) {
Shannon Nelson049a2be2014-10-17 03:14:50 +000010236 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010237 "failed to get tracking for %d queues for VSI %d err %d\n",
Björn Töpel74608d12017-05-24 07:55:35 +020010238 alloc_queue_pairs, vsi->seid, ret);
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010239 goto err_vsi;
10240 }
10241 vsi->base_queue = ret;
10242
10243 /* Update the FW view of the VSI. Force a reset of TC and queue
10244 * layout configurations.
10245 */
10246 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
10247 pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
10248 pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
10249 i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
Jacob Keller1596b5d2016-11-08 13:05:15 -080010250 if (vsi->type == I40E_VSI_MAIN)
10251 i40e_rm_default_mac_filter(vsi, pf->hw.mac.perm_addr);
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010252
10253 /* assign it some queues */
10254 ret = i40e_alloc_rings(vsi);
10255 if (ret)
10256 goto err_rings;
10257
10258 /* map all of the rings to the q_vectors */
10259 i40e_vsi_map_rings_to_vectors(vsi);
10260 return vsi;
10261
10262err_rings:
10263 i40e_vsi_free_q_vectors(vsi);
10264 if (vsi->netdev_registered) {
10265 vsi->netdev_registered = false;
10266 unregister_netdev(vsi->netdev);
10267 free_netdev(vsi->netdev);
10268 vsi->netdev = NULL;
10269 }
10270 i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
10271err_vsi:
10272 i40e_vsi_clear(vsi);
10273 return NULL;
10274}
10275
10276/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010277 * i40e_vsi_setup - Set up a VSI by a given type
10278 * @pf: board private structure
10279 * @type: VSI type
10280 * @uplink_seid: the switch element to link to
10281 * @param1: usage depends upon VSI type. For VF types, indicates VF id
10282 *
10283 * This allocates the sw VSI structure and its queue resources, then add a VSI
10284 * to the identified VEB.
10285 *
10286 * Returns pointer to the successfully allocated and configure VSI sw struct on
10287 * success, otherwise returns NULL on failure.
10288 **/
10289struct i40e_vsi *i40e_vsi_setup(struct i40e_pf *pf, u8 type,
10290 u16 uplink_seid, u32 param1)
10291{
10292 struct i40e_vsi *vsi = NULL;
10293 struct i40e_veb *veb = NULL;
Björn Töpel74608d12017-05-24 07:55:35 +020010294 u16 alloc_queue_pairs;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010295 int ret, i;
10296 int v_idx;
10297
10298 /* The requested uplink_seid must be either
10299 * - the PF's port seid
10300 * no VEB is needed because this is the PF
10301 * or this is a Flow Director special case VSI
10302 * - seid of an existing VEB
10303 * - seid of a VSI that owns an existing VEB
10304 * - seid of a VSI that doesn't own a VEB
10305 * a new VEB is created and the VSI becomes the owner
10306 * - seid of the PF VSI, which is what creates the first VEB
10307 * this is a special case of the previous
10308 *
10309 * Find which uplink_seid we were given and create a new VEB if needed
10310 */
10311 for (i = 0; i < I40E_MAX_VEB; i++) {
10312 if (pf->veb[i] && pf->veb[i]->seid == uplink_seid) {
10313 veb = pf->veb[i];
10314 break;
10315 }
10316 }
10317
10318 if (!veb && uplink_seid != pf->mac_seid) {
10319
Mitch Williams505682c2014-05-20 08:01:37 +000010320 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010321 if (pf->vsi[i] && pf->vsi[i]->seid == uplink_seid) {
10322 vsi = pf->vsi[i];
10323 break;
10324 }
10325 }
10326 if (!vsi) {
10327 dev_info(&pf->pdev->dev, "no such uplink_seid %d\n",
10328 uplink_seid);
10329 return NULL;
10330 }
10331
10332 if (vsi->uplink_seid == pf->mac_seid)
10333 veb = i40e_veb_setup(pf, 0, pf->mac_seid, vsi->seid,
10334 vsi->tc_config.enabled_tc);
10335 else if ((vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
10336 veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
10337 vsi->tc_config.enabled_tc);
Anjali Singhai Jain79c21a82014-11-13 03:06:14 +000010338 if (veb) {
10339 if (vsi->seid != pf->vsi[pf->lan_vsi]->seid) {
10340 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -040010341 "New VSI creation error, uplink seid of LAN VSI expected.\n");
Anjali Singhai Jain79c21a82014-11-13 03:06:14 +000010342 return NULL;
10343 }
Anjali Singhai Jainfa11cb32015-05-27 12:06:14 -040010344 /* We come up by default in VEPA mode if SRIOV is not
10345 * already enabled, in which case we can't force VEPA
10346 * mode.
10347 */
10348 if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
10349 veb->bridge_mode = BRIDGE_MODE_VEPA;
10350 pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
10351 }
Neerav Parikh51616012015-02-06 08:52:14 +000010352 i40e_config_bridge_mode(veb);
Anjali Singhai Jain79c21a82014-11-13 03:06:14 +000010353 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010354 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
10355 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
10356 veb = pf->veb[i];
10357 }
10358 if (!veb) {
10359 dev_info(&pf->pdev->dev, "couldn't add VEB\n");
10360 return NULL;
10361 }
10362
10363 vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
10364 uplink_seid = veb->seid;
10365 }
10366
10367 /* get vsi sw struct */
10368 v_idx = i40e_vsi_mem_alloc(pf, type);
10369 if (v_idx < 0)
10370 goto err_alloc;
10371 vsi = pf->vsi[v_idx];
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010372 if (!vsi)
10373 goto err_alloc;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010374 vsi->type = type;
10375 vsi->veb_idx = (veb ? veb->idx : I40E_NO_VEB);
10376
10377 if (type == I40E_VSI_MAIN)
10378 pf->lan_vsi = v_idx;
10379 else if (type == I40E_VSI_SRIOV)
10380 vsi->vf_id = param1;
10381 /* assign it some queues */
Björn Töpel74608d12017-05-24 07:55:35 +020010382 alloc_queue_pairs = vsi->alloc_queue_pairs *
10383 (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
10384
10385 ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010386 if (ret < 0) {
Shannon Nelson049a2be2014-10-17 03:14:50 +000010387 dev_info(&pf->pdev->dev,
10388 "failed to get tracking for %d queues for VSI %d err=%d\n",
Björn Töpel74608d12017-05-24 07:55:35 +020010389 alloc_queue_pairs, vsi->seid, ret);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010390 goto err_vsi;
10391 }
10392 vsi->base_queue = ret;
10393
10394 /* get a VSI from the hardware */
10395 vsi->uplink_seid = uplink_seid;
10396 ret = i40e_add_vsi(vsi);
10397 if (ret)
10398 goto err_vsi;
10399
10400 switch (vsi->type) {
10401 /* setup the netdev if needed */
10402 case I40E_VSI_MAIN:
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050010403 /* Apply relevant filters if a platform-specific mac
10404 * address was selected.
10405 */
10406 if (!!(pf->flags & I40E_FLAG_PF_MAC)) {
10407 ret = i40e_macaddr_init(vsi, pf->hw.mac.addr);
10408 if (ret) {
10409 dev_warn(&pf->pdev->dev,
10410 "could not set up macaddr; err %d\n",
10411 ret);
10412 }
10413 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010414 case I40E_VSI_VMDQ2:
10415 ret = i40e_config_netdev(vsi);
10416 if (ret)
10417 goto err_netdev;
10418 ret = register_netdev(vsi->netdev);
10419 if (ret)
10420 goto err_netdev;
10421 vsi->netdev_registered = true;
10422 netif_carrier_off(vsi->netdev);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -080010423#ifdef CONFIG_I40E_DCB
10424 /* Setup DCB netlink interface */
10425 i40e_dcbnl_setup(vsi);
10426#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010427 /* fall through */
10428
10429 case I40E_VSI_FDIR:
10430 /* set up vectors and rings if needed */
10431 ret = i40e_vsi_setup_vectors(vsi);
10432 if (ret)
10433 goto err_msix;
10434
10435 ret = i40e_alloc_rings(vsi);
10436 if (ret)
10437 goto err_rings;
10438
10439 /* map all of the rings to the q_vectors */
10440 i40e_vsi_map_rings_to_vectors(vsi);
10441
10442 i40e_vsi_reset_stats(vsi);
10443 break;
10444
10445 default:
10446 /* no netdev or rings for the other VSI types */
10447 break;
10448 }
10449
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -040010450 if ((pf->flags & I40E_FLAG_RSS_AQ_CAPABLE) &&
10451 (vsi->type == I40E_VSI_VMDQ2)) {
10452 ret = i40e_vsi_config_rss(vsi);
10453 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010454 return vsi;
10455
10456err_rings:
10457 i40e_vsi_free_q_vectors(vsi);
10458err_msix:
10459 if (vsi->netdev_registered) {
10460 vsi->netdev_registered = false;
10461 unregister_netdev(vsi->netdev);
10462 free_netdev(vsi->netdev);
10463 vsi->netdev = NULL;
10464 }
10465err_netdev:
10466 i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
10467err_vsi:
10468 i40e_vsi_clear(vsi);
10469err_alloc:
10470 return NULL;
10471}
10472
10473/**
10474 * i40e_veb_get_bw_info - Query VEB BW information
10475 * @veb: the veb to query
10476 *
10477 * Query the Tx scheduler BW configuration data for given VEB
10478 **/
10479static int i40e_veb_get_bw_info(struct i40e_veb *veb)
10480{
10481 struct i40e_aqc_query_switching_comp_ets_config_resp ets_data;
10482 struct i40e_aqc_query_switching_comp_bw_config_resp bw_data;
10483 struct i40e_pf *pf = veb->pf;
10484 struct i40e_hw *hw = &pf->hw;
10485 u32 tc_bw_max;
10486 int ret = 0;
10487 int i;
10488
10489 ret = i40e_aq_query_switch_comp_bw_config(hw, veb->seid,
10490 &bw_data, NULL);
10491 if (ret) {
10492 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010493 "query veb bw config failed, err %s aq_err %s\n",
10494 i40e_stat_str(&pf->hw, ret),
10495 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010496 goto out;
10497 }
10498
10499 ret = i40e_aq_query_switch_comp_ets_config(hw, veb->seid,
10500 &ets_data, NULL);
10501 if (ret) {
10502 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010503 "query veb bw ets config failed, err %s aq_err %s\n",
10504 i40e_stat_str(&pf->hw, ret),
10505 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010506 goto out;
10507 }
10508
10509 veb->bw_limit = le16_to_cpu(ets_data.port_bw_limit);
10510 veb->bw_max_quanta = ets_data.tc_bw_max;
10511 veb->is_abs_credits = bw_data.absolute_credits_enable;
Neerav Parikh23cd1f02014-11-12 00:18:41 +000010512 veb->enabled_tc = ets_data.tc_valid_bits;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010513 tc_bw_max = le16_to_cpu(bw_data.tc_bw_max[0]) |
10514 (le16_to_cpu(bw_data.tc_bw_max[1]) << 16);
10515 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
10516 veb->bw_tc_share_credits[i] = bw_data.tc_bw_share_credits[i];
10517 veb->bw_tc_limit_credits[i] =
10518 le16_to_cpu(bw_data.tc_bw_limits[i]);
10519 veb->bw_tc_max_quanta[i] = ((tc_bw_max >> (i*4)) & 0x7);
10520 }
10521
10522out:
10523 return ret;
10524}
10525
10526/**
10527 * i40e_veb_mem_alloc - Allocates the next available struct veb in the PF
10528 * @pf: board private structure
10529 *
10530 * On error: returns error code (negative)
10531 * On success: returns vsi index in PF (positive)
10532 **/
10533static int i40e_veb_mem_alloc(struct i40e_pf *pf)
10534{
10535 int ret = -ENOENT;
10536 struct i40e_veb *veb;
10537 int i;
10538
10539 /* Need to protect the allocation of switch elements at the PF level */
10540 mutex_lock(&pf->switch_mutex);
10541
10542 /* VEB list may be fragmented if VEB creation/destruction has
10543 * been happening. We can afford to do a quick scan to look
10544 * for any free slots in the list.
10545 *
10546 * find next empty veb slot, looping back around if necessary
10547 */
10548 i = 0;
10549 while ((i < I40E_MAX_VEB) && (pf->veb[i] != NULL))
10550 i++;
10551 if (i >= I40E_MAX_VEB) {
10552 ret = -ENOMEM;
10553 goto err_alloc_veb; /* out of VEB slots! */
10554 }
10555
10556 veb = kzalloc(sizeof(*veb), GFP_KERNEL);
10557 if (!veb) {
10558 ret = -ENOMEM;
10559 goto err_alloc_veb;
10560 }
10561 veb->pf = pf;
10562 veb->idx = i;
10563 veb->enabled_tc = 1;
10564
10565 pf->veb[i] = veb;
10566 ret = i;
10567err_alloc_veb:
10568 mutex_unlock(&pf->switch_mutex);
10569 return ret;
10570}
10571
10572/**
10573 * i40e_switch_branch_release - Delete a branch of the switch tree
10574 * @branch: where to start deleting
10575 *
10576 * This uses recursion to find the tips of the branch to be
10577 * removed, deleting until we get back to and can delete this VEB.
10578 **/
10579static void i40e_switch_branch_release(struct i40e_veb *branch)
10580{
10581 struct i40e_pf *pf = branch->pf;
10582 u16 branch_seid = branch->seid;
10583 u16 veb_idx = branch->idx;
10584 int i;
10585
10586 /* release any VEBs on this VEB - RECURSION */
10587 for (i = 0; i < I40E_MAX_VEB; i++) {
10588 if (!pf->veb[i])
10589 continue;
10590 if (pf->veb[i]->uplink_seid == branch->seid)
10591 i40e_switch_branch_release(pf->veb[i]);
10592 }
10593
10594 /* Release the VSIs on this VEB, but not the owner VSI.
10595 *
10596 * NOTE: Removing the last VSI on a VEB has the SIDE EFFECT of removing
10597 * the VEB itself, so don't use (*branch) after this loop.
10598 */
Mitch Williams505682c2014-05-20 08:01:37 +000010599 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010600 if (!pf->vsi[i])
10601 continue;
10602 if (pf->vsi[i]->uplink_seid == branch_seid &&
10603 (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
10604 i40e_vsi_release(pf->vsi[i]);
10605 }
10606 }
10607
10608 /* There's one corner case where the VEB might not have been
10609 * removed, so double check it here and remove it if needed.
10610 * This case happens if the veb was created from the debugfs
10611 * commands and no VSIs were added to it.
10612 */
10613 if (pf->veb[veb_idx])
10614 i40e_veb_release(pf->veb[veb_idx]);
10615}
10616
10617/**
10618 * i40e_veb_clear - remove veb struct
10619 * @veb: the veb to remove
10620 **/
10621static void i40e_veb_clear(struct i40e_veb *veb)
10622{
10623 if (!veb)
10624 return;
10625
10626 if (veb->pf) {
10627 struct i40e_pf *pf = veb->pf;
10628
10629 mutex_lock(&pf->switch_mutex);
10630 if (pf->veb[veb->idx] == veb)
10631 pf->veb[veb->idx] = NULL;
10632 mutex_unlock(&pf->switch_mutex);
10633 }
10634
10635 kfree(veb);
10636}
10637
10638/**
10639 * i40e_veb_release - Delete a VEB and free its resources
10640 * @veb: the VEB being removed
10641 **/
10642void i40e_veb_release(struct i40e_veb *veb)
10643{
10644 struct i40e_vsi *vsi = NULL;
10645 struct i40e_pf *pf;
10646 int i, n = 0;
10647
10648 pf = veb->pf;
10649
10650 /* find the remaining VSI and check for extras */
Mitch Williams505682c2014-05-20 08:01:37 +000010651 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010652 if (pf->vsi[i] && pf->vsi[i]->uplink_seid == veb->seid) {
10653 n++;
10654 vsi = pf->vsi[i];
10655 }
10656 }
10657 if (n != 1) {
10658 dev_info(&pf->pdev->dev,
10659 "can't remove VEB %d with %d VSIs left\n",
10660 veb->seid, n);
10661 return;
10662 }
10663
10664 /* move the remaining VSI to uplink veb */
10665 vsi->flags &= ~I40E_VSI_FLAG_VEB_OWNER;
10666 if (veb->uplink_seid) {
10667 vsi->uplink_seid = veb->uplink_seid;
10668 if (veb->uplink_seid == pf->mac_seid)
10669 vsi->veb_idx = I40E_NO_VEB;
10670 else
10671 vsi->veb_idx = veb->veb_idx;
10672 } else {
10673 /* floating VEB */
10674 vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
10675 vsi->veb_idx = pf->vsi[pf->lan_vsi]->veb_idx;
10676 }
10677
10678 i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
10679 i40e_veb_clear(veb);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010680}
10681
10682/**
10683 * i40e_add_veb - create the VEB in the switch
10684 * @veb: the VEB to be instantiated
10685 * @vsi: the controlling VSI
10686 **/
10687static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi)
10688{
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010689 struct i40e_pf *pf = veb->pf;
Shannon Nelson66fc3602016-01-13 16:51:42 -080010690 bool enable_stats = !!(pf->flags & I40E_FLAG_VEB_STATS_ENABLED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010691 int ret;
10692
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010693 ret = i40e_aq_add_veb(&pf->hw, veb->uplink_seid, vsi->seid,
Mitch Williams5bc16032016-05-16 10:26:43 -070010694 veb->enabled_tc, false,
Shannon Nelson66fc3602016-01-13 16:51:42 -080010695 &veb->seid, enable_stats, NULL);
Mitch Williams5bc16032016-05-16 10:26:43 -070010696
10697 /* get a VEB from the hardware */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010698 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010699 dev_info(&pf->pdev->dev,
10700 "couldn't add VEB, err %s aq_err %s\n",
10701 i40e_stat_str(&pf->hw, ret),
10702 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010703 return -EPERM;
10704 }
10705
10706 /* get statistics counter */
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010707 ret = i40e_aq_get_veb_parameters(&pf->hw, veb->seid, NULL, NULL,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010708 &veb->stats_idx, NULL, NULL, NULL);
10709 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010710 dev_info(&pf->pdev->dev,
10711 "couldn't get VEB statistics idx, err %s aq_err %s\n",
10712 i40e_stat_str(&pf->hw, ret),
10713 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010714 return -EPERM;
10715 }
10716 ret = i40e_veb_get_bw_info(veb);
10717 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010718 dev_info(&pf->pdev->dev,
10719 "couldn't get VEB bw info, err %s aq_err %s\n",
10720 i40e_stat_str(&pf->hw, ret),
10721 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10722 i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010723 return -ENOENT;
10724 }
10725
10726 vsi->uplink_seid = veb->seid;
10727 vsi->veb_idx = veb->idx;
10728 vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
10729
10730 return 0;
10731}
10732
10733/**
10734 * i40e_veb_setup - Set up a VEB
10735 * @pf: board private structure
10736 * @flags: VEB setup flags
10737 * @uplink_seid: the switch element to link to
10738 * @vsi_seid: the initial VSI seid
10739 * @enabled_tc: Enabled TC bit-map
10740 *
10741 * This allocates the sw VEB structure and links it into the switch
10742 * It is possible and legal for this to be a duplicate of an already
10743 * existing VEB. It is also possible for both uplink and vsi seids
10744 * to be zero, in order to create a floating VEB.
10745 *
10746 * Returns pointer to the successfully allocated VEB sw struct on
10747 * success, otherwise returns NULL on failure.
10748 **/
10749struct i40e_veb *i40e_veb_setup(struct i40e_pf *pf, u16 flags,
10750 u16 uplink_seid, u16 vsi_seid,
10751 u8 enabled_tc)
10752{
10753 struct i40e_veb *veb, *uplink_veb = NULL;
10754 int vsi_idx, veb_idx;
10755 int ret;
10756
10757 /* if one seid is 0, the other must be 0 to create a floating relay */
10758 if ((uplink_seid == 0 || vsi_seid == 0) &&
10759 (uplink_seid + vsi_seid != 0)) {
10760 dev_info(&pf->pdev->dev,
10761 "one, not both seid's are 0: uplink=%d vsi=%d\n",
10762 uplink_seid, vsi_seid);
10763 return NULL;
10764 }
10765
10766 /* make sure there is such a vsi and uplink */
Mitch Williams505682c2014-05-20 08:01:37 +000010767 for (vsi_idx = 0; vsi_idx < pf->num_alloc_vsi; vsi_idx++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010768 if (pf->vsi[vsi_idx] && pf->vsi[vsi_idx]->seid == vsi_seid)
10769 break;
Mitch Williams505682c2014-05-20 08:01:37 +000010770 if (vsi_idx >= pf->num_alloc_vsi && vsi_seid != 0) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010771 dev_info(&pf->pdev->dev, "vsi seid %d not found\n",
10772 vsi_seid);
10773 return NULL;
10774 }
10775
10776 if (uplink_seid && uplink_seid != pf->mac_seid) {
10777 for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
10778 if (pf->veb[veb_idx] &&
10779 pf->veb[veb_idx]->seid == uplink_seid) {
10780 uplink_veb = pf->veb[veb_idx];
10781 break;
10782 }
10783 }
10784 if (!uplink_veb) {
10785 dev_info(&pf->pdev->dev,
10786 "uplink seid %d not found\n", uplink_seid);
10787 return NULL;
10788 }
10789 }
10790
10791 /* get veb sw struct */
10792 veb_idx = i40e_veb_mem_alloc(pf);
10793 if (veb_idx < 0)
10794 goto err_alloc;
10795 veb = pf->veb[veb_idx];
10796 veb->flags = flags;
10797 veb->uplink_seid = uplink_seid;
10798 veb->veb_idx = (uplink_veb ? uplink_veb->idx : I40E_NO_VEB);
10799 veb->enabled_tc = (enabled_tc ? enabled_tc : 0x1);
10800
10801 /* create the VEB in the switch */
10802 ret = i40e_add_veb(veb, pf->vsi[vsi_idx]);
10803 if (ret)
10804 goto err_veb;
Shannon Nelson1bb8b932014-04-23 04:49:54 +000010805 if (vsi_idx == pf->lan_vsi)
10806 pf->lan_veb = veb->idx;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010807
10808 return veb;
10809
10810err_veb:
10811 i40e_veb_clear(veb);
10812err_alloc:
10813 return NULL;
10814}
10815
10816/**
Jeff Kirsherb40c82e62015-02-27 09:18:34 +000010817 * i40e_setup_pf_switch_element - set PF vars based on switch type
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010818 * @pf: board private structure
10819 * @ele: element we are building info from
10820 * @num_reported: total number of elements
10821 * @printconfig: should we print the contents
10822 *
10823 * helper function to assist in extracting a few useful SEID values.
10824 **/
10825static void i40e_setup_pf_switch_element(struct i40e_pf *pf,
10826 struct i40e_aqc_switch_config_element_resp *ele,
10827 u16 num_reported, bool printconfig)
10828{
10829 u16 downlink_seid = le16_to_cpu(ele->downlink_seid);
10830 u16 uplink_seid = le16_to_cpu(ele->uplink_seid);
10831 u8 element_type = ele->element_type;
10832 u16 seid = le16_to_cpu(ele->seid);
10833
10834 if (printconfig)
10835 dev_info(&pf->pdev->dev,
10836 "type=%d seid=%d uplink=%d downlink=%d\n",
10837 element_type, seid, uplink_seid, downlink_seid);
10838
10839 switch (element_type) {
10840 case I40E_SWITCH_ELEMENT_TYPE_MAC:
10841 pf->mac_seid = seid;
10842 break;
10843 case I40E_SWITCH_ELEMENT_TYPE_VEB:
10844 /* Main VEB? */
10845 if (uplink_seid != pf->mac_seid)
10846 break;
10847 if (pf->lan_veb == I40E_NO_VEB) {
10848 int v;
10849
10850 /* find existing or else empty VEB */
10851 for (v = 0; v < I40E_MAX_VEB; v++) {
10852 if (pf->veb[v] && (pf->veb[v]->seid == seid)) {
10853 pf->lan_veb = v;
10854 break;
10855 }
10856 }
10857 if (pf->lan_veb == I40E_NO_VEB) {
10858 v = i40e_veb_mem_alloc(pf);
10859 if (v < 0)
10860 break;
10861 pf->lan_veb = v;
10862 }
10863 }
10864
10865 pf->veb[pf->lan_veb]->seid = seid;
10866 pf->veb[pf->lan_veb]->uplink_seid = pf->mac_seid;
10867 pf->veb[pf->lan_veb]->pf = pf;
10868 pf->veb[pf->lan_veb]->veb_idx = I40E_NO_VEB;
10869 break;
10870 case I40E_SWITCH_ELEMENT_TYPE_VSI:
10871 if (num_reported != 1)
10872 break;
10873 /* This is immediately after a reset so we can assume this is
10874 * the PF's VSI
10875 */
10876 pf->mac_seid = uplink_seid;
10877 pf->pf_seid = downlink_seid;
10878 pf->main_vsi_seid = seid;
10879 if (printconfig)
10880 dev_info(&pf->pdev->dev,
10881 "pf_seid=%d main_vsi_seid=%d\n",
10882 pf->pf_seid, pf->main_vsi_seid);
10883 break;
10884 case I40E_SWITCH_ELEMENT_TYPE_PF:
10885 case I40E_SWITCH_ELEMENT_TYPE_VF:
10886 case I40E_SWITCH_ELEMENT_TYPE_EMP:
10887 case I40E_SWITCH_ELEMENT_TYPE_BMC:
10888 case I40E_SWITCH_ELEMENT_TYPE_PE:
10889 case I40E_SWITCH_ELEMENT_TYPE_PA:
10890 /* ignore these for now */
10891 break;
10892 default:
10893 dev_info(&pf->pdev->dev, "unknown element type=%d seid=%d\n",
10894 element_type, seid);
10895 break;
10896 }
10897}
10898
10899/**
10900 * i40e_fetch_switch_configuration - Get switch config from firmware
10901 * @pf: board private structure
10902 * @printconfig: should we print the contents
10903 *
10904 * Get the current switch configuration from the device and
10905 * extract a few useful SEID values.
10906 **/
10907int i40e_fetch_switch_configuration(struct i40e_pf *pf, bool printconfig)
10908{
10909 struct i40e_aqc_get_switch_config_resp *sw_config;
10910 u16 next_seid = 0;
10911 int ret = 0;
10912 u8 *aq_buf;
10913 int i;
10914
10915 aq_buf = kzalloc(I40E_AQ_LARGE_BUF, GFP_KERNEL);
10916 if (!aq_buf)
10917 return -ENOMEM;
10918
10919 sw_config = (struct i40e_aqc_get_switch_config_resp *)aq_buf;
10920 do {
10921 u16 num_reported, num_total;
10922
10923 ret = i40e_aq_get_switch_config(&pf->hw, sw_config,
10924 I40E_AQ_LARGE_BUF,
10925 &next_seid, NULL);
10926 if (ret) {
10927 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010928 "get switch config failed err %s aq_err %s\n",
10929 i40e_stat_str(&pf->hw, ret),
10930 i40e_aq_str(&pf->hw,
10931 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010932 kfree(aq_buf);
10933 return -ENOENT;
10934 }
10935
10936 num_reported = le16_to_cpu(sw_config->header.num_reported);
10937 num_total = le16_to_cpu(sw_config->header.num_total);
10938
10939 if (printconfig)
10940 dev_info(&pf->pdev->dev,
10941 "header: %d reported %d total\n",
10942 num_reported, num_total);
10943
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010944 for (i = 0; i < num_reported; i++) {
10945 struct i40e_aqc_switch_config_element_resp *ele =
10946 &sw_config->element[i];
10947
10948 i40e_setup_pf_switch_element(pf, ele, num_reported,
10949 printconfig);
10950 }
10951 } while (next_seid != 0);
10952
10953 kfree(aq_buf);
10954 return ret;
10955}
10956
10957/**
10958 * i40e_setup_pf_switch - Setup the HW switch on startup or after reset
10959 * @pf: board private structure
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010960 * @reinit: if the Main VSI needs to re-initialized.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010961 *
10962 * Returns 0 on success, negative value on failure
10963 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010964static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010965{
Anjali Singhai Jainb5569892016-05-03 15:13:12 -070010966 u16 flags = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010967 int ret;
10968
10969 /* find out what's out there already */
10970 ret = i40e_fetch_switch_configuration(pf, false);
10971 if (ret) {
10972 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010973 "couldn't fetch switch config, err %s aq_err %s\n",
10974 i40e_stat_str(&pf->hw, ret),
10975 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010976 return ret;
10977 }
10978 i40e_pf_reset_stats(pf);
10979
Anjali Singhai Jainb5569892016-05-03 15:13:12 -070010980 /* set the switch config bit for the whole device to
10981 * support limited promisc or true promisc
10982 * when user requests promisc. The default is limited
10983 * promisc.
10984 */
10985
10986 if ((pf->hw.pf_id == 0) &&
10987 !(pf->flags & I40E_FLAG_TRUE_PROMISC_SUPPORT))
10988 flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
10989
10990 if (pf->hw.pf_id == 0) {
10991 u16 valid_flags;
10992
10993 valid_flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
10994 ret = i40e_aq_set_switch_config(&pf->hw, flags, valid_flags,
10995 NULL);
10996 if (ret && pf->hw.aq.asq_last_status != I40E_AQ_RC_ESRCH) {
10997 dev_info(&pf->pdev->dev,
10998 "couldn't set switch config bits, err %s aq_err %s\n",
10999 i40e_stat_str(&pf->hw, ret),
11000 i40e_aq_str(&pf->hw,
11001 pf->hw.aq.asq_last_status));
11002 /* not a fatal problem, just keep going */
11003 }
11004 }
11005
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011006 /* first time setup */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000011007 if (pf->lan_vsi == I40E_NO_VSI || reinit) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011008 struct i40e_vsi *vsi = NULL;
11009 u16 uplink_seid;
11010
11011 /* Set up the PF VSI associated with the PF's main VSI
11012 * that is already in the HW switch
11013 */
11014 if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
11015 uplink_seid = pf->veb[pf->lan_veb]->seid;
11016 else
11017 uplink_seid = pf->mac_seid;
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000011018 if (pf->lan_vsi == I40E_NO_VSI)
11019 vsi = i40e_vsi_setup(pf, I40E_VSI_MAIN, uplink_seid, 0);
11020 else if (reinit)
11021 vsi = i40e_vsi_reinit_setup(pf->vsi[pf->lan_vsi]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011022 if (!vsi) {
11023 dev_info(&pf->pdev->dev, "setup of MAIN VSI failed\n");
11024 i40e_fdir_teardown(pf);
11025 return -EAGAIN;
11026 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011027 } else {
11028 /* force a reset of TC and queue layout configurations */
11029 u8 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
Jesse Brandeburg6995b362015-08-28 17:55:54 -040011030
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011031 pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
11032 pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
11033 i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
11034 }
11035 i40e_vlan_stripping_disable(pf->vsi[pf->lan_vsi]);
11036
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080011037 i40e_fdir_sb_setup(pf);
11038
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011039 /* Setup static PF queue filter control settings */
11040 ret = i40e_setup_pf_filter_control(pf);
11041 if (ret) {
11042 dev_info(&pf->pdev->dev, "setup_pf_filter_control failed: %d\n",
11043 ret);
11044 /* Failure here should not stop continuing other steps */
11045 }
11046
11047 /* enable RSS in the HW, even for only one queue, as the stack can use
11048 * the hash
11049 */
11050 if ((pf->flags & I40E_FLAG_RSS_ENABLED))
Helin Zhang043dd652015-10-21 19:56:23 -040011051 i40e_pf_config_rss(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011052
11053 /* fill in link information and enable LSE reporting */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011054 i40e_link_event(pf);
11055
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +000011056 /* Initialize user-specific link properties */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011057 pf->fc_autoneg_status = ((pf->hw.phy.link_info.an_info &
11058 I40E_AQ_AN_COMPLETED) ? true : false);
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +000011059
Jacob Kellerbeb0dff2014-01-11 05:43:19 +000011060 i40e_ptp_init(pf);
11061
Alexander Duyck1f190d92017-04-19 09:25:51 -040011062 /* repopulate tunnel port filters */
11063 i40e_sync_udp_filters(pf);
11064
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011065 return ret;
11066}
11067
11068/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011069 * i40e_determine_queue_usage - Work out queue distribution
11070 * @pf: board private structure
11071 **/
11072static void i40e_determine_queue_usage(struct i40e_pf *pf)
11073{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011074 int queues_left;
11075
11076 pf->num_lan_qps = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011077
11078 /* Find the max queues to be put into basic use. We'll always be
11079 * using TC0, whether or not DCB is running, and TC0 will get the
11080 * big RSS set.
11081 */
11082 queues_left = pf->hw.func_caps.num_tx_qp;
11083
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080011084 if ((queues_left == 1) ||
Frank Zhang9aa7e932014-05-20 08:01:42 +000011085 !(pf->flags & I40E_FLAG_MSIX_ENABLED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011086 /* one qp for PF, no queues for anything else */
11087 queues_left = 0;
Helin Zhangacd65442015-10-26 19:44:28 -040011088 pf->alloc_rss_size = pf->num_lan_qps = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011089
11090 /* make sure all the fancies are disabled */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080011091 pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060011092 I40E_FLAG_IWARP_ENABLED |
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080011093 I40E_FLAG_FD_SB_ENABLED |
11094 I40E_FLAG_FD_ATR_ENABLED |
Neerav Parikh4d9b6042014-05-22 06:31:51 +000011095 I40E_FLAG_DCB_CAPABLE |
Dave Ertmana0362442016-08-29 17:38:26 -070011096 I40E_FLAG_DCB_ENABLED |
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080011097 I40E_FLAG_SRIOV_ENABLED |
11098 I40E_FLAG_VMDQ_ENABLED);
Frank Zhang9aa7e932014-05-20 08:01:42 +000011099 } else if (!(pf->flags & (I40E_FLAG_RSS_ENABLED |
11100 I40E_FLAG_FD_SB_ENABLED |
Anjali Singhai Jainbbe7d0e2014-05-20 08:01:44 +000011101 I40E_FLAG_FD_ATR_ENABLED |
Neerav Parikh4d9b6042014-05-22 06:31:51 +000011102 I40E_FLAG_DCB_CAPABLE))) {
Frank Zhang9aa7e932014-05-20 08:01:42 +000011103 /* one qp for PF */
Helin Zhangacd65442015-10-26 19:44:28 -040011104 pf->alloc_rss_size = pf->num_lan_qps = 1;
Frank Zhang9aa7e932014-05-20 08:01:42 +000011105 queues_left -= pf->num_lan_qps;
11106
11107 pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060011108 I40E_FLAG_IWARP_ENABLED |
Frank Zhang9aa7e932014-05-20 08:01:42 +000011109 I40E_FLAG_FD_SB_ENABLED |
11110 I40E_FLAG_FD_ATR_ENABLED |
11111 I40E_FLAG_DCB_ENABLED |
11112 I40E_FLAG_VMDQ_ENABLED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011113 } else {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080011114 /* Not enough queues for all TCs */
Neerav Parikh4d9b6042014-05-22 06:31:51 +000011115 if ((pf->flags & I40E_FLAG_DCB_CAPABLE) &&
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080011116 (queues_left < I40E_MAX_TRAFFIC_CLASS)) {
Dave Ertmana0362442016-08-29 17:38:26 -070011117 pf->flags &= ~(I40E_FLAG_DCB_CAPABLE |
11118 I40E_FLAG_DCB_ENABLED);
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080011119 dev_info(&pf->pdev->dev, "not enough queues for DCB. DCB is disabled.\n");
11120 }
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +000011121 pf->num_lan_qps = max_t(int, pf->rss_size_max,
11122 num_online_cpus());
11123 pf->num_lan_qps = min_t(int, pf->num_lan_qps,
11124 pf->hw.func_caps.num_tx_qp);
11125
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080011126 queues_left -= pf->num_lan_qps;
11127 }
11128
11129 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
11130 if (queues_left > 1) {
11131 queues_left -= 1; /* save 1 queue for FD */
11132 } else {
11133 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
11134 dev_info(&pf->pdev->dev, "not enough queues for Flow Director. Flow Director feature is disabled\n");
11135 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011136 }
11137
11138 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
11139 pf->num_vf_qps && pf->num_req_vfs && queues_left) {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080011140 pf->num_req_vfs = min_t(int, pf->num_req_vfs,
11141 (queues_left / pf->num_vf_qps));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011142 queues_left -= (pf->num_req_vfs * pf->num_vf_qps);
11143 }
11144
11145 if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
11146 pf->num_vmdq_vsis && pf->num_vmdq_qps && queues_left) {
11147 pf->num_vmdq_vsis = min_t(int, pf->num_vmdq_vsis,
11148 (queues_left / pf->num_vmdq_qps));
11149 queues_left -= (pf->num_vmdq_vsis * pf->num_vmdq_qps);
11150 }
11151
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +000011152 pf->queues_left = queues_left;
Neerav Parikh8279e492015-09-03 17:18:50 -040011153 dev_dbg(&pf->pdev->dev,
11154 "qs_avail=%d FD SB=%d lan_qs=%d lan_tc0=%d vf=%d*%d vmdq=%d*%d, remaining=%d\n",
11155 pf->hw.func_caps.num_tx_qp,
11156 !!(pf->flags & I40E_FLAG_FD_SB_ENABLED),
Helin Zhangacd65442015-10-26 19:44:28 -040011157 pf->num_lan_qps, pf->alloc_rss_size, pf->num_req_vfs,
11158 pf->num_vf_qps, pf->num_vmdq_vsis, pf->num_vmdq_qps,
11159 queues_left);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011160}
11161
11162/**
11163 * i40e_setup_pf_filter_control - Setup PF static filter control
11164 * @pf: PF to be setup
11165 *
Jeff Kirsherb40c82e62015-02-27 09:18:34 +000011166 * i40e_setup_pf_filter_control sets up a PF's initial filter control
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011167 * settings. If PE/FCoE are enabled then it will also set the per PF
11168 * based filter sizes required for them. It also enables Flow director,
11169 * ethertype and macvlan type filter settings for the pf.
11170 *
11171 * Returns 0 on success, negative on failure
11172 **/
11173static int i40e_setup_pf_filter_control(struct i40e_pf *pf)
11174{
11175 struct i40e_filter_control_settings *settings = &pf->filter_settings;
11176
11177 settings->hash_lut_size = I40E_HASH_LUT_SIZE_128;
11178
11179 /* Flow Director is enabled */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080011180 if (pf->flags & (I40E_FLAG_FD_SB_ENABLED | I40E_FLAG_FD_ATR_ENABLED))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011181 settings->enable_fdir = true;
11182
11183 /* Ethtype and MACVLAN filters enabled for PF */
11184 settings->enable_ethtype = true;
11185 settings->enable_macvlan = true;
11186
11187 if (i40e_set_filter_control(&pf->hw, settings))
11188 return -ENOENT;
11189
11190 return 0;
11191}
11192
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000011193#define INFO_STRING_LEN 255
Shannon Nelson7fd89542015-10-21 19:47:04 -040011194#define REMAIN(__x) (INFO_STRING_LEN - (__x))
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000011195static void i40e_print_features(struct i40e_pf *pf)
11196{
11197 struct i40e_hw *hw = &pf->hw;
Joe Perches3b195842015-12-03 04:20:57 -080011198 char *buf;
11199 int i;
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000011200
Joe Perches3b195842015-12-03 04:20:57 -080011201 buf = kmalloc(INFO_STRING_LEN, GFP_KERNEL);
11202 if (!buf)
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000011203 return;
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000011204
Joe Perches3b195842015-12-03 04:20:57 -080011205 i = snprintf(buf, INFO_STRING_LEN, "Features: PF-id[%d]", hw->pf_id);
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000011206#ifdef CONFIG_PCI_IOV
Joe Perches3b195842015-12-03 04:20:57 -080011207 i += snprintf(&buf[i], REMAIN(i), " VFs: %d", pf->num_req_vfs);
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000011208#endif
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -070011209 i += snprintf(&buf[i], REMAIN(i), " VSIs: %d QP: %d",
Shannon Nelson7fd89542015-10-21 19:47:04 -040011210 pf->hw.func_caps.num_vsis,
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -070011211 pf->vsi[pf->lan_vsi]->num_queue_pairs);
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000011212 if (pf->flags & I40E_FLAG_RSS_ENABLED)
Joe Perches3b195842015-12-03 04:20:57 -080011213 i += snprintf(&buf[i], REMAIN(i), " RSS");
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000011214 if (pf->flags & I40E_FLAG_FD_ATR_ENABLED)
Joe Perches3b195842015-12-03 04:20:57 -080011215 i += snprintf(&buf[i], REMAIN(i), " FD_ATR");
Akeem G Abodunrinc6423ff2014-05-10 04:49:08 +000011216 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
Joe Perches3b195842015-12-03 04:20:57 -080011217 i += snprintf(&buf[i], REMAIN(i), " FD_SB");
11218 i += snprintf(&buf[i], REMAIN(i), " NTUPLE");
Akeem G Abodunrinc6423ff2014-05-10 04:49:08 +000011219 }
Neerav Parikh4d9b6042014-05-22 06:31:51 +000011220 if (pf->flags & I40E_FLAG_DCB_CAPABLE)
Joe Perches3b195842015-12-03 04:20:57 -080011221 i += snprintf(&buf[i], REMAIN(i), " DCB");
Joe Perches3b195842015-12-03 04:20:57 -080011222 i += snprintf(&buf[i], REMAIN(i), " VxLAN");
Singhai, Anjali6a899022015-12-14 12:21:18 -080011223 i += snprintf(&buf[i], REMAIN(i), " Geneve");
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000011224 if (pf->flags & I40E_FLAG_PTP)
Joe Perches3b195842015-12-03 04:20:57 -080011225 i += snprintf(&buf[i], REMAIN(i), " PTP");
Shannon Nelson6dec1012015-09-28 14:12:30 -040011226 if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
Joe Perches3b195842015-12-03 04:20:57 -080011227 i += snprintf(&buf[i], REMAIN(i), " VEB");
Shannon Nelson6dec1012015-09-28 14:12:30 -040011228 else
Joe Perches3b195842015-12-03 04:20:57 -080011229 i += snprintf(&buf[i], REMAIN(i), " VEPA");
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000011230
Joe Perches3b195842015-12-03 04:20:57 -080011231 dev_info(&pf->pdev->dev, "%s\n", buf);
11232 kfree(buf);
Shannon Nelson7fd89542015-10-21 19:47:04 -040011233 WARN_ON(i > INFO_STRING_LEN);
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000011234}
11235
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011236/**
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050011237 * i40e_get_platform_mac_addr - get platform-specific MAC address
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050011238 * @pdev: PCI device information struct
11239 * @pf: board private structure
11240 *
Jacob Keller41c4c2b2017-04-05 07:50:57 -040011241 * Look up the MAC address for the device. First we'll try
11242 * eth_platform_get_mac_address, which will check Open Firmware, or arch
11243 * specific fallback. Otherwise, we'll default to the stored value in
11244 * firmware.
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050011245 **/
11246static void i40e_get_platform_mac_addr(struct pci_dev *pdev, struct i40e_pf *pf)
11247{
Jacob Keller41c4c2b2017-04-05 07:50:57 -040011248 if (eth_platform_get_mac_address(&pdev->dev, pf->hw.mac.addr))
11249 i40e_get_mac_addr(&pf->hw, pf->hw.mac.addr);
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050011250}
11251
11252/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011253 * i40e_probe - Device initialization routine
11254 * @pdev: PCI device information struct
11255 * @ent: entry in i40e_pci_tbl
11256 *
Jeff Kirsherb40c82e62015-02-27 09:18:34 +000011257 * i40e_probe initializes a PF identified by a pci_dev structure.
11258 * The OS initialization, configuring of the PF private structure,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011259 * and a hardware reset occur.
11260 *
11261 * Returns 0 on success, negative on failure
11262 **/
11263static int i40e_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
11264{
Catherine Sullivane8278452015-02-06 08:52:08 +000011265 struct i40e_aq_get_phy_abilities_resp abilities;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011266 struct i40e_pf *pf;
11267 struct i40e_hw *hw;
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +000011268 static u16 pfs_found;
Shannon Nelson1d5109d2015-08-26 15:14:08 -040011269 u16 wol_nvm_bits;
Catherine Sullivand4dfb812013-11-28 06:39:21 +000011270 u16 link_status;
Jean Sacren6f66a482015-09-19 05:08:45 -060011271 int err;
Anjali Singhai Jain4f2f017c2015-10-21 19:47:07 -040011272 u32 val;
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +000011273 u32 i;
Helin Zhang58fc3262015-10-01 14:37:38 -040011274 u8 set_fc_aq_fail;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011275
11276 err = pci_enable_device_mem(pdev);
11277 if (err)
11278 return err;
11279
11280 /* set up for high or low dma */
Mitch Williams64942942014-02-11 08:26:33 +000011281 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
Mitch Williams64942942014-02-11 08:26:33 +000011282 if (err) {
Jean Sacrene3e3bfd2014-03-25 04:30:27 +000011283 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
11284 if (err) {
11285 dev_err(&pdev->dev,
11286 "DMA configuration failed: 0x%x\n", err);
11287 goto err_dma;
11288 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011289 }
11290
11291 /* set up pci connections */
Johannes Thumshirn56d766d2016-06-07 09:44:05 +020011292 err = pci_request_mem_regions(pdev, i40e_driver_name);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011293 if (err) {
11294 dev_info(&pdev->dev,
11295 "pci_request_selected_regions failed %d\n", err);
11296 goto err_pci_reg;
11297 }
11298
11299 pci_enable_pcie_error_reporting(pdev);
11300 pci_set_master(pdev);
11301
11302 /* Now that we have a PCI connection, we need to do the
11303 * low level device setup. This is primarily setting up
11304 * the Admin Queue structures and then querying for the
11305 * device's current profile information.
11306 */
11307 pf = kzalloc(sizeof(*pf), GFP_KERNEL);
11308 if (!pf) {
11309 err = -ENOMEM;
11310 goto err_pf_alloc;
11311 }
11312 pf->next_vsi = 0;
11313 pf->pdev = pdev;
Mauro S. M. Rodrigues9e6c9c02017-05-12 23:26:56 -030011314 set_bit(__I40E_DOWN, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011315
11316 hw = &pf->hw;
11317 hw->back = pf;
Anjali Singhai232f4702015-02-26 16:15:39 +000011318
Shannon Nelson2ac8b672015-07-23 16:54:37 -040011319 pf->ioremap_len = min_t(int, pci_resource_len(pdev, 0),
11320 I40E_MAX_CSR_SPACE);
Anjali Singhai232f4702015-02-26 16:15:39 +000011321
Shannon Nelson2ac8b672015-07-23 16:54:37 -040011322 hw->hw_addr = ioremap(pci_resource_start(pdev, 0), pf->ioremap_len);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011323 if (!hw->hw_addr) {
11324 err = -EIO;
11325 dev_info(&pdev->dev, "ioremap(0x%04x, 0x%04x) failed: 0x%x\n",
11326 (unsigned int)pci_resource_start(pdev, 0),
Shannon Nelson2ac8b672015-07-23 16:54:37 -040011327 pf->ioremap_len, err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011328 goto err_ioremap;
11329 }
11330 hw->vendor_id = pdev->vendor;
11331 hw->device_id = pdev->device;
11332 pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
11333 hw->subsystem_vendor_id = pdev->subsystem_vendor;
11334 hw->subsystem_device_id = pdev->subsystem_device;
11335 hw->bus.device = PCI_SLOT(pdev->devfn);
11336 hw->bus.func = PCI_FUNC(pdev->devfn);
Sudheer Mogilappagarib3f028f2017-02-09 23:58:22 -080011337 hw->bus.bus_id = pdev->bus->number;
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +000011338 pf->instance = pfs_found;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011339
Jacob Keller0e588de2017-02-06 14:38:50 -080011340 INIT_LIST_HEAD(&pf->l3_flex_pit_list);
11341 INIT_LIST_HEAD(&pf->l4_flex_pit_list);
11342
Shannon Nelsonde03d2b2016-03-10 14:59:44 -080011343 /* set up the locks for the AQ, do this only once in probe
11344 * and destroy them only once in remove
11345 */
11346 mutex_init(&hw->aq.asq_mutex);
11347 mutex_init(&hw->aq.arq_mutex);
11348
Alexander Duyck5d4ca232016-09-30 08:21:46 -040011349 pf->msg_enable = netif_msg_init(debug,
11350 NETIF_MSG_DRV |
11351 NETIF_MSG_PROBE |
11352 NETIF_MSG_LINK);
11353 if (debug < -1)
11354 pf->hw.debug_mask = debug;
Shannon Nelson5b5faa42014-10-17 03:14:51 +000011355
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +000011356 /* do a special CORER for clearing PXE mode once at init */
11357 if (hw->revision_id == 0 &&
11358 (rd32(hw, I40E_GLLAN_RCTL_0) & I40E_GLLAN_RCTL_0_PXE_MODE_MASK)) {
11359 wr32(hw, I40E_GLGEN_RTRIG, I40E_GLGEN_RTRIG_CORER_MASK);
11360 i40e_flush(hw);
11361 msleep(200);
11362 pf->corer_count++;
11363
11364 i40e_clear_pxe_mode(hw);
11365 }
11366
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011367 /* Reset here to make sure all is clean and to define PF 'n' */
Shannon Nelson838d41d2014-06-04 20:41:27 +000011368 i40e_clear_hw(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011369 err = i40e_pf_reset(hw);
11370 if (err) {
11371 dev_info(&pdev->dev, "Initial pf_reset failed: %d\n", err);
11372 goto err_pf_reset;
11373 }
11374 pf->pfr_count++;
11375
11376 hw->aq.num_arq_entries = I40E_AQ_LEN;
11377 hw->aq.num_asq_entries = I40E_AQ_LEN;
11378 hw->aq.arq_buf_size = I40E_MAX_AQ_BUF_SIZE;
11379 hw->aq.asq_buf_size = I40E_MAX_AQ_BUF_SIZE;
11380 pf->adminq_work_limit = I40E_AQ_WORK_LIMIT;
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +000011381
Carolyn Wybornyb294ac72014-12-11 07:06:39 +000011382 snprintf(pf->int_name, sizeof(pf->int_name) - 1,
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +000011383 "%s-%s:misc",
11384 dev_driver_string(&pf->pdev->dev), dev_name(&pdev->dev));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011385
11386 err = i40e_init_shared_code(hw);
11387 if (err) {
Anjali Singhai Jainb2a75c52015-04-27 14:57:20 -040011388 dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
11389 err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011390 goto err_pf_reset;
11391 }
11392
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +000011393 /* set up a default setting for link flow control */
11394 pf->hw.fc.requested_mode = I40E_FC_NONE;
11395
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011396 err = i40e_init_adminq(hw);
Carolyn Wyborny2b2426a762015-10-26 19:44:35 -040011397 if (err) {
11398 if (err == I40E_ERR_FIRMWARE_API_VERSION)
11399 dev_info(&pdev->dev,
11400 "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");
11401 else
11402 dev_info(&pdev->dev,
11403 "The driver for the device stopped because the device firmware failed to init. Try updating your NVM image.\n");
11404
11405 goto err_pf_reset;
11406 }
Filip Sadowski5bbb2e22017-06-07 05:43:09 -040011407 i40e_get_oem_version(hw);
Carolyn Wybornyf0b44442015-08-31 19:54:49 -040011408
Shannon Nelson6dec1012015-09-28 14:12:30 -040011409 /* provide nvm, fw, api versions */
11410 dev_info(&pdev->dev, "fw %d.%d.%05d api %d.%d nvm %s\n",
11411 hw->aq.fw_maj_ver, hw->aq.fw_min_ver, hw->aq.fw_build,
11412 hw->aq.api_maj_ver, hw->aq.api_min_ver,
11413 i40e_nvm_version_str(hw));
Carolyn Wybornyf0b44442015-08-31 19:54:49 -040011414
Catherine Sullivan7aa67612014-07-09 07:46:17 +000011415 if (hw->aq.api_maj_ver == I40E_FW_API_VERSION_MAJOR &&
11416 hw->aq.api_min_ver > I40E_FW_API_VERSION_MINOR)
Shannon Nelson278b6f62014-06-04 01:41:03 +000011417 dev_info(&pdev->dev,
Catherine Sullivan7aa67612014-07-09 07:46:17 +000011418 "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");
11419 else if (hw->aq.api_maj_ver < I40E_FW_API_VERSION_MAJOR ||
11420 hw->aq.api_min_ver < (I40E_FW_API_VERSION_MINOR - 1))
Shannon Nelson278b6f62014-06-04 01:41:03 +000011421 dev_info(&pdev->dev,
Catherine Sullivan7aa67612014-07-09 07:46:17 +000011422 "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 +000011423
Shannon Nelson4eb3f762014-03-06 08:59:58 +000011424 i40e_verify_eeprom(pf);
11425
Jesse Brandeburg2c5fe332014-04-23 04:49:57 +000011426 /* Rev 0 hardware was never productized */
11427 if (hw->revision_id < 1)
11428 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");
11429
Shannon Nelson6ff4ef82013-12-21 05:44:49 +000011430 i40e_clear_pxe_mode(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011431 err = i40e_get_capabilities(pf);
11432 if (err)
11433 goto err_adminq_setup;
11434
11435 err = i40e_sw_init(pf);
11436 if (err) {
11437 dev_info(&pdev->dev, "sw_init failed: %d\n", err);
11438 goto err_sw_init;
11439 }
11440
11441 err = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
Alexander Duyckc76cb6e2017-02-21 15:55:48 -080011442 hw->func_caps.num_rx_qp, 0, 0);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011443 if (err) {
11444 dev_info(&pdev->dev, "init_lan_hmc failed: %d\n", err);
11445 goto err_init_lan_hmc;
11446 }
11447
11448 err = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
11449 if (err) {
11450 dev_info(&pdev->dev, "configure_lan_hmc failed: %d\n", err);
11451 err = -ENOENT;
11452 goto err_configure_lan_hmc;
11453 }
11454
Neerav Parikhb686ece2014-12-14 01:55:11 +000011455 /* Disable LLDP for NICs that have firmware versions lower than v4.3.
11456 * Ignore error return codes because if it was already disabled via
11457 * hardware settings this will fail
11458 */
Neerav Parikhf1bbad32016-01-13 16:51:39 -080011459 if (pf->flags & I40E_FLAG_STOP_FW_LLDP) {
Neerav Parikhb686ece2014-12-14 01:55:11 +000011460 dev_info(&pdev->dev, "Stopping firmware LLDP agent.\n");
11461 i40e_aq_stop_lldp(hw, true, NULL);
11462 }
11463
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050011464 /* allow a platform config to override the HW addr */
11465 i40e_get_platform_mac_addr(pdev, pf);
Jacob Keller41c4c2b2017-04-05 07:50:57 -040011466
Jesse Brandeburgf62b5062013-11-28 06:39:27 +000011467 if (!is_valid_ether_addr(hw->mac.addr)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011468 dev_info(&pdev->dev, "invalid MAC address %pM\n", hw->mac.addr);
11469 err = -EIO;
11470 goto err_mac_addr;
11471 }
11472 dev_info(&pdev->dev, "MAC address: %pM\n", hw->mac.addr);
Greg Rose9a173902014-05-22 06:32:02 +000011473 ether_addr_copy(hw->mac.perm_addr, hw->mac.addr);
Neerav Parikh1f224ad2014-02-12 01:45:31 +000011474 i40e_get_port_mac_addr(hw, hw->mac.port_addr);
11475 if (is_valid_ether_addr(hw->mac.port_addr))
11476 pf->flags |= I40E_FLAG_PORT_ID_VALID;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011477
11478 pci_set_drvdata(pdev, pf);
11479 pci_save_state(pdev);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -080011480#ifdef CONFIG_I40E_DCB
11481 err = i40e_init_pf_dcb(pf);
11482 if (err) {
Shannon Nelsonaebfc812014-12-11 07:06:38 +000011483 dev_info(&pdev->dev, "DCB init failed %d, disabled\n", err);
David Ertmanc17ef432016-09-30 01:36:21 -070011484 pf->flags &= ~(I40E_FLAG_DCB_CAPABLE | I40E_FLAG_DCB_ENABLED);
Neerav Parikh014269f2014-04-01 07:11:48 +000011485 /* Continue without DCB enabled */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -080011486 }
11487#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011488
11489 /* set up periodic task facility */
11490 setup_timer(&pf->service_timer, i40e_service_timer, (unsigned long)pf);
11491 pf->service_timer_period = HZ;
11492
11493 INIT_WORK(&pf->service_task, i40e_service_task);
Jacob Keller0da36b92017-04-19 09:25:55 -040011494 clear_bit(__I40E_SERVICE_SCHED, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011495
Shannon Nelson1d5109d2015-08-26 15:14:08 -040011496 /* NVM bit on means WoL disabled for the port */
11497 i40e_read_nvm_word(hw, I40E_SR_NVM_WAKE_ON_LAN, &wol_nvm_bits);
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -080011498 if (BIT (hw->port) & wol_nvm_bits || hw->partition_id != 1)
Shannon Nelson1d5109d2015-08-26 15:14:08 -040011499 pf->wol_en = false;
11500 else
11501 pf->wol_en = true;
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011502 device_set_wakeup_enable(&pf->pdev->dev, pf->wol_en);
11503
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011504 /* set up the main switch operations */
11505 i40e_determine_queue_usage(pf);
Jesse Brandeburgc11472802015-04-07 19:45:39 -040011506 err = i40e_init_interrupt_scheme(pf);
11507 if (err)
11508 goto err_switch_setup;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011509
Mitch Williams505682c2014-05-20 08:01:37 +000011510 /* The number of VSIs reported by the FW is the minimum guaranteed
11511 * to us; HW supports far more and we share the remaining pool with
11512 * the other PFs. We allocate space for more than the guarantee with
11513 * the understanding that we might not get them all later.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011514 */
Mitch Williams505682c2014-05-20 08:01:37 +000011515 if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
11516 pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
11517 else
11518 pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
11519
11520 /* Set up the *vsi struct and our local tracking of the MAIN PF vsi. */
Jesse Brandeburgd17038d2015-12-23 12:05:55 -080011521 pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
11522 GFP_KERNEL);
Wei Yongjuned87ac02013-09-24 05:17:25 +000011523 if (!pf->vsi) {
11524 err = -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011525 goto err_switch_setup;
Wei Yongjuned87ac02013-09-24 05:17:25 +000011526 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011527
Anjali Singhai Jainfa11cb32015-05-27 12:06:14 -040011528#ifdef CONFIG_PCI_IOV
11529 /* prep for VF support */
11530 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
11531 (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
Jacob Keller0da36b92017-04-19 09:25:55 -040011532 !test_bit(__I40E_BAD_EEPROM, pf->state)) {
Anjali Singhai Jainfa11cb32015-05-27 12:06:14 -040011533 if (pci_num_vf(pdev))
11534 pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
11535 }
11536#endif
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000011537 err = i40e_setup_pf_switch(pf, false);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011538 if (err) {
11539 dev_info(&pdev->dev, "setup_pf_switch failed: %d\n", err);
11540 goto err_vsis;
11541 }
Helin Zhang58fc3262015-10-01 14:37:38 -040011542
11543 /* Make sure flow control is set according to current settings */
11544 err = i40e_set_fc(hw, &set_fc_aq_fail, true);
11545 if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_GET)
11546 dev_dbg(&pf->pdev->dev,
11547 "Set fc with err %s aq_err %s on get_phy_cap\n",
11548 i40e_stat_str(hw, err),
11549 i40e_aq_str(hw, hw->aq.asq_last_status));
11550 if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_SET)
11551 dev_dbg(&pf->pdev->dev,
11552 "Set fc with err %s aq_err %s on set_phy_config\n",
11553 i40e_stat_str(hw, err),
11554 i40e_aq_str(hw, hw->aq.asq_last_status));
11555 if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_UPDATE)
11556 dev_dbg(&pf->pdev->dev,
11557 "Set fc with err %s aq_err %s on get_link_info\n",
11558 i40e_stat_str(hw, err),
11559 i40e_aq_str(hw, hw->aq.asq_last_status));
11560
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +000011561 /* if FDIR VSI was set up, start it now */
Mitch Williams505682c2014-05-20 08:01:37 +000011562 for (i = 0; i < pf->num_alloc_vsi; i++) {
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +000011563 if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
11564 i40e_vsi_open(pf->vsi[i]);
11565 break;
11566 }
11567 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011568
Shannon Nelson2f0aff42016-01-04 10:33:08 -080011569 /* The driver only wants link up/down and module qualification
11570 * reports from firmware. Note the negative logic.
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +000011571 */
11572 err = i40e_aq_set_phy_int_mask(&pf->hw,
Shannon Nelson2f0aff42016-01-04 10:33:08 -080011573 ~(I40E_AQ_EVENT_LINK_UPDOWN |
Shannon Nelson867a79e2016-03-18 12:18:15 -070011574 I40E_AQ_EVENT_MEDIA_NA |
Shannon Nelson2f0aff42016-01-04 10:33:08 -080011575 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +000011576 if (err)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040011577 dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
11578 i40e_stat_str(&pf->hw, err),
11579 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +000011580
Anjali Singhai Jain4f2f017c2015-10-21 19:47:07 -040011581 /* Reconfigure hardware for allowing smaller MSS in the case
11582 * of TSO, so that we avoid the MDD being fired and causing
11583 * a reset in the case of small MSS+TSO.
11584 */
11585 val = rd32(hw, I40E_REG_MSS);
11586 if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
11587 val &= ~I40E_REG_MSS_MIN_MASK;
11588 val |= I40E_64BYTE_MSS;
11589 wr32(hw, I40E_REG_MSS, val);
11590 }
11591
Anjali Singhai Jain8eed76f2015-12-09 15:50:31 -080011592 if (pf->flags & I40E_FLAG_RESTART_AUTONEG) {
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +000011593 msleep(75);
11594 err = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
11595 if (err)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040011596 dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
11597 i40e_stat_str(&pf->hw, err),
11598 i40e_aq_str(&pf->hw,
11599 pf->hw.aq.asq_last_status));
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +000011600 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011601 /* The main driver is (mostly) up and happy. We need to set this state
11602 * before setting up the misc vector or we get a race and the vector
11603 * ends up disabled forever.
11604 */
Mauro S. M. Rodrigues9e6c9c02017-05-12 23:26:56 -030011605 clear_bit(__I40E_DOWN, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011606
11607 /* In case of MSIX we are going to setup the misc vector right here
11608 * to handle admin queue events etc. In case of legacy and MSI
11609 * the misc functionality and queue processing is combined in
11610 * the same vector and that gets setup at open.
11611 */
11612 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
11613 err = i40e_setup_misc_vector(pf);
11614 if (err) {
11615 dev_info(&pdev->dev,
11616 "setup of misc vector failed: %d\n", err);
11617 goto err_vsis;
11618 }
11619 }
11620
Greg Rosedf805f62014-04-04 04:43:16 +000011621#ifdef CONFIG_PCI_IOV
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011622 /* prep for VF support */
11623 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
Shannon Nelson4eb3f762014-03-06 08:59:58 +000011624 (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
Jacob Keller0da36b92017-04-19 09:25:55 -040011625 !test_bit(__I40E_BAD_EEPROM, pf->state)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011626 /* disable link interrupts for VFs */
11627 val = rd32(hw, I40E_PFGEN_PORTMDIO_NUM);
11628 val &= ~I40E_PFGEN_PORTMDIO_NUM_VFLINK_STAT_ENA_MASK;
11629 wr32(hw, I40E_PFGEN_PORTMDIO_NUM, val);
11630 i40e_flush(hw);
Mitch Williams4aeec012014-02-13 03:48:47 -080011631
11632 if (pci_num_vf(pdev)) {
11633 dev_info(&pdev->dev,
11634 "Active VFs found, allocating resources.\n");
11635 err = i40e_alloc_vfs(pf, pci_num_vf(pdev));
11636 if (err)
11637 dev_info(&pdev->dev,
11638 "Error %d allocating resources for existing VFs\n",
11639 err);
11640 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011641 }
Greg Rosedf805f62014-04-04 04:43:16 +000011642#endif /* CONFIG_PCI_IOV */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011643
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060011644 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11645 pf->iwarp_base_vector = i40e_get_lump(pf, pf->irq_pile,
11646 pf->num_iwarp_msix,
11647 I40E_IWARP_IRQ_PILE_ID);
11648 if (pf->iwarp_base_vector < 0) {
11649 dev_info(&pdev->dev,
11650 "failed to get tracking for %d vectors for IWARP err=%d\n",
11651 pf->num_iwarp_msix, pf->iwarp_base_vector);
11652 pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
11653 }
11654 }
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +000011655
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011656 i40e_dbg_pf_init(pf);
11657
11658 /* tell the firmware that we're starting */
Jesse Brandeburg44033fa2014-04-23 04:50:15 +000011659 i40e_send_version(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011660
11661 /* since everything's happy, start the service_task timer */
11662 mod_timer(&pf->service_timer,
11663 round_jiffies(jiffies + pf->service_timer_period));
11664
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060011665 /* add this PF to client device list and launch a client service task */
Mitch Williams004eb612017-04-04 12:40:16 -070011666 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11667 err = i40e_lan_add_device(pf);
11668 if (err)
11669 dev_info(&pdev->dev, "Failed to add PF to client API service list: %d\n",
11670 err);
11671 }
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060011672
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040011673#define PCI_SPEED_SIZE 8
11674#define PCI_WIDTH_SIZE 8
11675 /* Devices on the IOSF bus do not have this information
11676 * and will report PCI Gen 1 x 1 by default so don't bother
11677 * checking them.
11678 */
11679 if (!(pf->flags & I40E_FLAG_NO_PCI_LINK_CHECK)) {
11680 char speed[PCI_SPEED_SIZE] = "Unknown";
11681 char width[PCI_WIDTH_SIZE] = "Unknown";
Catherine Sullivand4dfb812013-11-28 06:39:21 +000011682
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040011683 /* Get the negotiated link width and speed from PCI config
11684 * space
11685 */
11686 pcie_capability_read_word(pf->pdev, PCI_EXP_LNKSTA,
11687 &link_status);
Catherine Sullivand4dfb812013-11-28 06:39:21 +000011688
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040011689 i40e_set_pci_config_data(hw, link_status);
Catherine Sullivand4dfb812013-11-28 06:39:21 +000011690
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040011691 switch (hw->bus.speed) {
11692 case i40e_bus_speed_8000:
11693 strncpy(speed, "8.0", PCI_SPEED_SIZE); break;
11694 case i40e_bus_speed_5000:
11695 strncpy(speed, "5.0", PCI_SPEED_SIZE); break;
11696 case i40e_bus_speed_2500:
11697 strncpy(speed, "2.5", PCI_SPEED_SIZE); break;
11698 default:
11699 break;
11700 }
11701 switch (hw->bus.width) {
11702 case i40e_bus_width_pcie_x8:
11703 strncpy(width, "8", PCI_WIDTH_SIZE); break;
11704 case i40e_bus_width_pcie_x4:
11705 strncpy(width, "4", PCI_WIDTH_SIZE); break;
11706 case i40e_bus_width_pcie_x2:
11707 strncpy(width, "2", PCI_WIDTH_SIZE); break;
11708 case i40e_bus_width_pcie_x1:
11709 strncpy(width, "1", PCI_WIDTH_SIZE); break;
11710 default:
11711 break;
11712 }
11713
11714 dev_info(&pdev->dev, "PCI-Express: Speed %sGT/s Width x%s\n",
11715 speed, width);
11716
11717 if (hw->bus.width < i40e_bus_width_pcie_x8 ||
11718 hw->bus.speed < i40e_bus_speed_8000) {
11719 dev_warn(&pdev->dev, "PCI-Express bandwidth available for this device may be insufficient for optimal performance.\n");
11720 dev_warn(&pdev->dev, "Please move the device to a different PCI-e link with more lanes and/or higher transfer rate.\n");
11721 }
Catherine Sullivand4dfb812013-11-28 06:39:21 +000011722 }
11723
Catherine Sullivane8278452015-02-06 08:52:08 +000011724 /* get the requested speeds from the fw */
11725 err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities, NULL);
11726 if (err)
Neerav Parikh8279e492015-09-03 17:18:50 -040011727 dev_dbg(&pf->pdev->dev, "get requested speeds ret = %s last_status = %s\n",
11728 i40e_stat_str(&pf->hw, err),
11729 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Catherine Sullivane8278452015-02-06 08:52:08 +000011730 pf->hw.phy.link_info.requested_speeds = abilities.link_speed;
11731
Catherine Sullivanfc72dbc2015-09-01 11:36:30 -040011732 /* get the supported phy types from the fw */
11733 err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities, NULL);
11734 if (err)
11735 dev_dbg(&pf->pdev->dev, "get supported phy types ret = %s last_status = %s\n",
11736 i40e_stat_str(&pf->hw, err),
11737 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Catherine Sullivanfc72dbc2015-09-01 11:36:30 -040011738
Anjali Singhai Jaine7358f52015-10-01 14:37:34 -040011739 /* Add a filter to drop all Flow control frames from any VSI from being
11740 * transmitted. By doing so we stop a malicious VF from sending out
11741 * PAUSE or PFC frames and potentially controlling traffic for other
11742 * PF/VF VSIs.
11743 * The FW can still send Flow control frames if enabled.
11744 */
11745 i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
11746 pf->main_vsi_seid);
11747
Carolyn Wyborny31b606d2016-02-17 16:12:12 -080011748 if ((pf->hw.device_id == I40E_DEV_ID_10G_BASE_T) ||
Henry Tieman4f9b4302016-11-08 13:05:18 -080011749 (pf->hw.device_id == I40E_DEV_ID_10G_BASE_T4))
11750 pf->flags |= I40E_FLAG_PHY_CONTROLS_LEDS;
Harshitha Ramamurthy4ad9f4f2016-11-08 13:05:09 -080011751 if (pf->hw.device_id == I40E_DEV_ID_SFP_I_X722)
11752 pf->flags |= I40E_FLAG_HAVE_CRT_RETIMER;
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000011753 /* print a string summarizing features */
11754 i40e_print_features(pf);
11755
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011756 return 0;
11757
11758 /* Unwind what we've done if something failed in the setup */
11759err_vsis:
Mauro S. M. Rodrigues9e6c9c02017-05-12 23:26:56 -030011760 set_bit(__I40E_DOWN, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011761 i40e_clear_interrupt_scheme(pf);
11762 kfree(pf->vsi);
Shannon Nelson04b03012013-11-28 06:39:34 +000011763err_switch_setup:
11764 i40e_reset_interrupt_capability(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011765 del_timer_sync(&pf->service_timer);
11766err_mac_addr:
11767err_configure_lan_hmc:
11768 (void)i40e_shutdown_lan_hmc(hw);
11769err_init_lan_hmc:
11770 kfree(pf->qp_pile);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011771err_sw_init:
11772err_adminq_setup:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011773err_pf_reset:
11774 iounmap(hw->hw_addr);
11775err_ioremap:
11776 kfree(pf);
11777err_pf_alloc:
11778 pci_disable_pcie_error_reporting(pdev);
Johannes Thumshirn56d766d2016-06-07 09:44:05 +020011779 pci_release_mem_regions(pdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011780err_pci_reg:
11781err_dma:
11782 pci_disable_device(pdev);
11783 return err;
11784}
11785
11786/**
11787 * i40e_remove - Device removal routine
11788 * @pdev: PCI device information struct
11789 *
11790 * i40e_remove is called by the PCI subsystem to alert the driver
11791 * that is should release a PCI device. This could be caused by a
11792 * Hot-Plug event, or because the driver is going to be removed from
11793 * memory.
11794 **/
11795static void i40e_remove(struct pci_dev *pdev)
11796{
11797 struct i40e_pf *pf = pci_get_drvdata(pdev);
Carolyn Wybornybcab2db2015-09-28 14:16:55 -040011798 struct i40e_hw *hw = &pf->hw;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011799 i40e_status ret_code;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011800 int i;
11801
11802 i40e_dbg_pf_exit(pf);
11803
Jacob Kellerbeb0dff2014-01-11 05:43:19 +000011804 i40e_ptp_stop(pf);
11805
Carolyn Wybornybcab2db2015-09-28 14:16:55 -040011806 /* Disable RSS in hw */
Shannon Nelson272cdaf22016-02-17 16:12:21 -080011807 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), 0);
11808 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), 0);
Carolyn Wybornybcab2db2015-09-28 14:16:55 -040011809
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011810 /* no more scheduling of any task */
Jacob Keller0da36b92017-04-19 09:25:55 -040011811 set_bit(__I40E_SUSPENDED, pf->state);
Mauro S. M. Rodrigues9e6c9c02017-05-12 23:26:56 -030011812 set_bit(__I40E_DOWN, pf->state);
Shannon Nelsonc99abb42016-03-10 14:59:45 -080011813 if (pf->service_timer.data)
11814 del_timer_sync(&pf->service_timer);
11815 if (pf->service_task.func)
11816 cancel_work_sync(&pf->service_task);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011817
Mitch Williams921c4672017-03-30 00:46:08 -070011818 /* Client close must be called explicitly here because the timer
11819 * has been stopped.
11820 */
11821 i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
11822
Mitch Williamseb2d80b2014-02-13 03:48:48 -080011823 if (pf->flags & I40E_FLAG_SRIOV_ENABLED) {
11824 i40e_free_vfs(pf);
11825 pf->flags &= ~I40E_FLAG_SRIOV_ENABLED;
11826 }
11827
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011828 i40e_fdir_teardown(pf);
11829
11830 /* If there is a switch structure or any orphans, remove them.
11831 * This will leave only the PF's VSI remaining.
11832 */
11833 for (i = 0; i < I40E_MAX_VEB; i++) {
11834 if (!pf->veb[i])
11835 continue;
11836
11837 if (pf->veb[i]->uplink_seid == pf->mac_seid ||
11838 pf->veb[i]->uplink_seid == 0)
11839 i40e_switch_branch_release(pf->veb[i]);
11840 }
11841
11842 /* Now we can shutdown the PF's VSI, just before we kill
11843 * adminq and hmc.
11844 */
11845 if (pf->vsi[pf->lan_vsi])
11846 i40e_vsi_release(pf->vsi[pf->lan_vsi]);
11847
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060011848 /* remove attached clients */
Mitch Williams004eb612017-04-04 12:40:16 -070011849 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11850 ret_code = i40e_lan_del_device(pf);
11851 if (ret_code)
11852 dev_warn(&pdev->dev, "Failed to delete client device: %d\n",
11853 ret_code);
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060011854 }
11855
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011856 /* shutdown and destroy the HMC */
Jesse Brandeburgf734dff2016-01-15 14:33:11 -080011857 if (hw->hmc.hmc_obj) {
11858 ret_code = i40e_shutdown_lan_hmc(hw);
Shannon Nelson60442de2014-04-23 04:50:13 +000011859 if (ret_code)
11860 dev_warn(&pdev->dev,
11861 "Failed to destroy the HMC resources: %d\n",
11862 ret_code);
11863 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011864
11865 /* shutdown the adminq */
Henry Tiemanac9c5c62016-09-06 18:05:11 -070011866 i40e_shutdown_adminq(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011867
Jesse Brandeburg8ddb3322015-11-18 15:47:06 -080011868 /* destroy the locks only once, here */
11869 mutex_destroy(&hw->aq.arq_mutex);
11870 mutex_destroy(&hw->aq.asq_mutex);
11871
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011872 /* Clear all dynamic memory lists of rings, q_vectors, and VSIs */
11873 i40e_clear_interrupt_scheme(pf);
Mitch Williams505682c2014-05-20 08:01:37 +000011874 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011875 if (pf->vsi[i]) {
11876 i40e_vsi_clear_rings(pf->vsi[i]);
11877 i40e_vsi_clear(pf->vsi[i]);
11878 pf->vsi[i] = NULL;
11879 }
11880 }
11881
11882 for (i = 0; i < I40E_MAX_VEB; i++) {
11883 kfree(pf->veb[i]);
11884 pf->veb[i] = NULL;
11885 }
11886
11887 kfree(pf->qp_pile);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011888 kfree(pf->vsi);
11889
Jesse Brandeburgf734dff2016-01-15 14:33:11 -080011890 iounmap(hw->hw_addr);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011891 kfree(pf);
Johannes Thumshirn56d766d2016-06-07 09:44:05 +020011892 pci_release_mem_regions(pdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011893
11894 pci_disable_pcie_error_reporting(pdev);
11895 pci_disable_device(pdev);
11896}
11897
11898/**
11899 * i40e_pci_error_detected - warning that something funky happened in PCI land
11900 * @pdev: PCI device information struct
11901 *
11902 * Called to warn that something happened and the error handling steps
11903 * are in progress. Allows the driver to quiesce things, be ready for
11904 * remediation.
11905 **/
11906static pci_ers_result_t i40e_pci_error_detected(struct pci_dev *pdev,
11907 enum pci_channel_state error)
11908{
11909 struct i40e_pf *pf = pci_get_drvdata(pdev);
11910
11911 dev_info(&pdev->dev, "%s: error %d\n", __func__, error);
11912
Guilherme G Piccoliedfc23ee2016-10-03 00:31:12 -070011913 if (!pf) {
11914 dev_info(&pdev->dev,
11915 "Cannot recover - error happened during device probe\n");
11916 return PCI_ERS_RESULT_DISCONNECT;
11917 }
11918
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011919 /* shutdown all operations */
Jacob Kellerdfc4ff62017-06-07 05:43:13 -040011920 if (!test_bit(__I40E_SUSPENDED, pf->state))
11921 i40e_prep_for_reset(pf, false);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011922
11923 /* Request a slot reset */
11924 return PCI_ERS_RESULT_NEED_RESET;
11925}
11926
11927/**
11928 * i40e_pci_error_slot_reset - a PCI slot reset just happened
11929 * @pdev: PCI device information struct
11930 *
11931 * Called to find if the driver can work with the device now that
11932 * the pci slot has been reset. If a basic connection seems good
11933 * (registers are readable and have sane content) then return a
11934 * happy little PCI_ERS_RESULT_xxx.
11935 **/
11936static pci_ers_result_t i40e_pci_error_slot_reset(struct pci_dev *pdev)
11937{
11938 struct i40e_pf *pf = pci_get_drvdata(pdev);
11939 pci_ers_result_t result;
11940 int err;
11941 u32 reg;
11942
Shannon Nelsonfb43201f2015-08-26 15:14:17 -040011943 dev_dbg(&pdev->dev, "%s\n", __func__);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011944 if (pci_enable_device_mem(pdev)) {
11945 dev_info(&pdev->dev,
11946 "Cannot re-enable PCI device after reset.\n");
11947 result = PCI_ERS_RESULT_DISCONNECT;
11948 } else {
11949 pci_set_master(pdev);
11950 pci_restore_state(pdev);
11951 pci_save_state(pdev);
11952 pci_wake_from_d3(pdev, false);
11953
11954 reg = rd32(&pf->hw, I40E_GLGEN_RTRIG);
11955 if (reg == 0)
11956 result = PCI_ERS_RESULT_RECOVERED;
11957 else
11958 result = PCI_ERS_RESULT_DISCONNECT;
11959 }
11960
11961 err = pci_cleanup_aer_uncorrect_error_status(pdev);
11962 if (err) {
11963 dev_info(&pdev->dev,
11964 "pci_cleanup_aer_uncorrect_error_status failed 0x%0x\n",
11965 err);
11966 /* non-fatal, continue */
11967 }
11968
11969 return result;
11970}
11971
11972/**
11973 * i40e_pci_error_resume - restart operations after PCI error recovery
11974 * @pdev: PCI device information struct
11975 *
11976 * Called to allow the driver to bring things back up after PCI error
11977 * and/or reset recovery has finished.
11978 **/
11979static void i40e_pci_error_resume(struct pci_dev *pdev)
11980{
11981 struct i40e_pf *pf = pci_get_drvdata(pdev);
11982
Shannon Nelsonfb43201f2015-08-26 15:14:17 -040011983 dev_dbg(&pdev->dev, "%s\n", __func__);
Jacob Keller0da36b92017-04-19 09:25:55 -040011984 if (test_bit(__I40E_SUSPENDED, pf->state))
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011985 return;
11986
Jacob Kellerdfc4ff62017-06-07 05:43:13 -040011987 i40e_handle_reset_warning(pf, false);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011988}
11989
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011990/**
Joshua Hay1d680052016-12-12 15:44:08 -080011991 * i40e_enable_mc_magic_wake - enable multicast magic packet wake up
11992 * using the mac_address_write admin q function
11993 * @pf: pointer to i40e_pf struct
11994 **/
11995static void i40e_enable_mc_magic_wake(struct i40e_pf *pf)
11996{
11997 struct i40e_hw *hw = &pf->hw;
11998 i40e_status ret;
11999 u8 mac_addr[6];
12000 u16 flags = 0;
12001
12002 /* Get current MAC address in case it's an LAA */
12003 if (pf->vsi[pf->lan_vsi] && pf->vsi[pf->lan_vsi]->netdev) {
12004 ether_addr_copy(mac_addr,
12005 pf->vsi[pf->lan_vsi]->netdev->dev_addr);
12006 } else {
12007 dev_err(&pf->pdev->dev,
12008 "Failed to retrieve MAC address; using default\n");
12009 ether_addr_copy(mac_addr, hw->mac.addr);
12010 }
12011
12012 /* The FW expects the mac address write cmd to first be called with
12013 * one of these flags before calling it again with the multicast
12014 * enable flags.
12015 */
12016 flags = I40E_AQC_WRITE_TYPE_LAA_WOL;
12017
12018 if (hw->func_caps.flex10_enable && hw->partition_id != 1)
12019 flags = I40E_AQC_WRITE_TYPE_LAA_ONLY;
12020
12021 ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
12022 if (ret) {
12023 dev_err(&pf->pdev->dev,
12024 "Failed to update MAC address registers; cannot enable Multicast Magic packet wake up");
12025 return;
12026 }
12027
12028 flags = I40E_AQC_MC_MAG_EN
12029 | I40E_AQC_WOL_PRESERVE_ON_PFR
12030 | I40E_AQC_WRITE_TYPE_UPDATE_MC_MAG;
12031 ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
12032 if (ret)
12033 dev_err(&pf->pdev->dev,
12034 "Failed to enable Multicast Magic Packet wake up\n");
12035}
12036
12037/**
Shannon Nelson9007bcc2013-11-26 10:49:23 +000012038 * i40e_shutdown - PCI callback for shutting down
12039 * @pdev: PCI device information struct
12040 **/
12041static void i40e_shutdown(struct pci_dev *pdev)
12042{
12043 struct i40e_pf *pf = pci_get_drvdata(pdev);
Shannon Nelson8e2773a2013-11-28 06:39:22 +000012044 struct i40e_hw *hw = &pf->hw;
Shannon Nelson9007bcc2013-11-26 10:49:23 +000012045
Jacob Keller0da36b92017-04-19 09:25:55 -040012046 set_bit(__I40E_SUSPENDED, pf->state);
Mauro S. M. Rodrigues9e6c9c02017-05-12 23:26:56 -030012047 set_bit(__I40E_DOWN, pf->state);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000012048 rtnl_lock();
Maciej Sosin373149f2017-04-05 07:50:55 -040012049 i40e_prep_for_reset(pf, true);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000012050 rtnl_unlock();
12051
Shannon Nelson8e2773a2013-11-28 06:39:22 +000012052 wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
12053 wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
12054
Catherine Sullivan02b42492015-07-10 19:35:59 -040012055 del_timer_sync(&pf->service_timer);
12056 cancel_work_sync(&pf->service_task);
12057 i40e_fdir_teardown(pf);
12058
Mitch Williams921c4672017-03-30 00:46:08 -070012059 /* Client close must be called explicitly here because the timer
12060 * has been stopped.
12061 */
12062 i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
12063
Joshua Hay1d680052016-12-12 15:44:08 -080012064 if (pf->wol_en && (pf->flags & I40E_FLAG_WOL_MC_MAGIC_PKT_WAKE))
12065 i40e_enable_mc_magic_wake(pf);
12066
Jacob Kellerdfc4ff62017-06-07 05:43:13 -040012067 i40e_prep_for_reset(pf, false);
Catherine Sullivan02b42492015-07-10 19:35:59 -040012068
12069 wr32(hw, I40E_PFPM_APM,
12070 (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
12071 wr32(hw, I40E_PFPM_WUFC,
12072 (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
12073
Shannon Nelsone1477582015-02-21 06:44:33 +000012074 i40e_clear_interrupt_scheme(pf);
12075
Shannon Nelson9007bcc2013-11-26 10:49:23 +000012076 if (system_state == SYSTEM_POWER_OFF) {
Shannon Nelson8e2773a2013-11-28 06:39:22 +000012077 pci_wake_from_d3(pdev, pf->wol_en);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000012078 pci_set_power_state(pdev, PCI_D3hot);
12079 }
12080}
12081
12082#ifdef CONFIG_PM
12083/**
12084 * i40e_suspend - PCI callback for moving to D3
12085 * @pdev: PCI device information struct
12086 **/
12087static int i40e_suspend(struct pci_dev *pdev, pm_message_t state)
12088{
12089 struct i40e_pf *pf = pci_get_drvdata(pdev);
Shannon Nelson8e2773a2013-11-28 06:39:22 +000012090 struct i40e_hw *hw = &pf->hw;
Greg Rose059ff692016-05-16 10:26:38 -070012091 int retval = 0;
Shannon Nelson9007bcc2013-11-26 10:49:23 +000012092
Jacob Keller0da36b92017-04-19 09:25:55 -040012093 set_bit(__I40E_SUSPENDED, pf->state);
Mauro S. M. Rodrigues9e6c9c02017-05-12 23:26:56 -030012094 set_bit(__I40E_DOWN, pf->state);
Mitch Williams3932dbf2015-03-31 00:45:04 -070012095
Joshua Hay1d680052016-12-12 15:44:08 -080012096 if (pf->wol_en && (pf->flags & I40E_FLAG_WOL_MC_MAGIC_PKT_WAKE))
12097 i40e_enable_mc_magic_wake(pf);
12098
Jacob Kellerdfc4ff62017-06-07 05:43:13 -040012099 i40e_prep_for_reset(pf, false);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000012100
Shannon Nelson8e2773a2013-11-28 06:39:22 +000012101 wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
12102 wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
12103
Greg Roseb33d3b72016-05-16 10:26:37 -070012104 i40e_stop_misc_vector(pf);
Carolyn Wyborny7c9ae7f2017-06-20 15:16:53 -070012105 if (pf->msix_entries) {
12106 synchronize_irq(pf->msix_entries[0].vector);
12107 free_irq(pf->msix_entries[0].vector, pf);
12108 }
Greg Rose059ff692016-05-16 10:26:38 -070012109 retval = pci_save_state(pdev);
12110 if (retval)
12111 return retval;
12112
Shannon Nelson8e2773a2013-11-28 06:39:22 +000012113 pci_wake_from_d3(pdev, pf->wol_en);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000012114 pci_set_power_state(pdev, PCI_D3hot);
12115
Greg Rose059ff692016-05-16 10:26:38 -070012116 return retval;
Shannon Nelson9007bcc2013-11-26 10:49:23 +000012117}
12118
12119/**
12120 * i40e_resume - PCI callback for waking up from D3
12121 * @pdev: PCI device information struct
12122 **/
12123static int i40e_resume(struct pci_dev *pdev)
12124{
12125 struct i40e_pf *pf = pci_get_drvdata(pdev);
12126 u32 err;
12127
12128 pci_set_power_state(pdev, PCI_D0);
12129 pci_restore_state(pdev);
12130 /* pci_restore_state() clears dev->state_saves, so
12131 * call pci_save_state() again to restore it.
12132 */
12133 pci_save_state(pdev);
12134
12135 err = pci_enable_device_mem(pdev);
12136 if (err) {
Shannon Nelsonfb43201f2015-08-26 15:14:17 -040012137 dev_err(&pdev->dev, "Cannot enable PCI device from suspend\n");
Shannon Nelson9007bcc2013-11-26 10:49:23 +000012138 return err;
12139 }
12140 pci_set_master(pdev);
12141
12142 /* no wakeup events while running */
12143 pci_wake_from_d3(pdev, false);
12144
12145 /* handling the reset will rebuild the device state */
Jacob Keller0da36b92017-04-19 09:25:55 -040012146 if (test_and_clear_bit(__I40E_SUSPENDED, pf->state)) {
Mauro S. M. Rodrigues9e6c9c02017-05-12 23:26:56 -030012147 clear_bit(__I40E_DOWN, pf->state);
Carolyn Wyborny7c9ae7f2017-06-20 15:16:53 -070012148 if (pf->msix_entries) {
12149 err = request_irq(pf->msix_entries[0].vector,
12150 i40e_intr, 0, pf->int_name, pf);
12151 if (err) {
12152 dev_err(&pf->pdev->dev,
12153 "request_irq for %s failed: %d\n",
12154 pf->int_name, err);
12155 }
12156 }
Jacob Kellerdfc4ff62017-06-07 05:43:13 -040012157 i40e_reset_and_rebuild(pf, false, false);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000012158 }
12159
12160 return 0;
12161}
12162
12163#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012164static const struct pci_error_handlers i40e_err_handler = {
12165 .error_detected = i40e_pci_error_detected,
12166 .slot_reset = i40e_pci_error_slot_reset,
12167 .resume = i40e_pci_error_resume,
12168};
12169
12170static struct pci_driver i40e_driver = {
12171 .name = i40e_driver_name,
12172 .id_table = i40e_pci_tbl,
12173 .probe = i40e_probe,
12174 .remove = i40e_remove,
Shannon Nelson9007bcc2013-11-26 10:49:23 +000012175#ifdef CONFIG_PM
12176 .suspend = i40e_suspend,
12177 .resume = i40e_resume,
12178#endif
12179 .shutdown = i40e_shutdown,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012180 .err_handler = &i40e_err_handler,
12181 .sriov_configure = i40e_pci_sriov_configure,
12182};
12183
12184/**
12185 * i40e_init_module - Driver registration routine
12186 *
12187 * i40e_init_module is the first routine called when the driver is
12188 * loaded. All it does is register with the PCI subsystem.
12189 **/
12190static int __init i40e_init_module(void)
12191{
12192 pr_info("%s: %s - version %s\n", i40e_driver_name,
12193 i40e_driver_string, i40e_driver_version_str);
12194 pr_info("%s: %s\n", i40e_driver_name, i40e_copyright);
Greg Rose96664482015-02-06 08:52:13 +000012195
Jacob Keller4d5957c2017-06-20 15:16:54 -070012196 /* There is no need to throttle the number of active tasks because
12197 * each device limits its own task using a state bit for scheduling
12198 * the service task, and the device tasks do not interfere with each
12199 * other, so we don't set a max task limit. We must set WQ_MEM_RECLAIM
12200 * since we need to be able to guarantee forward progress even under
12201 * memory pressure.
Jesse Brandeburg2803b162015-12-22 14:25:08 -080012202 */
Jacob Keller4d5957c2017-06-20 15:16:54 -070012203 i40e_wq = alloc_workqueue("%s", WQ_MEM_RECLAIM, 0, i40e_driver_name);
Jesse Brandeburg2803b162015-12-22 14:25:08 -080012204 if (!i40e_wq) {
12205 pr_err("%s: Failed to create workqueue\n", i40e_driver_name);
12206 return -ENOMEM;
12207 }
12208
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012209 i40e_dbg_init();
12210 return pci_register_driver(&i40e_driver);
12211}
12212module_init(i40e_init_module);
12213
12214/**
12215 * i40e_exit_module - Driver exit cleanup routine
12216 *
12217 * i40e_exit_module is called just before the driver is removed
12218 * from memory.
12219 **/
12220static void __exit i40e_exit_module(void)
12221{
12222 pci_unregister_driver(&i40e_driver);
Jesse Brandeburg2803b162015-12-22 14:25:08 -080012223 destroy_workqueue(i40e_wq);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012224 i40e_dbg_exit();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012225}
12226module_exit(i40e_exit_module);