blob: fbe34500ded2692852b1552979f54148693c3a1f [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/**
Amritha Nambiara9ce82f2017-09-07 04:00:22 -07001592 * i40e_config_rss_aq - Prepare for RSS using AQ commands
1593 * @vsi: vsi structure
1594 * @seed: RSS hash seed
1595 **/
1596static int i40e_config_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
1597 u8 *lut, u16 lut_size)
1598{
1599 struct i40e_pf *pf = vsi->back;
1600 struct i40e_hw *hw = &pf->hw;
1601 int ret = 0;
1602
1603 if (seed) {
1604 struct i40e_aqc_get_set_rss_key_data *seed_dw =
1605 (struct i40e_aqc_get_set_rss_key_data *)seed;
1606 ret = i40e_aq_set_rss_key(hw, vsi->id, seed_dw);
1607 if (ret) {
1608 dev_info(&pf->pdev->dev,
1609 "Cannot set RSS key, err %s aq_err %s\n",
1610 i40e_stat_str(hw, ret),
1611 i40e_aq_str(hw, hw->aq.asq_last_status));
1612 return ret;
1613 }
1614 }
1615 if (lut) {
1616 bool pf_lut = vsi->type == I40E_VSI_MAIN ? true : false;
1617
1618 ret = i40e_aq_set_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
1619 if (ret) {
1620 dev_info(&pf->pdev->dev,
1621 "Cannot set RSS lut, err %s aq_err %s\n",
1622 i40e_stat_str(hw, ret),
1623 i40e_aq_str(hw, hw->aq.asq_last_status));
1624 return ret;
1625 }
1626 }
1627 return ret;
1628}
1629
1630/**
1631 * i40e_vsi_config_rss - Prepare for VSI(VMDq) RSS if used
1632 * @vsi: VSI structure
1633 **/
1634static int i40e_vsi_config_rss(struct i40e_vsi *vsi)
1635{
1636 struct i40e_pf *pf = vsi->back;
1637 u8 seed[I40E_HKEY_ARRAY_SIZE];
1638 u8 *lut;
1639 int ret;
1640
1641 if (!(pf->hw_features & I40E_HW_RSS_AQ_CAPABLE))
1642 return 0;
1643 if (!vsi->rss_size)
1644 vsi->rss_size = min_t(int, pf->alloc_rss_size,
1645 vsi->num_queue_pairs);
1646 if (!vsi->rss_size)
1647 return -EINVAL;
1648 lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
1649 if (!lut)
1650 return -ENOMEM;
1651
1652 /* Use the user configured hash keys and lookup table if there is one,
1653 * otherwise use default
1654 */
1655 if (vsi->rss_lut_user)
1656 memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
1657 else
1658 i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
1659 if (vsi->rss_hkey_user)
1660 memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
1661 else
1662 netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
1663 ret = i40e_config_rss_aq(vsi, seed, lut, vsi->rss_table_size);
1664 kfree(lut);
1665 return ret;
1666}
1667
1668/**
1669 * i40e_vsi_setup_queue_map_mqprio - Prepares mqprio based tc_config
1670 * @vsi: the VSI being configured,
1671 * @ctxt: VSI context structure
1672 * @enabled_tc: number of traffic classes to enable
1673 *
1674 * Prepares VSI tc_config to have queue configurations based on MQPRIO options.
1675 **/
1676static int i40e_vsi_setup_queue_map_mqprio(struct i40e_vsi *vsi,
1677 struct i40e_vsi_context *ctxt,
1678 u8 enabled_tc)
1679{
1680 u16 qcount = 0, max_qcount, qmap, sections = 0;
1681 int i, override_q, pow, num_qps, ret;
1682 u8 netdev_tc = 0, offset = 0;
1683
1684 if (vsi->type != I40E_VSI_MAIN)
1685 return -EINVAL;
1686 sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
1687 sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
1688 vsi->tc_config.numtc = vsi->mqprio_qopt.qopt.num_tc;
1689 vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
1690 num_qps = vsi->mqprio_qopt.qopt.count[0];
1691
1692 /* find the next higher power-of-2 of num queue pairs */
1693 pow = ilog2(num_qps);
1694 if (!is_power_of_2(num_qps))
1695 pow++;
1696 qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
1697 (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
1698
1699 /* Setup queue offset/count for all TCs for given VSI */
1700 max_qcount = vsi->mqprio_qopt.qopt.count[0];
1701 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1702 /* See if the given TC is enabled for the given VSI */
1703 if (vsi->tc_config.enabled_tc & BIT(i)) {
1704 offset = vsi->mqprio_qopt.qopt.offset[i];
1705 qcount = vsi->mqprio_qopt.qopt.count[i];
1706 if (qcount > max_qcount)
1707 max_qcount = qcount;
1708 vsi->tc_config.tc_info[i].qoffset = offset;
1709 vsi->tc_config.tc_info[i].qcount = qcount;
1710 vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
1711 } else {
1712 /* TC is not enabled so set the offset to
1713 * default queue and allocate one queue
1714 * for the given TC.
1715 */
1716 vsi->tc_config.tc_info[i].qoffset = 0;
1717 vsi->tc_config.tc_info[i].qcount = 1;
1718 vsi->tc_config.tc_info[i].netdev_tc = 0;
1719 }
1720 }
1721
1722 /* Set actual Tx/Rx queue pairs */
1723 vsi->num_queue_pairs = offset + qcount;
1724
1725 /* Setup queue TC[0].qmap for given VSI context */
1726 ctxt->info.tc_mapping[0] = cpu_to_le16(qmap);
1727 ctxt->info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
1728 ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
1729 ctxt->info.valid_sections |= cpu_to_le16(sections);
1730
1731 /* Reconfigure RSS for main VSI with max queue count */
1732 vsi->rss_size = max_qcount;
1733 ret = i40e_vsi_config_rss(vsi);
1734 if (ret) {
1735 dev_info(&vsi->back->pdev->dev,
1736 "Failed to reconfig rss for num_queues (%u)\n",
1737 max_qcount);
1738 return ret;
1739 }
1740 vsi->reconfig_rss = true;
1741 dev_dbg(&vsi->back->pdev->dev,
1742 "Reconfigured rss with num_queues (%u)\n", max_qcount);
1743
1744 /* Find queue count available for channel VSIs and starting offset
1745 * for channel VSIs
1746 */
1747 override_q = vsi->mqprio_qopt.qopt.count[0];
1748 if (override_q && override_q < vsi->num_queue_pairs) {
1749 vsi->cnt_q_avail = vsi->num_queue_pairs - override_q;
1750 vsi->next_base_queue = override_q;
1751 }
1752 return 0;
1753}
1754
1755/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001756 * i40e_vsi_setup_queue_map - Setup a VSI queue map based on enabled_tc
1757 * @vsi: the VSI being setup
1758 * @ctxt: VSI context structure
1759 * @enabled_tc: Enabled TCs bitmap
1760 * @is_add: True if called before Add VSI
1761 *
1762 * Setup VSI queue mapping for enabled traffic classes.
1763 **/
1764static void i40e_vsi_setup_queue_map(struct i40e_vsi *vsi,
1765 struct i40e_vsi_context *ctxt,
1766 u8 enabled_tc,
1767 bool is_add)
1768{
1769 struct i40e_pf *pf = vsi->back;
1770 u16 sections = 0;
1771 u8 netdev_tc = 0;
1772 u16 numtc = 0;
1773 u16 qcount;
1774 u8 offset;
1775 u16 qmap;
1776 int i;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001777 u16 num_tc_qps = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001778
1779 sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
1780 offset = 0;
1781
1782 if (enabled_tc && (vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
1783 /* Find numtc from enabled TC bitmap */
1784 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08001785 if (enabled_tc & BIT(i)) /* TC is enabled */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001786 numtc++;
1787 }
1788 if (!numtc) {
1789 dev_warn(&pf->pdev->dev, "DCB is enabled but no TC enabled, forcing TC0\n");
1790 numtc = 1;
1791 }
1792 } else {
Amritha Nambiara9ce82f2017-09-07 04:00:22 -07001793 /* At least TC0 is enabled in non-DCB, non-MQPRIO case */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001794 numtc = 1;
1795 }
1796
1797 vsi->tc_config.numtc = numtc;
1798 vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001799 /* Number of queues per enabled TC */
Catherine Sullivan7d644022016-05-16 10:26:41 -07001800 qcount = vsi->alloc_queue_pairs;
1801
Anjali Singhai7f9ff472015-02-21 06:43:19 +00001802 num_tc_qps = qcount / numtc;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001803 num_tc_qps = min_t(int, num_tc_qps, i40e_pf_get_max_q_per_tc(pf));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001804
1805 /* Setup queue offset/count for all TCs for given VSI */
1806 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1807 /* See if the given TC is enabled for the given VSI */
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08001808 if (vsi->tc_config.enabled_tc & BIT(i)) {
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04001809 /* TC is enabled */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001810 int pow, num_qps;
1811
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001812 switch (vsi->type) {
1813 case I40E_VSI_MAIN:
Helin Zhangacd65442015-10-26 19:44:28 -04001814 qcount = min_t(int, pf->alloc_rss_size,
1815 num_tc_qps);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001816 break;
1817 case I40E_VSI_FDIR:
1818 case I40E_VSI_SRIOV:
1819 case I40E_VSI_VMDQ2:
1820 default:
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001821 qcount = num_tc_qps;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001822 WARN_ON(i != 0);
1823 break;
1824 }
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001825 vsi->tc_config.tc_info[i].qoffset = offset;
1826 vsi->tc_config.tc_info[i].qcount = qcount;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001827
Shannon Nelson1e200e42015-02-27 09:15:24 +00001828 /* find the next higher power-of-2 of num queue pairs */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001829 num_qps = qcount;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001830 pow = 0;
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04001831 while (num_qps && (BIT_ULL(pow) < qcount)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001832 pow++;
1833 num_qps >>= 1;
1834 }
1835
1836 vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
1837 qmap =
1838 (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
1839 (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
1840
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001841 offset += qcount;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001842 } else {
1843 /* TC is not enabled so set the offset to
1844 * default queue and allocate one queue
1845 * for the given TC.
1846 */
1847 vsi->tc_config.tc_info[i].qoffset = 0;
1848 vsi->tc_config.tc_info[i].qcount = 1;
1849 vsi->tc_config.tc_info[i].netdev_tc = 0;
1850
1851 qmap = 0;
1852 }
1853 ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
1854 }
1855
1856 /* Set actual Tx/Rx queue pairs */
1857 vsi->num_queue_pairs = offset;
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00001858 if ((vsi->type == I40E_VSI_MAIN) && (numtc == 1)) {
1859 if (vsi->req_queue_pairs > 0)
1860 vsi->num_queue_pairs = vsi->req_queue_pairs;
Anjali Singhai Jain26cdc442015-07-10 19:36:00 -04001861 else if (pf->flags & I40E_FLAG_MSIX_ENABLED)
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00001862 vsi->num_queue_pairs = pf->num_lan_msix;
1863 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001864
1865 /* Scheduler section valid can only be set for ADD VSI */
1866 if (is_add) {
1867 sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
1868
1869 ctxt->info.up_enable_bits = enabled_tc;
1870 }
1871 if (vsi->type == I40E_VSI_SRIOV) {
1872 ctxt->info.mapping_flags |=
1873 cpu_to_le16(I40E_AQ_VSI_QUE_MAP_NONCONTIG);
1874 for (i = 0; i < vsi->num_queue_pairs; i++)
1875 ctxt->info.queue_mapping[i] =
1876 cpu_to_le16(vsi->base_queue + i);
1877 } else {
1878 ctxt->info.mapping_flags |=
1879 cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
1880 ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
1881 }
1882 ctxt->info.valid_sections |= cpu_to_le16(sections);
1883}
1884
1885/**
Jacob Keller6622f5c2016-10-05 09:30:32 -07001886 * i40e_addr_sync - Callback for dev_(mc|uc)_sync to add address
1887 * @netdev: the netdevice
1888 * @addr: address to add
1889 *
1890 * Called by __dev_(mc|uc)_sync when an address needs to be added. We call
1891 * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
1892 */
1893static int i40e_addr_sync(struct net_device *netdev, const u8 *addr)
1894{
1895 struct i40e_netdev_priv *np = netdev_priv(netdev);
1896 struct i40e_vsi *vsi = np->vsi;
Jacob Keller6622f5c2016-10-05 09:30:32 -07001897
Jacob Kellerfeffdbe2016-11-11 12:39:35 -08001898 if (i40e_add_mac_filter(vsi, addr))
Jacob Keller6622f5c2016-10-05 09:30:32 -07001899 return 0;
1900 else
1901 return -ENOMEM;
1902}
1903
1904/**
1905 * i40e_addr_unsync - Callback for dev_(mc|uc)_sync to remove address
1906 * @netdev: the netdevice
1907 * @addr: address to add
1908 *
1909 * Called by __dev_(mc|uc)_sync when an address needs to be removed. We call
1910 * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
1911 */
1912static int i40e_addr_unsync(struct net_device *netdev, const u8 *addr)
1913{
1914 struct i40e_netdev_priv *np = netdev_priv(netdev);
1915 struct i40e_vsi *vsi = np->vsi;
1916
Jacob Kellerfeffdbe2016-11-11 12:39:35 -08001917 i40e_del_mac_filter(vsi, addr);
Jacob Keller6622f5c2016-10-05 09:30:32 -07001918
1919 return 0;
1920}
1921
1922/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001923 * i40e_set_rx_mode - NDO callback to set the netdev filters
1924 * @netdev: network interface device structure
1925 **/
1926static void i40e_set_rx_mode(struct net_device *netdev)
1927{
1928 struct i40e_netdev_priv *np = netdev_priv(netdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001929 struct i40e_vsi *vsi = np->vsi;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001930
Jacob Keller278e7d02016-10-05 09:30:37 -07001931 spin_lock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -04001932
Jacob Keller6622f5c2016-10-05 09:30:32 -07001933 __dev_uc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
1934 __dev_mc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001935
Jacob Keller278e7d02016-10-05 09:30:37 -07001936 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001937
1938 /* check for other flag changes */
1939 if (vsi->current_netdev_flags != vsi->netdev->flags) {
1940 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1941 vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
1942 }
1943}
1944
1945/**
Jacob Keller671889e2016-12-02 12:33:00 -08001946 * i40e_undo_del_filter_entries - Undo the changes made to MAC filter entries
Jacob Keller4a2ce272016-10-05 09:30:38 -07001947 * @vsi: Pointer to VSI struct
Kiran Patil21659032015-09-30 14:09:03 -04001948 * @from: Pointer to list which contains MAC filter entries - changes to
1949 * those entries needs to be undone.
1950 *
Jacob Keller671889e2016-12-02 12:33:00 -08001951 * MAC filter entries from this list were slated for deletion.
Kiran Patil21659032015-09-30 14:09:03 -04001952 **/
Jacob Keller671889e2016-12-02 12:33:00 -08001953static void i40e_undo_del_filter_entries(struct i40e_vsi *vsi,
1954 struct hlist_head *from)
Kiran Patil21659032015-09-30 14:09:03 -04001955{
Jacob Keller278e7d02016-10-05 09:30:37 -07001956 struct i40e_mac_filter *f;
1957 struct hlist_node *h;
Kiran Patil21659032015-09-30 14:09:03 -04001958
Jacob Keller278e7d02016-10-05 09:30:37 -07001959 hlist_for_each_entry_safe(f, h, from, hlist) {
1960 u64 key = i40e_addr_to_hkey(f->macaddr);
1961
Kiran Patil21659032015-09-30 14:09:03 -04001962 /* Move the element back into MAC filter list*/
Jacob Keller278e7d02016-10-05 09:30:37 -07001963 hlist_del(&f->hlist);
1964 hash_add(vsi->mac_filter_hash, &f->hlist, key);
Kiran Patil21659032015-09-30 14:09:03 -04001965 }
1966}
1967
1968/**
Jacob Keller671889e2016-12-02 12:33:00 -08001969 * i40e_undo_add_filter_entries - Undo the changes made to MAC filter entries
1970 * @vsi: Pointer to vsi struct
1971 * @from: Pointer to list which contains MAC filter entries - changes to
1972 * those entries needs to be undone.
1973 *
1974 * MAC filter entries from this list were slated for addition.
1975 **/
1976static void i40e_undo_add_filter_entries(struct i40e_vsi *vsi,
1977 struct hlist_head *from)
1978{
1979 struct i40e_new_mac_filter *new;
1980 struct hlist_node *h;
1981
1982 hlist_for_each_entry_safe(new, h, from, hlist) {
1983 /* We can simply free the wrapper structure */
1984 hlist_del(&new->hlist);
1985 kfree(new);
1986 }
1987}
1988
1989/**
Jacob Kellerd88d40b2016-12-02 12:32:59 -08001990 * i40e_next_entry - Get the next non-broadcast filter from a list
Jacob Keller671889e2016-12-02 12:33:00 -08001991 * @next: pointer to filter in list
Jacob Kellerd88d40b2016-12-02 12:32:59 -08001992 *
1993 * Returns the next non-broadcast filter in the list. Required so that we
1994 * ignore broadcast filters within the list, since these are not handled via
1995 * the normal firmware update path.
1996 */
Jacob Keller671889e2016-12-02 12:33:00 -08001997static
1998struct i40e_new_mac_filter *i40e_next_filter(struct i40e_new_mac_filter *next)
Jacob Kellerd88d40b2016-12-02 12:32:59 -08001999{
Jacob Keller584a8872017-03-07 15:17:52 -08002000 hlist_for_each_entry_continue(next, hlist) {
2001 if (!is_broadcast_ether_addr(next->f->macaddr))
2002 return next;
Jacob Kellerd88d40b2016-12-02 12:32:59 -08002003 }
2004
Jacob Keller584a8872017-03-07 15:17:52 -08002005 return NULL;
Jacob Kellerd88d40b2016-12-02 12:32:59 -08002006}
2007
2008/**
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002009 * i40e_update_filter_state - Update filter state based on return data
2010 * from firmware
2011 * @count: Number of filters added
2012 * @add_list: return data from fw
2013 * @head: pointer to first filter in current batch
Kiran Patil21659032015-09-30 14:09:03 -04002014 *
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002015 * MAC filter entries from list were slated to be added to device. Returns
2016 * number of successful filters. Note that 0 does NOT mean success!
Kiran Patil21659032015-09-30 14:09:03 -04002017 **/
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002018static int
2019i40e_update_filter_state(int count,
2020 struct i40e_aqc_add_macvlan_element_data *add_list,
Jacob Keller671889e2016-12-02 12:33:00 -08002021 struct i40e_new_mac_filter *add_head)
Kiran Patil21659032015-09-30 14:09:03 -04002022{
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002023 int retval = 0;
2024 int i;
Kiran Patil21659032015-09-30 14:09:03 -04002025
Jacob Kellerac9e2392016-11-11 12:39:27 -08002026 for (i = 0; i < count; i++) {
2027 /* Always check status of each filter. We don't need to check
2028 * the firmware return status because we pre-set the filter
2029 * status to I40E_AQC_MM_ERR_NO_RES when sending the filter
2030 * request to the adminq. Thus, if it no longer matches then
2031 * we know the filter is active.
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002032 */
Jacob Kellerac9e2392016-11-11 12:39:27 -08002033 if (add_list[i].match_method == I40E_AQC_MM_ERR_NO_RES) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002034 add_head->state = I40E_FILTER_FAILED;
Jacob Kellerac9e2392016-11-11 12:39:27 -08002035 } else {
2036 add_head->state = I40E_FILTER_ACTIVE;
2037 retval++;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002038 }
Jacob Kellerac9e2392016-11-11 12:39:27 -08002039
Jacob Kellerd88d40b2016-12-02 12:32:59 -08002040 add_head = i40e_next_filter(add_head);
2041 if (!add_head)
2042 break;
Kiran Patil21659032015-09-30 14:09:03 -04002043 }
Jacob Kellerac9e2392016-11-11 12:39:27 -08002044
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002045 return retval;
Kiran Patil21659032015-09-30 14:09:03 -04002046}
2047
2048/**
Jacob Keller00936312016-10-05 09:30:41 -07002049 * i40e_aqc_del_filters - Request firmware to delete a set of filters
2050 * @vsi: ptr to the VSI
2051 * @vsi_name: name to display in messages
2052 * @list: the list of filters to send to firmware
2053 * @num_del: the number of filters to delete
2054 * @retval: Set to -EIO on failure to delete
2055 *
2056 * Send a request to firmware via AdminQ to delete a set of filters. Uses
2057 * *retval instead of a return value so that success does not force ret_val to
2058 * be set to 0. This ensures that a sequence of calls to this function
2059 * preserve the previous value of *retval on successful delete.
2060 */
2061static
2062void i40e_aqc_del_filters(struct i40e_vsi *vsi, const char *vsi_name,
2063 struct i40e_aqc_remove_macvlan_element_data *list,
2064 int num_del, int *retval)
2065{
2066 struct i40e_hw *hw = &vsi->back->hw;
2067 i40e_status aq_ret;
2068 int aq_err;
2069
2070 aq_ret = i40e_aq_remove_macvlan(hw, vsi->seid, list, num_del, NULL);
2071 aq_err = hw->aq.asq_last_status;
2072
2073 /* Explicitly ignore and do not report when firmware returns ENOENT */
2074 if (aq_ret && !(aq_err == I40E_AQ_RC_ENOENT)) {
2075 *retval = -EIO;
2076 dev_info(&vsi->back->pdev->dev,
2077 "ignoring delete macvlan error on %s, err %s, aq_err %s\n",
2078 vsi_name, i40e_stat_str(hw, aq_ret),
2079 i40e_aq_str(hw, aq_err));
2080 }
2081}
2082
2083/**
2084 * i40e_aqc_add_filters - Request firmware to add a set of filters
2085 * @vsi: ptr to the VSI
2086 * @vsi_name: name to display in messages
2087 * @list: the list of filters to send to firmware
2088 * @add_head: Position in the add hlist
2089 * @num_add: the number of filters to add
2090 * @promisc_change: set to true on exit if promiscuous mode was forced on
2091 *
2092 * Send a request to firmware via AdminQ to add a chunk of filters. Will set
2093 * promisc_changed to true if the firmware has run out of space for more
2094 * filters.
2095 */
2096static
2097void i40e_aqc_add_filters(struct i40e_vsi *vsi, const char *vsi_name,
2098 struct i40e_aqc_add_macvlan_element_data *list,
Jacob Keller671889e2016-12-02 12:33:00 -08002099 struct i40e_new_mac_filter *add_head,
Jacob Keller00936312016-10-05 09:30:41 -07002100 int num_add, bool *promisc_changed)
2101{
2102 struct i40e_hw *hw = &vsi->back->hw;
Jacob Keller00936312016-10-05 09:30:41 -07002103 int aq_err, fcnt;
2104
Jacob Kellerac9e2392016-11-11 12:39:27 -08002105 i40e_aq_add_macvlan(hw, vsi->seid, list, num_add, NULL);
Jacob Keller00936312016-10-05 09:30:41 -07002106 aq_err = hw->aq.asq_last_status;
Jacob Kellerac9e2392016-11-11 12:39:27 -08002107 fcnt = i40e_update_filter_state(num_add, list, add_head);
Jacob Keller00936312016-10-05 09:30:41 -07002108
2109 if (fcnt != num_add) {
2110 *promisc_changed = true;
Jacob Keller0da36b92017-04-19 09:25:55 -04002111 set_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
Jacob Keller00936312016-10-05 09:30:41 -07002112 dev_warn(&vsi->back->pdev->dev,
2113 "Error %s adding RX filters on %s, promiscuous mode forced on\n",
2114 i40e_aq_str(hw, aq_err),
2115 vsi_name);
2116 }
2117}
2118
2119/**
Jacob Keller435c0842016-11-08 13:05:10 -08002120 * i40e_aqc_broadcast_filter - Set promiscuous broadcast flags
2121 * @vsi: pointer to the VSI
2122 * @f: filter data
2123 *
2124 * This function sets or clears the promiscuous broadcast flags for VLAN
2125 * filters in order to properly receive broadcast frames. Assumes that only
2126 * broadcast filters are passed.
Jacob Keller671889e2016-12-02 12:33:00 -08002127 *
2128 * Returns status indicating success or failure;
Jacob Keller435c0842016-11-08 13:05:10 -08002129 **/
Jacob Keller671889e2016-12-02 12:33:00 -08002130static i40e_status
2131i40e_aqc_broadcast_filter(struct i40e_vsi *vsi, const char *vsi_name,
2132 struct i40e_mac_filter *f)
Jacob Keller435c0842016-11-08 13:05:10 -08002133{
2134 bool enable = f->state == I40E_FILTER_NEW;
2135 struct i40e_hw *hw = &vsi->back->hw;
2136 i40e_status aq_ret;
2137
2138 if (f->vlan == I40E_VLAN_ANY) {
2139 aq_ret = i40e_aq_set_vsi_broadcast(hw,
2140 vsi->seid,
2141 enable,
2142 NULL);
2143 } else {
2144 aq_ret = i40e_aq_set_vsi_bc_promisc_on_vlan(hw,
2145 vsi->seid,
2146 enable,
2147 f->vlan,
2148 NULL);
2149 }
2150
Jacob Keller671889e2016-12-02 12:33:00 -08002151 if (aq_ret)
Jacob Keller435c0842016-11-08 13:05:10 -08002152 dev_warn(&vsi->back->pdev->dev,
2153 "Error %s setting broadcast promiscuous mode on %s\n",
2154 i40e_aq_str(hw, hw->aq.asq_last_status),
2155 vsi_name);
Jacob Keller671889e2016-12-02 12:33:00 -08002156
2157 return aq_ret;
Jacob Keller435c0842016-11-08 13:05:10 -08002158}
2159
2160/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002161 * i40e_sync_vsi_filters - Update the VSI filter list to the HW
2162 * @vsi: ptr to the VSI
2163 *
2164 * Push any outstanding VSI filter changes through the AdminQ.
2165 *
2166 * Returns 0 or error value
2167 **/
Jesse Brandeburg17652c62015-11-05 17:01:02 -08002168int i40e_sync_vsi_filters(struct i40e_vsi *vsi)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002169{
Jacob Keller278e7d02016-10-05 09:30:37 -07002170 struct hlist_head tmp_add_list, tmp_del_list;
Jacob Keller671889e2016-12-02 12:33:00 -08002171 struct i40e_mac_filter *f;
2172 struct i40e_new_mac_filter *new, *add_head = NULL;
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002173 struct i40e_hw *hw = &vsi->back->hw;
Jacob Keller38326212016-11-11 12:39:26 -08002174 unsigned int failed_filters = 0;
Alan Brady84f5ca62016-10-05 09:30:39 -07002175 unsigned int vlan_filters = 0;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002176 bool promisc_changed = false;
Shannon Nelson2d1de822016-05-16 10:26:44 -07002177 char vsi_name[16] = "PF";
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002178 int filter_list_len = 0;
Mitch Williamsea02e902015-11-09 15:35:50 -08002179 i40e_status aq_ret = 0;
Alan Brady84f5ca62016-10-05 09:30:39 -07002180 u32 changed_flags = 0;
Jacob Keller278e7d02016-10-05 09:30:37 -07002181 struct hlist_node *h;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002182 struct i40e_pf *pf;
2183 int num_add = 0;
2184 int num_del = 0;
Alan Brady84f5ca62016-10-05 09:30:39 -07002185 int retval = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002186 u16 cmd_flags;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002187 int list_size;
Jacob Keller278e7d02016-10-05 09:30:37 -07002188 int bkt;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002189
2190 /* empty array typed pointers, kcalloc later */
2191 struct i40e_aqc_add_macvlan_element_data *add_list;
2192 struct i40e_aqc_remove_macvlan_element_data *del_list;
2193
Jacob Keller0da36b92017-04-19 09:25:55 -04002194 while (test_and_set_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002195 usleep_range(1000, 2000);
2196 pf = vsi->back;
2197
2198 if (vsi->netdev) {
2199 changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
2200 vsi->current_netdev_flags = vsi->netdev->flags;
2201 }
2202
Jacob Keller278e7d02016-10-05 09:30:37 -07002203 INIT_HLIST_HEAD(&tmp_add_list);
2204 INIT_HLIST_HEAD(&tmp_del_list);
Kiran Patil21659032015-09-30 14:09:03 -04002205
Shannon Nelson2d1de822016-05-16 10:26:44 -07002206 if (vsi->type == I40E_VSI_SRIOV)
2207 snprintf(vsi_name, sizeof(vsi_name) - 1, "VF %d", vsi->vf_id);
2208 else if (vsi->type != I40E_VSI_MAIN)
2209 snprintf(vsi_name, sizeof(vsi_name) - 1, "vsi %d", vsi->seid);
2210
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002211 if (vsi->flags & I40E_VSI_FLAG_FILTER_CHANGED) {
2212 vsi->flags &= ~I40E_VSI_FLAG_FILTER_CHANGED;
2213
Jacob Keller278e7d02016-10-05 09:30:37 -07002214 spin_lock_bh(&vsi->mac_filter_hash_lock);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002215 /* Create a list of filters to delete. */
Jacob Keller278e7d02016-10-05 09:30:37 -07002216 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002217 if (f->state == I40E_FILTER_REMOVE) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002218 /* Move the element into temporary del_list */
Jacob Keller278e7d02016-10-05 09:30:37 -07002219 hash_del(&f->hlist);
2220 hlist_add_head(&f->hlist, &tmp_del_list);
Alan Brady84f5ca62016-10-05 09:30:39 -07002221
2222 /* Avoid counting removed filters */
2223 continue;
Kiran Patil21659032015-09-30 14:09:03 -04002224 }
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002225 if (f->state == I40E_FILTER_NEW) {
Jacob Keller671889e2016-12-02 12:33:00 -08002226 /* Create a temporary i40e_new_mac_filter */
2227 new = kzalloc(sizeof(*new), GFP_ATOMIC);
2228 if (!new)
2229 goto err_no_memory_locked;
2230
2231 /* Store pointer to the real filter */
2232 new->f = f;
2233 new->state = f->state;
2234
2235 /* Add it to the hash list */
2236 hlist_add_head(&new->hlist, &tmp_add_list);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002237 }
Alan Brady84f5ca62016-10-05 09:30:39 -07002238
Jacob Keller489a3262016-11-11 12:39:31 -08002239 /* Count the number of active (current and new) VLAN
2240 * filters we have now. Does not count filters which
2241 * are marked for deletion.
Alan Brady84f5ca62016-10-05 09:30:39 -07002242 */
2243 if (f->vlan > 0)
2244 vlan_filters++;
Alan Brady84f5ca62016-10-05 09:30:39 -07002245 }
2246
Jacob Keller489a3262016-11-11 12:39:31 -08002247 retval = i40e_correct_mac_vlan_filters(vsi,
2248 &tmp_add_list,
2249 &tmp_del_list,
2250 vlan_filters);
2251 if (retval)
2252 goto err_no_memory_locked;
Alan Brady84f5ca62016-10-05 09:30:39 -07002253
Jacob Keller278e7d02016-10-05 09:30:37 -07002254 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -04002255 }
2256
2257 /* Now process 'del_list' outside the lock */
Jacob Keller278e7d02016-10-05 09:30:37 -07002258 if (!hlist_empty(&tmp_del_list)) {
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002259 filter_list_len = hw->aq.asq_buf_size /
Kiran Patil21659032015-09-30 14:09:03 -04002260 sizeof(struct i40e_aqc_remove_macvlan_element_data);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002261 list_size = filter_list_len *
Shannon Nelsonf1199992015-11-19 11:34:23 -08002262 sizeof(struct i40e_aqc_remove_macvlan_element_data);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002263 del_list = kzalloc(list_size, GFP_ATOMIC);
Jacob Keller4a2ce272016-10-05 09:30:38 -07002264 if (!del_list)
2265 goto err_no_memory;
Kiran Patil21659032015-09-30 14:09:03 -04002266
Jacob Keller278e7d02016-10-05 09:30:37 -07002267 hlist_for_each_entry_safe(f, h, &tmp_del_list, hlist) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002268 cmd_flags = 0;
2269
Jacob Keller435c0842016-11-08 13:05:10 -08002270 /* handle broadcast filters by updating the broadcast
Jacob Kellerd88d40b2016-12-02 12:32:59 -08002271 * promiscuous flag and release filter list.
Jacob Keller435c0842016-11-08 13:05:10 -08002272 */
2273 if (is_broadcast_ether_addr(f->macaddr)) {
2274 i40e_aqc_broadcast_filter(vsi, vsi_name, f);
2275
2276 hlist_del(&f->hlist);
2277 kfree(f);
2278 continue;
2279 }
2280
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002281 /* add to delete list */
Greg Rose9a173902014-05-22 06:32:02 +00002282 ether_addr_copy(del_list[num_del].mac_addr, f->macaddr);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002283 if (f->vlan == I40E_VLAN_ANY) {
2284 del_list[num_del].vlan_tag = 0;
Alan Bradya6cb9142016-09-06 18:05:07 -07002285 cmd_flags |= I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002286 } else {
2287 del_list[num_del].vlan_tag =
2288 cpu_to_le16((u16)(f->vlan));
2289 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002290
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002291 cmd_flags |= I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
2292 del_list[num_del].flags = cmd_flags;
2293 num_del++;
2294
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002295 /* flush a full buffer */
2296 if (num_del == filter_list_len) {
Jacob Keller00936312016-10-05 09:30:41 -07002297 i40e_aqc_del_filters(vsi, vsi_name, del_list,
2298 num_del, &retval);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002299 memset(del_list, 0, list_size);
Jacob Keller00936312016-10-05 09:30:41 -07002300 num_del = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002301 }
Kiran Patil21659032015-09-30 14:09:03 -04002302 /* Release memory for MAC filter entries which were
2303 * synced up with HW.
2304 */
Jacob Keller278e7d02016-10-05 09:30:37 -07002305 hlist_del(&f->hlist);
Kiran Patil21659032015-09-30 14:09:03 -04002306 kfree(f);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002307 }
Kiran Patil21659032015-09-30 14:09:03 -04002308
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002309 if (num_del) {
Jacob Keller00936312016-10-05 09:30:41 -07002310 i40e_aqc_del_filters(vsi, vsi_name, del_list,
2311 num_del, &retval);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002312 }
2313
2314 kfree(del_list);
2315 del_list = NULL;
Kiran Patil21659032015-09-30 14:09:03 -04002316 }
2317
Jacob Keller278e7d02016-10-05 09:30:37 -07002318 if (!hlist_empty(&tmp_add_list)) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002319 /* Do all the adds now. */
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002320 filter_list_len = hw->aq.asq_buf_size /
Shannon Nelsonf1199992015-11-19 11:34:23 -08002321 sizeof(struct i40e_aqc_add_macvlan_element_data);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002322 list_size = filter_list_len *
2323 sizeof(struct i40e_aqc_add_macvlan_element_data);
2324 add_list = kzalloc(list_size, GFP_ATOMIC);
Jacob Keller4a2ce272016-10-05 09:30:38 -07002325 if (!add_list)
2326 goto err_no_memory;
2327
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002328 num_add = 0;
Jacob Keller671889e2016-12-02 12:33:00 -08002329 hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
Jacob Kellerd19cb642017-04-21 13:38:05 -07002330 if (test_bit(__I40E_VSI_OVERFLOW_PROMISC,
Jacob Keller0da36b92017-04-19 09:25:55 -04002331 vsi->state)) {
Jacob Keller671889e2016-12-02 12:33:00 -08002332 new->state = I40E_FILTER_FAILED;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002333 continue;
2334 }
Jacob Keller435c0842016-11-08 13:05:10 -08002335
2336 /* handle broadcast filters by updating the broadcast
2337 * promiscuous flag instead of adding a MAC filter.
2338 */
Jacob Keller671889e2016-12-02 12:33:00 -08002339 if (is_broadcast_ether_addr(new->f->macaddr)) {
2340 if (i40e_aqc_broadcast_filter(vsi, vsi_name,
2341 new->f))
2342 new->state = I40E_FILTER_FAILED;
2343 else
2344 new->state = I40E_FILTER_ACTIVE;
Jacob Keller435c0842016-11-08 13:05:10 -08002345 continue;
2346 }
2347
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002348 /* add to add array */
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002349 if (num_add == 0)
Jacob Keller671889e2016-12-02 12:33:00 -08002350 add_head = new;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002351 cmd_flags = 0;
Jacob Keller671889e2016-12-02 12:33:00 -08002352 ether_addr_copy(add_list[num_add].mac_addr,
2353 new->f->macaddr);
2354 if (new->f->vlan == I40E_VLAN_ANY) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002355 add_list[num_add].vlan_tag = 0;
2356 cmd_flags |= I40E_AQC_MACVLAN_ADD_IGNORE_VLAN;
2357 } else {
2358 add_list[num_add].vlan_tag =
Jacob Keller671889e2016-12-02 12:33:00 -08002359 cpu_to_le16((u16)(new->f->vlan));
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002360 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002361 add_list[num_add].queue_number = 0;
Jacob Kellerac9e2392016-11-11 12:39:27 -08002362 /* set invalid match method for later detection */
Keller, Jacob E0266ac42016-12-09 13:39:21 -08002363 add_list[num_add].match_method = I40E_AQC_MM_ERR_NO_RES;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002364 cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002365 add_list[num_add].flags = cpu_to_le16(cmd_flags);
2366 num_add++;
2367
2368 /* flush a full buffer */
2369 if (num_add == filter_list_len) {
Jacob Keller00936312016-10-05 09:30:41 -07002370 i40e_aqc_add_filters(vsi, vsi_name, add_list,
2371 add_head, num_add,
2372 &promisc_changed);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002373 memset(add_list, 0, list_size);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002374 num_add = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002375 }
2376 }
2377 if (num_add) {
Jacob Keller00936312016-10-05 09:30:41 -07002378 i40e_aqc_add_filters(vsi, vsi_name, add_list, add_head,
2379 num_add, &promisc_changed);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002380 }
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002381 /* Now move all of the filters from the temp add list back to
2382 * the VSI's list.
2383 */
Jacob Keller278e7d02016-10-05 09:30:37 -07002384 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller671889e2016-12-02 12:33:00 -08002385 hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
2386 /* Only update the state if we're still NEW */
2387 if (new->f->state == I40E_FILTER_NEW)
2388 new->f->state = new->state;
2389 hlist_del(&new->hlist);
2390 kfree(new);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002391 }
Jacob Keller278e7d02016-10-05 09:30:37 -07002392 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002393 kfree(add_list);
2394 add_list = NULL;
2395 }
2396
Jacob Keller38326212016-11-11 12:39:26 -08002397 /* Determine the number of active and failed filters. */
2398 spin_lock_bh(&vsi->mac_filter_hash_lock);
2399 vsi->active_filters = 0;
2400 hash_for_each(vsi->mac_filter_hash, bkt, f, hlist) {
2401 if (f->state == I40E_FILTER_ACTIVE)
2402 vsi->active_filters++;
2403 else if (f->state == I40E_FILTER_FAILED)
2404 failed_filters++;
2405 }
2406 spin_unlock_bh(&vsi->mac_filter_hash_lock);
2407
2408 /* If promiscuous mode has changed, we need to calculate a new
2409 * threshold for when we are safe to exit
2410 */
2411 if (promisc_changed)
2412 vsi->promisc_threshold = (vsi->active_filters * 3) / 4;
2413
2414 /* Check if we are able to exit overflow promiscuous mode. We can
2415 * safely exit if we didn't just enter, we no longer have any failed
2416 * filters, and we have reduced filters below the threshold value.
2417 */
Jacob Keller0da36b92017-04-19 09:25:55 -04002418 if (test_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state) &&
Jacob Keller38326212016-11-11 12:39:26 -08002419 !promisc_changed && !failed_filters &&
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002420 (vsi->active_filters < vsi->promisc_threshold)) {
Jacob Keller38326212016-11-11 12:39:26 -08002421 dev_info(&pf->pdev->dev,
2422 "filter logjam cleared on %s, leaving overflow promiscuous mode\n",
2423 vsi_name);
Jacob Keller0da36b92017-04-19 09:25:55 -04002424 clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
Jacob Keller38326212016-11-11 12:39:26 -08002425 promisc_changed = true;
2426 vsi->promisc_threshold = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002427 }
2428
Anjali Singhai Jaina856b5c2016-04-13 03:08:23 -07002429 /* if the VF is not trusted do not do promisc */
2430 if ((vsi->type == I40E_VSI_SRIOV) && !pf->vf[vsi->vf_id].trusted) {
Jacob Keller0da36b92017-04-19 09:25:55 -04002431 clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
Anjali Singhai Jaina856b5c2016-04-13 03:08:23 -07002432 goto out;
2433 }
2434
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002435 /* check for changes in promiscuous modes */
2436 if (changed_flags & IFF_ALLMULTI) {
2437 bool cur_multipromisc;
Jesse Brandeburg6995b362015-08-28 17:55:54 -04002438
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002439 cur_multipromisc = !!(vsi->current_netdev_flags & IFF_ALLMULTI);
Mitch Williamsea02e902015-11-09 15:35:50 -08002440 aq_ret = i40e_aq_set_vsi_multicast_promiscuous(&vsi->back->hw,
2441 vsi->seid,
2442 cur_multipromisc,
2443 NULL);
2444 if (aq_ret) {
2445 retval = i40e_aq_rc_to_posix(aq_ret,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002446 hw->aq.asq_last_status);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002447 dev_info(&pf->pdev->dev,
Shannon Nelson2d1de822016-05-16 10:26:44 -07002448 "set multi promisc failed on %s, err %s aq_err %s\n",
2449 vsi_name,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002450 i40e_stat_str(hw, aq_ret),
2451 i40e_aq_str(hw, hw->aq.asq_last_status));
Mitch Williamsea02e902015-11-09 15:35:50 -08002452 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002453 }
Alan Bradye5887232017-06-07 05:43:10 -04002454
2455 if ((changed_flags & IFF_PROMISC) || promisc_changed) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002456 bool cur_promisc;
Jesse Brandeburg6995b362015-08-28 17:55:54 -04002457
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002458 cur_promisc = (!!(vsi->current_netdev_flags & IFF_PROMISC) ||
Jacob Kellerd19cb642017-04-21 13:38:05 -07002459 test_bit(__I40E_VSI_OVERFLOW_PROMISC,
Jacob Keller0da36b92017-04-19 09:25:55 -04002460 vsi->state));
Anjali Singhai Jain6784ed52016-01-15 14:33:13 -08002461 if ((vsi->type == I40E_VSI_MAIN) &&
2462 (pf->lan_veb != I40E_NO_VEB) &&
2463 !(pf->flags & I40E_FLAG_MFP_ENABLED)) {
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002464 /* set defport ON for Main VSI instead of true promisc
2465 * this way we will get all unicast/multicast and VLAN
2466 * promisc behavior but will not get VF or VMDq traffic
2467 * replicated on the Main VSI.
2468 */
2469 if (pf->cur_promisc != cur_promisc) {
2470 pf->cur_promisc = cur_promisc;
Mitch Williams5bc16032016-05-16 10:26:43 -07002471 if (cur_promisc)
2472 aq_ret =
2473 i40e_aq_set_default_vsi(hw,
2474 vsi->seid,
2475 NULL);
2476 else
2477 aq_ret =
2478 i40e_aq_clear_default_vsi(hw,
2479 vsi->seid,
2480 NULL);
2481 if (aq_ret) {
2482 retval = i40e_aq_rc_to_posix(aq_ret,
2483 hw->aq.asq_last_status);
2484 dev_info(&pf->pdev->dev,
Shannon Nelson2d1de822016-05-16 10:26:44 -07002485 "Set default VSI failed on %s, err %s, aq_err %s\n",
2486 vsi_name,
Mitch Williams5bc16032016-05-16 10:26:43 -07002487 i40e_stat_str(hw, aq_ret),
2488 i40e_aq_str(hw,
2489 hw->aq.asq_last_status));
2490 }
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002491 }
2492 } else {
Mitch Williamsea02e902015-11-09 15:35:50 -08002493 aq_ret = i40e_aq_set_vsi_unicast_promiscuous(
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002494 hw,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002495 vsi->seid,
Anjali Singhai Jainb5569892016-05-03 15:13:12 -07002496 cur_promisc, NULL,
2497 true);
Mitch Williamsea02e902015-11-09 15:35:50 -08002498 if (aq_ret) {
2499 retval =
2500 i40e_aq_rc_to_posix(aq_ret,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002501 hw->aq.asq_last_status);
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002502 dev_info(&pf->pdev->dev,
Shannon Nelson2d1de822016-05-16 10:26:44 -07002503 "set unicast promisc failed on %s, err %s, aq_err %s\n",
2504 vsi_name,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002505 i40e_stat_str(hw, aq_ret),
2506 i40e_aq_str(hw,
2507 hw->aq.asq_last_status));
Mitch Williamsea02e902015-11-09 15:35:50 -08002508 }
2509 aq_ret = i40e_aq_set_vsi_multicast_promiscuous(
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002510 hw,
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002511 vsi->seid,
2512 cur_promisc, NULL);
Mitch Williamsea02e902015-11-09 15:35:50 -08002513 if (aq_ret) {
2514 retval =
2515 i40e_aq_rc_to_posix(aq_ret,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002516 hw->aq.asq_last_status);
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002517 dev_info(&pf->pdev->dev,
Shannon Nelson2d1de822016-05-16 10:26:44 -07002518 "set multicast promisc failed on %s, err %s, aq_err %s\n",
2519 vsi_name,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002520 i40e_stat_str(hw, aq_ret),
2521 i40e_aq_str(hw,
2522 hw->aq.asq_last_status));
Mitch Williamsea02e902015-11-09 15:35:50 -08002523 }
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002524 }
Mitch Williamsea02e902015-11-09 15:35:50 -08002525 aq_ret = i40e_aq_set_vsi_broadcast(&vsi->back->hw,
2526 vsi->seid,
2527 cur_promisc, NULL);
2528 if (aq_ret) {
2529 retval = i40e_aq_rc_to_posix(aq_ret,
2530 pf->hw.aq.asq_last_status);
Greg Rose1a103702013-11-28 06:42:39 +00002531 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002532 "set brdcast promisc failed, err %s, aq_err %s\n",
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002533 i40e_stat_str(hw, aq_ret),
2534 i40e_aq_str(hw,
2535 hw->aq.asq_last_status));
Mitch Williamsea02e902015-11-09 15:35:50 -08002536 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002537 }
Mitch Williamsea02e902015-11-09 15:35:50 -08002538out:
Jesse Brandeburg2818ccd2016-01-13 16:51:38 -08002539 /* if something went wrong then set the changed flag so we try again */
2540 if (retval)
2541 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2542
Jacob Keller0da36b92017-04-19 09:25:55 -04002543 clear_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state);
Mitch Williamsea02e902015-11-09 15:35:50 -08002544 return retval;
Jacob Keller4a2ce272016-10-05 09:30:38 -07002545
2546err_no_memory:
2547 /* Restore elements on the temporary add and delete lists */
2548 spin_lock_bh(&vsi->mac_filter_hash_lock);
Alan Brady84f5ca62016-10-05 09:30:39 -07002549err_no_memory_locked:
Jacob Keller671889e2016-12-02 12:33:00 -08002550 i40e_undo_del_filter_entries(vsi, &tmp_del_list);
2551 i40e_undo_add_filter_entries(vsi, &tmp_add_list);
Jacob Keller4a2ce272016-10-05 09:30:38 -07002552 spin_unlock_bh(&vsi->mac_filter_hash_lock);
2553
2554 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
Jacob Keller0da36b92017-04-19 09:25:55 -04002555 clear_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state);
Jacob Keller4a2ce272016-10-05 09:30:38 -07002556 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002557}
2558
2559/**
2560 * i40e_sync_filters_subtask - Sync the VSI filter list with HW
2561 * @pf: board private structure
2562 **/
2563static void i40e_sync_filters_subtask(struct i40e_pf *pf)
2564{
2565 int v;
2566
2567 if (!pf || !(pf->flags & I40E_FLAG_FILTER_SYNC))
2568 return;
2569 pf->flags &= ~I40E_FLAG_FILTER_SYNC;
2570
Mitch Williams505682c2014-05-20 08:01:37 +00002571 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002572 if (pf->vsi[v] &&
Jesse Brandeburg17652c62015-11-05 17:01:02 -08002573 (pf->vsi[v]->flags & I40E_VSI_FLAG_FILTER_CHANGED)) {
2574 int ret = i40e_sync_vsi_filters(pf->vsi[v]);
2575
2576 if (ret) {
2577 /* come back and try again later */
2578 pf->flags |= I40E_FLAG_FILTER_SYNC;
2579 break;
2580 }
2581 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002582 }
2583}
2584
2585/**
Björn Töpel0c8493d2017-05-24 07:55:34 +02002586 * i40e_max_xdp_frame_size - returns the maximum allowed frame size for XDP
2587 * @vsi: the vsi
2588 **/
2589static int i40e_max_xdp_frame_size(struct i40e_vsi *vsi)
2590{
2591 if (PAGE_SIZE >= 8192 || (vsi->back->flags & I40E_FLAG_LEGACY_RX))
2592 return I40E_RXBUFFER_2048;
2593 else
2594 return I40E_RXBUFFER_3072;
2595}
2596
2597/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002598 * i40e_change_mtu - NDO callback to change the Maximum Transfer Unit
2599 * @netdev: network interface device structure
2600 * @new_mtu: new value for maximum frame size
2601 *
2602 * Returns 0 on success, negative on failure
2603 **/
2604static int i40e_change_mtu(struct net_device *netdev, int new_mtu)
2605{
2606 struct i40e_netdev_priv *np = netdev_priv(netdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002607 struct i40e_vsi *vsi = np->vsi;
Mitch Williams0ef2d5a2017-01-24 10:24:00 -08002608 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002609
Björn Töpel0c8493d2017-05-24 07:55:34 +02002610 if (i40e_enabled_xdp_vsi(vsi)) {
2611 int frame_size = new_mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
2612
2613 if (frame_size > i40e_max_xdp_frame_size(vsi))
2614 return -EINVAL;
2615 }
2616
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002617 netdev_info(netdev, "changing MTU from %d to %d\n",
2618 netdev->mtu, new_mtu);
2619 netdev->mtu = new_mtu;
2620 if (netif_running(netdev))
2621 i40e_vsi_reinit_locked(vsi);
Mitch Williams0ef2d5a2017-01-24 10:24:00 -08002622 pf->flags |= (I40E_FLAG_SERVICE_CLIENT_REQUESTED |
2623 I40E_FLAG_CLIENT_L2_CHANGE);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002624 return 0;
2625}
2626
2627/**
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00002628 * i40e_ioctl - Access the hwtstamp interface
2629 * @netdev: network interface device structure
2630 * @ifr: interface request data
2631 * @cmd: ioctl command
2632 **/
2633int i40e_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
2634{
2635 struct i40e_netdev_priv *np = netdev_priv(netdev);
2636 struct i40e_pf *pf = np->vsi->back;
2637
2638 switch (cmd) {
2639 case SIOCGHWTSTAMP:
2640 return i40e_ptp_get_ts_config(pf, ifr);
2641 case SIOCSHWTSTAMP:
2642 return i40e_ptp_set_ts_config(pf, ifr);
2643 default:
2644 return -EOPNOTSUPP;
2645 }
2646}
2647
2648/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002649 * i40e_vlan_stripping_enable - Turn on vlan stripping for the VSI
2650 * @vsi: the vsi being adjusted
2651 **/
2652void i40e_vlan_stripping_enable(struct i40e_vsi *vsi)
2653{
2654 struct i40e_vsi_context ctxt;
2655 i40e_status ret;
2656
2657 if ((vsi->info.valid_sections &
2658 cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
2659 ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_MODE_MASK) == 0))
2660 return; /* already enabled */
2661
2662 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2663 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
2664 I40E_AQ_VSI_PVLAN_EMOD_STR_BOTH;
2665
2666 ctxt.seid = vsi->seid;
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07002667 ctxt.info = vsi->info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002668 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2669 if (ret) {
2670 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002671 "update vlan stripping failed, err %s aq_err %s\n",
2672 i40e_stat_str(&vsi->back->hw, ret),
2673 i40e_aq_str(&vsi->back->hw,
2674 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002675 }
2676}
2677
2678/**
2679 * i40e_vlan_stripping_disable - Turn off vlan stripping for the VSI
2680 * @vsi: the vsi being adjusted
2681 **/
2682void i40e_vlan_stripping_disable(struct i40e_vsi *vsi)
2683{
2684 struct i40e_vsi_context ctxt;
2685 i40e_status ret;
2686
2687 if ((vsi->info.valid_sections &
2688 cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
2689 ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_EMOD_MASK) ==
2690 I40E_AQ_VSI_PVLAN_EMOD_MASK))
2691 return; /* already disabled */
2692
2693 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2694 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
2695 I40E_AQ_VSI_PVLAN_EMOD_NOTHING;
2696
2697 ctxt.seid = vsi->seid;
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07002698 ctxt.info = vsi->info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002699 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2700 if (ret) {
2701 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002702 "update vlan stripping failed, err %s aq_err %s\n",
2703 i40e_stat_str(&vsi->back->hw, ret),
2704 i40e_aq_str(&vsi->back->hw,
2705 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002706 }
2707}
2708
2709/**
2710 * i40e_vlan_rx_register - Setup or shutdown vlan offload
2711 * @netdev: network interface to be adjusted
2712 * @features: netdev features to test if VLAN offload is enabled or not
2713 **/
2714static void i40e_vlan_rx_register(struct net_device *netdev, u32 features)
2715{
2716 struct i40e_netdev_priv *np = netdev_priv(netdev);
2717 struct i40e_vsi *vsi = np->vsi;
2718
2719 if (features & NETIF_F_HW_VLAN_CTAG_RX)
2720 i40e_vlan_stripping_enable(vsi);
2721 else
2722 i40e_vlan_stripping_disable(vsi);
2723}
2724
2725/**
Jacob Keller490a4ad2016-11-11 12:39:29 -08002726 * i40e_add_vlan_all_mac - Add a MAC/VLAN filter for each existing MAC address
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002727 * @vsi: the vsi being configured
2728 * @vid: vlan id to be added (0 = untagged only , -1 = any)
Jacob Keller490a4ad2016-11-11 12:39:29 -08002729 *
2730 * This is a helper function for adding a new MAC/VLAN filter with the
2731 * specified VLAN for each existing MAC address already in the hash table.
2732 * This function does *not* perform any accounting to update filters based on
2733 * VLAN mode.
2734 *
2735 * NOTE: this function expects to be called while under the
2736 * mac_filter_hash_lock
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002737 **/
Jacob Keller9af52f62016-11-11 12:39:30 -08002738int i40e_add_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002739{
Jacob Keller490a4ad2016-11-11 12:39:29 -08002740 struct i40e_mac_filter *f, *add_f;
Jacob Keller278e7d02016-10-05 09:30:37 -07002741 struct hlist_node *h;
2742 int bkt;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002743
Jacob Keller278e7d02016-10-05 09:30:37 -07002744 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Jacob Keller57b341d2016-10-05 09:30:35 -07002745 if (f->state == I40E_FILTER_REMOVE)
2746 continue;
Jacob Keller1bc87e82016-10-05 09:30:31 -07002747 add_f = i40e_add_filter(vsi, f->macaddr, vid);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002748 if (!add_f) {
2749 dev_info(&vsi->back->pdev->dev,
2750 "Could not add vlan filter %d for %pM\n",
2751 vid, f->macaddr);
2752 return -ENOMEM;
2753 }
2754 }
2755
Jacob Keller490a4ad2016-11-11 12:39:29 -08002756 return 0;
2757}
2758
2759/**
2760 * i40e_vsi_add_vlan - Add VSI membership for given VLAN
2761 * @vsi: the VSI being configured
Jacob Kellerf94484b2016-12-07 14:05:34 -08002762 * @vid: VLAN id to be added
Jacob Keller490a4ad2016-11-11 12:39:29 -08002763 **/
Jacob Kellerf94484b2016-12-07 14:05:34 -08002764int i40e_vsi_add_vlan(struct i40e_vsi *vsi, u16 vid)
Jacob Keller490a4ad2016-11-11 12:39:29 -08002765{
Jacob Keller489a3262016-11-11 12:39:31 -08002766 int err;
Jacob Keller490a4ad2016-11-11 12:39:29 -08002767
Jacob Kellerfcf6cfc2017-06-23 04:24:51 -04002768 if (vsi->info.pvid)
Jacob Kellerf94484b2016-12-07 14:05:34 -08002769 return -EINVAL;
2770
Jacob Kellerfcf6cfc2017-06-23 04:24:51 -04002771 /* The network stack will attempt to add VID=0, with the intention to
2772 * receive priority tagged packets with a VLAN of 0. Our HW receives
2773 * these packets by default when configured to receive untagged
2774 * packets, so we don't need to add a filter for this case.
2775 * Additionally, HW interprets adding a VID=0 filter as meaning to
2776 * receive *only* tagged traffic and stops receiving untagged traffic.
2777 * Thus, we do not want to actually add a filter for VID=0
2778 */
2779 if (!vid)
2780 return 0;
2781
Jacob Keller490a4ad2016-11-11 12:39:29 -08002782 /* Locked once because all functions invoked below iterates list*/
2783 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller490a4ad2016-11-11 12:39:29 -08002784 err = i40e_add_vlan_all_mac(vsi, vid);
Jacob Keller278e7d02016-10-05 09:30:37 -07002785 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller489a3262016-11-11 12:39:31 -08002786 if (err)
2787 return err;
Kiran Patil21659032015-09-30 14:09:03 -04002788
Jesse Brandeburg0e4425e2015-11-05 17:01:01 -08002789 /* schedule our worker thread which will take care of
2790 * applying the new filter changes
2791 */
2792 i40e_service_event_schedule(vsi->back);
2793 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002794}
2795
2796/**
Jacob Keller490a4ad2016-11-11 12:39:29 -08002797 * i40e_rm_vlan_all_mac - Remove MAC/VLAN pair for all MAC with the given VLAN
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002798 * @vsi: the vsi being configured
2799 * @vid: vlan id to be removed (0 = untagged only , -1 = any)
Jacob Keller490a4ad2016-11-11 12:39:29 -08002800 *
2801 * This function should be used to remove all VLAN filters which match the
2802 * given VID. It does not schedule the service event and does not take the
2803 * mac_filter_hash_lock so it may be combined with other operations under
2804 * a single invocation of the mac_filter_hash_lock.
2805 *
2806 * NOTE: this function expects to be called while under the
2807 * mac_filter_hash_lock
2808 */
Jacob Keller9af52f62016-11-11 12:39:30 -08002809void i40e_rm_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002810{
Alan Brady84f5ca62016-10-05 09:30:39 -07002811 struct i40e_mac_filter *f;
Jacob Keller278e7d02016-10-05 09:30:37 -07002812 struct hlist_node *h;
Jacob Keller278e7d02016-10-05 09:30:37 -07002813 int bkt;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002814
Jacob Keller278e7d02016-10-05 09:30:37 -07002815 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Jacob Keller290d2552016-10-05 09:30:36 -07002816 if (f->vlan == vid)
2817 __i40e_del_filter(vsi, f);
2818 }
Jacob Keller490a4ad2016-11-11 12:39:29 -08002819}
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002820
Jacob Keller490a4ad2016-11-11 12:39:29 -08002821/**
2822 * i40e_vsi_kill_vlan - Remove VSI membership for given VLAN
2823 * @vsi: the VSI being configured
Jacob Kellerf94484b2016-12-07 14:05:34 -08002824 * @vid: VLAN id to be removed
Jacob Keller490a4ad2016-11-11 12:39:29 -08002825 **/
Jacob Kellerf94484b2016-12-07 14:05:34 -08002826void i40e_vsi_kill_vlan(struct i40e_vsi *vsi, u16 vid)
Jacob Keller490a4ad2016-11-11 12:39:29 -08002827{
Jacob Kellerf94484b2016-12-07 14:05:34 -08002828 if (!vid || vsi->info.pvid)
2829 return;
2830
Jacob Keller490a4ad2016-11-11 12:39:29 -08002831 spin_lock_bh(&vsi->mac_filter_hash_lock);
2832 i40e_rm_vlan_all_mac(vsi, vid);
Jacob Keller278e7d02016-10-05 09:30:37 -07002833 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -04002834
Jesse Brandeburg0e4425e2015-11-05 17:01:01 -08002835 /* schedule our worker thread which will take care of
2836 * applying the new filter changes
2837 */
2838 i40e_service_event_schedule(vsi->back);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002839}
2840
2841/**
2842 * i40e_vlan_rx_add_vid - Add a vlan id filter to HW offload
2843 * @netdev: network interface to be adjusted
2844 * @vid: vlan id to be added
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002845 *
2846 * net_device_ops implementation for adding vlan ids
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002847 **/
2848static int i40e_vlan_rx_add_vid(struct net_device *netdev,
2849 __always_unused __be16 proto, u16 vid)
2850{
2851 struct i40e_netdev_priv *np = netdev_priv(netdev);
2852 struct i40e_vsi *vsi = np->vsi;
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002853 int ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002854
Jacob Keller6a1127852016-10-25 16:08:49 -07002855 if (vid >= VLAN_N_VID)
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002856 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002857
Jacob Kellerfcf6cfc2017-06-23 04:24:51 -04002858 ret = i40e_vsi_add_vlan(vsi, vid);
Jacob Keller6a1127852016-10-25 16:08:49 -07002859 if (!ret)
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002860 set_bit(vid, vsi->active_vlans);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002861
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002862 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002863}
2864
2865/**
2866 * i40e_vlan_rx_kill_vid - Remove a vlan id filter from HW offload
2867 * @netdev: network interface to be adjusted
2868 * @vid: vlan id to be removed
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002869 *
Akeem G Abodunrinfdfd9432014-02-11 08:24:15 +00002870 * net_device_ops implementation for removing vlan ids
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002871 **/
2872static int i40e_vlan_rx_kill_vid(struct net_device *netdev,
2873 __always_unused __be16 proto, u16 vid)
2874{
2875 struct i40e_netdev_priv *np = netdev_priv(netdev);
2876 struct i40e_vsi *vsi = np->vsi;
2877
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002878 /* return code is ignored as there is nothing a user
2879 * can do about failure to remove and a log message was
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002880 * already printed from the other function
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002881 */
2882 i40e_vsi_kill_vlan(vsi, vid);
2883
2884 clear_bit(vid, vsi->active_vlans);
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002885
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002886 return 0;
2887}
2888
2889/**
2890 * i40e_restore_vlan - Reinstate vlans when vsi/netdev comes back up
2891 * @vsi: the vsi being brought back up
2892 **/
2893static void i40e_restore_vlan(struct i40e_vsi *vsi)
2894{
2895 u16 vid;
2896
2897 if (!vsi->netdev)
2898 return;
2899
2900 i40e_vlan_rx_register(vsi->netdev, vsi->netdev->features);
2901
2902 for_each_set_bit(vid, vsi->active_vlans, VLAN_N_VID)
2903 i40e_vlan_rx_add_vid(vsi->netdev, htons(ETH_P_8021Q),
2904 vid);
2905}
2906
2907/**
2908 * i40e_vsi_add_pvid - Add pvid for the VSI
2909 * @vsi: the vsi being adjusted
2910 * @vid: the vlan id to set as a PVID
2911 **/
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002912int i40e_vsi_add_pvid(struct i40e_vsi *vsi, u16 vid)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002913{
2914 struct i40e_vsi_context ctxt;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002915 i40e_status ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002916
2917 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2918 vsi->info.pvid = cpu_to_le16(vid);
Greg Rose6c12fcb2013-11-28 06:39:34 +00002919 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_TAGGED |
2920 I40E_AQ_VSI_PVLAN_INSERT_PVID |
Greg Roseb774c7d2013-11-28 06:39:44 +00002921 I40E_AQ_VSI_PVLAN_EMOD_STR;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002922
2923 ctxt.seid = vsi->seid;
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07002924 ctxt.info = vsi->info;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002925 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2926 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002927 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002928 "add pvid failed, err %s aq_err %s\n",
2929 i40e_stat_str(&vsi->back->hw, ret),
2930 i40e_aq_str(&vsi->back->hw,
2931 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002932 return -ENOENT;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002933 }
2934
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002935 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002936}
2937
2938/**
2939 * i40e_vsi_remove_pvid - Remove the pvid from the VSI
2940 * @vsi: the vsi being adjusted
2941 *
2942 * Just use the vlan_rx_register() service to put it back to normal
2943 **/
2944void i40e_vsi_remove_pvid(struct i40e_vsi *vsi)
2945{
Greg Rose6c12fcb2013-11-28 06:39:34 +00002946 i40e_vlan_stripping_disable(vsi);
2947
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002948 vsi->info.pvid = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002949}
2950
2951/**
2952 * i40e_vsi_setup_tx_resources - Allocate VSI Tx queue resources
2953 * @vsi: ptr to the VSI
2954 *
2955 * If this function returns with an error, then it's possible one or
2956 * more of the rings is populated (while the rest are not). It is the
2957 * callers duty to clean those orphaned rings.
2958 *
2959 * Return 0 on success, negative on failure
2960 **/
2961static int i40e_vsi_setup_tx_resources(struct i40e_vsi *vsi)
2962{
2963 int i, err = 0;
2964
2965 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e152013-09-28 06:00:58 +00002966 err = i40e_setup_tx_descriptors(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002967
Björn Töpel74608d12017-05-24 07:55:35 +02002968 if (!i40e_enabled_xdp_vsi(vsi))
2969 return err;
2970
2971 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
2972 err = i40e_setup_tx_descriptors(vsi->xdp_rings[i]);
2973
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002974 return err;
2975}
2976
2977/**
2978 * i40e_vsi_free_tx_resources - Free Tx resources for VSI queues
2979 * @vsi: ptr to the VSI
2980 *
2981 * Free VSI's transmit software resources
2982 **/
2983static void i40e_vsi_free_tx_resources(struct i40e_vsi *vsi)
2984{
2985 int i;
2986
Björn Töpel74608d12017-05-24 07:55:35 +02002987 if (vsi->tx_rings) {
2988 for (i = 0; i < vsi->num_queue_pairs; i++)
2989 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
2990 i40e_free_tx_resources(vsi->tx_rings[i]);
2991 }
Greg Rose8e9dca52013-12-18 13:45:53 +00002992
Björn Töpel74608d12017-05-24 07:55:35 +02002993 if (vsi->xdp_rings) {
2994 for (i = 0; i < vsi->num_queue_pairs; i++)
2995 if (vsi->xdp_rings[i] && vsi->xdp_rings[i]->desc)
2996 i40e_free_tx_resources(vsi->xdp_rings[i]);
2997 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002998}
2999
3000/**
3001 * i40e_vsi_setup_rx_resources - Allocate VSI queues Rx resources
3002 * @vsi: ptr to the VSI
3003 *
3004 * If this function returns with an error, then it's possible one or
3005 * more of the rings is populated (while the rest are not). It is the
3006 * callers duty to clean those orphaned rings.
3007 *
3008 * Return 0 on success, negative on failure
3009 **/
3010static int i40e_vsi_setup_rx_resources(struct i40e_vsi *vsi)
3011{
3012 int i, err = 0;
3013
3014 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e152013-09-28 06:00:58 +00003015 err = i40e_setup_rx_descriptors(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003016 return err;
3017}
3018
3019/**
3020 * i40e_vsi_free_rx_resources - Free Rx Resources for VSI queues
3021 * @vsi: ptr to the VSI
3022 *
3023 * Free all receive software resources
3024 **/
3025static void i40e_vsi_free_rx_resources(struct i40e_vsi *vsi)
3026{
3027 int i;
3028
Greg Rose8e9dca52013-12-18 13:45:53 +00003029 if (!vsi->rx_rings)
3030 return;
3031
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003032 for (i = 0; i < vsi->num_queue_pairs; i++)
Greg Rose8e9dca52013-12-18 13:45:53 +00003033 if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
Alexander Duyck9f65e152013-09-28 06:00:58 +00003034 i40e_free_rx_resources(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003035}
3036
3037/**
Neerav Parikh3ffa0372014-11-12 00:19:02 +00003038 * i40e_config_xps_tx_ring - Configure XPS for a Tx ring
3039 * @ring: The Tx ring to configure
3040 *
3041 * This enables/disables XPS for a given Tx descriptor ring
3042 * based on the TCs enabled for the VSI that ring belongs to.
3043 **/
3044static void i40e_config_xps_tx_ring(struct i40e_ring *ring)
3045{
Jacob Kellerbe664cb2017-08-29 05:32:31 -04003046 int cpu;
Neerav Parikh3ffa0372014-11-12 00:19:02 +00003047
Amritha Nambiar8f88b302017-09-07 04:00:17 -07003048 if (!ring->q_vector || !ring->netdev || ring->ch)
Jesse Brandeburg9a660ee2015-02-26 16:13:22 +00003049 return;
3050
Jacob Keller6f853d42017-09-07 08:05:53 -04003051 /* We only initialize XPS once, so as not to overwrite user settings */
3052 if (test_and_set_bit(__I40E_TX_XPS_INIT_DONE, ring->state))
3053 return;
Jesse Brandeburg0e4425e2015-11-05 17:01:01 -08003054
Jacob Keller6f853d42017-09-07 08:05:53 -04003055 cpu = cpumask_local_spread(ring->q_vector->v_idx, -1);
3056 netif_set_xps_queue(ring->netdev, get_cpu_mask(cpu),
3057 ring->queue_index);
Neerav Parikh3ffa0372014-11-12 00:19:02 +00003058}
3059
3060/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003061 * i40e_configure_tx_ring - Configure a transmit ring context and rest
3062 * @ring: The Tx ring to configure
3063 *
3064 * Configure the Tx descriptor ring in the HMC context.
3065 **/
3066static int i40e_configure_tx_ring(struct i40e_ring *ring)
3067{
3068 struct i40e_vsi *vsi = ring->vsi;
3069 u16 pf_q = vsi->base_queue + ring->queue_index;
3070 struct i40e_hw *hw = &vsi->back->hw;
3071 struct i40e_hmc_obj_txq tx_ctx;
3072 i40e_status err = 0;
3073 u32 qtx_ctl = 0;
3074
3075 /* some ATR related tx ring init */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08003076 if (vsi->back->flags & I40E_FLAG_FD_ATR_ENABLED) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003077 ring->atr_sample_rate = vsi->back->atr_sample_rate;
3078 ring->atr_count = 0;
3079 } else {
3080 ring->atr_sample_rate = 0;
3081 }
3082
Neerav Parikh3ffa0372014-11-12 00:19:02 +00003083 /* configure XPS */
3084 i40e_config_xps_tx_ring(ring);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003085
3086 /* clear the context structure first */
3087 memset(&tx_ctx, 0, sizeof(tx_ctx));
3088
3089 tx_ctx.new_context = 1;
3090 tx_ctx.base = (ring->dma / 128);
3091 tx_ctx.qlen = ring->count;
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08003092 tx_ctx.fd_ena = !!(vsi->back->flags & (I40E_FLAG_FD_SB_ENABLED |
3093 I40E_FLAG_FD_ATR_ENABLED));
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003094 tx_ctx.timesync_ena = !!(vsi->back->flags & I40E_FLAG_PTP);
Jesse Brandeburg1943d8b2014-02-14 02:14:40 +00003095 /* FDIR VSI tx ring can still use RS bit and writebacks */
3096 if (vsi->type != I40E_VSI_FDIR)
3097 tx_ctx.head_wb_ena = 1;
3098 tx_ctx.head_wb_addr = ring->dma +
3099 (ring->count * sizeof(struct i40e_tx_desc));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003100
3101 /* As part of VSI creation/update, FW allocates certain
3102 * Tx arbitration queue sets for each TC enabled for
3103 * the VSI. The FW returns the handles to these queue
3104 * sets as part of the response buffer to Add VSI,
3105 * Update VSI, etc. AQ commands. It is expected that
3106 * these queue set handles be associated with the Tx
3107 * queues by the driver as part of the TX queue context
3108 * initialization. This has to be done regardless of
3109 * DCB as by default everything is mapped to TC0.
3110 */
Amritha Nambiar8f88b302017-09-07 04:00:17 -07003111
3112 if (ring->ch)
3113 tx_ctx.rdylist =
3114 le16_to_cpu(ring->ch->info.qs_handle[ring->dcb_tc]);
3115
3116 else
3117 tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[ring->dcb_tc]);
3118
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003119 tx_ctx.rdylist_act = 0;
3120
3121 /* clear the context in the HMC */
3122 err = i40e_clear_lan_tx_queue_context(hw, pf_q);
3123 if (err) {
3124 dev_info(&vsi->back->pdev->dev,
3125 "Failed to clear LAN Tx queue context on Tx ring %d (pf_q %d), error: %d\n",
3126 ring->queue_index, pf_q, err);
3127 return -ENOMEM;
3128 }
3129
3130 /* set the context in the HMC */
3131 err = i40e_set_lan_tx_queue_context(hw, pf_q, &tx_ctx);
3132 if (err) {
3133 dev_info(&vsi->back->pdev->dev,
3134 "Failed to set LAN Tx queue context on Tx ring %d (pf_q %d, error: %d\n",
3135 ring->queue_index, pf_q, err);
3136 return -ENOMEM;
3137 }
3138
3139 /* Now associate this queue with this PCI function */
Amritha Nambiar8f88b302017-09-07 04:00:17 -07003140 if (ring->ch) {
3141 if (ring->ch->type == I40E_VSI_VMDQ2)
3142 qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
3143 else
3144 return -EINVAL;
3145
3146 qtx_ctl |= (ring->ch->vsi_number <<
3147 I40E_QTX_CTL_VFVM_INDX_SHIFT) &
3148 I40E_QTX_CTL_VFVM_INDX_MASK;
Mitch Williams7a28d882014-10-17 03:14:52 +00003149 } else {
Amritha Nambiar8f88b302017-09-07 04:00:17 -07003150 if (vsi->type == I40E_VSI_VMDQ2) {
3151 qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
3152 qtx_ctl |= ((vsi->id) << I40E_QTX_CTL_VFVM_INDX_SHIFT) &
3153 I40E_QTX_CTL_VFVM_INDX_MASK;
3154 } else {
3155 qtx_ctl = I40E_QTX_CTL_PF_QUEUE;
3156 }
Mitch Williams7a28d882014-10-17 03:14:52 +00003157 }
3158
Shannon Nelson13fd9772013-09-28 07:14:19 +00003159 qtx_ctl |= ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT) &
3160 I40E_QTX_CTL_PF_INDX_MASK);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003161 wr32(hw, I40E_QTX_CTL(pf_q), qtx_ctl);
3162 i40e_flush(hw);
3163
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003164 /* cache tail off for easier writes later */
3165 ring->tail = hw->hw_addr + I40E_QTX_TAIL(pf_q);
3166
3167 return 0;
3168}
3169
3170/**
3171 * i40e_configure_rx_ring - Configure a receive ring context
3172 * @ring: The Rx ring to configure
3173 *
3174 * Configure the Rx descriptor ring in the HMC context.
3175 **/
3176static int i40e_configure_rx_ring(struct i40e_ring *ring)
3177{
3178 struct i40e_vsi *vsi = ring->vsi;
3179 u32 chain_len = vsi->back->hw.func_caps.rx_buf_chain_len;
3180 u16 pf_q = vsi->base_queue + ring->queue_index;
3181 struct i40e_hw *hw = &vsi->back->hw;
3182 struct i40e_hmc_obj_rxq rx_ctx;
3183 i40e_status err = 0;
3184
Jesse Brandeburgbd6cd4e2017-08-29 05:32:35 -04003185 bitmap_zero(ring->state, __I40E_RING_STATE_NBITS);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003186
3187 /* clear the context structure first */
3188 memset(&rx_ctx, 0, sizeof(rx_ctx));
3189
3190 ring->rx_buf_len = vsi->rx_buf_len;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003191
Alexander Duyckdab86af2017-03-14 10:15:27 -07003192 rx_ctx.dbuff = DIV_ROUND_UP(ring->rx_buf_len,
3193 BIT_ULL(I40E_RXQ_CTX_DBUFF_SHIFT));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003194
3195 rx_ctx.base = (ring->dma / 128);
3196 rx_ctx.qlen = ring->count;
3197
Jesse Brandeburgbec60fc2016-04-18 11:33:47 -07003198 /* use 32 byte descriptors */
3199 rx_ctx.dsize = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003200
Jesse Brandeburgbec60fc2016-04-18 11:33:47 -07003201 /* descriptor type is always zero
3202 * rx_ctx.dtype = 0;
3203 */
Jesse Brandeburgb32bfa172016-04-18 11:33:42 -07003204 rx_ctx.hsplit_0 = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003205
Jesse Brandeburgb32bfa172016-04-18 11:33:42 -07003206 rx_ctx.rxmax = min_t(u16, vsi->max_frame, chain_len * ring->rx_buf_len);
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00003207 if (hw->revision_id == 0)
3208 rx_ctx.lrxqthresh = 0;
3209 else
Jacob Keller7362be92017-09-07 08:05:50 -04003210 rx_ctx.lrxqthresh = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003211 rx_ctx.crcstrip = 1;
3212 rx_ctx.l2tsel = 1;
Jesse Brandeburgc4bbac32015-09-28 11:21:48 -07003213 /* this controls whether VLAN is stripped from inner headers */
3214 rx_ctx.showiv = 0;
Catherine Sullivanacb36762014-03-06 09:02:30 +00003215 /* set the prefena field to 1 because the manual says to */
3216 rx_ctx.prefena = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003217
3218 /* clear the context in the HMC */
3219 err = i40e_clear_lan_rx_queue_context(hw, pf_q);
3220 if (err) {
3221 dev_info(&vsi->back->pdev->dev,
3222 "Failed to clear LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
3223 ring->queue_index, pf_q, err);
3224 return -ENOMEM;
3225 }
3226
3227 /* set the context in the HMC */
3228 err = i40e_set_lan_rx_queue_context(hw, pf_q, &rx_ctx);
3229 if (err) {
3230 dev_info(&vsi->back->pdev->dev,
3231 "Failed to set LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
3232 ring->queue_index, pf_q, err);
3233 return -ENOMEM;
3234 }
3235
Alexander Duyckca9ec082017-04-05 07:51:02 -04003236 /* configure Rx buffer alignment */
3237 if (!vsi->netdev || (vsi->back->flags & I40E_FLAG_LEGACY_RX))
3238 clear_ring_build_skb_enabled(ring);
3239 else
3240 set_ring_build_skb_enabled(ring);
3241
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003242 /* cache tail for quicker writes, and clear the reg before use */
3243 ring->tail = hw->hw_addr + I40E_QRX_TAIL(pf_q);
3244 writel(0, ring->tail);
3245
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07003246 i40e_alloc_rx_buffers(ring, I40E_DESC_UNUSED(ring));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003247
3248 return 0;
3249}
3250
3251/**
3252 * i40e_vsi_configure_tx - Configure the VSI for Tx
3253 * @vsi: VSI structure describing this set of rings and resources
3254 *
3255 * Configure the Tx VSI for operation.
3256 **/
3257static int i40e_vsi_configure_tx(struct i40e_vsi *vsi)
3258{
3259 int err = 0;
3260 u16 i;
3261
Alexander Duyck9f65e152013-09-28 06:00:58 +00003262 for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
3263 err = i40e_configure_tx_ring(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003264
Björn Töpel74608d12017-05-24 07:55:35 +02003265 if (!i40e_enabled_xdp_vsi(vsi))
3266 return err;
3267
3268 for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
3269 err = i40e_configure_tx_ring(vsi->xdp_rings[i]);
3270
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003271 return err;
3272}
3273
3274/**
3275 * i40e_vsi_configure_rx - Configure the VSI for Rx
3276 * @vsi: the VSI being configured
3277 *
3278 * Configure the Rx VSI for operation.
3279 **/
3280static int i40e_vsi_configure_rx(struct i40e_vsi *vsi)
3281{
3282 int err = 0;
3283 u16 i;
3284
Alexander Duyckdab86af2017-03-14 10:15:27 -07003285 if (!vsi->netdev || (vsi->back->flags & I40E_FLAG_LEGACY_RX)) {
3286 vsi->max_frame = I40E_MAX_RXBUFFER;
3287 vsi->rx_buf_len = I40E_RXBUFFER_2048;
3288#if (PAGE_SIZE < 8192)
Alexander Duyckca9ec082017-04-05 07:51:02 -04003289 } else if (!I40E_2K_TOO_SMALL_WITH_PADDING &&
3290 (vsi->netdev->mtu <= ETH_DATA_LEN)) {
Alexander Duyckdab86af2017-03-14 10:15:27 -07003291 vsi->max_frame = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
3292 vsi->rx_buf_len = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
3293#endif
3294 } else {
3295 vsi->max_frame = I40E_MAX_RXBUFFER;
Alexander Duyck98efd692017-04-05 07:51:01 -04003296 vsi->rx_buf_len = (PAGE_SIZE < 8192) ? I40E_RXBUFFER_3072 :
3297 I40E_RXBUFFER_2048;
Alexander Duyckdab86af2017-03-14 10:15:27 -07003298 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003299
3300 /* set up individual rings */
3301 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e152013-09-28 06:00:58 +00003302 err = i40e_configure_rx_ring(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003303
3304 return err;
3305}
3306
3307/**
3308 * i40e_vsi_config_dcb_rings - Update rings to reflect DCB TC
3309 * @vsi: ptr to the VSI
3310 **/
3311static void i40e_vsi_config_dcb_rings(struct i40e_vsi *vsi)
3312{
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +00003313 struct i40e_ring *tx_ring, *rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003314 u16 qoffset, qcount;
3315 int i, n;
3316
Parikh, Neeravcd238a32015-02-21 06:43:37 +00003317 if (!(vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
3318 /* Reset the TC information */
3319 for (i = 0; i < vsi->num_queue_pairs; i++) {
3320 rx_ring = vsi->rx_rings[i];
3321 tx_ring = vsi->tx_rings[i];
3322 rx_ring->dcb_tc = 0;
3323 tx_ring->dcb_tc = 0;
3324 }
Amritha Nambiara9ce82f2017-09-07 04:00:22 -07003325 return;
Parikh, Neeravcd238a32015-02-21 06:43:37 +00003326 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003327
3328 for (n = 0; n < I40E_MAX_TRAFFIC_CLASS; n++) {
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04003329 if (!(vsi->tc_config.enabled_tc & BIT_ULL(n)))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003330 continue;
3331
3332 qoffset = vsi->tc_config.tc_info[n].qoffset;
3333 qcount = vsi->tc_config.tc_info[n].qcount;
3334 for (i = qoffset; i < (qoffset + qcount); i++) {
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +00003335 rx_ring = vsi->rx_rings[i];
3336 tx_ring = vsi->tx_rings[i];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003337 rx_ring->dcb_tc = n;
3338 tx_ring->dcb_tc = n;
3339 }
3340 }
3341}
3342
3343/**
3344 * i40e_set_vsi_rx_mode - Call set_rx_mode on a VSI
3345 * @vsi: ptr to the VSI
3346 **/
3347static void i40e_set_vsi_rx_mode(struct i40e_vsi *vsi)
3348{
3349 if (vsi->netdev)
3350 i40e_set_rx_mode(vsi->netdev);
3351}
3352
3353/**
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00003354 * i40e_fdir_filter_restore - Restore the Sideband Flow Director filters
3355 * @vsi: Pointer to the targeted VSI
3356 *
3357 * This function replays the hlist on the hw where all the SB Flow Director
3358 * filters were saved.
3359 **/
3360static void i40e_fdir_filter_restore(struct i40e_vsi *vsi)
3361{
3362 struct i40e_fdir_filter *filter;
3363 struct i40e_pf *pf = vsi->back;
3364 struct hlist_node *node;
3365
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00003366 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
3367 return;
3368
Jacob Keller6d069422017-02-06 14:38:44 -08003369 /* Reset FDir counters as we're replaying all existing filters */
Jacob Keller097dbf52017-02-06 14:38:46 -08003370 pf->fd_tcp4_filter_cnt = 0;
3371 pf->fd_udp4_filter_cnt = 0;
Jacob Kellerf223c872017-02-06 14:38:51 -08003372 pf->fd_sctp4_filter_cnt = 0;
Jacob Keller097dbf52017-02-06 14:38:46 -08003373 pf->fd_ip4_filter_cnt = 0;
Jacob Keller6d069422017-02-06 14:38:44 -08003374
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00003375 hlist_for_each_entry_safe(filter, node,
3376 &pf->fdir_filter_list, fdir_node) {
3377 i40e_add_del_fdir(vsi, filter, true);
3378 }
3379}
3380
3381/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003382 * i40e_vsi_configure - Set up the VSI for action
3383 * @vsi: the VSI being configured
3384 **/
3385static int i40e_vsi_configure(struct i40e_vsi *vsi)
3386{
3387 int err;
3388
3389 i40e_set_vsi_rx_mode(vsi);
3390 i40e_restore_vlan(vsi);
3391 i40e_vsi_config_dcb_rings(vsi);
3392 err = i40e_vsi_configure_tx(vsi);
3393 if (!err)
3394 err = i40e_vsi_configure_rx(vsi);
3395
3396 return err;
3397}
3398
3399/**
3400 * i40e_vsi_configure_msix - MSIX mode Interrupt Config in the HW
3401 * @vsi: the VSI being configured
3402 **/
3403static void i40e_vsi_configure_msix(struct i40e_vsi *vsi)
3404{
Björn Töpel74608d12017-05-24 07:55:35 +02003405 bool has_xdp = i40e_enabled_xdp_vsi(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003406 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003407 struct i40e_hw *hw = &pf->hw;
3408 u16 vector;
3409 int i, q;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003410 u32 qp;
3411
3412 /* The interrupt indexing is offset by 1 in the PFINT_ITRn
3413 * and PFINT_LNKLSTn registers, e.g.:
3414 * PFINT_ITRn[0..n-1] gets msix-1..msix-n (qpair interrupts)
3415 */
3416 qp = vsi->base_queue;
3417 vector = vsi->base_vector;
Alexander Duyck493fb302013-09-28 07:01:44 +00003418 for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
Jesse Brandeburgac26fc12015-09-28 14:12:37 -04003419 struct i40e_q_vector *q_vector = vsi->q_vectors[i];
3420
Jesse Brandeburgee2319c2015-09-28 14:16:54 -04003421 q_vector->itr_countdown = ITR_COUNTDOWN_START;
Kan Lianga75e8002016-02-19 09:24:04 -05003422 q_vector->rx.itr = ITR_TO_REG(vsi->rx_rings[i]->rx_itr_setting);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003423 q_vector->rx.latency_range = I40E_LOW_LATENCY;
3424 wr32(hw, I40E_PFINT_ITRN(I40E_RX_ITR, vector - 1),
3425 q_vector->rx.itr);
Kan Lianga75e8002016-02-19 09:24:04 -05003426 q_vector->tx.itr = ITR_TO_REG(vsi->tx_rings[i]->tx_itr_setting);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003427 q_vector->tx.latency_range = I40E_LOW_LATENCY;
3428 wr32(hw, I40E_PFINT_ITRN(I40E_TX_ITR, vector - 1),
3429 q_vector->tx.itr);
Jesse Brandeburgac26fc12015-09-28 14:12:37 -04003430 wr32(hw, I40E_PFINT_RATEN(vector - 1),
Alan Brady1c0e6a32016-11-28 16:06:02 -08003431 i40e_intrl_usec_to_reg(vsi->int_rate_limit));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003432
3433 /* Linked list for the queuepairs assigned to this vector */
3434 wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), qp);
3435 for (q = 0; q < q_vector->num_ringpairs; q++) {
Björn Töpel74608d12017-05-24 07:55:35 +02003436 u32 nextqp = has_xdp ? qp + vsi->alloc_queue_pairs : qp;
Jesse Brandeburgac26fc12015-09-28 14:12:37 -04003437 u32 val;
3438
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003439 val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
Björn Töpel74608d12017-05-24 07:55:35 +02003440 (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
3441 (vector << I40E_QINT_RQCTL_MSIX_INDX_SHIFT) |
3442 (nextqp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT) |
3443 (I40E_QUEUE_TYPE_TX <<
3444 I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003445
3446 wr32(hw, I40E_QINT_RQCTL(qp), val);
3447
Björn Töpel74608d12017-05-24 07:55:35 +02003448 if (has_xdp) {
3449 val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3450 (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3451 (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
3452 (qp << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) |
3453 (I40E_QUEUE_TYPE_TX <<
3454 I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3455
3456 wr32(hw, I40E_QINT_TQCTL(nextqp), val);
3457 }
3458
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003459 val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
Björn Töpel74608d12017-05-24 07:55:35 +02003460 (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3461 (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
3462 ((qp + 1) << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) |
3463 (I40E_QUEUE_TYPE_RX <<
3464 I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003465
3466 /* Terminate the linked list */
3467 if (q == (q_vector->num_ringpairs - 1))
Björn Töpel74608d12017-05-24 07:55:35 +02003468 val |= (I40E_QUEUE_END_OF_LIST <<
3469 I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003470
3471 wr32(hw, I40E_QINT_TQCTL(qp), val);
3472 qp++;
3473 }
3474 }
3475
3476 i40e_flush(hw);
3477}
3478
3479/**
3480 * i40e_enable_misc_int_causes - enable the non-queue interrupts
3481 * @hw: ptr to the hardware info
3482 **/
Jacob Kellerab437b52014-12-14 01:55:08 +00003483static void i40e_enable_misc_int_causes(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003484{
Jacob Kellerab437b52014-12-14 01:55:08 +00003485 struct i40e_hw *hw = &pf->hw;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003486 u32 val;
3487
3488 /* clear things first */
3489 wr32(hw, I40E_PFINT_ICR0_ENA, 0); /* disable all */
3490 rd32(hw, I40E_PFINT_ICR0); /* read to clear */
3491
3492 val = I40E_PFINT_ICR0_ENA_ECC_ERR_MASK |
3493 I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK |
3494 I40E_PFINT_ICR0_ENA_GRST_MASK |
3495 I40E_PFINT_ICR0_ENA_PCI_EXCEPTION_MASK |
3496 I40E_PFINT_ICR0_ENA_GPIO_MASK |
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003497 I40E_PFINT_ICR0_ENA_HMC_ERR_MASK |
3498 I40E_PFINT_ICR0_ENA_VFLR_MASK |
3499 I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
3500
Anjali Singhai Jain0d8e1432015-06-05 12:20:32 -04003501 if (pf->flags & I40E_FLAG_IWARP_ENABLED)
3502 val |= I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
3503
Jacob Kellerab437b52014-12-14 01:55:08 +00003504 if (pf->flags & I40E_FLAG_PTP)
3505 val |= I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
3506
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003507 wr32(hw, I40E_PFINT_ICR0_ENA, val);
3508
3509 /* SW_ITR_IDX = 0, but don't change INTENA */
Anjali Singhai Jain84ed40e2013-11-26 10:49:32 +00003510 wr32(hw, I40E_PFINT_DYN_CTL0, I40E_PFINT_DYN_CTL0_SW_ITR_INDX_MASK |
3511 I40E_PFINT_DYN_CTL0_INTENA_MSK_MASK);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003512
3513 /* OTHER_ITR_IDX = 0 */
3514 wr32(hw, I40E_PFINT_STAT_CTL0, 0);
3515}
3516
3517/**
3518 * i40e_configure_msi_and_legacy - Legacy mode interrupt config in the HW
3519 * @vsi: the VSI being configured
3520 **/
3521static void i40e_configure_msi_and_legacy(struct i40e_vsi *vsi)
3522{
Björn Töpel74608d12017-05-24 07:55:35 +02003523 u32 nextqp = i40e_enabled_xdp_vsi(vsi) ? vsi->alloc_queue_pairs : 0;
Alexander Duyck493fb302013-09-28 07:01:44 +00003524 struct i40e_q_vector *q_vector = vsi->q_vectors[0];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003525 struct i40e_pf *pf = vsi->back;
3526 struct i40e_hw *hw = &pf->hw;
3527 u32 val;
3528
3529 /* set the ITR configuration */
Jesse Brandeburgee2319c2015-09-28 14:16:54 -04003530 q_vector->itr_countdown = ITR_COUNTDOWN_START;
Kan Lianga75e8002016-02-19 09:24:04 -05003531 q_vector->rx.itr = ITR_TO_REG(vsi->rx_rings[0]->rx_itr_setting);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003532 q_vector->rx.latency_range = I40E_LOW_LATENCY;
3533 wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), q_vector->rx.itr);
Kan Lianga75e8002016-02-19 09:24:04 -05003534 q_vector->tx.itr = ITR_TO_REG(vsi->tx_rings[0]->tx_itr_setting);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003535 q_vector->tx.latency_range = I40E_LOW_LATENCY;
3536 wr32(hw, I40E_PFINT_ITR0(I40E_TX_ITR), q_vector->tx.itr);
3537
Jacob Kellerab437b52014-12-14 01:55:08 +00003538 i40e_enable_misc_int_causes(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003539
3540 /* FIRSTQ_INDX = 0, FIRSTQ_TYPE = 0 (rx) */
3541 wr32(hw, I40E_PFINT_LNKLST0, 0);
3542
Jesse Brandeburgf29eaa32014-02-11 08:24:12 +00003543 /* Associate the queue pair to the vector and enable the queue int */
Björn Töpel74608d12017-05-24 07:55:35 +02003544 val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3545 (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
3546 (nextqp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT)|
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003547 (I40E_QUEUE_TYPE_TX << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3548
3549 wr32(hw, I40E_QINT_RQCTL(0), val);
3550
Björn Töpel74608d12017-05-24 07:55:35 +02003551 if (i40e_enabled_xdp_vsi(vsi)) {
3552 val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3553 (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT)|
3554 (I40E_QUEUE_TYPE_TX
3555 << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3556
3557 wr32(hw, I40E_QINT_TQCTL(nextqp), val);
3558 }
3559
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003560 val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3561 (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3562 (I40E_QUEUE_END_OF_LIST << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
3563
3564 wr32(hw, I40E_QINT_TQCTL(0), val);
3565 i40e_flush(hw);
3566}
3567
3568/**
Mitch Williams2ef28cf2013-11-28 06:39:32 +00003569 * i40e_irq_dynamic_disable_icr0 - Disable default interrupt generation for icr0
3570 * @pf: board private structure
3571 **/
3572void i40e_irq_dynamic_disable_icr0(struct i40e_pf *pf)
3573{
3574 struct i40e_hw *hw = &pf->hw;
3575
3576 wr32(hw, I40E_PFINT_DYN_CTL0,
3577 I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
3578 i40e_flush(hw);
3579}
3580
3581/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003582 * i40e_irq_dynamic_enable_icr0 - Enable default interrupt generation for icr0
3583 * @pf: board private structure
3584 **/
Jacob Kellerdbadbbe2017-09-07 08:05:49 -04003585void i40e_irq_dynamic_enable_icr0(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003586{
3587 struct i40e_hw *hw = &pf->hw;
3588 u32 val;
3589
3590 val = I40E_PFINT_DYN_CTL0_INTENA_MASK |
Jacob Kellerdbadbbe2017-09-07 08:05:49 -04003591 I40E_PFINT_DYN_CTL0_CLEARPBA_MASK |
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003592 (I40E_ITR_NONE << I40E_PFINT_DYN_CTL0_ITR_INDX_SHIFT);
3593
3594 wr32(hw, I40E_PFINT_DYN_CTL0, val);
3595 i40e_flush(hw);
3596}
3597
3598/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003599 * i40e_msix_clean_rings - MSIX mode Interrupt Handler
3600 * @irq: interrupt number
3601 * @data: pointer to a q_vector
3602 **/
3603static irqreturn_t i40e_msix_clean_rings(int irq, void *data)
3604{
3605 struct i40e_q_vector *q_vector = data;
3606
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003607 if (!q_vector->tx.ring && !q_vector->rx.ring)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003608 return IRQ_HANDLED;
3609
Alexander Duyck5d3465a2015-09-29 15:19:50 -07003610 napi_schedule_irqoff(&q_vector->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003611
3612 return IRQ_HANDLED;
3613}
3614
3615/**
Alan Brady96db7762016-09-14 16:24:38 -07003616 * i40e_irq_affinity_notify - Callback for affinity changes
3617 * @notify: context as to what irq was changed
3618 * @mask: the new affinity mask
3619 *
3620 * This is a callback function used by the irq_set_affinity_notifier function
3621 * so that we may register to receive changes to the irq affinity masks.
3622 **/
3623static void i40e_irq_affinity_notify(struct irq_affinity_notify *notify,
3624 const cpumask_t *mask)
3625{
3626 struct i40e_q_vector *q_vector =
3627 container_of(notify, struct i40e_q_vector, affinity_notify);
3628
Jacob Keller7e4d01e2017-07-12 05:46:05 -04003629 cpumask_copy(&q_vector->affinity_mask, mask);
Alan Brady96db7762016-09-14 16:24:38 -07003630}
3631
3632/**
3633 * i40e_irq_affinity_release - Callback for affinity notifier release
3634 * @ref: internal core kernel usage
3635 *
3636 * This is a callback function used by the irq_set_affinity_notifier function
3637 * to inform the current notification subscriber that they will no longer
3638 * receive notifications.
3639 **/
3640static void i40e_irq_affinity_release(struct kref *ref) {}
3641
3642/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003643 * i40e_vsi_request_irq_msix - Initialize MSI-X interrupts
3644 * @vsi: the VSI being configured
3645 * @basename: name for the vector
3646 *
3647 * Allocates MSI-X vectors and requests interrupts from the kernel.
3648 **/
3649static int i40e_vsi_request_irq_msix(struct i40e_vsi *vsi, char *basename)
3650{
3651 int q_vectors = vsi->num_q_vectors;
3652 struct i40e_pf *pf = vsi->back;
3653 int base = vsi->base_vector;
3654 int rx_int_idx = 0;
3655 int tx_int_idx = 0;
3656 int vector, err;
Alan Brady96db7762016-09-14 16:24:38 -07003657 int irq_num;
Jacob Kellerbe664cb2017-08-29 05:32:31 -04003658 int cpu;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003659
3660 for (vector = 0; vector < q_vectors; vector++) {
Alexander Duyck493fb302013-09-28 07:01:44 +00003661 struct i40e_q_vector *q_vector = vsi->q_vectors[vector];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003662
Alan Brady96db7762016-09-14 16:24:38 -07003663 irq_num = pf->msix_entries[base + vector].vector;
3664
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003665 if (q_vector->tx.ring && q_vector->rx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003666 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3667 "%s-%s-%d", basename, "TxRx", rx_int_idx++);
3668 tx_int_idx++;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003669 } else if (q_vector->rx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003670 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3671 "%s-%s-%d", basename, "rx", rx_int_idx++);
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003672 } else if (q_vector->tx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003673 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3674 "%s-%s-%d", basename, "tx", tx_int_idx++);
3675 } else {
3676 /* skip this unused q_vector */
3677 continue;
3678 }
Alan Brady96db7762016-09-14 16:24:38 -07003679 err = request_irq(irq_num,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003680 vsi->irq_handler,
3681 0,
3682 q_vector->name,
3683 q_vector);
3684 if (err) {
3685 dev_info(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04003686 "MSIX request_irq failed, error: %d\n", err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003687 goto free_queue_irqs;
3688 }
Alan Brady96db7762016-09-14 16:24:38 -07003689
3690 /* register for affinity change notifications */
3691 q_vector->affinity_notify.notify = i40e_irq_affinity_notify;
3692 q_vector->affinity_notify.release = i40e_irq_affinity_release;
3693 irq_set_affinity_notifier(irq_num, &q_vector->affinity_notify);
Jacob Kellerbe664cb2017-08-29 05:32:31 -04003694 /* Spread affinity hints out across online CPUs.
3695 *
3696 * get_cpu_mask returns a static constant mask with
3697 * a permanent lifetime so it's ok to pass to
3698 * irq_set_affinity_hint without making a copy.
Jacob Keller759dc4a2017-07-14 09:10:10 -04003699 */
Jacob Kellerbe664cb2017-08-29 05:32:31 -04003700 cpu = cpumask_local_spread(q_vector->v_idx, -1);
3701 irq_set_affinity_hint(irq_num, get_cpu_mask(cpu));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003702 }
3703
Shannon Nelson63741842014-04-23 04:50:16 +00003704 vsi->irqs_ready = true;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003705 return 0;
3706
3707free_queue_irqs:
3708 while (vector) {
3709 vector--;
Alan Brady96db7762016-09-14 16:24:38 -07003710 irq_num = pf->msix_entries[base + vector].vector;
3711 irq_set_affinity_notifier(irq_num, NULL);
3712 irq_set_affinity_hint(irq_num, NULL);
3713 free_irq(irq_num, &vsi->q_vectors[vector]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003714 }
3715 return err;
3716}
3717
3718/**
3719 * i40e_vsi_disable_irq - Mask off queue interrupt generation on the VSI
3720 * @vsi: the VSI being un-configured
3721 **/
3722static void i40e_vsi_disable_irq(struct i40e_vsi *vsi)
3723{
3724 struct i40e_pf *pf = vsi->back;
3725 struct i40e_hw *hw = &pf->hw;
3726 int base = vsi->base_vector;
3727 int i;
3728
Shannon Nelson2e5c26ea2017-06-07 05:43:11 -04003729 /* disable interrupt causation from each queue */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003730 for (i = 0; i < vsi->num_queue_pairs; i++) {
Shannon Nelson2e5c26ea2017-06-07 05:43:11 -04003731 u32 val;
3732
3733 val = rd32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx));
3734 val &= ~I40E_QINT_TQCTL_CAUSE_ENA_MASK;
3735 wr32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx), val);
3736
3737 val = rd32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx));
3738 val &= ~I40E_QINT_RQCTL_CAUSE_ENA_MASK;
3739 wr32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx), val);
3740
Björn Töpel74608d12017-05-24 07:55:35 +02003741 if (!i40e_enabled_xdp_vsi(vsi))
3742 continue;
3743 wr32(hw, I40E_QINT_TQCTL(vsi->xdp_rings[i]->reg_idx), 0);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003744 }
3745
Shannon Nelson2e5c26ea2017-06-07 05:43:11 -04003746 /* disable each interrupt */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003747 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3748 for (i = vsi->base_vector;
3749 i < (vsi->num_q_vectors + vsi->base_vector); i++)
3750 wr32(hw, I40E_PFINT_DYN_CTLN(i - 1), 0);
3751
3752 i40e_flush(hw);
3753 for (i = 0; i < vsi->num_q_vectors; i++)
3754 synchronize_irq(pf->msix_entries[i + base].vector);
3755 } else {
3756 /* Legacy and MSI mode - this stops all interrupt handling */
3757 wr32(hw, I40E_PFINT_ICR0_ENA, 0);
3758 wr32(hw, I40E_PFINT_DYN_CTL0, 0);
3759 i40e_flush(hw);
3760 synchronize_irq(pf->pdev->irq);
3761 }
3762}
3763
3764/**
3765 * i40e_vsi_enable_irq - Enable IRQ for the given VSI
3766 * @vsi: the VSI being configured
3767 **/
3768static int i40e_vsi_enable_irq(struct i40e_vsi *vsi)
3769{
3770 struct i40e_pf *pf = vsi->back;
3771 int i;
3772
3773 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
Jesse Brandeburg78455482015-07-23 16:54:41 -04003774 for (i = 0; i < vsi->num_q_vectors; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003775 i40e_irq_dynamic_enable(vsi, i);
3776 } else {
Jacob Kellerdbadbbe2017-09-07 08:05:49 -04003777 i40e_irq_dynamic_enable_icr0(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003778 }
3779
Jesse Brandeburg1022cb62013-09-28 07:13:08 +00003780 i40e_flush(&pf->hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003781 return 0;
3782}
3783
3784/**
Jacob Kellerc17401a2017-07-14 09:27:02 -04003785 * i40e_free_misc_vector - Free the vector that handles non-queue events
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003786 * @pf: board private structure
3787 **/
Jacob Kellerc17401a2017-07-14 09:27:02 -04003788static void i40e_free_misc_vector(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003789{
3790 /* Disable ICR 0 */
3791 wr32(&pf->hw, I40E_PFINT_ICR0_ENA, 0);
3792 i40e_flush(&pf->hw);
Jacob Kellerc17401a2017-07-14 09:27:02 -04003793
3794 if (pf->flags & I40E_FLAG_MSIX_ENABLED && pf->msix_entries) {
3795 synchronize_irq(pf->msix_entries[0].vector);
3796 free_irq(pf->msix_entries[0].vector, pf);
3797 clear_bit(__I40E_MISC_IRQ_REQUESTED, pf->state);
3798 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003799}
3800
3801/**
3802 * i40e_intr - MSI/Legacy and non-queue interrupt handler
3803 * @irq: interrupt number
3804 * @data: pointer to a q_vector
3805 *
3806 * This is the handler used for all MSI/Legacy interrupts, and deals
3807 * with both queue and non-queue interrupts. This is also used in
3808 * MSIX mode to handle the non-queue interrupts.
3809 **/
3810static irqreturn_t i40e_intr(int irq, void *data)
3811{
3812 struct i40e_pf *pf = (struct i40e_pf *)data;
3813 struct i40e_hw *hw = &pf->hw;
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003814 irqreturn_t ret = IRQ_NONE;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003815 u32 icr0, icr0_remaining;
3816 u32 val, ena_mask;
3817
3818 icr0 = rd32(hw, I40E_PFINT_ICR0);
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003819 ena_mask = rd32(hw, I40E_PFINT_ICR0_ENA);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003820
Shannon Nelson116a57d2013-09-28 07:13:59 +00003821 /* if sharing a legacy IRQ, we might get called w/o an intr pending */
3822 if ((icr0 & I40E_PFINT_ICR0_INTEVENT_MASK) == 0)
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003823 goto enable_intr;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003824
Shannon Nelsoncd92e722013-11-16 10:00:44 +00003825 /* if interrupt but no bits showing, must be SWINT */
3826 if (((icr0 & ~I40E_PFINT_ICR0_INTEVENT_MASK) == 0) ||
3827 (icr0 & I40E_PFINT_ICR0_SWINT_MASK))
3828 pf->sw_int_count++;
3829
Anjali Singhai Jain0d8e1432015-06-05 12:20:32 -04003830 if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
Catherine Sullivan76429842017-06-07 05:43:12 -04003831 (icr0 & I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK)) {
Anjali Singhai Jain0d8e1432015-06-05 12:20:32 -04003832 ena_mask &= ~I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
Carolyn Wyborny23bb6dc2016-11-08 13:05:12 -08003833 dev_dbg(&pf->pdev->dev, "cleared PE_CRITERR\n");
Catherine Sullivan76429842017-06-07 05:43:12 -04003834 set_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
Anjali Singhai Jain0d8e1432015-06-05 12:20:32 -04003835 }
3836
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003837 /* only q0 is used in MSI/Legacy mode, and none are used in MSIX */
3838 if (icr0 & I40E_PFINT_ICR0_QUEUE_0_MASK) {
Alexander Duyck5d3465a2015-09-29 15:19:50 -07003839 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
3840 struct i40e_q_vector *q_vector = vsi->q_vectors[0];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003841
Anjali Singhai Jaina16ae2d2016-01-15 14:33:16 -08003842 /* We do not have a way to disarm Queue causes while leaving
3843 * interrupt enabled for all other causes, ideally
3844 * interrupt should be disabled while we are in NAPI but
3845 * this is not a performance path and napi_schedule()
3846 * can deal with rescheduling.
3847 */
Mauro S. M. Rodrigues9e6c9c02017-05-12 23:26:56 -03003848 if (!test_bit(__I40E_DOWN, pf->state))
Alexander Duyck5d3465a2015-09-29 15:19:50 -07003849 napi_schedule_irqoff(&q_vector->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003850 }
3851
3852 if (icr0 & I40E_PFINT_ICR0_ADMINQ_MASK) {
3853 ena_mask &= ~I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
Jacob Keller0da36b92017-04-19 09:25:55 -04003854 set_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state);
Shannon Nelson6e93d0c2016-01-15 14:33:18 -08003855 i40e_debug(&pf->hw, I40E_DEBUG_NVM, "AdminQ event\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003856 }
3857
3858 if (icr0 & I40E_PFINT_ICR0_MAL_DETECT_MASK) {
3859 ena_mask &= ~I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
Jacob Keller0da36b92017-04-19 09:25:55 -04003860 set_bit(__I40E_MDD_EVENT_PENDING, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003861 }
3862
3863 if (icr0 & I40E_PFINT_ICR0_VFLR_MASK) {
3864 ena_mask &= ~I40E_PFINT_ICR0_ENA_VFLR_MASK;
Jacob Keller0da36b92017-04-19 09:25:55 -04003865 set_bit(__I40E_VFLR_EVENT_PENDING, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003866 }
3867
3868 if (icr0 & I40E_PFINT_ICR0_GRST_MASK) {
Jacob Keller0da36b92017-04-19 09:25:55 -04003869 if (!test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
3870 set_bit(__I40E_RESET_INTR_RECEIVED, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003871 ena_mask &= ~I40E_PFINT_ICR0_ENA_GRST_MASK;
3872 val = rd32(hw, I40E_GLGEN_RSTAT);
3873 val = (val & I40E_GLGEN_RSTAT_RESET_TYPE_MASK)
3874 >> I40E_GLGEN_RSTAT_RESET_TYPE_SHIFT;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003875 if (val == I40E_RESET_CORER) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003876 pf->corer_count++;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003877 } else if (val == I40E_RESET_GLOBR) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003878 pf->globr_count++;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003879 } else if (val == I40E_RESET_EMPR) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003880 pf->empr_count++;
Jacob Keller0da36b92017-04-19 09:25:55 -04003881 set_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state);
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003882 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003883 }
3884
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003885 if (icr0 & I40E_PFINT_ICR0_HMC_ERR_MASK) {
3886 icr0 &= ~I40E_PFINT_ICR0_HMC_ERR_MASK;
3887 dev_info(&pf->pdev->dev, "HMC error interrupt\n");
Anjali Singhai Jain25fc0e62015-03-31 00:45:01 -07003888 dev_info(&pf->pdev->dev, "HMC error info 0x%x, HMC error data 0x%x\n",
3889 rd32(hw, I40E_PFHMC_ERRORINFO),
3890 rd32(hw, I40E_PFHMC_ERRORDATA));
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003891 }
3892
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003893 if (icr0 & I40E_PFINT_ICR0_TIMESYNC_MASK) {
3894 u32 prttsyn_stat = rd32(hw, I40E_PRTTSYN_STAT_0);
3895
3896 if (prttsyn_stat & I40E_PRTTSYN_STAT_0_TXTIME_MASK) {
Jacob Kellercafa1fc2014-04-24 18:05:03 -07003897 icr0 &= ~I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003898 i40e_ptp_tx_hwtstamp(pf);
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003899 }
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003900 }
3901
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003902 /* If a critical error is pending we have no choice but to reset the
3903 * device.
3904 * Report and mask out any remaining unexpected interrupts.
3905 */
3906 icr0_remaining = icr0 & ena_mask;
3907 if (icr0_remaining) {
3908 dev_info(&pf->pdev->dev, "unhandled interrupt icr0=0x%08x\n",
3909 icr0_remaining);
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003910 if ((icr0_remaining & I40E_PFINT_ICR0_PE_CRITERR_MASK) ||
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003911 (icr0_remaining & I40E_PFINT_ICR0_PCI_EXCEPTION_MASK) ||
Anjali Singhai Jainc0c28972014-02-12 01:45:34 +00003912 (icr0_remaining & I40E_PFINT_ICR0_ECC_ERR_MASK)) {
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003913 dev_info(&pf->pdev->dev, "device will be reset\n");
Jacob Keller0da36b92017-04-19 09:25:55 -04003914 set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003915 i40e_service_event_schedule(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003916 }
3917 ena_mask &= ~icr0_remaining;
3918 }
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003919 ret = IRQ_HANDLED;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003920
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003921enable_intr:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003922 /* re-enable interrupt causes */
3923 wr32(hw, I40E_PFINT_ICR0_ENA, ena_mask);
Mauro S. M. Rodrigues9e6c9c02017-05-12 23:26:56 -03003924 if (!test_bit(__I40E_DOWN, pf->state)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003925 i40e_service_event_schedule(pf);
Jacob Kellerdbadbbe2017-09-07 08:05:49 -04003926 i40e_irq_dynamic_enable_icr0(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003927 }
3928
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003929 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003930}
3931
3932/**
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003933 * i40e_clean_fdir_tx_irq - Reclaim resources after transmit completes
3934 * @tx_ring: tx ring to clean
3935 * @budget: how many cleans we're allowed
3936 *
3937 * Returns true if there's any budget left (e.g. the clean is finished)
3938 **/
3939static bool i40e_clean_fdir_tx_irq(struct i40e_ring *tx_ring, int budget)
3940{
3941 struct i40e_vsi *vsi = tx_ring->vsi;
3942 u16 i = tx_ring->next_to_clean;
3943 struct i40e_tx_buffer *tx_buf;
3944 struct i40e_tx_desc *tx_desc;
3945
3946 tx_buf = &tx_ring->tx_bi[i];
3947 tx_desc = I40E_TX_DESC(tx_ring, i);
3948 i -= tx_ring->count;
3949
3950 do {
3951 struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
3952
3953 /* if next_to_watch is not set then there is no work pending */
3954 if (!eop_desc)
3955 break;
3956
3957 /* prevent any other reads prior to eop_desc */
3958 read_barrier_depends();
3959
3960 /* if the descriptor isn't done, no work yet to do */
3961 if (!(eop_desc->cmd_type_offset_bsz &
3962 cpu_to_le64(I40E_TX_DESC_DTYPE_DESC_DONE)))
3963 break;
3964
3965 /* clear next_to_watch to prevent false hangs */
3966 tx_buf->next_to_watch = NULL;
3967
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003968 tx_desc->buffer_addr = 0;
3969 tx_desc->cmd_type_offset_bsz = 0;
3970 /* move past filter desc */
3971 tx_buf++;
3972 tx_desc++;
3973 i++;
3974 if (unlikely(!i)) {
3975 i -= tx_ring->count;
3976 tx_buf = tx_ring->tx_bi;
3977 tx_desc = I40E_TX_DESC(tx_ring, 0);
3978 }
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003979 /* unmap skb header data */
3980 dma_unmap_single(tx_ring->dev,
3981 dma_unmap_addr(tx_buf, dma),
3982 dma_unmap_len(tx_buf, len),
3983 DMA_TO_DEVICE);
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003984 if (tx_buf->tx_flags & I40E_TX_FLAGS_FD_SB)
3985 kfree(tx_buf->raw_buf);
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003986
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003987 tx_buf->raw_buf = NULL;
3988 tx_buf->tx_flags = 0;
3989 tx_buf->next_to_watch = NULL;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003990 dma_unmap_len_set(tx_buf, len, 0);
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003991 tx_desc->buffer_addr = 0;
3992 tx_desc->cmd_type_offset_bsz = 0;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003993
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003994 /* move us past the eop_desc for start of next FD desc */
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003995 tx_buf++;
3996 tx_desc++;
3997 i++;
3998 if (unlikely(!i)) {
3999 i -= tx_ring->count;
4000 tx_buf = tx_ring->tx_bi;
4001 tx_desc = I40E_TX_DESC(tx_ring, 0);
4002 }
4003
4004 /* update budget accounting */
4005 budget--;
4006 } while (likely(budget));
4007
4008 i += tx_ring->count;
4009 tx_ring->next_to_clean = i;
4010
Jesse Brandeburg6995b362015-08-28 17:55:54 -04004011 if (vsi->back->flags & I40E_FLAG_MSIX_ENABLED)
Jesse Brandeburg78455482015-07-23 16:54:41 -04004012 i40e_irq_dynamic_enable(vsi, tx_ring->q_vector->v_idx);
Jesse Brandeburg6995b362015-08-28 17:55:54 -04004013
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08004014 return budget > 0;
4015}
4016
4017/**
4018 * i40e_fdir_clean_ring - Interrupt Handler for FDIR SB ring
4019 * @irq: interrupt number
4020 * @data: pointer to a q_vector
4021 **/
4022static irqreturn_t i40e_fdir_clean_ring(int irq, void *data)
4023{
4024 struct i40e_q_vector *q_vector = data;
4025 struct i40e_vsi *vsi;
4026
4027 if (!q_vector->tx.ring)
4028 return IRQ_HANDLED;
4029
4030 vsi = q_vector->tx.ring->vsi;
4031 i40e_clean_fdir_tx_irq(q_vector->tx.ring, vsi->work_limit);
4032
4033 return IRQ_HANDLED;
4034}
4035
4036/**
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00004037 * i40e_map_vector_to_qp - Assigns the queue pair to the vector
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004038 * @vsi: the VSI being configured
4039 * @v_idx: vector index
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00004040 * @qp_idx: queue pair index
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004041 **/
Anjali Singhai Jain26cdc442015-07-10 19:36:00 -04004042static void i40e_map_vector_to_qp(struct i40e_vsi *vsi, int v_idx, int qp_idx)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004043{
Alexander Duyck493fb302013-09-28 07:01:44 +00004044 struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
Alexander Duyck9f65e152013-09-28 06:00:58 +00004045 struct i40e_ring *tx_ring = vsi->tx_rings[qp_idx];
4046 struct i40e_ring *rx_ring = vsi->rx_rings[qp_idx];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004047
4048 tx_ring->q_vector = q_vector;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00004049 tx_ring->next = q_vector->tx.ring;
4050 q_vector->tx.ring = tx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004051 q_vector->tx.count++;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00004052
Björn Töpel74608d12017-05-24 07:55:35 +02004053 /* Place XDP Tx ring in the same q_vector ring list as regular Tx */
4054 if (i40e_enabled_xdp_vsi(vsi)) {
4055 struct i40e_ring *xdp_ring = vsi->xdp_rings[qp_idx];
4056
4057 xdp_ring->q_vector = q_vector;
4058 xdp_ring->next = q_vector->tx.ring;
4059 q_vector->tx.ring = xdp_ring;
4060 q_vector->tx.count++;
4061 }
4062
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00004063 rx_ring->q_vector = q_vector;
4064 rx_ring->next = q_vector->rx.ring;
4065 q_vector->rx.ring = rx_ring;
4066 q_vector->rx.count++;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004067}
4068
4069/**
4070 * i40e_vsi_map_rings_to_vectors - Maps descriptor rings to vectors
4071 * @vsi: the VSI being configured
4072 *
4073 * This function maps descriptor rings to the queue-specific vectors
4074 * we were allotted through the MSI-X enabling code. Ideally, we'd have
4075 * one vector per queue pair, but on a constrained vector budget, we
4076 * group the queue pairs as "efficiently" as possible.
4077 **/
4078static void i40e_vsi_map_rings_to_vectors(struct i40e_vsi *vsi)
4079{
4080 int qp_remaining = vsi->num_queue_pairs;
4081 int q_vectors = vsi->num_q_vectors;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00004082 int num_ringpairs;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004083 int v_start = 0;
4084 int qp_idx = 0;
4085
4086 /* If we don't have enough vectors for a 1-to-1 mapping, we'll have to
4087 * group them so there are multiple queues per vector.
Anjali Singhai Jain70114ec2014-06-03 23:50:14 +00004088 * It is also important to go through all the vectors available to be
4089 * sure that if we don't use all the vectors, that the remaining vectors
4090 * are cleared. This is especially important when decreasing the
4091 * number of queues in use.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004092 */
Anjali Singhai Jain70114ec2014-06-03 23:50:14 +00004093 for (; v_start < q_vectors; v_start++) {
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00004094 struct i40e_q_vector *q_vector = vsi->q_vectors[v_start];
4095
4096 num_ringpairs = DIV_ROUND_UP(qp_remaining, q_vectors - v_start);
4097
4098 q_vector->num_ringpairs = num_ringpairs;
4099
4100 q_vector->rx.count = 0;
4101 q_vector->tx.count = 0;
4102 q_vector->rx.ring = NULL;
4103 q_vector->tx.ring = NULL;
4104
4105 while (num_ringpairs--) {
Anjali Singhai Jain26cdc442015-07-10 19:36:00 -04004106 i40e_map_vector_to_qp(vsi, v_start, qp_idx);
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00004107 qp_idx++;
4108 qp_remaining--;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004109 }
4110 }
4111}
4112
4113/**
4114 * i40e_vsi_request_irq - Request IRQ from the OS
4115 * @vsi: the VSI being configured
4116 * @basename: name for the vector
4117 **/
4118static int i40e_vsi_request_irq(struct i40e_vsi *vsi, char *basename)
4119{
4120 struct i40e_pf *pf = vsi->back;
4121 int err;
4122
4123 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
4124 err = i40e_vsi_request_irq_msix(vsi, basename);
4125 else if (pf->flags & I40E_FLAG_MSI_ENABLED)
4126 err = request_irq(pf->pdev->irq, i40e_intr, 0,
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00004127 pf->int_name, pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004128 else
4129 err = request_irq(pf->pdev->irq, i40e_intr, IRQF_SHARED,
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00004130 pf->int_name, pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004131
4132 if (err)
4133 dev_info(&pf->pdev->dev, "request_irq failed, Error %d\n", err);
4134
4135 return err;
4136}
4137
4138#ifdef CONFIG_NET_POLL_CONTROLLER
4139/**
Jesse Brandeburgd89d9672016-01-04 10:33:02 -08004140 * i40e_netpoll - A Polling 'interrupt' handler
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004141 * @netdev: network interface device structure
4142 *
4143 * This is used by netconsole to send skbs without having to re-enable
4144 * interrupts. It's not called while the normal interrupt routine is executing.
4145 **/
4146static void i40e_netpoll(struct net_device *netdev)
4147{
4148 struct i40e_netdev_priv *np = netdev_priv(netdev);
4149 struct i40e_vsi *vsi = np->vsi;
4150 struct i40e_pf *pf = vsi->back;
4151 int i;
4152
4153 /* if interface is down do nothing */
Jacob Keller0da36b92017-04-19 09:25:55 -04004154 if (test_bit(__I40E_VSI_DOWN, vsi->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004155 return;
4156
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004157 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4158 for (i = 0; i < vsi->num_q_vectors; i++)
Alexander Duyck493fb302013-09-28 07:01:44 +00004159 i40e_msix_clean_rings(0, vsi->q_vectors[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004160 } else {
4161 i40e_intr(pf->pdev->irq, netdev);
4162 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004163}
4164#endif
4165
Jacob Kellerc768e492017-04-13 04:45:47 -04004166#define I40E_QTX_ENA_WAIT_COUNT 50
4167
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004168/**
Neerav Parikh23527302014-06-03 23:50:15 +00004169 * i40e_pf_txq_wait - Wait for a PF's Tx queue to be enabled or disabled
4170 * @pf: the PF being configured
4171 * @pf_q: the PF queue
4172 * @enable: enable or disable state of the queue
4173 *
4174 * This routine will wait for the given Tx queue of the PF to reach the
4175 * enabled or disabled state.
4176 * Returns -ETIMEDOUT in case of failing to reach the requested state after
4177 * multiple retries; else will return 0 in case of success.
4178 **/
4179static int i40e_pf_txq_wait(struct i40e_pf *pf, int pf_q, bool enable)
4180{
4181 int i;
4182 u32 tx_reg;
4183
4184 for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
4185 tx_reg = rd32(&pf->hw, I40E_QTX_ENA(pf_q));
4186 if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
4187 break;
4188
Neerav Parikhf98a2002014-09-13 07:40:44 +00004189 usleep_range(10, 20);
Neerav Parikh23527302014-06-03 23:50:15 +00004190 }
4191 if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
4192 return -ETIMEDOUT;
4193
4194 return 0;
4195}
4196
4197/**
Jacob Kellerc768e492017-04-13 04:45:47 -04004198 * i40e_control_tx_q - Start or stop a particular Tx queue
4199 * @pf: the PF structure
4200 * @pf_q: the PF queue to configure
4201 * @enable: start or stop the queue
4202 *
4203 * This function enables or disables a single queue. Note that any delay
4204 * required after the operation is expected to be handled by the caller of
4205 * this function.
4206 **/
4207static void i40e_control_tx_q(struct i40e_pf *pf, int pf_q, bool enable)
4208{
4209 struct i40e_hw *hw = &pf->hw;
4210 u32 tx_reg;
4211 int i;
4212
4213 /* warn the TX unit of coming changes */
4214 i40e_pre_tx_queue_cfg(&pf->hw, pf_q, enable);
4215 if (!enable)
4216 usleep_range(10, 20);
4217
4218 for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
4219 tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
4220 if (((tx_reg >> I40E_QTX_ENA_QENA_REQ_SHIFT) & 1) ==
4221 ((tx_reg >> I40E_QTX_ENA_QENA_STAT_SHIFT) & 1))
4222 break;
4223 usleep_range(1000, 2000);
4224 }
4225
4226 /* Skip if the queue is already in the requested state */
4227 if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
4228 return;
4229
4230 /* turn on/off the queue */
4231 if (enable) {
4232 wr32(hw, I40E_QTX_HEAD(pf_q), 0);
4233 tx_reg |= I40E_QTX_ENA_QENA_REQ_MASK;
4234 } else {
4235 tx_reg &= ~I40E_QTX_ENA_QENA_REQ_MASK;
4236 }
4237
4238 wr32(hw, I40E_QTX_ENA(pf_q), tx_reg);
4239}
4240
4241/**
Björn Töpel74608d12017-05-24 07:55:35 +02004242 * i40e_control_wait_tx_q - Start/stop Tx queue and wait for completion
4243 * @seid: VSI SEID
4244 * @pf: the PF structure
4245 * @pf_q: the PF queue to configure
4246 * @is_xdp: true if the queue is used for XDP
4247 * @enable: start or stop the queue
4248 **/
4249static int i40e_control_wait_tx_q(int seid, struct i40e_pf *pf, int pf_q,
4250 bool is_xdp, bool enable)
4251{
4252 int ret;
4253
4254 i40e_control_tx_q(pf, pf_q, enable);
4255
4256 /* wait for the change to finish */
4257 ret = i40e_pf_txq_wait(pf, pf_q, enable);
4258 if (ret) {
4259 dev_info(&pf->pdev->dev,
4260 "VSI seid %d %sTx ring %d %sable timeout\n",
4261 seid, (is_xdp ? "XDP " : ""), pf_q,
4262 (enable ? "en" : "dis"));
4263 }
4264
4265 return ret;
4266}
4267
4268/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004269 * i40e_vsi_control_tx - Start or stop a VSI's rings
4270 * @vsi: the VSI being configured
4271 * @enable: start or stop the rings
4272 **/
4273static int i40e_vsi_control_tx(struct i40e_vsi *vsi, bool enable)
4274{
4275 struct i40e_pf *pf = vsi->back;
Jacob Kellerc768e492017-04-13 04:45:47 -04004276 int i, pf_q, ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004277
4278 pf_q = vsi->base_queue;
4279 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
Björn Töpel74608d12017-05-24 07:55:35 +02004280 ret = i40e_control_wait_tx_q(vsi->seid, pf,
4281 pf_q,
4282 false /*is xdp*/, enable);
4283 if (ret)
Neerav Parikh23527302014-06-03 23:50:15 +00004284 break;
Björn Töpel74608d12017-05-24 07:55:35 +02004285
4286 if (!i40e_enabled_xdp_vsi(vsi))
4287 continue;
4288
4289 ret = i40e_control_wait_tx_q(vsi->seid, pf,
4290 pf_q + vsi->alloc_queue_pairs,
4291 true /*is xdp*/, enable);
4292 if (ret)
4293 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004294 }
4295
Neerav Parikh23527302014-06-03 23:50:15 +00004296 return ret;
4297}
4298
4299/**
4300 * i40e_pf_rxq_wait - Wait for a PF's Rx queue to be enabled or disabled
4301 * @pf: the PF being configured
4302 * @pf_q: the PF queue
4303 * @enable: enable or disable state of the queue
4304 *
4305 * This routine will wait for the given Rx queue of the PF to reach the
4306 * enabled or disabled state.
4307 * Returns -ETIMEDOUT in case of failing to reach the requested state after
4308 * multiple retries; else will return 0 in case of success.
4309 **/
4310static int i40e_pf_rxq_wait(struct i40e_pf *pf, int pf_q, bool enable)
4311{
4312 int i;
4313 u32 rx_reg;
4314
4315 for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
4316 rx_reg = rd32(&pf->hw, I40E_QRX_ENA(pf_q));
4317 if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4318 break;
4319
Neerav Parikhf98a2002014-09-13 07:40:44 +00004320 usleep_range(10, 20);
Neerav Parikh23527302014-06-03 23:50:15 +00004321 }
4322 if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
4323 return -ETIMEDOUT;
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00004324
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004325 return 0;
4326}
4327
4328/**
Jacob Kellerc768e492017-04-13 04:45:47 -04004329 * i40e_control_rx_q - Start or stop a particular Rx queue
4330 * @pf: the PF structure
4331 * @pf_q: the PF queue to configure
4332 * @enable: start or stop the queue
4333 *
4334 * This function enables or disables a single queue. Note that any delay
4335 * required after the operation is expected to be handled by the caller of
4336 * this function.
4337 **/
4338static void i40e_control_rx_q(struct i40e_pf *pf, int pf_q, bool enable)
4339{
4340 struct i40e_hw *hw = &pf->hw;
4341 u32 rx_reg;
4342 int i;
4343
4344 for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
4345 rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
4346 if (((rx_reg >> I40E_QRX_ENA_QENA_REQ_SHIFT) & 1) ==
4347 ((rx_reg >> I40E_QRX_ENA_QENA_STAT_SHIFT) & 1))
4348 break;
4349 usleep_range(1000, 2000);
4350 }
4351
4352 /* Skip if the queue is already in the requested state */
4353 if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4354 return;
4355
4356 /* turn on/off the queue */
4357 if (enable)
4358 rx_reg |= I40E_QRX_ENA_QENA_REQ_MASK;
4359 else
4360 rx_reg &= ~I40E_QRX_ENA_QENA_REQ_MASK;
4361
4362 wr32(hw, I40E_QRX_ENA(pf_q), rx_reg);
4363}
4364
4365/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004366 * i40e_vsi_control_rx - Start or stop a VSI's rings
4367 * @vsi: the VSI being configured
4368 * @enable: start or stop the rings
4369 **/
4370static int i40e_vsi_control_rx(struct i40e_vsi *vsi, bool enable)
4371{
4372 struct i40e_pf *pf = vsi->back;
Jacob Kellerc768e492017-04-13 04:45:47 -04004373 int i, pf_q, ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004374
4375 pf_q = vsi->base_queue;
4376 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
Jacob Kellerc768e492017-04-13 04:45:47 -04004377 i40e_control_rx_q(pf, pf_q, enable);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004378
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004379 /* wait for the change to finish */
Neerav Parikh23527302014-06-03 23:50:15 +00004380 ret = i40e_pf_rxq_wait(pf, pf_q, enable);
4381 if (ret) {
4382 dev_info(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04004383 "VSI seid %d Rx ring %d %sable timeout\n",
4384 vsi->seid, pf_q, (enable ? "en" : "dis"));
Neerav Parikh23527302014-06-03 23:50:15 +00004385 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004386 }
4387 }
4388
Wyborny, Carolynd08a9f62017-03-28 08:00:48 -07004389 /* Due to HW errata, on Rx disable only, the register can indicate done
4390 * before it really is. Needs 50ms to be sure
4391 */
4392 if (!enable)
4393 mdelay(50);
4394
Neerav Parikh23527302014-06-03 23:50:15 +00004395 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004396}
4397
4398/**
Filip Sadowski3aa7b742016-10-11 15:26:58 -07004399 * i40e_vsi_start_rings - Start a VSI's rings
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004400 * @vsi: the VSI being configured
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004401 **/
Filip Sadowski3aa7b742016-10-11 15:26:58 -07004402int i40e_vsi_start_rings(struct i40e_vsi *vsi)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004403{
Anjali Singhai Jain3b867b22013-12-21 05:44:44 +00004404 int ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004405
4406 /* do rx first for enable and last for disable */
Filip Sadowski3aa7b742016-10-11 15:26:58 -07004407 ret = i40e_vsi_control_rx(vsi, true);
4408 if (ret)
4409 return ret;
4410 ret = i40e_vsi_control_tx(vsi, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004411
4412 return ret;
4413}
4414
4415/**
Filip Sadowski3aa7b742016-10-11 15:26:58 -07004416 * i40e_vsi_stop_rings - Stop a VSI's rings
4417 * @vsi: the VSI being configured
4418 **/
4419void i40e_vsi_stop_rings(struct i40e_vsi *vsi)
4420{
Jacob Keller34807562017-04-13 04:45:53 -04004421 /* When port TX is suspended, don't wait */
Jacob Keller0da36b92017-04-19 09:25:55 -04004422 if (test_bit(__I40E_PORT_SUSPENDED, vsi->back->state))
Jacob Keller34807562017-04-13 04:45:53 -04004423 return i40e_vsi_stop_rings_no_wait(vsi);
4424
Filip Sadowski3aa7b742016-10-11 15:26:58 -07004425 /* do rx first for enable and last for disable
4426 * Ignore return value, we need to shutdown whatever we can
4427 */
4428 i40e_vsi_control_tx(vsi, false);
4429 i40e_vsi_control_rx(vsi, false);
4430}
4431
4432/**
Jacob Kellere4b433f2017-04-13 04:45:52 -04004433 * i40e_vsi_stop_rings_no_wait - Stop a VSI's rings and do not delay
4434 * @vsi: the VSI being shutdown
4435 *
4436 * This function stops all the rings for a VSI but does not delay to verify
4437 * that rings have been disabled. It is expected that the caller is shutting
4438 * down multiple VSIs at once and will delay together for all the VSIs after
4439 * initiating the shutdown. This is particularly useful for shutting down lots
4440 * of VFs together. Otherwise, a large delay can be incurred while configuring
4441 * each VSI in serial.
4442 **/
4443void i40e_vsi_stop_rings_no_wait(struct i40e_vsi *vsi)
4444{
4445 struct i40e_pf *pf = vsi->back;
4446 int i, pf_q;
4447
4448 pf_q = vsi->base_queue;
4449 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4450 i40e_control_tx_q(pf, pf_q, false);
4451 i40e_control_rx_q(pf, pf_q, false);
4452 }
4453}
4454
4455/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004456 * i40e_vsi_free_irq - Free the irq association with the OS
4457 * @vsi: the VSI being configured
4458 **/
4459static void i40e_vsi_free_irq(struct i40e_vsi *vsi)
4460{
4461 struct i40e_pf *pf = vsi->back;
4462 struct i40e_hw *hw = &pf->hw;
4463 int base = vsi->base_vector;
4464 u32 val, qp;
4465 int i;
4466
4467 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4468 if (!vsi->q_vectors)
4469 return;
4470
Shannon Nelson63741842014-04-23 04:50:16 +00004471 if (!vsi->irqs_ready)
4472 return;
4473
4474 vsi->irqs_ready = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004475 for (i = 0; i < vsi->num_q_vectors; i++) {
Alan Brady96db7762016-09-14 16:24:38 -07004476 int irq_num;
4477 u16 vector;
4478
4479 vector = i + base;
4480 irq_num = pf->msix_entries[vector].vector;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004481
4482 /* free only the irqs that were actually requested */
Shannon Nelson78681b12013-11-28 06:39:36 +00004483 if (!vsi->q_vectors[i] ||
4484 !vsi->q_vectors[i]->num_ringpairs)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004485 continue;
4486
Alan Brady96db7762016-09-14 16:24:38 -07004487 /* clear the affinity notifier in the IRQ descriptor */
4488 irq_set_affinity_notifier(irq_num, NULL);
Jacob Keller759dc4a2017-07-14 09:10:10 -04004489 /* remove our suggested affinity mask for this IRQ */
Alan Brady96db7762016-09-14 16:24:38 -07004490 irq_set_affinity_hint(irq_num, NULL);
4491 synchronize_irq(irq_num);
4492 free_irq(irq_num, vsi->q_vectors[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004493
4494 /* Tear down the interrupt queue link list
4495 *
4496 * We know that they come in pairs and always
4497 * the Rx first, then the Tx. To clear the
4498 * link list, stick the EOL value into the
4499 * next_q field of the registers.
4500 */
4501 val = rd32(hw, I40E_PFINT_LNKLSTN(vector - 1));
4502 qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
4503 >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4504 val |= I40E_QUEUE_END_OF_LIST
4505 << I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4506 wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), val);
4507
4508 while (qp != I40E_QUEUE_END_OF_LIST) {
4509 u32 next;
4510
4511 val = rd32(hw, I40E_QINT_RQCTL(qp));
4512
4513 val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
4514 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
4515 I40E_QINT_RQCTL_CAUSE_ENA_MASK |
4516 I40E_QINT_RQCTL_INTEVENT_MASK);
4517
4518 val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
4519 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
4520
4521 wr32(hw, I40E_QINT_RQCTL(qp), val);
4522
4523 val = rd32(hw, I40E_QINT_TQCTL(qp));
4524
4525 next = (val & I40E_QINT_TQCTL_NEXTQ_INDX_MASK)
4526 >> I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT;
4527
4528 val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
4529 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
4530 I40E_QINT_TQCTL_CAUSE_ENA_MASK |
4531 I40E_QINT_TQCTL_INTEVENT_MASK);
4532
4533 val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
4534 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
4535
4536 wr32(hw, I40E_QINT_TQCTL(qp), val);
4537 qp = next;
4538 }
4539 }
4540 } else {
4541 free_irq(pf->pdev->irq, pf);
4542
4543 val = rd32(hw, I40E_PFINT_LNKLST0);
4544 qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
4545 >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4546 val |= I40E_QUEUE_END_OF_LIST
4547 << I40E_PFINT_LNKLST0_FIRSTQ_INDX_SHIFT;
4548 wr32(hw, I40E_PFINT_LNKLST0, val);
4549
4550 val = rd32(hw, I40E_QINT_RQCTL(qp));
4551 val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
4552 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
4553 I40E_QINT_RQCTL_CAUSE_ENA_MASK |
4554 I40E_QINT_RQCTL_INTEVENT_MASK);
4555
4556 val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
4557 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
4558
4559 wr32(hw, I40E_QINT_RQCTL(qp), val);
4560
4561 val = rd32(hw, I40E_QINT_TQCTL(qp));
4562
4563 val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
4564 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
4565 I40E_QINT_TQCTL_CAUSE_ENA_MASK |
4566 I40E_QINT_TQCTL_INTEVENT_MASK);
4567
4568 val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
4569 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
4570
4571 wr32(hw, I40E_QINT_TQCTL(qp), val);
4572 }
4573}
4574
4575/**
Alexander Duyck493fb302013-09-28 07:01:44 +00004576 * i40e_free_q_vector - Free memory allocated for specific interrupt vector
4577 * @vsi: the VSI being configured
4578 * @v_idx: Index of vector to be freed
4579 *
4580 * This function frees the memory allocated to the q_vector. In addition if
4581 * NAPI is enabled it will delete any references to the NAPI struct prior
4582 * to freeing the q_vector.
4583 **/
4584static void i40e_free_q_vector(struct i40e_vsi *vsi, int v_idx)
4585{
4586 struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00004587 struct i40e_ring *ring;
Alexander Duyck493fb302013-09-28 07:01:44 +00004588
4589 if (!q_vector)
4590 return;
4591
4592 /* disassociate q_vector from rings */
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00004593 i40e_for_each_ring(ring, q_vector->tx)
4594 ring->q_vector = NULL;
4595
4596 i40e_for_each_ring(ring, q_vector->rx)
4597 ring->q_vector = NULL;
Alexander Duyck493fb302013-09-28 07:01:44 +00004598
4599 /* only VSI w/ an associated netdev is set up w/ NAPI */
4600 if (vsi->netdev)
4601 netif_napi_del(&q_vector->napi);
4602
4603 vsi->q_vectors[v_idx] = NULL;
4604
4605 kfree_rcu(q_vector, rcu);
4606}
4607
4608/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004609 * i40e_vsi_free_q_vectors - Free memory allocated for interrupt vectors
4610 * @vsi: the VSI being un-configured
4611 *
4612 * This frees the memory allocated to the q_vectors and
4613 * deletes references to the NAPI struct.
4614 **/
4615static void i40e_vsi_free_q_vectors(struct i40e_vsi *vsi)
4616{
4617 int v_idx;
4618
Alexander Duyck493fb302013-09-28 07:01:44 +00004619 for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++)
4620 i40e_free_q_vector(vsi, v_idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004621}
4622
4623/**
4624 * i40e_reset_interrupt_capability - Disable interrupt setup in OS
4625 * @pf: board private structure
4626 **/
4627static void i40e_reset_interrupt_capability(struct i40e_pf *pf)
4628{
4629 /* If we're in Legacy mode, the interrupt was cleaned in vsi_close */
4630 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4631 pci_disable_msix(pf->pdev);
4632 kfree(pf->msix_entries);
4633 pf->msix_entries = NULL;
Shannon Nelson3b444392015-02-26 16:15:57 +00004634 kfree(pf->irq_pile);
4635 pf->irq_pile = NULL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004636 } else if (pf->flags & I40E_FLAG_MSI_ENABLED) {
4637 pci_disable_msi(pf->pdev);
4638 }
4639 pf->flags &= ~(I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
4640}
4641
4642/**
4643 * i40e_clear_interrupt_scheme - Clear the current interrupt scheme settings
4644 * @pf: board private structure
4645 *
4646 * We go through and clear interrupt specific resources and reset the structure
4647 * to pre-load conditions
4648 **/
4649static void i40e_clear_interrupt_scheme(struct i40e_pf *pf)
4650{
4651 int i;
4652
Jacob Kellerc17401a2017-07-14 09:27:02 -04004653 i40e_free_misc_vector(pf);
Shannon Nelsone1477582015-02-21 06:44:33 +00004654
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06004655 i40e_put_lump(pf->irq_pile, pf->iwarp_base_vector,
4656 I40E_IWARP_IRQ_PILE_ID);
4657
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004658 i40e_put_lump(pf->irq_pile, 0, I40E_PILE_VALID_BIT-1);
Mitch Williams505682c2014-05-20 08:01:37 +00004659 for (i = 0; i < pf->num_alloc_vsi; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004660 if (pf->vsi[i])
4661 i40e_vsi_free_q_vectors(pf->vsi[i]);
4662 i40e_reset_interrupt_capability(pf);
4663}
4664
4665/**
4666 * i40e_napi_enable_all - Enable NAPI for all q_vectors in the VSI
4667 * @vsi: the VSI being configured
4668 **/
4669static void i40e_napi_enable_all(struct i40e_vsi *vsi)
4670{
4671 int q_idx;
4672
4673 if (!vsi->netdev)
4674 return;
4675
Alexander Duyck13a8cd12017-03-24 15:01:42 -07004676 for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
4677 struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
4678
4679 if (q_vector->rx.ring || q_vector->tx.ring)
4680 napi_enable(&q_vector->napi);
4681 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004682}
4683
4684/**
4685 * i40e_napi_disable_all - Disable NAPI for all q_vectors in the VSI
4686 * @vsi: the VSI being configured
4687 **/
4688static void i40e_napi_disable_all(struct i40e_vsi *vsi)
4689{
4690 int q_idx;
4691
4692 if (!vsi->netdev)
4693 return;
4694
Alexander Duyck13a8cd12017-03-24 15:01:42 -07004695 for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
4696 struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
4697
4698 if (q_vector->rx.ring || q_vector->tx.ring)
4699 napi_disable(&q_vector->napi);
4700 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004701}
4702
4703/**
Shannon Nelson90ef8d42014-03-14 07:32:26 +00004704 * i40e_vsi_close - Shut down a VSI
4705 * @vsi: the vsi to be quelled
4706 **/
4707static void i40e_vsi_close(struct i40e_vsi *vsi)
4708{
Mitch Williams0ef2d5a2017-01-24 10:24:00 -08004709 struct i40e_pf *pf = vsi->back;
Jacob Keller0da36b92017-04-19 09:25:55 -04004710 if (!test_and_set_bit(__I40E_VSI_DOWN, vsi->state))
Shannon Nelson90ef8d42014-03-14 07:32:26 +00004711 i40e_down(vsi);
4712 i40e_vsi_free_irq(vsi);
4713 i40e_vsi_free_tx_resources(vsi);
4714 i40e_vsi_free_rx_resources(vsi);
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04004715 vsi->current_netdev_flags = 0;
Mitch Williams0ef2d5a2017-01-24 10:24:00 -08004716 pf->flags |= I40E_FLAG_SERVICE_CLIENT_REQUESTED;
Jacob Keller0da36b92017-04-19 09:25:55 -04004717 if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
Mitch Williams0ef2d5a2017-01-24 10:24:00 -08004718 pf->flags |= I40E_FLAG_CLIENT_RESET;
Shannon Nelson90ef8d42014-03-14 07:32:26 +00004719}
4720
4721/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004722 * i40e_quiesce_vsi - Pause a given VSI
4723 * @vsi: the VSI being paused
4724 **/
4725static void i40e_quiesce_vsi(struct i40e_vsi *vsi)
4726{
Jacob Keller0da36b92017-04-19 09:25:55 -04004727 if (test_bit(__I40E_VSI_DOWN, vsi->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004728 return;
4729
Jacob Keller0da36b92017-04-19 09:25:55 -04004730 set_bit(__I40E_VSI_NEEDS_RESTART, vsi->state);
Jesse Brandeburg6995b362015-08-28 17:55:54 -04004731 if (vsi->netdev && netif_running(vsi->netdev))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004732 vsi->netdev->netdev_ops->ndo_stop(vsi->netdev);
Jesse Brandeburg6995b362015-08-28 17:55:54 -04004733 else
Shannon Nelson90ef8d42014-03-14 07:32:26 +00004734 i40e_vsi_close(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004735}
4736
4737/**
4738 * i40e_unquiesce_vsi - Resume a given VSI
4739 * @vsi: the VSI being resumed
4740 **/
4741static void i40e_unquiesce_vsi(struct i40e_vsi *vsi)
4742{
Jacob Keller0da36b92017-04-19 09:25:55 -04004743 if (!test_and_clear_bit(__I40E_VSI_NEEDS_RESTART, vsi->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004744 return;
4745
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004746 if (vsi->netdev && netif_running(vsi->netdev))
4747 vsi->netdev->netdev_ops->ndo_open(vsi->netdev);
4748 else
Shannon Nelson8276f752014-03-14 07:32:27 +00004749 i40e_vsi_open(vsi); /* this clears the DOWN bit */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004750}
4751
4752/**
4753 * i40e_pf_quiesce_all_vsi - Pause all VSIs on a PF
4754 * @pf: the PF
4755 **/
4756static void i40e_pf_quiesce_all_vsi(struct i40e_pf *pf)
4757{
4758 int v;
4759
Mitch Williams505682c2014-05-20 08:01:37 +00004760 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004761 if (pf->vsi[v])
4762 i40e_quiesce_vsi(pf->vsi[v]);
4763 }
4764}
4765
4766/**
4767 * i40e_pf_unquiesce_all_vsi - Resume all VSIs on a PF
4768 * @pf: the PF
4769 **/
4770static void i40e_pf_unquiesce_all_vsi(struct i40e_pf *pf)
4771{
4772 int v;
4773
Mitch Williams505682c2014-05-20 08:01:37 +00004774 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004775 if (pf->vsi[v])
4776 i40e_unquiesce_vsi(pf->vsi[v]);
4777 }
4778}
4779
Neerav Parikh69129dc2014-11-12 00:18:46 +00004780/**
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004781 * i40e_vsi_wait_queues_disabled - Wait for VSI's queues to be disabled
Neerav Parikh69129dc2014-11-12 00:18:46 +00004782 * @vsi: the VSI being configured
4783 *
Jacob Kelleraf26ce22017-04-05 07:50:58 -04004784 * Wait until all queues on a given VSI have been disabled.
Neerav Parikh69129dc2014-11-12 00:18:46 +00004785 **/
Jacob Kellere4b433f2017-04-13 04:45:52 -04004786int i40e_vsi_wait_queues_disabled(struct i40e_vsi *vsi)
Neerav Parikh69129dc2014-11-12 00:18:46 +00004787{
4788 struct i40e_pf *pf = vsi->back;
4789 int i, pf_q, ret;
4790
4791 pf_q = vsi->base_queue;
4792 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
Jacob Kelleraf26ce22017-04-05 07:50:58 -04004793 /* Check and wait for the Tx queue */
Neerav Parikh69129dc2014-11-12 00:18:46 +00004794 ret = i40e_pf_txq_wait(pf, pf_q, false);
4795 if (ret) {
4796 dev_info(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04004797 "VSI seid %d Tx ring %d disable timeout\n",
4798 vsi->seid, pf_q);
Neerav Parikh69129dc2014-11-12 00:18:46 +00004799 return ret;
4800 }
Björn Töpel74608d12017-05-24 07:55:35 +02004801
4802 if (!i40e_enabled_xdp_vsi(vsi))
4803 goto wait_rx;
4804
4805 /* Check and wait for the XDP Tx queue */
4806 ret = i40e_pf_txq_wait(pf, pf_q + vsi->alloc_queue_pairs,
4807 false);
4808 if (ret) {
4809 dev_info(&pf->pdev->dev,
4810 "VSI seid %d XDP Tx ring %d disable timeout\n",
4811 vsi->seid, pf_q);
4812 return ret;
4813 }
4814wait_rx:
4815 /* Check and wait for the Rx queue */
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004816 ret = i40e_pf_rxq_wait(pf, pf_q, false);
4817 if (ret) {
4818 dev_info(&pf->pdev->dev,
4819 "VSI seid %d Rx ring %d disable timeout\n",
4820 vsi->seid, pf_q);
4821 return ret;
4822 }
4823 }
4824
Neerav Parikh69129dc2014-11-12 00:18:46 +00004825 return 0;
4826}
4827
Jacob Kellere4b433f2017-04-13 04:45:52 -04004828#ifdef CONFIG_I40E_DCB
Neerav Parikh69129dc2014-11-12 00:18:46 +00004829/**
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004830 * i40e_pf_wait_queues_disabled - Wait for all queues of PF VSIs to be disabled
Neerav Parikh69129dc2014-11-12 00:18:46 +00004831 * @pf: the PF
4832 *
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004833 * This function waits for the queues to be in disabled state for all the
Neerav Parikh69129dc2014-11-12 00:18:46 +00004834 * VSIs that are managed by this PF.
4835 **/
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004836static int i40e_pf_wait_queues_disabled(struct i40e_pf *pf)
Neerav Parikh69129dc2014-11-12 00:18:46 +00004837{
4838 int v, ret = 0;
4839
4840 for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
Alexander Duyckc76cb6e2017-02-21 15:55:48 -08004841 if (pf->vsi[v]) {
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004842 ret = i40e_vsi_wait_queues_disabled(pf->vsi[v]);
Neerav Parikh69129dc2014-11-12 00:18:46 +00004843 if (ret)
4844 break;
4845 }
4846 }
4847
4848 return ret;
4849}
4850
4851#endif
Kiran Patilb03a8c12015-09-24 18:13:15 -04004852
4853/**
4854 * i40e_detect_recover_hung_queue - Function to detect and recover hung_queue
4855 * @q_idx: TX queue number
4856 * @vsi: Pointer to VSI struct
4857 *
4858 * This function checks specified queue for given VSI. Detects hung condition.
Alan Brady17daabb2017-04-05 07:50:56 -04004859 * We proactively detect hung TX queues by checking if interrupts are disabled
4860 * but there are pending descriptors. If it appears hung, attempt to recover
4861 * by triggering a SW interrupt.
Kiran Patilb03a8c12015-09-24 18:13:15 -04004862 **/
4863static void i40e_detect_recover_hung_queue(int q_idx, struct i40e_vsi *vsi)
4864{
4865 struct i40e_ring *tx_ring = NULL;
4866 struct i40e_pf *pf;
Alan Brady17daabb2017-04-05 07:50:56 -04004867 u32 val, tx_pending;
Kiran Patilb03a8c12015-09-24 18:13:15 -04004868 int i;
4869
4870 pf = vsi->back;
4871
4872 /* now that we have an index, find the tx_ring struct */
4873 for (i = 0; i < vsi->num_queue_pairs; i++) {
4874 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc) {
4875 if (q_idx == vsi->tx_rings[i]->queue_index) {
4876 tx_ring = vsi->tx_rings[i];
4877 break;
4878 }
4879 }
4880 }
4881
4882 if (!tx_ring)
4883 return;
4884
4885 /* Read interrupt register */
4886 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
4887 val = rd32(&pf->hw,
4888 I40E_PFINT_DYN_CTLN(tx_ring->q_vector->v_idx +
4889 tx_ring->vsi->base_vector - 1));
4890 else
4891 val = rd32(&pf->hw, I40E_PFINT_DYN_CTL0);
4892
Alan Brady17daabb2017-04-05 07:50:56 -04004893 tx_pending = i40e_get_tx_pending(tx_ring);
Kiran Patilb03a8c12015-09-24 18:13:15 -04004894
Alan Brady17daabb2017-04-05 07:50:56 -04004895 /* Interrupts are disabled and TX pending is non-zero,
4896 * trigger the SW interrupt (don't wait). Worst case
4897 * there will be one extra interrupt which may result
4898 * into not cleaning any queues because queues are cleaned.
Kiran Patilb03a8c12015-09-24 18:13:15 -04004899 */
Alan Brady17daabb2017-04-05 07:50:56 -04004900 if (tx_pending && (!(val & I40E_PFINT_DYN_CTLN_INTENA_MASK)))
4901 i40e_force_wb(vsi, tx_ring->q_vector);
Kiran Patilb03a8c12015-09-24 18:13:15 -04004902}
4903
4904/**
4905 * i40e_detect_recover_hung - Function to detect and recover hung_queues
4906 * @pf: pointer to PF struct
4907 *
4908 * LAN VSI has netdev and netdev has TX queues. This function is to check
4909 * each of those TX queues if they are hung, trigger recovery by issuing
4910 * SW interrupt.
4911 **/
4912static void i40e_detect_recover_hung(struct i40e_pf *pf)
4913{
4914 struct net_device *netdev;
4915 struct i40e_vsi *vsi;
Jesse Brandeburgb85c94b2017-06-20 15:16:59 -07004916 unsigned int i;
Kiran Patilb03a8c12015-09-24 18:13:15 -04004917
4918 /* Only for LAN VSI */
4919 vsi = pf->vsi[pf->lan_vsi];
4920
4921 if (!vsi)
4922 return;
4923
4924 /* Make sure, VSI state is not DOWN/RECOVERY_PENDING */
Jacob Keller0da36b92017-04-19 09:25:55 -04004925 if (test_bit(__I40E_VSI_DOWN, vsi->back->state) ||
4926 test_bit(__I40E_RESET_RECOVERY_PENDING, vsi->back->state))
Kiran Patilb03a8c12015-09-24 18:13:15 -04004927 return;
4928
4929 /* Make sure type is MAIN VSI */
4930 if (vsi->type != I40E_VSI_MAIN)
4931 return;
4932
4933 netdev = vsi->netdev;
4934 if (!netdev)
4935 return;
4936
4937 /* Bail out if netif_carrier is not OK */
4938 if (!netif_carrier_ok(netdev))
4939 return;
4940
4941 /* Go thru' TX queues for netdev */
4942 for (i = 0; i < netdev->num_tx_queues; i++) {
4943 struct netdev_queue *q;
4944
4945 q = netdev_get_tx_queue(netdev, i);
4946 if (q)
4947 i40e_detect_recover_hung_queue(i, vsi);
4948 }
4949}
4950
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004951/**
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004952 * i40e_get_iscsi_tc_map - Return TC map for iSCSI APP
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00004953 * @pf: pointer to PF
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004954 *
4955 * Get TC map for ISCSI PF type that will include iSCSI TC
4956 * and LAN TC.
4957 **/
4958static u8 i40e_get_iscsi_tc_map(struct i40e_pf *pf)
4959{
4960 struct i40e_dcb_app_priority_table app;
4961 struct i40e_hw *hw = &pf->hw;
4962 u8 enabled_tc = 1; /* TC0 is always enabled */
4963 u8 tc, i;
4964 /* Get the iSCSI APP TLV */
4965 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
4966
4967 for (i = 0; i < dcbcfg->numapps; i++) {
4968 app = dcbcfg->app[i];
4969 if (app.selector == I40E_APP_SEL_TCPIP &&
4970 app.protocolid == I40E_APP_PROTOID_ISCSI) {
4971 tc = dcbcfg->etscfg.prioritytable[app.priority];
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08004972 enabled_tc |= BIT(tc);
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004973 break;
4974 }
4975 }
4976
4977 return enabled_tc;
4978}
4979
4980/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004981 * i40e_dcb_get_num_tc - Get the number of TCs from DCBx config
4982 * @dcbcfg: the corresponding DCBx configuration structure
4983 *
4984 * Return the number of TCs from given DCBx configuration
4985 **/
4986static u8 i40e_dcb_get_num_tc(struct i40e_dcbx_config *dcbcfg)
4987{
Dave Ertmanfbfe12c2016-08-12 09:56:32 -07004988 int i, tc_unused = 0;
Jesse Brandeburg078b5872013-09-25 23:41:14 +00004989 u8 num_tc = 0;
Dave Ertmanfbfe12c2016-08-12 09:56:32 -07004990 u8 ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004991
4992 /* Scan the ETS Config Priority Table to find
4993 * traffic class enabled for a given priority
Dave Ertmanfbfe12c2016-08-12 09:56:32 -07004994 * and create a bitmask of enabled TCs
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004995 */
Dave Ertmanfbfe12c2016-08-12 09:56:32 -07004996 for (i = 0; i < I40E_MAX_USER_PRIORITY; i++)
4997 num_tc |= BIT(dcbcfg->etscfg.prioritytable[i]);
4998
4999 /* Now scan the bitmask to check for
5000 * contiguous TCs starting with TC0
5001 */
5002 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5003 if (num_tc & BIT(i)) {
5004 if (!tc_unused) {
5005 ret++;
5006 } else {
5007 pr_err("Non-contiguous TC - Disabling DCB\n");
5008 return 1;
5009 }
5010 } else {
5011 tc_unused = 1;
5012 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005013 }
5014
Dave Ertmanfbfe12c2016-08-12 09:56:32 -07005015 /* There is always at least TC0 */
5016 if (!ret)
5017 ret = 1;
5018
5019 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005020}
5021
5022/**
5023 * i40e_dcb_get_enabled_tc - Get enabled traffic classes
5024 * @dcbcfg: the corresponding DCBx configuration structure
5025 *
5026 * Query the current DCB configuration and return the number of
5027 * traffic classes enabled from the given DCBX config
5028 **/
5029static u8 i40e_dcb_get_enabled_tc(struct i40e_dcbx_config *dcbcfg)
5030{
5031 u8 num_tc = i40e_dcb_get_num_tc(dcbcfg);
5032 u8 enabled_tc = 1;
5033 u8 i;
5034
5035 for (i = 0; i < num_tc; i++)
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005036 enabled_tc |= BIT(i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005037
5038 return enabled_tc;
5039}
5040
5041/**
Amritha Nambiara9ce82f2017-09-07 04:00:22 -07005042 * i40e_mqprio_get_enabled_tc - Get enabled traffic classes
5043 * @pf: PF being queried
5044 *
5045 * Query the current MQPRIO configuration and return the number of
5046 * traffic classes enabled.
5047 **/
5048static u8 i40e_mqprio_get_enabled_tc(struct i40e_pf *pf)
5049{
5050 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
5051 u8 num_tc = vsi->mqprio_qopt.qopt.num_tc;
5052 u8 enabled_tc = 1, i;
5053
5054 for (i = 1; i < num_tc; i++)
5055 enabled_tc |= BIT(i);
5056 return enabled_tc;
5057}
5058
5059/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005060 * i40e_pf_get_num_tc - Get enabled traffic classes for PF
5061 * @pf: PF being queried
5062 *
5063 * Return number of traffic classes enabled for the given PF
5064 **/
5065static u8 i40e_pf_get_num_tc(struct i40e_pf *pf)
5066{
5067 struct i40e_hw *hw = &pf->hw;
Dave Ertman52a08ca2016-07-27 12:02:34 -07005068 u8 i, enabled_tc = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005069 u8 num_tc = 0;
5070 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5071
Amritha Nambiara9ce82f2017-09-07 04:00:22 -07005072 if (pf->flags & I40E_FLAG_TC_MQPRIO)
5073 return pf->vsi[pf->lan_vsi]->mqprio_qopt.qopt.num_tc;
5074
5075 /* If neither MQPRIO nor DCB is enabled, then always use single TC */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005076 if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
5077 return 1;
5078
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005079 /* SFP mode will be enabled for all TCs on port */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00005080 if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
5081 return i40e_dcb_get_num_tc(dcbcfg);
5082
5083 /* MFP mode return count of enabled TCs for this PF */
5084 if (pf->hw.func_caps.iscsi)
5085 enabled_tc = i40e_get_iscsi_tc_map(pf);
5086 else
Neerav Parikhfc51de92015-02-24 06:58:53 +00005087 return 1; /* Only TC0 */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00005088
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00005089 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08005090 if (enabled_tc & BIT(i))
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00005091 num_tc++;
5092 }
5093 return num_tc;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005094}
5095
5096/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005097 * i40e_pf_get_pf_tc_map - Get bitmap for enabled traffic classes
5098 * @pf: PF being queried
5099 *
5100 * Return a bitmap for enabled traffic classes for this PF.
5101 **/
5102static u8 i40e_pf_get_tc_map(struct i40e_pf *pf)
5103{
Amritha Nambiara9ce82f2017-09-07 04:00:22 -07005104 if (pf->flags & I40E_FLAG_TC_MQPRIO)
5105 return i40e_mqprio_get_enabled_tc(pf);
5106
5107 /* If neither MQPRIO nor DCB is enabled for this PF then just return
5108 * default TC
5109 */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005110 if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
David Ertmanea6acb72016-09-20 07:10:50 -07005111 return I40E_DEFAULT_TRAFFIC_CLASS;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005112
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005113 /* SFP mode we want PF to be enabled for all TCs */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00005114 if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
5115 return i40e_dcb_get_enabled_tc(&pf->hw.local_dcbx_config);
5116
Neerav Parikhfc51de92015-02-24 06:58:53 +00005117 /* MFP enabled and iSCSI PF type */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00005118 if (pf->hw.func_caps.iscsi)
5119 return i40e_get_iscsi_tc_map(pf);
5120 else
David Ertmanea6acb72016-09-20 07:10:50 -07005121 return I40E_DEFAULT_TRAFFIC_CLASS;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005122}
5123
5124/**
5125 * i40e_vsi_get_bw_info - Query VSI BW Information
5126 * @vsi: the VSI being queried
5127 *
5128 * Returns 0 on success, negative value on failure
5129 **/
5130static int i40e_vsi_get_bw_info(struct i40e_vsi *vsi)
5131{
5132 struct i40e_aqc_query_vsi_ets_sla_config_resp bw_ets_config = {0};
5133 struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
5134 struct i40e_pf *pf = vsi->back;
5135 struct i40e_hw *hw = &pf->hw;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005136 i40e_status ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005137 u32 tc_bw_max;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005138 int i;
5139
5140 /* Get the VSI level BW configuration */
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005141 ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid, &bw_config, NULL);
5142 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005143 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005144 "couldn't get PF vsi bw config, err %s aq_err %s\n",
5145 i40e_stat_str(&pf->hw, ret),
5146 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00005147 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005148 }
5149
5150 /* Get the VSI level BW configuration per TC */
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005151 ret = i40e_aq_query_vsi_ets_sla_config(hw, vsi->seid, &bw_ets_config,
5152 NULL);
5153 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005154 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005155 "couldn't get PF vsi ets bw config, err %s aq_err %s\n",
5156 i40e_stat_str(&pf->hw, ret),
5157 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00005158 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005159 }
5160
5161 if (bw_config.tc_valid_bits != bw_ets_config.tc_valid_bits) {
5162 dev_info(&pf->pdev->dev,
5163 "Enabled TCs mismatch from querying VSI BW info 0x%08x 0x%08x\n",
5164 bw_config.tc_valid_bits,
5165 bw_ets_config.tc_valid_bits);
5166 /* Still continuing */
5167 }
5168
5169 vsi->bw_limit = le16_to_cpu(bw_config.port_bw_limit);
5170 vsi->bw_max_quanta = bw_config.max_bw;
5171 tc_bw_max = le16_to_cpu(bw_ets_config.tc_bw_max[0]) |
5172 (le16_to_cpu(bw_ets_config.tc_bw_max[1]) << 16);
5173 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5174 vsi->bw_ets_share_credits[i] = bw_ets_config.share_credits[i];
5175 vsi->bw_ets_limit_credits[i] =
5176 le16_to_cpu(bw_ets_config.credits[i]);
5177 /* 3 bits out of 4 for each TC */
5178 vsi->bw_ets_max_quanta[i] = (u8)((tc_bw_max >> (i*4)) & 0x7);
5179 }
Jesse Brandeburg078b5872013-09-25 23:41:14 +00005180
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00005181 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005182}
5183
5184/**
5185 * i40e_vsi_configure_bw_alloc - Configure VSI BW allocation per TC
5186 * @vsi: the VSI being configured
5187 * @enabled_tc: TC bitmap
5188 * @bw_credits: BW shared credits per TC
5189 *
5190 * Returns 0 on success, negative value on failure
5191 **/
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00005192static int i40e_vsi_configure_bw_alloc(struct i40e_vsi *vsi, u8 enabled_tc,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005193 u8 *bw_share)
5194{
5195 struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005196 i40e_status ret;
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00005197 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005198
Amritha Nambiar2027d4d2017-09-07 04:00:32 -07005199 if (vsi->back->flags & I40E_FLAG_TC_MQPRIO)
Amritha Nambiara9ce82f2017-09-07 04:00:22 -07005200 return 0;
Amritha Nambiar2027d4d2017-09-07 04:00:32 -07005201 if (!vsi->mqprio_qopt.qopt.hw) {
5202 ret = i40e_set_bw_limit(vsi, vsi->seid, 0);
5203 if (ret)
5204 dev_info(&vsi->back->pdev->dev,
5205 "Failed to reset tx rate for vsi->seid %u\n",
5206 vsi->seid);
5207 return ret;
5208 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005209 bw_data.tc_valid_bits = enabled_tc;
5210 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5211 bw_data.tc_bw_credits[i] = bw_share[i];
5212
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005213 ret = i40e_aq_config_vsi_tc_bw(&vsi->back->hw, vsi->seid, &bw_data,
5214 NULL);
5215 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005216 dev_info(&vsi->back->pdev->dev,
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005217 "AQ command Config VSI BW allocation per TC failed = %d\n",
5218 vsi->back->hw.aq.asq_last_status);
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00005219 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005220 }
5221
5222 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5223 vsi->info.qs_handle[i] = bw_data.qs_handles[i];
5224
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00005225 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005226}
5227
5228/**
5229 * i40e_vsi_config_netdev_tc - Setup the netdev TC configuration
5230 * @vsi: the VSI being configured
5231 * @enabled_tc: TC map to be enabled
5232 *
5233 **/
5234static void i40e_vsi_config_netdev_tc(struct i40e_vsi *vsi, u8 enabled_tc)
5235{
5236 struct net_device *netdev = vsi->netdev;
5237 struct i40e_pf *pf = vsi->back;
5238 struct i40e_hw *hw = &pf->hw;
5239 u8 netdev_tc = 0;
5240 int i;
5241 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5242
5243 if (!netdev)
5244 return;
5245
5246 if (!enabled_tc) {
5247 netdev_reset_tc(netdev);
5248 return;
5249 }
5250
5251 /* Set up actual enabled TCs on the VSI */
5252 if (netdev_set_num_tc(netdev, vsi->tc_config.numtc))
5253 return;
5254
5255 /* set per TC queues for the VSI */
5256 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5257 /* Only set TC queues for enabled tcs
5258 *
5259 * e.g. For a VSI that has TC0 and TC3 enabled the
5260 * enabled_tc bitmap would be 0x00001001; the driver
5261 * will set the numtc for netdev as 2 that will be
5262 * referenced by the netdev layer as TC 0 and 1.
5263 */
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08005264 if (vsi->tc_config.enabled_tc & BIT(i))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005265 netdev_set_tc_queue(netdev,
5266 vsi->tc_config.tc_info[i].netdev_tc,
5267 vsi->tc_config.tc_info[i].qcount,
5268 vsi->tc_config.tc_info[i].qoffset);
5269 }
5270
Amritha Nambiara9ce82f2017-09-07 04:00:22 -07005271 if (pf->flags & I40E_FLAG_TC_MQPRIO)
5272 return;
5273
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005274 /* Assign UP2TC map for the VSI */
5275 for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
5276 /* Get the actual TC# for the UP */
5277 u8 ets_tc = dcbcfg->etscfg.prioritytable[i];
5278 /* Get the mapped netdev TC# for the UP */
5279 netdev_tc = vsi->tc_config.tc_info[ets_tc].netdev_tc;
5280 netdev_set_prio_tc_map(netdev, i, netdev_tc);
5281 }
5282}
5283
5284/**
5285 * i40e_vsi_update_queue_map - Update our copy of VSi info with new queue map
5286 * @vsi: the VSI being configured
5287 * @ctxt: the ctxt buffer returned from AQ VSI update param command
5288 **/
5289static void i40e_vsi_update_queue_map(struct i40e_vsi *vsi,
5290 struct i40e_vsi_context *ctxt)
5291{
5292 /* copy just the sections touched not the entire info
5293 * since not all sections are valid as returned by
5294 * update vsi params
5295 */
5296 vsi->info.mapping_flags = ctxt->info.mapping_flags;
5297 memcpy(&vsi->info.queue_mapping,
5298 &ctxt->info.queue_mapping, sizeof(vsi->info.queue_mapping));
5299 memcpy(&vsi->info.tc_mapping, ctxt->info.tc_mapping,
5300 sizeof(vsi->info.tc_mapping));
5301}
5302
5303/**
5304 * i40e_vsi_config_tc - Configure VSI Tx Scheduler for given TC map
5305 * @vsi: VSI to be configured
5306 * @enabled_tc: TC bitmap
5307 *
5308 * This configures a particular VSI for TCs that are mapped to the
5309 * given TC bitmap. It uses default bandwidth share for TCs across
5310 * VSIs to configure TC for a particular VSI.
5311 *
5312 * NOTE:
5313 * It is expected that the VSI queues have been quisced before calling
5314 * this function.
5315 **/
5316static int i40e_vsi_config_tc(struct i40e_vsi *vsi, u8 enabled_tc)
5317{
5318 u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
5319 struct i40e_vsi_context ctxt;
5320 int ret = 0;
5321 int i;
5322
5323 /* Check if enabled_tc is same as existing or new TCs */
Amritha Nambiara9ce82f2017-09-07 04:00:22 -07005324 if (vsi->tc_config.enabled_tc == enabled_tc &&
5325 vsi->mqprio_qopt.mode != TC_MQPRIO_MODE_CHANNEL)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005326 return ret;
5327
5328 /* Enable ETS TCs with equal BW Share for now across all VSIs */
5329 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08005330 if (enabled_tc & BIT(i))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005331 bw_share[i] = 1;
5332 }
5333
5334 ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
5335 if (ret) {
5336 dev_info(&vsi->back->pdev->dev,
5337 "Failed configuring TC map %d for VSI %d\n",
5338 enabled_tc, vsi->seid);
5339 goto out;
5340 }
5341
5342 /* Update Queue Pairs Mapping for currently enabled UPs */
5343 ctxt.seid = vsi->seid;
5344 ctxt.pf_num = vsi->back->hw.pf_id;
5345 ctxt.vf_num = 0;
5346 ctxt.uplink_seid = vsi->uplink_seid;
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07005347 ctxt.info = vsi->info;
Amritha Nambiara9ce82f2017-09-07 04:00:22 -07005348 if (vsi->back->flags & I40E_FLAG_TC_MQPRIO) {
5349 ret = i40e_vsi_setup_queue_map_mqprio(vsi, &ctxt, enabled_tc);
5350 if (ret)
5351 goto out;
5352 } else {
5353 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
5354 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005355
Amritha Nambiara9ce82f2017-09-07 04:00:22 -07005356 /* On destroying the qdisc, reset vsi->rss_size, as number of enabled
5357 * queues changed.
5358 */
5359 if (!vsi->mqprio_qopt.qopt.hw && vsi->reconfig_rss) {
5360 vsi->rss_size = min_t(int, vsi->back->alloc_rss_size,
5361 vsi->num_queue_pairs);
5362 ret = i40e_vsi_config_rss(vsi);
5363 if (ret) {
5364 dev_info(&vsi->back->pdev->dev,
5365 "Failed to reconfig rss for num_queues\n");
5366 return ret;
5367 }
5368 vsi->reconfig_rss = false;
5369 }
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06005370 if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
5371 ctxt.info.valid_sections |=
5372 cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
5373 ctxt.info.queueing_opt_flags |= I40E_AQ_VSI_QUE_OPT_TCP_ENA;
5374 }
5375
Amritha Nambiara9ce82f2017-09-07 04:00:22 -07005376 /* Update the VSI after updating the VSI queue-mapping
5377 * information
5378 */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005379 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
5380 if (ret) {
5381 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005382 "Update vsi tc config failed, err %s aq_err %s\n",
5383 i40e_stat_str(&vsi->back->hw, ret),
5384 i40e_aq_str(&vsi->back->hw,
5385 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005386 goto out;
5387 }
5388 /* update the local VSI info with updated queue map */
5389 i40e_vsi_update_queue_map(vsi, &ctxt);
5390 vsi->info.valid_sections = 0;
5391
5392 /* Update current VSI BW information */
5393 ret = i40e_vsi_get_bw_info(vsi);
5394 if (ret) {
5395 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005396 "Failed updating vsi bw info, err %s aq_err %s\n",
5397 i40e_stat_str(&vsi->back->hw, ret),
5398 i40e_aq_str(&vsi->back->hw,
5399 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005400 goto out;
5401 }
5402
5403 /* Update the netdev TC setup */
5404 i40e_vsi_config_netdev_tc(vsi, enabled_tc);
5405out:
5406 return ret;
5407}
5408
5409/**
Amritha Nambiar5ecae412017-09-07 04:00:27 -07005410 * i40e_get_link_speed - Returns link speed for the interface
5411 * @vsi: VSI to be configured
5412 *
5413 **/
5414int i40e_get_link_speed(struct i40e_vsi *vsi)
5415{
5416 struct i40e_pf *pf = vsi->back;
5417
5418 switch (pf->hw.phy.link_info.link_speed) {
5419 case I40E_LINK_SPEED_40GB:
5420 return 40000;
5421 case I40E_LINK_SPEED_25GB:
5422 return 25000;
5423 case I40E_LINK_SPEED_20GB:
5424 return 20000;
5425 case I40E_LINK_SPEED_10GB:
5426 return 10000;
5427 case I40E_LINK_SPEED_1GB:
5428 return 1000;
5429 default:
5430 return -EINVAL;
5431 }
5432}
5433
5434/**
5435 * i40e_set_bw_limit - setup BW limit for Tx traffic based on max_tx_rate
5436 * @vsi: VSI to be configured
5437 * @seid: seid of the channel/VSI
5438 * @max_tx_rate: max TX rate to be configured as BW limit
5439 *
5440 * Helper function to set BW limit for a given VSI
5441 **/
5442int i40e_set_bw_limit(struct i40e_vsi *vsi, u16 seid, u64 max_tx_rate)
5443{
5444 struct i40e_pf *pf = vsi->back;
Alan Brady6c32e0d2017-10-09 15:48:45 -07005445 u64 credits = 0;
Amritha Nambiar5ecae412017-09-07 04:00:27 -07005446 int speed = 0;
5447 int ret = 0;
5448
5449 speed = i40e_get_link_speed(vsi);
5450 if (max_tx_rate > speed) {
5451 dev_err(&pf->pdev->dev,
5452 "Invalid max tx rate %llu specified for VSI seid %d.",
5453 max_tx_rate, seid);
5454 return -EINVAL;
5455 }
5456 if (max_tx_rate && max_tx_rate < 50) {
5457 dev_warn(&pf->pdev->dev,
5458 "Setting max tx rate to minimum usable value of 50Mbps.\n");
5459 max_tx_rate = 50;
5460 }
5461
5462 /* Tx rate credits are in values of 50Mbps, 0 is disabled */
Alan Brady6c32e0d2017-10-09 15:48:45 -07005463 credits = max_tx_rate;
5464 do_div(credits, I40E_BW_CREDIT_DIVISOR);
5465 ret = i40e_aq_config_vsi_bw_limit(&pf->hw, seid, credits,
Amritha Nambiar5ecae412017-09-07 04:00:27 -07005466 I40E_MAX_BW_INACTIVE_ACCUM, NULL);
5467 if (ret)
5468 dev_err(&pf->pdev->dev,
5469 "Failed set tx rate (%llu Mbps) for vsi->seid %u, err %s aq_err %s\n",
5470 max_tx_rate, seid, i40e_stat_str(&pf->hw, ret),
5471 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
5472 return ret;
5473}
5474
5475/**
Amritha Nambiar8f88b302017-09-07 04:00:17 -07005476 * i40e_remove_queue_channels - Remove queue channels for the TCs
5477 * @vsi: VSI to be configured
5478 *
5479 * Remove queue channels for the TCs
5480 **/
5481static void i40e_remove_queue_channels(struct i40e_vsi *vsi)
5482{
5483 struct i40e_channel *ch, *ch_tmp;
5484 int ret, i;
5485
5486 /* Reset rss size that was stored when reconfiguring rss for
5487 * channel VSIs with non-power-of-2 queue count.
5488 */
5489 vsi->current_rss_size = 0;
5490
5491 /* perform cleanup for channels if they exist */
5492 if (list_empty(&vsi->ch_list))
5493 return;
5494
5495 list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
5496 struct i40e_vsi *p_vsi;
5497
5498 list_del(&ch->list);
5499 p_vsi = ch->parent_vsi;
5500 if (!p_vsi || !ch->initialized) {
5501 kfree(ch);
5502 continue;
5503 }
5504 /* Reset queue contexts */
5505 for (i = 0; i < ch->num_queue_pairs; i++) {
5506 struct i40e_ring *tx_ring, *rx_ring;
5507 u16 pf_q;
5508
5509 pf_q = ch->base_queue + i;
5510 tx_ring = vsi->tx_rings[pf_q];
5511 tx_ring->ch = NULL;
5512
5513 rx_ring = vsi->rx_rings[pf_q];
5514 rx_ring->ch = NULL;
5515 }
5516
Amritha Nambiar2027d4d2017-09-07 04:00:32 -07005517 /* Reset BW configured for this VSI via mqprio */
5518 ret = i40e_set_bw_limit(vsi, ch->seid, 0);
5519 if (ret)
5520 dev_info(&vsi->back->pdev->dev,
5521 "Failed to reset tx rate for ch->seid %u\n",
5522 ch->seid);
5523
Amritha Nambiar8f88b302017-09-07 04:00:17 -07005524 /* delete VSI from FW */
5525 ret = i40e_aq_delete_element(&vsi->back->hw, ch->seid,
5526 NULL);
5527 if (ret)
5528 dev_err(&vsi->back->pdev->dev,
5529 "unable to remove channel (%d) for parent VSI(%d)\n",
5530 ch->seid, p_vsi->seid);
5531 kfree(ch);
5532 }
5533 INIT_LIST_HEAD(&vsi->ch_list);
5534}
5535
5536/**
5537 * i40e_is_any_channel - channel exist or not
5538 * @vsi: ptr to VSI to which channels are associated with
5539 *
5540 * Returns true or false if channel(s) exist for associated VSI or not
5541 **/
5542static bool i40e_is_any_channel(struct i40e_vsi *vsi)
5543{
5544 struct i40e_channel *ch, *ch_tmp;
5545
5546 list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
5547 if (ch->initialized)
5548 return true;
5549 }
5550
5551 return false;
5552}
5553
5554/**
5555 * i40e_get_max_queues_for_channel
5556 * @vsi: ptr to VSI to which channels are associated with
5557 *
5558 * Helper function which returns max value among the queue counts set on the
5559 * channels/TCs created.
5560 **/
5561static int i40e_get_max_queues_for_channel(struct i40e_vsi *vsi)
5562{
5563 struct i40e_channel *ch, *ch_tmp;
5564 int max = 0;
5565
5566 list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
5567 if (!ch->initialized)
5568 continue;
5569 if (ch->num_queue_pairs > max)
5570 max = ch->num_queue_pairs;
5571 }
5572
5573 return max;
5574}
5575
5576/**
5577 * i40e_validate_num_queues - validate num_queues w.r.t channel
5578 * @pf: ptr to PF device
5579 * @num_queues: number of queues
5580 * @vsi: the parent VSI
5581 * @reconfig_rss: indicates should the RSS be reconfigured or not
5582 *
5583 * This function validates number of queues in the context of new channel
5584 * which is being established and determines if RSS should be reconfigured
5585 * or not for parent VSI.
5586 **/
5587static int i40e_validate_num_queues(struct i40e_pf *pf, int num_queues,
5588 struct i40e_vsi *vsi, bool *reconfig_rss)
5589{
5590 int max_ch_queues;
5591
5592 if (!reconfig_rss)
5593 return -EINVAL;
5594
5595 *reconfig_rss = false;
5596
5597 if (num_queues > I40E_MAX_QUEUES_PER_CH) {
5598 dev_err(&pf->pdev->dev,
5599 "Failed to create VMDq VSI. User requested num_queues (%d) > I40E_MAX_QUEUES_PER_VSI (%u)\n",
5600 num_queues, I40E_MAX_QUEUES_PER_CH);
5601 return -EINVAL;
5602 }
5603
5604 if (vsi->current_rss_size) {
5605 if (num_queues > vsi->current_rss_size) {
5606 dev_dbg(&pf->pdev->dev,
5607 "Error: num_queues (%d) > vsi's current_size(%d)\n",
5608 num_queues, vsi->current_rss_size);
5609 return -EINVAL;
5610 } else if ((num_queues < vsi->current_rss_size) &&
5611 (!is_power_of_2(num_queues))) {
5612 dev_dbg(&pf->pdev->dev,
5613 "Error: num_queues (%d) < vsi's current_size(%d), but not power of 2\n",
5614 num_queues, vsi->current_rss_size);
5615 return -EINVAL;
5616 }
5617 }
5618
5619 if (!is_power_of_2(num_queues)) {
5620 /* Find the max num_queues configured for channel if channel
5621 * exist.
5622 * if channel exist, then enforce 'num_queues' to be more than
5623 * max ever queues configured for channel.
5624 */
5625 max_ch_queues = i40e_get_max_queues_for_channel(vsi);
5626 if (num_queues < max_ch_queues) {
5627 dev_dbg(&pf->pdev->dev,
5628 "Error: num_queues (%d) < max queues configured for channel(%d)\n",
5629 num_queues, max_ch_queues);
5630 return -EINVAL;
5631 }
5632 *reconfig_rss = true;
5633 }
5634
5635 return 0;
5636}
5637
5638/**
5639 * i40e_vsi_reconfig_rss - reconfig RSS based on specified rss_size
5640 * @vsi: the VSI being setup
5641 * @rss_size: size of RSS, accordingly LUT gets reprogrammed
5642 *
5643 * This function reconfigures RSS by reprogramming LUTs using 'rss_size'
5644 **/
5645static int i40e_vsi_reconfig_rss(struct i40e_vsi *vsi, u16 rss_size)
5646{
5647 struct i40e_pf *pf = vsi->back;
5648 u8 seed[I40E_HKEY_ARRAY_SIZE];
5649 struct i40e_hw *hw = &pf->hw;
5650 int local_rss_size;
5651 u8 *lut;
5652 int ret;
5653
5654 if (!vsi->rss_size)
5655 return -EINVAL;
5656
5657 if (rss_size > vsi->rss_size)
5658 return -EINVAL;
5659
5660 local_rss_size = min_t(int, vsi->rss_size, rss_size);
5661 lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
5662 if (!lut)
5663 return -ENOMEM;
5664
5665 /* Ignoring user configured lut if there is one */
5666 i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, local_rss_size);
5667
5668 /* Use user configured hash key if there is one, otherwise
5669 * use default.
5670 */
5671 if (vsi->rss_hkey_user)
5672 memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
5673 else
5674 netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
5675
5676 ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
5677 if (ret) {
5678 dev_info(&pf->pdev->dev,
5679 "Cannot set RSS lut, err %s aq_err %s\n",
5680 i40e_stat_str(hw, ret),
5681 i40e_aq_str(hw, hw->aq.asq_last_status));
5682 kfree(lut);
5683 return ret;
5684 }
5685 kfree(lut);
5686
5687 /* Do the update w.r.t. storing rss_size */
5688 if (!vsi->orig_rss_size)
5689 vsi->orig_rss_size = vsi->rss_size;
5690 vsi->current_rss_size = local_rss_size;
5691
5692 return ret;
5693}
5694
5695/**
5696 * i40e_channel_setup_queue_map - Setup a channel queue map
5697 * @pf: ptr to PF device
5698 * @vsi: the VSI being setup
5699 * @ctxt: VSI context structure
5700 * @ch: ptr to channel structure
5701 *
5702 * Setup queue map for a specific channel
5703 **/
5704static void i40e_channel_setup_queue_map(struct i40e_pf *pf,
5705 struct i40e_vsi_context *ctxt,
5706 struct i40e_channel *ch)
5707{
5708 u16 qcount, qmap, sections = 0;
5709 u8 offset = 0;
5710 int pow;
5711
5712 sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
5713 sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
5714
5715 qcount = min_t(int, ch->num_queue_pairs, pf->num_lan_msix);
5716 ch->num_queue_pairs = qcount;
5717
5718 /* find the next higher power-of-2 of num queue pairs */
5719 pow = ilog2(qcount);
5720 if (!is_power_of_2(qcount))
5721 pow++;
5722
5723 qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
5724 (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
5725
5726 /* Setup queue TC[0].qmap for given VSI context */
5727 ctxt->info.tc_mapping[0] = cpu_to_le16(qmap);
5728
5729 ctxt->info.up_enable_bits = 0x1; /* TC0 enabled */
5730 ctxt->info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
5731 ctxt->info.queue_mapping[0] = cpu_to_le16(ch->base_queue);
5732 ctxt->info.valid_sections |= cpu_to_le16(sections);
5733}
5734
5735/**
5736 * i40e_add_channel - add a channel by adding VSI
5737 * @pf: ptr to PF device
5738 * @uplink_seid: underlying HW switching element (VEB) ID
5739 * @ch: ptr to channel structure
5740 *
5741 * Add a channel (VSI) using add_vsi and queue_map
5742 **/
5743static int i40e_add_channel(struct i40e_pf *pf, u16 uplink_seid,
5744 struct i40e_channel *ch)
5745{
5746 struct i40e_hw *hw = &pf->hw;
5747 struct i40e_vsi_context ctxt;
5748 u8 enabled_tc = 0x1; /* TC0 enabled */
5749 int ret;
5750
5751 if (ch->type != I40E_VSI_VMDQ2) {
5752 dev_info(&pf->pdev->dev,
5753 "add new vsi failed, ch->type %d\n", ch->type);
5754 return -EINVAL;
5755 }
5756
5757 memset(&ctxt, 0, sizeof(ctxt));
5758 ctxt.pf_num = hw->pf_id;
5759 ctxt.vf_num = 0;
5760 ctxt.uplink_seid = uplink_seid;
5761 ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
5762 if (ch->type == I40E_VSI_VMDQ2)
5763 ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
5764
5765 if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED) {
5766 ctxt.info.valid_sections |=
5767 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
5768 ctxt.info.switch_id =
5769 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
5770 }
5771
5772 /* Set queue map for a given VSI context */
5773 i40e_channel_setup_queue_map(pf, &ctxt, ch);
5774
5775 /* Now time to create VSI */
5776 ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
5777 if (ret) {
5778 dev_info(&pf->pdev->dev,
5779 "add new vsi failed, err %s aq_err %s\n",
5780 i40e_stat_str(&pf->hw, ret),
5781 i40e_aq_str(&pf->hw,
5782 pf->hw.aq.asq_last_status));
5783 return -ENOENT;
5784 }
5785
5786 /* Success, update channel */
5787 ch->enabled_tc = enabled_tc;
5788 ch->seid = ctxt.seid;
5789 ch->vsi_number = ctxt.vsi_number;
5790 ch->stat_counter_idx = cpu_to_le16(ctxt.info.stat_counter_idx);
5791
5792 /* copy just the sections touched not the entire info
5793 * since not all sections are valid as returned by
5794 * update vsi params
5795 */
5796 ch->info.mapping_flags = ctxt.info.mapping_flags;
5797 memcpy(&ch->info.queue_mapping,
5798 &ctxt.info.queue_mapping, sizeof(ctxt.info.queue_mapping));
5799 memcpy(&ch->info.tc_mapping, ctxt.info.tc_mapping,
5800 sizeof(ctxt.info.tc_mapping));
5801
5802 return 0;
5803}
5804
5805static int i40e_channel_config_bw(struct i40e_vsi *vsi, struct i40e_channel *ch,
5806 u8 *bw_share)
5807{
5808 struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
5809 i40e_status ret;
5810 int i;
5811
5812 bw_data.tc_valid_bits = ch->enabled_tc;
5813 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5814 bw_data.tc_bw_credits[i] = bw_share[i];
5815
5816 ret = i40e_aq_config_vsi_tc_bw(&vsi->back->hw, ch->seid,
5817 &bw_data, NULL);
5818 if (ret) {
5819 dev_info(&vsi->back->pdev->dev,
5820 "Config VSI BW allocation per TC failed, aq_err: %d for new_vsi->seid %u\n",
5821 vsi->back->hw.aq.asq_last_status, ch->seid);
5822 return -EINVAL;
5823 }
5824
5825 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5826 ch->info.qs_handle[i] = bw_data.qs_handles[i];
5827
5828 return 0;
5829}
5830
5831/**
5832 * i40e_channel_config_tx_ring - config TX ring associated with new channel
5833 * @pf: ptr to PF device
5834 * @vsi: the VSI being setup
5835 * @ch: ptr to channel structure
5836 *
5837 * Configure TX rings associated with channel (VSI) since queues are being
5838 * from parent VSI.
5839 **/
5840static int i40e_channel_config_tx_ring(struct i40e_pf *pf,
5841 struct i40e_vsi *vsi,
5842 struct i40e_channel *ch)
5843{
5844 i40e_status ret;
5845 int i;
5846 u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
5847
5848 /* Enable ETS TCs with equal BW Share for now across all VSIs */
5849 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5850 if (ch->enabled_tc & BIT(i))
5851 bw_share[i] = 1;
5852 }
5853
5854 /* configure BW for new VSI */
5855 ret = i40e_channel_config_bw(vsi, ch, bw_share);
5856 if (ret) {
5857 dev_info(&vsi->back->pdev->dev,
5858 "Failed configuring TC map %d for channel (seid %u)\n",
5859 ch->enabled_tc, ch->seid);
5860 return ret;
5861 }
5862
5863 for (i = 0; i < ch->num_queue_pairs; i++) {
5864 struct i40e_ring *tx_ring, *rx_ring;
5865 u16 pf_q;
5866
5867 pf_q = ch->base_queue + i;
5868
5869 /* Get to TX ring ptr of main VSI, for re-setup TX queue
5870 * context
5871 */
5872 tx_ring = vsi->tx_rings[pf_q];
5873 tx_ring->ch = ch;
5874
5875 /* Get the RX ring ptr */
5876 rx_ring = vsi->rx_rings[pf_q];
5877 rx_ring->ch = ch;
5878 }
5879
5880 return 0;
5881}
5882
5883/**
5884 * i40e_setup_hw_channel - setup new channel
5885 * @pf: ptr to PF device
5886 * @vsi: the VSI being setup
5887 * @ch: ptr to channel structure
5888 * @uplink_seid: underlying HW switching element (VEB) ID
5889 * @type: type of channel to be created (VMDq2/VF)
5890 *
5891 * Setup new channel (VSI) based on specified type (VMDq2/VF)
5892 * and configures TX rings accordingly
5893 **/
5894static inline int i40e_setup_hw_channel(struct i40e_pf *pf,
5895 struct i40e_vsi *vsi,
5896 struct i40e_channel *ch,
5897 u16 uplink_seid, u8 type)
5898{
5899 int ret;
5900
5901 ch->initialized = false;
5902 ch->base_queue = vsi->next_base_queue;
5903 ch->type = type;
5904
5905 /* Proceed with creation of channel (VMDq2) VSI */
5906 ret = i40e_add_channel(pf, uplink_seid, ch);
5907 if (ret) {
5908 dev_info(&pf->pdev->dev,
5909 "failed to add_channel using uplink_seid %u\n",
5910 uplink_seid);
5911 return ret;
5912 }
5913
5914 /* Mark the successful creation of channel */
5915 ch->initialized = true;
5916
5917 /* Reconfigure TX queues using QTX_CTL register */
5918 ret = i40e_channel_config_tx_ring(pf, vsi, ch);
5919 if (ret) {
5920 dev_info(&pf->pdev->dev,
5921 "failed to configure TX rings for channel %u\n",
5922 ch->seid);
5923 return ret;
5924 }
5925
5926 /* update 'next_base_queue' */
5927 vsi->next_base_queue = vsi->next_base_queue + ch->num_queue_pairs;
5928 dev_dbg(&pf->pdev->dev,
5929 "Added channel: vsi_seid %u, vsi_number %u, stat_counter_idx %u, num_queue_pairs %u, pf->next_base_queue %d\n",
5930 ch->seid, ch->vsi_number, ch->stat_counter_idx,
5931 ch->num_queue_pairs,
5932 vsi->next_base_queue);
5933 return ret;
5934}
5935
5936/**
5937 * i40e_setup_channel - setup new channel using uplink element
5938 * @pf: ptr to PF device
5939 * @type: type of channel to be created (VMDq2/VF)
5940 * @uplink_seid: underlying HW switching element (VEB) ID
5941 * @ch: ptr to channel structure
5942 *
5943 * Setup new channel (VSI) based on specified type (VMDq2/VF)
5944 * and uplink switching element (uplink_seid)
5945 **/
5946static bool i40e_setup_channel(struct i40e_pf *pf, struct i40e_vsi *vsi,
5947 struct i40e_channel *ch)
5948{
5949 u8 vsi_type;
5950 u16 seid;
5951 int ret;
5952
5953 if (vsi->type == I40E_VSI_MAIN) {
5954 vsi_type = I40E_VSI_VMDQ2;
5955 } else {
5956 dev_err(&pf->pdev->dev, "unsupported parent vsi type(%d)\n",
5957 vsi->type);
5958 return false;
5959 }
5960
5961 /* underlying switching element */
5962 seid = pf->vsi[pf->lan_vsi]->uplink_seid;
5963
5964 /* create channel (VSI), configure TX rings */
5965 ret = i40e_setup_hw_channel(pf, vsi, ch, seid, vsi_type);
5966 if (ret) {
5967 dev_err(&pf->pdev->dev, "failed to setup hw_channel\n");
5968 return false;
5969 }
5970
5971 return ch->initialized ? true : false;
5972}
5973
5974/**
5975 * i40e_create_queue_channel - function to create channel
5976 * @vsi: VSI to be configured
5977 * @ch: ptr to channel (it contains channel specific params)
5978 *
5979 * This function creates channel (VSI) using num_queues specified by user,
5980 * reconfigs RSS if needed.
5981 **/
5982int i40e_create_queue_channel(struct i40e_vsi *vsi,
5983 struct i40e_channel *ch)
5984{
5985 struct i40e_pf *pf = vsi->back;
5986 bool reconfig_rss;
5987 int err;
5988
5989 if (!ch)
5990 return -EINVAL;
5991
5992 if (!ch->num_queue_pairs) {
5993 dev_err(&pf->pdev->dev, "Invalid num_queues requested: %d\n",
5994 ch->num_queue_pairs);
5995 return -EINVAL;
5996 }
5997
5998 /* validate user requested num_queues for channel */
5999 err = i40e_validate_num_queues(pf, ch->num_queue_pairs, vsi,
6000 &reconfig_rss);
6001 if (err) {
6002 dev_info(&pf->pdev->dev, "Failed to validate num_queues (%d)\n",
6003 ch->num_queue_pairs);
6004 return -EINVAL;
6005 }
6006
6007 /* By default we are in VEPA mode, if this is the first VF/VMDq
6008 * VSI to be added switch to VEB mode.
6009 */
6010 if ((!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) ||
6011 (!i40e_is_any_channel(vsi))) {
6012 if (!is_power_of_2(vsi->tc_config.tc_info[0].qcount)) {
6013 dev_dbg(&pf->pdev->dev,
6014 "Failed to create channel. Override queues (%u) not power of 2\n",
6015 vsi->tc_config.tc_info[0].qcount);
6016 return -EINVAL;
6017 }
6018
6019 if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
6020 pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
6021
6022 if (vsi->type == I40E_VSI_MAIN) {
6023 if (pf->flags & I40E_FLAG_TC_MQPRIO)
6024 i40e_do_reset(pf, I40E_PF_RESET_FLAG,
6025 true);
6026 else
6027 i40e_do_reset_safe(pf,
6028 I40E_PF_RESET_FLAG);
6029 }
6030 }
6031 /* now onwards for main VSI, number of queues will be value
6032 * of TC0's queue count
6033 */
6034 }
6035
6036 /* By this time, vsi->cnt_q_avail shall be set to non-zero and
6037 * it should be more than num_queues
6038 */
6039 if (!vsi->cnt_q_avail || vsi->cnt_q_avail < ch->num_queue_pairs) {
6040 dev_dbg(&pf->pdev->dev,
6041 "Error: cnt_q_avail (%u) less than num_queues %d\n",
6042 vsi->cnt_q_avail, ch->num_queue_pairs);
6043 return -EINVAL;
6044 }
6045
6046 /* reconfig_rss only if vsi type is MAIN_VSI */
6047 if (reconfig_rss && (vsi->type == I40E_VSI_MAIN)) {
6048 err = i40e_vsi_reconfig_rss(vsi, ch->num_queue_pairs);
6049 if (err) {
6050 dev_info(&pf->pdev->dev,
6051 "Error: unable to reconfig rss for num_queues (%u)\n",
6052 ch->num_queue_pairs);
6053 return -EINVAL;
6054 }
6055 }
6056
6057 if (!i40e_setup_channel(pf, vsi, ch)) {
6058 dev_info(&pf->pdev->dev, "Failed to setup channel\n");
6059 return -EINVAL;
6060 }
6061
6062 dev_info(&pf->pdev->dev,
6063 "Setup channel (id:%u) utilizing num_queues %d\n",
6064 ch->seid, ch->num_queue_pairs);
6065
Amritha Nambiar2027d4d2017-09-07 04:00:32 -07006066 /* configure VSI for BW limit */
6067 if (ch->max_tx_rate) {
Alan Brady6c32e0d2017-10-09 15:48:45 -07006068 u64 credits = ch->max_tx_rate;
6069
Amritha Nambiar2027d4d2017-09-07 04:00:32 -07006070 if (i40e_set_bw_limit(vsi, ch->seid, ch->max_tx_rate))
6071 return -EINVAL;
6072
Alan Brady6c32e0d2017-10-09 15:48:45 -07006073 do_div(credits, I40E_BW_CREDIT_DIVISOR);
Amritha Nambiar2027d4d2017-09-07 04:00:32 -07006074 dev_dbg(&pf->pdev->dev,
6075 "Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
6076 ch->max_tx_rate,
Alan Brady6c32e0d2017-10-09 15:48:45 -07006077 credits,
6078 ch->seid);
Amritha Nambiar2027d4d2017-09-07 04:00:32 -07006079 }
6080
Amritha Nambiar8f88b302017-09-07 04:00:17 -07006081 /* in case of VF, this will be main SRIOV VSI */
6082 ch->parent_vsi = vsi;
6083
6084 /* and update main_vsi's count for queue_available to use */
6085 vsi->cnt_q_avail -= ch->num_queue_pairs;
6086
6087 return 0;
6088}
6089
6090/**
6091 * i40e_configure_queue_channels - Add queue channel for the given TCs
6092 * @vsi: VSI to be configured
6093 *
6094 * Configures queue channel mapping to the given TCs
6095 **/
6096static int i40e_configure_queue_channels(struct i40e_vsi *vsi)
6097{
6098 struct i40e_channel *ch;
Alan Brady6c32e0d2017-10-09 15:48:45 -07006099 u64 max_rate = 0;
Amritha Nambiar8f88b302017-09-07 04:00:17 -07006100 int ret = 0, i;
6101
6102 /* Create app vsi with the TCs. Main VSI with TC0 is already set up */
Amritha Nambiaraa5cb02a2017-10-27 02:35:40 -07006103 vsi->tc_seid_map[0] = vsi->seid;
Amritha Nambiar8f88b302017-09-07 04:00:17 -07006104 for (i = 1; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6105 if (vsi->tc_config.enabled_tc & BIT(i)) {
6106 ch = kzalloc(sizeof(*ch), GFP_KERNEL);
6107 if (!ch) {
6108 ret = -ENOMEM;
6109 goto err_free;
6110 }
6111
6112 INIT_LIST_HEAD(&ch->list);
6113 ch->num_queue_pairs =
6114 vsi->tc_config.tc_info[i].qcount;
6115 ch->base_queue =
6116 vsi->tc_config.tc_info[i].qoffset;
6117
Amritha Nambiar2027d4d2017-09-07 04:00:32 -07006118 /* Bandwidth limit through tc interface is in bytes/s,
6119 * change to Mbit/s
6120 */
Alan Brady6c32e0d2017-10-09 15:48:45 -07006121 max_rate = vsi->mqprio_qopt.max_rate[i];
6122 do_div(max_rate, I40E_BW_MBPS_DIVISOR);
6123 ch->max_tx_rate = max_rate;
Amritha Nambiar2027d4d2017-09-07 04:00:32 -07006124
Amritha Nambiar8f88b302017-09-07 04:00:17 -07006125 list_add_tail(&ch->list, &vsi->ch_list);
6126
6127 ret = i40e_create_queue_channel(vsi, ch);
6128 if (ret) {
6129 dev_err(&vsi->back->pdev->dev,
6130 "Failed creating queue channel with TC%d: queues %d\n",
6131 i, ch->num_queue_pairs);
6132 goto err_free;
6133 }
Amritha Nambiaraa5cb02a2017-10-27 02:35:40 -07006134 vsi->tc_seid_map[i] = ch->seid;
Amritha Nambiar8f88b302017-09-07 04:00:17 -07006135 }
6136 }
6137 return ret;
6138
6139err_free:
6140 i40e_remove_queue_channels(vsi);
6141 return ret;
6142}
6143
6144/**
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006145 * i40e_veb_config_tc - Configure TCs for given VEB
6146 * @veb: given VEB
6147 * @enabled_tc: TC bitmap
6148 *
6149 * Configures given TC bitmap for VEB (switching) element
6150 **/
6151int i40e_veb_config_tc(struct i40e_veb *veb, u8 enabled_tc)
6152{
6153 struct i40e_aqc_configure_switching_comp_bw_config_data bw_data = {0};
6154 struct i40e_pf *pf = veb->pf;
6155 int ret = 0;
6156 int i;
6157
6158 /* No TCs or already enabled TCs just return */
6159 if (!enabled_tc || veb->enabled_tc == enabled_tc)
6160 return ret;
6161
6162 bw_data.tc_valid_bits = enabled_tc;
6163 /* bw_data.absolute_credits is not set (relative) */
6164
6165 /* Enable ETS TCs with equal BW Share for now */
6166 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006167 if (enabled_tc & BIT(i))
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006168 bw_data.tc_bw_share_credits[i] = 1;
6169 }
6170
6171 ret = i40e_aq_config_switch_comp_bw_config(&pf->hw, veb->seid,
6172 &bw_data, NULL);
6173 if (ret) {
6174 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006175 "VEB bw config failed, err %s aq_err %s\n",
6176 i40e_stat_str(&pf->hw, ret),
6177 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006178 goto out;
6179 }
6180
6181 /* Update the BW information */
6182 ret = i40e_veb_get_bw_info(veb);
6183 if (ret) {
6184 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006185 "Failed getting veb bw config, err %s aq_err %s\n",
6186 i40e_stat_str(&pf->hw, ret),
6187 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006188 }
6189
6190out:
6191 return ret;
6192}
6193
6194#ifdef CONFIG_I40E_DCB
6195/**
6196 * i40e_dcb_reconfigure - Reconfigure all VEBs and VSIs
6197 * @pf: PF struct
6198 *
6199 * Reconfigure VEB/VSIs on a given PF; it is assumed that
6200 * the caller would've quiesce all the VSIs before calling
6201 * this function
6202 **/
6203static void i40e_dcb_reconfigure(struct i40e_pf *pf)
6204{
6205 u8 tc_map = 0;
6206 int ret;
6207 u8 v;
6208
6209 /* Enable the TCs available on PF to all VEBs */
6210 tc_map = i40e_pf_get_tc_map(pf);
6211 for (v = 0; v < I40E_MAX_VEB; v++) {
6212 if (!pf->veb[v])
6213 continue;
6214 ret = i40e_veb_config_tc(pf->veb[v], tc_map);
6215 if (ret) {
6216 dev_info(&pf->pdev->dev,
6217 "Failed configuring TC for VEB seid=%d\n",
6218 pf->veb[v]->seid);
6219 /* Will try to configure as many components */
6220 }
6221 }
6222
6223 /* Update each VSI */
Mitch Williams505682c2014-05-20 08:01:37 +00006224 for (v = 0; v < pf->num_alloc_vsi; v++) {
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006225 if (!pf->vsi[v])
6226 continue;
6227
6228 /* - Enable all TCs for the LAN VSI
6229 * - For all others keep them at TC0 for now
6230 */
6231 if (v == pf->lan_vsi)
6232 tc_map = i40e_pf_get_tc_map(pf);
6233 else
David Ertmanea6acb72016-09-20 07:10:50 -07006234 tc_map = I40E_DEFAULT_TRAFFIC_CLASS;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006235
6236 ret = i40e_vsi_config_tc(pf->vsi[v], tc_map);
6237 if (ret) {
6238 dev_info(&pf->pdev->dev,
6239 "Failed configuring TC for VSI seid=%d\n",
6240 pf->vsi[v]->seid);
6241 /* Will try to configure as many components */
6242 } else {
Neerav Parikh0672a092014-04-01 07:11:47 +00006243 /* Re-configure VSI vectors based on updated TC map */
6244 i40e_vsi_map_rings_to_vectors(pf->vsi[v]);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006245 if (pf->vsi[v]->netdev)
6246 i40e_dcbnl_set_all(pf->vsi[v]);
6247 }
6248 }
6249}
6250
6251/**
Neerav Parikh2fd75f32014-11-12 00:18:20 +00006252 * i40e_resume_port_tx - Resume port Tx
6253 * @pf: PF struct
6254 *
6255 * Resume a port's Tx and issue a PF reset in case of failure to
6256 * resume.
6257 **/
6258static int i40e_resume_port_tx(struct i40e_pf *pf)
6259{
6260 struct i40e_hw *hw = &pf->hw;
6261 int ret;
6262
6263 ret = i40e_aq_resume_port_tx(hw, NULL);
6264 if (ret) {
6265 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006266 "Resume Port Tx failed, err %s aq_err %s\n",
6267 i40e_stat_str(&pf->hw, ret),
6268 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh2fd75f32014-11-12 00:18:20 +00006269 /* Schedule PF reset to recover */
Jacob Keller0da36b92017-04-19 09:25:55 -04006270 set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
Neerav Parikh2fd75f32014-11-12 00:18:20 +00006271 i40e_service_event_schedule(pf);
6272 }
6273
6274 return ret;
6275}
6276
6277/**
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006278 * i40e_init_pf_dcb - Initialize DCB configuration
6279 * @pf: PF being configured
6280 *
6281 * Query the current DCB configuration and cache it
6282 * in the hardware structure
6283 **/
6284static int i40e_init_pf_dcb(struct i40e_pf *pf)
6285{
6286 struct i40e_hw *hw = &pf->hw;
6287 int err = 0;
6288
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +00006289 /* Do not enable DCB for SW1 and SW2 images even if the FW is capable */
Jacob Kellerd36e41d2017-06-23 04:24:46 -04006290 if (pf->hw_features & I40E_HW_NO_DCB_SUPPORT)
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +00006291 goto out;
6292
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006293 /* Get the initial DCB configuration */
6294 err = i40e_init_dcb(hw);
6295 if (!err) {
6296 /* Device/Function is not DCBX capable */
6297 if ((!hw->func_caps.dcb) ||
6298 (hw->dcbx_status == I40E_DCBX_STATUS_DISABLED)) {
6299 dev_info(&pf->pdev->dev,
6300 "DCBX offload is not supported or is disabled for this PF.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006301 } else {
6302 /* When status is not DISABLED then DCBX in FW */
6303 pf->dcbx_cap = DCB_CAP_DCBX_LLD_MANAGED |
6304 DCB_CAP_DCBX_VER_IEEE;
Neerav Parikh4d9b6042014-05-22 06:31:51 +00006305
6306 pf->flags |= I40E_FLAG_DCB_CAPABLE;
Dave Ertmana0362442016-08-29 17:38:26 -07006307 /* Enable DCB tagging only when more than one TC
6308 * or explicitly disable if only one TC
6309 */
Neerav Parikh4d9b6042014-05-22 06:31:51 +00006310 if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
6311 pf->flags |= I40E_FLAG_DCB_ENABLED;
Dave Ertmana0362442016-08-29 17:38:26 -07006312 else
6313 pf->flags &= ~I40E_FLAG_DCB_ENABLED;
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00006314 dev_dbg(&pf->pdev->dev,
6315 "DCBX offload is supported for this PF.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006316 }
Neerav Parikh014269f2014-04-01 07:11:48 +00006317 } else {
Shannon Nelsonaebfc812014-12-11 07:06:38 +00006318 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006319 "Query for DCB configuration failed, err %s aq_err %s\n",
6320 i40e_stat_str(&pf->hw, err),
6321 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006322 }
6323
6324out:
6325 return err;
6326}
6327#endif /* CONFIG_I40E_DCB */
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00006328#define SPEED_SIZE 14
6329#define FC_SIZE 8
6330/**
6331 * i40e_print_link_message - print link up or down
6332 * @vsi: the VSI for which link needs a message
6333 */
Matt Jaredc156f852015-08-27 11:42:39 -04006334void i40e_print_link_message(struct i40e_vsi *vsi, bool isup)
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00006335{
Filip Sadowski7ec9ba12016-11-08 13:05:13 -08006336 enum i40e_aq_link_speed new_speed;
Sudheer Mogilappagari3fded462017-07-14 09:10:18 -04006337 struct i40e_pf *pf = vsi->back;
Shannon Nelsona9165492015-09-03 17:19:00 -04006338 char *speed = "Unknown";
6339 char *fc = "Unknown";
Henry Tieman3e03d7c2016-12-02 12:32:57 -08006340 char *fec = "";
Mariusz Stachura68e49702017-07-12 05:46:14 -04006341 char *req_fec = "";
Henry Tieman3e03d7c2016-12-02 12:32:57 -08006342 char *an = "";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00006343
Sudheer Mogilappagari3fded462017-07-14 09:10:18 -04006344 new_speed = pf->hw.phy.link_info.link_speed;
Filip Sadowski7ec9ba12016-11-08 13:05:13 -08006345
6346 if ((vsi->current_isup == isup) && (vsi->current_speed == new_speed))
Matt Jaredc156f852015-08-27 11:42:39 -04006347 return;
6348 vsi->current_isup = isup;
Filip Sadowski7ec9ba12016-11-08 13:05:13 -08006349 vsi->current_speed = new_speed;
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00006350 if (!isup) {
6351 netdev_info(vsi->netdev, "NIC Link is Down\n");
6352 return;
6353 }
6354
Greg Rose148c2d82014-12-11 07:06:27 +00006355 /* Warn user if link speed on NPAR enabled partition is not at
6356 * least 10GB
6357 */
Sudheer Mogilappagari3fded462017-07-14 09:10:18 -04006358 if (pf->hw.func_caps.npar_enable &&
6359 (pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_1GB ||
6360 pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_100MB))
Greg Rose148c2d82014-12-11 07:06:27 +00006361 netdev_warn(vsi->netdev,
6362 "The partition detected link speed that is less than 10Gbps\n");
6363
Sudheer Mogilappagari3fded462017-07-14 09:10:18 -04006364 switch (pf->hw.phy.link_info.link_speed) {
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00006365 case I40E_LINK_SPEED_40GB:
Shannon Nelsona9165492015-09-03 17:19:00 -04006366 speed = "40 G";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00006367 break;
Jesse Brandeburgae24b402015-03-27 00:12:09 -07006368 case I40E_LINK_SPEED_20GB:
Shannon Nelsona9165492015-09-03 17:19:00 -04006369 speed = "20 G";
Jesse Brandeburgae24b402015-03-27 00:12:09 -07006370 break;
Carolyn Wyborny31232372016-11-21 13:03:48 -08006371 case I40E_LINK_SPEED_25GB:
6372 speed = "25 G";
6373 break;
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00006374 case I40E_LINK_SPEED_10GB:
Shannon Nelsona9165492015-09-03 17:19:00 -04006375 speed = "10 G";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00006376 break;
6377 case I40E_LINK_SPEED_1GB:
Shannon Nelsona9165492015-09-03 17:19:00 -04006378 speed = "1000 M";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00006379 break;
Mitch Williams5960d332014-09-13 07:40:47 +00006380 case I40E_LINK_SPEED_100MB:
Shannon Nelsona9165492015-09-03 17:19:00 -04006381 speed = "100 M";
Mitch Williams5960d332014-09-13 07:40:47 +00006382 break;
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00006383 default:
6384 break;
6385 }
6386
Sudheer Mogilappagari3fded462017-07-14 09:10:18 -04006387 switch (pf->hw.fc.current_mode) {
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00006388 case I40E_FC_FULL:
Shannon Nelsona9165492015-09-03 17:19:00 -04006389 fc = "RX/TX";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00006390 break;
6391 case I40E_FC_TX_PAUSE:
Shannon Nelsona9165492015-09-03 17:19:00 -04006392 fc = "TX";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00006393 break;
6394 case I40E_FC_RX_PAUSE:
Shannon Nelsona9165492015-09-03 17:19:00 -04006395 fc = "RX";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00006396 break;
6397 default:
Shannon Nelsona9165492015-09-03 17:19:00 -04006398 fc = "None";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00006399 break;
6400 }
6401
Sudheer Mogilappagari3fded462017-07-14 09:10:18 -04006402 if (pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_25GB) {
Mariusz Stachura68e49702017-07-12 05:46:14 -04006403 req_fec = ", Requested FEC: None";
Henry Tieman3e03d7c2016-12-02 12:32:57 -08006404 fec = ", FEC: None";
6405 an = ", Autoneg: False";
6406
Sudheer Mogilappagari3fded462017-07-14 09:10:18 -04006407 if (pf->hw.phy.link_info.an_info & I40E_AQ_AN_COMPLETED)
Henry Tieman3e03d7c2016-12-02 12:32:57 -08006408 an = ", Autoneg: True";
6409
Sudheer Mogilappagari3fded462017-07-14 09:10:18 -04006410 if (pf->hw.phy.link_info.fec_info &
Henry Tieman3e03d7c2016-12-02 12:32:57 -08006411 I40E_AQ_CONFIG_FEC_KR_ENA)
6412 fec = ", FEC: CL74 FC-FEC/BASE-R";
Sudheer Mogilappagari3fded462017-07-14 09:10:18 -04006413 else if (pf->hw.phy.link_info.fec_info &
Henry Tieman3e03d7c2016-12-02 12:32:57 -08006414 I40E_AQ_CONFIG_FEC_RS_ENA)
6415 fec = ", FEC: CL108 RS-FEC";
Mariusz Stachura68e49702017-07-12 05:46:14 -04006416
6417 /* 'CL108 RS-FEC' should be displayed when RS is requested, or
6418 * both RS and FC are requested
6419 */
6420 if (vsi->back->hw.phy.link_info.req_fec_info &
6421 (I40E_AQ_REQUEST_FEC_KR | I40E_AQ_REQUEST_FEC_RS)) {
6422 if (vsi->back->hw.phy.link_info.req_fec_info &
6423 I40E_AQ_REQUEST_FEC_RS)
6424 req_fec = ", Requested FEC: CL108 RS-FEC";
6425 else
6426 req_fec = ", Requested FEC: CL74 FC-FEC/BASE-R";
6427 }
Henry Tieman3e03d7c2016-12-02 12:32:57 -08006428 }
6429
Mariusz Stachura68e49702017-07-12 05:46:14 -04006430 netdev_info(vsi->netdev, "NIC Link is Up, %sbps Full Duplex%s%s%s, Flow Control: %s\n",
6431 speed, req_fec, fec, an, fc);
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00006432}
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006433
6434/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006435 * i40e_up_complete - Finish the last steps of bringing up a connection
6436 * @vsi: the VSI being configured
6437 **/
6438static int i40e_up_complete(struct i40e_vsi *vsi)
6439{
6440 struct i40e_pf *pf = vsi->back;
6441 int err;
6442
6443 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
6444 i40e_vsi_configure_msix(vsi);
6445 else
6446 i40e_configure_msi_and_legacy(vsi);
6447
6448 /* start rings */
Filip Sadowski3aa7b742016-10-11 15:26:58 -07006449 err = i40e_vsi_start_rings(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006450 if (err)
6451 return err;
6452
Jacob Keller0da36b92017-04-19 09:25:55 -04006453 clear_bit(__I40E_VSI_DOWN, vsi->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006454 i40e_napi_enable_all(vsi);
6455 i40e_vsi_enable_irq(vsi);
6456
6457 if ((pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP) &&
6458 (vsi->netdev)) {
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00006459 i40e_print_link_message(vsi, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006460 netif_tx_start_all_queues(vsi->netdev);
6461 netif_carrier_on(vsi->netdev);
6462 }
Anjali Singhai Jainca64fa42014-02-11 08:26:30 +00006463
6464 /* replay FDIR SB filters */
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006465 if (vsi->type == I40E_VSI_FDIR) {
6466 /* reset fd counters */
Jacob Keller097dbf52017-02-06 14:38:46 -08006467 pf->fd_add_err = 0;
6468 pf->fd_atr_cnt = 0;
Anjali Singhai Jainca64fa42014-02-11 08:26:30 +00006469 i40e_fdir_filter_restore(vsi);
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006470 }
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06006471
6472 /* On the next run of the service_task, notify any clients of the new
6473 * opened netdev
6474 */
6475 pf->flags |= I40E_FLAG_SERVICE_CLIENT_REQUESTED;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006476 i40e_service_event_schedule(pf);
6477
6478 return 0;
6479}
6480
6481/**
6482 * i40e_vsi_reinit_locked - Reset the VSI
6483 * @vsi: the VSI being configured
6484 *
6485 * Rebuild the ring structs after some configuration
6486 * has changed, e.g. MTU size.
6487 **/
6488static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi)
6489{
6490 struct i40e_pf *pf = vsi->back;
6491
6492 WARN_ON(in_interrupt());
Jacob Keller0da36b92017-04-19 09:25:55 -04006493 while (test_and_set_bit(__I40E_CONFIG_BUSY, pf->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006494 usleep_range(1000, 2000);
6495 i40e_down(vsi);
6496
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006497 i40e_up(vsi);
Jacob Keller0da36b92017-04-19 09:25:55 -04006498 clear_bit(__I40E_CONFIG_BUSY, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006499}
6500
6501/**
6502 * i40e_up - Bring the connection back up after being down
6503 * @vsi: the VSI being configured
6504 **/
6505int i40e_up(struct i40e_vsi *vsi)
6506{
6507 int err;
6508
6509 err = i40e_vsi_configure(vsi);
6510 if (!err)
6511 err = i40e_up_complete(vsi);
6512
6513 return err;
6514}
6515
6516/**
6517 * i40e_down - Shutdown the connection processing
6518 * @vsi: the VSI being stopped
6519 **/
6520void i40e_down(struct i40e_vsi *vsi)
6521{
6522 int i;
6523
6524 /* It is assumed that the caller of this function
Jacob Kellerd19cb642017-04-21 13:38:05 -07006525 * sets the vsi->state __I40E_VSI_DOWN bit.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006526 */
6527 if (vsi->netdev) {
6528 netif_carrier_off(vsi->netdev);
6529 netif_tx_disable(vsi->netdev);
6530 }
6531 i40e_vsi_disable_irq(vsi);
Filip Sadowski3aa7b742016-10-11 15:26:58 -07006532 i40e_vsi_stop_rings(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006533 i40e_napi_disable_all(vsi);
6534
6535 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck9f65e152013-09-28 06:00:58 +00006536 i40e_clean_tx_ring(vsi->tx_rings[i]);
Björn Töpel74608d12017-05-24 07:55:35 +02006537 if (i40e_enabled_xdp_vsi(vsi))
6538 i40e_clean_tx_ring(vsi->xdp_rings[i]);
Alexander Duyck9f65e152013-09-28 06:00:58 +00006539 i40e_clean_rx_ring(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006540 }
Catherine Sullivanf980d442016-05-16 10:26:34 -07006541
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006542}
6543
6544/**
Amritha Nambiara9ce82f2017-09-07 04:00:22 -07006545 * i40e_validate_mqprio_qopt- validate queue mapping info
6546 * @vsi: the VSI being configured
6547 * @mqprio_qopt: queue parametrs
6548 **/
6549static int i40e_validate_mqprio_qopt(struct i40e_vsi *vsi,
6550 struct tc_mqprio_qopt_offload *mqprio_qopt)
6551{
Amritha Nambiar2027d4d2017-09-07 04:00:32 -07006552 u64 sum_max_rate = 0;
Alan Brady6c32e0d2017-10-09 15:48:45 -07006553 u64 max_rate = 0;
Amritha Nambiara9ce82f2017-09-07 04:00:22 -07006554 int i;
6555
6556 if (mqprio_qopt->qopt.offset[0] != 0 ||
6557 mqprio_qopt->qopt.num_tc < 1 ||
6558 mqprio_qopt->qopt.num_tc > I40E_MAX_TRAFFIC_CLASS)
6559 return -EINVAL;
6560 for (i = 0; ; i++) {
6561 if (!mqprio_qopt->qopt.count[i])
6562 return -EINVAL;
Amritha Nambiar2027d4d2017-09-07 04:00:32 -07006563 if (mqprio_qopt->min_rate[i]) {
6564 dev_err(&vsi->back->pdev->dev,
6565 "Invalid min tx rate (greater than 0) specified\n");
Amritha Nambiara9ce82f2017-09-07 04:00:22 -07006566 return -EINVAL;
Amritha Nambiar2027d4d2017-09-07 04:00:32 -07006567 }
Alan Brady6c32e0d2017-10-09 15:48:45 -07006568 max_rate = mqprio_qopt->max_rate[i];
6569 do_div(max_rate, I40E_BW_MBPS_DIVISOR);
6570 sum_max_rate += max_rate;
Amritha Nambiar2027d4d2017-09-07 04:00:32 -07006571
Amritha Nambiara9ce82f2017-09-07 04:00:22 -07006572 if (i >= mqprio_qopt->qopt.num_tc - 1)
6573 break;
6574 if (mqprio_qopt->qopt.offset[i + 1] !=
6575 (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i]))
6576 return -EINVAL;
6577 }
6578 if (vsi->num_queue_pairs <
6579 (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i])) {
6580 return -EINVAL;
6581 }
Amritha Nambiar2027d4d2017-09-07 04:00:32 -07006582 if (sum_max_rate > i40e_get_link_speed(vsi)) {
6583 dev_err(&vsi->back->pdev->dev,
6584 "Invalid max tx rate specified\n");
6585 return -EINVAL;
6586 }
Amritha Nambiara9ce82f2017-09-07 04:00:22 -07006587 return 0;
6588}
6589
6590/**
6591 * i40e_vsi_set_default_tc_config - set default values for tc configuration
6592 * @vsi: the VSI being configured
6593 **/
6594static void i40e_vsi_set_default_tc_config(struct i40e_vsi *vsi)
6595{
6596 u16 qcount;
6597 int i;
6598
6599 /* Only TC0 is enabled */
6600 vsi->tc_config.numtc = 1;
6601 vsi->tc_config.enabled_tc = 1;
6602 qcount = min_t(int, vsi->alloc_queue_pairs,
6603 i40e_pf_get_max_q_per_tc(vsi->back));
6604 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6605 /* For the TC that is not enabled set the offset to to default
6606 * queue and allocate one queue for the given TC.
6607 */
6608 vsi->tc_config.tc_info[i].qoffset = 0;
6609 if (i == 0)
6610 vsi->tc_config.tc_info[i].qcount = qcount;
6611 else
6612 vsi->tc_config.tc_info[i].qcount = 1;
6613 vsi->tc_config.tc_info[i].netdev_tc = 0;
6614 }
6615}
6616
6617/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006618 * i40e_setup_tc - configure multiple traffic classes
6619 * @netdev: net device to configure
Amritha Nambiara9ce82f2017-09-07 04:00:22 -07006620 * @type_data: tc offload data
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006621 **/
Amritha Nambiara9ce82f2017-09-07 04:00:22 -07006622static int i40e_setup_tc(struct net_device *netdev, void *type_data)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006623{
Amritha Nambiara9ce82f2017-09-07 04:00:22 -07006624 struct tc_mqprio_qopt_offload *mqprio_qopt = type_data;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006625 struct i40e_netdev_priv *np = netdev_priv(netdev);
6626 struct i40e_vsi *vsi = np->vsi;
6627 struct i40e_pf *pf = vsi->back;
Amritha Nambiara9ce82f2017-09-07 04:00:22 -07006628 u8 enabled_tc = 0, num_tc, hw;
6629 bool need_reset = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006630 int ret = -EINVAL;
Amritha Nambiara9ce82f2017-09-07 04:00:22 -07006631 u16 mode;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006632 int i;
6633
Amritha Nambiara9ce82f2017-09-07 04:00:22 -07006634 num_tc = mqprio_qopt->qopt.num_tc;
6635 hw = mqprio_qopt->qopt.hw;
6636 mode = mqprio_qopt->mode;
6637 if (!hw) {
6638 pf->flags &= ~I40E_FLAG_TC_MQPRIO;
6639 memcpy(&vsi->mqprio_qopt, mqprio_qopt, sizeof(*mqprio_qopt));
6640 goto config_tc;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006641 }
6642
6643 /* Check if MFP enabled */
6644 if (pf->flags & I40E_FLAG_MFP_ENABLED) {
Amritha Nambiara9ce82f2017-09-07 04:00:22 -07006645 netdev_info(netdev,
6646 "Configuring TC not supported in MFP mode\n");
6647 return ret;
6648 }
6649 switch (mode) {
6650 case TC_MQPRIO_MODE_DCB:
6651 pf->flags &= ~I40E_FLAG_TC_MQPRIO;
6652
6653 /* Check if DCB enabled to continue */
6654 if (!(pf->flags & I40E_FLAG_DCB_ENABLED)) {
6655 netdev_info(netdev,
6656 "DCB is not enabled for adapter\n");
6657 return ret;
6658 }
6659
6660 /* Check whether tc count is within enabled limit */
6661 if (num_tc > i40e_pf_get_num_tc(pf)) {
6662 netdev_info(netdev,
6663 "TC count greater than enabled on link for adapter\n");
6664 return ret;
6665 }
6666 break;
6667 case TC_MQPRIO_MODE_CHANNEL:
6668 if (pf->flags & I40E_FLAG_DCB_ENABLED) {
6669 netdev_info(netdev,
6670 "Full offload of TC Mqprio options is not supported when DCB is enabled\n");
6671 return ret;
6672 }
6673 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
6674 return ret;
6675 ret = i40e_validate_mqprio_qopt(vsi, mqprio_qopt);
6676 if (ret)
6677 return ret;
6678 memcpy(&vsi->mqprio_qopt, mqprio_qopt,
6679 sizeof(*mqprio_qopt));
6680 pf->flags |= I40E_FLAG_TC_MQPRIO;
6681 pf->flags &= ~I40E_FLAG_DCB_ENABLED;
6682 break;
6683 default:
6684 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006685 }
6686
Amritha Nambiara9ce82f2017-09-07 04:00:22 -07006687config_tc:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006688 /* Generate TC map for number of tc requested */
Amritha Nambiara9ce82f2017-09-07 04:00:22 -07006689 for (i = 0; i < num_tc; i++)
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006690 enabled_tc |= BIT(i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006691
6692 /* Requesting same TC configuration as already enabled */
Amritha Nambiara9ce82f2017-09-07 04:00:22 -07006693 if (enabled_tc == vsi->tc_config.enabled_tc &&
6694 mode != TC_MQPRIO_MODE_CHANNEL)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006695 return 0;
6696
6697 /* Quiesce VSI queues */
6698 i40e_quiesce_vsi(vsi);
6699
Amritha Nambiara9ce82f2017-09-07 04:00:22 -07006700 if (!hw && !(pf->flags & I40E_FLAG_TC_MQPRIO))
6701 i40e_remove_queue_channels(vsi);
6702
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006703 /* Configure VSI for enabled TCs */
6704 ret = i40e_vsi_config_tc(vsi, enabled_tc);
6705 if (ret) {
6706 netdev_info(netdev, "Failed configuring TC for VSI seid=%d\n",
6707 vsi->seid);
Amritha Nambiara9ce82f2017-09-07 04:00:22 -07006708 need_reset = true;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006709 goto exit;
6710 }
6711
Amritha Nambiar8f88b302017-09-07 04:00:17 -07006712 if (pf->flags & I40E_FLAG_TC_MQPRIO) {
Amritha Nambiar2027d4d2017-09-07 04:00:32 -07006713 if (vsi->mqprio_qopt.max_rate[0]) {
Alan Brady6c32e0d2017-10-09 15:48:45 -07006714 u64 max_tx_rate = vsi->mqprio_qopt.max_rate[0];
6715
6716 do_div(max_tx_rate, I40E_BW_MBPS_DIVISOR);
Amritha Nambiar2027d4d2017-09-07 04:00:32 -07006717 ret = i40e_set_bw_limit(vsi, vsi->seid, max_tx_rate);
6718 if (!ret) {
Alan Brady6c32e0d2017-10-09 15:48:45 -07006719 u64 credits = max_tx_rate;
6720
6721 do_div(credits, I40E_BW_CREDIT_DIVISOR);
Amritha Nambiar2027d4d2017-09-07 04:00:32 -07006722 dev_dbg(&vsi->back->pdev->dev,
6723 "Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
6724 max_tx_rate,
Alan Brady6c32e0d2017-10-09 15:48:45 -07006725 credits,
Amritha Nambiar2027d4d2017-09-07 04:00:32 -07006726 vsi->seid);
6727 } else {
6728 need_reset = true;
6729 goto exit;
6730 }
6731 }
Amritha Nambiar8f88b302017-09-07 04:00:17 -07006732 ret = i40e_configure_queue_channels(vsi);
6733 if (ret) {
6734 netdev_info(netdev,
6735 "Failed configuring queue channels\n");
Amritha Nambiara9ce82f2017-09-07 04:00:22 -07006736 need_reset = true;
Amritha Nambiar8f88b302017-09-07 04:00:17 -07006737 goto exit;
6738 }
6739 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006740
6741exit:
Amritha Nambiara9ce82f2017-09-07 04:00:22 -07006742 /* Reset the configuration data to defaults, only TC0 is enabled */
6743 if (need_reset) {
6744 i40e_vsi_set_default_tc_config(vsi);
6745 need_reset = false;
6746 }
6747
Amritha Nambiar8f88b302017-09-07 04:00:17 -07006748 /* Unquiesce VSI */
6749 i40e_unquiesce_vsi(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006750 return ret;
6751}
6752
Jiri Pirko2572ac52017-08-07 10:15:17 +02006753static int __i40e_setup_tc(struct net_device *netdev, enum tc_setup_type type,
Jiri Pirkode4784c2017-08-07 10:15:32 +02006754 void *type_data)
John Fastabende4c67342016-02-16 21:16:15 -08006755{
Jiri Pirko2572ac52017-08-07 10:15:17 +02006756 if (type != TC_SETUP_MQPRIO)
Jiri Pirko38cf0422017-08-07 10:15:31 +02006757 return -EOPNOTSUPP;
Amritha Nambiar56f36ac2017-03-15 10:39:25 -07006758
Amritha Nambiara9ce82f2017-09-07 04:00:22 -07006759 return i40e_setup_tc(netdev, type_data);
John Fastabende4c67342016-02-16 21:16:15 -08006760}
6761
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006762/**
6763 * i40e_open - Called when a network interface is made active
6764 * @netdev: network interface device structure
6765 *
6766 * The open entry point is called when a network interface is made
6767 * active by the system (IFF_UP). At this point all resources needed
6768 * for transmit and receive operations are allocated, the interrupt
6769 * handler is registered with the OS, the netdev watchdog subtask is
6770 * enabled, and the stack is notified that the interface is ready.
6771 *
6772 * Returns 0 on success, negative value on failure
6773 **/
Vasu Dev38e00432014-08-01 13:27:03 -07006774int i40e_open(struct net_device *netdev)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006775{
6776 struct i40e_netdev_priv *np = netdev_priv(netdev);
6777 struct i40e_vsi *vsi = np->vsi;
6778 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006779 int err;
6780
Shannon Nelson4eb3f762014-03-06 08:59:58 +00006781 /* disallow open during test or if eeprom is broken */
Jacob Keller0da36b92017-04-19 09:25:55 -04006782 if (test_bit(__I40E_TESTING, pf->state) ||
6783 test_bit(__I40E_BAD_EEPROM, pf->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006784 return -EBUSY;
6785
6786 netif_carrier_off(netdev);
6787
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00006788 err = i40e_vsi_open(vsi);
6789 if (err)
6790 return err;
6791
Jesse Brandeburg059dab62014-04-01 09:07:20 +00006792 /* configure global TSO hardware offload settings */
6793 wr32(&pf->hw, I40E_GLLAN_TSOMSK_F, be32_to_cpu(TCP_FLAG_PSH |
6794 TCP_FLAG_FIN) >> 16);
6795 wr32(&pf->hw, I40E_GLLAN_TSOMSK_M, be32_to_cpu(TCP_FLAG_PSH |
6796 TCP_FLAG_FIN |
6797 TCP_FLAG_CWR) >> 16);
6798 wr32(&pf->hw, I40E_GLLAN_TSOMSK_L, be32_to_cpu(TCP_FLAG_CWR) >> 16);
6799
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07006800 udp_tunnel_get_rx_info(netdev);
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06006801
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00006802 return 0;
6803}
6804
6805/**
6806 * i40e_vsi_open -
6807 * @vsi: the VSI to open
6808 *
6809 * Finish initialization of the VSI.
6810 *
6811 * Returns 0 on success, negative value on failure
Maciej Sosin373149f2017-04-05 07:50:55 -04006812 *
6813 * Note: expects to be called while under rtnl_lock()
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00006814 **/
6815int i40e_vsi_open(struct i40e_vsi *vsi)
6816{
6817 struct i40e_pf *pf = vsi->back;
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00006818 char int_name[I40E_INT_NAME_STR_LEN];
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00006819 int err;
6820
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006821 /* allocate descriptors */
6822 err = i40e_vsi_setup_tx_resources(vsi);
6823 if (err)
6824 goto err_setup_tx;
6825 err = i40e_vsi_setup_rx_resources(vsi);
6826 if (err)
6827 goto err_setup_rx;
6828
6829 err = i40e_vsi_configure(vsi);
6830 if (err)
6831 goto err_setup_rx;
6832
Shannon Nelsonc22e3c62014-03-14 07:32:25 +00006833 if (vsi->netdev) {
6834 snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
6835 dev_driver_string(&pf->pdev->dev), vsi->netdev->name);
6836 err = i40e_vsi_request_irq(vsi, int_name);
6837 if (err)
6838 goto err_setup_rx;
6839
6840 /* Notify the stack of the actual queue counts. */
6841 err = netif_set_real_num_tx_queues(vsi->netdev,
6842 vsi->num_queue_pairs);
6843 if (err)
6844 goto err_set_queues;
6845
6846 err = netif_set_real_num_rx_queues(vsi->netdev,
6847 vsi->num_queue_pairs);
6848 if (err)
6849 goto err_set_queues;
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00006850
6851 } else if (vsi->type == I40E_VSI_FDIR) {
Carolyn Wybornye240f672014-12-11 07:06:37 +00006852 snprintf(int_name, sizeof(int_name) - 1, "%s-%s:fdir",
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +00006853 dev_driver_string(&pf->pdev->dev),
6854 dev_name(&pf->pdev->dev));
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00006855 err = i40e_vsi_request_irq(vsi, int_name);
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +00006856
Shannon Nelsonc22e3c62014-03-14 07:32:25 +00006857 } else {
Jean Sacrence9ccb12014-05-01 14:31:18 +00006858 err = -EINVAL;
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00006859 goto err_setup_rx;
6860 }
Anjali Singhai Jain25946dd2013-11-26 10:49:14 +00006861
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006862 err = i40e_up_complete(vsi);
6863 if (err)
6864 goto err_up_complete;
6865
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006866 return 0;
6867
6868err_up_complete:
6869 i40e_down(vsi);
Anjali Singhai Jain25946dd2013-11-26 10:49:14 +00006870err_set_queues:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006871 i40e_vsi_free_irq(vsi);
6872err_setup_rx:
6873 i40e_vsi_free_rx_resources(vsi);
6874err_setup_tx:
6875 i40e_vsi_free_tx_resources(vsi);
6876 if (vsi == pf->vsi[pf->lan_vsi])
Amritha Nambiarff424182017-09-07 04:00:11 -07006877 i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006878
6879 return err;
6880}
6881
6882/**
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00006883 * i40e_fdir_filter_exit - Cleans up the Flow Director accounting
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00006884 * @pf: Pointer to PF
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00006885 *
6886 * This function destroys the hlist where all the Flow Director
6887 * filters were saved.
6888 **/
6889static void i40e_fdir_filter_exit(struct i40e_pf *pf)
6890{
6891 struct i40e_fdir_filter *filter;
Jacob Keller0e588de2017-02-06 14:38:50 -08006892 struct i40e_flex_pit *pit_entry, *tmp;
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00006893 struct hlist_node *node2;
6894
6895 hlist_for_each_entry_safe(filter, node2,
6896 &pf->fdir_filter_list, fdir_node) {
6897 hlist_del(&filter->fdir_node);
6898 kfree(filter);
6899 }
Jacob Keller097dbf52017-02-06 14:38:46 -08006900
Jacob Keller0e588de2017-02-06 14:38:50 -08006901 list_for_each_entry_safe(pit_entry, tmp, &pf->l3_flex_pit_list, list) {
6902 list_del(&pit_entry->list);
6903 kfree(pit_entry);
6904 }
6905 INIT_LIST_HEAD(&pf->l3_flex_pit_list);
6906
6907 list_for_each_entry_safe(pit_entry, tmp, &pf->l4_flex_pit_list, list) {
6908 list_del(&pit_entry->list);
6909 kfree(pit_entry);
6910 }
6911 INIT_LIST_HEAD(&pf->l4_flex_pit_list);
6912
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00006913 pf->fdir_pf_active_filters = 0;
Jacob Keller097dbf52017-02-06 14:38:46 -08006914 pf->fd_tcp4_filter_cnt = 0;
6915 pf->fd_udp4_filter_cnt = 0;
Jacob Kellerf223c872017-02-06 14:38:51 -08006916 pf->fd_sctp4_filter_cnt = 0;
Jacob Keller097dbf52017-02-06 14:38:46 -08006917 pf->fd_ip4_filter_cnt = 0;
Jacob Keller3bcee1e2017-02-06 14:38:46 -08006918
6919 /* Reprogram the default input set for TCP/IPv4 */
6920 i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_TCP,
6921 I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
6922 I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
6923
6924 /* Reprogram the default input set for UDP/IPv4 */
6925 i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_UDP,
6926 I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
6927 I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
6928
6929 /* Reprogram the default input set for SCTP/IPv4 */
6930 i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_SCTP,
6931 I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
6932 I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
6933
6934 /* Reprogram the default input set for Other/IPv4 */
6935 i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_OTHER,
6936 I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00006937}
6938
6939/**
Amritha Nambiaraaf66502017-10-27 02:35:56 -07006940 * i40e_cloud_filter_exit - Cleans up the cloud filters
6941 * @pf: Pointer to PF
6942 *
6943 * This function destroys the hlist where all the cloud filters
6944 * were saved.
6945 **/
6946static void i40e_cloud_filter_exit(struct i40e_pf *pf)
6947{
6948 struct i40e_cloud_filter *cfilter;
6949 struct hlist_node *node;
6950
6951 hlist_for_each_entry_safe(cfilter, node,
6952 &pf->cloud_filter_list, cloud_node) {
6953 hlist_del(&cfilter->cloud_node);
6954 kfree(cfilter);
6955 }
6956 pf->num_cloud_filters = 0;
6957}
6958
6959/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006960 * i40e_close - Disables a network interface
6961 * @netdev: network interface device structure
6962 *
6963 * The close entry point is called when an interface is de-activated
6964 * by the OS. The hardware is still under the driver's control, but
6965 * this netdev interface is disabled.
6966 *
6967 * Returns 0, this is not allowed to fail
6968 **/
Vasu Dev38e00432014-08-01 13:27:03 -07006969int i40e_close(struct net_device *netdev)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006970{
6971 struct i40e_netdev_priv *np = netdev_priv(netdev);
6972 struct i40e_vsi *vsi = np->vsi;
6973
Shannon Nelson90ef8d42014-03-14 07:32:26 +00006974 i40e_vsi_close(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006975
6976 return 0;
6977}
6978
6979/**
6980 * i40e_do_reset - Start a PF or Core Reset sequence
6981 * @pf: board private structure
6982 * @reset_flags: which reset is requested
Maciej Sosin373149f2017-04-05 07:50:55 -04006983 * @lock_acquired: indicates whether or not the lock has been acquired
6984 * before this function was called.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006985 *
6986 * The essential difference in resets is that the PF Reset
6987 * doesn't clear the packet buffers, doesn't reset the PE
6988 * firmware, and doesn't bother the other PFs on the chip.
6989 **/
Maciej Sosin373149f2017-04-05 07:50:55 -04006990void i40e_do_reset(struct i40e_pf *pf, u32 reset_flags, bool lock_acquired)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006991{
6992 u32 val;
6993
6994 WARN_ON(in_interrupt());
6995
Mitch Williams263fc482014-04-23 04:50:11 +00006996
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006997 /* do the biggest reset indicated */
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04006998 if (reset_flags & BIT_ULL(__I40E_GLOBAL_RESET_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006999
7000 /* Request a Global Reset
7001 *
7002 * This will start the chip's countdown to the actual full
7003 * chip reset event, and a warning interrupt to be sent
7004 * to all PFs, including the requestor. Our handler
7005 * for the warning interrupt will deal with the shutdown
7006 * and recovery of the switch setup.
7007 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00007008 dev_dbg(&pf->pdev->dev, "GlobalR requested\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007009 val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
7010 val |= I40E_GLGEN_RTRIG_GLOBR_MASK;
7011 wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
7012
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04007013 } else if (reset_flags & BIT_ULL(__I40E_CORE_RESET_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007014
7015 /* Request a Core Reset
7016 *
7017 * Same as Global Reset, except does *not* include the MAC/PHY
7018 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00007019 dev_dbg(&pf->pdev->dev, "CoreR requested\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007020 val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
7021 val |= I40E_GLGEN_RTRIG_CORER_MASK;
7022 wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
7023 i40e_flush(&pf->hw);
7024
Amritha Nambiarff424182017-09-07 04:00:11 -07007025 } else if (reset_flags & I40E_PF_RESET_FLAG) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007026
7027 /* Request a PF Reset
7028 *
7029 * Resets only the PF-specific registers
7030 *
7031 * This goes directly to the tear-down and rebuild of
7032 * the switch, since we need to do all the recovery as
7033 * for the Core Reset.
7034 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00007035 dev_dbg(&pf->pdev->dev, "PFR requested\n");
Maciej Sosin373149f2017-04-05 07:50:55 -04007036 i40e_handle_reset_warning(pf, lock_acquired);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007037
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04007038 } else if (reset_flags & BIT_ULL(__I40E_REINIT_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007039 int v;
7040
7041 /* Find the VSI(s) that requested a re-init */
7042 dev_info(&pf->pdev->dev,
7043 "VSI reinit requested\n");
Mitch Williams505682c2014-05-20 08:01:37 +00007044 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007045 struct i40e_vsi *vsi = pf->vsi[v];
Jesse Brandeburg6995b362015-08-28 17:55:54 -04007046
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007047 if (vsi != NULL &&
Jacob Kellerd19cb642017-04-21 13:38:05 -07007048 test_and_clear_bit(__I40E_VSI_REINIT_REQUESTED,
Jacob Keller0da36b92017-04-19 09:25:55 -04007049 vsi->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007050 i40e_vsi_reinit_locked(pf->vsi[v]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007051 }
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04007052 } else if (reset_flags & BIT_ULL(__I40E_DOWN_REQUESTED)) {
Neerav Parikhb5d06f02014-06-03 23:50:17 +00007053 int v;
7054
7055 /* Find the VSI(s) that needs to be brought down */
7056 dev_info(&pf->pdev->dev, "VSI down requested\n");
7057 for (v = 0; v < pf->num_alloc_vsi; v++) {
7058 struct i40e_vsi *vsi = pf->vsi[v];
Jesse Brandeburg6995b362015-08-28 17:55:54 -04007059
Neerav Parikhb5d06f02014-06-03 23:50:17 +00007060 if (vsi != NULL &&
Jacob Kellerd19cb642017-04-21 13:38:05 -07007061 test_and_clear_bit(__I40E_VSI_DOWN_REQUESTED,
Jacob Keller0da36b92017-04-19 09:25:55 -04007062 vsi->state)) {
7063 set_bit(__I40E_VSI_DOWN, vsi->state);
Neerav Parikhb5d06f02014-06-03 23:50:17 +00007064 i40e_down(vsi);
Neerav Parikhb5d06f02014-06-03 23:50:17 +00007065 }
7066 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007067 } else {
7068 dev_info(&pf->pdev->dev,
7069 "bad reset request 0x%08x\n", reset_flags);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007070 }
7071}
7072
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08007073#ifdef CONFIG_I40E_DCB
7074/**
7075 * i40e_dcb_need_reconfig - Check if DCB needs reconfig
7076 * @pf: board private structure
7077 * @old_cfg: current DCB config
7078 * @new_cfg: new DCB config
7079 **/
7080bool i40e_dcb_need_reconfig(struct i40e_pf *pf,
7081 struct i40e_dcbx_config *old_cfg,
7082 struct i40e_dcbx_config *new_cfg)
7083{
7084 bool need_reconfig = false;
7085
7086 /* Check if ETS configuration has changed */
7087 if (memcmp(&new_cfg->etscfg,
7088 &old_cfg->etscfg,
7089 sizeof(new_cfg->etscfg))) {
7090 /* If Priority Table has changed reconfig is needed */
7091 if (memcmp(&new_cfg->etscfg.prioritytable,
7092 &old_cfg->etscfg.prioritytable,
7093 sizeof(new_cfg->etscfg.prioritytable))) {
7094 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00007095 dev_dbg(&pf->pdev->dev, "ETS UP2TC changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08007096 }
7097
7098 if (memcmp(&new_cfg->etscfg.tcbwtable,
7099 &old_cfg->etscfg.tcbwtable,
7100 sizeof(new_cfg->etscfg.tcbwtable)))
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00007101 dev_dbg(&pf->pdev->dev, "ETS TC BW Table changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08007102
7103 if (memcmp(&new_cfg->etscfg.tsatable,
7104 &old_cfg->etscfg.tsatable,
7105 sizeof(new_cfg->etscfg.tsatable)))
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00007106 dev_dbg(&pf->pdev->dev, "ETS TSA Table changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08007107 }
7108
7109 /* Check if PFC configuration has changed */
7110 if (memcmp(&new_cfg->pfc,
7111 &old_cfg->pfc,
7112 sizeof(new_cfg->pfc))) {
7113 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00007114 dev_dbg(&pf->pdev->dev, "PFC config change detected.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08007115 }
7116
7117 /* Check if APP Table has changed */
7118 if (memcmp(&new_cfg->app,
7119 &old_cfg->app,
Dave Jones3d9667a2014-01-27 23:11:09 -05007120 sizeof(new_cfg->app))) {
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08007121 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00007122 dev_dbg(&pf->pdev->dev, "APP Table change detected.\n");
Dave Jones3d9667a2014-01-27 23:11:09 -05007123 }
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08007124
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04007125 dev_dbg(&pf->pdev->dev, "dcb need_reconfig=%d\n", need_reconfig);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08007126 return need_reconfig;
7127}
7128
7129/**
7130 * i40e_handle_lldp_event - Handle LLDP Change MIB event
7131 * @pf: board private structure
7132 * @e: event info posted on ARQ
7133 **/
7134static int i40e_handle_lldp_event(struct i40e_pf *pf,
7135 struct i40e_arq_event_info *e)
7136{
7137 struct i40e_aqc_lldp_get_mib *mib =
7138 (struct i40e_aqc_lldp_get_mib *)&e->desc.params.raw;
7139 struct i40e_hw *hw = &pf->hw;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08007140 struct i40e_dcbx_config tmp_dcbx_cfg;
7141 bool need_reconfig = false;
7142 int ret = 0;
7143 u8 type;
7144
Neerav Parikh4d9b6042014-05-22 06:31:51 +00007145 /* Not DCB capable or capability disabled */
David Ertmanea6acb72016-09-20 07:10:50 -07007146 if (!(pf->flags & I40E_FLAG_DCB_CAPABLE))
Neerav Parikh4d9b6042014-05-22 06:31:51 +00007147 return ret;
7148
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08007149 /* Ignore if event is not for Nearest Bridge */
7150 type = ((mib->type >> I40E_AQ_LLDP_BRIDGE_TYPE_SHIFT)
7151 & I40E_AQ_LLDP_BRIDGE_TYPE_MASK);
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04007152 dev_dbg(&pf->pdev->dev, "LLDP event mib bridge type 0x%x\n", type);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08007153 if (type != I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE)
7154 return ret;
7155
7156 /* Check MIB Type and return if event for Remote MIB update */
7157 type = mib->type & I40E_AQ_LLDP_MIB_TYPE_MASK;
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00007158 dev_dbg(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04007159 "LLDP event mib type %s\n", type ? "remote" : "local");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08007160 if (type == I40E_AQ_LLDP_MIB_REMOTE) {
7161 /* Update the remote cached instance and return */
7162 ret = i40e_aq_get_dcb_config(hw, I40E_AQ_LLDP_MIB_REMOTE,
7163 I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE,
7164 &hw->remote_dcbx_config);
7165 goto exit;
7166 }
7167
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00007168 /* Store the old configuration */
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07007169 tmp_dcbx_cfg = hw->local_dcbx_config;
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00007170
Neerav Parikh750fcbc2015-02-24 06:58:47 +00007171 /* Reset the old DCBx configuration data */
7172 memset(&hw->local_dcbx_config, 0, sizeof(hw->local_dcbx_config));
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00007173 /* Get updated DCBX data from firmware */
7174 ret = i40e_get_dcb_config(&pf->hw);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08007175 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04007176 dev_info(&pf->pdev->dev,
7177 "Failed querying DCB configuration data from firmware, err %s aq_err %s\n",
7178 i40e_stat_str(&pf->hw, ret),
7179 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08007180 goto exit;
7181 }
7182
7183 /* No change detected in DCBX configs */
Neerav Parikh750fcbc2015-02-24 06:58:47 +00007184 if (!memcmp(&tmp_dcbx_cfg, &hw->local_dcbx_config,
7185 sizeof(tmp_dcbx_cfg))) {
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00007186 dev_dbg(&pf->pdev->dev, "No change detected in DCBX configuration.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08007187 goto exit;
7188 }
7189
Neerav Parikh750fcbc2015-02-24 06:58:47 +00007190 need_reconfig = i40e_dcb_need_reconfig(pf, &tmp_dcbx_cfg,
7191 &hw->local_dcbx_config);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08007192
Neerav Parikh750fcbc2015-02-24 06:58:47 +00007193 i40e_dcbnl_flush_apps(pf, &tmp_dcbx_cfg, &hw->local_dcbx_config);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08007194
7195 if (!need_reconfig)
7196 goto exit;
7197
Neerav Parikh4d9b6042014-05-22 06:31:51 +00007198 /* Enable DCB tagging only when more than one TC */
Neerav Parikh750fcbc2015-02-24 06:58:47 +00007199 if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
Neerav Parikh4d9b6042014-05-22 06:31:51 +00007200 pf->flags |= I40E_FLAG_DCB_ENABLED;
7201 else
7202 pf->flags &= ~I40E_FLAG_DCB_ENABLED;
7203
Jacob Keller0da36b92017-04-19 09:25:55 -04007204 set_bit(__I40E_PORT_SUSPENDED, pf->state);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08007205 /* Reconfiguration needed quiesce all VSIs */
7206 i40e_pf_quiesce_all_vsi(pf);
7207
7208 /* Changes in configuration update VEB/VSI */
7209 i40e_dcb_reconfigure(pf);
7210
Neerav Parikh2fd75f32014-11-12 00:18:20 +00007211 ret = i40e_resume_port_tx(pf);
7212
Jacob Keller0da36b92017-04-19 09:25:55 -04007213 clear_bit(__I40E_PORT_SUSPENDED, pf->state);
Neerav Parikh2fd75f32014-11-12 00:18:20 +00007214 /* In case of error no point in resuming VSIs */
Neerav Parikh69129dc2014-11-12 00:18:46 +00007215 if (ret)
7216 goto exit;
7217
Neerav Parikh3fe06f42016-02-17 16:12:15 -08007218 /* Wait for the PF's queues to be disabled */
7219 ret = i40e_pf_wait_queues_disabled(pf);
Parikh, Neerav11e47702015-02-21 06:43:55 +00007220 if (ret) {
7221 /* Schedule PF reset to recover */
Jacob Keller0da36b92017-04-19 09:25:55 -04007222 set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
Parikh, Neerav11e47702015-02-21 06:43:55 +00007223 i40e_service_event_schedule(pf);
7224 } else {
Neerav Parikh2fd75f32014-11-12 00:18:20 +00007225 i40e_pf_unquiesce_all_vsi(pf);
Mitch Williams0ef2d5a2017-01-24 10:24:00 -08007226 pf->flags |= (I40E_FLAG_SERVICE_CLIENT_REQUESTED |
7227 I40E_FLAG_CLIENT_L2_CHANGE);
Parikh, Neerav11e47702015-02-21 06:43:55 +00007228 }
7229
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08007230exit:
7231 return ret;
7232}
7233#endif /* CONFIG_I40E_DCB */
7234
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007235/**
Anjali Singhai Jain233261862013-11-26 10:49:22 +00007236 * i40e_do_reset_safe - Protected reset path for userland calls.
7237 * @pf: board private structure
7238 * @reset_flags: which reset is requested
7239 *
7240 **/
7241void i40e_do_reset_safe(struct i40e_pf *pf, u32 reset_flags)
7242{
7243 rtnl_lock();
Maciej Sosin373149f2017-04-05 07:50:55 -04007244 i40e_do_reset(pf, reset_flags, true);
Anjali Singhai Jain233261862013-11-26 10:49:22 +00007245 rtnl_unlock();
7246}
7247
7248/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007249 * i40e_handle_lan_overflow_event - Handler for LAN queue overflow event
7250 * @pf: board private structure
7251 * @e: event info posted on ARQ
7252 *
7253 * Handler for LAN Queue Overflow Event generated by the firmware for PF
7254 * and VF queues
7255 **/
7256static void i40e_handle_lan_overflow_event(struct i40e_pf *pf,
7257 struct i40e_arq_event_info *e)
7258{
7259 struct i40e_aqc_lan_overflow *data =
7260 (struct i40e_aqc_lan_overflow *)&e->desc.params.raw;
7261 u32 queue = le32_to_cpu(data->prtdcb_rupto);
7262 u32 qtx_ctl = le32_to_cpu(data->otx_ctl);
7263 struct i40e_hw *hw = &pf->hw;
7264 struct i40e_vf *vf;
7265 u16 vf_id;
7266
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00007267 dev_dbg(&pf->pdev->dev, "overflow Rx Queue Number = %d QTX_CTL=0x%08x\n",
7268 queue, qtx_ctl);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007269
7270 /* Queue belongs to VF, find the VF and issue VF reset */
7271 if (((qtx_ctl & I40E_QTX_CTL_PFVF_Q_MASK)
7272 >> I40E_QTX_CTL_PFVF_Q_SHIFT) == I40E_QTX_CTL_VF_QUEUE) {
7273 vf_id = (u16)((qtx_ctl & I40E_QTX_CTL_VFVM_INDX_MASK)
7274 >> I40E_QTX_CTL_VFVM_INDX_SHIFT);
7275 vf_id -= hw->func_caps.vf_base_id;
7276 vf = &pf->vf[vf_id];
7277 i40e_vc_notify_vf_reset(vf);
7278 /* Allow VF to process pending reset notification */
7279 msleep(20);
7280 i40e_reset_vf(vf, false);
7281 }
7282}
7283
7284/**
Anjali Singhai Jain12957382014-06-04 04:22:47 +00007285 * i40e_get_cur_guaranteed_fd_count - Get the consumed guaranteed FD filters
7286 * @pf: board private structure
7287 **/
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00007288u32 i40e_get_cur_guaranteed_fd_count(struct i40e_pf *pf)
Anjali Singhai Jain12957382014-06-04 04:22:47 +00007289{
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00007290 u32 val, fcnt_prog;
Anjali Singhai Jain12957382014-06-04 04:22:47 +00007291
7292 val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
7293 fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK);
7294 return fcnt_prog;
7295}
7296
7297/**
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00007298 * i40e_get_current_fd_count - Get total FD filters programmed for this PF
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00007299 * @pf: board private structure
7300 **/
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00007301u32 i40e_get_current_fd_count(struct i40e_pf *pf)
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00007302{
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00007303 u32 val, fcnt_prog;
7304
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00007305 val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
7306 fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK) +
7307 ((val & I40E_PFQF_FDSTAT_BEST_CNT_MASK) >>
7308 I40E_PFQF_FDSTAT_BEST_CNT_SHIFT);
7309 return fcnt_prog;
7310}
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00007311
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00007312/**
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00007313 * i40e_get_global_fd_count - Get total FD filters programmed on device
7314 * @pf: board private structure
7315 **/
7316u32 i40e_get_global_fd_count(struct i40e_pf *pf)
7317{
7318 u32 val, fcnt_prog;
7319
7320 val = rd32(&pf->hw, I40E_GLQF_FDCNT_0);
7321 fcnt_prog = (val & I40E_GLQF_FDCNT_0_GUARANT_CNT_MASK) +
7322 ((val & I40E_GLQF_FDCNT_0_BESTCNT_MASK) >>
7323 I40E_GLQF_FDCNT_0_BESTCNT_SHIFT);
7324 return fcnt_prog;
7325}
7326
7327/**
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00007328 * i40e_fdir_check_and_reenable - Function to reenabe FD ATR or SB if disabled
7329 * @pf: board private structure
7330 **/
7331void i40e_fdir_check_and_reenable(struct i40e_pf *pf)
7332{
Carolyn Wyborny3487b6c2015-08-27 11:42:38 -04007333 struct i40e_fdir_filter *filter;
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00007334 u32 fcnt_prog, fcnt_avail;
Carolyn Wyborny3487b6c2015-08-27 11:42:38 -04007335 struct hlist_node *node;
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00007336
Jacob Keller0da36b92017-04-19 09:25:55 -04007337 if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00007338 return;
7339
Jacob Keller47994c12017-04-19 09:25:57 -04007340 /* Check if we have enough room to re-enable FDir SB capability. */
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00007341 fcnt_prog = i40e_get_global_fd_count(pf);
Anjali Singhai Jain12957382014-06-04 04:22:47 +00007342 fcnt_avail = pf->fdir_pf_filter_count;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00007343 if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM)) ||
7344 (pf->fd_add_err == 0) ||
7345 (i40e_get_current_atr_cnt(pf) < pf->fd_atr_cnt)) {
Jacob Keller47994c12017-04-19 09:25:57 -04007346 if (pf->flags & I40E_FLAG_FD_SB_AUTO_DISABLED) {
7347 pf->flags &= ~I40E_FLAG_FD_SB_AUTO_DISABLED;
7348 if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
7349 (I40E_DEBUG_FD & pf->hw.debug_mask))
Anjali Singhai Jain2e4875e2015-04-16 20:06:06 -04007350 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 +00007351 }
7352 }
Jacob Kellera3417d22016-09-06 18:05:10 -07007353
Jacob Keller47994c12017-04-19 09:25:57 -04007354 /* We should wait for even more space before re-enabling ATR.
7355 * Additionally, we cannot enable ATR as long as we still have TCP SB
7356 * rules active.
Jacob Kellera3417d22016-09-06 18:05:10 -07007357 */
Jacob Keller47994c12017-04-19 09:25:57 -04007358 if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) &&
7359 (pf->fd_tcp4_filter_cnt == 0)) {
7360 if (pf->flags & I40E_FLAG_FD_ATR_AUTO_DISABLED) {
7361 pf->flags &= ~I40E_FLAG_FD_ATR_AUTO_DISABLED;
7362 if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
7363 (I40E_DEBUG_FD & pf->hw.debug_mask))
Jacob Kellera3417d22016-09-06 18:05:10 -07007364 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 +00007365 }
7366 }
Carolyn Wyborny3487b6c2015-08-27 11:42:38 -04007367
7368 /* if hw had a problem adding a filter, delete it */
7369 if (pf->fd_inv > 0) {
7370 hlist_for_each_entry_safe(filter, node,
7371 &pf->fdir_filter_list, fdir_node) {
7372 if (filter->fd_id == pf->fd_inv) {
7373 hlist_del(&filter->fdir_node);
7374 kfree(filter);
7375 pf->fdir_pf_active_filters--;
Filip Sadowski013df592017-08-29 05:32:38 -04007376 pf->fd_inv = 0;
Carolyn Wyborny3487b6c2015-08-27 11:42:38 -04007377 }
7378 }
7379 }
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00007380}
7381
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00007382#define I40E_MIN_FD_FLUSH_INTERVAL 10
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00007383#define I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE 30
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00007384/**
7385 * i40e_fdir_flush_and_replay - Function to flush all FD filters and replay SB
7386 * @pf: board private structure
7387 **/
7388static void i40e_fdir_flush_and_replay(struct i40e_pf *pf)
7389{
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00007390 unsigned long min_flush_time;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00007391 int flush_wait_retry = 50;
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00007392 bool disable_atr = false;
7393 int fd_room;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00007394 int reg;
7395
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04007396 if (!time_after(jiffies, pf->fd_flush_timestamp +
7397 (I40E_MIN_FD_FLUSH_INTERVAL * HZ)))
7398 return;
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00007399
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04007400 /* If the flush is happening too quick and we have mostly SB rules we
7401 * should not re-enable ATR for some time.
7402 */
7403 min_flush_time = pf->fd_flush_timestamp +
7404 (I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE * HZ);
7405 fd_room = pf->fdir_pf_filter_count - pf->fdir_pf_active_filters;
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00007406
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04007407 if (!(time_after(jiffies, min_flush_time)) &&
7408 (fd_room < I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) {
7409 if (I40E_DEBUG_FD & pf->hw.debug_mask)
7410 dev_info(&pf->pdev->dev, "ATR disabled, not enough FD filter space.\n");
7411 disable_atr = true;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00007412 }
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04007413
7414 pf->fd_flush_timestamp = jiffies;
Jacob Keller47994c12017-04-19 09:25:57 -04007415 pf->flags |= I40E_FLAG_FD_ATR_AUTO_DISABLED;
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04007416 /* flush all filters */
7417 wr32(&pf->hw, I40E_PFQF_CTL_1,
7418 I40E_PFQF_CTL_1_CLEARFDTABLE_MASK);
7419 i40e_flush(&pf->hw);
7420 pf->fd_flush_cnt++;
7421 pf->fd_add_err = 0;
7422 do {
7423 /* Check FD flush status every 5-6msec */
7424 usleep_range(5000, 6000);
7425 reg = rd32(&pf->hw, I40E_PFQF_CTL_1);
7426 if (!(reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK))
7427 break;
7428 } while (flush_wait_retry--);
7429 if (reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK) {
7430 dev_warn(&pf->pdev->dev, "FD table did not flush, needs more time\n");
7431 } else {
7432 /* replay sideband filters */
7433 i40e_fdir_filter_restore(pf->vsi[pf->lan_vsi]);
Jacob Keller097dbf52017-02-06 14:38:46 -08007434 if (!disable_atr && !pf->fd_tcp4_filter_cnt)
Jacob Keller47994c12017-04-19 09:25:57 -04007435 pf->flags &= ~I40E_FLAG_FD_ATR_AUTO_DISABLED;
Jacob Keller0da36b92017-04-19 09:25:55 -04007436 clear_bit(__I40E_FD_FLUSH_REQUESTED, pf->state);
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04007437 if (I40E_DEBUG_FD & pf->hw.debug_mask)
7438 dev_info(&pf->pdev->dev, "FD Filter table flushed and FD-SB replayed.\n");
7439 }
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00007440}
7441
7442/**
7443 * i40e_get_current_atr_count - Get the count of total FD ATR filters programmed
7444 * @pf: board private structure
7445 **/
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00007446u32 i40e_get_current_atr_cnt(struct i40e_pf *pf)
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00007447{
7448 return i40e_get_current_fd_count(pf) - pf->fdir_pf_active_filters;
7449}
7450
7451/* We can see up to 256 filter programming desc in transit if the filters are
7452 * being applied really fast; before we see the first
7453 * filter miss error on Rx queue 0. Accumulating enough error messages before
7454 * reacting will make sure we don't cause flush too often.
7455 */
7456#define I40E_MAX_FD_PROGRAM_ERROR 256
7457
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00007458/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007459 * i40e_fdir_reinit_subtask - Worker thread to reinit FDIR filter table
7460 * @pf: board private structure
7461 **/
7462static void i40e_fdir_reinit_subtask(struct i40e_pf *pf)
7463{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007464
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007465 /* if interface is down do nothing */
Mauro S. M. Rodrigues9e6c9c02017-05-12 23:26:56 -03007466 if (test_bit(__I40E_DOWN, pf->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007467 return;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00007468
Jacob Keller0da36b92017-04-19 09:25:55 -04007469 if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00007470 i40e_fdir_flush_and_replay(pf);
7471
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00007472 i40e_fdir_check_and_reenable(pf);
7473
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007474}
7475
7476/**
7477 * i40e_vsi_link_event - notify VSI of a link event
7478 * @vsi: vsi to be notified
7479 * @link_up: link up or down
7480 **/
7481static void i40e_vsi_link_event(struct i40e_vsi *vsi, bool link_up)
7482{
Jacob Keller0da36b92017-04-19 09:25:55 -04007483 if (!vsi || test_bit(__I40E_VSI_DOWN, vsi->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007484 return;
7485
7486 switch (vsi->type) {
7487 case I40E_VSI_MAIN:
7488 if (!vsi->netdev || !vsi->netdev_registered)
7489 break;
7490
7491 if (link_up) {
7492 netif_carrier_on(vsi->netdev);
7493 netif_tx_wake_all_queues(vsi->netdev);
7494 } else {
7495 netif_carrier_off(vsi->netdev);
7496 netif_tx_stop_all_queues(vsi->netdev);
7497 }
7498 break;
7499
7500 case I40E_VSI_SRIOV:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007501 case I40E_VSI_VMDQ2:
7502 case I40E_VSI_CTRL:
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007503 case I40E_VSI_IWARP:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007504 case I40E_VSI_MIRROR:
7505 default:
7506 /* there is no notification for other VSIs */
7507 break;
7508 }
7509}
7510
7511/**
7512 * i40e_veb_link_event - notify elements on the veb of a link event
7513 * @veb: veb to be notified
7514 * @link_up: link up or down
7515 **/
7516static void i40e_veb_link_event(struct i40e_veb *veb, bool link_up)
7517{
7518 struct i40e_pf *pf;
7519 int i;
7520
7521 if (!veb || !veb->pf)
7522 return;
7523 pf = veb->pf;
7524
7525 /* depth first... */
7526 for (i = 0; i < I40E_MAX_VEB; i++)
7527 if (pf->veb[i] && (pf->veb[i]->uplink_seid == veb->seid))
7528 i40e_veb_link_event(pf->veb[i], link_up);
7529
7530 /* ... now the local VSIs */
Mitch Williams505682c2014-05-20 08:01:37 +00007531 for (i = 0; i < pf->num_alloc_vsi; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007532 if (pf->vsi[i] && (pf->vsi[i]->uplink_seid == veb->seid))
7533 i40e_vsi_link_event(pf->vsi[i], link_up);
7534}
7535
7536/**
7537 * i40e_link_event - Update netif_carrier status
7538 * @pf: board private structure
7539 **/
7540static void i40e_link_event(struct i40e_pf *pf)
7541{
Mitch Williams320684c2014-10-17 03:14:43 +00007542 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
Catherine Sullivanfef59dd2014-12-11 07:06:33 +00007543 u8 new_link_speed, old_link_speed;
Jesse Brandeburga72a5abc2015-08-26 15:14:19 -04007544 i40e_status status;
7545 bool new_link, old_link;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007546
Catherine Sullivan1f9610e2015-10-21 19:47:09 -04007547 /* save off old link status information */
7548 pf->hw.phy.link_info_old = pf->hw.phy.link_info;
7549
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00007550 /* set this to force the get_link_status call to refresh state */
7551 pf->hw.phy.get_link_info = true;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007552
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00007553 old_link = (pf->hw.phy.link_info_old.link_info & I40E_AQ_LINK_UP);
Jesse Brandeburga72a5abc2015-08-26 15:14:19 -04007554
7555 status = i40e_get_link_status(&pf->hw, &new_link);
Harshitha Ramamurthyae136702016-12-12 15:44:16 -08007556
7557 /* On success, disable temp link polling */
7558 if (status == I40E_SUCCESS) {
7559 if (pf->flags & I40E_FLAG_TEMP_LINK_POLLING)
7560 pf->flags &= ~I40E_FLAG_TEMP_LINK_POLLING;
7561 } else {
7562 /* Enable link polling temporarily until i40e_get_link_status
7563 * returns I40E_SUCCESS
7564 */
7565 pf->flags |= I40E_FLAG_TEMP_LINK_POLLING;
Jesse Brandeburga72a5abc2015-08-26 15:14:19 -04007566 dev_dbg(&pf->pdev->dev, "couldn't get link state, status: %d\n",
7567 status);
7568 return;
7569 }
7570
Catherine Sullivanfef59dd2014-12-11 07:06:33 +00007571 old_link_speed = pf->hw.phy.link_info_old.link_speed;
7572 new_link_speed = pf->hw.phy.link_info.link_speed;
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00007573
7574 if (new_link == old_link &&
Catherine Sullivanfef59dd2014-12-11 07:06:33 +00007575 new_link_speed == old_link_speed &&
Jacob Keller0da36b92017-04-19 09:25:55 -04007576 (test_bit(__I40E_VSI_DOWN, vsi->state) ||
Mitch Williams320684c2014-10-17 03:14:43 +00007577 new_link == netif_carrier_ok(vsi->netdev)))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007578 return;
Mitch Williams320684c2014-10-17 03:14:43 +00007579
Sudheer Mogilappagari9a034492017-07-14 09:10:16 -04007580 i40e_print_link_message(vsi, new_link);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007581
7582 /* Notify the base of the switch tree connected to
7583 * the link. Floating VEBs are not notified.
7584 */
7585 if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
7586 i40e_veb_link_event(pf->veb[pf->lan_veb], new_link);
7587 else
Mitch Williams320684c2014-10-17 03:14:43 +00007588 i40e_vsi_link_event(vsi, new_link);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007589
7590 if (pf->vf)
7591 i40e_vc_notify_link_state(pf);
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00007592
7593 if (pf->flags & I40E_FLAG_PTP)
7594 i40e_ptp_set_increment(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007595}
7596
7597/**
Shannon Nelson21536712014-10-25 10:35:25 +00007598 * i40e_watchdog_subtask - periodic checks not using event driven response
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007599 * @pf: board private structure
7600 **/
7601static void i40e_watchdog_subtask(struct i40e_pf *pf)
7602{
7603 int i;
7604
7605 /* if interface is down do nothing */
Mauro S. M. Rodrigues9e6c9c02017-05-12 23:26:56 -03007606 if (test_bit(__I40E_DOWN, pf->state) ||
Jacob Keller0da36b92017-04-19 09:25:55 -04007607 test_bit(__I40E_CONFIG_BUSY, pf->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007608 return;
7609
Shannon Nelson21536712014-10-25 10:35:25 +00007610 /* make sure we don't do these things too often */
7611 if (time_before(jiffies, (pf->service_timer_previous +
7612 pf->service_timer_period)))
7613 return;
7614 pf->service_timer_previous = jiffies;
7615
Harshitha Ramamurthyae136702016-12-12 15:44:16 -08007616 if ((pf->flags & I40E_FLAG_LINK_POLLING_ENABLED) ||
7617 (pf->flags & I40E_FLAG_TEMP_LINK_POLLING))
Shannon Nelson9ac77262015-08-27 11:42:40 -04007618 i40e_link_event(pf);
Shannon Nelson21536712014-10-25 10:35:25 +00007619
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007620 /* Update the stats for active netdevs so the network stack
7621 * can look at updated numbers whenever it cares to
7622 */
Mitch Williams505682c2014-05-20 08:01:37 +00007623 for (i = 0; i < pf->num_alloc_vsi; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007624 if (pf->vsi[i] && pf->vsi[i]->netdev)
7625 i40e_update_stats(pf->vsi[i]);
7626
Anjali Singhai Jaind1a8d272015-07-23 16:54:40 -04007627 if (pf->flags & I40E_FLAG_VEB_STATS_ENABLED) {
7628 /* Update the stats for the active switching components */
7629 for (i = 0; i < I40E_MAX_VEB; i++)
7630 if (pf->veb[i])
7631 i40e_update_veb_stats(pf->veb[i]);
7632 }
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00007633
Jacob Keller61189552017-05-03 10:29:01 -07007634 i40e_ptp_rx_hang(pf);
Jacob Keller0bc07062017-05-03 10:29:02 -07007635 i40e_ptp_tx_hang(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007636}
7637
7638/**
7639 * i40e_reset_subtask - Set up for resetting the device and driver
7640 * @pf: board private structure
7641 **/
7642static void i40e_reset_subtask(struct i40e_pf *pf)
7643{
7644 u32 reset_flags = 0;
7645
Jacob Keller0da36b92017-04-19 09:25:55 -04007646 if (test_bit(__I40E_REINIT_REQUESTED, pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08007647 reset_flags |= BIT(__I40E_REINIT_REQUESTED);
Jacob Keller0da36b92017-04-19 09:25:55 -04007648 clear_bit(__I40E_REINIT_REQUESTED, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007649 }
Jacob Keller0da36b92017-04-19 09:25:55 -04007650 if (test_bit(__I40E_PF_RESET_REQUESTED, pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08007651 reset_flags |= BIT(__I40E_PF_RESET_REQUESTED);
Jacob Keller0da36b92017-04-19 09:25:55 -04007652 clear_bit(__I40E_PF_RESET_REQUESTED, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007653 }
Jacob Keller0da36b92017-04-19 09:25:55 -04007654 if (test_bit(__I40E_CORE_RESET_REQUESTED, pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08007655 reset_flags |= BIT(__I40E_CORE_RESET_REQUESTED);
Jacob Keller0da36b92017-04-19 09:25:55 -04007656 clear_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007657 }
Jacob Keller0da36b92017-04-19 09:25:55 -04007658 if (test_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08007659 reset_flags |= BIT(__I40E_GLOBAL_RESET_REQUESTED);
Jacob Keller0da36b92017-04-19 09:25:55 -04007660 clear_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007661 }
Mauro S. M. Rodrigues9e6c9c02017-05-12 23:26:56 -03007662 if (test_bit(__I40E_DOWN_REQUESTED, pf->state)) {
7663 reset_flags |= BIT(__I40E_DOWN_REQUESTED);
7664 clear_bit(__I40E_DOWN_REQUESTED, pf->state);
Neerav Parikhb5d06f02014-06-03 23:50:17 +00007665 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007666
7667 /* If there's a recovery already waiting, it takes
7668 * precedence before starting a new reset sequence.
7669 */
Jacob Keller0da36b92017-04-19 09:25:55 -04007670 if (test_bit(__I40E_RESET_INTR_RECEIVED, pf->state)) {
Maciej Sosin373149f2017-04-05 07:50:55 -04007671 i40e_prep_for_reset(pf, false);
7672 i40e_reset(pf);
7673 i40e_rebuild(pf, false, false);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007674 }
7675
7676 /* If we're already down or resetting, just bail */
7677 if (reset_flags &&
Mauro S. M. Rodrigues9e6c9c02017-05-12 23:26:56 -03007678 !test_bit(__I40E_DOWN, pf->state) &&
Jacob Keller0da36b92017-04-19 09:25:55 -04007679 !test_bit(__I40E_CONFIG_BUSY, pf->state)) {
Jacob Kellerdfc4ff62017-06-07 05:43:13 -04007680 i40e_do_reset(pf, reset_flags, false);
Maciej Sosin373149f2017-04-05 07:50:55 -04007681 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007682}
7683
7684/**
7685 * i40e_handle_link_event - Handle link event
7686 * @pf: board private structure
7687 * @e: event info posted on ARQ
7688 **/
7689static void i40e_handle_link_event(struct i40e_pf *pf,
7690 struct i40e_arq_event_info *e)
7691{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007692 struct i40e_aqc_get_link_status *status =
7693 (struct i40e_aqc_get_link_status *)&e->desc.params.raw;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007694
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00007695 /* Do a new status request to re-enable LSE reporting
7696 * and load new status information into the hw struct
7697 * This completely ignores any state information
7698 * in the ARQ event info, instead choosing to always
7699 * issue the AQ update link status command.
7700 */
7701 i40e_link_event(pf);
7702
Filip Sadowski9a858172017-08-29 05:32:37 -04007703 /* Check if module meets thermal requirements */
7704 if (status->phy_type == I40E_PHY_TYPE_NOT_SUPPORTED_HIGH_TEMP) {
Carolyn Wyborny7b592f62014-07-10 07:58:19 +00007705 dev_err(&pf->pdev->dev,
Filip Sadowski9a858172017-08-29 05:32:37 -04007706 "Rx/Tx is disabled on this device because the module does not meet thermal requirements.\n");
7707 dev_err(&pf->pdev->dev,
7708 "Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules.\n");
7709 } else {
7710 /* check for unqualified module, if link is down, suppress
7711 * the message if link was forced to be down.
7712 */
7713 if ((status->link_info & I40E_AQ_MEDIA_AVAILABLE) &&
7714 (!(status->an_info & I40E_AQ_QUALIFIED_MODULE)) &&
7715 (!(status->link_info & I40E_AQ_LINK_UP)) &&
7716 (!(pf->flags & I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED))) {
7717 dev_err(&pf->pdev->dev,
7718 "Rx/Tx is disabled on this device because an unsupported SFP module type was detected.\n");
7719 dev_err(&pf->pdev->dev,
7720 "Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules.\n");
7721 }
7722 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007723}
7724
7725/**
7726 * i40e_clean_adminq_subtask - Clean the AdminQ rings
7727 * @pf: board private structure
7728 **/
7729static void i40e_clean_adminq_subtask(struct i40e_pf *pf)
7730{
7731 struct i40e_arq_event_info event;
7732 struct i40e_hw *hw = &pf->hw;
7733 u16 pending, i = 0;
7734 i40e_status ret;
7735 u16 opcode;
Shannon Nelson86df2422014-05-20 08:01:35 +00007736 u32 oldval;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007737 u32 val;
7738
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00007739 /* Do not run clean AQ when PF reset fails */
Jacob Keller0da36b92017-04-19 09:25:55 -04007740 if (test_bit(__I40E_RESET_FAILED, pf->state))
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00007741 return;
7742
Shannon Nelson86df2422014-05-20 08:01:35 +00007743 /* check for error indications */
7744 val = rd32(&pf->hw, pf->hw.aq.arq.len);
7745 oldval = val;
7746 if (val & I40E_PF_ARQLEN_ARQVFE_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08007747 if (hw->debug_mask & I40E_DEBUG_AQ)
7748 dev_info(&pf->pdev->dev, "ARQ VF Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00007749 val &= ~I40E_PF_ARQLEN_ARQVFE_MASK;
7750 }
7751 if (val & I40E_PF_ARQLEN_ARQOVFL_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08007752 if (hw->debug_mask & I40E_DEBUG_AQ)
7753 dev_info(&pf->pdev->dev, "ARQ Overflow Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00007754 val &= ~I40E_PF_ARQLEN_ARQOVFL_MASK;
Mitch Williams1d0a4ad2015-12-23 12:05:48 -08007755 pf->arq_overflows++;
Shannon Nelson86df2422014-05-20 08:01:35 +00007756 }
7757 if (val & I40E_PF_ARQLEN_ARQCRIT_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08007758 if (hw->debug_mask & I40E_DEBUG_AQ)
7759 dev_info(&pf->pdev->dev, "ARQ Critical Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00007760 val &= ~I40E_PF_ARQLEN_ARQCRIT_MASK;
7761 }
7762 if (oldval != val)
7763 wr32(&pf->hw, pf->hw.aq.arq.len, val);
7764
7765 val = rd32(&pf->hw, pf->hw.aq.asq.len);
7766 oldval = val;
7767 if (val & I40E_PF_ATQLEN_ATQVFE_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08007768 if (pf->hw.debug_mask & I40E_DEBUG_AQ)
7769 dev_info(&pf->pdev->dev, "ASQ VF Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00007770 val &= ~I40E_PF_ATQLEN_ATQVFE_MASK;
7771 }
7772 if (val & I40E_PF_ATQLEN_ATQOVFL_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08007773 if (pf->hw.debug_mask & I40E_DEBUG_AQ)
7774 dev_info(&pf->pdev->dev, "ASQ Overflow Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00007775 val &= ~I40E_PF_ATQLEN_ATQOVFL_MASK;
7776 }
7777 if (val & I40E_PF_ATQLEN_ATQCRIT_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08007778 if (pf->hw.debug_mask & I40E_DEBUG_AQ)
7779 dev_info(&pf->pdev->dev, "ASQ Critical Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00007780 val &= ~I40E_PF_ATQLEN_ATQCRIT_MASK;
7781 }
7782 if (oldval != val)
7783 wr32(&pf->hw, pf->hw.aq.asq.len, val);
7784
Mitch Williams1001dc32014-11-11 20:02:19 +00007785 event.buf_len = I40E_MAX_AQ_BUF_SIZE;
7786 event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007787 if (!event.msg_buf)
7788 return;
7789
7790 do {
7791 ret = i40e_clean_arq_element(hw, &event, &pending);
Mitch Williams56497972014-06-04 08:45:18 +00007792 if (ret == I40E_ERR_ADMIN_QUEUE_NO_WORK)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007793 break;
Mitch Williams56497972014-06-04 08:45:18 +00007794 else if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007795 dev_info(&pf->pdev->dev, "ARQ event error %d\n", ret);
7796 break;
7797 }
7798
7799 opcode = le16_to_cpu(event.desc.opcode);
7800 switch (opcode) {
7801
7802 case i40e_aqc_opc_get_link_status:
7803 i40e_handle_link_event(pf, &event);
7804 break;
7805 case i40e_aqc_opc_send_msg_to_pf:
7806 ret = i40e_vc_process_vf_msg(pf,
7807 le16_to_cpu(event.desc.retval),
7808 le32_to_cpu(event.desc.cookie_high),
7809 le32_to_cpu(event.desc.cookie_low),
7810 event.msg_buf,
Mitch Williams1001dc32014-11-11 20:02:19 +00007811 event.msg_len);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007812 break;
7813 case i40e_aqc_opc_lldp_update_mib:
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00007814 dev_dbg(&pf->pdev->dev, "ARQ: Update LLDP MIB event received\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08007815#ifdef CONFIG_I40E_DCB
7816 rtnl_lock();
7817 ret = i40e_handle_lldp_event(pf, &event);
7818 rtnl_unlock();
7819#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007820 break;
7821 case i40e_aqc_opc_event_lan_overflow:
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00007822 dev_dbg(&pf->pdev->dev, "ARQ LAN queue overflow event received\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007823 i40e_handle_lan_overflow_event(pf, &event);
7824 break;
Shannon Nelson0467bc92013-12-18 13:45:58 +00007825 case i40e_aqc_opc_send_msg_to_peer:
7826 dev_info(&pf->pdev->dev, "ARQ: Msg from other pf\n");
7827 break;
Shannon Nelson91a0f932015-03-19 14:32:01 -07007828 case i40e_aqc_opc_nvm_erase:
7829 case i40e_aqc_opc_nvm_update:
Michal Kosiarz00ada502015-11-19 11:34:20 -08007830 case i40e_aqc_opc_oem_post_update:
Shannon Nelson6e93d0c2016-01-15 14:33:18 -08007831 i40e_debug(&pf->hw, I40E_DEBUG_NVM,
7832 "ARQ NVM operation 0x%04x completed\n",
7833 opcode);
Shannon Nelson91a0f932015-03-19 14:32:01 -07007834 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007835 default:
7836 dev_info(&pf->pdev->dev,
Shannon Nelson56e5ca62016-03-10 14:59:48 -08007837 "ARQ: Unknown event 0x%04x ignored\n",
Shannon Nelson0467bc92013-12-18 13:45:58 +00007838 opcode);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007839 break;
7840 }
Christopher N Bednarz1fca3262017-03-10 12:22:03 -08007841 } while (i++ < pf->adminq_work_limit);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007842
Christopher N Bednarz1fca3262017-03-10 12:22:03 -08007843 if (i < pf->adminq_work_limit)
Jacob Keller0da36b92017-04-19 09:25:55 -04007844 clear_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state);
Christopher N Bednarz1fca3262017-03-10 12:22:03 -08007845
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007846 /* re-enable Admin queue interrupt cause */
7847 val = rd32(hw, I40E_PFINT_ICR0_ENA);
7848 val |= I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
7849 wr32(hw, I40E_PFINT_ICR0_ENA, val);
7850 i40e_flush(hw);
7851
7852 kfree(event.msg_buf);
7853}
7854
7855/**
Shannon Nelson4eb3f762014-03-06 08:59:58 +00007856 * i40e_verify_eeprom - make sure eeprom is good to use
7857 * @pf: board private structure
7858 **/
7859static void i40e_verify_eeprom(struct i40e_pf *pf)
7860{
7861 int err;
7862
7863 err = i40e_diag_eeprom_test(&pf->hw);
7864 if (err) {
7865 /* retry in case of garbage read */
7866 err = i40e_diag_eeprom_test(&pf->hw);
7867 if (err) {
7868 dev_info(&pf->pdev->dev, "eeprom check failed (%d), Tx/Rx traffic disabled\n",
7869 err);
Jacob Keller0da36b92017-04-19 09:25:55 -04007870 set_bit(__I40E_BAD_EEPROM, pf->state);
Shannon Nelson4eb3f762014-03-06 08:59:58 +00007871 }
7872 }
7873
Jacob Keller0da36b92017-04-19 09:25:55 -04007874 if (!err && test_bit(__I40E_BAD_EEPROM, pf->state)) {
Shannon Nelson4eb3f762014-03-06 08:59:58 +00007875 dev_info(&pf->pdev->dev, "eeprom check passed, Tx/Rx traffic enabled\n");
Jacob Keller0da36b92017-04-19 09:25:55 -04007876 clear_bit(__I40E_BAD_EEPROM, pf->state);
Shannon Nelson4eb3f762014-03-06 08:59:58 +00007877 }
7878}
7879
7880/**
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00007881 * i40e_enable_pf_switch_lb
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00007882 * @pf: pointer to the PF structure
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00007883 *
7884 * enable switch loop back or die - no point in a return value
7885 **/
7886static void i40e_enable_pf_switch_lb(struct i40e_pf *pf)
7887{
7888 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
7889 struct i40e_vsi_context ctxt;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04007890 int ret;
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00007891
7892 ctxt.seid = pf->main_vsi_seid;
7893 ctxt.pf_num = pf->hw.pf_id;
7894 ctxt.vf_num = 0;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04007895 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
7896 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00007897 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04007898 "couldn't get PF vsi config, err %s aq_err %s\n",
7899 i40e_stat_str(&pf->hw, ret),
7900 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00007901 return;
7902 }
7903 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
7904 ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
7905 ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
7906
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04007907 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
7908 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00007909 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04007910 "update vsi switch failed, err %s aq_err %s\n",
7911 i40e_stat_str(&pf->hw, ret),
7912 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00007913 }
7914}
7915
7916/**
7917 * i40e_disable_pf_switch_lb
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00007918 * @pf: pointer to the PF structure
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00007919 *
7920 * disable switch loop back or die - no point in a return value
7921 **/
7922static void i40e_disable_pf_switch_lb(struct i40e_pf *pf)
7923{
7924 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
7925 struct i40e_vsi_context ctxt;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04007926 int ret;
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00007927
7928 ctxt.seid = pf->main_vsi_seid;
7929 ctxt.pf_num = pf->hw.pf_id;
7930 ctxt.vf_num = 0;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04007931 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
7932 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00007933 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04007934 "couldn't get PF vsi config, err %s aq_err %s\n",
7935 i40e_stat_str(&pf->hw, ret),
7936 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00007937 return;
7938 }
7939 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
7940 ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
7941 ctxt.info.switch_id &= ~cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
7942
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04007943 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
7944 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00007945 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04007946 "update vsi switch failed, err %s aq_err %s\n",
7947 i40e_stat_str(&pf->hw, ret),
7948 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00007949 }
7950}
7951
7952/**
Neerav Parikh51616012015-02-06 08:52:14 +00007953 * i40e_config_bridge_mode - Configure the HW bridge mode
7954 * @veb: pointer to the bridge instance
7955 *
7956 * Configure the loop back mode for the LAN VSI that is downlink to the
7957 * specified HW bridge instance. It is expected this function is called
7958 * when a new HW bridge is instantiated.
7959 **/
7960static void i40e_config_bridge_mode(struct i40e_veb *veb)
7961{
7962 struct i40e_pf *pf = veb->pf;
7963
Shannon Nelson6dec1012015-09-28 14:12:30 -04007964 if (pf->hw.debug_mask & I40E_DEBUG_LAN)
7965 dev_info(&pf->pdev->dev, "enabling bridge mode: %s\n",
7966 veb->bridge_mode == BRIDGE_MODE_VEPA ? "VEPA" : "VEB");
Neerav Parikh51616012015-02-06 08:52:14 +00007967 if (veb->bridge_mode & BRIDGE_MODE_VEPA)
7968 i40e_disable_pf_switch_lb(pf);
7969 else
7970 i40e_enable_pf_switch_lb(pf);
7971}
7972
7973/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007974 * i40e_reconstitute_veb - rebuild the VEB and anything connected to it
7975 * @veb: pointer to the VEB instance
7976 *
7977 * This is a recursive function that first builds the attached VSIs then
7978 * recurses in to build the next layer of VEB. We track the connections
7979 * through our own index numbers because the seid's from the HW could
7980 * change across the reset.
7981 **/
7982static int i40e_reconstitute_veb(struct i40e_veb *veb)
7983{
7984 struct i40e_vsi *ctl_vsi = NULL;
7985 struct i40e_pf *pf = veb->pf;
7986 int v, veb_idx;
7987 int ret;
7988
7989 /* build VSI that owns this VEB, temporarily attached to base VEB */
Mitch Williams505682c2014-05-20 08:01:37 +00007990 for (v = 0; v < pf->num_alloc_vsi && !ctl_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007991 if (pf->vsi[v] &&
7992 pf->vsi[v]->veb_idx == veb->idx &&
7993 pf->vsi[v]->flags & I40E_VSI_FLAG_VEB_OWNER) {
7994 ctl_vsi = pf->vsi[v];
7995 break;
7996 }
7997 }
7998 if (!ctl_vsi) {
7999 dev_info(&pf->pdev->dev,
8000 "missing owner VSI for veb_idx %d\n", veb->idx);
8001 ret = -ENOENT;
8002 goto end_reconstitute;
8003 }
8004 if (ctl_vsi != pf->vsi[pf->lan_vsi])
8005 ctl_vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
8006 ret = i40e_add_vsi(ctl_vsi);
8007 if (ret) {
8008 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008009 "rebuild of veb_idx %d owner VSI failed: %d\n",
8010 veb->idx, ret);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008011 goto end_reconstitute;
8012 }
8013 i40e_vsi_reset_stats(ctl_vsi);
8014
8015 /* create the VEB in the switch and move the VSI onto the VEB */
8016 ret = i40e_add_veb(veb, ctl_vsi);
8017 if (ret)
8018 goto end_reconstitute;
8019
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07008020 if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
8021 veb->bridge_mode = BRIDGE_MODE_VEB;
8022 else
8023 veb->bridge_mode = BRIDGE_MODE_VEPA;
Neerav Parikh51616012015-02-06 08:52:14 +00008024 i40e_config_bridge_mode(veb);
Anjali Singhai Jainb64ba082014-11-13 03:06:15 +00008025
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008026 /* create the remaining VSIs attached to this VEB */
Mitch Williams505682c2014-05-20 08:01:37 +00008027 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008028 if (!pf->vsi[v] || pf->vsi[v] == ctl_vsi)
8029 continue;
8030
8031 if (pf->vsi[v]->veb_idx == veb->idx) {
8032 struct i40e_vsi *vsi = pf->vsi[v];
Jesse Brandeburg6995b362015-08-28 17:55:54 -04008033
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008034 vsi->uplink_seid = veb->seid;
8035 ret = i40e_add_vsi(vsi);
8036 if (ret) {
8037 dev_info(&pf->pdev->dev,
8038 "rebuild of vsi_idx %d failed: %d\n",
8039 v, ret);
8040 goto end_reconstitute;
8041 }
8042 i40e_vsi_reset_stats(vsi);
8043 }
8044 }
8045
8046 /* create any VEBs attached to this VEB - RECURSION */
8047 for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
8048 if (pf->veb[veb_idx] && pf->veb[veb_idx]->veb_idx == veb->idx) {
8049 pf->veb[veb_idx]->uplink_seid = veb->seid;
8050 ret = i40e_reconstitute_veb(pf->veb[veb_idx]);
8051 if (ret)
8052 break;
8053 }
8054 }
8055
8056end_reconstitute:
8057 return ret;
8058}
8059
8060/**
8061 * i40e_get_capabilities - get info about the HW
8062 * @pf: the PF struct
8063 **/
8064static int i40e_get_capabilities(struct i40e_pf *pf)
8065{
8066 struct i40e_aqc_list_capabilities_element_resp *cap_buf;
8067 u16 data_size;
8068 int buf_len;
8069 int err;
8070
8071 buf_len = 40 * sizeof(struct i40e_aqc_list_capabilities_element_resp);
8072 do {
8073 cap_buf = kzalloc(buf_len, GFP_KERNEL);
8074 if (!cap_buf)
8075 return -ENOMEM;
8076
8077 /* this loads the data into the hw struct for us */
8078 err = i40e_aq_discover_capabilities(&pf->hw, cap_buf, buf_len,
8079 &data_size,
8080 i40e_aqc_opc_list_func_capabilities,
8081 NULL);
8082 /* data loaded, buffer no longer needed */
8083 kfree(cap_buf);
8084
8085 if (pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOMEM) {
8086 /* retry with a larger buffer */
8087 buf_len = data_size;
8088 } else if (pf->hw.aq.asq_last_status != I40E_AQ_RC_OK) {
8089 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008090 "capability discovery failed, err %s aq_err %s\n",
8091 i40e_stat_str(&pf->hw, err),
8092 i40e_aq_str(&pf->hw,
8093 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008094 return -ENODEV;
8095 }
8096 } while (err);
8097
8098 if (pf->hw.debug_mask & I40E_DEBUG_USER)
8099 dev_info(&pf->pdev->dev,
8100 "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",
8101 pf->hw.pf_id, pf->hw.func_caps.num_vfs,
8102 pf->hw.func_caps.num_msix_vectors,
8103 pf->hw.func_caps.num_msix_vectors_vf,
8104 pf->hw.func_caps.fd_filters_guaranteed,
8105 pf->hw.func_caps.fd_filters_best_effort,
8106 pf->hw.func_caps.num_tx_qp,
8107 pf->hw.func_caps.num_vsis);
8108
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00008109#define DEF_NUM_VSI (1 + (pf->hw.func_caps.fcoe ? 1 : 0) \
8110 + pf->hw.func_caps.num_vfs)
8111 if (pf->hw.revision_id == 0 && (DEF_NUM_VSI > pf->hw.func_caps.num_vsis)) {
8112 dev_info(&pf->pdev->dev,
8113 "got num_vsis %d, setting num_vsis to %d\n",
8114 pf->hw.func_caps.num_vsis, DEF_NUM_VSI);
8115 pf->hw.func_caps.num_vsis = DEF_NUM_VSI;
8116 }
8117
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008118 return 0;
8119}
8120
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008121static int i40e_vsi_clear(struct i40e_vsi *vsi);
8122
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008123/**
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008124 * i40e_fdir_sb_setup - initialize the Flow Director resources for Sideband
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008125 * @pf: board private structure
8126 **/
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008127static void i40e_fdir_sb_setup(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008128{
8129 struct i40e_vsi *vsi;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008130
Jesse Brandeburg407e0632014-06-03 23:50:12 +00008131 /* quick workaround for an NVM issue that leaves a critical register
8132 * uninitialized
8133 */
8134 if (!rd32(&pf->hw, I40E_GLQF_HKEY(0))) {
8135 static const u32 hkey[] = {
8136 0xe640d33f, 0xcdfe98ab, 0x73fa7161, 0x0d7a7d36,
8137 0xeacb7d61, 0xaa4f05b6, 0x9c5c89ed, 0xfc425ddb,
8138 0xa4654832, 0xfc7461d4, 0x8f827619, 0xf5c63c21,
8139 0x95b3a76d};
Alexander Duyck4b816442016-10-11 15:26:53 -07008140 int i;
Jesse Brandeburg407e0632014-06-03 23:50:12 +00008141
8142 for (i = 0; i <= I40E_GLQF_HKEY_MAX_INDEX; i++)
8143 wr32(&pf->hw, I40E_GLQF_HKEY(i), hkey[i]);
8144 }
8145
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008146 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008147 return;
8148
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008149 /* find existing VSI and see if it needs configuring */
Alexander Duyck4b816442016-10-11 15:26:53 -07008150 vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008151
8152 /* create a new VSI if none exists */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008153 if (!vsi) {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008154 vsi = i40e_vsi_setup(pf, I40E_VSI_FDIR,
8155 pf->vsi[pf->lan_vsi]->seid, 0);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008156 if (!vsi) {
8157 dev_info(&pf->pdev->dev, "Couldn't create FDir VSI\n");
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00008158 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
8159 return;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008160 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008161 }
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00008162
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008163 i40e_vsi_setup_irqhandler(vsi, i40e_fdir_clean_ring);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008164}
8165
8166/**
8167 * i40e_fdir_teardown - release the Flow Director resources
8168 * @pf: board private structure
8169 **/
8170static void i40e_fdir_teardown(struct i40e_pf *pf)
8171{
Alexander Duyck4b816442016-10-11 15:26:53 -07008172 struct i40e_vsi *vsi;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008173
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00008174 i40e_fdir_filter_exit(pf);
Alexander Duyck4b816442016-10-11 15:26:53 -07008175 vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
8176 if (vsi)
8177 i40e_vsi_release(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008178}
8179
8180/**
Amritha Nambiar8f88b302017-09-07 04:00:17 -07008181 * i40e_rebuild_channels - Rebuilds channel VSIs if they existed before reset
8182 * @vsi: PF main vsi
8183 *
8184 * Rebuilds channel VSIs if they existed before reset
8185 **/
8186static int i40e_rebuild_channels(struct i40e_vsi *vsi)
8187{
8188 struct i40e_channel *ch, *ch_tmp;
8189 i40e_status ret;
8190
8191 if (list_empty(&vsi->ch_list))
8192 return 0;
8193
8194 list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
8195 if (!ch->initialized)
8196 break;
8197 /* Proceed with creation of channel (VMDq2) VSI */
8198 ret = i40e_add_channel(vsi->back, vsi->uplink_seid, ch);
8199 if (ret) {
8200 dev_info(&vsi->back->pdev->dev,
8201 "failed to rebuild channels using uplink_seid %u\n",
8202 vsi->uplink_seid);
8203 return ret;
8204 }
Amritha Nambiar2027d4d2017-09-07 04:00:32 -07008205 if (ch->max_tx_rate) {
Alan Brady6c32e0d2017-10-09 15:48:45 -07008206 u64 credits = ch->max_tx_rate;
8207
Amritha Nambiar2027d4d2017-09-07 04:00:32 -07008208 if (i40e_set_bw_limit(vsi, ch->seid,
8209 ch->max_tx_rate))
8210 return -EINVAL;
8211
Alan Brady6c32e0d2017-10-09 15:48:45 -07008212 do_div(credits, I40E_BW_CREDIT_DIVISOR);
Amritha Nambiar2027d4d2017-09-07 04:00:32 -07008213 dev_dbg(&vsi->back->pdev->dev,
8214 "Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
8215 ch->max_tx_rate,
Alan Brady6c32e0d2017-10-09 15:48:45 -07008216 credits,
Amritha Nambiar2027d4d2017-09-07 04:00:32 -07008217 ch->seid);
8218 }
Amritha Nambiar8f88b302017-09-07 04:00:17 -07008219 }
8220 return 0;
8221}
8222
8223/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00008224 * i40e_prep_for_reset - prep for the core to reset
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008225 * @pf: board private structure
Maciej Sosin373149f2017-04-05 07:50:55 -04008226 * @lock_acquired: indicates whether or not the lock has been acquired
8227 * before this function was called.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008228 *
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00008229 * Close up the VFs and other things in prep for PF Reset.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00008230 **/
Maciej Sosin373149f2017-04-05 07:50:55 -04008231static void i40e_prep_for_reset(struct i40e_pf *pf, bool lock_acquired)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008232{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008233 struct i40e_hw *hw = &pf->hw;
Shannon Nelson60442de2014-04-23 04:50:13 +00008234 i40e_status ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008235 u32 v;
8236
Jacob Keller0da36b92017-04-19 09:25:55 -04008237 clear_bit(__I40E_RESET_INTR_RECEIVED, pf->state);
8238 if (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
Shannon Nelson23cfbe02014-06-04 01:23:14 +00008239 return;
Mitch Williamsd3ce57342016-03-10 14:59:46 -08008240 if (i40e_check_asq_alive(&pf->hw))
8241 i40e_vc_notify_reset(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008242
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00008243 dev_dbg(&pf->pdev->dev, "Tearing down internal switch for reset\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008244
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008245 /* quiesce the VSIs and their queues that are not already DOWN */
Maciej Sosin373149f2017-04-05 07:50:55 -04008246 /* pf_quiesce_all_vsi modifies netdev structures -rtnl_lock needed */
8247 if (!lock_acquired)
8248 rtnl_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008249 i40e_pf_quiesce_all_vsi(pf);
Maciej Sosin373149f2017-04-05 07:50:55 -04008250 if (!lock_acquired)
8251 rtnl_unlock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008252
Mitch Williams505682c2014-05-20 08:01:37 +00008253 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008254 if (pf->vsi[v])
8255 pf->vsi[v]->seid = 0;
8256 }
8257
8258 i40e_shutdown_adminq(&pf->hw);
8259
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00008260 /* call shutdown HMC */
Shannon Nelson60442de2014-04-23 04:50:13 +00008261 if (hw->hmc.hmc_obj) {
8262 ret = i40e_shutdown_lan_hmc(hw);
Shannon Nelson23cfbe02014-06-04 01:23:14 +00008263 if (ret)
Shannon Nelson60442de2014-04-23 04:50:13 +00008264 dev_warn(&pf->pdev->dev,
8265 "shutdown_lan_hmc failed: %d\n", ret);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00008266 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00008267}
8268
8269/**
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00008270 * i40e_send_version - update firmware with driver version
8271 * @pf: PF struct
8272 */
8273static void i40e_send_version(struct i40e_pf *pf)
8274{
8275 struct i40e_driver_version dv;
8276
8277 dv.major_version = DRV_VERSION_MAJOR;
8278 dv.minor_version = DRV_VERSION_MINOR;
8279 dv.build_version = DRV_VERSION_BUILD;
8280 dv.subbuild_version = 0;
Rickard Strandqvist35a7d802014-07-29 09:26:25 +00008281 strlcpy(dv.driver_string, DRV_VERSION, sizeof(dv.driver_string));
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00008282 i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
8283}
8284
8285/**
Filip Sadowski5bbb2e22017-06-07 05:43:09 -04008286 * i40e_get_oem_version - get OEM specific version information
8287 * @hw: pointer to the hardware structure
8288 **/
8289static void i40e_get_oem_version(struct i40e_hw *hw)
8290{
8291 u16 block_offset = 0xffff;
8292 u16 block_length = 0;
8293 u16 capabilities = 0;
8294 u16 gen_snap = 0;
8295 u16 release = 0;
8296
8297#define I40E_SR_NVM_OEM_VERSION_PTR 0x1B
8298#define I40E_NVM_OEM_LENGTH_OFFSET 0x00
8299#define I40E_NVM_OEM_CAPABILITIES_OFFSET 0x01
8300#define I40E_NVM_OEM_GEN_OFFSET 0x02
8301#define I40E_NVM_OEM_RELEASE_OFFSET 0x03
8302#define I40E_NVM_OEM_CAPABILITIES_MASK 0x000F
8303#define I40E_NVM_OEM_LENGTH 3
8304
8305 /* Check if pointer to OEM version block is valid. */
8306 i40e_read_nvm_word(hw, I40E_SR_NVM_OEM_VERSION_PTR, &block_offset);
8307 if (block_offset == 0xffff)
8308 return;
8309
8310 /* Check if OEM version block has correct length. */
8311 i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_LENGTH_OFFSET,
8312 &block_length);
8313 if (block_length < I40E_NVM_OEM_LENGTH)
8314 return;
8315
8316 /* Check if OEM version format is as expected. */
8317 i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_CAPABILITIES_OFFSET,
8318 &capabilities);
8319 if ((capabilities & I40E_NVM_OEM_CAPABILITIES_MASK) != 0)
8320 return;
8321
8322 i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_GEN_OFFSET,
8323 &gen_snap);
8324 i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_RELEASE_OFFSET,
8325 &release);
8326 hw->nvm.oem_ver = (gen_snap << I40E_OEM_SNAP_SHIFT) | release;
8327 hw->nvm.eetrack = I40E_OEM_EETRACK_ID;
8328}
8329
8330/**
Maciej Sosin373149f2017-04-05 07:50:55 -04008331 * i40e_reset - wait for core reset to finish reset, reset pf if corer not seen
8332 * @pf: board private structure
8333 **/
8334static int i40e_reset(struct i40e_pf *pf)
8335{
8336 struct i40e_hw *hw = &pf->hw;
8337 i40e_status ret;
8338
8339 ret = i40e_pf_reset(hw);
8340 if (ret) {
8341 dev_info(&pf->pdev->dev, "PF reset failed, %d\n", ret);
Jacob Keller0da36b92017-04-19 09:25:55 -04008342 set_bit(__I40E_RESET_FAILED, pf->state);
8343 clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
Maciej Sosin373149f2017-04-05 07:50:55 -04008344 } else {
8345 pf->pfr_count++;
8346 }
8347 return ret;
8348}
8349
8350/**
8351 * i40e_rebuild - rebuild using a saved config
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00008352 * @pf: board private structure
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00008353 * @reinit: if the Main VSI needs to re-initialized.
Maciej Sosin373149f2017-04-05 07:50:55 -04008354 * @lock_acquired: indicates whether or not the lock has been acquired
8355 * before this function was called.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00008356 **/
Maciej Sosin373149f2017-04-05 07:50:55 -04008357static void i40e_rebuild(struct i40e_pf *pf, bool reinit, bool lock_acquired)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00008358{
Amritha Nambiar2027d4d2017-09-07 04:00:32 -07008359 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00008360 struct i40e_hw *hw = &pf->hw;
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00008361 u8 set_fc_aq_fail = 0;
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00008362 i40e_status ret;
Anjali Singhai Jain4f2f017c2015-10-21 19:47:07 -04008363 u32 val;
Maciej Sosin373149f2017-04-05 07:50:55 -04008364 int v;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008365
Mauro S. M. Rodrigues9e6c9c02017-05-12 23:26:56 -03008366 if (test_bit(__I40E_DOWN, pf->state))
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00008367 goto clear_recovery;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00008368 dev_dbg(&pf->pdev->dev, "Rebuilding internal switch\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008369
8370 /* rebuild the basics for the AdminQ, HMC, and initial HW switch */
8371 ret = i40e_init_adminq(&pf->hw);
8372 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008373 dev_info(&pf->pdev->dev, "Rebuild AdminQ failed, err %s aq_err %s\n",
8374 i40e_stat_str(&pf->hw, ret),
8375 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00008376 goto clear_recovery;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008377 }
Filip Sadowski5bbb2e22017-06-07 05:43:09 -04008378 i40e_get_oem_version(&pf->hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008379
Shannon Nelson4eb3f762014-03-06 08:59:58 +00008380 /* re-verify the eeprom if we just had an EMP reset */
Jacob Keller0da36b92017-04-19 09:25:55 -04008381 if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state))
Shannon Nelson4eb3f762014-03-06 08:59:58 +00008382 i40e_verify_eeprom(pf);
Shannon Nelson4eb3f762014-03-06 08:59:58 +00008383
Shannon Nelsone78ac4bf2014-05-10 04:49:09 +00008384 i40e_clear_pxe_mode(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008385 ret = i40e_get_capabilities(pf);
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008386 if (ret)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008387 goto end_core_reset;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008388
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008389 ret = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
Alexander Duyckc76cb6e2017-02-21 15:55:48 -08008390 hw->func_caps.num_rx_qp, 0, 0);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008391 if (ret) {
8392 dev_info(&pf->pdev->dev, "init_lan_hmc failed: %d\n", ret);
8393 goto end_core_reset;
8394 }
8395 ret = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
8396 if (ret) {
8397 dev_info(&pf->pdev->dev, "configure_lan_hmc failed: %d\n", ret);
8398 goto end_core_reset;
8399 }
8400
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08008401#ifdef CONFIG_I40E_DCB
8402 ret = i40e_init_pf_dcb(pf);
8403 if (ret) {
Shannon Nelsonaebfc812014-12-11 07:06:38 +00008404 dev_info(&pf->pdev->dev, "DCB init failed %d, disabled\n", ret);
8405 pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
8406 /* Continue without DCB enabled */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08008407 }
8408#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008409 /* do basic switch setup */
Maciej Sosin373149f2017-04-05 07:50:55 -04008410 if (!lock_acquired)
8411 rtnl_lock();
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00008412 ret = i40e_setup_pf_switch(pf, reinit);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008413 if (ret)
Maciej Sosin373149f2017-04-05 07:50:55 -04008414 goto end_unlock;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008415
Shannon Nelson2f0aff42016-01-04 10:33:08 -08008416 /* The driver only wants link up/down and module qualification
8417 * reports from firmware. Note the negative logic.
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +00008418 */
8419 ret = i40e_aq_set_phy_int_mask(&pf->hw,
Shannon Nelson2f0aff42016-01-04 10:33:08 -08008420 ~(I40E_AQ_EVENT_LINK_UPDOWN |
Shannon Nelson867a79e2016-03-18 12:18:15 -07008421 I40E_AQ_EVENT_MEDIA_NA |
Shannon Nelson2f0aff42016-01-04 10:33:08 -08008422 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +00008423 if (ret)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008424 dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
8425 i40e_stat_str(&pf->hw, ret),
8426 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +00008427
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00008428 /* make sure our flow control settings are restored */
8429 ret = i40e_set_fc(&pf->hw, &set_fc_aq_fail, true);
8430 if (ret)
Neerav Parikh8279e492015-09-03 17:18:50 -04008431 dev_dbg(&pf->pdev->dev, "setting flow control: ret = %s last_status = %s\n",
8432 i40e_stat_str(&pf->hw, ret),
8433 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00008434
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008435 /* Rebuild the VSIs and VEBs that existed before reset.
8436 * They are still in our local switch element arrays, so only
8437 * need to rebuild the switch model in the HW.
8438 *
8439 * If there were VEBs but the reconstitution failed, we'll try
8440 * try to recover minimal use by getting the basic PF VSI working.
8441 */
Amritha Nambiar2027d4d2017-09-07 04:00:32 -07008442 if (vsi->uplink_seid != pf->mac_seid) {
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00008443 dev_dbg(&pf->pdev->dev, "attempting to rebuild switch\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008444 /* find the one VEB connected to the MAC, and find orphans */
8445 for (v = 0; v < I40E_MAX_VEB; v++) {
8446 if (!pf->veb[v])
8447 continue;
8448
8449 if (pf->veb[v]->uplink_seid == pf->mac_seid ||
8450 pf->veb[v]->uplink_seid == 0) {
8451 ret = i40e_reconstitute_veb(pf->veb[v]);
8452
8453 if (!ret)
8454 continue;
8455
8456 /* If Main VEB failed, we're in deep doodoo,
8457 * so give up rebuilding the switch and set up
8458 * for minimal rebuild of PF VSI.
8459 * If orphan failed, we'll report the error
8460 * but try to keep going.
8461 */
8462 if (pf->veb[v]->uplink_seid == pf->mac_seid) {
8463 dev_info(&pf->pdev->dev,
8464 "rebuild of switch failed: %d, will try to set up simple PF connection\n",
8465 ret);
Amritha Nambiar2027d4d2017-09-07 04:00:32 -07008466 vsi->uplink_seid = pf->mac_seid;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008467 break;
8468 } else if (pf->veb[v]->uplink_seid == 0) {
8469 dev_info(&pf->pdev->dev,
8470 "rebuild of orphan VEB failed: %d\n",
8471 ret);
8472 }
8473 }
8474 }
8475 }
8476
Amritha Nambiar2027d4d2017-09-07 04:00:32 -07008477 if (vsi->uplink_seid == pf->mac_seid) {
Shannon Nelsoncde4cbc2014-06-04 01:23:17 +00008478 dev_dbg(&pf->pdev->dev, "attempting to rebuild PF VSI\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008479 /* no VEB, so rebuild only the Main VSI */
Amritha Nambiar2027d4d2017-09-07 04:00:32 -07008480 ret = i40e_add_vsi(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008481 if (ret) {
8482 dev_info(&pf->pdev->dev,
8483 "rebuild of Main VSI failed: %d\n", ret);
Maciej Sosin373149f2017-04-05 07:50:55 -04008484 goto end_unlock;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008485 }
8486 }
8487
Amritha Nambiar2027d4d2017-09-07 04:00:32 -07008488 if (vsi->mqprio_qopt.max_rate[0]) {
Alan Brady6c32e0d2017-10-09 15:48:45 -07008489 u64 max_tx_rate = vsi->mqprio_qopt.max_rate[0];
8490 u64 credits = 0;
Amritha Nambiar2027d4d2017-09-07 04:00:32 -07008491
Alan Brady6c32e0d2017-10-09 15:48:45 -07008492 do_div(max_tx_rate, I40E_BW_MBPS_DIVISOR);
Amritha Nambiar2027d4d2017-09-07 04:00:32 -07008493 ret = i40e_set_bw_limit(vsi, vsi->seid, max_tx_rate);
Alan Brady6c32e0d2017-10-09 15:48:45 -07008494 if (ret)
Amritha Nambiar2027d4d2017-09-07 04:00:32 -07008495 goto end_unlock;
Alan Brady6c32e0d2017-10-09 15:48:45 -07008496
8497 credits = max_tx_rate;
8498 do_div(credits, I40E_BW_CREDIT_DIVISOR);
8499 dev_dbg(&vsi->back->pdev->dev,
8500 "Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
8501 max_tx_rate,
8502 credits,
8503 vsi->seid);
Amritha Nambiar2027d4d2017-09-07 04:00:32 -07008504 }
8505
Amritha Nambiar8f88b302017-09-07 04:00:17 -07008506 /* PF Main VSI is rebuild by now, go ahead and rebuild channel VSIs
8507 * for this main VSI if they exist
8508 */
Amritha Nambiar2027d4d2017-09-07 04:00:32 -07008509 ret = i40e_rebuild_channels(vsi);
Amritha Nambiar8f88b302017-09-07 04:00:17 -07008510 if (ret)
8511 goto end_unlock;
8512
Anjali Singhai Jain4f2f017c2015-10-21 19:47:07 -04008513 /* Reconfigure hardware for allowing smaller MSS in the case
8514 * of TSO, so that we avoid the MDD being fired and causing
8515 * a reset in the case of small MSS+TSO.
8516 */
8517#define I40E_REG_MSS 0x000E64DC
8518#define I40E_REG_MSS_MIN_MASK 0x3FF0000
8519#define I40E_64BYTE_MSS 0x400000
8520 val = rd32(hw, I40E_REG_MSS);
8521 if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
8522 val &= ~I40E_REG_MSS_MIN_MASK;
8523 val |= I40E_64BYTE_MSS;
8524 wr32(hw, I40E_REG_MSS, val);
8525 }
8526
Jacob Kellerd36e41d2017-06-23 04:24:46 -04008527 if (pf->hw_features & I40E_HW_RESTART_AUTONEG) {
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +00008528 msleep(75);
8529 ret = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
8530 if (ret)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008531 dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
8532 i40e_stat_str(&pf->hw, ret),
8533 i40e_aq_str(&pf->hw,
8534 pf->hw.aq.asq_last_status));
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00008535 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008536 /* reinit the misc interrupt */
8537 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
8538 ret = i40e_setup_misc_vector(pf);
8539
Anjali Singhai Jaine7358f52015-10-01 14:37:34 -04008540 /* Add a filter to drop all Flow control frames from any VSI from being
8541 * transmitted. By doing so we stop a malicious VF from sending out
8542 * PAUSE or PFC frames and potentially controlling traffic for other
8543 * PF/VF VSIs.
8544 * The FW can still send Flow control frames if enabled.
8545 */
8546 i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
8547 pf->main_vsi_seid);
8548
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008549 /* restart the VSIs that were rebuilt and running before the reset */
8550 i40e_pf_unquiesce_all_vsi(pf);
8551
Jacob Keller024b05f2017-04-13 04:45:46 -04008552 /* Release the RTNL lock before we start resetting VFs */
8553 if (!lock_acquired)
8554 rtnl_unlock();
8555
Jacob Kellere4b433f2017-04-13 04:45:52 -04008556 i40e_reset_all_vfs(pf, true);
Mitch Williams69f64b22014-02-13 03:48:46 -08008557
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008558 /* tell the firmware that we're starting */
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00008559 i40e_send_version(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008560
Jacob Keller024b05f2017-04-13 04:45:46 -04008561 /* We've already released the lock, so don't do it again */
8562 goto end_core_reset;
8563
Maciej Sosin373149f2017-04-05 07:50:55 -04008564end_unlock:
Jacob Keller024b05f2017-04-13 04:45:46 -04008565 if (!lock_acquired)
8566 rtnl_unlock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008567end_core_reset:
Jacob Keller0da36b92017-04-19 09:25:55 -04008568 clear_bit(__I40E_RESET_FAILED, pf->state);
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00008569clear_recovery:
Jacob Keller0da36b92017-04-19 09:25:55 -04008570 clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008571}
8572
8573/**
Maciej Sosin373149f2017-04-05 07:50:55 -04008574 * i40e_reset_and_rebuild - reset and rebuild using a saved config
8575 * @pf: board private structure
8576 * @reinit: if the Main VSI needs to re-initialized.
8577 * @lock_acquired: indicates whether or not the lock has been acquired
8578 * before this function was called.
8579 **/
8580static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit,
8581 bool lock_acquired)
8582{
8583 int ret;
8584 /* Now we wait for GRST to settle out.
8585 * We don't have to delete the VEBs or VSIs from the hw switch
8586 * because the reset will make them disappear.
8587 */
8588 ret = i40e_reset(pf);
8589 if (!ret)
8590 i40e_rebuild(pf, reinit, lock_acquired);
8591}
8592
8593/**
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00008594 * i40e_handle_reset_warning - prep for the PF to reset, reset and rebuild
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00008595 * @pf: board private structure
8596 *
8597 * Close up the VFs and other things in prep for a Core Reset,
8598 * then get ready to rebuild the world.
Maciej Sosin373149f2017-04-05 07:50:55 -04008599 * @lock_acquired: indicates whether or not the lock has been acquired
8600 * before this function was called.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00008601 **/
Maciej Sosin373149f2017-04-05 07:50:55 -04008602static void i40e_handle_reset_warning(struct i40e_pf *pf, bool lock_acquired)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00008603{
Maciej Sosin373149f2017-04-05 07:50:55 -04008604 i40e_prep_for_reset(pf, lock_acquired);
8605 i40e_reset_and_rebuild(pf, false, lock_acquired);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00008606}
8607
8608/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008609 * i40e_handle_mdd_event
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00008610 * @pf: pointer to the PF structure
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008611 *
8612 * Called from the MDD irq handler to identify possibly malicious vfs
8613 **/
8614static void i40e_handle_mdd_event(struct i40e_pf *pf)
8615{
8616 struct i40e_hw *hw = &pf->hw;
8617 bool mdd_detected = false;
Neerav Parikhdf430b12014-06-04 01:23:15 +00008618 bool pf_mdd_detected = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008619 struct i40e_vf *vf;
8620 u32 reg;
8621 int i;
8622
Jacob Keller0da36b92017-04-19 09:25:55 -04008623 if (!test_bit(__I40E_MDD_EVENT_PENDING, pf->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008624 return;
8625
8626 /* find what triggered the MDD event */
8627 reg = rd32(hw, I40E_GL_MDET_TX);
8628 if (reg & I40E_GL_MDET_TX_VALID_MASK) {
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00008629 u8 pf_num = (reg & I40E_GL_MDET_TX_PF_NUM_MASK) >>
8630 I40E_GL_MDET_TX_PF_NUM_SHIFT;
Mitch Williams2089ad02014-10-17 03:14:53 +00008631 u16 vf_num = (reg & I40E_GL_MDET_TX_VF_NUM_MASK) >>
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00008632 I40E_GL_MDET_TX_VF_NUM_SHIFT;
Dan Carpenter013f6572014-10-22 20:06:29 -07008633 u8 event = (reg & I40E_GL_MDET_TX_EVENT_MASK) >>
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00008634 I40E_GL_MDET_TX_EVENT_SHIFT;
Mitch Williams2089ad02014-10-17 03:14:53 +00008635 u16 queue = ((reg & I40E_GL_MDET_TX_QUEUE_MASK) >>
8636 I40E_GL_MDET_TX_QUEUE_SHIFT) -
8637 pf->hw.func_caps.base_queue;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00008638 if (netif_msg_tx_err(pf))
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00008639 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 +00008640 event, queue, pf_num, vf_num);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008641 wr32(hw, I40E_GL_MDET_TX, 0xffffffff);
8642 mdd_detected = true;
8643 }
8644 reg = rd32(hw, I40E_GL_MDET_RX);
8645 if (reg & I40E_GL_MDET_RX_VALID_MASK) {
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00008646 u8 func = (reg & I40E_GL_MDET_RX_FUNCTION_MASK) >>
8647 I40E_GL_MDET_RX_FUNCTION_SHIFT;
Dan Carpenter013f6572014-10-22 20:06:29 -07008648 u8 event = (reg & I40E_GL_MDET_RX_EVENT_MASK) >>
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00008649 I40E_GL_MDET_RX_EVENT_SHIFT;
Mitch Williams2089ad02014-10-17 03:14:53 +00008650 u16 queue = ((reg & I40E_GL_MDET_RX_QUEUE_MASK) >>
8651 I40E_GL_MDET_RX_QUEUE_SHIFT) -
8652 pf->hw.func_caps.base_queue;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00008653 if (netif_msg_rx_err(pf))
8654 dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on RX queue %d of function 0x%02x\n",
8655 event, queue, func);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008656 wr32(hw, I40E_GL_MDET_RX, 0xffffffff);
8657 mdd_detected = true;
8658 }
8659
Neerav Parikhdf430b12014-06-04 01:23:15 +00008660 if (mdd_detected) {
8661 reg = rd32(hw, I40E_PF_MDET_TX);
8662 if (reg & I40E_PF_MDET_TX_VALID_MASK) {
8663 wr32(hw, I40E_PF_MDET_TX, 0xFFFF);
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00008664 dev_info(&pf->pdev->dev, "TX driver issue detected, PF reset issued\n");
Neerav Parikhdf430b12014-06-04 01:23:15 +00008665 pf_mdd_detected = true;
8666 }
8667 reg = rd32(hw, I40E_PF_MDET_RX);
8668 if (reg & I40E_PF_MDET_RX_VALID_MASK) {
8669 wr32(hw, I40E_PF_MDET_RX, 0xFFFF);
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00008670 dev_info(&pf->pdev->dev, "RX driver issue detected, PF reset issued\n");
Neerav Parikhdf430b12014-06-04 01:23:15 +00008671 pf_mdd_detected = true;
8672 }
8673 /* Queue belongs to the PF, initiate a reset */
8674 if (pf_mdd_detected) {
Jacob Keller0da36b92017-04-19 09:25:55 -04008675 set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
Neerav Parikhdf430b12014-06-04 01:23:15 +00008676 i40e_service_event_schedule(pf);
8677 }
8678 }
8679
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008680 /* see if one of the VFs needs its hand slapped */
8681 for (i = 0; i < pf->num_alloc_vfs && mdd_detected; i++) {
8682 vf = &(pf->vf[i]);
8683 reg = rd32(hw, I40E_VP_MDET_TX(i));
8684 if (reg & I40E_VP_MDET_TX_VALID_MASK) {
8685 wr32(hw, I40E_VP_MDET_TX(i), 0xFFFF);
8686 vf->num_mdd_events++;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00008687 dev_info(&pf->pdev->dev, "TX driver issue detected on VF %d\n",
8688 i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008689 }
8690
8691 reg = rd32(hw, I40E_VP_MDET_RX(i));
8692 if (reg & I40E_VP_MDET_RX_VALID_MASK) {
8693 wr32(hw, I40E_VP_MDET_RX(i), 0xFFFF);
8694 vf->num_mdd_events++;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00008695 dev_info(&pf->pdev->dev, "RX driver issue detected on VF %d\n",
8696 i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008697 }
8698
8699 if (vf->num_mdd_events > I40E_DEFAULT_NUM_MDD_EVENTS_ALLOWED) {
8700 dev_info(&pf->pdev->dev,
8701 "Too many MDD events on VF %d, disabled\n", i);
8702 dev_info(&pf->pdev->dev,
8703 "Use PF Control I/F to re-enable the VF\n");
Jacob Keller6322e632017-04-13 04:45:54 -04008704 set_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008705 }
8706 }
8707
8708 /* re-enable mdd interrupt cause */
Jacob Keller0da36b92017-04-19 09:25:55 -04008709 clear_bit(__I40E_MDD_EVENT_PENDING, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008710 reg = rd32(hw, I40E_PFINT_ICR0_ENA);
8711 reg |= I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
8712 wr32(hw, I40E_PFINT_ICR0_ENA, reg);
8713 i40e_flush(hw);
8714}
8715
Jacob Kellerd8b2c702017-06-20 15:17:00 -07008716static const char *i40e_tunnel_name(struct i40e_udp_port_config *port)
8717{
8718 switch (port->type) {
8719 case UDP_TUNNEL_TYPE_VXLAN:
8720 return "vxlan";
8721 case UDP_TUNNEL_TYPE_GENEVE:
8722 return "geneve";
8723 default:
8724 return "unknown";
8725 }
8726}
8727
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008728/**
Alexander Duyck1f190d92017-04-19 09:25:51 -04008729 * i40e_sync_udp_filters - Trigger a sync event for existing UDP filters
8730 * @pf: board private structure
8731 **/
8732static void i40e_sync_udp_filters(struct i40e_pf *pf)
8733{
8734 int i;
8735
8736 /* loop through and set pending bit for all active UDP filters */
8737 for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
8738 if (pf->udp_ports[i].port)
8739 pf->pending_udp_bitmap |= BIT_ULL(i);
8740 }
8741
8742 pf->flags |= I40E_FLAG_UDP_FILTER_SYNC;
8743}
8744
8745/**
Singhai, Anjali6a899022015-12-14 12:21:18 -08008746 * i40e_sync_udp_filters_subtask - Sync the VSI filter list with HW
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008747 * @pf: board private structure
8748 **/
Singhai, Anjali6a899022015-12-14 12:21:18 -08008749static void i40e_sync_udp_filters_subtask(struct i40e_pf *pf)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008750{
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008751 struct i40e_hw *hw = &pf->hw;
8752 i40e_status ret;
Jacob Kellerfe0b0cd2017-02-06 14:38:38 -08008753 u16 port;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008754 int i;
8755
Singhai, Anjali6a899022015-12-14 12:21:18 -08008756 if (!(pf->flags & I40E_FLAG_UDP_FILTER_SYNC))
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008757 return;
8758
Singhai, Anjali6a899022015-12-14 12:21:18 -08008759 pf->flags &= ~I40E_FLAG_UDP_FILTER_SYNC;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008760
8761 for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
Singhai, Anjali6a899022015-12-14 12:21:18 -08008762 if (pf->pending_udp_bitmap & BIT_ULL(i)) {
8763 pf->pending_udp_bitmap &= ~BIT_ULL(i);
Jacob Keller27826fd2017-04-19 09:25:50 -04008764 port = pf->udp_ports[i].port;
Shannon Nelsonc22c06c2015-03-31 00:45:04 -07008765 if (port)
Carolyn Wybornyb3f5c7b2016-08-24 11:33:51 -07008766 ret = i40e_aq_add_udp_tunnel(hw, port,
8767 pf->udp_ports[i].type,
8768 NULL, NULL);
Shannon Nelsonc22c06c2015-03-31 00:45:04 -07008769 else
8770 ret = i40e_aq_del_udp_tunnel(hw, i, NULL);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008771
8772 if (ret) {
Jacob Kellerd8b2c702017-06-20 15:17:00 -07008773 dev_info(&pf->pdev->dev,
8774 "%s %s port %d, index %d failed, err %s aq_err %s\n",
8775 i40e_tunnel_name(&pf->udp_ports[i]),
8776 port ? "add" : "delete",
8777 port, i,
8778 i40e_stat_str(&pf->hw, ret),
8779 i40e_aq_str(&pf->hw,
8780 pf->hw.aq.asq_last_status));
Jacob Keller27826fd2017-04-19 09:25:50 -04008781 pf->udp_ports[i].port = 0;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008782 }
8783 }
8784 }
8785}
8786
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008787/**
8788 * i40e_service_task - Run the driver's async subtasks
8789 * @work: pointer to work_struct containing our data
8790 **/
8791static void i40e_service_task(struct work_struct *work)
8792{
8793 struct i40e_pf *pf = container_of(work,
8794 struct i40e_pf,
8795 service_task);
8796 unsigned long start_time = jiffies;
8797
Shannon Nelsone57a2fe2014-06-03 23:50:19 +00008798 /* don't bother with service tasks if a reset is in progress */
Jacob Keller0da36b92017-04-19 09:25:55 -04008799 if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
Shannon Nelsone57a2fe2014-06-03 23:50:19 +00008800 return;
Shannon Nelsone57a2fe2014-06-03 23:50:19 +00008801
Jacob Keller0da36b92017-04-19 09:25:55 -04008802 if (test_and_set_bit(__I40E_SERVICE_SCHED, pf->state))
Mitch Williams91089032016-11-21 13:03:51 -08008803 return;
8804
Kiran Patilb03a8c12015-09-24 18:13:15 -04008805 i40e_detect_recover_hung(pf);
Jesse Brandeburg2818ccd2016-01-13 16:51:38 -08008806 i40e_sync_filters_subtask(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008807 i40e_reset_subtask(pf);
8808 i40e_handle_mdd_event(pf);
8809 i40e_vc_process_vflr_event(pf);
8810 i40e_watchdog_subtask(pf);
8811 i40e_fdir_reinit_subtask(pf);
Mitch Williams0ef2d5a2017-01-24 10:24:00 -08008812 if (pf->flags & I40E_FLAG_CLIENT_RESET) {
8813 /* Client subtask will reopen next time through. */
8814 i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], true);
8815 pf->flags &= ~I40E_FLAG_CLIENT_RESET;
8816 } else {
8817 i40e_client_subtask(pf);
8818 if (pf->flags & I40E_FLAG_CLIENT_L2_CHANGE) {
8819 i40e_notify_client_of_l2_param_changes(
8820 pf->vsi[pf->lan_vsi]);
8821 pf->flags &= ~I40E_FLAG_CLIENT_L2_CHANGE;
8822 }
8823 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008824 i40e_sync_filters_subtask(pf);
Singhai, Anjali6a899022015-12-14 12:21:18 -08008825 i40e_sync_udp_filters_subtask(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008826 i40e_clean_adminq_subtask(pf);
8827
Mitch Williams91089032016-11-21 13:03:51 -08008828 /* flush memory to make sure state is correct before next watchdog */
8829 smp_mb__before_atomic();
Jacob Keller0da36b92017-04-19 09:25:55 -04008830 clear_bit(__I40E_SERVICE_SCHED, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008831
8832 /* If the tasks have taken longer than one timer cycle or there
8833 * is more work to be done, reschedule the service task now
8834 * rather than wait for the timer to tick again.
8835 */
8836 if (time_after(jiffies, (start_time + pf->service_timer_period)) ||
Jacob Keller0da36b92017-04-19 09:25:55 -04008837 test_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state) ||
8838 test_bit(__I40E_MDD_EVENT_PENDING, pf->state) ||
8839 test_bit(__I40E_VFLR_EVENT_PENDING, pf->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008840 i40e_service_event_schedule(pf);
8841}
8842
8843/**
8844 * i40e_service_timer - timer callback
8845 * @data: pointer to PF struct
8846 **/
Kees Cook26566ea2017-10-16 17:29:35 -07008847static void i40e_service_timer(struct timer_list *t)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008848{
Kees Cook26566ea2017-10-16 17:29:35 -07008849 struct i40e_pf *pf = from_timer(pf, t, service_timer);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008850
8851 mod_timer(&pf->service_timer,
8852 round_jiffies(jiffies + pf->service_timer_period));
8853 i40e_service_event_schedule(pf);
8854}
8855
8856/**
8857 * i40e_set_num_rings_in_vsi - Determine number of rings in the VSI
8858 * @vsi: the VSI being configured
8859 **/
8860static int i40e_set_num_rings_in_vsi(struct i40e_vsi *vsi)
8861{
8862 struct i40e_pf *pf = vsi->back;
8863
8864 switch (vsi->type) {
8865 case I40E_VSI_MAIN:
8866 vsi->alloc_queue_pairs = pf->num_lan_qps;
8867 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
8868 I40E_REQ_DESCRIPTOR_MULTIPLE);
8869 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
8870 vsi->num_q_vectors = pf->num_lan_msix;
8871 else
8872 vsi->num_q_vectors = 1;
8873
8874 break;
8875
8876 case I40E_VSI_FDIR:
8877 vsi->alloc_queue_pairs = 1;
8878 vsi->num_desc = ALIGN(I40E_FDIR_RING_COUNT,
8879 I40E_REQ_DESCRIPTOR_MULTIPLE);
Tushar Davea70e4072016-05-16 12:40:53 -07008880 vsi->num_q_vectors = pf->num_fdsb_msix;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008881 break;
8882
8883 case I40E_VSI_VMDQ2:
8884 vsi->alloc_queue_pairs = pf->num_vmdq_qps;
8885 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
8886 I40E_REQ_DESCRIPTOR_MULTIPLE);
8887 vsi->num_q_vectors = pf->num_vmdq_msix;
8888 break;
8889
8890 case I40E_VSI_SRIOV:
8891 vsi->alloc_queue_pairs = pf->num_vf_qps;
8892 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
8893 I40E_REQ_DESCRIPTOR_MULTIPLE);
8894 break;
8895
8896 default:
8897 WARN_ON(1);
8898 return -ENODATA;
8899 }
8900
8901 return 0;
8902}
8903
8904/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00008905 * i40e_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the vsi
Rami Rosen3d7d7a82017-09-16 05:49:48 +03008906 * @vsi: VSI pointer
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00008907 * @alloc_qvectors: a bool to specify if q_vectors need to be allocated.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00008908 *
8909 * On error: returns error code (negative)
8910 * On success: returns 0
8911 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00008912static int i40e_vsi_alloc_arrays(struct i40e_vsi *vsi, bool alloc_qvectors)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00008913{
Björn Töpel74608d12017-05-24 07:55:35 +02008914 struct i40e_ring **next_rings;
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00008915 int size;
8916 int ret = 0;
8917
Björn Töpel74608d12017-05-24 07:55:35 +02008918 /* allocate memory for both Tx, XDP Tx and Rx ring pointers */
8919 size = sizeof(struct i40e_ring *) * vsi->alloc_queue_pairs *
8920 (i40e_enabled_xdp_vsi(vsi) ? 3 : 2);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00008921 vsi->tx_rings = kzalloc(size, GFP_KERNEL);
8922 if (!vsi->tx_rings)
8923 return -ENOMEM;
Björn Töpel74608d12017-05-24 07:55:35 +02008924 next_rings = vsi->tx_rings + vsi->alloc_queue_pairs;
8925 if (i40e_enabled_xdp_vsi(vsi)) {
8926 vsi->xdp_rings = next_rings;
8927 next_rings += vsi->alloc_queue_pairs;
8928 }
8929 vsi->rx_rings = next_rings;
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00008930
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00008931 if (alloc_qvectors) {
8932 /* allocate memory for q_vector pointers */
Julia Lawallf57e4fb2014-07-30 03:11:09 +00008933 size = sizeof(struct i40e_q_vector *) * vsi->num_q_vectors;
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00008934 vsi->q_vectors = kzalloc(size, GFP_KERNEL);
8935 if (!vsi->q_vectors) {
8936 ret = -ENOMEM;
8937 goto err_vectors;
8938 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00008939 }
8940 return ret;
8941
8942err_vectors:
8943 kfree(vsi->tx_rings);
8944 return ret;
8945}
8946
8947/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008948 * i40e_vsi_mem_alloc - Allocates the next available struct vsi in the PF
8949 * @pf: board private structure
8950 * @type: type of VSI
8951 *
8952 * On error: returns error code (negative)
8953 * On success: returns vsi index in PF (positive)
8954 **/
8955static int i40e_vsi_mem_alloc(struct i40e_pf *pf, enum i40e_vsi_type type)
8956{
8957 int ret = -ENODEV;
8958 struct i40e_vsi *vsi;
8959 int vsi_idx;
8960 int i;
8961
8962 /* Need to protect the allocation of the VSIs at the PF level */
8963 mutex_lock(&pf->switch_mutex);
8964
8965 /* VSI list may be fragmented if VSI creation/destruction has
8966 * been happening. We can afford to do a quick scan to look
8967 * for any free VSIs in the list.
8968 *
8969 * find next empty vsi slot, looping back around if necessary
8970 */
8971 i = pf->next_vsi;
Mitch Williams505682c2014-05-20 08:01:37 +00008972 while (i < pf->num_alloc_vsi && pf->vsi[i])
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008973 i++;
Mitch Williams505682c2014-05-20 08:01:37 +00008974 if (i >= pf->num_alloc_vsi) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008975 i = 0;
8976 while (i < pf->next_vsi && pf->vsi[i])
8977 i++;
8978 }
8979
Mitch Williams505682c2014-05-20 08:01:37 +00008980 if (i < pf->num_alloc_vsi && !pf->vsi[i]) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008981 vsi_idx = i; /* Found one! */
8982 } else {
8983 ret = -ENODEV;
Alexander Duyck493fb302013-09-28 07:01:44 +00008984 goto unlock_pf; /* out of VSI slots! */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008985 }
8986 pf->next_vsi = ++i;
8987
8988 vsi = kzalloc(sizeof(*vsi), GFP_KERNEL);
8989 if (!vsi) {
8990 ret = -ENOMEM;
Alexander Duyck493fb302013-09-28 07:01:44 +00008991 goto unlock_pf;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008992 }
8993 vsi->type = type;
8994 vsi->back = pf;
Jacob Keller0da36b92017-04-19 09:25:55 -04008995 set_bit(__I40E_VSI_DOWN, vsi->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008996 vsi->flags = 0;
8997 vsi->idx = vsi_idx;
Jesse Brandeburgac26fc12015-09-28 14:12:37 -04008998 vsi->int_rate_limit = 0;
Anjali Singhai Jain5db4cb52015-02-24 06:58:49 +00008999 vsi->rss_table_size = (vsi->type == I40E_VSI_MAIN) ?
9000 pf->rss_table_size : 64;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009001 vsi->netdev_registered = false;
9002 vsi->work_limit = I40E_DEFAULT_IRQ_WORK;
Jacob Keller278e7d02016-10-05 09:30:37 -07009003 hash_init(vsi->mac_filter_hash);
Shannon Nelson63741842014-04-23 04:50:16 +00009004 vsi->irqs_ready = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009005
Alexander Duyck9f65e152013-09-28 06:00:58 +00009006 ret = i40e_set_num_rings_in_vsi(vsi);
9007 if (ret)
9008 goto err_rings;
9009
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00009010 ret = i40e_vsi_alloc_arrays(vsi, true);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00009011 if (ret)
Alexander Duyck9f65e152013-09-28 06:00:58 +00009012 goto err_rings;
Alexander Duyck493fb302013-09-28 07:01:44 +00009013
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009014 /* Setup default MSIX irq handler for VSI */
9015 i40e_vsi_setup_irqhandler(vsi, i40e_msix_clean_rings);
9016
Kiran Patil21659032015-09-30 14:09:03 -04009017 /* Initialize VSI lock */
Jacob Keller278e7d02016-10-05 09:30:37 -07009018 spin_lock_init(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009019 pf->vsi[vsi_idx] = vsi;
9020 ret = vsi_idx;
Alexander Duyck493fb302013-09-28 07:01:44 +00009021 goto unlock_pf;
9022
Alexander Duyck9f65e152013-09-28 06:00:58 +00009023err_rings:
Alexander Duyck493fb302013-09-28 07:01:44 +00009024 pf->next_vsi = i - 1;
9025 kfree(vsi);
9026unlock_pf:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009027 mutex_unlock(&pf->switch_mutex);
9028 return ret;
9029}
9030
9031/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00009032 * i40e_vsi_free_arrays - Free queue and vector pointer arrays for the VSI
9033 * @type: VSI pointer
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00009034 * @free_qvectors: a bool to specify if q_vectors need to be freed.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00009035 *
9036 * On error: returns error code (negative)
9037 * On success: returns 0
9038 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00009039static void i40e_vsi_free_arrays(struct i40e_vsi *vsi, bool free_qvectors)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00009040{
9041 /* free the ring and vector containers */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00009042 if (free_qvectors) {
9043 kfree(vsi->q_vectors);
9044 vsi->q_vectors = NULL;
9045 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00009046 kfree(vsi->tx_rings);
9047 vsi->tx_rings = NULL;
9048 vsi->rx_rings = NULL;
Björn Töpel74608d12017-05-24 07:55:35 +02009049 vsi->xdp_rings = NULL;
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00009050}
9051
9052/**
Helin Zhang28c58692015-10-26 19:44:27 -04009053 * i40e_clear_rss_config_user - clear the user configured RSS hash keys
9054 * and lookup table
9055 * @vsi: Pointer to VSI structure
9056 */
9057static void i40e_clear_rss_config_user(struct i40e_vsi *vsi)
9058{
9059 if (!vsi)
9060 return;
9061
9062 kfree(vsi->rss_hkey_user);
9063 vsi->rss_hkey_user = NULL;
9064
9065 kfree(vsi->rss_lut_user);
9066 vsi->rss_lut_user = NULL;
9067}
9068
9069/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009070 * i40e_vsi_clear - Deallocate the VSI provided
9071 * @vsi: the VSI being un-configured
9072 **/
9073static int i40e_vsi_clear(struct i40e_vsi *vsi)
9074{
9075 struct i40e_pf *pf;
9076
9077 if (!vsi)
9078 return 0;
9079
9080 if (!vsi->back)
9081 goto free_vsi;
9082 pf = vsi->back;
9083
9084 mutex_lock(&pf->switch_mutex);
9085 if (!pf->vsi[vsi->idx]) {
9086 dev_err(&pf->pdev->dev, "pf->vsi[%d] is NULL, just free vsi[%d](%p,type %d)\n",
9087 vsi->idx, vsi->idx, vsi, vsi->type);
9088 goto unlock_vsi;
9089 }
9090
9091 if (pf->vsi[vsi->idx] != vsi) {
9092 dev_err(&pf->pdev->dev,
9093 "pf->vsi[%d](%p, type %d) != vsi[%d](%p,type %d): no free!\n",
9094 pf->vsi[vsi->idx]->idx,
9095 pf->vsi[vsi->idx],
9096 pf->vsi[vsi->idx]->type,
9097 vsi->idx, vsi, vsi->type);
9098 goto unlock_vsi;
9099 }
9100
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00009101 /* updates the PF for this cleared vsi */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009102 i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
9103 i40e_put_lump(pf->irq_pile, vsi->base_vector, vsi->idx);
9104
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00009105 i40e_vsi_free_arrays(vsi, true);
Helin Zhang28c58692015-10-26 19:44:27 -04009106 i40e_clear_rss_config_user(vsi);
Alexander Duyck493fb302013-09-28 07:01:44 +00009107
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009108 pf->vsi[vsi->idx] = NULL;
9109 if (vsi->idx < pf->next_vsi)
9110 pf->next_vsi = vsi->idx;
9111
9112unlock_vsi:
9113 mutex_unlock(&pf->switch_mutex);
9114free_vsi:
9115 kfree(vsi);
9116
9117 return 0;
9118}
9119
9120/**
Alexander Duyck9f65e152013-09-28 06:00:58 +00009121 * i40e_vsi_clear_rings - Deallocates the Rx and Tx rings for the provided VSI
9122 * @vsi: the VSI being cleaned
9123 **/
Shannon Nelsonbe1d5ee2013-11-28 06:39:23 +00009124static void i40e_vsi_clear_rings(struct i40e_vsi *vsi)
Alexander Duyck9f65e152013-09-28 06:00:58 +00009125{
9126 int i;
9127
Greg Rose8e9dca52013-12-18 13:45:53 +00009128 if (vsi->tx_rings && vsi->tx_rings[0]) {
Neerav Parikhd7397642013-11-28 06:39:37 +00009129 for (i = 0; i < vsi->alloc_queue_pairs; i++) {
Mitch Williams00403f02013-09-28 07:13:13 +00009130 kfree_rcu(vsi->tx_rings[i], rcu);
9131 vsi->tx_rings[i] = NULL;
9132 vsi->rx_rings[i] = NULL;
Björn Töpel74608d12017-05-24 07:55:35 +02009133 if (vsi->xdp_rings)
9134 vsi->xdp_rings[i] = NULL;
Mitch Williams00403f02013-09-28 07:13:13 +00009135 }
Shannon Nelsonbe1d5ee2013-11-28 06:39:23 +00009136 }
Alexander Duyck9f65e152013-09-28 06:00:58 +00009137}
9138
9139/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009140 * i40e_alloc_rings - Allocates the Rx and Tx rings for the provided VSI
9141 * @vsi: the VSI being configured
9142 **/
9143static int i40e_alloc_rings(struct i40e_vsi *vsi)
9144{
Björn Töpel74608d12017-05-24 07:55:35 +02009145 int i, qpv = i40e_enabled_xdp_vsi(vsi) ? 3 : 2;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009146 struct i40e_pf *pf = vsi->back;
Björn Töpel74608d12017-05-24 07:55:35 +02009147 struct i40e_ring *ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009148
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009149 /* Set basic values in the rings to be used later during open() */
Neerav Parikhd7397642013-11-28 06:39:37 +00009150 for (i = 0; i < vsi->alloc_queue_pairs; i++) {
Shannon Nelsonac6c5e32013-11-20 10:02:57 +00009151 /* allocate space for both Tx and Rx in one shot */
Björn Töpel74608d12017-05-24 07:55:35 +02009152 ring = kcalloc(qpv, sizeof(struct i40e_ring), GFP_KERNEL);
9153 if (!ring)
Alexander Duyck9f65e152013-09-28 06:00:58 +00009154 goto err_out;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009155
Björn Töpel74608d12017-05-24 07:55:35 +02009156 ring->queue_index = i;
9157 ring->reg_idx = vsi->base_queue + i;
9158 ring->ring_active = false;
9159 ring->vsi = vsi;
9160 ring->netdev = vsi->netdev;
9161 ring->dev = &pf->pdev->dev;
9162 ring->count = vsi->num_desc;
9163 ring->size = 0;
9164 ring->dcb_tc = 0;
Jacob Kellerd36e41d2017-06-23 04:24:46 -04009165 if (vsi->back->hw_features & I40E_HW_WB_ON_ITR_CAPABLE)
Björn Töpel74608d12017-05-24 07:55:35 +02009166 ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
9167 ring->tx_itr_setting = pf->tx_itr_default;
9168 vsi->tx_rings[i] = ring++;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009169
Björn Töpel74608d12017-05-24 07:55:35 +02009170 if (!i40e_enabled_xdp_vsi(vsi))
9171 goto setup_rx;
9172
9173 ring->queue_index = vsi->alloc_queue_pairs + i;
9174 ring->reg_idx = vsi->base_queue + ring->queue_index;
9175 ring->ring_active = false;
9176 ring->vsi = vsi;
9177 ring->netdev = NULL;
9178 ring->dev = &pf->pdev->dev;
9179 ring->count = vsi->num_desc;
9180 ring->size = 0;
9181 ring->dcb_tc = 0;
Jacob Kellerd36e41d2017-06-23 04:24:46 -04009182 if (vsi->back->hw_features & I40E_HW_WB_ON_ITR_CAPABLE)
Björn Töpel74608d12017-05-24 07:55:35 +02009183 ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
9184 set_ring_xdp(ring);
9185 ring->tx_itr_setting = pf->tx_itr_default;
9186 vsi->xdp_rings[i] = ring++;
9187
9188setup_rx:
9189 ring->queue_index = i;
9190 ring->reg_idx = vsi->base_queue + i;
9191 ring->ring_active = false;
9192 ring->vsi = vsi;
9193 ring->netdev = vsi->netdev;
9194 ring->dev = &pf->pdev->dev;
9195 ring->count = vsi->num_desc;
9196 ring->size = 0;
9197 ring->dcb_tc = 0;
9198 ring->rx_itr_setting = pf->rx_itr_default;
9199 vsi->rx_rings[i] = ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009200 }
9201
9202 return 0;
Alexander Duyck9f65e152013-09-28 06:00:58 +00009203
9204err_out:
9205 i40e_vsi_clear_rings(vsi);
9206 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009207}
9208
9209/**
9210 * i40e_reserve_msix_vectors - Reserve MSI-X vectors in the kernel
9211 * @pf: board private structure
9212 * @vectors: the number of MSI-X vectors to request
9213 *
9214 * Returns the number of vectors reserved, or error
9215 **/
9216static int i40e_reserve_msix_vectors(struct i40e_pf *pf, int vectors)
9217{
Alexander Gordeev7b37f372014-02-18 11:11:42 +01009218 vectors = pci_enable_msix_range(pf->pdev, pf->msix_entries,
9219 I40E_MIN_MSIX, vectors);
9220 if (vectors < 0) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009221 dev_info(&pf->pdev->dev,
Alexander Gordeev7b37f372014-02-18 11:11:42 +01009222 "MSI-X vector reservation failed: %d\n", vectors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009223 vectors = 0;
9224 }
9225
9226 return vectors;
9227}
9228
9229/**
9230 * i40e_init_msix - Setup the MSIX capability
9231 * @pf: board private structure
9232 *
9233 * Work with the OS to set up the MSIX vectors needed.
9234 *
Shannon Nelson3b444392015-02-26 16:15:57 +00009235 * Returns the number of vectors reserved or negative on failure
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009236 **/
9237static int i40e_init_msix(struct i40e_pf *pf)
9238{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009239 struct i40e_hw *hw = &pf->hw;
Jacob Kellerc0cf70a2017-01-24 10:24:01 -08009240 int cpus, extra_vectors;
Shannon Nelson1e200e42015-02-27 09:15:24 +00009241 int vectors_left;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009242 int v_budget, i;
Shannon Nelson3b444392015-02-26 16:15:57 +00009243 int v_actual;
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06009244 int iwarp_requested = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009245
9246 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
9247 return -ENODEV;
9248
9249 /* The number of vectors we'll request will be comprised of:
9250 * - Add 1 for "other" cause for Admin Queue events, etc.
9251 * - The number of LAN queue pairs
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00009252 * - Queues being used for RSS.
9253 * We don't need as many as max_rss_size vectors.
9254 * use rss_size instead in the calculation since that
9255 * is governed by number of cpus in the system.
9256 * - assumes symmetric Tx/Rx pairing
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009257 * - The number of VMDq pairs
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06009258 * - The CPU count within the NUMA node if iWARP is enabled
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009259 * Once we count this up, try the request.
9260 *
9261 * If we can't get what we want, we'll simplify to nearly nothing
9262 * and try again. If that still fails, we punt.
9263 */
Shannon Nelson1e200e42015-02-27 09:15:24 +00009264 vectors_left = hw->func_caps.num_msix_vectors;
9265 v_budget = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009266
Shannon Nelson1e200e42015-02-27 09:15:24 +00009267 /* reserve one vector for miscellaneous handler */
9268 if (vectors_left) {
9269 v_budget++;
9270 vectors_left--;
9271 }
9272
Jacob Kellerc0cf70a2017-01-24 10:24:01 -08009273 /* reserve some vectors for the main PF traffic queues. Initially we
9274 * only reserve at most 50% of the available vectors, in the case that
9275 * the number of online CPUs is large. This ensures that we can enable
9276 * extra features as well. Once we've enabled the other features, we
9277 * will use any remaining vectors to reach as close as we can to the
9278 * number of online CPUs.
9279 */
9280 cpus = num_online_cpus();
9281 pf->num_lan_msix = min_t(int, cpus, vectors_left / 2);
Shannon Nelson1e200e42015-02-27 09:15:24 +00009282 vectors_left -= pf->num_lan_msix;
Shannon Nelson1e200e42015-02-27 09:15:24 +00009283
9284 /* reserve one vector for sideband flow director */
9285 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
9286 if (vectors_left) {
Tushar Davea70e4072016-05-16 12:40:53 -07009287 pf->num_fdsb_msix = 1;
Shannon Nelson1e200e42015-02-27 09:15:24 +00009288 v_budget++;
9289 vectors_left--;
9290 } else {
Tushar Davea70e4072016-05-16 12:40:53 -07009291 pf->num_fdsb_msix = 0;
Shannon Nelson1e200e42015-02-27 09:15:24 +00009292 }
9293 }
John W Linville83840e42015-01-14 03:06:28 +00009294
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06009295 /* can we reserve enough for iWARP? */
9296 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02009297 iwarp_requested = pf->num_iwarp_msix;
9298
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06009299 if (!vectors_left)
9300 pf->num_iwarp_msix = 0;
9301 else if (vectors_left < pf->num_iwarp_msix)
9302 pf->num_iwarp_msix = 1;
9303 v_budget += pf->num_iwarp_msix;
9304 vectors_left -= pf->num_iwarp_msix;
9305 }
9306
Shannon Nelson1e200e42015-02-27 09:15:24 +00009307 /* any vectors left over go for VMDq support */
9308 if (pf->flags & I40E_FLAG_VMDQ_ENABLED) {
9309 int vmdq_vecs_wanted = pf->num_vmdq_vsis * pf->num_vmdq_qps;
9310 int vmdq_vecs = min_t(int, vectors_left, vmdq_vecs_wanted);
9311
Stefan Assmann9ca57e92016-09-19 13:37:49 +02009312 if (!vectors_left) {
9313 pf->num_vmdq_msix = 0;
9314 pf->num_vmdq_qps = 0;
9315 } else {
9316 /* if we're short on vectors for what's desired, we limit
9317 * the queues per vmdq. If this is still more than are
9318 * available, the user will need to change the number of
9319 * queues/vectors used by the PF later with the ethtool
9320 * channels command
9321 */
9322 if (vmdq_vecs < vmdq_vecs_wanted)
9323 pf->num_vmdq_qps = 1;
9324 pf->num_vmdq_msix = pf->num_vmdq_qps;
Shannon Nelson1e200e42015-02-27 09:15:24 +00009325
Stefan Assmann9ca57e92016-09-19 13:37:49 +02009326 v_budget += vmdq_vecs;
9327 vectors_left -= vmdq_vecs;
9328 }
Shannon Nelson1e200e42015-02-27 09:15:24 +00009329 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009330
Jacob Kellerc0cf70a2017-01-24 10:24:01 -08009331 /* On systems with a large number of SMP cores, we previously limited
9332 * the number of vectors for num_lan_msix to be at most 50% of the
9333 * available vectors, to allow for other features. Now, we add back
9334 * the remaining vectors. However, we ensure that the total
9335 * num_lan_msix will not exceed num_online_cpus(). To do this, we
9336 * calculate the number of vectors we can add without going over the
9337 * cap of CPUs. For systems with a small number of CPUs this will be
9338 * zero.
9339 */
9340 extra_vectors = min_t(int, cpus - pf->num_lan_msix, vectors_left);
9341 pf->num_lan_msix += extra_vectors;
9342 vectors_left -= extra_vectors;
9343
9344 WARN(vectors_left < 0,
9345 "Calculation of remaining vectors underflowed. This is an accounting bug when determining total MSI-X vectors.\n");
9346
9347 v_budget += pf->num_lan_msix;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009348 pf->msix_entries = kcalloc(v_budget, sizeof(struct msix_entry),
9349 GFP_KERNEL);
9350 if (!pf->msix_entries)
9351 return -ENOMEM;
9352
9353 for (i = 0; i < v_budget; i++)
9354 pf->msix_entries[i].entry = i;
Shannon Nelson3b444392015-02-26 16:15:57 +00009355 v_actual = i40e_reserve_msix_vectors(pf, v_budget);
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00009356
Shannon Nelson3b444392015-02-26 16:15:57 +00009357 if (v_actual < I40E_MIN_MSIX) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009358 pf->flags &= ~I40E_FLAG_MSIX_ENABLED;
9359 kfree(pf->msix_entries);
9360 pf->msix_entries = NULL;
Guilherme G Piccoli4c95aa52016-09-22 10:03:58 -03009361 pci_disable_msix(pf->pdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009362 return -ENODEV;
9363
Shannon Nelson3b444392015-02-26 16:15:57 +00009364 } else if (v_actual == I40E_MIN_MSIX) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009365 /* Adjust for minimal MSIX use */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009366 pf->num_vmdq_vsis = 0;
9367 pf->num_vmdq_qps = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009368 pf->num_lan_qps = 1;
9369 pf->num_lan_msix = 1;
9370
Shannon Nelson3e6b1cf2017-10-10 14:56:58 -07009371 } else if (v_actual != v_budget) {
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02009372 /* If we have limited resources, we will start with no vectors
9373 * for the special features and then allocate vectors to some
9374 * of these features based on the policy and at the end disable
9375 * the features that did not get any vectors.
9376 */
Shannon Nelson3b444392015-02-26 16:15:57 +00009377 int vec;
9378
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02009379 dev_info(&pf->pdev->dev,
Shannon Nelson3e6b1cf2017-10-10 14:56:58 -07009380 "MSI-X vector limit reached with %d, wanted %d, attempting to redistribute vectors\n",
9381 v_actual, v_budget);
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00009382 /* reserve the misc vector */
Shannon Nelson3b444392015-02-26 16:15:57 +00009383 vec = v_actual - 1;
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00009384
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009385 /* Scale vector usage down */
9386 pf->num_vmdq_msix = 1; /* force VMDqs to only one vector */
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00009387 pf->num_vmdq_vsis = 1;
Shannon Nelson1e200e42015-02-27 09:15:24 +00009388 pf->num_vmdq_qps = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009389
9390 /* partition out the remaining vectors */
9391 switch (vec) {
9392 case 2:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009393 pf->num_lan_msix = 1;
9394 break;
9395 case 3:
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06009396 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
9397 pf->num_lan_msix = 1;
9398 pf->num_iwarp_msix = 1;
9399 } else {
9400 pf->num_lan_msix = 2;
9401 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009402 break;
9403 default:
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06009404 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
9405 pf->num_iwarp_msix = min_t(int, (vec / 3),
9406 iwarp_requested);
9407 pf->num_vmdq_vsis = min_t(int, (vec / 3),
9408 I40E_DEFAULT_NUM_VMDQ_VSI);
9409 } else {
9410 pf->num_vmdq_vsis = min_t(int, (vec / 2),
9411 I40E_DEFAULT_NUM_VMDQ_VSI);
9412 }
Stefan Assmannabd97a92016-09-19 13:37:51 +02009413 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
9414 pf->num_fdsb_msix = 1;
9415 vec--;
9416 }
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06009417 pf->num_lan_msix = min_t(int,
9418 (vec - (pf->num_iwarp_msix + pf->num_vmdq_vsis)),
9419 pf->num_lan_msix);
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02009420 pf->num_lan_qps = pf->num_lan_msix;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009421 break;
9422 }
9423 }
9424
Stefan Assmannabd97a92016-09-19 13:37:51 +02009425 if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
9426 (pf->num_fdsb_msix == 0)) {
9427 dev_info(&pf->pdev->dev, "Sideband Flowdir disabled, not enough MSI-X vectors\n");
9428 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
9429 }
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00009430 if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
9431 (pf->num_vmdq_msix == 0)) {
9432 dev_info(&pf->pdev->dev, "VMDq disabled, not enough MSI-X vectors\n");
9433 pf->flags &= ~I40E_FLAG_VMDQ_ENABLED;
9434 }
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06009435
9436 if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
9437 (pf->num_iwarp_msix == 0)) {
9438 dev_info(&pf->pdev->dev, "IWARP disabled, not enough MSI-X vectors\n");
9439 pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
9440 }
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02009441 i40e_debug(&pf->hw, I40E_DEBUG_INIT,
9442 "MSI-X vector distribution: PF %d, VMDq %d, FDSB %d, iWARP %d\n",
9443 pf->num_lan_msix,
9444 pf->num_vmdq_msix * pf->num_vmdq_vsis,
9445 pf->num_fdsb_msix,
9446 pf->num_iwarp_msix);
9447
Shannon Nelson3b444392015-02-26 16:15:57 +00009448 return v_actual;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009449}
9450
9451/**
Greg Rose90e04072014-03-06 08:59:57 +00009452 * i40e_vsi_alloc_q_vector - Allocate memory for a single interrupt vector
Alexander Duyck493fb302013-09-28 07:01:44 +00009453 * @vsi: the VSI being configured
9454 * @v_idx: index of the vector in the vsi struct
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03009455 * @cpu: cpu to be used on affinity_mask
Alexander Duyck493fb302013-09-28 07:01:44 +00009456 *
9457 * We allocate one q_vector. If allocation fails we return -ENOMEM.
9458 **/
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03009459static int i40e_vsi_alloc_q_vector(struct i40e_vsi *vsi, int v_idx, int cpu)
Alexander Duyck493fb302013-09-28 07:01:44 +00009460{
9461 struct i40e_q_vector *q_vector;
9462
9463 /* allocate q_vector */
9464 q_vector = kzalloc(sizeof(struct i40e_q_vector), GFP_KERNEL);
9465 if (!q_vector)
9466 return -ENOMEM;
9467
9468 q_vector->vsi = vsi;
9469 q_vector->v_idx = v_idx;
Jacob Keller759dc4a2017-07-14 09:10:10 -04009470 cpumask_copy(&q_vector->affinity_mask, cpu_possible_mask);
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03009471
Alexander Duyck493fb302013-09-28 07:01:44 +00009472 if (vsi->netdev)
9473 netif_napi_add(vsi->netdev, &q_vector->napi,
Jesse Brandeburgeefeace2014-05-10 04:49:13 +00009474 i40e_napi_poll, NAPI_POLL_WEIGHT);
Alexander Duyck493fb302013-09-28 07:01:44 +00009475
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00009476 q_vector->rx.latency_range = I40E_LOW_LATENCY;
9477 q_vector->tx.latency_range = I40E_LOW_LATENCY;
9478
Alexander Duyck493fb302013-09-28 07:01:44 +00009479 /* tie q_vector and vsi together */
9480 vsi->q_vectors[v_idx] = q_vector;
9481
9482 return 0;
9483}
9484
9485/**
Greg Rose90e04072014-03-06 08:59:57 +00009486 * i40e_vsi_alloc_q_vectors - Allocate memory for interrupt vectors
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009487 * @vsi: the VSI being configured
9488 *
9489 * We allocate one q_vector per queue interrupt. If allocation fails we
9490 * return -ENOMEM.
9491 **/
Greg Rose90e04072014-03-06 08:59:57 +00009492static int i40e_vsi_alloc_q_vectors(struct i40e_vsi *vsi)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009493{
9494 struct i40e_pf *pf = vsi->back;
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03009495 int err, v_idx, num_q_vectors, current_cpu;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009496
9497 /* if not MSIX, give the one vector only to the LAN VSI */
9498 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
9499 num_q_vectors = vsi->num_q_vectors;
9500 else if (vsi == pf->vsi[pf->lan_vsi])
9501 num_q_vectors = 1;
9502 else
9503 return -EINVAL;
9504
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03009505 current_cpu = cpumask_first(cpu_online_mask);
9506
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009507 for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03009508 err = i40e_vsi_alloc_q_vector(vsi, v_idx, current_cpu);
Alexander Duyck493fb302013-09-28 07:01:44 +00009509 if (err)
9510 goto err_out;
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03009511 current_cpu = cpumask_next(current_cpu, cpu_online_mask);
9512 if (unlikely(current_cpu >= nr_cpu_ids))
9513 current_cpu = cpumask_first(cpu_online_mask);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009514 }
9515
9516 return 0;
Alexander Duyck493fb302013-09-28 07:01:44 +00009517
9518err_out:
9519 while (v_idx--)
9520 i40e_free_q_vector(vsi, v_idx);
9521
9522 return err;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009523}
9524
9525/**
9526 * i40e_init_interrupt_scheme - Determine proper interrupt scheme
9527 * @pf: board private structure to initialize
9528 **/
Jesse Brandeburgc11472802015-04-07 19:45:39 -04009529static int i40e_init_interrupt_scheme(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009530{
Shannon Nelson3b444392015-02-26 16:15:57 +00009531 int vectors = 0;
9532 ssize_t size;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009533
9534 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
Shannon Nelson3b444392015-02-26 16:15:57 +00009535 vectors = i40e_init_msix(pf);
9536 if (vectors < 0) {
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08009537 pf->flags &= ~(I40E_FLAG_MSIX_ENABLED |
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06009538 I40E_FLAG_IWARP_ENABLED |
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08009539 I40E_FLAG_RSS_ENABLED |
Neerav Parikh4d9b6042014-05-22 06:31:51 +00009540 I40E_FLAG_DCB_CAPABLE |
Dave Ertmana0362442016-08-29 17:38:26 -07009541 I40E_FLAG_DCB_ENABLED |
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08009542 I40E_FLAG_SRIOV_ENABLED |
9543 I40E_FLAG_FD_SB_ENABLED |
9544 I40E_FLAG_FD_ATR_ENABLED |
9545 I40E_FLAG_VMDQ_ENABLED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009546
9547 /* rework the queue expectations without MSIX */
9548 i40e_determine_queue_usage(pf);
9549 }
9550 }
9551
9552 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED) &&
9553 (pf->flags & I40E_FLAG_MSI_ENABLED)) {
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08009554 dev_info(&pf->pdev->dev, "MSI-X not available, trying MSI\n");
Shannon Nelson3b444392015-02-26 16:15:57 +00009555 vectors = pci_enable_msi(pf->pdev);
9556 if (vectors < 0) {
9557 dev_info(&pf->pdev->dev, "MSI init failed - %d\n",
9558 vectors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009559 pf->flags &= ~I40E_FLAG_MSI_ENABLED;
9560 }
Shannon Nelson3b444392015-02-26 16:15:57 +00009561 vectors = 1; /* one MSI or Legacy vector */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009562 }
9563
Shannon Nelson958a3e32013-09-28 07:13:28 +00009564 if (!(pf->flags & (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED)))
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08009565 dev_info(&pf->pdev->dev, "MSI-X and MSI not available, falling back to Legacy IRQ\n");
Shannon Nelson958a3e32013-09-28 07:13:28 +00009566
Shannon Nelson3b444392015-02-26 16:15:57 +00009567 /* set up vector assignment tracking */
9568 size = sizeof(struct i40e_lump_tracking) + (sizeof(u16) * vectors);
9569 pf->irq_pile = kzalloc(size, GFP_KERNEL);
Jesse Brandeburgc11472802015-04-07 19:45:39 -04009570 if (!pf->irq_pile) {
9571 dev_err(&pf->pdev->dev, "error allocating irq_pile memory\n");
9572 return -ENOMEM;
9573 }
Shannon Nelson3b444392015-02-26 16:15:57 +00009574 pf->irq_pile->num_entries = vectors;
9575 pf->irq_pile->search_hint = 0;
9576
Jesse Brandeburgc11472802015-04-07 19:45:39 -04009577 /* track first vector for misc interrupts, ignore return */
Shannon Nelson3b444392015-02-26 16:15:57 +00009578 (void)i40e_get_lump(pf, pf->irq_pile, 1, I40E_PILE_VALID_BIT - 1);
Jesse Brandeburgc11472802015-04-07 19:45:39 -04009579
9580 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009581}
9582
Jacob Kellerb980c062017-07-14 09:27:06 -04009583/**
9584 * i40e_restore_interrupt_scheme - Restore the interrupt scheme
9585 * @pf: private board data structure
9586 *
9587 * Restore the interrupt scheme that was cleared when we suspended the
9588 * device. This should be called during resume to re-allocate the q_vectors
9589 * and reacquire IRQs.
9590 */
9591static int i40e_restore_interrupt_scheme(struct i40e_pf *pf)
9592{
9593 int err, i;
9594
9595 /* We cleared the MSI and MSI-X flags when disabling the old interrupt
9596 * scheme. We need to re-enabled them here in order to attempt to
9597 * re-acquire the MSI or MSI-X vectors
9598 */
9599 pf->flags |= (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
9600
9601 err = i40e_init_interrupt_scheme(pf);
9602 if (err)
9603 return err;
9604
9605 /* Now that we've re-acquired IRQs, we need to remap the vectors and
9606 * rings together again.
9607 */
9608 for (i = 0; i < pf->num_alloc_vsi; i++) {
9609 if (pf->vsi[i]) {
9610 err = i40e_vsi_alloc_q_vectors(pf->vsi[i]);
9611 if (err)
9612 goto err_unwind;
9613 i40e_vsi_map_rings_to_vectors(pf->vsi[i]);
9614 }
9615 }
9616
9617 err = i40e_setup_misc_vector(pf);
9618 if (err)
9619 goto err_unwind;
9620
9621 return 0;
9622
9623err_unwind:
9624 while (i--) {
9625 if (pf->vsi[i])
9626 i40e_vsi_free_q_vectors(pf->vsi[i]);
9627 }
9628
9629 return err;
9630}
Jacob Kellerb980c062017-07-14 09:27:06 -04009631
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009632/**
9633 * i40e_setup_misc_vector - Setup the misc vector to handle non queue events
9634 * @pf: board private structure
9635 *
9636 * This sets up the handler for MSIX 0, which is used to manage the
9637 * non-queue interrupts, e.g. AdminQ and errors. This is not used
9638 * when in MSI or Legacy interrupt mode.
9639 **/
9640static int i40e_setup_misc_vector(struct i40e_pf *pf)
9641{
9642 struct i40e_hw *hw = &pf->hw;
9643 int err = 0;
9644
Jacob Kellerc17401a2017-07-14 09:27:02 -04009645 /* Only request the IRQ once, the first time through. */
9646 if (!test_and_set_bit(__I40E_MISC_IRQ_REQUESTED, pf->state)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009647 err = request_irq(pf->msix_entries[0].vector,
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00009648 i40e_intr, 0, pf->int_name, pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009649 if (err) {
Jacob Kellerc17401a2017-07-14 09:27:02 -04009650 clear_bit(__I40E_MISC_IRQ_REQUESTED, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009651 dev_info(&pf->pdev->dev,
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08009652 "request_irq for %s failed: %d\n",
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00009653 pf->int_name, err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009654 return -EFAULT;
9655 }
9656 }
9657
Jacob Kellerab437b52014-12-14 01:55:08 +00009658 i40e_enable_misc_int_causes(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009659
9660 /* associate no queues to the misc vector */
9661 wr32(hw, I40E_PFINT_LNKLST0, I40E_QUEUE_END_OF_LIST);
9662 wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), I40E_ITR_8K);
9663
9664 i40e_flush(hw);
9665
Jacob Kellerdbadbbe2017-09-07 08:05:49 -04009666 i40e_irq_dynamic_enable_icr0(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009667
9668 return err;
9669}
9670
9671/**
Anjali Singhai Jain95a73782015-12-22 14:25:04 -08009672 * i40e_get_rss_aq - Get RSS keys and lut by using AQ commands
9673 * @vsi: Pointer to vsi structure
9674 * @seed: Buffter to store the hash keys
9675 * @lut: Buffer to store the lookup table entries
9676 * @lut_size: Size of buffer to store the lookup table entries
9677 *
9678 * Return 0 on success, negative on failure
9679 */
9680static int i40e_get_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
9681 u8 *lut, u16 lut_size)
9682{
9683 struct i40e_pf *pf = vsi->back;
9684 struct i40e_hw *hw = &pf->hw;
9685 int ret = 0;
9686
9687 if (seed) {
9688 ret = i40e_aq_get_rss_key(hw, vsi->id,
9689 (struct i40e_aqc_get_set_rss_key_data *)seed);
9690 if (ret) {
9691 dev_info(&pf->pdev->dev,
9692 "Cannot get RSS key, err %s aq_err %s\n",
9693 i40e_stat_str(&pf->hw, ret),
9694 i40e_aq_str(&pf->hw,
9695 pf->hw.aq.asq_last_status));
9696 return ret;
9697 }
9698 }
9699
9700 if (lut) {
9701 bool pf_lut = vsi->type == I40E_VSI_MAIN ? true : false;
9702
9703 ret = i40e_aq_get_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
9704 if (ret) {
9705 dev_info(&pf->pdev->dev,
9706 "Cannot get RSS lut, err %s aq_err %s\n",
9707 i40e_stat_str(&pf->hw, ret),
9708 i40e_aq_str(&pf->hw,
9709 pf->hw.aq.asq_last_status));
9710 return ret;
9711 }
9712 }
9713
9714 return ret;
9715}
9716
9717/**
Helin Zhang043dd652015-10-21 19:56:23 -04009718 * i40e_config_rss_reg - Configure RSS keys and lut by writing registers
Helin Zhange69ff812015-10-21 19:56:22 -04009719 * @vsi: Pointer to vsi structure
9720 * @seed: RSS hash seed
9721 * @lut: Lookup table
9722 * @lut_size: Lookup table size
9723 *
9724 * Returns 0 on success, negative on failure
9725 **/
9726static int i40e_config_rss_reg(struct i40e_vsi *vsi, const u8 *seed,
9727 const u8 *lut, u16 lut_size)
9728{
9729 struct i40e_pf *pf = vsi->back;
9730 struct i40e_hw *hw = &pf->hw;
Mitch Williamsc4e18682016-04-12 08:30:40 -07009731 u16 vf_id = vsi->vf_id;
Helin Zhange69ff812015-10-21 19:56:22 -04009732 u8 i;
9733
9734 /* Fill out hash function seed */
9735 if (seed) {
9736 u32 *seed_dw = (u32 *)seed;
9737
Mitch Williamsc4e18682016-04-12 08:30:40 -07009738 if (vsi->type == I40E_VSI_MAIN) {
9739 for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
Lihong Yang26f77e52017-01-30 12:29:33 -08009740 wr32(hw, I40E_PFQF_HKEY(i), seed_dw[i]);
Mitch Williamsc4e18682016-04-12 08:30:40 -07009741 } else if (vsi->type == I40E_VSI_SRIOV) {
9742 for (i = 0; i <= I40E_VFQF_HKEY1_MAX_INDEX; i++)
Lihong Yang26f77e52017-01-30 12:29:33 -08009743 wr32(hw, I40E_VFQF_HKEY1(i, vf_id), seed_dw[i]);
Mitch Williamsc4e18682016-04-12 08:30:40 -07009744 } else {
9745 dev_err(&pf->pdev->dev, "Cannot set RSS seed - invalid VSI type\n");
9746 }
Helin Zhange69ff812015-10-21 19:56:22 -04009747 }
9748
9749 if (lut) {
9750 u32 *lut_dw = (u32 *)lut;
9751
Mitch Williamsc4e18682016-04-12 08:30:40 -07009752 if (vsi->type == I40E_VSI_MAIN) {
9753 if (lut_size != I40E_HLUT_ARRAY_SIZE)
9754 return -EINVAL;
9755 for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
9756 wr32(hw, I40E_PFQF_HLUT(i), lut_dw[i]);
9757 } else if (vsi->type == I40E_VSI_SRIOV) {
9758 if (lut_size != I40E_VF_HLUT_ARRAY_SIZE)
9759 return -EINVAL;
9760 for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
Lihong Yang26f77e52017-01-30 12:29:33 -08009761 wr32(hw, I40E_VFQF_HLUT1(i, vf_id), lut_dw[i]);
Mitch Williamsc4e18682016-04-12 08:30:40 -07009762 } else {
9763 dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
9764 }
Helin Zhange69ff812015-10-21 19:56:22 -04009765 }
9766 i40e_flush(hw);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04009767
9768 return 0;
9769}
9770
9771/**
Helin Zhang043dd652015-10-21 19:56:23 -04009772 * i40e_get_rss_reg - Get the RSS keys and lut by reading registers
9773 * @vsi: Pointer to VSI structure
9774 * @seed: Buffer to store the keys
9775 * @lut: Buffer to store the lookup table entries
9776 * @lut_size: Size of buffer to store the lookup table entries
9777 *
9778 * Returns 0 on success, negative on failure
9779 */
9780static int i40e_get_rss_reg(struct i40e_vsi *vsi, u8 *seed,
9781 u8 *lut, u16 lut_size)
9782{
9783 struct i40e_pf *pf = vsi->back;
9784 struct i40e_hw *hw = &pf->hw;
9785 u16 i;
9786
9787 if (seed) {
9788 u32 *seed_dw = (u32 *)seed;
9789
9790 for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
Shannon Nelson272cdaf22016-02-17 16:12:21 -08009791 seed_dw[i] = i40e_read_rx_ctl(hw, I40E_PFQF_HKEY(i));
Helin Zhang043dd652015-10-21 19:56:23 -04009792 }
9793 if (lut) {
9794 u32 *lut_dw = (u32 *)lut;
9795
9796 if (lut_size != I40E_HLUT_ARRAY_SIZE)
9797 return -EINVAL;
9798 for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
9799 lut_dw[i] = rd32(hw, I40E_PFQF_HLUT(i));
9800 }
9801
9802 return 0;
9803}
9804
9805/**
9806 * i40e_config_rss - Configure RSS keys and lut
9807 * @vsi: Pointer to VSI structure
9808 * @seed: RSS hash seed
9809 * @lut: Lookup table
9810 * @lut_size: Lookup table size
9811 *
9812 * Returns 0 on success, negative on failure
9813 */
9814int i40e_config_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
9815{
9816 struct i40e_pf *pf = vsi->back;
9817
Jacob Kellerd36e41d2017-06-23 04:24:46 -04009818 if (pf->hw_features & I40E_HW_RSS_AQ_CAPABLE)
Helin Zhang043dd652015-10-21 19:56:23 -04009819 return i40e_config_rss_aq(vsi, seed, lut, lut_size);
9820 else
9821 return i40e_config_rss_reg(vsi, seed, lut, lut_size);
9822}
9823
9824/**
9825 * i40e_get_rss - Get RSS keys and lut
9826 * @vsi: Pointer to VSI structure
9827 * @seed: Buffer to store the keys
9828 * @lut: Buffer to store the lookup table entries
9829 * lut_size: Size of buffer to store the lookup table entries
9830 *
9831 * Returns 0 on success, negative on failure
9832 */
9833int i40e_get_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
9834{
Anjali Singhai Jain95a73782015-12-22 14:25:04 -08009835 struct i40e_pf *pf = vsi->back;
9836
Jacob Kellerd36e41d2017-06-23 04:24:46 -04009837 if (pf->hw_features & I40E_HW_RSS_AQ_CAPABLE)
Anjali Singhai Jain95a73782015-12-22 14:25:04 -08009838 return i40e_get_rss_aq(vsi, seed, lut, lut_size);
9839 else
9840 return i40e_get_rss_reg(vsi, seed, lut, lut_size);
Helin Zhang043dd652015-10-21 19:56:23 -04009841}
9842
9843/**
Helin Zhange69ff812015-10-21 19:56:22 -04009844 * i40e_fill_rss_lut - Fill the RSS lookup table with default values
9845 * @pf: Pointer to board private structure
9846 * @lut: Lookup table
9847 * @rss_table_size: Lookup table size
9848 * @rss_size: Range of queue number for hashing
9849 */
Alan Bradyf1582352016-08-24 11:33:46 -07009850void i40e_fill_rss_lut(struct i40e_pf *pf, u8 *lut,
9851 u16 rss_table_size, u16 rss_size)
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04009852{
Helin Zhange69ff812015-10-21 19:56:22 -04009853 u16 i;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04009854
Helin Zhange69ff812015-10-21 19:56:22 -04009855 for (i = 0; i < rss_table_size; i++)
9856 lut[i] = i % rss_size;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04009857}
9858
9859/**
Helin Zhang043dd652015-10-21 19:56:23 -04009860 * i40e_pf_config_rss - Prepare for RSS if used
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009861 * @pf: board private structure
9862 **/
Helin Zhang043dd652015-10-21 19:56:23 -04009863static int i40e_pf_config_rss(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009864{
Anjali Singhai Jain66ddcff2015-02-24 06:58:50 +00009865 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04009866 u8 seed[I40E_HKEY_ARRAY_SIZE];
Helin Zhange69ff812015-10-21 19:56:22 -04009867 u8 *lut;
Anjali Singhai Jain4617e8c2013-11-20 10:02:56 +00009868 struct i40e_hw *hw = &pf->hw;
Carolyn Wybornye157ea32014-06-03 23:50:22 +00009869 u32 reg_val;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04009870 u64 hena;
Helin Zhange69ff812015-10-21 19:56:22 -04009871 int ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009872
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009873 /* By default we enable TCP/UDP with IPv4/IPv6 ptypes */
Shannon Nelson272cdaf22016-02-17 16:12:21 -08009874 hena = (u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(0)) |
9875 ((u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(1)) << 32);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04009876 hena |= i40e_pf_get_default_rss_hena(pf);
9877
Shannon Nelson272cdaf22016-02-17 16:12:21 -08009878 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), (u32)hena);
9879 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009880
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04009881 /* Determine the RSS table size based on the hardware capabilities */
Shannon Nelson272cdaf22016-02-17 16:12:21 -08009882 reg_val = i40e_read_rx_ctl(hw, I40E_PFQF_CTL_0);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04009883 reg_val = (pf->rss_table_size == 512) ?
9884 (reg_val | I40E_PFQF_CTL_0_HASHLUTSIZE_512) :
9885 (reg_val & ~I40E_PFQF_CTL_0_HASHLUTSIZE_512);
Shannon Nelson272cdaf22016-02-17 16:12:21 -08009886 i40e_write_rx_ctl(hw, I40E_PFQF_CTL_0, reg_val);
Carolyn Wybornye157ea32014-06-03 23:50:22 +00009887
Helin Zhang28c58692015-10-26 19:44:27 -04009888 /* Determine the RSS size of the VSI */
Harshitha Ramamurthyf25571b2017-02-21 15:55:40 -08009889 if (!vsi->rss_size) {
9890 u16 qcount;
9891
9892 qcount = vsi->num_queue_pairs / vsi->tc_config.numtc;
9893 vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
9894 }
Mitch Williamsa4fa59c2016-09-12 14:18:43 -07009895 if (!vsi->rss_size)
9896 return -EINVAL;
Helin Zhang28c58692015-10-26 19:44:27 -04009897
Helin Zhange69ff812015-10-21 19:56:22 -04009898 lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
9899 if (!lut)
9900 return -ENOMEM;
9901
Helin Zhang28c58692015-10-26 19:44:27 -04009902 /* Use user configured lut if there is one, otherwise use default */
9903 if (vsi->rss_lut_user)
9904 memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
9905 else
9906 i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
Helin Zhange69ff812015-10-21 19:56:22 -04009907
Helin Zhang28c58692015-10-26 19:44:27 -04009908 /* Use user configured hash key if there is one, otherwise
9909 * use default.
9910 */
9911 if (vsi->rss_hkey_user)
9912 memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
9913 else
9914 netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
Helin Zhang043dd652015-10-21 19:56:23 -04009915 ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
Helin Zhange69ff812015-10-21 19:56:22 -04009916 kfree(lut);
9917
9918 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009919}
9920
9921/**
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00009922 * i40e_reconfig_rss_queues - change number of queues for rss and rebuild
9923 * @pf: board private structure
9924 * @queue_count: the requested queue count for rss.
9925 *
9926 * returns 0 if rss is not enabled, if enabled returns the final rss queue
9927 * count which may be different from the requested queue count.
Maciej Sosin373149f2017-04-05 07:50:55 -04009928 * Note: expects to be called while under rtnl_lock()
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00009929 **/
9930int i40e_reconfig_rss_queues(struct i40e_pf *pf, int queue_count)
9931{
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00009932 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
9933 int new_rss_size;
9934
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00009935 if (!(pf->flags & I40E_FLAG_RSS_ENABLED))
9936 return 0;
9937
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00009938 new_rss_size = min_t(int, queue_count, pf->rss_size_max);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00009939
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00009940 if (queue_count != vsi->num_queue_pairs) {
Harshitha Ramamurthyf25571b2017-02-21 15:55:40 -08009941 u16 qcount;
9942
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00009943 vsi->req_queue_pairs = queue_count;
Maciej Sosin373149f2017-04-05 07:50:55 -04009944 i40e_prep_for_reset(pf, true);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00009945
Helin Zhangacd65442015-10-26 19:44:28 -04009946 pf->alloc_rss_size = new_rss_size;
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00009947
Maciej Sosin373149f2017-04-05 07:50:55 -04009948 i40e_reset_and_rebuild(pf, true, true);
Helin Zhang28c58692015-10-26 19:44:27 -04009949
9950 /* Discard the user configured hash keys and lut, if less
9951 * queues are enabled.
9952 */
9953 if (queue_count < vsi->rss_size) {
9954 i40e_clear_rss_config_user(vsi);
9955 dev_dbg(&pf->pdev->dev,
9956 "discard user configured hash keys and lut\n");
9957 }
9958
9959 /* Reset vsi->rss_size, as number of enabled queues changed */
Harshitha Ramamurthyf25571b2017-02-21 15:55:40 -08009960 qcount = vsi->num_queue_pairs / vsi->tc_config.numtc;
9961 vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
Helin Zhang28c58692015-10-26 19:44:27 -04009962
Helin Zhang043dd652015-10-21 19:56:23 -04009963 i40e_pf_config_rss(pf);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00009964 }
Lihong Yang12815052016-09-27 11:28:48 -07009965 dev_info(&pf->pdev->dev, "User requested queue count/HW max RSS count: %d/%d\n",
9966 vsi->req_queue_pairs, pf->rss_size_max);
Helin Zhangacd65442015-10-26 19:44:28 -04009967 return pf->alloc_rss_size;
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00009968}
9969
9970/**
Shannon Nelson4fc8c672017-06-07 05:43:08 -04009971 * i40e_get_partition_bw_setting - Retrieve BW settings for this PF partition
Greg Rosef4492db2015-02-06 08:52:12 +00009972 * @pf: board private structure
9973 **/
Shannon Nelson4fc8c672017-06-07 05:43:08 -04009974i40e_status i40e_get_partition_bw_setting(struct i40e_pf *pf)
Greg Rosef4492db2015-02-06 08:52:12 +00009975{
9976 i40e_status status;
9977 bool min_valid, max_valid;
9978 u32 max_bw, min_bw;
9979
9980 status = i40e_read_bw_from_alt_ram(&pf->hw, &max_bw, &min_bw,
9981 &min_valid, &max_valid);
9982
9983 if (!status) {
9984 if (min_valid)
Shannon Nelson4fc8c672017-06-07 05:43:08 -04009985 pf->min_bw = min_bw;
Greg Rosef4492db2015-02-06 08:52:12 +00009986 if (max_valid)
Shannon Nelson4fc8c672017-06-07 05:43:08 -04009987 pf->max_bw = max_bw;
Greg Rosef4492db2015-02-06 08:52:12 +00009988 }
9989
9990 return status;
9991}
9992
9993/**
Shannon Nelson4fc8c672017-06-07 05:43:08 -04009994 * i40e_set_partition_bw_setting - Set BW settings for this PF partition
Greg Rosef4492db2015-02-06 08:52:12 +00009995 * @pf: board private structure
9996 **/
Shannon Nelson4fc8c672017-06-07 05:43:08 -04009997i40e_status i40e_set_partition_bw_setting(struct i40e_pf *pf)
Greg Rosef4492db2015-02-06 08:52:12 +00009998{
9999 struct i40e_aqc_configure_partition_bw_data bw_data;
10000 i40e_status status;
10001
Jeff Kirsherb40c82e62015-02-27 09:18:34 +000010002 /* Set the valid bit for this PF */
Jesse Brandeburg41a1d042015-06-04 16:24:02 -040010003 bw_data.pf_valid_bits = cpu_to_le16(BIT(pf->hw.pf_id));
Shannon Nelson4fc8c672017-06-07 05:43:08 -040010004 bw_data.max_bw[pf->hw.pf_id] = pf->max_bw & I40E_ALT_BW_VALUE_MASK;
10005 bw_data.min_bw[pf->hw.pf_id] = pf->min_bw & I40E_ALT_BW_VALUE_MASK;
Greg Rosef4492db2015-02-06 08:52:12 +000010006
10007 /* Set the new bandwidths */
10008 status = i40e_aq_configure_partition_bw(&pf->hw, &bw_data, NULL);
10009
10010 return status;
10011}
10012
10013/**
Shannon Nelson4fc8c672017-06-07 05:43:08 -040010014 * i40e_commit_partition_bw_setting - Commit BW settings for this PF partition
Greg Rosef4492db2015-02-06 08:52:12 +000010015 * @pf: board private structure
10016 **/
Shannon Nelson4fc8c672017-06-07 05:43:08 -040010017i40e_status i40e_commit_partition_bw_setting(struct i40e_pf *pf)
Greg Rosef4492db2015-02-06 08:52:12 +000010018{
10019 /* Commit temporary BW setting to permanent NVM image */
10020 enum i40e_admin_queue_err last_aq_status;
10021 i40e_status ret;
10022 u16 nvm_word;
10023
10024 if (pf->hw.partition_id != 1) {
10025 dev_info(&pf->pdev->dev,
10026 "Commit BW only works on partition 1! This is partition %d",
10027 pf->hw.partition_id);
10028 ret = I40E_NOT_SUPPORTED;
10029 goto bw_commit_out;
10030 }
10031
10032 /* Acquire NVM for read access */
10033 ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_READ);
10034 last_aq_status = pf->hw.aq.asq_last_status;
10035 if (ret) {
10036 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010037 "Cannot acquire NVM for read access, err %s aq_err %s\n",
10038 i40e_stat_str(&pf->hw, ret),
10039 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +000010040 goto bw_commit_out;
10041 }
10042
10043 /* Read word 0x10 of NVM - SW compatibility word 1 */
10044 ret = i40e_aq_read_nvm(&pf->hw,
10045 I40E_SR_NVM_CONTROL_WORD,
10046 0x10, sizeof(nvm_word), &nvm_word,
10047 false, NULL);
10048 /* Save off last admin queue command status before releasing
10049 * the NVM
10050 */
10051 last_aq_status = pf->hw.aq.asq_last_status;
10052 i40e_release_nvm(&pf->hw);
10053 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010054 dev_info(&pf->pdev->dev, "NVM read error, err %s aq_err %s\n",
10055 i40e_stat_str(&pf->hw, ret),
10056 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +000010057 goto bw_commit_out;
10058 }
10059
10060 /* Wait a bit for NVM release to complete */
10061 msleep(50);
10062
10063 /* Acquire NVM for write access */
10064 ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_WRITE);
10065 last_aq_status = pf->hw.aq.asq_last_status;
10066 if (ret) {
10067 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010068 "Cannot acquire NVM for write access, err %s aq_err %s\n",
10069 i40e_stat_str(&pf->hw, ret),
10070 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +000010071 goto bw_commit_out;
10072 }
10073 /* Write it back out unchanged to initiate update NVM,
10074 * which will force a write of the shadow (alt) RAM to
10075 * the NVM - thus storing the bandwidth values permanently.
10076 */
10077 ret = i40e_aq_update_nvm(&pf->hw,
10078 I40E_SR_NVM_CONTROL_WORD,
10079 0x10, sizeof(nvm_word),
10080 &nvm_word, true, NULL);
10081 /* Save off last admin queue command status before releasing
10082 * the NVM
10083 */
10084 last_aq_status = pf->hw.aq.asq_last_status;
10085 i40e_release_nvm(&pf->hw);
10086 if (ret)
10087 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010088 "BW settings NOT SAVED, err %s aq_err %s\n",
10089 i40e_stat_str(&pf->hw, ret),
10090 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +000010091bw_commit_out:
10092
10093 return ret;
10094}
10095
10096/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010097 * i40e_sw_init - Initialize general software structures (struct i40e_pf)
10098 * @pf: board private structure to initialize
10099 *
10100 * i40e_sw_init initializes the Adapter private data structure.
10101 * Fields are initialized based on PCI device information and
10102 * OS network device settings (MTU size).
10103 **/
10104static int i40e_sw_init(struct i40e_pf *pf)
10105{
10106 int err = 0;
10107 int size;
10108
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010109 /* Set default capability flags */
10110 pf->flags = I40E_FLAG_RX_CSUM_ENABLED |
10111 I40E_FLAG_MSI_ENABLED |
Mitch Williams2bc7ee82015-02-06 08:52:11 +000010112 I40E_FLAG_MSIX_ENABLED;
10113
Mitch Williamsca99eb92014-04-04 04:43:07 +000010114 /* Set default ITR */
Jacob Keller42702552017-09-07 08:05:48 -040010115 pf->rx_itr_default = I40E_ITR_RX_DEF;
10116 pf->tx_itr_default = I40E_ITR_TX_DEF;
Mitch Williamsca99eb92014-04-04 04:43:07 +000010117
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +000010118 /* Depending on PF configurations, it is possible that the RSS
10119 * maximum might end up larger than the available queues
10120 */
Jesse Brandeburg41a1d042015-06-04 16:24:02 -040010121 pf->rss_size_max = BIT(pf->hw.func_caps.rss_table_entry_width);
Helin Zhangacd65442015-10-26 19:44:28 -040010122 pf->alloc_rss_size = 1;
Anjali Singhai Jain5db4cb52015-02-24 06:58:49 +000010123 pf->rss_table_size = pf->hw.func_caps.rss_table_size;
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +000010124 pf->rss_size_max = min_t(int, pf->rss_size_max,
10125 pf->hw.func_caps.num_tx_qp);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010126 if (pf->hw.func_caps.rss) {
10127 pf->flags |= I40E_FLAG_RSS_ENABLED;
Helin Zhangacd65442015-10-26 19:44:28 -040010128 pf->alloc_rss_size = min_t(int, pf->rss_size_max,
10129 num_online_cpus());
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010130 }
10131
Catherine Sullivan2050bc62013-12-18 13:46:03 +000010132 /* MFP mode enabled */
Pawel Orlowskic78b9532015-04-22 19:34:06 -040010133 if (pf->hw.func_caps.npar_enable || pf->hw.func_caps.flex10_enable) {
Catherine Sullivan2050bc62013-12-18 13:46:03 +000010134 pf->flags |= I40E_FLAG_MFP_ENABLED;
10135 dev_info(&pf->pdev->dev, "MFP mode Enabled\n");
Shannon Nelson4fc8c672017-06-07 05:43:08 -040010136 if (i40e_get_partition_bw_setting(pf)) {
Greg Rosef4492db2015-02-06 08:52:12 +000010137 dev_warn(&pf->pdev->dev,
Shannon Nelson4fc8c672017-06-07 05:43:08 -040010138 "Could not get partition bw settings\n");
10139 } else {
Greg Rosef4492db2015-02-06 08:52:12 +000010140 dev_info(&pf->pdev->dev,
Shannon Nelson4fc8c672017-06-07 05:43:08 -040010141 "Partition BW Min = %8.8x, Max = %8.8x\n",
10142 pf->min_bw, pf->max_bw);
10143
10144 /* nudge the Tx scheduler */
10145 i40e_set_partition_bw_setting(pf);
10146 }
Catherine Sullivan2050bc62013-12-18 13:46:03 +000010147 }
10148
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010149 if ((pf->hw.func_caps.fd_filters_guaranteed > 0) ||
10150 (pf->hw.func_caps.fd_filters_best_effort > 0)) {
10151 pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
10152 pf->atr_sample_rate = I40E_DEFAULT_ATR_SAMPLE_RATE;
Shannon Nelson6eae9c62015-09-03 17:18:55 -040010153 if (pf->flags & I40E_FLAG_MFP_ENABLED &&
10154 pf->hw.num_partitions > 1)
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010155 dev_info(&pf->pdev->dev,
Anjali Singhai Jain0b675842014-03-06 08:59:51 +000010156 "Flow Director Sideband mode Disabled in MFP mode\n");
Shannon Nelson6eae9c62015-09-03 17:18:55 -040010157 else
10158 pf->flags |= I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010159 pf->fdir_pf_filter_count =
10160 pf->hw.func_caps.fd_filters_guaranteed;
10161 pf->hw.fdir_shared_filter_count =
10162 pf->hw.func_caps.fd_filters_best_effort;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010163 }
10164
Anjali Singhai Jain5a433192017-06-23 04:24:45 -040010165 if (pf->hw.mac.type == I40E_MAC_X722) {
Jacob Kellerd36e41d2017-06-23 04:24:46 -040010166 pf->hw_features |= (I40E_HW_RSS_AQ_CAPABLE |
10167 I40E_HW_128_QP_RSS_CAPABLE |
10168 I40E_HW_ATR_EVICT_CAPABLE |
10169 I40E_HW_WB_ON_ITR_CAPABLE |
10170 I40E_HW_MULTIPLE_TCP_UDP_RSS_PCTYPE |
10171 I40E_HW_NO_PCI_LINK_CHECK |
10172 I40E_HW_USE_SET_LLDP_MIB |
10173 I40E_HW_GENEVE_OFFLOAD_CAPABLE |
10174 I40E_HW_PTP_L4_CAPABLE |
10175 I40E_HW_WOL_MC_MAGIC_PKT_WAKE |
10176 I40E_HW_OUTER_UDP_CSUM_CAPABLE);
Anjali Singhai Jain10a955f2017-06-23 04:24:48 -040010177
10178#define I40E_FDEVICT_PCTYPE_DEFAULT 0xc03
10179 if (rd32(&pf->hw, I40E_GLQF_FDEVICTENA(1)) !=
10180 I40E_FDEVICT_PCTYPE_DEFAULT) {
10181 dev_warn(&pf->pdev->dev,
10182 "FD EVICT PCTYPES are not right, disable FD HW EVICT\n");
10183 pf->hw_features &= ~I40E_HW_ATR_EVICT_CAPABLE;
10184 }
Anjali Singhai Jain5a433192017-06-23 04:24:45 -040010185 } else if ((pf->hw.aq.api_maj_ver > 1) ||
10186 ((pf->hw.aq.api_maj_ver == 1) &&
10187 (pf->hw.aq.api_min_ver > 4))) {
10188 /* Supported in FW API version higher than 1.4 */
Jacob Kellerd36e41d2017-06-23 04:24:46 -040010189 pf->hw_features |= I40E_HW_GENEVE_OFFLOAD_CAPABLE;
Anjali Singhai Jain5a433192017-06-23 04:24:45 -040010190 }
10191
10192 /* Enable HW ATR eviction if possible */
Jacob Kellerd36e41d2017-06-23 04:24:46 -040010193 if (pf->hw_features & I40E_HW_ATR_EVICT_CAPABLE)
Anjali Singhai Jain5a433192017-06-23 04:24:45 -040010194 pf->flags |= I40E_FLAG_HW_ATR_EVICT_ENABLED;
10195
Bimmy Pujari6de432c2016-11-11 12:39:38 -080010196 if ((pf->hw.mac.type == I40E_MAC_XL710) &&
Anjali Singhai Jain8eed76f2015-12-09 15:50:31 -080010197 (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 33)) ||
Neerav Parikhf1bbad32016-01-13 16:51:39 -080010198 (pf->hw.aq.fw_maj_ver < 4))) {
Jacob Kellerd36e41d2017-06-23 04:24:46 -040010199 pf->hw_features |= I40E_HW_RESTART_AUTONEG;
Neerav Parikhf1bbad32016-01-13 16:51:39 -080010200 /* No DCB support for FW < v4.33 */
Jacob Kellerd36e41d2017-06-23 04:24:46 -040010201 pf->hw_features |= I40E_HW_NO_DCB_SUPPORT;
Neerav Parikhf1bbad32016-01-13 16:51:39 -080010202 }
10203
10204 /* Disable FW LLDP if FW < v4.3 */
Bimmy Pujari6de432c2016-11-11 12:39:38 -080010205 if ((pf->hw.mac.type == I40E_MAC_XL710) &&
Neerav Parikhf1bbad32016-01-13 16:51:39 -080010206 (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 3)) ||
10207 (pf->hw.aq.fw_maj_ver < 4)))
Jacob Kellerd36e41d2017-06-23 04:24:46 -040010208 pf->hw_features |= I40E_HW_STOP_FW_LLDP;
Neerav Parikhf1bbad32016-01-13 16:51:39 -080010209
10210 /* Use the FW Set LLDP MIB API if FW > v4.40 */
Bimmy Pujari6de432c2016-11-11 12:39:38 -080010211 if ((pf->hw.mac.type == I40E_MAC_XL710) &&
Neerav Parikhf1bbad32016-01-13 16:51:39 -080010212 (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver >= 40)) ||
10213 (pf->hw.aq.fw_maj_ver >= 5)))
Jacob Kellerd36e41d2017-06-23 04:24:46 -040010214 pf->hw_features |= I40E_HW_USE_SET_LLDP_MIB;
Anjali Singhai Jain8eed76f2015-12-09 15:50:31 -080010215
Alan Bradyc3d26b72017-08-29 05:32:32 -040010216 /* Enable PTP L4 if FW > v6.0 */
10217 if (pf->hw.mac.type == I40E_MAC_XL710 &&
10218 pf->hw.aq.fw_maj_ver >= 6)
10219 pf->hw_features |= I40E_HW_PTP_L4_CAPABLE;
10220
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010221 if (pf->hw.func_caps.vmdq) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010222 pf->num_vmdq_vsis = I40E_DEFAULT_NUM_VMDQ_VSI;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -040010223 pf->flags |= I40E_FLAG_VMDQ_ENABLED;
Jesse Brandeburge9e53662015-10-02 17:57:21 -070010224 pf->num_vmdq_qps = i40e_default_queues_per_vmdq(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010225 }
10226
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060010227 if (pf->hw.func_caps.iwarp) {
10228 pf->flags |= I40E_FLAG_IWARP_ENABLED;
10229 /* IWARP needs one extra vector for CQP just like MISC.*/
10230 pf->num_iwarp_msix = (int)num_online_cpus() + 1;
10231 }
10232
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010233#ifdef CONFIG_PCI_IOV
Shannon Nelsonba252f132014-12-11 07:06:34 +000010234 if (pf->hw.func_caps.num_vfs && pf->hw.partition_id == 1) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010235 pf->num_vf_qps = I40E_DEFAULT_QUEUES_PER_VF;
10236 pf->flags |= I40E_FLAG_SRIOV_ENABLED;
10237 pf->num_req_vfs = min_t(int,
10238 pf->hw.func_caps.num_vfs,
10239 I40E_MAX_VF_COUNT);
10240 }
10241#endif /* CONFIG_PCI_IOV */
10242 pf->eeprom_version = 0xDEAD;
10243 pf->lan_veb = I40E_NO_VEB;
10244 pf->lan_vsi = I40E_NO_VSI;
10245
Anjali Singhai Jaind1a8d272015-07-23 16:54:40 -040010246 /* By default FW has this off for performance reasons */
10247 pf->flags &= ~I40E_FLAG_VEB_STATS_ENABLED;
10248
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010249 /* set up queue assignment tracking */
10250 size = sizeof(struct i40e_lump_tracking)
10251 + (sizeof(u16) * pf->hw.func_caps.num_tx_qp);
10252 pf->qp_pile = kzalloc(size, GFP_KERNEL);
10253 if (!pf->qp_pile) {
10254 err = -ENOMEM;
10255 goto sw_init_done;
10256 }
10257 pf->qp_pile->num_entries = pf->hw.func_caps.num_tx_qp;
10258 pf->qp_pile->search_hint = 0;
10259
Anjali Singhai Jain327fe042014-06-04 01:23:26 +000010260 pf->tx_timeout_recovery_level = 1;
10261
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010262 mutex_init(&pf->switch_mutex);
10263
10264sw_init_done:
10265 return err;
10266}
10267
10268/**
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +000010269 * i40e_set_ntuple - set the ntuple feature flag and take action
10270 * @pf: board private structure to initialize
10271 * @features: the feature set that the stack is suggesting
10272 *
10273 * returns a bool to indicate if reset needs to happen
10274 **/
10275bool i40e_set_ntuple(struct i40e_pf *pf, netdev_features_t features)
10276{
10277 bool need_reset = false;
10278
10279 /* Check if Flow Director n-tuple support was enabled or disabled. If
10280 * the state changed, we need to reset.
10281 */
10282 if (features & NETIF_F_NTUPLE) {
10283 /* Enable filters and mark for reset */
10284 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
10285 need_reset = true;
Tushar Davea70e4072016-05-16 12:40:53 -070010286 /* enable FD_SB only if there is MSI-X vector */
10287 if (pf->num_fdsb_msix > 0)
10288 pf->flags |= I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +000010289 } else {
10290 /* turn off filters, mark for reset and clear SW filter list */
10291 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
10292 need_reset = true;
10293 i40e_fdir_filter_exit(pf);
10294 }
Jacob Keller47994c12017-04-19 09:25:57 -040010295 pf->flags &= ~(I40E_FLAG_FD_SB_ENABLED |
10296 I40E_FLAG_FD_SB_AUTO_DISABLED);
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +000010297 /* reset fd counters */
Jacob Keller097dbf52017-02-06 14:38:46 -080010298 pf->fd_add_err = 0;
10299 pf->fd_atr_cnt = 0;
Anjali Singhai Jain8a4f34f2014-06-04 08:45:20 +000010300 /* if ATR was auto disabled it can be re-enabled. */
Jacob Keller47994c12017-04-19 09:25:57 -040010301 if (pf->flags & I40E_FLAG_FD_ATR_AUTO_DISABLED) {
10302 pf->flags &= ~I40E_FLAG_FD_ATR_AUTO_DISABLED;
10303 if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
10304 (I40E_DEBUG_FD & pf->hw.debug_mask))
Jacob Keller234dc4e2016-09-06 18:05:09 -070010305 dev_info(&pf->pdev->dev, "ATR re-enabled.\n");
10306 }
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +000010307 }
10308 return need_reset;
10309}
10310
10311/**
Alan Bradyd8ec9862016-07-27 12:02:38 -070010312 * i40e_clear_rss_lut - clear the rx hash lookup table
10313 * @vsi: the VSI being configured
10314 **/
10315static void i40e_clear_rss_lut(struct i40e_vsi *vsi)
10316{
10317 struct i40e_pf *pf = vsi->back;
10318 struct i40e_hw *hw = &pf->hw;
10319 u16 vf_id = vsi->vf_id;
10320 u8 i;
10321
10322 if (vsi->type == I40E_VSI_MAIN) {
10323 for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
10324 wr32(hw, I40E_PFQF_HLUT(i), 0);
10325 } else if (vsi->type == I40E_VSI_SRIOV) {
10326 for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
10327 i40e_write_rx_ctl(hw, I40E_VFQF_HLUT1(i, vf_id), 0);
10328 } else {
10329 dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
10330 }
10331}
10332
10333/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010334 * i40e_set_features - set the netdev feature flags
10335 * @netdev: ptr to the netdev being adjusted
10336 * @features: the feature set that the stack is suggesting
Maciej Sosin373149f2017-04-05 07:50:55 -040010337 * Note: expects to be called while under rtnl_lock()
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010338 **/
10339static int i40e_set_features(struct net_device *netdev,
10340 netdev_features_t features)
10341{
10342 struct i40e_netdev_priv *np = netdev_priv(netdev);
10343 struct i40e_vsi *vsi = np->vsi;
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +000010344 struct i40e_pf *pf = vsi->back;
10345 bool need_reset;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010346
Alan Bradyd8ec9862016-07-27 12:02:38 -070010347 if (features & NETIF_F_RXHASH && !(netdev->features & NETIF_F_RXHASH))
10348 i40e_pf_config_rss(pf);
10349 else if (!(features & NETIF_F_RXHASH) &&
10350 netdev->features & NETIF_F_RXHASH)
10351 i40e_clear_rss_lut(vsi);
10352
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010353 if (features & NETIF_F_HW_VLAN_CTAG_RX)
10354 i40e_vlan_stripping_enable(vsi);
10355 else
10356 i40e_vlan_stripping_disable(vsi);
10357
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +000010358 need_reset = i40e_set_ntuple(pf, features);
10359
10360 if (need_reset)
Amritha Nambiarff424182017-09-07 04:00:11 -070010361 i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +000010362
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010363 return 0;
10364}
10365
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +000010366/**
Singhai, Anjali6a899022015-12-14 12:21:18 -080010367 * i40e_get_udp_port_idx - Lookup a possibly offloaded for Rx UDP port
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +000010368 * @pf: board private structure
10369 * @port: The UDP port to look up
10370 *
10371 * Returns the index number or I40E_MAX_PF_UDP_OFFLOAD_PORTS if port not found
10372 **/
Jacob Kellerfe0b0cd2017-02-06 14:38:38 -080010373static u8 i40e_get_udp_port_idx(struct i40e_pf *pf, u16 port)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +000010374{
10375 u8 i;
10376
10377 for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
Jacob Keller27826fd2017-04-19 09:25:50 -040010378 if (pf->udp_ports[i].port == port)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +000010379 return i;
10380 }
10381
10382 return i;
10383}
10384
10385/**
Alexander Duyck06a5f7f2016-06-16 12:22:06 -070010386 * i40e_udp_tunnel_add - Get notifications about UDP tunnel ports that come up
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +000010387 * @netdev: This physical port's netdev
Alexander Duyck06a5f7f2016-06-16 12:22:06 -070010388 * @ti: Tunnel endpoint information
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +000010389 **/
Alexander Duyck06a5f7f2016-06-16 12:22:06 -070010390static void i40e_udp_tunnel_add(struct net_device *netdev,
10391 struct udp_tunnel_info *ti)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +000010392{
10393 struct i40e_netdev_priv *np = netdev_priv(netdev);
10394 struct i40e_vsi *vsi = np->vsi;
10395 struct i40e_pf *pf = vsi->back;
Jacob Kellerfe0b0cd2017-02-06 14:38:38 -080010396 u16 port = ntohs(ti->port);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +000010397 u8 next_idx;
10398 u8 idx;
10399
Singhai, Anjali6a899022015-12-14 12:21:18 -080010400 idx = i40e_get_udp_port_idx(pf, port);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +000010401
10402 /* Check if port already exists */
10403 if (idx < I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
Jacob Kellerfe0b0cd2017-02-06 14:38:38 -080010404 netdev_info(netdev, "port %d already offloaded\n", port);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +000010405 return;
10406 }
10407
10408 /* Now check if there is space to add the new port */
Singhai, Anjali6a899022015-12-14 12:21:18 -080010409 next_idx = i40e_get_udp_port_idx(pf, 0);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +000010410
10411 if (next_idx == I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
Alexander Duyck06a5f7f2016-06-16 12:22:06 -070010412 netdev_info(netdev, "maximum number of offloaded UDP ports reached, not adding port %d\n",
Jacob Kellerfe0b0cd2017-02-06 14:38:38 -080010413 port);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +000010414 return;
10415 }
10416
Alexander Duyck06a5f7f2016-06-16 12:22:06 -070010417 switch (ti->type) {
10418 case UDP_TUNNEL_TYPE_VXLAN:
10419 pf->udp_ports[next_idx].type = I40E_AQC_TUNNEL_TYPE_VXLAN;
10420 break;
10421 case UDP_TUNNEL_TYPE_GENEVE:
Jacob Kellerd36e41d2017-06-23 04:24:46 -040010422 if (!(pf->hw_features & I40E_HW_GENEVE_OFFLOAD_CAPABLE))
Alexander Duyck06a5f7f2016-06-16 12:22:06 -070010423 return;
10424 pf->udp_ports[next_idx].type = I40E_AQC_TUNNEL_TYPE_NGE;
10425 break;
10426 default:
10427 return;
10428 }
10429
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +000010430 /* New port: add it and mark its index in the bitmap */
Jacob Keller27826fd2017-04-19 09:25:50 -040010431 pf->udp_ports[next_idx].port = port;
Singhai, Anjali6a899022015-12-14 12:21:18 -080010432 pf->pending_udp_bitmap |= BIT_ULL(next_idx);
10433 pf->flags |= I40E_FLAG_UDP_FILTER_SYNC;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +000010434}
10435
10436/**
Alexander Duyck06a5f7f2016-06-16 12:22:06 -070010437 * i40e_udp_tunnel_del - Get notifications about UDP tunnel ports that go away
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +000010438 * @netdev: This physical port's netdev
Alexander Duyck06a5f7f2016-06-16 12:22:06 -070010439 * @ti: Tunnel endpoint information
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +000010440 **/
Alexander Duyck06a5f7f2016-06-16 12:22:06 -070010441static void i40e_udp_tunnel_del(struct net_device *netdev,
10442 struct udp_tunnel_info *ti)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +000010443{
10444 struct i40e_netdev_priv *np = netdev_priv(netdev);
10445 struct i40e_vsi *vsi = np->vsi;
10446 struct i40e_pf *pf = vsi->back;
Jacob Kellerfe0b0cd2017-02-06 14:38:38 -080010447 u16 port = ntohs(ti->port);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +000010448 u8 idx;
10449
Singhai, Anjali6a899022015-12-14 12:21:18 -080010450 idx = i40e_get_udp_port_idx(pf, port);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +000010451
10452 /* Check if port already exists */
Alexander Duyck06a5f7f2016-06-16 12:22:06 -070010453 if (idx >= I40E_MAX_PF_UDP_OFFLOAD_PORTS)
10454 goto not_found;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +000010455
Alexander Duyck06a5f7f2016-06-16 12:22:06 -070010456 switch (ti->type) {
10457 case UDP_TUNNEL_TYPE_VXLAN:
10458 if (pf->udp_ports[idx].type != I40E_AQC_TUNNEL_TYPE_VXLAN)
10459 goto not_found;
10460 break;
10461 case UDP_TUNNEL_TYPE_GENEVE:
10462 if (pf->udp_ports[idx].type != I40E_AQC_TUNNEL_TYPE_NGE)
10463 goto not_found;
10464 break;
10465 default:
10466 goto not_found;
Singhai, Anjali6a899022015-12-14 12:21:18 -080010467 }
10468
Alexander Duyck06a5f7f2016-06-16 12:22:06 -070010469 /* if port exists, set it to 0 (mark for deletion)
10470 * and make it pending
10471 */
Jacob Keller27826fd2017-04-19 09:25:50 -040010472 pf->udp_ports[idx].port = 0;
Alexander Duyck06a5f7f2016-06-16 12:22:06 -070010473 pf->pending_udp_bitmap |= BIT_ULL(idx);
Singhai, Anjali6a899022015-12-14 12:21:18 -080010474 pf->flags |= I40E_FLAG_UDP_FILTER_SYNC;
10475
Alexander Duyck06a5f7f2016-06-16 12:22:06 -070010476 return;
10477not_found:
10478 netdev_warn(netdev, "UDP port %d was not found, not deleting\n",
Jacob Kellerfe0b0cd2017-02-06 14:38:38 -080010479 port);
Singhai, Anjali6a899022015-12-14 12:21:18 -080010480}
10481
Neerav Parikh1f224ad2014-02-12 01:45:31 +000010482static int i40e_get_phys_port_id(struct net_device *netdev,
Jiri Pirko02637fc2014-11-28 14:34:16 +010010483 struct netdev_phys_item_id *ppid)
Neerav Parikh1f224ad2014-02-12 01:45:31 +000010484{
10485 struct i40e_netdev_priv *np = netdev_priv(netdev);
10486 struct i40e_pf *pf = np->vsi->back;
10487 struct i40e_hw *hw = &pf->hw;
10488
Jacob Kellerd36e41d2017-06-23 04:24:46 -040010489 if (!(pf->hw_features & I40E_HW_PORT_ID_VALID))
Neerav Parikh1f224ad2014-02-12 01:45:31 +000010490 return -EOPNOTSUPP;
10491
10492 ppid->id_len = min_t(int, sizeof(hw->mac.port_addr), sizeof(ppid->id));
10493 memcpy(ppid->id, hw->mac.port_addr, ppid->id_len);
10494
10495 return 0;
10496}
10497
Jesse Brandeburg2f90ade2014-11-20 16:30:02 -080010498/**
10499 * i40e_ndo_fdb_add - add an entry to the hardware database
10500 * @ndm: the input from the stack
10501 * @tb: pointer to array of nladdr (unused)
10502 * @dev: the net device pointer
10503 * @addr: the MAC address entry being added
10504 * @flags: instructions from stack about fdb operation
10505 */
Greg Rose4ba0dea2014-03-06 08:59:55 +000010506static int i40e_ndo_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
10507 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +010010508 const unsigned char *addr, u16 vid,
Greg Rose4ba0dea2014-03-06 08:59:55 +000010509 u16 flags)
Greg Rose4ba0dea2014-03-06 08:59:55 +000010510{
10511 struct i40e_netdev_priv *np = netdev_priv(dev);
10512 struct i40e_pf *pf = np->vsi->back;
10513 int err = 0;
10514
10515 if (!(pf->flags & I40E_FLAG_SRIOV_ENABLED))
10516 return -EOPNOTSUPP;
10517
Or Gerlitz65891fe2014-12-14 18:19:05 +020010518 if (vid) {
10519 pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
10520 return -EINVAL;
10521 }
10522
Greg Rose4ba0dea2014-03-06 08:59:55 +000010523 /* Hardware does not support aging addresses so if a
10524 * ndm_state is given only allow permanent addresses
10525 */
10526 if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
10527 netdev_info(dev, "FDB only supports static addresses\n");
10528 return -EINVAL;
10529 }
10530
10531 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
10532 err = dev_uc_add_excl(dev, addr);
10533 else if (is_multicast_ether_addr(addr))
10534 err = dev_mc_add_excl(dev, addr);
10535 else
10536 err = -EINVAL;
10537
10538 /* Only return duplicate errors if NLM_F_EXCL is set */
10539 if (err == -EEXIST && !(flags & NLM_F_EXCL))
10540 err = 0;
10541
10542 return err;
10543}
10544
Neerav Parikh51616012015-02-06 08:52:14 +000010545/**
10546 * i40e_ndo_bridge_setlink - Set the hardware bridge mode
10547 * @dev: the netdev being configured
10548 * @nlh: RTNL message
10549 *
10550 * Inserts a new hardware bridge if not already created and
10551 * enables the bridging mode requested (VEB or VEPA). If the
10552 * hardware bridge has already been inserted and the request
10553 * is to change the mode then that requires a PF reset to
10554 * allow rebuild of the components with required hardware
10555 * bridge mode enabled.
Maciej Sosin373149f2017-04-05 07:50:55 -040010556 *
10557 * Note: expects to be called while under rtnl_lock()
Neerav Parikh51616012015-02-06 08:52:14 +000010558 **/
10559static int i40e_ndo_bridge_setlink(struct net_device *dev,
Carolyn Wyborny9df70b62015-04-27 14:57:11 -040010560 struct nlmsghdr *nlh,
10561 u16 flags)
Neerav Parikh51616012015-02-06 08:52:14 +000010562{
10563 struct i40e_netdev_priv *np = netdev_priv(dev);
10564 struct i40e_vsi *vsi = np->vsi;
10565 struct i40e_pf *pf = vsi->back;
10566 struct i40e_veb *veb = NULL;
10567 struct nlattr *attr, *br_spec;
10568 int i, rem;
10569
10570 /* Only for PF VSI for now */
10571 if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
10572 return -EOPNOTSUPP;
10573
10574 /* Find the HW bridge for PF VSI */
10575 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
10576 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
10577 veb = pf->veb[i];
10578 }
10579
10580 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
10581
10582 nla_for_each_nested(attr, br_spec, rem) {
10583 __u16 mode;
10584
10585 if (nla_type(attr) != IFLA_BRIDGE_MODE)
10586 continue;
10587
10588 mode = nla_get_u16(attr);
10589 if ((mode != BRIDGE_MODE_VEPA) &&
10590 (mode != BRIDGE_MODE_VEB))
10591 return -EINVAL;
10592
10593 /* Insert a new HW bridge */
10594 if (!veb) {
10595 veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
10596 vsi->tc_config.enabled_tc);
10597 if (veb) {
10598 veb->bridge_mode = mode;
10599 i40e_config_bridge_mode(veb);
10600 } else {
10601 /* No Bridge HW offload available */
10602 return -ENOENT;
10603 }
10604 break;
10605 } else if (mode != veb->bridge_mode) {
10606 /* Existing HW bridge but different mode needs reset */
10607 veb->bridge_mode = mode;
Anjali Singhai Jainfc608612015-05-08 15:35:57 -070010608 /* TODO: If no VFs or VMDq VSIs, disallow VEB mode */
10609 if (mode == BRIDGE_MODE_VEB)
10610 pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
10611 else
10612 pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
Amritha Nambiarff424182017-09-07 04:00:11 -070010613 i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
Neerav Parikh51616012015-02-06 08:52:14 +000010614 break;
10615 }
10616 }
10617
10618 return 0;
10619}
10620
10621/**
10622 * i40e_ndo_bridge_getlink - Get the hardware bridge mode
10623 * @skb: skb buff
10624 * @pid: process id
10625 * @seq: RTNL message seq #
10626 * @dev: the netdev being configured
10627 * @filter_mask: unused
Jesse Brandeburgd4b2f9f2015-09-03 17:18:48 -040010628 * @nlflags: netlink flags passed in
Neerav Parikh51616012015-02-06 08:52:14 +000010629 *
10630 * Return the mode in which the hardware bridge is operating in
10631 * i.e VEB or VEPA.
10632 **/
Neerav Parikh51616012015-02-06 08:52:14 +000010633static int i40e_ndo_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
10634 struct net_device *dev,
Carolyn Wyborny9f4ffc42015-08-31 19:54:42 -040010635 u32 __always_unused filter_mask,
10636 int nlflags)
Neerav Parikh51616012015-02-06 08:52:14 +000010637{
10638 struct i40e_netdev_priv *np = netdev_priv(dev);
10639 struct i40e_vsi *vsi = np->vsi;
10640 struct i40e_pf *pf = vsi->back;
10641 struct i40e_veb *veb = NULL;
10642 int i;
10643
10644 /* Only for PF VSI for now */
10645 if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
10646 return -EOPNOTSUPP;
10647
10648 /* Find the HW bridge for the PF VSI */
10649 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
10650 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
10651 veb = pf->veb[i];
10652 }
10653
10654 if (!veb)
10655 return 0;
10656
Nicolas Dichtel46c264d2015-04-28 18:33:49 +020010657 return ndo_dflt_bridge_getlink(skb, pid, seq, dev, veb->bridge_mode,
Huaibin Wang599b0762016-09-26 09:51:18 +020010658 0, 0, nlflags, filter_mask, NULL);
Neerav Parikh51616012015-02-06 08:52:14 +000010659}
Neerav Parikh51616012015-02-06 08:52:14 +000010660
Joe Stringerf44a75e2015-04-14 17:09:14 -070010661/**
10662 * i40e_features_check - Validate encapsulated packet conforms to limits
10663 * @skb: skb buff
Jean Sacren2bc11c62015-09-19 05:08:43 -060010664 * @dev: This physical port's netdev
Joe Stringerf44a75e2015-04-14 17:09:14 -070010665 * @features: Offload features that the stack believes apply
10666 **/
10667static netdev_features_t i40e_features_check(struct sk_buff *skb,
10668 struct net_device *dev,
10669 netdev_features_t features)
10670{
Alexander Duyckf114dca2016-10-25 16:08:46 -070010671 size_t len;
10672
10673 /* No point in doing any of this if neither checksum nor GSO are
10674 * being requested for this frame. We can rule out both by just
10675 * checking for CHECKSUM_PARTIAL
10676 */
10677 if (skb->ip_summed != CHECKSUM_PARTIAL)
10678 return features;
10679
10680 /* We cannot support GSO if the MSS is going to be less than
10681 * 64 bytes. If it is then we need to drop support for GSO.
10682 */
10683 if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
10684 features &= ~NETIF_F_GSO_MASK;
10685
10686 /* MACLEN can support at most 63 words */
10687 len = skb_network_header(skb) - skb->data;
10688 if (len & ~(63 * 2))
10689 goto out_err;
10690
10691 /* IPLEN and EIPLEN can support at most 127 dwords */
10692 len = skb_transport_header(skb) - skb_network_header(skb);
10693 if (len & ~(127 * 4))
10694 goto out_err;
10695
10696 if (skb->encapsulation) {
10697 /* L4TUNLEN can support 127 words */
10698 len = skb_inner_network_header(skb) - skb_transport_header(skb);
10699 if (len & ~(127 * 2))
10700 goto out_err;
10701
10702 /* IPLEN can support at most 127 dwords */
10703 len = skb_inner_transport_header(skb) -
10704 skb_inner_network_header(skb);
10705 if (len & ~(127 * 4))
10706 goto out_err;
10707 }
10708
10709 /* No need to validate L4LEN as TCP is the only protocol with a
10710 * a flexible value and we support all possible values supported
10711 * by TCP, which is at most 15 dwords
10712 */
Joe Stringerf44a75e2015-04-14 17:09:14 -070010713
10714 return features;
Alexander Duyckf114dca2016-10-25 16:08:46 -070010715out_err:
10716 return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
Joe Stringerf44a75e2015-04-14 17:09:14 -070010717}
10718
Björn Töpel0c8493d2017-05-24 07:55:34 +020010719/**
10720 * i40e_xdp_setup - add/remove an XDP program
10721 * @vsi: VSI to changed
10722 * @prog: XDP program
10723 **/
10724static int i40e_xdp_setup(struct i40e_vsi *vsi,
10725 struct bpf_prog *prog)
10726{
10727 int frame_size = vsi->netdev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
10728 struct i40e_pf *pf = vsi->back;
10729 struct bpf_prog *old_prog;
10730 bool need_reset;
10731 int i;
10732
10733 /* Don't allow frames that span over multiple buffers */
10734 if (frame_size > vsi->rx_buf_len)
10735 return -EINVAL;
10736
10737 if (!i40e_enabled_xdp_vsi(vsi) && !prog)
10738 return 0;
10739
10740 /* When turning XDP on->off/off->on we reset and rebuild the rings. */
10741 need_reset = (i40e_enabled_xdp_vsi(vsi) != !!prog);
10742
10743 if (need_reset)
10744 i40e_prep_for_reset(pf, true);
10745
10746 old_prog = xchg(&vsi->xdp_prog, prog);
10747
10748 if (need_reset)
10749 i40e_reset_and_rebuild(pf, true, true);
10750
10751 for (i = 0; i < vsi->num_queue_pairs; i++)
10752 WRITE_ONCE(vsi->rx_rings[i]->xdp_prog, vsi->xdp_prog);
10753
10754 if (old_prog)
10755 bpf_prog_put(old_prog);
10756
10757 return 0;
10758}
10759
10760/**
10761 * i40e_xdp - implements ndo_xdp for i40e
10762 * @dev: netdevice
10763 * @xdp: XDP command
10764 **/
10765static int i40e_xdp(struct net_device *dev,
10766 struct netdev_xdp *xdp)
10767{
10768 struct i40e_netdev_priv *np = netdev_priv(dev);
10769 struct i40e_vsi *vsi = np->vsi;
10770
10771 if (vsi->type != I40E_VSI_MAIN)
10772 return -EINVAL;
10773
10774 switch (xdp->command) {
10775 case XDP_SETUP_PROG:
10776 return i40e_xdp_setup(vsi, xdp->prog);
10777 case XDP_QUERY_PROG:
10778 xdp->prog_attached = i40e_enabled_xdp_vsi(vsi);
Daniel Borkmanneb230392017-06-24 21:13:52 +020010779 xdp->prog_id = vsi->xdp_prog ? vsi->xdp_prog->aux->id : 0;
Björn Töpel0c8493d2017-05-24 07:55:34 +020010780 return 0;
10781 default:
10782 return -EINVAL;
10783 }
10784}
10785
Shannon Nelson37a29732015-02-27 09:15:19 +000010786static const struct net_device_ops i40e_netdev_ops = {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010787 .ndo_open = i40e_open,
10788 .ndo_stop = i40e_close,
10789 .ndo_start_xmit = i40e_lan_xmit_frame,
10790 .ndo_get_stats64 = i40e_get_netdev_stats_struct,
10791 .ndo_set_rx_mode = i40e_set_rx_mode,
10792 .ndo_validate_addr = eth_validate_addr,
10793 .ndo_set_mac_address = i40e_set_mac,
10794 .ndo_change_mtu = i40e_change_mtu,
Jacob Kellerbeb0dff2014-01-11 05:43:19 +000010795 .ndo_do_ioctl = i40e_ioctl,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010796 .ndo_tx_timeout = i40e_tx_timeout,
10797 .ndo_vlan_rx_add_vid = i40e_vlan_rx_add_vid,
10798 .ndo_vlan_rx_kill_vid = i40e_vlan_rx_kill_vid,
10799#ifdef CONFIG_NET_POLL_CONTROLLER
10800 .ndo_poll_controller = i40e_netpoll,
10801#endif
John Fastabende4c67342016-02-16 21:16:15 -080010802 .ndo_setup_tc = __i40e_setup_tc,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010803 .ndo_set_features = i40e_set_features,
10804 .ndo_set_vf_mac = i40e_ndo_set_vf_mac,
10805 .ndo_set_vf_vlan = i40e_ndo_set_vf_port_vlan,
Sucheta Chakrabortyed616682014-05-22 09:59:05 -040010806 .ndo_set_vf_rate = i40e_ndo_set_vf_bw,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010807 .ndo_get_vf_config = i40e_ndo_get_vf_config,
Mitch Williams588aefa2014-02-11 08:27:49 +000010808 .ndo_set_vf_link_state = i40e_ndo_set_vf_link_state,
Serey Konge6d90042014-07-12 07:28:14 +000010809 .ndo_set_vf_spoofchk = i40e_ndo_set_vf_spoofchk,
Anjali Singhai Jainc3bbbd22016-04-01 03:56:07 -070010810 .ndo_set_vf_trust = i40e_ndo_set_vf_trust,
Alexander Duyck06a5f7f2016-06-16 12:22:06 -070010811 .ndo_udp_tunnel_add = i40e_udp_tunnel_add,
10812 .ndo_udp_tunnel_del = i40e_udp_tunnel_del,
Neerav Parikh1f224ad2014-02-12 01:45:31 +000010813 .ndo_get_phys_port_id = i40e_get_phys_port_id,
Greg Rose4ba0dea2014-03-06 08:59:55 +000010814 .ndo_fdb_add = i40e_ndo_fdb_add,
Joe Stringerf44a75e2015-04-14 17:09:14 -070010815 .ndo_features_check = i40e_features_check,
Neerav Parikh51616012015-02-06 08:52:14 +000010816 .ndo_bridge_getlink = i40e_ndo_bridge_getlink,
10817 .ndo_bridge_setlink = i40e_ndo_bridge_setlink,
Björn Töpel0c8493d2017-05-24 07:55:34 +020010818 .ndo_xdp = i40e_xdp,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010819};
10820
10821/**
10822 * i40e_config_netdev - Setup the netdev flags
10823 * @vsi: the VSI being configured
10824 *
10825 * Returns 0 on success, negative value on failure
10826 **/
10827static int i40e_config_netdev(struct i40e_vsi *vsi)
10828{
10829 struct i40e_pf *pf = vsi->back;
10830 struct i40e_hw *hw = &pf->hw;
10831 struct i40e_netdev_priv *np;
10832 struct net_device *netdev;
Jacob Keller435c0842016-11-08 13:05:10 -080010833 u8 broadcast[ETH_ALEN];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010834 u8 mac_addr[ETH_ALEN];
10835 int etherdev_size;
Preethi Banalabacd75c2017-03-27 14:43:18 -070010836 netdev_features_t hw_enc_features;
10837 netdev_features_t hw_features;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010838
10839 etherdev_size = sizeof(struct i40e_netdev_priv);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +000010840 netdev = alloc_etherdev_mq(etherdev_size, vsi->alloc_queue_pairs);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010841 if (!netdev)
10842 return -ENOMEM;
10843
10844 vsi->netdev = netdev;
10845 np = netdev_priv(netdev);
10846 np->vsi = vsi;
10847
Preethi Banalabacd75c2017-03-27 14:43:18 -070010848 hw_enc_features = NETIF_F_SG |
10849 NETIF_F_IP_CSUM |
10850 NETIF_F_IPV6_CSUM |
10851 NETIF_F_HIGHDMA |
10852 NETIF_F_SOFT_FEATURES |
10853 NETIF_F_TSO |
10854 NETIF_F_TSO_ECN |
10855 NETIF_F_TSO6 |
10856 NETIF_F_GSO_GRE |
10857 NETIF_F_GSO_GRE_CSUM |
10858 NETIF_F_GSO_PARTIAL |
10859 NETIF_F_GSO_UDP_TUNNEL |
10860 NETIF_F_GSO_UDP_TUNNEL_CSUM |
10861 NETIF_F_SCTP_CRC |
10862 NETIF_F_RXHASH |
10863 NETIF_F_RXCSUM |
10864 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010865
Jacob Kellerd36e41d2017-06-23 04:24:46 -040010866 if (!(pf->hw_features & I40E_HW_OUTER_UDP_CSUM_CAPABLE))
Alexander Duyck1c7b4a22016-04-14 17:19:25 -040010867 netdev->gso_partial_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM;
10868
10869 netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
Alexander Duyckb0fe3302016-04-02 00:05:14 -070010870
Preethi Banalabacd75c2017-03-27 14:43:18 -070010871 netdev->hw_enc_features |= hw_enc_features;
10872
Alexander Duyckb0fe3302016-04-02 00:05:14 -070010873 /* record features VLANs can make use of */
Preethi Banalabacd75c2017-03-27 14:43:18 -070010874 netdev->vlan_features |= hw_enc_features | NETIF_F_TSO_MANGLEID;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010875
Anjali Singhai Jain2e86a0b2014-04-01 07:11:53 +000010876 if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
Alexander Duyckb0fe3302016-04-02 00:05:14 -070010877 netdev->hw_features |= NETIF_F_NTUPLE;
Preethi Banalabacd75c2017-03-27 14:43:18 -070010878 hw_features = hw_enc_features |
10879 NETIF_F_HW_VLAN_CTAG_TX |
10880 NETIF_F_HW_VLAN_CTAG_RX;
Anjali Singhai Jain2e86a0b2014-04-01 07:11:53 +000010881
Preethi Banalabacd75c2017-03-27 14:43:18 -070010882 netdev->hw_features |= hw_features;
Alexander Duyckb0fe3302016-04-02 00:05:14 -070010883
Preethi Banalabacd75c2017-03-27 14:43:18 -070010884 netdev->features |= hw_features | NETIF_F_HW_VLAN_CTAG_FILTER;
Alexander Duyck1c7b4a22016-04-14 17:19:25 -040010885 netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010886
10887 if (vsi->type == I40E_VSI_MAIN) {
10888 SET_NETDEV_DEV(netdev, &pf->pdev->dev);
Greg Rose9a173902014-05-22 06:32:02 +000010889 ether_addr_copy(mac_addr, hw->mac.perm_addr);
Jacob Keller41c4c2b2017-04-05 07:50:57 -040010890 /* The following steps are necessary for two reasons. First,
10891 * some older NVM configurations load a default MAC-VLAN
10892 * filter that will accept any tagged packet, and we want to
10893 * replace this with a normal filter. Additionally, it is
10894 * possible our MAC address was provided by the platform using
10895 * Open Firmware or similar.
10896 *
10897 * Thus, we need to remove the default filter and install one
10898 * specific to the MAC address.
Jacob Keller1596b5d2016-11-08 13:05:15 -080010899 */
10900 i40e_rm_default_mac_filter(vsi, mac_addr);
Jacob Keller278e7d02016-10-05 09:30:37 -070010901 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller9569a9a2016-11-11 12:39:37 -080010902 i40e_add_mac_filter(vsi, mac_addr);
Jacob Keller278e7d02016-10-05 09:30:37 -070010903 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010904 } else {
Jacob Keller8c9eb352017-07-12 05:46:12 -040010905 /* Relate the VSI_VMDQ name to the VSI_MAIN name. Note that we
10906 * are still limited by IFNAMSIZ, but we're adding 'v%d\0' to
10907 * the end, which is 4 bytes long, so force truncation of the
10908 * original name by IFNAMSIZ - 4
10909 */
10910 snprintf(netdev->name, IFNAMSIZ, "%.*sv%%d",
10911 IFNAMSIZ - 4,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010912 pf->vsi[pf->lan_vsi]->netdev->name);
10913 random_ether_addr(mac_addr);
Kiran Patil21659032015-09-30 14:09:03 -040010914
Jacob Keller278e7d02016-10-05 09:30:37 -070010915 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller9569a9a2016-11-11 12:39:37 -080010916 i40e_add_mac_filter(vsi, mac_addr);
Jacob Keller278e7d02016-10-05 09:30:37 -070010917 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010918 }
Kiran Patil21659032015-09-30 14:09:03 -040010919
Jacob Keller435c0842016-11-08 13:05:10 -080010920 /* Add the broadcast filter so that we initially will receive
10921 * broadcast packets. Note that when a new VLAN is first added the
10922 * driver will convert all filters marked I40E_VLAN_ANY into VLAN
10923 * specific filters as part of transitioning into "vlan" operation.
10924 * When more VLANs are added, the driver will copy each existing MAC
10925 * filter and add it for the new VLAN.
10926 *
10927 * Broadcast filters are handled specially by
10928 * i40e_sync_filters_subtask, as the driver must to set the broadcast
10929 * promiscuous bit instead of adding this directly as a MAC/VLAN
10930 * filter. The subtask will update the correct broadcast promiscuous
10931 * bits as VLANs become active or inactive.
10932 */
10933 eth_broadcast_addr(broadcast);
10934 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller9569a9a2016-11-11 12:39:37 -080010935 i40e_add_mac_filter(vsi, broadcast);
Jacob Keller435c0842016-11-08 13:05:10 -080010936 spin_unlock_bh(&vsi->mac_filter_hash_lock);
10937
Greg Rose9a173902014-05-22 06:32:02 +000010938 ether_addr_copy(netdev->dev_addr, mac_addr);
10939 ether_addr_copy(netdev->perm_addr, mac_addr);
Alexander Duyckb0fe3302016-04-02 00:05:14 -070010940
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010941 netdev->priv_flags |= IFF_UNICAST_FLT;
10942 netdev->priv_flags |= IFF_SUPP_NOFCS;
10943 /* Setup netdev TC information */
10944 i40e_vsi_config_netdev_tc(vsi, vsi->tc_config.enabled_tc);
10945
10946 netdev->netdev_ops = &i40e_netdev_ops;
10947 netdev->watchdog_timeo = 5 * HZ;
10948 i40e_set_ethtool_ops(netdev);
10949
Jarod Wilson91c527a2016-10-17 15:54:05 -040010950 /* MTU range: 68 - 9706 */
10951 netdev->min_mtu = ETH_MIN_MTU;
Mitch Williams1e3a5fd2017-06-23 04:24:43 -040010952 netdev->max_mtu = I40E_MAX_RXBUFFER - I40E_PACKET_HDR_PAD;
Jarod Wilson91c527a2016-10-17 15:54:05 -040010953
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010954 return 0;
10955}
10956
10957/**
10958 * i40e_vsi_delete - Delete a VSI from the switch
10959 * @vsi: the VSI being removed
10960 *
10961 * Returns 0 on success, negative value on failure
10962 **/
10963static void i40e_vsi_delete(struct i40e_vsi *vsi)
10964{
10965 /* remove default VSI is not allowed */
10966 if (vsi == vsi->back->vsi[vsi->back->lan_vsi])
10967 return;
10968
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010969 i40e_aq_delete_element(&vsi->back->hw, vsi->seid, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010970}
10971
10972/**
Neerav Parikh51616012015-02-06 08:52:14 +000010973 * i40e_is_vsi_uplink_mode_veb - Check if the VSI's uplink bridge mode is VEB
10974 * @vsi: the VSI being queried
10975 *
10976 * Returns 1 if HW bridge mode is VEB and return 0 in case of VEPA mode
10977 **/
10978int i40e_is_vsi_uplink_mode_veb(struct i40e_vsi *vsi)
10979{
10980 struct i40e_veb *veb;
10981 struct i40e_pf *pf = vsi->back;
10982
10983 /* Uplink is not a bridge so default to VEB */
10984 if (vsi->veb_idx == I40E_NO_VEB)
10985 return 1;
10986
10987 veb = pf->veb[vsi->veb_idx];
Akeem G Abodunrin09603ea2015-10-01 14:37:36 -040010988 if (!veb) {
10989 dev_info(&pf->pdev->dev,
10990 "There is no veb associated with the bridge\n");
10991 return -ENOENT;
10992 }
Neerav Parikh51616012015-02-06 08:52:14 +000010993
Akeem G Abodunrin09603ea2015-10-01 14:37:36 -040010994 /* Uplink is a bridge in VEPA mode */
10995 if (veb->bridge_mode & BRIDGE_MODE_VEPA) {
10996 return 0;
10997 } else {
10998 /* Uplink is a bridge in VEB mode */
10999 return 1;
11000 }
11001
11002 /* VEPA is now default bridge, so return 0 */
11003 return 0;
Neerav Parikh51616012015-02-06 08:52:14 +000011004}
11005
11006/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011007 * i40e_add_vsi - Add a VSI to the switch
11008 * @vsi: the VSI being configured
11009 *
11010 * This initializes a VSI context depending on the VSI type to be added and
11011 * passes it down to the add_vsi aq command.
11012 **/
11013static int i40e_add_vsi(struct i40e_vsi *vsi)
11014{
11015 int ret = -ENODEV;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011016 struct i40e_pf *pf = vsi->back;
11017 struct i40e_hw *hw = &pf->hw;
11018 struct i40e_vsi_context ctxt;
Jacob Keller278e7d02016-10-05 09:30:37 -070011019 struct i40e_mac_filter *f;
11020 struct hlist_node *h;
11021 int bkt;
Kiran Patil21659032015-09-30 14:09:03 -040011022
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011023 u8 enabled_tc = 0x1; /* TC0 enabled */
11024 int f_count = 0;
11025
11026 memset(&ctxt, 0, sizeof(ctxt));
11027 switch (vsi->type) {
11028 case I40E_VSI_MAIN:
11029 /* The PF's main VSI is already setup as part of the
11030 * device initialization, so we'll not bother with
11031 * the add_vsi call, but we will retrieve the current
11032 * VSI context.
11033 */
11034 ctxt.seid = pf->main_vsi_seid;
11035 ctxt.pf_num = pf->hw.pf_id;
11036 ctxt.vf_num = 0;
11037 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
11038 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
11039 if (ret) {
11040 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040011041 "couldn't get PF vsi config, err %s aq_err %s\n",
11042 i40e_stat_str(&pf->hw, ret),
11043 i40e_aq_str(&pf->hw,
11044 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011045 return -ENOENT;
11046 }
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -070011047 vsi->info = ctxt.info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011048 vsi->info.valid_sections = 0;
11049
11050 vsi->seid = ctxt.seid;
11051 vsi->id = ctxt.vsi_number;
11052
11053 enabled_tc = i40e_pf_get_tc_map(pf);
11054
Mitch Williams64615b52017-08-29 05:32:30 -040011055 /* Source pruning is enabled by default, so the flag is
11056 * negative logic - if it's set, we need to fiddle with
11057 * the VSI to disable source pruning.
11058 */
11059 if (pf->flags & I40E_FLAG_SOURCE_PRUNING_DISABLED) {
11060 memset(&ctxt, 0, sizeof(ctxt));
11061 ctxt.seid = pf->main_vsi_seid;
11062 ctxt.pf_num = pf->hw.pf_id;
11063 ctxt.vf_num = 0;
11064 ctxt.info.valid_sections |=
11065 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
11066 ctxt.info.switch_id =
11067 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_LOCAL_LB);
11068 ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
11069 if (ret) {
11070 dev_info(&pf->pdev->dev,
11071 "update vsi failed, err %s aq_err %s\n",
11072 i40e_stat_str(&pf->hw, ret),
11073 i40e_aq_str(&pf->hw,
11074 pf->hw.aq.asq_last_status));
11075 ret = -ENOENT;
11076 goto err;
11077 }
11078 }
11079
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011080 /* MFP mode setup queue map and update VSI */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +000011081 if ((pf->flags & I40E_FLAG_MFP_ENABLED) &&
11082 !(pf->hw.func_caps.iscsi)) { /* NIC type PF */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011083 memset(&ctxt, 0, sizeof(ctxt));
11084 ctxt.seid = pf->main_vsi_seid;
11085 ctxt.pf_num = pf->hw.pf_id;
11086 ctxt.vf_num = 0;
11087 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
11088 ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
11089 if (ret) {
11090 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040011091 "update vsi failed, err %s aq_err %s\n",
11092 i40e_stat_str(&pf->hw, ret),
11093 i40e_aq_str(&pf->hw,
11094 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011095 ret = -ENOENT;
11096 goto err;
11097 }
11098 /* update the local VSI info queue map */
11099 i40e_vsi_update_queue_map(vsi, &ctxt);
11100 vsi->info.valid_sections = 0;
11101 } else {
11102 /* Default/Main VSI is only enabled for TC0
11103 * reconfigure it to enable all TCs that are
11104 * available on the port in SFP mode.
Neerav Parikh63d7e5a2014-12-14 01:55:16 +000011105 * For MFP case the iSCSI PF would use this
11106 * flow to enable LAN+iSCSI TC.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011107 */
11108 ret = i40e_vsi_config_tc(vsi, enabled_tc);
11109 if (ret) {
Carolyn Wyborny19279232017-07-14 09:10:07 -040011110 /* Single TC condition is not fatal,
11111 * message and continue
11112 */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011113 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040011114 "failed to configure TCs for main VSI tc_map 0x%08x, err %s aq_err %s\n",
11115 enabled_tc,
11116 i40e_stat_str(&pf->hw, ret),
11117 i40e_aq_str(&pf->hw,
11118 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011119 }
11120 }
11121 break;
11122
11123 case I40E_VSI_FDIR:
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080011124 ctxt.pf_num = hw->pf_id;
11125 ctxt.vf_num = 0;
11126 ctxt.uplink_seid = vsi->uplink_seid;
Neerav Parikh2b18e592015-01-24 09:58:38 +000011127 ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080011128 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
Anjali Singhai Jainfc608612015-05-08 15:35:57 -070011129 if ((pf->flags & I40E_FLAG_VEB_MODE_ENABLED) &&
11130 (i40e_is_vsi_uplink_mode_veb(vsi))) {
Neerav Parikh51616012015-02-06 08:52:14 +000011131 ctxt.info.valid_sections |=
Anjali Singhai Jainfc608612015-05-08 15:35:57 -070011132 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
Neerav Parikh51616012015-02-06 08:52:14 +000011133 ctxt.info.switch_id =
Anjali Singhai Jainfc608612015-05-08 15:35:57 -070011134 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
Neerav Parikh51616012015-02-06 08:52:14 +000011135 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011136 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011137 break;
11138
11139 case I40E_VSI_VMDQ2:
11140 ctxt.pf_num = hw->pf_id;
11141 ctxt.vf_num = 0;
11142 ctxt.uplink_seid = vsi->uplink_seid;
Neerav Parikh2b18e592015-01-24 09:58:38 +000011143 ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011144 ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
11145
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011146 /* This VSI is connected to VEB so the switch_id
11147 * should be set to zero by default.
11148 */
Neerav Parikh51616012015-02-06 08:52:14 +000011149 if (i40e_is_vsi_uplink_mode_veb(vsi)) {
11150 ctxt.info.valid_sections |=
11151 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
11152 ctxt.info.switch_id =
11153 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
11154 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011155
11156 /* Setup the VSI tx/rx queue map for TC0 only for now */
11157 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
11158 break;
11159
11160 case I40E_VSI_SRIOV:
11161 ctxt.pf_num = hw->pf_id;
11162 ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
11163 ctxt.uplink_seid = vsi->uplink_seid;
Neerav Parikh2b18e592015-01-24 09:58:38 +000011164 ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011165 ctxt.flags = I40E_AQ_VSI_TYPE_VF;
11166
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011167 /* This VSI is connected to VEB so the switch_id
11168 * should be set to zero by default.
11169 */
Neerav Parikh51616012015-02-06 08:52:14 +000011170 if (i40e_is_vsi_uplink_mode_veb(vsi)) {
11171 ctxt.info.valid_sections |=
11172 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
11173 ctxt.info.switch_id =
11174 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
11175 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011176
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060011177 if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
11178 ctxt.info.valid_sections |=
11179 cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
11180 ctxt.info.queueing_opt_flags |=
Ashish Shah4b28cdb2016-05-03 15:13:17 -070011181 (I40E_AQ_VSI_QUE_OPT_TCP_ENA |
11182 I40E_AQ_VSI_QUE_OPT_RSS_LUT_VSI);
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060011183 }
11184
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011185 ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
11186 ctxt.info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_MODE_ALL;
Mitch Williamsc674d122014-05-20 08:01:40 +000011187 if (pf->vf[vsi->vf_id].spoofchk) {
11188 ctxt.info.valid_sections |=
11189 cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
11190 ctxt.info.sec_flags |=
11191 (I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
11192 I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
11193 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011194 /* Setup the VSI tx/rx queue map for TC0 only for now */
11195 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
11196 break;
11197
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060011198 case I40E_VSI_IWARP:
11199 /* send down message to iWARP */
11200 break;
11201
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011202 default:
11203 return -ENODEV;
11204 }
11205
11206 if (vsi->type != I40E_VSI_MAIN) {
11207 ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
11208 if (ret) {
11209 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040011210 "add vsi failed, err %s aq_err %s\n",
11211 i40e_stat_str(&pf->hw, ret),
11212 i40e_aq_str(&pf->hw,
11213 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011214 ret = -ENOENT;
11215 goto err;
11216 }
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -070011217 vsi->info = ctxt.info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011218 vsi->info.valid_sections = 0;
11219 vsi->seid = ctxt.seid;
11220 vsi->id = ctxt.vsi_number;
11221 }
11222
Mitch Williamsc3c7ea22016-06-20 09:10:38 -070011223 vsi->active_filters = 0;
Jacob Keller0da36b92017-04-19 09:25:55 -040011224 clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
Jacob Keller278e7d02016-10-05 09:30:37 -070011225 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011226 /* If macvlan filters already exist, force them to get loaded */
Jacob Keller278e7d02016-10-05 09:30:37 -070011227 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -070011228 f->state = I40E_FILTER_NEW;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011229 f_count++;
11230 }
Jacob Keller278e7d02016-10-05 09:30:37 -070011231 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -040011232
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011233 if (f_count) {
11234 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
11235 pf->flags |= I40E_FLAG_FILTER_SYNC;
11236 }
11237
11238 /* Update VSI BW information */
11239 ret = i40e_vsi_get_bw_info(vsi);
11240 if (ret) {
11241 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040011242 "couldn't get vsi bw info, err %s aq_err %s\n",
11243 i40e_stat_str(&pf->hw, ret),
11244 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011245 /* VSI is already added so not tearing that up */
11246 ret = 0;
11247 }
11248
11249err:
11250 return ret;
11251}
11252
11253/**
11254 * i40e_vsi_release - Delete a VSI and free its resources
11255 * @vsi: the VSI being removed
11256 *
11257 * Returns 0 on success or < 0 on error
11258 **/
11259int i40e_vsi_release(struct i40e_vsi *vsi)
11260{
Jacob Keller278e7d02016-10-05 09:30:37 -070011261 struct i40e_mac_filter *f;
11262 struct hlist_node *h;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011263 struct i40e_veb *veb = NULL;
11264 struct i40e_pf *pf;
11265 u16 uplink_seid;
Jacob Keller278e7d02016-10-05 09:30:37 -070011266 int i, n, bkt;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011267
11268 pf = vsi->back;
11269
11270 /* release of a VEB-owner or last VSI is not allowed */
11271 if (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) {
11272 dev_info(&pf->pdev->dev, "VSI %d has existing VEB %d\n",
11273 vsi->seid, vsi->uplink_seid);
11274 return -ENODEV;
11275 }
11276 if (vsi == pf->vsi[pf->lan_vsi] &&
Mauro S. M. Rodrigues9e6c9c02017-05-12 23:26:56 -030011277 !test_bit(__I40E_DOWN, pf->state)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011278 dev_info(&pf->pdev->dev, "Can't remove PF VSI\n");
11279 return -ENODEV;
11280 }
11281
11282 uplink_seid = vsi->uplink_seid;
11283 if (vsi->type != I40E_VSI_SRIOV) {
11284 if (vsi->netdev_registered) {
11285 vsi->netdev_registered = false;
11286 if (vsi->netdev) {
11287 /* results in a call to i40e_close() */
11288 unregister_netdev(vsi->netdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011289 }
11290 } else {
Shannon Nelson90ef8d42014-03-14 07:32:26 +000011291 i40e_vsi_close(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011292 }
11293 i40e_vsi_disable_irq(vsi);
11294 }
11295
Jacob Keller278e7d02016-10-05 09:30:37 -070011296 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller6622f5c2016-10-05 09:30:32 -070011297
11298 /* clear the sync flag on all filters */
11299 if (vsi->netdev) {
11300 __dev_uc_unsync(vsi->netdev, NULL);
11301 __dev_mc_unsync(vsi->netdev, NULL);
11302 }
11303
11304 /* make sure any remaining filters are marked for deletion */
Jacob Keller278e7d02016-10-05 09:30:37 -070011305 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
Jacob Keller290d2552016-10-05 09:30:36 -070011306 __i40e_del_filter(vsi, f);
Jacob Keller6622f5c2016-10-05 09:30:32 -070011307
Jacob Keller278e7d02016-10-05 09:30:37 -070011308 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -040011309
Jesse Brandeburg17652c62015-11-05 17:01:02 -080011310 i40e_sync_vsi_filters(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011311
11312 i40e_vsi_delete(vsi);
11313 i40e_vsi_free_q_vectors(vsi);
Shannon Nelsona4866592014-02-11 08:24:07 +000011314 if (vsi->netdev) {
11315 free_netdev(vsi->netdev);
11316 vsi->netdev = NULL;
11317 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011318 i40e_vsi_clear_rings(vsi);
11319 i40e_vsi_clear(vsi);
11320
11321 /* If this was the last thing on the VEB, except for the
11322 * controlling VSI, remove the VEB, which puts the controlling
11323 * VSI onto the next level down in the switch.
11324 *
11325 * Well, okay, there's one more exception here: don't remove
11326 * the orphan VEBs yet. We'll wait for an explicit remove request
11327 * from up the network stack.
11328 */
Mitch Williams505682c2014-05-20 08:01:37 +000011329 for (n = 0, i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011330 if (pf->vsi[i] &&
11331 pf->vsi[i]->uplink_seid == uplink_seid &&
11332 (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
11333 n++; /* count the VSIs */
11334 }
11335 }
11336 for (i = 0; i < I40E_MAX_VEB; i++) {
11337 if (!pf->veb[i])
11338 continue;
11339 if (pf->veb[i]->uplink_seid == uplink_seid)
11340 n++; /* count the VEBs */
11341 if (pf->veb[i]->seid == uplink_seid)
11342 veb = pf->veb[i];
11343 }
11344 if (n == 0 && veb && veb->uplink_seid != 0)
11345 i40e_veb_release(veb);
11346
11347 return 0;
11348}
11349
11350/**
11351 * i40e_vsi_setup_vectors - Set up the q_vectors for the given VSI
11352 * @vsi: ptr to the VSI
11353 *
11354 * This should only be called after i40e_vsi_mem_alloc() which allocates the
11355 * corresponding SW VSI structure and initializes num_queue_pairs for the
11356 * newly allocated VSI.
11357 *
11358 * Returns 0 on success or negative on failure
11359 **/
11360static int i40e_vsi_setup_vectors(struct i40e_vsi *vsi)
11361{
11362 int ret = -ENOENT;
11363 struct i40e_pf *pf = vsi->back;
11364
Alexander Duyck493fb302013-09-28 07:01:44 +000011365 if (vsi->q_vectors[0]) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011366 dev_info(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
11367 vsi->seid);
11368 return -EEXIST;
11369 }
11370
11371 if (vsi->base_vector) {
Jesse Brandeburgf29eaa32014-02-11 08:24:12 +000011372 dev_info(&pf->pdev->dev, "VSI %d has non-zero base vector %d\n",
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011373 vsi->seid, vsi->base_vector);
11374 return -EEXIST;
11375 }
11376
Greg Rose90e04072014-03-06 08:59:57 +000011377 ret = i40e_vsi_alloc_q_vectors(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011378 if (ret) {
11379 dev_info(&pf->pdev->dev,
11380 "failed to allocate %d q_vector for VSI %d, ret=%d\n",
11381 vsi->num_q_vectors, vsi->seid, ret);
11382 vsi->num_q_vectors = 0;
11383 goto vector_setup_out;
11384 }
11385
Anjali Singhai Jain26cdc442015-07-10 19:36:00 -040011386 /* In Legacy mode, we do not have to get any other vector since we
11387 * piggyback on the misc/ICR0 for queue interrupts.
11388 */
11389 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
11390 return ret;
Shannon Nelson958a3e32013-09-28 07:13:28 +000011391 if (vsi->num_q_vectors)
11392 vsi->base_vector = i40e_get_lump(pf, pf->irq_pile,
11393 vsi->num_q_vectors, vsi->idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011394 if (vsi->base_vector < 0) {
11395 dev_info(&pf->pdev->dev,
Shannon Nelson049a2be2014-10-17 03:14:50 +000011396 "failed to get tracking for %d vectors for VSI %d, err=%d\n",
11397 vsi->num_q_vectors, vsi->seid, vsi->base_vector);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011398 i40e_vsi_free_q_vectors(vsi);
11399 ret = -ENOENT;
11400 goto vector_setup_out;
11401 }
11402
11403vector_setup_out:
11404 return ret;
11405}
11406
11407/**
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000011408 * i40e_vsi_reinit_setup - return and reallocate resources for a VSI
11409 * @vsi: pointer to the vsi.
11410 *
11411 * This re-allocates a vsi's queue resources.
11412 *
11413 * Returns pointer to the successfully allocated and configured VSI sw struct
11414 * on success, otherwise returns NULL on failure.
11415 **/
11416static struct i40e_vsi *i40e_vsi_reinit_setup(struct i40e_vsi *vsi)
11417{
Björn Töpel74608d12017-05-24 07:55:35 +020011418 u16 alloc_queue_pairs;
John Underwoodf5340392016-02-18 09:19:24 -080011419 struct i40e_pf *pf;
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000011420 u8 enabled_tc;
11421 int ret;
11422
John Underwoodf5340392016-02-18 09:19:24 -080011423 if (!vsi)
11424 return NULL;
11425
11426 pf = vsi->back;
11427
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000011428 i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
11429 i40e_vsi_clear_rings(vsi);
11430
11431 i40e_vsi_free_arrays(vsi, false);
11432 i40e_set_num_rings_in_vsi(vsi);
11433 ret = i40e_vsi_alloc_arrays(vsi, false);
11434 if (ret)
11435 goto err_vsi;
11436
Björn Töpel74608d12017-05-24 07:55:35 +020011437 alloc_queue_pairs = vsi->alloc_queue_pairs *
11438 (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
11439
11440 ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000011441 if (ret < 0) {
Shannon Nelson049a2be2014-10-17 03:14:50 +000011442 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040011443 "failed to get tracking for %d queues for VSI %d err %d\n",
Björn Töpel74608d12017-05-24 07:55:35 +020011444 alloc_queue_pairs, vsi->seid, ret);
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000011445 goto err_vsi;
11446 }
11447 vsi->base_queue = ret;
11448
11449 /* Update the FW view of the VSI. Force a reset of TC and queue
11450 * layout configurations.
11451 */
11452 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
11453 pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
11454 pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
11455 i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
Jacob Keller1596b5d2016-11-08 13:05:15 -080011456 if (vsi->type == I40E_VSI_MAIN)
11457 i40e_rm_default_mac_filter(vsi, pf->hw.mac.perm_addr);
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000011458
11459 /* assign it some queues */
11460 ret = i40e_alloc_rings(vsi);
11461 if (ret)
11462 goto err_rings;
11463
11464 /* map all of the rings to the q_vectors */
11465 i40e_vsi_map_rings_to_vectors(vsi);
11466 return vsi;
11467
11468err_rings:
11469 i40e_vsi_free_q_vectors(vsi);
11470 if (vsi->netdev_registered) {
11471 vsi->netdev_registered = false;
11472 unregister_netdev(vsi->netdev);
11473 free_netdev(vsi->netdev);
11474 vsi->netdev = NULL;
11475 }
11476 i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
11477err_vsi:
11478 i40e_vsi_clear(vsi);
11479 return NULL;
11480}
11481
11482/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011483 * i40e_vsi_setup - Set up a VSI by a given type
11484 * @pf: board private structure
11485 * @type: VSI type
11486 * @uplink_seid: the switch element to link to
11487 * @param1: usage depends upon VSI type. For VF types, indicates VF id
11488 *
11489 * This allocates the sw VSI structure and its queue resources, then add a VSI
11490 * to the identified VEB.
11491 *
11492 * Returns pointer to the successfully allocated and configure VSI sw struct on
11493 * success, otherwise returns NULL on failure.
11494 **/
11495struct i40e_vsi *i40e_vsi_setup(struct i40e_pf *pf, u8 type,
11496 u16 uplink_seid, u32 param1)
11497{
11498 struct i40e_vsi *vsi = NULL;
11499 struct i40e_veb *veb = NULL;
Björn Töpel74608d12017-05-24 07:55:35 +020011500 u16 alloc_queue_pairs;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011501 int ret, i;
11502 int v_idx;
11503
11504 /* The requested uplink_seid must be either
11505 * - the PF's port seid
11506 * no VEB is needed because this is the PF
11507 * or this is a Flow Director special case VSI
11508 * - seid of an existing VEB
11509 * - seid of a VSI that owns an existing VEB
11510 * - seid of a VSI that doesn't own a VEB
11511 * a new VEB is created and the VSI becomes the owner
11512 * - seid of the PF VSI, which is what creates the first VEB
11513 * this is a special case of the previous
11514 *
11515 * Find which uplink_seid we were given and create a new VEB if needed
11516 */
11517 for (i = 0; i < I40E_MAX_VEB; i++) {
11518 if (pf->veb[i] && pf->veb[i]->seid == uplink_seid) {
11519 veb = pf->veb[i];
11520 break;
11521 }
11522 }
11523
11524 if (!veb && uplink_seid != pf->mac_seid) {
11525
Mitch Williams505682c2014-05-20 08:01:37 +000011526 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011527 if (pf->vsi[i] && pf->vsi[i]->seid == uplink_seid) {
11528 vsi = pf->vsi[i];
11529 break;
11530 }
11531 }
11532 if (!vsi) {
11533 dev_info(&pf->pdev->dev, "no such uplink_seid %d\n",
11534 uplink_seid);
11535 return NULL;
11536 }
11537
11538 if (vsi->uplink_seid == pf->mac_seid)
11539 veb = i40e_veb_setup(pf, 0, pf->mac_seid, vsi->seid,
11540 vsi->tc_config.enabled_tc);
11541 else if ((vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
11542 veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
11543 vsi->tc_config.enabled_tc);
Anjali Singhai Jain79c21a82014-11-13 03:06:14 +000011544 if (veb) {
11545 if (vsi->seid != pf->vsi[pf->lan_vsi]->seid) {
11546 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -040011547 "New VSI creation error, uplink seid of LAN VSI expected.\n");
Anjali Singhai Jain79c21a82014-11-13 03:06:14 +000011548 return NULL;
11549 }
Anjali Singhai Jainfa11cb32015-05-27 12:06:14 -040011550 /* We come up by default in VEPA mode if SRIOV is not
11551 * already enabled, in which case we can't force VEPA
11552 * mode.
11553 */
11554 if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
11555 veb->bridge_mode = BRIDGE_MODE_VEPA;
11556 pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
11557 }
Neerav Parikh51616012015-02-06 08:52:14 +000011558 i40e_config_bridge_mode(veb);
Anjali Singhai Jain79c21a82014-11-13 03:06:14 +000011559 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011560 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
11561 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
11562 veb = pf->veb[i];
11563 }
11564 if (!veb) {
11565 dev_info(&pf->pdev->dev, "couldn't add VEB\n");
11566 return NULL;
11567 }
11568
11569 vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
11570 uplink_seid = veb->seid;
11571 }
11572
11573 /* get vsi sw struct */
11574 v_idx = i40e_vsi_mem_alloc(pf, type);
11575 if (v_idx < 0)
11576 goto err_alloc;
11577 vsi = pf->vsi[v_idx];
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080011578 if (!vsi)
11579 goto err_alloc;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011580 vsi->type = type;
11581 vsi->veb_idx = (veb ? veb->idx : I40E_NO_VEB);
11582
11583 if (type == I40E_VSI_MAIN)
11584 pf->lan_vsi = v_idx;
11585 else if (type == I40E_VSI_SRIOV)
11586 vsi->vf_id = param1;
11587 /* assign it some queues */
Björn Töpel74608d12017-05-24 07:55:35 +020011588 alloc_queue_pairs = vsi->alloc_queue_pairs *
11589 (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
11590
11591 ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011592 if (ret < 0) {
Shannon Nelson049a2be2014-10-17 03:14:50 +000011593 dev_info(&pf->pdev->dev,
11594 "failed to get tracking for %d queues for VSI %d err=%d\n",
Björn Töpel74608d12017-05-24 07:55:35 +020011595 alloc_queue_pairs, vsi->seid, ret);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011596 goto err_vsi;
11597 }
11598 vsi->base_queue = ret;
11599
11600 /* get a VSI from the hardware */
11601 vsi->uplink_seid = uplink_seid;
11602 ret = i40e_add_vsi(vsi);
11603 if (ret)
11604 goto err_vsi;
11605
11606 switch (vsi->type) {
11607 /* setup the netdev if needed */
11608 case I40E_VSI_MAIN:
11609 case I40E_VSI_VMDQ2:
11610 ret = i40e_config_netdev(vsi);
11611 if (ret)
11612 goto err_netdev;
11613 ret = register_netdev(vsi->netdev);
11614 if (ret)
11615 goto err_netdev;
11616 vsi->netdev_registered = true;
11617 netif_carrier_off(vsi->netdev);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -080011618#ifdef CONFIG_I40E_DCB
11619 /* Setup DCB netlink interface */
11620 i40e_dcbnl_setup(vsi);
11621#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011622 /* fall through */
11623
11624 case I40E_VSI_FDIR:
11625 /* set up vectors and rings if needed */
11626 ret = i40e_vsi_setup_vectors(vsi);
11627 if (ret)
11628 goto err_msix;
11629
11630 ret = i40e_alloc_rings(vsi);
11631 if (ret)
11632 goto err_rings;
11633
11634 /* map all of the rings to the q_vectors */
11635 i40e_vsi_map_rings_to_vectors(vsi);
11636
11637 i40e_vsi_reset_stats(vsi);
11638 break;
11639
11640 default:
11641 /* no netdev or rings for the other VSI types */
11642 break;
11643 }
11644
Jacob Kellerd36e41d2017-06-23 04:24:46 -040011645 if ((pf->hw_features & I40E_HW_RSS_AQ_CAPABLE) &&
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -040011646 (vsi->type == I40E_VSI_VMDQ2)) {
11647 ret = i40e_vsi_config_rss(vsi);
11648 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011649 return vsi;
11650
11651err_rings:
11652 i40e_vsi_free_q_vectors(vsi);
11653err_msix:
11654 if (vsi->netdev_registered) {
11655 vsi->netdev_registered = false;
11656 unregister_netdev(vsi->netdev);
11657 free_netdev(vsi->netdev);
11658 vsi->netdev = NULL;
11659 }
11660err_netdev:
11661 i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
11662err_vsi:
11663 i40e_vsi_clear(vsi);
11664err_alloc:
11665 return NULL;
11666}
11667
11668/**
11669 * i40e_veb_get_bw_info - Query VEB BW information
11670 * @veb: the veb to query
11671 *
11672 * Query the Tx scheduler BW configuration data for given VEB
11673 **/
11674static int i40e_veb_get_bw_info(struct i40e_veb *veb)
11675{
11676 struct i40e_aqc_query_switching_comp_ets_config_resp ets_data;
11677 struct i40e_aqc_query_switching_comp_bw_config_resp bw_data;
11678 struct i40e_pf *pf = veb->pf;
11679 struct i40e_hw *hw = &pf->hw;
11680 u32 tc_bw_max;
11681 int ret = 0;
11682 int i;
11683
11684 ret = i40e_aq_query_switch_comp_bw_config(hw, veb->seid,
11685 &bw_data, NULL);
11686 if (ret) {
11687 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040011688 "query veb bw config failed, err %s aq_err %s\n",
11689 i40e_stat_str(&pf->hw, ret),
11690 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011691 goto out;
11692 }
11693
11694 ret = i40e_aq_query_switch_comp_ets_config(hw, veb->seid,
11695 &ets_data, NULL);
11696 if (ret) {
11697 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040011698 "query veb bw ets config failed, err %s aq_err %s\n",
11699 i40e_stat_str(&pf->hw, ret),
11700 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011701 goto out;
11702 }
11703
11704 veb->bw_limit = le16_to_cpu(ets_data.port_bw_limit);
11705 veb->bw_max_quanta = ets_data.tc_bw_max;
11706 veb->is_abs_credits = bw_data.absolute_credits_enable;
Neerav Parikh23cd1f02014-11-12 00:18:41 +000011707 veb->enabled_tc = ets_data.tc_valid_bits;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011708 tc_bw_max = le16_to_cpu(bw_data.tc_bw_max[0]) |
11709 (le16_to_cpu(bw_data.tc_bw_max[1]) << 16);
11710 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
11711 veb->bw_tc_share_credits[i] = bw_data.tc_bw_share_credits[i];
11712 veb->bw_tc_limit_credits[i] =
11713 le16_to_cpu(bw_data.tc_bw_limits[i]);
11714 veb->bw_tc_max_quanta[i] = ((tc_bw_max >> (i*4)) & 0x7);
11715 }
11716
11717out:
11718 return ret;
11719}
11720
11721/**
11722 * i40e_veb_mem_alloc - Allocates the next available struct veb in the PF
11723 * @pf: board private structure
11724 *
11725 * On error: returns error code (negative)
11726 * On success: returns vsi index in PF (positive)
11727 **/
11728static int i40e_veb_mem_alloc(struct i40e_pf *pf)
11729{
11730 int ret = -ENOENT;
11731 struct i40e_veb *veb;
11732 int i;
11733
11734 /* Need to protect the allocation of switch elements at the PF level */
11735 mutex_lock(&pf->switch_mutex);
11736
11737 /* VEB list may be fragmented if VEB creation/destruction has
11738 * been happening. We can afford to do a quick scan to look
11739 * for any free slots in the list.
11740 *
11741 * find next empty veb slot, looping back around if necessary
11742 */
11743 i = 0;
11744 while ((i < I40E_MAX_VEB) && (pf->veb[i] != NULL))
11745 i++;
11746 if (i >= I40E_MAX_VEB) {
11747 ret = -ENOMEM;
11748 goto err_alloc_veb; /* out of VEB slots! */
11749 }
11750
11751 veb = kzalloc(sizeof(*veb), GFP_KERNEL);
11752 if (!veb) {
11753 ret = -ENOMEM;
11754 goto err_alloc_veb;
11755 }
11756 veb->pf = pf;
11757 veb->idx = i;
11758 veb->enabled_tc = 1;
11759
11760 pf->veb[i] = veb;
11761 ret = i;
11762err_alloc_veb:
11763 mutex_unlock(&pf->switch_mutex);
11764 return ret;
11765}
11766
11767/**
11768 * i40e_switch_branch_release - Delete a branch of the switch tree
11769 * @branch: where to start deleting
11770 *
11771 * This uses recursion to find the tips of the branch to be
11772 * removed, deleting until we get back to and can delete this VEB.
11773 **/
11774static void i40e_switch_branch_release(struct i40e_veb *branch)
11775{
11776 struct i40e_pf *pf = branch->pf;
11777 u16 branch_seid = branch->seid;
11778 u16 veb_idx = branch->idx;
11779 int i;
11780
11781 /* release any VEBs on this VEB - RECURSION */
11782 for (i = 0; i < I40E_MAX_VEB; i++) {
11783 if (!pf->veb[i])
11784 continue;
11785 if (pf->veb[i]->uplink_seid == branch->seid)
11786 i40e_switch_branch_release(pf->veb[i]);
11787 }
11788
11789 /* Release the VSIs on this VEB, but not the owner VSI.
11790 *
11791 * NOTE: Removing the last VSI on a VEB has the SIDE EFFECT of removing
11792 * the VEB itself, so don't use (*branch) after this loop.
11793 */
Mitch Williams505682c2014-05-20 08:01:37 +000011794 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011795 if (!pf->vsi[i])
11796 continue;
11797 if (pf->vsi[i]->uplink_seid == branch_seid &&
11798 (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
11799 i40e_vsi_release(pf->vsi[i]);
11800 }
11801 }
11802
11803 /* There's one corner case where the VEB might not have been
11804 * removed, so double check it here and remove it if needed.
11805 * This case happens if the veb was created from the debugfs
11806 * commands and no VSIs were added to it.
11807 */
11808 if (pf->veb[veb_idx])
11809 i40e_veb_release(pf->veb[veb_idx]);
11810}
11811
11812/**
11813 * i40e_veb_clear - remove veb struct
11814 * @veb: the veb to remove
11815 **/
11816static void i40e_veb_clear(struct i40e_veb *veb)
11817{
11818 if (!veb)
11819 return;
11820
11821 if (veb->pf) {
11822 struct i40e_pf *pf = veb->pf;
11823
11824 mutex_lock(&pf->switch_mutex);
11825 if (pf->veb[veb->idx] == veb)
11826 pf->veb[veb->idx] = NULL;
11827 mutex_unlock(&pf->switch_mutex);
11828 }
11829
11830 kfree(veb);
11831}
11832
11833/**
11834 * i40e_veb_release - Delete a VEB and free its resources
11835 * @veb: the VEB being removed
11836 **/
11837void i40e_veb_release(struct i40e_veb *veb)
11838{
11839 struct i40e_vsi *vsi = NULL;
11840 struct i40e_pf *pf;
11841 int i, n = 0;
11842
11843 pf = veb->pf;
11844
11845 /* find the remaining VSI and check for extras */
Mitch Williams505682c2014-05-20 08:01:37 +000011846 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011847 if (pf->vsi[i] && pf->vsi[i]->uplink_seid == veb->seid) {
11848 n++;
11849 vsi = pf->vsi[i];
11850 }
11851 }
11852 if (n != 1) {
11853 dev_info(&pf->pdev->dev,
11854 "can't remove VEB %d with %d VSIs left\n",
11855 veb->seid, n);
11856 return;
11857 }
11858
11859 /* move the remaining VSI to uplink veb */
11860 vsi->flags &= ~I40E_VSI_FLAG_VEB_OWNER;
11861 if (veb->uplink_seid) {
11862 vsi->uplink_seid = veb->uplink_seid;
11863 if (veb->uplink_seid == pf->mac_seid)
11864 vsi->veb_idx = I40E_NO_VEB;
11865 else
11866 vsi->veb_idx = veb->veb_idx;
11867 } else {
11868 /* floating VEB */
11869 vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
11870 vsi->veb_idx = pf->vsi[pf->lan_vsi]->veb_idx;
11871 }
11872
11873 i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
11874 i40e_veb_clear(veb);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011875}
11876
11877/**
11878 * i40e_add_veb - create the VEB in the switch
11879 * @veb: the VEB to be instantiated
11880 * @vsi: the controlling VSI
11881 **/
11882static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi)
11883{
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040011884 struct i40e_pf *pf = veb->pf;
Shannon Nelson66fc3602016-01-13 16:51:42 -080011885 bool enable_stats = !!(pf->flags & I40E_FLAG_VEB_STATS_ENABLED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011886 int ret;
11887
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040011888 ret = i40e_aq_add_veb(&pf->hw, veb->uplink_seid, vsi->seid,
Mitch Williams5bc16032016-05-16 10:26:43 -070011889 veb->enabled_tc, false,
Shannon Nelson66fc3602016-01-13 16:51:42 -080011890 &veb->seid, enable_stats, NULL);
Mitch Williams5bc16032016-05-16 10:26:43 -070011891
11892 /* get a VEB from the hardware */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011893 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040011894 dev_info(&pf->pdev->dev,
11895 "couldn't add VEB, err %s aq_err %s\n",
11896 i40e_stat_str(&pf->hw, ret),
11897 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011898 return -EPERM;
11899 }
11900
11901 /* get statistics counter */
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040011902 ret = i40e_aq_get_veb_parameters(&pf->hw, veb->seid, NULL, NULL,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011903 &veb->stats_idx, NULL, NULL, NULL);
11904 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040011905 dev_info(&pf->pdev->dev,
11906 "couldn't get VEB statistics idx, err %s aq_err %s\n",
11907 i40e_stat_str(&pf->hw, ret),
11908 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011909 return -EPERM;
11910 }
11911 ret = i40e_veb_get_bw_info(veb);
11912 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040011913 dev_info(&pf->pdev->dev,
11914 "couldn't get VEB bw info, err %s aq_err %s\n",
11915 i40e_stat_str(&pf->hw, ret),
11916 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
11917 i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011918 return -ENOENT;
11919 }
11920
11921 vsi->uplink_seid = veb->seid;
11922 vsi->veb_idx = veb->idx;
11923 vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
11924
11925 return 0;
11926}
11927
11928/**
11929 * i40e_veb_setup - Set up a VEB
11930 * @pf: board private structure
11931 * @flags: VEB setup flags
11932 * @uplink_seid: the switch element to link to
11933 * @vsi_seid: the initial VSI seid
11934 * @enabled_tc: Enabled TC bit-map
11935 *
11936 * This allocates the sw VEB structure and links it into the switch
11937 * It is possible and legal for this to be a duplicate of an already
11938 * existing VEB. It is also possible for both uplink and vsi seids
11939 * to be zero, in order to create a floating VEB.
11940 *
11941 * Returns pointer to the successfully allocated VEB sw struct on
11942 * success, otherwise returns NULL on failure.
11943 **/
11944struct i40e_veb *i40e_veb_setup(struct i40e_pf *pf, u16 flags,
11945 u16 uplink_seid, u16 vsi_seid,
11946 u8 enabled_tc)
11947{
11948 struct i40e_veb *veb, *uplink_veb = NULL;
11949 int vsi_idx, veb_idx;
11950 int ret;
11951
11952 /* if one seid is 0, the other must be 0 to create a floating relay */
11953 if ((uplink_seid == 0 || vsi_seid == 0) &&
11954 (uplink_seid + vsi_seid != 0)) {
11955 dev_info(&pf->pdev->dev,
11956 "one, not both seid's are 0: uplink=%d vsi=%d\n",
11957 uplink_seid, vsi_seid);
11958 return NULL;
11959 }
11960
11961 /* make sure there is such a vsi and uplink */
Mitch Williams505682c2014-05-20 08:01:37 +000011962 for (vsi_idx = 0; vsi_idx < pf->num_alloc_vsi; vsi_idx++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011963 if (pf->vsi[vsi_idx] && pf->vsi[vsi_idx]->seid == vsi_seid)
11964 break;
Mitch Williams505682c2014-05-20 08:01:37 +000011965 if (vsi_idx >= pf->num_alloc_vsi && vsi_seid != 0) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011966 dev_info(&pf->pdev->dev, "vsi seid %d not found\n",
11967 vsi_seid);
11968 return NULL;
11969 }
11970
11971 if (uplink_seid && uplink_seid != pf->mac_seid) {
11972 for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
11973 if (pf->veb[veb_idx] &&
11974 pf->veb[veb_idx]->seid == uplink_seid) {
11975 uplink_veb = pf->veb[veb_idx];
11976 break;
11977 }
11978 }
11979 if (!uplink_veb) {
11980 dev_info(&pf->pdev->dev,
11981 "uplink seid %d not found\n", uplink_seid);
11982 return NULL;
11983 }
11984 }
11985
11986 /* get veb sw struct */
11987 veb_idx = i40e_veb_mem_alloc(pf);
11988 if (veb_idx < 0)
11989 goto err_alloc;
11990 veb = pf->veb[veb_idx];
11991 veb->flags = flags;
11992 veb->uplink_seid = uplink_seid;
11993 veb->veb_idx = (uplink_veb ? uplink_veb->idx : I40E_NO_VEB);
11994 veb->enabled_tc = (enabled_tc ? enabled_tc : 0x1);
11995
11996 /* create the VEB in the switch */
11997 ret = i40e_add_veb(veb, pf->vsi[vsi_idx]);
11998 if (ret)
11999 goto err_veb;
Shannon Nelson1bb8b932014-04-23 04:49:54 +000012000 if (vsi_idx == pf->lan_vsi)
12001 pf->lan_veb = veb->idx;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012002
12003 return veb;
12004
12005err_veb:
12006 i40e_veb_clear(veb);
12007err_alloc:
12008 return NULL;
12009}
12010
12011/**
Jeff Kirsherb40c82e62015-02-27 09:18:34 +000012012 * i40e_setup_pf_switch_element - set PF vars based on switch type
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012013 * @pf: board private structure
12014 * @ele: element we are building info from
12015 * @num_reported: total number of elements
12016 * @printconfig: should we print the contents
12017 *
12018 * helper function to assist in extracting a few useful SEID values.
12019 **/
12020static void i40e_setup_pf_switch_element(struct i40e_pf *pf,
12021 struct i40e_aqc_switch_config_element_resp *ele,
12022 u16 num_reported, bool printconfig)
12023{
12024 u16 downlink_seid = le16_to_cpu(ele->downlink_seid);
12025 u16 uplink_seid = le16_to_cpu(ele->uplink_seid);
12026 u8 element_type = ele->element_type;
12027 u16 seid = le16_to_cpu(ele->seid);
12028
12029 if (printconfig)
12030 dev_info(&pf->pdev->dev,
12031 "type=%d seid=%d uplink=%d downlink=%d\n",
12032 element_type, seid, uplink_seid, downlink_seid);
12033
12034 switch (element_type) {
12035 case I40E_SWITCH_ELEMENT_TYPE_MAC:
12036 pf->mac_seid = seid;
12037 break;
12038 case I40E_SWITCH_ELEMENT_TYPE_VEB:
12039 /* Main VEB? */
12040 if (uplink_seid != pf->mac_seid)
12041 break;
12042 if (pf->lan_veb == I40E_NO_VEB) {
12043 int v;
12044
12045 /* find existing or else empty VEB */
12046 for (v = 0; v < I40E_MAX_VEB; v++) {
12047 if (pf->veb[v] && (pf->veb[v]->seid == seid)) {
12048 pf->lan_veb = v;
12049 break;
12050 }
12051 }
12052 if (pf->lan_veb == I40E_NO_VEB) {
12053 v = i40e_veb_mem_alloc(pf);
12054 if (v < 0)
12055 break;
12056 pf->lan_veb = v;
12057 }
12058 }
12059
12060 pf->veb[pf->lan_veb]->seid = seid;
12061 pf->veb[pf->lan_veb]->uplink_seid = pf->mac_seid;
12062 pf->veb[pf->lan_veb]->pf = pf;
12063 pf->veb[pf->lan_veb]->veb_idx = I40E_NO_VEB;
12064 break;
12065 case I40E_SWITCH_ELEMENT_TYPE_VSI:
12066 if (num_reported != 1)
12067 break;
12068 /* This is immediately after a reset so we can assume this is
12069 * the PF's VSI
12070 */
12071 pf->mac_seid = uplink_seid;
12072 pf->pf_seid = downlink_seid;
12073 pf->main_vsi_seid = seid;
12074 if (printconfig)
12075 dev_info(&pf->pdev->dev,
12076 "pf_seid=%d main_vsi_seid=%d\n",
12077 pf->pf_seid, pf->main_vsi_seid);
12078 break;
12079 case I40E_SWITCH_ELEMENT_TYPE_PF:
12080 case I40E_SWITCH_ELEMENT_TYPE_VF:
12081 case I40E_SWITCH_ELEMENT_TYPE_EMP:
12082 case I40E_SWITCH_ELEMENT_TYPE_BMC:
12083 case I40E_SWITCH_ELEMENT_TYPE_PE:
12084 case I40E_SWITCH_ELEMENT_TYPE_PA:
12085 /* ignore these for now */
12086 break;
12087 default:
12088 dev_info(&pf->pdev->dev, "unknown element type=%d seid=%d\n",
12089 element_type, seid);
12090 break;
12091 }
12092}
12093
12094/**
12095 * i40e_fetch_switch_configuration - Get switch config from firmware
12096 * @pf: board private structure
12097 * @printconfig: should we print the contents
12098 *
12099 * Get the current switch configuration from the device and
12100 * extract a few useful SEID values.
12101 **/
12102int i40e_fetch_switch_configuration(struct i40e_pf *pf, bool printconfig)
12103{
12104 struct i40e_aqc_get_switch_config_resp *sw_config;
12105 u16 next_seid = 0;
12106 int ret = 0;
12107 u8 *aq_buf;
12108 int i;
12109
12110 aq_buf = kzalloc(I40E_AQ_LARGE_BUF, GFP_KERNEL);
12111 if (!aq_buf)
12112 return -ENOMEM;
12113
12114 sw_config = (struct i40e_aqc_get_switch_config_resp *)aq_buf;
12115 do {
12116 u16 num_reported, num_total;
12117
12118 ret = i40e_aq_get_switch_config(&pf->hw, sw_config,
12119 I40E_AQ_LARGE_BUF,
12120 &next_seid, NULL);
12121 if (ret) {
12122 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040012123 "get switch config failed err %s aq_err %s\n",
12124 i40e_stat_str(&pf->hw, ret),
12125 i40e_aq_str(&pf->hw,
12126 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012127 kfree(aq_buf);
12128 return -ENOENT;
12129 }
12130
12131 num_reported = le16_to_cpu(sw_config->header.num_reported);
12132 num_total = le16_to_cpu(sw_config->header.num_total);
12133
12134 if (printconfig)
12135 dev_info(&pf->pdev->dev,
12136 "header: %d reported %d total\n",
12137 num_reported, num_total);
12138
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012139 for (i = 0; i < num_reported; i++) {
12140 struct i40e_aqc_switch_config_element_resp *ele =
12141 &sw_config->element[i];
12142
12143 i40e_setup_pf_switch_element(pf, ele, num_reported,
12144 printconfig);
12145 }
12146 } while (next_seid != 0);
12147
12148 kfree(aq_buf);
12149 return ret;
12150}
12151
12152/**
12153 * i40e_setup_pf_switch - Setup the HW switch on startup or after reset
12154 * @pf: board private structure
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000012155 * @reinit: if the Main VSI needs to re-initialized.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012156 *
12157 * Returns 0 on success, negative value on failure
12158 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000012159static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012160{
Anjali Singhai Jainb5569892016-05-03 15:13:12 -070012161 u16 flags = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012162 int ret;
12163
12164 /* find out what's out there already */
12165 ret = i40e_fetch_switch_configuration(pf, false);
12166 if (ret) {
12167 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040012168 "couldn't fetch switch config, err %s aq_err %s\n",
12169 i40e_stat_str(&pf->hw, ret),
12170 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012171 return ret;
12172 }
12173 i40e_pf_reset_stats(pf);
12174
Anjali Singhai Jainb5569892016-05-03 15:13:12 -070012175 /* set the switch config bit for the whole device to
12176 * support limited promisc or true promisc
12177 * when user requests promisc. The default is limited
12178 * promisc.
12179 */
12180
12181 if ((pf->hw.pf_id == 0) &&
12182 !(pf->flags & I40E_FLAG_TRUE_PROMISC_SUPPORT))
12183 flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
12184
12185 if (pf->hw.pf_id == 0) {
12186 u16 valid_flags;
12187
12188 valid_flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
Amritha Nambiar5efe0c62017-10-27 02:35:45 -070012189 ret = i40e_aq_set_switch_config(&pf->hw, flags, valid_flags, 0,
Anjali Singhai Jainb5569892016-05-03 15:13:12 -070012190 NULL);
12191 if (ret && pf->hw.aq.asq_last_status != I40E_AQ_RC_ESRCH) {
12192 dev_info(&pf->pdev->dev,
12193 "couldn't set switch config bits, err %s aq_err %s\n",
12194 i40e_stat_str(&pf->hw, ret),
12195 i40e_aq_str(&pf->hw,
12196 pf->hw.aq.asq_last_status));
12197 /* not a fatal problem, just keep going */
12198 }
12199 }
12200
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012201 /* first time setup */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000012202 if (pf->lan_vsi == I40E_NO_VSI || reinit) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012203 struct i40e_vsi *vsi = NULL;
12204 u16 uplink_seid;
12205
12206 /* Set up the PF VSI associated with the PF's main VSI
12207 * that is already in the HW switch
12208 */
12209 if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
12210 uplink_seid = pf->veb[pf->lan_veb]->seid;
12211 else
12212 uplink_seid = pf->mac_seid;
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000012213 if (pf->lan_vsi == I40E_NO_VSI)
12214 vsi = i40e_vsi_setup(pf, I40E_VSI_MAIN, uplink_seid, 0);
12215 else if (reinit)
12216 vsi = i40e_vsi_reinit_setup(pf->vsi[pf->lan_vsi]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012217 if (!vsi) {
12218 dev_info(&pf->pdev->dev, "setup of MAIN VSI failed\n");
Amritha Nambiaraaf66502017-10-27 02:35:56 -070012219 i40e_cloud_filter_exit(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012220 i40e_fdir_teardown(pf);
12221 return -EAGAIN;
12222 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012223 } else {
12224 /* force a reset of TC and queue layout configurations */
12225 u8 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
Jesse Brandeburg6995b362015-08-28 17:55:54 -040012226
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012227 pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
12228 pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
12229 i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
12230 }
12231 i40e_vlan_stripping_disable(pf->vsi[pf->lan_vsi]);
12232
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080012233 i40e_fdir_sb_setup(pf);
12234
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012235 /* Setup static PF queue filter control settings */
12236 ret = i40e_setup_pf_filter_control(pf);
12237 if (ret) {
12238 dev_info(&pf->pdev->dev, "setup_pf_filter_control failed: %d\n",
12239 ret);
12240 /* Failure here should not stop continuing other steps */
12241 }
12242
12243 /* enable RSS in the HW, even for only one queue, as the stack can use
12244 * the hash
12245 */
12246 if ((pf->flags & I40E_FLAG_RSS_ENABLED))
Helin Zhang043dd652015-10-21 19:56:23 -040012247 i40e_pf_config_rss(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012248
12249 /* fill in link information and enable LSE reporting */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012250 i40e_link_event(pf);
12251
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +000012252 /* Initialize user-specific link properties */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012253 pf->fc_autoneg_status = ((pf->hw.phy.link_info.an_info &
12254 I40E_AQ_AN_COMPLETED) ? true : false);
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +000012255
Jacob Kellerbeb0dff2014-01-11 05:43:19 +000012256 i40e_ptp_init(pf);
12257
Alexander Duyck1f190d92017-04-19 09:25:51 -040012258 /* repopulate tunnel port filters */
12259 i40e_sync_udp_filters(pf);
12260
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012261 return ret;
12262}
12263
12264/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012265 * i40e_determine_queue_usage - Work out queue distribution
12266 * @pf: board private structure
12267 **/
12268static void i40e_determine_queue_usage(struct i40e_pf *pf)
12269{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012270 int queues_left;
Shannon Nelsone50d5752017-07-24 18:17:42 -070012271 int q_max;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012272
12273 pf->num_lan_qps = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012274
12275 /* Find the max queues to be put into basic use. We'll always be
12276 * using TC0, whether or not DCB is running, and TC0 will get the
12277 * big RSS set.
12278 */
12279 queues_left = pf->hw.func_caps.num_tx_qp;
12280
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080012281 if ((queues_left == 1) ||
Frank Zhang9aa7e932014-05-20 08:01:42 +000012282 !(pf->flags & I40E_FLAG_MSIX_ENABLED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012283 /* one qp for PF, no queues for anything else */
12284 queues_left = 0;
Helin Zhangacd65442015-10-26 19:44:28 -040012285 pf->alloc_rss_size = pf->num_lan_qps = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012286
12287 /* make sure all the fancies are disabled */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080012288 pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060012289 I40E_FLAG_IWARP_ENABLED |
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080012290 I40E_FLAG_FD_SB_ENABLED |
12291 I40E_FLAG_FD_ATR_ENABLED |
Neerav Parikh4d9b6042014-05-22 06:31:51 +000012292 I40E_FLAG_DCB_CAPABLE |
Dave Ertmana0362442016-08-29 17:38:26 -070012293 I40E_FLAG_DCB_ENABLED |
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080012294 I40E_FLAG_SRIOV_ENABLED |
12295 I40E_FLAG_VMDQ_ENABLED);
Frank Zhang9aa7e932014-05-20 08:01:42 +000012296 } else if (!(pf->flags & (I40E_FLAG_RSS_ENABLED |
12297 I40E_FLAG_FD_SB_ENABLED |
Anjali Singhai Jainbbe7d0e2014-05-20 08:01:44 +000012298 I40E_FLAG_FD_ATR_ENABLED |
Neerav Parikh4d9b6042014-05-22 06:31:51 +000012299 I40E_FLAG_DCB_CAPABLE))) {
Frank Zhang9aa7e932014-05-20 08:01:42 +000012300 /* one qp for PF */
Helin Zhangacd65442015-10-26 19:44:28 -040012301 pf->alloc_rss_size = pf->num_lan_qps = 1;
Frank Zhang9aa7e932014-05-20 08:01:42 +000012302 queues_left -= pf->num_lan_qps;
12303
12304 pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060012305 I40E_FLAG_IWARP_ENABLED |
Frank Zhang9aa7e932014-05-20 08:01:42 +000012306 I40E_FLAG_FD_SB_ENABLED |
12307 I40E_FLAG_FD_ATR_ENABLED |
12308 I40E_FLAG_DCB_ENABLED |
12309 I40E_FLAG_VMDQ_ENABLED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012310 } else {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080012311 /* Not enough queues for all TCs */
Neerav Parikh4d9b6042014-05-22 06:31:51 +000012312 if ((pf->flags & I40E_FLAG_DCB_CAPABLE) &&
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080012313 (queues_left < I40E_MAX_TRAFFIC_CLASS)) {
Dave Ertmana0362442016-08-29 17:38:26 -070012314 pf->flags &= ~(I40E_FLAG_DCB_CAPABLE |
12315 I40E_FLAG_DCB_ENABLED);
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080012316 dev_info(&pf->pdev->dev, "not enough queues for DCB. DCB is disabled.\n");
12317 }
Shannon Nelsone50d5752017-07-24 18:17:42 -070012318
12319 /* limit lan qps to the smaller of qps, cpus or msix */
12320 q_max = max_t(int, pf->rss_size_max, num_online_cpus());
12321 q_max = min_t(int, q_max, pf->hw.func_caps.num_tx_qp);
12322 q_max = min_t(int, q_max, pf->hw.func_caps.num_msix_vectors);
12323 pf->num_lan_qps = q_max;
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +000012324
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080012325 queues_left -= pf->num_lan_qps;
12326 }
12327
12328 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
12329 if (queues_left > 1) {
12330 queues_left -= 1; /* save 1 queue for FD */
12331 } else {
12332 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
12333 dev_info(&pf->pdev->dev, "not enough queues for Flow Director. Flow Director feature is disabled\n");
12334 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012335 }
12336
12337 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
12338 pf->num_vf_qps && pf->num_req_vfs && queues_left) {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080012339 pf->num_req_vfs = min_t(int, pf->num_req_vfs,
12340 (queues_left / pf->num_vf_qps));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012341 queues_left -= (pf->num_req_vfs * pf->num_vf_qps);
12342 }
12343
12344 if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
12345 pf->num_vmdq_vsis && pf->num_vmdq_qps && queues_left) {
12346 pf->num_vmdq_vsis = min_t(int, pf->num_vmdq_vsis,
12347 (queues_left / pf->num_vmdq_qps));
12348 queues_left -= (pf->num_vmdq_vsis * pf->num_vmdq_qps);
12349 }
12350
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +000012351 pf->queues_left = queues_left;
Neerav Parikh8279e492015-09-03 17:18:50 -040012352 dev_dbg(&pf->pdev->dev,
12353 "qs_avail=%d FD SB=%d lan_qs=%d lan_tc0=%d vf=%d*%d vmdq=%d*%d, remaining=%d\n",
12354 pf->hw.func_caps.num_tx_qp,
12355 !!(pf->flags & I40E_FLAG_FD_SB_ENABLED),
Helin Zhangacd65442015-10-26 19:44:28 -040012356 pf->num_lan_qps, pf->alloc_rss_size, pf->num_req_vfs,
12357 pf->num_vf_qps, pf->num_vmdq_vsis, pf->num_vmdq_qps,
12358 queues_left);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012359}
12360
12361/**
12362 * i40e_setup_pf_filter_control - Setup PF static filter control
12363 * @pf: PF to be setup
12364 *
Jeff Kirsherb40c82e62015-02-27 09:18:34 +000012365 * i40e_setup_pf_filter_control sets up a PF's initial filter control
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012366 * settings. If PE/FCoE are enabled then it will also set the per PF
12367 * based filter sizes required for them. It also enables Flow director,
12368 * ethertype and macvlan type filter settings for the pf.
12369 *
12370 * Returns 0 on success, negative on failure
12371 **/
12372static int i40e_setup_pf_filter_control(struct i40e_pf *pf)
12373{
12374 struct i40e_filter_control_settings *settings = &pf->filter_settings;
12375
12376 settings->hash_lut_size = I40E_HASH_LUT_SIZE_128;
12377
12378 /* Flow Director is enabled */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080012379 if (pf->flags & (I40E_FLAG_FD_SB_ENABLED | I40E_FLAG_FD_ATR_ENABLED))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012380 settings->enable_fdir = true;
12381
12382 /* Ethtype and MACVLAN filters enabled for PF */
12383 settings->enable_ethtype = true;
12384 settings->enable_macvlan = true;
12385
12386 if (i40e_set_filter_control(&pf->hw, settings))
12387 return -ENOENT;
12388
12389 return 0;
12390}
12391
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000012392#define INFO_STRING_LEN 255
Shannon Nelson7fd89542015-10-21 19:47:04 -040012393#define REMAIN(__x) (INFO_STRING_LEN - (__x))
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000012394static void i40e_print_features(struct i40e_pf *pf)
12395{
12396 struct i40e_hw *hw = &pf->hw;
Joe Perches3b195842015-12-03 04:20:57 -080012397 char *buf;
12398 int i;
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000012399
Joe Perches3b195842015-12-03 04:20:57 -080012400 buf = kmalloc(INFO_STRING_LEN, GFP_KERNEL);
12401 if (!buf)
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000012402 return;
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000012403
Joe Perches3b195842015-12-03 04:20:57 -080012404 i = snprintf(buf, INFO_STRING_LEN, "Features: PF-id[%d]", hw->pf_id);
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000012405#ifdef CONFIG_PCI_IOV
Joe Perches3b195842015-12-03 04:20:57 -080012406 i += snprintf(&buf[i], REMAIN(i), " VFs: %d", pf->num_req_vfs);
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000012407#endif
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -070012408 i += snprintf(&buf[i], REMAIN(i), " VSIs: %d QP: %d",
Shannon Nelson7fd89542015-10-21 19:47:04 -040012409 pf->hw.func_caps.num_vsis,
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -070012410 pf->vsi[pf->lan_vsi]->num_queue_pairs);
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000012411 if (pf->flags & I40E_FLAG_RSS_ENABLED)
Joe Perches3b195842015-12-03 04:20:57 -080012412 i += snprintf(&buf[i], REMAIN(i), " RSS");
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000012413 if (pf->flags & I40E_FLAG_FD_ATR_ENABLED)
Joe Perches3b195842015-12-03 04:20:57 -080012414 i += snprintf(&buf[i], REMAIN(i), " FD_ATR");
Akeem G Abodunrinc6423ff2014-05-10 04:49:08 +000012415 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
Joe Perches3b195842015-12-03 04:20:57 -080012416 i += snprintf(&buf[i], REMAIN(i), " FD_SB");
12417 i += snprintf(&buf[i], REMAIN(i), " NTUPLE");
Akeem G Abodunrinc6423ff2014-05-10 04:49:08 +000012418 }
Neerav Parikh4d9b6042014-05-22 06:31:51 +000012419 if (pf->flags & I40E_FLAG_DCB_CAPABLE)
Joe Perches3b195842015-12-03 04:20:57 -080012420 i += snprintf(&buf[i], REMAIN(i), " DCB");
Joe Perches3b195842015-12-03 04:20:57 -080012421 i += snprintf(&buf[i], REMAIN(i), " VxLAN");
Singhai, Anjali6a899022015-12-14 12:21:18 -080012422 i += snprintf(&buf[i], REMAIN(i), " Geneve");
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000012423 if (pf->flags & I40E_FLAG_PTP)
Joe Perches3b195842015-12-03 04:20:57 -080012424 i += snprintf(&buf[i], REMAIN(i), " PTP");
Shannon Nelson6dec1012015-09-28 14:12:30 -040012425 if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
Joe Perches3b195842015-12-03 04:20:57 -080012426 i += snprintf(&buf[i], REMAIN(i), " VEB");
Shannon Nelson6dec1012015-09-28 14:12:30 -040012427 else
Joe Perches3b195842015-12-03 04:20:57 -080012428 i += snprintf(&buf[i], REMAIN(i), " VEPA");
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000012429
Joe Perches3b195842015-12-03 04:20:57 -080012430 dev_info(&pf->pdev->dev, "%s\n", buf);
12431 kfree(buf);
Shannon Nelson7fd89542015-10-21 19:47:04 -040012432 WARN_ON(i > INFO_STRING_LEN);
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000012433}
12434
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012435/**
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050012436 * i40e_get_platform_mac_addr - get platform-specific MAC address
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050012437 * @pdev: PCI device information struct
12438 * @pf: board private structure
12439 *
Jacob Keller41c4c2b2017-04-05 07:50:57 -040012440 * Look up the MAC address for the device. First we'll try
12441 * eth_platform_get_mac_address, which will check Open Firmware, or arch
12442 * specific fallback. Otherwise, we'll default to the stored value in
12443 * firmware.
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050012444 **/
12445static void i40e_get_platform_mac_addr(struct pci_dev *pdev, struct i40e_pf *pf)
12446{
Jacob Keller41c4c2b2017-04-05 07:50:57 -040012447 if (eth_platform_get_mac_address(&pdev->dev, pf->hw.mac.addr))
12448 i40e_get_mac_addr(&pf->hw, pf->hw.mac.addr);
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050012449}
12450
12451/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012452 * i40e_probe - Device initialization routine
12453 * @pdev: PCI device information struct
12454 * @ent: entry in i40e_pci_tbl
12455 *
Jeff Kirsherb40c82e62015-02-27 09:18:34 +000012456 * i40e_probe initializes a PF identified by a pci_dev structure.
12457 * The OS initialization, configuring of the PF private structure,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012458 * and a hardware reset occur.
12459 *
12460 * Returns 0 on success, negative on failure
12461 **/
12462static int i40e_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
12463{
Catherine Sullivane8278452015-02-06 08:52:08 +000012464 struct i40e_aq_get_phy_abilities_resp abilities;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012465 struct i40e_pf *pf;
12466 struct i40e_hw *hw;
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +000012467 static u16 pfs_found;
Shannon Nelson1d5109d2015-08-26 15:14:08 -040012468 u16 wol_nvm_bits;
Catherine Sullivand4dfb812013-11-28 06:39:21 +000012469 u16 link_status;
Jean Sacren6f66a482015-09-19 05:08:45 -060012470 int err;
Anjali Singhai Jain4f2f017c2015-10-21 19:47:07 -040012471 u32 val;
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +000012472 u32 i;
Helin Zhang58fc3262015-10-01 14:37:38 -040012473 u8 set_fc_aq_fail;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012474
12475 err = pci_enable_device_mem(pdev);
12476 if (err)
12477 return err;
12478
12479 /* set up for high or low dma */
Mitch Williams64942942014-02-11 08:26:33 +000012480 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
Mitch Williams64942942014-02-11 08:26:33 +000012481 if (err) {
Jean Sacrene3e3bfd2014-03-25 04:30:27 +000012482 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
12483 if (err) {
12484 dev_err(&pdev->dev,
12485 "DMA configuration failed: 0x%x\n", err);
12486 goto err_dma;
12487 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012488 }
12489
12490 /* set up pci connections */
Johannes Thumshirn56d766d2016-06-07 09:44:05 +020012491 err = pci_request_mem_regions(pdev, i40e_driver_name);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012492 if (err) {
12493 dev_info(&pdev->dev,
12494 "pci_request_selected_regions failed %d\n", err);
12495 goto err_pci_reg;
12496 }
12497
12498 pci_enable_pcie_error_reporting(pdev);
12499 pci_set_master(pdev);
12500
12501 /* Now that we have a PCI connection, we need to do the
12502 * low level device setup. This is primarily setting up
12503 * the Admin Queue structures and then querying for the
12504 * device's current profile information.
12505 */
12506 pf = kzalloc(sizeof(*pf), GFP_KERNEL);
12507 if (!pf) {
12508 err = -ENOMEM;
12509 goto err_pf_alloc;
12510 }
12511 pf->next_vsi = 0;
12512 pf->pdev = pdev;
Mauro S. M. Rodrigues9e6c9c02017-05-12 23:26:56 -030012513 set_bit(__I40E_DOWN, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012514
12515 hw = &pf->hw;
12516 hw->back = pf;
Anjali Singhai232f4702015-02-26 16:15:39 +000012517
Shannon Nelson2ac8b672015-07-23 16:54:37 -040012518 pf->ioremap_len = min_t(int, pci_resource_len(pdev, 0),
12519 I40E_MAX_CSR_SPACE);
Anjali Singhai232f4702015-02-26 16:15:39 +000012520
Shannon Nelson2ac8b672015-07-23 16:54:37 -040012521 hw->hw_addr = ioremap(pci_resource_start(pdev, 0), pf->ioremap_len);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012522 if (!hw->hw_addr) {
12523 err = -EIO;
12524 dev_info(&pdev->dev, "ioremap(0x%04x, 0x%04x) failed: 0x%x\n",
12525 (unsigned int)pci_resource_start(pdev, 0),
Shannon Nelson2ac8b672015-07-23 16:54:37 -040012526 pf->ioremap_len, err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012527 goto err_ioremap;
12528 }
12529 hw->vendor_id = pdev->vendor;
12530 hw->device_id = pdev->device;
12531 pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
12532 hw->subsystem_vendor_id = pdev->subsystem_vendor;
12533 hw->subsystem_device_id = pdev->subsystem_device;
12534 hw->bus.device = PCI_SLOT(pdev->devfn);
12535 hw->bus.func = PCI_FUNC(pdev->devfn);
Sudheer Mogilappagarib3f028f2017-02-09 23:58:22 -080012536 hw->bus.bus_id = pdev->bus->number;
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +000012537 pf->instance = pfs_found;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012538
Scott Petersonab243ec2017-08-22 06:57:54 -040012539 /* Select something other than the 802.1ad ethertype for the
12540 * switch to use internally and drop on ingress.
12541 */
12542 hw->switch_tag = 0xffff;
12543 hw->first_tag = ETH_P_8021AD;
12544 hw->second_tag = ETH_P_8021Q;
12545
Jacob Keller0e588de2017-02-06 14:38:50 -080012546 INIT_LIST_HEAD(&pf->l3_flex_pit_list);
12547 INIT_LIST_HEAD(&pf->l4_flex_pit_list);
12548
Shannon Nelsonde03d2b2016-03-10 14:59:44 -080012549 /* set up the locks for the AQ, do this only once in probe
12550 * and destroy them only once in remove
12551 */
12552 mutex_init(&hw->aq.asq_mutex);
12553 mutex_init(&hw->aq.arq_mutex);
12554
Alexander Duyck5d4ca232016-09-30 08:21:46 -040012555 pf->msg_enable = netif_msg_init(debug,
12556 NETIF_MSG_DRV |
12557 NETIF_MSG_PROBE |
12558 NETIF_MSG_LINK);
12559 if (debug < -1)
12560 pf->hw.debug_mask = debug;
Shannon Nelson5b5faa42014-10-17 03:14:51 +000012561
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +000012562 /* do a special CORER for clearing PXE mode once at init */
12563 if (hw->revision_id == 0 &&
12564 (rd32(hw, I40E_GLLAN_RCTL_0) & I40E_GLLAN_RCTL_0_PXE_MODE_MASK)) {
12565 wr32(hw, I40E_GLGEN_RTRIG, I40E_GLGEN_RTRIG_CORER_MASK);
12566 i40e_flush(hw);
12567 msleep(200);
12568 pf->corer_count++;
12569
12570 i40e_clear_pxe_mode(hw);
12571 }
12572
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012573 /* Reset here to make sure all is clean and to define PF 'n' */
Shannon Nelson838d41d2014-06-04 20:41:27 +000012574 i40e_clear_hw(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012575 err = i40e_pf_reset(hw);
12576 if (err) {
12577 dev_info(&pdev->dev, "Initial pf_reset failed: %d\n", err);
12578 goto err_pf_reset;
12579 }
12580 pf->pfr_count++;
12581
12582 hw->aq.num_arq_entries = I40E_AQ_LEN;
12583 hw->aq.num_asq_entries = I40E_AQ_LEN;
12584 hw->aq.arq_buf_size = I40E_MAX_AQ_BUF_SIZE;
12585 hw->aq.asq_buf_size = I40E_MAX_AQ_BUF_SIZE;
12586 pf->adminq_work_limit = I40E_AQ_WORK_LIMIT;
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +000012587
Carolyn Wybornyb294ac72014-12-11 07:06:39 +000012588 snprintf(pf->int_name, sizeof(pf->int_name) - 1,
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +000012589 "%s-%s:misc",
12590 dev_driver_string(&pf->pdev->dev), dev_name(&pdev->dev));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012591
12592 err = i40e_init_shared_code(hw);
12593 if (err) {
Anjali Singhai Jainb2a75c52015-04-27 14:57:20 -040012594 dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
12595 err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012596 goto err_pf_reset;
12597 }
12598
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +000012599 /* set up a default setting for link flow control */
12600 pf->hw.fc.requested_mode = I40E_FC_NONE;
12601
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012602 err = i40e_init_adminq(hw);
Carolyn Wyborny2b2426a762015-10-26 19:44:35 -040012603 if (err) {
12604 if (err == I40E_ERR_FIRMWARE_API_VERSION)
12605 dev_info(&pdev->dev,
12606 "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");
12607 else
12608 dev_info(&pdev->dev,
12609 "The driver for the device stopped because the device firmware failed to init. Try updating your NVM image.\n");
12610
12611 goto err_pf_reset;
12612 }
Filip Sadowski5bbb2e22017-06-07 05:43:09 -040012613 i40e_get_oem_version(hw);
Carolyn Wybornyf0b44442015-08-31 19:54:49 -040012614
Shannon Nelson6dec1012015-09-28 14:12:30 -040012615 /* provide nvm, fw, api versions */
12616 dev_info(&pdev->dev, "fw %d.%d.%05d api %d.%d nvm %s\n",
12617 hw->aq.fw_maj_ver, hw->aq.fw_min_ver, hw->aq.fw_build,
12618 hw->aq.api_maj_ver, hw->aq.api_min_ver,
12619 i40e_nvm_version_str(hw));
Carolyn Wybornyf0b44442015-08-31 19:54:49 -040012620
Catherine Sullivan7aa67612014-07-09 07:46:17 +000012621 if (hw->aq.api_maj_ver == I40E_FW_API_VERSION_MAJOR &&
Mitch Williams22b965512017-07-14 09:27:09 -040012622 hw->aq.api_min_ver > I40E_FW_MINOR_VERSION(hw))
Shannon Nelson278b6f62014-06-04 01:41:03 +000012623 dev_info(&pdev->dev,
Catherine Sullivan7aa67612014-07-09 07:46:17 +000012624 "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 -040012625 else if (hw->aq.api_maj_ver == 1 && hw->aq.api_min_ver < 4)
Shannon Nelson278b6f62014-06-04 01:41:03 +000012626 dev_info(&pdev->dev,
Catherine Sullivan7aa67612014-07-09 07:46:17 +000012627 "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 +000012628
Shannon Nelson4eb3f762014-03-06 08:59:58 +000012629 i40e_verify_eeprom(pf);
12630
Jesse Brandeburg2c5fe332014-04-23 04:49:57 +000012631 /* Rev 0 hardware was never productized */
12632 if (hw->revision_id < 1)
12633 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");
12634
Shannon Nelson6ff4ef82013-12-21 05:44:49 +000012635 i40e_clear_pxe_mode(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012636 err = i40e_get_capabilities(pf);
12637 if (err)
12638 goto err_adminq_setup;
12639
12640 err = i40e_sw_init(pf);
12641 if (err) {
12642 dev_info(&pdev->dev, "sw_init failed: %d\n", err);
12643 goto err_sw_init;
12644 }
12645
12646 err = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
Alexander Duyckc76cb6e2017-02-21 15:55:48 -080012647 hw->func_caps.num_rx_qp, 0, 0);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012648 if (err) {
12649 dev_info(&pdev->dev, "init_lan_hmc failed: %d\n", err);
12650 goto err_init_lan_hmc;
12651 }
12652
12653 err = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
12654 if (err) {
12655 dev_info(&pdev->dev, "configure_lan_hmc failed: %d\n", err);
12656 err = -ENOENT;
12657 goto err_configure_lan_hmc;
12658 }
12659
Neerav Parikhb686ece2014-12-14 01:55:11 +000012660 /* Disable LLDP for NICs that have firmware versions lower than v4.3.
12661 * Ignore error return codes because if it was already disabled via
12662 * hardware settings this will fail
12663 */
Jacob Kellerd36e41d2017-06-23 04:24:46 -040012664 if (pf->hw_features & I40E_HW_STOP_FW_LLDP) {
Neerav Parikhb686ece2014-12-14 01:55:11 +000012665 dev_info(&pdev->dev, "Stopping firmware LLDP agent.\n");
12666 i40e_aq_stop_lldp(hw, true, NULL);
12667 }
12668
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050012669 /* allow a platform config to override the HW addr */
12670 i40e_get_platform_mac_addr(pdev, pf);
Jacob Keller41c4c2b2017-04-05 07:50:57 -040012671
Jesse Brandeburgf62b5062013-11-28 06:39:27 +000012672 if (!is_valid_ether_addr(hw->mac.addr)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012673 dev_info(&pdev->dev, "invalid MAC address %pM\n", hw->mac.addr);
12674 err = -EIO;
12675 goto err_mac_addr;
12676 }
12677 dev_info(&pdev->dev, "MAC address: %pM\n", hw->mac.addr);
Greg Rose9a173902014-05-22 06:32:02 +000012678 ether_addr_copy(hw->mac.perm_addr, hw->mac.addr);
Neerav Parikh1f224ad2014-02-12 01:45:31 +000012679 i40e_get_port_mac_addr(hw, hw->mac.port_addr);
12680 if (is_valid_ether_addr(hw->mac.port_addr))
Jacob Kellerd36e41d2017-06-23 04:24:46 -040012681 pf->hw_features |= I40E_HW_PORT_ID_VALID;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012682
12683 pci_set_drvdata(pdev, pf);
12684 pci_save_state(pdev);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -080012685#ifdef CONFIG_I40E_DCB
12686 err = i40e_init_pf_dcb(pf);
12687 if (err) {
Shannon Nelsonaebfc812014-12-11 07:06:38 +000012688 dev_info(&pdev->dev, "DCB init failed %d, disabled\n", err);
David Ertmanc17ef432016-09-30 01:36:21 -070012689 pf->flags &= ~(I40E_FLAG_DCB_CAPABLE | I40E_FLAG_DCB_ENABLED);
Neerav Parikh014269f2014-04-01 07:11:48 +000012690 /* Continue without DCB enabled */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -080012691 }
12692#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012693
12694 /* set up periodic task facility */
Kees Cook26566ea2017-10-16 17:29:35 -070012695 timer_setup(&pf->service_timer, i40e_service_timer, 0);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012696 pf->service_timer_period = HZ;
12697
12698 INIT_WORK(&pf->service_task, i40e_service_task);
Jacob Keller0da36b92017-04-19 09:25:55 -040012699 clear_bit(__I40E_SERVICE_SCHED, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012700
Shannon Nelson1d5109d2015-08-26 15:14:08 -040012701 /* NVM bit on means WoL disabled for the port */
12702 i40e_read_nvm_word(hw, I40E_SR_NVM_WAKE_ON_LAN, &wol_nvm_bits);
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -080012703 if (BIT (hw->port) & wol_nvm_bits || hw->partition_id != 1)
Shannon Nelson1d5109d2015-08-26 15:14:08 -040012704 pf->wol_en = false;
12705 else
12706 pf->wol_en = true;
Shannon Nelson8e2773a2013-11-28 06:39:22 +000012707 device_set_wakeup_enable(&pf->pdev->dev, pf->wol_en);
12708
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012709 /* set up the main switch operations */
12710 i40e_determine_queue_usage(pf);
Jesse Brandeburgc11472802015-04-07 19:45:39 -040012711 err = i40e_init_interrupt_scheme(pf);
12712 if (err)
12713 goto err_switch_setup;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012714
Mitch Williams505682c2014-05-20 08:01:37 +000012715 /* The number of VSIs reported by the FW is the minimum guaranteed
12716 * to us; HW supports far more and we share the remaining pool with
12717 * the other PFs. We allocate space for more than the guarantee with
12718 * the understanding that we might not get them all later.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012719 */
Mitch Williams505682c2014-05-20 08:01:37 +000012720 if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
12721 pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
12722 else
12723 pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
12724
12725 /* Set up the *vsi struct and our local tracking of the MAIN PF vsi. */
Jesse Brandeburgd17038d2015-12-23 12:05:55 -080012726 pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
12727 GFP_KERNEL);
Wei Yongjuned87ac02013-09-24 05:17:25 +000012728 if (!pf->vsi) {
12729 err = -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012730 goto err_switch_setup;
Wei Yongjuned87ac02013-09-24 05:17:25 +000012731 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012732
Anjali Singhai Jainfa11cb32015-05-27 12:06:14 -040012733#ifdef CONFIG_PCI_IOV
12734 /* prep for VF support */
12735 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
12736 (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
Jacob Keller0da36b92017-04-19 09:25:55 -040012737 !test_bit(__I40E_BAD_EEPROM, pf->state)) {
Anjali Singhai Jainfa11cb32015-05-27 12:06:14 -040012738 if (pci_num_vf(pdev))
12739 pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
12740 }
12741#endif
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000012742 err = i40e_setup_pf_switch(pf, false);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012743 if (err) {
12744 dev_info(&pdev->dev, "setup_pf_switch failed: %d\n", err);
12745 goto err_vsis;
12746 }
Amritha Nambiar8f88b302017-09-07 04:00:17 -070012747 INIT_LIST_HEAD(&pf->vsi[pf->lan_vsi]->ch_list);
Helin Zhang58fc3262015-10-01 14:37:38 -040012748
12749 /* Make sure flow control is set according to current settings */
12750 err = i40e_set_fc(hw, &set_fc_aq_fail, true);
12751 if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_GET)
12752 dev_dbg(&pf->pdev->dev,
12753 "Set fc with err %s aq_err %s on get_phy_cap\n",
12754 i40e_stat_str(hw, err),
12755 i40e_aq_str(hw, hw->aq.asq_last_status));
12756 if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_SET)
12757 dev_dbg(&pf->pdev->dev,
12758 "Set fc with err %s aq_err %s on set_phy_config\n",
12759 i40e_stat_str(hw, err),
12760 i40e_aq_str(hw, hw->aq.asq_last_status));
12761 if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_UPDATE)
12762 dev_dbg(&pf->pdev->dev,
12763 "Set fc with err %s aq_err %s on get_link_info\n",
12764 i40e_stat_str(hw, err),
12765 i40e_aq_str(hw, hw->aq.asq_last_status));
12766
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +000012767 /* if FDIR VSI was set up, start it now */
Mitch Williams505682c2014-05-20 08:01:37 +000012768 for (i = 0; i < pf->num_alloc_vsi; i++) {
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +000012769 if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
12770 i40e_vsi_open(pf->vsi[i]);
12771 break;
12772 }
12773 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012774
Shannon Nelson2f0aff42016-01-04 10:33:08 -080012775 /* The driver only wants link up/down and module qualification
12776 * reports from firmware. Note the negative logic.
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +000012777 */
12778 err = i40e_aq_set_phy_int_mask(&pf->hw,
Shannon Nelson2f0aff42016-01-04 10:33:08 -080012779 ~(I40E_AQ_EVENT_LINK_UPDOWN |
Shannon Nelson867a79e2016-03-18 12:18:15 -070012780 I40E_AQ_EVENT_MEDIA_NA |
Shannon Nelson2f0aff42016-01-04 10:33:08 -080012781 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +000012782 if (err)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040012783 dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
12784 i40e_stat_str(&pf->hw, err),
12785 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +000012786
Anjali Singhai Jain4f2f017c2015-10-21 19:47:07 -040012787 /* Reconfigure hardware for allowing smaller MSS in the case
12788 * of TSO, so that we avoid the MDD being fired and causing
12789 * a reset in the case of small MSS+TSO.
12790 */
12791 val = rd32(hw, I40E_REG_MSS);
12792 if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
12793 val &= ~I40E_REG_MSS_MIN_MASK;
12794 val |= I40E_64BYTE_MSS;
12795 wr32(hw, I40E_REG_MSS, val);
12796 }
12797
Jacob Kellerd36e41d2017-06-23 04:24:46 -040012798 if (pf->hw_features & I40E_HW_RESTART_AUTONEG) {
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +000012799 msleep(75);
12800 err = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
12801 if (err)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040012802 dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
12803 i40e_stat_str(&pf->hw, err),
12804 i40e_aq_str(&pf->hw,
12805 pf->hw.aq.asq_last_status));
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +000012806 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012807 /* The main driver is (mostly) up and happy. We need to set this state
12808 * before setting up the misc vector or we get a race and the vector
12809 * ends up disabled forever.
12810 */
Mauro S. M. Rodrigues9e6c9c02017-05-12 23:26:56 -030012811 clear_bit(__I40E_DOWN, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012812
12813 /* In case of MSIX we are going to setup the misc vector right here
12814 * to handle admin queue events etc. In case of legacy and MSI
12815 * the misc functionality and queue processing is combined in
12816 * the same vector and that gets setup at open.
12817 */
12818 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
12819 err = i40e_setup_misc_vector(pf);
12820 if (err) {
12821 dev_info(&pdev->dev,
12822 "setup of misc vector failed: %d\n", err);
12823 goto err_vsis;
12824 }
12825 }
12826
Greg Rosedf805f62014-04-04 04:43:16 +000012827#ifdef CONFIG_PCI_IOV
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012828 /* prep for VF support */
12829 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
Shannon Nelson4eb3f762014-03-06 08:59:58 +000012830 (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
Jacob Keller0da36b92017-04-19 09:25:55 -040012831 !test_bit(__I40E_BAD_EEPROM, pf->state)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012832 /* disable link interrupts for VFs */
12833 val = rd32(hw, I40E_PFGEN_PORTMDIO_NUM);
12834 val &= ~I40E_PFGEN_PORTMDIO_NUM_VFLINK_STAT_ENA_MASK;
12835 wr32(hw, I40E_PFGEN_PORTMDIO_NUM, val);
12836 i40e_flush(hw);
Mitch Williams4aeec012014-02-13 03:48:47 -080012837
12838 if (pci_num_vf(pdev)) {
12839 dev_info(&pdev->dev,
12840 "Active VFs found, allocating resources.\n");
12841 err = i40e_alloc_vfs(pf, pci_num_vf(pdev));
12842 if (err)
12843 dev_info(&pdev->dev,
12844 "Error %d allocating resources for existing VFs\n",
12845 err);
12846 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012847 }
Greg Rosedf805f62014-04-04 04:43:16 +000012848#endif /* CONFIG_PCI_IOV */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012849
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060012850 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
12851 pf->iwarp_base_vector = i40e_get_lump(pf, pf->irq_pile,
12852 pf->num_iwarp_msix,
12853 I40E_IWARP_IRQ_PILE_ID);
12854 if (pf->iwarp_base_vector < 0) {
12855 dev_info(&pdev->dev,
12856 "failed to get tracking for %d vectors for IWARP err=%d\n",
12857 pf->num_iwarp_msix, pf->iwarp_base_vector);
12858 pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
12859 }
12860 }
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +000012861
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012862 i40e_dbg_pf_init(pf);
12863
12864 /* tell the firmware that we're starting */
Jesse Brandeburg44033fa2014-04-23 04:50:15 +000012865 i40e_send_version(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012866
12867 /* since everything's happy, start the service_task timer */
12868 mod_timer(&pf->service_timer,
12869 round_jiffies(jiffies + pf->service_timer_period));
12870
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060012871 /* add this PF to client device list and launch a client service task */
Mitch Williams004eb612017-04-04 12:40:16 -070012872 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
12873 err = i40e_lan_add_device(pf);
12874 if (err)
12875 dev_info(&pdev->dev, "Failed to add PF to client API service list: %d\n",
12876 err);
12877 }
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060012878
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040012879#define PCI_SPEED_SIZE 8
12880#define PCI_WIDTH_SIZE 8
12881 /* Devices on the IOSF bus do not have this information
12882 * and will report PCI Gen 1 x 1 by default so don't bother
12883 * checking them.
12884 */
Jacob Kellerd36e41d2017-06-23 04:24:46 -040012885 if (!(pf->hw_features & I40E_HW_NO_PCI_LINK_CHECK)) {
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040012886 char speed[PCI_SPEED_SIZE] = "Unknown";
12887 char width[PCI_WIDTH_SIZE] = "Unknown";
Catherine Sullivand4dfb812013-11-28 06:39:21 +000012888
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040012889 /* Get the negotiated link width and speed from PCI config
12890 * space
12891 */
12892 pcie_capability_read_word(pf->pdev, PCI_EXP_LNKSTA,
12893 &link_status);
Catherine Sullivand4dfb812013-11-28 06:39:21 +000012894
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040012895 i40e_set_pci_config_data(hw, link_status);
Catherine Sullivand4dfb812013-11-28 06:39:21 +000012896
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040012897 switch (hw->bus.speed) {
12898 case i40e_bus_speed_8000:
12899 strncpy(speed, "8.0", PCI_SPEED_SIZE); break;
12900 case i40e_bus_speed_5000:
12901 strncpy(speed, "5.0", PCI_SPEED_SIZE); break;
12902 case i40e_bus_speed_2500:
12903 strncpy(speed, "2.5", PCI_SPEED_SIZE); break;
12904 default:
12905 break;
12906 }
12907 switch (hw->bus.width) {
12908 case i40e_bus_width_pcie_x8:
12909 strncpy(width, "8", PCI_WIDTH_SIZE); break;
12910 case i40e_bus_width_pcie_x4:
12911 strncpy(width, "4", PCI_WIDTH_SIZE); break;
12912 case i40e_bus_width_pcie_x2:
12913 strncpy(width, "2", PCI_WIDTH_SIZE); break;
12914 case i40e_bus_width_pcie_x1:
12915 strncpy(width, "1", PCI_WIDTH_SIZE); break;
12916 default:
12917 break;
12918 }
12919
12920 dev_info(&pdev->dev, "PCI-Express: Speed %sGT/s Width x%s\n",
12921 speed, width);
12922
12923 if (hw->bus.width < i40e_bus_width_pcie_x8 ||
12924 hw->bus.speed < i40e_bus_speed_8000) {
12925 dev_warn(&pdev->dev, "PCI-Express bandwidth available for this device may be insufficient for optimal performance.\n");
12926 dev_warn(&pdev->dev, "Please move the device to a different PCI-e link with more lanes and/or higher transfer rate.\n");
12927 }
Catherine Sullivand4dfb812013-11-28 06:39:21 +000012928 }
12929
Catherine Sullivane8278452015-02-06 08:52:08 +000012930 /* get the requested speeds from the fw */
12931 err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities, NULL);
12932 if (err)
Neerav Parikh8279e492015-09-03 17:18:50 -040012933 dev_dbg(&pf->pdev->dev, "get requested speeds ret = %s last_status = %s\n",
12934 i40e_stat_str(&pf->hw, err),
12935 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Catherine Sullivane8278452015-02-06 08:52:08 +000012936 pf->hw.phy.link_info.requested_speeds = abilities.link_speed;
12937
Catherine Sullivanfc72dbc2015-09-01 11:36:30 -040012938 /* get the supported phy types from the fw */
12939 err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities, NULL);
12940 if (err)
12941 dev_dbg(&pf->pdev->dev, "get supported phy types ret = %s last_status = %s\n",
12942 i40e_stat_str(&pf->hw, err),
12943 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Catherine Sullivanfc72dbc2015-09-01 11:36:30 -040012944
Anjali Singhai Jaine7358f52015-10-01 14:37:34 -040012945 /* Add a filter to drop all Flow control frames from any VSI from being
12946 * transmitted. By doing so we stop a malicious VF from sending out
12947 * PAUSE or PFC frames and potentially controlling traffic for other
12948 * PF/VF VSIs.
12949 * The FW can still send Flow control frames if enabled.
12950 */
12951 i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
12952 pf->main_vsi_seid);
12953
Carolyn Wyborny31b606d2016-02-17 16:12:12 -080012954 if ((pf->hw.device_id == I40E_DEV_ID_10G_BASE_T) ||
Henry Tieman4f9b4302016-11-08 13:05:18 -080012955 (pf->hw.device_id == I40E_DEV_ID_10G_BASE_T4))
Jacob Kellerd36e41d2017-06-23 04:24:46 -040012956 pf->hw_features |= I40E_HW_PHY_CONTROLS_LEDS;
Harshitha Ramamurthy4ad9f4f2016-11-08 13:05:09 -080012957 if (pf->hw.device_id == I40E_DEV_ID_SFP_I_X722)
Jacob Kellerd36e41d2017-06-23 04:24:46 -040012958 pf->hw_features |= I40E_HW_HAVE_CRT_RETIMER;
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000012959 /* print a string summarizing features */
12960 i40e_print_features(pf);
12961
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012962 return 0;
12963
12964 /* Unwind what we've done if something failed in the setup */
12965err_vsis:
Mauro S. M. Rodrigues9e6c9c02017-05-12 23:26:56 -030012966 set_bit(__I40E_DOWN, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012967 i40e_clear_interrupt_scheme(pf);
12968 kfree(pf->vsi);
Shannon Nelson04b03012013-11-28 06:39:34 +000012969err_switch_setup:
12970 i40e_reset_interrupt_capability(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012971 del_timer_sync(&pf->service_timer);
12972err_mac_addr:
12973err_configure_lan_hmc:
12974 (void)i40e_shutdown_lan_hmc(hw);
12975err_init_lan_hmc:
12976 kfree(pf->qp_pile);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012977err_sw_init:
12978err_adminq_setup:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012979err_pf_reset:
12980 iounmap(hw->hw_addr);
12981err_ioremap:
12982 kfree(pf);
12983err_pf_alloc:
12984 pci_disable_pcie_error_reporting(pdev);
Johannes Thumshirn56d766d2016-06-07 09:44:05 +020012985 pci_release_mem_regions(pdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012986err_pci_reg:
12987err_dma:
12988 pci_disable_device(pdev);
12989 return err;
12990}
12991
12992/**
12993 * i40e_remove - Device removal routine
12994 * @pdev: PCI device information struct
12995 *
12996 * i40e_remove is called by the PCI subsystem to alert the driver
12997 * that is should release a PCI device. This could be caused by a
12998 * Hot-Plug event, or because the driver is going to be removed from
12999 * memory.
13000 **/
13001static void i40e_remove(struct pci_dev *pdev)
13002{
13003 struct i40e_pf *pf = pci_get_drvdata(pdev);
Carolyn Wybornybcab2db2015-09-28 14:16:55 -040013004 struct i40e_hw *hw = &pf->hw;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013005 i40e_status ret_code;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013006 int i;
13007
13008 i40e_dbg_pf_exit(pf);
13009
Jacob Kellerbeb0dff2014-01-11 05:43:19 +000013010 i40e_ptp_stop(pf);
13011
Carolyn Wybornybcab2db2015-09-28 14:16:55 -040013012 /* Disable RSS in hw */
Shannon Nelson272cdaf22016-02-17 16:12:21 -080013013 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), 0);
13014 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), 0);
Carolyn Wybornybcab2db2015-09-28 14:16:55 -040013015
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013016 /* no more scheduling of any task */
Jacob Keller0da36b92017-04-19 09:25:55 -040013017 set_bit(__I40E_SUSPENDED, pf->state);
Mauro S. M. Rodrigues9e6c9c02017-05-12 23:26:56 -030013018 set_bit(__I40E_DOWN, pf->state);
Kees Cook26566ea2017-10-16 17:29:35 -070013019 if (pf->service_timer.function)
Shannon Nelsonc99abb42016-03-10 14:59:45 -080013020 del_timer_sync(&pf->service_timer);
13021 if (pf->service_task.func)
13022 cancel_work_sync(&pf->service_task);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013023
Mitch Williams921c4672017-03-30 00:46:08 -070013024 /* Client close must be called explicitly here because the timer
13025 * has been stopped.
13026 */
13027 i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
13028
Mitch Williamseb2d80b2014-02-13 03:48:48 -080013029 if (pf->flags & I40E_FLAG_SRIOV_ENABLED) {
13030 i40e_free_vfs(pf);
13031 pf->flags &= ~I40E_FLAG_SRIOV_ENABLED;
13032 }
13033
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013034 i40e_fdir_teardown(pf);
13035
13036 /* If there is a switch structure or any orphans, remove them.
13037 * This will leave only the PF's VSI remaining.
13038 */
13039 for (i = 0; i < I40E_MAX_VEB; i++) {
13040 if (!pf->veb[i])
13041 continue;
13042
13043 if (pf->veb[i]->uplink_seid == pf->mac_seid ||
13044 pf->veb[i]->uplink_seid == 0)
13045 i40e_switch_branch_release(pf->veb[i]);
13046 }
13047
13048 /* Now we can shutdown the PF's VSI, just before we kill
13049 * adminq and hmc.
13050 */
13051 if (pf->vsi[pf->lan_vsi])
13052 i40e_vsi_release(pf->vsi[pf->lan_vsi]);
13053
Amritha Nambiaraaf66502017-10-27 02:35:56 -070013054 i40e_cloud_filter_exit(pf);
13055
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060013056 /* remove attached clients */
Mitch Williams004eb612017-04-04 12:40:16 -070013057 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
13058 ret_code = i40e_lan_del_device(pf);
13059 if (ret_code)
13060 dev_warn(&pdev->dev, "Failed to delete client device: %d\n",
13061 ret_code);
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060013062 }
13063
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013064 /* shutdown and destroy the HMC */
Jesse Brandeburgf734dff2016-01-15 14:33:11 -080013065 if (hw->hmc.hmc_obj) {
13066 ret_code = i40e_shutdown_lan_hmc(hw);
Shannon Nelson60442de2014-04-23 04:50:13 +000013067 if (ret_code)
13068 dev_warn(&pdev->dev,
13069 "Failed to destroy the HMC resources: %d\n",
13070 ret_code);
13071 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013072
13073 /* shutdown the adminq */
Henry Tiemanac9c5c62016-09-06 18:05:11 -070013074 i40e_shutdown_adminq(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013075
Jesse Brandeburg8ddb3322015-11-18 15:47:06 -080013076 /* destroy the locks only once, here */
13077 mutex_destroy(&hw->aq.arq_mutex);
13078 mutex_destroy(&hw->aq.asq_mutex);
13079
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013080 /* Clear all dynamic memory lists of rings, q_vectors, and VSIs */
13081 i40e_clear_interrupt_scheme(pf);
Mitch Williams505682c2014-05-20 08:01:37 +000013082 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013083 if (pf->vsi[i]) {
13084 i40e_vsi_clear_rings(pf->vsi[i]);
13085 i40e_vsi_clear(pf->vsi[i]);
13086 pf->vsi[i] = NULL;
13087 }
13088 }
13089
13090 for (i = 0; i < I40E_MAX_VEB; i++) {
13091 kfree(pf->veb[i]);
13092 pf->veb[i] = NULL;
13093 }
13094
13095 kfree(pf->qp_pile);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013096 kfree(pf->vsi);
13097
Jesse Brandeburgf734dff2016-01-15 14:33:11 -080013098 iounmap(hw->hw_addr);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013099 kfree(pf);
Johannes Thumshirn56d766d2016-06-07 09:44:05 +020013100 pci_release_mem_regions(pdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013101
13102 pci_disable_pcie_error_reporting(pdev);
13103 pci_disable_device(pdev);
13104}
13105
13106/**
13107 * i40e_pci_error_detected - warning that something funky happened in PCI land
13108 * @pdev: PCI device information struct
13109 *
13110 * Called to warn that something happened and the error handling steps
13111 * are in progress. Allows the driver to quiesce things, be ready for
13112 * remediation.
13113 **/
13114static pci_ers_result_t i40e_pci_error_detected(struct pci_dev *pdev,
13115 enum pci_channel_state error)
13116{
13117 struct i40e_pf *pf = pci_get_drvdata(pdev);
13118
13119 dev_info(&pdev->dev, "%s: error %d\n", __func__, error);
13120
Guilherme G Piccoliedfc23ee2016-10-03 00:31:12 -070013121 if (!pf) {
13122 dev_info(&pdev->dev,
13123 "Cannot recover - error happened during device probe\n");
13124 return PCI_ERS_RESULT_DISCONNECT;
13125 }
13126
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013127 /* shutdown all operations */
Jacob Kellerdfc4ff62017-06-07 05:43:13 -040013128 if (!test_bit(__I40E_SUSPENDED, pf->state))
13129 i40e_prep_for_reset(pf, false);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013130
13131 /* Request a slot reset */
13132 return PCI_ERS_RESULT_NEED_RESET;
13133}
13134
13135/**
13136 * i40e_pci_error_slot_reset - a PCI slot reset just happened
13137 * @pdev: PCI device information struct
13138 *
13139 * Called to find if the driver can work with the device now that
13140 * the pci slot has been reset. If a basic connection seems good
13141 * (registers are readable and have sane content) then return a
13142 * happy little PCI_ERS_RESULT_xxx.
13143 **/
13144static pci_ers_result_t i40e_pci_error_slot_reset(struct pci_dev *pdev)
13145{
13146 struct i40e_pf *pf = pci_get_drvdata(pdev);
13147 pci_ers_result_t result;
13148 int err;
13149 u32 reg;
13150
Shannon Nelsonfb43201f2015-08-26 15:14:17 -040013151 dev_dbg(&pdev->dev, "%s\n", __func__);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013152 if (pci_enable_device_mem(pdev)) {
13153 dev_info(&pdev->dev,
13154 "Cannot re-enable PCI device after reset.\n");
13155 result = PCI_ERS_RESULT_DISCONNECT;
13156 } else {
13157 pci_set_master(pdev);
13158 pci_restore_state(pdev);
13159 pci_save_state(pdev);
13160 pci_wake_from_d3(pdev, false);
13161
13162 reg = rd32(&pf->hw, I40E_GLGEN_RTRIG);
13163 if (reg == 0)
13164 result = PCI_ERS_RESULT_RECOVERED;
13165 else
13166 result = PCI_ERS_RESULT_DISCONNECT;
13167 }
13168
13169 err = pci_cleanup_aer_uncorrect_error_status(pdev);
13170 if (err) {
13171 dev_info(&pdev->dev,
13172 "pci_cleanup_aer_uncorrect_error_status failed 0x%0x\n",
13173 err);
13174 /* non-fatal, continue */
13175 }
13176
13177 return result;
13178}
13179
13180/**
Alan Brady19b79602017-08-29 05:32:39 -040013181 * i40e_pci_error_reset_prepare - prepare device driver for pci reset
13182 * @pdev: PCI device information struct
13183 */
13184static void i40e_pci_error_reset_prepare(struct pci_dev *pdev)
13185{
13186 struct i40e_pf *pf = pci_get_drvdata(pdev);
13187
13188 i40e_prep_for_reset(pf, false);
13189}
13190
13191/**
13192 * i40e_pci_error_reset_done - pci reset done, device driver reset can begin
13193 * @pdev: PCI device information struct
13194 */
13195static void i40e_pci_error_reset_done(struct pci_dev *pdev)
13196{
13197 struct i40e_pf *pf = pci_get_drvdata(pdev);
13198
13199 i40e_reset_and_rebuild(pf, false, false);
13200}
13201
13202/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013203 * i40e_pci_error_resume - restart operations after PCI error recovery
13204 * @pdev: PCI device information struct
13205 *
13206 * Called to allow the driver to bring things back up after PCI error
13207 * and/or reset recovery has finished.
13208 **/
13209static void i40e_pci_error_resume(struct pci_dev *pdev)
13210{
13211 struct i40e_pf *pf = pci_get_drvdata(pdev);
13212
Shannon Nelsonfb43201f2015-08-26 15:14:17 -040013213 dev_dbg(&pdev->dev, "%s\n", __func__);
Jacob Keller0da36b92017-04-19 09:25:55 -040013214 if (test_bit(__I40E_SUSPENDED, pf->state))
Shannon Nelson9007bcc2013-11-26 10:49:23 +000013215 return;
13216
Jacob Kellerdfc4ff62017-06-07 05:43:13 -040013217 i40e_handle_reset_warning(pf, false);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013218}
13219
Shannon Nelson9007bcc2013-11-26 10:49:23 +000013220/**
Joshua Hay1d680052016-12-12 15:44:08 -080013221 * i40e_enable_mc_magic_wake - enable multicast magic packet wake up
13222 * using the mac_address_write admin q function
13223 * @pf: pointer to i40e_pf struct
13224 **/
13225static void i40e_enable_mc_magic_wake(struct i40e_pf *pf)
13226{
13227 struct i40e_hw *hw = &pf->hw;
13228 i40e_status ret;
13229 u8 mac_addr[6];
13230 u16 flags = 0;
13231
13232 /* Get current MAC address in case it's an LAA */
13233 if (pf->vsi[pf->lan_vsi] && pf->vsi[pf->lan_vsi]->netdev) {
13234 ether_addr_copy(mac_addr,
13235 pf->vsi[pf->lan_vsi]->netdev->dev_addr);
13236 } else {
13237 dev_err(&pf->pdev->dev,
13238 "Failed to retrieve MAC address; using default\n");
13239 ether_addr_copy(mac_addr, hw->mac.addr);
13240 }
13241
13242 /* The FW expects the mac address write cmd to first be called with
13243 * one of these flags before calling it again with the multicast
13244 * enable flags.
13245 */
13246 flags = I40E_AQC_WRITE_TYPE_LAA_WOL;
13247
13248 if (hw->func_caps.flex10_enable && hw->partition_id != 1)
13249 flags = I40E_AQC_WRITE_TYPE_LAA_ONLY;
13250
13251 ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
13252 if (ret) {
13253 dev_err(&pf->pdev->dev,
13254 "Failed to update MAC address registers; cannot enable Multicast Magic packet wake up");
13255 return;
13256 }
13257
13258 flags = I40E_AQC_MC_MAG_EN
13259 | I40E_AQC_WOL_PRESERVE_ON_PFR
13260 | I40E_AQC_WRITE_TYPE_UPDATE_MC_MAG;
13261 ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
13262 if (ret)
13263 dev_err(&pf->pdev->dev,
13264 "Failed to enable Multicast Magic Packet wake up\n");
13265}
13266
13267/**
Shannon Nelson9007bcc2013-11-26 10:49:23 +000013268 * i40e_shutdown - PCI callback for shutting down
13269 * @pdev: PCI device information struct
13270 **/
13271static void i40e_shutdown(struct pci_dev *pdev)
13272{
13273 struct i40e_pf *pf = pci_get_drvdata(pdev);
Shannon Nelson8e2773a2013-11-28 06:39:22 +000013274 struct i40e_hw *hw = &pf->hw;
Shannon Nelson9007bcc2013-11-26 10:49:23 +000013275
Jacob Keller0da36b92017-04-19 09:25:55 -040013276 set_bit(__I40E_SUSPENDED, pf->state);
Mauro S. M. Rodrigues9e6c9c02017-05-12 23:26:56 -030013277 set_bit(__I40E_DOWN, pf->state);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000013278 rtnl_lock();
Maciej Sosin373149f2017-04-05 07:50:55 -040013279 i40e_prep_for_reset(pf, true);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000013280 rtnl_unlock();
13281
Shannon Nelson8e2773a2013-11-28 06:39:22 +000013282 wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
13283 wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
13284
Catherine Sullivan02b42492015-07-10 19:35:59 -040013285 del_timer_sync(&pf->service_timer);
13286 cancel_work_sync(&pf->service_task);
Amritha Nambiaraaf66502017-10-27 02:35:56 -070013287 i40e_cloud_filter_exit(pf);
Catherine Sullivan02b42492015-07-10 19:35:59 -040013288 i40e_fdir_teardown(pf);
13289
Mitch Williams921c4672017-03-30 00:46:08 -070013290 /* Client close must be called explicitly here because the timer
13291 * has been stopped.
13292 */
13293 i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
13294
Jacob Kellerd36e41d2017-06-23 04:24:46 -040013295 if (pf->wol_en && (pf->hw_features & I40E_HW_WOL_MC_MAGIC_PKT_WAKE))
Joshua Hay1d680052016-12-12 15:44:08 -080013296 i40e_enable_mc_magic_wake(pf);
13297
Jacob Kellerdfc4ff62017-06-07 05:43:13 -040013298 i40e_prep_for_reset(pf, false);
Catherine Sullivan02b42492015-07-10 19:35:59 -040013299
13300 wr32(hw, I40E_PFPM_APM,
13301 (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
13302 wr32(hw, I40E_PFPM_WUFC,
13303 (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
13304
Shannon Nelsone1477582015-02-21 06:44:33 +000013305 i40e_clear_interrupt_scheme(pf);
13306
Shannon Nelson9007bcc2013-11-26 10:49:23 +000013307 if (system_state == SYSTEM_POWER_OFF) {
Shannon Nelson8e2773a2013-11-28 06:39:22 +000013308 pci_wake_from_d3(pdev, pf->wol_en);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000013309 pci_set_power_state(pdev, PCI_D3hot);
13310 }
13311}
13312
Shannon Nelson9007bcc2013-11-26 10:49:23 +000013313/**
Jacob Keller0e5d3da2017-07-14 09:27:03 -040013314 * i40e_suspend - PM callback for moving to D3
13315 * @dev: generic device information structure
Shannon Nelson9007bcc2013-11-26 10:49:23 +000013316 **/
Arnd Bergmann254d1522017-10-10 10:17:38 +020013317static int __maybe_unused i40e_suspend(struct device *dev)
Shannon Nelson9007bcc2013-11-26 10:49:23 +000013318{
Jacob Keller0e5d3da2017-07-14 09:27:03 -040013319 struct pci_dev *pdev = to_pci_dev(dev);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000013320 struct i40e_pf *pf = pci_get_drvdata(pdev);
Shannon Nelson8e2773a2013-11-28 06:39:22 +000013321 struct i40e_hw *hw = &pf->hw;
Shannon Nelson9007bcc2013-11-26 10:49:23 +000013322
Jacob Keller401586c2017-07-14 09:27:04 -040013323 /* If we're already suspended, then there is nothing to do */
13324 if (test_and_set_bit(__I40E_SUSPENDED, pf->state))
13325 return 0;
13326
Mauro S. M. Rodrigues9e6c9c02017-05-12 23:26:56 -030013327 set_bit(__I40E_DOWN, pf->state);
Mitch Williams3932dbf2015-03-31 00:45:04 -070013328
Jacob Keller5c499222017-07-14 09:27:05 -040013329 /* Ensure service task will not be running */
13330 del_timer_sync(&pf->service_timer);
13331 cancel_work_sync(&pf->service_task);
13332
Jacob Kellerd36e41d2017-06-23 04:24:46 -040013333 if (pf->wol_en && (pf->hw_features & I40E_HW_WOL_MC_MAGIC_PKT_WAKE))
Joshua Hay1d680052016-12-12 15:44:08 -080013334 i40e_enable_mc_magic_wake(pf);
13335
Jacob Kellerdfc4ff62017-06-07 05:43:13 -040013336 i40e_prep_for_reset(pf, false);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000013337
Shannon Nelson8e2773a2013-11-28 06:39:22 +000013338 wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
13339 wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
13340
Jacob Kellerb980c062017-07-14 09:27:06 -040013341 /* Clear the interrupt scheme and release our IRQs so that the system
13342 * can safely hibernate even when there are a large number of CPUs.
13343 * Otherwise hibernation might fail when mapping all the vectors back
13344 * to CPU0.
13345 */
13346 i40e_clear_interrupt_scheme(pf);
Jacob Kellerc17401a2017-07-14 09:27:02 -040013347
Jacob Keller0e5d3da2017-07-14 09:27:03 -040013348 return 0;
Shannon Nelson9007bcc2013-11-26 10:49:23 +000013349}
13350
13351/**
Jacob Keller0e5d3da2017-07-14 09:27:03 -040013352 * i40e_resume - PM callback for waking up from D3
13353 * @dev: generic device information structure
Shannon Nelson9007bcc2013-11-26 10:49:23 +000013354 **/
Arnd Bergmann254d1522017-10-10 10:17:38 +020013355static int __maybe_unused i40e_resume(struct device *dev)
Shannon Nelson9007bcc2013-11-26 10:49:23 +000013356{
Jacob Keller0e5d3da2017-07-14 09:27:03 -040013357 struct pci_dev *pdev = to_pci_dev(dev);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000013358 struct i40e_pf *pf = pci_get_drvdata(pdev);
Jacob Kellerb980c062017-07-14 09:27:06 -040013359 int err;
Shannon Nelson9007bcc2013-11-26 10:49:23 +000013360
Jacob Keller401586c2017-07-14 09:27:04 -040013361 /* If we're not suspended, then there is nothing to do */
13362 if (!test_bit(__I40E_SUSPENDED, pf->state))
13363 return 0;
13364
Jacob Kellerb980c062017-07-14 09:27:06 -040013365 /* We cleared the interrupt scheme when we suspended, so we need to
13366 * restore it now to resume device functionality.
13367 */
13368 err = i40e_restore_interrupt_scheme(pf);
13369 if (err) {
13370 dev_err(&pdev->dev, "Cannot restore interrupt scheme: %d\n",
13371 err);
13372 }
13373
Jacob Keller401586c2017-07-14 09:27:04 -040013374 clear_bit(__I40E_DOWN, pf->state);
13375 i40e_reset_and_rebuild(pf, false, false);
13376
13377 /* Clear suspended state last after everything is recovered */
13378 clear_bit(__I40E_SUSPENDED, pf->state);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000013379
Jacob Keller5c499222017-07-14 09:27:05 -040013380 /* Restart the service task */
13381 mod_timer(&pf->service_timer,
13382 round_jiffies(jiffies + pf->service_timer_period));
13383
Shannon Nelson9007bcc2013-11-26 10:49:23 +000013384 return 0;
13385}
13386
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013387static const struct pci_error_handlers i40e_err_handler = {
13388 .error_detected = i40e_pci_error_detected,
13389 .slot_reset = i40e_pci_error_slot_reset,
Alan Brady19b79602017-08-29 05:32:39 -040013390 .reset_prepare = i40e_pci_error_reset_prepare,
13391 .reset_done = i40e_pci_error_reset_done,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013392 .resume = i40e_pci_error_resume,
13393};
13394
Jacob Keller0e5d3da2017-07-14 09:27:03 -040013395static SIMPLE_DEV_PM_OPS(i40e_pm_ops, i40e_suspend, i40e_resume);
13396
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013397static struct pci_driver i40e_driver = {
13398 .name = i40e_driver_name,
13399 .id_table = i40e_pci_tbl,
13400 .probe = i40e_probe,
13401 .remove = i40e_remove,
Jacob Keller0e5d3da2017-07-14 09:27:03 -040013402 .driver = {
13403 .pm = &i40e_pm_ops,
13404 },
Shannon Nelson9007bcc2013-11-26 10:49:23 +000013405 .shutdown = i40e_shutdown,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013406 .err_handler = &i40e_err_handler,
13407 .sriov_configure = i40e_pci_sriov_configure,
13408};
13409
13410/**
13411 * i40e_init_module - Driver registration routine
13412 *
13413 * i40e_init_module is the first routine called when the driver is
13414 * loaded. All it does is register with the PCI subsystem.
13415 **/
13416static int __init i40e_init_module(void)
13417{
13418 pr_info("%s: %s - version %s\n", i40e_driver_name,
13419 i40e_driver_string, i40e_driver_version_str);
13420 pr_info("%s: %s\n", i40e_driver_name, i40e_copyright);
Greg Rose96664482015-02-06 08:52:13 +000013421
Jacob Keller4d5957c2017-06-20 15:16:54 -070013422 /* There is no need to throttle the number of active tasks because
13423 * each device limits its own task using a state bit for scheduling
13424 * the service task, and the device tasks do not interfere with each
13425 * other, so we don't set a max task limit. We must set WQ_MEM_RECLAIM
13426 * since we need to be able to guarantee forward progress even under
13427 * memory pressure.
Jesse Brandeburg2803b162015-12-22 14:25:08 -080013428 */
Jacob Keller4d5957c2017-06-20 15:16:54 -070013429 i40e_wq = alloc_workqueue("%s", WQ_MEM_RECLAIM, 0, i40e_driver_name);
Jesse Brandeburg2803b162015-12-22 14:25:08 -080013430 if (!i40e_wq) {
13431 pr_err("%s: Failed to create workqueue\n", i40e_driver_name);
13432 return -ENOMEM;
13433 }
13434
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013435 i40e_dbg_init();
13436 return pci_register_driver(&i40e_driver);
13437}
13438module_init(i40e_init_module);
13439
13440/**
13441 * i40e_exit_module - Driver exit cleanup routine
13442 *
13443 * i40e_exit_module is called just before the driver is removed
13444 * from memory.
13445 **/
13446static void __exit i40e_exit_module(void)
13447{
13448 pci_unregister_driver(&i40e_driver);
Jesse Brandeburg2803b162015-12-22 14:25:08 -080013449 destroy_workqueue(i40e_wq);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013450 i40e_dbg_exit();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013451}
13452module_exit(i40e_exit_module);