blob: 387f0863f79409fea2a2ce9d6e5b9a9a63f89489 [file] [log] [blame]
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001/*******************************************************************************
2 *
3 * Intel Ethernet Controller XL710 Family Linux Driver
Shannon Nelson4fc8c672017-06-07 05:43:08 -04004 * Copyright(c) 2013 - 2017 Intel Corporation.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
Greg Rosedc641b72013-12-18 13:45:51 +000015 * You should have received a copy of the GNU General Public License along
16 * with this program. If not, see <http://www.gnu.org/licenses/>.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000017 *
18 * The full GNU General Public License is included in this distribution in
19 * the file called "COPYING".
20 *
21 * Contact Information:
22 * e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
23 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
24 *
25 ******************************************************************************/
26
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050027#include <linux/etherdevice.h>
28#include <linux/of_net.h>
29#include <linux/pci.h>
Björn Töpel0c8493d2017-05-24 07:55:34 +020030#include <linux/bpf.h>
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050031
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000032/* Local includes */
33#include "i40e.h"
Shannon Nelson4eb3f762014-03-06 08:59:58 +000034#include "i40e_diag.h"
Alexander Duyck06a5f7f2016-06-16 12:22:06 -070035#include <net/udp_tunnel.h>
Scott Petersoned0980c2017-04-13 04:45:44 -040036/* All i40e tracepoints are defined by the include below, which
37 * must be included exactly once across the whole kernel with
38 * CREATE_TRACE_POINTS defined
39 */
40#define CREATE_TRACE_POINTS
41#include "i40e_trace.h"
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000042
43const char i40e_driver_name[] = "i40e";
44static const char i40e_driver_string[] =
45 "Intel(R) Ethernet Connection XL710 Network Driver";
46
47#define DRV_KERN "-k"
48
Bimmy Pujari15990832017-01-30 12:29:37 -080049#define DRV_VERSION_MAJOR 2
50#define DRV_VERSION_MINOR 1
Jacob Keller2318b402017-04-19 09:25:53 -040051#define DRV_VERSION_BUILD 14
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000052#define DRV_VERSION __stringify(DRV_VERSION_MAJOR) "." \
53 __stringify(DRV_VERSION_MINOR) "." \
54 __stringify(DRV_VERSION_BUILD) DRV_KERN
55const char i40e_driver_version_str[] = DRV_VERSION;
Jesse Brandeburg8fb905b2014-01-17 15:36:33 -080056static const char i40e_copyright[] = "Copyright (c) 2013 - 2014 Intel Corporation.";
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000057
58/* a bit of forward declarations */
59static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi);
Maciej Sosin373149f2017-04-05 07:50:55 -040060static void i40e_handle_reset_warning(struct i40e_pf *pf, bool lock_acquired);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000061static int i40e_add_vsi(struct i40e_vsi *vsi);
62static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi);
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000063static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000064static int i40e_setup_misc_vector(struct i40e_pf *pf);
65static void i40e_determine_queue_usage(struct i40e_pf *pf);
66static int i40e_setup_pf_filter_control(struct i40e_pf *pf);
Maciej Sosin373149f2017-04-05 07:50:55 -040067static void i40e_prep_for_reset(struct i40e_pf *pf, bool lock_acquired);
68static int i40e_reset(struct i40e_pf *pf);
69static void i40e_rebuild(struct i40e_pf *pf, bool reinit, bool lock_acquired);
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080070static void i40e_fdir_sb_setup(struct i40e_pf *pf);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -080071static int i40e_veb_get_bw_info(struct i40e_veb *veb);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000072
73/* i40e_pci_tbl - PCI Device ID Table
74 *
75 * Last entry must be all 0s
76 *
77 * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
78 * Class, Class Mask, private data (not used) }
79 */
Benoit Taine9baa3c32014-08-08 15:56:03 +020080static const struct pci_device_id i40e_pci_tbl[] = {
Shannon Nelsonab600852014-01-17 15:36:39 -080081 {PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_XL710), 0},
Shannon Nelsonab600852014-01-17 15:36:39 -080082 {PCI_VDEVICE(INTEL, I40E_DEV_ID_QEMU), 0},
Shannon Nelsonab600852014-01-17 15:36:39 -080083 {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_B), 0},
84 {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_C), 0},
Shannon Nelsonab600852014-01-17 15:36:39 -080085 {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_A), 0},
86 {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_B), 0},
87 {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_C), 0},
Mitch Williams5960d332014-09-13 07:40:47 +000088 {PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T), 0},
Shannon Nelsonbc5166b92015-08-26 15:14:10 -040089 {PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T4), 0},
Anjali Singhai Jain35dae512015-12-22 14:25:03 -080090 {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_X722), 0},
91 {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_X722), 0},
Anjali Singhai Jain87e6c1d2015-06-05 12:20:25 -040092 {PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_X722), 0},
93 {PCI_VDEVICE(INTEL, I40E_DEV_ID_1G_BASE_T_X722), 0},
94 {PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T_X722), 0},
Catherine Sullivand6bf58c2016-03-18 12:18:08 -070095 {PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_I_X722), 0},
Shannon Nelson48a3b512015-07-23 16:54:39 -040096 {PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2), 0},
97 {PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2_A), 0},
Carolyn Wyborny31232372016-11-21 13:03:48 -080098 {PCI_VDEVICE(INTEL, I40E_DEV_ID_25G_B), 0},
99 {PCI_VDEVICE(INTEL, I40E_DEV_ID_25G_SFP28), 0},
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000100 /* required last entry */
101 {0, }
102};
103MODULE_DEVICE_TABLE(pci, i40e_pci_tbl);
104
105#define I40E_MAX_VF_COUNT 128
106static int debug = -1;
Alexander Duyck5d4ca232016-09-30 08:21:46 -0400107module_param(debug, uint, 0);
108MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all), Debug mask (0x8XXXXXXX)");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000109
110MODULE_AUTHOR("Intel Corporation, <e1000-devel@lists.sourceforge.net>");
111MODULE_DESCRIPTION("Intel(R) Ethernet Connection XL710 Network Driver");
112MODULE_LICENSE("GPL");
113MODULE_VERSION(DRV_VERSION);
114
Jesse Brandeburg2803b162015-12-22 14:25:08 -0800115static struct workqueue_struct *i40e_wq;
116
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000117/**
118 * i40e_allocate_dma_mem_d - OS specific memory alloc for shared code
119 * @hw: pointer to the HW structure
120 * @mem: ptr to mem struct to fill out
121 * @size: size of memory requested
122 * @alignment: what to align the allocation to
123 **/
124int i40e_allocate_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem,
125 u64 size, u32 alignment)
126{
127 struct i40e_pf *pf = (struct i40e_pf *)hw->back;
128
129 mem->size = ALIGN(size, alignment);
130 mem->va = dma_zalloc_coherent(&pf->pdev->dev, mem->size,
131 &mem->pa, GFP_KERNEL);
Jesse Brandeburg93bc73b2013-09-13 08:23:18 +0000132 if (!mem->va)
133 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000134
Jesse Brandeburg93bc73b2013-09-13 08:23:18 +0000135 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000136}
137
138/**
139 * i40e_free_dma_mem_d - OS specific memory free for shared code
140 * @hw: pointer to the HW structure
141 * @mem: ptr to mem struct to free
142 **/
143int i40e_free_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem)
144{
145 struct i40e_pf *pf = (struct i40e_pf *)hw->back;
146
147 dma_free_coherent(&pf->pdev->dev, mem->size, mem->va, mem->pa);
148 mem->va = NULL;
149 mem->pa = 0;
150 mem->size = 0;
151
152 return 0;
153}
154
155/**
156 * i40e_allocate_virt_mem_d - OS specific memory alloc for shared code
157 * @hw: pointer to the HW structure
158 * @mem: ptr to mem struct to fill out
159 * @size: size of memory requested
160 **/
161int i40e_allocate_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem,
162 u32 size)
163{
164 mem->size = size;
165 mem->va = kzalloc(size, GFP_KERNEL);
166
Jesse Brandeburg93bc73b2013-09-13 08:23:18 +0000167 if (!mem->va)
168 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000169
Jesse Brandeburg93bc73b2013-09-13 08:23:18 +0000170 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000171}
172
173/**
174 * i40e_free_virt_mem_d - OS specific memory free for shared code
175 * @hw: pointer to the HW structure
176 * @mem: ptr to mem struct to free
177 **/
178int i40e_free_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem)
179{
180 /* it's ok to kfree a NULL pointer */
181 kfree(mem->va);
182 mem->va = NULL;
183 mem->size = 0;
184
185 return 0;
186}
187
188/**
189 * i40e_get_lump - find a lump of free generic resource
190 * @pf: board private structure
191 * @pile: the pile of resource to search
192 * @needed: the number of items needed
193 * @id: an owner id to stick on the items assigned
194 *
195 * Returns the base item index of the lump, or negative for error
196 *
197 * The search_hint trick and lack of advanced fit-finding only work
198 * because we're highly likely to have all the same size lump requests.
199 * Linear search time and any fragmentation should be minimal.
200 **/
201static int i40e_get_lump(struct i40e_pf *pf, struct i40e_lump_tracking *pile,
202 u16 needed, u16 id)
203{
204 int ret = -ENOMEM;
Jesse Brandeburgddf434a2013-09-13 08:23:19 +0000205 int i, j;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000206
207 if (!pile || needed == 0 || id >= I40E_PILE_VALID_BIT) {
208 dev_info(&pf->pdev->dev,
209 "param err: pile=%p needed=%d id=0x%04x\n",
210 pile, needed, id);
211 return -EINVAL;
212 }
213
214 /* start the linear search with an imperfect hint */
215 i = pile->search_hint;
Jesse Brandeburgddf434a2013-09-13 08:23:19 +0000216 while (i < pile->num_entries) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000217 /* skip already allocated entries */
218 if (pile->list[i] & I40E_PILE_VALID_BIT) {
219 i++;
220 continue;
221 }
222
223 /* do we have enough in this lump? */
224 for (j = 0; (j < needed) && ((i+j) < pile->num_entries); j++) {
225 if (pile->list[i+j] & I40E_PILE_VALID_BIT)
226 break;
227 }
228
229 if (j == needed) {
230 /* there was enough, so assign it to the requestor */
231 for (j = 0; j < needed; j++)
232 pile->list[i+j] = id | I40E_PILE_VALID_BIT;
233 ret = i;
234 pile->search_hint = i + j;
Jesse Brandeburgddf434a2013-09-13 08:23:19 +0000235 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000236 }
Jesse Brandeburg6995b362015-08-28 17:55:54 -0400237
238 /* not enough, so skip over it and continue looking */
239 i += j;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000240 }
241
242 return ret;
243}
244
245/**
246 * i40e_put_lump - return a lump of generic resource
247 * @pile: the pile of resource to search
248 * @index: the base item index
249 * @id: the owner id of the items assigned
250 *
251 * Returns the count of items in the lump
252 **/
253static int i40e_put_lump(struct i40e_lump_tracking *pile, u16 index, u16 id)
254{
255 int valid_id = (id | I40E_PILE_VALID_BIT);
256 int count = 0;
257 int i;
258
259 if (!pile || index >= pile->num_entries)
260 return -EINVAL;
261
262 for (i = index;
263 i < pile->num_entries && pile->list[i] == valid_id;
264 i++) {
265 pile->list[i] = 0;
266 count++;
267 }
268
269 if (count && index < pile->search_hint)
270 pile->search_hint = index;
271
272 return count;
273}
274
275/**
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700276 * i40e_find_vsi_from_id - searches for the vsi with the given id
277 * @pf - the pf structure to search for the vsi
278 * @id - id of the vsi it is searching for
279 **/
280struct i40e_vsi *i40e_find_vsi_from_id(struct i40e_pf *pf, u16 id)
281{
282 int i;
283
284 for (i = 0; i < pf->num_alloc_vsi; i++)
285 if (pf->vsi[i] && (pf->vsi[i]->id == id))
286 return pf->vsi[i];
287
288 return NULL;
289}
290
291/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000292 * i40e_service_event_schedule - Schedule the service task to wake up
293 * @pf: board private structure
294 *
295 * If not already scheduled, this puts the task into the work queue
296 **/
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -0600297void i40e_service_event_schedule(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000298{
Mauro S. M. Rodrigues9e6c9c02017-05-12 23:26:56 -0300299 if (!test_bit(__I40E_DOWN, pf->state) &&
Jacob Keller0da36b92017-04-19 09:25:55 -0400300 !test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
Jesse Brandeburg2803b162015-12-22 14:25:08 -0800301 queue_work(i40e_wq, &pf->service_task);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000302}
303
304/**
305 * i40e_tx_timeout - Respond to a Tx Hang
306 * @netdev: network interface device structure
307 *
308 * If any port has noticed a Tx timeout, it is likely that the whole
309 * device is munged, not just the one netdev port, so go for the full
310 * reset.
311 **/
312static void i40e_tx_timeout(struct net_device *netdev)
313{
314 struct i40e_netdev_priv *np = netdev_priv(netdev);
315 struct i40e_vsi *vsi = np->vsi;
316 struct i40e_pf *pf = vsi->back;
Kiran Patilb03a8c12015-09-24 18:13:15 -0400317 struct i40e_ring *tx_ring = NULL;
318 unsigned int i, hung_queue = 0;
319 u32 head, val;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000320
321 pf->tx_timeout_count++;
322
Kiran Patilb03a8c12015-09-24 18:13:15 -0400323 /* find the stopped queue the same way the stack does */
324 for (i = 0; i < netdev->num_tx_queues; i++) {
325 struct netdev_queue *q;
326 unsigned long trans_start;
327
328 q = netdev_get_tx_queue(netdev, i);
Florian Westphal9b366272016-05-03 16:33:14 +0200329 trans_start = q->trans_start;
Kiran Patilb03a8c12015-09-24 18:13:15 -0400330 if (netif_xmit_stopped(q) &&
331 time_after(jiffies,
332 (trans_start + netdev->watchdog_timeo))) {
333 hung_queue = i;
334 break;
335 }
336 }
337
338 if (i == netdev->num_tx_queues) {
339 netdev_info(netdev, "tx_timeout: no netdev hung queue found\n");
340 } else {
341 /* now that we have an index, find the tx_ring struct */
342 for (i = 0; i < vsi->num_queue_pairs; i++) {
343 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc) {
344 if (hung_queue ==
345 vsi->tx_rings[i]->queue_index) {
346 tx_ring = vsi->tx_rings[i];
347 break;
348 }
349 }
350 }
351 }
352
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000353 if (time_after(jiffies, (pf->tx_timeout_last_recovery + HZ*20)))
Kiran Patilb03a8c12015-09-24 18:13:15 -0400354 pf->tx_timeout_recovery_level = 1; /* reset after some time */
355 else if (time_before(jiffies,
356 (pf->tx_timeout_last_recovery + netdev->watchdog_timeo)))
357 return; /* don't do any new action before the next timeout */
358
359 if (tx_ring) {
360 head = i40e_get_head(tx_ring);
361 /* Read interrupt register */
362 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
363 val = rd32(&pf->hw,
364 I40E_PFINT_DYN_CTLN(tx_ring->q_vector->v_idx +
365 tx_ring->vsi->base_vector - 1));
366 else
367 val = rd32(&pf->hw, I40E_PFINT_DYN_CTL0);
368
369 netdev_info(netdev, "tx_timeout: VSI_seid: %d, Q %d, NTC: 0x%x, HWB: 0x%x, NTU: 0x%x, TAIL: 0x%x, INT: 0x%x\n",
370 vsi->seid, hung_queue, tx_ring->next_to_clean,
371 head, tx_ring->next_to_use,
372 readl(tx_ring->tail), val);
373 }
374
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000375 pf->tx_timeout_last_recovery = jiffies;
Kiran Patilb03a8c12015-09-24 18:13:15 -0400376 netdev_info(netdev, "tx_timeout recovery level %d, hung_queue %d\n",
377 pf->tx_timeout_recovery_level, hung_queue);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000378
379 switch (pf->tx_timeout_recovery_level) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000380 case 1:
Jacob Keller0da36b92017-04-19 09:25:55 -0400381 set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000382 break;
383 case 2:
Jacob Keller0da36b92017-04-19 09:25:55 -0400384 set_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000385 break;
386 case 3:
Jacob Keller0da36b92017-04-19 09:25:55 -0400387 set_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000388 break;
389 default:
390 netdev_err(netdev, "tx_timeout recovery unsuccessful\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000391 break;
392 }
Kiran Patilb03a8c12015-09-24 18:13:15 -0400393
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000394 i40e_service_event_schedule(pf);
395 pf->tx_timeout_recovery_level++;
396}
397
398/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000399 * i40e_get_vsi_stats_struct - Get System Network Statistics
400 * @vsi: the VSI we care about
401 *
402 * Returns the address of the device statistics structure.
403 * The statistics are actually updated from the service task.
404 **/
405struct rtnl_link_stats64 *i40e_get_vsi_stats_struct(struct i40e_vsi *vsi)
406{
407 return &vsi->net_stats;
408}
409
410/**
Björn Töpel74608d12017-05-24 07:55:35 +0200411 * i40e_get_netdev_stats_struct_tx - populate stats from a Tx ring
412 * @ring: Tx ring to get statistics from
413 * @stats: statistics entry to be updated
414 **/
415static void i40e_get_netdev_stats_struct_tx(struct i40e_ring *ring,
416 struct rtnl_link_stats64 *stats)
417{
418 u64 bytes, packets;
419 unsigned int start;
420
421 do {
422 start = u64_stats_fetch_begin_irq(&ring->syncp);
423 packets = ring->stats.packets;
424 bytes = ring->stats.bytes;
425 } while (u64_stats_fetch_retry_irq(&ring->syncp, start));
426
427 stats->tx_packets += packets;
428 stats->tx_bytes += bytes;
429}
430
431/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000432 * i40e_get_netdev_stats_struct - Get statistics for netdev interface
433 * @netdev: network interface device structure
434 *
435 * Returns the address of the device statistics structure.
436 * The statistics are actually updated from the service task.
437 **/
Alexander Duyck9eed69a2017-02-21 15:55:47 -0800438static void i40e_get_netdev_stats_struct(struct net_device *netdev,
stephen hemmingerbc1f4472017-01-06 19:12:52 -0800439 struct rtnl_link_stats64 *stats)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000440{
441 struct i40e_netdev_priv *np = netdev_priv(netdev);
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +0000442 struct i40e_ring *tx_ring, *rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000443 struct i40e_vsi *vsi = np->vsi;
Alexander Duyck980e9b12013-09-28 06:01:03 +0000444 struct rtnl_link_stats64 *vsi_stats = i40e_get_vsi_stats_struct(vsi);
445 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000446
Jacob Keller0da36b92017-04-19 09:25:55 -0400447 if (test_bit(__I40E_VSI_DOWN, vsi->state))
stephen hemmingerbc1f4472017-01-06 19:12:52 -0800448 return;
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +0000449
Jesse Brandeburg3c325ce2013-12-14 03:26:45 -0800450 if (!vsi->tx_rings)
stephen hemmingerbc1f4472017-01-06 19:12:52 -0800451 return;
Jesse Brandeburg3c325ce2013-12-14 03:26:45 -0800452
Alexander Duyck980e9b12013-09-28 06:01:03 +0000453 rcu_read_lock();
454 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck980e9b12013-09-28 06:01:03 +0000455 u64 bytes, packets;
456 unsigned int start;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000457
Alexander Duyck980e9b12013-09-28 06:01:03 +0000458 tx_ring = ACCESS_ONCE(vsi->tx_rings[i]);
459 if (!tx_ring)
460 continue;
Björn Töpel74608d12017-05-24 07:55:35 +0200461 i40e_get_netdev_stats_struct_tx(tx_ring, stats);
Alexander Duyck980e9b12013-09-28 06:01:03 +0000462
Alexander Duyck980e9b12013-09-28 06:01:03 +0000463 rx_ring = &tx_ring[1];
464
465 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700466 start = u64_stats_fetch_begin_irq(&rx_ring->syncp);
Alexander Duyck980e9b12013-09-28 06:01:03 +0000467 packets = rx_ring->stats.packets;
468 bytes = rx_ring->stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700469 } while (u64_stats_fetch_retry_irq(&rx_ring->syncp, start));
Alexander Duyck980e9b12013-09-28 06:01:03 +0000470
471 stats->rx_packets += packets;
472 stats->rx_bytes += bytes;
Björn Töpel74608d12017-05-24 07:55:35 +0200473
474 if (i40e_enabled_xdp_vsi(vsi))
475 i40e_get_netdev_stats_struct_tx(&rx_ring[1], stats);
Alexander Duyck980e9b12013-09-28 06:01:03 +0000476 }
477 rcu_read_unlock();
478
Akeem G Abodunrina5282f42014-05-10 04:49:03 +0000479 /* following stats updated by i40e_watchdog_subtask() */
Alexander Duyck980e9b12013-09-28 06:01:03 +0000480 stats->multicast = vsi_stats->multicast;
481 stats->tx_errors = vsi_stats->tx_errors;
482 stats->tx_dropped = vsi_stats->tx_dropped;
483 stats->rx_errors = vsi_stats->rx_errors;
Jesse Brandeburgd8201e22015-07-23 16:54:35 -0400484 stats->rx_dropped = vsi_stats->rx_dropped;
Alexander Duyck980e9b12013-09-28 06:01:03 +0000485 stats->rx_crc_errors = vsi_stats->rx_crc_errors;
486 stats->rx_length_errors = vsi_stats->rx_length_errors;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000487}
488
489/**
490 * i40e_vsi_reset_stats - Resets all stats of the given vsi
491 * @vsi: the VSI to have its stats reset
492 **/
493void i40e_vsi_reset_stats(struct i40e_vsi *vsi)
494{
495 struct rtnl_link_stats64 *ns;
496 int i;
497
498 if (!vsi)
499 return;
500
501 ns = i40e_get_vsi_stats_struct(vsi);
502 memset(ns, 0, sizeof(*ns));
503 memset(&vsi->net_stats_offsets, 0, sizeof(vsi->net_stats_offsets));
504 memset(&vsi->eth_stats, 0, sizeof(vsi->eth_stats));
505 memset(&vsi->eth_stats_offsets, 0, sizeof(vsi->eth_stats_offsets));
Greg Rose8e9dca52013-12-18 13:45:53 +0000506 if (vsi->rx_rings && vsi->rx_rings[0]) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000507 for (i = 0; i < vsi->num_queue_pairs; i++) {
Jesse Brandeburg6995b362015-08-28 17:55:54 -0400508 memset(&vsi->rx_rings[i]->stats, 0,
Alexander Duyck9f65e152013-09-28 06:00:58 +0000509 sizeof(vsi->rx_rings[i]->stats));
Jesse Brandeburg6995b362015-08-28 17:55:54 -0400510 memset(&vsi->rx_rings[i]->rx_stats, 0,
Alexander Duyck9f65e152013-09-28 06:00:58 +0000511 sizeof(vsi->rx_rings[i]->rx_stats));
Jesse Brandeburg6995b362015-08-28 17:55:54 -0400512 memset(&vsi->tx_rings[i]->stats, 0,
Alexander Duyck9f65e152013-09-28 06:00:58 +0000513 sizeof(vsi->tx_rings[i]->stats));
514 memset(&vsi->tx_rings[i]->tx_stats, 0,
515 sizeof(vsi->tx_rings[i]->tx_stats));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000516 }
Greg Rose8e9dca52013-12-18 13:45:53 +0000517 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000518 vsi->stat_offsets_loaded = false;
519}
520
521/**
Jeff Kirsherb40c82e62015-02-27 09:18:34 +0000522 * i40e_pf_reset_stats - Reset all of the stats for the given PF
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000523 * @pf: the PF to be reset
524 **/
525void i40e_pf_reset_stats(struct i40e_pf *pf)
526{
Shannon Nelsone91fdf72014-06-03 23:50:18 +0000527 int i;
528
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000529 memset(&pf->stats, 0, sizeof(pf->stats));
530 memset(&pf->stats_offsets, 0, sizeof(pf->stats_offsets));
531 pf->stat_offsets_loaded = false;
Shannon Nelsone91fdf72014-06-03 23:50:18 +0000532
533 for (i = 0; i < I40E_MAX_VEB; i++) {
534 if (pf->veb[i]) {
535 memset(&pf->veb[i]->stats, 0,
536 sizeof(pf->veb[i]->stats));
537 memset(&pf->veb[i]->stats_offsets, 0,
538 sizeof(pf->veb[i]->stats_offsets));
539 pf->veb[i]->stat_offsets_loaded = false;
540 }
541 }
Catherine Sullivan42bce042016-07-27 12:02:32 -0700542 pf->hw_csum_rx_error = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000543}
544
545/**
546 * i40e_stat_update48 - read and update a 48 bit stat from the chip
547 * @hw: ptr to the hardware info
548 * @hireg: the high 32 bit reg to read
549 * @loreg: the low 32 bit reg to read
550 * @offset_loaded: has the initial offset been loaded yet
551 * @offset: ptr to current offset value
552 * @stat: ptr to the stat
553 *
554 * Since the device stats are not reset at PFReset, they likely will not
555 * be zeroed when the driver starts. We'll save the first values read
556 * and use them as offsets to be subtracted from the raw values in order
557 * to report stats that count from zero. In the process, we also manage
558 * the potential roll-over.
559 **/
560static void i40e_stat_update48(struct i40e_hw *hw, u32 hireg, u32 loreg,
561 bool offset_loaded, u64 *offset, u64 *stat)
562{
563 u64 new_data;
564
Shannon Nelsonab600852014-01-17 15:36:39 -0800565 if (hw->device_id == I40E_DEV_ID_QEMU) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000566 new_data = rd32(hw, loreg);
567 new_data |= ((u64)(rd32(hw, hireg) & 0xFFFF)) << 32;
568 } else {
569 new_data = rd64(hw, loreg);
570 }
571 if (!offset_loaded)
572 *offset = new_data;
573 if (likely(new_data >= *offset))
574 *stat = new_data - *offset;
575 else
Jesse Brandeburg41a1d042015-06-04 16:24:02 -0400576 *stat = (new_data + BIT_ULL(48)) - *offset;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000577 *stat &= 0xFFFFFFFFFFFFULL;
578}
579
580/**
581 * i40e_stat_update32 - read and update a 32 bit stat from the chip
582 * @hw: ptr to the hardware info
583 * @reg: the hw reg to read
584 * @offset_loaded: has the initial offset been loaded yet
585 * @offset: ptr to current offset value
586 * @stat: ptr to the stat
587 **/
588static void i40e_stat_update32(struct i40e_hw *hw, u32 reg,
589 bool offset_loaded, u64 *offset, u64 *stat)
590{
591 u32 new_data;
592
593 new_data = rd32(hw, reg);
594 if (!offset_loaded)
595 *offset = new_data;
596 if (likely(new_data >= *offset))
597 *stat = (u32)(new_data - *offset);
598 else
Jesse Brandeburg41a1d042015-06-04 16:24:02 -0400599 *stat = (u32)((new_data + BIT_ULL(32)) - *offset);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000600}
601
602/**
Mariusz Stachura0dc86922017-07-14 09:27:00 -0400603 * i40e_stat_update_and_clear32 - read and clear hw reg, update a 32 bit stat
604 * @hw: ptr to the hardware info
605 * @reg: the hw reg to read and clear
606 * @stat: ptr to the stat
607 **/
608static void i40e_stat_update_and_clear32(struct i40e_hw *hw, u32 reg, u64 *stat)
609{
610 u32 new_data = rd32(hw, reg);
611
612 wr32(hw, reg, 1); /* must write a nonzero value to clear register */
613 *stat += new_data;
614}
615
616/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000617 * i40e_update_eth_stats - Update VSI-specific ethernet statistics counters.
618 * @vsi: the VSI to be updated
619 **/
620void i40e_update_eth_stats(struct i40e_vsi *vsi)
621{
622 int stat_idx = le16_to_cpu(vsi->info.stat_counter_idx);
623 struct i40e_pf *pf = vsi->back;
624 struct i40e_hw *hw = &pf->hw;
625 struct i40e_eth_stats *oes;
626 struct i40e_eth_stats *es; /* device's eth stats */
627
628 es = &vsi->eth_stats;
629 oes = &vsi->eth_stats_offsets;
630
631 /* Gather up the stats that the hw collects */
632 i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
633 vsi->stat_offsets_loaded,
634 &oes->tx_errors, &es->tx_errors);
635 i40e_stat_update32(hw, I40E_GLV_RDPC(stat_idx),
636 vsi->stat_offsets_loaded,
637 &oes->rx_discards, &es->rx_discards);
Shannon Nelson41a9e552014-04-23 04:50:20 +0000638 i40e_stat_update32(hw, I40E_GLV_RUPP(stat_idx),
639 vsi->stat_offsets_loaded,
640 &oes->rx_unknown_protocol, &es->rx_unknown_protocol);
641 i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
642 vsi->stat_offsets_loaded,
643 &oes->tx_errors, &es->tx_errors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000644
645 i40e_stat_update48(hw, I40E_GLV_GORCH(stat_idx),
646 I40E_GLV_GORCL(stat_idx),
647 vsi->stat_offsets_loaded,
648 &oes->rx_bytes, &es->rx_bytes);
649 i40e_stat_update48(hw, I40E_GLV_UPRCH(stat_idx),
650 I40E_GLV_UPRCL(stat_idx),
651 vsi->stat_offsets_loaded,
652 &oes->rx_unicast, &es->rx_unicast);
653 i40e_stat_update48(hw, I40E_GLV_MPRCH(stat_idx),
654 I40E_GLV_MPRCL(stat_idx),
655 vsi->stat_offsets_loaded,
656 &oes->rx_multicast, &es->rx_multicast);
657 i40e_stat_update48(hw, I40E_GLV_BPRCH(stat_idx),
658 I40E_GLV_BPRCL(stat_idx),
659 vsi->stat_offsets_loaded,
660 &oes->rx_broadcast, &es->rx_broadcast);
661
662 i40e_stat_update48(hw, I40E_GLV_GOTCH(stat_idx),
663 I40E_GLV_GOTCL(stat_idx),
664 vsi->stat_offsets_loaded,
665 &oes->tx_bytes, &es->tx_bytes);
666 i40e_stat_update48(hw, I40E_GLV_UPTCH(stat_idx),
667 I40E_GLV_UPTCL(stat_idx),
668 vsi->stat_offsets_loaded,
669 &oes->tx_unicast, &es->tx_unicast);
670 i40e_stat_update48(hw, I40E_GLV_MPTCH(stat_idx),
671 I40E_GLV_MPTCL(stat_idx),
672 vsi->stat_offsets_loaded,
673 &oes->tx_multicast, &es->tx_multicast);
674 i40e_stat_update48(hw, I40E_GLV_BPTCH(stat_idx),
675 I40E_GLV_BPTCL(stat_idx),
676 vsi->stat_offsets_loaded,
677 &oes->tx_broadcast, &es->tx_broadcast);
678 vsi->stat_offsets_loaded = true;
679}
680
681/**
682 * i40e_update_veb_stats - Update Switch component statistics
683 * @veb: the VEB being updated
684 **/
685static void i40e_update_veb_stats(struct i40e_veb *veb)
686{
687 struct i40e_pf *pf = veb->pf;
688 struct i40e_hw *hw = &pf->hw;
689 struct i40e_eth_stats *oes;
690 struct i40e_eth_stats *es; /* device's eth stats */
Neerav Parikhfe860af2015-07-10 19:36:02 -0400691 struct i40e_veb_tc_stats *veb_oes;
692 struct i40e_veb_tc_stats *veb_es;
693 int i, idx = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000694
695 idx = veb->stats_idx;
696 es = &veb->stats;
697 oes = &veb->stats_offsets;
Neerav Parikhfe860af2015-07-10 19:36:02 -0400698 veb_es = &veb->tc_stats;
699 veb_oes = &veb->tc_stats_offsets;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000700
701 /* Gather up the stats that the hw collects */
702 i40e_stat_update32(hw, I40E_GLSW_TDPC(idx),
703 veb->stat_offsets_loaded,
704 &oes->tx_discards, &es->tx_discards);
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +0000705 if (hw->revision_id > 0)
706 i40e_stat_update32(hw, I40E_GLSW_RUPP(idx),
707 veb->stat_offsets_loaded,
708 &oes->rx_unknown_protocol,
709 &es->rx_unknown_protocol);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000710 i40e_stat_update48(hw, I40E_GLSW_GORCH(idx), I40E_GLSW_GORCL(idx),
711 veb->stat_offsets_loaded,
712 &oes->rx_bytes, &es->rx_bytes);
713 i40e_stat_update48(hw, I40E_GLSW_UPRCH(idx), I40E_GLSW_UPRCL(idx),
714 veb->stat_offsets_loaded,
715 &oes->rx_unicast, &es->rx_unicast);
716 i40e_stat_update48(hw, I40E_GLSW_MPRCH(idx), I40E_GLSW_MPRCL(idx),
717 veb->stat_offsets_loaded,
718 &oes->rx_multicast, &es->rx_multicast);
719 i40e_stat_update48(hw, I40E_GLSW_BPRCH(idx), I40E_GLSW_BPRCL(idx),
720 veb->stat_offsets_loaded,
721 &oes->rx_broadcast, &es->rx_broadcast);
722
723 i40e_stat_update48(hw, I40E_GLSW_GOTCH(idx), I40E_GLSW_GOTCL(idx),
724 veb->stat_offsets_loaded,
725 &oes->tx_bytes, &es->tx_bytes);
726 i40e_stat_update48(hw, I40E_GLSW_UPTCH(idx), I40E_GLSW_UPTCL(idx),
727 veb->stat_offsets_loaded,
728 &oes->tx_unicast, &es->tx_unicast);
729 i40e_stat_update48(hw, I40E_GLSW_MPTCH(idx), I40E_GLSW_MPTCL(idx),
730 veb->stat_offsets_loaded,
731 &oes->tx_multicast, &es->tx_multicast);
732 i40e_stat_update48(hw, I40E_GLSW_BPTCH(idx), I40E_GLSW_BPTCL(idx),
733 veb->stat_offsets_loaded,
734 &oes->tx_broadcast, &es->tx_broadcast);
Neerav Parikhfe860af2015-07-10 19:36:02 -0400735 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
736 i40e_stat_update48(hw, I40E_GLVEBTC_RPCH(i, idx),
737 I40E_GLVEBTC_RPCL(i, idx),
738 veb->stat_offsets_loaded,
739 &veb_oes->tc_rx_packets[i],
740 &veb_es->tc_rx_packets[i]);
741 i40e_stat_update48(hw, I40E_GLVEBTC_RBCH(i, idx),
742 I40E_GLVEBTC_RBCL(i, idx),
743 veb->stat_offsets_loaded,
744 &veb_oes->tc_rx_bytes[i],
745 &veb_es->tc_rx_bytes[i]);
746 i40e_stat_update48(hw, I40E_GLVEBTC_TPCH(i, idx),
747 I40E_GLVEBTC_TPCL(i, idx),
748 veb->stat_offsets_loaded,
749 &veb_oes->tc_tx_packets[i],
750 &veb_es->tc_tx_packets[i]);
751 i40e_stat_update48(hw, I40E_GLVEBTC_TBCH(i, idx),
752 I40E_GLVEBTC_TBCL(i, idx),
753 veb->stat_offsets_loaded,
754 &veb_oes->tc_tx_bytes[i],
755 &veb_es->tc_tx_bytes[i]);
756 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000757 veb->stat_offsets_loaded = true;
758}
759
760/**
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000761 * i40e_update_vsi_stats - Update the vsi statistics counters.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000762 * @vsi: the VSI to be updated
763 *
764 * There are a few instances where we store the same stat in a
765 * couple of different structs. This is partly because we have
766 * the netdev stats that need to be filled out, which is slightly
767 * different from the "eth_stats" defined by the chip and used in
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000768 * VF communications. We sort it out here.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000769 **/
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000770static void i40e_update_vsi_stats(struct i40e_vsi *vsi)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000771{
772 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000773 struct rtnl_link_stats64 *ons;
774 struct rtnl_link_stats64 *ns; /* netdev stats */
775 struct i40e_eth_stats *oes;
776 struct i40e_eth_stats *es; /* device's eth stats */
777 u32 tx_restart, tx_busy;
Akeem G Abodunrinbf00b372014-10-17 03:14:39 +0000778 struct i40e_ring *p;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000779 u32 rx_page, rx_buf;
Akeem G Abodunrinbf00b372014-10-17 03:14:39 +0000780 u64 bytes, packets;
781 unsigned int start;
Anjali Singhai Jain2fc3d712015-08-27 11:42:29 -0400782 u64 tx_linearize;
Anjali Singhai Jain164c9f52015-10-21 19:47:08 -0400783 u64 tx_force_wb;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000784 u64 rx_p, rx_b;
785 u64 tx_p, tx_b;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000786 u16 q;
787
Jacob Keller0da36b92017-04-19 09:25:55 -0400788 if (test_bit(__I40E_VSI_DOWN, vsi->state) ||
789 test_bit(__I40E_CONFIG_BUSY, pf->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000790 return;
791
792 ns = i40e_get_vsi_stats_struct(vsi);
793 ons = &vsi->net_stats_offsets;
794 es = &vsi->eth_stats;
795 oes = &vsi->eth_stats_offsets;
796
797 /* Gather up the netdev and vsi stats that the driver collects
798 * on the fly during packet processing
799 */
800 rx_b = rx_p = 0;
801 tx_b = tx_p = 0;
Anjali Singhai Jain164c9f52015-10-21 19:47:08 -0400802 tx_restart = tx_busy = tx_linearize = tx_force_wb = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000803 rx_page = 0;
804 rx_buf = 0;
Alexander Duyck980e9b12013-09-28 06:01:03 +0000805 rcu_read_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000806 for (q = 0; q < vsi->num_queue_pairs; q++) {
Alexander Duyck980e9b12013-09-28 06:01:03 +0000807 /* locate Tx ring */
808 p = ACCESS_ONCE(vsi->tx_rings[q]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000809
Alexander Duyck980e9b12013-09-28 06:01:03 +0000810 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700811 start = u64_stats_fetch_begin_irq(&p->syncp);
Alexander Duyck980e9b12013-09-28 06:01:03 +0000812 packets = p->stats.packets;
813 bytes = p->stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700814 } while (u64_stats_fetch_retry_irq(&p->syncp, start));
Alexander Duyck980e9b12013-09-28 06:01:03 +0000815 tx_b += bytes;
816 tx_p += packets;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000817 tx_restart += p->tx_stats.restart_queue;
818 tx_busy += p->tx_stats.tx_busy;
Anjali Singhai Jain2fc3d712015-08-27 11:42:29 -0400819 tx_linearize += p->tx_stats.tx_linearize;
Anjali Singhai Jain164c9f52015-10-21 19:47:08 -0400820 tx_force_wb += p->tx_stats.tx_force_wb;
Alexander Duyck980e9b12013-09-28 06:01:03 +0000821
822 /* Rx queue is part of the same block as Tx queue */
823 p = &p[1];
824 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700825 start = u64_stats_fetch_begin_irq(&p->syncp);
Alexander Duyck980e9b12013-09-28 06:01:03 +0000826 packets = p->stats.packets;
827 bytes = p->stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700828 } while (u64_stats_fetch_retry_irq(&p->syncp, start));
Alexander Duyck980e9b12013-09-28 06:01:03 +0000829 rx_b += bytes;
830 rx_p += packets;
Mitch Williams420136c2013-12-18 13:45:59 +0000831 rx_buf += p->rx_stats.alloc_buff_failed;
832 rx_page += p->rx_stats.alloc_page_failed;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000833 }
Alexander Duyck980e9b12013-09-28 06:01:03 +0000834 rcu_read_unlock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000835 vsi->tx_restart = tx_restart;
836 vsi->tx_busy = tx_busy;
Anjali Singhai Jain2fc3d712015-08-27 11:42:29 -0400837 vsi->tx_linearize = tx_linearize;
Anjali Singhai Jain164c9f52015-10-21 19:47:08 -0400838 vsi->tx_force_wb = tx_force_wb;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000839 vsi->rx_page_failed = rx_page;
840 vsi->rx_buf_failed = rx_buf;
841
842 ns->rx_packets = rx_p;
843 ns->rx_bytes = rx_b;
844 ns->tx_packets = tx_p;
845 ns->tx_bytes = tx_b;
846
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000847 /* update netdev stats from eth stats */
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000848 i40e_update_eth_stats(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000849 ons->tx_errors = oes->tx_errors;
850 ns->tx_errors = es->tx_errors;
851 ons->multicast = oes->rx_multicast;
852 ns->multicast = es->rx_multicast;
Shannon Nelson41a9e552014-04-23 04:50:20 +0000853 ons->rx_dropped = oes->rx_discards;
854 ns->rx_dropped = es->rx_discards;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000855 ons->tx_dropped = oes->tx_discards;
856 ns->tx_dropped = es->tx_discards;
857
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000858 /* pull in a couple PF stats if this is the main vsi */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000859 if (vsi == pf->vsi[pf->lan_vsi]) {
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000860 ns->rx_crc_errors = pf->stats.crc_errors;
861 ns->rx_errors = pf->stats.crc_errors + pf->stats.illegal_bytes;
862 ns->rx_length_errors = pf->stats.rx_length_errors;
863 }
864}
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000865
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000866/**
Jeff Kirsherb40c82e62015-02-27 09:18:34 +0000867 * i40e_update_pf_stats - Update the PF statistics counters.
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000868 * @pf: the PF to be updated
869 **/
870static void i40e_update_pf_stats(struct i40e_pf *pf)
871{
872 struct i40e_hw_port_stats *osd = &pf->stats_offsets;
873 struct i40e_hw_port_stats *nsd = &pf->stats;
874 struct i40e_hw *hw = &pf->hw;
875 u32 val;
876 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000877
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000878 i40e_stat_update48(hw, I40E_GLPRT_GORCH(hw->port),
879 I40E_GLPRT_GORCL(hw->port),
880 pf->stat_offsets_loaded,
881 &osd->eth.rx_bytes, &nsd->eth.rx_bytes);
882 i40e_stat_update48(hw, I40E_GLPRT_GOTCH(hw->port),
883 I40E_GLPRT_GOTCL(hw->port),
884 pf->stat_offsets_loaded,
885 &osd->eth.tx_bytes, &nsd->eth.tx_bytes);
886 i40e_stat_update32(hw, I40E_GLPRT_RDPC(hw->port),
887 pf->stat_offsets_loaded,
888 &osd->eth.rx_discards,
889 &nsd->eth.rx_discards);
Shannon Nelson532d2832014-04-23 04:50:09 +0000890 i40e_stat_update48(hw, I40E_GLPRT_UPRCH(hw->port),
891 I40E_GLPRT_UPRCL(hw->port),
892 pf->stat_offsets_loaded,
893 &osd->eth.rx_unicast,
894 &nsd->eth.rx_unicast);
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000895 i40e_stat_update48(hw, I40E_GLPRT_MPRCH(hw->port),
896 I40E_GLPRT_MPRCL(hw->port),
897 pf->stat_offsets_loaded,
898 &osd->eth.rx_multicast,
899 &nsd->eth.rx_multicast);
Shannon Nelson532d2832014-04-23 04:50:09 +0000900 i40e_stat_update48(hw, I40E_GLPRT_BPRCH(hw->port),
901 I40E_GLPRT_BPRCL(hw->port),
902 pf->stat_offsets_loaded,
903 &osd->eth.rx_broadcast,
904 &nsd->eth.rx_broadcast);
905 i40e_stat_update48(hw, I40E_GLPRT_UPTCH(hw->port),
906 I40E_GLPRT_UPTCL(hw->port),
907 pf->stat_offsets_loaded,
908 &osd->eth.tx_unicast,
909 &nsd->eth.tx_unicast);
910 i40e_stat_update48(hw, I40E_GLPRT_MPTCH(hw->port),
911 I40E_GLPRT_MPTCL(hw->port),
912 pf->stat_offsets_loaded,
913 &osd->eth.tx_multicast,
914 &nsd->eth.tx_multicast);
915 i40e_stat_update48(hw, I40E_GLPRT_BPTCH(hw->port),
916 I40E_GLPRT_BPTCL(hw->port),
917 pf->stat_offsets_loaded,
918 &osd->eth.tx_broadcast,
919 &nsd->eth.tx_broadcast);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000920
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000921 i40e_stat_update32(hw, I40E_GLPRT_TDOLD(hw->port),
922 pf->stat_offsets_loaded,
923 &osd->tx_dropped_link_down,
924 &nsd->tx_dropped_link_down);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000925
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000926 i40e_stat_update32(hw, I40E_GLPRT_CRCERRS(hw->port),
927 pf->stat_offsets_loaded,
928 &osd->crc_errors, &nsd->crc_errors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000929
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000930 i40e_stat_update32(hw, I40E_GLPRT_ILLERRC(hw->port),
931 pf->stat_offsets_loaded,
932 &osd->illegal_bytes, &nsd->illegal_bytes);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000933
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000934 i40e_stat_update32(hw, I40E_GLPRT_MLFC(hw->port),
935 pf->stat_offsets_loaded,
936 &osd->mac_local_faults,
937 &nsd->mac_local_faults);
938 i40e_stat_update32(hw, I40E_GLPRT_MRFC(hw->port),
939 pf->stat_offsets_loaded,
940 &osd->mac_remote_faults,
941 &nsd->mac_remote_faults);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000942
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000943 i40e_stat_update32(hw, I40E_GLPRT_RLEC(hw->port),
944 pf->stat_offsets_loaded,
945 &osd->rx_length_errors,
946 &nsd->rx_length_errors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000947
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000948 i40e_stat_update32(hw, I40E_GLPRT_LXONRXC(hw->port),
949 pf->stat_offsets_loaded,
950 &osd->link_xon_rx, &nsd->link_xon_rx);
951 i40e_stat_update32(hw, I40E_GLPRT_LXONTXC(hw->port),
952 pf->stat_offsets_loaded,
953 &osd->link_xon_tx, &nsd->link_xon_tx);
Neerav Parikh95db2392015-11-06 15:26:09 -0800954 i40e_stat_update32(hw, I40E_GLPRT_LXOFFRXC(hw->port),
955 pf->stat_offsets_loaded,
956 &osd->link_xoff_rx, &nsd->link_xoff_rx);
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000957 i40e_stat_update32(hw, I40E_GLPRT_LXOFFTXC(hw->port),
958 pf->stat_offsets_loaded,
959 &osd->link_xoff_tx, &nsd->link_xoff_tx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000960
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000961 for (i = 0; i < 8; i++) {
Neerav Parikh95db2392015-11-06 15:26:09 -0800962 i40e_stat_update32(hw, I40E_GLPRT_PXOFFRXC(hw->port, i),
963 pf->stat_offsets_loaded,
964 &osd->priority_xoff_rx[i],
965 &nsd->priority_xoff_rx[i]);
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000966 i40e_stat_update32(hw, I40E_GLPRT_PXONRXC(hw->port, i),
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000967 pf->stat_offsets_loaded,
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000968 &osd->priority_xon_rx[i],
969 &nsd->priority_xon_rx[i]);
970 i40e_stat_update32(hw, I40E_GLPRT_PXONTXC(hw->port, i),
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000971 pf->stat_offsets_loaded,
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000972 &osd->priority_xon_tx[i],
973 &nsd->priority_xon_tx[i]);
974 i40e_stat_update32(hw, I40E_GLPRT_PXOFFTXC(hw->port, i),
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000975 pf->stat_offsets_loaded,
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000976 &osd->priority_xoff_tx[i],
977 &nsd->priority_xoff_tx[i]);
978 i40e_stat_update32(hw,
979 I40E_GLPRT_RXON2OFFCNT(hw->port, i),
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000980 pf->stat_offsets_loaded,
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000981 &osd->priority_xon_2_xoff[i],
982 &nsd->priority_xon_2_xoff[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000983 }
984
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000985 i40e_stat_update48(hw, I40E_GLPRT_PRC64H(hw->port),
986 I40E_GLPRT_PRC64L(hw->port),
987 pf->stat_offsets_loaded,
988 &osd->rx_size_64, &nsd->rx_size_64);
989 i40e_stat_update48(hw, I40E_GLPRT_PRC127H(hw->port),
990 I40E_GLPRT_PRC127L(hw->port),
991 pf->stat_offsets_loaded,
992 &osd->rx_size_127, &nsd->rx_size_127);
993 i40e_stat_update48(hw, I40E_GLPRT_PRC255H(hw->port),
994 I40E_GLPRT_PRC255L(hw->port),
995 pf->stat_offsets_loaded,
996 &osd->rx_size_255, &nsd->rx_size_255);
997 i40e_stat_update48(hw, I40E_GLPRT_PRC511H(hw->port),
998 I40E_GLPRT_PRC511L(hw->port),
999 pf->stat_offsets_loaded,
1000 &osd->rx_size_511, &nsd->rx_size_511);
1001 i40e_stat_update48(hw, I40E_GLPRT_PRC1023H(hw->port),
1002 I40E_GLPRT_PRC1023L(hw->port),
1003 pf->stat_offsets_loaded,
1004 &osd->rx_size_1023, &nsd->rx_size_1023);
1005 i40e_stat_update48(hw, I40E_GLPRT_PRC1522H(hw->port),
1006 I40E_GLPRT_PRC1522L(hw->port),
1007 pf->stat_offsets_loaded,
1008 &osd->rx_size_1522, &nsd->rx_size_1522);
1009 i40e_stat_update48(hw, I40E_GLPRT_PRC9522H(hw->port),
1010 I40E_GLPRT_PRC9522L(hw->port),
1011 pf->stat_offsets_loaded,
1012 &osd->rx_size_big, &nsd->rx_size_big);
1013
1014 i40e_stat_update48(hw, I40E_GLPRT_PTC64H(hw->port),
1015 I40E_GLPRT_PTC64L(hw->port),
1016 pf->stat_offsets_loaded,
1017 &osd->tx_size_64, &nsd->tx_size_64);
1018 i40e_stat_update48(hw, I40E_GLPRT_PTC127H(hw->port),
1019 I40E_GLPRT_PTC127L(hw->port),
1020 pf->stat_offsets_loaded,
1021 &osd->tx_size_127, &nsd->tx_size_127);
1022 i40e_stat_update48(hw, I40E_GLPRT_PTC255H(hw->port),
1023 I40E_GLPRT_PTC255L(hw->port),
1024 pf->stat_offsets_loaded,
1025 &osd->tx_size_255, &nsd->tx_size_255);
1026 i40e_stat_update48(hw, I40E_GLPRT_PTC511H(hw->port),
1027 I40E_GLPRT_PTC511L(hw->port),
1028 pf->stat_offsets_loaded,
1029 &osd->tx_size_511, &nsd->tx_size_511);
1030 i40e_stat_update48(hw, I40E_GLPRT_PTC1023H(hw->port),
1031 I40E_GLPRT_PTC1023L(hw->port),
1032 pf->stat_offsets_loaded,
1033 &osd->tx_size_1023, &nsd->tx_size_1023);
1034 i40e_stat_update48(hw, I40E_GLPRT_PTC1522H(hw->port),
1035 I40E_GLPRT_PTC1522L(hw->port),
1036 pf->stat_offsets_loaded,
1037 &osd->tx_size_1522, &nsd->tx_size_1522);
1038 i40e_stat_update48(hw, I40E_GLPRT_PTC9522H(hw->port),
1039 I40E_GLPRT_PTC9522L(hw->port),
1040 pf->stat_offsets_loaded,
1041 &osd->tx_size_big, &nsd->tx_size_big);
1042
1043 i40e_stat_update32(hw, I40E_GLPRT_RUC(hw->port),
1044 pf->stat_offsets_loaded,
1045 &osd->rx_undersize, &nsd->rx_undersize);
1046 i40e_stat_update32(hw, I40E_GLPRT_RFC(hw->port),
1047 pf->stat_offsets_loaded,
1048 &osd->rx_fragments, &nsd->rx_fragments);
1049 i40e_stat_update32(hw, I40E_GLPRT_ROC(hw->port),
1050 pf->stat_offsets_loaded,
1051 &osd->rx_oversize, &nsd->rx_oversize);
1052 i40e_stat_update32(hw, I40E_GLPRT_RJC(hw->port),
1053 pf->stat_offsets_loaded,
1054 &osd->rx_jabber, &nsd->rx_jabber);
1055
Anjali Singhai Jain433c47d2014-05-22 06:32:17 +00001056 /* FDIR stats */
Mariusz Stachura0dc86922017-07-14 09:27:00 -04001057 i40e_stat_update_and_clear32(hw,
1058 I40E_GLQF_PCNT(I40E_FD_ATR_STAT_IDX(hw->pf_id)),
1059 &nsd->fd_atr_match);
1060 i40e_stat_update_and_clear32(hw,
1061 I40E_GLQF_PCNT(I40E_FD_SB_STAT_IDX(hw->pf_id)),
1062 &nsd->fd_sb_match);
1063 i40e_stat_update_and_clear32(hw,
1064 I40E_GLQF_PCNT(I40E_FD_ATR_TUNNEL_STAT_IDX(hw->pf_id)),
1065 &nsd->fd_atr_tunnel_match);
Anjali Singhai Jain433c47d2014-05-22 06:32:17 +00001066
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001067 val = rd32(hw, I40E_PRTPM_EEE_STAT);
1068 nsd->tx_lpi_status =
1069 (val & I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_MASK) >>
1070 I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_SHIFT;
1071 nsd->rx_lpi_status =
1072 (val & I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_MASK) >>
1073 I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_SHIFT;
1074 i40e_stat_update32(hw, I40E_PRTPM_TLPIC,
1075 pf->stat_offsets_loaded,
1076 &osd->tx_lpi_count, &nsd->tx_lpi_count);
1077 i40e_stat_update32(hw, I40E_PRTPM_RLPIC,
1078 pf->stat_offsets_loaded,
1079 &osd->rx_lpi_count, &nsd->rx_lpi_count);
1080
Anjali Singhai Jaind0389e52015-04-22 19:34:05 -04001081 if (pf->flags & I40E_FLAG_FD_SB_ENABLED &&
Jacob Keller47994c12017-04-19 09:25:57 -04001082 !(pf->flags & I40E_FLAG_FD_SB_AUTO_DISABLED))
Anjali Singhai Jaind0389e52015-04-22 19:34:05 -04001083 nsd->fd_sb_status = true;
1084 else
1085 nsd->fd_sb_status = false;
1086
1087 if (pf->flags & I40E_FLAG_FD_ATR_ENABLED &&
Jacob Keller47994c12017-04-19 09:25:57 -04001088 !(pf->flags & I40E_FLAG_FD_ATR_AUTO_DISABLED))
Anjali Singhai Jaind0389e52015-04-22 19:34:05 -04001089 nsd->fd_atr_status = true;
1090 else
1091 nsd->fd_atr_status = false;
1092
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001093 pf->stat_offsets_loaded = true;
1094}
1095
1096/**
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001097 * i40e_update_stats - Update the various statistics counters.
1098 * @vsi: the VSI to be updated
1099 *
1100 * Update the various stats for this VSI and its related entities.
1101 **/
1102void i40e_update_stats(struct i40e_vsi *vsi)
1103{
1104 struct i40e_pf *pf = vsi->back;
1105
1106 if (vsi == pf->vsi[pf->lan_vsi])
1107 i40e_update_pf_stats(pf);
1108
1109 i40e_update_vsi_stats(vsi);
1110}
1111
1112/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001113 * i40e_find_filter - Search VSI filter list for specific mac/vlan filter
1114 * @vsi: the VSI to be searched
1115 * @macaddr: the MAC address
1116 * @vlan: the vlan
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001117 *
1118 * Returns ptr to the filter object or NULL
1119 **/
1120static struct i40e_mac_filter *i40e_find_filter(struct i40e_vsi *vsi,
Jacob Keller6622f5c2016-10-05 09:30:32 -07001121 const u8 *macaddr, s16 vlan)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001122{
1123 struct i40e_mac_filter *f;
Jacob Keller278e7d02016-10-05 09:30:37 -07001124 u64 key;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001125
1126 if (!vsi || !macaddr)
1127 return NULL;
1128
Jacob Keller278e7d02016-10-05 09:30:37 -07001129 key = i40e_addr_to_hkey(macaddr);
1130 hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001131 if ((ether_addr_equal(macaddr, f->macaddr)) &&
Jacob Keller1bc87e82016-10-05 09:30:31 -07001132 (vlan == f->vlan))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001133 return f;
1134 }
1135 return NULL;
1136}
1137
1138/**
1139 * i40e_find_mac - Find a mac addr in the macvlan filters list
1140 * @vsi: the VSI to be searched
1141 * @macaddr: the MAC address we are searching for
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001142 *
1143 * Returns the first filter with the provided MAC address or NULL if
1144 * MAC address was not found
1145 **/
Jacob Keller6622f5c2016-10-05 09:30:32 -07001146struct i40e_mac_filter *i40e_find_mac(struct i40e_vsi *vsi, const u8 *macaddr)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001147{
1148 struct i40e_mac_filter *f;
Jacob Keller278e7d02016-10-05 09:30:37 -07001149 u64 key;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001150
1151 if (!vsi || !macaddr)
1152 return NULL;
1153
Jacob Keller278e7d02016-10-05 09:30:37 -07001154 key = i40e_addr_to_hkey(macaddr);
1155 hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
Jacob Keller1bc87e82016-10-05 09:30:31 -07001156 if ((ether_addr_equal(macaddr, f->macaddr)))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001157 return f;
1158 }
1159 return NULL;
1160}
1161
1162/**
1163 * i40e_is_vsi_in_vlan - Check if VSI is in vlan mode
1164 * @vsi: the VSI to be searched
1165 *
1166 * Returns true if VSI is in vlan mode or false otherwise
1167 **/
1168bool i40e_is_vsi_in_vlan(struct i40e_vsi *vsi)
1169{
Jacob Kellercbebb852016-10-05 09:30:40 -07001170 /* If we have a PVID, always operate in VLAN mode */
1171 if (vsi->info.pvid)
1172 return true;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001173
Jacob Kellercbebb852016-10-05 09:30:40 -07001174 /* We need to operate in VLAN mode whenever we have any filters with
1175 * a VLAN other than I40E_VLAN_ALL. We could check the table each
1176 * time, incurring search cost repeatedly. However, we can notice two
1177 * things:
1178 *
1179 * 1) the only place where we can gain a VLAN filter is in
1180 * i40e_add_filter.
1181 *
1182 * 2) the only place where filters are actually removed is in
Jacob Keller0b7c8b52016-10-25 16:08:52 -07001183 * i40e_sync_filters_subtask.
Jacob Kellercbebb852016-10-05 09:30:40 -07001184 *
1185 * Thus, we can simply use a boolean value, has_vlan_filters which we
1186 * will set to true when we add a VLAN filter in i40e_add_filter. Then
1187 * we have to perform the full search after deleting filters in
Jacob Keller0b7c8b52016-10-25 16:08:52 -07001188 * i40e_sync_filters_subtask, but we already have to search
Jacob Kellercbebb852016-10-05 09:30:40 -07001189 * filters here and can perform the check at the same time. This
1190 * results in avoiding embedding a loop for VLAN mode inside another
1191 * loop over all the filters, and should maintain correctness as noted
1192 * above.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001193 */
Jacob Kellercbebb852016-10-05 09:30:40 -07001194 return vsi->has_vlan_filter;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001195}
1196
1197/**
Jacob Keller489a3262016-11-11 12:39:31 -08001198 * i40e_correct_mac_vlan_filters - Correct non-VLAN filters if necessary
1199 * @vsi: the VSI to configure
1200 * @tmp_add_list: list of filters ready to be added
1201 * @tmp_del_list: list of filters ready to be deleted
1202 * @vlan_filters: the number of active VLAN filters
1203 *
1204 * Update VLAN=0 and VLAN=-1 (I40E_VLAN_ANY) filters properly so that they
1205 * behave as expected. If we have any active VLAN filters remaining or about
1206 * to be added then we need to update non-VLAN filters to be marked as VLAN=0
1207 * so that they only match against untagged traffic. If we no longer have any
1208 * active VLAN filters, we need to make all non-VLAN filters marked as VLAN=-1
1209 * so that they match against both tagged and untagged traffic. In this way,
1210 * we ensure that we correctly receive the desired traffic. This ensures that
1211 * when we have an active VLAN we will receive only untagged traffic and
1212 * traffic matching active VLANs. If we have no active VLANs then we will
1213 * operate in non-VLAN mode and receive all traffic, tagged or untagged.
1214 *
1215 * Finally, in a similar fashion, this function also corrects filters when
1216 * there is an active PVID assigned to this VSI.
1217 *
1218 * In case of memory allocation failure return -ENOMEM. Otherwise, return 0.
1219 *
1220 * This function is only expected to be called from within
1221 * i40e_sync_vsi_filters.
1222 *
1223 * NOTE: This function expects to be called while under the
1224 * mac_filter_hash_lock
1225 */
1226static int i40e_correct_mac_vlan_filters(struct i40e_vsi *vsi,
1227 struct hlist_head *tmp_add_list,
1228 struct hlist_head *tmp_del_list,
1229 int vlan_filters)
1230{
Jacob Keller5cb25902016-12-12 15:44:15 -08001231 s16 pvid = le16_to_cpu(vsi->info.pvid);
Jacob Keller489a3262016-11-11 12:39:31 -08001232 struct i40e_mac_filter *f, *add_head;
Jacob Keller671889e2016-12-02 12:33:00 -08001233 struct i40e_new_mac_filter *new;
Jacob Keller489a3262016-11-11 12:39:31 -08001234 struct hlist_node *h;
1235 int bkt, new_vlan;
1236
1237 /* To determine if a particular filter needs to be replaced we
1238 * have the three following conditions:
1239 *
1240 * a) if we have a PVID assigned, then all filters which are
1241 * not marked as VLAN=PVID must be replaced with filters that
1242 * are.
1243 * b) otherwise, if we have any active VLANS, all filters
1244 * which are marked as VLAN=-1 must be replaced with
1245 * filters marked as VLAN=0
1246 * c) finally, if we do not have any active VLANS, all filters
1247 * which are marked as VLAN=0 must be replaced with filters
1248 * marked as VLAN=-1
1249 */
1250
1251 /* Update the filters about to be added in place */
Jacob Keller671889e2016-12-02 12:33:00 -08001252 hlist_for_each_entry(new, tmp_add_list, hlist) {
Jacob Keller5cb25902016-12-12 15:44:15 -08001253 if (pvid && new->f->vlan != pvid)
1254 new->f->vlan = pvid;
Jacob Keller671889e2016-12-02 12:33:00 -08001255 else if (vlan_filters && new->f->vlan == I40E_VLAN_ANY)
1256 new->f->vlan = 0;
1257 else if (!vlan_filters && new->f->vlan == 0)
1258 new->f->vlan = I40E_VLAN_ANY;
Jacob Keller489a3262016-11-11 12:39:31 -08001259 }
1260
1261 /* Update the remaining active filters */
1262 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1263 /* Combine the checks for whether a filter needs to be changed
1264 * and then determine the new VLAN inside the if block, in
1265 * order to avoid duplicating code for adding the new filter
1266 * then deleting the old filter.
1267 */
Jacob Keller5cb25902016-12-12 15:44:15 -08001268 if ((pvid && f->vlan != pvid) ||
Jacob Keller489a3262016-11-11 12:39:31 -08001269 (vlan_filters && f->vlan == I40E_VLAN_ANY) ||
1270 (!vlan_filters && f->vlan == 0)) {
1271 /* Determine the new vlan we will be adding */
Jacob Keller5cb25902016-12-12 15:44:15 -08001272 if (pvid)
1273 new_vlan = pvid;
Jacob Keller489a3262016-11-11 12:39:31 -08001274 else if (vlan_filters)
1275 new_vlan = 0;
1276 else
1277 new_vlan = I40E_VLAN_ANY;
1278
1279 /* Create the new filter */
1280 add_head = i40e_add_filter(vsi, f->macaddr, new_vlan);
1281 if (!add_head)
1282 return -ENOMEM;
1283
Jacob Keller671889e2016-12-02 12:33:00 -08001284 /* Create a temporary i40e_new_mac_filter */
1285 new = kzalloc(sizeof(*new), GFP_ATOMIC);
1286 if (!new)
1287 return -ENOMEM;
1288
1289 new->f = add_head;
1290 new->state = add_head->state;
1291
1292 /* Add the new filter to the tmp list */
1293 hlist_add_head(&new->hlist, tmp_add_list);
Jacob Keller489a3262016-11-11 12:39:31 -08001294
1295 /* Put the original filter into the delete list */
1296 f->state = I40E_FILTER_REMOVE;
1297 hash_del(&f->hlist);
1298 hlist_add_head(&f->hlist, tmp_del_list);
1299 }
1300 }
1301
1302 vsi->has_vlan_filter = !!vlan_filters;
1303
1304 return 0;
1305}
1306
1307/**
Jacob Keller1596b5d2016-11-08 13:05:15 -08001308 * i40e_rm_default_mac_filter - Remove the default MAC filter set by NVM
1309 * @vsi: the PF Main VSI - inappropriate for any other VSI
1310 * @macaddr: the MAC address
1311 *
1312 * Remove whatever filter the firmware set up so the driver can manage
1313 * its own filtering intelligently.
1314 **/
1315static void i40e_rm_default_mac_filter(struct i40e_vsi *vsi, u8 *macaddr)
1316{
1317 struct i40e_aqc_remove_macvlan_element_data element;
1318 struct i40e_pf *pf = vsi->back;
1319
1320 /* Only appropriate for the PF main VSI */
1321 if (vsi->type != I40E_VSI_MAIN)
1322 return;
1323
1324 memset(&element, 0, sizeof(element));
1325 ether_addr_copy(element.mac_addr, macaddr);
1326 element.vlan_tag = 0;
1327 /* Ignore error returns, some firmware does it this way... */
1328 element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
1329 i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1330
1331 memset(&element, 0, sizeof(element));
1332 ether_addr_copy(element.mac_addr, macaddr);
1333 element.vlan_tag = 0;
1334 /* ...and some firmware does it this way. */
1335 element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH |
1336 I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
1337 i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1338}
1339
1340/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001341 * i40e_add_filter - Add a mac/vlan filter to the VSI
1342 * @vsi: the VSI to be searched
1343 * @macaddr: the MAC address
1344 * @vlan: the vlan
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001345 *
1346 * Returns ptr to the filter object or NULL when no memory available.
Kiran Patil21659032015-09-30 14:09:03 -04001347 *
Jacob Keller278e7d02016-10-05 09:30:37 -07001348 * NOTE: This function is expected to be called with mac_filter_hash_lock
Kiran Patil21659032015-09-30 14:09:03 -04001349 * being held.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001350 **/
1351struct i40e_mac_filter *i40e_add_filter(struct i40e_vsi *vsi,
Jacob Keller6622f5c2016-10-05 09:30:32 -07001352 const u8 *macaddr, s16 vlan)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001353{
1354 struct i40e_mac_filter *f;
Jacob Keller278e7d02016-10-05 09:30:37 -07001355 u64 key;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001356
1357 if (!vsi || !macaddr)
1358 return NULL;
1359
Jacob Keller1bc87e82016-10-05 09:30:31 -07001360 f = i40e_find_filter(vsi, macaddr, vlan);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001361 if (!f) {
1362 f = kzalloc(sizeof(*f), GFP_ATOMIC);
1363 if (!f)
Jacob Keller1bc87e82016-10-05 09:30:31 -07001364 return NULL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001365
Jacob Kellercbebb852016-10-05 09:30:40 -07001366 /* Update the boolean indicating if we need to function in
1367 * VLAN mode.
1368 */
1369 if (vlan >= 0)
1370 vsi->has_vlan_filter = true;
1371
Greg Rose9a173902014-05-22 06:32:02 +00001372 ether_addr_copy(f->macaddr, macaddr);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001373 f->vlan = vlan;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001374 /* If we're in overflow promisc mode, set the state directly
1375 * to failed, so we don't bother to try sending the filter
1376 * to the hardware.
1377 */
Jacob Keller0da36b92017-04-19 09:25:55 -04001378 if (test_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state))
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001379 f->state = I40E_FILTER_FAILED;
1380 else
1381 f->state = I40E_FILTER_NEW;
Jacob Keller278e7d02016-10-05 09:30:37 -07001382 INIT_HLIST_NODE(&f->hlist);
1383
1384 key = i40e_addr_to_hkey(macaddr);
1385 hash_add(vsi->mac_filter_hash, &f->hlist, key);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001386
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001387 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1388 vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
1389 }
1390
Jacob Keller1bc87e82016-10-05 09:30:31 -07001391 /* If we're asked to add a filter that has been marked for removal, it
1392 * is safe to simply restore it to active state. __i40e_del_filter
1393 * will have simply deleted any filters which were previously marked
1394 * NEW or FAILED, so if it is currently marked REMOVE it must have
1395 * previously been ACTIVE. Since we haven't yet run the sync filters
1396 * task, just restore this filter to the ACTIVE state so that the
1397 * sync task leaves it in place
1398 */
1399 if (f->state == I40E_FILTER_REMOVE)
1400 f->state = I40E_FILTER_ACTIVE;
1401
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001402 return f;
1403}
1404
1405/**
Jacob Keller290d2552016-10-05 09:30:36 -07001406 * __i40e_del_filter - Remove a specific filter from the VSI
1407 * @vsi: VSI to remove from
1408 * @f: the filter to remove from the list
1409 *
1410 * This function should be called instead of i40e_del_filter only if you know
1411 * the exact filter you will remove already, such as via i40e_find_filter or
1412 * i40e_find_mac.
Kiran Patil21659032015-09-30 14:09:03 -04001413 *
Jacob Keller278e7d02016-10-05 09:30:37 -07001414 * NOTE: This function is expected to be called with mac_filter_hash_lock
Kiran Patil21659032015-09-30 14:09:03 -04001415 * being held.
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001416 * ANOTHER NOTE: This function MUST be called from within the context of
1417 * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
1418 * instead of list_for_each_entry().
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001419 **/
Jacob Keller148141b2016-11-11 12:39:36 -08001420void __i40e_del_filter(struct i40e_vsi *vsi, struct i40e_mac_filter *f)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001421{
Jacob Keller1bc87e82016-10-05 09:30:31 -07001422 if (!f)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001423 return;
1424
Alan Bradya410c822016-12-12 15:44:07 -08001425 /* If the filter was never added to firmware then we can just delete it
1426 * directly and we don't want to set the status to remove or else an
1427 * admin queue command will unnecessarily fire.
1428 */
Jacob Keller1bc87e82016-10-05 09:30:31 -07001429 if ((f->state == I40E_FILTER_FAILED) ||
1430 (f->state == I40E_FILTER_NEW)) {
Jacob Keller278e7d02016-10-05 09:30:37 -07001431 hash_del(&f->hlist);
Jacob Keller1bc87e82016-10-05 09:30:31 -07001432 kfree(f);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001433 } else {
Jacob Keller1bc87e82016-10-05 09:30:31 -07001434 f->state = I40E_FILTER_REMOVE;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001435 }
Alan Bradya410c822016-12-12 15:44:07 -08001436
1437 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1438 vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001439}
1440
1441/**
Jacob Keller290d2552016-10-05 09:30:36 -07001442 * i40e_del_filter - Remove a MAC/VLAN filter from the VSI
1443 * @vsi: the VSI to be searched
1444 * @macaddr: the MAC address
1445 * @vlan: the VLAN
1446 *
Jacob Keller278e7d02016-10-05 09:30:37 -07001447 * NOTE: This function is expected to be called with mac_filter_hash_lock
Jacob Keller290d2552016-10-05 09:30:36 -07001448 * being held.
1449 * ANOTHER NOTE: This function MUST be called from within the context of
1450 * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
1451 * instead of list_for_each_entry().
1452 **/
1453void i40e_del_filter(struct i40e_vsi *vsi, const u8 *macaddr, s16 vlan)
1454{
1455 struct i40e_mac_filter *f;
1456
1457 if (!vsi || !macaddr)
1458 return;
1459
1460 f = i40e_find_filter(vsi, macaddr, vlan);
1461 __i40e_del_filter(vsi, f);
1462}
1463
1464/**
Jacob Kellerfeffdbe2016-11-11 12:39:35 -08001465 * i40e_add_mac_filter - Add a MAC filter for all active VLANs
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001466 * @vsi: the VSI to be searched
1467 * @macaddr: the mac address to be filtered
1468 *
Jacob Kellerfeffdbe2016-11-11 12:39:35 -08001469 * If we're not in VLAN mode, just add the filter to I40E_VLAN_ANY. Otherwise,
1470 * go through all the macvlan filters and add a macvlan filter for each
Jacob Keller5feb3d72016-10-05 09:30:34 -07001471 * unique vlan that already exists. If a PVID has been assigned, instead only
1472 * add the macaddr to that VLAN.
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001473 *
Jacob Keller5feb3d72016-10-05 09:30:34 -07001474 * Returns last filter added on success, else NULL
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001475 **/
Jacob Kellerfeffdbe2016-11-11 12:39:35 -08001476struct i40e_mac_filter *i40e_add_mac_filter(struct i40e_vsi *vsi,
1477 const u8 *macaddr)
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001478{
Jacob Keller5feb3d72016-10-05 09:30:34 -07001479 struct i40e_mac_filter *f, *add = NULL;
Jacob Keller278e7d02016-10-05 09:30:37 -07001480 struct hlist_node *h;
1481 int bkt;
Jacob Keller5feb3d72016-10-05 09:30:34 -07001482
1483 if (vsi->info.pvid)
1484 return i40e_add_filter(vsi, macaddr,
1485 le16_to_cpu(vsi->info.pvid));
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001486
Jacob Keller7aaf95362016-11-11 12:39:33 -08001487 if (!i40e_is_vsi_in_vlan(vsi))
1488 return i40e_add_filter(vsi, macaddr, I40E_VLAN_ANY);
1489
Jacob Keller278e7d02016-10-05 09:30:37 -07001490 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Jacob Keller57b341d2016-10-05 09:30:35 -07001491 if (f->state == I40E_FILTER_REMOVE)
1492 continue;
Jacob Keller5feb3d72016-10-05 09:30:34 -07001493 add = i40e_add_filter(vsi, macaddr, f->vlan);
1494 if (!add)
1495 return NULL;
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001496 }
1497
Jacob Keller5feb3d72016-10-05 09:30:34 -07001498 return add;
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001499}
1500
1501/**
Jacob Kellerfeffdbe2016-11-11 12:39:35 -08001502 * i40e_del_mac_filter - Remove a MAC filter from all VLANs
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001503 * @vsi: the VSI to be searched
1504 * @macaddr: the mac address to be removed
1505 *
Jacob Kellerfeffdbe2016-11-11 12:39:35 -08001506 * Removes a given MAC address from a VSI regardless of what VLAN it has been
1507 * associated with.
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001508 *
1509 * Returns 0 for success, or error
1510 **/
Jacob Kellerfeffdbe2016-11-11 12:39:35 -08001511int i40e_del_mac_filter(struct i40e_vsi *vsi, const u8 *macaddr)
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001512{
Jacob Keller278e7d02016-10-05 09:30:37 -07001513 struct i40e_mac_filter *f;
1514 struct hlist_node *h;
Jacob Keller290d2552016-10-05 09:30:36 -07001515 bool found = false;
Jacob Keller278e7d02016-10-05 09:30:37 -07001516 int bkt;
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001517
Jacob Keller278e7d02016-10-05 09:30:37 -07001518 WARN(!spin_is_locked(&vsi->mac_filter_hash_lock),
1519 "Missing mac_filter_hash_lock\n");
1520 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Jacob Keller290d2552016-10-05 09:30:36 -07001521 if (ether_addr_equal(macaddr, f->macaddr)) {
1522 __i40e_del_filter(vsi, f);
1523 found = true;
1524 }
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001525 }
Jacob Keller290d2552016-10-05 09:30:36 -07001526
1527 if (found)
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001528 return 0;
Jacob Keller290d2552016-10-05 09:30:36 -07001529 else
1530 return -ENOENT;
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001531}
1532
1533/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001534 * i40e_set_mac - NDO callback to set mac address
1535 * @netdev: network interface device structure
1536 * @p: pointer to an address structure
1537 *
1538 * Returns 0 on success, negative on failure
1539 **/
1540static int i40e_set_mac(struct net_device *netdev, void *p)
1541{
1542 struct i40e_netdev_priv *np = netdev_priv(netdev);
1543 struct i40e_vsi *vsi = np->vsi;
Shannon Nelson30650cc2014-07-29 04:01:50 +00001544 struct i40e_pf *pf = vsi->back;
1545 struct i40e_hw *hw = &pf->hw;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001546 struct sockaddr *addr = p;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001547
1548 if (!is_valid_ether_addr(addr->sa_data))
1549 return -EADDRNOTAVAIL;
1550
Shannon Nelson30650cc2014-07-29 04:01:50 +00001551 if (ether_addr_equal(netdev->dev_addr, addr->sa_data)) {
1552 netdev_info(netdev, "already using mac address %pM\n",
1553 addr->sa_data);
1554 return 0;
1555 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001556
Jacob Keller0da36b92017-04-19 09:25:55 -04001557 if (test_bit(__I40E_VSI_DOWN, vsi->back->state) ||
1558 test_bit(__I40E_RESET_RECOVERY_PENDING, vsi->back->state))
Anjali Singhai Jain80f64282013-11-28 06:39:47 +00001559 return -EADDRNOTAVAIL;
1560
Shannon Nelson30650cc2014-07-29 04:01:50 +00001561 if (ether_addr_equal(hw->mac.addr, addr->sa_data))
1562 netdev_info(netdev, "returning to hw mac address %pM\n",
1563 hw->mac.addr);
1564 else
1565 netdev_info(netdev, "set new mac address %pM\n", addr->sa_data);
1566
Jacob Keller278e7d02016-10-05 09:30:37 -07001567 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Kellerfeffdbe2016-11-11 12:39:35 -08001568 i40e_del_mac_filter(vsi, netdev->dev_addr);
1569 i40e_add_mac_filter(vsi, addr->sa_data);
Jacob Keller278e7d02016-10-05 09:30:37 -07001570 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001571 ether_addr_copy(netdev->dev_addr, addr->sa_data);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001572 if (vsi->type == I40E_VSI_MAIN) {
1573 i40e_status ret;
Jesse Brandeburg6995b362015-08-28 17:55:54 -04001574
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001575 ret = i40e_aq_mac_address_write(&vsi->back->hw,
Shannon Nelsoncc412222014-06-04 01:23:21 +00001576 I40E_AQC_WRITE_TYPE_LAA_WOL,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001577 addr->sa_data, NULL);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001578 if (ret)
1579 netdev_info(netdev, "Ignoring error from firmware on LAA update, status %s, AQ ret %s\n",
1580 i40e_stat_str(hw, ret),
1581 i40e_aq_str(hw, hw->aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001582 }
1583
Jesse Brandeburgc53934c2016-01-04 10:33:06 -08001584 /* schedule our worker thread which will take care of
1585 * applying the new filter changes
1586 */
1587 i40e_service_event_schedule(vsi->back);
1588 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001589}
1590
1591/**
1592 * i40e_vsi_setup_queue_map - Setup a VSI queue map based on enabled_tc
1593 * @vsi: the VSI being setup
1594 * @ctxt: VSI context structure
1595 * @enabled_tc: Enabled TCs bitmap
1596 * @is_add: True if called before Add VSI
1597 *
1598 * Setup VSI queue mapping for enabled traffic classes.
1599 **/
1600static void i40e_vsi_setup_queue_map(struct i40e_vsi *vsi,
1601 struct i40e_vsi_context *ctxt,
1602 u8 enabled_tc,
1603 bool is_add)
1604{
1605 struct i40e_pf *pf = vsi->back;
1606 u16 sections = 0;
1607 u8 netdev_tc = 0;
1608 u16 numtc = 0;
1609 u16 qcount;
1610 u8 offset;
1611 u16 qmap;
1612 int i;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001613 u16 num_tc_qps = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001614
1615 sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
1616 offset = 0;
1617
1618 if (enabled_tc && (vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
1619 /* Find numtc from enabled TC bitmap */
1620 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08001621 if (enabled_tc & BIT(i)) /* TC is enabled */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001622 numtc++;
1623 }
1624 if (!numtc) {
1625 dev_warn(&pf->pdev->dev, "DCB is enabled but no TC enabled, forcing TC0\n");
1626 numtc = 1;
1627 }
1628 } else {
1629 /* At least TC0 is enabled in case of non-DCB case */
1630 numtc = 1;
1631 }
1632
1633 vsi->tc_config.numtc = numtc;
1634 vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001635 /* Number of queues per enabled TC */
Catherine Sullivan7d644022016-05-16 10:26:41 -07001636 qcount = vsi->alloc_queue_pairs;
1637
Anjali Singhai7f9ff472015-02-21 06:43:19 +00001638 num_tc_qps = qcount / numtc;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001639 num_tc_qps = min_t(int, num_tc_qps, i40e_pf_get_max_q_per_tc(pf));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001640
1641 /* Setup queue offset/count for all TCs for given VSI */
1642 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1643 /* See if the given TC is enabled for the given VSI */
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08001644 if (vsi->tc_config.enabled_tc & BIT(i)) {
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04001645 /* TC is enabled */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001646 int pow, num_qps;
1647
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001648 switch (vsi->type) {
1649 case I40E_VSI_MAIN:
Helin Zhangacd65442015-10-26 19:44:28 -04001650 qcount = min_t(int, pf->alloc_rss_size,
1651 num_tc_qps);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001652 break;
1653 case I40E_VSI_FDIR:
1654 case I40E_VSI_SRIOV:
1655 case I40E_VSI_VMDQ2:
1656 default:
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001657 qcount = num_tc_qps;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001658 WARN_ON(i != 0);
1659 break;
1660 }
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001661 vsi->tc_config.tc_info[i].qoffset = offset;
1662 vsi->tc_config.tc_info[i].qcount = qcount;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001663
Shannon Nelson1e200e42015-02-27 09:15:24 +00001664 /* find the next higher power-of-2 of num queue pairs */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001665 num_qps = qcount;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001666 pow = 0;
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04001667 while (num_qps && (BIT_ULL(pow) < qcount)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001668 pow++;
1669 num_qps >>= 1;
1670 }
1671
1672 vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
1673 qmap =
1674 (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
1675 (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
1676
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001677 offset += qcount;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001678 } else {
1679 /* TC is not enabled so set the offset to
1680 * default queue and allocate one queue
1681 * for the given TC.
1682 */
1683 vsi->tc_config.tc_info[i].qoffset = 0;
1684 vsi->tc_config.tc_info[i].qcount = 1;
1685 vsi->tc_config.tc_info[i].netdev_tc = 0;
1686
1687 qmap = 0;
1688 }
1689 ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
1690 }
1691
1692 /* Set actual Tx/Rx queue pairs */
1693 vsi->num_queue_pairs = offset;
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00001694 if ((vsi->type == I40E_VSI_MAIN) && (numtc == 1)) {
1695 if (vsi->req_queue_pairs > 0)
1696 vsi->num_queue_pairs = vsi->req_queue_pairs;
Anjali Singhai Jain26cdc442015-07-10 19:36:00 -04001697 else if (pf->flags & I40E_FLAG_MSIX_ENABLED)
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00001698 vsi->num_queue_pairs = pf->num_lan_msix;
1699 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001700
1701 /* Scheduler section valid can only be set for ADD VSI */
1702 if (is_add) {
1703 sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
1704
1705 ctxt->info.up_enable_bits = enabled_tc;
1706 }
1707 if (vsi->type == I40E_VSI_SRIOV) {
1708 ctxt->info.mapping_flags |=
1709 cpu_to_le16(I40E_AQ_VSI_QUE_MAP_NONCONTIG);
1710 for (i = 0; i < vsi->num_queue_pairs; i++)
1711 ctxt->info.queue_mapping[i] =
1712 cpu_to_le16(vsi->base_queue + i);
1713 } else {
1714 ctxt->info.mapping_flags |=
1715 cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
1716 ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
1717 }
1718 ctxt->info.valid_sections |= cpu_to_le16(sections);
1719}
1720
1721/**
Jacob Keller6622f5c2016-10-05 09:30:32 -07001722 * i40e_addr_sync - Callback for dev_(mc|uc)_sync to add address
1723 * @netdev: the netdevice
1724 * @addr: address to add
1725 *
1726 * Called by __dev_(mc|uc)_sync when an address needs to be added. We call
1727 * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
1728 */
1729static int i40e_addr_sync(struct net_device *netdev, const u8 *addr)
1730{
1731 struct i40e_netdev_priv *np = netdev_priv(netdev);
1732 struct i40e_vsi *vsi = np->vsi;
Jacob Keller6622f5c2016-10-05 09:30:32 -07001733
Jacob Kellerfeffdbe2016-11-11 12:39:35 -08001734 if (i40e_add_mac_filter(vsi, addr))
Jacob Keller6622f5c2016-10-05 09:30:32 -07001735 return 0;
1736 else
1737 return -ENOMEM;
1738}
1739
1740/**
1741 * i40e_addr_unsync - Callback for dev_(mc|uc)_sync to remove address
1742 * @netdev: the netdevice
1743 * @addr: address to add
1744 *
1745 * Called by __dev_(mc|uc)_sync when an address needs to be removed. We call
1746 * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
1747 */
1748static int i40e_addr_unsync(struct net_device *netdev, const u8 *addr)
1749{
1750 struct i40e_netdev_priv *np = netdev_priv(netdev);
1751 struct i40e_vsi *vsi = np->vsi;
1752
Jacob Kellerfeffdbe2016-11-11 12:39:35 -08001753 i40e_del_mac_filter(vsi, addr);
Jacob Keller6622f5c2016-10-05 09:30:32 -07001754
1755 return 0;
1756}
1757
1758/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001759 * i40e_set_rx_mode - NDO callback to set the netdev filters
1760 * @netdev: network interface device structure
1761 **/
1762static void i40e_set_rx_mode(struct net_device *netdev)
1763{
1764 struct i40e_netdev_priv *np = netdev_priv(netdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001765 struct i40e_vsi *vsi = np->vsi;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001766
Jacob Keller278e7d02016-10-05 09:30:37 -07001767 spin_lock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -04001768
Jacob Keller6622f5c2016-10-05 09:30:32 -07001769 __dev_uc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
1770 __dev_mc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001771
Jacob Keller278e7d02016-10-05 09:30:37 -07001772 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001773
1774 /* check for other flag changes */
1775 if (vsi->current_netdev_flags != vsi->netdev->flags) {
1776 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1777 vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
1778 }
Jesse Brandeburgc53934c2016-01-04 10:33:06 -08001779
1780 /* schedule our worker thread which will take care of
1781 * applying the new filter changes
1782 */
1783 i40e_service_event_schedule(vsi->back);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001784}
1785
1786/**
Jacob Keller671889e2016-12-02 12:33:00 -08001787 * i40e_undo_del_filter_entries - Undo the changes made to MAC filter entries
Jacob Keller4a2ce272016-10-05 09:30:38 -07001788 * @vsi: Pointer to VSI struct
Kiran Patil21659032015-09-30 14:09:03 -04001789 * @from: Pointer to list which contains MAC filter entries - changes to
1790 * those entries needs to be undone.
1791 *
Jacob Keller671889e2016-12-02 12:33:00 -08001792 * MAC filter entries from this list were slated for deletion.
Kiran Patil21659032015-09-30 14:09:03 -04001793 **/
Jacob Keller671889e2016-12-02 12:33:00 -08001794static void i40e_undo_del_filter_entries(struct i40e_vsi *vsi,
1795 struct hlist_head *from)
Kiran Patil21659032015-09-30 14:09:03 -04001796{
Jacob Keller278e7d02016-10-05 09:30:37 -07001797 struct i40e_mac_filter *f;
1798 struct hlist_node *h;
Kiran Patil21659032015-09-30 14:09:03 -04001799
Jacob Keller278e7d02016-10-05 09:30:37 -07001800 hlist_for_each_entry_safe(f, h, from, hlist) {
1801 u64 key = i40e_addr_to_hkey(f->macaddr);
1802
Kiran Patil21659032015-09-30 14:09:03 -04001803 /* Move the element back into MAC filter list*/
Jacob Keller278e7d02016-10-05 09:30:37 -07001804 hlist_del(&f->hlist);
1805 hash_add(vsi->mac_filter_hash, &f->hlist, key);
Kiran Patil21659032015-09-30 14:09:03 -04001806 }
1807}
1808
1809/**
Jacob Keller671889e2016-12-02 12:33:00 -08001810 * i40e_undo_add_filter_entries - Undo the changes made to MAC filter entries
1811 * @vsi: Pointer to vsi struct
1812 * @from: Pointer to list which contains MAC filter entries - changes to
1813 * those entries needs to be undone.
1814 *
1815 * MAC filter entries from this list were slated for addition.
1816 **/
1817static void i40e_undo_add_filter_entries(struct i40e_vsi *vsi,
1818 struct hlist_head *from)
1819{
1820 struct i40e_new_mac_filter *new;
1821 struct hlist_node *h;
1822
1823 hlist_for_each_entry_safe(new, h, from, hlist) {
1824 /* We can simply free the wrapper structure */
1825 hlist_del(&new->hlist);
1826 kfree(new);
1827 }
1828}
1829
1830/**
Jacob Kellerd88d40b2016-12-02 12:32:59 -08001831 * i40e_next_entry - Get the next non-broadcast filter from a list
Jacob Keller671889e2016-12-02 12:33:00 -08001832 * @next: pointer to filter in list
Jacob Kellerd88d40b2016-12-02 12:32:59 -08001833 *
1834 * Returns the next non-broadcast filter in the list. Required so that we
1835 * ignore broadcast filters within the list, since these are not handled via
1836 * the normal firmware update path.
1837 */
Jacob Keller671889e2016-12-02 12:33:00 -08001838static
1839struct i40e_new_mac_filter *i40e_next_filter(struct i40e_new_mac_filter *next)
Jacob Kellerd88d40b2016-12-02 12:32:59 -08001840{
Jacob Keller584a8872017-03-07 15:17:52 -08001841 hlist_for_each_entry_continue(next, hlist) {
1842 if (!is_broadcast_ether_addr(next->f->macaddr))
1843 return next;
Jacob Kellerd88d40b2016-12-02 12:32:59 -08001844 }
1845
Jacob Keller584a8872017-03-07 15:17:52 -08001846 return NULL;
Jacob Kellerd88d40b2016-12-02 12:32:59 -08001847}
1848
1849/**
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001850 * i40e_update_filter_state - Update filter state based on return data
1851 * from firmware
1852 * @count: Number of filters added
1853 * @add_list: return data from fw
1854 * @head: pointer to first filter in current batch
Kiran Patil21659032015-09-30 14:09:03 -04001855 *
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001856 * MAC filter entries from list were slated to be added to device. Returns
1857 * number of successful filters. Note that 0 does NOT mean success!
Kiran Patil21659032015-09-30 14:09:03 -04001858 **/
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001859static int
1860i40e_update_filter_state(int count,
1861 struct i40e_aqc_add_macvlan_element_data *add_list,
Jacob Keller671889e2016-12-02 12:33:00 -08001862 struct i40e_new_mac_filter *add_head)
Kiran Patil21659032015-09-30 14:09:03 -04001863{
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001864 int retval = 0;
1865 int i;
Kiran Patil21659032015-09-30 14:09:03 -04001866
Jacob Kellerac9e2392016-11-11 12:39:27 -08001867 for (i = 0; i < count; i++) {
1868 /* Always check status of each filter. We don't need to check
1869 * the firmware return status because we pre-set the filter
1870 * status to I40E_AQC_MM_ERR_NO_RES when sending the filter
1871 * request to the adminq. Thus, if it no longer matches then
1872 * we know the filter is active.
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001873 */
Jacob Kellerac9e2392016-11-11 12:39:27 -08001874 if (add_list[i].match_method == I40E_AQC_MM_ERR_NO_RES) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001875 add_head->state = I40E_FILTER_FAILED;
Jacob Kellerac9e2392016-11-11 12:39:27 -08001876 } else {
1877 add_head->state = I40E_FILTER_ACTIVE;
1878 retval++;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001879 }
Jacob Kellerac9e2392016-11-11 12:39:27 -08001880
Jacob Kellerd88d40b2016-12-02 12:32:59 -08001881 add_head = i40e_next_filter(add_head);
1882 if (!add_head)
1883 break;
Kiran Patil21659032015-09-30 14:09:03 -04001884 }
Jacob Kellerac9e2392016-11-11 12:39:27 -08001885
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001886 return retval;
Kiran Patil21659032015-09-30 14:09:03 -04001887}
1888
1889/**
Jacob Keller00936312016-10-05 09:30:41 -07001890 * i40e_aqc_del_filters - Request firmware to delete a set of filters
1891 * @vsi: ptr to the VSI
1892 * @vsi_name: name to display in messages
1893 * @list: the list of filters to send to firmware
1894 * @num_del: the number of filters to delete
1895 * @retval: Set to -EIO on failure to delete
1896 *
1897 * Send a request to firmware via AdminQ to delete a set of filters. Uses
1898 * *retval instead of a return value so that success does not force ret_val to
1899 * be set to 0. This ensures that a sequence of calls to this function
1900 * preserve the previous value of *retval on successful delete.
1901 */
1902static
1903void i40e_aqc_del_filters(struct i40e_vsi *vsi, const char *vsi_name,
1904 struct i40e_aqc_remove_macvlan_element_data *list,
1905 int num_del, int *retval)
1906{
1907 struct i40e_hw *hw = &vsi->back->hw;
1908 i40e_status aq_ret;
1909 int aq_err;
1910
1911 aq_ret = i40e_aq_remove_macvlan(hw, vsi->seid, list, num_del, NULL);
1912 aq_err = hw->aq.asq_last_status;
1913
1914 /* Explicitly ignore and do not report when firmware returns ENOENT */
1915 if (aq_ret && !(aq_err == I40E_AQ_RC_ENOENT)) {
1916 *retval = -EIO;
1917 dev_info(&vsi->back->pdev->dev,
1918 "ignoring delete macvlan error on %s, err %s, aq_err %s\n",
1919 vsi_name, i40e_stat_str(hw, aq_ret),
1920 i40e_aq_str(hw, aq_err));
1921 }
1922}
1923
1924/**
1925 * i40e_aqc_add_filters - Request firmware to add a set of filters
1926 * @vsi: ptr to the VSI
1927 * @vsi_name: name to display in messages
1928 * @list: the list of filters to send to firmware
1929 * @add_head: Position in the add hlist
1930 * @num_add: the number of filters to add
1931 * @promisc_change: set to true on exit if promiscuous mode was forced on
1932 *
1933 * Send a request to firmware via AdminQ to add a chunk of filters. Will set
1934 * promisc_changed to true if the firmware has run out of space for more
1935 * filters.
1936 */
1937static
1938void i40e_aqc_add_filters(struct i40e_vsi *vsi, const char *vsi_name,
1939 struct i40e_aqc_add_macvlan_element_data *list,
Jacob Keller671889e2016-12-02 12:33:00 -08001940 struct i40e_new_mac_filter *add_head,
Jacob Keller00936312016-10-05 09:30:41 -07001941 int num_add, bool *promisc_changed)
1942{
1943 struct i40e_hw *hw = &vsi->back->hw;
Jacob Keller00936312016-10-05 09:30:41 -07001944 int aq_err, fcnt;
1945
Jacob Kellerac9e2392016-11-11 12:39:27 -08001946 i40e_aq_add_macvlan(hw, vsi->seid, list, num_add, NULL);
Jacob Keller00936312016-10-05 09:30:41 -07001947 aq_err = hw->aq.asq_last_status;
Jacob Kellerac9e2392016-11-11 12:39:27 -08001948 fcnt = i40e_update_filter_state(num_add, list, add_head);
Jacob Keller00936312016-10-05 09:30:41 -07001949
1950 if (fcnt != num_add) {
1951 *promisc_changed = true;
Jacob Keller0da36b92017-04-19 09:25:55 -04001952 set_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
Jacob Keller00936312016-10-05 09:30:41 -07001953 dev_warn(&vsi->back->pdev->dev,
1954 "Error %s adding RX filters on %s, promiscuous mode forced on\n",
1955 i40e_aq_str(hw, aq_err),
1956 vsi_name);
1957 }
1958}
1959
1960/**
Jacob Keller435c0842016-11-08 13:05:10 -08001961 * i40e_aqc_broadcast_filter - Set promiscuous broadcast flags
1962 * @vsi: pointer to the VSI
1963 * @f: filter data
1964 *
1965 * This function sets or clears the promiscuous broadcast flags for VLAN
1966 * filters in order to properly receive broadcast frames. Assumes that only
1967 * broadcast filters are passed.
Jacob Keller671889e2016-12-02 12:33:00 -08001968 *
1969 * Returns status indicating success or failure;
Jacob Keller435c0842016-11-08 13:05:10 -08001970 **/
Jacob Keller671889e2016-12-02 12:33:00 -08001971static i40e_status
1972i40e_aqc_broadcast_filter(struct i40e_vsi *vsi, const char *vsi_name,
1973 struct i40e_mac_filter *f)
Jacob Keller435c0842016-11-08 13:05:10 -08001974{
1975 bool enable = f->state == I40E_FILTER_NEW;
1976 struct i40e_hw *hw = &vsi->back->hw;
1977 i40e_status aq_ret;
1978
1979 if (f->vlan == I40E_VLAN_ANY) {
1980 aq_ret = i40e_aq_set_vsi_broadcast(hw,
1981 vsi->seid,
1982 enable,
1983 NULL);
1984 } else {
1985 aq_ret = i40e_aq_set_vsi_bc_promisc_on_vlan(hw,
1986 vsi->seid,
1987 enable,
1988 f->vlan,
1989 NULL);
1990 }
1991
Jacob Keller671889e2016-12-02 12:33:00 -08001992 if (aq_ret)
Jacob Keller435c0842016-11-08 13:05:10 -08001993 dev_warn(&vsi->back->pdev->dev,
1994 "Error %s setting broadcast promiscuous mode on %s\n",
1995 i40e_aq_str(hw, hw->aq.asq_last_status),
1996 vsi_name);
Jacob Keller671889e2016-12-02 12:33:00 -08001997
1998 return aq_ret;
Jacob Keller435c0842016-11-08 13:05:10 -08001999}
2000
2001/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002002 * i40e_sync_vsi_filters - Update the VSI filter list to the HW
2003 * @vsi: ptr to the VSI
2004 *
2005 * Push any outstanding VSI filter changes through the AdminQ.
2006 *
2007 * Returns 0 or error value
2008 **/
Jesse Brandeburg17652c62015-11-05 17:01:02 -08002009int i40e_sync_vsi_filters(struct i40e_vsi *vsi)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002010{
Jacob Keller278e7d02016-10-05 09:30:37 -07002011 struct hlist_head tmp_add_list, tmp_del_list;
Jacob Keller671889e2016-12-02 12:33:00 -08002012 struct i40e_mac_filter *f;
2013 struct i40e_new_mac_filter *new, *add_head = NULL;
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002014 struct i40e_hw *hw = &vsi->back->hw;
Jacob Keller38326212016-11-11 12:39:26 -08002015 unsigned int failed_filters = 0;
Alan Brady84f5ca62016-10-05 09:30:39 -07002016 unsigned int vlan_filters = 0;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002017 bool promisc_changed = false;
Shannon Nelson2d1de822016-05-16 10:26:44 -07002018 char vsi_name[16] = "PF";
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002019 int filter_list_len = 0;
Mitch Williamsea02e902015-11-09 15:35:50 -08002020 i40e_status aq_ret = 0;
Alan Brady84f5ca62016-10-05 09:30:39 -07002021 u32 changed_flags = 0;
Jacob Keller278e7d02016-10-05 09:30:37 -07002022 struct hlist_node *h;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002023 struct i40e_pf *pf;
2024 int num_add = 0;
2025 int num_del = 0;
Alan Brady84f5ca62016-10-05 09:30:39 -07002026 int retval = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002027 u16 cmd_flags;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002028 int list_size;
Jacob Keller278e7d02016-10-05 09:30:37 -07002029 int bkt;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002030
2031 /* empty array typed pointers, kcalloc later */
2032 struct i40e_aqc_add_macvlan_element_data *add_list;
2033 struct i40e_aqc_remove_macvlan_element_data *del_list;
2034
Jacob Keller0da36b92017-04-19 09:25:55 -04002035 while (test_and_set_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002036 usleep_range(1000, 2000);
2037 pf = vsi->back;
2038
2039 if (vsi->netdev) {
2040 changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
2041 vsi->current_netdev_flags = vsi->netdev->flags;
2042 }
2043
Jacob Keller278e7d02016-10-05 09:30:37 -07002044 INIT_HLIST_HEAD(&tmp_add_list);
2045 INIT_HLIST_HEAD(&tmp_del_list);
Kiran Patil21659032015-09-30 14:09:03 -04002046
Shannon Nelson2d1de822016-05-16 10:26:44 -07002047 if (vsi->type == I40E_VSI_SRIOV)
2048 snprintf(vsi_name, sizeof(vsi_name) - 1, "VF %d", vsi->vf_id);
2049 else if (vsi->type != I40E_VSI_MAIN)
2050 snprintf(vsi_name, sizeof(vsi_name) - 1, "vsi %d", vsi->seid);
2051
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002052 if (vsi->flags & I40E_VSI_FLAG_FILTER_CHANGED) {
2053 vsi->flags &= ~I40E_VSI_FLAG_FILTER_CHANGED;
2054
Jacob Keller278e7d02016-10-05 09:30:37 -07002055 spin_lock_bh(&vsi->mac_filter_hash_lock);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002056 /* Create a list of filters to delete. */
Jacob Keller278e7d02016-10-05 09:30:37 -07002057 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002058 if (f->state == I40E_FILTER_REMOVE) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002059 /* Move the element into temporary del_list */
Jacob Keller278e7d02016-10-05 09:30:37 -07002060 hash_del(&f->hlist);
2061 hlist_add_head(&f->hlist, &tmp_del_list);
Alan Brady84f5ca62016-10-05 09:30:39 -07002062
2063 /* Avoid counting removed filters */
2064 continue;
Kiran Patil21659032015-09-30 14:09:03 -04002065 }
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002066 if (f->state == I40E_FILTER_NEW) {
Jacob Keller671889e2016-12-02 12:33:00 -08002067 /* Create a temporary i40e_new_mac_filter */
2068 new = kzalloc(sizeof(*new), GFP_ATOMIC);
2069 if (!new)
2070 goto err_no_memory_locked;
2071
2072 /* Store pointer to the real filter */
2073 new->f = f;
2074 new->state = f->state;
2075
2076 /* Add it to the hash list */
2077 hlist_add_head(&new->hlist, &tmp_add_list);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002078 }
Alan Brady84f5ca62016-10-05 09:30:39 -07002079
Jacob Keller489a3262016-11-11 12:39:31 -08002080 /* Count the number of active (current and new) VLAN
2081 * filters we have now. Does not count filters which
2082 * are marked for deletion.
Alan Brady84f5ca62016-10-05 09:30:39 -07002083 */
2084 if (f->vlan > 0)
2085 vlan_filters++;
Alan Brady84f5ca62016-10-05 09:30:39 -07002086 }
2087
Jacob Keller489a3262016-11-11 12:39:31 -08002088 retval = i40e_correct_mac_vlan_filters(vsi,
2089 &tmp_add_list,
2090 &tmp_del_list,
2091 vlan_filters);
2092 if (retval)
2093 goto err_no_memory_locked;
Alan Brady84f5ca62016-10-05 09:30:39 -07002094
Jacob Keller278e7d02016-10-05 09:30:37 -07002095 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -04002096 }
2097
2098 /* Now process 'del_list' outside the lock */
Jacob Keller278e7d02016-10-05 09:30:37 -07002099 if (!hlist_empty(&tmp_del_list)) {
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002100 filter_list_len = hw->aq.asq_buf_size /
Kiran Patil21659032015-09-30 14:09:03 -04002101 sizeof(struct i40e_aqc_remove_macvlan_element_data);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002102 list_size = filter_list_len *
Shannon Nelsonf1199992015-11-19 11:34:23 -08002103 sizeof(struct i40e_aqc_remove_macvlan_element_data);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002104 del_list = kzalloc(list_size, GFP_ATOMIC);
Jacob Keller4a2ce272016-10-05 09:30:38 -07002105 if (!del_list)
2106 goto err_no_memory;
Kiran Patil21659032015-09-30 14:09:03 -04002107
Jacob Keller278e7d02016-10-05 09:30:37 -07002108 hlist_for_each_entry_safe(f, h, &tmp_del_list, hlist) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002109 cmd_flags = 0;
2110
Jacob Keller435c0842016-11-08 13:05:10 -08002111 /* handle broadcast filters by updating the broadcast
Jacob Kellerd88d40b2016-12-02 12:32:59 -08002112 * promiscuous flag and release filter list.
Jacob Keller435c0842016-11-08 13:05:10 -08002113 */
2114 if (is_broadcast_ether_addr(f->macaddr)) {
2115 i40e_aqc_broadcast_filter(vsi, vsi_name, f);
2116
2117 hlist_del(&f->hlist);
2118 kfree(f);
2119 continue;
2120 }
2121
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002122 /* add to delete list */
Greg Rose9a173902014-05-22 06:32:02 +00002123 ether_addr_copy(del_list[num_del].mac_addr, f->macaddr);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002124 if (f->vlan == I40E_VLAN_ANY) {
2125 del_list[num_del].vlan_tag = 0;
Alan Bradya6cb9142016-09-06 18:05:07 -07002126 cmd_flags |= I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002127 } else {
2128 del_list[num_del].vlan_tag =
2129 cpu_to_le16((u16)(f->vlan));
2130 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002131
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002132 cmd_flags |= I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
2133 del_list[num_del].flags = cmd_flags;
2134 num_del++;
2135
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002136 /* flush a full buffer */
2137 if (num_del == filter_list_len) {
Jacob Keller00936312016-10-05 09:30:41 -07002138 i40e_aqc_del_filters(vsi, vsi_name, del_list,
2139 num_del, &retval);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002140 memset(del_list, 0, list_size);
Jacob Keller00936312016-10-05 09:30:41 -07002141 num_del = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002142 }
Kiran Patil21659032015-09-30 14:09:03 -04002143 /* Release memory for MAC filter entries which were
2144 * synced up with HW.
2145 */
Jacob Keller278e7d02016-10-05 09:30:37 -07002146 hlist_del(&f->hlist);
Kiran Patil21659032015-09-30 14:09:03 -04002147 kfree(f);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002148 }
Kiran Patil21659032015-09-30 14:09:03 -04002149
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002150 if (num_del) {
Jacob Keller00936312016-10-05 09:30:41 -07002151 i40e_aqc_del_filters(vsi, vsi_name, del_list,
2152 num_del, &retval);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002153 }
2154
2155 kfree(del_list);
2156 del_list = NULL;
Kiran Patil21659032015-09-30 14:09:03 -04002157 }
2158
Jacob Keller278e7d02016-10-05 09:30:37 -07002159 if (!hlist_empty(&tmp_add_list)) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002160 /* Do all the adds now. */
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002161 filter_list_len = hw->aq.asq_buf_size /
Shannon Nelsonf1199992015-11-19 11:34:23 -08002162 sizeof(struct i40e_aqc_add_macvlan_element_data);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002163 list_size = filter_list_len *
2164 sizeof(struct i40e_aqc_add_macvlan_element_data);
2165 add_list = kzalloc(list_size, GFP_ATOMIC);
Jacob Keller4a2ce272016-10-05 09:30:38 -07002166 if (!add_list)
2167 goto err_no_memory;
2168
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002169 num_add = 0;
Jacob Keller671889e2016-12-02 12:33:00 -08002170 hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
Jacob Kellerd19cb642017-04-21 13:38:05 -07002171 if (test_bit(__I40E_VSI_OVERFLOW_PROMISC,
Jacob Keller0da36b92017-04-19 09:25:55 -04002172 vsi->state)) {
Jacob Keller671889e2016-12-02 12:33:00 -08002173 new->state = I40E_FILTER_FAILED;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002174 continue;
2175 }
Jacob Keller435c0842016-11-08 13:05:10 -08002176
2177 /* handle broadcast filters by updating the broadcast
2178 * promiscuous flag instead of adding a MAC filter.
2179 */
Jacob Keller671889e2016-12-02 12:33:00 -08002180 if (is_broadcast_ether_addr(new->f->macaddr)) {
2181 if (i40e_aqc_broadcast_filter(vsi, vsi_name,
2182 new->f))
2183 new->state = I40E_FILTER_FAILED;
2184 else
2185 new->state = I40E_FILTER_ACTIVE;
Jacob Keller435c0842016-11-08 13:05:10 -08002186 continue;
2187 }
2188
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002189 /* add to add array */
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002190 if (num_add == 0)
Jacob Keller671889e2016-12-02 12:33:00 -08002191 add_head = new;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002192 cmd_flags = 0;
Jacob Keller671889e2016-12-02 12:33:00 -08002193 ether_addr_copy(add_list[num_add].mac_addr,
2194 new->f->macaddr);
2195 if (new->f->vlan == I40E_VLAN_ANY) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002196 add_list[num_add].vlan_tag = 0;
2197 cmd_flags |= I40E_AQC_MACVLAN_ADD_IGNORE_VLAN;
2198 } else {
2199 add_list[num_add].vlan_tag =
Jacob Keller671889e2016-12-02 12:33:00 -08002200 cpu_to_le16((u16)(new->f->vlan));
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002201 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002202 add_list[num_add].queue_number = 0;
Jacob Kellerac9e2392016-11-11 12:39:27 -08002203 /* set invalid match method for later detection */
Keller, Jacob E0266ac42016-12-09 13:39:21 -08002204 add_list[num_add].match_method = I40E_AQC_MM_ERR_NO_RES;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002205 cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002206 add_list[num_add].flags = cpu_to_le16(cmd_flags);
2207 num_add++;
2208
2209 /* flush a full buffer */
2210 if (num_add == filter_list_len) {
Jacob Keller00936312016-10-05 09:30:41 -07002211 i40e_aqc_add_filters(vsi, vsi_name, add_list,
2212 add_head, num_add,
2213 &promisc_changed);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002214 memset(add_list, 0, list_size);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002215 num_add = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002216 }
2217 }
2218 if (num_add) {
Jacob Keller00936312016-10-05 09:30:41 -07002219 i40e_aqc_add_filters(vsi, vsi_name, add_list, add_head,
2220 num_add, &promisc_changed);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002221 }
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002222 /* Now move all of the filters from the temp add list back to
2223 * the VSI's list.
2224 */
Jacob Keller278e7d02016-10-05 09:30:37 -07002225 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller671889e2016-12-02 12:33:00 -08002226 hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
2227 /* Only update the state if we're still NEW */
2228 if (new->f->state == I40E_FILTER_NEW)
2229 new->f->state = new->state;
2230 hlist_del(&new->hlist);
2231 kfree(new);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002232 }
Jacob Keller278e7d02016-10-05 09:30:37 -07002233 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002234 kfree(add_list);
2235 add_list = NULL;
2236 }
2237
Jacob Keller38326212016-11-11 12:39:26 -08002238 /* Determine the number of active and failed filters. */
2239 spin_lock_bh(&vsi->mac_filter_hash_lock);
2240 vsi->active_filters = 0;
2241 hash_for_each(vsi->mac_filter_hash, bkt, f, hlist) {
2242 if (f->state == I40E_FILTER_ACTIVE)
2243 vsi->active_filters++;
2244 else if (f->state == I40E_FILTER_FAILED)
2245 failed_filters++;
2246 }
2247 spin_unlock_bh(&vsi->mac_filter_hash_lock);
2248
2249 /* If promiscuous mode has changed, we need to calculate a new
2250 * threshold for when we are safe to exit
2251 */
2252 if (promisc_changed)
2253 vsi->promisc_threshold = (vsi->active_filters * 3) / 4;
2254
2255 /* Check if we are able to exit overflow promiscuous mode. We can
2256 * safely exit if we didn't just enter, we no longer have any failed
2257 * filters, and we have reduced filters below the threshold value.
2258 */
Jacob Keller0da36b92017-04-19 09:25:55 -04002259 if (test_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state) &&
Jacob Keller38326212016-11-11 12:39:26 -08002260 !promisc_changed && !failed_filters &&
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002261 (vsi->active_filters < vsi->promisc_threshold)) {
Jacob Keller38326212016-11-11 12:39:26 -08002262 dev_info(&pf->pdev->dev,
2263 "filter logjam cleared on %s, leaving overflow promiscuous mode\n",
2264 vsi_name);
Jacob Keller0da36b92017-04-19 09:25:55 -04002265 clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
Jacob Keller38326212016-11-11 12:39:26 -08002266 promisc_changed = true;
2267 vsi->promisc_threshold = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002268 }
2269
Anjali Singhai Jaina856b5c2016-04-13 03:08:23 -07002270 /* if the VF is not trusted do not do promisc */
2271 if ((vsi->type == I40E_VSI_SRIOV) && !pf->vf[vsi->vf_id].trusted) {
Jacob Keller0da36b92017-04-19 09:25:55 -04002272 clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
Anjali Singhai Jaina856b5c2016-04-13 03:08:23 -07002273 goto out;
2274 }
2275
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002276 /* check for changes in promiscuous modes */
2277 if (changed_flags & IFF_ALLMULTI) {
2278 bool cur_multipromisc;
Jesse Brandeburg6995b362015-08-28 17:55:54 -04002279
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002280 cur_multipromisc = !!(vsi->current_netdev_flags & IFF_ALLMULTI);
Mitch Williamsea02e902015-11-09 15:35:50 -08002281 aq_ret = i40e_aq_set_vsi_multicast_promiscuous(&vsi->back->hw,
2282 vsi->seid,
2283 cur_multipromisc,
2284 NULL);
2285 if (aq_ret) {
2286 retval = i40e_aq_rc_to_posix(aq_ret,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002287 hw->aq.asq_last_status);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002288 dev_info(&pf->pdev->dev,
Shannon Nelson2d1de822016-05-16 10:26:44 -07002289 "set multi promisc failed on %s, err %s aq_err %s\n",
2290 vsi_name,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002291 i40e_stat_str(hw, aq_ret),
2292 i40e_aq_str(hw, hw->aq.asq_last_status));
Mitch Williamsea02e902015-11-09 15:35:50 -08002293 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002294 }
Alan Bradye5887232017-06-07 05:43:10 -04002295
2296 if ((changed_flags & IFF_PROMISC) || promisc_changed) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002297 bool cur_promisc;
Jesse Brandeburg6995b362015-08-28 17:55:54 -04002298
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002299 cur_promisc = (!!(vsi->current_netdev_flags & IFF_PROMISC) ||
Jacob Kellerd19cb642017-04-21 13:38:05 -07002300 test_bit(__I40E_VSI_OVERFLOW_PROMISC,
Jacob Keller0da36b92017-04-19 09:25:55 -04002301 vsi->state));
Anjali Singhai Jain6784ed52016-01-15 14:33:13 -08002302 if ((vsi->type == I40E_VSI_MAIN) &&
2303 (pf->lan_veb != I40E_NO_VEB) &&
2304 !(pf->flags & I40E_FLAG_MFP_ENABLED)) {
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002305 /* set defport ON for Main VSI instead of true promisc
2306 * this way we will get all unicast/multicast and VLAN
2307 * promisc behavior but will not get VF or VMDq traffic
2308 * replicated on the Main VSI.
2309 */
2310 if (pf->cur_promisc != cur_promisc) {
2311 pf->cur_promisc = cur_promisc;
Mitch Williams5bc16032016-05-16 10:26:43 -07002312 if (cur_promisc)
2313 aq_ret =
2314 i40e_aq_set_default_vsi(hw,
2315 vsi->seid,
2316 NULL);
2317 else
2318 aq_ret =
2319 i40e_aq_clear_default_vsi(hw,
2320 vsi->seid,
2321 NULL);
2322 if (aq_ret) {
2323 retval = i40e_aq_rc_to_posix(aq_ret,
2324 hw->aq.asq_last_status);
2325 dev_info(&pf->pdev->dev,
Shannon Nelson2d1de822016-05-16 10:26:44 -07002326 "Set default VSI failed on %s, err %s, aq_err %s\n",
2327 vsi_name,
Mitch Williams5bc16032016-05-16 10:26:43 -07002328 i40e_stat_str(hw, aq_ret),
2329 i40e_aq_str(hw,
2330 hw->aq.asq_last_status));
2331 }
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002332 }
2333 } else {
Mitch Williamsea02e902015-11-09 15:35:50 -08002334 aq_ret = i40e_aq_set_vsi_unicast_promiscuous(
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002335 hw,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002336 vsi->seid,
Anjali Singhai Jainb5569892016-05-03 15:13:12 -07002337 cur_promisc, NULL,
2338 true);
Mitch Williamsea02e902015-11-09 15:35:50 -08002339 if (aq_ret) {
2340 retval =
2341 i40e_aq_rc_to_posix(aq_ret,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002342 hw->aq.asq_last_status);
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002343 dev_info(&pf->pdev->dev,
Shannon Nelson2d1de822016-05-16 10:26:44 -07002344 "set unicast promisc failed on %s, err %s, aq_err %s\n",
2345 vsi_name,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002346 i40e_stat_str(hw, aq_ret),
2347 i40e_aq_str(hw,
2348 hw->aq.asq_last_status));
Mitch Williamsea02e902015-11-09 15:35:50 -08002349 }
2350 aq_ret = i40e_aq_set_vsi_multicast_promiscuous(
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002351 hw,
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002352 vsi->seid,
2353 cur_promisc, NULL);
Mitch Williamsea02e902015-11-09 15:35:50 -08002354 if (aq_ret) {
2355 retval =
2356 i40e_aq_rc_to_posix(aq_ret,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002357 hw->aq.asq_last_status);
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002358 dev_info(&pf->pdev->dev,
Shannon Nelson2d1de822016-05-16 10:26:44 -07002359 "set multicast promisc failed on %s, err %s, aq_err %s\n",
2360 vsi_name,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002361 i40e_stat_str(hw, aq_ret),
2362 i40e_aq_str(hw,
2363 hw->aq.asq_last_status));
Mitch Williamsea02e902015-11-09 15:35:50 -08002364 }
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002365 }
Mitch Williamsea02e902015-11-09 15:35:50 -08002366 aq_ret = i40e_aq_set_vsi_broadcast(&vsi->back->hw,
2367 vsi->seid,
2368 cur_promisc, NULL);
2369 if (aq_ret) {
2370 retval = i40e_aq_rc_to_posix(aq_ret,
2371 pf->hw.aq.asq_last_status);
Greg Rose1a103702013-11-28 06:42:39 +00002372 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002373 "set brdcast promisc failed, err %s, aq_err %s\n",
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002374 i40e_stat_str(hw, aq_ret),
2375 i40e_aq_str(hw,
2376 hw->aq.asq_last_status));
Mitch Williamsea02e902015-11-09 15:35:50 -08002377 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002378 }
Mitch Williamsea02e902015-11-09 15:35:50 -08002379out:
Jesse Brandeburg2818ccd2016-01-13 16:51:38 -08002380 /* if something went wrong then set the changed flag so we try again */
2381 if (retval)
2382 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2383
Jacob Keller0da36b92017-04-19 09:25:55 -04002384 clear_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state);
Mitch Williamsea02e902015-11-09 15:35:50 -08002385 return retval;
Jacob Keller4a2ce272016-10-05 09:30:38 -07002386
2387err_no_memory:
2388 /* Restore elements on the temporary add and delete lists */
2389 spin_lock_bh(&vsi->mac_filter_hash_lock);
Alan Brady84f5ca62016-10-05 09:30:39 -07002390err_no_memory_locked:
Jacob Keller671889e2016-12-02 12:33:00 -08002391 i40e_undo_del_filter_entries(vsi, &tmp_del_list);
2392 i40e_undo_add_filter_entries(vsi, &tmp_add_list);
Jacob Keller4a2ce272016-10-05 09:30:38 -07002393 spin_unlock_bh(&vsi->mac_filter_hash_lock);
2394
2395 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
Jacob Keller0da36b92017-04-19 09:25:55 -04002396 clear_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state);
Jacob Keller4a2ce272016-10-05 09:30:38 -07002397 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002398}
2399
2400/**
2401 * i40e_sync_filters_subtask - Sync the VSI filter list with HW
2402 * @pf: board private structure
2403 **/
2404static void i40e_sync_filters_subtask(struct i40e_pf *pf)
2405{
2406 int v;
2407
2408 if (!pf || !(pf->flags & I40E_FLAG_FILTER_SYNC))
2409 return;
2410 pf->flags &= ~I40E_FLAG_FILTER_SYNC;
2411
Mitch Williams505682c2014-05-20 08:01:37 +00002412 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002413 if (pf->vsi[v] &&
Jesse Brandeburg17652c62015-11-05 17:01:02 -08002414 (pf->vsi[v]->flags & I40E_VSI_FLAG_FILTER_CHANGED)) {
2415 int ret = i40e_sync_vsi_filters(pf->vsi[v]);
2416
2417 if (ret) {
2418 /* come back and try again later */
2419 pf->flags |= I40E_FLAG_FILTER_SYNC;
2420 break;
2421 }
2422 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002423 }
2424}
2425
2426/**
Björn Töpel0c8493d2017-05-24 07:55:34 +02002427 * i40e_max_xdp_frame_size - returns the maximum allowed frame size for XDP
2428 * @vsi: the vsi
2429 **/
2430static int i40e_max_xdp_frame_size(struct i40e_vsi *vsi)
2431{
2432 if (PAGE_SIZE >= 8192 || (vsi->back->flags & I40E_FLAG_LEGACY_RX))
2433 return I40E_RXBUFFER_2048;
2434 else
2435 return I40E_RXBUFFER_3072;
2436}
2437
2438/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002439 * i40e_change_mtu - NDO callback to change the Maximum Transfer Unit
2440 * @netdev: network interface device structure
2441 * @new_mtu: new value for maximum frame size
2442 *
2443 * Returns 0 on success, negative on failure
2444 **/
2445static int i40e_change_mtu(struct net_device *netdev, int new_mtu)
2446{
2447 struct i40e_netdev_priv *np = netdev_priv(netdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002448 struct i40e_vsi *vsi = np->vsi;
Mitch Williams0ef2d5a2017-01-24 10:24:00 -08002449 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002450
Björn Töpel0c8493d2017-05-24 07:55:34 +02002451 if (i40e_enabled_xdp_vsi(vsi)) {
2452 int frame_size = new_mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
2453
2454 if (frame_size > i40e_max_xdp_frame_size(vsi))
2455 return -EINVAL;
2456 }
2457
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002458 netdev_info(netdev, "changing MTU from %d to %d\n",
2459 netdev->mtu, new_mtu);
2460 netdev->mtu = new_mtu;
2461 if (netif_running(netdev))
2462 i40e_vsi_reinit_locked(vsi);
Mitch Williams0ef2d5a2017-01-24 10:24:00 -08002463 pf->flags |= (I40E_FLAG_SERVICE_CLIENT_REQUESTED |
2464 I40E_FLAG_CLIENT_L2_CHANGE);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002465 return 0;
2466}
2467
2468/**
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00002469 * i40e_ioctl - Access the hwtstamp interface
2470 * @netdev: network interface device structure
2471 * @ifr: interface request data
2472 * @cmd: ioctl command
2473 **/
2474int i40e_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
2475{
2476 struct i40e_netdev_priv *np = netdev_priv(netdev);
2477 struct i40e_pf *pf = np->vsi->back;
2478
2479 switch (cmd) {
2480 case SIOCGHWTSTAMP:
2481 return i40e_ptp_get_ts_config(pf, ifr);
2482 case SIOCSHWTSTAMP:
2483 return i40e_ptp_set_ts_config(pf, ifr);
2484 default:
2485 return -EOPNOTSUPP;
2486 }
2487}
2488
2489/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002490 * i40e_vlan_stripping_enable - Turn on vlan stripping for the VSI
2491 * @vsi: the vsi being adjusted
2492 **/
2493void i40e_vlan_stripping_enable(struct i40e_vsi *vsi)
2494{
2495 struct i40e_vsi_context ctxt;
2496 i40e_status ret;
2497
2498 if ((vsi->info.valid_sections &
2499 cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
2500 ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_MODE_MASK) == 0))
2501 return; /* already enabled */
2502
2503 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2504 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
2505 I40E_AQ_VSI_PVLAN_EMOD_STR_BOTH;
2506
2507 ctxt.seid = vsi->seid;
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07002508 ctxt.info = vsi->info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002509 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2510 if (ret) {
2511 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002512 "update vlan stripping failed, err %s aq_err %s\n",
2513 i40e_stat_str(&vsi->back->hw, ret),
2514 i40e_aq_str(&vsi->back->hw,
2515 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002516 }
2517}
2518
2519/**
2520 * i40e_vlan_stripping_disable - Turn off vlan stripping for the VSI
2521 * @vsi: the vsi being adjusted
2522 **/
2523void i40e_vlan_stripping_disable(struct i40e_vsi *vsi)
2524{
2525 struct i40e_vsi_context ctxt;
2526 i40e_status ret;
2527
2528 if ((vsi->info.valid_sections &
2529 cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
2530 ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_EMOD_MASK) ==
2531 I40E_AQ_VSI_PVLAN_EMOD_MASK))
2532 return; /* already disabled */
2533
2534 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2535 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
2536 I40E_AQ_VSI_PVLAN_EMOD_NOTHING;
2537
2538 ctxt.seid = vsi->seid;
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07002539 ctxt.info = vsi->info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002540 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2541 if (ret) {
2542 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002543 "update vlan stripping failed, err %s aq_err %s\n",
2544 i40e_stat_str(&vsi->back->hw, ret),
2545 i40e_aq_str(&vsi->back->hw,
2546 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002547 }
2548}
2549
2550/**
2551 * i40e_vlan_rx_register - Setup or shutdown vlan offload
2552 * @netdev: network interface to be adjusted
2553 * @features: netdev features to test if VLAN offload is enabled or not
2554 **/
2555static void i40e_vlan_rx_register(struct net_device *netdev, u32 features)
2556{
2557 struct i40e_netdev_priv *np = netdev_priv(netdev);
2558 struct i40e_vsi *vsi = np->vsi;
2559
2560 if (features & NETIF_F_HW_VLAN_CTAG_RX)
2561 i40e_vlan_stripping_enable(vsi);
2562 else
2563 i40e_vlan_stripping_disable(vsi);
2564}
2565
2566/**
Jacob Keller490a4ad2016-11-11 12:39:29 -08002567 * i40e_add_vlan_all_mac - Add a MAC/VLAN filter for each existing MAC address
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002568 * @vsi: the vsi being configured
2569 * @vid: vlan id to be added (0 = untagged only , -1 = any)
Jacob Keller490a4ad2016-11-11 12:39:29 -08002570 *
2571 * This is a helper function for adding a new MAC/VLAN filter with the
2572 * specified VLAN for each existing MAC address already in the hash table.
2573 * This function does *not* perform any accounting to update filters based on
2574 * VLAN mode.
2575 *
2576 * NOTE: this function expects to be called while under the
2577 * mac_filter_hash_lock
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002578 **/
Jacob Keller9af52f62016-11-11 12:39:30 -08002579int i40e_add_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002580{
Jacob Keller490a4ad2016-11-11 12:39:29 -08002581 struct i40e_mac_filter *f, *add_f;
Jacob Keller278e7d02016-10-05 09:30:37 -07002582 struct hlist_node *h;
2583 int bkt;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002584
Jacob Keller278e7d02016-10-05 09:30:37 -07002585 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Jacob Keller57b341d2016-10-05 09:30:35 -07002586 if (f->state == I40E_FILTER_REMOVE)
2587 continue;
Jacob Keller1bc87e82016-10-05 09:30:31 -07002588 add_f = i40e_add_filter(vsi, f->macaddr, vid);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002589 if (!add_f) {
2590 dev_info(&vsi->back->pdev->dev,
2591 "Could not add vlan filter %d for %pM\n",
2592 vid, f->macaddr);
2593 return -ENOMEM;
2594 }
2595 }
2596
Jacob Keller490a4ad2016-11-11 12:39:29 -08002597 return 0;
2598}
2599
2600/**
2601 * i40e_vsi_add_vlan - Add VSI membership for given VLAN
2602 * @vsi: the VSI being configured
Jacob Kellerf94484b2016-12-07 14:05:34 -08002603 * @vid: VLAN id to be added
Jacob Keller490a4ad2016-11-11 12:39:29 -08002604 **/
Jacob Kellerf94484b2016-12-07 14:05:34 -08002605int i40e_vsi_add_vlan(struct i40e_vsi *vsi, u16 vid)
Jacob Keller490a4ad2016-11-11 12:39:29 -08002606{
Jacob Keller489a3262016-11-11 12:39:31 -08002607 int err;
Jacob Keller490a4ad2016-11-11 12:39:29 -08002608
Jacob Kellerfcf6cfc2017-06-23 04:24:51 -04002609 if (vsi->info.pvid)
Jacob Kellerf94484b2016-12-07 14:05:34 -08002610 return -EINVAL;
2611
Jacob Kellerfcf6cfc2017-06-23 04:24:51 -04002612 /* The network stack will attempt to add VID=0, with the intention to
2613 * receive priority tagged packets with a VLAN of 0. Our HW receives
2614 * these packets by default when configured to receive untagged
2615 * packets, so we don't need to add a filter for this case.
2616 * Additionally, HW interprets adding a VID=0 filter as meaning to
2617 * receive *only* tagged traffic and stops receiving untagged traffic.
2618 * Thus, we do not want to actually add a filter for VID=0
2619 */
2620 if (!vid)
2621 return 0;
2622
Jacob Keller490a4ad2016-11-11 12:39:29 -08002623 /* Locked once because all functions invoked below iterates list*/
2624 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller490a4ad2016-11-11 12:39:29 -08002625 err = i40e_add_vlan_all_mac(vsi, vid);
Jacob Keller278e7d02016-10-05 09:30:37 -07002626 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller489a3262016-11-11 12:39:31 -08002627 if (err)
2628 return err;
Kiran Patil21659032015-09-30 14:09:03 -04002629
Jesse Brandeburg0e4425e2015-11-05 17:01:01 -08002630 /* schedule our worker thread which will take care of
2631 * applying the new filter changes
2632 */
2633 i40e_service_event_schedule(vsi->back);
2634 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002635}
2636
2637/**
Jacob Keller490a4ad2016-11-11 12:39:29 -08002638 * i40e_rm_vlan_all_mac - Remove MAC/VLAN pair for all MAC with the given VLAN
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002639 * @vsi: the vsi being configured
2640 * @vid: vlan id to be removed (0 = untagged only , -1 = any)
Jacob Keller490a4ad2016-11-11 12:39:29 -08002641 *
2642 * This function should be used to remove all VLAN filters which match the
2643 * given VID. It does not schedule the service event and does not take the
2644 * mac_filter_hash_lock so it may be combined with other operations under
2645 * a single invocation of the mac_filter_hash_lock.
2646 *
2647 * NOTE: this function expects to be called while under the
2648 * mac_filter_hash_lock
2649 */
Jacob Keller9af52f62016-11-11 12:39:30 -08002650void i40e_rm_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002651{
Alan Brady84f5ca62016-10-05 09:30:39 -07002652 struct i40e_mac_filter *f;
Jacob Keller278e7d02016-10-05 09:30:37 -07002653 struct hlist_node *h;
Jacob Keller278e7d02016-10-05 09:30:37 -07002654 int bkt;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002655
Jacob Keller278e7d02016-10-05 09:30:37 -07002656 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Jacob Keller290d2552016-10-05 09:30:36 -07002657 if (f->vlan == vid)
2658 __i40e_del_filter(vsi, f);
2659 }
Jacob Keller490a4ad2016-11-11 12:39:29 -08002660}
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002661
Jacob Keller490a4ad2016-11-11 12:39:29 -08002662/**
2663 * i40e_vsi_kill_vlan - Remove VSI membership for given VLAN
2664 * @vsi: the VSI being configured
Jacob Kellerf94484b2016-12-07 14:05:34 -08002665 * @vid: VLAN id to be removed
Jacob Keller490a4ad2016-11-11 12:39:29 -08002666 **/
Jacob Kellerf94484b2016-12-07 14:05:34 -08002667void i40e_vsi_kill_vlan(struct i40e_vsi *vsi, u16 vid)
Jacob Keller490a4ad2016-11-11 12:39:29 -08002668{
Jacob Kellerf94484b2016-12-07 14:05:34 -08002669 if (!vid || vsi->info.pvid)
2670 return;
2671
Jacob Keller490a4ad2016-11-11 12:39:29 -08002672 spin_lock_bh(&vsi->mac_filter_hash_lock);
2673 i40e_rm_vlan_all_mac(vsi, vid);
Jacob Keller278e7d02016-10-05 09:30:37 -07002674 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -04002675
Jesse Brandeburg0e4425e2015-11-05 17:01:01 -08002676 /* schedule our worker thread which will take care of
2677 * applying the new filter changes
2678 */
2679 i40e_service_event_schedule(vsi->back);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002680}
2681
2682/**
2683 * i40e_vlan_rx_add_vid - Add a vlan id filter to HW offload
2684 * @netdev: network interface to be adjusted
2685 * @vid: vlan id to be added
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002686 *
2687 * net_device_ops implementation for adding vlan ids
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002688 **/
2689static int i40e_vlan_rx_add_vid(struct net_device *netdev,
2690 __always_unused __be16 proto, u16 vid)
2691{
2692 struct i40e_netdev_priv *np = netdev_priv(netdev);
2693 struct i40e_vsi *vsi = np->vsi;
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002694 int ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002695
Jacob Keller6a1127852016-10-25 16:08:49 -07002696 if (vid >= VLAN_N_VID)
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002697 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002698
Jacob Kellerfcf6cfc2017-06-23 04:24:51 -04002699 ret = i40e_vsi_add_vlan(vsi, vid);
Jacob Keller6a1127852016-10-25 16:08:49 -07002700 if (!ret)
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002701 set_bit(vid, vsi->active_vlans);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002702
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002703 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002704}
2705
2706/**
2707 * i40e_vlan_rx_kill_vid - Remove a vlan id filter from HW offload
2708 * @netdev: network interface to be adjusted
2709 * @vid: vlan id to be removed
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002710 *
Akeem G Abodunrinfdfd9432014-02-11 08:24:15 +00002711 * net_device_ops implementation for removing vlan ids
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002712 **/
2713static int i40e_vlan_rx_kill_vid(struct net_device *netdev,
2714 __always_unused __be16 proto, u16 vid)
2715{
2716 struct i40e_netdev_priv *np = netdev_priv(netdev);
2717 struct i40e_vsi *vsi = np->vsi;
2718
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002719 /* return code is ignored as there is nothing a user
2720 * can do about failure to remove and a log message was
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002721 * already printed from the other function
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002722 */
2723 i40e_vsi_kill_vlan(vsi, vid);
2724
2725 clear_bit(vid, vsi->active_vlans);
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002726
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002727 return 0;
2728}
2729
2730/**
2731 * i40e_restore_vlan - Reinstate vlans when vsi/netdev comes back up
2732 * @vsi: the vsi being brought back up
2733 **/
2734static void i40e_restore_vlan(struct i40e_vsi *vsi)
2735{
2736 u16 vid;
2737
2738 if (!vsi->netdev)
2739 return;
2740
2741 i40e_vlan_rx_register(vsi->netdev, vsi->netdev->features);
2742
2743 for_each_set_bit(vid, vsi->active_vlans, VLAN_N_VID)
2744 i40e_vlan_rx_add_vid(vsi->netdev, htons(ETH_P_8021Q),
2745 vid);
2746}
2747
2748/**
2749 * i40e_vsi_add_pvid - Add pvid for the VSI
2750 * @vsi: the vsi being adjusted
2751 * @vid: the vlan id to set as a PVID
2752 **/
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002753int i40e_vsi_add_pvid(struct i40e_vsi *vsi, u16 vid)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002754{
2755 struct i40e_vsi_context ctxt;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002756 i40e_status ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002757
2758 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2759 vsi->info.pvid = cpu_to_le16(vid);
Greg Rose6c12fcb2013-11-28 06:39:34 +00002760 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_TAGGED |
2761 I40E_AQ_VSI_PVLAN_INSERT_PVID |
Greg Roseb774c7d2013-11-28 06:39:44 +00002762 I40E_AQ_VSI_PVLAN_EMOD_STR;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002763
2764 ctxt.seid = vsi->seid;
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07002765 ctxt.info = vsi->info;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002766 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2767 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002768 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002769 "add pvid failed, err %s aq_err %s\n",
2770 i40e_stat_str(&vsi->back->hw, ret),
2771 i40e_aq_str(&vsi->back->hw,
2772 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002773 return -ENOENT;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002774 }
2775
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002776 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002777}
2778
2779/**
2780 * i40e_vsi_remove_pvid - Remove the pvid from the VSI
2781 * @vsi: the vsi being adjusted
2782 *
2783 * Just use the vlan_rx_register() service to put it back to normal
2784 **/
2785void i40e_vsi_remove_pvid(struct i40e_vsi *vsi)
2786{
Greg Rose6c12fcb2013-11-28 06:39:34 +00002787 i40e_vlan_stripping_disable(vsi);
2788
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002789 vsi->info.pvid = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002790}
2791
2792/**
2793 * i40e_vsi_setup_tx_resources - Allocate VSI Tx queue resources
2794 * @vsi: ptr to the VSI
2795 *
2796 * If this function returns with an error, then it's possible one or
2797 * more of the rings is populated (while the rest are not). It is the
2798 * callers duty to clean those orphaned rings.
2799 *
2800 * Return 0 on success, negative on failure
2801 **/
2802static int i40e_vsi_setup_tx_resources(struct i40e_vsi *vsi)
2803{
2804 int i, err = 0;
2805
2806 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e152013-09-28 06:00:58 +00002807 err = i40e_setup_tx_descriptors(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002808
Björn Töpel74608d12017-05-24 07:55:35 +02002809 if (!i40e_enabled_xdp_vsi(vsi))
2810 return err;
2811
2812 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
2813 err = i40e_setup_tx_descriptors(vsi->xdp_rings[i]);
2814
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002815 return err;
2816}
2817
2818/**
2819 * i40e_vsi_free_tx_resources - Free Tx resources for VSI queues
2820 * @vsi: ptr to the VSI
2821 *
2822 * Free VSI's transmit software resources
2823 **/
2824static void i40e_vsi_free_tx_resources(struct i40e_vsi *vsi)
2825{
2826 int i;
2827
Björn Töpel74608d12017-05-24 07:55:35 +02002828 if (vsi->tx_rings) {
2829 for (i = 0; i < vsi->num_queue_pairs; i++)
2830 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
2831 i40e_free_tx_resources(vsi->tx_rings[i]);
2832 }
Greg Rose8e9dca52013-12-18 13:45:53 +00002833
Björn Töpel74608d12017-05-24 07:55:35 +02002834 if (vsi->xdp_rings) {
2835 for (i = 0; i < vsi->num_queue_pairs; i++)
2836 if (vsi->xdp_rings[i] && vsi->xdp_rings[i]->desc)
2837 i40e_free_tx_resources(vsi->xdp_rings[i]);
2838 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002839}
2840
2841/**
2842 * i40e_vsi_setup_rx_resources - Allocate VSI queues Rx resources
2843 * @vsi: ptr to the VSI
2844 *
2845 * If this function returns with an error, then it's possible one or
2846 * more of the rings is populated (while the rest are not). It is the
2847 * callers duty to clean those orphaned rings.
2848 *
2849 * Return 0 on success, negative on failure
2850 **/
2851static int i40e_vsi_setup_rx_resources(struct i40e_vsi *vsi)
2852{
2853 int i, err = 0;
2854
2855 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e152013-09-28 06:00:58 +00002856 err = i40e_setup_rx_descriptors(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002857 return err;
2858}
2859
2860/**
2861 * i40e_vsi_free_rx_resources - Free Rx Resources for VSI queues
2862 * @vsi: ptr to the VSI
2863 *
2864 * Free all receive software resources
2865 **/
2866static void i40e_vsi_free_rx_resources(struct i40e_vsi *vsi)
2867{
2868 int i;
2869
Greg Rose8e9dca52013-12-18 13:45:53 +00002870 if (!vsi->rx_rings)
2871 return;
2872
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002873 for (i = 0; i < vsi->num_queue_pairs; i++)
Greg Rose8e9dca52013-12-18 13:45:53 +00002874 if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
Alexander Duyck9f65e152013-09-28 06:00:58 +00002875 i40e_free_rx_resources(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002876}
2877
2878/**
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002879 * i40e_config_xps_tx_ring - Configure XPS for a Tx ring
2880 * @ring: The Tx ring to configure
2881 *
2882 * This enables/disables XPS for a given Tx descriptor ring
2883 * based on the TCs enabled for the VSI that ring belongs to.
2884 **/
2885static void i40e_config_xps_tx_ring(struct i40e_ring *ring)
2886{
2887 struct i40e_vsi *vsi = ring->vsi;
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002888
Jesse Brandeburg9a660ee2015-02-26 16:13:22 +00002889 if (!ring->q_vector || !ring->netdev)
2890 return;
2891
Jacob Kellerba4460d2017-07-14 09:10:08 -04002892 if ((vsi->tc_config.numtc <= 1) &&
2893 !test_and_set_bit(__I40E_TX_XPS_INIT_DONE, &ring->state)) {
2894 netif_set_xps_queue(ring->netdev,
Jacob Keller759dc4a2017-07-14 09:10:10 -04002895 get_cpu_mask(ring->q_vector->v_idx),
Jacob Kellerba4460d2017-07-14 09:10:08 -04002896 ring->queue_index);
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002897 }
Jesse Brandeburg0e4425e2015-11-05 17:01:01 -08002898
2899 /* schedule our worker thread which will take care of
2900 * applying the new filter changes
2901 */
2902 i40e_service_event_schedule(vsi->back);
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002903}
2904
2905/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002906 * i40e_configure_tx_ring - Configure a transmit ring context and rest
2907 * @ring: The Tx ring to configure
2908 *
2909 * Configure the Tx descriptor ring in the HMC context.
2910 **/
2911static int i40e_configure_tx_ring(struct i40e_ring *ring)
2912{
2913 struct i40e_vsi *vsi = ring->vsi;
2914 u16 pf_q = vsi->base_queue + ring->queue_index;
2915 struct i40e_hw *hw = &vsi->back->hw;
2916 struct i40e_hmc_obj_txq tx_ctx;
2917 i40e_status err = 0;
2918 u32 qtx_ctl = 0;
2919
2920 /* some ATR related tx ring init */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08002921 if (vsi->back->flags & I40E_FLAG_FD_ATR_ENABLED) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002922 ring->atr_sample_rate = vsi->back->atr_sample_rate;
2923 ring->atr_count = 0;
2924 } else {
2925 ring->atr_sample_rate = 0;
2926 }
2927
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002928 /* configure XPS */
2929 i40e_config_xps_tx_ring(ring);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002930
2931 /* clear the context structure first */
2932 memset(&tx_ctx, 0, sizeof(tx_ctx));
2933
2934 tx_ctx.new_context = 1;
2935 tx_ctx.base = (ring->dma / 128);
2936 tx_ctx.qlen = ring->count;
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08002937 tx_ctx.fd_ena = !!(vsi->back->flags & (I40E_FLAG_FD_SB_ENABLED |
2938 I40E_FLAG_FD_ATR_ENABLED));
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00002939 tx_ctx.timesync_ena = !!(vsi->back->flags & I40E_FLAG_PTP);
Jesse Brandeburg1943d8b2014-02-14 02:14:40 +00002940 /* FDIR VSI tx ring can still use RS bit and writebacks */
2941 if (vsi->type != I40E_VSI_FDIR)
2942 tx_ctx.head_wb_ena = 1;
2943 tx_ctx.head_wb_addr = ring->dma +
2944 (ring->count * sizeof(struct i40e_tx_desc));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002945
2946 /* As part of VSI creation/update, FW allocates certain
2947 * Tx arbitration queue sets for each TC enabled for
2948 * the VSI. The FW returns the handles to these queue
2949 * sets as part of the response buffer to Add VSI,
2950 * Update VSI, etc. AQ commands. It is expected that
2951 * these queue set handles be associated with the Tx
2952 * queues by the driver as part of the TX queue context
2953 * initialization. This has to be done regardless of
2954 * DCB as by default everything is mapped to TC0.
2955 */
2956 tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[ring->dcb_tc]);
2957 tx_ctx.rdylist_act = 0;
2958
2959 /* clear the context in the HMC */
2960 err = i40e_clear_lan_tx_queue_context(hw, pf_q);
2961 if (err) {
2962 dev_info(&vsi->back->pdev->dev,
2963 "Failed to clear LAN Tx queue context on Tx ring %d (pf_q %d), error: %d\n",
2964 ring->queue_index, pf_q, err);
2965 return -ENOMEM;
2966 }
2967
2968 /* set the context in the HMC */
2969 err = i40e_set_lan_tx_queue_context(hw, pf_q, &tx_ctx);
2970 if (err) {
2971 dev_info(&vsi->back->pdev->dev,
2972 "Failed to set LAN Tx queue context on Tx ring %d (pf_q %d, error: %d\n",
2973 ring->queue_index, pf_q, err);
2974 return -ENOMEM;
2975 }
2976
2977 /* Now associate this queue with this PCI function */
Mitch Williams7a28d882014-10-17 03:14:52 +00002978 if (vsi->type == I40E_VSI_VMDQ2) {
Shannon Nelson9d8bf542014-01-14 00:49:50 -08002979 qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
Mitch Williams7a28d882014-10-17 03:14:52 +00002980 qtx_ctl |= ((vsi->id) << I40E_QTX_CTL_VFVM_INDX_SHIFT) &
2981 I40E_QTX_CTL_VFVM_INDX_MASK;
2982 } else {
Shannon Nelson9d8bf542014-01-14 00:49:50 -08002983 qtx_ctl = I40E_QTX_CTL_PF_QUEUE;
Mitch Williams7a28d882014-10-17 03:14:52 +00002984 }
2985
Shannon Nelson13fd9772013-09-28 07:14:19 +00002986 qtx_ctl |= ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT) &
2987 I40E_QTX_CTL_PF_INDX_MASK);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002988 wr32(hw, I40E_QTX_CTL(pf_q), qtx_ctl);
2989 i40e_flush(hw);
2990
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002991 /* cache tail off for easier writes later */
2992 ring->tail = hw->hw_addr + I40E_QTX_TAIL(pf_q);
2993
2994 return 0;
2995}
2996
2997/**
2998 * i40e_configure_rx_ring - Configure a receive ring context
2999 * @ring: The Rx ring to configure
3000 *
3001 * Configure the Rx descriptor ring in the HMC context.
3002 **/
3003static int i40e_configure_rx_ring(struct i40e_ring *ring)
3004{
3005 struct i40e_vsi *vsi = ring->vsi;
3006 u32 chain_len = vsi->back->hw.func_caps.rx_buf_chain_len;
3007 u16 pf_q = vsi->base_queue + ring->queue_index;
3008 struct i40e_hw *hw = &vsi->back->hw;
3009 struct i40e_hmc_obj_rxq rx_ctx;
3010 i40e_status err = 0;
3011
3012 ring->state = 0;
3013
3014 /* clear the context structure first */
3015 memset(&rx_ctx, 0, sizeof(rx_ctx));
3016
3017 ring->rx_buf_len = vsi->rx_buf_len;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003018
Alexander Duyckdab86af2017-03-14 10:15:27 -07003019 rx_ctx.dbuff = DIV_ROUND_UP(ring->rx_buf_len,
3020 BIT_ULL(I40E_RXQ_CTX_DBUFF_SHIFT));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003021
3022 rx_ctx.base = (ring->dma / 128);
3023 rx_ctx.qlen = ring->count;
3024
Jesse Brandeburgbec60fc2016-04-18 11:33:47 -07003025 /* use 32 byte descriptors */
3026 rx_ctx.dsize = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003027
Jesse Brandeburgbec60fc2016-04-18 11:33:47 -07003028 /* descriptor type is always zero
3029 * rx_ctx.dtype = 0;
3030 */
Jesse Brandeburgb32bfa172016-04-18 11:33:42 -07003031 rx_ctx.hsplit_0 = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003032
Jesse Brandeburgb32bfa172016-04-18 11:33:42 -07003033 rx_ctx.rxmax = min_t(u16, vsi->max_frame, chain_len * ring->rx_buf_len);
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00003034 if (hw->revision_id == 0)
3035 rx_ctx.lrxqthresh = 0;
3036 else
3037 rx_ctx.lrxqthresh = 2;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003038 rx_ctx.crcstrip = 1;
3039 rx_ctx.l2tsel = 1;
Jesse Brandeburgc4bbac32015-09-28 11:21:48 -07003040 /* this controls whether VLAN is stripped from inner headers */
3041 rx_ctx.showiv = 0;
Catherine Sullivanacb36762014-03-06 09:02:30 +00003042 /* set the prefena field to 1 because the manual says to */
3043 rx_ctx.prefena = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003044
3045 /* clear the context in the HMC */
3046 err = i40e_clear_lan_rx_queue_context(hw, pf_q);
3047 if (err) {
3048 dev_info(&vsi->back->pdev->dev,
3049 "Failed to clear LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
3050 ring->queue_index, pf_q, err);
3051 return -ENOMEM;
3052 }
3053
3054 /* set the context in the HMC */
3055 err = i40e_set_lan_rx_queue_context(hw, pf_q, &rx_ctx);
3056 if (err) {
3057 dev_info(&vsi->back->pdev->dev,
3058 "Failed to set LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
3059 ring->queue_index, pf_q, err);
3060 return -ENOMEM;
3061 }
3062
Alexander Duyckca9ec082017-04-05 07:51:02 -04003063 /* configure Rx buffer alignment */
3064 if (!vsi->netdev || (vsi->back->flags & I40E_FLAG_LEGACY_RX))
3065 clear_ring_build_skb_enabled(ring);
3066 else
3067 set_ring_build_skb_enabled(ring);
3068
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003069 /* cache tail for quicker writes, and clear the reg before use */
3070 ring->tail = hw->hw_addr + I40E_QRX_TAIL(pf_q);
3071 writel(0, ring->tail);
3072
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07003073 i40e_alloc_rx_buffers(ring, I40E_DESC_UNUSED(ring));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003074
3075 return 0;
3076}
3077
3078/**
3079 * i40e_vsi_configure_tx - Configure the VSI for Tx
3080 * @vsi: VSI structure describing this set of rings and resources
3081 *
3082 * Configure the Tx VSI for operation.
3083 **/
3084static int i40e_vsi_configure_tx(struct i40e_vsi *vsi)
3085{
3086 int err = 0;
3087 u16 i;
3088
Alexander Duyck9f65e152013-09-28 06:00:58 +00003089 for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
3090 err = i40e_configure_tx_ring(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003091
Björn Töpel74608d12017-05-24 07:55:35 +02003092 if (!i40e_enabled_xdp_vsi(vsi))
3093 return err;
3094
3095 for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
3096 err = i40e_configure_tx_ring(vsi->xdp_rings[i]);
3097
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003098 return err;
3099}
3100
3101/**
3102 * i40e_vsi_configure_rx - Configure the VSI for Rx
3103 * @vsi: the VSI being configured
3104 *
3105 * Configure the Rx VSI for operation.
3106 **/
3107static int i40e_vsi_configure_rx(struct i40e_vsi *vsi)
3108{
3109 int err = 0;
3110 u16 i;
3111
Alexander Duyckdab86af2017-03-14 10:15:27 -07003112 if (!vsi->netdev || (vsi->back->flags & I40E_FLAG_LEGACY_RX)) {
3113 vsi->max_frame = I40E_MAX_RXBUFFER;
3114 vsi->rx_buf_len = I40E_RXBUFFER_2048;
3115#if (PAGE_SIZE < 8192)
Alexander Duyckca9ec082017-04-05 07:51:02 -04003116 } else if (!I40E_2K_TOO_SMALL_WITH_PADDING &&
3117 (vsi->netdev->mtu <= ETH_DATA_LEN)) {
Alexander Duyckdab86af2017-03-14 10:15:27 -07003118 vsi->max_frame = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
3119 vsi->rx_buf_len = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
3120#endif
3121 } else {
3122 vsi->max_frame = I40E_MAX_RXBUFFER;
Alexander Duyck98efd692017-04-05 07:51:01 -04003123 vsi->rx_buf_len = (PAGE_SIZE < 8192) ? I40E_RXBUFFER_3072 :
3124 I40E_RXBUFFER_2048;
Alexander Duyckdab86af2017-03-14 10:15:27 -07003125 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003126
3127 /* set up individual rings */
3128 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e152013-09-28 06:00:58 +00003129 err = i40e_configure_rx_ring(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003130
3131 return err;
3132}
3133
3134/**
3135 * i40e_vsi_config_dcb_rings - Update rings to reflect DCB TC
3136 * @vsi: ptr to the VSI
3137 **/
3138static void i40e_vsi_config_dcb_rings(struct i40e_vsi *vsi)
3139{
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +00003140 struct i40e_ring *tx_ring, *rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003141 u16 qoffset, qcount;
3142 int i, n;
3143
Parikh, Neeravcd238a32015-02-21 06:43:37 +00003144 if (!(vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
3145 /* Reset the TC information */
3146 for (i = 0; i < vsi->num_queue_pairs; i++) {
3147 rx_ring = vsi->rx_rings[i];
3148 tx_ring = vsi->tx_rings[i];
3149 rx_ring->dcb_tc = 0;
3150 tx_ring->dcb_tc = 0;
3151 }
3152 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003153
3154 for (n = 0; n < I40E_MAX_TRAFFIC_CLASS; n++) {
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04003155 if (!(vsi->tc_config.enabled_tc & BIT_ULL(n)))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003156 continue;
3157
3158 qoffset = vsi->tc_config.tc_info[n].qoffset;
3159 qcount = vsi->tc_config.tc_info[n].qcount;
3160 for (i = qoffset; i < (qoffset + qcount); i++) {
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +00003161 rx_ring = vsi->rx_rings[i];
3162 tx_ring = vsi->tx_rings[i];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003163 rx_ring->dcb_tc = n;
3164 tx_ring->dcb_tc = n;
3165 }
3166 }
3167}
3168
3169/**
3170 * i40e_set_vsi_rx_mode - Call set_rx_mode on a VSI
3171 * @vsi: ptr to the VSI
3172 **/
3173static void i40e_set_vsi_rx_mode(struct i40e_vsi *vsi)
3174{
3175 if (vsi->netdev)
3176 i40e_set_rx_mode(vsi->netdev);
3177}
3178
3179/**
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00003180 * i40e_fdir_filter_restore - Restore the Sideband Flow Director filters
3181 * @vsi: Pointer to the targeted VSI
3182 *
3183 * This function replays the hlist on the hw where all the SB Flow Director
3184 * filters were saved.
3185 **/
3186static void i40e_fdir_filter_restore(struct i40e_vsi *vsi)
3187{
3188 struct i40e_fdir_filter *filter;
3189 struct i40e_pf *pf = vsi->back;
3190 struct hlist_node *node;
3191
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00003192 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
3193 return;
3194
Jacob Keller6d069422017-02-06 14:38:44 -08003195 /* Reset FDir counters as we're replaying all existing filters */
Jacob Keller097dbf52017-02-06 14:38:46 -08003196 pf->fd_tcp4_filter_cnt = 0;
3197 pf->fd_udp4_filter_cnt = 0;
Jacob Kellerf223c872017-02-06 14:38:51 -08003198 pf->fd_sctp4_filter_cnt = 0;
Jacob Keller097dbf52017-02-06 14:38:46 -08003199 pf->fd_ip4_filter_cnt = 0;
Jacob Keller6d069422017-02-06 14:38:44 -08003200
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00003201 hlist_for_each_entry_safe(filter, node,
3202 &pf->fdir_filter_list, fdir_node) {
3203 i40e_add_del_fdir(vsi, filter, true);
3204 }
3205}
3206
3207/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003208 * i40e_vsi_configure - Set up the VSI for action
3209 * @vsi: the VSI being configured
3210 **/
3211static int i40e_vsi_configure(struct i40e_vsi *vsi)
3212{
3213 int err;
3214
3215 i40e_set_vsi_rx_mode(vsi);
3216 i40e_restore_vlan(vsi);
3217 i40e_vsi_config_dcb_rings(vsi);
3218 err = i40e_vsi_configure_tx(vsi);
3219 if (!err)
3220 err = i40e_vsi_configure_rx(vsi);
3221
3222 return err;
3223}
3224
3225/**
3226 * i40e_vsi_configure_msix - MSIX mode Interrupt Config in the HW
3227 * @vsi: the VSI being configured
3228 **/
3229static void i40e_vsi_configure_msix(struct i40e_vsi *vsi)
3230{
Björn Töpel74608d12017-05-24 07:55:35 +02003231 bool has_xdp = i40e_enabled_xdp_vsi(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003232 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003233 struct i40e_hw *hw = &pf->hw;
3234 u16 vector;
3235 int i, q;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003236 u32 qp;
3237
3238 /* The interrupt indexing is offset by 1 in the PFINT_ITRn
3239 * and PFINT_LNKLSTn registers, e.g.:
3240 * PFINT_ITRn[0..n-1] gets msix-1..msix-n (qpair interrupts)
3241 */
3242 qp = vsi->base_queue;
3243 vector = vsi->base_vector;
Alexander Duyck493fb302013-09-28 07:01:44 +00003244 for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
Jesse Brandeburgac26fc12015-09-28 14:12:37 -04003245 struct i40e_q_vector *q_vector = vsi->q_vectors[i];
3246
Jesse Brandeburgee2319c2015-09-28 14:16:54 -04003247 q_vector->itr_countdown = ITR_COUNTDOWN_START;
Kan Lianga75e8002016-02-19 09:24:04 -05003248 q_vector->rx.itr = ITR_TO_REG(vsi->rx_rings[i]->rx_itr_setting);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003249 q_vector->rx.latency_range = I40E_LOW_LATENCY;
3250 wr32(hw, I40E_PFINT_ITRN(I40E_RX_ITR, vector - 1),
3251 q_vector->rx.itr);
Kan Lianga75e8002016-02-19 09:24:04 -05003252 q_vector->tx.itr = ITR_TO_REG(vsi->tx_rings[i]->tx_itr_setting);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003253 q_vector->tx.latency_range = I40E_LOW_LATENCY;
3254 wr32(hw, I40E_PFINT_ITRN(I40E_TX_ITR, vector - 1),
3255 q_vector->tx.itr);
Jesse Brandeburgac26fc12015-09-28 14:12:37 -04003256 wr32(hw, I40E_PFINT_RATEN(vector - 1),
Alan Brady1c0e6a32016-11-28 16:06:02 -08003257 i40e_intrl_usec_to_reg(vsi->int_rate_limit));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003258
3259 /* Linked list for the queuepairs assigned to this vector */
3260 wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), qp);
3261 for (q = 0; q < q_vector->num_ringpairs; q++) {
Björn Töpel74608d12017-05-24 07:55:35 +02003262 u32 nextqp = has_xdp ? qp + vsi->alloc_queue_pairs : qp;
Jesse Brandeburgac26fc12015-09-28 14:12:37 -04003263 u32 val;
3264
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003265 val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
Björn Töpel74608d12017-05-24 07:55:35 +02003266 (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
3267 (vector << I40E_QINT_RQCTL_MSIX_INDX_SHIFT) |
3268 (nextqp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT) |
3269 (I40E_QUEUE_TYPE_TX <<
3270 I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003271
3272 wr32(hw, I40E_QINT_RQCTL(qp), val);
3273
Björn Töpel74608d12017-05-24 07:55:35 +02003274 if (has_xdp) {
3275 val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3276 (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3277 (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
3278 (qp << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) |
3279 (I40E_QUEUE_TYPE_TX <<
3280 I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3281
3282 wr32(hw, I40E_QINT_TQCTL(nextqp), val);
3283 }
3284
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003285 val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
Björn Töpel74608d12017-05-24 07:55:35 +02003286 (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3287 (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
3288 ((qp + 1) << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) |
3289 (I40E_QUEUE_TYPE_RX <<
3290 I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003291
3292 /* Terminate the linked list */
3293 if (q == (q_vector->num_ringpairs - 1))
Björn Töpel74608d12017-05-24 07:55:35 +02003294 val |= (I40E_QUEUE_END_OF_LIST <<
3295 I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003296
3297 wr32(hw, I40E_QINT_TQCTL(qp), val);
3298 qp++;
3299 }
3300 }
3301
3302 i40e_flush(hw);
3303}
3304
3305/**
3306 * i40e_enable_misc_int_causes - enable the non-queue interrupts
3307 * @hw: ptr to the hardware info
3308 **/
Jacob Kellerab437b52014-12-14 01:55:08 +00003309static void i40e_enable_misc_int_causes(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003310{
Jacob Kellerab437b52014-12-14 01:55:08 +00003311 struct i40e_hw *hw = &pf->hw;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003312 u32 val;
3313
3314 /* clear things first */
3315 wr32(hw, I40E_PFINT_ICR0_ENA, 0); /* disable all */
3316 rd32(hw, I40E_PFINT_ICR0); /* read to clear */
3317
3318 val = I40E_PFINT_ICR0_ENA_ECC_ERR_MASK |
3319 I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK |
3320 I40E_PFINT_ICR0_ENA_GRST_MASK |
3321 I40E_PFINT_ICR0_ENA_PCI_EXCEPTION_MASK |
3322 I40E_PFINT_ICR0_ENA_GPIO_MASK |
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003323 I40E_PFINT_ICR0_ENA_HMC_ERR_MASK |
3324 I40E_PFINT_ICR0_ENA_VFLR_MASK |
3325 I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
3326
Anjali Singhai Jain0d8e1432015-06-05 12:20:32 -04003327 if (pf->flags & I40E_FLAG_IWARP_ENABLED)
3328 val |= I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
3329
Jacob Kellerab437b52014-12-14 01:55:08 +00003330 if (pf->flags & I40E_FLAG_PTP)
3331 val |= I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
3332
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003333 wr32(hw, I40E_PFINT_ICR0_ENA, val);
3334
3335 /* SW_ITR_IDX = 0, but don't change INTENA */
Anjali Singhai Jain84ed40e2013-11-26 10:49:32 +00003336 wr32(hw, I40E_PFINT_DYN_CTL0, I40E_PFINT_DYN_CTL0_SW_ITR_INDX_MASK |
3337 I40E_PFINT_DYN_CTL0_INTENA_MSK_MASK);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003338
3339 /* OTHER_ITR_IDX = 0 */
3340 wr32(hw, I40E_PFINT_STAT_CTL0, 0);
3341}
3342
3343/**
3344 * i40e_configure_msi_and_legacy - Legacy mode interrupt config in the HW
3345 * @vsi: the VSI being configured
3346 **/
3347static void i40e_configure_msi_and_legacy(struct i40e_vsi *vsi)
3348{
Björn Töpel74608d12017-05-24 07:55:35 +02003349 u32 nextqp = i40e_enabled_xdp_vsi(vsi) ? vsi->alloc_queue_pairs : 0;
Alexander Duyck493fb302013-09-28 07:01:44 +00003350 struct i40e_q_vector *q_vector = vsi->q_vectors[0];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003351 struct i40e_pf *pf = vsi->back;
3352 struct i40e_hw *hw = &pf->hw;
3353 u32 val;
3354
3355 /* set the ITR configuration */
Jesse Brandeburgee2319c2015-09-28 14:16:54 -04003356 q_vector->itr_countdown = ITR_COUNTDOWN_START;
Kan Lianga75e8002016-02-19 09:24:04 -05003357 q_vector->rx.itr = ITR_TO_REG(vsi->rx_rings[0]->rx_itr_setting);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003358 q_vector->rx.latency_range = I40E_LOW_LATENCY;
3359 wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), q_vector->rx.itr);
Kan Lianga75e8002016-02-19 09:24:04 -05003360 q_vector->tx.itr = ITR_TO_REG(vsi->tx_rings[0]->tx_itr_setting);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003361 q_vector->tx.latency_range = I40E_LOW_LATENCY;
3362 wr32(hw, I40E_PFINT_ITR0(I40E_TX_ITR), q_vector->tx.itr);
3363
Jacob Kellerab437b52014-12-14 01:55:08 +00003364 i40e_enable_misc_int_causes(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003365
3366 /* FIRSTQ_INDX = 0, FIRSTQ_TYPE = 0 (rx) */
3367 wr32(hw, I40E_PFINT_LNKLST0, 0);
3368
Jesse Brandeburgf29eaa32014-02-11 08:24:12 +00003369 /* Associate the queue pair to the vector and enable the queue int */
Björn Töpel74608d12017-05-24 07:55:35 +02003370 val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3371 (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
3372 (nextqp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT)|
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003373 (I40E_QUEUE_TYPE_TX << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3374
3375 wr32(hw, I40E_QINT_RQCTL(0), val);
3376
Björn Töpel74608d12017-05-24 07:55:35 +02003377 if (i40e_enabled_xdp_vsi(vsi)) {
3378 val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3379 (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT)|
3380 (I40E_QUEUE_TYPE_TX
3381 << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3382
3383 wr32(hw, I40E_QINT_TQCTL(nextqp), val);
3384 }
3385
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003386 val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3387 (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3388 (I40E_QUEUE_END_OF_LIST << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
3389
3390 wr32(hw, I40E_QINT_TQCTL(0), val);
3391 i40e_flush(hw);
3392}
3393
3394/**
Mitch Williams2ef28cf2013-11-28 06:39:32 +00003395 * i40e_irq_dynamic_disable_icr0 - Disable default interrupt generation for icr0
3396 * @pf: board private structure
3397 **/
3398void i40e_irq_dynamic_disable_icr0(struct i40e_pf *pf)
3399{
3400 struct i40e_hw *hw = &pf->hw;
3401
3402 wr32(hw, I40E_PFINT_DYN_CTL0,
3403 I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
3404 i40e_flush(hw);
3405}
3406
3407/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003408 * i40e_irq_dynamic_enable_icr0 - Enable default interrupt generation for icr0
3409 * @pf: board private structure
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08003410 * @clearpba: true when all pending interrupt events should be cleared
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003411 **/
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08003412void i40e_irq_dynamic_enable_icr0(struct i40e_pf *pf, bool clearpba)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003413{
3414 struct i40e_hw *hw = &pf->hw;
3415 u32 val;
3416
3417 val = I40E_PFINT_DYN_CTL0_INTENA_MASK |
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08003418 (clearpba ? I40E_PFINT_DYN_CTL0_CLEARPBA_MASK : 0) |
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003419 (I40E_ITR_NONE << I40E_PFINT_DYN_CTL0_ITR_INDX_SHIFT);
3420
3421 wr32(hw, I40E_PFINT_DYN_CTL0, val);
3422 i40e_flush(hw);
3423}
3424
3425/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003426 * i40e_msix_clean_rings - MSIX mode Interrupt Handler
3427 * @irq: interrupt number
3428 * @data: pointer to a q_vector
3429 **/
3430static irqreturn_t i40e_msix_clean_rings(int irq, void *data)
3431{
3432 struct i40e_q_vector *q_vector = data;
3433
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003434 if (!q_vector->tx.ring && !q_vector->rx.ring)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003435 return IRQ_HANDLED;
3436
Alexander Duyck5d3465a2015-09-29 15:19:50 -07003437 napi_schedule_irqoff(&q_vector->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003438
3439 return IRQ_HANDLED;
3440}
3441
3442/**
Alan Brady96db7762016-09-14 16:24:38 -07003443 * i40e_irq_affinity_notify - Callback for affinity changes
3444 * @notify: context as to what irq was changed
3445 * @mask: the new affinity mask
3446 *
3447 * This is a callback function used by the irq_set_affinity_notifier function
3448 * so that we may register to receive changes to the irq affinity masks.
3449 **/
3450static void i40e_irq_affinity_notify(struct irq_affinity_notify *notify,
3451 const cpumask_t *mask)
3452{
3453 struct i40e_q_vector *q_vector =
3454 container_of(notify, struct i40e_q_vector, affinity_notify);
3455
Jacob Keller7e4d01e2017-07-12 05:46:05 -04003456 cpumask_copy(&q_vector->affinity_mask, mask);
Alan Brady96db7762016-09-14 16:24:38 -07003457}
3458
3459/**
3460 * i40e_irq_affinity_release - Callback for affinity notifier release
3461 * @ref: internal core kernel usage
3462 *
3463 * This is a callback function used by the irq_set_affinity_notifier function
3464 * to inform the current notification subscriber that they will no longer
3465 * receive notifications.
3466 **/
3467static void i40e_irq_affinity_release(struct kref *ref) {}
3468
3469/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003470 * i40e_vsi_request_irq_msix - Initialize MSI-X interrupts
3471 * @vsi: the VSI being configured
3472 * @basename: name for the vector
3473 *
3474 * Allocates MSI-X vectors and requests interrupts from the kernel.
3475 **/
3476static int i40e_vsi_request_irq_msix(struct i40e_vsi *vsi, char *basename)
3477{
3478 int q_vectors = vsi->num_q_vectors;
3479 struct i40e_pf *pf = vsi->back;
3480 int base = vsi->base_vector;
3481 int rx_int_idx = 0;
3482 int tx_int_idx = 0;
3483 int vector, err;
Alan Brady96db7762016-09-14 16:24:38 -07003484 int irq_num;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003485
3486 for (vector = 0; vector < q_vectors; vector++) {
Alexander Duyck493fb302013-09-28 07:01:44 +00003487 struct i40e_q_vector *q_vector = vsi->q_vectors[vector];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003488
Alan Brady96db7762016-09-14 16:24:38 -07003489 irq_num = pf->msix_entries[base + vector].vector;
3490
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003491 if (q_vector->tx.ring && q_vector->rx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003492 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3493 "%s-%s-%d", basename, "TxRx", rx_int_idx++);
3494 tx_int_idx++;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003495 } else if (q_vector->rx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003496 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3497 "%s-%s-%d", basename, "rx", rx_int_idx++);
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003498 } else if (q_vector->tx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003499 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3500 "%s-%s-%d", basename, "tx", tx_int_idx++);
3501 } else {
3502 /* skip this unused q_vector */
3503 continue;
3504 }
Alan Brady96db7762016-09-14 16:24:38 -07003505 err = request_irq(irq_num,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003506 vsi->irq_handler,
3507 0,
3508 q_vector->name,
3509 q_vector);
3510 if (err) {
3511 dev_info(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04003512 "MSIX request_irq failed, error: %d\n", err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003513 goto free_queue_irqs;
3514 }
Alan Brady96db7762016-09-14 16:24:38 -07003515
3516 /* register for affinity change notifications */
3517 q_vector->affinity_notify.notify = i40e_irq_affinity_notify;
3518 q_vector->affinity_notify.release = i40e_irq_affinity_release;
3519 irq_set_affinity_notifier(irq_num, &q_vector->affinity_notify);
Jacob Keller759dc4a2017-07-14 09:10:10 -04003520 /* get_cpu_mask returns a static constant mask with
3521 * a permanent lifetime so it's ok to use here.
3522 */
3523 irq_set_affinity_hint(irq_num, get_cpu_mask(q_vector->v_idx));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003524 }
3525
Shannon Nelson63741842014-04-23 04:50:16 +00003526 vsi->irqs_ready = true;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003527 return 0;
3528
3529free_queue_irqs:
3530 while (vector) {
3531 vector--;
Alan Brady96db7762016-09-14 16:24:38 -07003532 irq_num = pf->msix_entries[base + vector].vector;
3533 irq_set_affinity_notifier(irq_num, NULL);
3534 irq_set_affinity_hint(irq_num, NULL);
3535 free_irq(irq_num, &vsi->q_vectors[vector]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003536 }
3537 return err;
3538}
3539
3540/**
3541 * i40e_vsi_disable_irq - Mask off queue interrupt generation on the VSI
3542 * @vsi: the VSI being un-configured
3543 **/
3544static void i40e_vsi_disable_irq(struct i40e_vsi *vsi)
3545{
3546 struct i40e_pf *pf = vsi->back;
3547 struct i40e_hw *hw = &pf->hw;
3548 int base = vsi->base_vector;
3549 int i;
3550
Shannon Nelson2e5c26ea2017-06-07 05:43:11 -04003551 /* disable interrupt causation from each queue */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003552 for (i = 0; i < vsi->num_queue_pairs; i++) {
Shannon Nelson2e5c26ea2017-06-07 05:43:11 -04003553 u32 val;
3554
3555 val = rd32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx));
3556 val &= ~I40E_QINT_TQCTL_CAUSE_ENA_MASK;
3557 wr32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx), val);
3558
3559 val = rd32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx));
3560 val &= ~I40E_QINT_RQCTL_CAUSE_ENA_MASK;
3561 wr32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx), val);
3562
Björn Töpel74608d12017-05-24 07:55:35 +02003563 if (!i40e_enabled_xdp_vsi(vsi))
3564 continue;
3565 wr32(hw, I40E_QINT_TQCTL(vsi->xdp_rings[i]->reg_idx), 0);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003566 }
3567
Shannon Nelson2e5c26ea2017-06-07 05:43:11 -04003568 /* disable each interrupt */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003569 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3570 for (i = vsi->base_vector;
3571 i < (vsi->num_q_vectors + vsi->base_vector); i++)
3572 wr32(hw, I40E_PFINT_DYN_CTLN(i - 1), 0);
3573
3574 i40e_flush(hw);
3575 for (i = 0; i < vsi->num_q_vectors; i++)
3576 synchronize_irq(pf->msix_entries[i + base].vector);
3577 } else {
3578 /* Legacy and MSI mode - this stops all interrupt handling */
3579 wr32(hw, I40E_PFINT_ICR0_ENA, 0);
3580 wr32(hw, I40E_PFINT_DYN_CTL0, 0);
3581 i40e_flush(hw);
3582 synchronize_irq(pf->pdev->irq);
3583 }
3584}
3585
3586/**
3587 * i40e_vsi_enable_irq - Enable IRQ for the given VSI
3588 * @vsi: the VSI being configured
3589 **/
3590static int i40e_vsi_enable_irq(struct i40e_vsi *vsi)
3591{
3592 struct i40e_pf *pf = vsi->back;
3593 int i;
3594
3595 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
Jesse Brandeburg78455482015-07-23 16:54:41 -04003596 for (i = 0; i < vsi->num_q_vectors; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003597 i40e_irq_dynamic_enable(vsi, i);
3598 } else {
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08003599 i40e_irq_dynamic_enable_icr0(pf, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003600 }
3601
Jesse Brandeburg1022cb62013-09-28 07:13:08 +00003602 i40e_flush(&pf->hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003603 return 0;
3604}
3605
3606/**
Jacob Kellerc17401a2017-07-14 09:27:02 -04003607 * i40e_free_misc_vector - Free the vector that handles non-queue events
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003608 * @pf: board private structure
3609 **/
Jacob Kellerc17401a2017-07-14 09:27:02 -04003610static void i40e_free_misc_vector(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003611{
3612 /* Disable ICR 0 */
3613 wr32(&pf->hw, I40E_PFINT_ICR0_ENA, 0);
3614 i40e_flush(&pf->hw);
Jacob Kellerc17401a2017-07-14 09:27:02 -04003615
3616 if (pf->flags & I40E_FLAG_MSIX_ENABLED && pf->msix_entries) {
3617 synchronize_irq(pf->msix_entries[0].vector);
3618 free_irq(pf->msix_entries[0].vector, pf);
3619 clear_bit(__I40E_MISC_IRQ_REQUESTED, pf->state);
3620 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003621}
3622
3623/**
3624 * i40e_intr - MSI/Legacy and non-queue interrupt handler
3625 * @irq: interrupt number
3626 * @data: pointer to a q_vector
3627 *
3628 * This is the handler used for all MSI/Legacy interrupts, and deals
3629 * with both queue and non-queue interrupts. This is also used in
3630 * MSIX mode to handle the non-queue interrupts.
3631 **/
3632static irqreturn_t i40e_intr(int irq, void *data)
3633{
3634 struct i40e_pf *pf = (struct i40e_pf *)data;
3635 struct i40e_hw *hw = &pf->hw;
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003636 irqreturn_t ret = IRQ_NONE;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003637 u32 icr0, icr0_remaining;
3638 u32 val, ena_mask;
3639
3640 icr0 = rd32(hw, I40E_PFINT_ICR0);
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003641 ena_mask = rd32(hw, I40E_PFINT_ICR0_ENA);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003642
Shannon Nelson116a57d2013-09-28 07:13:59 +00003643 /* if sharing a legacy IRQ, we might get called w/o an intr pending */
3644 if ((icr0 & I40E_PFINT_ICR0_INTEVENT_MASK) == 0)
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003645 goto enable_intr;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003646
Shannon Nelsoncd92e722013-11-16 10:00:44 +00003647 /* if interrupt but no bits showing, must be SWINT */
3648 if (((icr0 & ~I40E_PFINT_ICR0_INTEVENT_MASK) == 0) ||
3649 (icr0 & I40E_PFINT_ICR0_SWINT_MASK))
3650 pf->sw_int_count++;
3651
Anjali Singhai Jain0d8e1432015-06-05 12:20:32 -04003652 if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
Catherine Sullivan76429842017-06-07 05:43:12 -04003653 (icr0 & I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK)) {
Anjali Singhai Jain0d8e1432015-06-05 12:20:32 -04003654 ena_mask &= ~I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
Carolyn Wyborny23bb6dc2016-11-08 13:05:12 -08003655 dev_dbg(&pf->pdev->dev, "cleared PE_CRITERR\n");
Catherine Sullivan76429842017-06-07 05:43:12 -04003656 set_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
Anjali Singhai Jain0d8e1432015-06-05 12:20:32 -04003657 }
3658
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003659 /* only q0 is used in MSI/Legacy mode, and none are used in MSIX */
3660 if (icr0 & I40E_PFINT_ICR0_QUEUE_0_MASK) {
Alexander Duyck5d3465a2015-09-29 15:19:50 -07003661 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
3662 struct i40e_q_vector *q_vector = vsi->q_vectors[0];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003663
Anjali Singhai Jaina16ae2d2016-01-15 14:33:16 -08003664 /* We do not have a way to disarm Queue causes while leaving
3665 * interrupt enabled for all other causes, ideally
3666 * interrupt should be disabled while we are in NAPI but
3667 * this is not a performance path and napi_schedule()
3668 * can deal with rescheduling.
3669 */
Mauro S. M. Rodrigues9e6c9c02017-05-12 23:26:56 -03003670 if (!test_bit(__I40E_DOWN, pf->state))
Alexander Duyck5d3465a2015-09-29 15:19:50 -07003671 napi_schedule_irqoff(&q_vector->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003672 }
3673
3674 if (icr0 & I40E_PFINT_ICR0_ADMINQ_MASK) {
3675 ena_mask &= ~I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
Jacob Keller0da36b92017-04-19 09:25:55 -04003676 set_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state);
Shannon Nelson6e93d0c2016-01-15 14:33:18 -08003677 i40e_debug(&pf->hw, I40E_DEBUG_NVM, "AdminQ event\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003678 }
3679
3680 if (icr0 & I40E_PFINT_ICR0_MAL_DETECT_MASK) {
3681 ena_mask &= ~I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
Jacob Keller0da36b92017-04-19 09:25:55 -04003682 set_bit(__I40E_MDD_EVENT_PENDING, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003683 }
3684
3685 if (icr0 & I40E_PFINT_ICR0_VFLR_MASK) {
3686 ena_mask &= ~I40E_PFINT_ICR0_ENA_VFLR_MASK;
Jacob Keller0da36b92017-04-19 09:25:55 -04003687 set_bit(__I40E_VFLR_EVENT_PENDING, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003688 }
3689
3690 if (icr0 & I40E_PFINT_ICR0_GRST_MASK) {
Jacob Keller0da36b92017-04-19 09:25:55 -04003691 if (!test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
3692 set_bit(__I40E_RESET_INTR_RECEIVED, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003693 ena_mask &= ~I40E_PFINT_ICR0_ENA_GRST_MASK;
3694 val = rd32(hw, I40E_GLGEN_RSTAT);
3695 val = (val & I40E_GLGEN_RSTAT_RESET_TYPE_MASK)
3696 >> I40E_GLGEN_RSTAT_RESET_TYPE_SHIFT;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003697 if (val == I40E_RESET_CORER) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003698 pf->corer_count++;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003699 } else if (val == I40E_RESET_GLOBR) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003700 pf->globr_count++;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003701 } else if (val == I40E_RESET_EMPR) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003702 pf->empr_count++;
Jacob Keller0da36b92017-04-19 09:25:55 -04003703 set_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state);
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003704 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003705 }
3706
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003707 if (icr0 & I40E_PFINT_ICR0_HMC_ERR_MASK) {
3708 icr0 &= ~I40E_PFINT_ICR0_HMC_ERR_MASK;
3709 dev_info(&pf->pdev->dev, "HMC error interrupt\n");
Anjali Singhai Jain25fc0e62015-03-31 00:45:01 -07003710 dev_info(&pf->pdev->dev, "HMC error info 0x%x, HMC error data 0x%x\n",
3711 rd32(hw, I40E_PFHMC_ERRORINFO),
3712 rd32(hw, I40E_PFHMC_ERRORDATA));
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003713 }
3714
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003715 if (icr0 & I40E_PFINT_ICR0_TIMESYNC_MASK) {
3716 u32 prttsyn_stat = rd32(hw, I40E_PRTTSYN_STAT_0);
3717
3718 if (prttsyn_stat & I40E_PRTTSYN_STAT_0_TXTIME_MASK) {
Jacob Kellercafa1fc2014-04-24 18:05:03 -07003719 icr0 &= ~I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003720 i40e_ptp_tx_hwtstamp(pf);
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003721 }
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003722 }
3723
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003724 /* If a critical error is pending we have no choice but to reset the
3725 * device.
3726 * Report and mask out any remaining unexpected interrupts.
3727 */
3728 icr0_remaining = icr0 & ena_mask;
3729 if (icr0_remaining) {
3730 dev_info(&pf->pdev->dev, "unhandled interrupt icr0=0x%08x\n",
3731 icr0_remaining);
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003732 if ((icr0_remaining & I40E_PFINT_ICR0_PE_CRITERR_MASK) ||
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003733 (icr0_remaining & I40E_PFINT_ICR0_PCI_EXCEPTION_MASK) ||
Anjali Singhai Jainc0c28972014-02-12 01:45:34 +00003734 (icr0_remaining & I40E_PFINT_ICR0_ECC_ERR_MASK)) {
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003735 dev_info(&pf->pdev->dev, "device will be reset\n");
Jacob Keller0da36b92017-04-19 09:25:55 -04003736 set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003737 i40e_service_event_schedule(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003738 }
3739 ena_mask &= ~icr0_remaining;
3740 }
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003741 ret = IRQ_HANDLED;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003742
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003743enable_intr:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003744 /* re-enable interrupt causes */
3745 wr32(hw, I40E_PFINT_ICR0_ENA, ena_mask);
Mauro S. M. Rodrigues9e6c9c02017-05-12 23:26:56 -03003746 if (!test_bit(__I40E_DOWN, pf->state)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003747 i40e_service_event_schedule(pf);
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08003748 i40e_irq_dynamic_enable_icr0(pf, false);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003749 }
3750
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003751 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003752}
3753
3754/**
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003755 * i40e_clean_fdir_tx_irq - Reclaim resources after transmit completes
3756 * @tx_ring: tx ring to clean
3757 * @budget: how many cleans we're allowed
3758 *
3759 * Returns true if there's any budget left (e.g. the clean is finished)
3760 **/
3761static bool i40e_clean_fdir_tx_irq(struct i40e_ring *tx_ring, int budget)
3762{
3763 struct i40e_vsi *vsi = tx_ring->vsi;
3764 u16 i = tx_ring->next_to_clean;
3765 struct i40e_tx_buffer *tx_buf;
3766 struct i40e_tx_desc *tx_desc;
3767
3768 tx_buf = &tx_ring->tx_bi[i];
3769 tx_desc = I40E_TX_DESC(tx_ring, i);
3770 i -= tx_ring->count;
3771
3772 do {
3773 struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
3774
3775 /* if next_to_watch is not set then there is no work pending */
3776 if (!eop_desc)
3777 break;
3778
3779 /* prevent any other reads prior to eop_desc */
3780 read_barrier_depends();
3781
3782 /* if the descriptor isn't done, no work yet to do */
3783 if (!(eop_desc->cmd_type_offset_bsz &
3784 cpu_to_le64(I40E_TX_DESC_DTYPE_DESC_DONE)))
3785 break;
3786
3787 /* clear next_to_watch to prevent false hangs */
3788 tx_buf->next_to_watch = NULL;
3789
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003790 tx_desc->buffer_addr = 0;
3791 tx_desc->cmd_type_offset_bsz = 0;
3792 /* move past filter desc */
3793 tx_buf++;
3794 tx_desc++;
3795 i++;
3796 if (unlikely(!i)) {
3797 i -= tx_ring->count;
3798 tx_buf = tx_ring->tx_bi;
3799 tx_desc = I40E_TX_DESC(tx_ring, 0);
3800 }
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003801 /* unmap skb header data */
3802 dma_unmap_single(tx_ring->dev,
3803 dma_unmap_addr(tx_buf, dma),
3804 dma_unmap_len(tx_buf, len),
3805 DMA_TO_DEVICE);
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003806 if (tx_buf->tx_flags & I40E_TX_FLAGS_FD_SB)
3807 kfree(tx_buf->raw_buf);
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003808
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003809 tx_buf->raw_buf = NULL;
3810 tx_buf->tx_flags = 0;
3811 tx_buf->next_to_watch = NULL;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003812 dma_unmap_len_set(tx_buf, len, 0);
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003813 tx_desc->buffer_addr = 0;
3814 tx_desc->cmd_type_offset_bsz = 0;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003815
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003816 /* move us past the eop_desc for start of next FD desc */
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003817 tx_buf++;
3818 tx_desc++;
3819 i++;
3820 if (unlikely(!i)) {
3821 i -= tx_ring->count;
3822 tx_buf = tx_ring->tx_bi;
3823 tx_desc = I40E_TX_DESC(tx_ring, 0);
3824 }
3825
3826 /* update budget accounting */
3827 budget--;
3828 } while (likely(budget));
3829
3830 i += tx_ring->count;
3831 tx_ring->next_to_clean = i;
3832
Jesse Brandeburg6995b362015-08-28 17:55:54 -04003833 if (vsi->back->flags & I40E_FLAG_MSIX_ENABLED)
Jesse Brandeburg78455482015-07-23 16:54:41 -04003834 i40e_irq_dynamic_enable(vsi, tx_ring->q_vector->v_idx);
Jesse Brandeburg6995b362015-08-28 17:55:54 -04003835
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003836 return budget > 0;
3837}
3838
3839/**
3840 * i40e_fdir_clean_ring - Interrupt Handler for FDIR SB ring
3841 * @irq: interrupt number
3842 * @data: pointer to a q_vector
3843 **/
3844static irqreturn_t i40e_fdir_clean_ring(int irq, void *data)
3845{
3846 struct i40e_q_vector *q_vector = data;
3847 struct i40e_vsi *vsi;
3848
3849 if (!q_vector->tx.ring)
3850 return IRQ_HANDLED;
3851
3852 vsi = q_vector->tx.ring->vsi;
3853 i40e_clean_fdir_tx_irq(q_vector->tx.ring, vsi->work_limit);
3854
3855 return IRQ_HANDLED;
3856}
3857
3858/**
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003859 * i40e_map_vector_to_qp - Assigns the queue pair to the vector
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003860 * @vsi: the VSI being configured
3861 * @v_idx: vector index
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003862 * @qp_idx: queue pair index
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003863 **/
Anjali Singhai Jain26cdc442015-07-10 19:36:00 -04003864static void i40e_map_vector_to_qp(struct i40e_vsi *vsi, int v_idx, int qp_idx)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003865{
Alexander Duyck493fb302013-09-28 07:01:44 +00003866 struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
Alexander Duyck9f65e152013-09-28 06:00:58 +00003867 struct i40e_ring *tx_ring = vsi->tx_rings[qp_idx];
3868 struct i40e_ring *rx_ring = vsi->rx_rings[qp_idx];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003869
3870 tx_ring->q_vector = q_vector;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003871 tx_ring->next = q_vector->tx.ring;
3872 q_vector->tx.ring = tx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003873 q_vector->tx.count++;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003874
Björn Töpel74608d12017-05-24 07:55:35 +02003875 /* Place XDP Tx ring in the same q_vector ring list as regular Tx */
3876 if (i40e_enabled_xdp_vsi(vsi)) {
3877 struct i40e_ring *xdp_ring = vsi->xdp_rings[qp_idx];
3878
3879 xdp_ring->q_vector = q_vector;
3880 xdp_ring->next = q_vector->tx.ring;
3881 q_vector->tx.ring = xdp_ring;
3882 q_vector->tx.count++;
3883 }
3884
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003885 rx_ring->q_vector = q_vector;
3886 rx_ring->next = q_vector->rx.ring;
3887 q_vector->rx.ring = rx_ring;
3888 q_vector->rx.count++;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003889}
3890
3891/**
3892 * i40e_vsi_map_rings_to_vectors - Maps descriptor rings to vectors
3893 * @vsi: the VSI being configured
3894 *
3895 * This function maps descriptor rings to the queue-specific vectors
3896 * we were allotted through the MSI-X enabling code. Ideally, we'd have
3897 * one vector per queue pair, but on a constrained vector budget, we
3898 * group the queue pairs as "efficiently" as possible.
3899 **/
3900static void i40e_vsi_map_rings_to_vectors(struct i40e_vsi *vsi)
3901{
3902 int qp_remaining = vsi->num_queue_pairs;
3903 int q_vectors = vsi->num_q_vectors;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003904 int num_ringpairs;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003905 int v_start = 0;
3906 int qp_idx = 0;
3907
3908 /* If we don't have enough vectors for a 1-to-1 mapping, we'll have to
3909 * group them so there are multiple queues per vector.
Anjali Singhai Jain70114ec2014-06-03 23:50:14 +00003910 * It is also important to go through all the vectors available to be
3911 * sure that if we don't use all the vectors, that the remaining vectors
3912 * are cleared. This is especially important when decreasing the
3913 * number of queues in use.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003914 */
Anjali Singhai Jain70114ec2014-06-03 23:50:14 +00003915 for (; v_start < q_vectors; v_start++) {
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003916 struct i40e_q_vector *q_vector = vsi->q_vectors[v_start];
3917
3918 num_ringpairs = DIV_ROUND_UP(qp_remaining, q_vectors - v_start);
3919
3920 q_vector->num_ringpairs = num_ringpairs;
3921
3922 q_vector->rx.count = 0;
3923 q_vector->tx.count = 0;
3924 q_vector->rx.ring = NULL;
3925 q_vector->tx.ring = NULL;
3926
3927 while (num_ringpairs--) {
Anjali Singhai Jain26cdc442015-07-10 19:36:00 -04003928 i40e_map_vector_to_qp(vsi, v_start, qp_idx);
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003929 qp_idx++;
3930 qp_remaining--;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003931 }
3932 }
3933}
3934
3935/**
3936 * i40e_vsi_request_irq - Request IRQ from the OS
3937 * @vsi: the VSI being configured
3938 * @basename: name for the vector
3939 **/
3940static int i40e_vsi_request_irq(struct i40e_vsi *vsi, char *basename)
3941{
3942 struct i40e_pf *pf = vsi->back;
3943 int err;
3944
3945 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
3946 err = i40e_vsi_request_irq_msix(vsi, basename);
3947 else if (pf->flags & I40E_FLAG_MSI_ENABLED)
3948 err = request_irq(pf->pdev->irq, i40e_intr, 0,
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00003949 pf->int_name, pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003950 else
3951 err = request_irq(pf->pdev->irq, i40e_intr, IRQF_SHARED,
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00003952 pf->int_name, pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003953
3954 if (err)
3955 dev_info(&pf->pdev->dev, "request_irq failed, Error %d\n", err);
3956
3957 return err;
3958}
3959
3960#ifdef CONFIG_NET_POLL_CONTROLLER
3961/**
Jesse Brandeburgd89d9672016-01-04 10:33:02 -08003962 * i40e_netpoll - A Polling 'interrupt' handler
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003963 * @netdev: network interface device structure
3964 *
3965 * This is used by netconsole to send skbs without having to re-enable
3966 * interrupts. It's not called while the normal interrupt routine is executing.
3967 **/
3968static void i40e_netpoll(struct net_device *netdev)
3969{
3970 struct i40e_netdev_priv *np = netdev_priv(netdev);
3971 struct i40e_vsi *vsi = np->vsi;
3972 struct i40e_pf *pf = vsi->back;
3973 int i;
3974
3975 /* if interface is down do nothing */
Jacob Keller0da36b92017-04-19 09:25:55 -04003976 if (test_bit(__I40E_VSI_DOWN, vsi->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003977 return;
3978
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003979 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3980 for (i = 0; i < vsi->num_q_vectors; i++)
Alexander Duyck493fb302013-09-28 07:01:44 +00003981 i40e_msix_clean_rings(0, vsi->q_vectors[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003982 } else {
3983 i40e_intr(pf->pdev->irq, netdev);
3984 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003985}
3986#endif
3987
Jacob Kellerc768e492017-04-13 04:45:47 -04003988#define I40E_QTX_ENA_WAIT_COUNT 50
3989
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003990/**
Neerav Parikh23527302014-06-03 23:50:15 +00003991 * i40e_pf_txq_wait - Wait for a PF's Tx queue to be enabled or disabled
3992 * @pf: the PF being configured
3993 * @pf_q: the PF queue
3994 * @enable: enable or disable state of the queue
3995 *
3996 * This routine will wait for the given Tx queue of the PF to reach the
3997 * enabled or disabled state.
3998 * Returns -ETIMEDOUT in case of failing to reach the requested state after
3999 * multiple retries; else will return 0 in case of success.
4000 **/
4001static int i40e_pf_txq_wait(struct i40e_pf *pf, int pf_q, bool enable)
4002{
4003 int i;
4004 u32 tx_reg;
4005
4006 for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
4007 tx_reg = rd32(&pf->hw, I40E_QTX_ENA(pf_q));
4008 if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
4009 break;
4010
Neerav Parikhf98a2002014-09-13 07:40:44 +00004011 usleep_range(10, 20);
Neerav Parikh23527302014-06-03 23:50:15 +00004012 }
4013 if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
4014 return -ETIMEDOUT;
4015
4016 return 0;
4017}
4018
4019/**
Jacob Kellerc768e492017-04-13 04:45:47 -04004020 * i40e_control_tx_q - Start or stop a particular Tx queue
4021 * @pf: the PF structure
4022 * @pf_q: the PF queue to configure
4023 * @enable: start or stop the queue
4024 *
4025 * This function enables or disables a single queue. Note that any delay
4026 * required after the operation is expected to be handled by the caller of
4027 * this function.
4028 **/
4029static void i40e_control_tx_q(struct i40e_pf *pf, int pf_q, bool enable)
4030{
4031 struct i40e_hw *hw = &pf->hw;
4032 u32 tx_reg;
4033 int i;
4034
4035 /* warn the TX unit of coming changes */
4036 i40e_pre_tx_queue_cfg(&pf->hw, pf_q, enable);
4037 if (!enable)
4038 usleep_range(10, 20);
4039
4040 for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
4041 tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
4042 if (((tx_reg >> I40E_QTX_ENA_QENA_REQ_SHIFT) & 1) ==
4043 ((tx_reg >> I40E_QTX_ENA_QENA_STAT_SHIFT) & 1))
4044 break;
4045 usleep_range(1000, 2000);
4046 }
4047
4048 /* Skip if the queue is already in the requested state */
4049 if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
4050 return;
4051
4052 /* turn on/off the queue */
4053 if (enable) {
4054 wr32(hw, I40E_QTX_HEAD(pf_q), 0);
4055 tx_reg |= I40E_QTX_ENA_QENA_REQ_MASK;
4056 } else {
4057 tx_reg &= ~I40E_QTX_ENA_QENA_REQ_MASK;
4058 }
4059
4060 wr32(hw, I40E_QTX_ENA(pf_q), tx_reg);
4061}
4062
4063/**
Björn Töpel74608d12017-05-24 07:55:35 +02004064 * i40e_control_wait_tx_q - Start/stop Tx queue and wait for completion
4065 * @seid: VSI SEID
4066 * @pf: the PF structure
4067 * @pf_q: the PF queue to configure
4068 * @is_xdp: true if the queue is used for XDP
4069 * @enable: start or stop the queue
4070 **/
4071static int i40e_control_wait_tx_q(int seid, struct i40e_pf *pf, int pf_q,
4072 bool is_xdp, bool enable)
4073{
4074 int ret;
4075
4076 i40e_control_tx_q(pf, pf_q, enable);
4077
4078 /* wait for the change to finish */
4079 ret = i40e_pf_txq_wait(pf, pf_q, enable);
4080 if (ret) {
4081 dev_info(&pf->pdev->dev,
4082 "VSI seid %d %sTx ring %d %sable timeout\n",
4083 seid, (is_xdp ? "XDP " : ""), pf_q,
4084 (enable ? "en" : "dis"));
4085 }
4086
4087 return ret;
4088}
4089
4090/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004091 * i40e_vsi_control_tx - Start or stop a VSI's rings
4092 * @vsi: the VSI being configured
4093 * @enable: start or stop the rings
4094 **/
4095static int i40e_vsi_control_tx(struct i40e_vsi *vsi, bool enable)
4096{
4097 struct i40e_pf *pf = vsi->back;
Jacob Kellerc768e492017-04-13 04:45:47 -04004098 int i, pf_q, ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004099
4100 pf_q = vsi->base_queue;
4101 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
Björn Töpel74608d12017-05-24 07:55:35 +02004102 ret = i40e_control_wait_tx_q(vsi->seid, pf,
4103 pf_q,
4104 false /*is xdp*/, enable);
4105 if (ret)
Neerav Parikh23527302014-06-03 23:50:15 +00004106 break;
Björn Töpel74608d12017-05-24 07:55:35 +02004107
4108 if (!i40e_enabled_xdp_vsi(vsi))
4109 continue;
4110
4111 ret = i40e_control_wait_tx_q(vsi->seid, pf,
4112 pf_q + vsi->alloc_queue_pairs,
4113 true /*is xdp*/, enable);
4114 if (ret)
4115 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004116 }
4117
Neerav Parikh23527302014-06-03 23:50:15 +00004118 return ret;
4119}
4120
4121/**
4122 * i40e_pf_rxq_wait - Wait for a PF's Rx queue to be enabled or disabled
4123 * @pf: the PF being configured
4124 * @pf_q: the PF queue
4125 * @enable: enable or disable state of the queue
4126 *
4127 * This routine will wait for the given Rx queue of the PF to reach the
4128 * enabled or disabled state.
4129 * Returns -ETIMEDOUT in case of failing to reach the requested state after
4130 * multiple retries; else will return 0 in case of success.
4131 **/
4132static int i40e_pf_rxq_wait(struct i40e_pf *pf, int pf_q, bool enable)
4133{
4134 int i;
4135 u32 rx_reg;
4136
4137 for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
4138 rx_reg = rd32(&pf->hw, I40E_QRX_ENA(pf_q));
4139 if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4140 break;
4141
Neerav Parikhf98a2002014-09-13 07:40:44 +00004142 usleep_range(10, 20);
Neerav Parikh23527302014-06-03 23:50:15 +00004143 }
4144 if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
4145 return -ETIMEDOUT;
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00004146
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004147 return 0;
4148}
4149
4150/**
Jacob Kellerc768e492017-04-13 04:45:47 -04004151 * i40e_control_rx_q - Start or stop a particular Rx queue
4152 * @pf: the PF structure
4153 * @pf_q: the PF queue to configure
4154 * @enable: start or stop the queue
4155 *
4156 * This function enables or disables a single queue. Note that any delay
4157 * required after the operation is expected to be handled by the caller of
4158 * this function.
4159 **/
4160static void i40e_control_rx_q(struct i40e_pf *pf, int pf_q, bool enable)
4161{
4162 struct i40e_hw *hw = &pf->hw;
4163 u32 rx_reg;
4164 int i;
4165
4166 for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
4167 rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
4168 if (((rx_reg >> I40E_QRX_ENA_QENA_REQ_SHIFT) & 1) ==
4169 ((rx_reg >> I40E_QRX_ENA_QENA_STAT_SHIFT) & 1))
4170 break;
4171 usleep_range(1000, 2000);
4172 }
4173
4174 /* Skip if the queue is already in the requested state */
4175 if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4176 return;
4177
4178 /* turn on/off the queue */
4179 if (enable)
4180 rx_reg |= I40E_QRX_ENA_QENA_REQ_MASK;
4181 else
4182 rx_reg &= ~I40E_QRX_ENA_QENA_REQ_MASK;
4183
4184 wr32(hw, I40E_QRX_ENA(pf_q), rx_reg);
4185}
4186
4187/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004188 * i40e_vsi_control_rx - Start or stop a VSI's rings
4189 * @vsi: the VSI being configured
4190 * @enable: start or stop the rings
4191 **/
4192static int i40e_vsi_control_rx(struct i40e_vsi *vsi, bool enable)
4193{
4194 struct i40e_pf *pf = vsi->back;
Jacob Kellerc768e492017-04-13 04:45:47 -04004195 int i, pf_q, ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004196
4197 pf_q = vsi->base_queue;
4198 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
Jacob Kellerc768e492017-04-13 04:45:47 -04004199 i40e_control_rx_q(pf, pf_q, enable);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004200
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004201 /* wait for the change to finish */
Neerav Parikh23527302014-06-03 23:50:15 +00004202 ret = i40e_pf_rxq_wait(pf, pf_q, enable);
4203 if (ret) {
4204 dev_info(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04004205 "VSI seid %d Rx ring %d %sable timeout\n",
4206 vsi->seid, pf_q, (enable ? "en" : "dis"));
Neerav Parikh23527302014-06-03 23:50:15 +00004207 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004208 }
4209 }
4210
Wyborny, Carolynd08a9f62017-03-28 08:00:48 -07004211 /* Due to HW errata, on Rx disable only, the register can indicate done
4212 * before it really is. Needs 50ms to be sure
4213 */
4214 if (!enable)
4215 mdelay(50);
4216
Neerav Parikh23527302014-06-03 23:50:15 +00004217 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004218}
4219
4220/**
Filip Sadowski3aa7b742016-10-11 15:26:58 -07004221 * i40e_vsi_start_rings - Start a VSI's rings
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004222 * @vsi: the VSI being configured
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004223 **/
Filip Sadowski3aa7b742016-10-11 15:26:58 -07004224int i40e_vsi_start_rings(struct i40e_vsi *vsi)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004225{
Anjali Singhai Jain3b867b22013-12-21 05:44:44 +00004226 int ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004227
4228 /* do rx first for enable and last for disable */
Filip Sadowski3aa7b742016-10-11 15:26:58 -07004229 ret = i40e_vsi_control_rx(vsi, true);
4230 if (ret)
4231 return ret;
4232 ret = i40e_vsi_control_tx(vsi, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004233
4234 return ret;
4235}
4236
4237/**
Filip Sadowski3aa7b742016-10-11 15:26:58 -07004238 * i40e_vsi_stop_rings - Stop a VSI's rings
4239 * @vsi: the VSI being configured
4240 **/
4241void i40e_vsi_stop_rings(struct i40e_vsi *vsi)
4242{
Jacob Keller34807562017-04-13 04:45:53 -04004243 /* When port TX is suspended, don't wait */
Jacob Keller0da36b92017-04-19 09:25:55 -04004244 if (test_bit(__I40E_PORT_SUSPENDED, vsi->back->state))
Jacob Keller34807562017-04-13 04:45:53 -04004245 return i40e_vsi_stop_rings_no_wait(vsi);
4246
Filip Sadowski3aa7b742016-10-11 15:26:58 -07004247 /* do rx first for enable and last for disable
4248 * Ignore return value, we need to shutdown whatever we can
4249 */
4250 i40e_vsi_control_tx(vsi, false);
4251 i40e_vsi_control_rx(vsi, false);
4252}
4253
4254/**
Jacob Kellere4b433f2017-04-13 04:45:52 -04004255 * i40e_vsi_stop_rings_no_wait - Stop a VSI's rings and do not delay
4256 * @vsi: the VSI being shutdown
4257 *
4258 * This function stops all the rings for a VSI but does not delay to verify
4259 * that rings have been disabled. It is expected that the caller is shutting
4260 * down multiple VSIs at once and will delay together for all the VSIs after
4261 * initiating the shutdown. This is particularly useful for shutting down lots
4262 * of VFs together. Otherwise, a large delay can be incurred while configuring
4263 * each VSI in serial.
4264 **/
4265void i40e_vsi_stop_rings_no_wait(struct i40e_vsi *vsi)
4266{
4267 struct i40e_pf *pf = vsi->back;
4268 int i, pf_q;
4269
4270 pf_q = vsi->base_queue;
4271 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4272 i40e_control_tx_q(pf, pf_q, false);
4273 i40e_control_rx_q(pf, pf_q, false);
4274 }
4275}
4276
4277/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004278 * i40e_vsi_free_irq - Free the irq association with the OS
4279 * @vsi: the VSI being configured
4280 **/
4281static void i40e_vsi_free_irq(struct i40e_vsi *vsi)
4282{
4283 struct i40e_pf *pf = vsi->back;
4284 struct i40e_hw *hw = &pf->hw;
4285 int base = vsi->base_vector;
4286 u32 val, qp;
4287 int i;
4288
4289 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4290 if (!vsi->q_vectors)
4291 return;
4292
Shannon Nelson63741842014-04-23 04:50:16 +00004293 if (!vsi->irqs_ready)
4294 return;
4295
4296 vsi->irqs_ready = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004297 for (i = 0; i < vsi->num_q_vectors; i++) {
Alan Brady96db7762016-09-14 16:24:38 -07004298 int irq_num;
4299 u16 vector;
4300
4301 vector = i + base;
4302 irq_num = pf->msix_entries[vector].vector;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004303
4304 /* free only the irqs that were actually requested */
Shannon Nelson78681b12013-11-28 06:39:36 +00004305 if (!vsi->q_vectors[i] ||
4306 !vsi->q_vectors[i]->num_ringpairs)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004307 continue;
4308
Alan Brady96db7762016-09-14 16:24:38 -07004309 /* clear the affinity notifier in the IRQ descriptor */
4310 irq_set_affinity_notifier(irq_num, NULL);
Jacob Keller759dc4a2017-07-14 09:10:10 -04004311 /* remove our suggested affinity mask for this IRQ */
Alan Brady96db7762016-09-14 16:24:38 -07004312 irq_set_affinity_hint(irq_num, NULL);
4313 synchronize_irq(irq_num);
4314 free_irq(irq_num, vsi->q_vectors[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004315
4316 /* Tear down the interrupt queue link list
4317 *
4318 * We know that they come in pairs and always
4319 * the Rx first, then the Tx. To clear the
4320 * link list, stick the EOL value into the
4321 * next_q field of the registers.
4322 */
4323 val = rd32(hw, I40E_PFINT_LNKLSTN(vector - 1));
4324 qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
4325 >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4326 val |= I40E_QUEUE_END_OF_LIST
4327 << I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4328 wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), val);
4329
4330 while (qp != I40E_QUEUE_END_OF_LIST) {
4331 u32 next;
4332
4333 val = rd32(hw, I40E_QINT_RQCTL(qp));
4334
4335 val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
4336 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
4337 I40E_QINT_RQCTL_CAUSE_ENA_MASK |
4338 I40E_QINT_RQCTL_INTEVENT_MASK);
4339
4340 val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
4341 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
4342
4343 wr32(hw, I40E_QINT_RQCTL(qp), val);
4344
4345 val = rd32(hw, I40E_QINT_TQCTL(qp));
4346
4347 next = (val & I40E_QINT_TQCTL_NEXTQ_INDX_MASK)
4348 >> I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT;
4349
4350 val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
4351 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
4352 I40E_QINT_TQCTL_CAUSE_ENA_MASK |
4353 I40E_QINT_TQCTL_INTEVENT_MASK);
4354
4355 val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
4356 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
4357
4358 wr32(hw, I40E_QINT_TQCTL(qp), val);
4359 qp = next;
4360 }
4361 }
4362 } else {
4363 free_irq(pf->pdev->irq, pf);
4364
4365 val = rd32(hw, I40E_PFINT_LNKLST0);
4366 qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
4367 >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4368 val |= I40E_QUEUE_END_OF_LIST
4369 << I40E_PFINT_LNKLST0_FIRSTQ_INDX_SHIFT;
4370 wr32(hw, I40E_PFINT_LNKLST0, val);
4371
4372 val = rd32(hw, I40E_QINT_RQCTL(qp));
4373 val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
4374 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
4375 I40E_QINT_RQCTL_CAUSE_ENA_MASK |
4376 I40E_QINT_RQCTL_INTEVENT_MASK);
4377
4378 val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
4379 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
4380
4381 wr32(hw, I40E_QINT_RQCTL(qp), val);
4382
4383 val = rd32(hw, I40E_QINT_TQCTL(qp));
4384
4385 val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
4386 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
4387 I40E_QINT_TQCTL_CAUSE_ENA_MASK |
4388 I40E_QINT_TQCTL_INTEVENT_MASK);
4389
4390 val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
4391 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
4392
4393 wr32(hw, I40E_QINT_TQCTL(qp), val);
4394 }
4395}
4396
4397/**
Alexander Duyck493fb302013-09-28 07:01:44 +00004398 * i40e_free_q_vector - Free memory allocated for specific interrupt vector
4399 * @vsi: the VSI being configured
4400 * @v_idx: Index of vector to be freed
4401 *
4402 * This function frees the memory allocated to the q_vector. In addition if
4403 * NAPI is enabled it will delete any references to the NAPI struct prior
4404 * to freeing the q_vector.
4405 **/
4406static void i40e_free_q_vector(struct i40e_vsi *vsi, int v_idx)
4407{
4408 struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00004409 struct i40e_ring *ring;
Alexander Duyck493fb302013-09-28 07:01:44 +00004410
4411 if (!q_vector)
4412 return;
4413
4414 /* disassociate q_vector from rings */
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00004415 i40e_for_each_ring(ring, q_vector->tx)
4416 ring->q_vector = NULL;
4417
4418 i40e_for_each_ring(ring, q_vector->rx)
4419 ring->q_vector = NULL;
Alexander Duyck493fb302013-09-28 07:01:44 +00004420
4421 /* only VSI w/ an associated netdev is set up w/ NAPI */
4422 if (vsi->netdev)
4423 netif_napi_del(&q_vector->napi);
4424
4425 vsi->q_vectors[v_idx] = NULL;
4426
4427 kfree_rcu(q_vector, rcu);
4428}
4429
4430/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004431 * i40e_vsi_free_q_vectors - Free memory allocated for interrupt vectors
4432 * @vsi: the VSI being un-configured
4433 *
4434 * This frees the memory allocated to the q_vectors and
4435 * deletes references to the NAPI struct.
4436 **/
4437static void i40e_vsi_free_q_vectors(struct i40e_vsi *vsi)
4438{
4439 int v_idx;
4440
Alexander Duyck493fb302013-09-28 07:01:44 +00004441 for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++)
4442 i40e_free_q_vector(vsi, v_idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004443}
4444
4445/**
4446 * i40e_reset_interrupt_capability - Disable interrupt setup in OS
4447 * @pf: board private structure
4448 **/
4449static void i40e_reset_interrupt_capability(struct i40e_pf *pf)
4450{
4451 /* If we're in Legacy mode, the interrupt was cleaned in vsi_close */
4452 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4453 pci_disable_msix(pf->pdev);
4454 kfree(pf->msix_entries);
4455 pf->msix_entries = NULL;
Shannon Nelson3b444392015-02-26 16:15:57 +00004456 kfree(pf->irq_pile);
4457 pf->irq_pile = NULL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004458 } else if (pf->flags & I40E_FLAG_MSI_ENABLED) {
4459 pci_disable_msi(pf->pdev);
4460 }
4461 pf->flags &= ~(I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
4462}
4463
4464/**
4465 * i40e_clear_interrupt_scheme - Clear the current interrupt scheme settings
4466 * @pf: board private structure
4467 *
4468 * We go through and clear interrupt specific resources and reset the structure
4469 * to pre-load conditions
4470 **/
4471static void i40e_clear_interrupt_scheme(struct i40e_pf *pf)
4472{
4473 int i;
4474
Jacob Kellerc17401a2017-07-14 09:27:02 -04004475 i40e_free_misc_vector(pf);
Shannon Nelsone1477582015-02-21 06:44:33 +00004476
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06004477 i40e_put_lump(pf->irq_pile, pf->iwarp_base_vector,
4478 I40E_IWARP_IRQ_PILE_ID);
4479
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004480 i40e_put_lump(pf->irq_pile, 0, I40E_PILE_VALID_BIT-1);
Mitch Williams505682c2014-05-20 08:01:37 +00004481 for (i = 0; i < pf->num_alloc_vsi; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004482 if (pf->vsi[i])
4483 i40e_vsi_free_q_vectors(pf->vsi[i]);
4484 i40e_reset_interrupt_capability(pf);
4485}
4486
4487/**
4488 * i40e_napi_enable_all - Enable NAPI for all q_vectors in the VSI
4489 * @vsi: the VSI being configured
4490 **/
4491static void i40e_napi_enable_all(struct i40e_vsi *vsi)
4492{
4493 int q_idx;
4494
4495 if (!vsi->netdev)
4496 return;
4497
Alexander Duyck13a8cd12017-03-24 15:01:42 -07004498 for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
4499 struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
4500
4501 if (q_vector->rx.ring || q_vector->tx.ring)
4502 napi_enable(&q_vector->napi);
4503 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004504}
4505
4506/**
4507 * i40e_napi_disable_all - Disable NAPI for all q_vectors in the VSI
4508 * @vsi: the VSI being configured
4509 **/
4510static void i40e_napi_disable_all(struct i40e_vsi *vsi)
4511{
4512 int q_idx;
4513
4514 if (!vsi->netdev)
4515 return;
4516
Alexander Duyck13a8cd12017-03-24 15:01:42 -07004517 for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
4518 struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
4519
4520 if (q_vector->rx.ring || q_vector->tx.ring)
4521 napi_disable(&q_vector->napi);
4522 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004523}
4524
4525/**
Shannon Nelson90ef8d42014-03-14 07:32:26 +00004526 * i40e_vsi_close - Shut down a VSI
4527 * @vsi: the vsi to be quelled
4528 **/
4529static void i40e_vsi_close(struct i40e_vsi *vsi)
4530{
Mitch Williams0ef2d5a2017-01-24 10:24:00 -08004531 struct i40e_pf *pf = vsi->back;
Jacob Keller0da36b92017-04-19 09:25:55 -04004532 if (!test_and_set_bit(__I40E_VSI_DOWN, vsi->state))
Shannon Nelson90ef8d42014-03-14 07:32:26 +00004533 i40e_down(vsi);
4534 i40e_vsi_free_irq(vsi);
4535 i40e_vsi_free_tx_resources(vsi);
4536 i40e_vsi_free_rx_resources(vsi);
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04004537 vsi->current_netdev_flags = 0;
Mitch Williams0ef2d5a2017-01-24 10:24:00 -08004538 pf->flags |= I40E_FLAG_SERVICE_CLIENT_REQUESTED;
Jacob Keller0da36b92017-04-19 09:25:55 -04004539 if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
Mitch Williams0ef2d5a2017-01-24 10:24:00 -08004540 pf->flags |= I40E_FLAG_CLIENT_RESET;
Shannon Nelson90ef8d42014-03-14 07:32:26 +00004541}
4542
4543/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004544 * i40e_quiesce_vsi - Pause a given VSI
4545 * @vsi: the VSI being paused
4546 **/
4547static void i40e_quiesce_vsi(struct i40e_vsi *vsi)
4548{
Jacob Keller0da36b92017-04-19 09:25:55 -04004549 if (test_bit(__I40E_VSI_DOWN, vsi->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004550 return;
4551
Jacob Keller0da36b92017-04-19 09:25:55 -04004552 set_bit(__I40E_VSI_NEEDS_RESTART, vsi->state);
Jesse Brandeburg6995b362015-08-28 17:55:54 -04004553 if (vsi->netdev && netif_running(vsi->netdev))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004554 vsi->netdev->netdev_ops->ndo_stop(vsi->netdev);
Jesse Brandeburg6995b362015-08-28 17:55:54 -04004555 else
Shannon Nelson90ef8d42014-03-14 07:32:26 +00004556 i40e_vsi_close(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004557}
4558
4559/**
4560 * i40e_unquiesce_vsi - Resume a given VSI
4561 * @vsi: the VSI being resumed
4562 **/
4563static void i40e_unquiesce_vsi(struct i40e_vsi *vsi)
4564{
Jacob Keller0da36b92017-04-19 09:25:55 -04004565 if (!test_and_clear_bit(__I40E_VSI_NEEDS_RESTART, vsi->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004566 return;
4567
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004568 if (vsi->netdev && netif_running(vsi->netdev))
4569 vsi->netdev->netdev_ops->ndo_open(vsi->netdev);
4570 else
Shannon Nelson8276f752014-03-14 07:32:27 +00004571 i40e_vsi_open(vsi); /* this clears the DOWN bit */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004572}
4573
4574/**
4575 * i40e_pf_quiesce_all_vsi - Pause all VSIs on a PF
4576 * @pf: the PF
4577 **/
4578static void i40e_pf_quiesce_all_vsi(struct i40e_pf *pf)
4579{
4580 int v;
4581
Mitch Williams505682c2014-05-20 08:01:37 +00004582 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004583 if (pf->vsi[v])
4584 i40e_quiesce_vsi(pf->vsi[v]);
4585 }
4586}
4587
4588/**
4589 * i40e_pf_unquiesce_all_vsi - Resume all VSIs on a PF
4590 * @pf: the PF
4591 **/
4592static void i40e_pf_unquiesce_all_vsi(struct i40e_pf *pf)
4593{
4594 int v;
4595
Mitch Williams505682c2014-05-20 08:01:37 +00004596 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004597 if (pf->vsi[v])
4598 i40e_unquiesce_vsi(pf->vsi[v]);
4599 }
4600}
4601
Neerav Parikh69129dc2014-11-12 00:18:46 +00004602/**
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004603 * i40e_vsi_wait_queues_disabled - Wait for VSI's queues to be disabled
Neerav Parikh69129dc2014-11-12 00:18:46 +00004604 * @vsi: the VSI being configured
4605 *
Jacob Kelleraf26ce22017-04-05 07:50:58 -04004606 * Wait until all queues on a given VSI have been disabled.
Neerav Parikh69129dc2014-11-12 00:18:46 +00004607 **/
Jacob Kellere4b433f2017-04-13 04:45:52 -04004608int i40e_vsi_wait_queues_disabled(struct i40e_vsi *vsi)
Neerav Parikh69129dc2014-11-12 00:18:46 +00004609{
4610 struct i40e_pf *pf = vsi->back;
4611 int i, pf_q, ret;
4612
4613 pf_q = vsi->base_queue;
4614 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
Jacob Kelleraf26ce22017-04-05 07:50:58 -04004615 /* Check and wait for the Tx queue */
Neerav Parikh69129dc2014-11-12 00:18:46 +00004616 ret = i40e_pf_txq_wait(pf, pf_q, false);
4617 if (ret) {
4618 dev_info(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04004619 "VSI seid %d Tx ring %d disable timeout\n",
4620 vsi->seid, pf_q);
Neerav Parikh69129dc2014-11-12 00:18:46 +00004621 return ret;
4622 }
Björn Töpel74608d12017-05-24 07:55:35 +02004623
4624 if (!i40e_enabled_xdp_vsi(vsi))
4625 goto wait_rx;
4626
4627 /* Check and wait for the XDP Tx queue */
4628 ret = i40e_pf_txq_wait(pf, pf_q + vsi->alloc_queue_pairs,
4629 false);
4630 if (ret) {
4631 dev_info(&pf->pdev->dev,
4632 "VSI seid %d XDP Tx ring %d disable timeout\n",
4633 vsi->seid, pf_q);
4634 return ret;
4635 }
4636wait_rx:
4637 /* Check and wait for the Rx queue */
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004638 ret = i40e_pf_rxq_wait(pf, pf_q, false);
4639 if (ret) {
4640 dev_info(&pf->pdev->dev,
4641 "VSI seid %d Rx ring %d disable timeout\n",
4642 vsi->seid, pf_q);
4643 return ret;
4644 }
4645 }
4646
Neerav Parikh69129dc2014-11-12 00:18:46 +00004647 return 0;
4648}
4649
Jacob Kellere4b433f2017-04-13 04:45:52 -04004650#ifdef CONFIG_I40E_DCB
Neerav Parikh69129dc2014-11-12 00:18:46 +00004651/**
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004652 * i40e_pf_wait_queues_disabled - Wait for all queues of PF VSIs to be disabled
Neerav Parikh69129dc2014-11-12 00:18:46 +00004653 * @pf: the PF
4654 *
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004655 * This function waits for the queues to be in disabled state for all the
Neerav Parikh69129dc2014-11-12 00:18:46 +00004656 * VSIs that are managed by this PF.
4657 **/
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004658static int i40e_pf_wait_queues_disabled(struct i40e_pf *pf)
Neerav Parikh69129dc2014-11-12 00:18:46 +00004659{
4660 int v, ret = 0;
4661
4662 for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
Alexander Duyckc76cb6e2017-02-21 15:55:48 -08004663 if (pf->vsi[v]) {
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004664 ret = i40e_vsi_wait_queues_disabled(pf->vsi[v]);
Neerav Parikh69129dc2014-11-12 00:18:46 +00004665 if (ret)
4666 break;
4667 }
4668 }
4669
4670 return ret;
4671}
4672
4673#endif
Kiran Patilb03a8c12015-09-24 18:13:15 -04004674
4675/**
4676 * i40e_detect_recover_hung_queue - Function to detect and recover hung_queue
4677 * @q_idx: TX queue number
4678 * @vsi: Pointer to VSI struct
4679 *
4680 * This function checks specified queue for given VSI. Detects hung condition.
Alan Brady17daabb2017-04-05 07:50:56 -04004681 * We proactively detect hung TX queues by checking if interrupts are disabled
4682 * but there are pending descriptors. If it appears hung, attempt to recover
4683 * by triggering a SW interrupt.
Kiran Patilb03a8c12015-09-24 18:13:15 -04004684 **/
4685static void i40e_detect_recover_hung_queue(int q_idx, struct i40e_vsi *vsi)
4686{
4687 struct i40e_ring *tx_ring = NULL;
4688 struct i40e_pf *pf;
Alan Brady17daabb2017-04-05 07:50:56 -04004689 u32 val, tx_pending;
Kiran Patilb03a8c12015-09-24 18:13:15 -04004690 int i;
4691
4692 pf = vsi->back;
4693
4694 /* now that we have an index, find the tx_ring struct */
4695 for (i = 0; i < vsi->num_queue_pairs; i++) {
4696 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc) {
4697 if (q_idx == vsi->tx_rings[i]->queue_index) {
4698 tx_ring = vsi->tx_rings[i];
4699 break;
4700 }
4701 }
4702 }
4703
4704 if (!tx_ring)
4705 return;
4706
4707 /* Read interrupt register */
4708 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
4709 val = rd32(&pf->hw,
4710 I40E_PFINT_DYN_CTLN(tx_ring->q_vector->v_idx +
4711 tx_ring->vsi->base_vector - 1));
4712 else
4713 val = rd32(&pf->hw, I40E_PFINT_DYN_CTL0);
4714
Alan Brady17daabb2017-04-05 07:50:56 -04004715 tx_pending = i40e_get_tx_pending(tx_ring);
Kiran Patilb03a8c12015-09-24 18:13:15 -04004716
Alan Brady17daabb2017-04-05 07:50:56 -04004717 /* Interrupts are disabled and TX pending is non-zero,
4718 * trigger the SW interrupt (don't wait). Worst case
4719 * there will be one extra interrupt which may result
4720 * into not cleaning any queues because queues are cleaned.
Kiran Patilb03a8c12015-09-24 18:13:15 -04004721 */
Alan Brady17daabb2017-04-05 07:50:56 -04004722 if (tx_pending && (!(val & I40E_PFINT_DYN_CTLN_INTENA_MASK)))
4723 i40e_force_wb(vsi, tx_ring->q_vector);
Kiran Patilb03a8c12015-09-24 18:13:15 -04004724}
4725
4726/**
4727 * i40e_detect_recover_hung - Function to detect and recover hung_queues
4728 * @pf: pointer to PF struct
4729 *
4730 * LAN VSI has netdev and netdev has TX queues. This function is to check
4731 * each of those TX queues if they are hung, trigger recovery by issuing
4732 * SW interrupt.
4733 **/
4734static void i40e_detect_recover_hung(struct i40e_pf *pf)
4735{
4736 struct net_device *netdev;
4737 struct i40e_vsi *vsi;
Jesse Brandeburgb85c94b2017-06-20 15:16:59 -07004738 unsigned int i;
Kiran Patilb03a8c12015-09-24 18:13:15 -04004739
4740 /* Only for LAN VSI */
4741 vsi = pf->vsi[pf->lan_vsi];
4742
4743 if (!vsi)
4744 return;
4745
4746 /* Make sure, VSI state is not DOWN/RECOVERY_PENDING */
Jacob Keller0da36b92017-04-19 09:25:55 -04004747 if (test_bit(__I40E_VSI_DOWN, vsi->back->state) ||
4748 test_bit(__I40E_RESET_RECOVERY_PENDING, vsi->back->state))
Kiran Patilb03a8c12015-09-24 18:13:15 -04004749 return;
4750
4751 /* Make sure type is MAIN VSI */
4752 if (vsi->type != I40E_VSI_MAIN)
4753 return;
4754
4755 netdev = vsi->netdev;
4756 if (!netdev)
4757 return;
4758
4759 /* Bail out if netif_carrier is not OK */
4760 if (!netif_carrier_ok(netdev))
4761 return;
4762
4763 /* Go thru' TX queues for netdev */
4764 for (i = 0; i < netdev->num_tx_queues; i++) {
4765 struct netdev_queue *q;
4766
4767 q = netdev_get_tx_queue(netdev, i);
4768 if (q)
4769 i40e_detect_recover_hung_queue(i, vsi);
4770 }
4771}
4772
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004773/**
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004774 * i40e_get_iscsi_tc_map - Return TC map for iSCSI APP
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00004775 * @pf: pointer to PF
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004776 *
4777 * Get TC map for ISCSI PF type that will include iSCSI TC
4778 * and LAN TC.
4779 **/
4780static u8 i40e_get_iscsi_tc_map(struct i40e_pf *pf)
4781{
4782 struct i40e_dcb_app_priority_table app;
4783 struct i40e_hw *hw = &pf->hw;
4784 u8 enabled_tc = 1; /* TC0 is always enabled */
4785 u8 tc, i;
4786 /* Get the iSCSI APP TLV */
4787 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
4788
4789 for (i = 0; i < dcbcfg->numapps; i++) {
4790 app = dcbcfg->app[i];
4791 if (app.selector == I40E_APP_SEL_TCPIP &&
4792 app.protocolid == I40E_APP_PROTOID_ISCSI) {
4793 tc = dcbcfg->etscfg.prioritytable[app.priority];
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08004794 enabled_tc |= BIT(tc);
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004795 break;
4796 }
4797 }
4798
4799 return enabled_tc;
4800}
4801
4802/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004803 * i40e_dcb_get_num_tc - Get the number of TCs from DCBx config
4804 * @dcbcfg: the corresponding DCBx configuration structure
4805 *
4806 * Return the number of TCs from given DCBx configuration
4807 **/
4808static u8 i40e_dcb_get_num_tc(struct i40e_dcbx_config *dcbcfg)
4809{
Dave Ertmanfbfe12c2016-08-12 09:56:32 -07004810 int i, tc_unused = 0;
Jesse Brandeburg078b5872013-09-25 23:41:14 +00004811 u8 num_tc = 0;
Dave Ertmanfbfe12c2016-08-12 09:56:32 -07004812 u8 ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004813
4814 /* Scan the ETS Config Priority Table to find
4815 * traffic class enabled for a given priority
Dave Ertmanfbfe12c2016-08-12 09:56:32 -07004816 * and create a bitmask of enabled TCs
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004817 */
Dave Ertmanfbfe12c2016-08-12 09:56:32 -07004818 for (i = 0; i < I40E_MAX_USER_PRIORITY; i++)
4819 num_tc |= BIT(dcbcfg->etscfg.prioritytable[i]);
4820
4821 /* Now scan the bitmask to check for
4822 * contiguous TCs starting with TC0
4823 */
4824 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
4825 if (num_tc & BIT(i)) {
4826 if (!tc_unused) {
4827 ret++;
4828 } else {
4829 pr_err("Non-contiguous TC - Disabling DCB\n");
4830 return 1;
4831 }
4832 } else {
4833 tc_unused = 1;
4834 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004835 }
4836
Dave Ertmanfbfe12c2016-08-12 09:56:32 -07004837 /* There is always at least TC0 */
4838 if (!ret)
4839 ret = 1;
4840
4841 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004842}
4843
4844/**
4845 * i40e_dcb_get_enabled_tc - Get enabled traffic classes
4846 * @dcbcfg: the corresponding DCBx configuration structure
4847 *
4848 * Query the current DCB configuration and return the number of
4849 * traffic classes enabled from the given DCBX config
4850 **/
4851static u8 i40e_dcb_get_enabled_tc(struct i40e_dcbx_config *dcbcfg)
4852{
4853 u8 num_tc = i40e_dcb_get_num_tc(dcbcfg);
4854 u8 enabled_tc = 1;
4855 u8 i;
4856
4857 for (i = 0; i < num_tc; i++)
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04004858 enabled_tc |= BIT(i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004859
4860 return enabled_tc;
4861}
4862
4863/**
4864 * i40e_pf_get_num_tc - Get enabled traffic classes for PF
4865 * @pf: PF being queried
4866 *
4867 * Return number of traffic classes enabled for the given PF
4868 **/
4869static u8 i40e_pf_get_num_tc(struct i40e_pf *pf)
4870{
4871 struct i40e_hw *hw = &pf->hw;
Dave Ertman52a08ca2016-07-27 12:02:34 -07004872 u8 i, enabled_tc = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004873 u8 num_tc = 0;
4874 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
4875
4876 /* If DCB is not enabled then always in single TC */
4877 if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
4878 return 1;
4879
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004880 /* SFP mode will be enabled for all TCs on port */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004881 if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
4882 return i40e_dcb_get_num_tc(dcbcfg);
4883
4884 /* MFP mode return count of enabled TCs for this PF */
4885 if (pf->hw.func_caps.iscsi)
4886 enabled_tc = i40e_get_iscsi_tc_map(pf);
4887 else
Neerav Parikhfc51de92015-02-24 06:58:53 +00004888 return 1; /* Only TC0 */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004889
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004890 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08004891 if (enabled_tc & BIT(i))
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004892 num_tc++;
4893 }
4894 return num_tc;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004895}
4896
4897/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004898 * i40e_pf_get_pf_tc_map - Get bitmap for enabled traffic classes
4899 * @pf: PF being queried
4900 *
4901 * Return a bitmap for enabled traffic classes for this PF.
4902 **/
4903static u8 i40e_pf_get_tc_map(struct i40e_pf *pf)
4904{
4905 /* If DCB is not enabled for this PF then just return default TC */
4906 if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
David Ertmanea6acb72016-09-20 07:10:50 -07004907 return I40E_DEFAULT_TRAFFIC_CLASS;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004908
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004909 /* SFP mode we want PF to be enabled for all TCs */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004910 if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
4911 return i40e_dcb_get_enabled_tc(&pf->hw.local_dcbx_config);
4912
Neerav Parikhfc51de92015-02-24 06:58:53 +00004913 /* MFP enabled and iSCSI PF type */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004914 if (pf->hw.func_caps.iscsi)
4915 return i40e_get_iscsi_tc_map(pf);
4916 else
David Ertmanea6acb72016-09-20 07:10:50 -07004917 return I40E_DEFAULT_TRAFFIC_CLASS;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004918}
4919
4920/**
4921 * i40e_vsi_get_bw_info - Query VSI BW Information
4922 * @vsi: the VSI being queried
4923 *
4924 * Returns 0 on success, negative value on failure
4925 **/
4926static int i40e_vsi_get_bw_info(struct i40e_vsi *vsi)
4927{
4928 struct i40e_aqc_query_vsi_ets_sla_config_resp bw_ets_config = {0};
4929 struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
4930 struct i40e_pf *pf = vsi->back;
4931 struct i40e_hw *hw = &pf->hw;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004932 i40e_status ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004933 u32 tc_bw_max;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004934 int i;
4935
4936 /* Get the VSI level BW configuration */
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004937 ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid, &bw_config, NULL);
4938 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004939 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004940 "couldn't get PF vsi bw config, err %s aq_err %s\n",
4941 i40e_stat_str(&pf->hw, ret),
4942 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004943 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004944 }
4945
4946 /* Get the VSI level BW configuration per TC */
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004947 ret = i40e_aq_query_vsi_ets_sla_config(hw, vsi->seid, &bw_ets_config,
4948 NULL);
4949 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004950 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004951 "couldn't get PF vsi ets bw config, err %s aq_err %s\n",
4952 i40e_stat_str(&pf->hw, ret),
4953 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004954 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004955 }
4956
4957 if (bw_config.tc_valid_bits != bw_ets_config.tc_valid_bits) {
4958 dev_info(&pf->pdev->dev,
4959 "Enabled TCs mismatch from querying VSI BW info 0x%08x 0x%08x\n",
4960 bw_config.tc_valid_bits,
4961 bw_ets_config.tc_valid_bits);
4962 /* Still continuing */
4963 }
4964
4965 vsi->bw_limit = le16_to_cpu(bw_config.port_bw_limit);
4966 vsi->bw_max_quanta = bw_config.max_bw;
4967 tc_bw_max = le16_to_cpu(bw_ets_config.tc_bw_max[0]) |
4968 (le16_to_cpu(bw_ets_config.tc_bw_max[1]) << 16);
4969 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
4970 vsi->bw_ets_share_credits[i] = bw_ets_config.share_credits[i];
4971 vsi->bw_ets_limit_credits[i] =
4972 le16_to_cpu(bw_ets_config.credits[i]);
4973 /* 3 bits out of 4 for each TC */
4974 vsi->bw_ets_max_quanta[i] = (u8)((tc_bw_max >> (i*4)) & 0x7);
4975 }
Jesse Brandeburg078b5872013-09-25 23:41:14 +00004976
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004977 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004978}
4979
4980/**
4981 * i40e_vsi_configure_bw_alloc - Configure VSI BW allocation per TC
4982 * @vsi: the VSI being configured
4983 * @enabled_tc: TC bitmap
4984 * @bw_credits: BW shared credits per TC
4985 *
4986 * Returns 0 on success, negative value on failure
4987 **/
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004988static int i40e_vsi_configure_bw_alloc(struct i40e_vsi *vsi, u8 enabled_tc,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004989 u8 *bw_share)
4990{
4991 struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004992 i40e_status ret;
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004993 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004994
4995 bw_data.tc_valid_bits = enabled_tc;
4996 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
4997 bw_data.tc_bw_credits[i] = bw_share[i];
4998
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004999 ret = i40e_aq_config_vsi_tc_bw(&vsi->back->hw, vsi->seid, &bw_data,
5000 NULL);
5001 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005002 dev_info(&vsi->back->pdev->dev,
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005003 "AQ command Config VSI BW allocation per TC failed = %d\n",
5004 vsi->back->hw.aq.asq_last_status);
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00005005 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005006 }
5007
5008 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5009 vsi->info.qs_handle[i] = bw_data.qs_handles[i];
5010
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00005011 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005012}
5013
5014/**
5015 * i40e_vsi_config_netdev_tc - Setup the netdev TC configuration
5016 * @vsi: the VSI being configured
5017 * @enabled_tc: TC map to be enabled
5018 *
5019 **/
5020static void i40e_vsi_config_netdev_tc(struct i40e_vsi *vsi, u8 enabled_tc)
5021{
5022 struct net_device *netdev = vsi->netdev;
5023 struct i40e_pf *pf = vsi->back;
5024 struct i40e_hw *hw = &pf->hw;
5025 u8 netdev_tc = 0;
5026 int i;
5027 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5028
5029 if (!netdev)
5030 return;
5031
5032 if (!enabled_tc) {
5033 netdev_reset_tc(netdev);
5034 return;
5035 }
5036
5037 /* Set up actual enabled TCs on the VSI */
5038 if (netdev_set_num_tc(netdev, vsi->tc_config.numtc))
5039 return;
5040
5041 /* set per TC queues for the VSI */
5042 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5043 /* Only set TC queues for enabled tcs
5044 *
5045 * e.g. For a VSI that has TC0 and TC3 enabled the
5046 * enabled_tc bitmap would be 0x00001001; the driver
5047 * will set the numtc for netdev as 2 that will be
5048 * referenced by the netdev layer as TC 0 and 1.
5049 */
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08005050 if (vsi->tc_config.enabled_tc & BIT(i))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005051 netdev_set_tc_queue(netdev,
5052 vsi->tc_config.tc_info[i].netdev_tc,
5053 vsi->tc_config.tc_info[i].qcount,
5054 vsi->tc_config.tc_info[i].qoffset);
5055 }
5056
5057 /* Assign UP2TC map for the VSI */
5058 for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
5059 /* Get the actual TC# for the UP */
5060 u8 ets_tc = dcbcfg->etscfg.prioritytable[i];
5061 /* Get the mapped netdev TC# for the UP */
5062 netdev_tc = vsi->tc_config.tc_info[ets_tc].netdev_tc;
5063 netdev_set_prio_tc_map(netdev, i, netdev_tc);
5064 }
5065}
5066
5067/**
5068 * i40e_vsi_update_queue_map - Update our copy of VSi info with new queue map
5069 * @vsi: the VSI being configured
5070 * @ctxt: the ctxt buffer returned from AQ VSI update param command
5071 **/
5072static void i40e_vsi_update_queue_map(struct i40e_vsi *vsi,
5073 struct i40e_vsi_context *ctxt)
5074{
5075 /* copy just the sections touched not the entire info
5076 * since not all sections are valid as returned by
5077 * update vsi params
5078 */
5079 vsi->info.mapping_flags = ctxt->info.mapping_flags;
5080 memcpy(&vsi->info.queue_mapping,
5081 &ctxt->info.queue_mapping, sizeof(vsi->info.queue_mapping));
5082 memcpy(&vsi->info.tc_mapping, ctxt->info.tc_mapping,
5083 sizeof(vsi->info.tc_mapping));
5084}
5085
5086/**
5087 * i40e_vsi_config_tc - Configure VSI Tx Scheduler for given TC map
5088 * @vsi: VSI to be configured
5089 * @enabled_tc: TC bitmap
5090 *
5091 * This configures a particular VSI for TCs that are mapped to the
5092 * given TC bitmap. It uses default bandwidth share for TCs across
5093 * VSIs to configure TC for a particular VSI.
5094 *
5095 * NOTE:
5096 * It is expected that the VSI queues have been quisced before calling
5097 * this function.
5098 **/
5099static int i40e_vsi_config_tc(struct i40e_vsi *vsi, u8 enabled_tc)
5100{
5101 u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
5102 struct i40e_vsi_context ctxt;
5103 int ret = 0;
5104 int i;
5105
5106 /* Check if enabled_tc is same as existing or new TCs */
5107 if (vsi->tc_config.enabled_tc == enabled_tc)
5108 return ret;
5109
5110 /* Enable ETS TCs with equal BW Share for now across all VSIs */
5111 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08005112 if (enabled_tc & BIT(i))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005113 bw_share[i] = 1;
5114 }
5115
5116 ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
5117 if (ret) {
5118 dev_info(&vsi->back->pdev->dev,
5119 "Failed configuring TC map %d for VSI %d\n",
5120 enabled_tc, vsi->seid);
5121 goto out;
5122 }
5123
5124 /* Update Queue Pairs Mapping for currently enabled UPs */
5125 ctxt.seid = vsi->seid;
5126 ctxt.pf_num = vsi->back->hw.pf_id;
5127 ctxt.vf_num = 0;
5128 ctxt.uplink_seid = vsi->uplink_seid;
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07005129 ctxt.info = vsi->info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005130 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
5131
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06005132 if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
5133 ctxt.info.valid_sections |=
5134 cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
5135 ctxt.info.queueing_opt_flags |= I40E_AQ_VSI_QUE_OPT_TCP_ENA;
5136 }
5137
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005138 /* Update the VSI after updating the VSI queue-mapping information */
5139 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
5140 if (ret) {
5141 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005142 "Update vsi tc config failed, err %s aq_err %s\n",
5143 i40e_stat_str(&vsi->back->hw, ret),
5144 i40e_aq_str(&vsi->back->hw,
5145 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005146 goto out;
5147 }
5148 /* update the local VSI info with updated queue map */
5149 i40e_vsi_update_queue_map(vsi, &ctxt);
5150 vsi->info.valid_sections = 0;
5151
5152 /* Update current VSI BW information */
5153 ret = i40e_vsi_get_bw_info(vsi);
5154 if (ret) {
5155 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005156 "Failed updating vsi bw info, err %s aq_err %s\n",
5157 i40e_stat_str(&vsi->back->hw, ret),
5158 i40e_aq_str(&vsi->back->hw,
5159 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005160 goto out;
5161 }
5162
5163 /* Update the netdev TC setup */
5164 i40e_vsi_config_netdev_tc(vsi, enabled_tc);
5165out:
5166 return ret;
5167}
5168
5169/**
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005170 * i40e_veb_config_tc - Configure TCs for given VEB
5171 * @veb: given VEB
5172 * @enabled_tc: TC bitmap
5173 *
5174 * Configures given TC bitmap for VEB (switching) element
5175 **/
5176int i40e_veb_config_tc(struct i40e_veb *veb, u8 enabled_tc)
5177{
5178 struct i40e_aqc_configure_switching_comp_bw_config_data bw_data = {0};
5179 struct i40e_pf *pf = veb->pf;
5180 int ret = 0;
5181 int i;
5182
5183 /* No TCs or already enabled TCs just return */
5184 if (!enabled_tc || veb->enabled_tc == enabled_tc)
5185 return ret;
5186
5187 bw_data.tc_valid_bits = enabled_tc;
5188 /* bw_data.absolute_credits is not set (relative) */
5189
5190 /* Enable ETS TCs with equal BW Share for now */
5191 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08005192 if (enabled_tc & BIT(i))
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005193 bw_data.tc_bw_share_credits[i] = 1;
5194 }
5195
5196 ret = i40e_aq_config_switch_comp_bw_config(&pf->hw, veb->seid,
5197 &bw_data, NULL);
5198 if (ret) {
5199 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005200 "VEB bw config failed, err %s aq_err %s\n",
5201 i40e_stat_str(&pf->hw, ret),
5202 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005203 goto out;
5204 }
5205
5206 /* Update the BW information */
5207 ret = i40e_veb_get_bw_info(veb);
5208 if (ret) {
5209 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005210 "Failed getting veb bw config, err %s aq_err %s\n",
5211 i40e_stat_str(&pf->hw, ret),
5212 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005213 }
5214
5215out:
5216 return ret;
5217}
5218
5219#ifdef CONFIG_I40E_DCB
5220/**
5221 * i40e_dcb_reconfigure - Reconfigure all VEBs and VSIs
5222 * @pf: PF struct
5223 *
5224 * Reconfigure VEB/VSIs on a given PF; it is assumed that
5225 * the caller would've quiesce all the VSIs before calling
5226 * this function
5227 **/
5228static void i40e_dcb_reconfigure(struct i40e_pf *pf)
5229{
5230 u8 tc_map = 0;
5231 int ret;
5232 u8 v;
5233
5234 /* Enable the TCs available on PF to all VEBs */
5235 tc_map = i40e_pf_get_tc_map(pf);
5236 for (v = 0; v < I40E_MAX_VEB; v++) {
5237 if (!pf->veb[v])
5238 continue;
5239 ret = i40e_veb_config_tc(pf->veb[v], tc_map);
5240 if (ret) {
5241 dev_info(&pf->pdev->dev,
5242 "Failed configuring TC for VEB seid=%d\n",
5243 pf->veb[v]->seid);
5244 /* Will try to configure as many components */
5245 }
5246 }
5247
5248 /* Update each VSI */
Mitch Williams505682c2014-05-20 08:01:37 +00005249 for (v = 0; v < pf->num_alloc_vsi; v++) {
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005250 if (!pf->vsi[v])
5251 continue;
5252
5253 /* - Enable all TCs for the LAN VSI
5254 * - For all others keep them at TC0 for now
5255 */
5256 if (v == pf->lan_vsi)
5257 tc_map = i40e_pf_get_tc_map(pf);
5258 else
David Ertmanea6acb72016-09-20 07:10:50 -07005259 tc_map = I40E_DEFAULT_TRAFFIC_CLASS;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005260
5261 ret = i40e_vsi_config_tc(pf->vsi[v], tc_map);
5262 if (ret) {
5263 dev_info(&pf->pdev->dev,
5264 "Failed configuring TC for VSI seid=%d\n",
5265 pf->vsi[v]->seid);
5266 /* Will try to configure as many components */
5267 } else {
Neerav Parikh0672a092014-04-01 07:11:47 +00005268 /* Re-configure VSI vectors based on updated TC map */
5269 i40e_vsi_map_rings_to_vectors(pf->vsi[v]);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005270 if (pf->vsi[v]->netdev)
5271 i40e_dcbnl_set_all(pf->vsi[v]);
5272 }
5273 }
5274}
5275
5276/**
Neerav Parikh2fd75f32014-11-12 00:18:20 +00005277 * i40e_resume_port_tx - Resume port Tx
5278 * @pf: PF struct
5279 *
5280 * Resume a port's Tx and issue a PF reset in case of failure to
5281 * resume.
5282 **/
5283static int i40e_resume_port_tx(struct i40e_pf *pf)
5284{
5285 struct i40e_hw *hw = &pf->hw;
5286 int ret;
5287
5288 ret = i40e_aq_resume_port_tx(hw, NULL);
5289 if (ret) {
5290 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005291 "Resume Port Tx failed, err %s aq_err %s\n",
5292 i40e_stat_str(&pf->hw, ret),
5293 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh2fd75f32014-11-12 00:18:20 +00005294 /* Schedule PF reset to recover */
Jacob Keller0da36b92017-04-19 09:25:55 -04005295 set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
Neerav Parikh2fd75f32014-11-12 00:18:20 +00005296 i40e_service_event_schedule(pf);
5297 }
5298
5299 return ret;
5300}
5301
5302/**
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005303 * i40e_init_pf_dcb - Initialize DCB configuration
5304 * @pf: PF being configured
5305 *
5306 * Query the current DCB configuration and cache it
5307 * in the hardware structure
5308 **/
5309static int i40e_init_pf_dcb(struct i40e_pf *pf)
5310{
5311 struct i40e_hw *hw = &pf->hw;
5312 int err = 0;
5313
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +00005314 /* Do not enable DCB for SW1 and SW2 images even if the FW is capable */
Jacob Kellerd36e41d2017-06-23 04:24:46 -04005315 if (pf->hw_features & I40E_HW_NO_DCB_SUPPORT)
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +00005316 goto out;
5317
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005318 /* Get the initial DCB configuration */
5319 err = i40e_init_dcb(hw);
5320 if (!err) {
5321 /* Device/Function is not DCBX capable */
5322 if ((!hw->func_caps.dcb) ||
5323 (hw->dcbx_status == I40E_DCBX_STATUS_DISABLED)) {
5324 dev_info(&pf->pdev->dev,
5325 "DCBX offload is not supported or is disabled for this PF.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005326 } else {
5327 /* When status is not DISABLED then DCBX in FW */
5328 pf->dcbx_cap = DCB_CAP_DCBX_LLD_MANAGED |
5329 DCB_CAP_DCBX_VER_IEEE;
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005330
5331 pf->flags |= I40E_FLAG_DCB_CAPABLE;
Dave Ertmana0362442016-08-29 17:38:26 -07005332 /* Enable DCB tagging only when more than one TC
5333 * or explicitly disable if only one TC
5334 */
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005335 if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
5336 pf->flags |= I40E_FLAG_DCB_ENABLED;
Dave Ertmana0362442016-08-29 17:38:26 -07005337 else
5338 pf->flags &= ~I40E_FLAG_DCB_ENABLED;
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005339 dev_dbg(&pf->pdev->dev,
5340 "DCBX offload is supported for this PF.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005341 }
Neerav Parikh014269f2014-04-01 07:11:48 +00005342 } else {
Shannon Nelsonaebfc812014-12-11 07:06:38 +00005343 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005344 "Query for DCB configuration failed, err %s aq_err %s\n",
5345 i40e_stat_str(&pf->hw, err),
5346 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005347 }
5348
5349out:
5350 return err;
5351}
5352#endif /* CONFIG_I40E_DCB */
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005353#define SPEED_SIZE 14
5354#define FC_SIZE 8
5355/**
5356 * i40e_print_link_message - print link up or down
5357 * @vsi: the VSI for which link needs a message
5358 */
Matt Jaredc156f852015-08-27 11:42:39 -04005359void i40e_print_link_message(struct i40e_vsi *vsi, bool isup)
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005360{
Filip Sadowski7ec9ba12016-11-08 13:05:13 -08005361 enum i40e_aq_link_speed new_speed;
Sudheer Mogilappagari3fded462017-07-14 09:10:18 -04005362 struct i40e_pf *pf = vsi->back;
Shannon Nelsona9165492015-09-03 17:19:00 -04005363 char *speed = "Unknown";
5364 char *fc = "Unknown";
Henry Tieman3e03d7c2016-12-02 12:32:57 -08005365 char *fec = "";
Mariusz Stachura68e49702017-07-12 05:46:14 -04005366 char *req_fec = "";
Henry Tieman3e03d7c2016-12-02 12:32:57 -08005367 char *an = "";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005368
Sudheer Mogilappagari3fded462017-07-14 09:10:18 -04005369 new_speed = pf->hw.phy.link_info.link_speed;
Filip Sadowski7ec9ba12016-11-08 13:05:13 -08005370
5371 if ((vsi->current_isup == isup) && (vsi->current_speed == new_speed))
Matt Jaredc156f852015-08-27 11:42:39 -04005372 return;
5373 vsi->current_isup = isup;
Filip Sadowski7ec9ba12016-11-08 13:05:13 -08005374 vsi->current_speed = new_speed;
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005375 if (!isup) {
5376 netdev_info(vsi->netdev, "NIC Link is Down\n");
5377 return;
5378 }
5379
Greg Rose148c2d82014-12-11 07:06:27 +00005380 /* Warn user if link speed on NPAR enabled partition is not at
5381 * least 10GB
5382 */
Sudheer Mogilappagari3fded462017-07-14 09:10:18 -04005383 if (pf->hw.func_caps.npar_enable &&
5384 (pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_1GB ||
5385 pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_100MB))
Greg Rose148c2d82014-12-11 07:06:27 +00005386 netdev_warn(vsi->netdev,
5387 "The partition detected link speed that is less than 10Gbps\n");
5388
Sudheer Mogilappagari3fded462017-07-14 09:10:18 -04005389 switch (pf->hw.phy.link_info.link_speed) {
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005390 case I40E_LINK_SPEED_40GB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005391 speed = "40 G";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005392 break;
Jesse Brandeburgae24b402015-03-27 00:12:09 -07005393 case I40E_LINK_SPEED_20GB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005394 speed = "20 G";
Jesse Brandeburgae24b402015-03-27 00:12:09 -07005395 break;
Carolyn Wyborny31232372016-11-21 13:03:48 -08005396 case I40E_LINK_SPEED_25GB:
5397 speed = "25 G";
5398 break;
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005399 case I40E_LINK_SPEED_10GB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005400 speed = "10 G";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005401 break;
5402 case I40E_LINK_SPEED_1GB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005403 speed = "1000 M";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005404 break;
Mitch Williams5960d332014-09-13 07:40:47 +00005405 case I40E_LINK_SPEED_100MB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005406 speed = "100 M";
Mitch Williams5960d332014-09-13 07:40:47 +00005407 break;
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005408 default:
5409 break;
5410 }
5411
Sudheer Mogilappagari3fded462017-07-14 09:10:18 -04005412 switch (pf->hw.fc.current_mode) {
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005413 case I40E_FC_FULL:
Shannon Nelsona9165492015-09-03 17:19:00 -04005414 fc = "RX/TX";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005415 break;
5416 case I40E_FC_TX_PAUSE:
Shannon Nelsona9165492015-09-03 17:19:00 -04005417 fc = "TX";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005418 break;
5419 case I40E_FC_RX_PAUSE:
Shannon Nelsona9165492015-09-03 17:19:00 -04005420 fc = "RX";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005421 break;
5422 default:
Shannon Nelsona9165492015-09-03 17:19:00 -04005423 fc = "None";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005424 break;
5425 }
5426
Sudheer Mogilappagari3fded462017-07-14 09:10:18 -04005427 if (pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_25GB) {
Mariusz Stachura68e49702017-07-12 05:46:14 -04005428 req_fec = ", Requested FEC: None";
Henry Tieman3e03d7c2016-12-02 12:32:57 -08005429 fec = ", FEC: None";
5430 an = ", Autoneg: False";
5431
Sudheer Mogilappagari3fded462017-07-14 09:10:18 -04005432 if (pf->hw.phy.link_info.an_info & I40E_AQ_AN_COMPLETED)
Henry Tieman3e03d7c2016-12-02 12:32:57 -08005433 an = ", Autoneg: True";
5434
Sudheer Mogilappagari3fded462017-07-14 09:10:18 -04005435 if (pf->hw.phy.link_info.fec_info &
Henry Tieman3e03d7c2016-12-02 12:32:57 -08005436 I40E_AQ_CONFIG_FEC_KR_ENA)
5437 fec = ", FEC: CL74 FC-FEC/BASE-R";
Sudheer Mogilappagari3fded462017-07-14 09:10:18 -04005438 else if (pf->hw.phy.link_info.fec_info &
Henry Tieman3e03d7c2016-12-02 12:32:57 -08005439 I40E_AQ_CONFIG_FEC_RS_ENA)
5440 fec = ", FEC: CL108 RS-FEC";
Mariusz Stachura68e49702017-07-12 05:46:14 -04005441
5442 /* 'CL108 RS-FEC' should be displayed when RS is requested, or
5443 * both RS and FC are requested
5444 */
5445 if (vsi->back->hw.phy.link_info.req_fec_info &
5446 (I40E_AQ_REQUEST_FEC_KR | I40E_AQ_REQUEST_FEC_RS)) {
5447 if (vsi->back->hw.phy.link_info.req_fec_info &
5448 I40E_AQ_REQUEST_FEC_RS)
5449 req_fec = ", Requested FEC: CL108 RS-FEC";
5450 else
5451 req_fec = ", Requested FEC: CL74 FC-FEC/BASE-R";
5452 }
Henry Tieman3e03d7c2016-12-02 12:32:57 -08005453 }
5454
Mariusz Stachura68e49702017-07-12 05:46:14 -04005455 netdev_info(vsi->netdev, "NIC Link is Up, %sbps Full Duplex%s%s%s, Flow Control: %s\n",
5456 speed, req_fec, fec, an, fc);
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005457}
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005458
5459/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005460 * i40e_up_complete - Finish the last steps of bringing up a connection
5461 * @vsi: the VSI being configured
5462 **/
5463static int i40e_up_complete(struct i40e_vsi *vsi)
5464{
5465 struct i40e_pf *pf = vsi->back;
5466 int err;
5467
5468 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
5469 i40e_vsi_configure_msix(vsi);
5470 else
5471 i40e_configure_msi_and_legacy(vsi);
5472
5473 /* start rings */
Filip Sadowski3aa7b742016-10-11 15:26:58 -07005474 err = i40e_vsi_start_rings(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005475 if (err)
5476 return err;
5477
Jacob Keller0da36b92017-04-19 09:25:55 -04005478 clear_bit(__I40E_VSI_DOWN, vsi->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005479 i40e_napi_enable_all(vsi);
5480 i40e_vsi_enable_irq(vsi);
5481
5482 if ((pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP) &&
5483 (vsi->netdev)) {
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005484 i40e_print_link_message(vsi, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005485 netif_tx_start_all_queues(vsi->netdev);
5486 netif_carrier_on(vsi->netdev);
5487 }
Anjali Singhai Jainca64fa42014-02-11 08:26:30 +00005488
5489 /* replay FDIR SB filters */
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005490 if (vsi->type == I40E_VSI_FDIR) {
5491 /* reset fd counters */
Jacob Keller097dbf52017-02-06 14:38:46 -08005492 pf->fd_add_err = 0;
5493 pf->fd_atr_cnt = 0;
Anjali Singhai Jainca64fa42014-02-11 08:26:30 +00005494 i40e_fdir_filter_restore(vsi);
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005495 }
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06005496
5497 /* On the next run of the service_task, notify any clients of the new
5498 * opened netdev
5499 */
5500 pf->flags |= I40E_FLAG_SERVICE_CLIENT_REQUESTED;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005501 i40e_service_event_schedule(pf);
5502
5503 return 0;
5504}
5505
5506/**
5507 * i40e_vsi_reinit_locked - Reset the VSI
5508 * @vsi: the VSI being configured
5509 *
5510 * Rebuild the ring structs after some configuration
5511 * has changed, e.g. MTU size.
5512 **/
5513static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi)
5514{
5515 struct i40e_pf *pf = vsi->back;
5516
5517 WARN_ON(in_interrupt());
Jacob Keller0da36b92017-04-19 09:25:55 -04005518 while (test_and_set_bit(__I40E_CONFIG_BUSY, pf->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005519 usleep_range(1000, 2000);
5520 i40e_down(vsi);
5521
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005522 i40e_up(vsi);
Jacob Keller0da36b92017-04-19 09:25:55 -04005523 clear_bit(__I40E_CONFIG_BUSY, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005524}
5525
5526/**
5527 * i40e_up - Bring the connection back up after being down
5528 * @vsi: the VSI being configured
5529 **/
5530int i40e_up(struct i40e_vsi *vsi)
5531{
5532 int err;
5533
5534 err = i40e_vsi_configure(vsi);
5535 if (!err)
5536 err = i40e_up_complete(vsi);
5537
5538 return err;
5539}
5540
5541/**
5542 * i40e_down - Shutdown the connection processing
5543 * @vsi: the VSI being stopped
5544 **/
5545void i40e_down(struct i40e_vsi *vsi)
5546{
5547 int i;
5548
5549 /* It is assumed that the caller of this function
Jacob Kellerd19cb642017-04-21 13:38:05 -07005550 * sets the vsi->state __I40E_VSI_DOWN bit.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005551 */
5552 if (vsi->netdev) {
5553 netif_carrier_off(vsi->netdev);
5554 netif_tx_disable(vsi->netdev);
5555 }
5556 i40e_vsi_disable_irq(vsi);
Filip Sadowski3aa7b742016-10-11 15:26:58 -07005557 i40e_vsi_stop_rings(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005558 i40e_napi_disable_all(vsi);
5559
5560 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck9f65e152013-09-28 06:00:58 +00005561 i40e_clean_tx_ring(vsi->tx_rings[i]);
Björn Töpel74608d12017-05-24 07:55:35 +02005562 if (i40e_enabled_xdp_vsi(vsi))
5563 i40e_clean_tx_ring(vsi->xdp_rings[i]);
Alexander Duyck9f65e152013-09-28 06:00:58 +00005564 i40e_clean_rx_ring(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005565 }
Catherine Sullivanf980d442016-05-16 10:26:34 -07005566
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005567}
5568
5569/**
5570 * i40e_setup_tc - configure multiple traffic classes
5571 * @netdev: net device to configure
5572 * @tc: number of traffic classes to enable
5573 **/
5574static int i40e_setup_tc(struct net_device *netdev, u8 tc)
5575{
5576 struct i40e_netdev_priv *np = netdev_priv(netdev);
5577 struct i40e_vsi *vsi = np->vsi;
5578 struct i40e_pf *pf = vsi->back;
5579 u8 enabled_tc = 0;
5580 int ret = -EINVAL;
5581 int i;
5582
5583 /* Check if DCB enabled to continue */
5584 if (!(pf->flags & I40E_FLAG_DCB_ENABLED)) {
5585 netdev_info(netdev, "DCB is not enabled for adapter\n");
5586 goto exit;
5587 }
5588
5589 /* Check if MFP enabled */
5590 if (pf->flags & I40E_FLAG_MFP_ENABLED) {
5591 netdev_info(netdev, "Configuring TC not supported in MFP mode\n");
5592 goto exit;
5593 }
5594
5595 /* Check whether tc count is within enabled limit */
5596 if (tc > i40e_pf_get_num_tc(pf)) {
5597 netdev_info(netdev, "TC count greater than enabled on link for adapter\n");
5598 goto exit;
5599 }
5600
5601 /* Generate TC map for number of tc requested */
5602 for (i = 0; i < tc; i++)
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08005603 enabled_tc |= BIT(i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005604
5605 /* Requesting same TC configuration as already enabled */
5606 if (enabled_tc == vsi->tc_config.enabled_tc)
5607 return 0;
5608
5609 /* Quiesce VSI queues */
5610 i40e_quiesce_vsi(vsi);
5611
5612 /* Configure VSI for enabled TCs */
5613 ret = i40e_vsi_config_tc(vsi, enabled_tc);
5614 if (ret) {
5615 netdev_info(netdev, "Failed configuring TC for VSI seid=%d\n",
5616 vsi->seid);
5617 goto exit;
5618 }
5619
5620 /* Unquiesce VSI */
5621 i40e_unquiesce_vsi(vsi);
5622
5623exit:
5624 return ret;
5625}
5626
Jiri Pirko2572ac52017-08-07 10:15:17 +02005627static int __i40e_setup_tc(struct net_device *netdev, enum tc_setup_type type,
Jiri Pirkode4784c2017-08-07 10:15:32 +02005628 void *type_data)
John Fastabende4c67342016-02-16 21:16:15 -08005629{
Jiri Pirkode4784c2017-08-07 10:15:32 +02005630 struct tc_mqprio_qopt *mqprio = type_data;
5631
Jiri Pirko2572ac52017-08-07 10:15:17 +02005632 if (type != TC_SETUP_MQPRIO)
Jiri Pirko38cf0422017-08-07 10:15:31 +02005633 return -EOPNOTSUPP;
Amritha Nambiar56f36ac2017-03-15 10:39:25 -07005634
Jiri Pirkode4784c2017-08-07 10:15:32 +02005635 mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
Amritha Nambiar56f36ac2017-03-15 10:39:25 -07005636
Jiri Pirkode4784c2017-08-07 10:15:32 +02005637 return i40e_setup_tc(netdev, mqprio->num_tc);
John Fastabende4c67342016-02-16 21:16:15 -08005638}
5639
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005640/**
5641 * i40e_open - Called when a network interface is made active
5642 * @netdev: network interface device structure
5643 *
5644 * The open entry point is called when a network interface is made
5645 * active by the system (IFF_UP). At this point all resources needed
5646 * for transmit and receive operations are allocated, the interrupt
5647 * handler is registered with the OS, the netdev watchdog subtask is
5648 * enabled, and the stack is notified that the interface is ready.
5649 *
5650 * Returns 0 on success, negative value on failure
5651 **/
Vasu Dev38e00432014-08-01 13:27:03 -07005652int i40e_open(struct net_device *netdev)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005653{
5654 struct i40e_netdev_priv *np = netdev_priv(netdev);
5655 struct i40e_vsi *vsi = np->vsi;
5656 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005657 int err;
5658
Shannon Nelson4eb3f762014-03-06 08:59:58 +00005659 /* disallow open during test or if eeprom is broken */
Jacob Keller0da36b92017-04-19 09:25:55 -04005660 if (test_bit(__I40E_TESTING, pf->state) ||
5661 test_bit(__I40E_BAD_EEPROM, pf->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005662 return -EBUSY;
5663
5664 netif_carrier_off(netdev);
5665
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005666 err = i40e_vsi_open(vsi);
5667 if (err)
5668 return err;
5669
Jesse Brandeburg059dab62014-04-01 09:07:20 +00005670 /* configure global TSO hardware offload settings */
5671 wr32(&pf->hw, I40E_GLLAN_TSOMSK_F, be32_to_cpu(TCP_FLAG_PSH |
5672 TCP_FLAG_FIN) >> 16);
5673 wr32(&pf->hw, I40E_GLLAN_TSOMSK_M, be32_to_cpu(TCP_FLAG_PSH |
5674 TCP_FLAG_FIN |
5675 TCP_FLAG_CWR) >> 16);
5676 wr32(&pf->hw, I40E_GLLAN_TSOMSK_L, be32_to_cpu(TCP_FLAG_CWR) >> 16);
5677
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07005678 udp_tunnel_get_rx_info(netdev);
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06005679
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005680 return 0;
5681}
5682
5683/**
5684 * i40e_vsi_open -
5685 * @vsi: the VSI to open
5686 *
5687 * Finish initialization of the VSI.
5688 *
5689 * Returns 0 on success, negative value on failure
Maciej Sosin373149f2017-04-05 07:50:55 -04005690 *
5691 * Note: expects to be called while under rtnl_lock()
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005692 **/
5693int i40e_vsi_open(struct i40e_vsi *vsi)
5694{
5695 struct i40e_pf *pf = vsi->back;
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00005696 char int_name[I40E_INT_NAME_STR_LEN];
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005697 int err;
5698
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005699 /* allocate descriptors */
5700 err = i40e_vsi_setup_tx_resources(vsi);
5701 if (err)
5702 goto err_setup_tx;
5703 err = i40e_vsi_setup_rx_resources(vsi);
5704 if (err)
5705 goto err_setup_rx;
5706
5707 err = i40e_vsi_configure(vsi);
5708 if (err)
5709 goto err_setup_rx;
5710
Shannon Nelsonc22e3c62014-03-14 07:32:25 +00005711 if (vsi->netdev) {
5712 snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
5713 dev_driver_string(&pf->pdev->dev), vsi->netdev->name);
5714 err = i40e_vsi_request_irq(vsi, int_name);
5715 if (err)
5716 goto err_setup_rx;
5717
5718 /* Notify the stack of the actual queue counts. */
5719 err = netif_set_real_num_tx_queues(vsi->netdev,
5720 vsi->num_queue_pairs);
5721 if (err)
5722 goto err_set_queues;
5723
5724 err = netif_set_real_num_rx_queues(vsi->netdev,
5725 vsi->num_queue_pairs);
5726 if (err)
5727 goto err_set_queues;
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00005728
5729 } else if (vsi->type == I40E_VSI_FDIR) {
Carolyn Wybornye240f672014-12-11 07:06:37 +00005730 snprintf(int_name, sizeof(int_name) - 1, "%s-%s:fdir",
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +00005731 dev_driver_string(&pf->pdev->dev),
5732 dev_name(&pf->pdev->dev));
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00005733 err = i40e_vsi_request_irq(vsi, int_name);
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +00005734
Shannon Nelsonc22e3c62014-03-14 07:32:25 +00005735 } else {
Jean Sacrence9ccb12014-05-01 14:31:18 +00005736 err = -EINVAL;
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005737 goto err_setup_rx;
5738 }
Anjali Singhai Jain25946dd2013-11-26 10:49:14 +00005739
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005740 err = i40e_up_complete(vsi);
5741 if (err)
5742 goto err_up_complete;
5743
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005744 return 0;
5745
5746err_up_complete:
5747 i40e_down(vsi);
Anjali Singhai Jain25946dd2013-11-26 10:49:14 +00005748err_set_queues:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005749 i40e_vsi_free_irq(vsi);
5750err_setup_rx:
5751 i40e_vsi_free_rx_resources(vsi);
5752err_setup_tx:
5753 i40e_vsi_free_tx_resources(vsi);
5754 if (vsi == pf->vsi[pf->lan_vsi])
Maciej Sosin373149f2017-04-05 07:50:55 -04005755 i40e_do_reset(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED), true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005756
5757 return err;
5758}
5759
5760/**
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00005761 * i40e_fdir_filter_exit - Cleans up the Flow Director accounting
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00005762 * @pf: Pointer to PF
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00005763 *
5764 * This function destroys the hlist where all the Flow Director
5765 * filters were saved.
5766 **/
5767static void i40e_fdir_filter_exit(struct i40e_pf *pf)
5768{
5769 struct i40e_fdir_filter *filter;
Jacob Keller0e588de2017-02-06 14:38:50 -08005770 struct i40e_flex_pit *pit_entry, *tmp;
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00005771 struct hlist_node *node2;
5772
5773 hlist_for_each_entry_safe(filter, node2,
5774 &pf->fdir_filter_list, fdir_node) {
5775 hlist_del(&filter->fdir_node);
5776 kfree(filter);
5777 }
Jacob Keller097dbf52017-02-06 14:38:46 -08005778
Jacob Keller0e588de2017-02-06 14:38:50 -08005779 list_for_each_entry_safe(pit_entry, tmp, &pf->l3_flex_pit_list, list) {
5780 list_del(&pit_entry->list);
5781 kfree(pit_entry);
5782 }
5783 INIT_LIST_HEAD(&pf->l3_flex_pit_list);
5784
5785 list_for_each_entry_safe(pit_entry, tmp, &pf->l4_flex_pit_list, list) {
5786 list_del(&pit_entry->list);
5787 kfree(pit_entry);
5788 }
5789 INIT_LIST_HEAD(&pf->l4_flex_pit_list);
5790
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00005791 pf->fdir_pf_active_filters = 0;
Jacob Keller097dbf52017-02-06 14:38:46 -08005792 pf->fd_tcp4_filter_cnt = 0;
5793 pf->fd_udp4_filter_cnt = 0;
Jacob Kellerf223c872017-02-06 14:38:51 -08005794 pf->fd_sctp4_filter_cnt = 0;
Jacob Keller097dbf52017-02-06 14:38:46 -08005795 pf->fd_ip4_filter_cnt = 0;
Jacob Keller3bcee1e2017-02-06 14:38:46 -08005796
5797 /* Reprogram the default input set for TCP/IPv4 */
5798 i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_TCP,
5799 I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
5800 I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
5801
5802 /* Reprogram the default input set for UDP/IPv4 */
5803 i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_UDP,
5804 I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
5805 I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
5806
5807 /* Reprogram the default input set for SCTP/IPv4 */
5808 i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_SCTP,
5809 I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
5810 I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
5811
5812 /* Reprogram the default input set for Other/IPv4 */
5813 i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_OTHER,
5814 I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00005815}
5816
5817/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005818 * i40e_close - Disables a network interface
5819 * @netdev: network interface device structure
5820 *
5821 * The close entry point is called when an interface is de-activated
5822 * by the OS. The hardware is still under the driver's control, but
5823 * this netdev interface is disabled.
5824 *
5825 * Returns 0, this is not allowed to fail
5826 **/
Vasu Dev38e00432014-08-01 13:27:03 -07005827int i40e_close(struct net_device *netdev)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005828{
5829 struct i40e_netdev_priv *np = netdev_priv(netdev);
5830 struct i40e_vsi *vsi = np->vsi;
5831
Shannon Nelson90ef8d42014-03-14 07:32:26 +00005832 i40e_vsi_close(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005833
5834 return 0;
5835}
5836
5837/**
5838 * i40e_do_reset - Start a PF or Core Reset sequence
5839 * @pf: board private structure
5840 * @reset_flags: which reset is requested
Maciej Sosin373149f2017-04-05 07:50:55 -04005841 * @lock_acquired: indicates whether or not the lock has been acquired
5842 * before this function was called.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005843 *
5844 * The essential difference in resets is that the PF Reset
5845 * doesn't clear the packet buffers, doesn't reset the PE
5846 * firmware, and doesn't bother the other PFs on the chip.
5847 **/
Maciej Sosin373149f2017-04-05 07:50:55 -04005848void i40e_do_reset(struct i40e_pf *pf, u32 reset_flags, bool lock_acquired)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005849{
5850 u32 val;
5851
5852 WARN_ON(in_interrupt());
5853
Mitch Williams263fc482014-04-23 04:50:11 +00005854
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005855 /* do the biggest reset indicated */
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005856 if (reset_flags & BIT_ULL(__I40E_GLOBAL_RESET_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005857
5858 /* Request a Global Reset
5859 *
5860 * This will start the chip's countdown to the actual full
5861 * chip reset event, and a warning interrupt to be sent
5862 * to all PFs, including the requestor. Our handler
5863 * for the warning interrupt will deal with the shutdown
5864 * and recovery of the switch setup.
5865 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005866 dev_dbg(&pf->pdev->dev, "GlobalR requested\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005867 val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
5868 val |= I40E_GLGEN_RTRIG_GLOBR_MASK;
5869 wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
5870
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005871 } else if (reset_flags & BIT_ULL(__I40E_CORE_RESET_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005872
5873 /* Request a Core Reset
5874 *
5875 * Same as Global Reset, except does *not* include the MAC/PHY
5876 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005877 dev_dbg(&pf->pdev->dev, "CoreR requested\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005878 val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
5879 val |= I40E_GLGEN_RTRIG_CORER_MASK;
5880 wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
5881 i40e_flush(&pf->hw);
5882
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005883 } else if (reset_flags & BIT_ULL(__I40E_PF_RESET_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005884
5885 /* Request a PF Reset
5886 *
5887 * Resets only the PF-specific registers
5888 *
5889 * This goes directly to the tear-down and rebuild of
5890 * the switch, since we need to do all the recovery as
5891 * for the Core Reset.
5892 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005893 dev_dbg(&pf->pdev->dev, "PFR requested\n");
Maciej Sosin373149f2017-04-05 07:50:55 -04005894 i40e_handle_reset_warning(pf, lock_acquired);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005895
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005896 } else if (reset_flags & BIT_ULL(__I40E_REINIT_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005897 int v;
5898
5899 /* Find the VSI(s) that requested a re-init */
5900 dev_info(&pf->pdev->dev,
5901 "VSI reinit requested\n");
Mitch Williams505682c2014-05-20 08:01:37 +00005902 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005903 struct i40e_vsi *vsi = pf->vsi[v];
Jesse Brandeburg6995b362015-08-28 17:55:54 -04005904
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005905 if (vsi != NULL &&
Jacob Kellerd19cb642017-04-21 13:38:05 -07005906 test_and_clear_bit(__I40E_VSI_REINIT_REQUESTED,
Jacob Keller0da36b92017-04-19 09:25:55 -04005907 vsi->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005908 i40e_vsi_reinit_locked(pf->vsi[v]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005909 }
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005910 } else if (reset_flags & BIT_ULL(__I40E_DOWN_REQUESTED)) {
Neerav Parikhb5d06f02014-06-03 23:50:17 +00005911 int v;
5912
5913 /* Find the VSI(s) that needs to be brought down */
5914 dev_info(&pf->pdev->dev, "VSI down requested\n");
5915 for (v = 0; v < pf->num_alloc_vsi; v++) {
5916 struct i40e_vsi *vsi = pf->vsi[v];
Jesse Brandeburg6995b362015-08-28 17:55:54 -04005917
Neerav Parikhb5d06f02014-06-03 23:50:17 +00005918 if (vsi != NULL &&
Jacob Kellerd19cb642017-04-21 13:38:05 -07005919 test_and_clear_bit(__I40E_VSI_DOWN_REQUESTED,
Jacob Keller0da36b92017-04-19 09:25:55 -04005920 vsi->state)) {
5921 set_bit(__I40E_VSI_DOWN, vsi->state);
Neerav Parikhb5d06f02014-06-03 23:50:17 +00005922 i40e_down(vsi);
Neerav Parikhb5d06f02014-06-03 23:50:17 +00005923 }
5924 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005925 } else {
5926 dev_info(&pf->pdev->dev,
5927 "bad reset request 0x%08x\n", reset_flags);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005928 }
5929}
5930
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005931#ifdef CONFIG_I40E_DCB
5932/**
5933 * i40e_dcb_need_reconfig - Check if DCB needs reconfig
5934 * @pf: board private structure
5935 * @old_cfg: current DCB config
5936 * @new_cfg: new DCB config
5937 **/
5938bool i40e_dcb_need_reconfig(struct i40e_pf *pf,
5939 struct i40e_dcbx_config *old_cfg,
5940 struct i40e_dcbx_config *new_cfg)
5941{
5942 bool need_reconfig = false;
5943
5944 /* Check if ETS configuration has changed */
5945 if (memcmp(&new_cfg->etscfg,
5946 &old_cfg->etscfg,
5947 sizeof(new_cfg->etscfg))) {
5948 /* If Priority Table has changed reconfig is needed */
5949 if (memcmp(&new_cfg->etscfg.prioritytable,
5950 &old_cfg->etscfg.prioritytable,
5951 sizeof(new_cfg->etscfg.prioritytable))) {
5952 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005953 dev_dbg(&pf->pdev->dev, "ETS UP2TC changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005954 }
5955
5956 if (memcmp(&new_cfg->etscfg.tcbwtable,
5957 &old_cfg->etscfg.tcbwtable,
5958 sizeof(new_cfg->etscfg.tcbwtable)))
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005959 dev_dbg(&pf->pdev->dev, "ETS TC BW Table changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005960
5961 if (memcmp(&new_cfg->etscfg.tsatable,
5962 &old_cfg->etscfg.tsatable,
5963 sizeof(new_cfg->etscfg.tsatable)))
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005964 dev_dbg(&pf->pdev->dev, "ETS TSA Table changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005965 }
5966
5967 /* Check if PFC configuration has changed */
5968 if (memcmp(&new_cfg->pfc,
5969 &old_cfg->pfc,
5970 sizeof(new_cfg->pfc))) {
5971 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005972 dev_dbg(&pf->pdev->dev, "PFC config change detected.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005973 }
5974
5975 /* Check if APP Table has changed */
5976 if (memcmp(&new_cfg->app,
5977 &old_cfg->app,
Dave Jones3d9667a2014-01-27 23:11:09 -05005978 sizeof(new_cfg->app))) {
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005979 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005980 dev_dbg(&pf->pdev->dev, "APP Table change detected.\n");
Dave Jones3d9667a2014-01-27 23:11:09 -05005981 }
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005982
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04005983 dev_dbg(&pf->pdev->dev, "dcb need_reconfig=%d\n", need_reconfig);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005984 return need_reconfig;
5985}
5986
5987/**
5988 * i40e_handle_lldp_event - Handle LLDP Change MIB event
5989 * @pf: board private structure
5990 * @e: event info posted on ARQ
5991 **/
5992static int i40e_handle_lldp_event(struct i40e_pf *pf,
5993 struct i40e_arq_event_info *e)
5994{
5995 struct i40e_aqc_lldp_get_mib *mib =
5996 (struct i40e_aqc_lldp_get_mib *)&e->desc.params.raw;
5997 struct i40e_hw *hw = &pf->hw;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005998 struct i40e_dcbx_config tmp_dcbx_cfg;
5999 bool need_reconfig = false;
6000 int ret = 0;
6001 u8 type;
6002
Neerav Parikh4d9b6042014-05-22 06:31:51 +00006003 /* Not DCB capable or capability disabled */
David Ertmanea6acb72016-09-20 07:10:50 -07006004 if (!(pf->flags & I40E_FLAG_DCB_CAPABLE))
Neerav Parikh4d9b6042014-05-22 06:31:51 +00006005 return ret;
6006
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006007 /* Ignore if event is not for Nearest Bridge */
6008 type = ((mib->type >> I40E_AQ_LLDP_BRIDGE_TYPE_SHIFT)
6009 & I40E_AQ_LLDP_BRIDGE_TYPE_MASK);
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04006010 dev_dbg(&pf->pdev->dev, "LLDP event mib bridge type 0x%x\n", type);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006011 if (type != I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE)
6012 return ret;
6013
6014 /* Check MIB Type and return if event for Remote MIB update */
6015 type = mib->type & I40E_AQ_LLDP_MIB_TYPE_MASK;
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00006016 dev_dbg(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04006017 "LLDP event mib type %s\n", type ? "remote" : "local");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006018 if (type == I40E_AQ_LLDP_MIB_REMOTE) {
6019 /* Update the remote cached instance and return */
6020 ret = i40e_aq_get_dcb_config(hw, I40E_AQ_LLDP_MIB_REMOTE,
6021 I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE,
6022 &hw->remote_dcbx_config);
6023 goto exit;
6024 }
6025
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00006026 /* Store the old configuration */
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07006027 tmp_dcbx_cfg = hw->local_dcbx_config;
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00006028
Neerav Parikh750fcbc2015-02-24 06:58:47 +00006029 /* Reset the old DCBx configuration data */
6030 memset(&hw->local_dcbx_config, 0, sizeof(hw->local_dcbx_config));
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00006031 /* Get updated DCBX data from firmware */
6032 ret = i40e_get_dcb_config(&pf->hw);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006033 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006034 dev_info(&pf->pdev->dev,
6035 "Failed querying DCB configuration data from firmware, err %s aq_err %s\n",
6036 i40e_stat_str(&pf->hw, ret),
6037 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006038 goto exit;
6039 }
6040
6041 /* No change detected in DCBX configs */
Neerav Parikh750fcbc2015-02-24 06:58:47 +00006042 if (!memcmp(&tmp_dcbx_cfg, &hw->local_dcbx_config,
6043 sizeof(tmp_dcbx_cfg))) {
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006044 dev_dbg(&pf->pdev->dev, "No change detected in DCBX configuration.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006045 goto exit;
6046 }
6047
Neerav Parikh750fcbc2015-02-24 06:58:47 +00006048 need_reconfig = i40e_dcb_need_reconfig(pf, &tmp_dcbx_cfg,
6049 &hw->local_dcbx_config);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006050
Neerav Parikh750fcbc2015-02-24 06:58:47 +00006051 i40e_dcbnl_flush_apps(pf, &tmp_dcbx_cfg, &hw->local_dcbx_config);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006052
6053 if (!need_reconfig)
6054 goto exit;
6055
Neerav Parikh4d9b6042014-05-22 06:31:51 +00006056 /* Enable DCB tagging only when more than one TC */
Neerav Parikh750fcbc2015-02-24 06:58:47 +00006057 if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
Neerav Parikh4d9b6042014-05-22 06:31:51 +00006058 pf->flags |= I40E_FLAG_DCB_ENABLED;
6059 else
6060 pf->flags &= ~I40E_FLAG_DCB_ENABLED;
6061
Jacob Keller0da36b92017-04-19 09:25:55 -04006062 set_bit(__I40E_PORT_SUSPENDED, pf->state);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006063 /* Reconfiguration needed quiesce all VSIs */
6064 i40e_pf_quiesce_all_vsi(pf);
6065
6066 /* Changes in configuration update VEB/VSI */
6067 i40e_dcb_reconfigure(pf);
6068
Neerav Parikh2fd75f32014-11-12 00:18:20 +00006069 ret = i40e_resume_port_tx(pf);
6070
Jacob Keller0da36b92017-04-19 09:25:55 -04006071 clear_bit(__I40E_PORT_SUSPENDED, pf->state);
Neerav Parikh2fd75f32014-11-12 00:18:20 +00006072 /* In case of error no point in resuming VSIs */
Neerav Parikh69129dc2014-11-12 00:18:46 +00006073 if (ret)
6074 goto exit;
6075
Neerav Parikh3fe06f42016-02-17 16:12:15 -08006076 /* Wait for the PF's queues to be disabled */
6077 ret = i40e_pf_wait_queues_disabled(pf);
Parikh, Neerav11e47702015-02-21 06:43:55 +00006078 if (ret) {
6079 /* Schedule PF reset to recover */
Jacob Keller0da36b92017-04-19 09:25:55 -04006080 set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
Parikh, Neerav11e47702015-02-21 06:43:55 +00006081 i40e_service_event_schedule(pf);
6082 } else {
Neerav Parikh2fd75f32014-11-12 00:18:20 +00006083 i40e_pf_unquiesce_all_vsi(pf);
Mitch Williams0ef2d5a2017-01-24 10:24:00 -08006084 pf->flags |= (I40E_FLAG_SERVICE_CLIENT_REQUESTED |
6085 I40E_FLAG_CLIENT_L2_CHANGE);
Parikh, Neerav11e47702015-02-21 06:43:55 +00006086 }
6087
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006088exit:
6089 return ret;
6090}
6091#endif /* CONFIG_I40E_DCB */
6092
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006093/**
Anjali Singhai Jain233261862013-11-26 10:49:22 +00006094 * i40e_do_reset_safe - Protected reset path for userland calls.
6095 * @pf: board private structure
6096 * @reset_flags: which reset is requested
6097 *
6098 **/
6099void i40e_do_reset_safe(struct i40e_pf *pf, u32 reset_flags)
6100{
6101 rtnl_lock();
Maciej Sosin373149f2017-04-05 07:50:55 -04006102 i40e_do_reset(pf, reset_flags, true);
Anjali Singhai Jain233261862013-11-26 10:49:22 +00006103 rtnl_unlock();
6104}
6105
6106/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006107 * i40e_handle_lan_overflow_event - Handler for LAN queue overflow event
6108 * @pf: board private structure
6109 * @e: event info posted on ARQ
6110 *
6111 * Handler for LAN Queue Overflow Event generated by the firmware for PF
6112 * and VF queues
6113 **/
6114static void i40e_handle_lan_overflow_event(struct i40e_pf *pf,
6115 struct i40e_arq_event_info *e)
6116{
6117 struct i40e_aqc_lan_overflow *data =
6118 (struct i40e_aqc_lan_overflow *)&e->desc.params.raw;
6119 u32 queue = le32_to_cpu(data->prtdcb_rupto);
6120 u32 qtx_ctl = le32_to_cpu(data->otx_ctl);
6121 struct i40e_hw *hw = &pf->hw;
6122 struct i40e_vf *vf;
6123 u16 vf_id;
6124
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006125 dev_dbg(&pf->pdev->dev, "overflow Rx Queue Number = %d QTX_CTL=0x%08x\n",
6126 queue, qtx_ctl);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006127
6128 /* Queue belongs to VF, find the VF and issue VF reset */
6129 if (((qtx_ctl & I40E_QTX_CTL_PFVF_Q_MASK)
6130 >> I40E_QTX_CTL_PFVF_Q_SHIFT) == I40E_QTX_CTL_VF_QUEUE) {
6131 vf_id = (u16)((qtx_ctl & I40E_QTX_CTL_VFVM_INDX_MASK)
6132 >> I40E_QTX_CTL_VFVM_INDX_SHIFT);
6133 vf_id -= hw->func_caps.vf_base_id;
6134 vf = &pf->vf[vf_id];
6135 i40e_vc_notify_vf_reset(vf);
6136 /* Allow VF to process pending reset notification */
6137 msleep(20);
6138 i40e_reset_vf(vf, false);
6139 }
6140}
6141
6142/**
Anjali Singhai Jain12957382014-06-04 04:22:47 +00006143 * i40e_get_cur_guaranteed_fd_count - Get the consumed guaranteed FD filters
6144 * @pf: board private structure
6145 **/
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006146u32 i40e_get_cur_guaranteed_fd_count(struct i40e_pf *pf)
Anjali Singhai Jain12957382014-06-04 04:22:47 +00006147{
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006148 u32 val, fcnt_prog;
Anjali Singhai Jain12957382014-06-04 04:22:47 +00006149
6150 val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
6151 fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK);
6152 return fcnt_prog;
6153}
6154
6155/**
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006156 * i40e_get_current_fd_count - Get total FD filters programmed for this PF
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006157 * @pf: board private structure
6158 **/
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006159u32 i40e_get_current_fd_count(struct i40e_pf *pf)
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006160{
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006161 u32 val, fcnt_prog;
6162
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006163 val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
6164 fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK) +
6165 ((val & I40E_PFQF_FDSTAT_BEST_CNT_MASK) >>
6166 I40E_PFQF_FDSTAT_BEST_CNT_SHIFT);
6167 return fcnt_prog;
6168}
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006169
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006170/**
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006171 * i40e_get_global_fd_count - Get total FD filters programmed on device
6172 * @pf: board private structure
6173 **/
6174u32 i40e_get_global_fd_count(struct i40e_pf *pf)
6175{
6176 u32 val, fcnt_prog;
6177
6178 val = rd32(&pf->hw, I40E_GLQF_FDCNT_0);
6179 fcnt_prog = (val & I40E_GLQF_FDCNT_0_GUARANT_CNT_MASK) +
6180 ((val & I40E_GLQF_FDCNT_0_BESTCNT_MASK) >>
6181 I40E_GLQF_FDCNT_0_BESTCNT_SHIFT);
6182 return fcnt_prog;
6183}
6184
6185/**
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006186 * i40e_fdir_check_and_reenable - Function to reenabe FD ATR or SB if disabled
6187 * @pf: board private structure
6188 **/
6189void i40e_fdir_check_and_reenable(struct i40e_pf *pf)
6190{
Carolyn Wyborny3487b6c2015-08-27 11:42:38 -04006191 struct i40e_fdir_filter *filter;
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006192 u32 fcnt_prog, fcnt_avail;
Carolyn Wyborny3487b6c2015-08-27 11:42:38 -04006193 struct hlist_node *node;
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006194
Jacob Keller0da36b92017-04-19 09:25:55 -04006195 if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006196 return;
6197
Jacob Keller47994c12017-04-19 09:25:57 -04006198 /* Check if we have enough room to re-enable FDir SB capability. */
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006199 fcnt_prog = i40e_get_global_fd_count(pf);
Anjali Singhai Jain12957382014-06-04 04:22:47 +00006200 fcnt_avail = pf->fdir_pf_filter_count;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006201 if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM)) ||
6202 (pf->fd_add_err == 0) ||
6203 (i40e_get_current_atr_cnt(pf) < pf->fd_atr_cnt)) {
Jacob Keller47994c12017-04-19 09:25:57 -04006204 if (pf->flags & I40E_FLAG_FD_SB_AUTO_DISABLED) {
6205 pf->flags &= ~I40E_FLAG_FD_SB_AUTO_DISABLED;
6206 if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
6207 (I40E_DEBUG_FD & pf->hw.debug_mask))
Anjali Singhai Jain2e4875e2015-04-16 20:06:06 -04006208 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 +00006209 }
6210 }
Jacob Kellera3417d22016-09-06 18:05:10 -07006211
Jacob Keller47994c12017-04-19 09:25:57 -04006212 /* We should wait for even more space before re-enabling ATR.
6213 * Additionally, we cannot enable ATR as long as we still have TCP SB
6214 * rules active.
Jacob Kellera3417d22016-09-06 18:05:10 -07006215 */
Jacob Keller47994c12017-04-19 09:25:57 -04006216 if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) &&
6217 (pf->fd_tcp4_filter_cnt == 0)) {
6218 if (pf->flags & I40E_FLAG_FD_ATR_AUTO_DISABLED) {
6219 pf->flags &= ~I40E_FLAG_FD_ATR_AUTO_DISABLED;
6220 if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
6221 (I40E_DEBUG_FD & pf->hw.debug_mask))
Jacob Kellera3417d22016-09-06 18:05:10 -07006222 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 +00006223 }
6224 }
Carolyn Wyborny3487b6c2015-08-27 11:42:38 -04006225
6226 /* if hw had a problem adding a filter, delete it */
6227 if (pf->fd_inv > 0) {
6228 hlist_for_each_entry_safe(filter, node,
6229 &pf->fdir_filter_list, fdir_node) {
6230 if (filter->fd_id == pf->fd_inv) {
6231 hlist_del(&filter->fdir_node);
6232 kfree(filter);
6233 pf->fdir_pf_active_filters--;
6234 }
6235 }
6236 }
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006237}
6238
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006239#define I40E_MIN_FD_FLUSH_INTERVAL 10
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006240#define I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE 30
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006241/**
6242 * i40e_fdir_flush_and_replay - Function to flush all FD filters and replay SB
6243 * @pf: board private structure
6244 **/
6245static void i40e_fdir_flush_and_replay(struct i40e_pf *pf)
6246{
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006247 unsigned long min_flush_time;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006248 int flush_wait_retry = 50;
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006249 bool disable_atr = false;
6250 int fd_room;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006251 int reg;
6252
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04006253 if (!time_after(jiffies, pf->fd_flush_timestamp +
6254 (I40E_MIN_FD_FLUSH_INTERVAL * HZ)))
6255 return;
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006256
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04006257 /* If the flush is happening too quick and we have mostly SB rules we
6258 * should not re-enable ATR for some time.
6259 */
6260 min_flush_time = pf->fd_flush_timestamp +
6261 (I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE * HZ);
6262 fd_room = pf->fdir_pf_filter_count - pf->fdir_pf_active_filters;
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006263
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04006264 if (!(time_after(jiffies, min_flush_time)) &&
6265 (fd_room < I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) {
6266 if (I40E_DEBUG_FD & pf->hw.debug_mask)
6267 dev_info(&pf->pdev->dev, "ATR disabled, not enough FD filter space.\n");
6268 disable_atr = true;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006269 }
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04006270
6271 pf->fd_flush_timestamp = jiffies;
Jacob Keller47994c12017-04-19 09:25:57 -04006272 pf->flags |= I40E_FLAG_FD_ATR_AUTO_DISABLED;
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04006273 /* flush all filters */
6274 wr32(&pf->hw, I40E_PFQF_CTL_1,
6275 I40E_PFQF_CTL_1_CLEARFDTABLE_MASK);
6276 i40e_flush(&pf->hw);
6277 pf->fd_flush_cnt++;
6278 pf->fd_add_err = 0;
6279 do {
6280 /* Check FD flush status every 5-6msec */
6281 usleep_range(5000, 6000);
6282 reg = rd32(&pf->hw, I40E_PFQF_CTL_1);
6283 if (!(reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK))
6284 break;
6285 } while (flush_wait_retry--);
6286 if (reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK) {
6287 dev_warn(&pf->pdev->dev, "FD table did not flush, needs more time\n");
6288 } else {
6289 /* replay sideband filters */
6290 i40e_fdir_filter_restore(pf->vsi[pf->lan_vsi]);
Jacob Keller097dbf52017-02-06 14:38:46 -08006291 if (!disable_atr && !pf->fd_tcp4_filter_cnt)
Jacob Keller47994c12017-04-19 09:25:57 -04006292 pf->flags &= ~I40E_FLAG_FD_ATR_AUTO_DISABLED;
Jacob Keller0da36b92017-04-19 09:25:55 -04006293 clear_bit(__I40E_FD_FLUSH_REQUESTED, pf->state);
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04006294 if (I40E_DEBUG_FD & pf->hw.debug_mask)
6295 dev_info(&pf->pdev->dev, "FD Filter table flushed and FD-SB replayed.\n");
6296 }
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006297}
6298
6299/**
6300 * i40e_get_current_atr_count - Get the count of total FD ATR filters programmed
6301 * @pf: board private structure
6302 **/
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006303u32 i40e_get_current_atr_cnt(struct i40e_pf *pf)
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006304{
6305 return i40e_get_current_fd_count(pf) - pf->fdir_pf_active_filters;
6306}
6307
6308/* We can see up to 256 filter programming desc in transit if the filters are
6309 * being applied really fast; before we see the first
6310 * filter miss error on Rx queue 0. Accumulating enough error messages before
6311 * reacting will make sure we don't cause flush too often.
6312 */
6313#define I40E_MAX_FD_PROGRAM_ERROR 256
6314
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006315/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006316 * i40e_fdir_reinit_subtask - Worker thread to reinit FDIR filter table
6317 * @pf: board private structure
6318 **/
6319static void i40e_fdir_reinit_subtask(struct i40e_pf *pf)
6320{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006321
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006322 /* if interface is down do nothing */
Mauro S. M. Rodrigues9e6c9c02017-05-12 23:26:56 -03006323 if (test_bit(__I40E_DOWN, pf->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006324 return;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006325
Jacob Keller0da36b92017-04-19 09:25:55 -04006326 if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006327 i40e_fdir_flush_and_replay(pf);
6328
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006329 i40e_fdir_check_and_reenable(pf);
6330
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006331}
6332
6333/**
6334 * i40e_vsi_link_event - notify VSI of a link event
6335 * @vsi: vsi to be notified
6336 * @link_up: link up or down
6337 **/
6338static void i40e_vsi_link_event(struct i40e_vsi *vsi, bool link_up)
6339{
Jacob Keller0da36b92017-04-19 09:25:55 -04006340 if (!vsi || test_bit(__I40E_VSI_DOWN, vsi->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006341 return;
6342
6343 switch (vsi->type) {
6344 case I40E_VSI_MAIN:
6345 if (!vsi->netdev || !vsi->netdev_registered)
6346 break;
6347
6348 if (link_up) {
6349 netif_carrier_on(vsi->netdev);
6350 netif_tx_wake_all_queues(vsi->netdev);
6351 } else {
6352 netif_carrier_off(vsi->netdev);
6353 netif_tx_stop_all_queues(vsi->netdev);
6354 }
6355 break;
6356
6357 case I40E_VSI_SRIOV:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006358 case I40E_VSI_VMDQ2:
6359 case I40E_VSI_CTRL:
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06006360 case I40E_VSI_IWARP:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006361 case I40E_VSI_MIRROR:
6362 default:
6363 /* there is no notification for other VSIs */
6364 break;
6365 }
6366}
6367
6368/**
6369 * i40e_veb_link_event - notify elements on the veb of a link event
6370 * @veb: veb to be notified
6371 * @link_up: link up or down
6372 **/
6373static void i40e_veb_link_event(struct i40e_veb *veb, bool link_up)
6374{
6375 struct i40e_pf *pf;
6376 int i;
6377
6378 if (!veb || !veb->pf)
6379 return;
6380 pf = veb->pf;
6381
6382 /* depth first... */
6383 for (i = 0; i < I40E_MAX_VEB; i++)
6384 if (pf->veb[i] && (pf->veb[i]->uplink_seid == veb->seid))
6385 i40e_veb_link_event(pf->veb[i], link_up);
6386
6387 /* ... now the local VSIs */
Mitch Williams505682c2014-05-20 08:01:37 +00006388 for (i = 0; i < pf->num_alloc_vsi; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006389 if (pf->vsi[i] && (pf->vsi[i]->uplink_seid == veb->seid))
6390 i40e_vsi_link_event(pf->vsi[i], link_up);
6391}
6392
6393/**
6394 * i40e_link_event - Update netif_carrier status
6395 * @pf: board private structure
6396 **/
6397static void i40e_link_event(struct i40e_pf *pf)
6398{
Mitch Williams320684c2014-10-17 03:14:43 +00006399 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
Catherine Sullivanfef59dd2014-12-11 07:06:33 +00006400 u8 new_link_speed, old_link_speed;
Jesse Brandeburga72a5abc2015-08-26 15:14:19 -04006401 i40e_status status;
6402 bool new_link, old_link;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006403
Catherine Sullivan1f9610e2015-10-21 19:47:09 -04006404 /* save off old link status information */
6405 pf->hw.phy.link_info_old = pf->hw.phy.link_info;
6406
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00006407 /* set this to force the get_link_status call to refresh state */
6408 pf->hw.phy.get_link_info = true;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006409
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00006410 old_link = (pf->hw.phy.link_info_old.link_info & I40E_AQ_LINK_UP);
Jesse Brandeburga72a5abc2015-08-26 15:14:19 -04006411
6412 status = i40e_get_link_status(&pf->hw, &new_link);
Harshitha Ramamurthyae136702016-12-12 15:44:16 -08006413
6414 /* On success, disable temp link polling */
6415 if (status == I40E_SUCCESS) {
6416 if (pf->flags & I40E_FLAG_TEMP_LINK_POLLING)
6417 pf->flags &= ~I40E_FLAG_TEMP_LINK_POLLING;
6418 } else {
6419 /* Enable link polling temporarily until i40e_get_link_status
6420 * returns I40E_SUCCESS
6421 */
6422 pf->flags |= I40E_FLAG_TEMP_LINK_POLLING;
Jesse Brandeburga72a5abc2015-08-26 15:14:19 -04006423 dev_dbg(&pf->pdev->dev, "couldn't get link state, status: %d\n",
6424 status);
6425 return;
6426 }
6427
Catherine Sullivanfef59dd2014-12-11 07:06:33 +00006428 old_link_speed = pf->hw.phy.link_info_old.link_speed;
6429 new_link_speed = pf->hw.phy.link_info.link_speed;
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00006430
6431 if (new_link == old_link &&
Catherine Sullivanfef59dd2014-12-11 07:06:33 +00006432 new_link_speed == old_link_speed &&
Jacob Keller0da36b92017-04-19 09:25:55 -04006433 (test_bit(__I40E_VSI_DOWN, vsi->state) ||
Mitch Williams320684c2014-10-17 03:14:43 +00006434 new_link == netif_carrier_ok(vsi->netdev)))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006435 return;
Mitch Williams320684c2014-10-17 03:14:43 +00006436
Sudheer Mogilappagari9a034492017-07-14 09:10:16 -04006437 i40e_print_link_message(vsi, new_link);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006438
6439 /* Notify the base of the switch tree connected to
6440 * the link. Floating VEBs are not notified.
6441 */
6442 if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
6443 i40e_veb_link_event(pf->veb[pf->lan_veb], new_link);
6444 else
Mitch Williams320684c2014-10-17 03:14:43 +00006445 i40e_vsi_link_event(vsi, new_link);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006446
6447 if (pf->vf)
6448 i40e_vc_notify_link_state(pf);
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00006449
6450 if (pf->flags & I40E_FLAG_PTP)
6451 i40e_ptp_set_increment(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006452}
6453
6454/**
Shannon Nelson21536712014-10-25 10:35:25 +00006455 * i40e_watchdog_subtask - periodic checks not using event driven response
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006456 * @pf: board private structure
6457 **/
6458static void i40e_watchdog_subtask(struct i40e_pf *pf)
6459{
6460 int i;
6461
6462 /* if interface is down do nothing */
Mauro S. M. Rodrigues9e6c9c02017-05-12 23:26:56 -03006463 if (test_bit(__I40E_DOWN, pf->state) ||
Jacob Keller0da36b92017-04-19 09:25:55 -04006464 test_bit(__I40E_CONFIG_BUSY, pf->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006465 return;
6466
Shannon Nelson21536712014-10-25 10:35:25 +00006467 /* make sure we don't do these things too often */
6468 if (time_before(jiffies, (pf->service_timer_previous +
6469 pf->service_timer_period)))
6470 return;
6471 pf->service_timer_previous = jiffies;
6472
Harshitha Ramamurthyae136702016-12-12 15:44:16 -08006473 if ((pf->flags & I40E_FLAG_LINK_POLLING_ENABLED) ||
6474 (pf->flags & I40E_FLAG_TEMP_LINK_POLLING))
Shannon Nelson9ac77262015-08-27 11:42:40 -04006475 i40e_link_event(pf);
Shannon Nelson21536712014-10-25 10:35:25 +00006476
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006477 /* Update the stats for active netdevs so the network stack
6478 * can look at updated numbers whenever it cares to
6479 */
Mitch Williams505682c2014-05-20 08:01:37 +00006480 for (i = 0; i < pf->num_alloc_vsi; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006481 if (pf->vsi[i] && pf->vsi[i]->netdev)
6482 i40e_update_stats(pf->vsi[i]);
6483
Anjali Singhai Jaind1a8d272015-07-23 16:54:40 -04006484 if (pf->flags & I40E_FLAG_VEB_STATS_ENABLED) {
6485 /* Update the stats for the active switching components */
6486 for (i = 0; i < I40E_MAX_VEB; i++)
6487 if (pf->veb[i])
6488 i40e_update_veb_stats(pf->veb[i]);
6489 }
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00006490
Jacob Keller61189552017-05-03 10:29:01 -07006491 i40e_ptp_rx_hang(pf);
Jacob Keller0bc07062017-05-03 10:29:02 -07006492 i40e_ptp_tx_hang(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006493}
6494
6495/**
6496 * i40e_reset_subtask - Set up for resetting the device and driver
6497 * @pf: board private structure
6498 **/
6499static void i40e_reset_subtask(struct i40e_pf *pf)
6500{
6501 u32 reset_flags = 0;
6502
Jacob Keller0da36b92017-04-19 09:25:55 -04006503 if (test_bit(__I40E_REINIT_REQUESTED, pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006504 reset_flags |= BIT(__I40E_REINIT_REQUESTED);
Jacob Keller0da36b92017-04-19 09:25:55 -04006505 clear_bit(__I40E_REINIT_REQUESTED, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006506 }
Jacob Keller0da36b92017-04-19 09:25:55 -04006507 if (test_bit(__I40E_PF_RESET_REQUESTED, pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006508 reset_flags |= BIT(__I40E_PF_RESET_REQUESTED);
Jacob Keller0da36b92017-04-19 09:25:55 -04006509 clear_bit(__I40E_PF_RESET_REQUESTED, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006510 }
Jacob Keller0da36b92017-04-19 09:25:55 -04006511 if (test_bit(__I40E_CORE_RESET_REQUESTED, pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006512 reset_flags |= BIT(__I40E_CORE_RESET_REQUESTED);
Jacob Keller0da36b92017-04-19 09:25:55 -04006513 clear_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006514 }
Jacob Keller0da36b92017-04-19 09:25:55 -04006515 if (test_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006516 reset_flags |= BIT(__I40E_GLOBAL_RESET_REQUESTED);
Jacob Keller0da36b92017-04-19 09:25:55 -04006517 clear_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006518 }
Mauro S. M. Rodrigues9e6c9c02017-05-12 23:26:56 -03006519 if (test_bit(__I40E_DOWN_REQUESTED, pf->state)) {
6520 reset_flags |= BIT(__I40E_DOWN_REQUESTED);
6521 clear_bit(__I40E_DOWN_REQUESTED, pf->state);
Neerav Parikhb5d06f02014-06-03 23:50:17 +00006522 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006523
6524 /* If there's a recovery already waiting, it takes
6525 * precedence before starting a new reset sequence.
6526 */
Jacob Keller0da36b92017-04-19 09:25:55 -04006527 if (test_bit(__I40E_RESET_INTR_RECEIVED, pf->state)) {
Maciej Sosin373149f2017-04-05 07:50:55 -04006528 i40e_prep_for_reset(pf, false);
6529 i40e_reset(pf);
6530 i40e_rebuild(pf, false, false);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006531 }
6532
6533 /* If we're already down or resetting, just bail */
6534 if (reset_flags &&
Mauro S. M. Rodrigues9e6c9c02017-05-12 23:26:56 -03006535 !test_bit(__I40E_DOWN, pf->state) &&
Jacob Keller0da36b92017-04-19 09:25:55 -04006536 !test_bit(__I40E_CONFIG_BUSY, pf->state)) {
Jacob Kellerdfc4ff62017-06-07 05:43:13 -04006537 i40e_do_reset(pf, reset_flags, false);
Maciej Sosin373149f2017-04-05 07:50:55 -04006538 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006539}
6540
6541/**
6542 * i40e_handle_link_event - Handle link event
6543 * @pf: board private structure
6544 * @e: event info posted on ARQ
6545 **/
6546static void i40e_handle_link_event(struct i40e_pf *pf,
6547 struct i40e_arq_event_info *e)
6548{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006549 struct i40e_aqc_get_link_status *status =
6550 (struct i40e_aqc_get_link_status *)&e->desc.params.raw;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006551
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00006552 /* Do a new status request to re-enable LSE reporting
6553 * and load new status information into the hw struct
6554 * This completely ignores any state information
6555 * in the ARQ event info, instead choosing to always
6556 * issue the AQ update link status command.
6557 */
6558 i40e_link_event(pf);
6559
Carolyn Wyborny7b592f62014-07-10 07:58:19 +00006560 /* check for unqualified module, if link is down */
6561 if ((status->link_info & I40E_AQ_MEDIA_AVAILABLE) &&
6562 (!(status->an_info & I40E_AQ_QUALIFIED_MODULE)) &&
6563 (!(status->link_info & I40E_AQ_LINK_UP)))
6564 dev_err(&pf->pdev->dev,
6565 "The driver failed to link because an unqualified module was detected.\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006566}
6567
6568/**
6569 * i40e_clean_adminq_subtask - Clean the AdminQ rings
6570 * @pf: board private structure
6571 **/
6572static void i40e_clean_adminq_subtask(struct i40e_pf *pf)
6573{
6574 struct i40e_arq_event_info event;
6575 struct i40e_hw *hw = &pf->hw;
6576 u16 pending, i = 0;
6577 i40e_status ret;
6578 u16 opcode;
Shannon Nelson86df2422014-05-20 08:01:35 +00006579 u32 oldval;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006580 u32 val;
6581
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00006582 /* Do not run clean AQ when PF reset fails */
Jacob Keller0da36b92017-04-19 09:25:55 -04006583 if (test_bit(__I40E_RESET_FAILED, pf->state))
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00006584 return;
6585
Shannon Nelson86df2422014-05-20 08:01:35 +00006586 /* check for error indications */
6587 val = rd32(&pf->hw, pf->hw.aq.arq.len);
6588 oldval = val;
6589 if (val & I40E_PF_ARQLEN_ARQVFE_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006590 if (hw->debug_mask & I40E_DEBUG_AQ)
6591 dev_info(&pf->pdev->dev, "ARQ VF Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006592 val &= ~I40E_PF_ARQLEN_ARQVFE_MASK;
6593 }
6594 if (val & I40E_PF_ARQLEN_ARQOVFL_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006595 if (hw->debug_mask & I40E_DEBUG_AQ)
6596 dev_info(&pf->pdev->dev, "ARQ Overflow Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006597 val &= ~I40E_PF_ARQLEN_ARQOVFL_MASK;
Mitch Williams1d0a4ad2015-12-23 12:05:48 -08006598 pf->arq_overflows++;
Shannon Nelson86df2422014-05-20 08:01:35 +00006599 }
6600 if (val & I40E_PF_ARQLEN_ARQCRIT_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006601 if (hw->debug_mask & I40E_DEBUG_AQ)
6602 dev_info(&pf->pdev->dev, "ARQ Critical Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006603 val &= ~I40E_PF_ARQLEN_ARQCRIT_MASK;
6604 }
6605 if (oldval != val)
6606 wr32(&pf->hw, pf->hw.aq.arq.len, val);
6607
6608 val = rd32(&pf->hw, pf->hw.aq.asq.len);
6609 oldval = val;
6610 if (val & I40E_PF_ATQLEN_ATQVFE_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006611 if (pf->hw.debug_mask & I40E_DEBUG_AQ)
6612 dev_info(&pf->pdev->dev, "ASQ VF Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006613 val &= ~I40E_PF_ATQLEN_ATQVFE_MASK;
6614 }
6615 if (val & I40E_PF_ATQLEN_ATQOVFL_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006616 if (pf->hw.debug_mask & I40E_DEBUG_AQ)
6617 dev_info(&pf->pdev->dev, "ASQ Overflow Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006618 val &= ~I40E_PF_ATQLEN_ATQOVFL_MASK;
6619 }
6620 if (val & I40E_PF_ATQLEN_ATQCRIT_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006621 if (pf->hw.debug_mask & I40E_DEBUG_AQ)
6622 dev_info(&pf->pdev->dev, "ASQ Critical Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006623 val &= ~I40E_PF_ATQLEN_ATQCRIT_MASK;
6624 }
6625 if (oldval != val)
6626 wr32(&pf->hw, pf->hw.aq.asq.len, val);
6627
Mitch Williams1001dc32014-11-11 20:02:19 +00006628 event.buf_len = I40E_MAX_AQ_BUF_SIZE;
6629 event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006630 if (!event.msg_buf)
6631 return;
6632
6633 do {
6634 ret = i40e_clean_arq_element(hw, &event, &pending);
Mitch Williams56497972014-06-04 08:45:18 +00006635 if (ret == I40E_ERR_ADMIN_QUEUE_NO_WORK)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006636 break;
Mitch Williams56497972014-06-04 08:45:18 +00006637 else if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006638 dev_info(&pf->pdev->dev, "ARQ event error %d\n", ret);
6639 break;
6640 }
6641
6642 opcode = le16_to_cpu(event.desc.opcode);
6643 switch (opcode) {
6644
6645 case i40e_aqc_opc_get_link_status:
6646 i40e_handle_link_event(pf, &event);
6647 break;
6648 case i40e_aqc_opc_send_msg_to_pf:
6649 ret = i40e_vc_process_vf_msg(pf,
6650 le16_to_cpu(event.desc.retval),
6651 le32_to_cpu(event.desc.cookie_high),
6652 le32_to_cpu(event.desc.cookie_low),
6653 event.msg_buf,
Mitch Williams1001dc32014-11-11 20:02:19 +00006654 event.msg_len);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006655 break;
6656 case i40e_aqc_opc_lldp_update_mib:
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006657 dev_dbg(&pf->pdev->dev, "ARQ: Update LLDP MIB event received\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006658#ifdef CONFIG_I40E_DCB
6659 rtnl_lock();
6660 ret = i40e_handle_lldp_event(pf, &event);
6661 rtnl_unlock();
6662#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006663 break;
6664 case i40e_aqc_opc_event_lan_overflow:
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006665 dev_dbg(&pf->pdev->dev, "ARQ LAN queue overflow event received\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006666 i40e_handle_lan_overflow_event(pf, &event);
6667 break;
Shannon Nelson0467bc92013-12-18 13:45:58 +00006668 case i40e_aqc_opc_send_msg_to_peer:
6669 dev_info(&pf->pdev->dev, "ARQ: Msg from other pf\n");
6670 break;
Shannon Nelson91a0f932015-03-19 14:32:01 -07006671 case i40e_aqc_opc_nvm_erase:
6672 case i40e_aqc_opc_nvm_update:
Michal Kosiarz00ada502015-11-19 11:34:20 -08006673 case i40e_aqc_opc_oem_post_update:
Shannon Nelson6e93d0c2016-01-15 14:33:18 -08006674 i40e_debug(&pf->hw, I40E_DEBUG_NVM,
6675 "ARQ NVM operation 0x%04x completed\n",
6676 opcode);
Shannon Nelson91a0f932015-03-19 14:32:01 -07006677 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006678 default:
6679 dev_info(&pf->pdev->dev,
Shannon Nelson56e5ca62016-03-10 14:59:48 -08006680 "ARQ: Unknown event 0x%04x ignored\n",
Shannon Nelson0467bc92013-12-18 13:45:58 +00006681 opcode);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006682 break;
6683 }
Christopher N Bednarz1fca3262017-03-10 12:22:03 -08006684 } while (i++ < pf->adminq_work_limit);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006685
Christopher N Bednarz1fca3262017-03-10 12:22:03 -08006686 if (i < pf->adminq_work_limit)
Jacob Keller0da36b92017-04-19 09:25:55 -04006687 clear_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state);
Christopher N Bednarz1fca3262017-03-10 12:22:03 -08006688
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006689 /* re-enable Admin queue interrupt cause */
6690 val = rd32(hw, I40E_PFINT_ICR0_ENA);
6691 val |= I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
6692 wr32(hw, I40E_PFINT_ICR0_ENA, val);
6693 i40e_flush(hw);
6694
6695 kfree(event.msg_buf);
6696}
6697
6698/**
Shannon Nelson4eb3f762014-03-06 08:59:58 +00006699 * i40e_verify_eeprom - make sure eeprom is good to use
6700 * @pf: board private structure
6701 **/
6702static void i40e_verify_eeprom(struct i40e_pf *pf)
6703{
6704 int err;
6705
6706 err = i40e_diag_eeprom_test(&pf->hw);
6707 if (err) {
6708 /* retry in case of garbage read */
6709 err = i40e_diag_eeprom_test(&pf->hw);
6710 if (err) {
6711 dev_info(&pf->pdev->dev, "eeprom check failed (%d), Tx/Rx traffic disabled\n",
6712 err);
Jacob Keller0da36b92017-04-19 09:25:55 -04006713 set_bit(__I40E_BAD_EEPROM, pf->state);
Shannon Nelson4eb3f762014-03-06 08:59:58 +00006714 }
6715 }
6716
Jacob Keller0da36b92017-04-19 09:25:55 -04006717 if (!err && test_bit(__I40E_BAD_EEPROM, pf->state)) {
Shannon Nelson4eb3f762014-03-06 08:59:58 +00006718 dev_info(&pf->pdev->dev, "eeprom check passed, Tx/Rx traffic enabled\n");
Jacob Keller0da36b92017-04-19 09:25:55 -04006719 clear_bit(__I40E_BAD_EEPROM, pf->state);
Shannon Nelson4eb3f762014-03-06 08:59:58 +00006720 }
6721}
6722
6723/**
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006724 * i40e_enable_pf_switch_lb
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00006725 * @pf: pointer to the PF structure
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006726 *
6727 * enable switch loop back or die - no point in a return value
6728 **/
6729static void i40e_enable_pf_switch_lb(struct i40e_pf *pf)
6730{
6731 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
6732 struct i40e_vsi_context ctxt;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006733 int ret;
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006734
6735 ctxt.seid = pf->main_vsi_seid;
6736 ctxt.pf_num = pf->hw.pf_id;
6737 ctxt.vf_num = 0;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006738 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
6739 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006740 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006741 "couldn't get PF vsi config, err %s aq_err %s\n",
6742 i40e_stat_str(&pf->hw, ret),
6743 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006744 return;
6745 }
6746 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
6747 ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
6748 ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
6749
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006750 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
6751 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006752 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006753 "update vsi switch failed, err %s aq_err %s\n",
6754 i40e_stat_str(&pf->hw, ret),
6755 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006756 }
6757}
6758
6759/**
6760 * i40e_disable_pf_switch_lb
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00006761 * @pf: pointer to the PF structure
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006762 *
6763 * disable switch loop back or die - no point in a return value
6764 **/
6765static void i40e_disable_pf_switch_lb(struct i40e_pf *pf)
6766{
6767 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
6768 struct i40e_vsi_context ctxt;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006769 int ret;
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006770
6771 ctxt.seid = pf->main_vsi_seid;
6772 ctxt.pf_num = pf->hw.pf_id;
6773 ctxt.vf_num = 0;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006774 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
6775 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006776 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006777 "couldn't get PF vsi config, err %s aq_err %s\n",
6778 i40e_stat_str(&pf->hw, ret),
6779 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006780 return;
6781 }
6782 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
6783 ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
6784 ctxt.info.switch_id &= ~cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
6785
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006786 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
6787 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006788 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006789 "update vsi switch failed, err %s aq_err %s\n",
6790 i40e_stat_str(&pf->hw, ret),
6791 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006792 }
6793}
6794
6795/**
Neerav Parikh51616012015-02-06 08:52:14 +00006796 * i40e_config_bridge_mode - Configure the HW bridge mode
6797 * @veb: pointer to the bridge instance
6798 *
6799 * Configure the loop back mode for the LAN VSI that is downlink to the
6800 * specified HW bridge instance. It is expected this function is called
6801 * when a new HW bridge is instantiated.
6802 **/
6803static void i40e_config_bridge_mode(struct i40e_veb *veb)
6804{
6805 struct i40e_pf *pf = veb->pf;
6806
Shannon Nelson6dec1012015-09-28 14:12:30 -04006807 if (pf->hw.debug_mask & I40E_DEBUG_LAN)
6808 dev_info(&pf->pdev->dev, "enabling bridge mode: %s\n",
6809 veb->bridge_mode == BRIDGE_MODE_VEPA ? "VEPA" : "VEB");
Neerav Parikh51616012015-02-06 08:52:14 +00006810 if (veb->bridge_mode & BRIDGE_MODE_VEPA)
6811 i40e_disable_pf_switch_lb(pf);
6812 else
6813 i40e_enable_pf_switch_lb(pf);
6814}
6815
6816/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006817 * i40e_reconstitute_veb - rebuild the VEB and anything connected to it
6818 * @veb: pointer to the VEB instance
6819 *
6820 * This is a recursive function that first builds the attached VSIs then
6821 * recurses in to build the next layer of VEB. We track the connections
6822 * through our own index numbers because the seid's from the HW could
6823 * change across the reset.
6824 **/
6825static int i40e_reconstitute_veb(struct i40e_veb *veb)
6826{
6827 struct i40e_vsi *ctl_vsi = NULL;
6828 struct i40e_pf *pf = veb->pf;
6829 int v, veb_idx;
6830 int ret;
6831
6832 /* build VSI that owns this VEB, temporarily attached to base VEB */
Mitch Williams505682c2014-05-20 08:01:37 +00006833 for (v = 0; v < pf->num_alloc_vsi && !ctl_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006834 if (pf->vsi[v] &&
6835 pf->vsi[v]->veb_idx == veb->idx &&
6836 pf->vsi[v]->flags & I40E_VSI_FLAG_VEB_OWNER) {
6837 ctl_vsi = pf->vsi[v];
6838 break;
6839 }
6840 }
6841 if (!ctl_vsi) {
6842 dev_info(&pf->pdev->dev,
6843 "missing owner VSI for veb_idx %d\n", veb->idx);
6844 ret = -ENOENT;
6845 goto end_reconstitute;
6846 }
6847 if (ctl_vsi != pf->vsi[pf->lan_vsi])
6848 ctl_vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
6849 ret = i40e_add_vsi(ctl_vsi);
6850 if (ret) {
6851 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006852 "rebuild of veb_idx %d owner VSI failed: %d\n",
6853 veb->idx, ret);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006854 goto end_reconstitute;
6855 }
6856 i40e_vsi_reset_stats(ctl_vsi);
6857
6858 /* create the VEB in the switch and move the VSI onto the VEB */
6859 ret = i40e_add_veb(veb, ctl_vsi);
6860 if (ret)
6861 goto end_reconstitute;
6862
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07006863 if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
6864 veb->bridge_mode = BRIDGE_MODE_VEB;
6865 else
6866 veb->bridge_mode = BRIDGE_MODE_VEPA;
Neerav Parikh51616012015-02-06 08:52:14 +00006867 i40e_config_bridge_mode(veb);
Anjali Singhai Jainb64ba082014-11-13 03:06:15 +00006868
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006869 /* create the remaining VSIs attached to this VEB */
Mitch Williams505682c2014-05-20 08:01:37 +00006870 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006871 if (!pf->vsi[v] || pf->vsi[v] == ctl_vsi)
6872 continue;
6873
6874 if (pf->vsi[v]->veb_idx == veb->idx) {
6875 struct i40e_vsi *vsi = pf->vsi[v];
Jesse Brandeburg6995b362015-08-28 17:55:54 -04006876
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006877 vsi->uplink_seid = veb->seid;
6878 ret = i40e_add_vsi(vsi);
6879 if (ret) {
6880 dev_info(&pf->pdev->dev,
6881 "rebuild of vsi_idx %d failed: %d\n",
6882 v, ret);
6883 goto end_reconstitute;
6884 }
6885 i40e_vsi_reset_stats(vsi);
6886 }
6887 }
6888
6889 /* create any VEBs attached to this VEB - RECURSION */
6890 for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
6891 if (pf->veb[veb_idx] && pf->veb[veb_idx]->veb_idx == veb->idx) {
6892 pf->veb[veb_idx]->uplink_seid = veb->seid;
6893 ret = i40e_reconstitute_veb(pf->veb[veb_idx]);
6894 if (ret)
6895 break;
6896 }
6897 }
6898
6899end_reconstitute:
6900 return ret;
6901}
6902
6903/**
6904 * i40e_get_capabilities - get info about the HW
6905 * @pf: the PF struct
6906 **/
6907static int i40e_get_capabilities(struct i40e_pf *pf)
6908{
6909 struct i40e_aqc_list_capabilities_element_resp *cap_buf;
6910 u16 data_size;
6911 int buf_len;
6912 int err;
6913
6914 buf_len = 40 * sizeof(struct i40e_aqc_list_capabilities_element_resp);
6915 do {
6916 cap_buf = kzalloc(buf_len, GFP_KERNEL);
6917 if (!cap_buf)
6918 return -ENOMEM;
6919
6920 /* this loads the data into the hw struct for us */
6921 err = i40e_aq_discover_capabilities(&pf->hw, cap_buf, buf_len,
6922 &data_size,
6923 i40e_aqc_opc_list_func_capabilities,
6924 NULL);
6925 /* data loaded, buffer no longer needed */
6926 kfree(cap_buf);
6927
6928 if (pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOMEM) {
6929 /* retry with a larger buffer */
6930 buf_len = data_size;
6931 } else if (pf->hw.aq.asq_last_status != I40E_AQ_RC_OK) {
6932 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006933 "capability discovery failed, err %s aq_err %s\n",
6934 i40e_stat_str(&pf->hw, err),
6935 i40e_aq_str(&pf->hw,
6936 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006937 return -ENODEV;
6938 }
6939 } while (err);
6940
6941 if (pf->hw.debug_mask & I40E_DEBUG_USER)
6942 dev_info(&pf->pdev->dev,
6943 "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",
6944 pf->hw.pf_id, pf->hw.func_caps.num_vfs,
6945 pf->hw.func_caps.num_msix_vectors,
6946 pf->hw.func_caps.num_msix_vectors_vf,
6947 pf->hw.func_caps.fd_filters_guaranteed,
6948 pf->hw.func_caps.fd_filters_best_effort,
6949 pf->hw.func_caps.num_tx_qp,
6950 pf->hw.func_caps.num_vsis);
6951
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00006952#define DEF_NUM_VSI (1 + (pf->hw.func_caps.fcoe ? 1 : 0) \
6953 + pf->hw.func_caps.num_vfs)
6954 if (pf->hw.revision_id == 0 && (DEF_NUM_VSI > pf->hw.func_caps.num_vsis)) {
6955 dev_info(&pf->pdev->dev,
6956 "got num_vsis %d, setting num_vsis to %d\n",
6957 pf->hw.func_caps.num_vsis, DEF_NUM_VSI);
6958 pf->hw.func_caps.num_vsis = DEF_NUM_VSI;
6959 }
6960
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006961 return 0;
6962}
6963
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006964static int i40e_vsi_clear(struct i40e_vsi *vsi);
6965
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006966/**
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006967 * i40e_fdir_sb_setup - initialize the Flow Director resources for Sideband
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006968 * @pf: board private structure
6969 **/
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006970static void i40e_fdir_sb_setup(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006971{
6972 struct i40e_vsi *vsi;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006973
Jesse Brandeburg407e0632014-06-03 23:50:12 +00006974 /* quick workaround for an NVM issue that leaves a critical register
6975 * uninitialized
6976 */
6977 if (!rd32(&pf->hw, I40E_GLQF_HKEY(0))) {
6978 static const u32 hkey[] = {
6979 0xe640d33f, 0xcdfe98ab, 0x73fa7161, 0x0d7a7d36,
6980 0xeacb7d61, 0xaa4f05b6, 0x9c5c89ed, 0xfc425ddb,
6981 0xa4654832, 0xfc7461d4, 0x8f827619, 0xf5c63c21,
6982 0x95b3a76d};
Alexander Duyck4b816442016-10-11 15:26:53 -07006983 int i;
Jesse Brandeburg407e0632014-06-03 23:50:12 +00006984
6985 for (i = 0; i <= I40E_GLQF_HKEY_MAX_INDEX; i++)
6986 wr32(&pf->hw, I40E_GLQF_HKEY(i), hkey[i]);
6987 }
6988
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006989 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006990 return;
6991
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006992 /* find existing VSI and see if it needs configuring */
Alexander Duyck4b816442016-10-11 15:26:53 -07006993 vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006994
6995 /* create a new VSI if none exists */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006996 if (!vsi) {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006997 vsi = i40e_vsi_setup(pf, I40E_VSI_FDIR,
6998 pf->vsi[pf->lan_vsi]->seid, 0);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006999 if (!vsi) {
7000 dev_info(&pf->pdev->dev, "Couldn't create FDir VSI\n");
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00007001 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
7002 return;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007003 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007004 }
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00007005
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08007006 i40e_vsi_setup_irqhandler(vsi, i40e_fdir_clean_ring);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007007}
7008
7009/**
7010 * i40e_fdir_teardown - release the Flow Director resources
7011 * @pf: board private structure
7012 **/
7013static void i40e_fdir_teardown(struct i40e_pf *pf)
7014{
Alexander Duyck4b816442016-10-11 15:26:53 -07007015 struct i40e_vsi *vsi;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007016
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00007017 i40e_fdir_filter_exit(pf);
Alexander Duyck4b816442016-10-11 15:26:53 -07007018 vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
7019 if (vsi)
7020 i40e_vsi_release(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007021}
7022
7023/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007024 * i40e_prep_for_reset - prep for the core to reset
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007025 * @pf: board private structure
Maciej Sosin373149f2017-04-05 07:50:55 -04007026 * @lock_acquired: indicates whether or not the lock has been acquired
7027 * before this function was called.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007028 *
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00007029 * Close up the VFs and other things in prep for PF Reset.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007030 **/
Maciej Sosin373149f2017-04-05 07:50:55 -04007031static void i40e_prep_for_reset(struct i40e_pf *pf, bool lock_acquired)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007032{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007033 struct i40e_hw *hw = &pf->hw;
Shannon Nelson60442de2014-04-23 04:50:13 +00007034 i40e_status ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007035 u32 v;
7036
Jacob Keller0da36b92017-04-19 09:25:55 -04007037 clear_bit(__I40E_RESET_INTR_RECEIVED, pf->state);
7038 if (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
Shannon Nelson23cfbe02014-06-04 01:23:14 +00007039 return;
Mitch Williamsd3ce57342016-03-10 14:59:46 -08007040 if (i40e_check_asq_alive(&pf->hw))
7041 i40e_vc_notify_reset(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007042
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00007043 dev_dbg(&pf->pdev->dev, "Tearing down internal switch for reset\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007044
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007045 /* quiesce the VSIs and their queues that are not already DOWN */
Maciej Sosin373149f2017-04-05 07:50:55 -04007046 /* pf_quiesce_all_vsi modifies netdev structures -rtnl_lock needed */
7047 if (!lock_acquired)
7048 rtnl_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007049 i40e_pf_quiesce_all_vsi(pf);
Maciej Sosin373149f2017-04-05 07:50:55 -04007050 if (!lock_acquired)
7051 rtnl_unlock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007052
Mitch Williams505682c2014-05-20 08:01:37 +00007053 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007054 if (pf->vsi[v])
7055 pf->vsi[v]->seid = 0;
7056 }
7057
7058 i40e_shutdown_adminq(&pf->hw);
7059
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007060 /* call shutdown HMC */
Shannon Nelson60442de2014-04-23 04:50:13 +00007061 if (hw->hmc.hmc_obj) {
7062 ret = i40e_shutdown_lan_hmc(hw);
Shannon Nelson23cfbe02014-06-04 01:23:14 +00007063 if (ret)
Shannon Nelson60442de2014-04-23 04:50:13 +00007064 dev_warn(&pf->pdev->dev,
7065 "shutdown_lan_hmc failed: %d\n", ret);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007066 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007067}
7068
7069/**
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00007070 * i40e_send_version - update firmware with driver version
7071 * @pf: PF struct
7072 */
7073static void i40e_send_version(struct i40e_pf *pf)
7074{
7075 struct i40e_driver_version dv;
7076
7077 dv.major_version = DRV_VERSION_MAJOR;
7078 dv.minor_version = DRV_VERSION_MINOR;
7079 dv.build_version = DRV_VERSION_BUILD;
7080 dv.subbuild_version = 0;
Rickard Strandqvist35a7d802014-07-29 09:26:25 +00007081 strlcpy(dv.driver_string, DRV_VERSION, sizeof(dv.driver_string));
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00007082 i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
7083}
7084
7085/**
Filip Sadowski5bbb2e22017-06-07 05:43:09 -04007086 * i40e_get_oem_version - get OEM specific version information
7087 * @hw: pointer to the hardware structure
7088 **/
7089static void i40e_get_oem_version(struct i40e_hw *hw)
7090{
7091 u16 block_offset = 0xffff;
7092 u16 block_length = 0;
7093 u16 capabilities = 0;
7094 u16 gen_snap = 0;
7095 u16 release = 0;
7096
7097#define I40E_SR_NVM_OEM_VERSION_PTR 0x1B
7098#define I40E_NVM_OEM_LENGTH_OFFSET 0x00
7099#define I40E_NVM_OEM_CAPABILITIES_OFFSET 0x01
7100#define I40E_NVM_OEM_GEN_OFFSET 0x02
7101#define I40E_NVM_OEM_RELEASE_OFFSET 0x03
7102#define I40E_NVM_OEM_CAPABILITIES_MASK 0x000F
7103#define I40E_NVM_OEM_LENGTH 3
7104
7105 /* Check if pointer to OEM version block is valid. */
7106 i40e_read_nvm_word(hw, I40E_SR_NVM_OEM_VERSION_PTR, &block_offset);
7107 if (block_offset == 0xffff)
7108 return;
7109
7110 /* Check if OEM version block has correct length. */
7111 i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_LENGTH_OFFSET,
7112 &block_length);
7113 if (block_length < I40E_NVM_OEM_LENGTH)
7114 return;
7115
7116 /* Check if OEM version format is as expected. */
7117 i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_CAPABILITIES_OFFSET,
7118 &capabilities);
7119 if ((capabilities & I40E_NVM_OEM_CAPABILITIES_MASK) != 0)
7120 return;
7121
7122 i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_GEN_OFFSET,
7123 &gen_snap);
7124 i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_RELEASE_OFFSET,
7125 &release);
7126 hw->nvm.oem_ver = (gen_snap << I40E_OEM_SNAP_SHIFT) | release;
7127 hw->nvm.eetrack = I40E_OEM_EETRACK_ID;
7128}
7129
7130/**
Maciej Sosin373149f2017-04-05 07:50:55 -04007131 * i40e_reset - wait for core reset to finish reset, reset pf if corer not seen
7132 * @pf: board private structure
7133 **/
7134static int i40e_reset(struct i40e_pf *pf)
7135{
7136 struct i40e_hw *hw = &pf->hw;
7137 i40e_status ret;
7138
7139 ret = i40e_pf_reset(hw);
7140 if (ret) {
7141 dev_info(&pf->pdev->dev, "PF reset failed, %d\n", ret);
Jacob Keller0da36b92017-04-19 09:25:55 -04007142 set_bit(__I40E_RESET_FAILED, pf->state);
7143 clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
Maciej Sosin373149f2017-04-05 07:50:55 -04007144 } else {
7145 pf->pfr_count++;
7146 }
7147 return ret;
7148}
7149
7150/**
7151 * i40e_rebuild - rebuild using a saved config
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007152 * @pf: board private structure
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007153 * @reinit: if the Main VSI needs to re-initialized.
Maciej Sosin373149f2017-04-05 07:50:55 -04007154 * @lock_acquired: indicates whether or not the lock has been acquired
7155 * before this function was called.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007156 **/
Maciej Sosin373149f2017-04-05 07:50:55 -04007157static void i40e_rebuild(struct i40e_pf *pf, bool reinit, bool lock_acquired)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007158{
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007159 struct i40e_hw *hw = &pf->hw;
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00007160 u8 set_fc_aq_fail = 0;
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007161 i40e_status ret;
Anjali Singhai Jain4f2f017c2015-10-21 19:47:07 -04007162 u32 val;
Maciej Sosin373149f2017-04-05 07:50:55 -04007163 int v;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007164
Mauro S. M. Rodrigues9e6c9c02017-05-12 23:26:56 -03007165 if (test_bit(__I40E_DOWN, pf->state))
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00007166 goto clear_recovery;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00007167 dev_dbg(&pf->pdev->dev, "Rebuilding internal switch\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007168
7169 /* rebuild the basics for the AdminQ, HMC, and initial HW switch */
7170 ret = i40e_init_adminq(&pf->hw);
7171 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04007172 dev_info(&pf->pdev->dev, "Rebuild AdminQ failed, err %s aq_err %s\n",
7173 i40e_stat_str(&pf->hw, ret),
7174 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00007175 goto clear_recovery;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007176 }
Filip Sadowski5bbb2e22017-06-07 05:43:09 -04007177 i40e_get_oem_version(&pf->hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007178
Shannon Nelson4eb3f762014-03-06 08:59:58 +00007179 /* re-verify the eeprom if we just had an EMP reset */
Jacob Keller0da36b92017-04-19 09:25:55 -04007180 if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state))
Shannon Nelson4eb3f762014-03-06 08:59:58 +00007181 i40e_verify_eeprom(pf);
Shannon Nelson4eb3f762014-03-06 08:59:58 +00007182
Shannon Nelsone78ac4bf2014-05-10 04:49:09 +00007183 i40e_clear_pxe_mode(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007184 ret = i40e_get_capabilities(pf);
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04007185 if (ret)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007186 goto end_core_reset;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007187
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007188 ret = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
Alexander Duyckc76cb6e2017-02-21 15:55:48 -08007189 hw->func_caps.num_rx_qp, 0, 0);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007190 if (ret) {
7191 dev_info(&pf->pdev->dev, "init_lan_hmc failed: %d\n", ret);
7192 goto end_core_reset;
7193 }
7194 ret = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
7195 if (ret) {
7196 dev_info(&pf->pdev->dev, "configure_lan_hmc failed: %d\n", ret);
7197 goto end_core_reset;
7198 }
7199
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08007200#ifdef CONFIG_I40E_DCB
7201 ret = i40e_init_pf_dcb(pf);
7202 if (ret) {
Shannon Nelsonaebfc812014-12-11 07:06:38 +00007203 dev_info(&pf->pdev->dev, "DCB init failed %d, disabled\n", ret);
7204 pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
7205 /* Continue without DCB enabled */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08007206 }
7207#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007208 /* do basic switch setup */
Maciej Sosin373149f2017-04-05 07:50:55 -04007209 if (!lock_acquired)
7210 rtnl_lock();
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007211 ret = i40e_setup_pf_switch(pf, reinit);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007212 if (ret)
Maciej Sosin373149f2017-04-05 07:50:55 -04007213 goto end_unlock;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007214
Shannon Nelson2f0aff42016-01-04 10:33:08 -08007215 /* The driver only wants link up/down and module qualification
7216 * reports from firmware. Note the negative logic.
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +00007217 */
7218 ret = i40e_aq_set_phy_int_mask(&pf->hw,
Shannon Nelson2f0aff42016-01-04 10:33:08 -08007219 ~(I40E_AQ_EVENT_LINK_UPDOWN |
Shannon Nelson867a79e2016-03-18 12:18:15 -07007220 I40E_AQ_EVENT_MEDIA_NA |
Shannon Nelson2f0aff42016-01-04 10:33:08 -08007221 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +00007222 if (ret)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04007223 dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
7224 i40e_stat_str(&pf->hw, ret),
7225 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +00007226
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00007227 /* make sure our flow control settings are restored */
7228 ret = i40e_set_fc(&pf->hw, &set_fc_aq_fail, true);
7229 if (ret)
Neerav Parikh8279e492015-09-03 17:18:50 -04007230 dev_dbg(&pf->pdev->dev, "setting flow control: ret = %s last_status = %s\n",
7231 i40e_stat_str(&pf->hw, ret),
7232 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00007233
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007234 /* Rebuild the VSIs and VEBs that existed before reset.
7235 * They are still in our local switch element arrays, so only
7236 * need to rebuild the switch model in the HW.
7237 *
7238 * If there were VEBs but the reconstitution failed, we'll try
7239 * try to recover minimal use by getting the basic PF VSI working.
7240 */
7241 if (pf->vsi[pf->lan_vsi]->uplink_seid != pf->mac_seid) {
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00007242 dev_dbg(&pf->pdev->dev, "attempting to rebuild switch\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007243 /* find the one VEB connected to the MAC, and find orphans */
7244 for (v = 0; v < I40E_MAX_VEB; v++) {
7245 if (!pf->veb[v])
7246 continue;
7247
7248 if (pf->veb[v]->uplink_seid == pf->mac_seid ||
7249 pf->veb[v]->uplink_seid == 0) {
7250 ret = i40e_reconstitute_veb(pf->veb[v]);
7251
7252 if (!ret)
7253 continue;
7254
7255 /* If Main VEB failed, we're in deep doodoo,
7256 * so give up rebuilding the switch and set up
7257 * for minimal rebuild of PF VSI.
7258 * If orphan failed, we'll report the error
7259 * but try to keep going.
7260 */
7261 if (pf->veb[v]->uplink_seid == pf->mac_seid) {
7262 dev_info(&pf->pdev->dev,
7263 "rebuild of switch failed: %d, will try to set up simple PF connection\n",
7264 ret);
7265 pf->vsi[pf->lan_vsi]->uplink_seid
7266 = pf->mac_seid;
7267 break;
7268 } else if (pf->veb[v]->uplink_seid == 0) {
7269 dev_info(&pf->pdev->dev,
7270 "rebuild of orphan VEB failed: %d\n",
7271 ret);
7272 }
7273 }
7274 }
7275 }
7276
7277 if (pf->vsi[pf->lan_vsi]->uplink_seid == pf->mac_seid) {
Shannon Nelsoncde4cbc2014-06-04 01:23:17 +00007278 dev_dbg(&pf->pdev->dev, "attempting to rebuild PF VSI\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007279 /* no VEB, so rebuild only the Main VSI */
7280 ret = i40e_add_vsi(pf->vsi[pf->lan_vsi]);
7281 if (ret) {
7282 dev_info(&pf->pdev->dev,
7283 "rebuild of Main VSI failed: %d\n", ret);
Maciej Sosin373149f2017-04-05 07:50:55 -04007284 goto end_unlock;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007285 }
7286 }
7287
Anjali Singhai Jain4f2f017c2015-10-21 19:47:07 -04007288 /* Reconfigure hardware for allowing smaller MSS in the case
7289 * of TSO, so that we avoid the MDD being fired and causing
7290 * a reset in the case of small MSS+TSO.
7291 */
7292#define I40E_REG_MSS 0x000E64DC
7293#define I40E_REG_MSS_MIN_MASK 0x3FF0000
7294#define I40E_64BYTE_MSS 0x400000
7295 val = rd32(hw, I40E_REG_MSS);
7296 if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
7297 val &= ~I40E_REG_MSS_MIN_MASK;
7298 val |= I40E_64BYTE_MSS;
7299 wr32(hw, I40E_REG_MSS, val);
7300 }
7301
Jacob Kellerd36e41d2017-06-23 04:24:46 -04007302 if (pf->hw_features & I40E_HW_RESTART_AUTONEG) {
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +00007303 msleep(75);
7304 ret = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
7305 if (ret)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04007306 dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
7307 i40e_stat_str(&pf->hw, ret),
7308 i40e_aq_str(&pf->hw,
7309 pf->hw.aq.asq_last_status));
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00007310 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007311 /* reinit the misc interrupt */
7312 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
7313 ret = i40e_setup_misc_vector(pf);
7314
Anjali Singhai Jaine7358f52015-10-01 14:37:34 -04007315 /* Add a filter to drop all Flow control frames from any VSI from being
7316 * transmitted. By doing so we stop a malicious VF from sending out
7317 * PAUSE or PFC frames and potentially controlling traffic for other
7318 * PF/VF VSIs.
7319 * The FW can still send Flow control frames if enabled.
7320 */
7321 i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
7322 pf->main_vsi_seid);
7323
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007324 /* restart the VSIs that were rebuilt and running before the reset */
7325 i40e_pf_unquiesce_all_vsi(pf);
7326
Jacob Keller024b05f2017-04-13 04:45:46 -04007327 /* Release the RTNL lock before we start resetting VFs */
7328 if (!lock_acquired)
7329 rtnl_unlock();
7330
Jacob Kellere4b433f2017-04-13 04:45:52 -04007331 i40e_reset_all_vfs(pf, true);
Mitch Williams69f64b22014-02-13 03:48:46 -08007332
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007333 /* tell the firmware that we're starting */
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00007334 i40e_send_version(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007335
Jacob Keller024b05f2017-04-13 04:45:46 -04007336 /* We've already released the lock, so don't do it again */
7337 goto end_core_reset;
7338
Maciej Sosin373149f2017-04-05 07:50:55 -04007339end_unlock:
Jacob Keller024b05f2017-04-13 04:45:46 -04007340 if (!lock_acquired)
7341 rtnl_unlock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007342end_core_reset:
Jacob Keller0da36b92017-04-19 09:25:55 -04007343 clear_bit(__I40E_RESET_FAILED, pf->state);
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00007344clear_recovery:
Jacob Keller0da36b92017-04-19 09:25:55 -04007345 clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007346}
7347
7348/**
Maciej Sosin373149f2017-04-05 07:50:55 -04007349 * i40e_reset_and_rebuild - reset and rebuild using a saved config
7350 * @pf: board private structure
7351 * @reinit: if the Main VSI needs to re-initialized.
7352 * @lock_acquired: indicates whether or not the lock has been acquired
7353 * before this function was called.
7354 **/
7355static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit,
7356 bool lock_acquired)
7357{
7358 int ret;
7359 /* Now we wait for GRST to settle out.
7360 * We don't have to delete the VEBs or VSIs from the hw switch
7361 * because the reset will make them disappear.
7362 */
7363 ret = i40e_reset(pf);
7364 if (!ret)
7365 i40e_rebuild(pf, reinit, lock_acquired);
7366}
7367
7368/**
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00007369 * i40e_handle_reset_warning - prep for the PF to reset, reset and rebuild
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007370 * @pf: board private structure
7371 *
7372 * Close up the VFs and other things in prep for a Core Reset,
7373 * then get ready to rebuild the world.
Maciej Sosin373149f2017-04-05 07:50:55 -04007374 * @lock_acquired: indicates whether or not the lock has been acquired
7375 * before this function was called.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007376 **/
Maciej Sosin373149f2017-04-05 07:50:55 -04007377static void i40e_handle_reset_warning(struct i40e_pf *pf, bool lock_acquired)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007378{
Maciej Sosin373149f2017-04-05 07:50:55 -04007379 i40e_prep_for_reset(pf, lock_acquired);
7380 i40e_reset_and_rebuild(pf, false, lock_acquired);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007381}
7382
7383/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007384 * i40e_handle_mdd_event
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00007385 * @pf: pointer to the PF structure
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007386 *
7387 * Called from the MDD irq handler to identify possibly malicious vfs
7388 **/
7389static void i40e_handle_mdd_event(struct i40e_pf *pf)
7390{
7391 struct i40e_hw *hw = &pf->hw;
7392 bool mdd_detected = false;
Neerav Parikhdf430b12014-06-04 01:23:15 +00007393 bool pf_mdd_detected = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007394 struct i40e_vf *vf;
7395 u32 reg;
7396 int i;
7397
Jacob Keller0da36b92017-04-19 09:25:55 -04007398 if (!test_bit(__I40E_MDD_EVENT_PENDING, pf->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007399 return;
7400
7401 /* find what triggered the MDD event */
7402 reg = rd32(hw, I40E_GL_MDET_TX);
7403 if (reg & I40E_GL_MDET_TX_VALID_MASK) {
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00007404 u8 pf_num = (reg & I40E_GL_MDET_TX_PF_NUM_MASK) >>
7405 I40E_GL_MDET_TX_PF_NUM_SHIFT;
Mitch Williams2089ad02014-10-17 03:14:53 +00007406 u16 vf_num = (reg & I40E_GL_MDET_TX_VF_NUM_MASK) >>
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00007407 I40E_GL_MDET_TX_VF_NUM_SHIFT;
Dan Carpenter013f6572014-10-22 20:06:29 -07007408 u8 event = (reg & I40E_GL_MDET_TX_EVENT_MASK) >>
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00007409 I40E_GL_MDET_TX_EVENT_SHIFT;
Mitch Williams2089ad02014-10-17 03:14:53 +00007410 u16 queue = ((reg & I40E_GL_MDET_TX_QUEUE_MASK) >>
7411 I40E_GL_MDET_TX_QUEUE_SHIFT) -
7412 pf->hw.func_caps.base_queue;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007413 if (netif_msg_tx_err(pf))
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00007414 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 +00007415 event, queue, pf_num, vf_num);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007416 wr32(hw, I40E_GL_MDET_TX, 0xffffffff);
7417 mdd_detected = true;
7418 }
7419 reg = rd32(hw, I40E_GL_MDET_RX);
7420 if (reg & I40E_GL_MDET_RX_VALID_MASK) {
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00007421 u8 func = (reg & I40E_GL_MDET_RX_FUNCTION_MASK) >>
7422 I40E_GL_MDET_RX_FUNCTION_SHIFT;
Dan Carpenter013f6572014-10-22 20:06:29 -07007423 u8 event = (reg & I40E_GL_MDET_RX_EVENT_MASK) >>
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00007424 I40E_GL_MDET_RX_EVENT_SHIFT;
Mitch Williams2089ad02014-10-17 03:14:53 +00007425 u16 queue = ((reg & I40E_GL_MDET_RX_QUEUE_MASK) >>
7426 I40E_GL_MDET_RX_QUEUE_SHIFT) -
7427 pf->hw.func_caps.base_queue;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007428 if (netif_msg_rx_err(pf))
7429 dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on RX queue %d of function 0x%02x\n",
7430 event, queue, func);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007431 wr32(hw, I40E_GL_MDET_RX, 0xffffffff);
7432 mdd_detected = true;
7433 }
7434
Neerav Parikhdf430b12014-06-04 01:23:15 +00007435 if (mdd_detected) {
7436 reg = rd32(hw, I40E_PF_MDET_TX);
7437 if (reg & I40E_PF_MDET_TX_VALID_MASK) {
7438 wr32(hw, I40E_PF_MDET_TX, 0xFFFF);
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007439 dev_info(&pf->pdev->dev, "TX driver issue detected, PF reset issued\n");
Neerav Parikhdf430b12014-06-04 01:23:15 +00007440 pf_mdd_detected = true;
7441 }
7442 reg = rd32(hw, I40E_PF_MDET_RX);
7443 if (reg & I40E_PF_MDET_RX_VALID_MASK) {
7444 wr32(hw, I40E_PF_MDET_RX, 0xFFFF);
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007445 dev_info(&pf->pdev->dev, "RX driver issue detected, PF reset issued\n");
Neerav Parikhdf430b12014-06-04 01:23:15 +00007446 pf_mdd_detected = true;
7447 }
7448 /* Queue belongs to the PF, initiate a reset */
7449 if (pf_mdd_detected) {
Jacob Keller0da36b92017-04-19 09:25:55 -04007450 set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
Neerav Parikhdf430b12014-06-04 01:23:15 +00007451 i40e_service_event_schedule(pf);
7452 }
7453 }
7454
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007455 /* see if one of the VFs needs its hand slapped */
7456 for (i = 0; i < pf->num_alloc_vfs && mdd_detected; i++) {
7457 vf = &(pf->vf[i]);
7458 reg = rd32(hw, I40E_VP_MDET_TX(i));
7459 if (reg & I40E_VP_MDET_TX_VALID_MASK) {
7460 wr32(hw, I40E_VP_MDET_TX(i), 0xFFFF);
7461 vf->num_mdd_events++;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007462 dev_info(&pf->pdev->dev, "TX driver issue detected on VF %d\n",
7463 i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007464 }
7465
7466 reg = rd32(hw, I40E_VP_MDET_RX(i));
7467 if (reg & I40E_VP_MDET_RX_VALID_MASK) {
7468 wr32(hw, I40E_VP_MDET_RX(i), 0xFFFF);
7469 vf->num_mdd_events++;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007470 dev_info(&pf->pdev->dev, "RX driver issue detected on VF %d\n",
7471 i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007472 }
7473
7474 if (vf->num_mdd_events > I40E_DEFAULT_NUM_MDD_EVENTS_ALLOWED) {
7475 dev_info(&pf->pdev->dev,
7476 "Too many MDD events on VF %d, disabled\n", i);
7477 dev_info(&pf->pdev->dev,
7478 "Use PF Control I/F to re-enable the VF\n");
Jacob Keller6322e632017-04-13 04:45:54 -04007479 set_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007480 }
7481 }
7482
7483 /* re-enable mdd interrupt cause */
Jacob Keller0da36b92017-04-19 09:25:55 -04007484 clear_bit(__I40E_MDD_EVENT_PENDING, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007485 reg = rd32(hw, I40E_PFINT_ICR0_ENA);
7486 reg |= I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
7487 wr32(hw, I40E_PFINT_ICR0_ENA, reg);
7488 i40e_flush(hw);
7489}
7490
Jacob Kellerd8b2c702017-06-20 15:17:00 -07007491static const char *i40e_tunnel_name(struct i40e_udp_port_config *port)
7492{
7493 switch (port->type) {
7494 case UDP_TUNNEL_TYPE_VXLAN:
7495 return "vxlan";
7496 case UDP_TUNNEL_TYPE_GENEVE:
7497 return "geneve";
7498 default:
7499 return "unknown";
7500 }
7501}
7502
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007503/**
Alexander Duyck1f190d92017-04-19 09:25:51 -04007504 * i40e_sync_udp_filters - Trigger a sync event for existing UDP filters
7505 * @pf: board private structure
7506 **/
7507static void i40e_sync_udp_filters(struct i40e_pf *pf)
7508{
7509 int i;
7510
7511 /* loop through and set pending bit for all active UDP filters */
7512 for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
7513 if (pf->udp_ports[i].port)
7514 pf->pending_udp_bitmap |= BIT_ULL(i);
7515 }
7516
7517 pf->flags |= I40E_FLAG_UDP_FILTER_SYNC;
7518}
7519
7520/**
Singhai, Anjali6a899022015-12-14 12:21:18 -08007521 * i40e_sync_udp_filters_subtask - Sync the VSI filter list with HW
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007522 * @pf: board private structure
7523 **/
Singhai, Anjali6a899022015-12-14 12:21:18 -08007524static void i40e_sync_udp_filters_subtask(struct i40e_pf *pf)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007525{
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007526 struct i40e_hw *hw = &pf->hw;
7527 i40e_status ret;
Jacob Kellerfe0b0cd2017-02-06 14:38:38 -08007528 u16 port;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007529 int i;
7530
Singhai, Anjali6a899022015-12-14 12:21:18 -08007531 if (!(pf->flags & I40E_FLAG_UDP_FILTER_SYNC))
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007532 return;
7533
Singhai, Anjali6a899022015-12-14 12:21:18 -08007534 pf->flags &= ~I40E_FLAG_UDP_FILTER_SYNC;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007535
7536 for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
Singhai, Anjali6a899022015-12-14 12:21:18 -08007537 if (pf->pending_udp_bitmap & BIT_ULL(i)) {
7538 pf->pending_udp_bitmap &= ~BIT_ULL(i);
Jacob Keller27826fd2017-04-19 09:25:50 -04007539 port = pf->udp_ports[i].port;
Shannon Nelsonc22c06c2015-03-31 00:45:04 -07007540 if (port)
Carolyn Wybornyb3f5c7b2016-08-24 11:33:51 -07007541 ret = i40e_aq_add_udp_tunnel(hw, port,
7542 pf->udp_ports[i].type,
7543 NULL, NULL);
Shannon Nelsonc22c06c2015-03-31 00:45:04 -07007544 else
7545 ret = i40e_aq_del_udp_tunnel(hw, i, NULL);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007546
7547 if (ret) {
Jacob Kellerd8b2c702017-06-20 15:17:00 -07007548 dev_info(&pf->pdev->dev,
7549 "%s %s port %d, index %d failed, err %s aq_err %s\n",
7550 i40e_tunnel_name(&pf->udp_ports[i]),
7551 port ? "add" : "delete",
7552 port, i,
7553 i40e_stat_str(&pf->hw, ret),
7554 i40e_aq_str(&pf->hw,
7555 pf->hw.aq.asq_last_status));
Jacob Keller27826fd2017-04-19 09:25:50 -04007556 pf->udp_ports[i].port = 0;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007557 }
7558 }
7559 }
7560}
7561
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007562/**
7563 * i40e_service_task - Run the driver's async subtasks
7564 * @work: pointer to work_struct containing our data
7565 **/
7566static void i40e_service_task(struct work_struct *work)
7567{
7568 struct i40e_pf *pf = container_of(work,
7569 struct i40e_pf,
7570 service_task);
7571 unsigned long start_time = jiffies;
7572
Shannon Nelsone57a2fe2014-06-03 23:50:19 +00007573 /* don't bother with service tasks if a reset is in progress */
Jacob Keller0da36b92017-04-19 09:25:55 -04007574 if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
Shannon Nelsone57a2fe2014-06-03 23:50:19 +00007575 return;
Shannon Nelsone57a2fe2014-06-03 23:50:19 +00007576
Jacob Keller0da36b92017-04-19 09:25:55 -04007577 if (test_and_set_bit(__I40E_SERVICE_SCHED, pf->state))
Mitch Williams91089032016-11-21 13:03:51 -08007578 return;
7579
Kiran Patilb03a8c12015-09-24 18:13:15 -04007580 i40e_detect_recover_hung(pf);
Jesse Brandeburg2818ccd2016-01-13 16:51:38 -08007581 i40e_sync_filters_subtask(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007582 i40e_reset_subtask(pf);
7583 i40e_handle_mdd_event(pf);
7584 i40e_vc_process_vflr_event(pf);
7585 i40e_watchdog_subtask(pf);
7586 i40e_fdir_reinit_subtask(pf);
Mitch Williams0ef2d5a2017-01-24 10:24:00 -08007587 if (pf->flags & I40E_FLAG_CLIENT_RESET) {
7588 /* Client subtask will reopen next time through. */
7589 i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], true);
7590 pf->flags &= ~I40E_FLAG_CLIENT_RESET;
7591 } else {
7592 i40e_client_subtask(pf);
7593 if (pf->flags & I40E_FLAG_CLIENT_L2_CHANGE) {
7594 i40e_notify_client_of_l2_param_changes(
7595 pf->vsi[pf->lan_vsi]);
7596 pf->flags &= ~I40E_FLAG_CLIENT_L2_CHANGE;
7597 }
7598 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007599 i40e_sync_filters_subtask(pf);
Singhai, Anjali6a899022015-12-14 12:21:18 -08007600 i40e_sync_udp_filters_subtask(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007601 i40e_clean_adminq_subtask(pf);
7602
Mitch Williams91089032016-11-21 13:03:51 -08007603 /* flush memory to make sure state is correct before next watchdog */
7604 smp_mb__before_atomic();
Jacob Keller0da36b92017-04-19 09:25:55 -04007605 clear_bit(__I40E_SERVICE_SCHED, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007606
7607 /* If the tasks have taken longer than one timer cycle or there
7608 * is more work to be done, reschedule the service task now
7609 * rather than wait for the timer to tick again.
7610 */
7611 if (time_after(jiffies, (start_time + pf->service_timer_period)) ||
Jacob Keller0da36b92017-04-19 09:25:55 -04007612 test_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state) ||
7613 test_bit(__I40E_MDD_EVENT_PENDING, pf->state) ||
7614 test_bit(__I40E_VFLR_EVENT_PENDING, pf->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007615 i40e_service_event_schedule(pf);
7616}
7617
7618/**
7619 * i40e_service_timer - timer callback
7620 * @data: pointer to PF struct
7621 **/
7622static void i40e_service_timer(unsigned long data)
7623{
7624 struct i40e_pf *pf = (struct i40e_pf *)data;
7625
7626 mod_timer(&pf->service_timer,
7627 round_jiffies(jiffies + pf->service_timer_period));
7628 i40e_service_event_schedule(pf);
7629}
7630
7631/**
7632 * i40e_set_num_rings_in_vsi - Determine number of rings in the VSI
7633 * @vsi: the VSI being configured
7634 **/
7635static int i40e_set_num_rings_in_vsi(struct i40e_vsi *vsi)
7636{
7637 struct i40e_pf *pf = vsi->back;
7638
7639 switch (vsi->type) {
7640 case I40E_VSI_MAIN:
7641 vsi->alloc_queue_pairs = pf->num_lan_qps;
7642 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
7643 I40E_REQ_DESCRIPTOR_MULTIPLE);
7644 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
7645 vsi->num_q_vectors = pf->num_lan_msix;
7646 else
7647 vsi->num_q_vectors = 1;
7648
7649 break;
7650
7651 case I40E_VSI_FDIR:
7652 vsi->alloc_queue_pairs = 1;
7653 vsi->num_desc = ALIGN(I40E_FDIR_RING_COUNT,
7654 I40E_REQ_DESCRIPTOR_MULTIPLE);
Tushar Davea70e4072016-05-16 12:40:53 -07007655 vsi->num_q_vectors = pf->num_fdsb_msix;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007656 break;
7657
7658 case I40E_VSI_VMDQ2:
7659 vsi->alloc_queue_pairs = pf->num_vmdq_qps;
7660 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
7661 I40E_REQ_DESCRIPTOR_MULTIPLE);
7662 vsi->num_q_vectors = pf->num_vmdq_msix;
7663 break;
7664
7665 case I40E_VSI_SRIOV:
7666 vsi->alloc_queue_pairs = pf->num_vf_qps;
7667 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
7668 I40E_REQ_DESCRIPTOR_MULTIPLE);
7669 break;
7670
7671 default:
7672 WARN_ON(1);
7673 return -ENODATA;
7674 }
7675
7676 return 0;
7677}
7678
7679/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007680 * i40e_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the vsi
7681 * @type: VSI pointer
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007682 * @alloc_qvectors: a bool to specify if q_vectors need to be allocated.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007683 *
7684 * On error: returns error code (negative)
7685 * On success: returns 0
7686 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007687static int i40e_vsi_alloc_arrays(struct i40e_vsi *vsi, bool alloc_qvectors)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007688{
Björn Töpel74608d12017-05-24 07:55:35 +02007689 struct i40e_ring **next_rings;
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007690 int size;
7691 int ret = 0;
7692
Björn Töpel74608d12017-05-24 07:55:35 +02007693 /* allocate memory for both Tx, XDP Tx and Rx ring pointers */
7694 size = sizeof(struct i40e_ring *) * vsi->alloc_queue_pairs *
7695 (i40e_enabled_xdp_vsi(vsi) ? 3 : 2);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007696 vsi->tx_rings = kzalloc(size, GFP_KERNEL);
7697 if (!vsi->tx_rings)
7698 return -ENOMEM;
Björn Töpel74608d12017-05-24 07:55:35 +02007699 next_rings = vsi->tx_rings + vsi->alloc_queue_pairs;
7700 if (i40e_enabled_xdp_vsi(vsi)) {
7701 vsi->xdp_rings = next_rings;
7702 next_rings += vsi->alloc_queue_pairs;
7703 }
7704 vsi->rx_rings = next_rings;
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007705
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007706 if (alloc_qvectors) {
7707 /* allocate memory for q_vector pointers */
Julia Lawallf57e4fb2014-07-30 03:11:09 +00007708 size = sizeof(struct i40e_q_vector *) * vsi->num_q_vectors;
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007709 vsi->q_vectors = kzalloc(size, GFP_KERNEL);
7710 if (!vsi->q_vectors) {
7711 ret = -ENOMEM;
7712 goto err_vectors;
7713 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007714 }
7715 return ret;
7716
7717err_vectors:
7718 kfree(vsi->tx_rings);
7719 return ret;
7720}
7721
7722/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007723 * i40e_vsi_mem_alloc - Allocates the next available struct vsi in the PF
7724 * @pf: board private structure
7725 * @type: type of VSI
7726 *
7727 * On error: returns error code (negative)
7728 * On success: returns vsi index in PF (positive)
7729 **/
7730static int i40e_vsi_mem_alloc(struct i40e_pf *pf, enum i40e_vsi_type type)
7731{
7732 int ret = -ENODEV;
7733 struct i40e_vsi *vsi;
7734 int vsi_idx;
7735 int i;
7736
7737 /* Need to protect the allocation of the VSIs at the PF level */
7738 mutex_lock(&pf->switch_mutex);
7739
7740 /* VSI list may be fragmented if VSI creation/destruction has
7741 * been happening. We can afford to do a quick scan to look
7742 * for any free VSIs in the list.
7743 *
7744 * find next empty vsi slot, looping back around if necessary
7745 */
7746 i = pf->next_vsi;
Mitch Williams505682c2014-05-20 08:01:37 +00007747 while (i < pf->num_alloc_vsi && pf->vsi[i])
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007748 i++;
Mitch Williams505682c2014-05-20 08:01:37 +00007749 if (i >= pf->num_alloc_vsi) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007750 i = 0;
7751 while (i < pf->next_vsi && pf->vsi[i])
7752 i++;
7753 }
7754
Mitch Williams505682c2014-05-20 08:01:37 +00007755 if (i < pf->num_alloc_vsi && !pf->vsi[i]) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007756 vsi_idx = i; /* Found one! */
7757 } else {
7758 ret = -ENODEV;
Alexander Duyck493fb302013-09-28 07:01:44 +00007759 goto unlock_pf; /* out of VSI slots! */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007760 }
7761 pf->next_vsi = ++i;
7762
7763 vsi = kzalloc(sizeof(*vsi), GFP_KERNEL);
7764 if (!vsi) {
7765 ret = -ENOMEM;
Alexander Duyck493fb302013-09-28 07:01:44 +00007766 goto unlock_pf;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007767 }
7768 vsi->type = type;
7769 vsi->back = pf;
Jacob Keller0da36b92017-04-19 09:25:55 -04007770 set_bit(__I40E_VSI_DOWN, vsi->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007771 vsi->flags = 0;
7772 vsi->idx = vsi_idx;
Jesse Brandeburgac26fc12015-09-28 14:12:37 -04007773 vsi->int_rate_limit = 0;
Anjali Singhai Jain5db4cb52015-02-24 06:58:49 +00007774 vsi->rss_table_size = (vsi->type == I40E_VSI_MAIN) ?
7775 pf->rss_table_size : 64;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007776 vsi->netdev_registered = false;
7777 vsi->work_limit = I40E_DEFAULT_IRQ_WORK;
Jacob Keller278e7d02016-10-05 09:30:37 -07007778 hash_init(vsi->mac_filter_hash);
Shannon Nelson63741842014-04-23 04:50:16 +00007779 vsi->irqs_ready = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007780
Alexander Duyck9f65e152013-09-28 06:00:58 +00007781 ret = i40e_set_num_rings_in_vsi(vsi);
7782 if (ret)
7783 goto err_rings;
7784
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007785 ret = i40e_vsi_alloc_arrays(vsi, true);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007786 if (ret)
Alexander Duyck9f65e152013-09-28 06:00:58 +00007787 goto err_rings;
Alexander Duyck493fb302013-09-28 07:01:44 +00007788
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007789 /* Setup default MSIX irq handler for VSI */
7790 i40e_vsi_setup_irqhandler(vsi, i40e_msix_clean_rings);
7791
Kiran Patil21659032015-09-30 14:09:03 -04007792 /* Initialize VSI lock */
Jacob Keller278e7d02016-10-05 09:30:37 -07007793 spin_lock_init(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007794 pf->vsi[vsi_idx] = vsi;
7795 ret = vsi_idx;
Alexander Duyck493fb302013-09-28 07:01:44 +00007796 goto unlock_pf;
7797
Alexander Duyck9f65e152013-09-28 06:00:58 +00007798err_rings:
Alexander Duyck493fb302013-09-28 07:01:44 +00007799 pf->next_vsi = i - 1;
7800 kfree(vsi);
7801unlock_pf:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007802 mutex_unlock(&pf->switch_mutex);
7803 return ret;
7804}
7805
7806/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007807 * i40e_vsi_free_arrays - Free queue and vector pointer arrays for the VSI
7808 * @type: VSI pointer
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007809 * @free_qvectors: a bool to specify if q_vectors need to be freed.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007810 *
7811 * On error: returns error code (negative)
7812 * On success: returns 0
7813 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007814static void i40e_vsi_free_arrays(struct i40e_vsi *vsi, bool free_qvectors)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007815{
7816 /* free the ring and vector containers */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007817 if (free_qvectors) {
7818 kfree(vsi->q_vectors);
7819 vsi->q_vectors = NULL;
7820 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007821 kfree(vsi->tx_rings);
7822 vsi->tx_rings = NULL;
7823 vsi->rx_rings = NULL;
Björn Töpel74608d12017-05-24 07:55:35 +02007824 vsi->xdp_rings = NULL;
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007825}
7826
7827/**
Helin Zhang28c58692015-10-26 19:44:27 -04007828 * i40e_clear_rss_config_user - clear the user configured RSS hash keys
7829 * and lookup table
7830 * @vsi: Pointer to VSI structure
7831 */
7832static void i40e_clear_rss_config_user(struct i40e_vsi *vsi)
7833{
7834 if (!vsi)
7835 return;
7836
7837 kfree(vsi->rss_hkey_user);
7838 vsi->rss_hkey_user = NULL;
7839
7840 kfree(vsi->rss_lut_user);
7841 vsi->rss_lut_user = NULL;
7842}
7843
7844/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007845 * i40e_vsi_clear - Deallocate the VSI provided
7846 * @vsi: the VSI being un-configured
7847 **/
7848static int i40e_vsi_clear(struct i40e_vsi *vsi)
7849{
7850 struct i40e_pf *pf;
7851
7852 if (!vsi)
7853 return 0;
7854
7855 if (!vsi->back)
7856 goto free_vsi;
7857 pf = vsi->back;
7858
7859 mutex_lock(&pf->switch_mutex);
7860 if (!pf->vsi[vsi->idx]) {
7861 dev_err(&pf->pdev->dev, "pf->vsi[%d] is NULL, just free vsi[%d](%p,type %d)\n",
7862 vsi->idx, vsi->idx, vsi, vsi->type);
7863 goto unlock_vsi;
7864 }
7865
7866 if (pf->vsi[vsi->idx] != vsi) {
7867 dev_err(&pf->pdev->dev,
7868 "pf->vsi[%d](%p, type %d) != vsi[%d](%p,type %d): no free!\n",
7869 pf->vsi[vsi->idx]->idx,
7870 pf->vsi[vsi->idx],
7871 pf->vsi[vsi->idx]->type,
7872 vsi->idx, vsi, vsi->type);
7873 goto unlock_vsi;
7874 }
7875
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00007876 /* updates the PF for this cleared vsi */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007877 i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
7878 i40e_put_lump(pf->irq_pile, vsi->base_vector, vsi->idx);
7879
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007880 i40e_vsi_free_arrays(vsi, true);
Helin Zhang28c58692015-10-26 19:44:27 -04007881 i40e_clear_rss_config_user(vsi);
Alexander Duyck493fb302013-09-28 07:01:44 +00007882
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007883 pf->vsi[vsi->idx] = NULL;
7884 if (vsi->idx < pf->next_vsi)
7885 pf->next_vsi = vsi->idx;
7886
7887unlock_vsi:
7888 mutex_unlock(&pf->switch_mutex);
7889free_vsi:
7890 kfree(vsi);
7891
7892 return 0;
7893}
7894
7895/**
Alexander Duyck9f65e152013-09-28 06:00:58 +00007896 * i40e_vsi_clear_rings - Deallocates the Rx and Tx rings for the provided VSI
7897 * @vsi: the VSI being cleaned
7898 **/
Shannon Nelsonbe1d5ee2013-11-28 06:39:23 +00007899static void i40e_vsi_clear_rings(struct i40e_vsi *vsi)
Alexander Duyck9f65e152013-09-28 06:00:58 +00007900{
7901 int i;
7902
Greg Rose8e9dca52013-12-18 13:45:53 +00007903 if (vsi->tx_rings && vsi->tx_rings[0]) {
Neerav Parikhd7397642013-11-28 06:39:37 +00007904 for (i = 0; i < vsi->alloc_queue_pairs; i++) {
Mitch Williams00403f02013-09-28 07:13:13 +00007905 kfree_rcu(vsi->tx_rings[i], rcu);
7906 vsi->tx_rings[i] = NULL;
7907 vsi->rx_rings[i] = NULL;
Björn Töpel74608d12017-05-24 07:55:35 +02007908 if (vsi->xdp_rings)
7909 vsi->xdp_rings[i] = NULL;
Mitch Williams00403f02013-09-28 07:13:13 +00007910 }
Shannon Nelsonbe1d5ee2013-11-28 06:39:23 +00007911 }
Alexander Duyck9f65e152013-09-28 06:00:58 +00007912}
7913
7914/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007915 * i40e_alloc_rings - Allocates the Rx and Tx rings for the provided VSI
7916 * @vsi: the VSI being configured
7917 **/
7918static int i40e_alloc_rings(struct i40e_vsi *vsi)
7919{
Björn Töpel74608d12017-05-24 07:55:35 +02007920 int i, qpv = i40e_enabled_xdp_vsi(vsi) ? 3 : 2;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007921 struct i40e_pf *pf = vsi->back;
Björn Töpel74608d12017-05-24 07:55:35 +02007922 struct i40e_ring *ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007923
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007924 /* Set basic values in the rings to be used later during open() */
Neerav Parikhd7397642013-11-28 06:39:37 +00007925 for (i = 0; i < vsi->alloc_queue_pairs; i++) {
Shannon Nelsonac6c5e32013-11-20 10:02:57 +00007926 /* allocate space for both Tx and Rx in one shot */
Björn Töpel74608d12017-05-24 07:55:35 +02007927 ring = kcalloc(qpv, sizeof(struct i40e_ring), GFP_KERNEL);
7928 if (!ring)
Alexander Duyck9f65e152013-09-28 06:00:58 +00007929 goto err_out;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007930
Björn Töpel74608d12017-05-24 07:55:35 +02007931 ring->queue_index = i;
7932 ring->reg_idx = vsi->base_queue + i;
7933 ring->ring_active = false;
7934 ring->vsi = vsi;
7935 ring->netdev = vsi->netdev;
7936 ring->dev = &pf->pdev->dev;
7937 ring->count = vsi->num_desc;
7938 ring->size = 0;
7939 ring->dcb_tc = 0;
Jacob Kellerd36e41d2017-06-23 04:24:46 -04007940 if (vsi->back->hw_features & I40E_HW_WB_ON_ITR_CAPABLE)
Björn Töpel74608d12017-05-24 07:55:35 +02007941 ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
7942 ring->tx_itr_setting = pf->tx_itr_default;
7943 vsi->tx_rings[i] = ring++;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007944
Björn Töpel74608d12017-05-24 07:55:35 +02007945 if (!i40e_enabled_xdp_vsi(vsi))
7946 goto setup_rx;
7947
7948 ring->queue_index = vsi->alloc_queue_pairs + i;
7949 ring->reg_idx = vsi->base_queue + ring->queue_index;
7950 ring->ring_active = false;
7951 ring->vsi = vsi;
7952 ring->netdev = NULL;
7953 ring->dev = &pf->pdev->dev;
7954 ring->count = vsi->num_desc;
7955 ring->size = 0;
7956 ring->dcb_tc = 0;
Jacob Kellerd36e41d2017-06-23 04:24:46 -04007957 if (vsi->back->hw_features & I40E_HW_WB_ON_ITR_CAPABLE)
Björn Töpel74608d12017-05-24 07:55:35 +02007958 ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
7959 set_ring_xdp(ring);
7960 ring->tx_itr_setting = pf->tx_itr_default;
7961 vsi->xdp_rings[i] = ring++;
7962
7963setup_rx:
7964 ring->queue_index = i;
7965 ring->reg_idx = vsi->base_queue + i;
7966 ring->ring_active = false;
7967 ring->vsi = vsi;
7968 ring->netdev = vsi->netdev;
7969 ring->dev = &pf->pdev->dev;
7970 ring->count = vsi->num_desc;
7971 ring->size = 0;
7972 ring->dcb_tc = 0;
7973 ring->rx_itr_setting = pf->rx_itr_default;
7974 vsi->rx_rings[i] = ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007975 }
7976
7977 return 0;
Alexander Duyck9f65e152013-09-28 06:00:58 +00007978
7979err_out:
7980 i40e_vsi_clear_rings(vsi);
7981 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007982}
7983
7984/**
7985 * i40e_reserve_msix_vectors - Reserve MSI-X vectors in the kernel
7986 * @pf: board private structure
7987 * @vectors: the number of MSI-X vectors to request
7988 *
7989 * Returns the number of vectors reserved, or error
7990 **/
7991static int i40e_reserve_msix_vectors(struct i40e_pf *pf, int vectors)
7992{
Alexander Gordeev7b37f372014-02-18 11:11:42 +01007993 vectors = pci_enable_msix_range(pf->pdev, pf->msix_entries,
7994 I40E_MIN_MSIX, vectors);
7995 if (vectors < 0) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007996 dev_info(&pf->pdev->dev,
Alexander Gordeev7b37f372014-02-18 11:11:42 +01007997 "MSI-X vector reservation failed: %d\n", vectors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007998 vectors = 0;
7999 }
8000
8001 return vectors;
8002}
8003
8004/**
8005 * i40e_init_msix - Setup the MSIX capability
8006 * @pf: board private structure
8007 *
8008 * Work with the OS to set up the MSIX vectors needed.
8009 *
Shannon Nelson3b444392015-02-26 16:15:57 +00008010 * Returns the number of vectors reserved or negative on failure
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008011 **/
8012static int i40e_init_msix(struct i40e_pf *pf)
8013{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008014 struct i40e_hw *hw = &pf->hw;
Jacob Kellerc0cf70a2017-01-24 10:24:01 -08008015 int cpus, extra_vectors;
Shannon Nelson1e200e42015-02-27 09:15:24 +00008016 int vectors_left;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008017 int v_budget, i;
Shannon Nelson3b444392015-02-26 16:15:57 +00008018 int v_actual;
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06008019 int iwarp_requested = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008020
8021 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
8022 return -ENODEV;
8023
8024 /* The number of vectors we'll request will be comprised of:
8025 * - Add 1 for "other" cause for Admin Queue events, etc.
8026 * - The number of LAN queue pairs
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008027 * - Queues being used for RSS.
8028 * We don't need as many as max_rss_size vectors.
8029 * use rss_size instead in the calculation since that
8030 * is governed by number of cpus in the system.
8031 * - assumes symmetric Tx/Rx pairing
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008032 * - The number of VMDq pairs
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06008033 * - The CPU count within the NUMA node if iWARP is enabled
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008034 * Once we count this up, try the request.
8035 *
8036 * If we can't get what we want, we'll simplify to nearly nothing
8037 * and try again. If that still fails, we punt.
8038 */
Shannon Nelson1e200e42015-02-27 09:15:24 +00008039 vectors_left = hw->func_caps.num_msix_vectors;
8040 v_budget = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008041
Shannon Nelson1e200e42015-02-27 09:15:24 +00008042 /* reserve one vector for miscellaneous handler */
8043 if (vectors_left) {
8044 v_budget++;
8045 vectors_left--;
8046 }
8047
Jacob Kellerc0cf70a2017-01-24 10:24:01 -08008048 /* reserve some vectors for the main PF traffic queues. Initially we
8049 * only reserve at most 50% of the available vectors, in the case that
8050 * the number of online CPUs is large. This ensures that we can enable
8051 * extra features as well. Once we've enabled the other features, we
8052 * will use any remaining vectors to reach as close as we can to the
8053 * number of online CPUs.
8054 */
8055 cpus = num_online_cpus();
8056 pf->num_lan_msix = min_t(int, cpus, vectors_left / 2);
Shannon Nelson1e200e42015-02-27 09:15:24 +00008057 vectors_left -= pf->num_lan_msix;
Shannon Nelson1e200e42015-02-27 09:15:24 +00008058
8059 /* reserve one vector for sideband flow director */
8060 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
8061 if (vectors_left) {
Tushar Davea70e4072016-05-16 12:40:53 -07008062 pf->num_fdsb_msix = 1;
Shannon Nelson1e200e42015-02-27 09:15:24 +00008063 v_budget++;
8064 vectors_left--;
8065 } else {
Tushar Davea70e4072016-05-16 12:40:53 -07008066 pf->num_fdsb_msix = 0;
Shannon Nelson1e200e42015-02-27 09:15:24 +00008067 }
8068 }
John W Linville83840e42015-01-14 03:06:28 +00008069
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06008070 /* can we reserve enough for iWARP? */
8071 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02008072 iwarp_requested = pf->num_iwarp_msix;
8073
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06008074 if (!vectors_left)
8075 pf->num_iwarp_msix = 0;
8076 else if (vectors_left < pf->num_iwarp_msix)
8077 pf->num_iwarp_msix = 1;
8078 v_budget += pf->num_iwarp_msix;
8079 vectors_left -= pf->num_iwarp_msix;
8080 }
8081
Shannon Nelson1e200e42015-02-27 09:15:24 +00008082 /* any vectors left over go for VMDq support */
8083 if (pf->flags & I40E_FLAG_VMDQ_ENABLED) {
8084 int vmdq_vecs_wanted = pf->num_vmdq_vsis * pf->num_vmdq_qps;
8085 int vmdq_vecs = min_t(int, vectors_left, vmdq_vecs_wanted);
8086
Stefan Assmann9ca57e92016-09-19 13:37:49 +02008087 if (!vectors_left) {
8088 pf->num_vmdq_msix = 0;
8089 pf->num_vmdq_qps = 0;
8090 } else {
8091 /* if we're short on vectors for what's desired, we limit
8092 * the queues per vmdq. If this is still more than are
8093 * available, the user will need to change the number of
8094 * queues/vectors used by the PF later with the ethtool
8095 * channels command
8096 */
8097 if (vmdq_vecs < vmdq_vecs_wanted)
8098 pf->num_vmdq_qps = 1;
8099 pf->num_vmdq_msix = pf->num_vmdq_qps;
Shannon Nelson1e200e42015-02-27 09:15:24 +00008100
Stefan Assmann9ca57e92016-09-19 13:37:49 +02008101 v_budget += vmdq_vecs;
8102 vectors_left -= vmdq_vecs;
8103 }
Shannon Nelson1e200e42015-02-27 09:15:24 +00008104 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008105
Jacob Kellerc0cf70a2017-01-24 10:24:01 -08008106 /* On systems with a large number of SMP cores, we previously limited
8107 * the number of vectors for num_lan_msix to be at most 50% of the
8108 * available vectors, to allow for other features. Now, we add back
8109 * the remaining vectors. However, we ensure that the total
8110 * num_lan_msix will not exceed num_online_cpus(). To do this, we
8111 * calculate the number of vectors we can add without going over the
8112 * cap of CPUs. For systems with a small number of CPUs this will be
8113 * zero.
8114 */
8115 extra_vectors = min_t(int, cpus - pf->num_lan_msix, vectors_left);
8116 pf->num_lan_msix += extra_vectors;
8117 vectors_left -= extra_vectors;
8118
8119 WARN(vectors_left < 0,
8120 "Calculation of remaining vectors underflowed. This is an accounting bug when determining total MSI-X vectors.\n");
8121
8122 v_budget += pf->num_lan_msix;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008123 pf->msix_entries = kcalloc(v_budget, sizeof(struct msix_entry),
8124 GFP_KERNEL);
8125 if (!pf->msix_entries)
8126 return -ENOMEM;
8127
8128 for (i = 0; i < v_budget; i++)
8129 pf->msix_entries[i].entry = i;
Shannon Nelson3b444392015-02-26 16:15:57 +00008130 v_actual = i40e_reserve_msix_vectors(pf, v_budget);
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00008131
Shannon Nelson3b444392015-02-26 16:15:57 +00008132 if (v_actual < I40E_MIN_MSIX) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008133 pf->flags &= ~I40E_FLAG_MSIX_ENABLED;
8134 kfree(pf->msix_entries);
8135 pf->msix_entries = NULL;
Guilherme G Piccoli4c95aa52016-09-22 10:03:58 -03008136 pci_disable_msix(pf->pdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008137 return -ENODEV;
8138
Shannon Nelson3b444392015-02-26 16:15:57 +00008139 } else if (v_actual == I40E_MIN_MSIX) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008140 /* Adjust for minimal MSIX use */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008141 pf->num_vmdq_vsis = 0;
8142 pf->num_vmdq_qps = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008143 pf->num_lan_qps = 1;
8144 pf->num_lan_msix = 1;
8145
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02008146 } else if (!vectors_left) {
8147 /* If we have limited resources, we will start with no vectors
8148 * for the special features and then allocate vectors to some
8149 * of these features based on the policy and at the end disable
8150 * the features that did not get any vectors.
8151 */
Shannon Nelson3b444392015-02-26 16:15:57 +00008152 int vec;
8153
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02008154 dev_info(&pf->pdev->dev,
8155 "MSI-X vector limit reached, attempting to redistribute vectors\n");
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00008156 /* reserve the misc vector */
Shannon Nelson3b444392015-02-26 16:15:57 +00008157 vec = v_actual - 1;
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00008158
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008159 /* Scale vector usage down */
8160 pf->num_vmdq_msix = 1; /* force VMDqs to only one vector */
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00008161 pf->num_vmdq_vsis = 1;
Shannon Nelson1e200e42015-02-27 09:15:24 +00008162 pf->num_vmdq_qps = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008163
8164 /* partition out the remaining vectors */
8165 switch (vec) {
8166 case 2:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008167 pf->num_lan_msix = 1;
8168 break;
8169 case 3:
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06008170 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
8171 pf->num_lan_msix = 1;
8172 pf->num_iwarp_msix = 1;
8173 } else {
8174 pf->num_lan_msix = 2;
8175 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008176 break;
8177 default:
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06008178 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
8179 pf->num_iwarp_msix = min_t(int, (vec / 3),
8180 iwarp_requested);
8181 pf->num_vmdq_vsis = min_t(int, (vec / 3),
8182 I40E_DEFAULT_NUM_VMDQ_VSI);
8183 } else {
8184 pf->num_vmdq_vsis = min_t(int, (vec / 2),
8185 I40E_DEFAULT_NUM_VMDQ_VSI);
8186 }
Stefan Assmannabd97a92016-09-19 13:37:51 +02008187 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
8188 pf->num_fdsb_msix = 1;
8189 vec--;
8190 }
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06008191 pf->num_lan_msix = min_t(int,
8192 (vec - (pf->num_iwarp_msix + pf->num_vmdq_vsis)),
8193 pf->num_lan_msix);
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02008194 pf->num_lan_qps = pf->num_lan_msix;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008195 break;
8196 }
8197 }
8198
Stefan Assmannabd97a92016-09-19 13:37:51 +02008199 if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
8200 (pf->num_fdsb_msix == 0)) {
8201 dev_info(&pf->pdev->dev, "Sideband Flowdir disabled, not enough MSI-X vectors\n");
8202 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
8203 }
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00008204 if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
8205 (pf->num_vmdq_msix == 0)) {
8206 dev_info(&pf->pdev->dev, "VMDq disabled, not enough MSI-X vectors\n");
8207 pf->flags &= ~I40E_FLAG_VMDQ_ENABLED;
8208 }
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06008209
8210 if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
8211 (pf->num_iwarp_msix == 0)) {
8212 dev_info(&pf->pdev->dev, "IWARP disabled, not enough MSI-X vectors\n");
8213 pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
8214 }
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02008215 i40e_debug(&pf->hw, I40E_DEBUG_INIT,
8216 "MSI-X vector distribution: PF %d, VMDq %d, FDSB %d, iWARP %d\n",
8217 pf->num_lan_msix,
8218 pf->num_vmdq_msix * pf->num_vmdq_vsis,
8219 pf->num_fdsb_msix,
8220 pf->num_iwarp_msix);
8221
Shannon Nelson3b444392015-02-26 16:15:57 +00008222 return v_actual;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008223}
8224
8225/**
Greg Rose90e04072014-03-06 08:59:57 +00008226 * i40e_vsi_alloc_q_vector - Allocate memory for a single interrupt vector
Alexander Duyck493fb302013-09-28 07:01:44 +00008227 * @vsi: the VSI being configured
8228 * @v_idx: index of the vector in the vsi struct
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03008229 * @cpu: cpu to be used on affinity_mask
Alexander Duyck493fb302013-09-28 07:01:44 +00008230 *
8231 * We allocate one q_vector. If allocation fails we return -ENOMEM.
8232 **/
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03008233static int i40e_vsi_alloc_q_vector(struct i40e_vsi *vsi, int v_idx, int cpu)
Alexander Duyck493fb302013-09-28 07:01:44 +00008234{
8235 struct i40e_q_vector *q_vector;
8236
8237 /* allocate q_vector */
8238 q_vector = kzalloc(sizeof(struct i40e_q_vector), GFP_KERNEL);
8239 if (!q_vector)
8240 return -ENOMEM;
8241
8242 q_vector->vsi = vsi;
8243 q_vector->v_idx = v_idx;
Jacob Keller759dc4a2017-07-14 09:10:10 -04008244 cpumask_copy(&q_vector->affinity_mask, cpu_possible_mask);
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03008245
Alexander Duyck493fb302013-09-28 07:01:44 +00008246 if (vsi->netdev)
8247 netif_napi_add(vsi->netdev, &q_vector->napi,
Jesse Brandeburgeefeace2014-05-10 04:49:13 +00008248 i40e_napi_poll, NAPI_POLL_WEIGHT);
Alexander Duyck493fb302013-09-28 07:01:44 +00008249
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00008250 q_vector->rx.latency_range = I40E_LOW_LATENCY;
8251 q_vector->tx.latency_range = I40E_LOW_LATENCY;
8252
Alexander Duyck493fb302013-09-28 07:01:44 +00008253 /* tie q_vector and vsi together */
8254 vsi->q_vectors[v_idx] = q_vector;
8255
8256 return 0;
8257}
8258
8259/**
Greg Rose90e04072014-03-06 08:59:57 +00008260 * i40e_vsi_alloc_q_vectors - Allocate memory for interrupt vectors
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008261 * @vsi: the VSI being configured
8262 *
8263 * We allocate one q_vector per queue interrupt. If allocation fails we
8264 * return -ENOMEM.
8265 **/
Greg Rose90e04072014-03-06 08:59:57 +00008266static int i40e_vsi_alloc_q_vectors(struct i40e_vsi *vsi)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008267{
8268 struct i40e_pf *pf = vsi->back;
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03008269 int err, v_idx, num_q_vectors, current_cpu;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008270
8271 /* if not MSIX, give the one vector only to the LAN VSI */
8272 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
8273 num_q_vectors = vsi->num_q_vectors;
8274 else if (vsi == pf->vsi[pf->lan_vsi])
8275 num_q_vectors = 1;
8276 else
8277 return -EINVAL;
8278
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03008279 current_cpu = cpumask_first(cpu_online_mask);
8280
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008281 for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03008282 err = i40e_vsi_alloc_q_vector(vsi, v_idx, current_cpu);
Alexander Duyck493fb302013-09-28 07:01:44 +00008283 if (err)
8284 goto err_out;
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03008285 current_cpu = cpumask_next(current_cpu, cpu_online_mask);
8286 if (unlikely(current_cpu >= nr_cpu_ids))
8287 current_cpu = cpumask_first(cpu_online_mask);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008288 }
8289
8290 return 0;
Alexander Duyck493fb302013-09-28 07:01:44 +00008291
8292err_out:
8293 while (v_idx--)
8294 i40e_free_q_vector(vsi, v_idx);
8295
8296 return err;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008297}
8298
8299/**
8300 * i40e_init_interrupt_scheme - Determine proper interrupt scheme
8301 * @pf: board private structure to initialize
8302 **/
Jesse Brandeburgc11472802015-04-07 19:45:39 -04008303static int i40e_init_interrupt_scheme(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008304{
Shannon Nelson3b444392015-02-26 16:15:57 +00008305 int vectors = 0;
8306 ssize_t size;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008307
8308 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
Shannon Nelson3b444392015-02-26 16:15:57 +00008309 vectors = i40e_init_msix(pf);
8310 if (vectors < 0) {
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08008311 pf->flags &= ~(I40E_FLAG_MSIX_ENABLED |
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06008312 I40E_FLAG_IWARP_ENABLED |
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08008313 I40E_FLAG_RSS_ENABLED |
Neerav Parikh4d9b6042014-05-22 06:31:51 +00008314 I40E_FLAG_DCB_CAPABLE |
Dave Ertmana0362442016-08-29 17:38:26 -07008315 I40E_FLAG_DCB_ENABLED |
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08008316 I40E_FLAG_SRIOV_ENABLED |
8317 I40E_FLAG_FD_SB_ENABLED |
8318 I40E_FLAG_FD_ATR_ENABLED |
8319 I40E_FLAG_VMDQ_ENABLED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008320
8321 /* rework the queue expectations without MSIX */
8322 i40e_determine_queue_usage(pf);
8323 }
8324 }
8325
8326 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED) &&
8327 (pf->flags & I40E_FLAG_MSI_ENABLED)) {
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08008328 dev_info(&pf->pdev->dev, "MSI-X not available, trying MSI\n");
Shannon Nelson3b444392015-02-26 16:15:57 +00008329 vectors = pci_enable_msi(pf->pdev);
8330 if (vectors < 0) {
8331 dev_info(&pf->pdev->dev, "MSI init failed - %d\n",
8332 vectors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008333 pf->flags &= ~I40E_FLAG_MSI_ENABLED;
8334 }
Shannon Nelson3b444392015-02-26 16:15:57 +00008335 vectors = 1; /* one MSI or Legacy vector */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008336 }
8337
Shannon Nelson958a3e32013-09-28 07:13:28 +00008338 if (!(pf->flags & (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED)))
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08008339 dev_info(&pf->pdev->dev, "MSI-X and MSI not available, falling back to Legacy IRQ\n");
Shannon Nelson958a3e32013-09-28 07:13:28 +00008340
Shannon Nelson3b444392015-02-26 16:15:57 +00008341 /* set up vector assignment tracking */
8342 size = sizeof(struct i40e_lump_tracking) + (sizeof(u16) * vectors);
8343 pf->irq_pile = kzalloc(size, GFP_KERNEL);
Jesse Brandeburgc11472802015-04-07 19:45:39 -04008344 if (!pf->irq_pile) {
8345 dev_err(&pf->pdev->dev, "error allocating irq_pile memory\n");
8346 return -ENOMEM;
8347 }
Shannon Nelson3b444392015-02-26 16:15:57 +00008348 pf->irq_pile->num_entries = vectors;
8349 pf->irq_pile->search_hint = 0;
8350
Jesse Brandeburgc11472802015-04-07 19:45:39 -04008351 /* track first vector for misc interrupts, ignore return */
Shannon Nelson3b444392015-02-26 16:15:57 +00008352 (void)i40e_get_lump(pf, pf->irq_pile, 1, I40E_PILE_VALID_BIT - 1);
Jesse Brandeburgc11472802015-04-07 19:45:39 -04008353
8354 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008355}
8356
Jacob Kellerb980c062017-07-14 09:27:06 -04008357#ifdef CONFIG_PM
8358/**
8359 * i40e_restore_interrupt_scheme - Restore the interrupt scheme
8360 * @pf: private board data structure
8361 *
8362 * Restore the interrupt scheme that was cleared when we suspended the
8363 * device. This should be called during resume to re-allocate the q_vectors
8364 * and reacquire IRQs.
8365 */
8366static int i40e_restore_interrupt_scheme(struct i40e_pf *pf)
8367{
8368 int err, i;
8369
8370 /* We cleared the MSI and MSI-X flags when disabling the old interrupt
8371 * scheme. We need to re-enabled them here in order to attempt to
8372 * re-acquire the MSI or MSI-X vectors
8373 */
8374 pf->flags |= (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
8375
8376 err = i40e_init_interrupt_scheme(pf);
8377 if (err)
8378 return err;
8379
8380 /* Now that we've re-acquired IRQs, we need to remap the vectors and
8381 * rings together again.
8382 */
8383 for (i = 0; i < pf->num_alloc_vsi; i++) {
8384 if (pf->vsi[i]) {
8385 err = i40e_vsi_alloc_q_vectors(pf->vsi[i]);
8386 if (err)
8387 goto err_unwind;
8388 i40e_vsi_map_rings_to_vectors(pf->vsi[i]);
8389 }
8390 }
8391
8392 err = i40e_setup_misc_vector(pf);
8393 if (err)
8394 goto err_unwind;
8395
8396 return 0;
8397
8398err_unwind:
8399 while (i--) {
8400 if (pf->vsi[i])
8401 i40e_vsi_free_q_vectors(pf->vsi[i]);
8402 }
8403
8404 return err;
8405}
8406#endif /* CONFIG_PM */
8407
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008408/**
8409 * i40e_setup_misc_vector - Setup the misc vector to handle non queue events
8410 * @pf: board private structure
8411 *
8412 * This sets up the handler for MSIX 0, which is used to manage the
8413 * non-queue interrupts, e.g. AdminQ and errors. This is not used
8414 * when in MSI or Legacy interrupt mode.
8415 **/
8416static int i40e_setup_misc_vector(struct i40e_pf *pf)
8417{
8418 struct i40e_hw *hw = &pf->hw;
8419 int err = 0;
8420
Jacob Kellerc17401a2017-07-14 09:27:02 -04008421 /* Only request the IRQ once, the first time through. */
8422 if (!test_and_set_bit(__I40E_MISC_IRQ_REQUESTED, pf->state)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008423 err = request_irq(pf->msix_entries[0].vector,
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00008424 i40e_intr, 0, pf->int_name, pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008425 if (err) {
Jacob Kellerc17401a2017-07-14 09:27:02 -04008426 clear_bit(__I40E_MISC_IRQ_REQUESTED, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008427 dev_info(&pf->pdev->dev,
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08008428 "request_irq for %s failed: %d\n",
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00008429 pf->int_name, err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008430 return -EFAULT;
8431 }
8432 }
8433
Jacob Kellerab437b52014-12-14 01:55:08 +00008434 i40e_enable_misc_int_causes(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008435
8436 /* associate no queues to the misc vector */
8437 wr32(hw, I40E_PFINT_LNKLST0, I40E_QUEUE_END_OF_LIST);
8438 wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), I40E_ITR_8K);
8439
8440 i40e_flush(hw);
8441
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08008442 i40e_irq_dynamic_enable_icr0(pf, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008443
8444 return err;
8445}
8446
8447/**
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008448 * i40e_config_rss_aq - Prepare for RSS using AQ commands
8449 * @vsi: vsi structure
8450 * @seed: RSS hash seed
8451 **/
Helin Zhange69ff812015-10-21 19:56:22 -04008452static int i40e_config_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
8453 u8 *lut, u16 lut_size)
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008454{
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008455 struct i40e_pf *pf = vsi->back;
8456 struct i40e_hw *hw = &pf->hw;
Jacob Keller776b2e12016-07-19 16:23:30 -07008457 int ret = 0;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008458
Jacob Keller776b2e12016-07-19 16:23:30 -07008459 if (seed) {
8460 struct i40e_aqc_get_set_rss_key_data *seed_dw =
8461 (struct i40e_aqc_get_set_rss_key_data *)seed;
8462 ret = i40e_aq_set_rss_key(hw, vsi->id, seed_dw);
8463 if (ret) {
8464 dev_info(&pf->pdev->dev,
8465 "Cannot set RSS key, err %s aq_err %s\n",
8466 i40e_stat_str(hw, ret),
8467 i40e_aq_str(hw, hw->aq.asq_last_status));
8468 return ret;
8469 }
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008470 }
Jacob Keller776b2e12016-07-19 16:23:30 -07008471 if (lut) {
8472 bool pf_lut = vsi->type == I40E_VSI_MAIN ? true : false;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008473
Jacob Keller776b2e12016-07-19 16:23:30 -07008474 ret = i40e_aq_set_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
8475 if (ret) {
8476 dev_info(&pf->pdev->dev,
8477 "Cannot set RSS lut, err %s aq_err %s\n",
8478 i40e_stat_str(hw, ret),
8479 i40e_aq_str(hw, hw->aq.asq_last_status));
8480 return ret;
8481 }
8482 }
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008483 return ret;
8484}
8485
8486/**
Anjali Singhai Jain95a73782015-12-22 14:25:04 -08008487 * i40e_get_rss_aq - Get RSS keys and lut by using AQ commands
8488 * @vsi: Pointer to vsi structure
8489 * @seed: Buffter to store the hash keys
8490 * @lut: Buffer to store the lookup table entries
8491 * @lut_size: Size of buffer to store the lookup table entries
8492 *
8493 * Return 0 on success, negative on failure
8494 */
8495static int i40e_get_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
8496 u8 *lut, u16 lut_size)
8497{
8498 struct i40e_pf *pf = vsi->back;
8499 struct i40e_hw *hw = &pf->hw;
8500 int ret = 0;
8501
8502 if (seed) {
8503 ret = i40e_aq_get_rss_key(hw, vsi->id,
8504 (struct i40e_aqc_get_set_rss_key_data *)seed);
8505 if (ret) {
8506 dev_info(&pf->pdev->dev,
8507 "Cannot get RSS key, err %s aq_err %s\n",
8508 i40e_stat_str(&pf->hw, ret),
8509 i40e_aq_str(&pf->hw,
8510 pf->hw.aq.asq_last_status));
8511 return ret;
8512 }
8513 }
8514
8515 if (lut) {
8516 bool pf_lut = vsi->type == I40E_VSI_MAIN ? true : false;
8517
8518 ret = i40e_aq_get_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
8519 if (ret) {
8520 dev_info(&pf->pdev->dev,
8521 "Cannot get RSS lut, err %s aq_err %s\n",
8522 i40e_stat_str(&pf->hw, ret),
8523 i40e_aq_str(&pf->hw,
8524 pf->hw.aq.asq_last_status));
8525 return ret;
8526 }
8527 }
8528
8529 return ret;
8530}
8531
8532/**
Jacob Keller0582b962016-07-19 16:23:29 -07008533 * i40e_vsi_config_rss - Prepare for VSI(VMDq) RSS if used
8534 * @vsi: VSI structure
8535 **/
8536static int i40e_vsi_config_rss(struct i40e_vsi *vsi)
8537{
8538 u8 seed[I40E_HKEY_ARRAY_SIZE];
8539 struct i40e_pf *pf = vsi->back;
8540 u8 *lut;
8541 int ret;
8542
Jacob Kellerd36e41d2017-06-23 04:24:46 -04008543 if (!(pf->hw_features & I40E_HW_RSS_AQ_CAPABLE))
Jacob Keller0582b962016-07-19 16:23:29 -07008544 return 0;
8545
Jacob Keller552b9962016-07-19 16:23:31 -07008546 if (!vsi->rss_size)
8547 vsi->rss_size = min_t(int, pf->alloc_rss_size,
8548 vsi->num_queue_pairs);
8549 if (!vsi->rss_size)
8550 return -EINVAL;
8551
Jacob Keller0582b962016-07-19 16:23:29 -07008552 lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
8553 if (!lut)
8554 return -ENOMEM;
Jacob Keller552b9962016-07-19 16:23:31 -07008555 /* Use the user configured hash keys and lookup table if there is one,
8556 * otherwise use default
8557 */
8558 if (vsi->rss_lut_user)
8559 memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
8560 else
8561 i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
8562 if (vsi->rss_hkey_user)
8563 memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
8564 else
8565 netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
Jacob Keller0582b962016-07-19 16:23:29 -07008566 ret = i40e_config_rss_aq(vsi, seed, lut, vsi->rss_table_size);
8567 kfree(lut);
8568
8569 return ret;
8570}
8571
8572/**
Helin Zhang043dd652015-10-21 19:56:23 -04008573 * i40e_config_rss_reg - Configure RSS keys and lut by writing registers
Helin Zhange69ff812015-10-21 19:56:22 -04008574 * @vsi: Pointer to vsi structure
8575 * @seed: RSS hash seed
8576 * @lut: Lookup table
8577 * @lut_size: Lookup table size
8578 *
8579 * Returns 0 on success, negative on failure
8580 **/
8581static int i40e_config_rss_reg(struct i40e_vsi *vsi, const u8 *seed,
8582 const u8 *lut, u16 lut_size)
8583{
8584 struct i40e_pf *pf = vsi->back;
8585 struct i40e_hw *hw = &pf->hw;
Mitch Williamsc4e18682016-04-12 08:30:40 -07008586 u16 vf_id = vsi->vf_id;
Helin Zhange69ff812015-10-21 19:56:22 -04008587 u8 i;
8588
8589 /* Fill out hash function seed */
8590 if (seed) {
8591 u32 *seed_dw = (u32 *)seed;
8592
Mitch Williamsc4e18682016-04-12 08:30:40 -07008593 if (vsi->type == I40E_VSI_MAIN) {
8594 for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
Lihong Yang26f77e52017-01-30 12:29:33 -08008595 wr32(hw, I40E_PFQF_HKEY(i), seed_dw[i]);
Mitch Williamsc4e18682016-04-12 08:30:40 -07008596 } else if (vsi->type == I40E_VSI_SRIOV) {
8597 for (i = 0; i <= I40E_VFQF_HKEY1_MAX_INDEX; i++)
Lihong Yang26f77e52017-01-30 12:29:33 -08008598 wr32(hw, I40E_VFQF_HKEY1(i, vf_id), seed_dw[i]);
Mitch Williamsc4e18682016-04-12 08:30:40 -07008599 } else {
8600 dev_err(&pf->pdev->dev, "Cannot set RSS seed - invalid VSI type\n");
8601 }
Helin Zhange69ff812015-10-21 19:56:22 -04008602 }
8603
8604 if (lut) {
8605 u32 *lut_dw = (u32 *)lut;
8606
Mitch Williamsc4e18682016-04-12 08:30:40 -07008607 if (vsi->type == I40E_VSI_MAIN) {
8608 if (lut_size != I40E_HLUT_ARRAY_SIZE)
8609 return -EINVAL;
8610 for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
8611 wr32(hw, I40E_PFQF_HLUT(i), lut_dw[i]);
8612 } else if (vsi->type == I40E_VSI_SRIOV) {
8613 if (lut_size != I40E_VF_HLUT_ARRAY_SIZE)
8614 return -EINVAL;
8615 for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
Lihong Yang26f77e52017-01-30 12:29:33 -08008616 wr32(hw, I40E_VFQF_HLUT1(i, vf_id), lut_dw[i]);
Mitch Williamsc4e18682016-04-12 08:30:40 -07008617 } else {
8618 dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
8619 }
Helin Zhange69ff812015-10-21 19:56:22 -04008620 }
8621 i40e_flush(hw);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008622
8623 return 0;
8624}
8625
8626/**
Helin Zhang043dd652015-10-21 19:56:23 -04008627 * i40e_get_rss_reg - Get the RSS keys and lut by reading registers
8628 * @vsi: Pointer to VSI structure
8629 * @seed: Buffer to store the keys
8630 * @lut: Buffer to store the lookup table entries
8631 * @lut_size: Size of buffer to store the lookup table entries
8632 *
8633 * Returns 0 on success, negative on failure
8634 */
8635static int i40e_get_rss_reg(struct i40e_vsi *vsi, u8 *seed,
8636 u8 *lut, u16 lut_size)
8637{
8638 struct i40e_pf *pf = vsi->back;
8639 struct i40e_hw *hw = &pf->hw;
8640 u16 i;
8641
8642 if (seed) {
8643 u32 *seed_dw = (u32 *)seed;
8644
8645 for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
Shannon Nelson272cdaf22016-02-17 16:12:21 -08008646 seed_dw[i] = i40e_read_rx_ctl(hw, I40E_PFQF_HKEY(i));
Helin Zhang043dd652015-10-21 19:56:23 -04008647 }
8648 if (lut) {
8649 u32 *lut_dw = (u32 *)lut;
8650
8651 if (lut_size != I40E_HLUT_ARRAY_SIZE)
8652 return -EINVAL;
8653 for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
8654 lut_dw[i] = rd32(hw, I40E_PFQF_HLUT(i));
8655 }
8656
8657 return 0;
8658}
8659
8660/**
8661 * i40e_config_rss - Configure RSS keys and lut
8662 * @vsi: Pointer to VSI structure
8663 * @seed: RSS hash seed
8664 * @lut: Lookup table
8665 * @lut_size: Lookup table size
8666 *
8667 * Returns 0 on success, negative on failure
8668 */
8669int i40e_config_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
8670{
8671 struct i40e_pf *pf = vsi->back;
8672
Jacob Kellerd36e41d2017-06-23 04:24:46 -04008673 if (pf->hw_features & I40E_HW_RSS_AQ_CAPABLE)
Helin Zhang043dd652015-10-21 19:56:23 -04008674 return i40e_config_rss_aq(vsi, seed, lut, lut_size);
8675 else
8676 return i40e_config_rss_reg(vsi, seed, lut, lut_size);
8677}
8678
8679/**
8680 * i40e_get_rss - Get RSS keys and lut
8681 * @vsi: Pointer to VSI structure
8682 * @seed: Buffer to store the keys
8683 * @lut: Buffer to store the lookup table entries
8684 * lut_size: Size of buffer to store the lookup table entries
8685 *
8686 * Returns 0 on success, negative on failure
8687 */
8688int i40e_get_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
8689{
Anjali Singhai Jain95a73782015-12-22 14:25:04 -08008690 struct i40e_pf *pf = vsi->back;
8691
Jacob Kellerd36e41d2017-06-23 04:24:46 -04008692 if (pf->hw_features & I40E_HW_RSS_AQ_CAPABLE)
Anjali Singhai Jain95a73782015-12-22 14:25:04 -08008693 return i40e_get_rss_aq(vsi, seed, lut, lut_size);
8694 else
8695 return i40e_get_rss_reg(vsi, seed, lut, lut_size);
Helin Zhang043dd652015-10-21 19:56:23 -04008696}
8697
8698/**
Helin Zhange69ff812015-10-21 19:56:22 -04008699 * i40e_fill_rss_lut - Fill the RSS lookup table with default values
8700 * @pf: Pointer to board private structure
8701 * @lut: Lookup table
8702 * @rss_table_size: Lookup table size
8703 * @rss_size: Range of queue number for hashing
8704 */
Alan Bradyf1582352016-08-24 11:33:46 -07008705void i40e_fill_rss_lut(struct i40e_pf *pf, u8 *lut,
8706 u16 rss_table_size, u16 rss_size)
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008707{
Helin Zhange69ff812015-10-21 19:56:22 -04008708 u16 i;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008709
Helin Zhange69ff812015-10-21 19:56:22 -04008710 for (i = 0; i < rss_table_size; i++)
8711 lut[i] = i % rss_size;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008712}
8713
8714/**
Helin Zhang043dd652015-10-21 19:56:23 -04008715 * i40e_pf_config_rss - Prepare for RSS if used
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008716 * @pf: board private structure
8717 **/
Helin Zhang043dd652015-10-21 19:56:23 -04008718static int i40e_pf_config_rss(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008719{
Anjali Singhai Jain66ddcff2015-02-24 06:58:50 +00008720 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008721 u8 seed[I40E_HKEY_ARRAY_SIZE];
Helin Zhange69ff812015-10-21 19:56:22 -04008722 u8 *lut;
Anjali Singhai Jain4617e8c2013-11-20 10:02:56 +00008723 struct i40e_hw *hw = &pf->hw;
Carolyn Wybornye157ea32014-06-03 23:50:22 +00008724 u32 reg_val;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008725 u64 hena;
Helin Zhange69ff812015-10-21 19:56:22 -04008726 int ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008727
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008728 /* By default we enable TCP/UDP with IPv4/IPv6 ptypes */
Shannon Nelson272cdaf22016-02-17 16:12:21 -08008729 hena = (u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(0)) |
8730 ((u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(1)) << 32);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008731 hena |= i40e_pf_get_default_rss_hena(pf);
8732
Shannon Nelson272cdaf22016-02-17 16:12:21 -08008733 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), (u32)hena);
8734 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008735
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008736 /* Determine the RSS table size based on the hardware capabilities */
Shannon Nelson272cdaf22016-02-17 16:12:21 -08008737 reg_val = i40e_read_rx_ctl(hw, I40E_PFQF_CTL_0);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008738 reg_val = (pf->rss_table_size == 512) ?
8739 (reg_val | I40E_PFQF_CTL_0_HASHLUTSIZE_512) :
8740 (reg_val & ~I40E_PFQF_CTL_0_HASHLUTSIZE_512);
Shannon Nelson272cdaf22016-02-17 16:12:21 -08008741 i40e_write_rx_ctl(hw, I40E_PFQF_CTL_0, reg_val);
Carolyn Wybornye157ea32014-06-03 23:50:22 +00008742
Helin Zhang28c58692015-10-26 19:44:27 -04008743 /* Determine the RSS size of the VSI */
Harshitha Ramamurthyf25571b2017-02-21 15:55:40 -08008744 if (!vsi->rss_size) {
8745 u16 qcount;
8746
8747 qcount = vsi->num_queue_pairs / vsi->tc_config.numtc;
8748 vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
8749 }
Mitch Williamsa4fa59c2016-09-12 14:18:43 -07008750 if (!vsi->rss_size)
8751 return -EINVAL;
Helin Zhang28c58692015-10-26 19:44:27 -04008752
Helin Zhange69ff812015-10-21 19:56:22 -04008753 lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
8754 if (!lut)
8755 return -ENOMEM;
8756
Helin Zhang28c58692015-10-26 19:44:27 -04008757 /* Use user configured lut if there is one, otherwise use default */
8758 if (vsi->rss_lut_user)
8759 memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
8760 else
8761 i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
Helin Zhange69ff812015-10-21 19:56:22 -04008762
Helin Zhang28c58692015-10-26 19:44:27 -04008763 /* Use user configured hash key if there is one, otherwise
8764 * use default.
8765 */
8766 if (vsi->rss_hkey_user)
8767 memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
8768 else
8769 netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
Helin Zhang043dd652015-10-21 19:56:23 -04008770 ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
Helin Zhange69ff812015-10-21 19:56:22 -04008771 kfree(lut);
8772
8773 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008774}
8775
8776/**
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008777 * i40e_reconfig_rss_queues - change number of queues for rss and rebuild
8778 * @pf: board private structure
8779 * @queue_count: the requested queue count for rss.
8780 *
8781 * returns 0 if rss is not enabled, if enabled returns the final rss queue
8782 * count which may be different from the requested queue count.
Maciej Sosin373149f2017-04-05 07:50:55 -04008783 * Note: expects to be called while under rtnl_lock()
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008784 **/
8785int i40e_reconfig_rss_queues(struct i40e_pf *pf, int queue_count)
8786{
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00008787 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
8788 int new_rss_size;
8789
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008790 if (!(pf->flags & I40E_FLAG_RSS_ENABLED))
8791 return 0;
8792
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00008793 new_rss_size = min_t(int, queue_count, pf->rss_size_max);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008794
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00008795 if (queue_count != vsi->num_queue_pairs) {
Harshitha Ramamurthyf25571b2017-02-21 15:55:40 -08008796 u16 qcount;
8797
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00008798 vsi->req_queue_pairs = queue_count;
Maciej Sosin373149f2017-04-05 07:50:55 -04008799 i40e_prep_for_reset(pf, true);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008800
Helin Zhangacd65442015-10-26 19:44:28 -04008801 pf->alloc_rss_size = new_rss_size;
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008802
Maciej Sosin373149f2017-04-05 07:50:55 -04008803 i40e_reset_and_rebuild(pf, true, true);
Helin Zhang28c58692015-10-26 19:44:27 -04008804
8805 /* Discard the user configured hash keys and lut, if less
8806 * queues are enabled.
8807 */
8808 if (queue_count < vsi->rss_size) {
8809 i40e_clear_rss_config_user(vsi);
8810 dev_dbg(&pf->pdev->dev,
8811 "discard user configured hash keys and lut\n");
8812 }
8813
8814 /* Reset vsi->rss_size, as number of enabled queues changed */
Harshitha Ramamurthyf25571b2017-02-21 15:55:40 -08008815 qcount = vsi->num_queue_pairs / vsi->tc_config.numtc;
8816 vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
Helin Zhang28c58692015-10-26 19:44:27 -04008817
Helin Zhang043dd652015-10-21 19:56:23 -04008818 i40e_pf_config_rss(pf);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008819 }
Lihong Yang12815052016-09-27 11:28:48 -07008820 dev_info(&pf->pdev->dev, "User requested queue count/HW max RSS count: %d/%d\n",
8821 vsi->req_queue_pairs, pf->rss_size_max);
Helin Zhangacd65442015-10-26 19:44:28 -04008822 return pf->alloc_rss_size;
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008823}
8824
8825/**
Shannon Nelson4fc8c672017-06-07 05:43:08 -04008826 * i40e_get_partition_bw_setting - Retrieve BW settings for this PF partition
Greg Rosef4492db2015-02-06 08:52:12 +00008827 * @pf: board private structure
8828 **/
Shannon Nelson4fc8c672017-06-07 05:43:08 -04008829i40e_status i40e_get_partition_bw_setting(struct i40e_pf *pf)
Greg Rosef4492db2015-02-06 08:52:12 +00008830{
8831 i40e_status status;
8832 bool min_valid, max_valid;
8833 u32 max_bw, min_bw;
8834
8835 status = i40e_read_bw_from_alt_ram(&pf->hw, &max_bw, &min_bw,
8836 &min_valid, &max_valid);
8837
8838 if (!status) {
8839 if (min_valid)
Shannon Nelson4fc8c672017-06-07 05:43:08 -04008840 pf->min_bw = min_bw;
Greg Rosef4492db2015-02-06 08:52:12 +00008841 if (max_valid)
Shannon Nelson4fc8c672017-06-07 05:43:08 -04008842 pf->max_bw = max_bw;
Greg Rosef4492db2015-02-06 08:52:12 +00008843 }
8844
8845 return status;
8846}
8847
8848/**
Shannon Nelson4fc8c672017-06-07 05:43:08 -04008849 * i40e_set_partition_bw_setting - Set BW settings for this PF partition
Greg Rosef4492db2015-02-06 08:52:12 +00008850 * @pf: board private structure
8851 **/
Shannon Nelson4fc8c672017-06-07 05:43:08 -04008852i40e_status i40e_set_partition_bw_setting(struct i40e_pf *pf)
Greg Rosef4492db2015-02-06 08:52:12 +00008853{
8854 struct i40e_aqc_configure_partition_bw_data bw_data;
8855 i40e_status status;
8856
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00008857 /* Set the valid bit for this PF */
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04008858 bw_data.pf_valid_bits = cpu_to_le16(BIT(pf->hw.pf_id));
Shannon Nelson4fc8c672017-06-07 05:43:08 -04008859 bw_data.max_bw[pf->hw.pf_id] = pf->max_bw & I40E_ALT_BW_VALUE_MASK;
8860 bw_data.min_bw[pf->hw.pf_id] = pf->min_bw & I40E_ALT_BW_VALUE_MASK;
Greg Rosef4492db2015-02-06 08:52:12 +00008861
8862 /* Set the new bandwidths */
8863 status = i40e_aq_configure_partition_bw(&pf->hw, &bw_data, NULL);
8864
8865 return status;
8866}
8867
8868/**
Shannon Nelson4fc8c672017-06-07 05:43:08 -04008869 * i40e_commit_partition_bw_setting - Commit BW settings for this PF partition
Greg Rosef4492db2015-02-06 08:52:12 +00008870 * @pf: board private structure
8871 **/
Shannon Nelson4fc8c672017-06-07 05:43:08 -04008872i40e_status i40e_commit_partition_bw_setting(struct i40e_pf *pf)
Greg Rosef4492db2015-02-06 08:52:12 +00008873{
8874 /* Commit temporary BW setting to permanent NVM image */
8875 enum i40e_admin_queue_err last_aq_status;
8876 i40e_status ret;
8877 u16 nvm_word;
8878
8879 if (pf->hw.partition_id != 1) {
8880 dev_info(&pf->pdev->dev,
8881 "Commit BW only works on partition 1! This is partition %d",
8882 pf->hw.partition_id);
8883 ret = I40E_NOT_SUPPORTED;
8884 goto bw_commit_out;
8885 }
8886
8887 /* Acquire NVM for read access */
8888 ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_READ);
8889 last_aq_status = pf->hw.aq.asq_last_status;
8890 if (ret) {
8891 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008892 "Cannot acquire NVM for read access, err %s aq_err %s\n",
8893 i40e_stat_str(&pf->hw, ret),
8894 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +00008895 goto bw_commit_out;
8896 }
8897
8898 /* Read word 0x10 of NVM - SW compatibility word 1 */
8899 ret = i40e_aq_read_nvm(&pf->hw,
8900 I40E_SR_NVM_CONTROL_WORD,
8901 0x10, sizeof(nvm_word), &nvm_word,
8902 false, NULL);
8903 /* Save off last admin queue command status before releasing
8904 * the NVM
8905 */
8906 last_aq_status = pf->hw.aq.asq_last_status;
8907 i40e_release_nvm(&pf->hw);
8908 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008909 dev_info(&pf->pdev->dev, "NVM read error, err %s aq_err %s\n",
8910 i40e_stat_str(&pf->hw, ret),
8911 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +00008912 goto bw_commit_out;
8913 }
8914
8915 /* Wait a bit for NVM release to complete */
8916 msleep(50);
8917
8918 /* Acquire NVM for write access */
8919 ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_WRITE);
8920 last_aq_status = pf->hw.aq.asq_last_status;
8921 if (ret) {
8922 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008923 "Cannot acquire NVM for write access, err %s aq_err %s\n",
8924 i40e_stat_str(&pf->hw, ret),
8925 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +00008926 goto bw_commit_out;
8927 }
8928 /* Write it back out unchanged to initiate update NVM,
8929 * which will force a write of the shadow (alt) RAM to
8930 * the NVM - thus storing the bandwidth values permanently.
8931 */
8932 ret = i40e_aq_update_nvm(&pf->hw,
8933 I40E_SR_NVM_CONTROL_WORD,
8934 0x10, sizeof(nvm_word),
8935 &nvm_word, true, NULL);
8936 /* Save off last admin queue command status before releasing
8937 * the NVM
8938 */
8939 last_aq_status = pf->hw.aq.asq_last_status;
8940 i40e_release_nvm(&pf->hw);
8941 if (ret)
8942 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008943 "BW settings NOT SAVED, err %s aq_err %s\n",
8944 i40e_stat_str(&pf->hw, ret),
8945 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +00008946bw_commit_out:
8947
8948 return ret;
8949}
8950
8951/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008952 * i40e_sw_init - Initialize general software structures (struct i40e_pf)
8953 * @pf: board private structure to initialize
8954 *
8955 * i40e_sw_init initializes the Adapter private data structure.
8956 * Fields are initialized based on PCI device information and
8957 * OS network device settings (MTU size).
8958 **/
8959static int i40e_sw_init(struct i40e_pf *pf)
8960{
8961 int err = 0;
8962 int size;
8963
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008964 /* Set default capability flags */
8965 pf->flags = I40E_FLAG_RX_CSUM_ENABLED |
8966 I40E_FLAG_MSI_ENABLED |
Mitch Williams2bc7ee82015-02-06 08:52:11 +00008967 I40E_FLAG_MSIX_ENABLED;
8968
Mitch Williamsca99eb92014-04-04 04:43:07 +00008969 /* Set default ITR */
8970 pf->rx_itr_default = I40E_ITR_DYNAMIC | I40E_ITR_RX_DEF;
8971 pf->tx_itr_default = I40E_ITR_DYNAMIC | I40E_ITR_TX_DEF;
8972
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00008973 /* Depending on PF configurations, it is possible that the RSS
8974 * maximum might end up larger than the available queues
8975 */
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04008976 pf->rss_size_max = BIT(pf->hw.func_caps.rss_table_entry_width);
Helin Zhangacd65442015-10-26 19:44:28 -04008977 pf->alloc_rss_size = 1;
Anjali Singhai Jain5db4cb52015-02-24 06:58:49 +00008978 pf->rss_table_size = pf->hw.func_caps.rss_table_size;
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00008979 pf->rss_size_max = min_t(int, pf->rss_size_max,
8980 pf->hw.func_caps.num_tx_qp);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008981 if (pf->hw.func_caps.rss) {
8982 pf->flags |= I40E_FLAG_RSS_ENABLED;
Helin Zhangacd65442015-10-26 19:44:28 -04008983 pf->alloc_rss_size = min_t(int, pf->rss_size_max,
8984 num_online_cpus());
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008985 }
8986
Catherine Sullivan2050bc62013-12-18 13:46:03 +00008987 /* MFP mode enabled */
Pawel Orlowskic78b9532015-04-22 19:34:06 -04008988 if (pf->hw.func_caps.npar_enable || pf->hw.func_caps.flex10_enable) {
Catherine Sullivan2050bc62013-12-18 13:46:03 +00008989 pf->flags |= I40E_FLAG_MFP_ENABLED;
8990 dev_info(&pf->pdev->dev, "MFP mode Enabled\n");
Shannon Nelson4fc8c672017-06-07 05:43:08 -04008991 if (i40e_get_partition_bw_setting(pf)) {
Greg Rosef4492db2015-02-06 08:52:12 +00008992 dev_warn(&pf->pdev->dev,
Shannon Nelson4fc8c672017-06-07 05:43:08 -04008993 "Could not get partition bw settings\n");
8994 } else {
Greg Rosef4492db2015-02-06 08:52:12 +00008995 dev_info(&pf->pdev->dev,
Shannon Nelson4fc8c672017-06-07 05:43:08 -04008996 "Partition BW Min = %8.8x, Max = %8.8x\n",
8997 pf->min_bw, pf->max_bw);
8998
8999 /* nudge the Tx scheduler */
9000 i40e_set_partition_bw_setting(pf);
9001 }
Catherine Sullivan2050bc62013-12-18 13:46:03 +00009002 }
9003
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009004 if ((pf->hw.func_caps.fd_filters_guaranteed > 0) ||
9005 (pf->hw.func_caps.fd_filters_best_effort > 0)) {
9006 pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
9007 pf->atr_sample_rate = I40E_DEFAULT_ATR_SAMPLE_RATE;
Shannon Nelson6eae9c62015-09-03 17:18:55 -04009008 if (pf->flags & I40E_FLAG_MFP_ENABLED &&
9009 pf->hw.num_partitions > 1)
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009010 dev_info(&pf->pdev->dev,
Anjali Singhai Jain0b675842014-03-06 08:59:51 +00009011 "Flow Director Sideband mode Disabled in MFP mode\n");
Shannon Nelson6eae9c62015-09-03 17:18:55 -04009012 else
9013 pf->flags |= I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009014 pf->fdir_pf_filter_count =
9015 pf->hw.func_caps.fd_filters_guaranteed;
9016 pf->hw.fdir_shared_filter_count =
9017 pf->hw.func_caps.fd_filters_best_effort;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009018 }
9019
Anjali Singhai Jain5a433192017-06-23 04:24:45 -04009020 if (pf->hw.mac.type == I40E_MAC_X722) {
Jacob Kellerd36e41d2017-06-23 04:24:46 -04009021 pf->hw_features |= (I40E_HW_RSS_AQ_CAPABLE |
9022 I40E_HW_128_QP_RSS_CAPABLE |
9023 I40E_HW_ATR_EVICT_CAPABLE |
9024 I40E_HW_WB_ON_ITR_CAPABLE |
9025 I40E_HW_MULTIPLE_TCP_UDP_RSS_PCTYPE |
9026 I40E_HW_NO_PCI_LINK_CHECK |
9027 I40E_HW_USE_SET_LLDP_MIB |
9028 I40E_HW_GENEVE_OFFLOAD_CAPABLE |
9029 I40E_HW_PTP_L4_CAPABLE |
9030 I40E_HW_WOL_MC_MAGIC_PKT_WAKE |
9031 I40E_HW_OUTER_UDP_CSUM_CAPABLE);
Anjali Singhai Jain10a955f2017-06-23 04:24:48 -04009032
9033#define I40E_FDEVICT_PCTYPE_DEFAULT 0xc03
9034 if (rd32(&pf->hw, I40E_GLQF_FDEVICTENA(1)) !=
9035 I40E_FDEVICT_PCTYPE_DEFAULT) {
9036 dev_warn(&pf->pdev->dev,
9037 "FD EVICT PCTYPES are not right, disable FD HW EVICT\n");
9038 pf->hw_features &= ~I40E_HW_ATR_EVICT_CAPABLE;
9039 }
Anjali Singhai Jain5a433192017-06-23 04:24:45 -04009040 } else if ((pf->hw.aq.api_maj_ver > 1) ||
9041 ((pf->hw.aq.api_maj_ver == 1) &&
9042 (pf->hw.aq.api_min_ver > 4))) {
9043 /* Supported in FW API version higher than 1.4 */
Jacob Kellerd36e41d2017-06-23 04:24:46 -04009044 pf->hw_features |= I40E_HW_GENEVE_OFFLOAD_CAPABLE;
Anjali Singhai Jain5a433192017-06-23 04:24:45 -04009045 }
9046
9047 /* Enable HW ATR eviction if possible */
Jacob Kellerd36e41d2017-06-23 04:24:46 -04009048 if (pf->hw_features & I40E_HW_ATR_EVICT_CAPABLE)
Anjali Singhai Jain5a433192017-06-23 04:24:45 -04009049 pf->flags |= I40E_FLAG_HW_ATR_EVICT_ENABLED;
9050
Bimmy Pujari6de432c2016-11-11 12:39:38 -08009051 if ((pf->hw.mac.type == I40E_MAC_XL710) &&
Anjali Singhai Jain8eed76f2015-12-09 15:50:31 -08009052 (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 33)) ||
Neerav Parikhf1bbad32016-01-13 16:51:39 -08009053 (pf->hw.aq.fw_maj_ver < 4))) {
Jacob Kellerd36e41d2017-06-23 04:24:46 -04009054 pf->hw_features |= I40E_HW_RESTART_AUTONEG;
Neerav Parikhf1bbad32016-01-13 16:51:39 -08009055 /* No DCB support for FW < v4.33 */
Jacob Kellerd36e41d2017-06-23 04:24:46 -04009056 pf->hw_features |= I40E_HW_NO_DCB_SUPPORT;
Neerav Parikhf1bbad32016-01-13 16:51:39 -08009057 }
9058
9059 /* Disable FW LLDP if FW < v4.3 */
Bimmy Pujari6de432c2016-11-11 12:39:38 -08009060 if ((pf->hw.mac.type == I40E_MAC_XL710) &&
Neerav Parikhf1bbad32016-01-13 16:51:39 -08009061 (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 3)) ||
9062 (pf->hw.aq.fw_maj_ver < 4)))
Jacob Kellerd36e41d2017-06-23 04:24:46 -04009063 pf->hw_features |= I40E_HW_STOP_FW_LLDP;
Neerav Parikhf1bbad32016-01-13 16:51:39 -08009064
9065 /* Use the FW Set LLDP MIB API if FW > v4.40 */
Bimmy Pujari6de432c2016-11-11 12:39:38 -08009066 if ((pf->hw.mac.type == I40E_MAC_XL710) &&
Neerav Parikhf1bbad32016-01-13 16:51:39 -08009067 (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver >= 40)) ||
9068 (pf->hw.aq.fw_maj_ver >= 5)))
Jacob Kellerd36e41d2017-06-23 04:24:46 -04009069 pf->hw_features |= I40E_HW_USE_SET_LLDP_MIB;
Anjali Singhai Jain8eed76f2015-12-09 15:50:31 -08009070
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009071 if (pf->hw.func_caps.vmdq) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009072 pf->num_vmdq_vsis = I40E_DEFAULT_NUM_VMDQ_VSI;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04009073 pf->flags |= I40E_FLAG_VMDQ_ENABLED;
Jesse Brandeburge9e53662015-10-02 17:57:21 -07009074 pf->num_vmdq_qps = i40e_default_queues_per_vmdq(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009075 }
9076
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06009077 if (pf->hw.func_caps.iwarp) {
9078 pf->flags |= I40E_FLAG_IWARP_ENABLED;
9079 /* IWARP needs one extra vector for CQP just like MISC.*/
9080 pf->num_iwarp_msix = (int)num_online_cpus() + 1;
9081 }
9082
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009083#ifdef CONFIG_PCI_IOV
Shannon Nelsonba252f132014-12-11 07:06:34 +00009084 if (pf->hw.func_caps.num_vfs && pf->hw.partition_id == 1) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009085 pf->num_vf_qps = I40E_DEFAULT_QUEUES_PER_VF;
9086 pf->flags |= I40E_FLAG_SRIOV_ENABLED;
9087 pf->num_req_vfs = min_t(int,
9088 pf->hw.func_caps.num_vfs,
9089 I40E_MAX_VF_COUNT);
9090 }
9091#endif /* CONFIG_PCI_IOV */
9092 pf->eeprom_version = 0xDEAD;
9093 pf->lan_veb = I40E_NO_VEB;
9094 pf->lan_vsi = I40E_NO_VSI;
9095
Anjali Singhai Jaind1a8d272015-07-23 16:54:40 -04009096 /* By default FW has this off for performance reasons */
9097 pf->flags &= ~I40E_FLAG_VEB_STATS_ENABLED;
9098
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009099 /* set up queue assignment tracking */
9100 size = sizeof(struct i40e_lump_tracking)
9101 + (sizeof(u16) * pf->hw.func_caps.num_tx_qp);
9102 pf->qp_pile = kzalloc(size, GFP_KERNEL);
9103 if (!pf->qp_pile) {
9104 err = -ENOMEM;
9105 goto sw_init_done;
9106 }
9107 pf->qp_pile->num_entries = pf->hw.func_caps.num_tx_qp;
9108 pf->qp_pile->search_hint = 0;
9109
Anjali Singhai Jain327fe042014-06-04 01:23:26 +00009110 pf->tx_timeout_recovery_level = 1;
9111
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009112 mutex_init(&pf->switch_mutex);
9113
9114sw_init_done:
9115 return err;
9116}
9117
9118/**
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00009119 * i40e_set_ntuple - set the ntuple feature flag and take action
9120 * @pf: board private structure to initialize
9121 * @features: the feature set that the stack is suggesting
9122 *
9123 * returns a bool to indicate if reset needs to happen
9124 **/
9125bool i40e_set_ntuple(struct i40e_pf *pf, netdev_features_t features)
9126{
9127 bool need_reset = false;
9128
9129 /* Check if Flow Director n-tuple support was enabled or disabled. If
9130 * the state changed, we need to reset.
9131 */
9132 if (features & NETIF_F_NTUPLE) {
9133 /* Enable filters and mark for reset */
9134 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
9135 need_reset = true;
Tushar Davea70e4072016-05-16 12:40:53 -07009136 /* enable FD_SB only if there is MSI-X vector */
9137 if (pf->num_fdsb_msix > 0)
9138 pf->flags |= I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00009139 } else {
9140 /* turn off filters, mark for reset and clear SW filter list */
9141 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
9142 need_reset = true;
9143 i40e_fdir_filter_exit(pf);
9144 }
Jacob Keller47994c12017-04-19 09:25:57 -04009145 pf->flags &= ~(I40E_FLAG_FD_SB_ENABLED |
9146 I40E_FLAG_FD_SB_AUTO_DISABLED);
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00009147 /* reset fd counters */
Jacob Keller097dbf52017-02-06 14:38:46 -08009148 pf->fd_add_err = 0;
9149 pf->fd_atr_cnt = 0;
Anjali Singhai Jain8a4f34f2014-06-04 08:45:20 +00009150 /* if ATR was auto disabled it can be re-enabled. */
Jacob Keller47994c12017-04-19 09:25:57 -04009151 if (pf->flags & I40E_FLAG_FD_ATR_AUTO_DISABLED) {
9152 pf->flags &= ~I40E_FLAG_FD_ATR_AUTO_DISABLED;
9153 if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
9154 (I40E_DEBUG_FD & pf->hw.debug_mask))
Jacob Keller234dc4e2016-09-06 18:05:09 -07009155 dev_info(&pf->pdev->dev, "ATR re-enabled.\n");
9156 }
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00009157 }
9158 return need_reset;
9159}
9160
9161/**
Alan Bradyd8ec9862016-07-27 12:02:38 -07009162 * i40e_clear_rss_lut - clear the rx hash lookup table
9163 * @vsi: the VSI being configured
9164 **/
9165static void i40e_clear_rss_lut(struct i40e_vsi *vsi)
9166{
9167 struct i40e_pf *pf = vsi->back;
9168 struct i40e_hw *hw = &pf->hw;
9169 u16 vf_id = vsi->vf_id;
9170 u8 i;
9171
9172 if (vsi->type == I40E_VSI_MAIN) {
9173 for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
9174 wr32(hw, I40E_PFQF_HLUT(i), 0);
9175 } else if (vsi->type == I40E_VSI_SRIOV) {
9176 for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
9177 i40e_write_rx_ctl(hw, I40E_VFQF_HLUT1(i, vf_id), 0);
9178 } else {
9179 dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
9180 }
9181}
9182
9183/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009184 * i40e_set_features - set the netdev feature flags
9185 * @netdev: ptr to the netdev being adjusted
9186 * @features: the feature set that the stack is suggesting
Maciej Sosin373149f2017-04-05 07:50:55 -04009187 * Note: expects to be called while under rtnl_lock()
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009188 **/
9189static int i40e_set_features(struct net_device *netdev,
9190 netdev_features_t features)
9191{
9192 struct i40e_netdev_priv *np = netdev_priv(netdev);
9193 struct i40e_vsi *vsi = np->vsi;
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00009194 struct i40e_pf *pf = vsi->back;
9195 bool need_reset;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009196
Alan Bradyd8ec9862016-07-27 12:02:38 -07009197 if (features & NETIF_F_RXHASH && !(netdev->features & NETIF_F_RXHASH))
9198 i40e_pf_config_rss(pf);
9199 else if (!(features & NETIF_F_RXHASH) &&
9200 netdev->features & NETIF_F_RXHASH)
9201 i40e_clear_rss_lut(vsi);
9202
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009203 if (features & NETIF_F_HW_VLAN_CTAG_RX)
9204 i40e_vlan_stripping_enable(vsi);
9205 else
9206 i40e_vlan_stripping_disable(vsi);
9207
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00009208 need_reset = i40e_set_ntuple(pf, features);
9209
9210 if (need_reset)
Maciej Sosin373149f2017-04-05 07:50:55 -04009211 i40e_do_reset(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED), true);
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00009212
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009213 return 0;
9214}
9215
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009216/**
Singhai, Anjali6a899022015-12-14 12:21:18 -08009217 * i40e_get_udp_port_idx - Lookup a possibly offloaded for Rx UDP port
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009218 * @pf: board private structure
9219 * @port: The UDP port to look up
9220 *
9221 * Returns the index number or I40E_MAX_PF_UDP_OFFLOAD_PORTS if port not found
9222 **/
Jacob Kellerfe0b0cd2017-02-06 14:38:38 -08009223static u8 i40e_get_udp_port_idx(struct i40e_pf *pf, u16 port)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009224{
9225 u8 i;
9226
9227 for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
Jacob Keller27826fd2017-04-19 09:25:50 -04009228 if (pf->udp_ports[i].port == port)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009229 return i;
9230 }
9231
9232 return i;
9233}
9234
9235/**
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009236 * i40e_udp_tunnel_add - Get notifications about UDP tunnel ports that come up
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009237 * @netdev: This physical port's netdev
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009238 * @ti: Tunnel endpoint information
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009239 **/
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009240static void i40e_udp_tunnel_add(struct net_device *netdev,
9241 struct udp_tunnel_info *ti)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009242{
9243 struct i40e_netdev_priv *np = netdev_priv(netdev);
9244 struct i40e_vsi *vsi = np->vsi;
9245 struct i40e_pf *pf = vsi->back;
Jacob Kellerfe0b0cd2017-02-06 14:38:38 -08009246 u16 port = ntohs(ti->port);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009247 u8 next_idx;
9248 u8 idx;
9249
Singhai, Anjali6a899022015-12-14 12:21:18 -08009250 idx = i40e_get_udp_port_idx(pf, port);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009251
9252 /* Check if port already exists */
9253 if (idx < I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
Jacob Kellerfe0b0cd2017-02-06 14:38:38 -08009254 netdev_info(netdev, "port %d already offloaded\n", port);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009255 return;
9256 }
9257
9258 /* Now check if there is space to add the new port */
Singhai, Anjali6a899022015-12-14 12:21:18 -08009259 next_idx = i40e_get_udp_port_idx(pf, 0);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009260
9261 if (next_idx == I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009262 netdev_info(netdev, "maximum number of offloaded UDP ports reached, not adding port %d\n",
Jacob Kellerfe0b0cd2017-02-06 14:38:38 -08009263 port);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009264 return;
9265 }
9266
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009267 switch (ti->type) {
9268 case UDP_TUNNEL_TYPE_VXLAN:
9269 pf->udp_ports[next_idx].type = I40E_AQC_TUNNEL_TYPE_VXLAN;
9270 break;
9271 case UDP_TUNNEL_TYPE_GENEVE:
Jacob Kellerd36e41d2017-06-23 04:24:46 -04009272 if (!(pf->hw_features & I40E_HW_GENEVE_OFFLOAD_CAPABLE))
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009273 return;
9274 pf->udp_ports[next_idx].type = I40E_AQC_TUNNEL_TYPE_NGE;
9275 break;
9276 default:
9277 return;
9278 }
9279
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009280 /* New port: add it and mark its index in the bitmap */
Jacob Keller27826fd2017-04-19 09:25:50 -04009281 pf->udp_ports[next_idx].port = port;
Singhai, Anjali6a899022015-12-14 12:21:18 -08009282 pf->pending_udp_bitmap |= BIT_ULL(next_idx);
9283 pf->flags |= I40E_FLAG_UDP_FILTER_SYNC;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009284}
9285
9286/**
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009287 * i40e_udp_tunnel_del - Get notifications about UDP tunnel ports that go away
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009288 * @netdev: This physical port's netdev
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009289 * @ti: Tunnel endpoint information
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009290 **/
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009291static void i40e_udp_tunnel_del(struct net_device *netdev,
9292 struct udp_tunnel_info *ti)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009293{
9294 struct i40e_netdev_priv *np = netdev_priv(netdev);
9295 struct i40e_vsi *vsi = np->vsi;
9296 struct i40e_pf *pf = vsi->back;
Jacob Kellerfe0b0cd2017-02-06 14:38:38 -08009297 u16 port = ntohs(ti->port);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009298 u8 idx;
9299
Singhai, Anjali6a899022015-12-14 12:21:18 -08009300 idx = i40e_get_udp_port_idx(pf, port);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009301
9302 /* Check if port already exists */
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009303 if (idx >= I40E_MAX_PF_UDP_OFFLOAD_PORTS)
9304 goto not_found;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009305
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009306 switch (ti->type) {
9307 case UDP_TUNNEL_TYPE_VXLAN:
9308 if (pf->udp_ports[idx].type != I40E_AQC_TUNNEL_TYPE_VXLAN)
9309 goto not_found;
9310 break;
9311 case UDP_TUNNEL_TYPE_GENEVE:
9312 if (pf->udp_ports[idx].type != I40E_AQC_TUNNEL_TYPE_NGE)
9313 goto not_found;
9314 break;
9315 default:
9316 goto not_found;
Singhai, Anjali6a899022015-12-14 12:21:18 -08009317 }
9318
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009319 /* if port exists, set it to 0 (mark for deletion)
9320 * and make it pending
9321 */
Jacob Keller27826fd2017-04-19 09:25:50 -04009322 pf->udp_ports[idx].port = 0;
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009323 pf->pending_udp_bitmap |= BIT_ULL(idx);
Singhai, Anjali6a899022015-12-14 12:21:18 -08009324 pf->flags |= I40E_FLAG_UDP_FILTER_SYNC;
9325
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009326 return;
9327not_found:
9328 netdev_warn(netdev, "UDP port %d was not found, not deleting\n",
Jacob Kellerfe0b0cd2017-02-06 14:38:38 -08009329 port);
Singhai, Anjali6a899022015-12-14 12:21:18 -08009330}
9331
Neerav Parikh1f224ad2014-02-12 01:45:31 +00009332static int i40e_get_phys_port_id(struct net_device *netdev,
Jiri Pirko02637fc2014-11-28 14:34:16 +01009333 struct netdev_phys_item_id *ppid)
Neerav Parikh1f224ad2014-02-12 01:45:31 +00009334{
9335 struct i40e_netdev_priv *np = netdev_priv(netdev);
9336 struct i40e_pf *pf = np->vsi->back;
9337 struct i40e_hw *hw = &pf->hw;
9338
Jacob Kellerd36e41d2017-06-23 04:24:46 -04009339 if (!(pf->hw_features & I40E_HW_PORT_ID_VALID))
Neerav Parikh1f224ad2014-02-12 01:45:31 +00009340 return -EOPNOTSUPP;
9341
9342 ppid->id_len = min_t(int, sizeof(hw->mac.port_addr), sizeof(ppid->id));
9343 memcpy(ppid->id, hw->mac.port_addr, ppid->id_len);
9344
9345 return 0;
9346}
9347
Jesse Brandeburg2f90ade2014-11-20 16:30:02 -08009348/**
9349 * i40e_ndo_fdb_add - add an entry to the hardware database
9350 * @ndm: the input from the stack
9351 * @tb: pointer to array of nladdr (unused)
9352 * @dev: the net device pointer
9353 * @addr: the MAC address entry being added
9354 * @flags: instructions from stack about fdb operation
9355 */
Greg Rose4ba0dea2014-03-06 08:59:55 +00009356static int i40e_ndo_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
9357 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +01009358 const unsigned char *addr, u16 vid,
Greg Rose4ba0dea2014-03-06 08:59:55 +00009359 u16 flags)
Greg Rose4ba0dea2014-03-06 08:59:55 +00009360{
9361 struct i40e_netdev_priv *np = netdev_priv(dev);
9362 struct i40e_pf *pf = np->vsi->back;
9363 int err = 0;
9364
9365 if (!(pf->flags & I40E_FLAG_SRIOV_ENABLED))
9366 return -EOPNOTSUPP;
9367
Or Gerlitz65891fe2014-12-14 18:19:05 +02009368 if (vid) {
9369 pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
9370 return -EINVAL;
9371 }
9372
Greg Rose4ba0dea2014-03-06 08:59:55 +00009373 /* Hardware does not support aging addresses so if a
9374 * ndm_state is given only allow permanent addresses
9375 */
9376 if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
9377 netdev_info(dev, "FDB only supports static addresses\n");
9378 return -EINVAL;
9379 }
9380
9381 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
9382 err = dev_uc_add_excl(dev, addr);
9383 else if (is_multicast_ether_addr(addr))
9384 err = dev_mc_add_excl(dev, addr);
9385 else
9386 err = -EINVAL;
9387
9388 /* Only return duplicate errors if NLM_F_EXCL is set */
9389 if (err == -EEXIST && !(flags & NLM_F_EXCL))
9390 err = 0;
9391
9392 return err;
9393}
9394
Neerav Parikh51616012015-02-06 08:52:14 +00009395/**
9396 * i40e_ndo_bridge_setlink - Set the hardware bridge mode
9397 * @dev: the netdev being configured
9398 * @nlh: RTNL message
9399 *
9400 * Inserts a new hardware bridge if not already created and
9401 * enables the bridging mode requested (VEB or VEPA). If the
9402 * hardware bridge has already been inserted and the request
9403 * is to change the mode then that requires a PF reset to
9404 * allow rebuild of the components with required hardware
9405 * bridge mode enabled.
Maciej Sosin373149f2017-04-05 07:50:55 -04009406 *
9407 * Note: expects to be called while under rtnl_lock()
Neerav Parikh51616012015-02-06 08:52:14 +00009408 **/
9409static int i40e_ndo_bridge_setlink(struct net_device *dev,
Carolyn Wyborny9df70b62015-04-27 14:57:11 -04009410 struct nlmsghdr *nlh,
9411 u16 flags)
Neerav Parikh51616012015-02-06 08:52:14 +00009412{
9413 struct i40e_netdev_priv *np = netdev_priv(dev);
9414 struct i40e_vsi *vsi = np->vsi;
9415 struct i40e_pf *pf = vsi->back;
9416 struct i40e_veb *veb = NULL;
9417 struct nlattr *attr, *br_spec;
9418 int i, rem;
9419
9420 /* Only for PF VSI for now */
9421 if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
9422 return -EOPNOTSUPP;
9423
9424 /* Find the HW bridge for PF VSI */
9425 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
9426 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
9427 veb = pf->veb[i];
9428 }
9429
9430 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
9431
9432 nla_for_each_nested(attr, br_spec, rem) {
9433 __u16 mode;
9434
9435 if (nla_type(attr) != IFLA_BRIDGE_MODE)
9436 continue;
9437
9438 mode = nla_get_u16(attr);
9439 if ((mode != BRIDGE_MODE_VEPA) &&
9440 (mode != BRIDGE_MODE_VEB))
9441 return -EINVAL;
9442
9443 /* Insert a new HW bridge */
9444 if (!veb) {
9445 veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
9446 vsi->tc_config.enabled_tc);
9447 if (veb) {
9448 veb->bridge_mode = mode;
9449 i40e_config_bridge_mode(veb);
9450 } else {
9451 /* No Bridge HW offload available */
9452 return -ENOENT;
9453 }
9454 break;
9455 } else if (mode != veb->bridge_mode) {
9456 /* Existing HW bridge but different mode needs reset */
9457 veb->bridge_mode = mode;
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07009458 /* TODO: If no VFs or VMDq VSIs, disallow VEB mode */
9459 if (mode == BRIDGE_MODE_VEB)
9460 pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
9461 else
9462 pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
Maciej Sosin373149f2017-04-05 07:50:55 -04009463 i40e_do_reset(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED),
9464 true);
Neerav Parikh51616012015-02-06 08:52:14 +00009465 break;
9466 }
9467 }
9468
9469 return 0;
9470}
9471
9472/**
9473 * i40e_ndo_bridge_getlink - Get the hardware bridge mode
9474 * @skb: skb buff
9475 * @pid: process id
9476 * @seq: RTNL message seq #
9477 * @dev: the netdev being configured
9478 * @filter_mask: unused
Jesse Brandeburgd4b2f9f2015-09-03 17:18:48 -04009479 * @nlflags: netlink flags passed in
Neerav Parikh51616012015-02-06 08:52:14 +00009480 *
9481 * Return the mode in which the hardware bridge is operating in
9482 * i.e VEB or VEPA.
9483 **/
Neerav Parikh51616012015-02-06 08:52:14 +00009484static int i40e_ndo_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
9485 struct net_device *dev,
Carolyn Wyborny9f4ffc42015-08-31 19:54:42 -04009486 u32 __always_unused filter_mask,
9487 int nlflags)
Neerav Parikh51616012015-02-06 08:52:14 +00009488{
9489 struct i40e_netdev_priv *np = netdev_priv(dev);
9490 struct i40e_vsi *vsi = np->vsi;
9491 struct i40e_pf *pf = vsi->back;
9492 struct i40e_veb *veb = NULL;
9493 int i;
9494
9495 /* Only for PF VSI for now */
9496 if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
9497 return -EOPNOTSUPP;
9498
9499 /* Find the HW bridge for the PF VSI */
9500 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
9501 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
9502 veb = pf->veb[i];
9503 }
9504
9505 if (!veb)
9506 return 0;
9507
Nicolas Dichtel46c264d2015-04-28 18:33:49 +02009508 return ndo_dflt_bridge_getlink(skb, pid, seq, dev, veb->bridge_mode,
Huaibin Wang599b0762016-09-26 09:51:18 +02009509 0, 0, nlflags, filter_mask, NULL);
Neerav Parikh51616012015-02-06 08:52:14 +00009510}
Neerav Parikh51616012015-02-06 08:52:14 +00009511
Joe Stringerf44a75e2015-04-14 17:09:14 -07009512/**
9513 * i40e_features_check - Validate encapsulated packet conforms to limits
9514 * @skb: skb buff
Jean Sacren2bc11c62015-09-19 05:08:43 -06009515 * @dev: This physical port's netdev
Joe Stringerf44a75e2015-04-14 17:09:14 -07009516 * @features: Offload features that the stack believes apply
9517 **/
9518static netdev_features_t i40e_features_check(struct sk_buff *skb,
9519 struct net_device *dev,
9520 netdev_features_t features)
9521{
Alexander Duyckf114dca2016-10-25 16:08:46 -07009522 size_t len;
9523
9524 /* No point in doing any of this if neither checksum nor GSO are
9525 * being requested for this frame. We can rule out both by just
9526 * checking for CHECKSUM_PARTIAL
9527 */
9528 if (skb->ip_summed != CHECKSUM_PARTIAL)
9529 return features;
9530
9531 /* We cannot support GSO if the MSS is going to be less than
9532 * 64 bytes. If it is then we need to drop support for GSO.
9533 */
9534 if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
9535 features &= ~NETIF_F_GSO_MASK;
9536
9537 /* MACLEN can support at most 63 words */
9538 len = skb_network_header(skb) - skb->data;
9539 if (len & ~(63 * 2))
9540 goto out_err;
9541
9542 /* IPLEN and EIPLEN can support at most 127 dwords */
9543 len = skb_transport_header(skb) - skb_network_header(skb);
9544 if (len & ~(127 * 4))
9545 goto out_err;
9546
9547 if (skb->encapsulation) {
9548 /* L4TUNLEN can support 127 words */
9549 len = skb_inner_network_header(skb) - skb_transport_header(skb);
9550 if (len & ~(127 * 2))
9551 goto out_err;
9552
9553 /* IPLEN can support at most 127 dwords */
9554 len = skb_inner_transport_header(skb) -
9555 skb_inner_network_header(skb);
9556 if (len & ~(127 * 4))
9557 goto out_err;
9558 }
9559
9560 /* No need to validate L4LEN as TCP is the only protocol with a
9561 * a flexible value and we support all possible values supported
9562 * by TCP, which is at most 15 dwords
9563 */
Joe Stringerf44a75e2015-04-14 17:09:14 -07009564
9565 return features;
Alexander Duyckf114dca2016-10-25 16:08:46 -07009566out_err:
9567 return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
Joe Stringerf44a75e2015-04-14 17:09:14 -07009568}
9569
Björn Töpel0c8493d2017-05-24 07:55:34 +02009570/**
9571 * i40e_xdp_setup - add/remove an XDP program
9572 * @vsi: VSI to changed
9573 * @prog: XDP program
9574 **/
9575static int i40e_xdp_setup(struct i40e_vsi *vsi,
9576 struct bpf_prog *prog)
9577{
9578 int frame_size = vsi->netdev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
9579 struct i40e_pf *pf = vsi->back;
9580 struct bpf_prog *old_prog;
9581 bool need_reset;
9582 int i;
9583
9584 /* Don't allow frames that span over multiple buffers */
9585 if (frame_size > vsi->rx_buf_len)
9586 return -EINVAL;
9587
9588 if (!i40e_enabled_xdp_vsi(vsi) && !prog)
9589 return 0;
9590
9591 /* When turning XDP on->off/off->on we reset and rebuild the rings. */
9592 need_reset = (i40e_enabled_xdp_vsi(vsi) != !!prog);
9593
9594 if (need_reset)
9595 i40e_prep_for_reset(pf, true);
9596
9597 old_prog = xchg(&vsi->xdp_prog, prog);
9598
9599 if (need_reset)
9600 i40e_reset_and_rebuild(pf, true, true);
9601
9602 for (i = 0; i < vsi->num_queue_pairs; i++)
9603 WRITE_ONCE(vsi->rx_rings[i]->xdp_prog, vsi->xdp_prog);
9604
9605 if (old_prog)
9606 bpf_prog_put(old_prog);
9607
9608 return 0;
9609}
9610
9611/**
9612 * i40e_xdp - implements ndo_xdp for i40e
9613 * @dev: netdevice
9614 * @xdp: XDP command
9615 **/
9616static int i40e_xdp(struct net_device *dev,
9617 struct netdev_xdp *xdp)
9618{
9619 struct i40e_netdev_priv *np = netdev_priv(dev);
9620 struct i40e_vsi *vsi = np->vsi;
9621
9622 if (vsi->type != I40E_VSI_MAIN)
9623 return -EINVAL;
9624
9625 switch (xdp->command) {
9626 case XDP_SETUP_PROG:
9627 return i40e_xdp_setup(vsi, xdp->prog);
9628 case XDP_QUERY_PROG:
9629 xdp->prog_attached = i40e_enabled_xdp_vsi(vsi);
Daniel Borkmanneb230392017-06-24 21:13:52 +02009630 xdp->prog_id = vsi->xdp_prog ? vsi->xdp_prog->aux->id : 0;
Björn Töpel0c8493d2017-05-24 07:55:34 +02009631 return 0;
9632 default:
9633 return -EINVAL;
9634 }
9635}
9636
Shannon Nelson37a29732015-02-27 09:15:19 +00009637static const struct net_device_ops i40e_netdev_ops = {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009638 .ndo_open = i40e_open,
9639 .ndo_stop = i40e_close,
9640 .ndo_start_xmit = i40e_lan_xmit_frame,
9641 .ndo_get_stats64 = i40e_get_netdev_stats_struct,
9642 .ndo_set_rx_mode = i40e_set_rx_mode,
9643 .ndo_validate_addr = eth_validate_addr,
9644 .ndo_set_mac_address = i40e_set_mac,
9645 .ndo_change_mtu = i40e_change_mtu,
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00009646 .ndo_do_ioctl = i40e_ioctl,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009647 .ndo_tx_timeout = i40e_tx_timeout,
9648 .ndo_vlan_rx_add_vid = i40e_vlan_rx_add_vid,
9649 .ndo_vlan_rx_kill_vid = i40e_vlan_rx_kill_vid,
9650#ifdef CONFIG_NET_POLL_CONTROLLER
9651 .ndo_poll_controller = i40e_netpoll,
9652#endif
John Fastabende4c67342016-02-16 21:16:15 -08009653 .ndo_setup_tc = __i40e_setup_tc,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009654 .ndo_set_features = i40e_set_features,
9655 .ndo_set_vf_mac = i40e_ndo_set_vf_mac,
9656 .ndo_set_vf_vlan = i40e_ndo_set_vf_port_vlan,
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04009657 .ndo_set_vf_rate = i40e_ndo_set_vf_bw,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009658 .ndo_get_vf_config = i40e_ndo_get_vf_config,
Mitch Williams588aefa2014-02-11 08:27:49 +00009659 .ndo_set_vf_link_state = i40e_ndo_set_vf_link_state,
Serey Konge6d90042014-07-12 07:28:14 +00009660 .ndo_set_vf_spoofchk = i40e_ndo_set_vf_spoofchk,
Anjali Singhai Jainc3bbbd22016-04-01 03:56:07 -07009661 .ndo_set_vf_trust = i40e_ndo_set_vf_trust,
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009662 .ndo_udp_tunnel_add = i40e_udp_tunnel_add,
9663 .ndo_udp_tunnel_del = i40e_udp_tunnel_del,
Neerav Parikh1f224ad2014-02-12 01:45:31 +00009664 .ndo_get_phys_port_id = i40e_get_phys_port_id,
Greg Rose4ba0dea2014-03-06 08:59:55 +00009665 .ndo_fdb_add = i40e_ndo_fdb_add,
Joe Stringerf44a75e2015-04-14 17:09:14 -07009666 .ndo_features_check = i40e_features_check,
Neerav Parikh51616012015-02-06 08:52:14 +00009667 .ndo_bridge_getlink = i40e_ndo_bridge_getlink,
9668 .ndo_bridge_setlink = i40e_ndo_bridge_setlink,
Björn Töpel0c8493d2017-05-24 07:55:34 +02009669 .ndo_xdp = i40e_xdp,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009670};
9671
9672/**
9673 * i40e_config_netdev - Setup the netdev flags
9674 * @vsi: the VSI being configured
9675 *
9676 * Returns 0 on success, negative value on failure
9677 **/
9678static int i40e_config_netdev(struct i40e_vsi *vsi)
9679{
9680 struct i40e_pf *pf = vsi->back;
9681 struct i40e_hw *hw = &pf->hw;
9682 struct i40e_netdev_priv *np;
9683 struct net_device *netdev;
Jacob Keller435c0842016-11-08 13:05:10 -08009684 u8 broadcast[ETH_ALEN];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009685 u8 mac_addr[ETH_ALEN];
9686 int etherdev_size;
Preethi Banalabacd75c2017-03-27 14:43:18 -07009687 netdev_features_t hw_enc_features;
9688 netdev_features_t hw_features;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009689
9690 etherdev_size = sizeof(struct i40e_netdev_priv);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00009691 netdev = alloc_etherdev_mq(etherdev_size, vsi->alloc_queue_pairs);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009692 if (!netdev)
9693 return -ENOMEM;
9694
9695 vsi->netdev = netdev;
9696 np = netdev_priv(netdev);
9697 np->vsi = vsi;
9698
Preethi Banalabacd75c2017-03-27 14:43:18 -07009699 hw_enc_features = NETIF_F_SG |
9700 NETIF_F_IP_CSUM |
9701 NETIF_F_IPV6_CSUM |
9702 NETIF_F_HIGHDMA |
9703 NETIF_F_SOFT_FEATURES |
9704 NETIF_F_TSO |
9705 NETIF_F_TSO_ECN |
9706 NETIF_F_TSO6 |
9707 NETIF_F_GSO_GRE |
9708 NETIF_F_GSO_GRE_CSUM |
9709 NETIF_F_GSO_PARTIAL |
9710 NETIF_F_GSO_UDP_TUNNEL |
9711 NETIF_F_GSO_UDP_TUNNEL_CSUM |
9712 NETIF_F_SCTP_CRC |
9713 NETIF_F_RXHASH |
9714 NETIF_F_RXCSUM |
9715 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009716
Jacob Kellerd36e41d2017-06-23 04:24:46 -04009717 if (!(pf->hw_features & I40E_HW_OUTER_UDP_CSUM_CAPABLE))
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04009718 netdev->gso_partial_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM;
9719
9720 netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009721
Preethi Banalabacd75c2017-03-27 14:43:18 -07009722 netdev->hw_enc_features |= hw_enc_features;
9723
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009724 /* record features VLANs can make use of */
Preethi Banalabacd75c2017-03-27 14:43:18 -07009725 netdev->vlan_features |= hw_enc_features | NETIF_F_TSO_MANGLEID;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009726
Anjali Singhai Jain2e86a0b2014-04-01 07:11:53 +00009727 if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009728 netdev->hw_features |= NETIF_F_NTUPLE;
Preethi Banalabacd75c2017-03-27 14:43:18 -07009729 hw_features = hw_enc_features |
9730 NETIF_F_HW_VLAN_CTAG_TX |
9731 NETIF_F_HW_VLAN_CTAG_RX;
Anjali Singhai Jain2e86a0b2014-04-01 07:11:53 +00009732
Preethi Banalabacd75c2017-03-27 14:43:18 -07009733 netdev->hw_features |= hw_features;
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009734
Preethi Banalabacd75c2017-03-27 14:43:18 -07009735 netdev->features |= hw_features | NETIF_F_HW_VLAN_CTAG_FILTER;
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04009736 netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009737
9738 if (vsi->type == I40E_VSI_MAIN) {
9739 SET_NETDEV_DEV(netdev, &pf->pdev->dev);
Greg Rose9a173902014-05-22 06:32:02 +00009740 ether_addr_copy(mac_addr, hw->mac.perm_addr);
Jacob Keller41c4c2b2017-04-05 07:50:57 -04009741 /* The following steps are necessary for two reasons. First,
9742 * some older NVM configurations load a default MAC-VLAN
9743 * filter that will accept any tagged packet, and we want to
9744 * replace this with a normal filter. Additionally, it is
9745 * possible our MAC address was provided by the platform using
9746 * Open Firmware or similar.
9747 *
9748 * Thus, we need to remove the default filter and install one
9749 * specific to the MAC address.
Jacob Keller1596b5d2016-11-08 13:05:15 -08009750 */
9751 i40e_rm_default_mac_filter(vsi, mac_addr);
Jacob Keller278e7d02016-10-05 09:30:37 -07009752 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller9569a9a2016-11-11 12:39:37 -08009753 i40e_add_mac_filter(vsi, mac_addr);
Jacob Keller278e7d02016-10-05 09:30:37 -07009754 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009755 } else {
Jacob Keller8c9eb352017-07-12 05:46:12 -04009756 /* Relate the VSI_VMDQ name to the VSI_MAIN name. Note that we
9757 * are still limited by IFNAMSIZ, but we're adding 'v%d\0' to
9758 * the end, which is 4 bytes long, so force truncation of the
9759 * original name by IFNAMSIZ - 4
9760 */
9761 snprintf(netdev->name, IFNAMSIZ, "%.*sv%%d",
9762 IFNAMSIZ - 4,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009763 pf->vsi[pf->lan_vsi]->netdev->name);
9764 random_ether_addr(mac_addr);
Kiran Patil21659032015-09-30 14:09:03 -04009765
Jacob Keller278e7d02016-10-05 09:30:37 -07009766 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller9569a9a2016-11-11 12:39:37 -08009767 i40e_add_mac_filter(vsi, mac_addr);
Jacob Keller278e7d02016-10-05 09:30:37 -07009768 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009769 }
Kiran Patil21659032015-09-30 14:09:03 -04009770
Jacob Keller435c0842016-11-08 13:05:10 -08009771 /* Add the broadcast filter so that we initially will receive
9772 * broadcast packets. Note that when a new VLAN is first added the
9773 * driver will convert all filters marked I40E_VLAN_ANY into VLAN
9774 * specific filters as part of transitioning into "vlan" operation.
9775 * When more VLANs are added, the driver will copy each existing MAC
9776 * filter and add it for the new VLAN.
9777 *
9778 * Broadcast filters are handled specially by
9779 * i40e_sync_filters_subtask, as the driver must to set the broadcast
9780 * promiscuous bit instead of adding this directly as a MAC/VLAN
9781 * filter. The subtask will update the correct broadcast promiscuous
9782 * bits as VLANs become active or inactive.
9783 */
9784 eth_broadcast_addr(broadcast);
9785 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller9569a9a2016-11-11 12:39:37 -08009786 i40e_add_mac_filter(vsi, broadcast);
Jacob Keller435c0842016-11-08 13:05:10 -08009787 spin_unlock_bh(&vsi->mac_filter_hash_lock);
9788
Greg Rose9a173902014-05-22 06:32:02 +00009789 ether_addr_copy(netdev->dev_addr, mac_addr);
9790 ether_addr_copy(netdev->perm_addr, mac_addr);
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009791
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009792 netdev->priv_flags |= IFF_UNICAST_FLT;
9793 netdev->priv_flags |= IFF_SUPP_NOFCS;
9794 /* Setup netdev TC information */
9795 i40e_vsi_config_netdev_tc(vsi, vsi->tc_config.enabled_tc);
9796
9797 netdev->netdev_ops = &i40e_netdev_ops;
9798 netdev->watchdog_timeo = 5 * HZ;
9799 i40e_set_ethtool_ops(netdev);
9800
Jarod Wilson91c527a2016-10-17 15:54:05 -04009801 /* MTU range: 68 - 9706 */
9802 netdev->min_mtu = ETH_MIN_MTU;
Mitch Williams1e3a5fd2017-06-23 04:24:43 -04009803 netdev->max_mtu = I40E_MAX_RXBUFFER - I40E_PACKET_HDR_PAD;
Jarod Wilson91c527a2016-10-17 15:54:05 -04009804
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009805 return 0;
9806}
9807
9808/**
9809 * i40e_vsi_delete - Delete a VSI from the switch
9810 * @vsi: the VSI being removed
9811 *
9812 * Returns 0 on success, negative value on failure
9813 **/
9814static void i40e_vsi_delete(struct i40e_vsi *vsi)
9815{
9816 /* remove default VSI is not allowed */
9817 if (vsi == vsi->back->vsi[vsi->back->lan_vsi])
9818 return;
9819
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009820 i40e_aq_delete_element(&vsi->back->hw, vsi->seid, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009821}
9822
9823/**
Neerav Parikh51616012015-02-06 08:52:14 +00009824 * i40e_is_vsi_uplink_mode_veb - Check if the VSI's uplink bridge mode is VEB
9825 * @vsi: the VSI being queried
9826 *
9827 * Returns 1 if HW bridge mode is VEB and return 0 in case of VEPA mode
9828 **/
9829int i40e_is_vsi_uplink_mode_veb(struct i40e_vsi *vsi)
9830{
9831 struct i40e_veb *veb;
9832 struct i40e_pf *pf = vsi->back;
9833
9834 /* Uplink is not a bridge so default to VEB */
9835 if (vsi->veb_idx == I40E_NO_VEB)
9836 return 1;
9837
9838 veb = pf->veb[vsi->veb_idx];
Akeem G Abodunrin09603ea2015-10-01 14:37:36 -04009839 if (!veb) {
9840 dev_info(&pf->pdev->dev,
9841 "There is no veb associated with the bridge\n");
9842 return -ENOENT;
9843 }
Neerav Parikh51616012015-02-06 08:52:14 +00009844
Akeem G Abodunrin09603ea2015-10-01 14:37:36 -04009845 /* Uplink is a bridge in VEPA mode */
9846 if (veb->bridge_mode & BRIDGE_MODE_VEPA) {
9847 return 0;
9848 } else {
9849 /* Uplink is a bridge in VEB mode */
9850 return 1;
9851 }
9852
9853 /* VEPA is now default bridge, so return 0 */
9854 return 0;
Neerav Parikh51616012015-02-06 08:52:14 +00009855}
9856
9857/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009858 * i40e_add_vsi - Add a VSI to the switch
9859 * @vsi: the VSI being configured
9860 *
9861 * This initializes a VSI context depending on the VSI type to be added and
9862 * passes it down to the add_vsi aq command.
9863 **/
9864static int i40e_add_vsi(struct i40e_vsi *vsi)
9865{
9866 int ret = -ENODEV;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009867 struct i40e_pf *pf = vsi->back;
9868 struct i40e_hw *hw = &pf->hw;
9869 struct i40e_vsi_context ctxt;
Jacob Keller278e7d02016-10-05 09:30:37 -07009870 struct i40e_mac_filter *f;
9871 struct hlist_node *h;
9872 int bkt;
Kiran Patil21659032015-09-30 14:09:03 -04009873
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009874 u8 enabled_tc = 0x1; /* TC0 enabled */
9875 int f_count = 0;
9876
9877 memset(&ctxt, 0, sizeof(ctxt));
9878 switch (vsi->type) {
9879 case I40E_VSI_MAIN:
9880 /* The PF's main VSI is already setup as part of the
9881 * device initialization, so we'll not bother with
9882 * the add_vsi call, but we will retrieve the current
9883 * VSI context.
9884 */
9885 ctxt.seid = pf->main_vsi_seid;
9886 ctxt.pf_num = pf->hw.pf_id;
9887 ctxt.vf_num = 0;
9888 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
9889 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
9890 if (ret) {
9891 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009892 "couldn't get PF vsi config, err %s aq_err %s\n",
9893 i40e_stat_str(&pf->hw, ret),
9894 i40e_aq_str(&pf->hw,
9895 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009896 return -ENOENT;
9897 }
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07009898 vsi->info = ctxt.info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009899 vsi->info.valid_sections = 0;
9900
9901 vsi->seid = ctxt.seid;
9902 vsi->id = ctxt.vsi_number;
9903
9904 enabled_tc = i40e_pf_get_tc_map(pf);
9905
9906 /* MFP mode setup queue map and update VSI */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00009907 if ((pf->flags & I40E_FLAG_MFP_ENABLED) &&
9908 !(pf->hw.func_caps.iscsi)) { /* NIC type PF */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009909 memset(&ctxt, 0, sizeof(ctxt));
9910 ctxt.seid = pf->main_vsi_seid;
9911 ctxt.pf_num = pf->hw.pf_id;
9912 ctxt.vf_num = 0;
9913 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
9914 ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
9915 if (ret) {
9916 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009917 "update vsi failed, err %s aq_err %s\n",
9918 i40e_stat_str(&pf->hw, ret),
9919 i40e_aq_str(&pf->hw,
9920 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009921 ret = -ENOENT;
9922 goto err;
9923 }
9924 /* update the local VSI info queue map */
9925 i40e_vsi_update_queue_map(vsi, &ctxt);
9926 vsi->info.valid_sections = 0;
9927 } else {
9928 /* Default/Main VSI is only enabled for TC0
9929 * reconfigure it to enable all TCs that are
9930 * available on the port in SFP mode.
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00009931 * For MFP case the iSCSI PF would use this
9932 * flow to enable LAN+iSCSI TC.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009933 */
9934 ret = i40e_vsi_config_tc(vsi, enabled_tc);
9935 if (ret) {
Carolyn Wyborny19279232017-07-14 09:10:07 -04009936 /* Single TC condition is not fatal,
9937 * message and continue
9938 */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009939 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009940 "failed to configure TCs for main VSI tc_map 0x%08x, err %s aq_err %s\n",
9941 enabled_tc,
9942 i40e_stat_str(&pf->hw, ret),
9943 i40e_aq_str(&pf->hw,
9944 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009945 }
9946 }
9947 break;
9948
9949 case I40E_VSI_FDIR:
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009950 ctxt.pf_num = hw->pf_id;
9951 ctxt.vf_num = 0;
9952 ctxt.uplink_seid = vsi->uplink_seid;
Neerav Parikh2b18e592015-01-24 09:58:38 +00009953 ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009954 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07009955 if ((pf->flags & I40E_FLAG_VEB_MODE_ENABLED) &&
9956 (i40e_is_vsi_uplink_mode_veb(vsi))) {
Neerav Parikh51616012015-02-06 08:52:14 +00009957 ctxt.info.valid_sections |=
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07009958 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
Neerav Parikh51616012015-02-06 08:52:14 +00009959 ctxt.info.switch_id =
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07009960 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
Neerav Parikh51616012015-02-06 08:52:14 +00009961 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009962 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009963 break;
9964
9965 case I40E_VSI_VMDQ2:
9966 ctxt.pf_num = hw->pf_id;
9967 ctxt.vf_num = 0;
9968 ctxt.uplink_seid = vsi->uplink_seid;
Neerav Parikh2b18e592015-01-24 09:58:38 +00009969 ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009970 ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
9971
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009972 /* This VSI is connected to VEB so the switch_id
9973 * should be set to zero by default.
9974 */
Neerav Parikh51616012015-02-06 08:52:14 +00009975 if (i40e_is_vsi_uplink_mode_veb(vsi)) {
9976 ctxt.info.valid_sections |=
9977 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
9978 ctxt.info.switch_id =
9979 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
9980 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009981
9982 /* Setup the VSI tx/rx queue map for TC0 only for now */
9983 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
9984 break;
9985
9986 case I40E_VSI_SRIOV:
9987 ctxt.pf_num = hw->pf_id;
9988 ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
9989 ctxt.uplink_seid = vsi->uplink_seid;
Neerav Parikh2b18e592015-01-24 09:58:38 +00009990 ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009991 ctxt.flags = I40E_AQ_VSI_TYPE_VF;
9992
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009993 /* This VSI is connected to VEB so the switch_id
9994 * should be set to zero by default.
9995 */
Neerav Parikh51616012015-02-06 08:52:14 +00009996 if (i40e_is_vsi_uplink_mode_veb(vsi)) {
9997 ctxt.info.valid_sections |=
9998 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
9999 ctxt.info.switch_id =
10000 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
10001 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010002
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060010003 if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
10004 ctxt.info.valid_sections |=
10005 cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
10006 ctxt.info.queueing_opt_flags |=
Ashish Shah4b28cdb2016-05-03 15:13:17 -070010007 (I40E_AQ_VSI_QUE_OPT_TCP_ENA |
10008 I40E_AQ_VSI_QUE_OPT_RSS_LUT_VSI);
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060010009 }
10010
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010011 ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
10012 ctxt.info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_MODE_ALL;
Mitch Williamsc674d122014-05-20 08:01:40 +000010013 if (pf->vf[vsi->vf_id].spoofchk) {
10014 ctxt.info.valid_sections |=
10015 cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
10016 ctxt.info.sec_flags |=
10017 (I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
10018 I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
10019 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010020 /* Setup the VSI tx/rx queue map for TC0 only for now */
10021 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
10022 break;
10023
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060010024 case I40E_VSI_IWARP:
10025 /* send down message to iWARP */
10026 break;
10027
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010028 default:
10029 return -ENODEV;
10030 }
10031
10032 if (vsi->type != I40E_VSI_MAIN) {
10033 ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
10034 if (ret) {
10035 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010036 "add vsi failed, err %s aq_err %s\n",
10037 i40e_stat_str(&pf->hw, ret),
10038 i40e_aq_str(&pf->hw,
10039 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010040 ret = -ENOENT;
10041 goto err;
10042 }
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -070010043 vsi->info = ctxt.info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010044 vsi->info.valid_sections = 0;
10045 vsi->seid = ctxt.seid;
10046 vsi->id = ctxt.vsi_number;
10047 }
10048
Mitch Williamsc3c7ea22016-06-20 09:10:38 -070010049 vsi->active_filters = 0;
Jacob Keller0da36b92017-04-19 09:25:55 -040010050 clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
Jacob Keller278e7d02016-10-05 09:30:37 -070010051 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010052 /* If macvlan filters already exist, force them to get loaded */
Jacob Keller278e7d02016-10-05 09:30:37 -070010053 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -070010054 f->state = I40E_FILTER_NEW;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010055 f_count++;
10056 }
Jacob Keller278e7d02016-10-05 09:30:37 -070010057 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -040010058
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010059 if (f_count) {
10060 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
10061 pf->flags |= I40E_FLAG_FILTER_SYNC;
10062 }
10063
10064 /* Update VSI BW information */
10065 ret = i40e_vsi_get_bw_info(vsi);
10066 if (ret) {
10067 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010068 "couldn't get vsi bw info, err %s aq_err %s\n",
10069 i40e_stat_str(&pf->hw, ret),
10070 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010071 /* VSI is already added so not tearing that up */
10072 ret = 0;
10073 }
10074
10075err:
10076 return ret;
10077}
10078
10079/**
10080 * i40e_vsi_release - Delete a VSI and free its resources
10081 * @vsi: the VSI being removed
10082 *
10083 * Returns 0 on success or < 0 on error
10084 **/
10085int i40e_vsi_release(struct i40e_vsi *vsi)
10086{
Jacob Keller278e7d02016-10-05 09:30:37 -070010087 struct i40e_mac_filter *f;
10088 struct hlist_node *h;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010089 struct i40e_veb *veb = NULL;
10090 struct i40e_pf *pf;
10091 u16 uplink_seid;
Jacob Keller278e7d02016-10-05 09:30:37 -070010092 int i, n, bkt;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010093
10094 pf = vsi->back;
10095
10096 /* release of a VEB-owner or last VSI is not allowed */
10097 if (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) {
10098 dev_info(&pf->pdev->dev, "VSI %d has existing VEB %d\n",
10099 vsi->seid, vsi->uplink_seid);
10100 return -ENODEV;
10101 }
10102 if (vsi == pf->vsi[pf->lan_vsi] &&
Mauro S. M. Rodrigues9e6c9c02017-05-12 23:26:56 -030010103 !test_bit(__I40E_DOWN, pf->state)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010104 dev_info(&pf->pdev->dev, "Can't remove PF VSI\n");
10105 return -ENODEV;
10106 }
10107
10108 uplink_seid = vsi->uplink_seid;
10109 if (vsi->type != I40E_VSI_SRIOV) {
10110 if (vsi->netdev_registered) {
10111 vsi->netdev_registered = false;
10112 if (vsi->netdev) {
10113 /* results in a call to i40e_close() */
10114 unregister_netdev(vsi->netdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010115 }
10116 } else {
Shannon Nelson90ef8d42014-03-14 07:32:26 +000010117 i40e_vsi_close(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010118 }
10119 i40e_vsi_disable_irq(vsi);
10120 }
10121
Jacob Keller278e7d02016-10-05 09:30:37 -070010122 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller6622f5c2016-10-05 09:30:32 -070010123
10124 /* clear the sync flag on all filters */
10125 if (vsi->netdev) {
10126 __dev_uc_unsync(vsi->netdev, NULL);
10127 __dev_mc_unsync(vsi->netdev, NULL);
10128 }
10129
10130 /* make sure any remaining filters are marked for deletion */
Jacob Keller278e7d02016-10-05 09:30:37 -070010131 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
Jacob Keller290d2552016-10-05 09:30:36 -070010132 __i40e_del_filter(vsi, f);
Jacob Keller6622f5c2016-10-05 09:30:32 -070010133
Jacob Keller278e7d02016-10-05 09:30:37 -070010134 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -040010135
Jesse Brandeburg17652c62015-11-05 17:01:02 -080010136 i40e_sync_vsi_filters(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010137
10138 i40e_vsi_delete(vsi);
10139 i40e_vsi_free_q_vectors(vsi);
Shannon Nelsona4866592014-02-11 08:24:07 +000010140 if (vsi->netdev) {
10141 free_netdev(vsi->netdev);
10142 vsi->netdev = NULL;
10143 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010144 i40e_vsi_clear_rings(vsi);
10145 i40e_vsi_clear(vsi);
10146
10147 /* If this was the last thing on the VEB, except for the
10148 * controlling VSI, remove the VEB, which puts the controlling
10149 * VSI onto the next level down in the switch.
10150 *
10151 * Well, okay, there's one more exception here: don't remove
10152 * the orphan VEBs yet. We'll wait for an explicit remove request
10153 * from up the network stack.
10154 */
Mitch Williams505682c2014-05-20 08:01:37 +000010155 for (n = 0, i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010156 if (pf->vsi[i] &&
10157 pf->vsi[i]->uplink_seid == uplink_seid &&
10158 (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
10159 n++; /* count the VSIs */
10160 }
10161 }
10162 for (i = 0; i < I40E_MAX_VEB; i++) {
10163 if (!pf->veb[i])
10164 continue;
10165 if (pf->veb[i]->uplink_seid == uplink_seid)
10166 n++; /* count the VEBs */
10167 if (pf->veb[i]->seid == uplink_seid)
10168 veb = pf->veb[i];
10169 }
10170 if (n == 0 && veb && veb->uplink_seid != 0)
10171 i40e_veb_release(veb);
10172
10173 return 0;
10174}
10175
10176/**
10177 * i40e_vsi_setup_vectors - Set up the q_vectors for the given VSI
10178 * @vsi: ptr to the VSI
10179 *
10180 * This should only be called after i40e_vsi_mem_alloc() which allocates the
10181 * corresponding SW VSI structure and initializes num_queue_pairs for the
10182 * newly allocated VSI.
10183 *
10184 * Returns 0 on success or negative on failure
10185 **/
10186static int i40e_vsi_setup_vectors(struct i40e_vsi *vsi)
10187{
10188 int ret = -ENOENT;
10189 struct i40e_pf *pf = vsi->back;
10190
Alexander Duyck493fb302013-09-28 07:01:44 +000010191 if (vsi->q_vectors[0]) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010192 dev_info(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
10193 vsi->seid);
10194 return -EEXIST;
10195 }
10196
10197 if (vsi->base_vector) {
Jesse Brandeburgf29eaa32014-02-11 08:24:12 +000010198 dev_info(&pf->pdev->dev, "VSI %d has non-zero base vector %d\n",
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010199 vsi->seid, vsi->base_vector);
10200 return -EEXIST;
10201 }
10202
Greg Rose90e04072014-03-06 08:59:57 +000010203 ret = i40e_vsi_alloc_q_vectors(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010204 if (ret) {
10205 dev_info(&pf->pdev->dev,
10206 "failed to allocate %d q_vector for VSI %d, ret=%d\n",
10207 vsi->num_q_vectors, vsi->seid, ret);
10208 vsi->num_q_vectors = 0;
10209 goto vector_setup_out;
10210 }
10211
Anjali Singhai Jain26cdc442015-07-10 19:36:00 -040010212 /* In Legacy mode, we do not have to get any other vector since we
10213 * piggyback on the misc/ICR0 for queue interrupts.
10214 */
10215 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
10216 return ret;
Shannon Nelson958a3e32013-09-28 07:13:28 +000010217 if (vsi->num_q_vectors)
10218 vsi->base_vector = i40e_get_lump(pf, pf->irq_pile,
10219 vsi->num_q_vectors, vsi->idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010220 if (vsi->base_vector < 0) {
10221 dev_info(&pf->pdev->dev,
Shannon Nelson049a2be2014-10-17 03:14:50 +000010222 "failed to get tracking for %d vectors for VSI %d, err=%d\n",
10223 vsi->num_q_vectors, vsi->seid, vsi->base_vector);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010224 i40e_vsi_free_q_vectors(vsi);
10225 ret = -ENOENT;
10226 goto vector_setup_out;
10227 }
10228
10229vector_setup_out:
10230 return ret;
10231}
10232
10233/**
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010234 * i40e_vsi_reinit_setup - return and reallocate resources for a VSI
10235 * @vsi: pointer to the vsi.
10236 *
10237 * This re-allocates a vsi's queue resources.
10238 *
10239 * Returns pointer to the successfully allocated and configured VSI sw struct
10240 * on success, otherwise returns NULL on failure.
10241 **/
10242static struct i40e_vsi *i40e_vsi_reinit_setup(struct i40e_vsi *vsi)
10243{
Björn Töpel74608d12017-05-24 07:55:35 +020010244 u16 alloc_queue_pairs;
John Underwoodf5340392016-02-18 09:19:24 -080010245 struct i40e_pf *pf;
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010246 u8 enabled_tc;
10247 int ret;
10248
John Underwoodf5340392016-02-18 09:19:24 -080010249 if (!vsi)
10250 return NULL;
10251
10252 pf = vsi->back;
10253
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010254 i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
10255 i40e_vsi_clear_rings(vsi);
10256
10257 i40e_vsi_free_arrays(vsi, false);
10258 i40e_set_num_rings_in_vsi(vsi);
10259 ret = i40e_vsi_alloc_arrays(vsi, false);
10260 if (ret)
10261 goto err_vsi;
10262
Björn Töpel74608d12017-05-24 07:55:35 +020010263 alloc_queue_pairs = vsi->alloc_queue_pairs *
10264 (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
10265
10266 ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010267 if (ret < 0) {
Shannon Nelson049a2be2014-10-17 03:14:50 +000010268 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010269 "failed to get tracking for %d queues for VSI %d err %d\n",
Björn Töpel74608d12017-05-24 07:55:35 +020010270 alloc_queue_pairs, vsi->seid, ret);
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010271 goto err_vsi;
10272 }
10273 vsi->base_queue = ret;
10274
10275 /* Update the FW view of the VSI. Force a reset of TC and queue
10276 * layout configurations.
10277 */
10278 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
10279 pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
10280 pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
10281 i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
Jacob Keller1596b5d2016-11-08 13:05:15 -080010282 if (vsi->type == I40E_VSI_MAIN)
10283 i40e_rm_default_mac_filter(vsi, pf->hw.mac.perm_addr);
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010284
10285 /* assign it some queues */
10286 ret = i40e_alloc_rings(vsi);
10287 if (ret)
10288 goto err_rings;
10289
10290 /* map all of the rings to the q_vectors */
10291 i40e_vsi_map_rings_to_vectors(vsi);
10292 return vsi;
10293
10294err_rings:
10295 i40e_vsi_free_q_vectors(vsi);
10296 if (vsi->netdev_registered) {
10297 vsi->netdev_registered = false;
10298 unregister_netdev(vsi->netdev);
10299 free_netdev(vsi->netdev);
10300 vsi->netdev = NULL;
10301 }
10302 i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
10303err_vsi:
10304 i40e_vsi_clear(vsi);
10305 return NULL;
10306}
10307
10308/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010309 * i40e_vsi_setup - Set up a VSI by a given type
10310 * @pf: board private structure
10311 * @type: VSI type
10312 * @uplink_seid: the switch element to link to
10313 * @param1: usage depends upon VSI type. For VF types, indicates VF id
10314 *
10315 * This allocates the sw VSI structure and its queue resources, then add a VSI
10316 * to the identified VEB.
10317 *
10318 * Returns pointer to the successfully allocated and configure VSI sw struct on
10319 * success, otherwise returns NULL on failure.
10320 **/
10321struct i40e_vsi *i40e_vsi_setup(struct i40e_pf *pf, u8 type,
10322 u16 uplink_seid, u32 param1)
10323{
10324 struct i40e_vsi *vsi = NULL;
10325 struct i40e_veb *veb = NULL;
Björn Töpel74608d12017-05-24 07:55:35 +020010326 u16 alloc_queue_pairs;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010327 int ret, i;
10328 int v_idx;
10329
10330 /* The requested uplink_seid must be either
10331 * - the PF's port seid
10332 * no VEB is needed because this is the PF
10333 * or this is a Flow Director special case VSI
10334 * - seid of an existing VEB
10335 * - seid of a VSI that owns an existing VEB
10336 * - seid of a VSI that doesn't own a VEB
10337 * a new VEB is created and the VSI becomes the owner
10338 * - seid of the PF VSI, which is what creates the first VEB
10339 * this is a special case of the previous
10340 *
10341 * Find which uplink_seid we were given and create a new VEB if needed
10342 */
10343 for (i = 0; i < I40E_MAX_VEB; i++) {
10344 if (pf->veb[i] && pf->veb[i]->seid == uplink_seid) {
10345 veb = pf->veb[i];
10346 break;
10347 }
10348 }
10349
10350 if (!veb && uplink_seid != pf->mac_seid) {
10351
Mitch Williams505682c2014-05-20 08:01:37 +000010352 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010353 if (pf->vsi[i] && pf->vsi[i]->seid == uplink_seid) {
10354 vsi = pf->vsi[i];
10355 break;
10356 }
10357 }
10358 if (!vsi) {
10359 dev_info(&pf->pdev->dev, "no such uplink_seid %d\n",
10360 uplink_seid);
10361 return NULL;
10362 }
10363
10364 if (vsi->uplink_seid == pf->mac_seid)
10365 veb = i40e_veb_setup(pf, 0, pf->mac_seid, vsi->seid,
10366 vsi->tc_config.enabled_tc);
10367 else if ((vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
10368 veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
10369 vsi->tc_config.enabled_tc);
Anjali Singhai Jain79c21a82014-11-13 03:06:14 +000010370 if (veb) {
10371 if (vsi->seid != pf->vsi[pf->lan_vsi]->seid) {
10372 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -040010373 "New VSI creation error, uplink seid of LAN VSI expected.\n");
Anjali Singhai Jain79c21a82014-11-13 03:06:14 +000010374 return NULL;
10375 }
Anjali Singhai Jainfa11cb32015-05-27 12:06:14 -040010376 /* We come up by default in VEPA mode if SRIOV is not
10377 * already enabled, in which case we can't force VEPA
10378 * mode.
10379 */
10380 if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
10381 veb->bridge_mode = BRIDGE_MODE_VEPA;
10382 pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
10383 }
Neerav Parikh51616012015-02-06 08:52:14 +000010384 i40e_config_bridge_mode(veb);
Anjali Singhai Jain79c21a82014-11-13 03:06:14 +000010385 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010386 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
10387 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
10388 veb = pf->veb[i];
10389 }
10390 if (!veb) {
10391 dev_info(&pf->pdev->dev, "couldn't add VEB\n");
10392 return NULL;
10393 }
10394
10395 vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
10396 uplink_seid = veb->seid;
10397 }
10398
10399 /* get vsi sw struct */
10400 v_idx = i40e_vsi_mem_alloc(pf, type);
10401 if (v_idx < 0)
10402 goto err_alloc;
10403 vsi = pf->vsi[v_idx];
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010404 if (!vsi)
10405 goto err_alloc;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010406 vsi->type = type;
10407 vsi->veb_idx = (veb ? veb->idx : I40E_NO_VEB);
10408
10409 if (type == I40E_VSI_MAIN)
10410 pf->lan_vsi = v_idx;
10411 else if (type == I40E_VSI_SRIOV)
10412 vsi->vf_id = param1;
10413 /* assign it some queues */
Björn Töpel74608d12017-05-24 07:55:35 +020010414 alloc_queue_pairs = vsi->alloc_queue_pairs *
10415 (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
10416
10417 ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010418 if (ret < 0) {
Shannon Nelson049a2be2014-10-17 03:14:50 +000010419 dev_info(&pf->pdev->dev,
10420 "failed to get tracking for %d queues for VSI %d err=%d\n",
Björn Töpel74608d12017-05-24 07:55:35 +020010421 alloc_queue_pairs, vsi->seid, ret);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010422 goto err_vsi;
10423 }
10424 vsi->base_queue = ret;
10425
10426 /* get a VSI from the hardware */
10427 vsi->uplink_seid = uplink_seid;
10428 ret = i40e_add_vsi(vsi);
10429 if (ret)
10430 goto err_vsi;
10431
10432 switch (vsi->type) {
10433 /* setup the netdev if needed */
10434 case I40E_VSI_MAIN:
10435 case I40E_VSI_VMDQ2:
10436 ret = i40e_config_netdev(vsi);
10437 if (ret)
10438 goto err_netdev;
10439 ret = register_netdev(vsi->netdev);
10440 if (ret)
10441 goto err_netdev;
10442 vsi->netdev_registered = true;
10443 netif_carrier_off(vsi->netdev);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -080010444#ifdef CONFIG_I40E_DCB
10445 /* Setup DCB netlink interface */
10446 i40e_dcbnl_setup(vsi);
10447#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010448 /* fall through */
10449
10450 case I40E_VSI_FDIR:
10451 /* set up vectors and rings if needed */
10452 ret = i40e_vsi_setup_vectors(vsi);
10453 if (ret)
10454 goto err_msix;
10455
10456 ret = i40e_alloc_rings(vsi);
10457 if (ret)
10458 goto err_rings;
10459
10460 /* map all of the rings to the q_vectors */
10461 i40e_vsi_map_rings_to_vectors(vsi);
10462
10463 i40e_vsi_reset_stats(vsi);
10464 break;
10465
10466 default:
10467 /* no netdev or rings for the other VSI types */
10468 break;
10469 }
10470
Jacob Kellerd36e41d2017-06-23 04:24:46 -040010471 if ((pf->hw_features & I40E_HW_RSS_AQ_CAPABLE) &&
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -040010472 (vsi->type == I40E_VSI_VMDQ2)) {
10473 ret = i40e_vsi_config_rss(vsi);
10474 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010475 return vsi;
10476
10477err_rings:
10478 i40e_vsi_free_q_vectors(vsi);
10479err_msix:
10480 if (vsi->netdev_registered) {
10481 vsi->netdev_registered = false;
10482 unregister_netdev(vsi->netdev);
10483 free_netdev(vsi->netdev);
10484 vsi->netdev = NULL;
10485 }
10486err_netdev:
10487 i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
10488err_vsi:
10489 i40e_vsi_clear(vsi);
10490err_alloc:
10491 return NULL;
10492}
10493
10494/**
10495 * i40e_veb_get_bw_info - Query VEB BW information
10496 * @veb: the veb to query
10497 *
10498 * Query the Tx scheduler BW configuration data for given VEB
10499 **/
10500static int i40e_veb_get_bw_info(struct i40e_veb *veb)
10501{
10502 struct i40e_aqc_query_switching_comp_ets_config_resp ets_data;
10503 struct i40e_aqc_query_switching_comp_bw_config_resp bw_data;
10504 struct i40e_pf *pf = veb->pf;
10505 struct i40e_hw *hw = &pf->hw;
10506 u32 tc_bw_max;
10507 int ret = 0;
10508 int i;
10509
10510 ret = i40e_aq_query_switch_comp_bw_config(hw, veb->seid,
10511 &bw_data, NULL);
10512 if (ret) {
10513 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010514 "query veb bw config failed, err %s aq_err %s\n",
10515 i40e_stat_str(&pf->hw, ret),
10516 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010517 goto out;
10518 }
10519
10520 ret = i40e_aq_query_switch_comp_ets_config(hw, veb->seid,
10521 &ets_data, NULL);
10522 if (ret) {
10523 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010524 "query veb bw ets config failed, err %s aq_err %s\n",
10525 i40e_stat_str(&pf->hw, ret),
10526 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010527 goto out;
10528 }
10529
10530 veb->bw_limit = le16_to_cpu(ets_data.port_bw_limit);
10531 veb->bw_max_quanta = ets_data.tc_bw_max;
10532 veb->is_abs_credits = bw_data.absolute_credits_enable;
Neerav Parikh23cd1f02014-11-12 00:18:41 +000010533 veb->enabled_tc = ets_data.tc_valid_bits;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010534 tc_bw_max = le16_to_cpu(bw_data.tc_bw_max[0]) |
10535 (le16_to_cpu(bw_data.tc_bw_max[1]) << 16);
10536 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
10537 veb->bw_tc_share_credits[i] = bw_data.tc_bw_share_credits[i];
10538 veb->bw_tc_limit_credits[i] =
10539 le16_to_cpu(bw_data.tc_bw_limits[i]);
10540 veb->bw_tc_max_quanta[i] = ((tc_bw_max >> (i*4)) & 0x7);
10541 }
10542
10543out:
10544 return ret;
10545}
10546
10547/**
10548 * i40e_veb_mem_alloc - Allocates the next available struct veb in the PF
10549 * @pf: board private structure
10550 *
10551 * On error: returns error code (negative)
10552 * On success: returns vsi index in PF (positive)
10553 **/
10554static int i40e_veb_mem_alloc(struct i40e_pf *pf)
10555{
10556 int ret = -ENOENT;
10557 struct i40e_veb *veb;
10558 int i;
10559
10560 /* Need to protect the allocation of switch elements at the PF level */
10561 mutex_lock(&pf->switch_mutex);
10562
10563 /* VEB list may be fragmented if VEB creation/destruction has
10564 * been happening. We can afford to do a quick scan to look
10565 * for any free slots in the list.
10566 *
10567 * find next empty veb slot, looping back around if necessary
10568 */
10569 i = 0;
10570 while ((i < I40E_MAX_VEB) && (pf->veb[i] != NULL))
10571 i++;
10572 if (i >= I40E_MAX_VEB) {
10573 ret = -ENOMEM;
10574 goto err_alloc_veb; /* out of VEB slots! */
10575 }
10576
10577 veb = kzalloc(sizeof(*veb), GFP_KERNEL);
10578 if (!veb) {
10579 ret = -ENOMEM;
10580 goto err_alloc_veb;
10581 }
10582 veb->pf = pf;
10583 veb->idx = i;
10584 veb->enabled_tc = 1;
10585
10586 pf->veb[i] = veb;
10587 ret = i;
10588err_alloc_veb:
10589 mutex_unlock(&pf->switch_mutex);
10590 return ret;
10591}
10592
10593/**
10594 * i40e_switch_branch_release - Delete a branch of the switch tree
10595 * @branch: where to start deleting
10596 *
10597 * This uses recursion to find the tips of the branch to be
10598 * removed, deleting until we get back to and can delete this VEB.
10599 **/
10600static void i40e_switch_branch_release(struct i40e_veb *branch)
10601{
10602 struct i40e_pf *pf = branch->pf;
10603 u16 branch_seid = branch->seid;
10604 u16 veb_idx = branch->idx;
10605 int i;
10606
10607 /* release any VEBs on this VEB - RECURSION */
10608 for (i = 0; i < I40E_MAX_VEB; i++) {
10609 if (!pf->veb[i])
10610 continue;
10611 if (pf->veb[i]->uplink_seid == branch->seid)
10612 i40e_switch_branch_release(pf->veb[i]);
10613 }
10614
10615 /* Release the VSIs on this VEB, but not the owner VSI.
10616 *
10617 * NOTE: Removing the last VSI on a VEB has the SIDE EFFECT of removing
10618 * the VEB itself, so don't use (*branch) after this loop.
10619 */
Mitch Williams505682c2014-05-20 08:01:37 +000010620 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010621 if (!pf->vsi[i])
10622 continue;
10623 if (pf->vsi[i]->uplink_seid == branch_seid &&
10624 (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
10625 i40e_vsi_release(pf->vsi[i]);
10626 }
10627 }
10628
10629 /* There's one corner case where the VEB might not have been
10630 * removed, so double check it here and remove it if needed.
10631 * This case happens if the veb was created from the debugfs
10632 * commands and no VSIs were added to it.
10633 */
10634 if (pf->veb[veb_idx])
10635 i40e_veb_release(pf->veb[veb_idx]);
10636}
10637
10638/**
10639 * i40e_veb_clear - remove veb struct
10640 * @veb: the veb to remove
10641 **/
10642static void i40e_veb_clear(struct i40e_veb *veb)
10643{
10644 if (!veb)
10645 return;
10646
10647 if (veb->pf) {
10648 struct i40e_pf *pf = veb->pf;
10649
10650 mutex_lock(&pf->switch_mutex);
10651 if (pf->veb[veb->idx] == veb)
10652 pf->veb[veb->idx] = NULL;
10653 mutex_unlock(&pf->switch_mutex);
10654 }
10655
10656 kfree(veb);
10657}
10658
10659/**
10660 * i40e_veb_release - Delete a VEB and free its resources
10661 * @veb: the VEB being removed
10662 **/
10663void i40e_veb_release(struct i40e_veb *veb)
10664{
10665 struct i40e_vsi *vsi = NULL;
10666 struct i40e_pf *pf;
10667 int i, n = 0;
10668
10669 pf = veb->pf;
10670
10671 /* find the remaining VSI and check for extras */
Mitch Williams505682c2014-05-20 08:01:37 +000010672 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010673 if (pf->vsi[i] && pf->vsi[i]->uplink_seid == veb->seid) {
10674 n++;
10675 vsi = pf->vsi[i];
10676 }
10677 }
10678 if (n != 1) {
10679 dev_info(&pf->pdev->dev,
10680 "can't remove VEB %d with %d VSIs left\n",
10681 veb->seid, n);
10682 return;
10683 }
10684
10685 /* move the remaining VSI to uplink veb */
10686 vsi->flags &= ~I40E_VSI_FLAG_VEB_OWNER;
10687 if (veb->uplink_seid) {
10688 vsi->uplink_seid = veb->uplink_seid;
10689 if (veb->uplink_seid == pf->mac_seid)
10690 vsi->veb_idx = I40E_NO_VEB;
10691 else
10692 vsi->veb_idx = veb->veb_idx;
10693 } else {
10694 /* floating VEB */
10695 vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
10696 vsi->veb_idx = pf->vsi[pf->lan_vsi]->veb_idx;
10697 }
10698
10699 i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
10700 i40e_veb_clear(veb);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010701}
10702
10703/**
10704 * i40e_add_veb - create the VEB in the switch
10705 * @veb: the VEB to be instantiated
10706 * @vsi: the controlling VSI
10707 **/
10708static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi)
10709{
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010710 struct i40e_pf *pf = veb->pf;
Shannon Nelson66fc3602016-01-13 16:51:42 -080010711 bool enable_stats = !!(pf->flags & I40E_FLAG_VEB_STATS_ENABLED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010712 int ret;
10713
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010714 ret = i40e_aq_add_veb(&pf->hw, veb->uplink_seid, vsi->seid,
Mitch Williams5bc16032016-05-16 10:26:43 -070010715 veb->enabled_tc, false,
Shannon Nelson66fc3602016-01-13 16:51:42 -080010716 &veb->seid, enable_stats, NULL);
Mitch Williams5bc16032016-05-16 10:26:43 -070010717
10718 /* get a VEB from the hardware */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010719 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010720 dev_info(&pf->pdev->dev,
10721 "couldn't add VEB, err %s aq_err %s\n",
10722 i40e_stat_str(&pf->hw, ret),
10723 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010724 return -EPERM;
10725 }
10726
10727 /* get statistics counter */
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010728 ret = i40e_aq_get_veb_parameters(&pf->hw, veb->seid, NULL, NULL,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010729 &veb->stats_idx, NULL, NULL, NULL);
10730 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010731 dev_info(&pf->pdev->dev,
10732 "couldn't get VEB statistics idx, err %s aq_err %s\n",
10733 i40e_stat_str(&pf->hw, ret),
10734 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010735 return -EPERM;
10736 }
10737 ret = i40e_veb_get_bw_info(veb);
10738 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010739 dev_info(&pf->pdev->dev,
10740 "couldn't get VEB bw info, err %s aq_err %s\n",
10741 i40e_stat_str(&pf->hw, ret),
10742 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10743 i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010744 return -ENOENT;
10745 }
10746
10747 vsi->uplink_seid = veb->seid;
10748 vsi->veb_idx = veb->idx;
10749 vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
10750
10751 return 0;
10752}
10753
10754/**
10755 * i40e_veb_setup - Set up a VEB
10756 * @pf: board private structure
10757 * @flags: VEB setup flags
10758 * @uplink_seid: the switch element to link to
10759 * @vsi_seid: the initial VSI seid
10760 * @enabled_tc: Enabled TC bit-map
10761 *
10762 * This allocates the sw VEB structure and links it into the switch
10763 * It is possible and legal for this to be a duplicate of an already
10764 * existing VEB. It is also possible for both uplink and vsi seids
10765 * to be zero, in order to create a floating VEB.
10766 *
10767 * Returns pointer to the successfully allocated VEB sw struct on
10768 * success, otherwise returns NULL on failure.
10769 **/
10770struct i40e_veb *i40e_veb_setup(struct i40e_pf *pf, u16 flags,
10771 u16 uplink_seid, u16 vsi_seid,
10772 u8 enabled_tc)
10773{
10774 struct i40e_veb *veb, *uplink_veb = NULL;
10775 int vsi_idx, veb_idx;
10776 int ret;
10777
10778 /* if one seid is 0, the other must be 0 to create a floating relay */
10779 if ((uplink_seid == 0 || vsi_seid == 0) &&
10780 (uplink_seid + vsi_seid != 0)) {
10781 dev_info(&pf->pdev->dev,
10782 "one, not both seid's are 0: uplink=%d vsi=%d\n",
10783 uplink_seid, vsi_seid);
10784 return NULL;
10785 }
10786
10787 /* make sure there is such a vsi and uplink */
Mitch Williams505682c2014-05-20 08:01:37 +000010788 for (vsi_idx = 0; vsi_idx < pf->num_alloc_vsi; vsi_idx++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010789 if (pf->vsi[vsi_idx] && pf->vsi[vsi_idx]->seid == vsi_seid)
10790 break;
Mitch Williams505682c2014-05-20 08:01:37 +000010791 if (vsi_idx >= pf->num_alloc_vsi && vsi_seid != 0) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010792 dev_info(&pf->pdev->dev, "vsi seid %d not found\n",
10793 vsi_seid);
10794 return NULL;
10795 }
10796
10797 if (uplink_seid && uplink_seid != pf->mac_seid) {
10798 for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
10799 if (pf->veb[veb_idx] &&
10800 pf->veb[veb_idx]->seid == uplink_seid) {
10801 uplink_veb = pf->veb[veb_idx];
10802 break;
10803 }
10804 }
10805 if (!uplink_veb) {
10806 dev_info(&pf->pdev->dev,
10807 "uplink seid %d not found\n", uplink_seid);
10808 return NULL;
10809 }
10810 }
10811
10812 /* get veb sw struct */
10813 veb_idx = i40e_veb_mem_alloc(pf);
10814 if (veb_idx < 0)
10815 goto err_alloc;
10816 veb = pf->veb[veb_idx];
10817 veb->flags = flags;
10818 veb->uplink_seid = uplink_seid;
10819 veb->veb_idx = (uplink_veb ? uplink_veb->idx : I40E_NO_VEB);
10820 veb->enabled_tc = (enabled_tc ? enabled_tc : 0x1);
10821
10822 /* create the VEB in the switch */
10823 ret = i40e_add_veb(veb, pf->vsi[vsi_idx]);
10824 if (ret)
10825 goto err_veb;
Shannon Nelson1bb8b932014-04-23 04:49:54 +000010826 if (vsi_idx == pf->lan_vsi)
10827 pf->lan_veb = veb->idx;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010828
10829 return veb;
10830
10831err_veb:
10832 i40e_veb_clear(veb);
10833err_alloc:
10834 return NULL;
10835}
10836
10837/**
Jeff Kirsherb40c82e62015-02-27 09:18:34 +000010838 * i40e_setup_pf_switch_element - set PF vars based on switch type
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010839 * @pf: board private structure
10840 * @ele: element we are building info from
10841 * @num_reported: total number of elements
10842 * @printconfig: should we print the contents
10843 *
10844 * helper function to assist in extracting a few useful SEID values.
10845 **/
10846static void i40e_setup_pf_switch_element(struct i40e_pf *pf,
10847 struct i40e_aqc_switch_config_element_resp *ele,
10848 u16 num_reported, bool printconfig)
10849{
10850 u16 downlink_seid = le16_to_cpu(ele->downlink_seid);
10851 u16 uplink_seid = le16_to_cpu(ele->uplink_seid);
10852 u8 element_type = ele->element_type;
10853 u16 seid = le16_to_cpu(ele->seid);
10854
10855 if (printconfig)
10856 dev_info(&pf->pdev->dev,
10857 "type=%d seid=%d uplink=%d downlink=%d\n",
10858 element_type, seid, uplink_seid, downlink_seid);
10859
10860 switch (element_type) {
10861 case I40E_SWITCH_ELEMENT_TYPE_MAC:
10862 pf->mac_seid = seid;
10863 break;
10864 case I40E_SWITCH_ELEMENT_TYPE_VEB:
10865 /* Main VEB? */
10866 if (uplink_seid != pf->mac_seid)
10867 break;
10868 if (pf->lan_veb == I40E_NO_VEB) {
10869 int v;
10870
10871 /* find existing or else empty VEB */
10872 for (v = 0; v < I40E_MAX_VEB; v++) {
10873 if (pf->veb[v] && (pf->veb[v]->seid == seid)) {
10874 pf->lan_veb = v;
10875 break;
10876 }
10877 }
10878 if (pf->lan_veb == I40E_NO_VEB) {
10879 v = i40e_veb_mem_alloc(pf);
10880 if (v < 0)
10881 break;
10882 pf->lan_veb = v;
10883 }
10884 }
10885
10886 pf->veb[pf->lan_veb]->seid = seid;
10887 pf->veb[pf->lan_veb]->uplink_seid = pf->mac_seid;
10888 pf->veb[pf->lan_veb]->pf = pf;
10889 pf->veb[pf->lan_veb]->veb_idx = I40E_NO_VEB;
10890 break;
10891 case I40E_SWITCH_ELEMENT_TYPE_VSI:
10892 if (num_reported != 1)
10893 break;
10894 /* This is immediately after a reset so we can assume this is
10895 * the PF's VSI
10896 */
10897 pf->mac_seid = uplink_seid;
10898 pf->pf_seid = downlink_seid;
10899 pf->main_vsi_seid = seid;
10900 if (printconfig)
10901 dev_info(&pf->pdev->dev,
10902 "pf_seid=%d main_vsi_seid=%d\n",
10903 pf->pf_seid, pf->main_vsi_seid);
10904 break;
10905 case I40E_SWITCH_ELEMENT_TYPE_PF:
10906 case I40E_SWITCH_ELEMENT_TYPE_VF:
10907 case I40E_SWITCH_ELEMENT_TYPE_EMP:
10908 case I40E_SWITCH_ELEMENT_TYPE_BMC:
10909 case I40E_SWITCH_ELEMENT_TYPE_PE:
10910 case I40E_SWITCH_ELEMENT_TYPE_PA:
10911 /* ignore these for now */
10912 break;
10913 default:
10914 dev_info(&pf->pdev->dev, "unknown element type=%d seid=%d\n",
10915 element_type, seid);
10916 break;
10917 }
10918}
10919
10920/**
10921 * i40e_fetch_switch_configuration - Get switch config from firmware
10922 * @pf: board private structure
10923 * @printconfig: should we print the contents
10924 *
10925 * Get the current switch configuration from the device and
10926 * extract a few useful SEID values.
10927 **/
10928int i40e_fetch_switch_configuration(struct i40e_pf *pf, bool printconfig)
10929{
10930 struct i40e_aqc_get_switch_config_resp *sw_config;
10931 u16 next_seid = 0;
10932 int ret = 0;
10933 u8 *aq_buf;
10934 int i;
10935
10936 aq_buf = kzalloc(I40E_AQ_LARGE_BUF, GFP_KERNEL);
10937 if (!aq_buf)
10938 return -ENOMEM;
10939
10940 sw_config = (struct i40e_aqc_get_switch_config_resp *)aq_buf;
10941 do {
10942 u16 num_reported, num_total;
10943
10944 ret = i40e_aq_get_switch_config(&pf->hw, sw_config,
10945 I40E_AQ_LARGE_BUF,
10946 &next_seid, NULL);
10947 if (ret) {
10948 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010949 "get switch config failed err %s aq_err %s\n",
10950 i40e_stat_str(&pf->hw, ret),
10951 i40e_aq_str(&pf->hw,
10952 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010953 kfree(aq_buf);
10954 return -ENOENT;
10955 }
10956
10957 num_reported = le16_to_cpu(sw_config->header.num_reported);
10958 num_total = le16_to_cpu(sw_config->header.num_total);
10959
10960 if (printconfig)
10961 dev_info(&pf->pdev->dev,
10962 "header: %d reported %d total\n",
10963 num_reported, num_total);
10964
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010965 for (i = 0; i < num_reported; i++) {
10966 struct i40e_aqc_switch_config_element_resp *ele =
10967 &sw_config->element[i];
10968
10969 i40e_setup_pf_switch_element(pf, ele, num_reported,
10970 printconfig);
10971 }
10972 } while (next_seid != 0);
10973
10974 kfree(aq_buf);
10975 return ret;
10976}
10977
10978/**
10979 * i40e_setup_pf_switch - Setup the HW switch on startup or after reset
10980 * @pf: board private structure
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010981 * @reinit: if the Main VSI needs to re-initialized.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010982 *
10983 * Returns 0 on success, negative value on failure
10984 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010985static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010986{
Anjali Singhai Jainb5569892016-05-03 15:13:12 -070010987 u16 flags = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010988 int ret;
10989
10990 /* find out what's out there already */
10991 ret = i40e_fetch_switch_configuration(pf, false);
10992 if (ret) {
10993 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010994 "couldn't fetch switch config, err %s aq_err %s\n",
10995 i40e_stat_str(&pf->hw, ret),
10996 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010997 return ret;
10998 }
10999 i40e_pf_reset_stats(pf);
11000
Anjali Singhai Jainb5569892016-05-03 15:13:12 -070011001 /* set the switch config bit for the whole device to
11002 * support limited promisc or true promisc
11003 * when user requests promisc. The default is limited
11004 * promisc.
11005 */
11006
11007 if ((pf->hw.pf_id == 0) &&
11008 !(pf->flags & I40E_FLAG_TRUE_PROMISC_SUPPORT))
11009 flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
11010
11011 if (pf->hw.pf_id == 0) {
11012 u16 valid_flags;
11013
11014 valid_flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
11015 ret = i40e_aq_set_switch_config(&pf->hw, flags, valid_flags,
11016 NULL);
11017 if (ret && pf->hw.aq.asq_last_status != I40E_AQ_RC_ESRCH) {
11018 dev_info(&pf->pdev->dev,
11019 "couldn't set switch config bits, err %s aq_err %s\n",
11020 i40e_stat_str(&pf->hw, ret),
11021 i40e_aq_str(&pf->hw,
11022 pf->hw.aq.asq_last_status));
11023 /* not a fatal problem, just keep going */
11024 }
11025 }
11026
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011027 /* first time setup */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000011028 if (pf->lan_vsi == I40E_NO_VSI || reinit) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011029 struct i40e_vsi *vsi = NULL;
11030 u16 uplink_seid;
11031
11032 /* Set up the PF VSI associated with the PF's main VSI
11033 * that is already in the HW switch
11034 */
11035 if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
11036 uplink_seid = pf->veb[pf->lan_veb]->seid;
11037 else
11038 uplink_seid = pf->mac_seid;
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000011039 if (pf->lan_vsi == I40E_NO_VSI)
11040 vsi = i40e_vsi_setup(pf, I40E_VSI_MAIN, uplink_seid, 0);
11041 else if (reinit)
11042 vsi = i40e_vsi_reinit_setup(pf->vsi[pf->lan_vsi]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011043 if (!vsi) {
11044 dev_info(&pf->pdev->dev, "setup of MAIN VSI failed\n");
11045 i40e_fdir_teardown(pf);
11046 return -EAGAIN;
11047 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011048 } else {
11049 /* force a reset of TC and queue layout configurations */
11050 u8 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
Jesse Brandeburg6995b362015-08-28 17:55:54 -040011051
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011052 pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
11053 pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
11054 i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
11055 }
11056 i40e_vlan_stripping_disable(pf->vsi[pf->lan_vsi]);
11057
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080011058 i40e_fdir_sb_setup(pf);
11059
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011060 /* Setup static PF queue filter control settings */
11061 ret = i40e_setup_pf_filter_control(pf);
11062 if (ret) {
11063 dev_info(&pf->pdev->dev, "setup_pf_filter_control failed: %d\n",
11064 ret);
11065 /* Failure here should not stop continuing other steps */
11066 }
11067
11068 /* enable RSS in the HW, even for only one queue, as the stack can use
11069 * the hash
11070 */
11071 if ((pf->flags & I40E_FLAG_RSS_ENABLED))
Helin Zhang043dd652015-10-21 19:56:23 -040011072 i40e_pf_config_rss(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011073
11074 /* fill in link information and enable LSE reporting */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011075 i40e_link_event(pf);
11076
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +000011077 /* Initialize user-specific link properties */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011078 pf->fc_autoneg_status = ((pf->hw.phy.link_info.an_info &
11079 I40E_AQ_AN_COMPLETED) ? true : false);
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +000011080
Jacob Kellerbeb0dff2014-01-11 05:43:19 +000011081 i40e_ptp_init(pf);
11082
Alexander Duyck1f190d92017-04-19 09:25:51 -040011083 /* repopulate tunnel port filters */
11084 i40e_sync_udp_filters(pf);
11085
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011086 return ret;
11087}
11088
11089/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011090 * i40e_determine_queue_usage - Work out queue distribution
11091 * @pf: board private structure
11092 **/
11093static void i40e_determine_queue_usage(struct i40e_pf *pf)
11094{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011095 int queues_left;
Shannon Nelsone50d5752017-07-24 18:17:42 -070011096 int q_max;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011097
11098 pf->num_lan_qps = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011099
11100 /* Find the max queues to be put into basic use. We'll always be
11101 * using TC0, whether or not DCB is running, and TC0 will get the
11102 * big RSS set.
11103 */
11104 queues_left = pf->hw.func_caps.num_tx_qp;
11105
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080011106 if ((queues_left == 1) ||
Frank Zhang9aa7e932014-05-20 08:01:42 +000011107 !(pf->flags & I40E_FLAG_MSIX_ENABLED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011108 /* one qp for PF, no queues for anything else */
11109 queues_left = 0;
Helin Zhangacd65442015-10-26 19:44:28 -040011110 pf->alloc_rss_size = pf->num_lan_qps = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011111
11112 /* make sure all the fancies are disabled */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080011113 pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060011114 I40E_FLAG_IWARP_ENABLED |
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080011115 I40E_FLAG_FD_SB_ENABLED |
11116 I40E_FLAG_FD_ATR_ENABLED |
Neerav Parikh4d9b6042014-05-22 06:31:51 +000011117 I40E_FLAG_DCB_CAPABLE |
Dave Ertmana0362442016-08-29 17:38:26 -070011118 I40E_FLAG_DCB_ENABLED |
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080011119 I40E_FLAG_SRIOV_ENABLED |
11120 I40E_FLAG_VMDQ_ENABLED);
Frank Zhang9aa7e932014-05-20 08:01:42 +000011121 } else if (!(pf->flags & (I40E_FLAG_RSS_ENABLED |
11122 I40E_FLAG_FD_SB_ENABLED |
Anjali Singhai Jainbbe7d0e2014-05-20 08:01:44 +000011123 I40E_FLAG_FD_ATR_ENABLED |
Neerav Parikh4d9b6042014-05-22 06:31:51 +000011124 I40E_FLAG_DCB_CAPABLE))) {
Frank Zhang9aa7e932014-05-20 08:01:42 +000011125 /* one qp for PF */
Helin Zhangacd65442015-10-26 19:44:28 -040011126 pf->alloc_rss_size = pf->num_lan_qps = 1;
Frank Zhang9aa7e932014-05-20 08:01:42 +000011127 queues_left -= pf->num_lan_qps;
11128
11129 pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060011130 I40E_FLAG_IWARP_ENABLED |
Frank Zhang9aa7e932014-05-20 08:01:42 +000011131 I40E_FLAG_FD_SB_ENABLED |
11132 I40E_FLAG_FD_ATR_ENABLED |
11133 I40E_FLAG_DCB_ENABLED |
11134 I40E_FLAG_VMDQ_ENABLED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011135 } else {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080011136 /* Not enough queues for all TCs */
Neerav Parikh4d9b6042014-05-22 06:31:51 +000011137 if ((pf->flags & I40E_FLAG_DCB_CAPABLE) &&
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080011138 (queues_left < I40E_MAX_TRAFFIC_CLASS)) {
Dave Ertmana0362442016-08-29 17:38:26 -070011139 pf->flags &= ~(I40E_FLAG_DCB_CAPABLE |
11140 I40E_FLAG_DCB_ENABLED);
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080011141 dev_info(&pf->pdev->dev, "not enough queues for DCB. DCB is disabled.\n");
11142 }
Shannon Nelsone50d5752017-07-24 18:17:42 -070011143
11144 /* limit lan qps to the smaller of qps, cpus or msix */
11145 q_max = max_t(int, pf->rss_size_max, num_online_cpus());
11146 q_max = min_t(int, q_max, pf->hw.func_caps.num_tx_qp);
11147 q_max = min_t(int, q_max, pf->hw.func_caps.num_msix_vectors);
11148 pf->num_lan_qps = q_max;
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +000011149
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080011150 queues_left -= pf->num_lan_qps;
11151 }
11152
11153 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
11154 if (queues_left > 1) {
11155 queues_left -= 1; /* save 1 queue for FD */
11156 } else {
11157 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
11158 dev_info(&pf->pdev->dev, "not enough queues for Flow Director. Flow Director feature is disabled\n");
11159 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011160 }
11161
11162 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
11163 pf->num_vf_qps && pf->num_req_vfs && queues_left) {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080011164 pf->num_req_vfs = min_t(int, pf->num_req_vfs,
11165 (queues_left / pf->num_vf_qps));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011166 queues_left -= (pf->num_req_vfs * pf->num_vf_qps);
11167 }
11168
11169 if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
11170 pf->num_vmdq_vsis && pf->num_vmdq_qps && queues_left) {
11171 pf->num_vmdq_vsis = min_t(int, pf->num_vmdq_vsis,
11172 (queues_left / pf->num_vmdq_qps));
11173 queues_left -= (pf->num_vmdq_vsis * pf->num_vmdq_qps);
11174 }
11175
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +000011176 pf->queues_left = queues_left;
Neerav Parikh8279e492015-09-03 17:18:50 -040011177 dev_dbg(&pf->pdev->dev,
11178 "qs_avail=%d FD SB=%d lan_qs=%d lan_tc0=%d vf=%d*%d vmdq=%d*%d, remaining=%d\n",
11179 pf->hw.func_caps.num_tx_qp,
11180 !!(pf->flags & I40E_FLAG_FD_SB_ENABLED),
Helin Zhangacd65442015-10-26 19:44:28 -040011181 pf->num_lan_qps, pf->alloc_rss_size, pf->num_req_vfs,
11182 pf->num_vf_qps, pf->num_vmdq_vsis, pf->num_vmdq_qps,
11183 queues_left);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011184}
11185
11186/**
11187 * i40e_setup_pf_filter_control - Setup PF static filter control
11188 * @pf: PF to be setup
11189 *
Jeff Kirsherb40c82e62015-02-27 09:18:34 +000011190 * i40e_setup_pf_filter_control sets up a PF's initial filter control
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011191 * settings. If PE/FCoE are enabled then it will also set the per PF
11192 * based filter sizes required for them. It also enables Flow director,
11193 * ethertype and macvlan type filter settings for the pf.
11194 *
11195 * Returns 0 on success, negative on failure
11196 **/
11197static int i40e_setup_pf_filter_control(struct i40e_pf *pf)
11198{
11199 struct i40e_filter_control_settings *settings = &pf->filter_settings;
11200
11201 settings->hash_lut_size = I40E_HASH_LUT_SIZE_128;
11202
11203 /* Flow Director is enabled */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080011204 if (pf->flags & (I40E_FLAG_FD_SB_ENABLED | I40E_FLAG_FD_ATR_ENABLED))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011205 settings->enable_fdir = true;
11206
11207 /* Ethtype and MACVLAN filters enabled for PF */
11208 settings->enable_ethtype = true;
11209 settings->enable_macvlan = true;
11210
11211 if (i40e_set_filter_control(&pf->hw, settings))
11212 return -ENOENT;
11213
11214 return 0;
11215}
11216
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000011217#define INFO_STRING_LEN 255
Shannon Nelson7fd89542015-10-21 19:47:04 -040011218#define REMAIN(__x) (INFO_STRING_LEN - (__x))
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000011219static void i40e_print_features(struct i40e_pf *pf)
11220{
11221 struct i40e_hw *hw = &pf->hw;
Joe Perches3b195842015-12-03 04:20:57 -080011222 char *buf;
11223 int i;
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000011224
Joe Perches3b195842015-12-03 04:20:57 -080011225 buf = kmalloc(INFO_STRING_LEN, GFP_KERNEL);
11226 if (!buf)
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000011227 return;
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000011228
Joe Perches3b195842015-12-03 04:20:57 -080011229 i = snprintf(buf, INFO_STRING_LEN, "Features: PF-id[%d]", hw->pf_id);
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000011230#ifdef CONFIG_PCI_IOV
Joe Perches3b195842015-12-03 04:20:57 -080011231 i += snprintf(&buf[i], REMAIN(i), " VFs: %d", pf->num_req_vfs);
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000011232#endif
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -070011233 i += snprintf(&buf[i], REMAIN(i), " VSIs: %d QP: %d",
Shannon Nelson7fd89542015-10-21 19:47:04 -040011234 pf->hw.func_caps.num_vsis,
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -070011235 pf->vsi[pf->lan_vsi]->num_queue_pairs);
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000011236 if (pf->flags & I40E_FLAG_RSS_ENABLED)
Joe Perches3b195842015-12-03 04:20:57 -080011237 i += snprintf(&buf[i], REMAIN(i), " RSS");
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000011238 if (pf->flags & I40E_FLAG_FD_ATR_ENABLED)
Joe Perches3b195842015-12-03 04:20:57 -080011239 i += snprintf(&buf[i], REMAIN(i), " FD_ATR");
Akeem G Abodunrinc6423ff2014-05-10 04:49:08 +000011240 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
Joe Perches3b195842015-12-03 04:20:57 -080011241 i += snprintf(&buf[i], REMAIN(i), " FD_SB");
11242 i += snprintf(&buf[i], REMAIN(i), " NTUPLE");
Akeem G Abodunrinc6423ff2014-05-10 04:49:08 +000011243 }
Neerav Parikh4d9b6042014-05-22 06:31:51 +000011244 if (pf->flags & I40E_FLAG_DCB_CAPABLE)
Joe Perches3b195842015-12-03 04:20:57 -080011245 i += snprintf(&buf[i], REMAIN(i), " DCB");
Joe Perches3b195842015-12-03 04:20:57 -080011246 i += snprintf(&buf[i], REMAIN(i), " VxLAN");
Singhai, Anjali6a899022015-12-14 12:21:18 -080011247 i += snprintf(&buf[i], REMAIN(i), " Geneve");
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000011248 if (pf->flags & I40E_FLAG_PTP)
Joe Perches3b195842015-12-03 04:20:57 -080011249 i += snprintf(&buf[i], REMAIN(i), " PTP");
Shannon Nelson6dec1012015-09-28 14:12:30 -040011250 if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
Joe Perches3b195842015-12-03 04:20:57 -080011251 i += snprintf(&buf[i], REMAIN(i), " VEB");
Shannon Nelson6dec1012015-09-28 14:12:30 -040011252 else
Joe Perches3b195842015-12-03 04:20:57 -080011253 i += snprintf(&buf[i], REMAIN(i), " VEPA");
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000011254
Joe Perches3b195842015-12-03 04:20:57 -080011255 dev_info(&pf->pdev->dev, "%s\n", buf);
11256 kfree(buf);
Shannon Nelson7fd89542015-10-21 19:47:04 -040011257 WARN_ON(i > INFO_STRING_LEN);
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000011258}
11259
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011260/**
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050011261 * i40e_get_platform_mac_addr - get platform-specific MAC address
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050011262 * @pdev: PCI device information struct
11263 * @pf: board private structure
11264 *
Jacob Keller41c4c2b2017-04-05 07:50:57 -040011265 * Look up the MAC address for the device. First we'll try
11266 * eth_platform_get_mac_address, which will check Open Firmware, or arch
11267 * specific fallback. Otherwise, we'll default to the stored value in
11268 * firmware.
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050011269 **/
11270static void i40e_get_platform_mac_addr(struct pci_dev *pdev, struct i40e_pf *pf)
11271{
Jacob Keller41c4c2b2017-04-05 07:50:57 -040011272 if (eth_platform_get_mac_address(&pdev->dev, pf->hw.mac.addr))
11273 i40e_get_mac_addr(&pf->hw, pf->hw.mac.addr);
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050011274}
11275
11276/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011277 * i40e_probe - Device initialization routine
11278 * @pdev: PCI device information struct
11279 * @ent: entry in i40e_pci_tbl
11280 *
Jeff Kirsherb40c82e62015-02-27 09:18:34 +000011281 * i40e_probe initializes a PF identified by a pci_dev structure.
11282 * The OS initialization, configuring of the PF private structure,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011283 * and a hardware reset occur.
11284 *
11285 * Returns 0 on success, negative on failure
11286 **/
11287static int i40e_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
11288{
Catherine Sullivane8278452015-02-06 08:52:08 +000011289 struct i40e_aq_get_phy_abilities_resp abilities;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011290 struct i40e_pf *pf;
11291 struct i40e_hw *hw;
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +000011292 static u16 pfs_found;
Shannon Nelson1d5109d2015-08-26 15:14:08 -040011293 u16 wol_nvm_bits;
Catherine Sullivand4dfb812013-11-28 06:39:21 +000011294 u16 link_status;
Jean Sacren6f66a482015-09-19 05:08:45 -060011295 int err;
Anjali Singhai Jain4f2f017c2015-10-21 19:47:07 -040011296 u32 val;
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +000011297 u32 i;
Helin Zhang58fc3262015-10-01 14:37:38 -040011298 u8 set_fc_aq_fail;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011299
11300 err = pci_enable_device_mem(pdev);
11301 if (err)
11302 return err;
11303
11304 /* set up for high or low dma */
Mitch Williams64942942014-02-11 08:26:33 +000011305 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
Mitch Williams64942942014-02-11 08:26:33 +000011306 if (err) {
Jean Sacrene3e3bfd2014-03-25 04:30:27 +000011307 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
11308 if (err) {
11309 dev_err(&pdev->dev,
11310 "DMA configuration failed: 0x%x\n", err);
11311 goto err_dma;
11312 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011313 }
11314
11315 /* set up pci connections */
Johannes Thumshirn56d766d2016-06-07 09:44:05 +020011316 err = pci_request_mem_regions(pdev, i40e_driver_name);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011317 if (err) {
11318 dev_info(&pdev->dev,
11319 "pci_request_selected_regions failed %d\n", err);
11320 goto err_pci_reg;
11321 }
11322
11323 pci_enable_pcie_error_reporting(pdev);
11324 pci_set_master(pdev);
11325
11326 /* Now that we have a PCI connection, we need to do the
11327 * low level device setup. This is primarily setting up
11328 * the Admin Queue structures and then querying for the
11329 * device's current profile information.
11330 */
11331 pf = kzalloc(sizeof(*pf), GFP_KERNEL);
11332 if (!pf) {
11333 err = -ENOMEM;
11334 goto err_pf_alloc;
11335 }
11336 pf->next_vsi = 0;
11337 pf->pdev = pdev;
Mauro S. M. Rodrigues9e6c9c02017-05-12 23:26:56 -030011338 set_bit(__I40E_DOWN, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011339
11340 hw = &pf->hw;
11341 hw->back = pf;
Anjali Singhai232f4702015-02-26 16:15:39 +000011342
Shannon Nelson2ac8b672015-07-23 16:54:37 -040011343 pf->ioremap_len = min_t(int, pci_resource_len(pdev, 0),
11344 I40E_MAX_CSR_SPACE);
Anjali Singhai232f4702015-02-26 16:15:39 +000011345
Shannon Nelson2ac8b672015-07-23 16:54:37 -040011346 hw->hw_addr = ioremap(pci_resource_start(pdev, 0), pf->ioremap_len);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011347 if (!hw->hw_addr) {
11348 err = -EIO;
11349 dev_info(&pdev->dev, "ioremap(0x%04x, 0x%04x) failed: 0x%x\n",
11350 (unsigned int)pci_resource_start(pdev, 0),
Shannon Nelson2ac8b672015-07-23 16:54:37 -040011351 pf->ioremap_len, err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011352 goto err_ioremap;
11353 }
11354 hw->vendor_id = pdev->vendor;
11355 hw->device_id = pdev->device;
11356 pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
11357 hw->subsystem_vendor_id = pdev->subsystem_vendor;
11358 hw->subsystem_device_id = pdev->subsystem_device;
11359 hw->bus.device = PCI_SLOT(pdev->devfn);
11360 hw->bus.func = PCI_FUNC(pdev->devfn);
Sudheer Mogilappagarib3f028f2017-02-09 23:58:22 -080011361 hw->bus.bus_id = pdev->bus->number;
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +000011362 pf->instance = pfs_found;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011363
Jacob Keller0e588de2017-02-06 14:38:50 -080011364 INIT_LIST_HEAD(&pf->l3_flex_pit_list);
11365 INIT_LIST_HEAD(&pf->l4_flex_pit_list);
11366
Shannon Nelsonde03d2b2016-03-10 14:59:44 -080011367 /* set up the locks for the AQ, do this only once in probe
11368 * and destroy them only once in remove
11369 */
11370 mutex_init(&hw->aq.asq_mutex);
11371 mutex_init(&hw->aq.arq_mutex);
11372
Alexander Duyck5d4ca232016-09-30 08:21:46 -040011373 pf->msg_enable = netif_msg_init(debug,
11374 NETIF_MSG_DRV |
11375 NETIF_MSG_PROBE |
11376 NETIF_MSG_LINK);
11377 if (debug < -1)
11378 pf->hw.debug_mask = debug;
Shannon Nelson5b5faa42014-10-17 03:14:51 +000011379
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +000011380 /* do a special CORER for clearing PXE mode once at init */
11381 if (hw->revision_id == 0 &&
11382 (rd32(hw, I40E_GLLAN_RCTL_0) & I40E_GLLAN_RCTL_0_PXE_MODE_MASK)) {
11383 wr32(hw, I40E_GLGEN_RTRIG, I40E_GLGEN_RTRIG_CORER_MASK);
11384 i40e_flush(hw);
11385 msleep(200);
11386 pf->corer_count++;
11387
11388 i40e_clear_pxe_mode(hw);
11389 }
11390
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011391 /* Reset here to make sure all is clean and to define PF 'n' */
Shannon Nelson838d41d2014-06-04 20:41:27 +000011392 i40e_clear_hw(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011393 err = i40e_pf_reset(hw);
11394 if (err) {
11395 dev_info(&pdev->dev, "Initial pf_reset failed: %d\n", err);
11396 goto err_pf_reset;
11397 }
11398 pf->pfr_count++;
11399
11400 hw->aq.num_arq_entries = I40E_AQ_LEN;
11401 hw->aq.num_asq_entries = I40E_AQ_LEN;
11402 hw->aq.arq_buf_size = I40E_MAX_AQ_BUF_SIZE;
11403 hw->aq.asq_buf_size = I40E_MAX_AQ_BUF_SIZE;
11404 pf->adminq_work_limit = I40E_AQ_WORK_LIMIT;
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +000011405
Carolyn Wybornyb294ac72014-12-11 07:06:39 +000011406 snprintf(pf->int_name, sizeof(pf->int_name) - 1,
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +000011407 "%s-%s:misc",
11408 dev_driver_string(&pf->pdev->dev), dev_name(&pdev->dev));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011409
11410 err = i40e_init_shared_code(hw);
11411 if (err) {
Anjali Singhai Jainb2a75c52015-04-27 14:57:20 -040011412 dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
11413 err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011414 goto err_pf_reset;
11415 }
11416
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +000011417 /* set up a default setting for link flow control */
11418 pf->hw.fc.requested_mode = I40E_FC_NONE;
11419
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011420 err = i40e_init_adminq(hw);
Carolyn Wyborny2b2426a762015-10-26 19:44:35 -040011421 if (err) {
11422 if (err == I40E_ERR_FIRMWARE_API_VERSION)
11423 dev_info(&pdev->dev,
11424 "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");
11425 else
11426 dev_info(&pdev->dev,
11427 "The driver for the device stopped because the device firmware failed to init. Try updating your NVM image.\n");
11428
11429 goto err_pf_reset;
11430 }
Filip Sadowski5bbb2e22017-06-07 05:43:09 -040011431 i40e_get_oem_version(hw);
Carolyn Wybornyf0b44442015-08-31 19:54:49 -040011432
Shannon Nelson6dec1012015-09-28 14:12:30 -040011433 /* provide nvm, fw, api versions */
11434 dev_info(&pdev->dev, "fw %d.%d.%05d api %d.%d nvm %s\n",
11435 hw->aq.fw_maj_ver, hw->aq.fw_min_ver, hw->aq.fw_build,
11436 hw->aq.api_maj_ver, hw->aq.api_min_ver,
11437 i40e_nvm_version_str(hw));
Carolyn Wybornyf0b44442015-08-31 19:54:49 -040011438
Catherine Sullivan7aa67612014-07-09 07:46:17 +000011439 if (hw->aq.api_maj_ver == I40E_FW_API_VERSION_MAJOR &&
Mitch Williams22b965512017-07-14 09:27:09 -040011440 hw->aq.api_min_ver > I40E_FW_MINOR_VERSION(hw))
Shannon Nelson278b6f62014-06-04 01:41:03 +000011441 dev_info(&pdev->dev,
Catherine Sullivan7aa67612014-07-09 07:46:17 +000011442 "The driver for the device detected a newer version of the NVM image than expected. Please install the most recent version of the network driver.\n");
Mariusz Stachurae04ea002017-07-14 09:10:19 -040011443 else if (hw->aq.api_maj_ver == 1 && hw->aq.api_min_ver < 4)
Shannon Nelson278b6f62014-06-04 01:41:03 +000011444 dev_info(&pdev->dev,
Catherine Sullivan7aa67612014-07-09 07:46:17 +000011445 "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 +000011446
Shannon Nelson4eb3f762014-03-06 08:59:58 +000011447 i40e_verify_eeprom(pf);
11448
Jesse Brandeburg2c5fe332014-04-23 04:49:57 +000011449 /* Rev 0 hardware was never productized */
11450 if (hw->revision_id < 1)
11451 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");
11452
Shannon Nelson6ff4ef82013-12-21 05:44:49 +000011453 i40e_clear_pxe_mode(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011454 err = i40e_get_capabilities(pf);
11455 if (err)
11456 goto err_adminq_setup;
11457
11458 err = i40e_sw_init(pf);
11459 if (err) {
11460 dev_info(&pdev->dev, "sw_init failed: %d\n", err);
11461 goto err_sw_init;
11462 }
11463
11464 err = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
Alexander Duyckc76cb6e2017-02-21 15:55:48 -080011465 hw->func_caps.num_rx_qp, 0, 0);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011466 if (err) {
11467 dev_info(&pdev->dev, "init_lan_hmc failed: %d\n", err);
11468 goto err_init_lan_hmc;
11469 }
11470
11471 err = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
11472 if (err) {
11473 dev_info(&pdev->dev, "configure_lan_hmc failed: %d\n", err);
11474 err = -ENOENT;
11475 goto err_configure_lan_hmc;
11476 }
11477
Neerav Parikhb686ece2014-12-14 01:55:11 +000011478 /* Disable LLDP for NICs that have firmware versions lower than v4.3.
11479 * Ignore error return codes because if it was already disabled via
11480 * hardware settings this will fail
11481 */
Jacob Kellerd36e41d2017-06-23 04:24:46 -040011482 if (pf->hw_features & I40E_HW_STOP_FW_LLDP) {
Neerav Parikhb686ece2014-12-14 01:55:11 +000011483 dev_info(&pdev->dev, "Stopping firmware LLDP agent.\n");
11484 i40e_aq_stop_lldp(hw, true, NULL);
11485 }
11486
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050011487 /* allow a platform config to override the HW addr */
11488 i40e_get_platform_mac_addr(pdev, pf);
Jacob Keller41c4c2b2017-04-05 07:50:57 -040011489
Jesse Brandeburgf62b5062013-11-28 06:39:27 +000011490 if (!is_valid_ether_addr(hw->mac.addr)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011491 dev_info(&pdev->dev, "invalid MAC address %pM\n", hw->mac.addr);
11492 err = -EIO;
11493 goto err_mac_addr;
11494 }
11495 dev_info(&pdev->dev, "MAC address: %pM\n", hw->mac.addr);
Greg Rose9a173902014-05-22 06:32:02 +000011496 ether_addr_copy(hw->mac.perm_addr, hw->mac.addr);
Neerav Parikh1f224ad2014-02-12 01:45:31 +000011497 i40e_get_port_mac_addr(hw, hw->mac.port_addr);
11498 if (is_valid_ether_addr(hw->mac.port_addr))
Jacob Kellerd36e41d2017-06-23 04:24:46 -040011499 pf->hw_features |= I40E_HW_PORT_ID_VALID;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011500
11501 pci_set_drvdata(pdev, pf);
11502 pci_save_state(pdev);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -080011503#ifdef CONFIG_I40E_DCB
11504 err = i40e_init_pf_dcb(pf);
11505 if (err) {
Shannon Nelsonaebfc812014-12-11 07:06:38 +000011506 dev_info(&pdev->dev, "DCB init failed %d, disabled\n", err);
David Ertmanc17ef432016-09-30 01:36:21 -070011507 pf->flags &= ~(I40E_FLAG_DCB_CAPABLE | I40E_FLAG_DCB_ENABLED);
Neerav Parikh014269f2014-04-01 07:11:48 +000011508 /* Continue without DCB enabled */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -080011509 }
11510#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011511
11512 /* set up periodic task facility */
11513 setup_timer(&pf->service_timer, i40e_service_timer, (unsigned long)pf);
11514 pf->service_timer_period = HZ;
11515
11516 INIT_WORK(&pf->service_task, i40e_service_task);
Jacob Keller0da36b92017-04-19 09:25:55 -040011517 clear_bit(__I40E_SERVICE_SCHED, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011518
Shannon Nelson1d5109d2015-08-26 15:14:08 -040011519 /* NVM bit on means WoL disabled for the port */
11520 i40e_read_nvm_word(hw, I40E_SR_NVM_WAKE_ON_LAN, &wol_nvm_bits);
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -080011521 if (BIT (hw->port) & wol_nvm_bits || hw->partition_id != 1)
Shannon Nelson1d5109d2015-08-26 15:14:08 -040011522 pf->wol_en = false;
11523 else
11524 pf->wol_en = true;
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011525 device_set_wakeup_enable(&pf->pdev->dev, pf->wol_en);
11526
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011527 /* set up the main switch operations */
11528 i40e_determine_queue_usage(pf);
Jesse Brandeburgc11472802015-04-07 19:45:39 -040011529 err = i40e_init_interrupt_scheme(pf);
11530 if (err)
11531 goto err_switch_setup;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011532
Mitch Williams505682c2014-05-20 08:01:37 +000011533 /* The number of VSIs reported by the FW is the minimum guaranteed
11534 * to us; HW supports far more and we share the remaining pool with
11535 * the other PFs. We allocate space for more than the guarantee with
11536 * the understanding that we might not get them all later.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011537 */
Mitch Williams505682c2014-05-20 08:01:37 +000011538 if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
11539 pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
11540 else
11541 pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
11542
11543 /* Set up the *vsi struct and our local tracking of the MAIN PF vsi. */
Jesse Brandeburgd17038d2015-12-23 12:05:55 -080011544 pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
11545 GFP_KERNEL);
Wei Yongjuned87ac02013-09-24 05:17:25 +000011546 if (!pf->vsi) {
11547 err = -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011548 goto err_switch_setup;
Wei Yongjuned87ac02013-09-24 05:17:25 +000011549 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011550
Anjali Singhai Jainfa11cb32015-05-27 12:06:14 -040011551#ifdef CONFIG_PCI_IOV
11552 /* prep for VF support */
11553 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
11554 (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
Jacob Keller0da36b92017-04-19 09:25:55 -040011555 !test_bit(__I40E_BAD_EEPROM, pf->state)) {
Anjali Singhai Jainfa11cb32015-05-27 12:06:14 -040011556 if (pci_num_vf(pdev))
11557 pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
11558 }
11559#endif
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000011560 err = i40e_setup_pf_switch(pf, false);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011561 if (err) {
11562 dev_info(&pdev->dev, "setup_pf_switch failed: %d\n", err);
11563 goto err_vsis;
11564 }
Helin Zhang58fc3262015-10-01 14:37:38 -040011565
11566 /* Make sure flow control is set according to current settings */
11567 err = i40e_set_fc(hw, &set_fc_aq_fail, true);
11568 if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_GET)
11569 dev_dbg(&pf->pdev->dev,
11570 "Set fc with err %s aq_err %s on get_phy_cap\n",
11571 i40e_stat_str(hw, err),
11572 i40e_aq_str(hw, hw->aq.asq_last_status));
11573 if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_SET)
11574 dev_dbg(&pf->pdev->dev,
11575 "Set fc with err %s aq_err %s on set_phy_config\n",
11576 i40e_stat_str(hw, err),
11577 i40e_aq_str(hw, hw->aq.asq_last_status));
11578 if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_UPDATE)
11579 dev_dbg(&pf->pdev->dev,
11580 "Set fc with err %s aq_err %s on get_link_info\n",
11581 i40e_stat_str(hw, err),
11582 i40e_aq_str(hw, hw->aq.asq_last_status));
11583
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +000011584 /* if FDIR VSI was set up, start it now */
Mitch Williams505682c2014-05-20 08:01:37 +000011585 for (i = 0; i < pf->num_alloc_vsi; i++) {
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +000011586 if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
11587 i40e_vsi_open(pf->vsi[i]);
11588 break;
11589 }
11590 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011591
Shannon Nelson2f0aff42016-01-04 10:33:08 -080011592 /* The driver only wants link up/down and module qualification
11593 * reports from firmware. Note the negative logic.
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +000011594 */
11595 err = i40e_aq_set_phy_int_mask(&pf->hw,
Shannon Nelson2f0aff42016-01-04 10:33:08 -080011596 ~(I40E_AQ_EVENT_LINK_UPDOWN |
Shannon Nelson867a79e2016-03-18 12:18:15 -070011597 I40E_AQ_EVENT_MEDIA_NA |
Shannon Nelson2f0aff42016-01-04 10:33:08 -080011598 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +000011599 if (err)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040011600 dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
11601 i40e_stat_str(&pf->hw, err),
11602 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +000011603
Anjali Singhai Jain4f2f017c2015-10-21 19:47:07 -040011604 /* Reconfigure hardware for allowing smaller MSS in the case
11605 * of TSO, so that we avoid the MDD being fired and causing
11606 * a reset in the case of small MSS+TSO.
11607 */
11608 val = rd32(hw, I40E_REG_MSS);
11609 if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
11610 val &= ~I40E_REG_MSS_MIN_MASK;
11611 val |= I40E_64BYTE_MSS;
11612 wr32(hw, I40E_REG_MSS, val);
11613 }
11614
Jacob Kellerd36e41d2017-06-23 04:24:46 -040011615 if (pf->hw_features & I40E_HW_RESTART_AUTONEG) {
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +000011616 msleep(75);
11617 err = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
11618 if (err)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040011619 dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
11620 i40e_stat_str(&pf->hw, err),
11621 i40e_aq_str(&pf->hw,
11622 pf->hw.aq.asq_last_status));
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +000011623 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011624 /* The main driver is (mostly) up and happy. We need to set this state
11625 * before setting up the misc vector or we get a race and the vector
11626 * ends up disabled forever.
11627 */
Mauro S. M. Rodrigues9e6c9c02017-05-12 23:26:56 -030011628 clear_bit(__I40E_DOWN, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011629
11630 /* In case of MSIX we are going to setup the misc vector right here
11631 * to handle admin queue events etc. In case of legacy and MSI
11632 * the misc functionality and queue processing is combined in
11633 * the same vector and that gets setup at open.
11634 */
11635 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
11636 err = i40e_setup_misc_vector(pf);
11637 if (err) {
11638 dev_info(&pdev->dev,
11639 "setup of misc vector failed: %d\n", err);
11640 goto err_vsis;
11641 }
11642 }
11643
Greg Rosedf805f62014-04-04 04:43:16 +000011644#ifdef CONFIG_PCI_IOV
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011645 /* prep for VF support */
11646 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
Shannon Nelson4eb3f762014-03-06 08:59:58 +000011647 (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
Jacob Keller0da36b92017-04-19 09:25:55 -040011648 !test_bit(__I40E_BAD_EEPROM, pf->state)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011649 /* disable link interrupts for VFs */
11650 val = rd32(hw, I40E_PFGEN_PORTMDIO_NUM);
11651 val &= ~I40E_PFGEN_PORTMDIO_NUM_VFLINK_STAT_ENA_MASK;
11652 wr32(hw, I40E_PFGEN_PORTMDIO_NUM, val);
11653 i40e_flush(hw);
Mitch Williams4aeec012014-02-13 03:48:47 -080011654
11655 if (pci_num_vf(pdev)) {
11656 dev_info(&pdev->dev,
11657 "Active VFs found, allocating resources.\n");
11658 err = i40e_alloc_vfs(pf, pci_num_vf(pdev));
11659 if (err)
11660 dev_info(&pdev->dev,
11661 "Error %d allocating resources for existing VFs\n",
11662 err);
11663 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011664 }
Greg Rosedf805f62014-04-04 04:43:16 +000011665#endif /* CONFIG_PCI_IOV */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011666
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060011667 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11668 pf->iwarp_base_vector = i40e_get_lump(pf, pf->irq_pile,
11669 pf->num_iwarp_msix,
11670 I40E_IWARP_IRQ_PILE_ID);
11671 if (pf->iwarp_base_vector < 0) {
11672 dev_info(&pdev->dev,
11673 "failed to get tracking for %d vectors for IWARP err=%d\n",
11674 pf->num_iwarp_msix, pf->iwarp_base_vector);
11675 pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
11676 }
11677 }
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +000011678
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011679 i40e_dbg_pf_init(pf);
11680
11681 /* tell the firmware that we're starting */
Jesse Brandeburg44033fa2014-04-23 04:50:15 +000011682 i40e_send_version(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011683
11684 /* since everything's happy, start the service_task timer */
11685 mod_timer(&pf->service_timer,
11686 round_jiffies(jiffies + pf->service_timer_period));
11687
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060011688 /* add this PF to client device list and launch a client service task */
Mitch Williams004eb612017-04-04 12:40:16 -070011689 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11690 err = i40e_lan_add_device(pf);
11691 if (err)
11692 dev_info(&pdev->dev, "Failed to add PF to client API service list: %d\n",
11693 err);
11694 }
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060011695
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040011696#define PCI_SPEED_SIZE 8
11697#define PCI_WIDTH_SIZE 8
11698 /* Devices on the IOSF bus do not have this information
11699 * and will report PCI Gen 1 x 1 by default so don't bother
11700 * checking them.
11701 */
Jacob Kellerd36e41d2017-06-23 04:24:46 -040011702 if (!(pf->hw_features & I40E_HW_NO_PCI_LINK_CHECK)) {
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040011703 char speed[PCI_SPEED_SIZE] = "Unknown";
11704 char width[PCI_WIDTH_SIZE] = "Unknown";
Catherine Sullivand4dfb812013-11-28 06:39:21 +000011705
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040011706 /* Get the negotiated link width and speed from PCI config
11707 * space
11708 */
11709 pcie_capability_read_word(pf->pdev, PCI_EXP_LNKSTA,
11710 &link_status);
Catherine Sullivand4dfb812013-11-28 06:39:21 +000011711
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040011712 i40e_set_pci_config_data(hw, link_status);
Catherine Sullivand4dfb812013-11-28 06:39:21 +000011713
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040011714 switch (hw->bus.speed) {
11715 case i40e_bus_speed_8000:
11716 strncpy(speed, "8.0", PCI_SPEED_SIZE); break;
11717 case i40e_bus_speed_5000:
11718 strncpy(speed, "5.0", PCI_SPEED_SIZE); break;
11719 case i40e_bus_speed_2500:
11720 strncpy(speed, "2.5", PCI_SPEED_SIZE); break;
11721 default:
11722 break;
11723 }
11724 switch (hw->bus.width) {
11725 case i40e_bus_width_pcie_x8:
11726 strncpy(width, "8", PCI_WIDTH_SIZE); break;
11727 case i40e_bus_width_pcie_x4:
11728 strncpy(width, "4", PCI_WIDTH_SIZE); break;
11729 case i40e_bus_width_pcie_x2:
11730 strncpy(width, "2", PCI_WIDTH_SIZE); break;
11731 case i40e_bus_width_pcie_x1:
11732 strncpy(width, "1", PCI_WIDTH_SIZE); break;
11733 default:
11734 break;
11735 }
11736
11737 dev_info(&pdev->dev, "PCI-Express: Speed %sGT/s Width x%s\n",
11738 speed, width);
11739
11740 if (hw->bus.width < i40e_bus_width_pcie_x8 ||
11741 hw->bus.speed < i40e_bus_speed_8000) {
11742 dev_warn(&pdev->dev, "PCI-Express bandwidth available for this device may be insufficient for optimal performance.\n");
11743 dev_warn(&pdev->dev, "Please move the device to a different PCI-e link with more lanes and/or higher transfer rate.\n");
11744 }
Catherine Sullivand4dfb812013-11-28 06:39:21 +000011745 }
11746
Catherine Sullivane8278452015-02-06 08:52:08 +000011747 /* get the requested speeds from the fw */
11748 err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities, NULL);
11749 if (err)
Neerav Parikh8279e492015-09-03 17:18:50 -040011750 dev_dbg(&pf->pdev->dev, "get requested speeds ret = %s last_status = %s\n",
11751 i40e_stat_str(&pf->hw, err),
11752 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Catherine Sullivane8278452015-02-06 08:52:08 +000011753 pf->hw.phy.link_info.requested_speeds = abilities.link_speed;
11754
Catherine Sullivanfc72dbc2015-09-01 11:36:30 -040011755 /* get the supported phy types from the fw */
11756 err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities, NULL);
11757 if (err)
11758 dev_dbg(&pf->pdev->dev, "get supported phy types ret = %s last_status = %s\n",
11759 i40e_stat_str(&pf->hw, err),
11760 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Catherine Sullivanfc72dbc2015-09-01 11:36:30 -040011761
Anjali Singhai Jaine7358f52015-10-01 14:37:34 -040011762 /* Add a filter to drop all Flow control frames from any VSI from being
11763 * transmitted. By doing so we stop a malicious VF from sending out
11764 * PAUSE or PFC frames and potentially controlling traffic for other
11765 * PF/VF VSIs.
11766 * The FW can still send Flow control frames if enabled.
11767 */
11768 i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
11769 pf->main_vsi_seid);
11770
Carolyn Wyborny31b606d2016-02-17 16:12:12 -080011771 if ((pf->hw.device_id == I40E_DEV_ID_10G_BASE_T) ||
Henry Tieman4f9b4302016-11-08 13:05:18 -080011772 (pf->hw.device_id == I40E_DEV_ID_10G_BASE_T4))
Jacob Kellerd36e41d2017-06-23 04:24:46 -040011773 pf->hw_features |= I40E_HW_PHY_CONTROLS_LEDS;
Harshitha Ramamurthy4ad9f4f2016-11-08 13:05:09 -080011774 if (pf->hw.device_id == I40E_DEV_ID_SFP_I_X722)
Jacob Kellerd36e41d2017-06-23 04:24:46 -040011775 pf->hw_features |= I40E_HW_HAVE_CRT_RETIMER;
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000011776 /* print a string summarizing features */
11777 i40e_print_features(pf);
11778
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011779 return 0;
11780
11781 /* Unwind what we've done if something failed in the setup */
11782err_vsis:
Mauro S. M. Rodrigues9e6c9c02017-05-12 23:26:56 -030011783 set_bit(__I40E_DOWN, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011784 i40e_clear_interrupt_scheme(pf);
11785 kfree(pf->vsi);
Shannon Nelson04b03012013-11-28 06:39:34 +000011786err_switch_setup:
11787 i40e_reset_interrupt_capability(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011788 del_timer_sync(&pf->service_timer);
11789err_mac_addr:
11790err_configure_lan_hmc:
11791 (void)i40e_shutdown_lan_hmc(hw);
11792err_init_lan_hmc:
11793 kfree(pf->qp_pile);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011794err_sw_init:
11795err_adminq_setup:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011796err_pf_reset:
11797 iounmap(hw->hw_addr);
11798err_ioremap:
11799 kfree(pf);
11800err_pf_alloc:
11801 pci_disable_pcie_error_reporting(pdev);
Johannes Thumshirn56d766d2016-06-07 09:44:05 +020011802 pci_release_mem_regions(pdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011803err_pci_reg:
11804err_dma:
11805 pci_disable_device(pdev);
11806 return err;
11807}
11808
11809/**
11810 * i40e_remove - Device removal routine
11811 * @pdev: PCI device information struct
11812 *
11813 * i40e_remove is called by the PCI subsystem to alert the driver
11814 * that is should release a PCI device. This could be caused by a
11815 * Hot-Plug event, or because the driver is going to be removed from
11816 * memory.
11817 **/
11818static void i40e_remove(struct pci_dev *pdev)
11819{
11820 struct i40e_pf *pf = pci_get_drvdata(pdev);
Carolyn Wybornybcab2db2015-09-28 14:16:55 -040011821 struct i40e_hw *hw = &pf->hw;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011822 i40e_status ret_code;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011823 int i;
11824
11825 i40e_dbg_pf_exit(pf);
11826
Jacob Kellerbeb0dff2014-01-11 05:43:19 +000011827 i40e_ptp_stop(pf);
11828
Carolyn Wybornybcab2db2015-09-28 14:16:55 -040011829 /* Disable RSS in hw */
Shannon Nelson272cdaf22016-02-17 16:12:21 -080011830 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), 0);
11831 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), 0);
Carolyn Wybornybcab2db2015-09-28 14:16:55 -040011832
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011833 /* no more scheduling of any task */
Jacob Keller0da36b92017-04-19 09:25:55 -040011834 set_bit(__I40E_SUSPENDED, pf->state);
Mauro S. M. Rodrigues9e6c9c02017-05-12 23:26:56 -030011835 set_bit(__I40E_DOWN, pf->state);
Shannon Nelsonc99abb42016-03-10 14:59:45 -080011836 if (pf->service_timer.data)
11837 del_timer_sync(&pf->service_timer);
11838 if (pf->service_task.func)
11839 cancel_work_sync(&pf->service_task);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011840
Mitch Williams921c4672017-03-30 00:46:08 -070011841 /* Client close must be called explicitly here because the timer
11842 * has been stopped.
11843 */
11844 i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
11845
Mitch Williamseb2d80b2014-02-13 03:48:48 -080011846 if (pf->flags & I40E_FLAG_SRIOV_ENABLED) {
11847 i40e_free_vfs(pf);
11848 pf->flags &= ~I40E_FLAG_SRIOV_ENABLED;
11849 }
11850
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011851 i40e_fdir_teardown(pf);
11852
11853 /* If there is a switch structure or any orphans, remove them.
11854 * This will leave only the PF's VSI remaining.
11855 */
11856 for (i = 0; i < I40E_MAX_VEB; i++) {
11857 if (!pf->veb[i])
11858 continue;
11859
11860 if (pf->veb[i]->uplink_seid == pf->mac_seid ||
11861 pf->veb[i]->uplink_seid == 0)
11862 i40e_switch_branch_release(pf->veb[i]);
11863 }
11864
11865 /* Now we can shutdown the PF's VSI, just before we kill
11866 * adminq and hmc.
11867 */
11868 if (pf->vsi[pf->lan_vsi])
11869 i40e_vsi_release(pf->vsi[pf->lan_vsi]);
11870
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060011871 /* remove attached clients */
Mitch Williams004eb612017-04-04 12:40:16 -070011872 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11873 ret_code = i40e_lan_del_device(pf);
11874 if (ret_code)
11875 dev_warn(&pdev->dev, "Failed to delete client device: %d\n",
11876 ret_code);
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060011877 }
11878
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011879 /* shutdown and destroy the HMC */
Jesse Brandeburgf734dff2016-01-15 14:33:11 -080011880 if (hw->hmc.hmc_obj) {
11881 ret_code = i40e_shutdown_lan_hmc(hw);
Shannon Nelson60442de2014-04-23 04:50:13 +000011882 if (ret_code)
11883 dev_warn(&pdev->dev,
11884 "Failed to destroy the HMC resources: %d\n",
11885 ret_code);
11886 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011887
11888 /* shutdown the adminq */
Henry Tiemanac9c5c62016-09-06 18:05:11 -070011889 i40e_shutdown_adminq(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011890
Jesse Brandeburg8ddb3322015-11-18 15:47:06 -080011891 /* destroy the locks only once, here */
11892 mutex_destroy(&hw->aq.arq_mutex);
11893 mutex_destroy(&hw->aq.asq_mutex);
11894
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011895 /* Clear all dynamic memory lists of rings, q_vectors, and VSIs */
11896 i40e_clear_interrupt_scheme(pf);
Mitch Williams505682c2014-05-20 08:01:37 +000011897 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011898 if (pf->vsi[i]) {
11899 i40e_vsi_clear_rings(pf->vsi[i]);
11900 i40e_vsi_clear(pf->vsi[i]);
11901 pf->vsi[i] = NULL;
11902 }
11903 }
11904
11905 for (i = 0; i < I40E_MAX_VEB; i++) {
11906 kfree(pf->veb[i]);
11907 pf->veb[i] = NULL;
11908 }
11909
11910 kfree(pf->qp_pile);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011911 kfree(pf->vsi);
11912
Jesse Brandeburgf734dff2016-01-15 14:33:11 -080011913 iounmap(hw->hw_addr);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011914 kfree(pf);
Johannes Thumshirn56d766d2016-06-07 09:44:05 +020011915 pci_release_mem_regions(pdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011916
11917 pci_disable_pcie_error_reporting(pdev);
11918 pci_disable_device(pdev);
11919}
11920
11921/**
11922 * i40e_pci_error_detected - warning that something funky happened in PCI land
11923 * @pdev: PCI device information struct
11924 *
11925 * Called to warn that something happened and the error handling steps
11926 * are in progress. Allows the driver to quiesce things, be ready for
11927 * remediation.
11928 **/
11929static pci_ers_result_t i40e_pci_error_detected(struct pci_dev *pdev,
11930 enum pci_channel_state error)
11931{
11932 struct i40e_pf *pf = pci_get_drvdata(pdev);
11933
11934 dev_info(&pdev->dev, "%s: error %d\n", __func__, error);
11935
Guilherme G Piccoliedfc23ee2016-10-03 00:31:12 -070011936 if (!pf) {
11937 dev_info(&pdev->dev,
11938 "Cannot recover - error happened during device probe\n");
11939 return PCI_ERS_RESULT_DISCONNECT;
11940 }
11941
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011942 /* shutdown all operations */
Jacob Kellerdfc4ff62017-06-07 05:43:13 -040011943 if (!test_bit(__I40E_SUSPENDED, pf->state))
11944 i40e_prep_for_reset(pf, false);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011945
11946 /* Request a slot reset */
11947 return PCI_ERS_RESULT_NEED_RESET;
11948}
11949
11950/**
11951 * i40e_pci_error_slot_reset - a PCI slot reset just happened
11952 * @pdev: PCI device information struct
11953 *
11954 * Called to find if the driver can work with the device now that
11955 * the pci slot has been reset. If a basic connection seems good
11956 * (registers are readable and have sane content) then return a
11957 * happy little PCI_ERS_RESULT_xxx.
11958 **/
11959static pci_ers_result_t i40e_pci_error_slot_reset(struct pci_dev *pdev)
11960{
11961 struct i40e_pf *pf = pci_get_drvdata(pdev);
11962 pci_ers_result_t result;
11963 int err;
11964 u32 reg;
11965
Shannon Nelsonfb43201f2015-08-26 15:14:17 -040011966 dev_dbg(&pdev->dev, "%s\n", __func__);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011967 if (pci_enable_device_mem(pdev)) {
11968 dev_info(&pdev->dev,
11969 "Cannot re-enable PCI device after reset.\n");
11970 result = PCI_ERS_RESULT_DISCONNECT;
11971 } else {
11972 pci_set_master(pdev);
11973 pci_restore_state(pdev);
11974 pci_save_state(pdev);
11975 pci_wake_from_d3(pdev, false);
11976
11977 reg = rd32(&pf->hw, I40E_GLGEN_RTRIG);
11978 if (reg == 0)
11979 result = PCI_ERS_RESULT_RECOVERED;
11980 else
11981 result = PCI_ERS_RESULT_DISCONNECT;
11982 }
11983
11984 err = pci_cleanup_aer_uncorrect_error_status(pdev);
11985 if (err) {
11986 dev_info(&pdev->dev,
11987 "pci_cleanup_aer_uncorrect_error_status failed 0x%0x\n",
11988 err);
11989 /* non-fatal, continue */
11990 }
11991
11992 return result;
11993}
11994
11995/**
11996 * i40e_pci_error_resume - restart operations after PCI error recovery
11997 * @pdev: PCI device information struct
11998 *
11999 * Called to allow the driver to bring things back up after PCI error
12000 * and/or reset recovery has finished.
12001 **/
12002static void i40e_pci_error_resume(struct pci_dev *pdev)
12003{
12004 struct i40e_pf *pf = pci_get_drvdata(pdev);
12005
Shannon Nelsonfb43201f2015-08-26 15:14:17 -040012006 dev_dbg(&pdev->dev, "%s\n", __func__);
Jacob Keller0da36b92017-04-19 09:25:55 -040012007 if (test_bit(__I40E_SUSPENDED, pf->state))
Shannon Nelson9007bcc2013-11-26 10:49:23 +000012008 return;
12009
Jacob Kellerdfc4ff62017-06-07 05:43:13 -040012010 i40e_handle_reset_warning(pf, false);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012011}
12012
Shannon Nelson9007bcc2013-11-26 10:49:23 +000012013/**
Joshua Hay1d680052016-12-12 15:44:08 -080012014 * i40e_enable_mc_magic_wake - enable multicast magic packet wake up
12015 * using the mac_address_write admin q function
12016 * @pf: pointer to i40e_pf struct
12017 **/
12018static void i40e_enable_mc_magic_wake(struct i40e_pf *pf)
12019{
12020 struct i40e_hw *hw = &pf->hw;
12021 i40e_status ret;
12022 u8 mac_addr[6];
12023 u16 flags = 0;
12024
12025 /* Get current MAC address in case it's an LAA */
12026 if (pf->vsi[pf->lan_vsi] && pf->vsi[pf->lan_vsi]->netdev) {
12027 ether_addr_copy(mac_addr,
12028 pf->vsi[pf->lan_vsi]->netdev->dev_addr);
12029 } else {
12030 dev_err(&pf->pdev->dev,
12031 "Failed to retrieve MAC address; using default\n");
12032 ether_addr_copy(mac_addr, hw->mac.addr);
12033 }
12034
12035 /* The FW expects the mac address write cmd to first be called with
12036 * one of these flags before calling it again with the multicast
12037 * enable flags.
12038 */
12039 flags = I40E_AQC_WRITE_TYPE_LAA_WOL;
12040
12041 if (hw->func_caps.flex10_enable && hw->partition_id != 1)
12042 flags = I40E_AQC_WRITE_TYPE_LAA_ONLY;
12043
12044 ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
12045 if (ret) {
12046 dev_err(&pf->pdev->dev,
12047 "Failed to update MAC address registers; cannot enable Multicast Magic packet wake up");
12048 return;
12049 }
12050
12051 flags = I40E_AQC_MC_MAG_EN
12052 | I40E_AQC_WOL_PRESERVE_ON_PFR
12053 | I40E_AQC_WRITE_TYPE_UPDATE_MC_MAG;
12054 ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
12055 if (ret)
12056 dev_err(&pf->pdev->dev,
12057 "Failed to enable Multicast Magic Packet wake up\n");
12058}
12059
12060/**
Shannon Nelson9007bcc2013-11-26 10:49:23 +000012061 * i40e_shutdown - PCI callback for shutting down
12062 * @pdev: PCI device information struct
12063 **/
12064static void i40e_shutdown(struct pci_dev *pdev)
12065{
12066 struct i40e_pf *pf = pci_get_drvdata(pdev);
Shannon Nelson8e2773a2013-11-28 06:39:22 +000012067 struct i40e_hw *hw = &pf->hw;
Shannon Nelson9007bcc2013-11-26 10:49:23 +000012068
Jacob Keller0da36b92017-04-19 09:25:55 -040012069 set_bit(__I40E_SUSPENDED, pf->state);
Mauro S. M. Rodrigues9e6c9c02017-05-12 23:26:56 -030012070 set_bit(__I40E_DOWN, pf->state);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000012071 rtnl_lock();
Maciej Sosin373149f2017-04-05 07:50:55 -040012072 i40e_prep_for_reset(pf, true);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000012073 rtnl_unlock();
12074
Shannon Nelson8e2773a2013-11-28 06:39:22 +000012075 wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
12076 wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
12077
Catherine Sullivan02b42492015-07-10 19:35:59 -040012078 del_timer_sync(&pf->service_timer);
12079 cancel_work_sync(&pf->service_task);
12080 i40e_fdir_teardown(pf);
12081
Mitch Williams921c4672017-03-30 00:46:08 -070012082 /* Client close must be called explicitly here because the timer
12083 * has been stopped.
12084 */
12085 i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
12086
Jacob Kellerd36e41d2017-06-23 04:24:46 -040012087 if (pf->wol_en && (pf->hw_features & I40E_HW_WOL_MC_MAGIC_PKT_WAKE))
Joshua Hay1d680052016-12-12 15:44:08 -080012088 i40e_enable_mc_magic_wake(pf);
12089
Jacob Kellerdfc4ff62017-06-07 05:43:13 -040012090 i40e_prep_for_reset(pf, false);
Catherine Sullivan02b42492015-07-10 19:35:59 -040012091
12092 wr32(hw, I40E_PFPM_APM,
12093 (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
12094 wr32(hw, I40E_PFPM_WUFC,
12095 (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
12096
Shannon Nelsone1477582015-02-21 06:44:33 +000012097 i40e_clear_interrupt_scheme(pf);
12098
Shannon Nelson9007bcc2013-11-26 10:49:23 +000012099 if (system_state == SYSTEM_POWER_OFF) {
Shannon Nelson8e2773a2013-11-28 06:39:22 +000012100 pci_wake_from_d3(pdev, pf->wol_en);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000012101 pci_set_power_state(pdev, PCI_D3hot);
12102 }
12103}
12104
12105#ifdef CONFIG_PM
12106/**
Jacob Keller0e5d3da2017-07-14 09:27:03 -040012107 * i40e_suspend - PM callback for moving to D3
12108 * @dev: generic device information structure
Shannon Nelson9007bcc2013-11-26 10:49:23 +000012109 **/
Jacob Keller0e5d3da2017-07-14 09:27:03 -040012110static int i40e_suspend(struct device *dev)
Shannon Nelson9007bcc2013-11-26 10:49:23 +000012111{
Jacob Keller0e5d3da2017-07-14 09:27:03 -040012112 struct pci_dev *pdev = to_pci_dev(dev);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000012113 struct i40e_pf *pf = pci_get_drvdata(pdev);
Shannon Nelson8e2773a2013-11-28 06:39:22 +000012114 struct i40e_hw *hw = &pf->hw;
Shannon Nelson9007bcc2013-11-26 10:49:23 +000012115
Jacob Keller401586c2017-07-14 09:27:04 -040012116 /* If we're already suspended, then there is nothing to do */
12117 if (test_and_set_bit(__I40E_SUSPENDED, pf->state))
12118 return 0;
12119
Mauro S. M. Rodrigues9e6c9c02017-05-12 23:26:56 -030012120 set_bit(__I40E_DOWN, pf->state);
Mitch Williams3932dbf2015-03-31 00:45:04 -070012121
Jacob Keller5c499222017-07-14 09:27:05 -040012122 /* Ensure service task will not be running */
12123 del_timer_sync(&pf->service_timer);
12124 cancel_work_sync(&pf->service_task);
12125
Jacob Kellerd36e41d2017-06-23 04:24:46 -040012126 if (pf->wol_en && (pf->hw_features & I40E_HW_WOL_MC_MAGIC_PKT_WAKE))
Joshua Hay1d680052016-12-12 15:44:08 -080012127 i40e_enable_mc_magic_wake(pf);
12128
Jacob Kellerdfc4ff62017-06-07 05:43:13 -040012129 i40e_prep_for_reset(pf, false);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000012130
Shannon Nelson8e2773a2013-11-28 06:39:22 +000012131 wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
12132 wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
12133
Jacob Kellerb980c062017-07-14 09:27:06 -040012134 /* Clear the interrupt scheme and release our IRQs so that the system
12135 * can safely hibernate even when there are a large number of CPUs.
12136 * Otherwise hibernation might fail when mapping all the vectors back
12137 * to CPU0.
12138 */
12139 i40e_clear_interrupt_scheme(pf);
Jacob Kellerc17401a2017-07-14 09:27:02 -040012140
Jacob Keller0e5d3da2017-07-14 09:27:03 -040012141 return 0;
Shannon Nelson9007bcc2013-11-26 10:49:23 +000012142}
12143
12144/**
Jacob Keller0e5d3da2017-07-14 09:27:03 -040012145 * i40e_resume - PM callback for waking up from D3
12146 * @dev: generic device information structure
Shannon Nelson9007bcc2013-11-26 10:49:23 +000012147 **/
Jacob Keller0e5d3da2017-07-14 09:27:03 -040012148static int i40e_resume(struct device *dev)
Shannon Nelson9007bcc2013-11-26 10:49:23 +000012149{
Jacob Keller0e5d3da2017-07-14 09:27:03 -040012150 struct pci_dev *pdev = to_pci_dev(dev);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000012151 struct i40e_pf *pf = pci_get_drvdata(pdev);
Jacob Kellerb980c062017-07-14 09:27:06 -040012152 int err;
Shannon Nelson9007bcc2013-11-26 10:49:23 +000012153
Jacob Keller401586c2017-07-14 09:27:04 -040012154 /* If we're not suspended, then there is nothing to do */
12155 if (!test_bit(__I40E_SUSPENDED, pf->state))
12156 return 0;
12157
Jacob Kellerb980c062017-07-14 09:27:06 -040012158 /* We cleared the interrupt scheme when we suspended, so we need to
12159 * restore it now to resume device functionality.
12160 */
12161 err = i40e_restore_interrupt_scheme(pf);
12162 if (err) {
12163 dev_err(&pdev->dev, "Cannot restore interrupt scheme: %d\n",
12164 err);
12165 }
12166
Jacob Keller401586c2017-07-14 09:27:04 -040012167 clear_bit(__I40E_DOWN, pf->state);
12168 i40e_reset_and_rebuild(pf, false, false);
12169
12170 /* Clear suspended state last after everything is recovered */
12171 clear_bit(__I40E_SUSPENDED, pf->state);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000012172
Jacob Keller5c499222017-07-14 09:27:05 -040012173 /* Restart the service task */
12174 mod_timer(&pf->service_timer,
12175 round_jiffies(jiffies + pf->service_timer_period));
12176
Shannon Nelson9007bcc2013-11-26 10:49:23 +000012177 return 0;
12178}
12179
Jacob Keller0e5d3da2017-07-14 09:27:03 -040012180#endif /* CONFIG_PM */
12181
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012182static const struct pci_error_handlers i40e_err_handler = {
12183 .error_detected = i40e_pci_error_detected,
12184 .slot_reset = i40e_pci_error_slot_reset,
12185 .resume = i40e_pci_error_resume,
12186};
12187
Jacob Keller0e5d3da2017-07-14 09:27:03 -040012188static SIMPLE_DEV_PM_OPS(i40e_pm_ops, i40e_suspend, i40e_resume);
12189
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012190static struct pci_driver i40e_driver = {
12191 .name = i40e_driver_name,
12192 .id_table = i40e_pci_tbl,
12193 .probe = i40e_probe,
12194 .remove = i40e_remove,
Shannon Nelson9007bcc2013-11-26 10:49:23 +000012195#ifdef CONFIG_PM
Jacob Keller0e5d3da2017-07-14 09:27:03 -040012196 .driver = {
12197 .pm = &i40e_pm_ops,
12198 },
12199#endif /* CONFIG_PM */
Shannon Nelson9007bcc2013-11-26 10:49:23 +000012200 .shutdown = i40e_shutdown,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012201 .err_handler = &i40e_err_handler,
12202 .sriov_configure = i40e_pci_sriov_configure,
12203};
12204
12205/**
12206 * i40e_init_module - Driver registration routine
12207 *
12208 * i40e_init_module is the first routine called when the driver is
12209 * loaded. All it does is register with the PCI subsystem.
12210 **/
12211static int __init i40e_init_module(void)
12212{
12213 pr_info("%s: %s - version %s\n", i40e_driver_name,
12214 i40e_driver_string, i40e_driver_version_str);
12215 pr_info("%s: %s\n", i40e_driver_name, i40e_copyright);
Greg Rose96664482015-02-06 08:52:13 +000012216
Jacob Keller4d5957c2017-06-20 15:16:54 -070012217 /* There is no need to throttle the number of active tasks because
12218 * each device limits its own task using a state bit for scheduling
12219 * the service task, and the device tasks do not interfere with each
12220 * other, so we don't set a max task limit. We must set WQ_MEM_RECLAIM
12221 * since we need to be able to guarantee forward progress even under
12222 * memory pressure.
Jesse Brandeburg2803b162015-12-22 14:25:08 -080012223 */
Jacob Keller4d5957c2017-06-20 15:16:54 -070012224 i40e_wq = alloc_workqueue("%s", WQ_MEM_RECLAIM, 0, i40e_driver_name);
Jesse Brandeburg2803b162015-12-22 14:25:08 -080012225 if (!i40e_wq) {
12226 pr_err("%s: Failed to create workqueue\n", i40e_driver_name);
12227 return -ENOMEM;
12228 }
12229
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012230 i40e_dbg_init();
12231 return pci_register_driver(&i40e_driver);
12232}
12233module_init(i40e_init_module);
12234
12235/**
12236 * i40e_exit_module - Driver exit cleanup routine
12237 *
12238 * i40e_exit_module is called just before the driver is removed
12239 * from memory.
12240 **/
12241static void __exit i40e_exit_module(void)
12242{
12243 pci_unregister_driver(&i40e_driver);
Jesse Brandeburg2803b162015-12-22 14:25:08 -080012244 destroy_workqueue(i40e_wq);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012245 i40e_dbg_exit();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012246}
12247module_exit(i40e_exit_module);