blob: 60b11fdeca2d5d955232c3cb6dc6eb86b5168c7e [file] [log] [blame]
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001/*******************************************************************************
2 *
3 * Intel Ethernet Controller XL710 Family Linux Driver
Shannon Nelson4fc8c672017-06-07 05:43:08 -04004 * Copyright(c) 2013 - 2017 Intel Corporation.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
Greg Rosedc641b72013-12-18 13:45:51 +000015 * You should have received a copy of the GNU General Public License along
16 * with this program. If not, see <http://www.gnu.org/licenses/>.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000017 *
18 * The full GNU General Public License is included in this distribution in
19 * the file called "COPYING".
20 *
21 * Contact Information:
22 * e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
23 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
24 *
25 ******************************************************************************/
26
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050027#include <linux/etherdevice.h>
28#include <linux/of_net.h>
29#include <linux/pci.h>
Björn Töpel0c8493d2017-05-24 07:55:34 +020030#include <linux/bpf.h>
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050031
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000032/* Local includes */
33#include "i40e.h"
Shannon Nelson4eb3f762014-03-06 08:59:58 +000034#include "i40e_diag.h"
Alexander Duyck06a5f7f2016-06-16 12:22:06 -070035#include <net/udp_tunnel.h>
Scott Petersoned0980c2017-04-13 04:45:44 -040036/* All i40e tracepoints are defined by the include below, which
37 * must be included exactly once across the whole kernel with
38 * CREATE_TRACE_POINTS defined
39 */
40#define CREATE_TRACE_POINTS
41#include "i40e_trace.h"
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000042
43const char i40e_driver_name[] = "i40e";
44static const char i40e_driver_string[] =
45 "Intel(R) Ethernet Connection XL710 Network Driver";
46
47#define DRV_KERN "-k"
48
Bimmy Pujari15990832017-01-30 12:29:37 -080049#define DRV_VERSION_MAJOR 2
50#define DRV_VERSION_MINOR 1
Jacob Keller2318b402017-04-19 09:25:53 -040051#define DRV_VERSION_BUILD 14
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000052#define DRV_VERSION __stringify(DRV_VERSION_MAJOR) "." \
53 __stringify(DRV_VERSION_MINOR) "." \
54 __stringify(DRV_VERSION_BUILD) DRV_KERN
55const char i40e_driver_version_str[] = DRV_VERSION;
Jesse Brandeburg8fb905b2014-01-17 15:36:33 -080056static const char i40e_copyright[] = "Copyright (c) 2013 - 2014 Intel Corporation.";
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000057
58/* a bit of forward declarations */
59static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi);
Maciej Sosin373149f2017-04-05 07:50:55 -040060static void i40e_handle_reset_warning(struct i40e_pf *pf, bool lock_acquired);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000061static int i40e_add_vsi(struct i40e_vsi *vsi);
62static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi);
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000063static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000064static int i40e_setup_misc_vector(struct i40e_pf *pf);
65static void i40e_determine_queue_usage(struct i40e_pf *pf);
66static int i40e_setup_pf_filter_control(struct i40e_pf *pf);
Maciej Sosin373149f2017-04-05 07:50:55 -040067static void i40e_prep_for_reset(struct i40e_pf *pf, bool lock_acquired);
68static int i40e_reset(struct i40e_pf *pf);
69static void i40e_rebuild(struct i40e_pf *pf, bool reinit, bool lock_acquired);
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080070static void i40e_fdir_sb_setup(struct i40e_pf *pf);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -080071static int i40e_veb_get_bw_info(struct i40e_veb *veb);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000072
73/* i40e_pci_tbl - PCI Device ID Table
74 *
75 * Last entry must be all 0s
76 *
77 * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
78 * Class, Class Mask, private data (not used) }
79 */
Benoit Taine9baa3c32014-08-08 15:56:03 +020080static const struct pci_device_id i40e_pci_tbl[] = {
Shannon Nelsonab600852014-01-17 15:36:39 -080081 {PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_XL710), 0},
Shannon Nelsonab600852014-01-17 15:36:39 -080082 {PCI_VDEVICE(INTEL, I40E_DEV_ID_QEMU), 0},
Shannon Nelsonab600852014-01-17 15:36:39 -080083 {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_B), 0},
84 {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_C), 0},
Shannon Nelsonab600852014-01-17 15:36:39 -080085 {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_A), 0},
86 {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_B), 0},
87 {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_C), 0},
Mitch Williams5960d332014-09-13 07:40:47 +000088 {PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T), 0},
Shannon Nelsonbc5166b92015-08-26 15:14:10 -040089 {PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T4), 0},
Anjali Singhai Jain35dae512015-12-22 14:25:03 -080090 {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_X722), 0},
91 {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_X722), 0},
Anjali Singhai Jain87e6c1d2015-06-05 12:20:25 -040092 {PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_X722), 0},
93 {PCI_VDEVICE(INTEL, I40E_DEV_ID_1G_BASE_T_X722), 0},
94 {PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T_X722), 0},
Catherine Sullivand6bf58c2016-03-18 12:18:08 -070095 {PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_I_X722), 0},
Shannon Nelson48a3b512015-07-23 16:54:39 -040096 {PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2), 0},
97 {PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2_A), 0},
Carolyn Wyborny31232372016-11-21 13:03:48 -080098 {PCI_VDEVICE(INTEL, I40E_DEV_ID_25G_B), 0},
99 {PCI_VDEVICE(INTEL, I40E_DEV_ID_25G_SFP28), 0},
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000100 /* required last entry */
101 {0, }
102};
103MODULE_DEVICE_TABLE(pci, i40e_pci_tbl);
104
105#define I40E_MAX_VF_COUNT 128
106static int debug = -1;
Alexander Duyck5d4ca232016-09-30 08:21:46 -0400107module_param(debug, uint, 0);
108MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all), Debug mask (0x8XXXXXXX)");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000109
110MODULE_AUTHOR("Intel Corporation, <e1000-devel@lists.sourceforge.net>");
111MODULE_DESCRIPTION("Intel(R) Ethernet Connection XL710 Network Driver");
112MODULE_LICENSE("GPL");
113MODULE_VERSION(DRV_VERSION);
114
Jesse Brandeburg2803b162015-12-22 14:25:08 -0800115static struct workqueue_struct *i40e_wq;
116
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000117/**
118 * i40e_allocate_dma_mem_d - OS specific memory alloc for shared code
119 * @hw: pointer to the HW structure
120 * @mem: ptr to mem struct to fill out
121 * @size: size of memory requested
122 * @alignment: what to align the allocation to
123 **/
124int i40e_allocate_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem,
125 u64 size, u32 alignment)
126{
127 struct i40e_pf *pf = (struct i40e_pf *)hw->back;
128
129 mem->size = ALIGN(size, alignment);
130 mem->va = dma_zalloc_coherent(&pf->pdev->dev, mem->size,
131 &mem->pa, GFP_KERNEL);
Jesse Brandeburg93bc73b2013-09-13 08:23:18 +0000132 if (!mem->va)
133 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000134
Jesse Brandeburg93bc73b2013-09-13 08:23:18 +0000135 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000136}
137
138/**
139 * i40e_free_dma_mem_d - OS specific memory free for shared code
140 * @hw: pointer to the HW structure
141 * @mem: ptr to mem struct to free
142 **/
143int i40e_free_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem)
144{
145 struct i40e_pf *pf = (struct i40e_pf *)hw->back;
146
147 dma_free_coherent(&pf->pdev->dev, mem->size, mem->va, mem->pa);
148 mem->va = NULL;
149 mem->pa = 0;
150 mem->size = 0;
151
152 return 0;
153}
154
155/**
156 * i40e_allocate_virt_mem_d - OS specific memory alloc for shared code
157 * @hw: pointer to the HW structure
158 * @mem: ptr to mem struct to fill out
159 * @size: size of memory requested
160 **/
161int i40e_allocate_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem,
162 u32 size)
163{
164 mem->size = size;
165 mem->va = kzalloc(size, GFP_KERNEL);
166
Jesse Brandeburg93bc73b2013-09-13 08:23:18 +0000167 if (!mem->va)
168 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000169
Jesse Brandeburg93bc73b2013-09-13 08:23:18 +0000170 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000171}
172
173/**
174 * i40e_free_virt_mem_d - OS specific memory free for shared code
175 * @hw: pointer to the HW structure
176 * @mem: ptr to mem struct to free
177 **/
178int i40e_free_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem)
179{
180 /* it's ok to kfree a NULL pointer */
181 kfree(mem->va);
182 mem->va = NULL;
183 mem->size = 0;
184
185 return 0;
186}
187
188/**
189 * i40e_get_lump - find a lump of free generic resource
190 * @pf: board private structure
191 * @pile: the pile of resource to search
192 * @needed: the number of items needed
193 * @id: an owner id to stick on the items assigned
194 *
195 * Returns the base item index of the lump, or negative for error
196 *
197 * The search_hint trick and lack of advanced fit-finding only work
198 * because we're highly likely to have all the same size lump requests.
199 * Linear search time and any fragmentation should be minimal.
200 **/
201static int i40e_get_lump(struct i40e_pf *pf, struct i40e_lump_tracking *pile,
202 u16 needed, u16 id)
203{
204 int ret = -ENOMEM;
Jesse Brandeburgddf434a2013-09-13 08:23:19 +0000205 int i, j;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000206
207 if (!pile || needed == 0 || id >= I40E_PILE_VALID_BIT) {
208 dev_info(&pf->pdev->dev,
209 "param err: pile=%p needed=%d id=0x%04x\n",
210 pile, needed, id);
211 return -EINVAL;
212 }
213
214 /* start the linear search with an imperfect hint */
215 i = pile->search_hint;
Jesse Brandeburgddf434a2013-09-13 08:23:19 +0000216 while (i < pile->num_entries) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000217 /* skip already allocated entries */
218 if (pile->list[i] & I40E_PILE_VALID_BIT) {
219 i++;
220 continue;
221 }
222
223 /* do we have enough in this lump? */
224 for (j = 0; (j < needed) && ((i+j) < pile->num_entries); j++) {
225 if (pile->list[i+j] & I40E_PILE_VALID_BIT)
226 break;
227 }
228
229 if (j == needed) {
230 /* there was enough, so assign it to the requestor */
231 for (j = 0; j < needed; j++)
232 pile->list[i+j] = id | I40E_PILE_VALID_BIT;
233 ret = i;
234 pile->search_hint = i + j;
Jesse Brandeburgddf434a2013-09-13 08:23:19 +0000235 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000236 }
Jesse Brandeburg6995b362015-08-28 17:55:54 -0400237
238 /* not enough, so skip over it and continue looking */
239 i += j;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000240 }
241
242 return ret;
243}
244
245/**
246 * i40e_put_lump - return a lump of generic resource
247 * @pile: the pile of resource to search
248 * @index: the base item index
249 * @id: the owner id of the items assigned
250 *
251 * Returns the count of items in the lump
252 **/
253static int i40e_put_lump(struct i40e_lump_tracking *pile, u16 index, u16 id)
254{
255 int valid_id = (id | I40E_PILE_VALID_BIT);
256 int count = 0;
257 int i;
258
259 if (!pile || index >= pile->num_entries)
260 return -EINVAL;
261
262 for (i = index;
263 i < pile->num_entries && pile->list[i] == valid_id;
264 i++) {
265 pile->list[i] = 0;
266 count++;
267 }
268
269 if (count && index < pile->search_hint)
270 pile->search_hint = index;
271
272 return count;
273}
274
275/**
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700276 * i40e_find_vsi_from_id - searches for the vsi with the given id
277 * @pf - the pf structure to search for the vsi
278 * @id - id of the vsi it is searching for
279 **/
280struct i40e_vsi *i40e_find_vsi_from_id(struct i40e_pf *pf, u16 id)
281{
282 int i;
283
284 for (i = 0; i < pf->num_alloc_vsi; i++)
285 if (pf->vsi[i] && (pf->vsi[i]->id == id))
286 return pf->vsi[i];
287
288 return NULL;
289}
290
291/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000292 * i40e_service_event_schedule - Schedule the service task to wake up
293 * @pf: board private structure
294 *
295 * If not already scheduled, this puts the task into the work queue
296 **/
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -0600297void i40e_service_event_schedule(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000298{
Mauro S. M. Rodrigues9e6c9c02017-05-12 23:26:56 -0300299 if (!test_bit(__I40E_DOWN, pf->state) &&
Jacob Keller0da36b92017-04-19 09:25:55 -0400300 !test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
Jesse Brandeburg2803b162015-12-22 14:25:08 -0800301 queue_work(i40e_wq, &pf->service_task);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000302}
303
304/**
305 * i40e_tx_timeout - Respond to a Tx Hang
306 * @netdev: network interface device structure
307 *
308 * If any port has noticed a Tx timeout, it is likely that the whole
309 * device is munged, not just the one netdev port, so go for the full
310 * reset.
311 **/
312static void i40e_tx_timeout(struct net_device *netdev)
313{
314 struct i40e_netdev_priv *np = netdev_priv(netdev);
315 struct i40e_vsi *vsi = np->vsi;
316 struct i40e_pf *pf = vsi->back;
Kiran Patilb03a8c12015-09-24 18:13:15 -0400317 struct i40e_ring *tx_ring = NULL;
318 unsigned int i, hung_queue = 0;
319 u32 head, val;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000320
321 pf->tx_timeout_count++;
322
Kiran Patilb03a8c12015-09-24 18:13:15 -0400323 /* find the stopped queue the same way the stack does */
324 for (i = 0; i < netdev->num_tx_queues; i++) {
325 struct netdev_queue *q;
326 unsigned long trans_start;
327
328 q = netdev_get_tx_queue(netdev, i);
Florian Westphal9b366272016-05-03 16:33:14 +0200329 trans_start = q->trans_start;
Kiran Patilb03a8c12015-09-24 18:13:15 -0400330 if (netif_xmit_stopped(q) &&
331 time_after(jiffies,
332 (trans_start + netdev->watchdog_timeo))) {
333 hung_queue = i;
334 break;
335 }
336 }
337
338 if (i == netdev->num_tx_queues) {
339 netdev_info(netdev, "tx_timeout: no netdev hung queue found\n");
340 } else {
341 /* now that we have an index, find the tx_ring struct */
342 for (i = 0; i < vsi->num_queue_pairs; i++) {
343 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc) {
344 if (hung_queue ==
345 vsi->tx_rings[i]->queue_index) {
346 tx_ring = vsi->tx_rings[i];
347 break;
348 }
349 }
350 }
351 }
352
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000353 if (time_after(jiffies, (pf->tx_timeout_last_recovery + HZ*20)))
Kiran Patilb03a8c12015-09-24 18:13:15 -0400354 pf->tx_timeout_recovery_level = 1; /* reset after some time */
355 else if (time_before(jiffies,
356 (pf->tx_timeout_last_recovery + netdev->watchdog_timeo)))
357 return; /* don't do any new action before the next timeout */
358
359 if (tx_ring) {
360 head = i40e_get_head(tx_ring);
361 /* Read interrupt register */
362 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
363 val = rd32(&pf->hw,
364 I40E_PFINT_DYN_CTLN(tx_ring->q_vector->v_idx +
365 tx_ring->vsi->base_vector - 1));
366 else
367 val = rd32(&pf->hw, I40E_PFINT_DYN_CTL0);
368
369 netdev_info(netdev, "tx_timeout: VSI_seid: %d, Q %d, NTC: 0x%x, HWB: 0x%x, NTU: 0x%x, TAIL: 0x%x, INT: 0x%x\n",
370 vsi->seid, hung_queue, tx_ring->next_to_clean,
371 head, tx_ring->next_to_use,
372 readl(tx_ring->tail), val);
373 }
374
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000375 pf->tx_timeout_last_recovery = jiffies;
Kiran Patilb03a8c12015-09-24 18:13:15 -0400376 netdev_info(netdev, "tx_timeout recovery level %d, hung_queue %d\n",
377 pf->tx_timeout_recovery_level, hung_queue);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000378
379 switch (pf->tx_timeout_recovery_level) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000380 case 1:
Jacob Keller0da36b92017-04-19 09:25:55 -0400381 set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000382 break;
383 case 2:
Jacob Keller0da36b92017-04-19 09:25:55 -0400384 set_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000385 break;
386 case 3:
Jacob Keller0da36b92017-04-19 09:25:55 -0400387 set_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000388 break;
389 default:
390 netdev_err(netdev, "tx_timeout recovery unsuccessful\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000391 break;
392 }
Kiran Patilb03a8c12015-09-24 18:13:15 -0400393
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000394 i40e_service_event_schedule(pf);
395 pf->tx_timeout_recovery_level++;
396}
397
398/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000399 * i40e_get_vsi_stats_struct - Get System Network Statistics
400 * @vsi: the VSI we care about
401 *
402 * Returns the address of the device statistics structure.
403 * The statistics are actually updated from the service task.
404 **/
405struct rtnl_link_stats64 *i40e_get_vsi_stats_struct(struct i40e_vsi *vsi)
406{
407 return &vsi->net_stats;
408}
409
410/**
Björn Töpel74608d12017-05-24 07:55:35 +0200411 * i40e_get_netdev_stats_struct_tx - populate stats from a Tx ring
412 * @ring: Tx ring to get statistics from
413 * @stats: statistics entry to be updated
414 **/
415static void i40e_get_netdev_stats_struct_tx(struct i40e_ring *ring,
416 struct rtnl_link_stats64 *stats)
417{
418 u64 bytes, packets;
419 unsigned int start;
420
421 do {
422 start = u64_stats_fetch_begin_irq(&ring->syncp);
423 packets = ring->stats.packets;
424 bytes = ring->stats.bytes;
425 } while (u64_stats_fetch_retry_irq(&ring->syncp, start));
426
427 stats->tx_packets += packets;
428 stats->tx_bytes += bytes;
429}
430
431/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000432 * i40e_get_netdev_stats_struct - Get statistics for netdev interface
433 * @netdev: network interface device structure
434 *
435 * Returns the address of the device statistics structure.
436 * The statistics are actually updated from the service task.
437 **/
Alexander Duyck9eed69a2017-02-21 15:55:47 -0800438static void i40e_get_netdev_stats_struct(struct net_device *netdev,
stephen hemmingerbc1f4472017-01-06 19:12:52 -0800439 struct rtnl_link_stats64 *stats)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000440{
441 struct i40e_netdev_priv *np = netdev_priv(netdev);
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +0000442 struct i40e_ring *tx_ring, *rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000443 struct i40e_vsi *vsi = np->vsi;
Alexander Duyck980e9b12013-09-28 06:01:03 +0000444 struct rtnl_link_stats64 *vsi_stats = i40e_get_vsi_stats_struct(vsi);
445 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000446
Jacob Keller0da36b92017-04-19 09:25:55 -0400447 if (test_bit(__I40E_VSI_DOWN, vsi->state))
stephen hemmingerbc1f4472017-01-06 19:12:52 -0800448 return;
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +0000449
Jesse Brandeburg3c325ce2013-12-14 03:26:45 -0800450 if (!vsi->tx_rings)
stephen hemmingerbc1f4472017-01-06 19:12:52 -0800451 return;
Jesse Brandeburg3c325ce2013-12-14 03:26:45 -0800452
Alexander Duyck980e9b12013-09-28 06:01:03 +0000453 rcu_read_lock();
454 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck980e9b12013-09-28 06:01:03 +0000455 u64 bytes, packets;
456 unsigned int start;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000457
Alexander Duyck980e9b12013-09-28 06:01:03 +0000458 tx_ring = ACCESS_ONCE(vsi->tx_rings[i]);
459 if (!tx_ring)
460 continue;
Björn Töpel74608d12017-05-24 07:55:35 +0200461 i40e_get_netdev_stats_struct_tx(tx_ring, stats);
Alexander Duyck980e9b12013-09-28 06:01:03 +0000462
Alexander Duyck980e9b12013-09-28 06:01:03 +0000463 rx_ring = &tx_ring[1];
464
465 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700466 start = u64_stats_fetch_begin_irq(&rx_ring->syncp);
Alexander Duyck980e9b12013-09-28 06:01:03 +0000467 packets = rx_ring->stats.packets;
468 bytes = rx_ring->stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700469 } while (u64_stats_fetch_retry_irq(&rx_ring->syncp, start));
Alexander Duyck980e9b12013-09-28 06:01:03 +0000470
471 stats->rx_packets += packets;
472 stats->rx_bytes += bytes;
Björn Töpel74608d12017-05-24 07:55:35 +0200473
474 if (i40e_enabled_xdp_vsi(vsi))
475 i40e_get_netdev_stats_struct_tx(&rx_ring[1], stats);
Alexander Duyck980e9b12013-09-28 06:01:03 +0000476 }
477 rcu_read_unlock();
478
Akeem G Abodunrina5282f42014-05-10 04:49:03 +0000479 /* following stats updated by i40e_watchdog_subtask() */
Alexander Duyck980e9b12013-09-28 06:01:03 +0000480 stats->multicast = vsi_stats->multicast;
481 stats->tx_errors = vsi_stats->tx_errors;
482 stats->tx_dropped = vsi_stats->tx_dropped;
483 stats->rx_errors = vsi_stats->rx_errors;
Jesse Brandeburgd8201e22015-07-23 16:54:35 -0400484 stats->rx_dropped = vsi_stats->rx_dropped;
Alexander Duyck980e9b12013-09-28 06:01:03 +0000485 stats->rx_crc_errors = vsi_stats->rx_crc_errors;
486 stats->rx_length_errors = vsi_stats->rx_length_errors;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000487}
488
489/**
490 * i40e_vsi_reset_stats - Resets all stats of the given vsi
491 * @vsi: the VSI to have its stats reset
492 **/
493void i40e_vsi_reset_stats(struct i40e_vsi *vsi)
494{
495 struct rtnl_link_stats64 *ns;
496 int i;
497
498 if (!vsi)
499 return;
500
501 ns = i40e_get_vsi_stats_struct(vsi);
502 memset(ns, 0, sizeof(*ns));
503 memset(&vsi->net_stats_offsets, 0, sizeof(vsi->net_stats_offsets));
504 memset(&vsi->eth_stats, 0, sizeof(vsi->eth_stats));
505 memset(&vsi->eth_stats_offsets, 0, sizeof(vsi->eth_stats_offsets));
Greg Rose8e9dca52013-12-18 13:45:53 +0000506 if (vsi->rx_rings && vsi->rx_rings[0]) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000507 for (i = 0; i < vsi->num_queue_pairs; i++) {
Jesse Brandeburg6995b362015-08-28 17:55:54 -0400508 memset(&vsi->rx_rings[i]->stats, 0,
Alexander Duyck9f65e152013-09-28 06:00:58 +0000509 sizeof(vsi->rx_rings[i]->stats));
Jesse Brandeburg6995b362015-08-28 17:55:54 -0400510 memset(&vsi->rx_rings[i]->rx_stats, 0,
Alexander Duyck9f65e152013-09-28 06:00:58 +0000511 sizeof(vsi->rx_rings[i]->rx_stats));
Jesse Brandeburg6995b362015-08-28 17:55:54 -0400512 memset(&vsi->tx_rings[i]->stats, 0,
Alexander Duyck9f65e152013-09-28 06:00:58 +0000513 sizeof(vsi->tx_rings[i]->stats));
514 memset(&vsi->tx_rings[i]->tx_stats, 0,
515 sizeof(vsi->tx_rings[i]->tx_stats));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000516 }
Greg Rose8e9dca52013-12-18 13:45:53 +0000517 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000518 vsi->stat_offsets_loaded = false;
519}
520
521/**
Jeff Kirsherb40c82e62015-02-27 09:18:34 +0000522 * i40e_pf_reset_stats - Reset all of the stats for the given PF
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000523 * @pf: the PF to be reset
524 **/
525void i40e_pf_reset_stats(struct i40e_pf *pf)
526{
Shannon Nelsone91fdf72014-06-03 23:50:18 +0000527 int i;
528
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000529 memset(&pf->stats, 0, sizeof(pf->stats));
530 memset(&pf->stats_offsets, 0, sizeof(pf->stats_offsets));
531 pf->stat_offsets_loaded = false;
Shannon Nelsone91fdf72014-06-03 23:50:18 +0000532
533 for (i = 0; i < I40E_MAX_VEB; i++) {
534 if (pf->veb[i]) {
535 memset(&pf->veb[i]->stats, 0,
536 sizeof(pf->veb[i]->stats));
537 memset(&pf->veb[i]->stats_offsets, 0,
538 sizeof(pf->veb[i]->stats_offsets));
539 pf->veb[i]->stat_offsets_loaded = false;
540 }
541 }
Catherine Sullivan42bce042016-07-27 12:02:32 -0700542 pf->hw_csum_rx_error = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000543}
544
545/**
546 * i40e_stat_update48 - read and update a 48 bit stat from the chip
547 * @hw: ptr to the hardware info
548 * @hireg: the high 32 bit reg to read
549 * @loreg: the low 32 bit reg to read
550 * @offset_loaded: has the initial offset been loaded yet
551 * @offset: ptr to current offset value
552 * @stat: ptr to the stat
553 *
554 * Since the device stats are not reset at PFReset, they likely will not
555 * be zeroed when the driver starts. We'll save the first values read
556 * and use them as offsets to be subtracted from the raw values in order
557 * to report stats that count from zero. In the process, we also manage
558 * the potential roll-over.
559 **/
560static void i40e_stat_update48(struct i40e_hw *hw, u32 hireg, u32 loreg,
561 bool offset_loaded, u64 *offset, u64 *stat)
562{
563 u64 new_data;
564
Shannon Nelsonab600852014-01-17 15:36:39 -0800565 if (hw->device_id == I40E_DEV_ID_QEMU) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000566 new_data = rd32(hw, loreg);
567 new_data |= ((u64)(rd32(hw, hireg) & 0xFFFF)) << 32;
568 } else {
569 new_data = rd64(hw, loreg);
570 }
571 if (!offset_loaded)
572 *offset = new_data;
573 if (likely(new_data >= *offset))
574 *stat = new_data - *offset;
575 else
Jesse Brandeburg41a1d042015-06-04 16:24:02 -0400576 *stat = (new_data + BIT_ULL(48)) - *offset;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000577 *stat &= 0xFFFFFFFFFFFFULL;
578}
579
580/**
581 * i40e_stat_update32 - read and update a 32 bit stat from the chip
582 * @hw: ptr to the hardware info
583 * @reg: the hw reg to read
584 * @offset_loaded: has the initial offset been loaded yet
585 * @offset: ptr to current offset value
586 * @stat: ptr to the stat
587 **/
588static void i40e_stat_update32(struct i40e_hw *hw, u32 reg,
589 bool offset_loaded, u64 *offset, u64 *stat)
590{
591 u32 new_data;
592
593 new_data = rd32(hw, reg);
594 if (!offset_loaded)
595 *offset = new_data;
596 if (likely(new_data >= *offset))
597 *stat = (u32)(new_data - *offset);
598 else
Jesse Brandeburg41a1d042015-06-04 16:24:02 -0400599 *stat = (u32)((new_data + BIT_ULL(32)) - *offset);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000600}
601
602/**
Mariusz Stachura0dc86922017-07-14 09:27:00 -0400603 * i40e_stat_update_and_clear32 - read and clear hw reg, update a 32 bit stat
604 * @hw: ptr to the hardware info
605 * @reg: the hw reg to read and clear
606 * @stat: ptr to the stat
607 **/
608static void i40e_stat_update_and_clear32(struct i40e_hw *hw, u32 reg, u64 *stat)
609{
610 u32 new_data = rd32(hw, reg);
611
612 wr32(hw, reg, 1); /* must write a nonzero value to clear register */
613 *stat += new_data;
614}
615
616/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000617 * i40e_update_eth_stats - Update VSI-specific ethernet statistics counters.
618 * @vsi: the VSI to be updated
619 **/
620void i40e_update_eth_stats(struct i40e_vsi *vsi)
621{
622 int stat_idx = le16_to_cpu(vsi->info.stat_counter_idx);
623 struct i40e_pf *pf = vsi->back;
624 struct i40e_hw *hw = &pf->hw;
625 struct i40e_eth_stats *oes;
626 struct i40e_eth_stats *es; /* device's eth stats */
627
628 es = &vsi->eth_stats;
629 oes = &vsi->eth_stats_offsets;
630
631 /* Gather up the stats that the hw collects */
632 i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
633 vsi->stat_offsets_loaded,
634 &oes->tx_errors, &es->tx_errors);
635 i40e_stat_update32(hw, I40E_GLV_RDPC(stat_idx),
636 vsi->stat_offsets_loaded,
637 &oes->rx_discards, &es->rx_discards);
Shannon Nelson41a9e552014-04-23 04:50:20 +0000638 i40e_stat_update32(hw, I40E_GLV_RUPP(stat_idx),
639 vsi->stat_offsets_loaded,
640 &oes->rx_unknown_protocol, &es->rx_unknown_protocol);
641 i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
642 vsi->stat_offsets_loaded,
643 &oes->tx_errors, &es->tx_errors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000644
645 i40e_stat_update48(hw, I40E_GLV_GORCH(stat_idx),
646 I40E_GLV_GORCL(stat_idx),
647 vsi->stat_offsets_loaded,
648 &oes->rx_bytes, &es->rx_bytes);
649 i40e_stat_update48(hw, I40E_GLV_UPRCH(stat_idx),
650 I40E_GLV_UPRCL(stat_idx),
651 vsi->stat_offsets_loaded,
652 &oes->rx_unicast, &es->rx_unicast);
653 i40e_stat_update48(hw, I40E_GLV_MPRCH(stat_idx),
654 I40E_GLV_MPRCL(stat_idx),
655 vsi->stat_offsets_loaded,
656 &oes->rx_multicast, &es->rx_multicast);
657 i40e_stat_update48(hw, I40E_GLV_BPRCH(stat_idx),
658 I40E_GLV_BPRCL(stat_idx),
659 vsi->stat_offsets_loaded,
660 &oes->rx_broadcast, &es->rx_broadcast);
661
662 i40e_stat_update48(hw, I40E_GLV_GOTCH(stat_idx),
663 I40E_GLV_GOTCL(stat_idx),
664 vsi->stat_offsets_loaded,
665 &oes->tx_bytes, &es->tx_bytes);
666 i40e_stat_update48(hw, I40E_GLV_UPTCH(stat_idx),
667 I40E_GLV_UPTCL(stat_idx),
668 vsi->stat_offsets_loaded,
669 &oes->tx_unicast, &es->tx_unicast);
670 i40e_stat_update48(hw, I40E_GLV_MPTCH(stat_idx),
671 I40E_GLV_MPTCL(stat_idx),
672 vsi->stat_offsets_loaded,
673 &oes->tx_multicast, &es->tx_multicast);
674 i40e_stat_update48(hw, I40E_GLV_BPTCH(stat_idx),
675 I40E_GLV_BPTCL(stat_idx),
676 vsi->stat_offsets_loaded,
677 &oes->tx_broadcast, &es->tx_broadcast);
678 vsi->stat_offsets_loaded = true;
679}
680
681/**
682 * i40e_update_veb_stats - Update Switch component statistics
683 * @veb: the VEB being updated
684 **/
685static void i40e_update_veb_stats(struct i40e_veb *veb)
686{
687 struct i40e_pf *pf = veb->pf;
688 struct i40e_hw *hw = &pf->hw;
689 struct i40e_eth_stats *oes;
690 struct i40e_eth_stats *es; /* device's eth stats */
Neerav Parikhfe860af2015-07-10 19:36:02 -0400691 struct i40e_veb_tc_stats *veb_oes;
692 struct i40e_veb_tc_stats *veb_es;
693 int i, idx = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000694
695 idx = veb->stats_idx;
696 es = &veb->stats;
697 oes = &veb->stats_offsets;
Neerav Parikhfe860af2015-07-10 19:36:02 -0400698 veb_es = &veb->tc_stats;
699 veb_oes = &veb->tc_stats_offsets;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000700
701 /* Gather up the stats that the hw collects */
702 i40e_stat_update32(hw, I40E_GLSW_TDPC(idx),
703 veb->stat_offsets_loaded,
704 &oes->tx_discards, &es->tx_discards);
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +0000705 if (hw->revision_id > 0)
706 i40e_stat_update32(hw, I40E_GLSW_RUPP(idx),
707 veb->stat_offsets_loaded,
708 &oes->rx_unknown_protocol,
709 &es->rx_unknown_protocol);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000710 i40e_stat_update48(hw, I40E_GLSW_GORCH(idx), I40E_GLSW_GORCL(idx),
711 veb->stat_offsets_loaded,
712 &oes->rx_bytes, &es->rx_bytes);
713 i40e_stat_update48(hw, I40E_GLSW_UPRCH(idx), I40E_GLSW_UPRCL(idx),
714 veb->stat_offsets_loaded,
715 &oes->rx_unicast, &es->rx_unicast);
716 i40e_stat_update48(hw, I40E_GLSW_MPRCH(idx), I40E_GLSW_MPRCL(idx),
717 veb->stat_offsets_loaded,
718 &oes->rx_multicast, &es->rx_multicast);
719 i40e_stat_update48(hw, I40E_GLSW_BPRCH(idx), I40E_GLSW_BPRCL(idx),
720 veb->stat_offsets_loaded,
721 &oes->rx_broadcast, &es->rx_broadcast);
722
723 i40e_stat_update48(hw, I40E_GLSW_GOTCH(idx), I40E_GLSW_GOTCL(idx),
724 veb->stat_offsets_loaded,
725 &oes->tx_bytes, &es->tx_bytes);
726 i40e_stat_update48(hw, I40E_GLSW_UPTCH(idx), I40E_GLSW_UPTCL(idx),
727 veb->stat_offsets_loaded,
728 &oes->tx_unicast, &es->tx_unicast);
729 i40e_stat_update48(hw, I40E_GLSW_MPTCH(idx), I40E_GLSW_MPTCL(idx),
730 veb->stat_offsets_loaded,
731 &oes->tx_multicast, &es->tx_multicast);
732 i40e_stat_update48(hw, I40E_GLSW_BPTCH(idx), I40E_GLSW_BPTCL(idx),
733 veb->stat_offsets_loaded,
734 &oes->tx_broadcast, &es->tx_broadcast);
Neerav Parikhfe860af2015-07-10 19:36:02 -0400735 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
736 i40e_stat_update48(hw, I40E_GLVEBTC_RPCH(i, idx),
737 I40E_GLVEBTC_RPCL(i, idx),
738 veb->stat_offsets_loaded,
739 &veb_oes->tc_rx_packets[i],
740 &veb_es->tc_rx_packets[i]);
741 i40e_stat_update48(hw, I40E_GLVEBTC_RBCH(i, idx),
742 I40E_GLVEBTC_RBCL(i, idx),
743 veb->stat_offsets_loaded,
744 &veb_oes->tc_rx_bytes[i],
745 &veb_es->tc_rx_bytes[i]);
746 i40e_stat_update48(hw, I40E_GLVEBTC_TPCH(i, idx),
747 I40E_GLVEBTC_TPCL(i, idx),
748 veb->stat_offsets_loaded,
749 &veb_oes->tc_tx_packets[i],
750 &veb_es->tc_tx_packets[i]);
751 i40e_stat_update48(hw, I40E_GLVEBTC_TBCH(i, idx),
752 I40E_GLVEBTC_TBCL(i, idx),
753 veb->stat_offsets_loaded,
754 &veb_oes->tc_tx_bytes[i],
755 &veb_es->tc_tx_bytes[i]);
756 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000757 veb->stat_offsets_loaded = true;
758}
759
760/**
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000761 * i40e_update_vsi_stats - Update the vsi statistics counters.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000762 * @vsi: the VSI to be updated
763 *
764 * There are a few instances where we store the same stat in a
765 * couple of different structs. This is partly because we have
766 * the netdev stats that need to be filled out, which is slightly
767 * different from the "eth_stats" defined by the chip and used in
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000768 * VF communications. We sort it out here.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000769 **/
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000770static void i40e_update_vsi_stats(struct i40e_vsi *vsi)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000771{
772 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000773 struct rtnl_link_stats64 *ons;
774 struct rtnl_link_stats64 *ns; /* netdev stats */
775 struct i40e_eth_stats *oes;
776 struct i40e_eth_stats *es; /* device's eth stats */
777 u32 tx_restart, tx_busy;
Akeem G Abodunrinbf00b372014-10-17 03:14:39 +0000778 struct i40e_ring *p;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000779 u32 rx_page, rx_buf;
Akeem G Abodunrinbf00b372014-10-17 03:14:39 +0000780 u64 bytes, packets;
781 unsigned int start;
Anjali Singhai Jain2fc3d712015-08-27 11:42:29 -0400782 u64 tx_linearize;
Anjali Singhai Jain164c9f52015-10-21 19:47:08 -0400783 u64 tx_force_wb;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000784 u64 rx_p, rx_b;
785 u64 tx_p, tx_b;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000786 u16 q;
787
Jacob Keller0da36b92017-04-19 09:25:55 -0400788 if (test_bit(__I40E_VSI_DOWN, vsi->state) ||
789 test_bit(__I40E_CONFIG_BUSY, pf->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000790 return;
791
792 ns = i40e_get_vsi_stats_struct(vsi);
793 ons = &vsi->net_stats_offsets;
794 es = &vsi->eth_stats;
795 oes = &vsi->eth_stats_offsets;
796
797 /* Gather up the netdev and vsi stats that the driver collects
798 * on the fly during packet processing
799 */
800 rx_b = rx_p = 0;
801 tx_b = tx_p = 0;
Anjali Singhai Jain164c9f52015-10-21 19:47:08 -0400802 tx_restart = tx_busy = tx_linearize = tx_force_wb = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000803 rx_page = 0;
804 rx_buf = 0;
Alexander Duyck980e9b12013-09-28 06:01:03 +0000805 rcu_read_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000806 for (q = 0; q < vsi->num_queue_pairs; q++) {
Alexander Duyck980e9b12013-09-28 06:01:03 +0000807 /* locate Tx ring */
808 p = ACCESS_ONCE(vsi->tx_rings[q]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000809
Alexander Duyck980e9b12013-09-28 06:01:03 +0000810 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700811 start = u64_stats_fetch_begin_irq(&p->syncp);
Alexander Duyck980e9b12013-09-28 06:01:03 +0000812 packets = p->stats.packets;
813 bytes = p->stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700814 } while (u64_stats_fetch_retry_irq(&p->syncp, start));
Alexander Duyck980e9b12013-09-28 06:01:03 +0000815 tx_b += bytes;
816 tx_p += packets;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000817 tx_restart += p->tx_stats.restart_queue;
818 tx_busy += p->tx_stats.tx_busy;
Anjali Singhai Jain2fc3d712015-08-27 11:42:29 -0400819 tx_linearize += p->tx_stats.tx_linearize;
Anjali Singhai Jain164c9f52015-10-21 19:47:08 -0400820 tx_force_wb += p->tx_stats.tx_force_wb;
Alexander Duyck980e9b12013-09-28 06:01:03 +0000821
822 /* Rx queue is part of the same block as Tx queue */
823 p = &p[1];
824 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700825 start = u64_stats_fetch_begin_irq(&p->syncp);
Alexander Duyck980e9b12013-09-28 06:01:03 +0000826 packets = p->stats.packets;
827 bytes = p->stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700828 } while (u64_stats_fetch_retry_irq(&p->syncp, start));
Alexander Duyck980e9b12013-09-28 06:01:03 +0000829 rx_b += bytes;
830 rx_p += packets;
Mitch Williams420136c2013-12-18 13:45:59 +0000831 rx_buf += p->rx_stats.alloc_buff_failed;
832 rx_page += p->rx_stats.alloc_page_failed;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000833 }
Alexander Duyck980e9b12013-09-28 06:01:03 +0000834 rcu_read_unlock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000835 vsi->tx_restart = tx_restart;
836 vsi->tx_busy = tx_busy;
Anjali Singhai Jain2fc3d712015-08-27 11:42:29 -0400837 vsi->tx_linearize = tx_linearize;
Anjali Singhai Jain164c9f52015-10-21 19:47:08 -0400838 vsi->tx_force_wb = tx_force_wb;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000839 vsi->rx_page_failed = rx_page;
840 vsi->rx_buf_failed = rx_buf;
841
842 ns->rx_packets = rx_p;
843 ns->rx_bytes = rx_b;
844 ns->tx_packets = tx_p;
845 ns->tx_bytes = tx_b;
846
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000847 /* update netdev stats from eth stats */
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000848 i40e_update_eth_stats(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000849 ons->tx_errors = oes->tx_errors;
850 ns->tx_errors = es->tx_errors;
851 ons->multicast = oes->rx_multicast;
852 ns->multicast = es->rx_multicast;
Shannon Nelson41a9e552014-04-23 04:50:20 +0000853 ons->rx_dropped = oes->rx_discards;
854 ns->rx_dropped = es->rx_discards;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000855 ons->tx_dropped = oes->tx_discards;
856 ns->tx_dropped = es->tx_discards;
857
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000858 /* pull in a couple PF stats if this is the main vsi */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000859 if (vsi == pf->vsi[pf->lan_vsi]) {
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000860 ns->rx_crc_errors = pf->stats.crc_errors;
861 ns->rx_errors = pf->stats.crc_errors + pf->stats.illegal_bytes;
862 ns->rx_length_errors = pf->stats.rx_length_errors;
863 }
864}
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000865
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000866/**
Jeff Kirsherb40c82e62015-02-27 09:18:34 +0000867 * i40e_update_pf_stats - Update the PF statistics counters.
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000868 * @pf: the PF to be updated
869 **/
870static void i40e_update_pf_stats(struct i40e_pf *pf)
871{
872 struct i40e_hw_port_stats *osd = &pf->stats_offsets;
873 struct i40e_hw_port_stats *nsd = &pf->stats;
874 struct i40e_hw *hw = &pf->hw;
875 u32 val;
876 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000877
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000878 i40e_stat_update48(hw, I40E_GLPRT_GORCH(hw->port),
879 I40E_GLPRT_GORCL(hw->port),
880 pf->stat_offsets_loaded,
881 &osd->eth.rx_bytes, &nsd->eth.rx_bytes);
882 i40e_stat_update48(hw, I40E_GLPRT_GOTCH(hw->port),
883 I40E_GLPRT_GOTCL(hw->port),
884 pf->stat_offsets_loaded,
885 &osd->eth.tx_bytes, &nsd->eth.tx_bytes);
886 i40e_stat_update32(hw, I40E_GLPRT_RDPC(hw->port),
887 pf->stat_offsets_loaded,
888 &osd->eth.rx_discards,
889 &nsd->eth.rx_discards);
Shannon Nelson532d2832014-04-23 04:50:09 +0000890 i40e_stat_update48(hw, I40E_GLPRT_UPRCH(hw->port),
891 I40E_GLPRT_UPRCL(hw->port),
892 pf->stat_offsets_loaded,
893 &osd->eth.rx_unicast,
894 &nsd->eth.rx_unicast);
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000895 i40e_stat_update48(hw, I40E_GLPRT_MPRCH(hw->port),
896 I40E_GLPRT_MPRCL(hw->port),
897 pf->stat_offsets_loaded,
898 &osd->eth.rx_multicast,
899 &nsd->eth.rx_multicast);
Shannon Nelson532d2832014-04-23 04:50:09 +0000900 i40e_stat_update48(hw, I40E_GLPRT_BPRCH(hw->port),
901 I40E_GLPRT_BPRCL(hw->port),
902 pf->stat_offsets_loaded,
903 &osd->eth.rx_broadcast,
904 &nsd->eth.rx_broadcast);
905 i40e_stat_update48(hw, I40E_GLPRT_UPTCH(hw->port),
906 I40E_GLPRT_UPTCL(hw->port),
907 pf->stat_offsets_loaded,
908 &osd->eth.tx_unicast,
909 &nsd->eth.tx_unicast);
910 i40e_stat_update48(hw, I40E_GLPRT_MPTCH(hw->port),
911 I40E_GLPRT_MPTCL(hw->port),
912 pf->stat_offsets_loaded,
913 &osd->eth.tx_multicast,
914 &nsd->eth.tx_multicast);
915 i40e_stat_update48(hw, I40E_GLPRT_BPTCH(hw->port),
916 I40E_GLPRT_BPTCL(hw->port),
917 pf->stat_offsets_loaded,
918 &osd->eth.tx_broadcast,
919 &nsd->eth.tx_broadcast);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000920
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000921 i40e_stat_update32(hw, I40E_GLPRT_TDOLD(hw->port),
922 pf->stat_offsets_loaded,
923 &osd->tx_dropped_link_down,
924 &nsd->tx_dropped_link_down);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000925
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000926 i40e_stat_update32(hw, I40E_GLPRT_CRCERRS(hw->port),
927 pf->stat_offsets_loaded,
928 &osd->crc_errors, &nsd->crc_errors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000929
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000930 i40e_stat_update32(hw, I40E_GLPRT_ILLERRC(hw->port),
931 pf->stat_offsets_loaded,
932 &osd->illegal_bytes, &nsd->illegal_bytes);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000933
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000934 i40e_stat_update32(hw, I40E_GLPRT_MLFC(hw->port),
935 pf->stat_offsets_loaded,
936 &osd->mac_local_faults,
937 &nsd->mac_local_faults);
938 i40e_stat_update32(hw, I40E_GLPRT_MRFC(hw->port),
939 pf->stat_offsets_loaded,
940 &osd->mac_remote_faults,
941 &nsd->mac_remote_faults);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000942
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000943 i40e_stat_update32(hw, I40E_GLPRT_RLEC(hw->port),
944 pf->stat_offsets_loaded,
945 &osd->rx_length_errors,
946 &nsd->rx_length_errors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000947
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000948 i40e_stat_update32(hw, I40E_GLPRT_LXONRXC(hw->port),
949 pf->stat_offsets_loaded,
950 &osd->link_xon_rx, &nsd->link_xon_rx);
951 i40e_stat_update32(hw, I40E_GLPRT_LXONTXC(hw->port),
952 pf->stat_offsets_loaded,
953 &osd->link_xon_tx, &nsd->link_xon_tx);
Neerav Parikh95db2392015-11-06 15:26:09 -0800954 i40e_stat_update32(hw, I40E_GLPRT_LXOFFRXC(hw->port),
955 pf->stat_offsets_loaded,
956 &osd->link_xoff_rx, &nsd->link_xoff_rx);
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000957 i40e_stat_update32(hw, I40E_GLPRT_LXOFFTXC(hw->port),
958 pf->stat_offsets_loaded,
959 &osd->link_xoff_tx, &nsd->link_xoff_tx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000960
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000961 for (i = 0; i < 8; i++) {
Neerav Parikh95db2392015-11-06 15:26:09 -0800962 i40e_stat_update32(hw, I40E_GLPRT_PXOFFRXC(hw->port, i),
963 pf->stat_offsets_loaded,
964 &osd->priority_xoff_rx[i],
965 &nsd->priority_xoff_rx[i]);
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000966 i40e_stat_update32(hw, I40E_GLPRT_PXONRXC(hw->port, i),
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000967 pf->stat_offsets_loaded,
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000968 &osd->priority_xon_rx[i],
969 &nsd->priority_xon_rx[i]);
970 i40e_stat_update32(hw, I40E_GLPRT_PXONTXC(hw->port, i),
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000971 pf->stat_offsets_loaded,
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000972 &osd->priority_xon_tx[i],
973 &nsd->priority_xon_tx[i]);
974 i40e_stat_update32(hw, I40E_GLPRT_PXOFFTXC(hw->port, i),
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000975 pf->stat_offsets_loaded,
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000976 &osd->priority_xoff_tx[i],
977 &nsd->priority_xoff_tx[i]);
978 i40e_stat_update32(hw,
979 I40E_GLPRT_RXON2OFFCNT(hw->port, i),
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000980 pf->stat_offsets_loaded,
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000981 &osd->priority_xon_2_xoff[i],
982 &nsd->priority_xon_2_xoff[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000983 }
984
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000985 i40e_stat_update48(hw, I40E_GLPRT_PRC64H(hw->port),
986 I40E_GLPRT_PRC64L(hw->port),
987 pf->stat_offsets_loaded,
988 &osd->rx_size_64, &nsd->rx_size_64);
989 i40e_stat_update48(hw, I40E_GLPRT_PRC127H(hw->port),
990 I40E_GLPRT_PRC127L(hw->port),
991 pf->stat_offsets_loaded,
992 &osd->rx_size_127, &nsd->rx_size_127);
993 i40e_stat_update48(hw, I40E_GLPRT_PRC255H(hw->port),
994 I40E_GLPRT_PRC255L(hw->port),
995 pf->stat_offsets_loaded,
996 &osd->rx_size_255, &nsd->rx_size_255);
997 i40e_stat_update48(hw, I40E_GLPRT_PRC511H(hw->port),
998 I40E_GLPRT_PRC511L(hw->port),
999 pf->stat_offsets_loaded,
1000 &osd->rx_size_511, &nsd->rx_size_511);
1001 i40e_stat_update48(hw, I40E_GLPRT_PRC1023H(hw->port),
1002 I40E_GLPRT_PRC1023L(hw->port),
1003 pf->stat_offsets_loaded,
1004 &osd->rx_size_1023, &nsd->rx_size_1023);
1005 i40e_stat_update48(hw, I40E_GLPRT_PRC1522H(hw->port),
1006 I40E_GLPRT_PRC1522L(hw->port),
1007 pf->stat_offsets_loaded,
1008 &osd->rx_size_1522, &nsd->rx_size_1522);
1009 i40e_stat_update48(hw, I40E_GLPRT_PRC9522H(hw->port),
1010 I40E_GLPRT_PRC9522L(hw->port),
1011 pf->stat_offsets_loaded,
1012 &osd->rx_size_big, &nsd->rx_size_big);
1013
1014 i40e_stat_update48(hw, I40E_GLPRT_PTC64H(hw->port),
1015 I40E_GLPRT_PTC64L(hw->port),
1016 pf->stat_offsets_loaded,
1017 &osd->tx_size_64, &nsd->tx_size_64);
1018 i40e_stat_update48(hw, I40E_GLPRT_PTC127H(hw->port),
1019 I40E_GLPRT_PTC127L(hw->port),
1020 pf->stat_offsets_loaded,
1021 &osd->tx_size_127, &nsd->tx_size_127);
1022 i40e_stat_update48(hw, I40E_GLPRT_PTC255H(hw->port),
1023 I40E_GLPRT_PTC255L(hw->port),
1024 pf->stat_offsets_loaded,
1025 &osd->tx_size_255, &nsd->tx_size_255);
1026 i40e_stat_update48(hw, I40E_GLPRT_PTC511H(hw->port),
1027 I40E_GLPRT_PTC511L(hw->port),
1028 pf->stat_offsets_loaded,
1029 &osd->tx_size_511, &nsd->tx_size_511);
1030 i40e_stat_update48(hw, I40E_GLPRT_PTC1023H(hw->port),
1031 I40E_GLPRT_PTC1023L(hw->port),
1032 pf->stat_offsets_loaded,
1033 &osd->tx_size_1023, &nsd->tx_size_1023);
1034 i40e_stat_update48(hw, I40E_GLPRT_PTC1522H(hw->port),
1035 I40E_GLPRT_PTC1522L(hw->port),
1036 pf->stat_offsets_loaded,
1037 &osd->tx_size_1522, &nsd->tx_size_1522);
1038 i40e_stat_update48(hw, I40E_GLPRT_PTC9522H(hw->port),
1039 I40E_GLPRT_PTC9522L(hw->port),
1040 pf->stat_offsets_loaded,
1041 &osd->tx_size_big, &nsd->tx_size_big);
1042
1043 i40e_stat_update32(hw, I40E_GLPRT_RUC(hw->port),
1044 pf->stat_offsets_loaded,
1045 &osd->rx_undersize, &nsd->rx_undersize);
1046 i40e_stat_update32(hw, I40E_GLPRT_RFC(hw->port),
1047 pf->stat_offsets_loaded,
1048 &osd->rx_fragments, &nsd->rx_fragments);
1049 i40e_stat_update32(hw, I40E_GLPRT_ROC(hw->port),
1050 pf->stat_offsets_loaded,
1051 &osd->rx_oversize, &nsd->rx_oversize);
1052 i40e_stat_update32(hw, I40E_GLPRT_RJC(hw->port),
1053 pf->stat_offsets_loaded,
1054 &osd->rx_jabber, &nsd->rx_jabber);
1055
Anjali Singhai Jain433c47d2014-05-22 06:32:17 +00001056 /* FDIR stats */
Mariusz Stachura0dc86922017-07-14 09:27:00 -04001057 i40e_stat_update_and_clear32(hw,
1058 I40E_GLQF_PCNT(I40E_FD_ATR_STAT_IDX(hw->pf_id)),
1059 &nsd->fd_atr_match);
1060 i40e_stat_update_and_clear32(hw,
1061 I40E_GLQF_PCNT(I40E_FD_SB_STAT_IDX(hw->pf_id)),
1062 &nsd->fd_sb_match);
1063 i40e_stat_update_and_clear32(hw,
1064 I40E_GLQF_PCNT(I40E_FD_ATR_TUNNEL_STAT_IDX(hw->pf_id)),
1065 &nsd->fd_atr_tunnel_match);
Anjali Singhai Jain433c47d2014-05-22 06:32:17 +00001066
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001067 val = rd32(hw, I40E_PRTPM_EEE_STAT);
1068 nsd->tx_lpi_status =
1069 (val & I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_MASK) >>
1070 I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_SHIFT;
1071 nsd->rx_lpi_status =
1072 (val & I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_MASK) >>
1073 I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_SHIFT;
1074 i40e_stat_update32(hw, I40E_PRTPM_TLPIC,
1075 pf->stat_offsets_loaded,
1076 &osd->tx_lpi_count, &nsd->tx_lpi_count);
1077 i40e_stat_update32(hw, I40E_PRTPM_RLPIC,
1078 pf->stat_offsets_loaded,
1079 &osd->rx_lpi_count, &nsd->rx_lpi_count);
1080
Anjali Singhai Jaind0389e52015-04-22 19:34:05 -04001081 if (pf->flags & I40E_FLAG_FD_SB_ENABLED &&
Jacob Keller47994c12017-04-19 09:25:57 -04001082 !(pf->flags & I40E_FLAG_FD_SB_AUTO_DISABLED))
Anjali Singhai Jaind0389e52015-04-22 19:34:05 -04001083 nsd->fd_sb_status = true;
1084 else
1085 nsd->fd_sb_status = false;
1086
1087 if (pf->flags & I40E_FLAG_FD_ATR_ENABLED &&
Jacob Keller47994c12017-04-19 09:25:57 -04001088 !(pf->flags & I40E_FLAG_FD_ATR_AUTO_DISABLED))
Anjali Singhai Jaind0389e52015-04-22 19:34:05 -04001089 nsd->fd_atr_status = true;
1090 else
1091 nsd->fd_atr_status = false;
1092
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001093 pf->stat_offsets_loaded = true;
1094}
1095
1096/**
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001097 * i40e_update_stats - Update the various statistics counters.
1098 * @vsi: the VSI to be updated
1099 *
1100 * Update the various stats for this VSI and its related entities.
1101 **/
1102void i40e_update_stats(struct i40e_vsi *vsi)
1103{
1104 struct i40e_pf *pf = vsi->back;
1105
1106 if (vsi == pf->vsi[pf->lan_vsi])
1107 i40e_update_pf_stats(pf);
1108
1109 i40e_update_vsi_stats(vsi);
1110}
1111
1112/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001113 * i40e_find_filter - Search VSI filter list for specific mac/vlan filter
1114 * @vsi: the VSI to be searched
1115 * @macaddr: the MAC address
1116 * @vlan: the vlan
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001117 *
1118 * Returns ptr to the filter object or NULL
1119 **/
1120static struct i40e_mac_filter *i40e_find_filter(struct i40e_vsi *vsi,
Jacob Keller6622f5c2016-10-05 09:30:32 -07001121 const u8 *macaddr, s16 vlan)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001122{
1123 struct i40e_mac_filter *f;
Jacob Keller278e7d02016-10-05 09:30:37 -07001124 u64 key;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001125
1126 if (!vsi || !macaddr)
1127 return NULL;
1128
Jacob Keller278e7d02016-10-05 09:30:37 -07001129 key = i40e_addr_to_hkey(macaddr);
1130 hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001131 if ((ether_addr_equal(macaddr, f->macaddr)) &&
Jacob Keller1bc87e82016-10-05 09:30:31 -07001132 (vlan == f->vlan))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001133 return f;
1134 }
1135 return NULL;
1136}
1137
1138/**
1139 * i40e_find_mac - Find a mac addr in the macvlan filters list
1140 * @vsi: the VSI to be searched
1141 * @macaddr: the MAC address we are searching for
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001142 *
1143 * Returns the first filter with the provided MAC address or NULL if
1144 * MAC address was not found
1145 **/
Jacob Keller6622f5c2016-10-05 09:30:32 -07001146struct i40e_mac_filter *i40e_find_mac(struct i40e_vsi *vsi, const u8 *macaddr)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001147{
1148 struct i40e_mac_filter *f;
Jacob Keller278e7d02016-10-05 09:30:37 -07001149 u64 key;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001150
1151 if (!vsi || !macaddr)
1152 return NULL;
1153
Jacob Keller278e7d02016-10-05 09:30:37 -07001154 key = i40e_addr_to_hkey(macaddr);
1155 hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
Jacob Keller1bc87e82016-10-05 09:30:31 -07001156 if ((ether_addr_equal(macaddr, f->macaddr)))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001157 return f;
1158 }
1159 return NULL;
1160}
1161
1162/**
1163 * i40e_is_vsi_in_vlan - Check if VSI is in vlan mode
1164 * @vsi: the VSI to be searched
1165 *
1166 * Returns true if VSI is in vlan mode or false otherwise
1167 **/
1168bool i40e_is_vsi_in_vlan(struct i40e_vsi *vsi)
1169{
Jacob Kellercbebb852016-10-05 09:30:40 -07001170 /* If we have a PVID, always operate in VLAN mode */
1171 if (vsi->info.pvid)
1172 return true;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001173
Jacob Kellercbebb852016-10-05 09:30:40 -07001174 /* We need to operate in VLAN mode whenever we have any filters with
1175 * a VLAN other than I40E_VLAN_ALL. We could check the table each
1176 * time, incurring search cost repeatedly. However, we can notice two
1177 * things:
1178 *
1179 * 1) the only place where we can gain a VLAN filter is in
1180 * i40e_add_filter.
1181 *
1182 * 2) the only place where filters are actually removed is in
Jacob Keller0b7c8b52016-10-25 16:08:52 -07001183 * i40e_sync_filters_subtask.
Jacob Kellercbebb852016-10-05 09:30:40 -07001184 *
1185 * Thus, we can simply use a boolean value, has_vlan_filters which we
1186 * will set to true when we add a VLAN filter in i40e_add_filter. Then
1187 * we have to perform the full search after deleting filters in
Jacob Keller0b7c8b52016-10-25 16:08:52 -07001188 * i40e_sync_filters_subtask, but we already have to search
Jacob Kellercbebb852016-10-05 09:30:40 -07001189 * filters here and can perform the check at the same time. This
1190 * results in avoiding embedding a loop for VLAN mode inside another
1191 * loop over all the filters, and should maintain correctness as noted
1192 * above.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001193 */
Jacob Kellercbebb852016-10-05 09:30:40 -07001194 return vsi->has_vlan_filter;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001195}
1196
1197/**
Jacob Keller489a3262016-11-11 12:39:31 -08001198 * i40e_correct_mac_vlan_filters - Correct non-VLAN filters if necessary
1199 * @vsi: the VSI to configure
1200 * @tmp_add_list: list of filters ready to be added
1201 * @tmp_del_list: list of filters ready to be deleted
1202 * @vlan_filters: the number of active VLAN filters
1203 *
1204 * Update VLAN=0 and VLAN=-1 (I40E_VLAN_ANY) filters properly so that they
1205 * behave as expected. If we have any active VLAN filters remaining or about
1206 * to be added then we need to update non-VLAN filters to be marked as VLAN=0
1207 * so that they only match against untagged traffic. If we no longer have any
1208 * active VLAN filters, we need to make all non-VLAN filters marked as VLAN=-1
1209 * so that they match against both tagged and untagged traffic. In this way,
1210 * we ensure that we correctly receive the desired traffic. This ensures that
1211 * when we have an active VLAN we will receive only untagged traffic and
1212 * traffic matching active VLANs. If we have no active VLANs then we will
1213 * operate in non-VLAN mode and receive all traffic, tagged or untagged.
1214 *
1215 * Finally, in a similar fashion, this function also corrects filters when
1216 * there is an active PVID assigned to this VSI.
1217 *
1218 * In case of memory allocation failure return -ENOMEM. Otherwise, return 0.
1219 *
1220 * This function is only expected to be called from within
1221 * i40e_sync_vsi_filters.
1222 *
1223 * NOTE: This function expects to be called while under the
1224 * mac_filter_hash_lock
1225 */
1226static int i40e_correct_mac_vlan_filters(struct i40e_vsi *vsi,
1227 struct hlist_head *tmp_add_list,
1228 struct hlist_head *tmp_del_list,
1229 int vlan_filters)
1230{
Jacob Keller5cb25902016-12-12 15:44:15 -08001231 s16 pvid = le16_to_cpu(vsi->info.pvid);
Jacob Keller489a3262016-11-11 12:39:31 -08001232 struct i40e_mac_filter *f, *add_head;
Jacob Keller671889e2016-12-02 12:33:00 -08001233 struct i40e_new_mac_filter *new;
Jacob Keller489a3262016-11-11 12:39:31 -08001234 struct hlist_node *h;
1235 int bkt, new_vlan;
1236
1237 /* To determine if a particular filter needs to be replaced we
1238 * have the three following conditions:
1239 *
1240 * a) if we have a PVID assigned, then all filters which are
1241 * not marked as VLAN=PVID must be replaced with filters that
1242 * are.
1243 * b) otherwise, if we have any active VLANS, all filters
1244 * which are marked as VLAN=-1 must be replaced with
1245 * filters marked as VLAN=0
1246 * c) finally, if we do not have any active VLANS, all filters
1247 * which are marked as VLAN=0 must be replaced with filters
1248 * marked as VLAN=-1
1249 */
1250
1251 /* Update the filters about to be added in place */
Jacob Keller671889e2016-12-02 12:33:00 -08001252 hlist_for_each_entry(new, tmp_add_list, hlist) {
Jacob Keller5cb25902016-12-12 15:44:15 -08001253 if (pvid && new->f->vlan != pvid)
1254 new->f->vlan = pvid;
Jacob Keller671889e2016-12-02 12:33:00 -08001255 else if (vlan_filters && new->f->vlan == I40E_VLAN_ANY)
1256 new->f->vlan = 0;
1257 else if (!vlan_filters && new->f->vlan == 0)
1258 new->f->vlan = I40E_VLAN_ANY;
Jacob Keller489a3262016-11-11 12:39:31 -08001259 }
1260
1261 /* Update the remaining active filters */
1262 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1263 /* Combine the checks for whether a filter needs to be changed
1264 * and then determine the new VLAN inside the if block, in
1265 * order to avoid duplicating code for adding the new filter
1266 * then deleting the old filter.
1267 */
Jacob Keller5cb25902016-12-12 15:44:15 -08001268 if ((pvid && f->vlan != pvid) ||
Jacob Keller489a3262016-11-11 12:39:31 -08001269 (vlan_filters && f->vlan == I40E_VLAN_ANY) ||
1270 (!vlan_filters && f->vlan == 0)) {
1271 /* Determine the new vlan we will be adding */
Jacob Keller5cb25902016-12-12 15:44:15 -08001272 if (pvid)
1273 new_vlan = pvid;
Jacob Keller489a3262016-11-11 12:39:31 -08001274 else if (vlan_filters)
1275 new_vlan = 0;
1276 else
1277 new_vlan = I40E_VLAN_ANY;
1278
1279 /* Create the new filter */
1280 add_head = i40e_add_filter(vsi, f->macaddr, new_vlan);
1281 if (!add_head)
1282 return -ENOMEM;
1283
Jacob Keller671889e2016-12-02 12:33:00 -08001284 /* Create a temporary i40e_new_mac_filter */
1285 new = kzalloc(sizeof(*new), GFP_ATOMIC);
1286 if (!new)
1287 return -ENOMEM;
1288
1289 new->f = add_head;
1290 new->state = add_head->state;
1291
1292 /* Add the new filter to the tmp list */
1293 hlist_add_head(&new->hlist, tmp_add_list);
Jacob Keller489a3262016-11-11 12:39:31 -08001294
1295 /* Put the original filter into the delete list */
1296 f->state = I40E_FILTER_REMOVE;
1297 hash_del(&f->hlist);
1298 hlist_add_head(&f->hlist, tmp_del_list);
1299 }
1300 }
1301
1302 vsi->has_vlan_filter = !!vlan_filters;
1303
1304 return 0;
1305}
1306
1307/**
Jacob Keller1596b5d2016-11-08 13:05:15 -08001308 * i40e_rm_default_mac_filter - Remove the default MAC filter set by NVM
1309 * @vsi: the PF Main VSI - inappropriate for any other VSI
1310 * @macaddr: the MAC address
1311 *
1312 * Remove whatever filter the firmware set up so the driver can manage
1313 * its own filtering intelligently.
1314 **/
1315static void i40e_rm_default_mac_filter(struct i40e_vsi *vsi, u8 *macaddr)
1316{
1317 struct i40e_aqc_remove_macvlan_element_data element;
1318 struct i40e_pf *pf = vsi->back;
1319
1320 /* Only appropriate for the PF main VSI */
1321 if (vsi->type != I40E_VSI_MAIN)
1322 return;
1323
1324 memset(&element, 0, sizeof(element));
1325 ether_addr_copy(element.mac_addr, macaddr);
1326 element.vlan_tag = 0;
1327 /* Ignore error returns, some firmware does it this way... */
1328 element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
1329 i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1330
1331 memset(&element, 0, sizeof(element));
1332 ether_addr_copy(element.mac_addr, macaddr);
1333 element.vlan_tag = 0;
1334 /* ...and some firmware does it this way. */
1335 element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH |
1336 I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
1337 i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1338}
1339
1340/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001341 * i40e_add_filter - Add a mac/vlan filter to the VSI
1342 * @vsi: the VSI to be searched
1343 * @macaddr: the MAC address
1344 * @vlan: the vlan
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001345 *
1346 * Returns ptr to the filter object or NULL when no memory available.
Kiran Patil21659032015-09-30 14:09:03 -04001347 *
Jacob Keller278e7d02016-10-05 09:30:37 -07001348 * NOTE: This function is expected to be called with mac_filter_hash_lock
Kiran Patil21659032015-09-30 14:09:03 -04001349 * being held.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001350 **/
1351struct i40e_mac_filter *i40e_add_filter(struct i40e_vsi *vsi,
Jacob Keller6622f5c2016-10-05 09:30:32 -07001352 const u8 *macaddr, s16 vlan)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001353{
1354 struct i40e_mac_filter *f;
Jacob Keller278e7d02016-10-05 09:30:37 -07001355 u64 key;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001356
1357 if (!vsi || !macaddr)
1358 return NULL;
1359
Jacob Keller1bc87e82016-10-05 09:30:31 -07001360 f = i40e_find_filter(vsi, macaddr, vlan);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001361 if (!f) {
1362 f = kzalloc(sizeof(*f), GFP_ATOMIC);
1363 if (!f)
Jacob Keller1bc87e82016-10-05 09:30:31 -07001364 return NULL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001365
Jacob Kellercbebb852016-10-05 09:30:40 -07001366 /* Update the boolean indicating if we need to function in
1367 * VLAN mode.
1368 */
1369 if (vlan >= 0)
1370 vsi->has_vlan_filter = true;
1371
Greg Rose9a173902014-05-22 06:32:02 +00001372 ether_addr_copy(f->macaddr, macaddr);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001373 f->vlan = vlan;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001374 /* If we're in overflow promisc mode, set the state directly
1375 * to failed, so we don't bother to try sending the filter
1376 * to the hardware.
1377 */
Jacob Keller0da36b92017-04-19 09:25:55 -04001378 if (test_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state))
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001379 f->state = I40E_FILTER_FAILED;
1380 else
1381 f->state = I40E_FILTER_NEW;
Jacob Keller278e7d02016-10-05 09:30:37 -07001382 INIT_HLIST_NODE(&f->hlist);
1383
1384 key = i40e_addr_to_hkey(macaddr);
1385 hash_add(vsi->mac_filter_hash, &f->hlist, key);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001386
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001387 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1388 vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
1389 }
1390
Jacob Keller1bc87e82016-10-05 09:30:31 -07001391 /* If we're asked to add a filter that has been marked for removal, it
1392 * is safe to simply restore it to active state. __i40e_del_filter
1393 * will have simply deleted any filters which were previously marked
1394 * NEW or FAILED, so if it is currently marked REMOVE it must have
1395 * previously been ACTIVE. Since we haven't yet run the sync filters
1396 * task, just restore this filter to the ACTIVE state so that the
1397 * sync task leaves it in place
1398 */
1399 if (f->state == I40E_FILTER_REMOVE)
1400 f->state = I40E_FILTER_ACTIVE;
1401
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001402 return f;
1403}
1404
1405/**
Jacob Keller290d2552016-10-05 09:30:36 -07001406 * __i40e_del_filter - Remove a specific filter from the VSI
1407 * @vsi: VSI to remove from
1408 * @f: the filter to remove from the list
1409 *
1410 * This function should be called instead of i40e_del_filter only if you know
1411 * the exact filter you will remove already, such as via i40e_find_filter or
1412 * i40e_find_mac.
Kiran Patil21659032015-09-30 14:09:03 -04001413 *
Jacob Keller278e7d02016-10-05 09:30:37 -07001414 * NOTE: This function is expected to be called with mac_filter_hash_lock
Kiran Patil21659032015-09-30 14:09:03 -04001415 * being held.
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001416 * ANOTHER NOTE: This function MUST be called from within the context of
1417 * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
1418 * instead of list_for_each_entry().
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001419 **/
Jacob Keller148141b2016-11-11 12:39:36 -08001420void __i40e_del_filter(struct i40e_vsi *vsi, struct i40e_mac_filter *f)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001421{
Jacob Keller1bc87e82016-10-05 09:30:31 -07001422 if (!f)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001423 return;
1424
Alan Bradya410c822016-12-12 15:44:07 -08001425 /* If the filter was never added to firmware then we can just delete it
1426 * directly and we don't want to set the status to remove or else an
1427 * admin queue command will unnecessarily fire.
1428 */
Jacob Keller1bc87e82016-10-05 09:30:31 -07001429 if ((f->state == I40E_FILTER_FAILED) ||
1430 (f->state == I40E_FILTER_NEW)) {
Jacob Keller278e7d02016-10-05 09:30:37 -07001431 hash_del(&f->hlist);
Jacob Keller1bc87e82016-10-05 09:30:31 -07001432 kfree(f);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001433 } else {
Jacob Keller1bc87e82016-10-05 09:30:31 -07001434 f->state = I40E_FILTER_REMOVE;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001435 }
Alan Bradya410c822016-12-12 15:44:07 -08001436
1437 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1438 vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001439}
1440
1441/**
Jacob Keller290d2552016-10-05 09:30:36 -07001442 * i40e_del_filter - Remove a MAC/VLAN filter from the VSI
1443 * @vsi: the VSI to be searched
1444 * @macaddr: the MAC address
1445 * @vlan: the VLAN
1446 *
Jacob Keller278e7d02016-10-05 09:30:37 -07001447 * NOTE: This function is expected to be called with mac_filter_hash_lock
Jacob Keller290d2552016-10-05 09:30:36 -07001448 * being held.
1449 * ANOTHER NOTE: This function MUST be called from within the context of
1450 * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
1451 * instead of list_for_each_entry().
1452 **/
1453void i40e_del_filter(struct i40e_vsi *vsi, const u8 *macaddr, s16 vlan)
1454{
1455 struct i40e_mac_filter *f;
1456
1457 if (!vsi || !macaddr)
1458 return;
1459
1460 f = i40e_find_filter(vsi, macaddr, vlan);
1461 __i40e_del_filter(vsi, f);
1462}
1463
1464/**
Jacob Kellerfeffdbe2016-11-11 12:39:35 -08001465 * i40e_add_mac_filter - Add a MAC filter for all active VLANs
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001466 * @vsi: the VSI to be searched
1467 * @macaddr: the mac address to be filtered
1468 *
Jacob Kellerfeffdbe2016-11-11 12:39:35 -08001469 * If we're not in VLAN mode, just add the filter to I40E_VLAN_ANY. Otherwise,
1470 * go through all the macvlan filters and add a macvlan filter for each
Jacob Keller5feb3d72016-10-05 09:30:34 -07001471 * unique vlan that already exists. If a PVID has been assigned, instead only
1472 * add the macaddr to that VLAN.
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001473 *
Jacob Keller5feb3d72016-10-05 09:30:34 -07001474 * Returns last filter added on success, else NULL
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001475 **/
Jacob Kellerfeffdbe2016-11-11 12:39:35 -08001476struct i40e_mac_filter *i40e_add_mac_filter(struct i40e_vsi *vsi,
1477 const u8 *macaddr)
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001478{
Jacob Keller5feb3d72016-10-05 09:30:34 -07001479 struct i40e_mac_filter *f, *add = NULL;
Jacob Keller278e7d02016-10-05 09:30:37 -07001480 struct hlist_node *h;
1481 int bkt;
Jacob Keller5feb3d72016-10-05 09:30:34 -07001482
1483 if (vsi->info.pvid)
1484 return i40e_add_filter(vsi, macaddr,
1485 le16_to_cpu(vsi->info.pvid));
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001486
Jacob Keller7aaf95362016-11-11 12:39:33 -08001487 if (!i40e_is_vsi_in_vlan(vsi))
1488 return i40e_add_filter(vsi, macaddr, I40E_VLAN_ANY);
1489
Jacob Keller278e7d02016-10-05 09:30:37 -07001490 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Jacob Keller57b341d2016-10-05 09:30:35 -07001491 if (f->state == I40E_FILTER_REMOVE)
1492 continue;
Jacob Keller5feb3d72016-10-05 09:30:34 -07001493 add = i40e_add_filter(vsi, macaddr, f->vlan);
1494 if (!add)
1495 return NULL;
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001496 }
1497
Jacob Keller5feb3d72016-10-05 09:30:34 -07001498 return add;
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001499}
1500
1501/**
Jacob Kellerfeffdbe2016-11-11 12:39:35 -08001502 * i40e_del_mac_filter - Remove a MAC filter from all VLANs
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001503 * @vsi: the VSI to be searched
1504 * @macaddr: the mac address to be removed
1505 *
Jacob Kellerfeffdbe2016-11-11 12:39:35 -08001506 * Removes a given MAC address from a VSI regardless of what VLAN it has been
1507 * associated with.
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001508 *
1509 * Returns 0 for success, or error
1510 **/
Jacob Kellerfeffdbe2016-11-11 12:39:35 -08001511int i40e_del_mac_filter(struct i40e_vsi *vsi, const u8 *macaddr)
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001512{
Jacob Keller278e7d02016-10-05 09:30:37 -07001513 struct i40e_mac_filter *f;
1514 struct hlist_node *h;
Jacob Keller290d2552016-10-05 09:30:36 -07001515 bool found = false;
Jacob Keller278e7d02016-10-05 09:30:37 -07001516 int bkt;
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001517
Jacob Keller278e7d02016-10-05 09:30:37 -07001518 WARN(!spin_is_locked(&vsi->mac_filter_hash_lock),
1519 "Missing mac_filter_hash_lock\n");
1520 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Jacob Keller290d2552016-10-05 09:30:36 -07001521 if (ether_addr_equal(macaddr, f->macaddr)) {
1522 __i40e_del_filter(vsi, f);
1523 found = true;
1524 }
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001525 }
Jacob Keller290d2552016-10-05 09:30:36 -07001526
1527 if (found)
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001528 return 0;
Jacob Keller290d2552016-10-05 09:30:36 -07001529 else
1530 return -ENOENT;
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001531}
1532
1533/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001534 * i40e_set_mac - NDO callback to set mac address
1535 * @netdev: network interface device structure
1536 * @p: pointer to an address structure
1537 *
1538 * Returns 0 on success, negative on failure
1539 **/
1540static int i40e_set_mac(struct net_device *netdev, void *p)
1541{
1542 struct i40e_netdev_priv *np = netdev_priv(netdev);
1543 struct i40e_vsi *vsi = np->vsi;
Shannon Nelson30650cc2014-07-29 04:01:50 +00001544 struct i40e_pf *pf = vsi->back;
1545 struct i40e_hw *hw = &pf->hw;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001546 struct sockaddr *addr = p;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001547
1548 if (!is_valid_ether_addr(addr->sa_data))
1549 return -EADDRNOTAVAIL;
1550
Shannon Nelson30650cc2014-07-29 04:01:50 +00001551 if (ether_addr_equal(netdev->dev_addr, addr->sa_data)) {
1552 netdev_info(netdev, "already using mac address %pM\n",
1553 addr->sa_data);
1554 return 0;
1555 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001556
Jacob Keller0da36b92017-04-19 09:25:55 -04001557 if (test_bit(__I40E_VSI_DOWN, vsi->back->state) ||
1558 test_bit(__I40E_RESET_RECOVERY_PENDING, vsi->back->state))
Anjali Singhai Jain80f64282013-11-28 06:39:47 +00001559 return -EADDRNOTAVAIL;
1560
Shannon Nelson30650cc2014-07-29 04:01:50 +00001561 if (ether_addr_equal(hw->mac.addr, addr->sa_data))
1562 netdev_info(netdev, "returning to hw mac address %pM\n",
1563 hw->mac.addr);
1564 else
1565 netdev_info(netdev, "set new mac address %pM\n", addr->sa_data);
1566
Jacob Keller278e7d02016-10-05 09:30:37 -07001567 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Kellerfeffdbe2016-11-11 12:39:35 -08001568 i40e_del_mac_filter(vsi, netdev->dev_addr);
1569 i40e_add_mac_filter(vsi, addr->sa_data);
Jacob Keller278e7d02016-10-05 09:30:37 -07001570 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001571 ether_addr_copy(netdev->dev_addr, addr->sa_data);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001572 if (vsi->type == I40E_VSI_MAIN) {
1573 i40e_status ret;
Jesse Brandeburg6995b362015-08-28 17:55:54 -04001574
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001575 ret = i40e_aq_mac_address_write(&vsi->back->hw,
Shannon Nelsoncc412222014-06-04 01:23:21 +00001576 I40E_AQC_WRITE_TYPE_LAA_WOL,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001577 addr->sa_data, NULL);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001578 if (ret)
1579 netdev_info(netdev, "Ignoring error from firmware on LAA update, status %s, AQ ret %s\n",
1580 i40e_stat_str(hw, ret),
1581 i40e_aq_str(hw, hw->aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001582 }
1583
Jesse Brandeburgc53934c2016-01-04 10:33:06 -08001584 /* schedule our worker thread which will take care of
1585 * applying the new filter changes
1586 */
1587 i40e_service_event_schedule(vsi->back);
1588 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001589}
1590
1591/**
1592 * i40e_vsi_setup_queue_map - Setup a VSI queue map based on enabled_tc
1593 * @vsi: the VSI being setup
1594 * @ctxt: VSI context structure
1595 * @enabled_tc: Enabled TCs bitmap
1596 * @is_add: True if called before Add VSI
1597 *
1598 * Setup VSI queue mapping for enabled traffic classes.
1599 **/
1600static void i40e_vsi_setup_queue_map(struct i40e_vsi *vsi,
1601 struct i40e_vsi_context *ctxt,
1602 u8 enabled_tc,
1603 bool is_add)
1604{
1605 struct i40e_pf *pf = vsi->back;
1606 u16 sections = 0;
1607 u8 netdev_tc = 0;
1608 u16 numtc = 0;
1609 u16 qcount;
1610 u8 offset;
1611 u16 qmap;
1612 int i;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001613 u16 num_tc_qps = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001614
1615 sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
1616 offset = 0;
1617
1618 if (enabled_tc && (vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
1619 /* Find numtc from enabled TC bitmap */
1620 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08001621 if (enabled_tc & BIT(i)) /* TC is enabled */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001622 numtc++;
1623 }
1624 if (!numtc) {
1625 dev_warn(&pf->pdev->dev, "DCB is enabled but no TC enabled, forcing TC0\n");
1626 numtc = 1;
1627 }
1628 } else {
1629 /* At least TC0 is enabled in case of non-DCB case */
1630 numtc = 1;
1631 }
1632
1633 vsi->tc_config.numtc = numtc;
1634 vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001635 /* Number of queues per enabled TC */
Catherine Sullivan7d644022016-05-16 10:26:41 -07001636 qcount = vsi->alloc_queue_pairs;
1637
Anjali Singhai7f9ff472015-02-21 06:43:19 +00001638 num_tc_qps = qcount / numtc;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001639 num_tc_qps = min_t(int, num_tc_qps, i40e_pf_get_max_q_per_tc(pf));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001640
1641 /* Setup queue offset/count for all TCs for given VSI */
1642 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1643 /* See if the given TC is enabled for the given VSI */
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08001644 if (vsi->tc_config.enabled_tc & BIT(i)) {
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04001645 /* TC is enabled */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001646 int pow, num_qps;
1647
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001648 switch (vsi->type) {
1649 case I40E_VSI_MAIN:
Helin Zhangacd65442015-10-26 19:44:28 -04001650 qcount = min_t(int, pf->alloc_rss_size,
1651 num_tc_qps);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001652 break;
1653 case I40E_VSI_FDIR:
1654 case I40E_VSI_SRIOV:
1655 case I40E_VSI_VMDQ2:
1656 default:
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001657 qcount = num_tc_qps;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001658 WARN_ON(i != 0);
1659 break;
1660 }
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001661 vsi->tc_config.tc_info[i].qoffset = offset;
1662 vsi->tc_config.tc_info[i].qcount = qcount;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001663
Shannon Nelson1e200e42015-02-27 09:15:24 +00001664 /* find the next higher power-of-2 of num queue pairs */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001665 num_qps = qcount;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001666 pow = 0;
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04001667 while (num_qps && (BIT_ULL(pow) < qcount)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001668 pow++;
1669 num_qps >>= 1;
1670 }
1671
1672 vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
1673 qmap =
1674 (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
1675 (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
1676
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001677 offset += qcount;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001678 } else {
1679 /* TC is not enabled so set the offset to
1680 * default queue and allocate one queue
1681 * for the given TC.
1682 */
1683 vsi->tc_config.tc_info[i].qoffset = 0;
1684 vsi->tc_config.tc_info[i].qcount = 1;
1685 vsi->tc_config.tc_info[i].netdev_tc = 0;
1686
1687 qmap = 0;
1688 }
1689 ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
1690 }
1691
1692 /* Set actual Tx/Rx queue pairs */
1693 vsi->num_queue_pairs = offset;
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00001694 if ((vsi->type == I40E_VSI_MAIN) && (numtc == 1)) {
1695 if (vsi->req_queue_pairs > 0)
1696 vsi->num_queue_pairs = vsi->req_queue_pairs;
Anjali Singhai Jain26cdc442015-07-10 19:36:00 -04001697 else if (pf->flags & I40E_FLAG_MSIX_ENABLED)
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00001698 vsi->num_queue_pairs = pf->num_lan_msix;
1699 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001700
1701 /* Scheduler section valid can only be set for ADD VSI */
1702 if (is_add) {
1703 sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
1704
1705 ctxt->info.up_enable_bits = enabled_tc;
1706 }
1707 if (vsi->type == I40E_VSI_SRIOV) {
1708 ctxt->info.mapping_flags |=
1709 cpu_to_le16(I40E_AQ_VSI_QUE_MAP_NONCONTIG);
1710 for (i = 0; i < vsi->num_queue_pairs; i++)
1711 ctxt->info.queue_mapping[i] =
1712 cpu_to_le16(vsi->base_queue + i);
1713 } else {
1714 ctxt->info.mapping_flags |=
1715 cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
1716 ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
1717 }
1718 ctxt->info.valid_sections |= cpu_to_le16(sections);
1719}
1720
1721/**
Jacob Keller6622f5c2016-10-05 09:30:32 -07001722 * i40e_addr_sync - Callback for dev_(mc|uc)_sync to add address
1723 * @netdev: the netdevice
1724 * @addr: address to add
1725 *
1726 * Called by __dev_(mc|uc)_sync when an address needs to be added. We call
1727 * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
1728 */
1729static int i40e_addr_sync(struct net_device *netdev, const u8 *addr)
1730{
1731 struct i40e_netdev_priv *np = netdev_priv(netdev);
1732 struct i40e_vsi *vsi = np->vsi;
Jacob Keller6622f5c2016-10-05 09:30:32 -07001733
Jacob Kellerfeffdbe2016-11-11 12:39:35 -08001734 if (i40e_add_mac_filter(vsi, addr))
Jacob Keller6622f5c2016-10-05 09:30:32 -07001735 return 0;
1736 else
1737 return -ENOMEM;
1738}
1739
1740/**
1741 * i40e_addr_unsync - Callback for dev_(mc|uc)_sync to remove address
1742 * @netdev: the netdevice
1743 * @addr: address to add
1744 *
1745 * Called by __dev_(mc|uc)_sync when an address needs to be removed. We call
1746 * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
1747 */
1748static int i40e_addr_unsync(struct net_device *netdev, const u8 *addr)
1749{
1750 struct i40e_netdev_priv *np = netdev_priv(netdev);
1751 struct i40e_vsi *vsi = np->vsi;
1752
Jacob Kellerfeffdbe2016-11-11 12:39:35 -08001753 i40e_del_mac_filter(vsi, addr);
Jacob Keller6622f5c2016-10-05 09:30:32 -07001754
1755 return 0;
1756}
1757
1758/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001759 * i40e_set_rx_mode - NDO callback to set the netdev filters
1760 * @netdev: network interface device structure
1761 **/
1762static void i40e_set_rx_mode(struct net_device *netdev)
1763{
1764 struct i40e_netdev_priv *np = netdev_priv(netdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001765 struct i40e_vsi *vsi = np->vsi;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001766
Jacob Keller278e7d02016-10-05 09:30:37 -07001767 spin_lock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -04001768
Jacob Keller6622f5c2016-10-05 09:30:32 -07001769 __dev_uc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
1770 __dev_mc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001771
Jacob Keller278e7d02016-10-05 09:30:37 -07001772 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001773
1774 /* check for other flag changes */
1775 if (vsi->current_netdev_flags != vsi->netdev->flags) {
1776 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1777 vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
1778 }
1779}
1780
1781/**
Jacob Keller671889e2016-12-02 12:33:00 -08001782 * i40e_undo_del_filter_entries - Undo the changes made to MAC filter entries
Jacob Keller4a2ce272016-10-05 09:30:38 -07001783 * @vsi: Pointer to VSI struct
Kiran Patil21659032015-09-30 14:09:03 -04001784 * @from: Pointer to list which contains MAC filter entries - changes to
1785 * those entries needs to be undone.
1786 *
Jacob Keller671889e2016-12-02 12:33:00 -08001787 * MAC filter entries from this list were slated for deletion.
Kiran Patil21659032015-09-30 14:09:03 -04001788 **/
Jacob Keller671889e2016-12-02 12:33:00 -08001789static void i40e_undo_del_filter_entries(struct i40e_vsi *vsi,
1790 struct hlist_head *from)
Kiran Patil21659032015-09-30 14:09:03 -04001791{
Jacob Keller278e7d02016-10-05 09:30:37 -07001792 struct i40e_mac_filter *f;
1793 struct hlist_node *h;
Kiran Patil21659032015-09-30 14:09:03 -04001794
Jacob Keller278e7d02016-10-05 09:30:37 -07001795 hlist_for_each_entry_safe(f, h, from, hlist) {
1796 u64 key = i40e_addr_to_hkey(f->macaddr);
1797
Kiran Patil21659032015-09-30 14:09:03 -04001798 /* Move the element back into MAC filter list*/
Jacob Keller278e7d02016-10-05 09:30:37 -07001799 hlist_del(&f->hlist);
1800 hash_add(vsi->mac_filter_hash, &f->hlist, key);
Kiran Patil21659032015-09-30 14:09:03 -04001801 }
1802}
1803
1804/**
Jacob Keller671889e2016-12-02 12:33:00 -08001805 * i40e_undo_add_filter_entries - Undo the changes made to MAC filter entries
1806 * @vsi: Pointer to vsi struct
1807 * @from: Pointer to list which contains MAC filter entries - changes to
1808 * those entries needs to be undone.
1809 *
1810 * MAC filter entries from this list were slated for addition.
1811 **/
1812static void i40e_undo_add_filter_entries(struct i40e_vsi *vsi,
1813 struct hlist_head *from)
1814{
1815 struct i40e_new_mac_filter *new;
1816 struct hlist_node *h;
1817
1818 hlist_for_each_entry_safe(new, h, from, hlist) {
1819 /* We can simply free the wrapper structure */
1820 hlist_del(&new->hlist);
1821 kfree(new);
1822 }
1823}
1824
1825/**
Jacob Kellerd88d40b2016-12-02 12:32:59 -08001826 * i40e_next_entry - Get the next non-broadcast filter from a list
Jacob Keller671889e2016-12-02 12:33:00 -08001827 * @next: pointer to filter in list
Jacob Kellerd88d40b2016-12-02 12:32:59 -08001828 *
1829 * Returns the next non-broadcast filter in the list. Required so that we
1830 * ignore broadcast filters within the list, since these are not handled via
1831 * the normal firmware update path.
1832 */
Jacob Keller671889e2016-12-02 12:33:00 -08001833static
1834struct i40e_new_mac_filter *i40e_next_filter(struct i40e_new_mac_filter *next)
Jacob Kellerd88d40b2016-12-02 12:32:59 -08001835{
Jacob Keller584a8872017-03-07 15:17:52 -08001836 hlist_for_each_entry_continue(next, hlist) {
1837 if (!is_broadcast_ether_addr(next->f->macaddr))
1838 return next;
Jacob Kellerd88d40b2016-12-02 12:32:59 -08001839 }
1840
Jacob Keller584a8872017-03-07 15:17:52 -08001841 return NULL;
Jacob Kellerd88d40b2016-12-02 12:32:59 -08001842}
1843
1844/**
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001845 * i40e_update_filter_state - Update filter state based on return data
1846 * from firmware
1847 * @count: Number of filters added
1848 * @add_list: return data from fw
1849 * @head: pointer to first filter in current batch
Kiran Patil21659032015-09-30 14:09:03 -04001850 *
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001851 * MAC filter entries from list were slated to be added to device. Returns
1852 * number of successful filters. Note that 0 does NOT mean success!
Kiran Patil21659032015-09-30 14:09:03 -04001853 **/
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001854static int
1855i40e_update_filter_state(int count,
1856 struct i40e_aqc_add_macvlan_element_data *add_list,
Jacob Keller671889e2016-12-02 12:33:00 -08001857 struct i40e_new_mac_filter *add_head)
Kiran Patil21659032015-09-30 14:09:03 -04001858{
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001859 int retval = 0;
1860 int i;
Kiran Patil21659032015-09-30 14:09:03 -04001861
Jacob Kellerac9e2392016-11-11 12:39:27 -08001862 for (i = 0; i < count; i++) {
1863 /* Always check status of each filter. We don't need to check
1864 * the firmware return status because we pre-set the filter
1865 * status to I40E_AQC_MM_ERR_NO_RES when sending the filter
1866 * request to the adminq. Thus, if it no longer matches then
1867 * we know the filter is active.
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001868 */
Jacob Kellerac9e2392016-11-11 12:39:27 -08001869 if (add_list[i].match_method == I40E_AQC_MM_ERR_NO_RES) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001870 add_head->state = I40E_FILTER_FAILED;
Jacob Kellerac9e2392016-11-11 12:39:27 -08001871 } else {
1872 add_head->state = I40E_FILTER_ACTIVE;
1873 retval++;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001874 }
Jacob Kellerac9e2392016-11-11 12:39:27 -08001875
Jacob Kellerd88d40b2016-12-02 12:32:59 -08001876 add_head = i40e_next_filter(add_head);
1877 if (!add_head)
1878 break;
Kiran Patil21659032015-09-30 14:09:03 -04001879 }
Jacob Kellerac9e2392016-11-11 12:39:27 -08001880
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001881 return retval;
Kiran Patil21659032015-09-30 14:09:03 -04001882}
1883
1884/**
Jacob Keller00936312016-10-05 09:30:41 -07001885 * i40e_aqc_del_filters - Request firmware to delete a set of filters
1886 * @vsi: ptr to the VSI
1887 * @vsi_name: name to display in messages
1888 * @list: the list of filters to send to firmware
1889 * @num_del: the number of filters to delete
1890 * @retval: Set to -EIO on failure to delete
1891 *
1892 * Send a request to firmware via AdminQ to delete a set of filters. Uses
1893 * *retval instead of a return value so that success does not force ret_val to
1894 * be set to 0. This ensures that a sequence of calls to this function
1895 * preserve the previous value of *retval on successful delete.
1896 */
1897static
1898void i40e_aqc_del_filters(struct i40e_vsi *vsi, const char *vsi_name,
1899 struct i40e_aqc_remove_macvlan_element_data *list,
1900 int num_del, int *retval)
1901{
1902 struct i40e_hw *hw = &vsi->back->hw;
1903 i40e_status aq_ret;
1904 int aq_err;
1905
1906 aq_ret = i40e_aq_remove_macvlan(hw, vsi->seid, list, num_del, NULL);
1907 aq_err = hw->aq.asq_last_status;
1908
1909 /* Explicitly ignore and do not report when firmware returns ENOENT */
1910 if (aq_ret && !(aq_err == I40E_AQ_RC_ENOENT)) {
1911 *retval = -EIO;
1912 dev_info(&vsi->back->pdev->dev,
1913 "ignoring delete macvlan error on %s, err %s, aq_err %s\n",
1914 vsi_name, i40e_stat_str(hw, aq_ret),
1915 i40e_aq_str(hw, aq_err));
1916 }
1917}
1918
1919/**
1920 * i40e_aqc_add_filters - Request firmware to add a set of filters
1921 * @vsi: ptr to the VSI
1922 * @vsi_name: name to display in messages
1923 * @list: the list of filters to send to firmware
1924 * @add_head: Position in the add hlist
1925 * @num_add: the number of filters to add
1926 * @promisc_change: set to true on exit if promiscuous mode was forced on
1927 *
1928 * Send a request to firmware via AdminQ to add a chunk of filters. Will set
1929 * promisc_changed to true if the firmware has run out of space for more
1930 * filters.
1931 */
1932static
1933void i40e_aqc_add_filters(struct i40e_vsi *vsi, const char *vsi_name,
1934 struct i40e_aqc_add_macvlan_element_data *list,
Jacob Keller671889e2016-12-02 12:33:00 -08001935 struct i40e_new_mac_filter *add_head,
Jacob Keller00936312016-10-05 09:30:41 -07001936 int num_add, bool *promisc_changed)
1937{
1938 struct i40e_hw *hw = &vsi->back->hw;
Jacob Keller00936312016-10-05 09:30:41 -07001939 int aq_err, fcnt;
1940
Jacob Kellerac9e2392016-11-11 12:39:27 -08001941 i40e_aq_add_macvlan(hw, vsi->seid, list, num_add, NULL);
Jacob Keller00936312016-10-05 09:30:41 -07001942 aq_err = hw->aq.asq_last_status;
Jacob Kellerac9e2392016-11-11 12:39:27 -08001943 fcnt = i40e_update_filter_state(num_add, list, add_head);
Jacob Keller00936312016-10-05 09:30:41 -07001944
1945 if (fcnt != num_add) {
1946 *promisc_changed = true;
Jacob Keller0da36b92017-04-19 09:25:55 -04001947 set_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
Jacob Keller00936312016-10-05 09:30:41 -07001948 dev_warn(&vsi->back->pdev->dev,
1949 "Error %s adding RX filters on %s, promiscuous mode forced on\n",
1950 i40e_aq_str(hw, aq_err),
1951 vsi_name);
1952 }
1953}
1954
1955/**
Jacob Keller435c0842016-11-08 13:05:10 -08001956 * i40e_aqc_broadcast_filter - Set promiscuous broadcast flags
1957 * @vsi: pointer to the VSI
1958 * @f: filter data
1959 *
1960 * This function sets or clears the promiscuous broadcast flags for VLAN
1961 * filters in order to properly receive broadcast frames. Assumes that only
1962 * broadcast filters are passed.
Jacob Keller671889e2016-12-02 12:33:00 -08001963 *
1964 * Returns status indicating success or failure;
Jacob Keller435c0842016-11-08 13:05:10 -08001965 **/
Jacob Keller671889e2016-12-02 12:33:00 -08001966static i40e_status
1967i40e_aqc_broadcast_filter(struct i40e_vsi *vsi, const char *vsi_name,
1968 struct i40e_mac_filter *f)
Jacob Keller435c0842016-11-08 13:05:10 -08001969{
1970 bool enable = f->state == I40E_FILTER_NEW;
1971 struct i40e_hw *hw = &vsi->back->hw;
1972 i40e_status aq_ret;
1973
1974 if (f->vlan == I40E_VLAN_ANY) {
1975 aq_ret = i40e_aq_set_vsi_broadcast(hw,
1976 vsi->seid,
1977 enable,
1978 NULL);
1979 } else {
1980 aq_ret = i40e_aq_set_vsi_bc_promisc_on_vlan(hw,
1981 vsi->seid,
1982 enable,
1983 f->vlan,
1984 NULL);
1985 }
1986
Jacob Keller671889e2016-12-02 12:33:00 -08001987 if (aq_ret)
Jacob Keller435c0842016-11-08 13:05:10 -08001988 dev_warn(&vsi->back->pdev->dev,
1989 "Error %s setting broadcast promiscuous mode on %s\n",
1990 i40e_aq_str(hw, hw->aq.asq_last_status),
1991 vsi_name);
Jacob Keller671889e2016-12-02 12:33:00 -08001992
1993 return aq_ret;
Jacob Keller435c0842016-11-08 13:05:10 -08001994}
1995
1996/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001997 * i40e_sync_vsi_filters - Update the VSI filter list to the HW
1998 * @vsi: ptr to the VSI
1999 *
2000 * Push any outstanding VSI filter changes through the AdminQ.
2001 *
2002 * Returns 0 or error value
2003 **/
Jesse Brandeburg17652c62015-11-05 17:01:02 -08002004int i40e_sync_vsi_filters(struct i40e_vsi *vsi)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002005{
Jacob Keller278e7d02016-10-05 09:30:37 -07002006 struct hlist_head tmp_add_list, tmp_del_list;
Jacob Keller671889e2016-12-02 12:33:00 -08002007 struct i40e_mac_filter *f;
2008 struct i40e_new_mac_filter *new, *add_head = NULL;
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002009 struct i40e_hw *hw = &vsi->back->hw;
Jacob Keller38326212016-11-11 12:39:26 -08002010 unsigned int failed_filters = 0;
Alan Brady84f5ca62016-10-05 09:30:39 -07002011 unsigned int vlan_filters = 0;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002012 bool promisc_changed = false;
Shannon Nelson2d1de822016-05-16 10:26:44 -07002013 char vsi_name[16] = "PF";
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002014 int filter_list_len = 0;
Mitch Williamsea02e902015-11-09 15:35:50 -08002015 i40e_status aq_ret = 0;
Alan Brady84f5ca62016-10-05 09:30:39 -07002016 u32 changed_flags = 0;
Jacob Keller278e7d02016-10-05 09:30:37 -07002017 struct hlist_node *h;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002018 struct i40e_pf *pf;
2019 int num_add = 0;
2020 int num_del = 0;
Alan Brady84f5ca62016-10-05 09:30:39 -07002021 int retval = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002022 u16 cmd_flags;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002023 int list_size;
Jacob Keller278e7d02016-10-05 09:30:37 -07002024 int bkt;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002025
2026 /* empty array typed pointers, kcalloc later */
2027 struct i40e_aqc_add_macvlan_element_data *add_list;
2028 struct i40e_aqc_remove_macvlan_element_data *del_list;
2029
Jacob Keller0da36b92017-04-19 09:25:55 -04002030 while (test_and_set_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002031 usleep_range(1000, 2000);
2032 pf = vsi->back;
2033
2034 if (vsi->netdev) {
2035 changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
2036 vsi->current_netdev_flags = vsi->netdev->flags;
2037 }
2038
Jacob Keller278e7d02016-10-05 09:30:37 -07002039 INIT_HLIST_HEAD(&tmp_add_list);
2040 INIT_HLIST_HEAD(&tmp_del_list);
Kiran Patil21659032015-09-30 14:09:03 -04002041
Shannon Nelson2d1de822016-05-16 10:26:44 -07002042 if (vsi->type == I40E_VSI_SRIOV)
2043 snprintf(vsi_name, sizeof(vsi_name) - 1, "VF %d", vsi->vf_id);
2044 else if (vsi->type != I40E_VSI_MAIN)
2045 snprintf(vsi_name, sizeof(vsi_name) - 1, "vsi %d", vsi->seid);
2046
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002047 if (vsi->flags & I40E_VSI_FLAG_FILTER_CHANGED) {
2048 vsi->flags &= ~I40E_VSI_FLAG_FILTER_CHANGED;
2049
Jacob Keller278e7d02016-10-05 09:30:37 -07002050 spin_lock_bh(&vsi->mac_filter_hash_lock);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002051 /* Create a list of filters to delete. */
Jacob Keller278e7d02016-10-05 09:30:37 -07002052 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002053 if (f->state == I40E_FILTER_REMOVE) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002054 /* Move the element into temporary del_list */
Jacob Keller278e7d02016-10-05 09:30:37 -07002055 hash_del(&f->hlist);
2056 hlist_add_head(&f->hlist, &tmp_del_list);
Alan Brady84f5ca62016-10-05 09:30:39 -07002057
2058 /* Avoid counting removed filters */
2059 continue;
Kiran Patil21659032015-09-30 14:09:03 -04002060 }
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002061 if (f->state == I40E_FILTER_NEW) {
Jacob Keller671889e2016-12-02 12:33:00 -08002062 /* Create a temporary i40e_new_mac_filter */
2063 new = kzalloc(sizeof(*new), GFP_ATOMIC);
2064 if (!new)
2065 goto err_no_memory_locked;
2066
2067 /* Store pointer to the real filter */
2068 new->f = f;
2069 new->state = f->state;
2070
2071 /* Add it to the hash list */
2072 hlist_add_head(&new->hlist, &tmp_add_list);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002073 }
Alan Brady84f5ca62016-10-05 09:30:39 -07002074
Jacob Keller489a3262016-11-11 12:39:31 -08002075 /* Count the number of active (current and new) VLAN
2076 * filters we have now. Does not count filters which
2077 * are marked for deletion.
Alan Brady84f5ca62016-10-05 09:30:39 -07002078 */
2079 if (f->vlan > 0)
2080 vlan_filters++;
Alan Brady84f5ca62016-10-05 09:30:39 -07002081 }
2082
Jacob Keller489a3262016-11-11 12:39:31 -08002083 retval = i40e_correct_mac_vlan_filters(vsi,
2084 &tmp_add_list,
2085 &tmp_del_list,
2086 vlan_filters);
2087 if (retval)
2088 goto err_no_memory_locked;
Alan Brady84f5ca62016-10-05 09:30:39 -07002089
Jacob Keller278e7d02016-10-05 09:30:37 -07002090 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -04002091 }
2092
2093 /* Now process 'del_list' outside the lock */
Jacob Keller278e7d02016-10-05 09:30:37 -07002094 if (!hlist_empty(&tmp_del_list)) {
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002095 filter_list_len = hw->aq.asq_buf_size /
Kiran Patil21659032015-09-30 14:09:03 -04002096 sizeof(struct i40e_aqc_remove_macvlan_element_data);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002097 list_size = filter_list_len *
Shannon Nelsonf1199992015-11-19 11:34:23 -08002098 sizeof(struct i40e_aqc_remove_macvlan_element_data);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002099 del_list = kzalloc(list_size, GFP_ATOMIC);
Jacob Keller4a2ce272016-10-05 09:30:38 -07002100 if (!del_list)
2101 goto err_no_memory;
Kiran Patil21659032015-09-30 14:09:03 -04002102
Jacob Keller278e7d02016-10-05 09:30:37 -07002103 hlist_for_each_entry_safe(f, h, &tmp_del_list, hlist) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002104 cmd_flags = 0;
2105
Jacob Keller435c0842016-11-08 13:05:10 -08002106 /* handle broadcast filters by updating the broadcast
Jacob Kellerd88d40b2016-12-02 12:32:59 -08002107 * promiscuous flag and release filter list.
Jacob Keller435c0842016-11-08 13:05:10 -08002108 */
2109 if (is_broadcast_ether_addr(f->macaddr)) {
2110 i40e_aqc_broadcast_filter(vsi, vsi_name, f);
2111
2112 hlist_del(&f->hlist);
2113 kfree(f);
2114 continue;
2115 }
2116
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002117 /* add to delete list */
Greg Rose9a173902014-05-22 06:32:02 +00002118 ether_addr_copy(del_list[num_del].mac_addr, f->macaddr);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002119 if (f->vlan == I40E_VLAN_ANY) {
2120 del_list[num_del].vlan_tag = 0;
Alan Bradya6cb9142016-09-06 18:05:07 -07002121 cmd_flags |= I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002122 } else {
2123 del_list[num_del].vlan_tag =
2124 cpu_to_le16((u16)(f->vlan));
2125 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002126
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002127 cmd_flags |= I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
2128 del_list[num_del].flags = cmd_flags;
2129 num_del++;
2130
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002131 /* flush a full buffer */
2132 if (num_del == filter_list_len) {
Jacob Keller00936312016-10-05 09:30:41 -07002133 i40e_aqc_del_filters(vsi, vsi_name, del_list,
2134 num_del, &retval);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002135 memset(del_list, 0, list_size);
Jacob Keller00936312016-10-05 09:30:41 -07002136 num_del = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002137 }
Kiran Patil21659032015-09-30 14:09:03 -04002138 /* Release memory for MAC filter entries which were
2139 * synced up with HW.
2140 */
Jacob Keller278e7d02016-10-05 09:30:37 -07002141 hlist_del(&f->hlist);
Kiran Patil21659032015-09-30 14:09:03 -04002142 kfree(f);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002143 }
Kiran Patil21659032015-09-30 14:09:03 -04002144
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002145 if (num_del) {
Jacob Keller00936312016-10-05 09:30:41 -07002146 i40e_aqc_del_filters(vsi, vsi_name, del_list,
2147 num_del, &retval);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002148 }
2149
2150 kfree(del_list);
2151 del_list = NULL;
Kiran Patil21659032015-09-30 14:09:03 -04002152 }
2153
Jacob Keller278e7d02016-10-05 09:30:37 -07002154 if (!hlist_empty(&tmp_add_list)) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002155 /* Do all the adds now. */
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002156 filter_list_len = hw->aq.asq_buf_size /
Shannon Nelsonf1199992015-11-19 11:34:23 -08002157 sizeof(struct i40e_aqc_add_macvlan_element_data);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002158 list_size = filter_list_len *
2159 sizeof(struct i40e_aqc_add_macvlan_element_data);
2160 add_list = kzalloc(list_size, GFP_ATOMIC);
Jacob Keller4a2ce272016-10-05 09:30:38 -07002161 if (!add_list)
2162 goto err_no_memory;
2163
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002164 num_add = 0;
Jacob Keller671889e2016-12-02 12:33:00 -08002165 hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
Jacob Kellerd19cb642017-04-21 13:38:05 -07002166 if (test_bit(__I40E_VSI_OVERFLOW_PROMISC,
Jacob Keller0da36b92017-04-19 09:25:55 -04002167 vsi->state)) {
Jacob Keller671889e2016-12-02 12:33:00 -08002168 new->state = I40E_FILTER_FAILED;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002169 continue;
2170 }
Jacob Keller435c0842016-11-08 13:05:10 -08002171
2172 /* handle broadcast filters by updating the broadcast
2173 * promiscuous flag instead of adding a MAC filter.
2174 */
Jacob Keller671889e2016-12-02 12:33:00 -08002175 if (is_broadcast_ether_addr(new->f->macaddr)) {
2176 if (i40e_aqc_broadcast_filter(vsi, vsi_name,
2177 new->f))
2178 new->state = I40E_FILTER_FAILED;
2179 else
2180 new->state = I40E_FILTER_ACTIVE;
Jacob Keller435c0842016-11-08 13:05:10 -08002181 continue;
2182 }
2183
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002184 /* add to add array */
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002185 if (num_add == 0)
Jacob Keller671889e2016-12-02 12:33:00 -08002186 add_head = new;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002187 cmd_flags = 0;
Jacob Keller671889e2016-12-02 12:33:00 -08002188 ether_addr_copy(add_list[num_add].mac_addr,
2189 new->f->macaddr);
2190 if (new->f->vlan == I40E_VLAN_ANY) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002191 add_list[num_add].vlan_tag = 0;
2192 cmd_flags |= I40E_AQC_MACVLAN_ADD_IGNORE_VLAN;
2193 } else {
2194 add_list[num_add].vlan_tag =
Jacob Keller671889e2016-12-02 12:33:00 -08002195 cpu_to_le16((u16)(new->f->vlan));
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002196 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002197 add_list[num_add].queue_number = 0;
Jacob Kellerac9e2392016-11-11 12:39:27 -08002198 /* set invalid match method for later detection */
Keller, Jacob E0266ac42016-12-09 13:39:21 -08002199 add_list[num_add].match_method = I40E_AQC_MM_ERR_NO_RES;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002200 cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002201 add_list[num_add].flags = cpu_to_le16(cmd_flags);
2202 num_add++;
2203
2204 /* flush a full buffer */
2205 if (num_add == filter_list_len) {
Jacob Keller00936312016-10-05 09:30:41 -07002206 i40e_aqc_add_filters(vsi, vsi_name, add_list,
2207 add_head, num_add,
2208 &promisc_changed);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002209 memset(add_list, 0, list_size);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002210 num_add = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002211 }
2212 }
2213 if (num_add) {
Jacob Keller00936312016-10-05 09:30:41 -07002214 i40e_aqc_add_filters(vsi, vsi_name, add_list, add_head,
2215 num_add, &promisc_changed);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002216 }
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002217 /* Now move all of the filters from the temp add list back to
2218 * the VSI's list.
2219 */
Jacob Keller278e7d02016-10-05 09:30:37 -07002220 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller671889e2016-12-02 12:33:00 -08002221 hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
2222 /* Only update the state if we're still NEW */
2223 if (new->f->state == I40E_FILTER_NEW)
2224 new->f->state = new->state;
2225 hlist_del(&new->hlist);
2226 kfree(new);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002227 }
Jacob Keller278e7d02016-10-05 09:30:37 -07002228 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002229 kfree(add_list);
2230 add_list = NULL;
2231 }
2232
Jacob Keller38326212016-11-11 12:39:26 -08002233 /* Determine the number of active and failed filters. */
2234 spin_lock_bh(&vsi->mac_filter_hash_lock);
2235 vsi->active_filters = 0;
2236 hash_for_each(vsi->mac_filter_hash, bkt, f, hlist) {
2237 if (f->state == I40E_FILTER_ACTIVE)
2238 vsi->active_filters++;
2239 else if (f->state == I40E_FILTER_FAILED)
2240 failed_filters++;
2241 }
2242 spin_unlock_bh(&vsi->mac_filter_hash_lock);
2243
2244 /* If promiscuous mode has changed, we need to calculate a new
2245 * threshold for when we are safe to exit
2246 */
2247 if (promisc_changed)
2248 vsi->promisc_threshold = (vsi->active_filters * 3) / 4;
2249
2250 /* Check if we are able to exit overflow promiscuous mode. We can
2251 * safely exit if we didn't just enter, we no longer have any failed
2252 * filters, and we have reduced filters below the threshold value.
2253 */
Jacob Keller0da36b92017-04-19 09:25:55 -04002254 if (test_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state) &&
Jacob Keller38326212016-11-11 12:39:26 -08002255 !promisc_changed && !failed_filters &&
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002256 (vsi->active_filters < vsi->promisc_threshold)) {
Jacob Keller38326212016-11-11 12:39:26 -08002257 dev_info(&pf->pdev->dev,
2258 "filter logjam cleared on %s, leaving overflow promiscuous mode\n",
2259 vsi_name);
Jacob Keller0da36b92017-04-19 09:25:55 -04002260 clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
Jacob Keller38326212016-11-11 12:39:26 -08002261 promisc_changed = true;
2262 vsi->promisc_threshold = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002263 }
2264
Anjali Singhai Jaina856b5c2016-04-13 03:08:23 -07002265 /* if the VF is not trusted do not do promisc */
2266 if ((vsi->type == I40E_VSI_SRIOV) && !pf->vf[vsi->vf_id].trusted) {
Jacob Keller0da36b92017-04-19 09:25:55 -04002267 clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
Anjali Singhai Jaina856b5c2016-04-13 03:08:23 -07002268 goto out;
2269 }
2270
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002271 /* check for changes in promiscuous modes */
2272 if (changed_flags & IFF_ALLMULTI) {
2273 bool cur_multipromisc;
Jesse Brandeburg6995b362015-08-28 17:55:54 -04002274
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002275 cur_multipromisc = !!(vsi->current_netdev_flags & IFF_ALLMULTI);
Mitch Williamsea02e902015-11-09 15:35:50 -08002276 aq_ret = i40e_aq_set_vsi_multicast_promiscuous(&vsi->back->hw,
2277 vsi->seid,
2278 cur_multipromisc,
2279 NULL);
2280 if (aq_ret) {
2281 retval = i40e_aq_rc_to_posix(aq_ret,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002282 hw->aq.asq_last_status);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002283 dev_info(&pf->pdev->dev,
Shannon Nelson2d1de822016-05-16 10:26:44 -07002284 "set multi promisc failed on %s, err %s aq_err %s\n",
2285 vsi_name,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002286 i40e_stat_str(hw, aq_ret),
2287 i40e_aq_str(hw, hw->aq.asq_last_status));
Mitch Williamsea02e902015-11-09 15:35:50 -08002288 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002289 }
Alan Bradye5887232017-06-07 05:43:10 -04002290
2291 if ((changed_flags & IFF_PROMISC) || promisc_changed) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002292 bool cur_promisc;
Jesse Brandeburg6995b362015-08-28 17:55:54 -04002293
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002294 cur_promisc = (!!(vsi->current_netdev_flags & IFF_PROMISC) ||
Jacob Kellerd19cb642017-04-21 13:38:05 -07002295 test_bit(__I40E_VSI_OVERFLOW_PROMISC,
Jacob Keller0da36b92017-04-19 09:25:55 -04002296 vsi->state));
Anjali Singhai Jain6784ed52016-01-15 14:33:13 -08002297 if ((vsi->type == I40E_VSI_MAIN) &&
2298 (pf->lan_veb != I40E_NO_VEB) &&
2299 !(pf->flags & I40E_FLAG_MFP_ENABLED)) {
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002300 /* set defport ON for Main VSI instead of true promisc
2301 * this way we will get all unicast/multicast and VLAN
2302 * promisc behavior but will not get VF or VMDq traffic
2303 * replicated on the Main VSI.
2304 */
2305 if (pf->cur_promisc != cur_promisc) {
2306 pf->cur_promisc = cur_promisc;
Mitch Williams5bc16032016-05-16 10:26:43 -07002307 if (cur_promisc)
2308 aq_ret =
2309 i40e_aq_set_default_vsi(hw,
2310 vsi->seid,
2311 NULL);
2312 else
2313 aq_ret =
2314 i40e_aq_clear_default_vsi(hw,
2315 vsi->seid,
2316 NULL);
2317 if (aq_ret) {
2318 retval = i40e_aq_rc_to_posix(aq_ret,
2319 hw->aq.asq_last_status);
2320 dev_info(&pf->pdev->dev,
Shannon Nelson2d1de822016-05-16 10:26:44 -07002321 "Set default VSI failed on %s, err %s, aq_err %s\n",
2322 vsi_name,
Mitch Williams5bc16032016-05-16 10:26:43 -07002323 i40e_stat_str(hw, aq_ret),
2324 i40e_aq_str(hw,
2325 hw->aq.asq_last_status));
2326 }
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002327 }
2328 } else {
Mitch Williamsea02e902015-11-09 15:35:50 -08002329 aq_ret = i40e_aq_set_vsi_unicast_promiscuous(
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002330 hw,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002331 vsi->seid,
Anjali Singhai Jainb5569892016-05-03 15:13:12 -07002332 cur_promisc, NULL,
2333 true);
Mitch Williamsea02e902015-11-09 15:35:50 -08002334 if (aq_ret) {
2335 retval =
2336 i40e_aq_rc_to_posix(aq_ret,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002337 hw->aq.asq_last_status);
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002338 dev_info(&pf->pdev->dev,
Shannon Nelson2d1de822016-05-16 10:26:44 -07002339 "set unicast promisc failed on %s, err %s, aq_err %s\n",
2340 vsi_name,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002341 i40e_stat_str(hw, aq_ret),
2342 i40e_aq_str(hw,
2343 hw->aq.asq_last_status));
Mitch Williamsea02e902015-11-09 15:35:50 -08002344 }
2345 aq_ret = i40e_aq_set_vsi_multicast_promiscuous(
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002346 hw,
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002347 vsi->seid,
2348 cur_promisc, NULL);
Mitch Williamsea02e902015-11-09 15:35:50 -08002349 if (aq_ret) {
2350 retval =
2351 i40e_aq_rc_to_posix(aq_ret,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002352 hw->aq.asq_last_status);
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002353 dev_info(&pf->pdev->dev,
Shannon Nelson2d1de822016-05-16 10:26:44 -07002354 "set multicast promisc failed on %s, err %s, aq_err %s\n",
2355 vsi_name,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002356 i40e_stat_str(hw, aq_ret),
2357 i40e_aq_str(hw,
2358 hw->aq.asq_last_status));
Mitch Williamsea02e902015-11-09 15:35:50 -08002359 }
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002360 }
Mitch Williamsea02e902015-11-09 15:35:50 -08002361 aq_ret = i40e_aq_set_vsi_broadcast(&vsi->back->hw,
2362 vsi->seid,
2363 cur_promisc, NULL);
2364 if (aq_ret) {
2365 retval = i40e_aq_rc_to_posix(aq_ret,
2366 pf->hw.aq.asq_last_status);
Greg Rose1a103702013-11-28 06:42:39 +00002367 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002368 "set brdcast promisc failed, err %s, aq_err %s\n",
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002369 i40e_stat_str(hw, aq_ret),
2370 i40e_aq_str(hw,
2371 hw->aq.asq_last_status));
Mitch Williamsea02e902015-11-09 15:35:50 -08002372 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002373 }
Mitch Williamsea02e902015-11-09 15:35:50 -08002374out:
Jesse Brandeburg2818ccd2016-01-13 16:51:38 -08002375 /* if something went wrong then set the changed flag so we try again */
2376 if (retval)
2377 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2378
Jacob Keller0da36b92017-04-19 09:25:55 -04002379 clear_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state);
Mitch Williamsea02e902015-11-09 15:35:50 -08002380 return retval;
Jacob Keller4a2ce272016-10-05 09:30:38 -07002381
2382err_no_memory:
2383 /* Restore elements on the temporary add and delete lists */
2384 spin_lock_bh(&vsi->mac_filter_hash_lock);
Alan Brady84f5ca62016-10-05 09:30:39 -07002385err_no_memory_locked:
Jacob Keller671889e2016-12-02 12:33:00 -08002386 i40e_undo_del_filter_entries(vsi, &tmp_del_list);
2387 i40e_undo_add_filter_entries(vsi, &tmp_add_list);
Jacob Keller4a2ce272016-10-05 09:30:38 -07002388 spin_unlock_bh(&vsi->mac_filter_hash_lock);
2389
2390 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
Jacob Keller0da36b92017-04-19 09:25:55 -04002391 clear_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state);
Jacob Keller4a2ce272016-10-05 09:30:38 -07002392 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002393}
2394
2395/**
2396 * i40e_sync_filters_subtask - Sync the VSI filter list with HW
2397 * @pf: board private structure
2398 **/
2399static void i40e_sync_filters_subtask(struct i40e_pf *pf)
2400{
2401 int v;
2402
2403 if (!pf || !(pf->flags & I40E_FLAG_FILTER_SYNC))
2404 return;
2405 pf->flags &= ~I40E_FLAG_FILTER_SYNC;
2406
Mitch Williams505682c2014-05-20 08:01:37 +00002407 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002408 if (pf->vsi[v] &&
Jesse Brandeburg17652c62015-11-05 17:01:02 -08002409 (pf->vsi[v]->flags & I40E_VSI_FLAG_FILTER_CHANGED)) {
2410 int ret = i40e_sync_vsi_filters(pf->vsi[v]);
2411
2412 if (ret) {
2413 /* come back and try again later */
2414 pf->flags |= I40E_FLAG_FILTER_SYNC;
2415 break;
2416 }
2417 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002418 }
2419}
2420
2421/**
Björn Töpel0c8493d2017-05-24 07:55:34 +02002422 * i40e_max_xdp_frame_size - returns the maximum allowed frame size for XDP
2423 * @vsi: the vsi
2424 **/
2425static int i40e_max_xdp_frame_size(struct i40e_vsi *vsi)
2426{
2427 if (PAGE_SIZE >= 8192 || (vsi->back->flags & I40E_FLAG_LEGACY_RX))
2428 return I40E_RXBUFFER_2048;
2429 else
2430 return I40E_RXBUFFER_3072;
2431}
2432
2433/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002434 * i40e_change_mtu - NDO callback to change the Maximum Transfer Unit
2435 * @netdev: network interface device structure
2436 * @new_mtu: new value for maximum frame size
2437 *
2438 * Returns 0 on success, negative on failure
2439 **/
2440static int i40e_change_mtu(struct net_device *netdev, int new_mtu)
2441{
2442 struct i40e_netdev_priv *np = netdev_priv(netdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002443 struct i40e_vsi *vsi = np->vsi;
Mitch Williams0ef2d5a2017-01-24 10:24:00 -08002444 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002445
Björn Töpel0c8493d2017-05-24 07:55:34 +02002446 if (i40e_enabled_xdp_vsi(vsi)) {
2447 int frame_size = new_mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
2448
2449 if (frame_size > i40e_max_xdp_frame_size(vsi))
2450 return -EINVAL;
2451 }
2452
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002453 netdev_info(netdev, "changing MTU from %d to %d\n",
2454 netdev->mtu, new_mtu);
2455 netdev->mtu = new_mtu;
2456 if (netif_running(netdev))
2457 i40e_vsi_reinit_locked(vsi);
Mitch Williams0ef2d5a2017-01-24 10:24:00 -08002458 pf->flags |= (I40E_FLAG_SERVICE_CLIENT_REQUESTED |
2459 I40E_FLAG_CLIENT_L2_CHANGE);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002460 return 0;
2461}
2462
2463/**
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00002464 * i40e_ioctl - Access the hwtstamp interface
2465 * @netdev: network interface device structure
2466 * @ifr: interface request data
2467 * @cmd: ioctl command
2468 **/
2469int i40e_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
2470{
2471 struct i40e_netdev_priv *np = netdev_priv(netdev);
2472 struct i40e_pf *pf = np->vsi->back;
2473
2474 switch (cmd) {
2475 case SIOCGHWTSTAMP:
2476 return i40e_ptp_get_ts_config(pf, ifr);
2477 case SIOCSHWTSTAMP:
2478 return i40e_ptp_set_ts_config(pf, ifr);
2479 default:
2480 return -EOPNOTSUPP;
2481 }
2482}
2483
2484/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002485 * i40e_vlan_stripping_enable - Turn on vlan stripping for the VSI
2486 * @vsi: the vsi being adjusted
2487 **/
2488void i40e_vlan_stripping_enable(struct i40e_vsi *vsi)
2489{
2490 struct i40e_vsi_context ctxt;
2491 i40e_status ret;
2492
2493 if ((vsi->info.valid_sections &
2494 cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
2495 ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_MODE_MASK) == 0))
2496 return; /* already enabled */
2497
2498 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2499 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
2500 I40E_AQ_VSI_PVLAN_EMOD_STR_BOTH;
2501
2502 ctxt.seid = vsi->seid;
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07002503 ctxt.info = vsi->info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002504 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2505 if (ret) {
2506 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002507 "update vlan stripping failed, err %s aq_err %s\n",
2508 i40e_stat_str(&vsi->back->hw, ret),
2509 i40e_aq_str(&vsi->back->hw,
2510 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002511 }
2512}
2513
2514/**
2515 * i40e_vlan_stripping_disable - Turn off vlan stripping for the VSI
2516 * @vsi: the vsi being adjusted
2517 **/
2518void i40e_vlan_stripping_disable(struct i40e_vsi *vsi)
2519{
2520 struct i40e_vsi_context ctxt;
2521 i40e_status ret;
2522
2523 if ((vsi->info.valid_sections &
2524 cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
2525 ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_EMOD_MASK) ==
2526 I40E_AQ_VSI_PVLAN_EMOD_MASK))
2527 return; /* already disabled */
2528
2529 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2530 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
2531 I40E_AQ_VSI_PVLAN_EMOD_NOTHING;
2532
2533 ctxt.seid = vsi->seid;
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07002534 ctxt.info = vsi->info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002535 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2536 if (ret) {
2537 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002538 "update vlan stripping failed, err %s aq_err %s\n",
2539 i40e_stat_str(&vsi->back->hw, ret),
2540 i40e_aq_str(&vsi->back->hw,
2541 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002542 }
2543}
2544
2545/**
2546 * i40e_vlan_rx_register - Setup or shutdown vlan offload
2547 * @netdev: network interface to be adjusted
2548 * @features: netdev features to test if VLAN offload is enabled or not
2549 **/
2550static void i40e_vlan_rx_register(struct net_device *netdev, u32 features)
2551{
2552 struct i40e_netdev_priv *np = netdev_priv(netdev);
2553 struct i40e_vsi *vsi = np->vsi;
2554
2555 if (features & NETIF_F_HW_VLAN_CTAG_RX)
2556 i40e_vlan_stripping_enable(vsi);
2557 else
2558 i40e_vlan_stripping_disable(vsi);
2559}
2560
2561/**
Jacob Keller490a4ad2016-11-11 12:39:29 -08002562 * i40e_add_vlan_all_mac - Add a MAC/VLAN filter for each existing MAC address
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002563 * @vsi: the vsi being configured
2564 * @vid: vlan id to be added (0 = untagged only , -1 = any)
Jacob Keller490a4ad2016-11-11 12:39:29 -08002565 *
2566 * This is a helper function for adding a new MAC/VLAN filter with the
2567 * specified VLAN for each existing MAC address already in the hash table.
2568 * This function does *not* perform any accounting to update filters based on
2569 * VLAN mode.
2570 *
2571 * NOTE: this function expects to be called while under the
2572 * mac_filter_hash_lock
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002573 **/
Jacob Keller9af52f62016-11-11 12:39:30 -08002574int i40e_add_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002575{
Jacob Keller490a4ad2016-11-11 12:39:29 -08002576 struct i40e_mac_filter *f, *add_f;
Jacob Keller278e7d02016-10-05 09:30:37 -07002577 struct hlist_node *h;
2578 int bkt;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002579
Jacob Keller278e7d02016-10-05 09:30:37 -07002580 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Jacob Keller57b341d2016-10-05 09:30:35 -07002581 if (f->state == I40E_FILTER_REMOVE)
2582 continue;
Jacob Keller1bc87e82016-10-05 09:30:31 -07002583 add_f = i40e_add_filter(vsi, f->macaddr, vid);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002584 if (!add_f) {
2585 dev_info(&vsi->back->pdev->dev,
2586 "Could not add vlan filter %d for %pM\n",
2587 vid, f->macaddr);
2588 return -ENOMEM;
2589 }
2590 }
2591
Jacob Keller490a4ad2016-11-11 12:39:29 -08002592 return 0;
2593}
2594
2595/**
2596 * i40e_vsi_add_vlan - Add VSI membership for given VLAN
2597 * @vsi: the VSI being configured
Jacob Kellerf94484b2016-12-07 14:05:34 -08002598 * @vid: VLAN id to be added
Jacob Keller490a4ad2016-11-11 12:39:29 -08002599 **/
Jacob Kellerf94484b2016-12-07 14:05:34 -08002600int i40e_vsi_add_vlan(struct i40e_vsi *vsi, u16 vid)
Jacob Keller490a4ad2016-11-11 12:39:29 -08002601{
Jacob Keller489a3262016-11-11 12:39:31 -08002602 int err;
Jacob Keller490a4ad2016-11-11 12:39:29 -08002603
Jacob Kellerfcf6cfc2017-06-23 04:24:51 -04002604 if (vsi->info.pvid)
Jacob Kellerf94484b2016-12-07 14:05:34 -08002605 return -EINVAL;
2606
Jacob Kellerfcf6cfc2017-06-23 04:24:51 -04002607 /* The network stack will attempt to add VID=0, with the intention to
2608 * receive priority tagged packets with a VLAN of 0. Our HW receives
2609 * these packets by default when configured to receive untagged
2610 * packets, so we don't need to add a filter for this case.
2611 * Additionally, HW interprets adding a VID=0 filter as meaning to
2612 * receive *only* tagged traffic and stops receiving untagged traffic.
2613 * Thus, we do not want to actually add a filter for VID=0
2614 */
2615 if (!vid)
2616 return 0;
2617
Jacob Keller490a4ad2016-11-11 12:39:29 -08002618 /* Locked once because all functions invoked below iterates list*/
2619 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller490a4ad2016-11-11 12:39:29 -08002620 err = i40e_add_vlan_all_mac(vsi, vid);
Jacob Keller278e7d02016-10-05 09:30:37 -07002621 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller489a3262016-11-11 12:39:31 -08002622 if (err)
2623 return err;
Kiran Patil21659032015-09-30 14:09:03 -04002624
Jesse Brandeburg0e4425e2015-11-05 17:01:01 -08002625 /* schedule our worker thread which will take care of
2626 * applying the new filter changes
2627 */
2628 i40e_service_event_schedule(vsi->back);
2629 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002630}
2631
2632/**
Jacob Keller490a4ad2016-11-11 12:39:29 -08002633 * i40e_rm_vlan_all_mac - Remove MAC/VLAN pair for all MAC with the given VLAN
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002634 * @vsi: the vsi being configured
2635 * @vid: vlan id to be removed (0 = untagged only , -1 = any)
Jacob Keller490a4ad2016-11-11 12:39:29 -08002636 *
2637 * This function should be used to remove all VLAN filters which match the
2638 * given VID. It does not schedule the service event and does not take the
2639 * mac_filter_hash_lock so it may be combined with other operations under
2640 * a single invocation of the mac_filter_hash_lock.
2641 *
2642 * NOTE: this function expects to be called while under the
2643 * mac_filter_hash_lock
2644 */
Jacob Keller9af52f62016-11-11 12:39:30 -08002645void i40e_rm_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002646{
Alan Brady84f5ca62016-10-05 09:30:39 -07002647 struct i40e_mac_filter *f;
Jacob Keller278e7d02016-10-05 09:30:37 -07002648 struct hlist_node *h;
Jacob Keller278e7d02016-10-05 09:30:37 -07002649 int bkt;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002650
Jacob Keller278e7d02016-10-05 09:30:37 -07002651 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Jacob Keller290d2552016-10-05 09:30:36 -07002652 if (f->vlan == vid)
2653 __i40e_del_filter(vsi, f);
2654 }
Jacob Keller490a4ad2016-11-11 12:39:29 -08002655}
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002656
Jacob Keller490a4ad2016-11-11 12:39:29 -08002657/**
2658 * i40e_vsi_kill_vlan - Remove VSI membership for given VLAN
2659 * @vsi: the VSI being configured
Jacob Kellerf94484b2016-12-07 14:05:34 -08002660 * @vid: VLAN id to be removed
Jacob Keller490a4ad2016-11-11 12:39:29 -08002661 **/
Jacob Kellerf94484b2016-12-07 14:05:34 -08002662void i40e_vsi_kill_vlan(struct i40e_vsi *vsi, u16 vid)
Jacob Keller490a4ad2016-11-11 12:39:29 -08002663{
Jacob Kellerf94484b2016-12-07 14:05:34 -08002664 if (!vid || vsi->info.pvid)
2665 return;
2666
Jacob Keller490a4ad2016-11-11 12:39:29 -08002667 spin_lock_bh(&vsi->mac_filter_hash_lock);
2668 i40e_rm_vlan_all_mac(vsi, vid);
Jacob Keller278e7d02016-10-05 09:30:37 -07002669 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -04002670
Jesse Brandeburg0e4425e2015-11-05 17:01:01 -08002671 /* schedule our worker thread which will take care of
2672 * applying the new filter changes
2673 */
2674 i40e_service_event_schedule(vsi->back);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002675}
2676
2677/**
2678 * i40e_vlan_rx_add_vid - Add a vlan id filter to HW offload
2679 * @netdev: network interface to be adjusted
2680 * @vid: vlan id to be added
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002681 *
2682 * net_device_ops implementation for adding vlan ids
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002683 **/
2684static int i40e_vlan_rx_add_vid(struct net_device *netdev,
2685 __always_unused __be16 proto, u16 vid)
2686{
2687 struct i40e_netdev_priv *np = netdev_priv(netdev);
2688 struct i40e_vsi *vsi = np->vsi;
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002689 int ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002690
Jacob Keller6a1127852016-10-25 16:08:49 -07002691 if (vid >= VLAN_N_VID)
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002692 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002693
Jacob Kellerfcf6cfc2017-06-23 04:24:51 -04002694 ret = i40e_vsi_add_vlan(vsi, vid);
Jacob Keller6a1127852016-10-25 16:08:49 -07002695 if (!ret)
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002696 set_bit(vid, vsi->active_vlans);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002697
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002698 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002699}
2700
2701/**
2702 * i40e_vlan_rx_kill_vid - Remove a vlan id filter from HW offload
2703 * @netdev: network interface to be adjusted
2704 * @vid: vlan id to be removed
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002705 *
Akeem G Abodunrinfdfd9432014-02-11 08:24:15 +00002706 * net_device_ops implementation for removing vlan ids
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002707 **/
2708static int i40e_vlan_rx_kill_vid(struct net_device *netdev,
2709 __always_unused __be16 proto, u16 vid)
2710{
2711 struct i40e_netdev_priv *np = netdev_priv(netdev);
2712 struct i40e_vsi *vsi = np->vsi;
2713
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002714 /* return code is ignored as there is nothing a user
2715 * can do about failure to remove and a log message was
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002716 * already printed from the other function
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002717 */
2718 i40e_vsi_kill_vlan(vsi, vid);
2719
2720 clear_bit(vid, vsi->active_vlans);
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002721
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002722 return 0;
2723}
2724
2725/**
2726 * i40e_restore_vlan - Reinstate vlans when vsi/netdev comes back up
2727 * @vsi: the vsi being brought back up
2728 **/
2729static void i40e_restore_vlan(struct i40e_vsi *vsi)
2730{
2731 u16 vid;
2732
2733 if (!vsi->netdev)
2734 return;
2735
2736 i40e_vlan_rx_register(vsi->netdev, vsi->netdev->features);
2737
2738 for_each_set_bit(vid, vsi->active_vlans, VLAN_N_VID)
2739 i40e_vlan_rx_add_vid(vsi->netdev, htons(ETH_P_8021Q),
2740 vid);
2741}
2742
2743/**
2744 * i40e_vsi_add_pvid - Add pvid for the VSI
2745 * @vsi: the vsi being adjusted
2746 * @vid: the vlan id to set as a PVID
2747 **/
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002748int i40e_vsi_add_pvid(struct i40e_vsi *vsi, u16 vid)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002749{
2750 struct i40e_vsi_context ctxt;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002751 i40e_status ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002752
2753 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2754 vsi->info.pvid = cpu_to_le16(vid);
Greg Rose6c12fcb2013-11-28 06:39:34 +00002755 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_TAGGED |
2756 I40E_AQ_VSI_PVLAN_INSERT_PVID |
Greg Roseb774c7d2013-11-28 06:39:44 +00002757 I40E_AQ_VSI_PVLAN_EMOD_STR;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002758
2759 ctxt.seid = vsi->seid;
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07002760 ctxt.info = vsi->info;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002761 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2762 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002763 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002764 "add pvid failed, err %s aq_err %s\n",
2765 i40e_stat_str(&vsi->back->hw, ret),
2766 i40e_aq_str(&vsi->back->hw,
2767 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002768 return -ENOENT;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002769 }
2770
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002771 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002772}
2773
2774/**
2775 * i40e_vsi_remove_pvid - Remove the pvid from the VSI
2776 * @vsi: the vsi being adjusted
2777 *
2778 * Just use the vlan_rx_register() service to put it back to normal
2779 **/
2780void i40e_vsi_remove_pvid(struct i40e_vsi *vsi)
2781{
Greg Rose6c12fcb2013-11-28 06:39:34 +00002782 i40e_vlan_stripping_disable(vsi);
2783
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002784 vsi->info.pvid = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002785}
2786
2787/**
2788 * i40e_vsi_setup_tx_resources - Allocate VSI Tx queue resources
2789 * @vsi: ptr to the VSI
2790 *
2791 * If this function returns with an error, then it's possible one or
2792 * more of the rings is populated (while the rest are not). It is the
2793 * callers duty to clean those orphaned rings.
2794 *
2795 * Return 0 on success, negative on failure
2796 **/
2797static int i40e_vsi_setup_tx_resources(struct i40e_vsi *vsi)
2798{
2799 int i, err = 0;
2800
2801 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e152013-09-28 06:00:58 +00002802 err = i40e_setup_tx_descriptors(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002803
Björn Töpel74608d12017-05-24 07:55:35 +02002804 if (!i40e_enabled_xdp_vsi(vsi))
2805 return err;
2806
2807 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
2808 err = i40e_setup_tx_descriptors(vsi->xdp_rings[i]);
2809
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002810 return err;
2811}
2812
2813/**
2814 * i40e_vsi_free_tx_resources - Free Tx resources for VSI queues
2815 * @vsi: ptr to the VSI
2816 *
2817 * Free VSI's transmit software resources
2818 **/
2819static void i40e_vsi_free_tx_resources(struct i40e_vsi *vsi)
2820{
2821 int i;
2822
Björn Töpel74608d12017-05-24 07:55:35 +02002823 if (vsi->tx_rings) {
2824 for (i = 0; i < vsi->num_queue_pairs; i++)
2825 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
2826 i40e_free_tx_resources(vsi->tx_rings[i]);
2827 }
Greg Rose8e9dca52013-12-18 13:45:53 +00002828
Björn Töpel74608d12017-05-24 07:55:35 +02002829 if (vsi->xdp_rings) {
2830 for (i = 0; i < vsi->num_queue_pairs; i++)
2831 if (vsi->xdp_rings[i] && vsi->xdp_rings[i]->desc)
2832 i40e_free_tx_resources(vsi->xdp_rings[i]);
2833 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002834}
2835
2836/**
2837 * i40e_vsi_setup_rx_resources - Allocate VSI queues Rx resources
2838 * @vsi: ptr to the VSI
2839 *
2840 * If this function returns with an error, then it's possible one or
2841 * more of the rings is populated (while the rest are not). It is the
2842 * callers duty to clean those orphaned rings.
2843 *
2844 * Return 0 on success, negative on failure
2845 **/
2846static int i40e_vsi_setup_rx_resources(struct i40e_vsi *vsi)
2847{
2848 int i, err = 0;
2849
2850 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e152013-09-28 06:00:58 +00002851 err = i40e_setup_rx_descriptors(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002852 return err;
2853}
2854
2855/**
2856 * i40e_vsi_free_rx_resources - Free Rx Resources for VSI queues
2857 * @vsi: ptr to the VSI
2858 *
2859 * Free all receive software resources
2860 **/
2861static void i40e_vsi_free_rx_resources(struct i40e_vsi *vsi)
2862{
2863 int i;
2864
Greg Rose8e9dca52013-12-18 13:45:53 +00002865 if (!vsi->rx_rings)
2866 return;
2867
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002868 for (i = 0; i < vsi->num_queue_pairs; i++)
Greg Rose8e9dca52013-12-18 13:45:53 +00002869 if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
Alexander Duyck9f65e152013-09-28 06:00:58 +00002870 i40e_free_rx_resources(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002871}
2872
2873/**
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002874 * i40e_config_xps_tx_ring - Configure XPS for a Tx ring
2875 * @ring: The Tx ring to configure
2876 *
2877 * This enables/disables XPS for a given Tx descriptor ring
2878 * based on the TCs enabled for the VSI that ring belongs to.
2879 **/
2880static void i40e_config_xps_tx_ring(struct i40e_ring *ring)
2881{
2882 struct i40e_vsi *vsi = ring->vsi;
Jacob Kellerbe664cb2017-08-29 05:32:31 -04002883 int cpu;
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002884
Jesse Brandeburg9a660ee2015-02-26 16:13:22 +00002885 if (!ring->q_vector || !ring->netdev)
2886 return;
2887
Jacob Kellerba4460d2017-07-14 09:10:08 -04002888 if ((vsi->tc_config.numtc <= 1) &&
Jesse Brandeburgbd6cd4e2017-08-29 05:32:35 -04002889 !test_and_set_bit(__I40E_TX_XPS_INIT_DONE, ring->state)) {
Jacob Kellerbe664cb2017-08-29 05:32:31 -04002890 cpu = cpumask_local_spread(ring->q_vector->v_idx, -1);
2891 netif_set_xps_queue(ring->netdev, get_cpu_mask(cpu),
Jacob Kellerba4460d2017-07-14 09:10:08 -04002892 ring->queue_index);
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002893 }
Jesse Brandeburg0e4425e2015-11-05 17:01:01 -08002894
2895 /* schedule our worker thread which will take care of
2896 * applying the new filter changes
2897 */
2898 i40e_service_event_schedule(vsi->back);
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002899}
2900
2901/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002902 * i40e_configure_tx_ring - Configure a transmit ring context and rest
2903 * @ring: The Tx ring to configure
2904 *
2905 * Configure the Tx descriptor ring in the HMC context.
2906 **/
2907static int i40e_configure_tx_ring(struct i40e_ring *ring)
2908{
2909 struct i40e_vsi *vsi = ring->vsi;
2910 u16 pf_q = vsi->base_queue + ring->queue_index;
2911 struct i40e_hw *hw = &vsi->back->hw;
2912 struct i40e_hmc_obj_txq tx_ctx;
2913 i40e_status err = 0;
2914 u32 qtx_ctl = 0;
2915
2916 /* some ATR related tx ring init */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08002917 if (vsi->back->flags & I40E_FLAG_FD_ATR_ENABLED) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002918 ring->atr_sample_rate = vsi->back->atr_sample_rate;
2919 ring->atr_count = 0;
2920 } else {
2921 ring->atr_sample_rate = 0;
2922 }
2923
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002924 /* configure XPS */
2925 i40e_config_xps_tx_ring(ring);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002926
2927 /* clear the context structure first */
2928 memset(&tx_ctx, 0, sizeof(tx_ctx));
2929
2930 tx_ctx.new_context = 1;
2931 tx_ctx.base = (ring->dma / 128);
2932 tx_ctx.qlen = ring->count;
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08002933 tx_ctx.fd_ena = !!(vsi->back->flags & (I40E_FLAG_FD_SB_ENABLED |
2934 I40E_FLAG_FD_ATR_ENABLED));
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00002935 tx_ctx.timesync_ena = !!(vsi->back->flags & I40E_FLAG_PTP);
Jesse Brandeburg1943d8b2014-02-14 02:14:40 +00002936 /* FDIR VSI tx ring can still use RS bit and writebacks */
2937 if (vsi->type != I40E_VSI_FDIR)
2938 tx_ctx.head_wb_ena = 1;
2939 tx_ctx.head_wb_addr = ring->dma +
2940 (ring->count * sizeof(struct i40e_tx_desc));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002941
2942 /* As part of VSI creation/update, FW allocates certain
2943 * Tx arbitration queue sets for each TC enabled for
2944 * the VSI. The FW returns the handles to these queue
2945 * sets as part of the response buffer to Add VSI,
2946 * Update VSI, etc. AQ commands. It is expected that
2947 * these queue set handles be associated with the Tx
2948 * queues by the driver as part of the TX queue context
2949 * initialization. This has to be done regardless of
2950 * DCB as by default everything is mapped to TC0.
2951 */
2952 tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[ring->dcb_tc]);
2953 tx_ctx.rdylist_act = 0;
2954
2955 /* clear the context in the HMC */
2956 err = i40e_clear_lan_tx_queue_context(hw, pf_q);
2957 if (err) {
2958 dev_info(&vsi->back->pdev->dev,
2959 "Failed to clear LAN Tx queue context on Tx ring %d (pf_q %d), error: %d\n",
2960 ring->queue_index, pf_q, err);
2961 return -ENOMEM;
2962 }
2963
2964 /* set the context in the HMC */
2965 err = i40e_set_lan_tx_queue_context(hw, pf_q, &tx_ctx);
2966 if (err) {
2967 dev_info(&vsi->back->pdev->dev,
2968 "Failed to set LAN Tx queue context on Tx ring %d (pf_q %d, error: %d\n",
2969 ring->queue_index, pf_q, err);
2970 return -ENOMEM;
2971 }
2972
2973 /* Now associate this queue with this PCI function */
Mitch Williams7a28d882014-10-17 03:14:52 +00002974 if (vsi->type == I40E_VSI_VMDQ2) {
Shannon Nelson9d8bf542014-01-14 00:49:50 -08002975 qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
Mitch Williams7a28d882014-10-17 03:14:52 +00002976 qtx_ctl |= ((vsi->id) << I40E_QTX_CTL_VFVM_INDX_SHIFT) &
2977 I40E_QTX_CTL_VFVM_INDX_MASK;
2978 } else {
Shannon Nelson9d8bf542014-01-14 00:49:50 -08002979 qtx_ctl = I40E_QTX_CTL_PF_QUEUE;
Mitch Williams7a28d882014-10-17 03:14:52 +00002980 }
2981
Shannon Nelson13fd9772013-09-28 07:14:19 +00002982 qtx_ctl |= ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT) &
2983 I40E_QTX_CTL_PF_INDX_MASK);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002984 wr32(hw, I40E_QTX_CTL(pf_q), qtx_ctl);
2985 i40e_flush(hw);
2986
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002987 /* cache tail off for easier writes later */
2988 ring->tail = hw->hw_addr + I40E_QTX_TAIL(pf_q);
2989
2990 return 0;
2991}
2992
2993/**
2994 * i40e_configure_rx_ring - Configure a receive ring context
2995 * @ring: The Rx ring to configure
2996 *
2997 * Configure the Rx descriptor ring in the HMC context.
2998 **/
2999static int i40e_configure_rx_ring(struct i40e_ring *ring)
3000{
3001 struct i40e_vsi *vsi = ring->vsi;
3002 u32 chain_len = vsi->back->hw.func_caps.rx_buf_chain_len;
3003 u16 pf_q = vsi->base_queue + ring->queue_index;
3004 struct i40e_hw *hw = &vsi->back->hw;
3005 struct i40e_hmc_obj_rxq rx_ctx;
3006 i40e_status err = 0;
3007
Jesse Brandeburgbd6cd4e2017-08-29 05:32:35 -04003008 bitmap_zero(ring->state, __I40E_RING_STATE_NBITS);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003009
3010 /* clear the context structure first */
3011 memset(&rx_ctx, 0, sizeof(rx_ctx));
3012
3013 ring->rx_buf_len = vsi->rx_buf_len;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003014
Alexander Duyckdab86af2017-03-14 10:15:27 -07003015 rx_ctx.dbuff = DIV_ROUND_UP(ring->rx_buf_len,
3016 BIT_ULL(I40E_RXQ_CTX_DBUFF_SHIFT));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003017
3018 rx_ctx.base = (ring->dma / 128);
3019 rx_ctx.qlen = ring->count;
3020
Jesse Brandeburgbec60fc2016-04-18 11:33:47 -07003021 /* use 32 byte descriptors */
3022 rx_ctx.dsize = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003023
Jesse Brandeburgbec60fc2016-04-18 11:33:47 -07003024 /* descriptor type is always zero
3025 * rx_ctx.dtype = 0;
3026 */
Jesse Brandeburgb32bfa172016-04-18 11:33:42 -07003027 rx_ctx.hsplit_0 = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003028
Jesse Brandeburgb32bfa172016-04-18 11:33:42 -07003029 rx_ctx.rxmax = min_t(u16, vsi->max_frame, chain_len * ring->rx_buf_len);
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00003030 if (hw->revision_id == 0)
3031 rx_ctx.lrxqthresh = 0;
3032 else
3033 rx_ctx.lrxqthresh = 2;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003034 rx_ctx.crcstrip = 1;
3035 rx_ctx.l2tsel = 1;
Jesse Brandeburgc4bbac32015-09-28 11:21:48 -07003036 /* this controls whether VLAN is stripped from inner headers */
3037 rx_ctx.showiv = 0;
Catherine Sullivanacb36762014-03-06 09:02:30 +00003038 /* set the prefena field to 1 because the manual says to */
3039 rx_ctx.prefena = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003040
3041 /* clear the context in the HMC */
3042 err = i40e_clear_lan_rx_queue_context(hw, pf_q);
3043 if (err) {
3044 dev_info(&vsi->back->pdev->dev,
3045 "Failed to clear LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
3046 ring->queue_index, pf_q, err);
3047 return -ENOMEM;
3048 }
3049
3050 /* set the context in the HMC */
3051 err = i40e_set_lan_rx_queue_context(hw, pf_q, &rx_ctx);
3052 if (err) {
3053 dev_info(&vsi->back->pdev->dev,
3054 "Failed to set LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
3055 ring->queue_index, pf_q, err);
3056 return -ENOMEM;
3057 }
3058
Alexander Duyckca9ec082017-04-05 07:51:02 -04003059 /* configure Rx buffer alignment */
3060 if (!vsi->netdev || (vsi->back->flags & I40E_FLAG_LEGACY_RX))
3061 clear_ring_build_skb_enabled(ring);
3062 else
3063 set_ring_build_skb_enabled(ring);
3064
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003065 /* cache tail for quicker writes, and clear the reg before use */
3066 ring->tail = hw->hw_addr + I40E_QRX_TAIL(pf_q);
3067 writel(0, ring->tail);
3068
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07003069 i40e_alloc_rx_buffers(ring, I40E_DESC_UNUSED(ring));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003070
3071 return 0;
3072}
3073
3074/**
3075 * i40e_vsi_configure_tx - Configure the VSI for Tx
3076 * @vsi: VSI structure describing this set of rings and resources
3077 *
3078 * Configure the Tx VSI for operation.
3079 **/
3080static int i40e_vsi_configure_tx(struct i40e_vsi *vsi)
3081{
3082 int err = 0;
3083 u16 i;
3084
Alexander Duyck9f65e152013-09-28 06:00:58 +00003085 for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
3086 err = i40e_configure_tx_ring(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003087
Björn Töpel74608d12017-05-24 07:55:35 +02003088 if (!i40e_enabled_xdp_vsi(vsi))
3089 return err;
3090
3091 for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
3092 err = i40e_configure_tx_ring(vsi->xdp_rings[i]);
3093
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003094 return err;
3095}
3096
3097/**
3098 * i40e_vsi_configure_rx - Configure the VSI for Rx
3099 * @vsi: the VSI being configured
3100 *
3101 * Configure the Rx VSI for operation.
3102 **/
3103static int i40e_vsi_configure_rx(struct i40e_vsi *vsi)
3104{
3105 int err = 0;
3106 u16 i;
3107
Alexander Duyckdab86af2017-03-14 10:15:27 -07003108 if (!vsi->netdev || (vsi->back->flags & I40E_FLAG_LEGACY_RX)) {
3109 vsi->max_frame = I40E_MAX_RXBUFFER;
3110 vsi->rx_buf_len = I40E_RXBUFFER_2048;
3111#if (PAGE_SIZE < 8192)
Alexander Duyckca9ec082017-04-05 07:51:02 -04003112 } else if (!I40E_2K_TOO_SMALL_WITH_PADDING &&
3113 (vsi->netdev->mtu <= ETH_DATA_LEN)) {
Alexander Duyckdab86af2017-03-14 10:15:27 -07003114 vsi->max_frame = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
3115 vsi->rx_buf_len = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
3116#endif
3117 } else {
3118 vsi->max_frame = I40E_MAX_RXBUFFER;
Alexander Duyck98efd692017-04-05 07:51:01 -04003119 vsi->rx_buf_len = (PAGE_SIZE < 8192) ? I40E_RXBUFFER_3072 :
3120 I40E_RXBUFFER_2048;
Alexander Duyckdab86af2017-03-14 10:15:27 -07003121 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003122
3123 /* set up individual rings */
3124 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e152013-09-28 06:00:58 +00003125 err = i40e_configure_rx_ring(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003126
3127 return err;
3128}
3129
3130/**
3131 * i40e_vsi_config_dcb_rings - Update rings to reflect DCB TC
3132 * @vsi: ptr to the VSI
3133 **/
3134static void i40e_vsi_config_dcb_rings(struct i40e_vsi *vsi)
3135{
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +00003136 struct i40e_ring *tx_ring, *rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003137 u16 qoffset, qcount;
3138 int i, n;
3139
Parikh, Neeravcd238a32015-02-21 06:43:37 +00003140 if (!(vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
3141 /* Reset the TC information */
3142 for (i = 0; i < vsi->num_queue_pairs; i++) {
3143 rx_ring = vsi->rx_rings[i];
3144 tx_ring = vsi->tx_rings[i];
3145 rx_ring->dcb_tc = 0;
3146 tx_ring->dcb_tc = 0;
3147 }
3148 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003149
3150 for (n = 0; n < I40E_MAX_TRAFFIC_CLASS; n++) {
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04003151 if (!(vsi->tc_config.enabled_tc & BIT_ULL(n)))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003152 continue;
3153
3154 qoffset = vsi->tc_config.tc_info[n].qoffset;
3155 qcount = vsi->tc_config.tc_info[n].qcount;
3156 for (i = qoffset; i < (qoffset + qcount); i++) {
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +00003157 rx_ring = vsi->rx_rings[i];
3158 tx_ring = vsi->tx_rings[i];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003159 rx_ring->dcb_tc = n;
3160 tx_ring->dcb_tc = n;
3161 }
3162 }
3163}
3164
3165/**
3166 * i40e_set_vsi_rx_mode - Call set_rx_mode on a VSI
3167 * @vsi: ptr to the VSI
3168 **/
3169static void i40e_set_vsi_rx_mode(struct i40e_vsi *vsi)
3170{
3171 if (vsi->netdev)
3172 i40e_set_rx_mode(vsi->netdev);
3173}
3174
3175/**
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00003176 * i40e_fdir_filter_restore - Restore the Sideband Flow Director filters
3177 * @vsi: Pointer to the targeted VSI
3178 *
3179 * This function replays the hlist on the hw where all the SB Flow Director
3180 * filters were saved.
3181 **/
3182static void i40e_fdir_filter_restore(struct i40e_vsi *vsi)
3183{
3184 struct i40e_fdir_filter *filter;
3185 struct i40e_pf *pf = vsi->back;
3186 struct hlist_node *node;
3187
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00003188 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
3189 return;
3190
Jacob Keller6d069422017-02-06 14:38:44 -08003191 /* Reset FDir counters as we're replaying all existing filters */
Jacob Keller097dbf52017-02-06 14:38:46 -08003192 pf->fd_tcp4_filter_cnt = 0;
3193 pf->fd_udp4_filter_cnt = 0;
Jacob Kellerf223c872017-02-06 14:38:51 -08003194 pf->fd_sctp4_filter_cnt = 0;
Jacob Keller097dbf52017-02-06 14:38:46 -08003195 pf->fd_ip4_filter_cnt = 0;
Jacob Keller6d069422017-02-06 14:38:44 -08003196
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00003197 hlist_for_each_entry_safe(filter, node,
3198 &pf->fdir_filter_list, fdir_node) {
3199 i40e_add_del_fdir(vsi, filter, true);
3200 }
3201}
3202
3203/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003204 * i40e_vsi_configure - Set up the VSI for action
3205 * @vsi: the VSI being configured
3206 **/
3207static int i40e_vsi_configure(struct i40e_vsi *vsi)
3208{
3209 int err;
3210
3211 i40e_set_vsi_rx_mode(vsi);
3212 i40e_restore_vlan(vsi);
3213 i40e_vsi_config_dcb_rings(vsi);
3214 err = i40e_vsi_configure_tx(vsi);
3215 if (!err)
3216 err = i40e_vsi_configure_rx(vsi);
3217
3218 return err;
3219}
3220
3221/**
3222 * i40e_vsi_configure_msix - MSIX mode Interrupt Config in the HW
3223 * @vsi: the VSI being configured
3224 **/
3225static void i40e_vsi_configure_msix(struct i40e_vsi *vsi)
3226{
Björn Töpel74608d12017-05-24 07:55:35 +02003227 bool has_xdp = i40e_enabled_xdp_vsi(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003228 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003229 struct i40e_hw *hw = &pf->hw;
3230 u16 vector;
3231 int i, q;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003232 u32 qp;
3233
3234 /* The interrupt indexing is offset by 1 in the PFINT_ITRn
3235 * and PFINT_LNKLSTn registers, e.g.:
3236 * PFINT_ITRn[0..n-1] gets msix-1..msix-n (qpair interrupts)
3237 */
3238 qp = vsi->base_queue;
3239 vector = vsi->base_vector;
Alexander Duyck493fb302013-09-28 07:01:44 +00003240 for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
Jesse Brandeburgac26fc12015-09-28 14:12:37 -04003241 struct i40e_q_vector *q_vector = vsi->q_vectors[i];
3242
Jesse Brandeburgee2319c2015-09-28 14:16:54 -04003243 q_vector->itr_countdown = ITR_COUNTDOWN_START;
Kan Lianga75e8002016-02-19 09:24:04 -05003244 q_vector->rx.itr = ITR_TO_REG(vsi->rx_rings[i]->rx_itr_setting);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003245 q_vector->rx.latency_range = I40E_LOW_LATENCY;
3246 wr32(hw, I40E_PFINT_ITRN(I40E_RX_ITR, vector - 1),
3247 q_vector->rx.itr);
Kan Lianga75e8002016-02-19 09:24:04 -05003248 q_vector->tx.itr = ITR_TO_REG(vsi->tx_rings[i]->tx_itr_setting);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003249 q_vector->tx.latency_range = I40E_LOW_LATENCY;
3250 wr32(hw, I40E_PFINT_ITRN(I40E_TX_ITR, vector - 1),
3251 q_vector->tx.itr);
Jesse Brandeburgac26fc12015-09-28 14:12:37 -04003252 wr32(hw, I40E_PFINT_RATEN(vector - 1),
Alan Brady1c0e6a32016-11-28 16:06:02 -08003253 i40e_intrl_usec_to_reg(vsi->int_rate_limit));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003254
3255 /* Linked list for the queuepairs assigned to this vector */
3256 wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), qp);
3257 for (q = 0; q < q_vector->num_ringpairs; q++) {
Björn Töpel74608d12017-05-24 07:55:35 +02003258 u32 nextqp = has_xdp ? qp + vsi->alloc_queue_pairs : qp;
Jesse Brandeburgac26fc12015-09-28 14:12:37 -04003259 u32 val;
3260
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003261 val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
Björn Töpel74608d12017-05-24 07:55:35 +02003262 (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
3263 (vector << I40E_QINT_RQCTL_MSIX_INDX_SHIFT) |
3264 (nextqp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT) |
3265 (I40E_QUEUE_TYPE_TX <<
3266 I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003267
3268 wr32(hw, I40E_QINT_RQCTL(qp), val);
3269
Björn Töpel74608d12017-05-24 07:55:35 +02003270 if (has_xdp) {
3271 val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3272 (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3273 (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
3274 (qp << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) |
3275 (I40E_QUEUE_TYPE_TX <<
3276 I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3277
3278 wr32(hw, I40E_QINT_TQCTL(nextqp), val);
3279 }
3280
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003281 val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
Björn Töpel74608d12017-05-24 07:55:35 +02003282 (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3283 (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
3284 ((qp + 1) << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) |
3285 (I40E_QUEUE_TYPE_RX <<
3286 I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003287
3288 /* Terminate the linked list */
3289 if (q == (q_vector->num_ringpairs - 1))
Björn Töpel74608d12017-05-24 07:55:35 +02003290 val |= (I40E_QUEUE_END_OF_LIST <<
3291 I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003292
3293 wr32(hw, I40E_QINT_TQCTL(qp), val);
3294 qp++;
3295 }
3296 }
3297
3298 i40e_flush(hw);
3299}
3300
3301/**
3302 * i40e_enable_misc_int_causes - enable the non-queue interrupts
3303 * @hw: ptr to the hardware info
3304 **/
Jacob Kellerab437b52014-12-14 01:55:08 +00003305static void i40e_enable_misc_int_causes(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003306{
Jacob Kellerab437b52014-12-14 01:55:08 +00003307 struct i40e_hw *hw = &pf->hw;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003308 u32 val;
3309
3310 /* clear things first */
3311 wr32(hw, I40E_PFINT_ICR0_ENA, 0); /* disable all */
3312 rd32(hw, I40E_PFINT_ICR0); /* read to clear */
3313
3314 val = I40E_PFINT_ICR0_ENA_ECC_ERR_MASK |
3315 I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK |
3316 I40E_PFINT_ICR0_ENA_GRST_MASK |
3317 I40E_PFINT_ICR0_ENA_PCI_EXCEPTION_MASK |
3318 I40E_PFINT_ICR0_ENA_GPIO_MASK |
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003319 I40E_PFINT_ICR0_ENA_HMC_ERR_MASK |
3320 I40E_PFINT_ICR0_ENA_VFLR_MASK |
3321 I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
3322
Anjali Singhai Jain0d8e1432015-06-05 12:20:32 -04003323 if (pf->flags & I40E_FLAG_IWARP_ENABLED)
3324 val |= I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
3325
Jacob Kellerab437b52014-12-14 01:55:08 +00003326 if (pf->flags & I40E_FLAG_PTP)
3327 val |= I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
3328
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003329 wr32(hw, I40E_PFINT_ICR0_ENA, val);
3330
3331 /* SW_ITR_IDX = 0, but don't change INTENA */
Anjali Singhai Jain84ed40e2013-11-26 10:49:32 +00003332 wr32(hw, I40E_PFINT_DYN_CTL0, I40E_PFINT_DYN_CTL0_SW_ITR_INDX_MASK |
3333 I40E_PFINT_DYN_CTL0_INTENA_MSK_MASK);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003334
3335 /* OTHER_ITR_IDX = 0 */
3336 wr32(hw, I40E_PFINT_STAT_CTL0, 0);
3337}
3338
3339/**
3340 * i40e_configure_msi_and_legacy - Legacy mode interrupt config in the HW
3341 * @vsi: the VSI being configured
3342 **/
3343static void i40e_configure_msi_and_legacy(struct i40e_vsi *vsi)
3344{
Björn Töpel74608d12017-05-24 07:55:35 +02003345 u32 nextqp = i40e_enabled_xdp_vsi(vsi) ? vsi->alloc_queue_pairs : 0;
Alexander Duyck493fb302013-09-28 07:01:44 +00003346 struct i40e_q_vector *q_vector = vsi->q_vectors[0];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003347 struct i40e_pf *pf = vsi->back;
3348 struct i40e_hw *hw = &pf->hw;
3349 u32 val;
3350
3351 /* set the ITR configuration */
Jesse Brandeburgee2319c2015-09-28 14:16:54 -04003352 q_vector->itr_countdown = ITR_COUNTDOWN_START;
Kan Lianga75e8002016-02-19 09:24:04 -05003353 q_vector->rx.itr = ITR_TO_REG(vsi->rx_rings[0]->rx_itr_setting);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003354 q_vector->rx.latency_range = I40E_LOW_LATENCY;
3355 wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), q_vector->rx.itr);
Kan Lianga75e8002016-02-19 09:24:04 -05003356 q_vector->tx.itr = ITR_TO_REG(vsi->tx_rings[0]->tx_itr_setting);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003357 q_vector->tx.latency_range = I40E_LOW_LATENCY;
3358 wr32(hw, I40E_PFINT_ITR0(I40E_TX_ITR), q_vector->tx.itr);
3359
Jacob Kellerab437b52014-12-14 01:55:08 +00003360 i40e_enable_misc_int_causes(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003361
3362 /* FIRSTQ_INDX = 0, FIRSTQ_TYPE = 0 (rx) */
3363 wr32(hw, I40E_PFINT_LNKLST0, 0);
3364
Jesse Brandeburgf29eaa32014-02-11 08:24:12 +00003365 /* Associate the queue pair to the vector and enable the queue int */
Björn Töpel74608d12017-05-24 07:55:35 +02003366 val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3367 (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
3368 (nextqp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT)|
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003369 (I40E_QUEUE_TYPE_TX << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3370
3371 wr32(hw, I40E_QINT_RQCTL(0), val);
3372
Björn Töpel74608d12017-05-24 07:55:35 +02003373 if (i40e_enabled_xdp_vsi(vsi)) {
3374 val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3375 (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT)|
3376 (I40E_QUEUE_TYPE_TX
3377 << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3378
3379 wr32(hw, I40E_QINT_TQCTL(nextqp), val);
3380 }
3381
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003382 val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3383 (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3384 (I40E_QUEUE_END_OF_LIST << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
3385
3386 wr32(hw, I40E_QINT_TQCTL(0), val);
3387 i40e_flush(hw);
3388}
3389
3390/**
Mitch Williams2ef28cf2013-11-28 06:39:32 +00003391 * i40e_irq_dynamic_disable_icr0 - Disable default interrupt generation for icr0
3392 * @pf: board private structure
3393 **/
3394void i40e_irq_dynamic_disable_icr0(struct i40e_pf *pf)
3395{
3396 struct i40e_hw *hw = &pf->hw;
3397
3398 wr32(hw, I40E_PFINT_DYN_CTL0,
3399 I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
3400 i40e_flush(hw);
3401}
3402
3403/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003404 * i40e_irq_dynamic_enable_icr0 - Enable default interrupt generation for icr0
3405 * @pf: board private structure
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08003406 * @clearpba: true when all pending interrupt events should be cleared
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003407 **/
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08003408void i40e_irq_dynamic_enable_icr0(struct i40e_pf *pf, bool clearpba)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003409{
3410 struct i40e_hw *hw = &pf->hw;
3411 u32 val;
3412
3413 val = I40E_PFINT_DYN_CTL0_INTENA_MASK |
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08003414 (clearpba ? I40E_PFINT_DYN_CTL0_CLEARPBA_MASK : 0) |
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003415 (I40E_ITR_NONE << I40E_PFINT_DYN_CTL0_ITR_INDX_SHIFT);
3416
3417 wr32(hw, I40E_PFINT_DYN_CTL0, val);
3418 i40e_flush(hw);
3419}
3420
3421/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003422 * i40e_msix_clean_rings - MSIX mode Interrupt Handler
3423 * @irq: interrupt number
3424 * @data: pointer to a q_vector
3425 **/
3426static irqreturn_t i40e_msix_clean_rings(int irq, void *data)
3427{
3428 struct i40e_q_vector *q_vector = data;
3429
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003430 if (!q_vector->tx.ring && !q_vector->rx.ring)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003431 return IRQ_HANDLED;
3432
Alexander Duyck5d3465a2015-09-29 15:19:50 -07003433 napi_schedule_irqoff(&q_vector->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003434
3435 return IRQ_HANDLED;
3436}
3437
3438/**
Alan Brady96db7762016-09-14 16:24:38 -07003439 * i40e_irq_affinity_notify - Callback for affinity changes
3440 * @notify: context as to what irq was changed
3441 * @mask: the new affinity mask
3442 *
3443 * This is a callback function used by the irq_set_affinity_notifier function
3444 * so that we may register to receive changes to the irq affinity masks.
3445 **/
3446static void i40e_irq_affinity_notify(struct irq_affinity_notify *notify,
3447 const cpumask_t *mask)
3448{
3449 struct i40e_q_vector *q_vector =
3450 container_of(notify, struct i40e_q_vector, affinity_notify);
3451
Jacob Keller7e4d01e2017-07-12 05:46:05 -04003452 cpumask_copy(&q_vector->affinity_mask, mask);
Alan Brady96db7762016-09-14 16:24:38 -07003453}
3454
3455/**
3456 * i40e_irq_affinity_release - Callback for affinity notifier release
3457 * @ref: internal core kernel usage
3458 *
3459 * This is a callback function used by the irq_set_affinity_notifier function
3460 * to inform the current notification subscriber that they will no longer
3461 * receive notifications.
3462 **/
3463static void i40e_irq_affinity_release(struct kref *ref) {}
3464
3465/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003466 * i40e_vsi_request_irq_msix - Initialize MSI-X interrupts
3467 * @vsi: the VSI being configured
3468 * @basename: name for the vector
3469 *
3470 * Allocates MSI-X vectors and requests interrupts from the kernel.
3471 **/
3472static int i40e_vsi_request_irq_msix(struct i40e_vsi *vsi, char *basename)
3473{
3474 int q_vectors = vsi->num_q_vectors;
3475 struct i40e_pf *pf = vsi->back;
3476 int base = vsi->base_vector;
3477 int rx_int_idx = 0;
3478 int tx_int_idx = 0;
3479 int vector, err;
Alan Brady96db7762016-09-14 16:24:38 -07003480 int irq_num;
Jacob Kellerbe664cb2017-08-29 05:32:31 -04003481 int cpu;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003482
3483 for (vector = 0; vector < q_vectors; vector++) {
Alexander Duyck493fb302013-09-28 07:01:44 +00003484 struct i40e_q_vector *q_vector = vsi->q_vectors[vector];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003485
Alan Brady96db7762016-09-14 16:24:38 -07003486 irq_num = pf->msix_entries[base + vector].vector;
3487
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003488 if (q_vector->tx.ring && q_vector->rx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003489 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3490 "%s-%s-%d", basename, "TxRx", rx_int_idx++);
3491 tx_int_idx++;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003492 } else if (q_vector->rx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003493 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3494 "%s-%s-%d", basename, "rx", rx_int_idx++);
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003495 } else if (q_vector->tx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003496 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3497 "%s-%s-%d", basename, "tx", tx_int_idx++);
3498 } else {
3499 /* skip this unused q_vector */
3500 continue;
3501 }
Alan Brady96db7762016-09-14 16:24:38 -07003502 err = request_irq(irq_num,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003503 vsi->irq_handler,
3504 0,
3505 q_vector->name,
3506 q_vector);
3507 if (err) {
3508 dev_info(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04003509 "MSIX request_irq failed, error: %d\n", err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003510 goto free_queue_irqs;
3511 }
Alan Brady96db7762016-09-14 16:24:38 -07003512
3513 /* register for affinity change notifications */
3514 q_vector->affinity_notify.notify = i40e_irq_affinity_notify;
3515 q_vector->affinity_notify.release = i40e_irq_affinity_release;
3516 irq_set_affinity_notifier(irq_num, &q_vector->affinity_notify);
Jacob Kellerbe664cb2017-08-29 05:32:31 -04003517 /* Spread affinity hints out across online CPUs.
3518 *
3519 * get_cpu_mask returns a static constant mask with
3520 * a permanent lifetime so it's ok to pass to
3521 * irq_set_affinity_hint without making a copy.
Jacob Keller759dc4a2017-07-14 09:10:10 -04003522 */
Jacob Kellerbe664cb2017-08-29 05:32:31 -04003523 cpu = cpumask_local_spread(q_vector->v_idx, -1);
3524 irq_set_affinity_hint(irq_num, get_cpu_mask(cpu));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003525 }
3526
Shannon Nelson63741842014-04-23 04:50:16 +00003527 vsi->irqs_ready = true;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003528 return 0;
3529
3530free_queue_irqs:
3531 while (vector) {
3532 vector--;
Alan Brady96db7762016-09-14 16:24:38 -07003533 irq_num = pf->msix_entries[base + vector].vector;
3534 irq_set_affinity_notifier(irq_num, NULL);
3535 irq_set_affinity_hint(irq_num, NULL);
3536 free_irq(irq_num, &vsi->q_vectors[vector]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003537 }
3538 return err;
3539}
3540
3541/**
3542 * i40e_vsi_disable_irq - Mask off queue interrupt generation on the VSI
3543 * @vsi: the VSI being un-configured
3544 **/
3545static void i40e_vsi_disable_irq(struct i40e_vsi *vsi)
3546{
3547 struct i40e_pf *pf = vsi->back;
3548 struct i40e_hw *hw = &pf->hw;
3549 int base = vsi->base_vector;
3550 int i;
3551
Shannon Nelson2e5c26ea2017-06-07 05:43:11 -04003552 /* disable interrupt causation from each queue */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003553 for (i = 0; i < vsi->num_queue_pairs; i++) {
Shannon Nelson2e5c26ea2017-06-07 05:43:11 -04003554 u32 val;
3555
3556 val = rd32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx));
3557 val &= ~I40E_QINT_TQCTL_CAUSE_ENA_MASK;
3558 wr32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx), val);
3559
3560 val = rd32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx));
3561 val &= ~I40E_QINT_RQCTL_CAUSE_ENA_MASK;
3562 wr32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx), val);
3563
Björn Töpel74608d12017-05-24 07:55:35 +02003564 if (!i40e_enabled_xdp_vsi(vsi))
3565 continue;
3566 wr32(hw, I40E_QINT_TQCTL(vsi->xdp_rings[i]->reg_idx), 0);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003567 }
3568
Shannon Nelson2e5c26ea2017-06-07 05:43:11 -04003569 /* disable each interrupt */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003570 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3571 for (i = vsi->base_vector;
3572 i < (vsi->num_q_vectors + vsi->base_vector); i++)
3573 wr32(hw, I40E_PFINT_DYN_CTLN(i - 1), 0);
3574
3575 i40e_flush(hw);
3576 for (i = 0; i < vsi->num_q_vectors; i++)
3577 synchronize_irq(pf->msix_entries[i + base].vector);
3578 } else {
3579 /* Legacy and MSI mode - this stops all interrupt handling */
3580 wr32(hw, I40E_PFINT_ICR0_ENA, 0);
3581 wr32(hw, I40E_PFINT_DYN_CTL0, 0);
3582 i40e_flush(hw);
3583 synchronize_irq(pf->pdev->irq);
3584 }
3585}
3586
3587/**
3588 * i40e_vsi_enable_irq - Enable IRQ for the given VSI
3589 * @vsi: the VSI being configured
3590 **/
3591static int i40e_vsi_enable_irq(struct i40e_vsi *vsi)
3592{
3593 struct i40e_pf *pf = vsi->back;
3594 int i;
3595
3596 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
Jesse Brandeburg78455482015-07-23 16:54:41 -04003597 for (i = 0; i < vsi->num_q_vectors; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003598 i40e_irq_dynamic_enable(vsi, i);
3599 } else {
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08003600 i40e_irq_dynamic_enable_icr0(pf, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003601 }
3602
Jesse Brandeburg1022cb62013-09-28 07:13:08 +00003603 i40e_flush(&pf->hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003604 return 0;
3605}
3606
3607/**
Jacob Kellerc17401a2017-07-14 09:27:02 -04003608 * i40e_free_misc_vector - Free the vector that handles non-queue events
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003609 * @pf: board private structure
3610 **/
Jacob Kellerc17401a2017-07-14 09:27:02 -04003611static void i40e_free_misc_vector(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003612{
3613 /* Disable ICR 0 */
3614 wr32(&pf->hw, I40E_PFINT_ICR0_ENA, 0);
3615 i40e_flush(&pf->hw);
Jacob Kellerc17401a2017-07-14 09:27:02 -04003616
3617 if (pf->flags & I40E_FLAG_MSIX_ENABLED && pf->msix_entries) {
3618 synchronize_irq(pf->msix_entries[0].vector);
3619 free_irq(pf->msix_entries[0].vector, pf);
3620 clear_bit(__I40E_MISC_IRQ_REQUESTED, pf->state);
3621 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003622}
3623
3624/**
3625 * i40e_intr - MSI/Legacy and non-queue interrupt handler
3626 * @irq: interrupt number
3627 * @data: pointer to a q_vector
3628 *
3629 * This is the handler used for all MSI/Legacy interrupts, and deals
3630 * with both queue and non-queue interrupts. This is also used in
3631 * MSIX mode to handle the non-queue interrupts.
3632 **/
3633static irqreturn_t i40e_intr(int irq, void *data)
3634{
3635 struct i40e_pf *pf = (struct i40e_pf *)data;
3636 struct i40e_hw *hw = &pf->hw;
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003637 irqreturn_t ret = IRQ_NONE;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003638 u32 icr0, icr0_remaining;
3639 u32 val, ena_mask;
3640
3641 icr0 = rd32(hw, I40E_PFINT_ICR0);
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003642 ena_mask = rd32(hw, I40E_PFINT_ICR0_ENA);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003643
Shannon Nelson116a57d2013-09-28 07:13:59 +00003644 /* if sharing a legacy IRQ, we might get called w/o an intr pending */
3645 if ((icr0 & I40E_PFINT_ICR0_INTEVENT_MASK) == 0)
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003646 goto enable_intr;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003647
Shannon Nelsoncd92e722013-11-16 10:00:44 +00003648 /* if interrupt but no bits showing, must be SWINT */
3649 if (((icr0 & ~I40E_PFINT_ICR0_INTEVENT_MASK) == 0) ||
3650 (icr0 & I40E_PFINT_ICR0_SWINT_MASK))
3651 pf->sw_int_count++;
3652
Anjali Singhai Jain0d8e1432015-06-05 12:20:32 -04003653 if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
Catherine Sullivan76429842017-06-07 05:43:12 -04003654 (icr0 & I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK)) {
Anjali Singhai Jain0d8e1432015-06-05 12:20:32 -04003655 ena_mask &= ~I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
Carolyn Wyborny23bb6dc2016-11-08 13:05:12 -08003656 dev_dbg(&pf->pdev->dev, "cleared PE_CRITERR\n");
Catherine Sullivan76429842017-06-07 05:43:12 -04003657 set_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
Anjali Singhai Jain0d8e1432015-06-05 12:20:32 -04003658 }
3659
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003660 /* only q0 is used in MSI/Legacy mode, and none are used in MSIX */
3661 if (icr0 & I40E_PFINT_ICR0_QUEUE_0_MASK) {
Alexander Duyck5d3465a2015-09-29 15:19:50 -07003662 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
3663 struct i40e_q_vector *q_vector = vsi->q_vectors[0];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003664
Anjali Singhai Jaina16ae2d2016-01-15 14:33:16 -08003665 /* We do not have a way to disarm Queue causes while leaving
3666 * interrupt enabled for all other causes, ideally
3667 * interrupt should be disabled while we are in NAPI but
3668 * this is not a performance path and napi_schedule()
3669 * can deal with rescheduling.
3670 */
Mauro S. M. Rodrigues9e6c9c02017-05-12 23:26:56 -03003671 if (!test_bit(__I40E_DOWN, pf->state))
Alexander Duyck5d3465a2015-09-29 15:19:50 -07003672 napi_schedule_irqoff(&q_vector->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003673 }
3674
3675 if (icr0 & I40E_PFINT_ICR0_ADMINQ_MASK) {
3676 ena_mask &= ~I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
Jacob Keller0da36b92017-04-19 09:25:55 -04003677 set_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state);
Shannon Nelson6e93d0c2016-01-15 14:33:18 -08003678 i40e_debug(&pf->hw, I40E_DEBUG_NVM, "AdminQ event\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003679 }
3680
3681 if (icr0 & I40E_PFINT_ICR0_MAL_DETECT_MASK) {
3682 ena_mask &= ~I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
Jacob Keller0da36b92017-04-19 09:25:55 -04003683 set_bit(__I40E_MDD_EVENT_PENDING, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003684 }
3685
3686 if (icr0 & I40E_PFINT_ICR0_VFLR_MASK) {
3687 ena_mask &= ~I40E_PFINT_ICR0_ENA_VFLR_MASK;
Jacob Keller0da36b92017-04-19 09:25:55 -04003688 set_bit(__I40E_VFLR_EVENT_PENDING, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003689 }
3690
3691 if (icr0 & I40E_PFINT_ICR0_GRST_MASK) {
Jacob Keller0da36b92017-04-19 09:25:55 -04003692 if (!test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
3693 set_bit(__I40E_RESET_INTR_RECEIVED, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003694 ena_mask &= ~I40E_PFINT_ICR0_ENA_GRST_MASK;
3695 val = rd32(hw, I40E_GLGEN_RSTAT);
3696 val = (val & I40E_GLGEN_RSTAT_RESET_TYPE_MASK)
3697 >> I40E_GLGEN_RSTAT_RESET_TYPE_SHIFT;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003698 if (val == I40E_RESET_CORER) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003699 pf->corer_count++;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003700 } else if (val == I40E_RESET_GLOBR) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003701 pf->globr_count++;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003702 } else if (val == I40E_RESET_EMPR) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003703 pf->empr_count++;
Jacob Keller0da36b92017-04-19 09:25:55 -04003704 set_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state);
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003705 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003706 }
3707
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003708 if (icr0 & I40E_PFINT_ICR0_HMC_ERR_MASK) {
3709 icr0 &= ~I40E_PFINT_ICR0_HMC_ERR_MASK;
3710 dev_info(&pf->pdev->dev, "HMC error interrupt\n");
Anjali Singhai Jain25fc0e62015-03-31 00:45:01 -07003711 dev_info(&pf->pdev->dev, "HMC error info 0x%x, HMC error data 0x%x\n",
3712 rd32(hw, I40E_PFHMC_ERRORINFO),
3713 rd32(hw, I40E_PFHMC_ERRORDATA));
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003714 }
3715
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003716 if (icr0 & I40E_PFINT_ICR0_TIMESYNC_MASK) {
3717 u32 prttsyn_stat = rd32(hw, I40E_PRTTSYN_STAT_0);
3718
3719 if (prttsyn_stat & I40E_PRTTSYN_STAT_0_TXTIME_MASK) {
Jacob Kellercafa1fc2014-04-24 18:05:03 -07003720 icr0 &= ~I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003721 i40e_ptp_tx_hwtstamp(pf);
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003722 }
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003723 }
3724
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003725 /* If a critical error is pending we have no choice but to reset the
3726 * device.
3727 * Report and mask out any remaining unexpected interrupts.
3728 */
3729 icr0_remaining = icr0 & ena_mask;
3730 if (icr0_remaining) {
3731 dev_info(&pf->pdev->dev, "unhandled interrupt icr0=0x%08x\n",
3732 icr0_remaining);
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003733 if ((icr0_remaining & I40E_PFINT_ICR0_PE_CRITERR_MASK) ||
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003734 (icr0_remaining & I40E_PFINT_ICR0_PCI_EXCEPTION_MASK) ||
Anjali Singhai Jainc0c28972014-02-12 01:45:34 +00003735 (icr0_remaining & I40E_PFINT_ICR0_ECC_ERR_MASK)) {
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003736 dev_info(&pf->pdev->dev, "device will be reset\n");
Jacob Keller0da36b92017-04-19 09:25:55 -04003737 set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003738 i40e_service_event_schedule(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003739 }
3740 ena_mask &= ~icr0_remaining;
3741 }
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003742 ret = IRQ_HANDLED;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003743
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003744enable_intr:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003745 /* re-enable interrupt causes */
3746 wr32(hw, I40E_PFINT_ICR0_ENA, ena_mask);
Mauro S. M. Rodrigues9e6c9c02017-05-12 23:26:56 -03003747 if (!test_bit(__I40E_DOWN, pf->state)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003748 i40e_service_event_schedule(pf);
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08003749 i40e_irq_dynamic_enable_icr0(pf, false);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003750 }
3751
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003752 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003753}
3754
3755/**
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003756 * i40e_clean_fdir_tx_irq - Reclaim resources after transmit completes
3757 * @tx_ring: tx ring to clean
3758 * @budget: how many cleans we're allowed
3759 *
3760 * Returns true if there's any budget left (e.g. the clean is finished)
3761 **/
3762static bool i40e_clean_fdir_tx_irq(struct i40e_ring *tx_ring, int budget)
3763{
3764 struct i40e_vsi *vsi = tx_ring->vsi;
3765 u16 i = tx_ring->next_to_clean;
3766 struct i40e_tx_buffer *tx_buf;
3767 struct i40e_tx_desc *tx_desc;
3768
3769 tx_buf = &tx_ring->tx_bi[i];
3770 tx_desc = I40E_TX_DESC(tx_ring, i);
3771 i -= tx_ring->count;
3772
3773 do {
3774 struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
3775
3776 /* if next_to_watch is not set then there is no work pending */
3777 if (!eop_desc)
3778 break;
3779
3780 /* prevent any other reads prior to eop_desc */
3781 read_barrier_depends();
3782
3783 /* if the descriptor isn't done, no work yet to do */
3784 if (!(eop_desc->cmd_type_offset_bsz &
3785 cpu_to_le64(I40E_TX_DESC_DTYPE_DESC_DONE)))
3786 break;
3787
3788 /* clear next_to_watch to prevent false hangs */
3789 tx_buf->next_to_watch = NULL;
3790
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003791 tx_desc->buffer_addr = 0;
3792 tx_desc->cmd_type_offset_bsz = 0;
3793 /* move past filter desc */
3794 tx_buf++;
3795 tx_desc++;
3796 i++;
3797 if (unlikely(!i)) {
3798 i -= tx_ring->count;
3799 tx_buf = tx_ring->tx_bi;
3800 tx_desc = I40E_TX_DESC(tx_ring, 0);
3801 }
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003802 /* unmap skb header data */
3803 dma_unmap_single(tx_ring->dev,
3804 dma_unmap_addr(tx_buf, dma),
3805 dma_unmap_len(tx_buf, len),
3806 DMA_TO_DEVICE);
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003807 if (tx_buf->tx_flags & I40E_TX_FLAGS_FD_SB)
3808 kfree(tx_buf->raw_buf);
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003809
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003810 tx_buf->raw_buf = NULL;
3811 tx_buf->tx_flags = 0;
3812 tx_buf->next_to_watch = NULL;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003813 dma_unmap_len_set(tx_buf, len, 0);
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003814 tx_desc->buffer_addr = 0;
3815 tx_desc->cmd_type_offset_bsz = 0;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003816
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003817 /* move us past the eop_desc for start of next FD desc */
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003818 tx_buf++;
3819 tx_desc++;
3820 i++;
3821 if (unlikely(!i)) {
3822 i -= tx_ring->count;
3823 tx_buf = tx_ring->tx_bi;
3824 tx_desc = I40E_TX_DESC(tx_ring, 0);
3825 }
3826
3827 /* update budget accounting */
3828 budget--;
3829 } while (likely(budget));
3830
3831 i += tx_ring->count;
3832 tx_ring->next_to_clean = i;
3833
Jesse Brandeburg6995b362015-08-28 17:55:54 -04003834 if (vsi->back->flags & I40E_FLAG_MSIX_ENABLED)
Jesse Brandeburg78455482015-07-23 16:54:41 -04003835 i40e_irq_dynamic_enable(vsi, tx_ring->q_vector->v_idx);
Jesse Brandeburg6995b362015-08-28 17:55:54 -04003836
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003837 return budget > 0;
3838}
3839
3840/**
3841 * i40e_fdir_clean_ring - Interrupt Handler for FDIR SB ring
3842 * @irq: interrupt number
3843 * @data: pointer to a q_vector
3844 **/
3845static irqreturn_t i40e_fdir_clean_ring(int irq, void *data)
3846{
3847 struct i40e_q_vector *q_vector = data;
3848 struct i40e_vsi *vsi;
3849
3850 if (!q_vector->tx.ring)
3851 return IRQ_HANDLED;
3852
3853 vsi = q_vector->tx.ring->vsi;
3854 i40e_clean_fdir_tx_irq(q_vector->tx.ring, vsi->work_limit);
3855
3856 return IRQ_HANDLED;
3857}
3858
3859/**
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003860 * i40e_map_vector_to_qp - Assigns the queue pair to the vector
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003861 * @vsi: the VSI being configured
3862 * @v_idx: vector index
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003863 * @qp_idx: queue pair index
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003864 **/
Anjali Singhai Jain26cdc442015-07-10 19:36:00 -04003865static void i40e_map_vector_to_qp(struct i40e_vsi *vsi, int v_idx, int qp_idx)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003866{
Alexander Duyck493fb302013-09-28 07:01:44 +00003867 struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
Alexander Duyck9f65e152013-09-28 06:00:58 +00003868 struct i40e_ring *tx_ring = vsi->tx_rings[qp_idx];
3869 struct i40e_ring *rx_ring = vsi->rx_rings[qp_idx];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003870
3871 tx_ring->q_vector = q_vector;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003872 tx_ring->next = q_vector->tx.ring;
3873 q_vector->tx.ring = tx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003874 q_vector->tx.count++;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003875
Björn Töpel74608d12017-05-24 07:55:35 +02003876 /* Place XDP Tx ring in the same q_vector ring list as regular Tx */
3877 if (i40e_enabled_xdp_vsi(vsi)) {
3878 struct i40e_ring *xdp_ring = vsi->xdp_rings[qp_idx];
3879
3880 xdp_ring->q_vector = q_vector;
3881 xdp_ring->next = q_vector->tx.ring;
3882 q_vector->tx.ring = xdp_ring;
3883 q_vector->tx.count++;
3884 }
3885
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003886 rx_ring->q_vector = q_vector;
3887 rx_ring->next = q_vector->rx.ring;
3888 q_vector->rx.ring = rx_ring;
3889 q_vector->rx.count++;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003890}
3891
3892/**
3893 * i40e_vsi_map_rings_to_vectors - Maps descriptor rings to vectors
3894 * @vsi: the VSI being configured
3895 *
3896 * This function maps descriptor rings to the queue-specific vectors
3897 * we were allotted through the MSI-X enabling code. Ideally, we'd have
3898 * one vector per queue pair, but on a constrained vector budget, we
3899 * group the queue pairs as "efficiently" as possible.
3900 **/
3901static void i40e_vsi_map_rings_to_vectors(struct i40e_vsi *vsi)
3902{
3903 int qp_remaining = vsi->num_queue_pairs;
3904 int q_vectors = vsi->num_q_vectors;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003905 int num_ringpairs;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003906 int v_start = 0;
3907 int qp_idx = 0;
3908
3909 /* If we don't have enough vectors for a 1-to-1 mapping, we'll have to
3910 * group them so there are multiple queues per vector.
Anjali Singhai Jain70114ec2014-06-03 23:50:14 +00003911 * It is also important to go through all the vectors available to be
3912 * sure that if we don't use all the vectors, that the remaining vectors
3913 * are cleared. This is especially important when decreasing the
3914 * number of queues in use.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003915 */
Anjali Singhai Jain70114ec2014-06-03 23:50:14 +00003916 for (; v_start < q_vectors; v_start++) {
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003917 struct i40e_q_vector *q_vector = vsi->q_vectors[v_start];
3918
3919 num_ringpairs = DIV_ROUND_UP(qp_remaining, q_vectors - v_start);
3920
3921 q_vector->num_ringpairs = num_ringpairs;
3922
3923 q_vector->rx.count = 0;
3924 q_vector->tx.count = 0;
3925 q_vector->rx.ring = NULL;
3926 q_vector->tx.ring = NULL;
3927
3928 while (num_ringpairs--) {
Anjali Singhai Jain26cdc442015-07-10 19:36:00 -04003929 i40e_map_vector_to_qp(vsi, v_start, qp_idx);
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003930 qp_idx++;
3931 qp_remaining--;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003932 }
3933 }
3934}
3935
3936/**
3937 * i40e_vsi_request_irq - Request IRQ from the OS
3938 * @vsi: the VSI being configured
3939 * @basename: name for the vector
3940 **/
3941static int i40e_vsi_request_irq(struct i40e_vsi *vsi, char *basename)
3942{
3943 struct i40e_pf *pf = vsi->back;
3944 int err;
3945
3946 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
3947 err = i40e_vsi_request_irq_msix(vsi, basename);
3948 else if (pf->flags & I40E_FLAG_MSI_ENABLED)
3949 err = request_irq(pf->pdev->irq, i40e_intr, 0,
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00003950 pf->int_name, pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003951 else
3952 err = request_irq(pf->pdev->irq, i40e_intr, IRQF_SHARED,
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00003953 pf->int_name, pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003954
3955 if (err)
3956 dev_info(&pf->pdev->dev, "request_irq failed, Error %d\n", err);
3957
3958 return err;
3959}
3960
3961#ifdef CONFIG_NET_POLL_CONTROLLER
3962/**
Jesse Brandeburgd89d9672016-01-04 10:33:02 -08003963 * i40e_netpoll - A Polling 'interrupt' handler
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003964 * @netdev: network interface device structure
3965 *
3966 * This is used by netconsole to send skbs without having to re-enable
3967 * interrupts. It's not called while the normal interrupt routine is executing.
3968 **/
3969static void i40e_netpoll(struct net_device *netdev)
3970{
3971 struct i40e_netdev_priv *np = netdev_priv(netdev);
3972 struct i40e_vsi *vsi = np->vsi;
3973 struct i40e_pf *pf = vsi->back;
3974 int i;
3975
3976 /* if interface is down do nothing */
Jacob Keller0da36b92017-04-19 09:25:55 -04003977 if (test_bit(__I40E_VSI_DOWN, vsi->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003978 return;
3979
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003980 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3981 for (i = 0; i < vsi->num_q_vectors; i++)
Alexander Duyck493fb302013-09-28 07:01:44 +00003982 i40e_msix_clean_rings(0, vsi->q_vectors[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003983 } else {
3984 i40e_intr(pf->pdev->irq, netdev);
3985 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003986}
3987#endif
3988
Jacob Kellerc768e492017-04-13 04:45:47 -04003989#define I40E_QTX_ENA_WAIT_COUNT 50
3990
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003991/**
Neerav Parikh23527302014-06-03 23:50:15 +00003992 * i40e_pf_txq_wait - Wait for a PF's Tx queue to be enabled or disabled
3993 * @pf: the PF being configured
3994 * @pf_q: the PF queue
3995 * @enable: enable or disable state of the queue
3996 *
3997 * This routine will wait for the given Tx queue of the PF to reach the
3998 * enabled or disabled state.
3999 * Returns -ETIMEDOUT in case of failing to reach the requested state after
4000 * multiple retries; else will return 0 in case of success.
4001 **/
4002static int i40e_pf_txq_wait(struct i40e_pf *pf, int pf_q, bool enable)
4003{
4004 int i;
4005 u32 tx_reg;
4006
4007 for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
4008 tx_reg = rd32(&pf->hw, I40E_QTX_ENA(pf_q));
4009 if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
4010 break;
4011
Neerav Parikhf98a2002014-09-13 07:40:44 +00004012 usleep_range(10, 20);
Neerav Parikh23527302014-06-03 23:50:15 +00004013 }
4014 if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
4015 return -ETIMEDOUT;
4016
4017 return 0;
4018}
4019
4020/**
Jacob Kellerc768e492017-04-13 04:45:47 -04004021 * i40e_control_tx_q - Start or stop a particular Tx queue
4022 * @pf: the PF structure
4023 * @pf_q: the PF queue to configure
4024 * @enable: start or stop the queue
4025 *
4026 * This function enables or disables a single queue. Note that any delay
4027 * required after the operation is expected to be handled by the caller of
4028 * this function.
4029 **/
4030static void i40e_control_tx_q(struct i40e_pf *pf, int pf_q, bool enable)
4031{
4032 struct i40e_hw *hw = &pf->hw;
4033 u32 tx_reg;
4034 int i;
4035
4036 /* warn the TX unit of coming changes */
4037 i40e_pre_tx_queue_cfg(&pf->hw, pf_q, enable);
4038 if (!enable)
4039 usleep_range(10, 20);
4040
4041 for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
4042 tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
4043 if (((tx_reg >> I40E_QTX_ENA_QENA_REQ_SHIFT) & 1) ==
4044 ((tx_reg >> I40E_QTX_ENA_QENA_STAT_SHIFT) & 1))
4045 break;
4046 usleep_range(1000, 2000);
4047 }
4048
4049 /* Skip if the queue is already in the requested state */
4050 if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
4051 return;
4052
4053 /* turn on/off the queue */
4054 if (enable) {
4055 wr32(hw, I40E_QTX_HEAD(pf_q), 0);
4056 tx_reg |= I40E_QTX_ENA_QENA_REQ_MASK;
4057 } else {
4058 tx_reg &= ~I40E_QTX_ENA_QENA_REQ_MASK;
4059 }
4060
4061 wr32(hw, I40E_QTX_ENA(pf_q), tx_reg);
4062}
4063
4064/**
Björn Töpel74608d12017-05-24 07:55:35 +02004065 * i40e_control_wait_tx_q - Start/stop Tx queue and wait for completion
4066 * @seid: VSI SEID
4067 * @pf: the PF structure
4068 * @pf_q: the PF queue to configure
4069 * @is_xdp: true if the queue is used for XDP
4070 * @enable: start or stop the queue
4071 **/
4072static int i40e_control_wait_tx_q(int seid, struct i40e_pf *pf, int pf_q,
4073 bool is_xdp, bool enable)
4074{
4075 int ret;
4076
4077 i40e_control_tx_q(pf, pf_q, enable);
4078
4079 /* wait for the change to finish */
4080 ret = i40e_pf_txq_wait(pf, pf_q, enable);
4081 if (ret) {
4082 dev_info(&pf->pdev->dev,
4083 "VSI seid %d %sTx ring %d %sable timeout\n",
4084 seid, (is_xdp ? "XDP " : ""), pf_q,
4085 (enable ? "en" : "dis"));
4086 }
4087
4088 return ret;
4089}
4090
4091/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004092 * i40e_vsi_control_tx - Start or stop a VSI's rings
4093 * @vsi: the VSI being configured
4094 * @enable: start or stop the rings
4095 **/
4096static int i40e_vsi_control_tx(struct i40e_vsi *vsi, bool enable)
4097{
4098 struct i40e_pf *pf = vsi->back;
Jacob Kellerc768e492017-04-13 04:45:47 -04004099 int i, pf_q, ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004100
4101 pf_q = vsi->base_queue;
4102 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
Björn Töpel74608d12017-05-24 07:55:35 +02004103 ret = i40e_control_wait_tx_q(vsi->seid, pf,
4104 pf_q,
4105 false /*is xdp*/, enable);
4106 if (ret)
Neerav Parikh23527302014-06-03 23:50:15 +00004107 break;
Björn Töpel74608d12017-05-24 07:55:35 +02004108
4109 if (!i40e_enabled_xdp_vsi(vsi))
4110 continue;
4111
4112 ret = i40e_control_wait_tx_q(vsi->seid, pf,
4113 pf_q + vsi->alloc_queue_pairs,
4114 true /*is xdp*/, enable);
4115 if (ret)
4116 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004117 }
4118
Neerav Parikh23527302014-06-03 23:50:15 +00004119 return ret;
4120}
4121
4122/**
4123 * i40e_pf_rxq_wait - Wait for a PF's Rx queue to be enabled or disabled
4124 * @pf: the PF being configured
4125 * @pf_q: the PF queue
4126 * @enable: enable or disable state of the queue
4127 *
4128 * This routine will wait for the given Rx queue of the PF to reach the
4129 * enabled or disabled state.
4130 * Returns -ETIMEDOUT in case of failing to reach the requested state after
4131 * multiple retries; else will return 0 in case of success.
4132 **/
4133static int i40e_pf_rxq_wait(struct i40e_pf *pf, int pf_q, bool enable)
4134{
4135 int i;
4136 u32 rx_reg;
4137
4138 for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
4139 rx_reg = rd32(&pf->hw, I40E_QRX_ENA(pf_q));
4140 if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4141 break;
4142
Neerav Parikhf98a2002014-09-13 07:40:44 +00004143 usleep_range(10, 20);
Neerav Parikh23527302014-06-03 23:50:15 +00004144 }
4145 if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
4146 return -ETIMEDOUT;
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00004147
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004148 return 0;
4149}
4150
4151/**
Jacob Kellerc768e492017-04-13 04:45:47 -04004152 * i40e_control_rx_q - Start or stop a particular Rx queue
4153 * @pf: the PF structure
4154 * @pf_q: the PF queue to configure
4155 * @enable: start or stop the queue
4156 *
4157 * This function enables or disables a single queue. Note that any delay
4158 * required after the operation is expected to be handled by the caller of
4159 * this function.
4160 **/
4161static void i40e_control_rx_q(struct i40e_pf *pf, int pf_q, bool enable)
4162{
4163 struct i40e_hw *hw = &pf->hw;
4164 u32 rx_reg;
4165 int i;
4166
4167 for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
4168 rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
4169 if (((rx_reg >> I40E_QRX_ENA_QENA_REQ_SHIFT) & 1) ==
4170 ((rx_reg >> I40E_QRX_ENA_QENA_STAT_SHIFT) & 1))
4171 break;
4172 usleep_range(1000, 2000);
4173 }
4174
4175 /* Skip if the queue is already in the requested state */
4176 if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4177 return;
4178
4179 /* turn on/off the queue */
4180 if (enable)
4181 rx_reg |= I40E_QRX_ENA_QENA_REQ_MASK;
4182 else
4183 rx_reg &= ~I40E_QRX_ENA_QENA_REQ_MASK;
4184
4185 wr32(hw, I40E_QRX_ENA(pf_q), rx_reg);
4186}
4187
4188/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004189 * i40e_vsi_control_rx - Start or stop a VSI's rings
4190 * @vsi: the VSI being configured
4191 * @enable: start or stop the rings
4192 **/
4193static int i40e_vsi_control_rx(struct i40e_vsi *vsi, bool enable)
4194{
4195 struct i40e_pf *pf = vsi->back;
Jacob Kellerc768e492017-04-13 04:45:47 -04004196 int i, pf_q, ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004197
4198 pf_q = vsi->base_queue;
4199 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
Jacob Kellerc768e492017-04-13 04:45:47 -04004200 i40e_control_rx_q(pf, pf_q, enable);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004201
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004202 /* wait for the change to finish */
Neerav Parikh23527302014-06-03 23:50:15 +00004203 ret = i40e_pf_rxq_wait(pf, pf_q, enable);
4204 if (ret) {
4205 dev_info(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04004206 "VSI seid %d Rx ring %d %sable timeout\n",
4207 vsi->seid, pf_q, (enable ? "en" : "dis"));
Neerav Parikh23527302014-06-03 23:50:15 +00004208 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004209 }
4210 }
4211
Wyborny, Carolynd08a9f62017-03-28 08:00:48 -07004212 /* Due to HW errata, on Rx disable only, the register can indicate done
4213 * before it really is. Needs 50ms to be sure
4214 */
4215 if (!enable)
4216 mdelay(50);
4217
Neerav Parikh23527302014-06-03 23:50:15 +00004218 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004219}
4220
4221/**
Filip Sadowski3aa7b742016-10-11 15:26:58 -07004222 * i40e_vsi_start_rings - Start a VSI's rings
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004223 * @vsi: the VSI being configured
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004224 **/
Filip Sadowski3aa7b742016-10-11 15:26:58 -07004225int i40e_vsi_start_rings(struct i40e_vsi *vsi)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004226{
Anjali Singhai Jain3b867b22013-12-21 05:44:44 +00004227 int ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004228
4229 /* do rx first for enable and last for disable */
Filip Sadowski3aa7b742016-10-11 15:26:58 -07004230 ret = i40e_vsi_control_rx(vsi, true);
4231 if (ret)
4232 return ret;
4233 ret = i40e_vsi_control_tx(vsi, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004234
4235 return ret;
4236}
4237
4238/**
Filip Sadowski3aa7b742016-10-11 15:26:58 -07004239 * i40e_vsi_stop_rings - Stop a VSI's rings
4240 * @vsi: the VSI being configured
4241 **/
4242void i40e_vsi_stop_rings(struct i40e_vsi *vsi)
4243{
Jacob Keller34807562017-04-13 04:45:53 -04004244 /* When port TX is suspended, don't wait */
Jacob Keller0da36b92017-04-19 09:25:55 -04004245 if (test_bit(__I40E_PORT_SUSPENDED, vsi->back->state))
Jacob Keller34807562017-04-13 04:45:53 -04004246 return i40e_vsi_stop_rings_no_wait(vsi);
4247
Filip Sadowski3aa7b742016-10-11 15:26:58 -07004248 /* do rx first for enable and last for disable
4249 * Ignore return value, we need to shutdown whatever we can
4250 */
4251 i40e_vsi_control_tx(vsi, false);
4252 i40e_vsi_control_rx(vsi, false);
4253}
4254
4255/**
Jacob Kellere4b433f2017-04-13 04:45:52 -04004256 * i40e_vsi_stop_rings_no_wait - Stop a VSI's rings and do not delay
4257 * @vsi: the VSI being shutdown
4258 *
4259 * This function stops all the rings for a VSI but does not delay to verify
4260 * that rings have been disabled. It is expected that the caller is shutting
4261 * down multiple VSIs at once and will delay together for all the VSIs after
4262 * initiating the shutdown. This is particularly useful for shutting down lots
4263 * of VFs together. Otherwise, a large delay can be incurred while configuring
4264 * each VSI in serial.
4265 **/
4266void i40e_vsi_stop_rings_no_wait(struct i40e_vsi *vsi)
4267{
4268 struct i40e_pf *pf = vsi->back;
4269 int i, pf_q;
4270
4271 pf_q = vsi->base_queue;
4272 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4273 i40e_control_tx_q(pf, pf_q, false);
4274 i40e_control_rx_q(pf, pf_q, false);
4275 }
4276}
4277
4278/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004279 * i40e_vsi_free_irq - Free the irq association with the OS
4280 * @vsi: the VSI being configured
4281 **/
4282static void i40e_vsi_free_irq(struct i40e_vsi *vsi)
4283{
4284 struct i40e_pf *pf = vsi->back;
4285 struct i40e_hw *hw = &pf->hw;
4286 int base = vsi->base_vector;
4287 u32 val, qp;
4288 int i;
4289
4290 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4291 if (!vsi->q_vectors)
4292 return;
4293
Shannon Nelson63741842014-04-23 04:50:16 +00004294 if (!vsi->irqs_ready)
4295 return;
4296
4297 vsi->irqs_ready = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004298 for (i = 0; i < vsi->num_q_vectors; i++) {
Alan Brady96db7762016-09-14 16:24:38 -07004299 int irq_num;
4300 u16 vector;
4301
4302 vector = i + base;
4303 irq_num = pf->msix_entries[vector].vector;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004304
4305 /* free only the irqs that were actually requested */
Shannon Nelson78681b12013-11-28 06:39:36 +00004306 if (!vsi->q_vectors[i] ||
4307 !vsi->q_vectors[i]->num_ringpairs)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004308 continue;
4309
Alan Brady96db7762016-09-14 16:24:38 -07004310 /* clear the affinity notifier in the IRQ descriptor */
4311 irq_set_affinity_notifier(irq_num, NULL);
Jacob Keller759dc4a2017-07-14 09:10:10 -04004312 /* remove our suggested affinity mask for this IRQ */
Alan Brady96db7762016-09-14 16:24:38 -07004313 irq_set_affinity_hint(irq_num, NULL);
4314 synchronize_irq(irq_num);
4315 free_irq(irq_num, vsi->q_vectors[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004316
4317 /* Tear down the interrupt queue link list
4318 *
4319 * We know that they come in pairs and always
4320 * the Rx first, then the Tx. To clear the
4321 * link list, stick the EOL value into the
4322 * next_q field of the registers.
4323 */
4324 val = rd32(hw, I40E_PFINT_LNKLSTN(vector - 1));
4325 qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
4326 >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4327 val |= I40E_QUEUE_END_OF_LIST
4328 << I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4329 wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), val);
4330
4331 while (qp != I40E_QUEUE_END_OF_LIST) {
4332 u32 next;
4333
4334 val = rd32(hw, I40E_QINT_RQCTL(qp));
4335
4336 val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
4337 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
4338 I40E_QINT_RQCTL_CAUSE_ENA_MASK |
4339 I40E_QINT_RQCTL_INTEVENT_MASK);
4340
4341 val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
4342 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
4343
4344 wr32(hw, I40E_QINT_RQCTL(qp), val);
4345
4346 val = rd32(hw, I40E_QINT_TQCTL(qp));
4347
4348 next = (val & I40E_QINT_TQCTL_NEXTQ_INDX_MASK)
4349 >> I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT;
4350
4351 val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
4352 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
4353 I40E_QINT_TQCTL_CAUSE_ENA_MASK |
4354 I40E_QINT_TQCTL_INTEVENT_MASK);
4355
4356 val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
4357 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
4358
4359 wr32(hw, I40E_QINT_TQCTL(qp), val);
4360 qp = next;
4361 }
4362 }
4363 } else {
4364 free_irq(pf->pdev->irq, pf);
4365
4366 val = rd32(hw, I40E_PFINT_LNKLST0);
4367 qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
4368 >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4369 val |= I40E_QUEUE_END_OF_LIST
4370 << I40E_PFINT_LNKLST0_FIRSTQ_INDX_SHIFT;
4371 wr32(hw, I40E_PFINT_LNKLST0, val);
4372
4373 val = rd32(hw, I40E_QINT_RQCTL(qp));
4374 val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
4375 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
4376 I40E_QINT_RQCTL_CAUSE_ENA_MASK |
4377 I40E_QINT_RQCTL_INTEVENT_MASK);
4378
4379 val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
4380 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
4381
4382 wr32(hw, I40E_QINT_RQCTL(qp), val);
4383
4384 val = rd32(hw, I40E_QINT_TQCTL(qp));
4385
4386 val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
4387 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
4388 I40E_QINT_TQCTL_CAUSE_ENA_MASK |
4389 I40E_QINT_TQCTL_INTEVENT_MASK);
4390
4391 val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
4392 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
4393
4394 wr32(hw, I40E_QINT_TQCTL(qp), val);
4395 }
4396}
4397
4398/**
Alexander Duyck493fb302013-09-28 07:01:44 +00004399 * i40e_free_q_vector - Free memory allocated for specific interrupt vector
4400 * @vsi: the VSI being configured
4401 * @v_idx: Index of vector to be freed
4402 *
4403 * This function frees the memory allocated to the q_vector. In addition if
4404 * NAPI is enabled it will delete any references to the NAPI struct prior
4405 * to freeing the q_vector.
4406 **/
4407static void i40e_free_q_vector(struct i40e_vsi *vsi, int v_idx)
4408{
4409 struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00004410 struct i40e_ring *ring;
Alexander Duyck493fb302013-09-28 07:01:44 +00004411
4412 if (!q_vector)
4413 return;
4414
4415 /* disassociate q_vector from rings */
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00004416 i40e_for_each_ring(ring, q_vector->tx)
4417 ring->q_vector = NULL;
4418
4419 i40e_for_each_ring(ring, q_vector->rx)
4420 ring->q_vector = NULL;
Alexander Duyck493fb302013-09-28 07:01:44 +00004421
4422 /* only VSI w/ an associated netdev is set up w/ NAPI */
4423 if (vsi->netdev)
4424 netif_napi_del(&q_vector->napi);
4425
4426 vsi->q_vectors[v_idx] = NULL;
4427
4428 kfree_rcu(q_vector, rcu);
4429}
4430
4431/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004432 * i40e_vsi_free_q_vectors - Free memory allocated for interrupt vectors
4433 * @vsi: the VSI being un-configured
4434 *
4435 * This frees the memory allocated to the q_vectors and
4436 * deletes references to the NAPI struct.
4437 **/
4438static void i40e_vsi_free_q_vectors(struct i40e_vsi *vsi)
4439{
4440 int v_idx;
4441
Alexander Duyck493fb302013-09-28 07:01:44 +00004442 for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++)
4443 i40e_free_q_vector(vsi, v_idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004444}
4445
4446/**
4447 * i40e_reset_interrupt_capability - Disable interrupt setup in OS
4448 * @pf: board private structure
4449 **/
4450static void i40e_reset_interrupt_capability(struct i40e_pf *pf)
4451{
4452 /* If we're in Legacy mode, the interrupt was cleaned in vsi_close */
4453 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4454 pci_disable_msix(pf->pdev);
4455 kfree(pf->msix_entries);
4456 pf->msix_entries = NULL;
Shannon Nelson3b444392015-02-26 16:15:57 +00004457 kfree(pf->irq_pile);
4458 pf->irq_pile = NULL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004459 } else if (pf->flags & I40E_FLAG_MSI_ENABLED) {
4460 pci_disable_msi(pf->pdev);
4461 }
4462 pf->flags &= ~(I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
4463}
4464
4465/**
4466 * i40e_clear_interrupt_scheme - Clear the current interrupt scheme settings
4467 * @pf: board private structure
4468 *
4469 * We go through and clear interrupt specific resources and reset the structure
4470 * to pre-load conditions
4471 **/
4472static void i40e_clear_interrupt_scheme(struct i40e_pf *pf)
4473{
4474 int i;
4475
Jacob Kellerc17401a2017-07-14 09:27:02 -04004476 i40e_free_misc_vector(pf);
Shannon Nelsone1477582015-02-21 06:44:33 +00004477
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06004478 i40e_put_lump(pf->irq_pile, pf->iwarp_base_vector,
4479 I40E_IWARP_IRQ_PILE_ID);
4480
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004481 i40e_put_lump(pf->irq_pile, 0, I40E_PILE_VALID_BIT-1);
Mitch Williams505682c2014-05-20 08:01:37 +00004482 for (i = 0; i < pf->num_alloc_vsi; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004483 if (pf->vsi[i])
4484 i40e_vsi_free_q_vectors(pf->vsi[i]);
4485 i40e_reset_interrupt_capability(pf);
4486}
4487
4488/**
4489 * i40e_napi_enable_all - Enable NAPI for all q_vectors in the VSI
4490 * @vsi: the VSI being configured
4491 **/
4492static void i40e_napi_enable_all(struct i40e_vsi *vsi)
4493{
4494 int q_idx;
4495
4496 if (!vsi->netdev)
4497 return;
4498
Alexander Duyck13a8cd12017-03-24 15:01:42 -07004499 for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
4500 struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
4501
4502 if (q_vector->rx.ring || q_vector->tx.ring)
4503 napi_enable(&q_vector->napi);
4504 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004505}
4506
4507/**
4508 * i40e_napi_disable_all - Disable NAPI for all q_vectors in the VSI
4509 * @vsi: the VSI being configured
4510 **/
4511static void i40e_napi_disable_all(struct i40e_vsi *vsi)
4512{
4513 int q_idx;
4514
4515 if (!vsi->netdev)
4516 return;
4517
Alexander Duyck13a8cd12017-03-24 15:01:42 -07004518 for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
4519 struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
4520
4521 if (q_vector->rx.ring || q_vector->tx.ring)
4522 napi_disable(&q_vector->napi);
4523 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004524}
4525
4526/**
Shannon Nelson90ef8d42014-03-14 07:32:26 +00004527 * i40e_vsi_close - Shut down a VSI
4528 * @vsi: the vsi to be quelled
4529 **/
4530static void i40e_vsi_close(struct i40e_vsi *vsi)
4531{
Mitch Williams0ef2d5a2017-01-24 10:24:00 -08004532 struct i40e_pf *pf = vsi->back;
Jacob Keller0da36b92017-04-19 09:25:55 -04004533 if (!test_and_set_bit(__I40E_VSI_DOWN, vsi->state))
Shannon Nelson90ef8d42014-03-14 07:32:26 +00004534 i40e_down(vsi);
4535 i40e_vsi_free_irq(vsi);
4536 i40e_vsi_free_tx_resources(vsi);
4537 i40e_vsi_free_rx_resources(vsi);
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04004538 vsi->current_netdev_flags = 0;
Mitch Williams0ef2d5a2017-01-24 10:24:00 -08004539 pf->flags |= I40E_FLAG_SERVICE_CLIENT_REQUESTED;
Jacob Keller0da36b92017-04-19 09:25:55 -04004540 if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
Mitch Williams0ef2d5a2017-01-24 10:24:00 -08004541 pf->flags |= I40E_FLAG_CLIENT_RESET;
Shannon Nelson90ef8d42014-03-14 07:32:26 +00004542}
4543
4544/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004545 * i40e_quiesce_vsi - Pause a given VSI
4546 * @vsi: the VSI being paused
4547 **/
4548static void i40e_quiesce_vsi(struct i40e_vsi *vsi)
4549{
Jacob Keller0da36b92017-04-19 09:25:55 -04004550 if (test_bit(__I40E_VSI_DOWN, vsi->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004551 return;
4552
Jacob Keller0da36b92017-04-19 09:25:55 -04004553 set_bit(__I40E_VSI_NEEDS_RESTART, vsi->state);
Jesse Brandeburg6995b362015-08-28 17:55:54 -04004554 if (vsi->netdev && netif_running(vsi->netdev))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004555 vsi->netdev->netdev_ops->ndo_stop(vsi->netdev);
Jesse Brandeburg6995b362015-08-28 17:55:54 -04004556 else
Shannon Nelson90ef8d42014-03-14 07:32:26 +00004557 i40e_vsi_close(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004558}
4559
4560/**
4561 * i40e_unquiesce_vsi - Resume a given VSI
4562 * @vsi: the VSI being resumed
4563 **/
4564static void i40e_unquiesce_vsi(struct i40e_vsi *vsi)
4565{
Jacob Keller0da36b92017-04-19 09:25:55 -04004566 if (!test_and_clear_bit(__I40E_VSI_NEEDS_RESTART, vsi->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004567 return;
4568
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004569 if (vsi->netdev && netif_running(vsi->netdev))
4570 vsi->netdev->netdev_ops->ndo_open(vsi->netdev);
4571 else
Shannon Nelson8276f752014-03-14 07:32:27 +00004572 i40e_vsi_open(vsi); /* this clears the DOWN bit */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004573}
4574
4575/**
4576 * i40e_pf_quiesce_all_vsi - Pause all VSIs on a PF
4577 * @pf: the PF
4578 **/
4579static void i40e_pf_quiesce_all_vsi(struct i40e_pf *pf)
4580{
4581 int v;
4582
Mitch Williams505682c2014-05-20 08:01:37 +00004583 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004584 if (pf->vsi[v])
4585 i40e_quiesce_vsi(pf->vsi[v]);
4586 }
4587}
4588
4589/**
4590 * i40e_pf_unquiesce_all_vsi - Resume all VSIs on a PF
4591 * @pf: the PF
4592 **/
4593static void i40e_pf_unquiesce_all_vsi(struct i40e_pf *pf)
4594{
4595 int v;
4596
Mitch Williams505682c2014-05-20 08:01:37 +00004597 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004598 if (pf->vsi[v])
4599 i40e_unquiesce_vsi(pf->vsi[v]);
4600 }
4601}
4602
Neerav Parikh69129dc2014-11-12 00:18:46 +00004603/**
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004604 * i40e_vsi_wait_queues_disabled - Wait for VSI's queues to be disabled
Neerav Parikh69129dc2014-11-12 00:18:46 +00004605 * @vsi: the VSI being configured
4606 *
Jacob Kelleraf26ce22017-04-05 07:50:58 -04004607 * Wait until all queues on a given VSI have been disabled.
Neerav Parikh69129dc2014-11-12 00:18:46 +00004608 **/
Jacob Kellere4b433f2017-04-13 04:45:52 -04004609int i40e_vsi_wait_queues_disabled(struct i40e_vsi *vsi)
Neerav Parikh69129dc2014-11-12 00:18:46 +00004610{
4611 struct i40e_pf *pf = vsi->back;
4612 int i, pf_q, ret;
4613
4614 pf_q = vsi->base_queue;
4615 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
Jacob Kelleraf26ce22017-04-05 07:50:58 -04004616 /* Check and wait for the Tx queue */
Neerav Parikh69129dc2014-11-12 00:18:46 +00004617 ret = i40e_pf_txq_wait(pf, pf_q, false);
4618 if (ret) {
4619 dev_info(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04004620 "VSI seid %d Tx ring %d disable timeout\n",
4621 vsi->seid, pf_q);
Neerav Parikh69129dc2014-11-12 00:18:46 +00004622 return ret;
4623 }
Björn Töpel74608d12017-05-24 07:55:35 +02004624
4625 if (!i40e_enabled_xdp_vsi(vsi))
4626 goto wait_rx;
4627
4628 /* Check and wait for the XDP Tx queue */
4629 ret = i40e_pf_txq_wait(pf, pf_q + vsi->alloc_queue_pairs,
4630 false);
4631 if (ret) {
4632 dev_info(&pf->pdev->dev,
4633 "VSI seid %d XDP Tx ring %d disable timeout\n",
4634 vsi->seid, pf_q);
4635 return ret;
4636 }
4637wait_rx:
4638 /* Check and wait for the Rx queue */
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004639 ret = i40e_pf_rxq_wait(pf, pf_q, false);
4640 if (ret) {
4641 dev_info(&pf->pdev->dev,
4642 "VSI seid %d Rx ring %d disable timeout\n",
4643 vsi->seid, pf_q);
4644 return ret;
4645 }
4646 }
4647
Neerav Parikh69129dc2014-11-12 00:18:46 +00004648 return 0;
4649}
4650
Jacob Kellere4b433f2017-04-13 04:45:52 -04004651#ifdef CONFIG_I40E_DCB
Neerav Parikh69129dc2014-11-12 00:18:46 +00004652/**
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004653 * i40e_pf_wait_queues_disabled - Wait for all queues of PF VSIs to be disabled
Neerav Parikh69129dc2014-11-12 00:18:46 +00004654 * @pf: the PF
4655 *
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004656 * This function waits for the queues to be in disabled state for all the
Neerav Parikh69129dc2014-11-12 00:18:46 +00004657 * VSIs that are managed by this PF.
4658 **/
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004659static int i40e_pf_wait_queues_disabled(struct i40e_pf *pf)
Neerav Parikh69129dc2014-11-12 00:18:46 +00004660{
4661 int v, ret = 0;
4662
4663 for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
Alexander Duyckc76cb6e2017-02-21 15:55:48 -08004664 if (pf->vsi[v]) {
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004665 ret = i40e_vsi_wait_queues_disabled(pf->vsi[v]);
Neerav Parikh69129dc2014-11-12 00:18:46 +00004666 if (ret)
4667 break;
4668 }
4669 }
4670
4671 return ret;
4672}
4673
4674#endif
Kiran Patilb03a8c12015-09-24 18:13:15 -04004675
4676/**
4677 * i40e_detect_recover_hung_queue - Function to detect and recover hung_queue
4678 * @q_idx: TX queue number
4679 * @vsi: Pointer to VSI struct
4680 *
4681 * This function checks specified queue for given VSI. Detects hung condition.
Alan Brady17daabb2017-04-05 07:50:56 -04004682 * We proactively detect hung TX queues by checking if interrupts are disabled
4683 * but there are pending descriptors. If it appears hung, attempt to recover
4684 * by triggering a SW interrupt.
Kiran Patilb03a8c12015-09-24 18:13:15 -04004685 **/
4686static void i40e_detect_recover_hung_queue(int q_idx, struct i40e_vsi *vsi)
4687{
4688 struct i40e_ring *tx_ring = NULL;
4689 struct i40e_pf *pf;
Alan Brady17daabb2017-04-05 07:50:56 -04004690 u32 val, tx_pending;
Kiran Patilb03a8c12015-09-24 18:13:15 -04004691 int i;
4692
4693 pf = vsi->back;
4694
4695 /* now that we have an index, find the tx_ring struct */
4696 for (i = 0; i < vsi->num_queue_pairs; i++) {
4697 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc) {
4698 if (q_idx == vsi->tx_rings[i]->queue_index) {
4699 tx_ring = vsi->tx_rings[i];
4700 break;
4701 }
4702 }
4703 }
4704
4705 if (!tx_ring)
4706 return;
4707
4708 /* Read interrupt register */
4709 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
4710 val = rd32(&pf->hw,
4711 I40E_PFINT_DYN_CTLN(tx_ring->q_vector->v_idx +
4712 tx_ring->vsi->base_vector - 1));
4713 else
4714 val = rd32(&pf->hw, I40E_PFINT_DYN_CTL0);
4715
Alan Brady17daabb2017-04-05 07:50:56 -04004716 tx_pending = i40e_get_tx_pending(tx_ring);
Kiran Patilb03a8c12015-09-24 18:13:15 -04004717
Alan Brady17daabb2017-04-05 07:50:56 -04004718 /* Interrupts are disabled and TX pending is non-zero,
4719 * trigger the SW interrupt (don't wait). Worst case
4720 * there will be one extra interrupt which may result
4721 * into not cleaning any queues because queues are cleaned.
Kiran Patilb03a8c12015-09-24 18:13:15 -04004722 */
Alan Brady17daabb2017-04-05 07:50:56 -04004723 if (tx_pending && (!(val & I40E_PFINT_DYN_CTLN_INTENA_MASK)))
4724 i40e_force_wb(vsi, tx_ring->q_vector);
Kiran Patilb03a8c12015-09-24 18:13:15 -04004725}
4726
4727/**
4728 * i40e_detect_recover_hung - Function to detect and recover hung_queues
4729 * @pf: pointer to PF struct
4730 *
4731 * LAN VSI has netdev and netdev has TX queues. This function is to check
4732 * each of those TX queues if they are hung, trigger recovery by issuing
4733 * SW interrupt.
4734 **/
4735static void i40e_detect_recover_hung(struct i40e_pf *pf)
4736{
4737 struct net_device *netdev;
4738 struct i40e_vsi *vsi;
Jesse Brandeburgb85c94b2017-06-20 15:16:59 -07004739 unsigned int i;
Kiran Patilb03a8c12015-09-24 18:13:15 -04004740
4741 /* Only for LAN VSI */
4742 vsi = pf->vsi[pf->lan_vsi];
4743
4744 if (!vsi)
4745 return;
4746
4747 /* Make sure, VSI state is not DOWN/RECOVERY_PENDING */
Jacob Keller0da36b92017-04-19 09:25:55 -04004748 if (test_bit(__I40E_VSI_DOWN, vsi->back->state) ||
4749 test_bit(__I40E_RESET_RECOVERY_PENDING, vsi->back->state))
Kiran Patilb03a8c12015-09-24 18:13:15 -04004750 return;
4751
4752 /* Make sure type is MAIN VSI */
4753 if (vsi->type != I40E_VSI_MAIN)
4754 return;
4755
4756 netdev = vsi->netdev;
4757 if (!netdev)
4758 return;
4759
4760 /* Bail out if netif_carrier is not OK */
4761 if (!netif_carrier_ok(netdev))
4762 return;
4763
4764 /* Go thru' TX queues for netdev */
4765 for (i = 0; i < netdev->num_tx_queues; i++) {
4766 struct netdev_queue *q;
4767
4768 q = netdev_get_tx_queue(netdev, i);
4769 if (q)
4770 i40e_detect_recover_hung_queue(i, vsi);
4771 }
4772}
4773
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004774/**
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004775 * i40e_get_iscsi_tc_map - Return TC map for iSCSI APP
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00004776 * @pf: pointer to PF
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004777 *
4778 * Get TC map for ISCSI PF type that will include iSCSI TC
4779 * and LAN TC.
4780 **/
4781static u8 i40e_get_iscsi_tc_map(struct i40e_pf *pf)
4782{
4783 struct i40e_dcb_app_priority_table app;
4784 struct i40e_hw *hw = &pf->hw;
4785 u8 enabled_tc = 1; /* TC0 is always enabled */
4786 u8 tc, i;
4787 /* Get the iSCSI APP TLV */
4788 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
4789
4790 for (i = 0; i < dcbcfg->numapps; i++) {
4791 app = dcbcfg->app[i];
4792 if (app.selector == I40E_APP_SEL_TCPIP &&
4793 app.protocolid == I40E_APP_PROTOID_ISCSI) {
4794 tc = dcbcfg->etscfg.prioritytable[app.priority];
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08004795 enabled_tc |= BIT(tc);
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004796 break;
4797 }
4798 }
4799
4800 return enabled_tc;
4801}
4802
4803/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004804 * i40e_dcb_get_num_tc - Get the number of TCs from DCBx config
4805 * @dcbcfg: the corresponding DCBx configuration structure
4806 *
4807 * Return the number of TCs from given DCBx configuration
4808 **/
4809static u8 i40e_dcb_get_num_tc(struct i40e_dcbx_config *dcbcfg)
4810{
Dave Ertmanfbfe12c2016-08-12 09:56:32 -07004811 int i, tc_unused = 0;
Jesse Brandeburg078b5872013-09-25 23:41:14 +00004812 u8 num_tc = 0;
Dave Ertmanfbfe12c2016-08-12 09:56:32 -07004813 u8 ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004814
4815 /* Scan the ETS Config Priority Table to find
4816 * traffic class enabled for a given priority
Dave Ertmanfbfe12c2016-08-12 09:56:32 -07004817 * and create a bitmask of enabled TCs
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004818 */
Dave Ertmanfbfe12c2016-08-12 09:56:32 -07004819 for (i = 0; i < I40E_MAX_USER_PRIORITY; i++)
4820 num_tc |= BIT(dcbcfg->etscfg.prioritytable[i]);
4821
4822 /* Now scan the bitmask to check for
4823 * contiguous TCs starting with TC0
4824 */
4825 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
4826 if (num_tc & BIT(i)) {
4827 if (!tc_unused) {
4828 ret++;
4829 } else {
4830 pr_err("Non-contiguous TC - Disabling DCB\n");
4831 return 1;
4832 }
4833 } else {
4834 tc_unused = 1;
4835 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004836 }
4837
Dave Ertmanfbfe12c2016-08-12 09:56:32 -07004838 /* There is always at least TC0 */
4839 if (!ret)
4840 ret = 1;
4841
4842 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004843}
4844
4845/**
4846 * i40e_dcb_get_enabled_tc - Get enabled traffic classes
4847 * @dcbcfg: the corresponding DCBx configuration structure
4848 *
4849 * Query the current DCB configuration and return the number of
4850 * traffic classes enabled from the given DCBX config
4851 **/
4852static u8 i40e_dcb_get_enabled_tc(struct i40e_dcbx_config *dcbcfg)
4853{
4854 u8 num_tc = i40e_dcb_get_num_tc(dcbcfg);
4855 u8 enabled_tc = 1;
4856 u8 i;
4857
4858 for (i = 0; i < num_tc; i++)
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04004859 enabled_tc |= BIT(i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004860
4861 return enabled_tc;
4862}
4863
4864/**
4865 * i40e_pf_get_num_tc - Get enabled traffic classes for PF
4866 * @pf: PF being queried
4867 *
4868 * Return number of traffic classes enabled for the given PF
4869 **/
4870static u8 i40e_pf_get_num_tc(struct i40e_pf *pf)
4871{
4872 struct i40e_hw *hw = &pf->hw;
Dave Ertman52a08ca2016-07-27 12:02:34 -07004873 u8 i, enabled_tc = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004874 u8 num_tc = 0;
4875 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
4876
4877 /* If DCB is not enabled then always in single TC */
4878 if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
4879 return 1;
4880
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004881 /* SFP mode will be enabled for all TCs on port */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004882 if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
4883 return i40e_dcb_get_num_tc(dcbcfg);
4884
4885 /* MFP mode return count of enabled TCs for this PF */
4886 if (pf->hw.func_caps.iscsi)
4887 enabled_tc = i40e_get_iscsi_tc_map(pf);
4888 else
Neerav Parikhfc51de92015-02-24 06:58:53 +00004889 return 1; /* Only TC0 */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004890
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004891 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08004892 if (enabled_tc & BIT(i))
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004893 num_tc++;
4894 }
4895 return num_tc;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004896}
4897
4898/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004899 * i40e_pf_get_pf_tc_map - Get bitmap for enabled traffic classes
4900 * @pf: PF being queried
4901 *
4902 * Return a bitmap for enabled traffic classes for this PF.
4903 **/
4904static u8 i40e_pf_get_tc_map(struct i40e_pf *pf)
4905{
4906 /* If DCB is not enabled for this PF then just return default TC */
4907 if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
David Ertmanea6acb72016-09-20 07:10:50 -07004908 return I40E_DEFAULT_TRAFFIC_CLASS;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004909
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004910 /* SFP mode we want PF to be enabled for all TCs */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004911 if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
4912 return i40e_dcb_get_enabled_tc(&pf->hw.local_dcbx_config);
4913
Neerav Parikhfc51de92015-02-24 06:58:53 +00004914 /* MFP enabled and iSCSI PF type */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004915 if (pf->hw.func_caps.iscsi)
4916 return i40e_get_iscsi_tc_map(pf);
4917 else
David Ertmanea6acb72016-09-20 07:10:50 -07004918 return I40E_DEFAULT_TRAFFIC_CLASS;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004919}
4920
4921/**
4922 * i40e_vsi_get_bw_info - Query VSI BW Information
4923 * @vsi: the VSI being queried
4924 *
4925 * Returns 0 on success, negative value on failure
4926 **/
4927static int i40e_vsi_get_bw_info(struct i40e_vsi *vsi)
4928{
4929 struct i40e_aqc_query_vsi_ets_sla_config_resp bw_ets_config = {0};
4930 struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
4931 struct i40e_pf *pf = vsi->back;
4932 struct i40e_hw *hw = &pf->hw;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004933 i40e_status ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004934 u32 tc_bw_max;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004935 int i;
4936
4937 /* Get the VSI level BW configuration */
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004938 ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid, &bw_config, NULL);
4939 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004940 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004941 "couldn't get PF vsi bw config, err %s aq_err %s\n",
4942 i40e_stat_str(&pf->hw, ret),
4943 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004944 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004945 }
4946
4947 /* Get the VSI level BW configuration per TC */
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004948 ret = i40e_aq_query_vsi_ets_sla_config(hw, vsi->seid, &bw_ets_config,
4949 NULL);
4950 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004951 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004952 "couldn't get PF vsi ets bw config, err %s aq_err %s\n",
4953 i40e_stat_str(&pf->hw, ret),
4954 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004955 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004956 }
4957
4958 if (bw_config.tc_valid_bits != bw_ets_config.tc_valid_bits) {
4959 dev_info(&pf->pdev->dev,
4960 "Enabled TCs mismatch from querying VSI BW info 0x%08x 0x%08x\n",
4961 bw_config.tc_valid_bits,
4962 bw_ets_config.tc_valid_bits);
4963 /* Still continuing */
4964 }
4965
4966 vsi->bw_limit = le16_to_cpu(bw_config.port_bw_limit);
4967 vsi->bw_max_quanta = bw_config.max_bw;
4968 tc_bw_max = le16_to_cpu(bw_ets_config.tc_bw_max[0]) |
4969 (le16_to_cpu(bw_ets_config.tc_bw_max[1]) << 16);
4970 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
4971 vsi->bw_ets_share_credits[i] = bw_ets_config.share_credits[i];
4972 vsi->bw_ets_limit_credits[i] =
4973 le16_to_cpu(bw_ets_config.credits[i]);
4974 /* 3 bits out of 4 for each TC */
4975 vsi->bw_ets_max_quanta[i] = (u8)((tc_bw_max >> (i*4)) & 0x7);
4976 }
Jesse Brandeburg078b5872013-09-25 23:41:14 +00004977
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004978 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004979}
4980
4981/**
4982 * i40e_vsi_configure_bw_alloc - Configure VSI BW allocation per TC
4983 * @vsi: the VSI being configured
4984 * @enabled_tc: TC bitmap
4985 * @bw_credits: BW shared credits per TC
4986 *
4987 * Returns 0 on success, negative value on failure
4988 **/
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004989static int i40e_vsi_configure_bw_alloc(struct i40e_vsi *vsi, u8 enabled_tc,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004990 u8 *bw_share)
4991{
4992 struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004993 i40e_status ret;
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004994 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004995
4996 bw_data.tc_valid_bits = enabled_tc;
4997 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
4998 bw_data.tc_bw_credits[i] = bw_share[i];
4999
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005000 ret = i40e_aq_config_vsi_tc_bw(&vsi->back->hw, vsi->seid, &bw_data,
5001 NULL);
5002 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005003 dev_info(&vsi->back->pdev->dev,
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005004 "AQ command Config VSI BW allocation per TC failed = %d\n",
5005 vsi->back->hw.aq.asq_last_status);
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00005006 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005007 }
5008
5009 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5010 vsi->info.qs_handle[i] = bw_data.qs_handles[i];
5011
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00005012 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005013}
5014
5015/**
5016 * i40e_vsi_config_netdev_tc - Setup the netdev TC configuration
5017 * @vsi: the VSI being configured
5018 * @enabled_tc: TC map to be enabled
5019 *
5020 **/
5021static void i40e_vsi_config_netdev_tc(struct i40e_vsi *vsi, u8 enabled_tc)
5022{
5023 struct net_device *netdev = vsi->netdev;
5024 struct i40e_pf *pf = vsi->back;
5025 struct i40e_hw *hw = &pf->hw;
5026 u8 netdev_tc = 0;
5027 int i;
5028 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5029
5030 if (!netdev)
5031 return;
5032
5033 if (!enabled_tc) {
5034 netdev_reset_tc(netdev);
5035 return;
5036 }
5037
5038 /* Set up actual enabled TCs on the VSI */
5039 if (netdev_set_num_tc(netdev, vsi->tc_config.numtc))
5040 return;
5041
5042 /* set per TC queues for the VSI */
5043 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5044 /* Only set TC queues for enabled tcs
5045 *
5046 * e.g. For a VSI that has TC0 and TC3 enabled the
5047 * enabled_tc bitmap would be 0x00001001; the driver
5048 * will set the numtc for netdev as 2 that will be
5049 * referenced by the netdev layer as TC 0 and 1.
5050 */
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08005051 if (vsi->tc_config.enabled_tc & BIT(i))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005052 netdev_set_tc_queue(netdev,
5053 vsi->tc_config.tc_info[i].netdev_tc,
5054 vsi->tc_config.tc_info[i].qcount,
5055 vsi->tc_config.tc_info[i].qoffset);
5056 }
5057
5058 /* Assign UP2TC map for the VSI */
5059 for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
5060 /* Get the actual TC# for the UP */
5061 u8 ets_tc = dcbcfg->etscfg.prioritytable[i];
5062 /* Get the mapped netdev TC# for the UP */
5063 netdev_tc = vsi->tc_config.tc_info[ets_tc].netdev_tc;
5064 netdev_set_prio_tc_map(netdev, i, netdev_tc);
5065 }
5066}
5067
5068/**
5069 * i40e_vsi_update_queue_map - Update our copy of VSi info with new queue map
5070 * @vsi: the VSI being configured
5071 * @ctxt: the ctxt buffer returned from AQ VSI update param command
5072 **/
5073static void i40e_vsi_update_queue_map(struct i40e_vsi *vsi,
5074 struct i40e_vsi_context *ctxt)
5075{
5076 /* copy just the sections touched not the entire info
5077 * since not all sections are valid as returned by
5078 * update vsi params
5079 */
5080 vsi->info.mapping_flags = ctxt->info.mapping_flags;
5081 memcpy(&vsi->info.queue_mapping,
5082 &ctxt->info.queue_mapping, sizeof(vsi->info.queue_mapping));
5083 memcpy(&vsi->info.tc_mapping, ctxt->info.tc_mapping,
5084 sizeof(vsi->info.tc_mapping));
5085}
5086
5087/**
5088 * i40e_vsi_config_tc - Configure VSI Tx Scheduler for given TC map
5089 * @vsi: VSI to be configured
5090 * @enabled_tc: TC bitmap
5091 *
5092 * This configures a particular VSI for TCs that are mapped to the
5093 * given TC bitmap. It uses default bandwidth share for TCs across
5094 * VSIs to configure TC for a particular VSI.
5095 *
5096 * NOTE:
5097 * It is expected that the VSI queues have been quisced before calling
5098 * this function.
5099 **/
5100static int i40e_vsi_config_tc(struct i40e_vsi *vsi, u8 enabled_tc)
5101{
5102 u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
5103 struct i40e_vsi_context ctxt;
5104 int ret = 0;
5105 int i;
5106
5107 /* Check if enabled_tc is same as existing or new TCs */
5108 if (vsi->tc_config.enabled_tc == enabled_tc)
5109 return ret;
5110
5111 /* Enable ETS TCs with equal BW Share for now across all VSIs */
5112 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08005113 if (enabled_tc & BIT(i))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005114 bw_share[i] = 1;
5115 }
5116
5117 ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
5118 if (ret) {
5119 dev_info(&vsi->back->pdev->dev,
5120 "Failed configuring TC map %d for VSI %d\n",
5121 enabled_tc, vsi->seid);
5122 goto out;
5123 }
5124
5125 /* Update Queue Pairs Mapping for currently enabled UPs */
5126 ctxt.seid = vsi->seid;
5127 ctxt.pf_num = vsi->back->hw.pf_id;
5128 ctxt.vf_num = 0;
5129 ctxt.uplink_seid = vsi->uplink_seid;
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07005130 ctxt.info = vsi->info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005131 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
5132
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06005133 if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
5134 ctxt.info.valid_sections |=
5135 cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
5136 ctxt.info.queueing_opt_flags |= I40E_AQ_VSI_QUE_OPT_TCP_ENA;
5137 }
5138
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005139 /* Update the VSI after updating the VSI queue-mapping information */
5140 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
5141 if (ret) {
5142 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005143 "Update vsi tc config failed, err %s aq_err %s\n",
5144 i40e_stat_str(&vsi->back->hw, ret),
5145 i40e_aq_str(&vsi->back->hw,
5146 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005147 goto out;
5148 }
5149 /* update the local VSI info with updated queue map */
5150 i40e_vsi_update_queue_map(vsi, &ctxt);
5151 vsi->info.valid_sections = 0;
5152
5153 /* Update current VSI BW information */
5154 ret = i40e_vsi_get_bw_info(vsi);
5155 if (ret) {
5156 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005157 "Failed updating vsi bw info, err %s aq_err %s\n",
5158 i40e_stat_str(&vsi->back->hw, ret),
5159 i40e_aq_str(&vsi->back->hw,
5160 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005161 goto out;
5162 }
5163
5164 /* Update the netdev TC setup */
5165 i40e_vsi_config_netdev_tc(vsi, enabled_tc);
5166out:
5167 return ret;
5168}
5169
5170/**
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005171 * i40e_veb_config_tc - Configure TCs for given VEB
5172 * @veb: given VEB
5173 * @enabled_tc: TC bitmap
5174 *
5175 * Configures given TC bitmap for VEB (switching) element
5176 **/
5177int i40e_veb_config_tc(struct i40e_veb *veb, u8 enabled_tc)
5178{
5179 struct i40e_aqc_configure_switching_comp_bw_config_data bw_data = {0};
5180 struct i40e_pf *pf = veb->pf;
5181 int ret = 0;
5182 int i;
5183
5184 /* No TCs or already enabled TCs just return */
5185 if (!enabled_tc || veb->enabled_tc == enabled_tc)
5186 return ret;
5187
5188 bw_data.tc_valid_bits = enabled_tc;
5189 /* bw_data.absolute_credits is not set (relative) */
5190
5191 /* Enable ETS TCs with equal BW Share for now */
5192 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08005193 if (enabled_tc & BIT(i))
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005194 bw_data.tc_bw_share_credits[i] = 1;
5195 }
5196
5197 ret = i40e_aq_config_switch_comp_bw_config(&pf->hw, veb->seid,
5198 &bw_data, NULL);
5199 if (ret) {
5200 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005201 "VEB bw config failed, err %s aq_err %s\n",
5202 i40e_stat_str(&pf->hw, ret),
5203 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005204 goto out;
5205 }
5206
5207 /* Update the BW information */
5208 ret = i40e_veb_get_bw_info(veb);
5209 if (ret) {
5210 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005211 "Failed getting veb bw config, err %s aq_err %s\n",
5212 i40e_stat_str(&pf->hw, ret),
5213 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005214 }
5215
5216out:
5217 return ret;
5218}
5219
5220#ifdef CONFIG_I40E_DCB
5221/**
5222 * i40e_dcb_reconfigure - Reconfigure all VEBs and VSIs
5223 * @pf: PF struct
5224 *
5225 * Reconfigure VEB/VSIs on a given PF; it is assumed that
5226 * the caller would've quiesce all the VSIs before calling
5227 * this function
5228 **/
5229static void i40e_dcb_reconfigure(struct i40e_pf *pf)
5230{
5231 u8 tc_map = 0;
5232 int ret;
5233 u8 v;
5234
5235 /* Enable the TCs available on PF to all VEBs */
5236 tc_map = i40e_pf_get_tc_map(pf);
5237 for (v = 0; v < I40E_MAX_VEB; v++) {
5238 if (!pf->veb[v])
5239 continue;
5240 ret = i40e_veb_config_tc(pf->veb[v], tc_map);
5241 if (ret) {
5242 dev_info(&pf->pdev->dev,
5243 "Failed configuring TC for VEB seid=%d\n",
5244 pf->veb[v]->seid);
5245 /* Will try to configure as many components */
5246 }
5247 }
5248
5249 /* Update each VSI */
Mitch Williams505682c2014-05-20 08:01:37 +00005250 for (v = 0; v < pf->num_alloc_vsi; v++) {
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005251 if (!pf->vsi[v])
5252 continue;
5253
5254 /* - Enable all TCs for the LAN VSI
5255 * - For all others keep them at TC0 for now
5256 */
5257 if (v == pf->lan_vsi)
5258 tc_map = i40e_pf_get_tc_map(pf);
5259 else
David Ertmanea6acb72016-09-20 07:10:50 -07005260 tc_map = I40E_DEFAULT_TRAFFIC_CLASS;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005261
5262 ret = i40e_vsi_config_tc(pf->vsi[v], tc_map);
5263 if (ret) {
5264 dev_info(&pf->pdev->dev,
5265 "Failed configuring TC for VSI seid=%d\n",
5266 pf->vsi[v]->seid);
5267 /* Will try to configure as many components */
5268 } else {
Neerav Parikh0672a092014-04-01 07:11:47 +00005269 /* Re-configure VSI vectors based on updated TC map */
5270 i40e_vsi_map_rings_to_vectors(pf->vsi[v]);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005271 if (pf->vsi[v]->netdev)
5272 i40e_dcbnl_set_all(pf->vsi[v]);
5273 }
5274 }
5275}
5276
5277/**
Neerav Parikh2fd75f32014-11-12 00:18:20 +00005278 * i40e_resume_port_tx - Resume port Tx
5279 * @pf: PF struct
5280 *
5281 * Resume a port's Tx and issue a PF reset in case of failure to
5282 * resume.
5283 **/
5284static int i40e_resume_port_tx(struct i40e_pf *pf)
5285{
5286 struct i40e_hw *hw = &pf->hw;
5287 int ret;
5288
5289 ret = i40e_aq_resume_port_tx(hw, NULL);
5290 if (ret) {
5291 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005292 "Resume Port Tx failed, err %s aq_err %s\n",
5293 i40e_stat_str(&pf->hw, ret),
5294 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh2fd75f32014-11-12 00:18:20 +00005295 /* Schedule PF reset to recover */
Jacob Keller0da36b92017-04-19 09:25:55 -04005296 set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
Neerav Parikh2fd75f32014-11-12 00:18:20 +00005297 i40e_service_event_schedule(pf);
5298 }
5299
5300 return ret;
5301}
5302
5303/**
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005304 * i40e_init_pf_dcb - Initialize DCB configuration
5305 * @pf: PF being configured
5306 *
5307 * Query the current DCB configuration and cache it
5308 * in the hardware structure
5309 **/
5310static int i40e_init_pf_dcb(struct i40e_pf *pf)
5311{
5312 struct i40e_hw *hw = &pf->hw;
5313 int err = 0;
5314
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +00005315 /* Do not enable DCB for SW1 and SW2 images even if the FW is capable */
Jacob Kellerd36e41d2017-06-23 04:24:46 -04005316 if (pf->hw_features & I40E_HW_NO_DCB_SUPPORT)
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +00005317 goto out;
5318
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005319 /* Get the initial DCB configuration */
5320 err = i40e_init_dcb(hw);
5321 if (!err) {
5322 /* Device/Function is not DCBX capable */
5323 if ((!hw->func_caps.dcb) ||
5324 (hw->dcbx_status == I40E_DCBX_STATUS_DISABLED)) {
5325 dev_info(&pf->pdev->dev,
5326 "DCBX offload is not supported or is disabled for this PF.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005327 } else {
5328 /* When status is not DISABLED then DCBX in FW */
5329 pf->dcbx_cap = DCB_CAP_DCBX_LLD_MANAGED |
5330 DCB_CAP_DCBX_VER_IEEE;
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005331
5332 pf->flags |= I40E_FLAG_DCB_CAPABLE;
Dave Ertmana0362442016-08-29 17:38:26 -07005333 /* Enable DCB tagging only when more than one TC
5334 * or explicitly disable if only one TC
5335 */
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005336 if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
5337 pf->flags |= I40E_FLAG_DCB_ENABLED;
Dave Ertmana0362442016-08-29 17:38:26 -07005338 else
5339 pf->flags &= ~I40E_FLAG_DCB_ENABLED;
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005340 dev_dbg(&pf->pdev->dev,
5341 "DCBX offload is supported for this PF.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005342 }
Neerav Parikh014269f2014-04-01 07:11:48 +00005343 } else {
Shannon Nelsonaebfc812014-12-11 07:06:38 +00005344 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005345 "Query for DCB configuration failed, err %s aq_err %s\n",
5346 i40e_stat_str(&pf->hw, err),
5347 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005348 }
5349
5350out:
5351 return err;
5352}
5353#endif /* CONFIG_I40E_DCB */
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005354#define SPEED_SIZE 14
5355#define FC_SIZE 8
5356/**
5357 * i40e_print_link_message - print link up or down
5358 * @vsi: the VSI for which link needs a message
5359 */
Matt Jaredc156f852015-08-27 11:42:39 -04005360void i40e_print_link_message(struct i40e_vsi *vsi, bool isup)
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005361{
Filip Sadowski7ec9ba12016-11-08 13:05:13 -08005362 enum i40e_aq_link_speed new_speed;
Sudheer Mogilappagari3fded462017-07-14 09:10:18 -04005363 struct i40e_pf *pf = vsi->back;
Shannon Nelsona9165492015-09-03 17:19:00 -04005364 char *speed = "Unknown";
5365 char *fc = "Unknown";
Henry Tieman3e03d7c2016-12-02 12:32:57 -08005366 char *fec = "";
Mariusz Stachura68e49702017-07-12 05:46:14 -04005367 char *req_fec = "";
Henry Tieman3e03d7c2016-12-02 12:32:57 -08005368 char *an = "";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005369
Sudheer Mogilappagari3fded462017-07-14 09:10:18 -04005370 new_speed = pf->hw.phy.link_info.link_speed;
Filip Sadowski7ec9ba12016-11-08 13:05:13 -08005371
5372 if ((vsi->current_isup == isup) && (vsi->current_speed == new_speed))
Matt Jaredc156f852015-08-27 11:42:39 -04005373 return;
5374 vsi->current_isup = isup;
Filip Sadowski7ec9ba12016-11-08 13:05:13 -08005375 vsi->current_speed = new_speed;
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005376 if (!isup) {
5377 netdev_info(vsi->netdev, "NIC Link is Down\n");
5378 return;
5379 }
5380
Greg Rose148c2d82014-12-11 07:06:27 +00005381 /* Warn user if link speed on NPAR enabled partition is not at
5382 * least 10GB
5383 */
Sudheer Mogilappagari3fded462017-07-14 09:10:18 -04005384 if (pf->hw.func_caps.npar_enable &&
5385 (pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_1GB ||
5386 pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_100MB))
Greg Rose148c2d82014-12-11 07:06:27 +00005387 netdev_warn(vsi->netdev,
5388 "The partition detected link speed that is less than 10Gbps\n");
5389
Sudheer Mogilappagari3fded462017-07-14 09:10:18 -04005390 switch (pf->hw.phy.link_info.link_speed) {
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005391 case I40E_LINK_SPEED_40GB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005392 speed = "40 G";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005393 break;
Jesse Brandeburgae24b402015-03-27 00:12:09 -07005394 case I40E_LINK_SPEED_20GB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005395 speed = "20 G";
Jesse Brandeburgae24b402015-03-27 00:12:09 -07005396 break;
Carolyn Wyborny31232372016-11-21 13:03:48 -08005397 case I40E_LINK_SPEED_25GB:
5398 speed = "25 G";
5399 break;
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005400 case I40E_LINK_SPEED_10GB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005401 speed = "10 G";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005402 break;
5403 case I40E_LINK_SPEED_1GB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005404 speed = "1000 M";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005405 break;
Mitch Williams5960d332014-09-13 07:40:47 +00005406 case I40E_LINK_SPEED_100MB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005407 speed = "100 M";
Mitch Williams5960d332014-09-13 07:40:47 +00005408 break;
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005409 default:
5410 break;
5411 }
5412
Sudheer Mogilappagari3fded462017-07-14 09:10:18 -04005413 switch (pf->hw.fc.current_mode) {
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005414 case I40E_FC_FULL:
Shannon Nelsona9165492015-09-03 17:19:00 -04005415 fc = "RX/TX";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005416 break;
5417 case I40E_FC_TX_PAUSE:
Shannon Nelsona9165492015-09-03 17:19:00 -04005418 fc = "TX";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005419 break;
5420 case I40E_FC_RX_PAUSE:
Shannon Nelsona9165492015-09-03 17:19:00 -04005421 fc = "RX";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005422 break;
5423 default:
Shannon Nelsona9165492015-09-03 17:19:00 -04005424 fc = "None";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005425 break;
5426 }
5427
Sudheer Mogilappagari3fded462017-07-14 09:10:18 -04005428 if (pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_25GB) {
Mariusz Stachura68e49702017-07-12 05:46:14 -04005429 req_fec = ", Requested FEC: None";
Henry Tieman3e03d7c2016-12-02 12:32:57 -08005430 fec = ", FEC: None";
5431 an = ", Autoneg: False";
5432
Sudheer Mogilappagari3fded462017-07-14 09:10:18 -04005433 if (pf->hw.phy.link_info.an_info & I40E_AQ_AN_COMPLETED)
Henry Tieman3e03d7c2016-12-02 12:32:57 -08005434 an = ", Autoneg: True";
5435
Sudheer Mogilappagari3fded462017-07-14 09:10:18 -04005436 if (pf->hw.phy.link_info.fec_info &
Henry Tieman3e03d7c2016-12-02 12:32:57 -08005437 I40E_AQ_CONFIG_FEC_KR_ENA)
5438 fec = ", FEC: CL74 FC-FEC/BASE-R";
Sudheer Mogilappagari3fded462017-07-14 09:10:18 -04005439 else if (pf->hw.phy.link_info.fec_info &
Henry Tieman3e03d7c2016-12-02 12:32:57 -08005440 I40E_AQ_CONFIG_FEC_RS_ENA)
5441 fec = ", FEC: CL108 RS-FEC";
Mariusz Stachura68e49702017-07-12 05:46:14 -04005442
5443 /* 'CL108 RS-FEC' should be displayed when RS is requested, or
5444 * both RS and FC are requested
5445 */
5446 if (vsi->back->hw.phy.link_info.req_fec_info &
5447 (I40E_AQ_REQUEST_FEC_KR | I40E_AQ_REQUEST_FEC_RS)) {
5448 if (vsi->back->hw.phy.link_info.req_fec_info &
5449 I40E_AQ_REQUEST_FEC_RS)
5450 req_fec = ", Requested FEC: CL108 RS-FEC";
5451 else
5452 req_fec = ", Requested FEC: CL74 FC-FEC/BASE-R";
5453 }
Henry Tieman3e03d7c2016-12-02 12:32:57 -08005454 }
5455
Mariusz Stachura68e49702017-07-12 05:46:14 -04005456 netdev_info(vsi->netdev, "NIC Link is Up, %sbps Full Duplex%s%s%s, Flow Control: %s\n",
5457 speed, req_fec, fec, an, fc);
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005458}
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005459
5460/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005461 * i40e_up_complete - Finish the last steps of bringing up a connection
5462 * @vsi: the VSI being configured
5463 **/
5464static int i40e_up_complete(struct i40e_vsi *vsi)
5465{
5466 struct i40e_pf *pf = vsi->back;
5467 int err;
5468
5469 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
5470 i40e_vsi_configure_msix(vsi);
5471 else
5472 i40e_configure_msi_and_legacy(vsi);
5473
5474 /* start rings */
Filip Sadowski3aa7b742016-10-11 15:26:58 -07005475 err = i40e_vsi_start_rings(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005476 if (err)
5477 return err;
5478
Jacob Keller0da36b92017-04-19 09:25:55 -04005479 clear_bit(__I40E_VSI_DOWN, vsi->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005480 i40e_napi_enable_all(vsi);
5481 i40e_vsi_enable_irq(vsi);
5482
5483 if ((pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP) &&
5484 (vsi->netdev)) {
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005485 i40e_print_link_message(vsi, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005486 netif_tx_start_all_queues(vsi->netdev);
5487 netif_carrier_on(vsi->netdev);
5488 }
Anjali Singhai Jainca64fa42014-02-11 08:26:30 +00005489
5490 /* replay FDIR SB filters */
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005491 if (vsi->type == I40E_VSI_FDIR) {
5492 /* reset fd counters */
Jacob Keller097dbf52017-02-06 14:38:46 -08005493 pf->fd_add_err = 0;
5494 pf->fd_atr_cnt = 0;
Anjali Singhai Jainca64fa42014-02-11 08:26:30 +00005495 i40e_fdir_filter_restore(vsi);
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005496 }
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06005497
5498 /* On the next run of the service_task, notify any clients of the new
5499 * opened netdev
5500 */
5501 pf->flags |= I40E_FLAG_SERVICE_CLIENT_REQUESTED;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005502 i40e_service_event_schedule(pf);
5503
5504 return 0;
5505}
5506
5507/**
5508 * i40e_vsi_reinit_locked - Reset the VSI
5509 * @vsi: the VSI being configured
5510 *
5511 * Rebuild the ring structs after some configuration
5512 * has changed, e.g. MTU size.
5513 **/
5514static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi)
5515{
5516 struct i40e_pf *pf = vsi->back;
5517
5518 WARN_ON(in_interrupt());
Jacob Keller0da36b92017-04-19 09:25:55 -04005519 while (test_and_set_bit(__I40E_CONFIG_BUSY, pf->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005520 usleep_range(1000, 2000);
5521 i40e_down(vsi);
5522
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005523 i40e_up(vsi);
Jacob Keller0da36b92017-04-19 09:25:55 -04005524 clear_bit(__I40E_CONFIG_BUSY, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005525}
5526
5527/**
5528 * i40e_up - Bring the connection back up after being down
5529 * @vsi: the VSI being configured
5530 **/
5531int i40e_up(struct i40e_vsi *vsi)
5532{
5533 int err;
5534
5535 err = i40e_vsi_configure(vsi);
5536 if (!err)
5537 err = i40e_up_complete(vsi);
5538
5539 return err;
5540}
5541
5542/**
5543 * i40e_down - Shutdown the connection processing
5544 * @vsi: the VSI being stopped
5545 **/
5546void i40e_down(struct i40e_vsi *vsi)
5547{
5548 int i;
5549
5550 /* It is assumed that the caller of this function
Jacob Kellerd19cb642017-04-21 13:38:05 -07005551 * sets the vsi->state __I40E_VSI_DOWN bit.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005552 */
5553 if (vsi->netdev) {
5554 netif_carrier_off(vsi->netdev);
5555 netif_tx_disable(vsi->netdev);
5556 }
5557 i40e_vsi_disable_irq(vsi);
Filip Sadowski3aa7b742016-10-11 15:26:58 -07005558 i40e_vsi_stop_rings(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005559 i40e_napi_disable_all(vsi);
5560
5561 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck9f65e152013-09-28 06:00:58 +00005562 i40e_clean_tx_ring(vsi->tx_rings[i]);
Björn Töpel74608d12017-05-24 07:55:35 +02005563 if (i40e_enabled_xdp_vsi(vsi))
5564 i40e_clean_tx_ring(vsi->xdp_rings[i]);
Alexander Duyck9f65e152013-09-28 06:00:58 +00005565 i40e_clean_rx_ring(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005566 }
Catherine Sullivanf980d442016-05-16 10:26:34 -07005567
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005568}
5569
5570/**
5571 * i40e_setup_tc - configure multiple traffic classes
5572 * @netdev: net device to configure
5573 * @tc: number of traffic classes to enable
5574 **/
5575static int i40e_setup_tc(struct net_device *netdev, u8 tc)
5576{
5577 struct i40e_netdev_priv *np = netdev_priv(netdev);
5578 struct i40e_vsi *vsi = np->vsi;
5579 struct i40e_pf *pf = vsi->back;
5580 u8 enabled_tc = 0;
5581 int ret = -EINVAL;
5582 int i;
5583
5584 /* Check if DCB enabled to continue */
5585 if (!(pf->flags & I40E_FLAG_DCB_ENABLED)) {
5586 netdev_info(netdev, "DCB is not enabled for adapter\n");
5587 goto exit;
5588 }
5589
5590 /* Check if MFP enabled */
5591 if (pf->flags & I40E_FLAG_MFP_ENABLED) {
5592 netdev_info(netdev, "Configuring TC not supported in MFP mode\n");
5593 goto exit;
5594 }
5595
5596 /* Check whether tc count is within enabled limit */
5597 if (tc > i40e_pf_get_num_tc(pf)) {
5598 netdev_info(netdev, "TC count greater than enabled on link for adapter\n");
5599 goto exit;
5600 }
5601
5602 /* Generate TC map for number of tc requested */
5603 for (i = 0; i < tc; i++)
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08005604 enabled_tc |= BIT(i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005605
5606 /* Requesting same TC configuration as already enabled */
5607 if (enabled_tc == vsi->tc_config.enabled_tc)
5608 return 0;
5609
5610 /* Quiesce VSI queues */
5611 i40e_quiesce_vsi(vsi);
5612
5613 /* Configure VSI for enabled TCs */
5614 ret = i40e_vsi_config_tc(vsi, enabled_tc);
5615 if (ret) {
5616 netdev_info(netdev, "Failed configuring TC for VSI seid=%d\n",
5617 vsi->seid);
5618 goto exit;
5619 }
5620
5621 /* Unquiesce VSI */
5622 i40e_unquiesce_vsi(vsi);
5623
5624exit:
5625 return ret;
5626}
5627
Jiri Pirko2572ac52017-08-07 10:15:17 +02005628static int __i40e_setup_tc(struct net_device *netdev, enum tc_setup_type type,
Jiri Pirkode4784c2017-08-07 10:15:32 +02005629 void *type_data)
John Fastabende4c67342016-02-16 21:16:15 -08005630{
Jiri Pirkode4784c2017-08-07 10:15:32 +02005631 struct tc_mqprio_qopt *mqprio = type_data;
5632
Jiri Pirko2572ac52017-08-07 10:15:17 +02005633 if (type != TC_SETUP_MQPRIO)
Jiri Pirko38cf0422017-08-07 10:15:31 +02005634 return -EOPNOTSUPP;
Amritha Nambiar56f36ac2017-03-15 10:39:25 -07005635
Jiri Pirkode4784c2017-08-07 10:15:32 +02005636 mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
Amritha Nambiar56f36ac2017-03-15 10:39:25 -07005637
Jiri Pirkode4784c2017-08-07 10:15:32 +02005638 return i40e_setup_tc(netdev, mqprio->num_tc);
John Fastabende4c67342016-02-16 21:16:15 -08005639}
5640
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005641/**
5642 * i40e_open - Called when a network interface is made active
5643 * @netdev: network interface device structure
5644 *
5645 * The open entry point is called when a network interface is made
5646 * active by the system (IFF_UP). At this point all resources needed
5647 * for transmit and receive operations are allocated, the interrupt
5648 * handler is registered with the OS, the netdev watchdog subtask is
5649 * enabled, and the stack is notified that the interface is ready.
5650 *
5651 * Returns 0 on success, negative value on failure
5652 **/
Vasu Dev38e00432014-08-01 13:27:03 -07005653int i40e_open(struct net_device *netdev)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005654{
5655 struct i40e_netdev_priv *np = netdev_priv(netdev);
5656 struct i40e_vsi *vsi = np->vsi;
5657 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005658 int err;
5659
Shannon Nelson4eb3f762014-03-06 08:59:58 +00005660 /* disallow open during test or if eeprom is broken */
Jacob Keller0da36b92017-04-19 09:25:55 -04005661 if (test_bit(__I40E_TESTING, pf->state) ||
5662 test_bit(__I40E_BAD_EEPROM, pf->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005663 return -EBUSY;
5664
5665 netif_carrier_off(netdev);
5666
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005667 err = i40e_vsi_open(vsi);
5668 if (err)
5669 return err;
5670
Jesse Brandeburg059dab62014-04-01 09:07:20 +00005671 /* configure global TSO hardware offload settings */
5672 wr32(&pf->hw, I40E_GLLAN_TSOMSK_F, be32_to_cpu(TCP_FLAG_PSH |
5673 TCP_FLAG_FIN) >> 16);
5674 wr32(&pf->hw, I40E_GLLAN_TSOMSK_M, be32_to_cpu(TCP_FLAG_PSH |
5675 TCP_FLAG_FIN |
5676 TCP_FLAG_CWR) >> 16);
5677 wr32(&pf->hw, I40E_GLLAN_TSOMSK_L, be32_to_cpu(TCP_FLAG_CWR) >> 16);
5678
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07005679 udp_tunnel_get_rx_info(netdev);
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06005680
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005681 return 0;
5682}
5683
5684/**
5685 * i40e_vsi_open -
5686 * @vsi: the VSI to open
5687 *
5688 * Finish initialization of the VSI.
5689 *
5690 * Returns 0 on success, negative value on failure
Maciej Sosin373149f2017-04-05 07:50:55 -04005691 *
5692 * Note: expects to be called while under rtnl_lock()
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005693 **/
5694int i40e_vsi_open(struct i40e_vsi *vsi)
5695{
5696 struct i40e_pf *pf = vsi->back;
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00005697 char int_name[I40E_INT_NAME_STR_LEN];
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005698 int err;
5699
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005700 /* allocate descriptors */
5701 err = i40e_vsi_setup_tx_resources(vsi);
5702 if (err)
5703 goto err_setup_tx;
5704 err = i40e_vsi_setup_rx_resources(vsi);
5705 if (err)
5706 goto err_setup_rx;
5707
5708 err = i40e_vsi_configure(vsi);
5709 if (err)
5710 goto err_setup_rx;
5711
Shannon Nelsonc22e3c62014-03-14 07:32:25 +00005712 if (vsi->netdev) {
5713 snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
5714 dev_driver_string(&pf->pdev->dev), vsi->netdev->name);
5715 err = i40e_vsi_request_irq(vsi, int_name);
5716 if (err)
5717 goto err_setup_rx;
5718
5719 /* Notify the stack of the actual queue counts. */
5720 err = netif_set_real_num_tx_queues(vsi->netdev,
5721 vsi->num_queue_pairs);
5722 if (err)
5723 goto err_set_queues;
5724
5725 err = netif_set_real_num_rx_queues(vsi->netdev,
5726 vsi->num_queue_pairs);
5727 if (err)
5728 goto err_set_queues;
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00005729
5730 } else if (vsi->type == I40E_VSI_FDIR) {
Carolyn Wybornye240f672014-12-11 07:06:37 +00005731 snprintf(int_name, sizeof(int_name) - 1, "%s-%s:fdir",
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +00005732 dev_driver_string(&pf->pdev->dev),
5733 dev_name(&pf->pdev->dev));
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00005734 err = i40e_vsi_request_irq(vsi, int_name);
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +00005735
Shannon Nelsonc22e3c62014-03-14 07:32:25 +00005736 } else {
Jean Sacrence9ccb12014-05-01 14:31:18 +00005737 err = -EINVAL;
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005738 goto err_setup_rx;
5739 }
Anjali Singhai Jain25946dd2013-11-26 10:49:14 +00005740
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005741 err = i40e_up_complete(vsi);
5742 if (err)
5743 goto err_up_complete;
5744
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005745 return 0;
5746
5747err_up_complete:
5748 i40e_down(vsi);
Anjali Singhai Jain25946dd2013-11-26 10:49:14 +00005749err_set_queues:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005750 i40e_vsi_free_irq(vsi);
5751err_setup_rx:
5752 i40e_vsi_free_rx_resources(vsi);
5753err_setup_tx:
5754 i40e_vsi_free_tx_resources(vsi);
5755 if (vsi == pf->vsi[pf->lan_vsi])
Maciej Sosin373149f2017-04-05 07:50:55 -04005756 i40e_do_reset(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED), true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005757
5758 return err;
5759}
5760
5761/**
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00005762 * i40e_fdir_filter_exit - Cleans up the Flow Director accounting
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00005763 * @pf: Pointer to PF
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00005764 *
5765 * This function destroys the hlist where all the Flow Director
5766 * filters were saved.
5767 **/
5768static void i40e_fdir_filter_exit(struct i40e_pf *pf)
5769{
5770 struct i40e_fdir_filter *filter;
Jacob Keller0e588de2017-02-06 14:38:50 -08005771 struct i40e_flex_pit *pit_entry, *tmp;
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00005772 struct hlist_node *node2;
5773
5774 hlist_for_each_entry_safe(filter, node2,
5775 &pf->fdir_filter_list, fdir_node) {
5776 hlist_del(&filter->fdir_node);
5777 kfree(filter);
5778 }
Jacob Keller097dbf52017-02-06 14:38:46 -08005779
Jacob Keller0e588de2017-02-06 14:38:50 -08005780 list_for_each_entry_safe(pit_entry, tmp, &pf->l3_flex_pit_list, list) {
5781 list_del(&pit_entry->list);
5782 kfree(pit_entry);
5783 }
5784 INIT_LIST_HEAD(&pf->l3_flex_pit_list);
5785
5786 list_for_each_entry_safe(pit_entry, tmp, &pf->l4_flex_pit_list, list) {
5787 list_del(&pit_entry->list);
5788 kfree(pit_entry);
5789 }
5790 INIT_LIST_HEAD(&pf->l4_flex_pit_list);
5791
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00005792 pf->fdir_pf_active_filters = 0;
Jacob Keller097dbf52017-02-06 14:38:46 -08005793 pf->fd_tcp4_filter_cnt = 0;
5794 pf->fd_udp4_filter_cnt = 0;
Jacob Kellerf223c872017-02-06 14:38:51 -08005795 pf->fd_sctp4_filter_cnt = 0;
Jacob Keller097dbf52017-02-06 14:38:46 -08005796 pf->fd_ip4_filter_cnt = 0;
Jacob Keller3bcee1e2017-02-06 14:38:46 -08005797
5798 /* Reprogram the default input set for TCP/IPv4 */
5799 i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_TCP,
5800 I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
5801 I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
5802
5803 /* Reprogram the default input set for UDP/IPv4 */
5804 i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_UDP,
5805 I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
5806 I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
5807
5808 /* Reprogram the default input set for SCTP/IPv4 */
5809 i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_SCTP,
5810 I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
5811 I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
5812
5813 /* Reprogram the default input set for Other/IPv4 */
5814 i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_OTHER,
5815 I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00005816}
5817
5818/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005819 * i40e_close - Disables a network interface
5820 * @netdev: network interface device structure
5821 *
5822 * The close entry point is called when an interface is de-activated
5823 * by the OS. The hardware is still under the driver's control, but
5824 * this netdev interface is disabled.
5825 *
5826 * Returns 0, this is not allowed to fail
5827 **/
Vasu Dev38e00432014-08-01 13:27:03 -07005828int i40e_close(struct net_device *netdev)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005829{
5830 struct i40e_netdev_priv *np = netdev_priv(netdev);
5831 struct i40e_vsi *vsi = np->vsi;
5832
Shannon Nelson90ef8d42014-03-14 07:32:26 +00005833 i40e_vsi_close(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005834
5835 return 0;
5836}
5837
5838/**
5839 * i40e_do_reset - Start a PF or Core Reset sequence
5840 * @pf: board private structure
5841 * @reset_flags: which reset is requested
Maciej Sosin373149f2017-04-05 07:50:55 -04005842 * @lock_acquired: indicates whether or not the lock has been acquired
5843 * before this function was called.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005844 *
5845 * The essential difference in resets is that the PF Reset
5846 * doesn't clear the packet buffers, doesn't reset the PE
5847 * firmware, and doesn't bother the other PFs on the chip.
5848 **/
Maciej Sosin373149f2017-04-05 07:50:55 -04005849void i40e_do_reset(struct i40e_pf *pf, u32 reset_flags, bool lock_acquired)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005850{
5851 u32 val;
5852
5853 WARN_ON(in_interrupt());
5854
Mitch Williams263fc482014-04-23 04:50:11 +00005855
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005856 /* do the biggest reset indicated */
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005857 if (reset_flags & BIT_ULL(__I40E_GLOBAL_RESET_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005858
5859 /* Request a Global Reset
5860 *
5861 * This will start the chip's countdown to the actual full
5862 * chip reset event, and a warning interrupt to be sent
5863 * to all PFs, including the requestor. Our handler
5864 * for the warning interrupt will deal with the shutdown
5865 * and recovery of the switch setup.
5866 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005867 dev_dbg(&pf->pdev->dev, "GlobalR requested\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005868 val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
5869 val |= I40E_GLGEN_RTRIG_GLOBR_MASK;
5870 wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
5871
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005872 } else if (reset_flags & BIT_ULL(__I40E_CORE_RESET_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005873
5874 /* Request a Core Reset
5875 *
5876 * Same as Global Reset, except does *not* include the MAC/PHY
5877 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005878 dev_dbg(&pf->pdev->dev, "CoreR requested\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005879 val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
5880 val |= I40E_GLGEN_RTRIG_CORER_MASK;
5881 wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
5882 i40e_flush(&pf->hw);
5883
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005884 } else if (reset_flags & BIT_ULL(__I40E_PF_RESET_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005885
5886 /* Request a PF Reset
5887 *
5888 * Resets only the PF-specific registers
5889 *
5890 * This goes directly to the tear-down and rebuild of
5891 * the switch, since we need to do all the recovery as
5892 * for the Core Reset.
5893 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005894 dev_dbg(&pf->pdev->dev, "PFR requested\n");
Maciej Sosin373149f2017-04-05 07:50:55 -04005895 i40e_handle_reset_warning(pf, lock_acquired);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005896
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005897 } else if (reset_flags & BIT_ULL(__I40E_REINIT_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005898 int v;
5899
5900 /* Find the VSI(s) that requested a re-init */
5901 dev_info(&pf->pdev->dev,
5902 "VSI reinit requested\n");
Mitch Williams505682c2014-05-20 08:01:37 +00005903 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005904 struct i40e_vsi *vsi = pf->vsi[v];
Jesse Brandeburg6995b362015-08-28 17:55:54 -04005905
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005906 if (vsi != NULL &&
Jacob Kellerd19cb642017-04-21 13:38:05 -07005907 test_and_clear_bit(__I40E_VSI_REINIT_REQUESTED,
Jacob Keller0da36b92017-04-19 09:25:55 -04005908 vsi->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005909 i40e_vsi_reinit_locked(pf->vsi[v]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005910 }
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005911 } else if (reset_flags & BIT_ULL(__I40E_DOWN_REQUESTED)) {
Neerav Parikhb5d06f02014-06-03 23:50:17 +00005912 int v;
5913
5914 /* Find the VSI(s) that needs to be brought down */
5915 dev_info(&pf->pdev->dev, "VSI down requested\n");
5916 for (v = 0; v < pf->num_alloc_vsi; v++) {
5917 struct i40e_vsi *vsi = pf->vsi[v];
Jesse Brandeburg6995b362015-08-28 17:55:54 -04005918
Neerav Parikhb5d06f02014-06-03 23:50:17 +00005919 if (vsi != NULL &&
Jacob Kellerd19cb642017-04-21 13:38:05 -07005920 test_and_clear_bit(__I40E_VSI_DOWN_REQUESTED,
Jacob Keller0da36b92017-04-19 09:25:55 -04005921 vsi->state)) {
5922 set_bit(__I40E_VSI_DOWN, vsi->state);
Neerav Parikhb5d06f02014-06-03 23:50:17 +00005923 i40e_down(vsi);
Neerav Parikhb5d06f02014-06-03 23:50:17 +00005924 }
5925 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005926 } else {
5927 dev_info(&pf->pdev->dev,
5928 "bad reset request 0x%08x\n", reset_flags);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005929 }
5930}
5931
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005932#ifdef CONFIG_I40E_DCB
5933/**
5934 * i40e_dcb_need_reconfig - Check if DCB needs reconfig
5935 * @pf: board private structure
5936 * @old_cfg: current DCB config
5937 * @new_cfg: new DCB config
5938 **/
5939bool i40e_dcb_need_reconfig(struct i40e_pf *pf,
5940 struct i40e_dcbx_config *old_cfg,
5941 struct i40e_dcbx_config *new_cfg)
5942{
5943 bool need_reconfig = false;
5944
5945 /* Check if ETS configuration has changed */
5946 if (memcmp(&new_cfg->etscfg,
5947 &old_cfg->etscfg,
5948 sizeof(new_cfg->etscfg))) {
5949 /* If Priority Table has changed reconfig is needed */
5950 if (memcmp(&new_cfg->etscfg.prioritytable,
5951 &old_cfg->etscfg.prioritytable,
5952 sizeof(new_cfg->etscfg.prioritytable))) {
5953 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005954 dev_dbg(&pf->pdev->dev, "ETS UP2TC changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005955 }
5956
5957 if (memcmp(&new_cfg->etscfg.tcbwtable,
5958 &old_cfg->etscfg.tcbwtable,
5959 sizeof(new_cfg->etscfg.tcbwtable)))
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005960 dev_dbg(&pf->pdev->dev, "ETS TC BW Table changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005961
5962 if (memcmp(&new_cfg->etscfg.tsatable,
5963 &old_cfg->etscfg.tsatable,
5964 sizeof(new_cfg->etscfg.tsatable)))
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005965 dev_dbg(&pf->pdev->dev, "ETS TSA Table changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005966 }
5967
5968 /* Check if PFC configuration has changed */
5969 if (memcmp(&new_cfg->pfc,
5970 &old_cfg->pfc,
5971 sizeof(new_cfg->pfc))) {
5972 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005973 dev_dbg(&pf->pdev->dev, "PFC config change detected.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005974 }
5975
5976 /* Check if APP Table has changed */
5977 if (memcmp(&new_cfg->app,
5978 &old_cfg->app,
Dave Jones3d9667a2014-01-27 23:11:09 -05005979 sizeof(new_cfg->app))) {
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005980 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005981 dev_dbg(&pf->pdev->dev, "APP Table change detected.\n");
Dave Jones3d9667a2014-01-27 23:11:09 -05005982 }
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005983
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04005984 dev_dbg(&pf->pdev->dev, "dcb need_reconfig=%d\n", need_reconfig);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005985 return need_reconfig;
5986}
5987
5988/**
5989 * i40e_handle_lldp_event - Handle LLDP Change MIB event
5990 * @pf: board private structure
5991 * @e: event info posted on ARQ
5992 **/
5993static int i40e_handle_lldp_event(struct i40e_pf *pf,
5994 struct i40e_arq_event_info *e)
5995{
5996 struct i40e_aqc_lldp_get_mib *mib =
5997 (struct i40e_aqc_lldp_get_mib *)&e->desc.params.raw;
5998 struct i40e_hw *hw = &pf->hw;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005999 struct i40e_dcbx_config tmp_dcbx_cfg;
6000 bool need_reconfig = false;
6001 int ret = 0;
6002 u8 type;
6003
Neerav Parikh4d9b6042014-05-22 06:31:51 +00006004 /* Not DCB capable or capability disabled */
David Ertmanea6acb72016-09-20 07:10:50 -07006005 if (!(pf->flags & I40E_FLAG_DCB_CAPABLE))
Neerav Parikh4d9b6042014-05-22 06:31:51 +00006006 return ret;
6007
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006008 /* Ignore if event is not for Nearest Bridge */
6009 type = ((mib->type >> I40E_AQ_LLDP_BRIDGE_TYPE_SHIFT)
6010 & I40E_AQ_LLDP_BRIDGE_TYPE_MASK);
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04006011 dev_dbg(&pf->pdev->dev, "LLDP event mib bridge type 0x%x\n", type);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006012 if (type != I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE)
6013 return ret;
6014
6015 /* Check MIB Type and return if event for Remote MIB update */
6016 type = mib->type & I40E_AQ_LLDP_MIB_TYPE_MASK;
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00006017 dev_dbg(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04006018 "LLDP event mib type %s\n", type ? "remote" : "local");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006019 if (type == I40E_AQ_LLDP_MIB_REMOTE) {
6020 /* Update the remote cached instance and return */
6021 ret = i40e_aq_get_dcb_config(hw, I40E_AQ_LLDP_MIB_REMOTE,
6022 I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE,
6023 &hw->remote_dcbx_config);
6024 goto exit;
6025 }
6026
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00006027 /* Store the old configuration */
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07006028 tmp_dcbx_cfg = hw->local_dcbx_config;
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00006029
Neerav Parikh750fcbc2015-02-24 06:58:47 +00006030 /* Reset the old DCBx configuration data */
6031 memset(&hw->local_dcbx_config, 0, sizeof(hw->local_dcbx_config));
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00006032 /* Get updated DCBX data from firmware */
6033 ret = i40e_get_dcb_config(&pf->hw);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006034 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006035 dev_info(&pf->pdev->dev,
6036 "Failed querying DCB configuration data from firmware, err %s aq_err %s\n",
6037 i40e_stat_str(&pf->hw, ret),
6038 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006039 goto exit;
6040 }
6041
6042 /* No change detected in DCBX configs */
Neerav Parikh750fcbc2015-02-24 06:58:47 +00006043 if (!memcmp(&tmp_dcbx_cfg, &hw->local_dcbx_config,
6044 sizeof(tmp_dcbx_cfg))) {
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006045 dev_dbg(&pf->pdev->dev, "No change detected in DCBX configuration.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006046 goto exit;
6047 }
6048
Neerav Parikh750fcbc2015-02-24 06:58:47 +00006049 need_reconfig = i40e_dcb_need_reconfig(pf, &tmp_dcbx_cfg,
6050 &hw->local_dcbx_config);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006051
Neerav Parikh750fcbc2015-02-24 06:58:47 +00006052 i40e_dcbnl_flush_apps(pf, &tmp_dcbx_cfg, &hw->local_dcbx_config);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006053
6054 if (!need_reconfig)
6055 goto exit;
6056
Neerav Parikh4d9b6042014-05-22 06:31:51 +00006057 /* Enable DCB tagging only when more than one TC */
Neerav Parikh750fcbc2015-02-24 06:58:47 +00006058 if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
Neerav Parikh4d9b6042014-05-22 06:31:51 +00006059 pf->flags |= I40E_FLAG_DCB_ENABLED;
6060 else
6061 pf->flags &= ~I40E_FLAG_DCB_ENABLED;
6062
Jacob Keller0da36b92017-04-19 09:25:55 -04006063 set_bit(__I40E_PORT_SUSPENDED, pf->state);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006064 /* Reconfiguration needed quiesce all VSIs */
6065 i40e_pf_quiesce_all_vsi(pf);
6066
6067 /* Changes in configuration update VEB/VSI */
6068 i40e_dcb_reconfigure(pf);
6069
Neerav Parikh2fd75f32014-11-12 00:18:20 +00006070 ret = i40e_resume_port_tx(pf);
6071
Jacob Keller0da36b92017-04-19 09:25:55 -04006072 clear_bit(__I40E_PORT_SUSPENDED, pf->state);
Neerav Parikh2fd75f32014-11-12 00:18:20 +00006073 /* In case of error no point in resuming VSIs */
Neerav Parikh69129dc2014-11-12 00:18:46 +00006074 if (ret)
6075 goto exit;
6076
Neerav Parikh3fe06f42016-02-17 16:12:15 -08006077 /* Wait for the PF's queues to be disabled */
6078 ret = i40e_pf_wait_queues_disabled(pf);
Parikh, Neerav11e47702015-02-21 06:43:55 +00006079 if (ret) {
6080 /* Schedule PF reset to recover */
Jacob Keller0da36b92017-04-19 09:25:55 -04006081 set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
Parikh, Neerav11e47702015-02-21 06:43:55 +00006082 i40e_service_event_schedule(pf);
6083 } else {
Neerav Parikh2fd75f32014-11-12 00:18:20 +00006084 i40e_pf_unquiesce_all_vsi(pf);
Mitch Williams0ef2d5a2017-01-24 10:24:00 -08006085 pf->flags |= (I40E_FLAG_SERVICE_CLIENT_REQUESTED |
6086 I40E_FLAG_CLIENT_L2_CHANGE);
Parikh, Neerav11e47702015-02-21 06:43:55 +00006087 }
6088
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006089exit:
6090 return ret;
6091}
6092#endif /* CONFIG_I40E_DCB */
6093
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006094/**
Anjali Singhai Jain233261862013-11-26 10:49:22 +00006095 * i40e_do_reset_safe - Protected reset path for userland calls.
6096 * @pf: board private structure
6097 * @reset_flags: which reset is requested
6098 *
6099 **/
6100void i40e_do_reset_safe(struct i40e_pf *pf, u32 reset_flags)
6101{
6102 rtnl_lock();
Maciej Sosin373149f2017-04-05 07:50:55 -04006103 i40e_do_reset(pf, reset_flags, true);
Anjali Singhai Jain233261862013-11-26 10:49:22 +00006104 rtnl_unlock();
6105}
6106
6107/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006108 * i40e_handle_lan_overflow_event - Handler for LAN queue overflow event
6109 * @pf: board private structure
6110 * @e: event info posted on ARQ
6111 *
6112 * Handler for LAN Queue Overflow Event generated by the firmware for PF
6113 * and VF queues
6114 **/
6115static void i40e_handle_lan_overflow_event(struct i40e_pf *pf,
6116 struct i40e_arq_event_info *e)
6117{
6118 struct i40e_aqc_lan_overflow *data =
6119 (struct i40e_aqc_lan_overflow *)&e->desc.params.raw;
6120 u32 queue = le32_to_cpu(data->prtdcb_rupto);
6121 u32 qtx_ctl = le32_to_cpu(data->otx_ctl);
6122 struct i40e_hw *hw = &pf->hw;
6123 struct i40e_vf *vf;
6124 u16 vf_id;
6125
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006126 dev_dbg(&pf->pdev->dev, "overflow Rx Queue Number = %d QTX_CTL=0x%08x\n",
6127 queue, qtx_ctl);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006128
6129 /* Queue belongs to VF, find the VF and issue VF reset */
6130 if (((qtx_ctl & I40E_QTX_CTL_PFVF_Q_MASK)
6131 >> I40E_QTX_CTL_PFVF_Q_SHIFT) == I40E_QTX_CTL_VF_QUEUE) {
6132 vf_id = (u16)((qtx_ctl & I40E_QTX_CTL_VFVM_INDX_MASK)
6133 >> I40E_QTX_CTL_VFVM_INDX_SHIFT);
6134 vf_id -= hw->func_caps.vf_base_id;
6135 vf = &pf->vf[vf_id];
6136 i40e_vc_notify_vf_reset(vf);
6137 /* Allow VF to process pending reset notification */
6138 msleep(20);
6139 i40e_reset_vf(vf, false);
6140 }
6141}
6142
6143/**
Anjali Singhai Jain12957382014-06-04 04:22:47 +00006144 * i40e_get_cur_guaranteed_fd_count - Get the consumed guaranteed FD filters
6145 * @pf: board private structure
6146 **/
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006147u32 i40e_get_cur_guaranteed_fd_count(struct i40e_pf *pf)
Anjali Singhai Jain12957382014-06-04 04:22:47 +00006148{
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006149 u32 val, fcnt_prog;
Anjali Singhai Jain12957382014-06-04 04:22:47 +00006150
6151 val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
6152 fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK);
6153 return fcnt_prog;
6154}
6155
6156/**
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006157 * i40e_get_current_fd_count - Get total FD filters programmed for this PF
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006158 * @pf: board private structure
6159 **/
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006160u32 i40e_get_current_fd_count(struct i40e_pf *pf)
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006161{
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006162 u32 val, fcnt_prog;
6163
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006164 val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
6165 fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK) +
6166 ((val & I40E_PFQF_FDSTAT_BEST_CNT_MASK) >>
6167 I40E_PFQF_FDSTAT_BEST_CNT_SHIFT);
6168 return fcnt_prog;
6169}
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006170
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006171/**
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006172 * i40e_get_global_fd_count - Get total FD filters programmed on device
6173 * @pf: board private structure
6174 **/
6175u32 i40e_get_global_fd_count(struct i40e_pf *pf)
6176{
6177 u32 val, fcnt_prog;
6178
6179 val = rd32(&pf->hw, I40E_GLQF_FDCNT_0);
6180 fcnt_prog = (val & I40E_GLQF_FDCNT_0_GUARANT_CNT_MASK) +
6181 ((val & I40E_GLQF_FDCNT_0_BESTCNT_MASK) >>
6182 I40E_GLQF_FDCNT_0_BESTCNT_SHIFT);
6183 return fcnt_prog;
6184}
6185
6186/**
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006187 * i40e_fdir_check_and_reenable - Function to reenabe FD ATR or SB if disabled
6188 * @pf: board private structure
6189 **/
6190void i40e_fdir_check_and_reenable(struct i40e_pf *pf)
6191{
Carolyn Wyborny3487b6c2015-08-27 11:42:38 -04006192 struct i40e_fdir_filter *filter;
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006193 u32 fcnt_prog, fcnt_avail;
Carolyn Wyborny3487b6c2015-08-27 11:42:38 -04006194 struct hlist_node *node;
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006195
Jacob Keller0da36b92017-04-19 09:25:55 -04006196 if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006197 return;
6198
Jacob Keller47994c12017-04-19 09:25:57 -04006199 /* Check if we have enough room to re-enable FDir SB capability. */
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006200 fcnt_prog = i40e_get_global_fd_count(pf);
Anjali Singhai Jain12957382014-06-04 04:22:47 +00006201 fcnt_avail = pf->fdir_pf_filter_count;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006202 if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM)) ||
6203 (pf->fd_add_err == 0) ||
6204 (i40e_get_current_atr_cnt(pf) < pf->fd_atr_cnt)) {
Jacob Keller47994c12017-04-19 09:25:57 -04006205 if (pf->flags & I40E_FLAG_FD_SB_AUTO_DISABLED) {
6206 pf->flags &= ~I40E_FLAG_FD_SB_AUTO_DISABLED;
6207 if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
6208 (I40E_DEBUG_FD & pf->hw.debug_mask))
Anjali Singhai Jain2e4875e2015-04-16 20:06:06 -04006209 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 +00006210 }
6211 }
Jacob Kellera3417d22016-09-06 18:05:10 -07006212
Jacob Keller47994c12017-04-19 09:25:57 -04006213 /* We should wait for even more space before re-enabling ATR.
6214 * Additionally, we cannot enable ATR as long as we still have TCP SB
6215 * rules active.
Jacob Kellera3417d22016-09-06 18:05:10 -07006216 */
Jacob Keller47994c12017-04-19 09:25:57 -04006217 if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) &&
6218 (pf->fd_tcp4_filter_cnt == 0)) {
6219 if (pf->flags & I40E_FLAG_FD_ATR_AUTO_DISABLED) {
6220 pf->flags &= ~I40E_FLAG_FD_ATR_AUTO_DISABLED;
6221 if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
6222 (I40E_DEBUG_FD & pf->hw.debug_mask))
Jacob Kellera3417d22016-09-06 18:05:10 -07006223 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 +00006224 }
6225 }
Carolyn Wyborny3487b6c2015-08-27 11:42:38 -04006226
6227 /* if hw had a problem adding a filter, delete it */
6228 if (pf->fd_inv > 0) {
6229 hlist_for_each_entry_safe(filter, node,
6230 &pf->fdir_filter_list, fdir_node) {
6231 if (filter->fd_id == pf->fd_inv) {
6232 hlist_del(&filter->fdir_node);
6233 kfree(filter);
6234 pf->fdir_pf_active_filters--;
Filip Sadowski013df592017-08-29 05:32:38 -04006235 pf->fd_inv = 0;
Carolyn Wyborny3487b6c2015-08-27 11:42:38 -04006236 }
6237 }
6238 }
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006239}
6240
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006241#define I40E_MIN_FD_FLUSH_INTERVAL 10
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006242#define I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE 30
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006243/**
6244 * i40e_fdir_flush_and_replay - Function to flush all FD filters and replay SB
6245 * @pf: board private structure
6246 **/
6247static void i40e_fdir_flush_and_replay(struct i40e_pf *pf)
6248{
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006249 unsigned long min_flush_time;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006250 int flush_wait_retry = 50;
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006251 bool disable_atr = false;
6252 int fd_room;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006253 int reg;
6254
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04006255 if (!time_after(jiffies, pf->fd_flush_timestamp +
6256 (I40E_MIN_FD_FLUSH_INTERVAL * HZ)))
6257 return;
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006258
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04006259 /* If the flush is happening too quick and we have mostly SB rules we
6260 * should not re-enable ATR for some time.
6261 */
6262 min_flush_time = pf->fd_flush_timestamp +
6263 (I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE * HZ);
6264 fd_room = pf->fdir_pf_filter_count - pf->fdir_pf_active_filters;
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006265
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04006266 if (!(time_after(jiffies, min_flush_time)) &&
6267 (fd_room < I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) {
6268 if (I40E_DEBUG_FD & pf->hw.debug_mask)
6269 dev_info(&pf->pdev->dev, "ATR disabled, not enough FD filter space.\n");
6270 disable_atr = true;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006271 }
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04006272
6273 pf->fd_flush_timestamp = jiffies;
Jacob Keller47994c12017-04-19 09:25:57 -04006274 pf->flags |= I40E_FLAG_FD_ATR_AUTO_DISABLED;
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04006275 /* flush all filters */
6276 wr32(&pf->hw, I40E_PFQF_CTL_1,
6277 I40E_PFQF_CTL_1_CLEARFDTABLE_MASK);
6278 i40e_flush(&pf->hw);
6279 pf->fd_flush_cnt++;
6280 pf->fd_add_err = 0;
6281 do {
6282 /* Check FD flush status every 5-6msec */
6283 usleep_range(5000, 6000);
6284 reg = rd32(&pf->hw, I40E_PFQF_CTL_1);
6285 if (!(reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK))
6286 break;
6287 } while (flush_wait_retry--);
6288 if (reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK) {
6289 dev_warn(&pf->pdev->dev, "FD table did not flush, needs more time\n");
6290 } else {
6291 /* replay sideband filters */
6292 i40e_fdir_filter_restore(pf->vsi[pf->lan_vsi]);
Jacob Keller097dbf52017-02-06 14:38:46 -08006293 if (!disable_atr && !pf->fd_tcp4_filter_cnt)
Jacob Keller47994c12017-04-19 09:25:57 -04006294 pf->flags &= ~I40E_FLAG_FD_ATR_AUTO_DISABLED;
Jacob Keller0da36b92017-04-19 09:25:55 -04006295 clear_bit(__I40E_FD_FLUSH_REQUESTED, pf->state);
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04006296 if (I40E_DEBUG_FD & pf->hw.debug_mask)
6297 dev_info(&pf->pdev->dev, "FD Filter table flushed and FD-SB replayed.\n");
6298 }
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006299}
6300
6301/**
6302 * i40e_get_current_atr_count - Get the count of total FD ATR filters programmed
6303 * @pf: board private structure
6304 **/
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006305u32 i40e_get_current_atr_cnt(struct i40e_pf *pf)
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006306{
6307 return i40e_get_current_fd_count(pf) - pf->fdir_pf_active_filters;
6308}
6309
6310/* We can see up to 256 filter programming desc in transit if the filters are
6311 * being applied really fast; before we see the first
6312 * filter miss error on Rx queue 0. Accumulating enough error messages before
6313 * reacting will make sure we don't cause flush too often.
6314 */
6315#define I40E_MAX_FD_PROGRAM_ERROR 256
6316
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006317/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006318 * i40e_fdir_reinit_subtask - Worker thread to reinit FDIR filter table
6319 * @pf: board private structure
6320 **/
6321static void i40e_fdir_reinit_subtask(struct i40e_pf *pf)
6322{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006323
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006324 /* if interface is down do nothing */
Mauro S. M. Rodrigues9e6c9c02017-05-12 23:26:56 -03006325 if (test_bit(__I40E_DOWN, pf->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006326 return;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006327
Jacob Keller0da36b92017-04-19 09:25:55 -04006328 if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006329 i40e_fdir_flush_and_replay(pf);
6330
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006331 i40e_fdir_check_and_reenable(pf);
6332
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006333}
6334
6335/**
6336 * i40e_vsi_link_event - notify VSI of a link event
6337 * @vsi: vsi to be notified
6338 * @link_up: link up or down
6339 **/
6340static void i40e_vsi_link_event(struct i40e_vsi *vsi, bool link_up)
6341{
Jacob Keller0da36b92017-04-19 09:25:55 -04006342 if (!vsi || test_bit(__I40E_VSI_DOWN, vsi->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006343 return;
6344
6345 switch (vsi->type) {
6346 case I40E_VSI_MAIN:
6347 if (!vsi->netdev || !vsi->netdev_registered)
6348 break;
6349
6350 if (link_up) {
6351 netif_carrier_on(vsi->netdev);
6352 netif_tx_wake_all_queues(vsi->netdev);
6353 } else {
6354 netif_carrier_off(vsi->netdev);
6355 netif_tx_stop_all_queues(vsi->netdev);
6356 }
6357 break;
6358
6359 case I40E_VSI_SRIOV:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006360 case I40E_VSI_VMDQ2:
6361 case I40E_VSI_CTRL:
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06006362 case I40E_VSI_IWARP:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006363 case I40E_VSI_MIRROR:
6364 default:
6365 /* there is no notification for other VSIs */
6366 break;
6367 }
6368}
6369
6370/**
6371 * i40e_veb_link_event - notify elements on the veb of a link event
6372 * @veb: veb to be notified
6373 * @link_up: link up or down
6374 **/
6375static void i40e_veb_link_event(struct i40e_veb *veb, bool link_up)
6376{
6377 struct i40e_pf *pf;
6378 int i;
6379
6380 if (!veb || !veb->pf)
6381 return;
6382 pf = veb->pf;
6383
6384 /* depth first... */
6385 for (i = 0; i < I40E_MAX_VEB; i++)
6386 if (pf->veb[i] && (pf->veb[i]->uplink_seid == veb->seid))
6387 i40e_veb_link_event(pf->veb[i], link_up);
6388
6389 /* ... now the local VSIs */
Mitch Williams505682c2014-05-20 08:01:37 +00006390 for (i = 0; i < pf->num_alloc_vsi; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006391 if (pf->vsi[i] && (pf->vsi[i]->uplink_seid == veb->seid))
6392 i40e_vsi_link_event(pf->vsi[i], link_up);
6393}
6394
6395/**
6396 * i40e_link_event - Update netif_carrier status
6397 * @pf: board private structure
6398 **/
6399static void i40e_link_event(struct i40e_pf *pf)
6400{
Mitch Williams320684c2014-10-17 03:14:43 +00006401 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
Catherine Sullivanfef59dd2014-12-11 07:06:33 +00006402 u8 new_link_speed, old_link_speed;
Jesse Brandeburga72a5abc2015-08-26 15:14:19 -04006403 i40e_status status;
6404 bool new_link, old_link;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006405
Catherine Sullivan1f9610e2015-10-21 19:47:09 -04006406 /* save off old link status information */
6407 pf->hw.phy.link_info_old = pf->hw.phy.link_info;
6408
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00006409 /* set this to force the get_link_status call to refresh state */
6410 pf->hw.phy.get_link_info = true;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006411
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00006412 old_link = (pf->hw.phy.link_info_old.link_info & I40E_AQ_LINK_UP);
Jesse Brandeburga72a5abc2015-08-26 15:14:19 -04006413
6414 status = i40e_get_link_status(&pf->hw, &new_link);
Harshitha Ramamurthyae136702016-12-12 15:44:16 -08006415
6416 /* On success, disable temp link polling */
6417 if (status == I40E_SUCCESS) {
6418 if (pf->flags & I40E_FLAG_TEMP_LINK_POLLING)
6419 pf->flags &= ~I40E_FLAG_TEMP_LINK_POLLING;
6420 } else {
6421 /* Enable link polling temporarily until i40e_get_link_status
6422 * returns I40E_SUCCESS
6423 */
6424 pf->flags |= I40E_FLAG_TEMP_LINK_POLLING;
Jesse Brandeburga72a5abc2015-08-26 15:14:19 -04006425 dev_dbg(&pf->pdev->dev, "couldn't get link state, status: %d\n",
6426 status);
6427 return;
6428 }
6429
Catherine Sullivanfef59dd2014-12-11 07:06:33 +00006430 old_link_speed = pf->hw.phy.link_info_old.link_speed;
6431 new_link_speed = pf->hw.phy.link_info.link_speed;
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00006432
6433 if (new_link == old_link &&
Catherine Sullivanfef59dd2014-12-11 07:06:33 +00006434 new_link_speed == old_link_speed &&
Jacob Keller0da36b92017-04-19 09:25:55 -04006435 (test_bit(__I40E_VSI_DOWN, vsi->state) ||
Mitch Williams320684c2014-10-17 03:14:43 +00006436 new_link == netif_carrier_ok(vsi->netdev)))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006437 return;
Mitch Williams320684c2014-10-17 03:14:43 +00006438
Sudheer Mogilappagari9a034492017-07-14 09:10:16 -04006439 i40e_print_link_message(vsi, new_link);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006440
6441 /* Notify the base of the switch tree connected to
6442 * the link. Floating VEBs are not notified.
6443 */
6444 if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
6445 i40e_veb_link_event(pf->veb[pf->lan_veb], new_link);
6446 else
Mitch Williams320684c2014-10-17 03:14:43 +00006447 i40e_vsi_link_event(vsi, new_link);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006448
6449 if (pf->vf)
6450 i40e_vc_notify_link_state(pf);
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00006451
6452 if (pf->flags & I40E_FLAG_PTP)
6453 i40e_ptp_set_increment(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006454}
6455
6456/**
Shannon Nelson21536712014-10-25 10:35:25 +00006457 * i40e_watchdog_subtask - periodic checks not using event driven response
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006458 * @pf: board private structure
6459 **/
6460static void i40e_watchdog_subtask(struct i40e_pf *pf)
6461{
6462 int i;
6463
6464 /* if interface is down do nothing */
Mauro S. M. Rodrigues9e6c9c02017-05-12 23:26:56 -03006465 if (test_bit(__I40E_DOWN, pf->state) ||
Jacob Keller0da36b92017-04-19 09:25:55 -04006466 test_bit(__I40E_CONFIG_BUSY, pf->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006467 return;
6468
Shannon Nelson21536712014-10-25 10:35:25 +00006469 /* make sure we don't do these things too often */
6470 if (time_before(jiffies, (pf->service_timer_previous +
6471 pf->service_timer_period)))
6472 return;
6473 pf->service_timer_previous = jiffies;
6474
Harshitha Ramamurthyae136702016-12-12 15:44:16 -08006475 if ((pf->flags & I40E_FLAG_LINK_POLLING_ENABLED) ||
6476 (pf->flags & I40E_FLAG_TEMP_LINK_POLLING))
Shannon Nelson9ac77262015-08-27 11:42:40 -04006477 i40e_link_event(pf);
Shannon Nelson21536712014-10-25 10:35:25 +00006478
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006479 /* Update the stats for active netdevs so the network stack
6480 * can look at updated numbers whenever it cares to
6481 */
Mitch Williams505682c2014-05-20 08:01:37 +00006482 for (i = 0; i < pf->num_alloc_vsi; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006483 if (pf->vsi[i] && pf->vsi[i]->netdev)
6484 i40e_update_stats(pf->vsi[i]);
6485
Anjali Singhai Jaind1a8d272015-07-23 16:54:40 -04006486 if (pf->flags & I40E_FLAG_VEB_STATS_ENABLED) {
6487 /* Update the stats for the active switching components */
6488 for (i = 0; i < I40E_MAX_VEB; i++)
6489 if (pf->veb[i])
6490 i40e_update_veb_stats(pf->veb[i]);
6491 }
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00006492
Jacob Keller61189552017-05-03 10:29:01 -07006493 i40e_ptp_rx_hang(pf);
Jacob Keller0bc07062017-05-03 10:29:02 -07006494 i40e_ptp_tx_hang(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006495}
6496
6497/**
6498 * i40e_reset_subtask - Set up for resetting the device and driver
6499 * @pf: board private structure
6500 **/
6501static void i40e_reset_subtask(struct i40e_pf *pf)
6502{
6503 u32 reset_flags = 0;
6504
Jacob Keller0da36b92017-04-19 09:25:55 -04006505 if (test_bit(__I40E_REINIT_REQUESTED, pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006506 reset_flags |= BIT(__I40E_REINIT_REQUESTED);
Jacob Keller0da36b92017-04-19 09:25:55 -04006507 clear_bit(__I40E_REINIT_REQUESTED, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006508 }
Jacob Keller0da36b92017-04-19 09:25:55 -04006509 if (test_bit(__I40E_PF_RESET_REQUESTED, pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006510 reset_flags |= BIT(__I40E_PF_RESET_REQUESTED);
Jacob Keller0da36b92017-04-19 09:25:55 -04006511 clear_bit(__I40E_PF_RESET_REQUESTED, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006512 }
Jacob Keller0da36b92017-04-19 09:25:55 -04006513 if (test_bit(__I40E_CORE_RESET_REQUESTED, pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006514 reset_flags |= BIT(__I40E_CORE_RESET_REQUESTED);
Jacob Keller0da36b92017-04-19 09:25:55 -04006515 clear_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006516 }
Jacob Keller0da36b92017-04-19 09:25:55 -04006517 if (test_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006518 reset_flags |= BIT(__I40E_GLOBAL_RESET_REQUESTED);
Jacob Keller0da36b92017-04-19 09:25:55 -04006519 clear_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006520 }
Mauro S. M. Rodrigues9e6c9c02017-05-12 23:26:56 -03006521 if (test_bit(__I40E_DOWN_REQUESTED, pf->state)) {
6522 reset_flags |= BIT(__I40E_DOWN_REQUESTED);
6523 clear_bit(__I40E_DOWN_REQUESTED, pf->state);
Neerav Parikhb5d06f02014-06-03 23:50:17 +00006524 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006525
6526 /* If there's a recovery already waiting, it takes
6527 * precedence before starting a new reset sequence.
6528 */
Jacob Keller0da36b92017-04-19 09:25:55 -04006529 if (test_bit(__I40E_RESET_INTR_RECEIVED, pf->state)) {
Maciej Sosin373149f2017-04-05 07:50:55 -04006530 i40e_prep_for_reset(pf, false);
6531 i40e_reset(pf);
6532 i40e_rebuild(pf, false, false);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006533 }
6534
6535 /* If we're already down or resetting, just bail */
6536 if (reset_flags &&
Mauro S. M. Rodrigues9e6c9c02017-05-12 23:26:56 -03006537 !test_bit(__I40E_DOWN, pf->state) &&
Jacob Keller0da36b92017-04-19 09:25:55 -04006538 !test_bit(__I40E_CONFIG_BUSY, pf->state)) {
Jacob Kellerdfc4ff62017-06-07 05:43:13 -04006539 i40e_do_reset(pf, reset_flags, false);
Maciej Sosin373149f2017-04-05 07:50:55 -04006540 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006541}
6542
6543/**
6544 * i40e_handle_link_event - Handle link event
6545 * @pf: board private structure
6546 * @e: event info posted on ARQ
6547 **/
6548static void i40e_handle_link_event(struct i40e_pf *pf,
6549 struct i40e_arq_event_info *e)
6550{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006551 struct i40e_aqc_get_link_status *status =
6552 (struct i40e_aqc_get_link_status *)&e->desc.params.raw;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006553
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00006554 /* Do a new status request to re-enable LSE reporting
6555 * and load new status information into the hw struct
6556 * This completely ignores any state information
6557 * in the ARQ event info, instead choosing to always
6558 * issue the AQ update link status command.
6559 */
6560 i40e_link_event(pf);
6561
Filip Sadowski9a858172017-08-29 05:32:37 -04006562 /* Check if module meets thermal requirements */
6563 if (status->phy_type == I40E_PHY_TYPE_NOT_SUPPORTED_HIGH_TEMP) {
Carolyn Wyborny7b592f62014-07-10 07:58:19 +00006564 dev_err(&pf->pdev->dev,
Filip Sadowski9a858172017-08-29 05:32:37 -04006565 "Rx/Tx is disabled on this device because the module does not meet thermal requirements.\n");
6566 dev_err(&pf->pdev->dev,
6567 "Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules.\n");
6568 } else {
6569 /* check for unqualified module, if link is down, suppress
6570 * the message if link was forced to be down.
6571 */
6572 if ((status->link_info & I40E_AQ_MEDIA_AVAILABLE) &&
6573 (!(status->an_info & I40E_AQ_QUALIFIED_MODULE)) &&
6574 (!(status->link_info & I40E_AQ_LINK_UP)) &&
6575 (!(pf->flags & I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED))) {
6576 dev_err(&pf->pdev->dev,
6577 "Rx/Tx is disabled on this device because an unsupported SFP module type was detected.\n");
6578 dev_err(&pf->pdev->dev,
6579 "Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules.\n");
6580 }
6581 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006582}
6583
6584/**
6585 * i40e_clean_adminq_subtask - Clean the AdminQ rings
6586 * @pf: board private structure
6587 **/
6588static void i40e_clean_adminq_subtask(struct i40e_pf *pf)
6589{
6590 struct i40e_arq_event_info event;
6591 struct i40e_hw *hw = &pf->hw;
6592 u16 pending, i = 0;
6593 i40e_status ret;
6594 u16 opcode;
Shannon Nelson86df2422014-05-20 08:01:35 +00006595 u32 oldval;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006596 u32 val;
6597
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00006598 /* Do not run clean AQ when PF reset fails */
Jacob Keller0da36b92017-04-19 09:25:55 -04006599 if (test_bit(__I40E_RESET_FAILED, pf->state))
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00006600 return;
6601
Shannon Nelson86df2422014-05-20 08:01:35 +00006602 /* check for error indications */
6603 val = rd32(&pf->hw, pf->hw.aq.arq.len);
6604 oldval = val;
6605 if (val & I40E_PF_ARQLEN_ARQVFE_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006606 if (hw->debug_mask & I40E_DEBUG_AQ)
6607 dev_info(&pf->pdev->dev, "ARQ VF Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006608 val &= ~I40E_PF_ARQLEN_ARQVFE_MASK;
6609 }
6610 if (val & I40E_PF_ARQLEN_ARQOVFL_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006611 if (hw->debug_mask & I40E_DEBUG_AQ)
6612 dev_info(&pf->pdev->dev, "ARQ Overflow Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006613 val &= ~I40E_PF_ARQLEN_ARQOVFL_MASK;
Mitch Williams1d0a4ad2015-12-23 12:05:48 -08006614 pf->arq_overflows++;
Shannon Nelson86df2422014-05-20 08:01:35 +00006615 }
6616 if (val & I40E_PF_ARQLEN_ARQCRIT_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006617 if (hw->debug_mask & I40E_DEBUG_AQ)
6618 dev_info(&pf->pdev->dev, "ARQ Critical Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006619 val &= ~I40E_PF_ARQLEN_ARQCRIT_MASK;
6620 }
6621 if (oldval != val)
6622 wr32(&pf->hw, pf->hw.aq.arq.len, val);
6623
6624 val = rd32(&pf->hw, pf->hw.aq.asq.len);
6625 oldval = val;
6626 if (val & I40E_PF_ATQLEN_ATQVFE_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006627 if (pf->hw.debug_mask & I40E_DEBUG_AQ)
6628 dev_info(&pf->pdev->dev, "ASQ VF Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006629 val &= ~I40E_PF_ATQLEN_ATQVFE_MASK;
6630 }
6631 if (val & I40E_PF_ATQLEN_ATQOVFL_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006632 if (pf->hw.debug_mask & I40E_DEBUG_AQ)
6633 dev_info(&pf->pdev->dev, "ASQ Overflow Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006634 val &= ~I40E_PF_ATQLEN_ATQOVFL_MASK;
6635 }
6636 if (val & I40E_PF_ATQLEN_ATQCRIT_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006637 if (pf->hw.debug_mask & I40E_DEBUG_AQ)
6638 dev_info(&pf->pdev->dev, "ASQ Critical Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006639 val &= ~I40E_PF_ATQLEN_ATQCRIT_MASK;
6640 }
6641 if (oldval != val)
6642 wr32(&pf->hw, pf->hw.aq.asq.len, val);
6643
Mitch Williams1001dc32014-11-11 20:02:19 +00006644 event.buf_len = I40E_MAX_AQ_BUF_SIZE;
6645 event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006646 if (!event.msg_buf)
6647 return;
6648
6649 do {
6650 ret = i40e_clean_arq_element(hw, &event, &pending);
Mitch Williams56497972014-06-04 08:45:18 +00006651 if (ret == I40E_ERR_ADMIN_QUEUE_NO_WORK)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006652 break;
Mitch Williams56497972014-06-04 08:45:18 +00006653 else if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006654 dev_info(&pf->pdev->dev, "ARQ event error %d\n", ret);
6655 break;
6656 }
6657
6658 opcode = le16_to_cpu(event.desc.opcode);
6659 switch (opcode) {
6660
6661 case i40e_aqc_opc_get_link_status:
6662 i40e_handle_link_event(pf, &event);
6663 break;
6664 case i40e_aqc_opc_send_msg_to_pf:
6665 ret = i40e_vc_process_vf_msg(pf,
6666 le16_to_cpu(event.desc.retval),
6667 le32_to_cpu(event.desc.cookie_high),
6668 le32_to_cpu(event.desc.cookie_low),
6669 event.msg_buf,
Mitch Williams1001dc32014-11-11 20:02:19 +00006670 event.msg_len);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006671 break;
6672 case i40e_aqc_opc_lldp_update_mib:
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006673 dev_dbg(&pf->pdev->dev, "ARQ: Update LLDP MIB event received\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006674#ifdef CONFIG_I40E_DCB
6675 rtnl_lock();
6676 ret = i40e_handle_lldp_event(pf, &event);
6677 rtnl_unlock();
6678#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006679 break;
6680 case i40e_aqc_opc_event_lan_overflow:
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006681 dev_dbg(&pf->pdev->dev, "ARQ LAN queue overflow event received\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006682 i40e_handle_lan_overflow_event(pf, &event);
6683 break;
Shannon Nelson0467bc92013-12-18 13:45:58 +00006684 case i40e_aqc_opc_send_msg_to_peer:
6685 dev_info(&pf->pdev->dev, "ARQ: Msg from other pf\n");
6686 break;
Shannon Nelson91a0f932015-03-19 14:32:01 -07006687 case i40e_aqc_opc_nvm_erase:
6688 case i40e_aqc_opc_nvm_update:
Michal Kosiarz00ada502015-11-19 11:34:20 -08006689 case i40e_aqc_opc_oem_post_update:
Shannon Nelson6e93d0c2016-01-15 14:33:18 -08006690 i40e_debug(&pf->hw, I40E_DEBUG_NVM,
6691 "ARQ NVM operation 0x%04x completed\n",
6692 opcode);
Shannon Nelson91a0f932015-03-19 14:32:01 -07006693 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006694 default:
6695 dev_info(&pf->pdev->dev,
Shannon Nelson56e5ca62016-03-10 14:59:48 -08006696 "ARQ: Unknown event 0x%04x ignored\n",
Shannon Nelson0467bc92013-12-18 13:45:58 +00006697 opcode);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006698 break;
6699 }
Christopher N Bednarz1fca3262017-03-10 12:22:03 -08006700 } while (i++ < pf->adminq_work_limit);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006701
Christopher N Bednarz1fca3262017-03-10 12:22:03 -08006702 if (i < pf->adminq_work_limit)
Jacob Keller0da36b92017-04-19 09:25:55 -04006703 clear_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state);
Christopher N Bednarz1fca3262017-03-10 12:22:03 -08006704
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006705 /* re-enable Admin queue interrupt cause */
6706 val = rd32(hw, I40E_PFINT_ICR0_ENA);
6707 val |= I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
6708 wr32(hw, I40E_PFINT_ICR0_ENA, val);
6709 i40e_flush(hw);
6710
6711 kfree(event.msg_buf);
6712}
6713
6714/**
Shannon Nelson4eb3f762014-03-06 08:59:58 +00006715 * i40e_verify_eeprom - make sure eeprom is good to use
6716 * @pf: board private structure
6717 **/
6718static void i40e_verify_eeprom(struct i40e_pf *pf)
6719{
6720 int err;
6721
6722 err = i40e_diag_eeprom_test(&pf->hw);
6723 if (err) {
6724 /* retry in case of garbage read */
6725 err = i40e_diag_eeprom_test(&pf->hw);
6726 if (err) {
6727 dev_info(&pf->pdev->dev, "eeprom check failed (%d), Tx/Rx traffic disabled\n",
6728 err);
Jacob Keller0da36b92017-04-19 09:25:55 -04006729 set_bit(__I40E_BAD_EEPROM, pf->state);
Shannon Nelson4eb3f762014-03-06 08:59:58 +00006730 }
6731 }
6732
Jacob Keller0da36b92017-04-19 09:25:55 -04006733 if (!err && test_bit(__I40E_BAD_EEPROM, pf->state)) {
Shannon Nelson4eb3f762014-03-06 08:59:58 +00006734 dev_info(&pf->pdev->dev, "eeprom check passed, Tx/Rx traffic enabled\n");
Jacob Keller0da36b92017-04-19 09:25:55 -04006735 clear_bit(__I40E_BAD_EEPROM, pf->state);
Shannon Nelson4eb3f762014-03-06 08:59:58 +00006736 }
6737}
6738
6739/**
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006740 * i40e_enable_pf_switch_lb
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00006741 * @pf: pointer to the PF structure
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006742 *
6743 * enable switch loop back or die - no point in a return value
6744 **/
6745static void i40e_enable_pf_switch_lb(struct i40e_pf *pf)
6746{
6747 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
6748 struct i40e_vsi_context ctxt;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006749 int ret;
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006750
6751 ctxt.seid = pf->main_vsi_seid;
6752 ctxt.pf_num = pf->hw.pf_id;
6753 ctxt.vf_num = 0;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006754 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
6755 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006756 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006757 "couldn't get PF vsi config, err %s aq_err %s\n",
6758 i40e_stat_str(&pf->hw, ret),
6759 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006760 return;
6761 }
6762 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
6763 ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
6764 ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
6765
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006766 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
6767 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006768 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006769 "update vsi switch failed, err %s aq_err %s\n",
6770 i40e_stat_str(&pf->hw, ret),
6771 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006772 }
6773}
6774
6775/**
6776 * i40e_disable_pf_switch_lb
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00006777 * @pf: pointer to the PF structure
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006778 *
6779 * disable switch loop back or die - no point in a return value
6780 **/
6781static void i40e_disable_pf_switch_lb(struct i40e_pf *pf)
6782{
6783 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
6784 struct i40e_vsi_context ctxt;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006785 int ret;
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006786
6787 ctxt.seid = pf->main_vsi_seid;
6788 ctxt.pf_num = pf->hw.pf_id;
6789 ctxt.vf_num = 0;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006790 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
6791 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006792 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006793 "couldn't get PF vsi config, err %s aq_err %s\n",
6794 i40e_stat_str(&pf->hw, ret),
6795 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006796 return;
6797 }
6798 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
6799 ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
6800 ctxt.info.switch_id &= ~cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
6801
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006802 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
6803 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006804 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006805 "update vsi switch failed, err %s aq_err %s\n",
6806 i40e_stat_str(&pf->hw, ret),
6807 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006808 }
6809}
6810
6811/**
Neerav Parikh51616012015-02-06 08:52:14 +00006812 * i40e_config_bridge_mode - Configure the HW bridge mode
6813 * @veb: pointer to the bridge instance
6814 *
6815 * Configure the loop back mode for the LAN VSI that is downlink to the
6816 * specified HW bridge instance. It is expected this function is called
6817 * when a new HW bridge is instantiated.
6818 **/
6819static void i40e_config_bridge_mode(struct i40e_veb *veb)
6820{
6821 struct i40e_pf *pf = veb->pf;
6822
Shannon Nelson6dec1012015-09-28 14:12:30 -04006823 if (pf->hw.debug_mask & I40E_DEBUG_LAN)
6824 dev_info(&pf->pdev->dev, "enabling bridge mode: %s\n",
6825 veb->bridge_mode == BRIDGE_MODE_VEPA ? "VEPA" : "VEB");
Neerav Parikh51616012015-02-06 08:52:14 +00006826 if (veb->bridge_mode & BRIDGE_MODE_VEPA)
6827 i40e_disable_pf_switch_lb(pf);
6828 else
6829 i40e_enable_pf_switch_lb(pf);
6830}
6831
6832/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006833 * i40e_reconstitute_veb - rebuild the VEB and anything connected to it
6834 * @veb: pointer to the VEB instance
6835 *
6836 * This is a recursive function that first builds the attached VSIs then
6837 * recurses in to build the next layer of VEB. We track the connections
6838 * through our own index numbers because the seid's from the HW could
6839 * change across the reset.
6840 **/
6841static int i40e_reconstitute_veb(struct i40e_veb *veb)
6842{
6843 struct i40e_vsi *ctl_vsi = NULL;
6844 struct i40e_pf *pf = veb->pf;
6845 int v, veb_idx;
6846 int ret;
6847
6848 /* build VSI that owns this VEB, temporarily attached to base VEB */
Mitch Williams505682c2014-05-20 08:01:37 +00006849 for (v = 0; v < pf->num_alloc_vsi && !ctl_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006850 if (pf->vsi[v] &&
6851 pf->vsi[v]->veb_idx == veb->idx &&
6852 pf->vsi[v]->flags & I40E_VSI_FLAG_VEB_OWNER) {
6853 ctl_vsi = pf->vsi[v];
6854 break;
6855 }
6856 }
6857 if (!ctl_vsi) {
6858 dev_info(&pf->pdev->dev,
6859 "missing owner VSI for veb_idx %d\n", veb->idx);
6860 ret = -ENOENT;
6861 goto end_reconstitute;
6862 }
6863 if (ctl_vsi != pf->vsi[pf->lan_vsi])
6864 ctl_vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
6865 ret = i40e_add_vsi(ctl_vsi);
6866 if (ret) {
6867 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006868 "rebuild of veb_idx %d owner VSI failed: %d\n",
6869 veb->idx, ret);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006870 goto end_reconstitute;
6871 }
6872 i40e_vsi_reset_stats(ctl_vsi);
6873
6874 /* create the VEB in the switch and move the VSI onto the VEB */
6875 ret = i40e_add_veb(veb, ctl_vsi);
6876 if (ret)
6877 goto end_reconstitute;
6878
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07006879 if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
6880 veb->bridge_mode = BRIDGE_MODE_VEB;
6881 else
6882 veb->bridge_mode = BRIDGE_MODE_VEPA;
Neerav Parikh51616012015-02-06 08:52:14 +00006883 i40e_config_bridge_mode(veb);
Anjali Singhai Jainb64ba082014-11-13 03:06:15 +00006884
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006885 /* create the remaining VSIs attached to this VEB */
Mitch Williams505682c2014-05-20 08:01:37 +00006886 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006887 if (!pf->vsi[v] || pf->vsi[v] == ctl_vsi)
6888 continue;
6889
6890 if (pf->vsi[v]->veb_idx == veb->idx) {
6891 struct i40e_vsi *vsi = pf->vsi[v];
Jesse Brandeburg6995b362015-08-28 17:55:54 -04006892
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006893 vsi->uplink_seid = veb->seid;
6894 ret = i40e_add_vsi(vsi);
6895 if (ret) {
6896 dev_info(&pf->pdev->dev,
6897 "rebuild of vsi_idx %d failed: %d\n",
6898 v, ret);
6899 goto end_reconstitute;
6900 }
6901 i40e_vsi_reset_stats(vsi);
6902 }
6903 }
6904
6905 /* create any VEBs attached to this VEB - RECURSION */
6906 for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
6907 if (pf->veb[veb_idx] && pf->veb[veb_idx]->veb_idx == veb->idx) {
6908 pf->veb[veb_idx]->uplink_seid = veb->seid;
6909 ret = i40e_reconstitute_veb(pf->veb[veb_idx]);
6910 if (ret)
6911 break;
6912 }
6913 }
6914
6915end_reconstitute:
6916 return ret;
6917}
6918
6919/**
6920 * i40e_get_capabilities - get info about the HW
6921 * @pf: the PF struct
6922 **/
6923static int i40e_get_capabilities(struct i40e_pf *pf)
6924{
6925 struct i40e_aqc_list_capabilities_element_resp *cap_buf;
6926 u16 data_size;
6927 int buf_len;
6928 int err;
6929
6930 buf_len = 40 * sizeof(struct i40e_aqc_list_capabilities_element_resp);
6931 do {
6932 cap_buf = kzalloc(buf_len, GFP_KERNEL);
6933 if (!cap_buf)
6934 return -ENOMEM;
6935
6936 /* this loads the data into the hw struct for us */
6937 err = i40e_aq_discover_capabilities(&pf->hw, cap_buf, buf_len,
6938 &data_size,
6939 i40e_aqc_opc_list_func_capabilities,
6940 NULL);
6941 /* data loaded, buffer no longer needed */
6942 kfree(cap_buf);
6943
6944 if (pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOMEM) {
6945 /* retry with a larger buffer */
6946 buf_len = data_size;
6947 } else if (pf->hw.aq.asq_last_status != I40E_AQ_RC_OK) {
6948 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006949 "capability discovery failed, err %s aq_err %s\n",
6950 i40e_stat_str(&pf->hw, err),
6951 i40e_aq_str(&pf->hw,
6952 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006953 return -ENODEV;
6954 }
6955 } while (err);
6956
6957 if (pf->hw.debug_mask & I40E_DEBUG_USER)
6958 dev_info(&pf->pdev->dev,
6959 "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",
6960 pf->hw.pf_id, pf->hw.func_caps.num_vfs,
6961 pf->hw.func_caps.num_msix_vectors,
6962 pf->hw.func_caps.num_msix_vectors_vf,
6963 pf->hw.func_caps.fd_filters_guaranteed,
6964 pf->hw.func_caps.fd_filters_best_effort,
6965 pf->hw.func_caps.num_tx_qp,
6966 pf->hw.func_caps.num_vsis);
6967
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00006968#define DEF_NUM_VSI (1 + (pf->hw.func_caps.fcoe ? 1 : 0) \
6969 + pf->hw.func_caps.num_vfs)
6970 if (pf->hw.revision_id == 0 && (DEF_NUM_VSI > pf->hw.func_caps.num_vsis)) {
6971 dev_info(&pf->pdev->dev,
6972 "got num_vsis %d, setting num_vsis to %d\n",
6973 pf->hw.func_caps.num_vsis, DEF_NUM_VSI);
6974 pf->hw.func_caps.num_vsis = DEF_NUM_VSI;
6975 }
6976
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006977 return 0;
6978}
6979
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006980static int i40e_vsi_clear(struct i40e_vsi *vsi);
6981
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006982/**
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006983 * i40e_fdir_sb_setup - initialize the Flow Director resources for Sideband
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006984 * @pf: board private structure
6985 **/
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006986static void i40e_fdir_sb_setup(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006987{
6988 struct i40e_vsi *vsi;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006989
Jesse Brandeburg407e0632014-06-03 23:50:12 +00006990 /* quick workaround for an NVM issue that leaves a critical register
6991 * uninitialized
6992 */
6993 if (!rd32(&pf->hw, I40E_GLQF_HKEY(0))) {
6994 static const u32 hkey[] = {
6995 0xe640d33f, 0xcdfe98ab, 0x73fa7161, 0x0d7a7d36,
6996 0xeacb7d61, 0xaa4f05b6, 0x9c5c89ed, 0xfc425ddb,
6997 0xa4654832, 0xfc7461d4, 0x8f827619, 0xf5c63c21,
6998 0x95b3a76d};
Alexander Duyck4b816442016-10-11 15:26:53 -07006999 int i;
Jesse Brandeburg407e0632014-06-03 23:50:12 +00007000
7001 for (i = 0; i <= I40E_GLQF_HKEY_MAX_INDEX; i++)
7002 wr32(&pf->hw, I40E_GLQF_HKEY(i), hkey[i]);
7003 }
7004
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08007005 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007006 return;
7007
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08007008 /* find existing VSI and see if it needs configuring */
Alexander Duyck4b816442016-10-11 15:26:53 -07007009 vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08007010
7011 /* create a new VSI if none exists */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007012 if (!vsi) {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08007013 vsi = i40e_vsi_setup(pf, I40E_VSI_FDIR,
7014 pf->vsi[pf->lan_vsi]->seid, 0);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007015 if (!vsi) {
7016 dev_info(&pf->pdev->dev, "Couldn't create FDir VSI\n");
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00007017 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
7018 return;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007019 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007020 }
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00007021
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08007022 i40e_vsi_setup_irqhandler(vsi, i40e_fdir_clean_ring);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007023}
7024
7025/**
7026 * i40e_fdir_teardown - release the Flow Director resources
7027 * @pf: board private structure
7028 **/
7029static void i40e_fdir_teardown(struct i40e_pf *pf)
7030{
Alexander Duyck4b816442016-10-11 15:26:53 -07007031 struct i40e_vsi *vsi;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007032
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00007033 i40e_fdir_filter_exit(pf);
Alexander Duyck4b816442016-10-11 15:26:53 -07007034 vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
7035 if (vsi)
7036 i40e_vsi_release(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007037}
7038
7039/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007040 * i40e_prep_for_reset - prep for the core to reset
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007041 * @pf: board private structure
Maciej Sosin373149f2017-04-05 07:50:55 -04007042 * @lock_acquired: indicates whether or not the lock has been acquired
7043 * before this function was called.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007044 *
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00007045 * Close up the VFs and other things in prep for PF Reset.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007046 **/
Maciej Sosin373149f2017-04-05 07:50:55 -04007047static void i40e_prep_for_reset(struct i40e_pf *pf, bool lock_acquired)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007048{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007049 struct i40e_hw *hw = &pf->hw;
Shannon Nelson60442de2014-04-23 04:50:13 +00007050 i40e_status ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007051 u32 v;
7052
Jacob Keller0da36b92017-04-19 09:25:55 -04007053 clear_bit(__I40E_RESET_INTR_RECEIVED, pf->state);
7054 if (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
Shannon Nelson23cfbe02014-06-04 01:23:14 +00007055 return;
Mitch Williamsd3ce57342016-03-10 14:59:46 -08007056 if (i40e_check_asq_alive(&pf->hw))
7057 i40e_vc_notify_reset(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007058
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00007059 dev_dbg(&pf->pdev->dev, "Tearing down internal switch for reset\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007060
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007061 /* quiesce the VSIs and their queues that are not already DOWN */
Maciej Sosin373149f2017-04-05 07:50:55 -04007062 /* pf_quiesce_all_vsi modifies netdev structures -rtnl_lock needed */
7063 if (!lock_acquired)
7064 rtnl_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007065 i40e_pf_quiesce_all_vsi(pf);
Maciej Sosin373149f2017-04-05 07:50:55 -04007066 if (!lock_acquired)
7067 rtnl_unlock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007068
Mitch Williams505682c2014-05-20 08:01:37 +00007069 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007070 if (pf->vsi[v])
7071 pf->vsi[v]->seid = 0;
7072 }
7073
7074 i40e_shutdown_adminq(&pf->hw);
7075
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007076 /* call shutdown HMC */
Shannon Nelson60442de2014-04-23 04:50:13 +00007077 if (hw->hmc.hmc_obj) {
7078 ret = i40e_shutdown_lan_hmc(hw);
Shannon Nelson23cfbe02014-06-04 01:23:14 +00007079 if (ret)
Shannon Nelson60442de2014-04-23 04:50:13 +00007080 dev_warn(&pf->pdev->dev,
7081 "shutdown_lan_hmc failed: %d\n", ret);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007082 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007083}
7084
7085/**
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00007086 * i40e_send_version - update firmware with driver version
7087 * @pf: PF struct
7088 */
7089static void i40e_send_version(struct i40e_pf *pf)
7090{
7091 struct i40e_driver_version dv;
7092
7093 dv.major_version = DRV_VERSION_MAJOR;
7094 dv.minor_version = DRV_VERSION_MINOR;
7095 dv.build_version = DRV_VERSION_BUILD;
7096 dv.subbuild_version = 0;
Rickard Strandqvist35a7d802014-07-29 09:26:25 +00007097 strlcpy(dv.driver_string, DRV_VERSION, sizeof(dv.driver_string));
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00007098 i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
7099}
7100
7101/**
Filip Sadowski5bbb2e22017-06-07 05:43:09 -04007102 * i40e_get_oem_version - get OEM specific version information
7103 * @hw: pointer to the hardware structure
7104 **/
7105static void i40e_get_oem_version(struct i40e_hw *hw)
7106{
7107 u16 block_offset = 0xffff;
7108 u16 block_length = 0;
7109 u16 capabilities = 0;
7110 u16 gen_snap = 0;
7111 u16 release = 0;
7112
7113#define I40E_SR_NVM_OEM_VERSION_PTR 0x1B
7114#define I40E_NVM_OEM_LENGTH_OFFSET 0x00
7115#define I40E_NVM_OEM_CAPABILITIES_OFFSET 0x01
7116#define I40E_NVM_OEM_GEN_OFFSET 0x02
7117#define I40E_NVM_OEM_RELEASE_OFFSET 0x03
7118#define I40E_NVM_OEM_CAPABILITIES_MASK 0x000F
7119#define I40E_NVM_OEM_LENGTH 3
7120
7121 /* Check if pointer to OEM version block is valid. */
7122 i40e_read_nvm_word(hw, I40E_SR_NVM_OEM_VERSION_PTR, &block_offset);
7123 if (block_offset == 0xffff)
7124 return;
7125
7126 /* Check if OEM version block has correct length. */
7127 i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_LENGTH_OFFSET,
7128 &block_length);
7129 if (block_length < I40E_NVM_OEM_LENGTH)
7130 return;
7131
7132 /* Check if OEM version format is as expected. */
7133 i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_CAPABILITIES_OFFSET,
7134 &capabilities);
7135 if ((capabilities & I40E_NVM_OEM_CAPABILITIES_MASK) != 0)
7136 return;
7137
7138 i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_GEN_OFFSET,
7139 &gen_snap);
7140 i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_RELEASE_OFFSET,
7141 &release);
7142 hw->nvm.oem_ver = (gen_snap << I40E_OEM_SNAP_SHIFT) | release;
7143 hw->nvm.eetrack = I40E_OEM_EETRACK_ID;
7144}
7145
7146/**
Maciej Sosin373149f2017-04-05 07:50:55 -04007147 * i40e_reset - wait for core reset to finish reset, reset pf if corer not seen
7148 * @pf: board private structure
7149 **/
7150static int i40e_reset(struct i40e_pf *pf)
7151{
7152 struct i40e_hw *hw = &pf->hw;
7153 i40e_status ret;
7154
7155 ret = i40e_pf_reset(hw);
7156 if (ret) {
7157 dev_info(&pf->pdev->dev, "PF reset failed, %d\n", ret);
Jacob Keller0da36b92017-04-19 09:25:55 -04007158 set_bit(__I40E_RESET_FAILED, pf->state);
7159 clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
Maciej Sosin373149f2017-04-05 07:50:55 -04007160 } else {
7161 pf->pfr_count++;
7162 }
7163 return ret;
7164}
7165
7166/**
7167 * i40e_rebuild - rebuild using a saved config
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007168 * @pf: board private structure
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007169 * @reinit: if the Main VSI needs to re-initialized.
Maciej Sosin373149f2017-04-05 07:50:55 -04007170 * @lock_acquired: indicates whether or not the lock has been acquired
7171 * before this function was called.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007172 **/
Maciej Sosin373149f2017-04-05 07:50:55 -04007173static void i40e_rebuild(struct i40e_pf *pf, bool reinit, bool lock_acquired)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007174{
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007175 struct i40e_hw *hw = &pf->hw;
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00007176 u8 set_fc_aq_fail = 0;
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007177 i40e_status ret;
Anjali Singhai Jain4f2f017c2015-10-21 19:47:07 -04007178 u32 val;
Maciej Sosin373149f2017-04-05 07:50:55 -04007179 int v;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007180
Mauro S. M. Rodrigues9e6c9c02017-05-12 23:26:56 -03007181 if (test_bit(__I40E_DOWN, pf->state))
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00007182 goto clear_recovery;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00007183 dev_dbg(&pf->pdev->dev, "Rebuilding internal switch\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007184
7185 /* rebuild the basics for the AdminQ, HMC, and initial HW switch */
7186 ret = i40e_init_adminq(&pf->hw);
7187 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04007188 dev_info(&pf->pdev->dev, "Rebuild AdminQ failed, err %s aq_err %s\n",
7189 i40e_stat_str(&pf->hw, ret),
7190 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00007191 goto clear_recovery;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007192 }
Filip Sadowski5bbb2e22017-06-07 05:43:09 -04007193 i40e_get_oem_version(&pf->hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007194
Shannon Nelson4eb3f762014-03-06 08:59:58 +00007195 /* re-verify the eeprom if we just had an EMP reset */
Jacob Keller0da36b92017-04-19 09:25:55 -04007196 if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state))
Shannon Nelson4eb3f762014-03-06 08:59:58 +00007197 i40e_verify_eeprom(pf);
Shannon Nelson4eb3f762014-03-06 08:59:58 +00007198
Shannon Nelsone78ac4bf2014-05-10 04:49:09 +00007199 i40e_clear_pxe_mode(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007200 ret = i40e_get_capabilities(pf);
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04007201 if (ret)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007202 goto end_core_reset;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007203
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007204 ret = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
Alexander Duyckc76cb6e2017-02-21 15:55:48 -08007205 hw->func_caps.num_rx_qp, 0, 0);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007206 if (ret) {
7207 dev_info(&pf->pdev->dev, "init_lan_hmc failed: %d\n", ret);
7208 goto end_core_reset;
7209 }
7210 ret = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
7211 if (ret) {
7212 dev_info(&pf->pdev->dev, "configure_lan_hmc failed: %d\n", ret);
7213 goto end_core_reset;
7214 }
7215
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08007216#ifdef CONFIG_I40E_DCB
7217 ret = i40e_init_pf_dcb(pf);
7218 if (ret) {
Shannon Nelsonaebfc812014-12-11 07:06:38 +00007219 dev_info(&pf->pdev->dev, "DCB init failed %d, disabled\n", ret);
7220 pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
7221 /* Continue without DCB enabled */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08007222 }
7223#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007224 /* do basic switch setup */
Maciej Sosin373149f2017-04-05 07:50:55 -04007225 if (!lock_acquired)
7226 rtnl_lock();
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007227 ret = i40e_setup_pf_switch(pf, reinit);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007228 if (ret)
Maciej Sosin373149f2017-04-05 07:50:55 -04007229 goto end_unlock;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007230
Shannon Nelson2f0aff42016-01-04 10:33:08 -08007231 /* The driver only wants link up/down and module qualification
7232 * reports from firmware. Note the negative logic.
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +00007233 */
7234 ret = i40e_aq_set_phy_int_mask(&pf->hw,
Shannon Nelson2f0aff42016-01-04 10:33:08 -08007235 ~(I40E_AQ_EVENT_LINK_UPDOWN |
Shannon Nelson867a79e2016-03-18 12:18:15 -07007236 I40E_AQ_EVENT_MEDIA_NA |
Shannon Nelson2f0aff42016-01-04 10:33:08 -08007237 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +00007238 if (ret)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04007239 dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
7240 i40e_stat_str(&pf->hw, ret),
7241 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +00007242
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00007243 /* make sure our flow control settings are restored */
7244 ret = i40e_set_fc(&pf->hw, &set_fc_aq_fail, true);
7245 if (ret)
Neerav Parikh8279e492015-09-03 17:18:50 -04007246 dev_dbg(&pf->pdev->dev, "setting flow control: ret = %s last_status = %s\n",
7247 i40e_stat_str(&pf->hw, ret),
7248 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00007249
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007250 /* Rebuild the VSIs and VEBs that existed before reset.
7251 * They are still in our local switch element arrays, so only
7252 * need to rebuild the switch model in the HW.
7253 *
7254 * If there were VEBs but the reconstitution failed, we'll try
7255 * try to recover minimal use by getting the basic PF VSI working.
7256 */
7257 if (pf->vsi[pf->lan_vsi]->uplink_seid != pf->mac_seid) {
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00007258 dev_dbg(&pf->pdev->dev, "attempting to rebuild switch\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007259 /* find the one VEB connected to the MAC, and find orphans */
7260 for (v = 0; v < I40E_MAX_VEB; v++) {
7261 if (!pf->veb[v])
7262 continue;
7263
7264 if (pf->veb[v]->uplink_seid == pf->mac_seid ||
7265 pf->veb[v]->uplink_seid == 0) {
7266 ret = i40e_reconstitute_veb(pf->veb[v]);
7267
7268 if (!ret)
7269 continue;
7270
7271 /* If Main VEB failed, we're in deep doodoo,
7272 * so give up rebuilding the switch and set up
7273 * for minimal rebuild of PF VSI.
7274 * If orphan failed, we'll report the error
7275 * but try to keep going.
7276 */
7277 if (pf->veb[v]->uplink_seid == pf->mac_seid) {
7278 dev_info(&pf->pdev->dev,
7279 "rebuild of switch failed: %d, will try to set up simple PF connection\n",
7280 ret);
7281 pf->vsi[pf->lan_vsi]->uplink_seid
7282 = pf->mac_seid;
7283 break;
7284 } else if (pf->veb[v]->uplink_seid == 0) {
7285 dev_info(&pf->pdev->dev,
7286 "rebuild of orphan VEB failed: %d\n",
7287 ret);
7288 }
7289 }
7290 }
7291 }
7292
7293 if (pf->vsi[pf->lan_vsi]->uplink_seid == pf->mac_seid) {
Shannon Nelsoncde4cbc2014-06-04 01:23:17 +00007294 dev_dbg(&pf->pdev->dev, "attempting to rebuild PF VSI\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007295 /* no VEB, so rebuild only the Main VSI */
7296 ret = i40e_add_vsi(pf->vsi[pf->lan_vsi]);
7297 if (ret) {
7298 dev_info(&pf->pdev->dev,
7299 "rebuild of Main VSI failed: %d\n", ret);
Maciej Sosin373149f2017-04-05 07:50:55 -04007300 goto end_unlock;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007301 }
7302 }
7303
Anjali Singhai Jain4f2f017c2015-10-21 19:47:07 -04007304 /* Reconfigure hardware for allowing smaller MSS in the case
7305 * of TSO, so that we avoid the MDD being fired and causing
7306 * a reset in the case of small MSS+TSO.
7307 */
7308#define I40E_REG_MSS 0x000E64DC
7309#define I40E_REG_MSS_MIN_MASK 0x3FF0000
7310#define I40E_64BYTE_MSS 0x400000
7311 val = rd32(hw, I40E_REG_MSS);
7312 if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
7313 val &= ~I40E_REG_MSS_MIN_MASK;
7314 val |= I40E_64BYTE_MSS;
7315 wr32(hw, I40E_REG_MSS, val);
7316 }
7317
Jacob Kellerd36e41d2017-06-23 04:24:46 -04007318 if (pf->hw_features & I40E_HW_RESTART_AUTONEG) {
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +00007319 msleep(75);
7320 ret = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
7321 if (ret)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04007322 dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
7323 i40e_stat_str(&pf->hw, ret),
7324 i40e_aq_str(&pf->hw,
7325 pf->hw.aq.asq_last_status));
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00007326 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007327 /* reinit the misc interrupt */
7328 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
7329 ret = i40e_setup_misc_vector(pf);
7330
Anjali Singhai Jaine7358f52015-10-01 14:37:34 -04007331 /* Add a filter to drop all Flow control frames from any VSI from being
7332 * transmitted. By doing so we stop a malicious VF from sending out
7333 * PAUSE or PFC frames and potentially controlling traffic for other
7334 * PF/VF VSIs.
7335 * The FW can still send Flow control frames if enabled.
7336 */
7337 i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
7338 pf->main_vsi_seid);
7339
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007340 /* restart the VSIs that were rebuilt and running before the reset */
7341 i40e_pf_unquiesce_all_vsi(pf);
7342
Jacob Keller024b05f2017-04-13 04:45:46 -04007343 /* Release the RTNL lock before we start resetting VFs */
7344 if (!lock_acquired)
7345 rtnl_unlock();
7346
Jacob Kellere4b433f2017-04-13 04:45:52 -04007347 i40e_reset_all_vfs(pf, true);
Mitch Williams69f64b22014-02-13 03:48:46 -08007348
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007349 /* tell the firmware that we're starting */
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00007350 i40e_send_version(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007351
Jacob Keller024b05f2017-04-13 04:45:46 -04007352 /* We've already released the lock, so don't do it again */
7353 goto end_core_reset;
7354
Maciej Sosin373149f2017-04-05 07:50:55 -04007355end_unlock:
Jacob Keller024b05f2017-04-13 04:45:46 -04007356 if (!lock_acquired)
7357 rtnl_unlock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007358end_core_reset:
Jacob Keller0da36b92017-04-19 09:25:55 -04007359 clear_bit(__I40E_RESET_FAILED, pf->state);
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00007360clear_recovery:
Jacob Keller0da36b92017-04-19 09:25:55 -04007361 clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007362}
7363
7364/**
Maciej Sosin373149f2017-04-05 07:50:55 -04007365 * i40e_reset_and_rebuild - reset and rebuild using a saved config
7366 * @pf: board private structure
7367 * @reinit: if the Main VSI needs to re-initialized.
7368 * @lock_acquired: indicates whether or not the lock has been acquired
7369 * before this function was called.
7370 **/
7371static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit,
7372 bool lock_acquired)
7373{
7374 int ret;
7375 /* Now we wait for GRST to settle out.
7376 * We don't have to delete the VEBs or VSIs from the hw switch
7377 * because the reset will make them disappear.
7378 */
7379 ret = i40e_reset(pf);
7380 if (!ret)
7381 i40e_rebuild(pf, reinit, lock_acquired);
7382}
7383
7384/**
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00007385 * i40e_handle_reset_warning - prep for the PF to reset, reset and rebuild
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007386 * @pf: board private structure
7387 *
7388 * Close up the VFs and other things in prep for a Core Reset,
7389 * then get ready to rebuild the world.
Maciej Sosin373149f2017-04-05 07:50:55 -04007390 * @lock_acquired: indicates whether or not the lock has been acquired
7391 * before this function was called.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007392 **/
Maciej Sosin373149f2017-04-05 07:50:55 -04007393static void i40e_handle_reset_warning(struct i40e_pf *pf, bool lock_acquired)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007394{
Maciej Sosin373149f2017-04-05 07:50:55 -04007395 i40e_prep_for_reset(pf, lock_acquired);
7396 i40e_reset_and_rebuild(pf, false, lock_acquired);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007397}
7398
7399/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007400 * i40e_handle_mdd_event
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00007401 * @pf: pointer to the PF structure
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007402 *
7403 * Called from the MDD irq handler to identify possibly malicious vfs
7404 **/
7405static void i40e_handle_mdd_event(struct i40e_pf *pf)
7406{
7407 struct i40e_hw *hw = &pf->hw;
7408 bool mdd_detected = false;
Neerav Parikhdf430b12014-06-04 01:23:15 +00007409 bool pf_mdd_detected = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007410 struct i40e_vf *vf;
7411 u32 reg;
7412 int i;
7413
Jacob Keller0da36b92017-04-19 09:25:55 -04007414 if (!test_bit(__I40E_MDD_EVENT_PENDING, pf->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007415 return;
7416
7417 /* find what triggered the MDD event */
7418 reg = rd32(hw, I40E_GL_MDET_TX);
7419 if (reg & I40E_GL_MDET_TX_VALID_MASK) {
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00007420 u8 pf_num = (reg & I40E_GL_MDET_TX_PF_NUM_MASK) >>
7421 I40E_GL_MDET_TX_PF_NUM_SHIFT;
Mitch Williams2089ad02014-10-17 03:14:53 +00007422 u16 vf_num = (reg & I40E_GL_MDET_TX_VF_NUM_MASK) >>
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00007423 I40E_GL_MDET_TX_VF_NUM_SHIFT;
Dan Carpenter013f6572014-10-22 20:06:29 -07007424 u8 event = (reg & I40E_GL_MDET_TX_EVENT_MASK) >>
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00007425 I40E_GL_MDET_TX_EVENT_SHIFT;
Mitch Williams2089ad02014-10-17 03:14:53 +00007426 u16 queue = ((reg & I40E_GL_MDET_TX_QUEUE_MASK) >>
7427 I40E_GL_MDET_TX_QUEUE_SHIFT) -
7428 pf->hw.func_caps.base_queue;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007429 if (netif_msg_tx_err(pf))
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00007430 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 +00007431 event, queue, pf_num, vf_num);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007432 wr32(hw, I40E_GL_MDET_TX, 0xffffffff);
7433 mdd_detected = true;
7434 }
7435 reg = rd32(hw, I40E_GL_MDET_RX);
7436 if (reg & I40E_GL_MDET_RX_VALID_MASK) {
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00007437 u8 func = (reg & I40E_GL_MDET_RX_FUNCTION_MASK) >>
7438 I40E_GL_MDET_RX_FUNCTION_SHIFT;
Dan Carpenter013f6572014-10-22 20:06:29 -07007439 u8 event = (reg & I40E_GL_MDET_RX_EVENT_MASK) >>
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00007440 I40E_GL_MDET_RX_EVENT_SHIFT;
Mitch Williams2089ad02014-10-17 03:14:53 +00007441 u16 queue = ((reg & I40E_GL_MDET_RX_QUEUE_MASK) >>
7442 I40E_GL_MDET_RX_QUEUE_SHIFT) -
7443 pf->hw.func_caps.base_queue;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007444 if (netif_msg_rx_err(pf))
7445 dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on RX queue %d of function 0x%02x\n",
7446 event, queue, func);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007447 wr32(hw, I40E_GL_MDET_RX, 0xffffffff);
7448 mdd_detected = true;
7449 }
7450
Neerav Parikhdf430b12014-06-04 01:23:15 +00007451 if (mdd_detected) {
7452 reg = rd32(hw, I40E_PF_MDET_TX);
7453 if (reg & I40E_PF_MDET_TX_VALID_MASK) {
7454 wr32(hw, I40E_PF_MDET_TX, 0xFFFF);
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007455 dev_info(&pf->pdev->dev, "TX driver issue detected, PF reset issued\n");
Neerav Parikhdf430b12014-06-04 01:23:15 +00007456 pf_mdd_detected = true;
7457 }
7458 reg = rd32(hw, I40E_PF_MDET_RX);
7459 if (reg & I40E_PF_MDET_RX_VALID_MASK) {
7460 wr32(hw, I40E_PF_MDET_RX, 0xFFFF);
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007461 dev_info(&pf->pdev->dev, "RX driver issue detected, PF reset issued\n");
Neerav Parikhdf430b12014-06-04 01:23:15 +00007462 pf_mdd_detected = true;
7463 }
7464 /* Queue belongs to the PF, initiate a reset */
7465 if (pf_mdd_detected) {
Jacob Keller0da36b92017-04-19 09:25:55 -04007466 set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
Neerav Parikhdf430b12014-06-04 01:23:15 +00007467 i40e_service_event_schedule(pf);
7468 }
7469 }
7470
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007471 /* see if one of the VFs needs its hand slapped */
7472 for (i = 0; i < pf->num_alloc_vfs && mdd_detected; i++) {
7473 vf = &(pf->vf[i]);
7474 reg = rd32(hw, I40E_VP_MDET_TX(i));
7475 if (reg & I40E_VP_MDET_TX_VALID_MASK) {
7476 wr32(hw, I40E_VP_MDET_TX(i), 0xFFFF);
7477 vf->num_mdd_events++;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007478 dev_info(&pf->pdev->dev, "TX driver issue detected on VF %d\n",
7479 i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007480 }
7481
7482 reg = rd32(hw, I40E_VP_MDET_RX(i));
7483 if (reg & I40E_VP_MDET_RX_VALID_MASK) {
7484 wr32(hw, I40E_VP_MDET_RX(i), 0xFFFF);
7485 vf->num_mdd_events++;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007486 dev_info(&pf->pdev->dev, "RX driver issue detected on VF %d\n",
7487 i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007488 }
7489
7490 if (vf->num_mdd_events > I40E_DEFAULT_NUM_MDD_EVENTS_ALLOWED) {
7491 dev_info(&pf->pdev->dev,
7492 "Too many MDD events on VF %d, disabled\n", i);
7493 dev_info(&pf->pdev->dev,
7494 "Use PF Control I/F to re-enable the VF\n");
Jacob Keller6322e632017-04-13 04:45:54 -04007495 set_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007496 }
7497 }
7498
7499 /* re-enable mdd interrupt cause */
Jacob Keller0da36b92017-04-19 09:25:55 -04007500 clear_bit(__I40E_MDD_EVENT_PENDING, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007501 reg = rd32(hw, I40E_PFINT_ICR0_ENA);
7502 reg |= I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
7503 wr32(hw, I40E_PFINT_ICR0_ENA, reg);
7504 i40e_flush(hw);
7505}
7506
Jacob Kellerd8b2c702017-06-20 15:17:00 -07007507static const char *i40e_tunnel_name(struct i40e_udp_port_config *port)
7508{
7509 switch (port->type) {
7510 case UDP_TUNNEL_TYPE_VXLAN:
7511 return "vxlan";
7512 case UDP_TUNNEL_TYPE_GENEVE:
7513 return "geneve";
7514 default:
7515 return "unknown";
7516 }
7517}
7518
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007519/**
Alexander Duyck1f190d92017-04-19 09:25:51 -04007520 * i40e_sync_udp_filters - Trigger a sync event for existing UDP filters
7521 * @pf: board private structure
7522 **/
7523static void i40e_sync_udp_filters(struct i40e_pf *pf)
7524{
7525 int i;
7526
7527 /* loop through and set pending bit for all active UDP filters */
7528 for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
7529 if (pf->udp_ports[i].port)
7530 pf->pending_udp_bitmap |= BIT_ULL(i);
7531 }
7532
7533 pf->flags |= I40E_FLAG_UDP_FILTER_SYNC;
7534}
7535
7536/**
Singhai, Anjali6a899022015-12-14 12:21:18 -08007537 * i40e_sync_udp_filters_subtask - Sync the VSI filter list with HW
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007538 * @pf: board private structure
7539 **/
Singhai, Anjali6a899022015-12-14 12:21:18 -08007540static void i40e_sync_udp_filters_subtask(struct i40e_pf *pf)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007541{
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007542 struct i40e_hw *hw = &pf->hw;
7543 i40e_status ret;
Jacob Kellerfe0b0cd2017-02-06 14:38:38 -08007544 u16 port;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007545 int i;
7546
Singhai, Anjali6a899022015-12-14 12:21:18 -08007547 if (!(pf->flags & I40E_FLAG_UDP_FILTER_SYNC))
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007548 return;
7549
Singhai, Anjali6a899022015-12-14 12:21:18 -08007550 pf->flags &= ~I40E_FLAG_UDP_FILTER_SYNC;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007551
7552 for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
Singhai, Anjali6a899022015-12-14 12:21:18 -08007553 if (pf->pending_udp_bitmap & BIT_ULL(i)) {
7554 pf->pending_udp_bitmap &= ~BIT_ULL(i);
Jacob Keller27826fd2017-04-19 09:25:50 -04007555 port = pf->udp_ports[i].port;
Shannon Nelsonc22c06c2015-03-31 00:45:04 -07007556 if (port)
Carolyn Wybornyb3f5c7b2016-08-24 11:33:51 -07007557 ret = i40e_aq_add_udp_tunnel(hw, port,
7558 pf->udp_ports[i].type,
7559 NULL, NULL);
Shannon Nelsonc22c06c2015-03-31 00:45:04 -07007560 else
7561 ret = i40e_aq_del_udp_tunnel(hw, i, NULL);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007562
7563 if (ret) {
Jacob Kellerd8b2c702017-06-20 15:17:00 -07007564 dev_info(&pf->pdev->dev,
7565 "%s %s port %d, index %d failed, err %s aq_err %s\n",
7566 i40e_tunnel_name(&pf->udp_ports[i]),
7567 port ? "add" : "delete",
7568 port, i,
7569 i40e_stat_str(&pf->hw, ret),
7570 i40e_aq_str(&pf->hw,
7571 pf->hw.aq.asq_last_status));
Jacob Keller27826fd2017-04-19 09:25:50 -04007572 pf->udp_ports[i].port = 0;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007573 }
7574 }
7575 }
7576}
7577
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007578/**
7579 * i40e_service_task - Run the driver's async subtasks
7580 * @work: pointer to work_struct containing our data
7581 **/
7582static void i40e_service_task(struct work_struct *work)
7583{
7584 struct i40e_pf *pf = container_of(work,
7585 struct i40e_pf,
7586 service_task);
7587 unsigned long start_time = jiffies;
7588
Shannon Nelsone57a2fe2014-06-03 23:50:19 +00007589 /* don't bother with service tasks if a reset is in progress */
Jacob Keller0da36b92017-04-19 09:25:55 -04007590 if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
Shannon Nelsone57a2fe2014-06-03 23:50:19 +00007591 return;
Shannon Nelsone57a2fe2014-06-03 23:50:19 +00007592
Jacob Keller0da36b92017-04-19 09:25:55 -04007593 if (test_and_set_bit(__I40E_SERVICE_SCHED, pf->state))
Mitch Williams91089032016-11-21 13:03:51 -08007594 return;
7595
Kiran Patilb03a8c12015-09-24 18:13:15 -04007596 i40e_detect_recover_hung(pf);
Jesse Brandeburg2818ccd2016-01-13 16:51:38 -08007597 i40e_sync_filters_subtask(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007598 i40e_reset_subtask(pf);
7599 i40e_handle_mdd_event(pf);
7600 i40e_vc_process_vflr_event(pf);
7601 i40e_watchdog_subtask(pf);
7602 i40e_fdir_reinit_subtask(pf);
Mitch Williams0ef2d5a2017-01-24 10:24:00 -08007603 if (pf->flags & I40E_FLAG_CLIENT_RESET) {
7604 /* Client subtask will reopen next time through. */
7605 i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], true);
7606 pf->flags &= ~I40E_FLAG_CLIENT_RESET;
7607 } else {
7608 i40e_client_subtask(pf);
7609 if (pf->flags & I40E_FLAG_CLIENT_L2_CHANGE) {
7610 i40e_notify_client_of_l2_param_changes(
7611 pf->vsi[pf->lan_vsi]);
7612 pf->flags &= ~I40E_FLAG_CLIENT_L2_CHANGE;
7613 }
7614 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007615 i40e_sync_filters_subtask(pf);
Singhai, Anjali6a899022015-12-14 12:21:18 -08007616 i40e_sync_udp_filters_subtask(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007617 i40e_clean_adminq_subtask(pf);
7618
Mitch Williams91089032016-11-21 13:03:51 -08007619 /* flush memory to make sure state is correct before next watchdog */
7620 smp_mb__before_atomic();
Jacob Keller0da36b92017-04-19 09:25:55 -04007621 clear_bit(__I40E_SERVICE_SCHED, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007622
7623 /* If the tasks have taken longer than one timer cycle or there
7624 * is more work to be done, reschedule the service task now
7625 * rather than wait for the timer to tick again.
7626 */
7627 if (time_after(jiffies, (start_time + pf->service_timer_period)) ||
Jacob Keller0da36b92017-04-19 09:25:55 -04007628 test_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state) ||
7629 test_bit(__I40E_MDD_EVENT_PENDING, pf->state) ||
7630 test_bit(__I40E_VFLR_EVENT_PENDING, pf->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007631 i40e_service_event_schedule(pf);
7632}
7633
7634/**
7635 * i40e_service_timer - timer callback
7636 * @data: pointer to PF struct
7637 **/
7638static void i40e_service_timer(unsigned long data)
7639{
7640 struct i40e_pf *pf = (struct i40e_pf *)data;
7641
7642 mod_timer(&pf->service_timer,
7643 round_jiffies(jiffies + pf->service_timer_period));
7644 i40e_service_event_schedule(pf);
7645}
7646
7647/**
7648 * i40e_set_num_rings_in_vsi - Determine number of rings in the VSI
7649 * @vsi: the VSI being configured
7650 **/
7651static int i40e_set_num_rings_in_vsi(struct i40e_vsi *vsi)
7652{
7653 struct i40e_pf *pf = vsi->back;
7654
7655 switch (vsi->type) {
7656 case I40E_VSI_MAIN:
7657 vsi->alloc_queue_pairs = pf->num_lan_qps;
7658 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
7659 I40E_REQ_DESCRIPTOR_MULTIPLE);
7660 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
7661 vsi->num_q_vectors = pf->num_lan_msix;
7662 else
7663 vsi->num_q_vectors = 1;
7664
7665 break;
7666
7667 case I40E_VSI_FDIR:
7668 vsi->alloc_queue_pairs = 1;
7669 vsi->num_desc = ALIGN(I40E_FDIR_RING_COUNT,
7670 I40E_REQ_DESCRIPTOR_MULTIPLE);
Tushar Davea70e4072016-05-16 12:40:53 -07007671 vsi->num_q_vectors = pf->num_fdsb_msix;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007672 break;
7673
7674 case I40E_VSI_VMDQ2:
7675 vsi->alloc_queue_pairs = pf->num_vmdq_qps;
7676 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
7677 I40E_REQ_DESCRIPTOR_MULTIPLE);
7678 vsi->num_q_vectors = pf->num_vmdq_msix;
7679 break;
7680
7681 case I40E_VSI_SRIOV:
7682 vsi->alloc_queue_pairs = pf->num_vf_qps;
7683 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
7684 I40E_REQ_DESCRIPTOR_MULTIPLE);
7685 break;
7686
7687 default:
7688 WARN_ON(1);
7689 return -ENODATA;
7690 }
7691
7692 return 0;
7693}
7694
7695/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007696 * i40e_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the vsi
7697 * @type: VSI pointer
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007698 * @alloc_qvectors: a bool to specify if q_vectors need to be allocated.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007699 *
7700 * On error: returns error code (negative)
7701 * On success: returns 0
7702 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007703static int i40e_vsi_alloc_arrays(struct i40e_vsi *vsi, bool alloc_qvectors)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007704{
Björn Töpel74608d12017-05-24 07:55:35 +02007705 struct i40e_ring **next_rings;
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007706 int size;
7707 int ret = 0;
7708
Björn Töpel74608d12017-05-24 07:55:35 +02007709 /* allocate memory for both Tx, XDP Tx and Rx ring pointers */
7710 size = sizeof(struct i40e_ring *) * vsi->alloc_queue_pairs *
7711 (i40e_enabled_xdp_vsi(vsi) ? 3 : 2);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007712 vsi->tx_rings = kzalloc(size, GFP_KERNEL);
7713 if (!vsi->tx_rings)
7714 return -ENOMEM;
Björn Töpel74608d12017-05-24 07:55:35 +02007715 next_rings = vsi->tx_rings + vsi->alloc_queue_pairs;
7716 if (i40e_enabled_xdp_vsi(vsi)) {
7717 vsi->xdp_rings = next_rings;
7718 next_rings += vsi->alloc_queue_pairs;
7719 }
7720 vsi->rx_rings = next_rings;
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007721
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007722 if (alloc_qvectors) {
7723 /* allocate memory for q_vector pointers */
Julia Lawallf57e4fb2014-07-30 03:11:09 +00007724 size = sizeof(struct i40e_q_vector *) * vsi->num_q_vectors;
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007725 vsi->q_vectors = kzalloc(size, GFP_KERNEL);
7726 if (!vsi->q_vectors) {
7727 ret = -ENOMEM;
7728 goto err_vectors;
7729 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007730 }
7731 return ret;
7732
7733err_vectors:
7734 kfree(vsi->tx_rings);
7735 return ret;
7736}
7737
7738/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007739 * i40e_vsi_mem_alloc - Allocates the next available struct vsi in the PF
7740 * @pf: board private structure
7741 * @type: type of VSI
7742 *
7743 * On error: returns error code (negative)
7744 * On success: returns vsi index in PF (positive)
7745 **/
7746static int i40e_vsi_mem_alloc(struct i40e_pf *pf, enum i40e_vsi_type type)
7747{
7748 int ret = -ENODEV;
7749 struct i40e_vsi *vsi;
7750 int vsi_idx;
7751 int i;
7752
7753 /* Need to protect the allocation of the VSIs at the PF level */
7754 mutex_lock(&pf->switch_mutex);
7755
7756 /* VSI list may be fragmented if VSI creation/destruction has
7757 * been happening. We can afford to do a quick scan to look
7758 * for any free VSIs in the list.
7759 *
7760 * find next empty vsi slot, looping back around if necessary
7761 */
7762 i = pf->next_vsi;
Mitch Williams505682c2014-05-20 08:01:37 +00007763 while (i < pf->num_alloc_vsi && pf->vsi[i])
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007764 i++;
Mitch Williams505682c2014-05-20 08:01:37 +00007765 if (i >= pf->num_alloc_vsi) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007766 i = 0;
7767 while (i < pf->next_vsi && pf->vsi[i])
7768 i++;
7769 }
7770
Mitch Williams505682c2014-05-20 08:01:37 +00007771 if (i < pf->num_alloc_vsi && !pf->vsi[i]) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007772 vsi_idx = i; /* Found one! */
7773 } else {
7774 ret = -ENODEV;
Alexander Duyck493fb302013-09-28 07:01:44 +00007775 goto unlock_pf; /* out of VSI slots! */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007776 }
7777 pf->next_vsi = ++i;
7778
7779 vsi = kzalloc(sizeof(*vsi), GFP_KERNEL);
7780 if (!vsi) {
7781 ret = -ENOMEM;
Alexander Duyck493fb302013-09-28 07:01:44 +00007782 goto unlock_pf;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007783 }
7784 vsi->type = type;
7785 vsi->back = pf;
Jacob Keller0da36b92017-04-19 09:25:55 -04007786 set_bit(__I40E_VSI_DOWN, vsi->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007787 vsi->flags = 0;
7788 vsi->idx = vsi_idx;
Jesse Brandeburgac26fc12015-09-28 14:12:37 -04007789 vsi->int_rate_limit = 0;
Anjali Singhai Jain5db4cb52015-02-24 06:58:49 +00007790 vsi->rss_table_size = (vsi->type == I40E_VSI_MAIN) ?
7791 pf->rss_table_size : 64;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007792 vsi->netdev_registered = false;
7793 vsi->work_limit = I40E_DEFAULT_IRQ_WORK;
Jacob Keller278e7d02016-10-05 09:30:37 -07007794 hash_init(vsi->mac_filter_hash);
Shannon Nelson63741842014-04-23 04:50:16 +00007795 vsi->irqs_ready = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007796
Alexander Duyck9f65e152013-09-28 06:00:58 +00007797 ret = i40e_set_num_rings_in_vsi(vsi);
7798 if (ret)
7799 goto err_rings;
7800
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007801 ret = i40e_vsi_alloc_arrays(vsi, true);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007802 if (ret)
Alexander Duyck9f65e152013-09-28 06:00:58 +00007803 goto err_rings;
Alexander Duyck493fb302013-09-28 07:01:44 +00007804
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007805 /* Setup default MSIX irq handler for VSI */
7806 i40e_vsi_setup_irqhandler(vsi, i40e_msix_clean_rings);
7807
Kiran Patil21659032015-09-30 14:09:03 -04007808 /* Initialize VSI lock */
Jacob Keller278e7d02016-10-05 09:30:37 -07007809 spin_lock_init(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007810 pf->vsi[vsi_idx] = vsi;
7811 ret = vsi_idx;
Alexander Duyck493fb302013-09-28 07:01:44 +00007812 goto unlock_pf;
7813
Alexander Duyck9f65e152013-09-28 06:00:58 +00007814err_rings:
Alexander Duyck493fb302013-09-28 07:01:44 +00007815 pf->next_vsi = i - 1;
7816 kfree(vsi);
7817unlock_pf:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007818 mutex_unlock(&pf->switch_mutex);
7819 return ret;
7820}
7821
7822/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007823 * i40e_vsi_free_arrays - Free queue and vector pointer arrays for the VSI
7824 * @type: VSI pointer
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007825 * @free_qvectors: a bool to specify if q_vectors need to be freed.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007826 *
7827 * On error: returns error code (negative)
7828 * On success: returns 0
7829 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007830static void i40e_vsi_free_arrays(struct i40e_vsi *vsi, bool free_qvectors)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007831{
7832 /* free the ring and vector containers */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007833 if (free_qvectors) {
7834 kfree(vsi->q_vectors);
7835 vsi->q_vectors = NULL;
7836 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007837 kfree(vsi->tx_rings);
7838 vsi->tx_rings = NULL;
7839 vsi->rx_rings = NULL;
Björn Töpel74608d12017-05-24 07:55:35 +02007840 vsi->xdp_rings = NULL;
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007841}
7842
7843/**
Helin Zhang28c58692015-10-26 19:44:27 -04007844 * i40e_clear_rss_config_user - clear the user configured RSS hash keys
7845 * and lookup table
7846 * @vsi: Pointer to VSI structure
7847 */
7848static void i40e_clear_rss_config_user(struct i40e_vsi *vsi)
7849{
7850 if (!vsi)
7851 return;
7852
7853 kfree(vsi->rss_hkey_user);
7854 vsi->rss_hkey_user = NULL;
7855
7856 kfree(vsi->rss_lut_user);
7857 vsi->rss_lut_user = NULL;
7858}
7859
7860/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007861 * i40e_vsi_clear - Deallocate the VSI provided
7862 * @vsi: the VSI being un-configured
7863 **/
7864static int i40e_vsi_clear(struct i40e_vsi *vsi)
7865{
7866 struct i40e_pf *pf;
7867
7868 if (!vsi)
7869 return 0;
7870
7871 if (!vsi->back)
7872 goto free_vsi;
7873 pf = vsi->back;
7874
7875 mutex_lock(&pf->switch_mutex);
7876 if (!pf->vsi[vsi->idx]) {
7877 dev_err(&pf->pdev->dev, "pf->vsi[%d] is NULL, just free vsi[%d](%p,type %d)\n",
7878 vsi->idx, vsi->idx, vsi, vsi->type);
7879 goto unlock_vsi;
7880 }
7881
7882 if (pf->vsi[vsi->idx] != vsi) {
7883 dev_err(&pf->pdev->dev,
7884 "pf->vsi[%d](%p, type %d) != vsi[%d](%p,type %d): no free!\n",
7885 pf->vsi[vsi->idx]->idx,
7886 pf->vsi[vsi->idx],
7887 pf->vsi[vsi->idx]->type,
7888 vsi->idx, vsi, vsi->type);
7889 goto unlock_vsi;
7890 }
7891
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00007892 /* updates the PF for this cleared vsi */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007893 i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
7894 i40e_put_lump(pf->irq_pile, vsi->base_vector, vsi->idx);
7895
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007896 i40e_vsi_free_arrays(vsi, true);
Helin Zhang28c58692015-10-26 19:44:27 -04007897 i40e_clear_rss_config_user(vsi);
Alexander Duyck493fb302013-09-28 07:01:44 +00007898
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007899 pf->vsi[vsi->idx] = NULL;
7900 if (vsi->idx < pf->next_vsi)
7901 pf->next_vsi = vsi->idx;
7902
7903unlock_vsi:
7904 mutex_unlock(&pf->switch_mutex);
7905free_vsi:
7906 kfree(vsi);
7907
7908 return 0;
7909}
7910
7911/**
Alexander Duyck9f65e152013-09-28 06:00:58 +00007912 * i40e_vsi_clear_rings - Deallocates the Rx and Tx rings for the provided VSI
7913 * @vsi: the VSI being cleaned
7914 **/
Shannon Nelsonbe1d5ee2013-11-28 06:39:23 +00007915static void i40e_vsi_clear_rings(struct i40e_vsi *vsi)
Alexander Duyck9f65e152013-09-28 06:00:58 +00007916{
7917 int i;
7918
Greg Rose8e9dca52013-12-18 13:45:53 +00007919 if (vsi->tx_rings && vsi->tx_rings[0]) {
Neerav Parikhd7397642013-11-28 06:39:37 +00007920 for (i = 0; i < vsi->alloc_queue_pairs; i++) {
Mitch Williams00403f02013-09-28 07:13:13 +00007921 kfree_rcu(vsi->tx_rings[i], rcu);
7922 vsi->tx_rings[i] = NULL;
7923 vsi->rx_rings[i] = NULL;
Björn Töpel74608d12017-05-24 07:55:35 +02007924 if (vsi->xdp_rings)
7925 vsi->xdp_rings[i] = NULL;
Mitch Williams00403f02013-09-28 07:13:13 +00007926 }
Shannon Nelsonbe1d5ee2013-11-28 06:39:23 +00007927 }
Alexander Duyck9f65e152013-09-28 06:00:58 +00007928}
7929
7930/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007931 * i40e_alloc_rings - Allocates the Rx and Tx rings for the provided VSI
7932 * @vsi: the VSI being configured
7933 **/
7934static int i40e_alloc_rings(struct i40e_vsi *vsi)
7935{
Björn Töpel74608d12017-05-24 07:55:35 +02007936 int i, qpv = i40e_enabled_xdp_vsi(vsi) ? 3 : 2;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007937 struct i40e_pf *pf = vsi->back;
Björn Töpel74608d12017-05-24 07:55:35 +02007938 struct i40e_ring *ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007939
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007940 /* Set basic values in the rings to be used later during open() */
Neerav Parikhd7397642013-11-28 06:39:37 +00007941 for (i = 0; i < vsi->alloc_queue_pairs; i++) {
Shannon Nelsonac6c5e32013-11-20 10:02:57 +00007942 /* allocate space for both Tx and Rx in one shot */
Björn Töpel74608d12017-05-24 07:55:35 +02007943 ring = kcalloc(qpv, sizeof(struct i40e_ring), GFP_KERNEL);
7944 if (!ring)
Alexander Duyck9f65e152013-09-28 06:00:58 +00007945 goto err_out;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007946
Björn Töpel74608d12017-05-24 07:55:35 +02007947 ring->queue_index = i;
7948 ring->reg_idx = vsi->base_queue + i;
7949 ring->ring_active = false;
7950 ring->vsi = vsi;
7951 ring->netdev = vsi->netdev;
7952 ring->dev = &pf->pdev->dev;
7953 ring->count = vsi->num_desc;
7954 ring->size = 0;
7955 ring->dcb_tc = 0;
Jacob Kellerd36e41d2017-06-23 04:24:46 -04007956 if (vsi->back->hw_features & I40E_HW_WB_ON_ITR_CAPABLE)
Björn Töpel74608d12017-05-24 07:55:35 +02007957 ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
7958 ring->tx_itr_setting = pf->tx_itr_default;
7959 vsi->tx_rings[i] = ring++;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007960
Björn Töpel74608d12017-05-24 07:55:35 +02007961 if (!i40e_enabled_xdp_vsi(vsi))
7962 goto setup_rx;
7963
7964 ring->queue_index = vsi->alloc_queue_pairs + i;
7965 ring->reg_idx = vsi->base_queue + ring->queue_index;
7966 ring->ring_active = false;
7967 ring->vsi = vsi;
7968 ring->netdev = NULL;
7969 ring->dev = &pf->pdev->dev;
7970 ring->count = vsi->num_desc;
7971 ring->size = 0;
7972 ring->dcb_tc = 0;
Jacob Kellerd36e41d2017-06-23 04:24:46 -04007973 if (vsi->back->hw_features & I40E_HW_WB_ON_ITR_CAPABLE)
Björn Töpel74608d12017-05-24 07:55:35 +02007974 ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
7975 set_ring_xdp(ring);
7976 ring->tx_itr_setting = pf->tx_itr_default;
7977 vsi->xdp_rings[i] = ring++;
7978
7979setup_rx:
7980 ring->queue_index = i;
7981 ring->reg_idx = vsi->base_queue + i;
7982 ring->ring_active = false;
7983 ring->vsi = vsi;
7984 ring->netdev = vsi->netdev;
7985 ring->dev = &pf->pdev->dev;
7986 ring->count = vsi->num_desc;
7987 ring->size = 0;
7988 ring->dcb_tc = 0;
7989 ring->rx_itr_setting = pf->rx_itr_default;
7990 vsi->rx_rings[i] = ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007991 }
7992
7993 return 0;
Alexander Duyck9f65e152013-09-28 06:00:58 +00007994
7995err_out:
7996 i40e_vsi_clear_rings(vsi);
7997 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007998}
7999
8000/**
8001 * i40e_reserve_msix_vectors - Reserve MSI-X vectors in the kernel
8002 * @pf: board private structure
8003 * @vectors: the number of MSI-X vectors to request
8004 *
8005 * Returns the number of vectors reserved, or error
8006 **/
8007static int i40e_reserve_msix_vectors(struct i40e_pf *pf, int vectors)
8008{
Alexander Gordeev7b37f372014-02-18 11:11:42 +01008009 vectors = pci_enable_msix_range(pf->pdev, pf->msix_entries,
8010 I40E_MIN_MSIX, vectors);
8011 if (vectors < 0) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008012 dev_info(&pf->pdev->dev,
Alexander Gordeev7b37f372014-02-18 11:11:42 +01008013 "MSI-X vector reservation failed: %d\n", vectors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008014 vectors = 0;
8015 }
8016
8017 return vectors;
8018}
8019
8020/**
8021 * i40e_init_msix - Setup the MSIX capability
8022 * @pf: board private structure
8023 *
8024 * Work with the OS to set up the MSIX vectors needed.
8025 *
Shannon Nelson3b444392015-02-26 16:15:57 +00008026 * Returns the number of vectors reserved or negative on failure
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008027 **/
8028static int i40e_init_msix(struct i40e_pf *pf)
8029{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008030 struct i40e_hw *hw = &pf->hw;
Jacob Kellerc0cf70a2017-01-24 10:24:01 -08008031 int cpus, extra_vectors;
Shannon Nelson1e200e42015-02-27 09:15:24 +00008032 int vectors_left;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008033 int v_budget, i;
Shannon Nelson3b444392015-02-26 16:15:57 +00008034 int v_actual;
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06008035 int iwarp_requested = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008036
8037 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
8038 return -ENODEV;
8039
8040 /* The number of vectors we'll request will be comprised of:
8041 * - Add 1 for "other" cause for Admin Queue events, etc.
8042 * - The number of LAN queue pairs
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008043 * - Queues being used for RSS.
8044 * We don't need as many as max_rss_size vectors.
8045 * use rss_size instead in the calculation since that
8046 * is governed by number of cpus in the system.
8047 * - assumes symmetric Tx/Rx pairing
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008048 * - The number of VMDq pairs
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06008049 * - The CPU count within the NUMA node if iWARP is enabled
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008050 * Once we count this up, try the request.
8051 *
8052 * If we can't get what we want, we'll simplify to nearly nothing
8053 * and try again. If that still fails, we punt.
8054 */
Shannon Nelson1e200e42015-02-27 09:15:24 +00008055 vectors_left = hw->func_caps.num_msix_vectors;
8056 v_budget = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008057
Shannon Nelson1e200e42015-02-27 09:15:24 +00008058 /* reserve one vector for miscellaneous handler */
8059 if (vectors_left) {
8060 v_budget++;
8061 vectors_left--;
8062 }
8063
Jacob Kellerc0cf70a2017-01-24 10:24:01 -08008064 /* reserve some vectors for the main PF traffic queues. Initially we
8065 * only reserve at most 50% of the available vectors, in the case that
8066 * the number of online CPUs is large. This ensures that we can enable
8067 * extra features as well. Once we've enabled the other features, we
8068 * will use any remaining vectors to reach as close as we can to the
8069 * number of online CPUs.
8070 */
8071 cpus = num_online_cpus();
8072 pf->num_lan_msix = min_t(int, cpus, vectors_left / 2);
Shannon Nelson1e200e42015-02-27 09:15:24 +00008073 vectors_left -= pf->num_lan_msix;
Shannon Nelson1e200e42015-02-27 09:15:24 +00008074
8075 /* reserve one vector for sideband flow director */
8076 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
8077 if (vectors_left) {
Tushar Davea70e4072016-05-16 12:40:53 -07008078 pf->num_fdsb_msix = 1;
Shannon Nelson1e200e42015-02-27 09:15:24 +00008079 v_budget++;
8080 vectors_left--;
8081 } else {
Tushar Davea70e4072016-05-16 12:40:53 -07008082 pf->num_fdsb_msix = 0;
Shannon Nelson1e200e42015-02-27 09:15:24 +00008083 }
8084 }
John W Linville83840e42015-01-14 03:06:28 +00008085
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06008086 /* can we reserve enough for iWARP? */
8087 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02008088 iwarp_requested = pf->num_iwarp_msix;
8089
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06008090 if (!vectors_left)
8091 pf->num_iwarp_msix = 0;
8092 else if (vectors_left < pf->num_iwarp_msix)
8093 pf->num_iwarp_msix = 1;
8094 v_budget += pf->num_iwarp_msix;
8095 vectors_left -= pf->num_iwarp_msix;
8096 }
8097
Shannon Nelson1e200e42015-02-27 09:15:24 +00008098 /* any vectors left over go for VMDq support */
8099 if (pf->flags & I40E_FLAG_VMDQ_ENABLED) {
8100 int vmdq_vecs_wanted = pf->num_vmdq_vsis * pf->num_vmdq_qps;
8101 int vmdq_vecs = min_t(int, vectors_left, vmdq_vecs_wanted);
8102
Stefan Assmann9ca57e92016-09-19 13:37:49 +02008103 if (!vectors_left) {
8104 pf->num_vmdq_msix = 0;
8105 pf->num_vmdq_qps = 0;
8106 } else {
8107 /* if we're short on vectors for what's desired, we limit
8108 * the queues per vmdq. If this is still more than are
8109 * available, the user will need to change the number of
8110 * queues/vectors used by the PF later with the ethtool
8111 * channels command
8112 */
8113 if (vmdq_vecs < vmdq_vecs_wanted)
8114 pf->num_vmdq_qps = 1;
8115 pf->num_vmdq_msix = pf->num_vmdq_qps;
Shannon Nelson1e200e42015-02-27 09:15:24 +00008116
Stefan Assmann9ca57e92016-09-19 13:37:49 +02008117 v_budget += vmdq_vecs;
8118 vectors_left -= vmdq_vecs;
8119 }
Shannon Nelson1e200e42015-02-27 09:15:24 +00008120 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008121
Jacob Kellerc0cf70a2017-01-24 10:24:01 -08008122 /* On systems with a large number of SMP cores, we previously limited
8123 * the number of vectors for num_lan_msix to be at most 50% of the
8124 * available vectors, to allow for other features. Now, we add back
8125 * the remaining vectors. However, we ensure that the total
8126 * num_lan_msix will not exceed num_online_cpus(). To do this, we
8127 * calculate the number of vectors we can add without going over the
8128 * cap of CPUs. For systems with a small number of CPUs this will be
8129 * zero.
8130 */
8131 extra_vectors = min_t(int, cpus - pf->num_lan_msix, vectors_left);
8132 pf->num_lan_msix += extra_vectors;
8133 vectors_left -= extra_vectors;
8134
8135 WARN(vectors_left < 0,
8136 "Calculation of remaining vectors underflowed. This is an accounting bug when determining total MSI-X vectors.\n");
8137
8138 v_budget += pf->num_lan_msix;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008139 pf->msix_entries = kcalloc(v_budget, sizeof(struct msix_entry),
8140 GFP_KERNEL);
8141 if (!pf->msix_entries)
8142 return -ENOMEM;
8143
8144 for (i = 0; i < v_budget; i++)
8145 pf->msix_entries[i].entry = i;
Shannon Nelson3b444392015-02-26 16:15:57 +00008146 v_actual = i40e_reserve_msix_vectors(pf, v_budget);
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00008147
Shannon Nelson3b444392015-02-26 16:15:57 +00008148 if (v_actual < I40E_MIN_MSIX) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008149 pf->flags &= ~I40E_FLAG_MSIX_ENABLED;
8150 kfree(pf->msix_entries);
8151 pf->msix_entries = NULL;
Guilherme G Piccoli4c95aa52016-09-22 10:03:58 -03008152 pci_disable_msix(pf->pdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008153 return -ENODEV;
8154
Shannon Nelson3b444392015-02-26 16:15:57 +00008155 } else if (v_actual == I40E_MIN_MSIX) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008156 /* Adjust for minimal MSIX use */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008157 pf->num_vmdq_vsis = 0;
8158 pf->num_vmdq_qps = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008159 pf->num_lan_qps = 1;
8160 pf->num_lan_msix = 1;
8161
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02008162 } else if (!vectors_left) {
8163 /* If we have limited resources, we will start with no vectors
8164 * for the special features and then allocate vectors to some
8165 * of these features based on the policy and at the end disable
8166 * the features that did not get any vectors.
8167 */
Shannon Nelson3b444392015-02-26 16:15:57 +00008168 int vec;
8169
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02008170 dev_info(&pf->pdev->dev,
8171 "MSI-X vector limit reached, attempting to redistribute vectors\n");
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00008172 /* reserve the misc vector */
Shannon Nelson3b444392015-02-26 16:15:57 +00008173 vec = v_actual - 1;
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00008174
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008175 /* Scale vector usage down */
8176 pf->num_vmdq_msix = 1; /* force VMDqs to only one vector */
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00008177 pf->num_vmdq_vsis = 1;
Shannon Nelson1e200e42015-02-27 09:15:24 +00008178 pf->num_vmdq_qps = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008179
8180 /* partition out the remaining vectors */
8181 switch (vec) {
8182 case 2:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008183 pf->num_lan_msix = 1;
8184 break;
8185 case 3:
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06008186 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
8187 pf->num_lan_msix = 1;
8188 pf->num_iwarp_msix = 1;
8189 } else {
8190 pf->num_lan_msix = 2;
8191 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008192 break;
8193 default:
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06008194 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
8195 pf->num_iwarp_msix = min_t(int, (vec / 3),
8196 iwarp_requested);
8197 pf->num_vmdq_vsis = min_t(int, (vec / 3),
8198 I40E_DEFAULT_NUM_VMDQ_VSI);
8199 } else {
8200 pf->num_vmdq_vsis = min_t(int, (vec / 2),
8201 I40E_DEFAULT_NUM_VMDQ_VSI);
8202 }
Stefan Assmannabd97a92016-09-19 13:37:51 +02008203 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
8204 pf->num_fdsb_msix = 1;
8205 vec--;
8206 }
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06008207 pf->num_lan_msix = min_t(int,
8208 (vec - (pf->num_iwarp_msix + pf->num_vmdq_vsis)),
8209 pf->num_lan_msix);
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02008210 pf->num_lan_qps = pf->num_lan_msix;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008211 break;
8212 }
8213 }
8214
Stefan Assmannabd97a92016-09-19 13:37:51 +02008215 if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
8216 (pf->num_fdsb_msix == 0)) {
8217 dev_info(&pf->pdev->dev, "Sideband Flowdir disabled, not enough MSI-X vectors\n");
8218 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
8219 }
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00008220 if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
8221 (pf->num_vmdq_msix == 0)) {
8222 dev_info(&pf->pdev->dev, "VMDq disabled, not enough MSI-X vectors\n");
8223 pf->flags &= ~I40E_FLAG_VMDQ_ENABLED;
8224 }
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06008225
8226 if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
8227 (pf->num_iwarp_msix == 0)) {
8228 dev_info(&pf->pdev->dev, "IWARP disabled, not enough MSI-X vectors\n");
8229 pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
8230 }
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02008231 i40e_debug(&pf->hw, I40E_DEBUG_INIT,
8232 "MSI-X vector distribution: PF %d, VMDq %d, FDSB %d, iWARP %d\n",
8233 pf->num_lan_msix,
8234 pf->num_vmdq_msix * pf->num_vmdq_vsis,
8235 pf->num_fdsb_msix,
8236 pf->num_iwarp_msix);
8237
Shannon Nelson3b444392015-02-26 16:15:57 +00008238 return v_actual;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008239}
8240
8241/**
Greg Rose90e04072014-03-06 08:59:57 +00008242 * i40e_vsi_alloc_q_vector - Allocate memory for a single interrupt vector
Alexander Duyck493fb302013-09-28 07:01:44 +00008243 * @vsi: the VSI being configured
8244 * @v_idx: index of the vector in the vsi struct
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03008245 * @cpu: cpu to be used on affinity_mask
Alexander Duyck493fb302013-09-28 07:01:44 +00008246 *
8247 * We allocate one q_vector. If allocation fails we return -ENOMEM.
8248 **/
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03008249static int i40e_vsi_alloc_q_vector(struct i40e_vsi *vsi, int v_idx, int cpu)
Alexander Duyck493fb302013-09-28 07:01:44 +00008250{
8251 struct i40e_q_vector *q_vector;
8252
8253 /* allocate q_vector */
8254 q_vector = kzalloc(sizeof(struct i40e_q_vector), GFP_KERNEL);
8255 if (!q_vector)
8256 return -ENOMEM;
8257
8258 q_vector->vsi = vsi;
8259 q_vector->v_idx = v_idx;
Jacob Keller759dc4a2017-07-14 09:10:10 -04008260 cpumask_copy(&q_vector->affinity_mask, cpu_possible_mask);
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03008261
Alexander Duyck493fb302013-09-28 07:01:44 +00008262 if (vsi->netdev)
8263 netif_napi_add(vsi->netdev, &q_vector->napi,
Jesse Brandeburgeefeace2014-05-10 04:49:13 +00008264 i40e_napi_poll, NAPI_POLL_WEIGHT);
Alexander Duyck493fb302013-09-28 07:01:44 +00008265
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00008266 q_vector->rx.latency_range = I40E_LOW_LATENCY;
8267 q_vector->tx.latency_range = I40E_LOW_LATENCY;
8268
Alexander Duyck493fb302013-09-28 07:01:44 +00008269 /* tie q_vector and vsi together */
8270 vsi->q_vectors[v_idx] = q_vector;
8271
8272 return 0;
8273}
8274
8275/**
Greg Rose90e04072014-03-06 08:59:57 +00008276 * i40e_vsi_alloc_q_vectors - Allocate memory for interrupt vectors
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008277 * @vsi: the VSI being configured
8278 *
8279 * We allocate one q_vector per queue interrupt. If allocation fails we
8280 * return -ENOMEM.
8281 **/
Greg Rose90e04072014-03-06 08:59:57 +00008282static int i40e_vsi_alloc_q_vectors(struct i40e_vsi *vsi)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008283{
8284 struct i40e_pf *pf = vsi->back;
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03008285 int err, v_idx, num_q_vectors, current_cpu;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008286
8287 /* if not MSIX, give the one vector only to the LAN VSI */
8288 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
8289 num_q_vectors = vsi->num_q_vectors;
8290 else if (vsi == pf->vsi[pf->lan_vsi])
8291 num_q_vectors = 1;
8292 else
8293 return -EINVAL;
8294
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03008295 current_cpu = cpumask_first(cpu_online_mask);
8296
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008297 for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03008298 err = i40e_vsi_alloc_q_vector(vsi, v_idx, current_cpu);
Alexander Duyck493fb302013-09-28 07:01:44 +00008299 if (err)
8300 goto err_out;
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03008301 current_cpu = cpumask_next(current_cpu, cpu_online_mask);
8302 if (unlikely(current_cpu >= nr_cpu_ids))
8303 current_cpu = cpumask_first(cpu_online_mask);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008304 }
8305
8306 return 0;
Alexander Duyck493fb302013-09-28 07:01:44 +00008307
8308err_out:
8309 while (v_idx--)
8310 i40e_free_q_vector(vsi, v_idx);
8311
8312 return err;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008313}
8314
8315/**
8316 * i40e_init_interrupt_scheme - Determine proper interrupt scheme
8317 * @pf: board private structure to initialize
8318 **/
Jesse Brandeburgc11472802015-04-07 19:45:39 -04008319static int i40e_init_interrupt_scheme(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008320{
Shannon Nelson3b444392015-02-26 16:15:57 +00008321 int vectors = 0;
8322 ssize_t size;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008323
8324 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
Shannon Nelson3b444392015-02-26 16:15:57 +00008325 vectors = i40e_init_msix(pf);
8326 if (vectors < 0) {
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08008327 pf->flags &= ~(I40E_FLAG_MSIX_ENABLED |
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06008328 I40E_FLAG_IWARP_ENABLED |
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08008329 I40E_FLAG_RSS_ENABLED |
Neerav Parikh4d9b6042014-05-22 06:31:51 +00008330 I40E_FLAG_DCB_CAPABLE |
Dave Ertmana0362442016-08-29 17:38:26 -07008331 I40E_FLAG_DCB_ENABLED |
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08008332 I40E_FLAG_SRIOV_ENABLED |
8333 I40E_FLAG_FD_SB_ENABLED |
8334 I40E_FLAG_FD_ATR_ENABLED |
8335 I40E_FLAG_VMDQ_ENABLED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008336
8337 /* rework the queue expectations without MSIX */
8338 i40e_determine_queue_usage(pf);
8339 }
8340 }
8341
8342 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED) &&
8343 (pf->flags & I40E_FLAG_MSI_ENABLED)) {
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08008344 dev_info(&pf->pdev->dev, "MSI-X not available, trying MSI\n");
Shannon Nelson3b444392015-02-26 16:15:57 +00008345 vectors = pci_enable_msi(pf->pdev);
8346 if (vectors < 0) {
8347 dev_info(&pf->pdev->dev, "MSI init failed - %d\n",
8348 vectors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008349 pf->flags &= ~I40E_FLAG_MSI_ENABLED;
8350 }
Shannon Nelson3b444392015-02-26 16:15:57 +00008351 vectors = 1; /* one MSI or Legacy vector */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008352 }
8353
Shannon Nelson958a3e32013-09-28 07:13:28 +00008354 if (!(pf->flags & (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED)))
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08008355 dev_info(&pf->pdev->dev, "MSI-X and MSI not available, falling back to Legacy IRQ\n");
Shannon Nelson958a3e32013-09-28 07:13:28 +00008356
Shannon Nelson3b444392015-02-26 16:15:57 +00008357 /* set up vector assignment tracking */
8358 size = sizeof(struct i40e_lump_tracking) + (sizeof(u16) * vectors);
8359 pf->irq_pile = kzalloc(size, GFP_KERNEL);
Jesse Brandeburgc11472802015-04-07 19:45:39 -04008360 if (!pf->irq_pile) {
8361 dev_err(&pf->pdev->dev, "error allocating irq_pile memory\n");
8362 return -ENOMEM;
8363 }
Shannon Nelson3b444392015-02-26 16:15:57 +00008364 pf->irq_pile->num_entries = vectors;
8365 pf->irq_pile->search_hint = 0;
8366
Jesse Brandeburgc11472802015-04-07 19:45:39 -04008367 /* track first vector for misc interrupts, ignore return */
Shannon Nelson3b444392015-02-26 16:15:57 +00008368 (void)i40e_get_lump(pf, pf->irq_pile, 1, I40E_PILE_VALID_BIT - 1);
Jesse Brandeburgc11472802015-04-07 19:45:39 -04008369
8370 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008371}
8372
Jacob Kellerb980c062017-07-14 09:27:06 -04008373#ifdef CONFIG_PM
8374/**
8375 * i40e_restore_interrupt_scheme - Restore the interrupt scheme
8376 * @pf: private board data structure
8377 *
8378 * Restore the interrupt scheme that was cleared when we suspended the
8379 * device. This should be called during resume to re-allocate the q_vectors
8380 * and reacquire IRQs.
8381 */
8382static int i40e_restore_interrupt_scheme(struct i40e_pf *pf)
8383{
8384 int err, i;
8385
8386 /* We cleared the MSI and MSI-X flags when disabling the old interrupt
8387 * scheme. We need to re-enabled them here in order to attempt to
8388 * re-acquire the MSI or MSI-X vectors
8389 */
8390 pf->flags |= (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
8391
8392 err = i40e_init_interrupt_scheme(pf);
8393 if (err)
8394 return err;
8395
8396 /* Now that we've re-acquired IRQs, we need to remap the vectors and
8397 * rings together again.
8398 */
8399 for (i = 0; i < pf->num_alloc_vsi; i++) {
8400 if (pf->vsi[i]) {
8401 err = i40e_vsi_alloc_q_vectors(pf->vsi[i]);
8402 if (err)
8403 goto err_unwind;
8404 i40e_vsi_map_rings_to_vectors(pf->vsi[i]);
8405 }
8406 }
8407
8408 err = i40e_setup_misc_vector(pf);
8409 if (err)
8410 goto err_unwind;
8411
8412 return 0;
8413
8414err_unwind:
8415 while (i--) {
8416 if (pf->vsi[i])
8417 i40e_vsi_free_q_vectors(pf->vsi[i]);
8418 }
8419
8420 return err;
8421}
8422#endif /* CONFIG_PM */
8423
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008424/**
8425 * i40e_setup_misc_vector - Setup the misc vector to handle non queue events
8426 * @pf: board private structure
8427 *
8428 * This sets up the handler for MSIX 0, which is used to manage the
8429 * non-queue interrupts, e.g. AdminQ and errors. This is not used
8430 * when in MSI or Legacy interrupt mode.
8431 **/
8432static int i40e_setup_misc_vector(struct i40e_pf *pf)
8433{
8434 struct i40e_hw *hw = &pf->hw;
8435 int err = 0;
8436
Jacob Kellerc17401a2017-07-14 09:27:02 -04008437 /* Only request the IRQ once, the first time through. */
8438 if (!test_and_set_bit(__I40E_MISC_IRQ_REQUESTED, pf->state)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008439 err = request_irq(pf->msix_entries[0].vector,
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00008440 i40e_intr, 0, pf->int_name, pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008441 if (err) {
Jacob Kellerc17401a2017-07-14 09:27:02 -04008442 clear_bit(__I40E_MISC_IRQ_REQUESTED, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008443 dev_info(&pf->pdev->dev,
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08008444 "request_irq for %s failed: %d\n",
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00008445 pf->int_name, err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008446 return -EFAULT;
8447 }
8448 }
8449
Jacob Kellerab437b52014-12-14 01:55:08 +00008450 i40e_enable_misc_int_causes(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008451
8452 /* associate no queues to the misc vector */
8453 wr32(hw, I40E_PFINT_LNKLST0, I40E_QUEUE_END_OF_LIST);
8454 wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), I40E_ITR_8K);
8455
8456 i40e_flush(hw);
8457
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08008458 i40e_irq_dynamic_enable_icr0(pf, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008459
8460 return err;
8461}
8462
8463/**
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008464 * i40e_config_rss_aq - Prepare for RSS using AQ commands
8465 * @vsi: vsi structure
8466 * @seed: RSS hash seed
8467 **/
Helin Zhange69ff812015-10-21 19:56:22 -04008468static int i40e_config_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
8469 u8 *lut, u16 lut_size)
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008470{
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008471 struct i40e_pf *pf = vsi->back;
8472 struct i40e_hw *hw = &pf->hw;
Jacob Keller776b2e12016-07-19 16:23:30 -07008473 int ret = 0;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008474
Jacob Keller776b2e12016-07-19 16:23:30 -07008475 if (seed) {
8476 struct i40e_aqc_get_set_rss_key_data *seed_dw =
8477 (struct i40e_aqc_get_set_rss_key_data *)seed;
8478 ret = i40e_aq_set_rss_key(hw, vsi->id, seed_dw);
8479 if (ret) {
8480 dev_info(&pf->pdev->dev,
8481 "Cannot set RSS key, err %s aq_err %s\n",
8482 i40e_stat_str(hw, ret),
8483 i40e_aq_str(hw, hw->aq.asq_last_status));
8484 return ret;
8485 }
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008486 }
Jacob Keller776b2e12016-07-19 16:23:30 -07008487 if (lut) {
8488 bool pf_lut = vsi->type == I40E_VSI_MAIN ? true : false;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008489
Jacob Keller776b2e12016-07-19 16:23:30 -07008490 ret = i40e_aq_set_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
8491 if (ret) {
8492 dev_info(&pf->pdev->dev,
8493 "Cannot set RSS lut, err %s aq_err %s\n",
8494 i40e_stat_str(hw, ret),
8495 i40e_aq_str(hw, hw->aq.asq_last_status));
8496 return ret;
8497 }
8498 }
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008499 return ret;
8500}
8501
8502/**
Anjali Singhai Jain95a73782015-12-22 14:25:04 -08008503 * i40e_get_rss_aq - Get RSS keys and lut by using AQ commands
8504 * @vsi: Pointer to vsi structure
8505 * @seed: Buffter to store the hash keys
8506 * @lut: Buffer to store the lookup table entries
8507 * @lut_size: Size of buffer to store the lookup table entries
8508 *
8509 * Return 0 on success, negative on failure
8510 */
8511static int i40e_get_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
8512 u8 *lut, u16 lut_size)
8513{
8514 struct i40e_pf *pf = vsi->back;
8515 struct i40e_hw *hw = &pf->hw;
8516 int ret = 0;
8517
8518 if (seed) {
8519 ret = i40e_aq_get_rss_key(hw, vsi->id,
8520 (struct i40e_aqc_get_set_rss_key_data *)seed);
8521 if (ret) {
8522 dev_info(&pf->pdev->dev,
8523 "Cannot get RSS key, err %s aq_err %s\n",
8524 i40e_stat_str(&pf->hw, ret),
8525 i40e_aq_str(&pf->hw,
8526 pf->hw.aq.asq_last_status));
8527 return ret;
8528 }
8529 }
8530
8531 if (lut) {
8532 bool pf_lut = vsi->type == I40E_VSI_MAIN ? true : false;
8533
8534 ret = i40e_aq_get_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
8535 if (ret) {
8536 dev_info(&pf->pdev->dev,
8537 "Cannot get RSS lut, err %s aq_err %s\n",
8538 i40e_stat_str(&pf->hw, ret),
8539 i40e_aq_str(&pf->hw,
8540 pf->hw.aq.asq_last_status));
8541 return ret;
8542 }
8543 }
8544
8545 return ret;
8546}
8547
8548/**
Jacob Keller0582b962016-07-19 16:23:29 -07008549 * i40e_vsi_config_rss - Prepare for VSI(VMDq) RSS if used
8550 * @vsi: VSI structure
8551 **/
8552static int i40e_vsi_config_rss(struct i40e_vsi *vsi)
8553{
8554 u8 seed[I40E_HKEY_ARRAY_SIZE];
8555 struct i40e_pf *pf = vsi->back;
8556 u8 *lut;
8557 int ret;
8558
Jacob Kellerd36e41d2017-06-23 04:24:46 -04008559 if (!(pf->hw_features & I40E_HW_RSS_AQ_CAPABLE))
Jacob Keller0582b962016-07-19 16:23:29 -07008560 return 0;
8561
Jacob Keller552b9962016-07-19 16:23:31 -07008562 if (!vsi->rss_size)
8563 vsi->rss_size = min_t(int, pf->alloc_rss_size,
8564 vsi->num_queue_pairs);
8565 if (!vsi->rss_size)
8566 return -EINVAL;
8567
Jacob Keller0582b962016-07-19 16:23:29 -07008568 lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
8569 if (!lut)
8570 return -ENOMEM;
Jacob Keller552b9962016-07-19 16:23:31 -07008571 /* Use the user configured hash keys and lookup table if there is one,
8572 * otherwise use default
8573 */
8574 if (vsi->rss_lut_user)
8575 memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
8576 else
8577 i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
8578 if (vsi->rss_hkey_user)
8579 memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
8580 else
8581 netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
Jacob Keller0582b962016-07-19 16:23:29 -07008582 ret = i40e_config_rss_aq(vsi, seed, lut, vsi->rss_table_size);
8583 kfree(lut);
8584
8585 return ret;
8586}
8587
8588/**
Helin Zhang043dd652015-10-21 19:56:23 -04008589 * i40e_config_rss_reg - Configure RSS keys and lut by writing registers
Helin Zhange69ff812015-10-21 19:56:22 -04008590 * @vsi: Pointer to vsi structure
8591 * @seed: RSS hash seed
8592 * @lut: Lookup table
8593 * @lut_size: Lookup table size
8594 *
8595 * Returns 0 on success, negative on failure
8596 **/
8597static int i40e_config_rss_reg(struct i40e_vsi *vsi, const u8 *seed,
8598 const u8 *lut, u16 lut_size)
8599{
8600 struct i40e_pf *pf = vsi->back;
8601 struct i40e_hw *hw = &pf->hw;
Mitch Williamsc4e18682016-04-12 08:30:40 -07008602 u16 vf_id = vsi->vf_id;
Helin Zhange69ff812015-10-21 19:56:22 -04008603 u8 i;
8604
8605 /* Fill out hash function seed */
8606 if (seed) {
8607 u32 *seed_dw = (u32 *)seed;
8608
Mitch Williamsc4e18682016-04-12 08:30:40 -07008609 if (vsi->type == I40E_VSI_MAIN) {
8610 for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
Lihong Yang26f77e52017-01-30 12:29:33 -08008611 wr32(hw, I40E_PFQF_HKEY(i), seed_dw[i]);
Mitch Williamsc4e18682016-04-12 08:30:40 -07008612 } else if (vsi->type == I40E_VSI_SRIOV) {
8613 for (i = 0; i <= I40E_VFQF_HKEY1_MAX_INDEX; i++)
Lihong Yang26f77e52017-01-30 12:29:33 -08008614 wr32(hw, I40E_VFQF_HKEY1(i, vf_id), seed_dw[i]);
Mitch Williamsc4e18682016-04-12 08:30:40 -07008615 } else {
8616 dev_err(&pf->pdev->dev, "Cannot set RSS seed - invalid VSI type\n");
8617 }
Helin Zhange69ff812015-10-21 19:56:22 -04008618 }
8619
8620 if (lut) {
8621 u32 *lut_dw = (u32 *)lut;
8622
Mitch Williamsc4e18682016-04-12 08:30:40 -07008623 if (vsi->type == I40E_VSI_MAIN) {
8624 if (lut_size != I40E_HLUT_ARRAY_SIZE)
8625 return -EINVAL;
8626 for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
8627 wr32(hw, I40E_PFQF_HLUT(i), lut_dw[i]);
8628 } else if (vsi->type == I40E_VSI_SRIOV) {
8629 if (lut_size != I40E_VF_HLUT_ARRAY_SIZE)
8630 return -EINVAL;
8631 for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
Lihong Yang26f77e52017-01-30 12:29:33 -08008632 wr32(hw, I40E_VFQF_HLUT1(i, vf_id), lut_dw[i]);
Mitch Williamsc4e18682016-04-12 08:30:40 -07008633 } else {
8634 dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
8635 }
Helin Zhange69ff812015-10-21 19:56:22 -04008636 }
8637 i40e_flush(hw);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008638
8639 return 0;
8640}
8641
8642/**
Helin Zhang043dd652015-10-21 19:56:23 -04008643 * i40e_get_rss_reg - Get the RSS keys and lut by reading registers
8644 * @vsi: Pointer to VSI structure
8645 * @seed: Buffer to store the keys
8646 * @lut: Buffer to store the lookup table entries
8647 * @lut_size: Size of buffer to store the lookup table entries
8648 *
8649 * Returns 0 on success, negative on failure
8650 */
8651static int i40e_get_rss_reg(struct i40e_vsi *vsi, u8 *seed,
8652 u8 *lut, u16 lut_size)
8653{
8654 struct i40e_pf *pf = vsi->back;
8655 struct i40e_hw *hw = &pf->hw;
8656 u16 i;
8657
8658 if (seed) {
8659 u32 *seed_dw = (u32 *)seed;
8660
8661 for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
Shannon Nelson272cdaf22016-02-17 16:12:21 -08008662 seed_dw[i] = i40e_read_rx_ctl(hw, I40E_PFQF_HKEY(i));
Helin Zhang043dd652015-10-21 19:56:23 -04008663 }
8664 if (lut) {
8665 u32 *lut_dw = (u32 *)lut;
8666
8667 if (lut_size != I40E_HLUT_ARRAY_SIZE)
8668 return -EINVAL;
8669 for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
8670 lut_dw[i] = rd32(hw, I40E_PFQF_HLUT(i));
8671 }
8672
8673 return 0;
8674}
8675
8676/**
8677 * i40e_config_rss - Configure RSS keys and lut
8678 * @vsi: Pointer to VSI structure
8679 * @seed: RSS hash seed
8680 * @lut: Lookup table
8681 * @lut_size: Lookup table size
8682 *
8683 * Returns 0 on success, negative on failure
8684 */
8685int i40e_config_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
8686{
8687 struct i40e_pf *pf = vsi->back;
8688
Jacob Kellerd36e41d2017-06-23 04:24:46 -04008689 if (pf->hw_features & I40E_HW_RSS_AQ_CAPABLE)
Helin Zhang043dd652015-10-21 19:56:23 -04008690 return i40e_config_rss_aq(vsi, seed, lut, lut_size);
8691 else
8692 return i40e_config_rss_reg(vsi, seed, lut, lut_size);
8693}
8694
8695/**
8696 * i40e_get_rss - Get RSS keys and lut
8697 * @vsi: Pointer to VSI structure
8698 * @seed: Buffer to store the keys
8699 * @lut: Buffer to store the lookup table entries
8700 * lut_size: Size of buffer to store the lookup table entries
8701 *
8702 * Returns 0 on success, negative on failure
8703 */
8704int i40e_get_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
8705{
Anjali Singhai Jain95a73782015-12-22 14:25:04 -08008706 struct i40e_pf *pf = vsi->back;
8707
Jacob Kellerd36e41d2017-06-23 04:24:46 -04008708 if (pf->hw_features & I40E_HW_RSS_AQ_CAPABLE)
Anjali Singhai Jain95a73782015-12-22 14:25:04 -08008709 return i40e_get_rss_aq(vsi, seed, lut, lut_size);
8710 else
8711 return i40e_get_rss_reg(vsi, seed, lut, lut_size);
Helin Zhang043dd652015-10-21 19:56:23 -04008712}
8713
8714/**
Helin Zhange69ff812015-10-21 19:56:22 -04008715 * i40e_fill_rss_lut - Fill the RSS lookup table with default values
8716 * @pf: Pointer to board private structure
8717 * @lut: Lookup table
8718 * @rss_table_size: Lookup table size
8719 * @rss_size: Range of queue number for hashing
8720 */
Alan Bradyf1582352016-08-24 11:33:46 -07008721void i40e_fill_rss_lut(struct i40e_pf *pf, u8 *lut,
8722 u16 rss_table_size, u16 rss_size)
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008723{
Helin Zhange69ff812015-10-21 19:56:22 -04008724 u16 i;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008725
Helin Zhange69ff812015-10-21 19:56:22 -04008726 for (i = 0; i < rss_table_size; i++)
8727 lut[i] = i % rss_size;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008728}
8729
8730/**
Helin Zhang043dd652015-10-21 19:56:23 -04008731 * i40e_pf_config_rss - Prepare for RSS if used
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008732 * @pf: board private structure
8733 **/
Helin Zhang043dd652015-10-21 19:56:23 -04008734static int i40e_pf_config_rss(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008735{
Anjali Singhai Jain66ddcff2015-02-24 06:58:50 +00008736 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008737 u8 seed[I40E_HKEY_ARRAY_SIZE];
Helin Zhange69ff812015-10-21 19:56:22 -04008738 u8 *lut;
Anjali Singhai Jain4617e8c2013-11-20 10:02:56 +00008739 struct i40e_hw *hw = &pf->hw;
Carolyn Wybornye157ea32014-06-03 23:50:22 +00008740 u32 reg_val;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008741 u64 hena;
Helin Zhange69ff812015-10-21 19:56:22 -04008742 int ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008743
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008744 /* By default we enable TCP/UDP with IPv4/IPv6 ptypes */
Shannon Nelson272cdaf22016-02-17 16:12:21 -08008745 hena = (u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(0)) |
8746 ((u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(1)) << 32);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008747 hena |= i40e_pf_get_default_rss_hena(pf);
8748
Shannon Nelson272cdaf22016-02-17 16:12:21 -08008749 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), (u32)hena);
8750 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008751
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008752 /* Determine the RSS table size based on the hardware capabilities */
Shannon Nelson272cdaf22016-02-17 16:12:21 -08008753 reg_val = i40e_read_rx_ctl(hw, I40E_PFQF_CTL_0);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008754 reg_val = (pf->rss_table_size == 512) ?
8755 (reg_val | I40E_PFQF_CTL_0_HASHLUTSIZE_512) :
8756 (reg_val & ~I40E_PFQF_CTL_0_HASHLUTSIZE_512);
Shannon Nelson272cdaf22016-02-17 16:12:21 -08008757 i40e_write_rx_ctl(hw, I40E_PFQF_CTL_0, reg_val);
Carolyn Wybornye157ea32014-06-03 23:50:22 +00008758
Helin Zhang28c58692015-10-26 19:44:27 -04008759 /* Determine the RSS size of the VSI */
Harshitha Ramamurthyf25571b2017-02-21 15:55:40 -08008760 if (!vsi->rss_size) {
8761 u16 qcount;
8762
8763 qcount = vsi->num_queue_pairs / vsi->tc_config.numtc;
8764 vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
8765 }
Mitch Williamsa4fa59c2016-09-12 14:18:43 -07008766 if (!vsi->rss_size)
8767 return -EINVAL;
Helin Zhang28c58692015-10-26 19:44:27 -04008768
Helin Zhange69ff812015-10-21 19:56:22 -04008769 lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
8770 if (!lut)
8771 return -ENOMEM;
8772
Helin Zhang28c58692015-10-26 19:44:27 -04008773 /* Use user configured lut if there is one, otherwise use default */
8774 if (vsi->rss_lut_user)
8775 memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
8776 else
8777 i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
Helin Zhange69ff812015-10-21 19:56:22 -04008778
Helin Zhang28c58692015-10-26 19:44:27 -04008779 /* Use user configured hash key if there is one, otherwise
8780 * use default.
8781 */
8782 if (vsi->rss_hkey_user)
8783 memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
8784 else
8785 netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
Helin Zhang043dd652015-10-21 19:56:23 -04008786 ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
Helin Zhange69ff812015-10-21 19:56:22 -04008787 kfree(lut);
8788
8789 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008790}
8791
8792/**
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008793 * i40e_reconfig_rss_queues - change number of queues for rss and rebuild
8794 * @pf: board private structure
8795 * @queue_count: the requested queue count for rss.
8796 *
8797 * returns 0 if rss is not enabled, if enabled returns the final rss queue
8798 * count which may be different from the requested queue count.
Maciej Sosin373149f2017-04-05 07:50:55 -04008799 * Note: expects to be called while under rtnl_lock()
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008800 **/
8801int i40e_reconfig_rss_queues(struct i40e_pf *pf, int queue_count)
8802{
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00008803 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
8804 int new_rss_size;
8805
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008806 if (!(pf->flags & I40E_FLAG_RSS_ENABLED))
8807 return 0;
8808
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00008809 new_rss_size = min_t(int, queue_count, pf->rss_size_max);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008810
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00008811 if (queue_count != vsi->num_queue_pairs) {
Harshitha Ramamurthyf25571b2017-02-21 15:55:40 -08008812 u16 qcount;
8813
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00008814 vsi->req_queue_pairs = queue_count;
Maciej Sosin373149f2017-04-05 07:50:55 -04008815 i40e_prep_for_reset(pf, true);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008816
Helin Zhangacd65442015-10-26 19:44:28 -04008817 pf->alloc_rss_size = new_rss_size;
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008818
Maciej Sosin373149f2017-04-05 07:50:55 -04008819 i40e_reset_and_rebuild(pf, true, true);
Helin Zhang28c58692015-10-26 19:44:27 -04008820
8821 /* Discard the user configured hash keys and lut, if less
8822 * queues are enabled.
8823 */
8824 if (queue_count < vsi->rss_size) {
8825 i40e_clear_rss_config_user(vsi);
8826 dev_dbg(&pf->pdev->dev,
8827 "discard user configured hash keys and lut\n");
8828 }
8829
8830 /* Reset vsi->rss_size, as number of enabled queues changed */
Harshitha Ramamurthyf25571b2017-02-21 15:55:40 -08008831 qcount = vsi->num_queue_pairs / vsi->tc_config.numtc;
8832 vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
Helin Zhang28c58692015-10-26 19:44:27 -04008833
Helin Zhang043dd652015-10-21 19:56:23 -04008834 i40e_pf_config_rss(pf);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008835 }
Lihong Yang12815052016-09-27 11:28:48 -07008836 dev_info(&pf->pdev->dev, "User requested queue count/HW max RSS count: %d/%d\n",
8837 vsi->req_queue_pairs, pf->rss_size_max);
Helin Zhangacd65442015-10-26 19:44:28 -04008838 return pf->alloc_rss_size;
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008839}
8840
8841/**
Shannon Nelson4fc8c672017-06-07 05:43:08 -04008842 * i40e_get_partition_bw_setting - Retrieve BW settings for this PF partition
Greg Rosef4492db2015-02-06 08:52:12 +00008843 * @pf: board private structure
8844 **/
Shannon Nelson4fc8c672017-06-07 05:43:08 -04008845i40e_status i40e_get_partition_bw_setting(struct i40e_pf *pf)
Greg Rosef4492db2015-02-06 08:52:12 +00008846{
8847 i40e_status status;
8848 bool min_valid, max_valid;
8849 u32 max_bw, min_bw;
8850
8851 status = i40e_read_bw_from_alt_ram(&pf->hw, &max_bw, &min_bw,
8852 &min_valid, &max_valid);
8853
8854 if (!status) {
8855 if (min_valid)
Shannon Nelson4fc8c672017-06-07 05:43:08 -04008856 pf->min_bw = min_bw;
Greg Rosef4492db2015-02-06 08:52:12 +00008857 if (max_valid)
Shannon Nelson4fc8c672017-06-07 05:43:08 -04008858 pf->max_bw = max_bw;
Greg Rosef4492db2015-02-06 08:52:12 +00008859 }
8860
8861 return status;
8862}
8863
8864/**
Shannon Nelson4fc8c672017-06-07 05:43:08 -04008865 * i40e_set_partition_bw_setting - Set BW settings for this PF partition
Greg Rosef4492db2015-02-06 08:52:12 +00008866 * @pf: board private structure
8867 **/
Shannon Nelson4fc8c672017-06-07 05:43:08 -04008868i40e_status i40e_set_partition_bw_setting(struct i40e_pf *pf)
Greg Rosef4492db2015-02-06 08:52:12 +00008869{
8870 struct i40e_aqc_configure_partition_bw_data bw_data;
8871 i40e_status status;
8872
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00008873 /* Set the valid bit for this PF */
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04008874 bw_data.pf_valid_bits = cpu_to_le16(BIT(pf->hw.pf_id));
Shannon Nelson4fc8c672017-06-07 05:43:08 -04008875 bw_data.max_bw[pf->hw.pf_id] = pf->max_bw & I40E_ALT_BW_VALUE_MASK;
8876 bw_data.min_bw[pf->hw.pf_id] = pf->min_bw & I40E_ALT_BW_VALUE_MASK;
Greg Rosef4492db2015-02-06 08:52:12 +00008877
8878 /* Set the new bandwidths */
8879 status = i40e_aq_configure_partition_bw(&pf->hw, &bw_data, NULL);
8880
8881 return status;
8882}
8883
8884/**
Shannon Nelson4fc8c672017-06-07 05:43:08 -04008885 * i40e_commit_partition_bw_setting - Commit BW settings for this PF partition
Greg Rosef4492db2015-02-06 08:52:12 +00008886 * @pf: board private structure
8887 **/
Shannon Nelson4fc8c672017-06-07 05:43:08 -04008888i40e_status i40e_commit_partition_bw_setting(struct i40e_pf *pf)
Greg Rosef4492db2015-02-06 08:52:12 +00008889{
8890 /* Commit temporary BW setting to permanent NVM image */
8891 enum i40e_admin_queue_err last_aq_status;
8892 i40e_status ret;
8893 u16 nvm_word;
8894
8895 if (pf->hw.partition_id != 1) {
8896 dev_info(&pf->pdev->dev,
8897 "Commit BW only works on partition 1! This is partition %d",
8898 pf->hw.partition_id);
8899 ret = I40E_NOT_SUPPORTED;
8900 goto bw_commit_out;
8901 }
8902
8903 /* Acquire NVM for read access */
8904 ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_READ);
8905 last_aq_status = pf->hw.aq.asq_last_status;
8906 if (ret) {
8907 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008908 "Cannot acquire NVM for read access, err %s aq_err %s\n",
8909 i40e_stat_str(&pf->hw, ret),
8910 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +00008911 goto bw_commit_out;
8912 }
8913
8914 /* Read word 0x10 of NVM - SW compatibility word 1 */
8915 ret = i40e_aq_read_nvm(&pf->hw,
8916 I40E_SR_NVM_CONTROL_WORD,
8917 0x10, sizeof(nvm_word), &nvm_word,
8918 false, NULL);
8919 /* Save off last admin queue command status before releasing
8920 * the NVM
8921 */
8922 last_aq_status = pf->hw.aq.asq_last_status;
8923 i40e_release_nvm(&pf->hw);
8924 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008925 dev_info(&pf->pdev->dev, "NVM read error, err %s aq_err %s\n",
8926 i40e_stat_str(&pf->hw, ret),
8927 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +00008928 goto bw_commit_out;
8929 }
8930
8931 /* Wait a bit for NVM release to complete */
8932 msleep(50);
8933
8934 /* Acquire NVM for write access */
8935 ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_WRITE);
8936 last_aq_status = pf->hw.aq.asq_last_status;
8937 if (ret) {
8938 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008939 "Cannot acquire NVM for write access, err %s aq_err %s\n",
8940 i40e_stat_str(&pf->hw, ret),
8941 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +00008942 goto bw_commit_out;
8943 }
8944 /* Write it back out unchanged to initiate update NVM,
8945 * which will force a write of the shadow (alt) RAM to
8946 * the NVM - thus storing the bandwidth values permanently.
8947 */
8948 ret = i40e_aq_update_nvm(&pf->hw,
8949 I40E_SR_NVM_CONTROL_WORD,
8950 0x10, sizeof(nvm_word),
8951 &nvm_word, true, NULL);
8952 /* Save off last admin queue command status before releasing
8953 * the NVM
8954 */
8955 last_aq_status = pf->hw.aq.asq_last_status;
8956 i40e_release_nvm(&pf->hw);
8957 if (ret)
8958 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008959 "BW settings NOT SAVED, err %s aq_err %s\n",
8960 i40e_stat_str(&pf->hw, ret),
8961 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +00008962bw_commit_out:
8963
8964 return ret;
8965}
8966
8967/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008968 * i40e_sw_init - Initialize general software structures (struct i40e_pf)
8969 * @pf: board private structure to initialize
8970 *
8971 * i40e_sw_init initializes the Adapter private data structure.
8972 * Fields are initialized based on PCI device information and
8973 * OS network device settings (MTU size).
8974 **/
8975static int i40e_sw_init(struct i40e_pf *pf)
8976{
8977 int err = 0;
8978 int size;
8979
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008980 /* Set default capability flags */
8981 pf->flags = I40E_FLAG_RX_CSUM_ENABLED |
8982 I40E_FLAG_MSI_ENABLED |
Mitch Williams2bc7ee82015-02-06 08:52:11 +00008983 I40E_FLAG_MSIX_ENABLED;
8984
Mitch Williamsca99eb92014-04-04 04:43:07 +00008985 /* Set default ITR */
8986 pf->rx_itr_default = I40E_ITR_DYNAMIC | I40E_ITR_RX_DEF;
8987 pf->tx_itr_default = I40E_ITR_DYNAMIC | I40E_ITR_TX_DEF;
8988
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00008989 /* Depending on PF configurations, it is possible that the RSS
8990 * maximum might end up larger than the available queues
8991 */
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04008992 pf->rss_size_max = BIT(pf->hw.func_caps.rss_table_entry_width);
Helin Zhangacd65442015-10-26 19:44:28 -04008993 pf->alloc_rss_size = 1;
Anjali Singhai Jain5db4cb52015-02-24 06:58:49 +00008994 pf->rss_table_size = pf->hw.func_caps.rss_table_size;
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00008995 pf->rss_size_max = min_t(int, pf->rss_size_max,
8996 pf->hw.func_caps.num_tx_qp);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008997 if (pf->hw.func_caps.rss) {
8998 pf->flags |= I40E_FLAG_RSS_ENABLED;
Helin Zhangacd65442015-10-26 19:44:28 -04008999 pf->alloc_rss_size = min_t(int, pf->rss_size_max,
9000 num_online_cpus());
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009001 }
9002
Catherine Sullivan2050bc62013-12-18 13:46:03 +00009003 /* MFP mode enabled */
Pawel Orlowskic78b9532015-04-22 19:34:06 -04009004 if (pf->hw.func_caps.npar_enable || pf->hw.func_caps.flex10_enable) {
Catherine Sullivan2050bc62013-12-18 13:46:03 +00009005 pf->flags |= I40E_FLAG_MFP_ENABLED;
9006 dev_info(&pf->pdev->dev, "MFP mode Enabled\n");
Shannon Nelson4fc8c672017-06-07 05:43:08 -04009007 if (i40e_get_partition_bw_setting(pf)) {
Greg Rosef4492db2015-02-06 08:52:12 +00009008 dev_warn(&pf->pdev->dev,
Shannon Nelson4fc8c672017-06-07 05:43:08 -04009009 "Could not get partition bw settings\n");
9010 } else {
Greg Rosef4492db2015-02-06 08:52:12 +00009011 dev_info(&pf->pdev->dev,
Shannon Nelson4fc8c672017-06-07 05:43:08 -04009012 "Partition BW Min = %8.8x, Max = %8.8x\n",
9013 pf->min_bw, pf->max_bw);
9014
9015 /* nudge the Tx scheduler */
9016 i40e_set_partition_bw_setting(pf);
9017 }
Catherine Sullivan2050bc62013-12-18 13:46:03 +00009018 }
9019
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009020 if ((pf->hw.func_caps.fd_filters_guaranteed > 0) ||
9021 (pf->hw.func_caps.fd_filters_best_effort > 0)) {
9022 pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
9023 pf->atr_sample_rate = I40E_DEFAULT_ATR_SAMPLE_RATE;
Shannon Nelson6eae9c62015-09-03 17:18:55 -04009024 if (pf->flags & I40E_FLAG_MFP_ENABLED &&
9025 pf->hw.num_partitions > 1)
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009026 dev_info(&pf->pdev->dev,
Anjali Singhai Jain0b675842014-03-06 08:59:51 +00009027 "Flow Director Sideband mode Disabled in MFP mode\n");
Shannon Nelson6eae9c62015-09-03 17:18:55 -04009028 else
9029 pf->flags |= I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009030 pf->fdir_pf_filter_count =
9031 pf->hw.func_caps.fd_filters_guaranteed;
9032 pf->hw.fdir_shared_filter_count =
9033 pf->hw.func_caps.fd_filters_best_effort;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009034 }
9035
Anjali Singhai Jain5a433192017-06-23 04:24:45 -04009036 if (pf->hw.mac.type == I40E_MAC_X722) {
Jacob Kellerd36e41d2017-06-23 04:24:46 -04009037 pf->hw_features |= (I40E_HW_RSS_AQ_CAPABLE |
9038 I40E_HW_128_QP_RSS_CAPABLE |
9039 I40E_HW_ATR_EVICT_CAPABLE |
9040 I40E_HW_WB_ON_ITR_CAPABLE |
9041 I40E_HW_MULTIPLE_TCP_UDP_RSS_PCTYPE |
9042 I40E_HW_NO_PCI_LINK_CHECK |
9043 I40E_HW_USE_SET_LLDP_MIB |
9044 I40E_HW_GENEVE_OFFLOAD_CAPABLE |
9045 I40E_HW_PTP_L4_CAPABLE |
9046 I40E_HW_WOL_MC_MAGIC_PKT_WAKE |
9047 I40E_HW_OUTER_UDP_CSUM_CAPABLE);
Anjali Singhai Jain10a955f2017-06-23 04:24:48 -04009048
9049#define I40E_FDEVICT_PCTYPE_DEFAULT 0xc03
9050 if (rd32(&pf->hw, I40E_GLQF_FDEVICTENA(1)) !=
9051 I40E_FDEVICT_PCTYPE_DEFAULT) {
9052 dev_warn(&pf->pdev->dev,
9053 "FD EVICT PCTYPES are not right, disable FD HW EVICT\n");
9054 pf->hw_features &= ~I40E_HW_ATR_EVICT_CAPABLE;
9055 }
Anjali Singhai Jain5a433192017-06-23 04:24:45 -04009056 } else if ((pf->hw.aq.api_maj_ver > 1) ||
9057 ((pf->hw.aq.api_maj_ver == 1) &&
9058 (pf->hw.aq.api_min_ver > 4))) {
9059 /* Supported in FW API version higher than 1.4 */
Jacob Kellerd36e41d2017-06-23 04:24:46 -04009060 pf->hw_features |= I40E_HW_GENEVE_OFFLOAD_CAPABLE;
Anjali Singhai Jain5a433192017-06-23 04:24:45 -04009061 }
9062
9063 /* Enable HW ATR eviction if possible */
Jacob Kellerd36e41d2017-06-23 04:24:46 -04009064 if (pf->hw_features & I40E_HW_ATR_EVICT_CAPABLE)
Anjali Singhai Jain5a433192017-06-23 04:24:45 -04009065 pf->flags |= I40E_FLAG_HW_ATR_EVICT_ENABLED;
9066
Bimmy Pujari6de432c2016-11-11 12:39:38 -08009067 if ((pf->hw.mac.type == I40E_MAC_XL710) &&
Anjali Singhai Jain8eed76f2015-12-09 15:50:31 -08009068 (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 33)) ||
Neerav Parikhf1bbad32016-01-13 16:51:39 -08009069 (pf->hw.aq.fw_maj_ver < 4))) {
Jacob Kellerd36e41d2017-06-23 04:24:46 -04009070 pf->hw_features |= I40E_HW_RESTART_AUTONEG;
Neerav Parikhf1bbad32016-01-13 16:51:39 -08009071 /* No DCB support for FW < v4.33 */
Jacob Kellerd36e41d2017-06-23 04:24:46 -04009072 pf->hw_features |= I40E_HW_NO_DCB_SUPPORT;
Neerav Parikhf1bbad32016-01-13 16:51:39 -08009073 }
9074
9075 /* Disable FW LLDP if FW < v4.3 */
Bimmy Pujari6de432c2016-11-11 12:39:38 -08009076 if ((pf->hw.mac.type == I40E_MAC_XL710) &&
Neerav Parikhf1bbad32016-01-13 16:51:39 -08009077 (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 3)) ||
9078 (pf->hw.aq.fw_maj_ver < 4)))
Jacob Kellerd36e41d2017-06-23 04:24:46 -04009079 pf->hw_features |= I40E_HW_STOP_FW_LLDP;
Neerav Parikhf1bbad32016-01-13 16:51:39 -08009080
9081 /* Use the FW Set LLDP MIB API if FW > v4.40 */
Bimmy Pujari6de432c2016-11-11 12:39:38 -08009082 if ((pf->hw.mac.type == I40E_MAC_XL710) &&
Neerav Parikhf1bbad32016-01-13 16:51:39 -08009083 (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver >= 40)) ||
9084 (pf->hw.aq.fw_maj_ver >= 5)))
Jacob Kellerd36e41d2017-06-23 04:24:46 -04009085 pf->hw_features |= I40E_HW_USE_SET_LLDP_MIB;
Anjali Singhai Jain8eed76f2015-12-09 15:50:31 -08009086
Alan Bradyc3d26b72017-08-29 05:32:32 -04009087 /* Enable PTP L4 if FW > v6.0 */
9088 if (pf->hw.mac.type == I40E_MAC_XL710 &&
9089 pf->hw.aq.fw_maj_ver >= 6)
9090 pf->hw_features |= I40E_HW_PTP_L4_CAPABLE;
9091
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009092 if (pf->hw.func_caps.vmdq) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009093 pf->num_vmdq_vsis = I40E_DEFAULT_NUM_VMDQ_VSI;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04009094 pf->flags |= I40E_FLAG_VMDQ_ENABLED;
Jesse Brandeburge9e53662015-10-02 17:57:21 -07009095 pf->num_vmdq_qps = i40e_default_queues_per_vmdq(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009096 }
9097
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06009098 if (pf->hw.func_caps.iwarp) {
9099 pf->flags |= I40E_FLAG_IWARP_ENABLED;
9100 /* IWARP needs one extra vector for CQP just like MISC.*/
9101 pf->num_iwarp_msix = (int)num_online_cpus() + 1;
9102 }
9103
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009104#ifdef CONFIG_PCI_IOV
Shannon Nelsonba252f132014-12-11 07:06:34 +00009105 if (pf->hw.func_caps.num_vfs && pf->hw.partition_id == 1) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009106 pf->num_vf_qps = I40E_DEFAULT_QUEUES_PER_VF;
9107 pf->flags |= I40E_FLAG_SRIOV_ENABLED;
9108 pf->num_req_vfs = min_t(int,
9109 pf->hw.func_caps.num_vfs,
9110 I40E_MAX_VF_COUNT);
9111 }
9112#endif /* CONFIG_PCI_IOV */
9113 pf->eeprom_version = 0xDEAD;
9114 pf->lan_veb = I40E_NO_VEB;
9115 pf->lan_vsi = I40E_NO_VSI;
9116
Anjali Singhai Jaind1a8d272015-07-23 16:54:40 -04009117 /* By default FW has this off for performance reasons */
9118 pf->flags &= ~I40E_FLAG_VEB_STATS_ENABLED;
9119
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009120 /* set up queue assignment tracking */
9121 size = sizeof(struct i40e_lump_tracking)
9122 + (sizeof(u16) * pf->hw.func_caps.num_tx_qp);
9123 pf->qp_pile = kzalloc(size, GFP_KERNEL);
9124 if (!pf->qp_pile) {
9125 err = -ENOMEM;
9126 goto sw_init_done;
9127 }
9128 pf->qp_pile->num_entries = pf->hw.func_caps.num_tx_qp;
9129 pf->qp_pile->search_hint = 0;
9130
Anjali Singhai Jain327fe042014-06-04 01:23:26 +00009131 pf->tx_timeout_recovery_level = 1;
9132
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009133 mutex_init(&pf->switch_mutex);
9134
9135sw_init_done:
9136 return err;
9137}
9138
9139/**
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00009140 * i40e_set_ntuple - set the ntuple feature flag and take action
9141 * @pf: board private structure to initialize
9142 * @features: the feature set that the stack is suggesting
9143 *
9144 * returns a bool to indicate if reset needs to happen
9145 **/
9146bool i40e_set_ntuple(struct i40e_pf *pf, netdev_features_t features)
9147{
9148 bool need_reset = false;
9149
9150 /* Check if Flow Director n-tuple support was enabled or disabled. If
9151 * the state changed, we need to reset.
9152 */
9153 if (features & NETIF_F_NTUPLE) {
9154 /* Enable filters and mark for reset */
9155 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
9156 need_reset = true;
Tushar Davea70e4072016-05-16 12:40:53 -07009157 /* enable FD_SB only if there is MSI-X vector */
9158 if (pf->num_fdsb_msix > 0)
9159 pf->flags |= I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00009160 } else {
9161 /* turn off filters, mark for reset and clear SW filter list */
9162 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
9163 need_reset = true;
9164 i40e_fdir_filter_exit(pf);
9165 }
Jacob Keller47994c12017-04-19 09:25:57 -04009166 pf->flags &= ~(I40E_FLAG_FD_SB_ENABLED |
9167 I40E_FLAG_FD_SB_AUTO_DISABLED);
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00009168 /* reset fd counters */
Jacob Keller097dbf52017-02-06 14:38:46 -08009169 pf->fd_add_err = 0;
9170 pf->fd_atr_cnt = 0;
Anjali Singhai Jain8a4f34f2014-06-04 08:45:20 +00009171 /* if ATR was auto disabled it can be re-enabled. */
Jacob Keller47994c12017-04-19 09:25:57 -04009172 if (pf->flags & I40E_FLAG_FD_ATR_AUTO_DISABLED) {
9173 pf->flags &= ~I40E_FLAG_FD_ATR_AUTO_DISABLED;
9174 if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
9175 (I40E_DEBUG_FD & pf->hw.debug_mask))
Jacob Keller234dc4e2016-09-06 18:05:09 -07009176 dev_info(&pf->pdev->dev, "ATR re-enabled.\n");
9177 }
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00009178 }
9179 return need_reset;
9180}
9181
9182/**
Alan Bradyd8ec9862016-07-27 12:02:38 -07009183 * i40e_clear_rss_lut - clear the rx hash lookup table
9184 * @vsi: the VSI being configured
9185 **/
9186static void i40e_clear_rss_lut(struct i40e_vsi *vsi)
9187{
9188 struct i40e_pf *pf = vsi->back;
9189 struct i40e_hw *hw = &pf->hw;
9190 u16 vf_id = vsi->vf_id;
9191 u8 i;
9192
9193 if (vsi->type == I40E_VSI_MAIN) {
9194 for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
9195 wr32(hw, I40E_PFQF_HLUT(i), 0);
9196 } else if (vsi->type == I40E_VSI_SRIOV) {
9197 for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
9198 i40e_write_rx_ctl(hw, I40E_VFQF_HLUT1(i, vf_id), 0);
9199 } else {
9200 dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
9201 }
9202}
9203
9204/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009205 * i40e_set_features - set the netdev feature flags
9206 * @netdev: ptr to the netdev being adjusted
9207 * @features: the feature set that the stack is suggesting
Maciej Sosin373149f2017-04-05 07:50:55 -04009208 * Note: expects to be called while under rtnl_lock()
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009209 **/
9210static int i40e_set_features(struct net_device *netdev,
9211 netdev_features_t features)
9212{
9213 struct i40e_netdev_priv *np = netdev_priv(netdev);
9214 struct i40e_vsi *vsi = np->vsi;
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00009215 struct i40e_pf *pf = vsi->back;
9216 bool need_reset;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009217
Alan Bradyd8ec9862016-07-27 12:02:38 -07009218 if (features & NETIF_F_RXHASH && !(netdev->features & NETIF_F_RXHASH))
9219 i40e_pf_config_rss(pf);
9220 else if (!(features & NETIF_F_RXHASH) &&
9221 netdev->features & NETIF_F_RXHASH)
9222 i40e_clear_rss_lut(vsi);
9223
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009224 if (features & NETIF_F_HW_VLAN_CTAG_RX)
9225 i40e_vlan_stripping_enable(vsi);
9226 else
9227 i40e_vlan_stripping_disable(vsi);
9228
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00009229 need_reset = i40e_set_ntuple(pf, features);
9230
9231 if (need_reset)
Maciej Sosin373149f2017-04-05 07:50:55 -04009232 i40e_do_reset(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED), true);
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00009233
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009234 return 0;
9235}
9236
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009237/**
Singhai, Anjali6a899022015-12-14 12:21:18 -08009238 * i40e_get_udp_port_idx - Lookup a possibly offloaded for Rx UDP port
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009239 * @pf: board private structure
9240 * @port: The UDP port to look up
9241 *
9242 * Returns the index number or I40E_MAX_PF_UDP_OFFLOAD_PORTS if port not found
9243 **/
Jacob Kellerfe0b0cd2017-02-06 14:38:38 -08009244static u8 i40e_get_udp_port_idx(struct i40e_pf *pf, u16 port)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009245{
9246 u8 i;
9247
9248 for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
Jacob Keller27826fd2017-04-19 09:25:50 -04009249 if (pf->udp_ports[i].port == port)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009250 return i;
9251 }
9252
9253 return i;
9254}
9255
9256/**
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009257 * i40e_udp_tunnel_add - Get notifications about UDP tunnel ports that come up
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009258 * @netdev: This physical port's netdev
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009259 * @ti: Tunnel endpoint information
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009260 **/
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009261static void i40e_udp_tunnel_add(struct net_device *netdev,
9262 struct udp_tunnel_info *ti)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009263{
9264 struct i40e_netdev_priv *np = netdev_priv(netdev);
9265 struct i40e_vsi *vsi = np->vsi;
9266 struct i40e_pf *pf = vsi->back;
Jacob Kellerfe0b0cd2017-02-06 14:38:38 -08009267 u16 port = ntohs(ti->port);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009268 u8 next_idx;
9269 u8 idx;
9270
Singhai, Anjali6a899022015-12-14 12:21:18 -08009271 idx = i40e_get_udp_port_idx(pf, port);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009272
9273 /* Check if port already exists */
9274 if (idx < I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
Jacob Kellerfe0b0cd2017-02-06 14:38:38 -08009275 netdev_info(netdev, "port %d already offloaded\n", port);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009276 return;
9277 }
9278
9279 /* Now check if there is space to add the new port */
Singhai, Anjali6a899022015-12-14 12:21:18 -08009280 next_idx = i40e_get_udp_port_idx(pf, 0);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009281
9282 if (next_idx == I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009283 netdev_info(netdev, "maximum number of offloaded UDP ports reached, not adding port %d\n",
Jacob Kellerfe0b0cd2017-02-06 14:38:38 -08009284 port);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009285 return;
9286 }
9287
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009288 switch (ti->type) {
9289 case UDP_TUNNEL_TYPE_VXLAN:
9290 pf->udp_ports[next_idx].type = I40E_AQC_TUNNEL_TYPE_VXLAN;
9291 break;
9292 case UDP_TUNNEL_TYPE_GENEVE:
Jacob Kellerd36e41d2017-06-23 04:24:46 -04009293 if (!(pf->hw_features & I40E_HW_GENEVE_OFFLOAD_CAPABLE))
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009294 return;
9295 pf->udp_ports[next_idx].type = I40E_AQC_TUNNEL_TYPE_NGE;
9296 break;
9297 default:
9298 return;
9299 }
9300
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009301 /* New port: add it and mark its index in the bitmap */
Jacob Keller27826fd2017-04-19 09:25:50 -04009302 pf->udp_ports[next_idx].port = port;
Singhai, Anjali6a899022015-12-14 12:21:18 -08009303 pf->pending_udp_bitmap |= BIT_ULL(next_idx);
9304 pf->flags |= I40E_FLAG_UDP_FILTER_SYNC;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009305}
9306
9307/**
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009308 * i40e_udp_tunnel_del - Get notifications about UDP tunnel ports that go away
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009309 * @netdev: This physical port's netdev
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009310 * @ti: Tunnel endpoint information
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009311 **/
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009312static void i40e_udp_tunnel_del(struct net_device *netdev,
9313 struct udp_tunnel_info *ti)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009314{
9315 struct i40e_netdev_priv *np = netdev_priv(netdev);
9316 struct i40e_vsi *vsi = np->vsi;
9317 struct i40e_pf *pf = vsi->back;
Jacob Kellerfe0b0cd2017-02-06 14:38:38 -08009318 u16 port = ntohs(ti->port);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009319 u8 idx;
9320
Singhai, Anjali6a899022015-12-14 12:21:18 -08009321 idx = i40e_get_udp_port_idx(pf, port);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009322
9323 /* Check if port already exists */
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009324 if (idx >= I40E_MAX_PF_UDP_OFFLOAD_PORTS)
9325 goto not_found;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009326
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009327 switch (ti->type) {
9328 case UDP_TUNNEL_TYPE_VXLAN:
9329 if (pf->udp_ports[idx].type != I40E_AQC_TUNNEL_TYPE_VXLAN)
9330 goto not_found;
9331 break;
9332 case UDP_TUNNEL_TYPE_GENEVE:
9333 if (pf->udp_ports[idx].type != I40E_AQC_TUNNEL_TYPE_NGE)
9334 goto not_found;
9335 break;
9336 default:
9337 goto not_found;
Singhai, Anjali6a899022015-12-14 12:21:18 -08009338 }
9339
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009340 /* if port exists, set it to 0 (mark for deletion)
9341 * and make it pending
9342 */
Jacob Keller27826fd2017-04-19 09:25:50 -04009343 pf->udp_ports[idx].port = 0;
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009344 pf->pending_udp_bitmap |= BIT_ULL(idx);
Singhai, Anjali6a899022015-12-14 12:21:18 -08009345 pf->flags |= I40E_FLAG_UDP_FILTER_SYNC;
9346
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009347 return;
9348not_found:
9349 netdev_warn(netdev, "UDP port %d was not found, not deleting\n",
Jacob Kellerfe0b0cd2017-02-06 14:38:38 -08009350 port);
Singhai, Anjali6a899022015-12-14 12:21:18 -08009351}
9352
Neerav Parikh1f224ad2014-02-12 01:45:31 +00009353static int i40e_get_phys_port_id(struct net_device *netdev,
Jiri Pirko02637fc2014-11-28 14:34:16 +01009354 struct netdev_phys_item_id *ppid)
Neerav Parikh1f224ad2014-02-12 01:45:31 +00009355{
9356 struct i40e_netdev_priv *np = netdev_priv(netdev);
9357 struct i40e_pf *pf = np->vsi->back;
9358 struct i40e_hw *hw = &pf->hw;
9359
Jacob Kellerd36e41d2017-06-23 04:24:46 -04009360 if (!(pf->hw_features & I40E_HW_PORT_ID_VALID))
Neerav Parikh1f224ad2014-02-12 01:45:31 +00009361 return -EOPNOTSUPP;
9362
9363 ppid->id_len = min_t(int, sizeof(hw->mac.port_addr), sizeof(ppid->id));
9364 memcpy(ppid->id, hw->mac.port_addr, ppid->id_len);
9365
9366 return 0;
9367}
9368
Jesse Brandeburg2f90ade2014-11-20 16:30:02 -08009369/**
9370 * i40e_ndo_fdb_add - add an entry to the hardware database
9371 * @ndm: the input from the stack
9372 * @tb: pointer to array of nladdr (unused)
9373 * @dev: the net device pointer
9374 * @addr: the MAC address entry being added
9375 * @flags: instructions from stack about fdb operation
9376 */
Greg Rose4ba0dea2014-03-06 08:59:55 +00009377static int i40e_ndo_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
9378 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +01009379 const unsigned char *addr, u16 vid,
Greg Rose4ba0dea2014-03-06 08:59:55 +00009380 u16 flags)
Greg Rose4ba0dea2014-03-06 08:59:55 +00009381{
9382 struct i40e_netdev_priv *np = netdev_priv(dev);
9383 struct i40e_pf *pf = np->vsi->back;
9384 int err = 0;
9385
9386 if (!(pf->flags & I40E_FLAG_SRIOV_ENABLED))
9387 return -EOPNOTSUPP;
9388
Or Gerlitz65891fe2014-12-14 18:19:05 +02009389 if (vid) {
9390 pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
9391 return -EINVAL;
9392 }
9393
Greg Rose4ba0dea2014-03-06 08:59:55 +00009394 /* Hardware does not support aging addresses so if a
9395 * ndm_state is given only allow permanent addresses
9396 */
9397 if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
9398 netdev_info(dev, "FDB only supports static addresses\n");
9399 return -EINVAL;
9400 }
9401
9402 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
9403 err = dev_uc_add_excl(dev, addr);
9404 else if (is_multicast_ether_addr(addr))
9405 err = dev_mc_add_excl(dev, addr);
9406 else
9407 err = -EINVAL;
9408
9409 /* Only return duplicate errors if NLM_F_EXCL is set */
9410 if (err == -EEXIST && !(flags & NLM_F_EXCL))
9411 err = 0;
9412
9413 return err;
9414}
9415
Neerav Parikh51616012015-02-06 08:52:14 +00009416/**
9417 * i40e_ndo_bridge_setlink - Set the hardware bridge mode
9418 * @dev: the netdev being configured
9419 * @nlh: RTNL message
9420 *
9421 * Inserts a new hardware bridge if not already created and
9422 * enables the bridging mode requested (VEB or VEPA). If the
9423 * hardware bridge has already been inserted and the request
9424 * is to change the mode then that requires a PF reset to
9425 * allow rebuild of the components with required hardware
9426 * bridge mode enabled.
Maciej Sosin373149f2017-04-05 07:50:55 -04009427 *
9428 * Note: expects to be called while under rtnl_lock()
Neerav Parikh51616012015-02-06 08:52:14 +00009429 **/
9430static int i40e_ndo_bridge_setlink(struct net_device *dev,
Carolyn Wyborny9df70b62015-04-27 14:57:11 -04009431 struct nlmsghdr *nlh,
9432 u16 flags)
Neerav Parikh51616012015-02-06 08:52:14 +00009433{
9434 struct i40e_netdev_priv *np = netdev_priv(dev);
9435 struct i40e_vsi *vsi = np->vsi;
9436 struct i40e_pf *pf = vsi->back;
9437 struct i40e_veb *veb = NULL;
9438 struct nlattr *attr, *br_spec;
9439 int i, rem;
9440
9441 /* Only for PF VSI for now */
9442 if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
9443 return -EOPNOTSUPP;
9444
9445 /* Find the HW bridge for PF VSI */
9446 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
9447 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
9448 veb = pf->veb[i];
9449 }
9450
9451 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
9452
9453 nla_for_each_nested(attr, br_spec, rem) {
9454 __u16 mode;
9455
9456 if (nla_type(attr) != IFLA_BRIDGE_MODE)
9457 continue;
9458
9459 mode = nla_get_u16(attr);
9460 if ((mode != BRIDGE_MODE_VEPA) &&
9461 (mode != BRIDGE_MODE_VEB))
9462 return -EINVAL;
9463
9464 /* Insert a new HW bridge */
9465 if (!veb) {
9466 veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
9467 vsi->tc_config.enabled_tc);
9468 if (veb) {
9469 veb->bridge_mode = mode;
9470 i40e_config_bridge_mode(veb);
9471 } else {
9472 /* No Bridge HW offload available */
9473 return -ENOENT;
9474 }
9475 break;
9476 } else if (mode != veb->bridge_mode) {
9477 /* Existing HW bridge but different mode needs reset */
9478 veb->bridge_mode = mode;
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07009479 /* TODO: If no VFs or VMDq VSIs, disallow VEB mode */
9480 if (mode == BRIDGE_MODE_VEB)
9481 pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
9482 else
9483 pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
Maciej Sosin373149f2017-04-05 07:50:55 -04009484 i40e_do_reset(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED),
9485 true);
Neerav Parikh51616012015-02-06 08:52:14 +00009486 break;
9487 }
9488 }
9489
9490 return 0;
9491}
9492
9493/**
9494 * i40e_ndo_bridge_getlink - Get the hardware bridge mode
9495 * @skb: skb buff
9496 * @pid: process id
9497 * @seq: RTNL message seq #
9498 * @dev: the netdev being configured
9499 * @filter_mask: unused
Jesse Brandeburgd4b2f9f2015-09-03 17:18:48 -04009500 * @nlflags: netlink flags passed in
Neerav Parikh51616012015-02-06 08:52:14 +00009501 *
9502 * Return the mode in which the hardware bridge is operating in
9503 * i.e VEB or VEPA.
9504 **/
Neerav Parikh51616012015-02-06 08:52:14 +00009505static int i40e_ndo_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
9506 struct net_device *dev,
Carolyn Wyborny9f4ffc42015-08-31 19:54:42 -04009507 u32 __always_unused filter_mask,
9508 int nlflags)
Neerav Parikh51616012015-02-06 08:52:14 +00009509{
9510 struct i40e_netdev_priv *np = netdev_priv(dev);
9511 struct i40e_vsi *vsi = np->vsi;
9512 struct i40e_pf *pf = vsi->back;
9513 struct i40e_veb *veb = NULL;
9514 int i;
9515
9516 /* Only for PF VSI for now */
9517 if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
9518 return -EOPNOTSUPP;
9519
9520 /* Find the HW bridge for the PF VSI */
9521 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
9522 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
9523 veb = pf->veb[i];
9524 }
9525
9526 if (!veb)
9527 return 0;
9528
Nicolas Dichtel46c264d2015-04-28 18:33:49 +02009529 return ndo_dflt_bridge_getlink(skb, pid, seq, dev, veb->bridge_mode,
Huaibin Wang599b0762016-09-26 09:51:18 +02009530 0, 0, nlflags, filter_mask, NULL);
Neerav Parikh51616012015-02-06 08:52:14 +00009531}
Neerav Parikh51616012015-02-06 08:52:14 +00009532
Joe Stringerf44a75e2015-04-14 17:09:14 -07009533/**
9534 * i40e_features_check - Validate encapsulated packet conforms to limits
9535 * @skb: skb buff
Jean Sacren2bc11c62015-09-19 05:08:43 -06009536 * @dev: This physical port's netdev
Joe Stringerf44a75e2015-04-14 17:09:14 -07009537 * @features: Offload features that the stack believes apply
9538 **/
9539static netdev_features_t i40e_features_check(struct sk_buff *skb,
9540 struct net_device *dev,
9541 netdev_features_t features)
9542{
Alexander Duyckf114dca2016-10-25 16:08:46 -07009543 size_t len;
9544
9545 /* No point in doing any of this if neither checksum nor GSO are
9546 * being requested for this frame. We can rule out both by just
9547 * checking for CHECKSUM_PARTIAL
9548 */
9549 if (skb->ip_summed != CHECKSUM_PARTIAL)
9550 return features;
9551
9552 /* We cannot support GSO if the MSS is going to be less than
9553 * 64 bytes. If it is then we need to drop support for GSO.
9554 */
9555 if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
9556 features &= ~NETIF_F_GSO_MASK;
9557
9558 /* MACLEN can support at most 63 words */
9559 len = skb_network_header(skb) - skb->data;
9560 if (len & ~(63 * 2))
9561 goto out_err;
9562
9563 /* IPLEN and EIPLEN can support at most 127 dwords */
9564 len = skb_transport_header(skb) - skb_network_header(skb);
9565 if (len & ~(127 * 4))
9566 goto out_err;
9567
9568 if (skb->encapsulation) {
9569 /* L4TUNLEN can support 127 words */
9570 len = skb_inner_network_header(skb) - skb_transport_header(skb);
9571 if (len & ~(127 * 2))
9572 goto out_err;
9573
9574 /* IPLEN can support at most 127 dwords */
9575 len = skb_inner_transport_header(skb) -
9576 skb_inner_network_header(skb);
9577 if (len & ~(127 * 4))
9578 goto out_err;
9579 }
9580
9581 /* No need to validate L4LEN as TCP is the only protocol with a
9582 * a flexible value and we support all possible values supported
9583 * by TCP, which is at most 15 dwords
9584 */
Joe Stringerf44a75e2015-04-14 17:09:14 -07009585
9586 return features;
Alexander Duyckf114dca2016-10-25 16:08:46 -07009587out_err:
9588 return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
Joe Stringerf44a75e2015-04-14 17:09:14 -07009589}
9590
Björn Töpel0c8493d2017-05-24 07:55:34 +02009591/**
9592 * i40e_xdp_setup - add/remove an XDP program
9593 * @vsi: VSI to changed
9594 * @prog: XDP program
9595 **/
9596static int i40e_xdp_setup(struct i40e_vsi *vsi,
9597 struct bpf_prog *prog)
9598{
9599 int frame_size = vsi->netdev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
9600 struct i40e_pf *pf = vsi->back;
9601 struct bpf_prog *old_prog;
9602 bool need_reset;
9603 int i;
9604
9605 /* Don't allow frames that span over multiple buffers */
9606 if (frame_size > vsi->rx_buf_len)
9607 return -EINVAL;
9608
9609 if (!i40e_enabled_xdp_vsi(vsi) && !prog)
9610 return 0;
9611
9612 /* When turning XDP on->off/off->on we reset and rebuild the rings. */
9613 need_reset = (i40e_enabled_xdp_vsi(vsi) != !!prog);
9614
9615 if (need_reset)
9616 i40e_prep_for_reset(pf, true);
9617
9618 old_prog = xchg(&vsi->xdp_prog, prog);
9619
9620 if (need_reset)
9621 i40e_reset_and_rebuild(pf, true, true);
9622
9623 for (i = 0; i < vsi->num_queue_pairs; i++)
9624 WRITE_ONCE(vsi->rx_rings[i]->xdp_prog, vsi->xdp_prog);
9625
9626 if (old_prog)
9627 bpf_prog_put(old_prog);
9628
9629 return 0;
9630}
9631
9632/**
9633 * i40e_xdp - implements ndo_xdp for i40e
9634 * @dev: netdevice
9635 * @xdp: XDP command
9636 **/
9637static int i40e_xdp(struct net_device *dev,
9638 struct netdev_xdp *xdp)
9639{
9640 struct i40e_netdev_priv *np = netdev_priv(dev);
9641 struct i40e_vsi *vsi = np->vsi;
9642
9643 if (vsi->type != I40E_VSI_MAIN)
9644 return -EINVAL;
9645
9646 switch (xdp->command) {
9647 case XDP_SETUP_PROG:
9648 return i40e_xdp_setup(vsi, xdp->prog);
9649 case XDP_QUERY_PROG:
9650 xdp->prog_attached = i40e_enabled_xdp_vsi(vsi);
Daniel Borkmanneb230392017-06-24 21:13:52 +02009651 xdp->prog_id = vsi->xdp_prog ? vsi->xdp_prog->aux->id : 0;
Björn Töpel0c8493d2017-05-24 07:55:34 +02009652 return 0;
9653 default:
9654 return -EINVAL;
9655 }
9656}
9657
Shannon Nelson37a29732015-02-27 09:15:19 +00009658static const struct net_device_ops i40e_netdev_ops = {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009659 .ndo_open = i40e_open,
9660 .ndo_stop = i40e_close,
9661 .ndo_start_xmit = i40e_lan_xmit_frame,
9662 .ndo_get_stats64 = i40e_get_netdev_stats_struct,
9663 .ndo_set_rx_mode = i40e_set_rx_mode,
9664 .ndo_validate_addr = eth_validate_addr,
9665 .ndo_set_mac_address = i40e_set_mac,
9666 .ndo_change_mtu = i40e_change_mtu,
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00009667 .ndo_do_ioctl = i40e_ioctl,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009668 .ndo_tx_timeout = i40e_tx_timeout,
9669 .ndo_vlan_rx_add_vid = i40e_vlan_rx_add_vid,
9670 .ndo_vlan_rx_kill_vid = i40e_vlan_rx_kill_vid,
9671#ifdef CONFIG_NET_POLL_CONTROLLER
9672 .ndo_poll_controller = i40e_netpoll,
9673#endif
John Fastabende4c67342016-02-16 21:16:15 -08009674 .ndo_setup_tc = __i40e_setup_tc,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009675 .ndo_set_features = i40e_set_features,
9676 .ndo_set_vf_mac = i40e_ndo_set_vf_mac,
9677 .ndo_set_vf_vlan = i40e_ndo_set_vf_port_vlan,
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04009678 .ndo_set_vf_rate = i40e_ndo_set_vf_bw,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009679 .ndo_get_vf_config = i40e_ndo_get_vf_config,
Mitch Williams588aefa2014-02-11 08:27:49 +00009680 .ndo_set_vf_link_state = i40e_ndo_set_vf_link_state,
Serey Konge6d90042014-07-12 07:28:14 +00009681 .ndo_set_vf_spoofchk = i40e_ndo_set_vf_spoofchk,
Anjali Singhai Jainc3bbbd22016-04-01 03:56:07 -07009682 .ndo_set_vf_trust = i40e_ndo_set_vf_trust,
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009683 .ndo_udp_tunnel_add = i40e_udp_tunnel_add,
9684 .ndo_udp_tunnel_del = i40e_udp_tunnel_del,
Neerav Parikh1f224ad2014-02-12 01:45:31 +00009685 .ndo_get_phys_port_id = i40e_get_phys_port_id,
Greg Rose4ba0dea2014-03-06 08:59:55 +00009686 .ndo_fdb_add = i40e_ndo_fdb_add,
Joe Stringerf44a75e2015-04-14 17:09:14 -07009687 .ndo_features_check = i40e_features_check,
Neerav Parikh51616012015-02-06 08:52:14 +00009688 .ndo_bridge_getlink = i40e_ndo_bridge_getlink,
9689 .ndo_bridge_setlink = i40e_ndo_bridge_setlink,
Björn Töpel0c8493d2017-05-24 07:55:34 +02009690 .ndo_xdp = i40e_xdp,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009691};
9692
9693/**
9694 * i40e_config_netdev - Setup the netdev flags
9695 * @vsi: the VSI being configured
9696 *
9697 * Returns 0 on success, negative value on failure
9698 **/
9699static int i40e_config_netdev(struct i40e_vsi *vsi)
9700{
9701 struct i40e_pf *pf = vsi->back;
9702 struct i40e_hw *hw = &pf->hw;
9703 struct i40e_netdev_priv *np;
9704 struct net_device *netdev;
Jacob Keller435c0842016-11-08 13:05:10 -08009705 u8 broadcast[ETH_ALEN];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009706 u8 mac_addr[ETH_ALEN];
9707 int etherdev_size;
Preethi Banalabacd75c2017-03-27 14:43:18 -07009708 netdev_features_t hw_enc_features;
9709 netdev_features_t hw_features;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009710
9711 etherdev_size = sizeof(struct i40e_netdev_priv);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00009712 netdev = alloc_etherdev_mq(etherdev_size, vsi->alloc_queue_pairs);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009713 if (!netdev)
9714 return -ENOMEM;
9715
9716 vsi->netdev = netdev;
9717 np = netdev_priv(netdev);
9718 np->vsi = vsi;
9719
Preethi Banalabacd75c2017-03-27 14:43:18 -07009720 hw_enc_features = NETIF_F_SG |
9721 NETIF_F_IP_CSUM |
9722 NETIF_F_IPV6_CSUM |
9723 NETIF_F_HIGHDMA |
9724 NETIF_F_SOFT_FEATURES |
9725 NETIF_F_TSO |
9726 NETIF_F_TSO_ECN |
9727 NETIF_F_TSO6 |
9728 NETIF_F_GSO_GRE |
9729 NETIF_F_GSO_GRE_CSUM |
9730 NETIF_F_GSO_PARTIAL |
9731 NETIF_F_GSO_UDP_TUNNEL |
9732 NETIF_F_GSO_UDP_TUNNEL_CSUM |
9733 NETIF_F_SCTP_CRC |
9734 NETIF_F_RXHASH |
9735 NETIF_F_RXCSUM |
9736 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009737
Jacob Kellerd36e41d2017-06-23 04:24:46 -04009738 if (!(pf->hw_features & I40E_HW_OUTER_UDP_CSUM_CAPABLE))
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04009739 netdev->gso_partial_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM;
9740
9741 netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009742
Preethi Banalabacd75c2017-03-27 14:43:18 -07009743 netdev->hw_enc_features |= hw_enc_features;
9744
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009745 /* record features VLANs can make use of */
Preethi Banalabacd75c2017-03-27 14:43:18 -07009746 netdev->vlan_features |= hw_enc_features | NETIF_F_TSO_MANGLEID;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009747
Anjali Singhai Jain2e86a0b2014-04-01 07:11:53 +00009748 if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009749 netdev->hw_features |= NETIF_F_NTUPLE;
Preethi Banalabacd75c2017-03-27 14:43:18 -07009750 hw_features = hw_enc_features |
9751 NETIF_F_HW_VLAN_CTAG_TX |
9752 NETIF_F_HW_VLAN_CTAG_RX;
Anjali Singhai Jain2e86a0b2014-04-01 07:11:53 +00009753
Preethi Banalabacd75c2017-03-27 14:43:18 -07009754 netdev->hw_features |= hw_features;
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009755
Preethi Banalabacd75c2017-03-27 14:43:18 -07009756 netdev->features |= hw_features | NETIF_F_HW_VLAN_CTAG_FILTER;
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04009757 netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009758
9759 if (vsi->type == I40E_VSI_MAIN) {
9760 SET_NETDEV_DEV(netdev, &pf->pdev->dev);
Greg Rose9a173902014-05-22 06:32:02 +00009761 ether_addr_copy(mac_addr, hw->mac.perm_addr);
Jacob Keller41c4c2b2017-04-05 07:50:57 -04009762 /* The following steps are necessary for two reasons. First,
9763 * some older NVM configurations load a default MAC-VLAN
9764 * filter that will accept any tagged packet, and we want to
9765 * replace this with a normal filter. Additionally, it is
9766 * possible our MAC address was provided by the platform using
9767 * Open Firmware or similar.
9768 *
9769 * Thus, we need to remove the default filter and install one
9770 * specific to the MAC address.
Jacob Keller1596b5d2016-11-08 13:05:15 -08009771 */
9772 i40e_rm_default_mac_filter(vsi, mac_addr);
Jacob Keller278e7d02016-10-05 09:30:37 -07009773 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller9569a9a2016-11-11 12:39:37 -08009774 i40e_add_mac_filter(vsi, mac_addr);
Jacob Keller278e7d02016-10-05 09:30:37 -07009775 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009776 } else {
Jacob Keller8c9eb352017-07-12 05:46:12 -04009777 /* Relate the VSI_VMDQ name to the VSI_MAIN name. Note that we
9778 * are still limited by IFNAMSIZ, but we're adding 'v%d\0' to
9779 * the end, which is 4 bytes long, so force truncation of the
9780 * original name by IFNAMSIZ - 4
9781 */
9782 snprintf(netdev->name, IFNAMSIZ, "%.*sv%%d",
9783 IFNAMSIZ - 4,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009784 pf->vsi[pf->lan_vsi]->netdev->name);
9785 random_ether_addr(mac_addr);
Kiran Patil21659032015-09-30 14:09:03 -04009786
Jacob Keller278e7d02016-10-05 09:30:37 -07009787 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller9569a9a2016-11-11 12:39:37 -08009788 i40e_add_mac_filter(vsi, mac_addr);
Jacob Keller278e7d02016-10-05 09:30:37 -07009789 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009790 }
Kiran Patil21659032015-09-30 14:09:03 -04009791
Jacob Keller435c0842016-11-08 13:05:10 -08009792 /* Add the broadcast filter so that we initially will receive
9793 * broadcast packets. Note that when a new VLAN is first added the
9794 * driver will convert all filters marked I40E_VLAN_ANY into VLAN
9795 * specific filters as part of transitioning into "vlan" operation.
9796 * When more VLANs are added, the driver will copy each existing MAC
9797 * filter and add it for the new VLAN.
9798 *
9799 * Broadcast filters are handled specially by
9800 * i40e_sync_filters_subtask, as the driver must to set the broadcast
9801 * promiscuous bit instead of adding this directly as a MAC/VLAN
9802 * filter. The subtask will update the correct broadcast promiscuous
9803 * bits as VLANs become active or inactive.
9804 */
9805 eth_broadcast_addr(broadcast);
9806 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller9569a9a2016-11-11 12:39:37 -08009807 i40e_add_mac_filter(vsi, broadcast);
Jacob Keller435c0842016-11-08 13:05:10 -08009808 spin_unlock_bh(&vsi->mac_filter_hash_lock);
9809
Greg Rose9a173902014-05-22 06:32:02 +00009810 ether_addr_copy(netdev->dev_addr, mac_addr);
9811 ether_addr_copy(netdev->perm_addr, mac_addr);
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009812
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009813 netdev->priv_flags |= IFF_UNICAST_FLT;
9814 netdev->priv_flags |= IFF_SUPP_NOFCS;
9815 /* Setup netdev TC information */
9816 i40e_vsi_config_netdev_tc(vsi, vsi->tc_config.enabled_tc);
9817
9818 netdev->netdev_ops = &i40e_netdev_ops;
9819 netdev->watchdog_timeo = 5 * HZ;
9820 i40e_set_ethtool_ops(netdev);
9821
Jarod Wilson91c527a2016-10-17 15:54:05 -04009822 /* MTU range: 68 - 9706 */
9823 netdev->min_mtu = ETH_MIN_MTU;
Mitch Williams1e3a5fd2017-06-23 04:24:43 -04009824 netdev->max_mtu = I40E_MAX_RXBUFFER - I40E_PACKET_HDR_PAD;
Jarod Wilson91c527a2016-10-17 15:54:05 -04009825
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009826 return 0;
9827}
9828
9829/**
9830 * i40e_vsi_delete - Delete a VSI from the switch
9831 * @vsi: the VSI being removed
9832 *
9833 * Returns 0 on success, negative value on failure
9834 **/
9835static void i40e_vsi_delete(struct i40e_vsi *vsi)
9836{
9837 /* remove default VSI is not allowed */
9838 if (vsi == vsi->back->vsi[vsi->back->lan_vsi])
9839 return;
9840
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009841 i40e_aq_delete_element(&vsi->back->hw, vsi->seid, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009842}
9843
9844/**
Neerav Parikh51616012015-02-06 08:52:14 +00009845 * i40e_is_vsi_uplink_mode_veb - Check if the VSI's uplink bridge mode is VEB
9846 * @vsi: the VSI being queried
9847 *
9848 * Returns 1 if HW bridge mode is VEB and return 0 in case of VEPA mode
9849 **/
9850int i40e_is_vsi_uplink_mode_veb(struct i40e_vsi *vsi)
9851{
9852 struct i40e_veb *veb;
9853 struct i40e_pf *pf = vsi->back;
9854
9855 /* Uplink is not a bridge so default to VEB */
9856 if (vsi->veb_idx == I40E_NO_VEB)
9857 return 1;
9858
9859 veb = pf->veb[vsi->veb_idx];
Akeem G Abodunrin09603ea2015-10-01 14:37:36 -04009860 if (!veb) {
9861 dev_info(&pf->pdev->dev,
9862 "There is no veb associated with the bridge\n");
9863 return -ENOENT;
9864 }
Neerav Parikh51616012015-02-06 08:52:14 +00009865
Akeem G Abodunrin09603ea2015-10-01 14:37:36 -04009866 /* Uplink is a bridge in VEPA mode */
9867 if (veb->bridge_mode & BRIDGE_MODE_VEPA) {
9868 return 0;
9869 } else {
9870 /* Uplink is a bridge in VEB mode */
9871 return 1;
9872 }
9873
9874 /* VEPA is now default bridge, so return 0 */
9875 return 0;
Neerav Parikh51616012015-02-06 08:52:14 +00009876}
9877
9878/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009879 * i40e_add_vsi - Add a VSI to the switch
9880 * @vsi: the VSI being configured
9881 *
9882 * This initializes a VSI context depending on the VSI type to be added and
9883 * passes it down to the add_vsi aq command.
9884 **/
9885static int i40e_add_vsi(struct i40e_vsi *vsi)
9886{
9887 int ret = -ENODEV;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009888 struct i40e_pf *pf = vsi->back;
9889 struct i40e_hw *hw = &pf->hw;
9890 struct i40e_vsi_context ctxt;
Jacob Keller278e7d02016-10-05 09:30:37 -07009891 struct i40e_mac_filter *f;
9892 struct hlist_node *h;
9893 int bkt;
Kiran Patil21659032015-09-30 14:09:03 -04009894
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009895 u8 enabled_tc = 0x1; /* TC0 enabled */
9896 int f_count = 0;
9897
9898 memset(&ctxt, 0, sizeof(ctxt));
9899 switch (vsi->type) {
9900 case I40E_VSI_MAIN:
9901 /* The PF's main VSI is already setup as part of the
9902 * device initialization, so we'll not bother with
9903 * the add_vsi call, but we will retrieve the current
9904 * VSI context.
9905 */
9906 ctxt.seid = pf->main_vsi_seid;
9907 ctxt.pf_num = pf->hw.pf_id;
9908 ctxt.vf_num = 0;
9909 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
9910 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
9911 if (ret) {
9912 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009913 "couldn't get PF vsi config, err %s aq_err %s\n",
9914 i40e_stat_str(&pf->hw, ret),
9915 i40e_aq_str(&pf->hw,
9916 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009917 return -ENOENT;
9918 }
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07009919 vsi->info = ctxt.info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009920 vsi->info.valid_sections = 0;
9921
9922 vsi->seid = ctxt.seid;
9923 vsi->id = ctxt.vsi_number;
9924
9925 enabled_tc = i40e_pf_get_tc_map(pf);
9926
Mitch Williams64615b52017-08-29 05:32:30 -04009927 /* Source pruning is enabled by default, so the flag is
9928 * negative logic - if it's set, we need to fiddle with
9929 * the VSI to disable source pruning.
9930 */
9931 if (pf->flags & I40E_FLAG_SOURCE_PRUNING_DISABLED) {
9932 memset(&ctxt, 0, sizeof(ctxt));
9933 ctxt.seid = pf->main_vsi_seid;
9934 ctxt.pf_num = pf->hw.pf_id;
9935 ctxt.vf_num = 0;
9936 ctxt.info.valid_sections |=
9937 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
9938 ctxt.info.switch_id =
9939 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_LOCAL_LB);
9940 ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
9941 if (ret) {
9942 dev_info(&pf->pdev->dev,
9943 "update vsi failed, err %s aq_err %s\n",
9944 i40e_stat_str(&pf->hw, ret),
9945 i40e_aq_str(&pf->hw,
9946 pf->hw.aq.asq_last_status));
9947 ret = -ENOENT;
9948 goto err;
9949 }
9950 }
9951
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009952 /* MFP mode setup queue map and update VSI */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00009953 if ((pf->flags & I40E_FLAG_MFP_ENABLED) &&
9954 !(pf->hw.func_caps.iscsi)) { /* NIC type PF */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009955 memset(&ctxt, 0, sizeof(ctxt));
9956 ctxt.seid = pf->main_vsi_seid;
9957 ctxt.pf_num = pf->hw.pf_id;
9958 ctxt.vf_num = 0;
9959 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
9960 ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
9961 if (ret) {
9962 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009963 "update vsi failed, err %s aq_err %s\n",
9964 i40e_stat_str(&pf->hw, ret),
9965 i40e_aq_str(&pf->hw,
9966 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009967 ret = -ENOENT;
9968 goto err;
9969 }
9970 /* update the local VSI info queue map */
9971 i40e_vsi_update_queue_map(vsi, &ctxt);
9972 vsi->info.valid_sections = 0;
9973 } else {
9974 /* Default/Main VSI is only enabled for TC0
9975 * reconfigure it to enable all TCs that are
9976 * available on the port in SFP mode.
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00009977 * For MFP case the iSCSI PF would use this
9978 * flow to enable LAN+iSCSI TC.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009979 */
9980 ret = i40e_vsi_config_tc(vsi, enabled_tc);
9981 if (ret) {
Carolyn Wyborny19279232017-07-14 09:10:07 -04009982 /* Single TC condition is not fatal,
9983 * message and continue
9984 */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009985 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009986 "failed to configure TCs for main VSI tc_map 0x%08x, err %s aq_err %s\n",
9987 enabled_tc,
9988 i40e_stat_str(&pf->hw, ret),
9989 i40e_aq_str(&pf->hw,
9990 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009991 }
9992 }
9993 break;
9994
9995 case I40E_VSI_FDIR:
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009996 ctxt.pf_num = hw->pf_id;
9997 ctxt.vf_num = 0;
9998 ctxt.uplink_seid = vsi->uplink_seid;
Neerav Parikh2b18e592015-01-24 09:58:38 +00009999 ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010000 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
Anjali Singhai Jainfc608612015-05-08 15:35:57 -070010001 if ((pf->flags & I40E_FLAG_VEB_MODE_ENABLED) &&
10002 (i40e_is_vsi_uplink_mode_veb(vsi))) {
Neerav Parikh51616012015-02-06 08:52:14 +000010003 ctxt.info.valid_sections |=
Anjali Singhai Jainfc608612015-05-08 15:35:57 -070010004 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
Neerav Parikh51616012015-02-06 08:52:14 +000010005 ctxt.info.switch_id =
Anjali Singhai Jainfc608612015-05-08 15:35:57 -070010006 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
Neerav Parikh51616012015-02-06 08:52:14 +000010007 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010008 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010009 break;
10010
10011 case I40E_VSI_VMDQ2:
10012 ctxt.pf_num = hw->pf_id;
10013 ctxt.vf_num = 0;
10014 ctxt.uplink_seid = vsi->uplink_seid;
Neerav Parikh2b18e592015-01-24 09:58:38 +000010015 ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010016 ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
10017
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010018 /* This VSI is connected to VEB so the switch_id
10019 * should be set to zero by default.
10020 */
Neerav Parikh51616012015-02-06 08:52:14 +000010021 if (i40e_is_vsi_uplink_mode_veb(vsi)) {
10022 ctxt.info.valid_sections |=
10023 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
10024 ctxt.info.switch_id =
10025 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
10026 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010027
10028 /* Setup the VSI tx/rx queue map for TC0 only for now */
10029 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
10030 break;
10031
10032 case I40E_VSI_SRIOV:
10033 ctxt.pf_num = hw->pf_id;
10034 ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
10035 ctxt.uplink_seid = vsi->uplink_seid;
Neerav Parikh2b18e592015-01-24 09:58:38 +000010036 ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010037 ctxt.flags = I40E_AQ_VSI_TYPE_VF;
10038
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010039 /* This VSI is connected to VEB so the switch_id
10040 * should be set to zero by default.
10041 */
Neerav Parikh51616012015-02-06 08:52:14 +000010042 if (i40e_is_vsi_uplink_mode_veb(vsi)) {
10043 ctxt.info.valid_sections |=
10044 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
10045 ctxt.info.switch_id =
10046 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
10047 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010048
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060010049 if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
10050 ctxt.info.valid_sections |=
10051 cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
10052 ctxt.info.queueing_opt_flags |=
Ashish Shah4b28cdb2016-05-03 15:13:17 -070010053 (I40E_AQ_VSI_QUE_OPT_TCP_ENA |
10054 I40E_AQ_VSI_QUE_OPT_RSS_LUT_VSI);
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060010055 }
10056
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010057 ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
10058 ctxt.info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_MODE_ALL;
Mitch Williamsc674d122014-05-20 08:01:40 +000010059 if (pf->vf[vsi->vf_id].spoofchk) {
10060 ctxt.info.valid_sections |=
10061 cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
10062 ctxt.info.sec_flags |=
10063 (I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
10064 I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
10065 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010066 /* Setup the VSI tx/rx queue map for TC0 only for now */
10067 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
10068 break;
10069
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060010070 case I40E_VSI_IWARP:
10071 /* send down message to iWARP */
10072 break;
10073
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010074 default:
10075 return -ENODEV;
10076 }
10077
10078 if (vsi->type != I40E_VSI_MAIN) {
10079 ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
10080 if (ret) {
10081 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010082 "add vsi failed, err %s aq_err %s\n",
10083 i40e_stat_str(&pf->hw, ret),
10084 i40e_aq_str(&pf->hw,
10085 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010086 ret = -ENOENT;
10087 goto err;
10088 }
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -070010089 vsi->info = ctxt.info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010090 vsi->info.valid_sections = 0;
10091 vsi->seid = ctxt.seid;
10092 vsi->id = ctxt.vsi_number;
10093 }
10094
Mitch Williamsc3c7ea22016-06-20 09:10:38 -070010095 vsi->active_filters = 0;
Jacob Keller0da36b92017-04-19 09:25:55 -040010096 clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
Jacob Keller278e7d02016-10-05 09:30:37 -070010097 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010098 /* If macvlan filters already exist, force them to get loaded */
Jacob Keller278e7d02016-10-05 09:30:37 -070010099 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -070010100 f->state = I40E_FILTER_NEW;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010101 f_count++;
10102 }
Jacob Keller278e7d02016-10-05 09:30:37 -070010103 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -040010104
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010105 if (f_count) {
10106 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
10107 pf->flags |= I40E_FLAG_FILTER_SYNC;
10108 }
10109
10110 /* Update VSI BW information */
10111 ret = i40e_vsi_get_bw_info(vsi);
10112 if (ret) {
10113 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010114 "couldn't get vsi bw info, err %s aq_err %s\n",
10115 i40e_stat_str(&pf->hw, ret),
10116 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010117 /* VSI is already added so not tearing that up */
10118 ret = 0;
10119 }
10120
10121err:
10122 return ret;
10123}
10124
10125/**
10126 * i40e_vsi_release - Delete a VSI and free its resources
10127 * @vsi: the VSI being removed
10128 *
10129 * Returns 0 on success or < 0 on error
10130 **/
10131int i40e_vsi_release(struct i40e_vsi *vsi)
10132{
Jacob Keller278e7d02016-10-05 09:30:37 -070010133 struct i40e_mac_filter *f;
10134 struct hlist_node *h;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010135 struct i40e_veb *veb = NULL;
10136 struct i40e_pf *pf;
10137 u16 uplink_seid;
Jacob Keller278e7d02016-10-05 09:30:37 -070010138 int i, n, bkt;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010139
10140 pf = vsi->back;
10141
10142 /* release of a VEB-owner or last VSI is not allowed */
10143 if (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) {
10144 dev_info(&pf->pdev->dev, "VSI %d has existing VEB %d\n",
10145 vsi->seid, vsi->uplink_seid);
10146 return -ENODEV;
10147 }
10148 if (vsi == pf->vsi[pf->lan_vsi] &&
Mauro S. M. Rodrigues9e6c9c02017-05-12 23:26:56 -030010149 !test_bit(__I40E_DOWN, pf->state)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010150 dev_info(&pf->pdev->dev, "Can't remove PF VSI\n");
10151 return -ENODEV;
10152 }
10153
10154 uplink_seid = vsi->uplink_seid;
10155 if (vsi->type != I40E_VSI_SRIOV) {
10156 if (vsi->netdev_registered) {
10157 vsi->netdev_registered = false;
10158 if (vsi->netdev) {
10159 /* results in a call to i40e_close() */
10160 unregister_netdev(vsi->netdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010161 }
10162 } else {
Shannon Nelson90ef8d42014-03-14 07:32:26 +000010163 i40e_vsi_close(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010164 }
10165 i40e_vsi_disable_irq(vsi);
10166 }
10167
Jacob Keller278e7d02016-10-05 09:30:37 -070010168 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller6622f5c2016-10-05 09:30:32 -070010169
10170 /* clear the sync flag on all filters */
10171 if (vsi->netdev) {
10172 __dev_uc_unsync(vsi->netdev, NULL);
10173 __dev_mc_unsync(vsi->netdev, NULL);
10174 }
10175
10176 /* make sure any remaining filters are marked for deletion */
Jacob Keller278e7d02016-10-05 09:30:37 -070010177 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
Jacob Keller290d2552016-10-05 09:30:36 -070010178 __i40e_del_filter(vsi, f);
Jacob Keller6622f5c2016-10-05 09:30:32 -070010179
Jacob Keller278e7d02016-10-05 09:30:37 -070010180 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -040010181
Jesse Brandeburg17652c62015-11-05 17:01:02 -080010182 i40e_sync_vsi_filters(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010183
10184 i40e_vsi_delete(vsi);
10185 i40e_vsi_free_q_vectors(vsi);
Shannon Nelsona4866592014-02-11 08:24:07 +000010186 if (vsi->netdev) {
10187 free_netdev(vsi->netdev);
10188 vsi->netdev = NULL;
10189 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010190 i40e_vsi_clear_rings(vsi);
10191 i40e_vsi_clear(vsi);
10192
10193 /* If this was the last thing on the VEB, except for the
10194 * controlling VSI, remove the VEB, which puts the controlling
10195 * VSI onto the next level down in the switch.
10196 *
10197 * Well, okay, there's one more exception here: don't remove
10198 * the orphan VEBs yet. We'll wait for an explicit remove request
10199 * from up the network stack.
10200 */
Mitch Williams505682c2014-05-20 08:01:37 +000010201 for (n = 0, i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010202 if (pf->vsi[i] &&
10203 pf->vsi[i]->uplink_seid == uplink_seid &&
10204 (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
10205 n++; /* count the VSIs */
10206 }
10207 }
10208 for (i = 0; i < I40E_MAX_VEB; i++) {
10209 if (!pf->veb[i])
10210 continue;
10211 if (pf->veb[i]->uplink_seid == uplink_seid)
10212 n++; /* count the VEBs */
10213 if (pf->veb[i]->seid == uplink_seid)
10214 veb = pf->veb[i];
10215 }
10216 if (n == 0 && veb && veb->uplink_seid != 0)
10217 i40e_veb_release(veb);
10218
10219 return 0;
10220}
10221
10222/**
10223 * i40e_vsi_setup_vectors - Set up the q_vectors for the given VSI
10224 * @vsi: ptr to the VSI
10225 *
10226 * This should only be called after i40e_vsi_mem_alloc() which allocates the
10227 * corresponding SW VSI structure and initializes num_queue_pairs for the
10228 * newly allocated VSI.
10229 *
10230 * Returns 0 on success or negative on failure
10231 **/
10232static int i40e_vsi_setup_vectors(struct i40e_vsi *vsi)
10233{
10234 int ret = -ENOENT;
10235 struct i40e_pf *pf = vsi->back;
10236
Alexander Duyck493fb302013-09-28 07:01:44 +000010237 if (vsi->q_vectors[0]) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010238 dev_info(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
10239 vsi->seid);
10240 return -EEXIST;
10241 }
10242
10243 if (vsi->base_vector) {
Jesse Brandeburgf29eaa32014-02-11 08:24:12 +000010244 dev_info(&pf->pdev->dev, "VSI %d has non-zero base vector %d\n",
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010245 vsi->seid, vsi->base_vector);
10246 return -EEXIST;
10247 }
10248
Greg Rose90e04072014-03-06 08:59:57 +000010249 ret = i40e_vsi_alloc_q_vectors(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010250 if (ret) {
10251 dev_info(&pf->pdev->dev,
10252 "failed to allocate %d q_vector for VSI %d, ret=%d\n",
10253 vsi->num_q_vectors, vsi->seid, ret);
10254 vsi->num_q_vectors = 0;
10255 goto vector_setup_out;
10256 }
10257
Anjali Singhai Jain26cdc442015-07-10 19:36:00 -040010258 /* In Legacy mode, we do not have to get any other vector since we
10259 * piggyback on the misc/ICR0 for queue interrupts.
10260 */
10261 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
10262 return ret;
Shannon Nelson958a3e32013-09-28 07:13:28 +000010263 if (vsi->num_q_vectors)
10264 vsi->base_vector = i40e_get_lump(pf, pf->irq_pile,
10265 vsi->num_q_vectors, vsi->idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010266 if (vsi->base_vector < 0) {
10267 dev_info(&pf->pdev->dev,
Shannon Nelson049a2be2014-10-17 03:14:50 +000010268 "failed to get tracking for %d vectors for VSI %d, err=%d\n",
10269 vsi->num_q_vectors, vsi->seid, vsi->base_vector);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010270 i40e_vsi_free_q_vectors(vsi);
10271 ret = -ENOENT;
10272 goto vector_setup_out;
10273 }
10274
10275vector_setup_out:
10276 return ret;
10277}
10278
10279/**
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010280 * i40e_vsi_reinit_setup - return and reallocate resources for a VSI
10281 * @vsi: pointer to the vsi.
10282 *
10283 * This re-allocates a vsi's queue resources.
10284 *
10285 * Returns pointer to the successfully allocated and configured VSI sw struct
10286 * on success, otherwise returns NULL on failure.
10287 **/
10288static struct i40e_vsi *i40e_vsi_reinit_setup(struct i40e_vsi *vsi)
10289{
Björn Töpel74608d12017-05-24 07:55:35 +020010290 u16 alloc_queue_pairs;
John Underwoodf5340392016-02-18 09:19:24 -080010291 struct i40e_pf *pf;
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010292 u8 enabled_tc;
10293 int ret;
10294
John Underwoodf5340392016-02-18 09:19:24 -080010295 if (!vsi)
10296 return NULL;
10297
10298 pf = vsi->back;
10299
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010300 i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
10301 i40e_vsi_clear_rings(vsi);
10302
10303 i40e_vsi_free_arrays(vsi, false);
10304 i40e_set_num_rings_in_vsi(vsi);
10305 ret = i40e_vsi_alloc_arrays(vsi, false);
10306 if (ret)
10307 goto err_vsi;
10308
Björn Töpel74608d12017-05-24 07:55:35 +020010309 alloc_queue_pairs = vsi->alloc_queue_pairs *
10310 (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
10311
10312 ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010313 if (ret < 0) {
Shannon Nelson049a2be2014-10-17 03:14:50 +000010314 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010315 "failed to get tracking for %d queues for VSI %d err %d\n",
Björn Töpel74608d12017-05-24 07:55:35 +020010316 alloc_queue_pairs, vsi->seid, ret);
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010317 goto err_vsi;
10318 }
10319 vsi->base_queue = ret;
10320
10321 /* Update the FW view of the VSI. Force a reset of TC and queue
10322 * layout configurations.
10323 */
10324 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
10325 pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
10326 pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
10327 i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
Jacob Keller1596b5d2016-11-08 13:05:15 -080010328 if (vsi->type == I40E_VSI_MAIN)
10329 i40e_rm_default_mac_filter(vsi, pf->hw.mac.perm_addr);
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010330
10331 /* assign it some queues */
10332 ret = i40e_alloc_rings(vsi);
10333 if (ret)
10334 goto err_rings;
10335
10336 /* map all of the rings to the q_vectors */
10337 i40e_vsi_map_rings_to_vectors(vsi);
10338 return vsi;
10339
10340err_rings:
10341 i40e_vsi_free_q_vectors(vsi);
10342 if (vsi->netdev_registered) {
10343 vsi->netdev_registered = false;
10344 unregister_netdev(vsi->netdev);
10345 free_netdev(vsi->netdev);
10346 vsi->netdev = NULL;
10347 }
10348 i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
10349err_vsi:
10350 i40e_vsi_clear(vsi);
10351 return NULL;
10352}
10353
10354/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010355 * i40e_vsi_setup - Set up a VSI by a given type
10356 * @pf: board private structure
10357 * @type: VSI type
10358 * @uplink_seid: the switch element to link to
10359 * @param1: usage depends upon VSI type. For VF types, indicates VF id
10360 *
10361 * This allocates the sw VSI structure and its queue resources, then add a VSI
10362 * to the identified VEB.
10363 *
10364 * Returns pointer to the successfully allocated and configure VSI sw struct on
10365 * success, otherwise returns NULL on failure.
10366 **/
10367struct i40e_vsi *i40e_vsi_setup(struct i40e_pf *pf, u8 type,
10368 u16 uplink_seid, u32 param1)
10369{
10370 struct i40e_vsi *vsi = NULL;
10371 struct i40e_veb *veb = NULL;
Björn Töpel74608d12017-05-24 07:55:35 +020010372 u16 alloc_queue_pairs;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010373 int ret, i;
10374 int v_idx;
10375
10376 /* The requested uplink_seid must be either
10377 * - the PF's port seid
10378 * no VEB is needed because this is the PF
10379 * or this is a Flow Director special case VSI
10380 * - seid of an existing VEB
10381 * - seid of a VSI that owns an existing VEB
10382 * - seid of a VSI that doesn't own a VEB
10383 * a new VEB is created and the VSI becomes the owner
10384 * - seid of the PF VSI, which is what creates the first VEB
10385 * this is a special case of the previous
10386 *
10387 * Find which uplink_seid we were given and create a new VEB if needed
10388 */
10389 for (i = 0; i < I40E_MAX_VEB; i++) {
10390 if (pf->veb[i] && pf->veb[i]->seid == uplink_seid) {
10391 veb = pf->veb[i];
10392 break;
10393 }
10394 }
10395
10396 if (!veb && uplink_seid != pf->mac_seid) {
10397
Mitch Williams505682c2014-05-20 08:01:37 +000010398 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010399 if (pf->vsi[i] && pf->vsi[i]->seid == uplink_seid) {
10400 vsi = pf->vsi[i];
10401 break;
10402 }
10403 }
10404 if (!vsi) {
10405 dev_info(&pf->pdev->dev, "no such uplink_seid %d\n",
10406 uplink_seid);
10407 return NULL;
10408 }
10409
10410 if (vsi->uplink_seid == pf->mac_seid)
10411 veb = i40e_veb_setup(pf, 0, pf->mac_seid, vsi->seid,
10412 vsi->tc_config.enabled_tc);
10413 else if ((vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
10414 veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
10415 vsi->tc_config.enabled_tc);
Anjali Singhai Jain79c21a82014-11-13 03:06:14 +000010416 if (veb) {
10417 if (vsi->seid != pf->vsi[pf->lan_vsi]->seid) {
10418 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -040010419 "New VSI creation error, uplink seid of LAN VSI expected.\n");
Anjali Singhai Jain79c21a82014-11-13 03:06:14 +000010420 return NULL;
10421 }
Anjali Singhai Jainfa11cb32015-05-27 12:06:14 -040010422 /* We come up by default in VEPA mode if SRIOV is not
10423 * already enabled, in which case we can't force VEPA
10424 * mode.
10425 */
10426 if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
10427 veb->bridge_mode = BRIDGE_MODE_VEPA;
10428 pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
10429 }
Neerav Parikh51616012015-02-06 08:52:14 +000010430 i40e_config_bridge_mode(veb);
Anjali Singhai Jain79c21a82014-11-13 03:06:14 +000010431 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010432 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
10433 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
10434 veb = pf->veb[i];
10435 }
10436 if (!veb) {
10437 dev_info(&pf->pdev->dev, "couldn't add VEB\n");
10438 return NULL;
10439 }
10440
10441 vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
10442 uplink_seid = veb->seid;
10443 }
10444
10445 /* get vsi sw struct */
10446 v_idx = i40e_vsi_mem_alloc(pf, type);
10447 if (v_idx < 0)
10448 goto err_alloc;
10449 vsi = pf->vsi[v_idx];
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010450 if (!vsi)
10451 goto err_alloc;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010452 vsi->type = type;
10453 vsi->veb_idx = (veb ? veb->idx : I40E_NO_VEB);
10454
10455 if (type == I40E_VSI_MAIN)
10456 pf->lan_vsi = v_idx;
10457 else if (type == I40E_VSI_SRIOV)
10458 vsi->vf_id = param1;
10459 /* assign it some queues */
Björn Töpel74608d12017-05-24 07:55:35 +020010460 alloc_queue_pairs = vsi->alloc_queue_pairs *
10461 (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
10462
10463 ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010464 if (ret < 0) {
Shannon Nelson049a2be2014-10-17 03:14:50 +000010465 dev_info(&pf->pdev->dev,
10466 "failed to get tracking for %d queues for VSI %d err=%d\n",
Björn Töpel74608d12017-05-24 07:55:35 +020010467 alloc_queue_pairs, vsi->seid, ret);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010468 goto err_vsi;
10469 }
10470 vsi->base_queue = ret;
10471
10472 /* get a VSI from the hardware */
10473 vsi->uplink_seid = uplink_seid;
10474 ret = i40e_add_vsi(vsi);
10475 if (ret)
10476 goto err_vsi;
10477
10478 switch (vsi->type) {
10479 /* setup the netdev if needed */
10480 case I40E_VSI_MAIN:
10481 case I40E_VSI_VMDQ2:
10482 ret = i40e_config_netdev(vsi);
10483 if (ret)
10484 goto err_netdev;
10485 ret = register_netdev(vsi->netdev);
10486 if (ret)
10487 goto err_netdev;
10488 vsi->netdev_registered = true;
10489 netif_carrier_off(vsi->netdev);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -080010490#ifdef CONFIG_I40E_DCB
10491 /* Setup DCB netlink interface */
10492 i40e_dcbnl_setup(vsi);
10493#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010494 /* fall through */
10495
10496 case I40E_VSI_FDIR:
10497 /* set up vectors and rings if needed */
10498 ret = i40e_vsi_setup_vectors(vsi);
10499 if (ret)
10500 goto err_msix;
10501
10502 ret = i40e_alloc_rings(vsi);
10503 if (ret)
10504 goto err_rings;
10505
10506 /* map all of the rings to the q_vectors */
10507 i40e_vsi_map_rings_to_vectors(vsi);
10508
10509 i40e_vsi_reset_stats(vsi);
10510 break;
10511
10512 default:
10513 /* no netdev or rings for the other VSI types */
10514 break;
10515 }
10516
Jacob Kellerd36e41d2017-06-23 04:24:46 -040010517 if ((pf->hw_features & I40E_HW_RSS_AQ_CAPABLE) &&
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -040010518 (vsi->type == I40E_VSI_VMDQ2)) {
10519 ret = i40e_vsi_config_rss(vsi);
10520 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010521 return vsi;
10522
10523err_rings:
10524 i40e_vsi_free_q_vectors(vsi);
10525err_msix:
10526 if (vsi->netdev_registered) {
10527 vsi->netdev_registered = false;
10528 unregister_netdev(vsi->netdev);
10529 free_netdev(vsi->netdev);
10530 vsi->netdev = NULL;
10531 }
10532err_netdev:
10533 i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
10534err_vsi:
10535 i40e_vsi_clear(vsi);
10536err_alloc:
10537 return NULL;
10538}
10539
10540/**
10541 * i40e_veb_get_bw_info - Query VEB BW information
10542 * @veb: the veb to query
10543 *
10544 * Query the Tx scheduler BW configuration data for given VEB
10545 **/
10546static int i40e_veb_get_bw_info(struct i40e_veb *veb)
10547{
10548 struct i40e_aqc_query_switching_comp_ets_config_resp ets_data;
10549 struct i40e_aqc_query_switching_comp_bw_config_resp bw_data;
10550 struct i40e_pf *pf = veb->pf;
10551 struct i40e_hw *hw = &pf->hw;
10552 u32 tc_bw_max;
10553 int ret = 0;
10554 int i;
10555
10556 ret = i40e_aq_query_switch_comp_bw_config(hw, veb->seid,
10557 &bw_data, NULL);
10558 if (ret) {
10559 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010560 "query veb bw config failed, err %s aq_err %s\n",
10561 i40e_stat_str(&pf->hw, ret),
10562 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010563 goto out;
10564 }
10565
10566 ret = i40e_aq_query_switch_comp_ets_config(hw, veb->seid,
10567 &ets_data, NULL);
10568 if (ret) {
10569 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010570 "query veb bw ets config failed, err %s aq_err %s\n",
10571 i40e_stat_str(&pf->hw, ret),
10572 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010573 goto out;
10574 }
10575
10576 veb->bw_limit = le16_to_cpu(ets_data.port_bw_limit);
10577 veb->bw_max_quanta = ets_data.tc_bw_max;
10578 veb->is_abs_credits = bw_data.absolute_credits_enable;
Neerav Parikh23cd1f02014-11-12 00:18:41 +000010579 veb->enabled_tc = ets_data.tc_valid_bits;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010580 tc_bw_max = le16_to_cpu(bw_data.tc_bw_max[0]) |
10581 (le16_to_cpu(bw_data.tc_bw_max[1]) << 16);
10582 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
10583 veb->bw_tc_share_credits[i] = bw_data.tc_bw_share_credits[i];
10584 veb->bw_tc_limit_credits[i] =
10585 le16_to_cpu(bw_data.tc_bw_limits[i]);
10586 veb->bw_tc_max_quanta[i] = ((tc_bw_max >> (i*4)) & 0x7);
10587 }
10588
10589out:
10590 return ret;
10591}
10592
10593/**
10594 * i40e_veb_mem_alloc - Allocates the next available struct veb in the PF
10595 * @pf: board private structure
10596 *
10597 * On error: returns error code (negative)
10598 * On success: returns vsi index in PF (positive)
10599 **/
10600static int i40e_veb_mem_alloc(struct i40e_pf *pf)
10601{
10602 int ret = -ENOENT;
10603 struct i40e_veb *veb;
10604 int i;
10605
10606 /* Need to protect the allocation of switch elements at the PF level */
10607 mutex_lock(&pf->switch_mutex);
10608
10609 /* VEB list may be fragmented if VEB creation/destruction has
10610 * been happening. We can afford to do a quick scan to look
10611 * for any free slots in the list.
10612 *
10613 * find next empty veb slot, looping back around if necessary
10614 */
10615 i = 0;
10616 while ((i < I40E_MAX_VEB) && (pf->veb[i] != NULL))
10617 i++;
10618 if (i >= I40E_MAX_VEB) {
10619 ret = -ENOMEM;
10620 goto err_alloc_veb; /* out of VEB slots! */
10621 }
10622
10623 veb = kzalloc(sizeof(*veb), GFP_KERNEL);
10624 if (!veb) {
10625 ret = -ENOMEM;
10626 goto err_alloc_veb;
10627 }
10628 veb->pf = pf;
10629 veb->idx = i;
10630 veb->enabled_tc = 1;
10631
10632 pf->veb[i] = veb;
10633 ret = i;
10634err_alloc_veb:
10635 mutex_unlock(&pf->switch_mutex);
10636 return ret;
10637}
10638
10639/**
10640 * i40e_switch_branch_release - Delete a branch of the switch tree
10641 * @branch: where to start deleting
10642 *
10643 * This uses recursion to find the tips of the branch to be
10644 * removed, deleting until we get back to and can delete this VEB.
10645 **/
10646static void i40e_switch_branch_release(struct i40e_veb *branch)
10647{
10648 struct i40e_pf *pf = branch->pf;
10649 u16 branch_seid = branch->seid;
10650 u16 veb_idx = branch->idx;
10651 int i;
10652
10653 /* release any VEBs on this VEB - RECURSION */
10654 for (i = 0; i < I40E_MAX_VEB; i++) {
10655 if (!pf->veb[i])
10656 continue;
10657 if (pf->veb[i]->uplink_seid == branch->seid)
10658 i40e_switch_branch_release(pf->veb[i]);
10659 }
10660
10661 /* Release the VSIs on this VEB, but not the owner VSI.
10662 *
10663 * NOTE: Removing the last VSI on a VEB has the SIDE EFFECT of removing
10664 * the VEB itself, so don't use (*branch) after this loop.
10665 */
Mitch Williams505682c2014-05-20 08:01:37 +000010666 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010667 if (!pf->vsi[i])
10668 continue;
10669 if (pf->vsi[i]->uplink_seid == branch_seid &&
10670 (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
10671 i40e_vsi_release(pf->vsi[i]);
10672 }
10673 }
10674
10675 /* There's one corner case where the VEB might not have been
10676 * removed, so double check it here and remove it if needed.
10677 * This case happens if the veb was created from the debugfs
10678 * commands and no VSIs were added to it.
10679 */
10680 if (pf->veb[veb_idx])
10681 i40e_veb_release(pf->veb[veb_idx]);
10682}
10683
10684/**
10685 * i40e_veb_clear - remove veb struct
10686 * @veb: the veb to remove
10687 **/
10688static void i40e_veb_clear(struct i40e_veb *veb)
10689{
10690 if (!veb)
10691 return;
10692
10693 if (veb->pf) {
10694 struct i40e_pf *pf = veb->pf;
10695
10696 mutex_lock(&pf->switch_mutex);
10697 if (pf->veb[veb->idx] == veb)
10698 pf->veb[veb->idx] = NULL;
10699 mutex_unlock(&pf->switch_mutex);
10700 }
10701
10702 kfree(veb);
10703}
10704
10705/**
10706 * i40e_veb_release - Delete a VEB and free its resources
10707 * @veb: the VEB being removed
10708 **/
10709void i40e_veb_release(struct i40e_veb *veb)
10710{
10711 struct i40e_vsi *vsi = NULL;
10712 struct i40e_pf *pf;
10713 int i, n = 0;
10714
10715 pf = veb->pf;
10716
10717 /* find the remaining VSI and check for extras */
Mitch Williams505682c2014-05-20 08:01:37 +000010718 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010719 if (pf->vsi[i] && pf->vsi[i]->uplink_seid == veb->seid) {
10720 n++;
10721 vsi = pf->vsi[i];
10722 }
10723 }
10724 if (n != 1) {
10725 dev_info(&pf->pdev->dev,
10726 "can't remove VEB %d with %d VSIs left\n",
10727 veb->seid, n);
10728 return;
10729 }
10730
10731 /* move the remaining VSI to uplink veb */
10732 vsi->flags &= ~I40E_VSI_FLAG_VEB_OWNER;
10733 if (veb->uplink_seid) {
10734 vsi->uplink_seid = veb->uplink_seid;
10735 if (veb->uplink_seid == pf->mac_seid)
10736 vsi->veb_idx = I40E_NO_VEB;
10737 else
10738 vsi->veb_idx = veb->veb_idx;
10739 } else {
10740 /* floating VEB */
10741 vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
10742 vsi->veb_idx = pf->vsi[pf->lan_vsi]->veb_idx;
10743 }
10744
10745 i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
10746 i40e_veb_clear(veb);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010747}
10748
10749/**
10750 * i40e_add_veb - create the VEB in the switch
10751 * @veb: the VEB to be instantiated
10752 * @vsi: the controlling VSI
10753 **/
10754static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi)
10755{
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010756 struct i40e_pf *pf = veb->pf;
Shannon Nelson66fc3602016-01-13 16:51:42 -080010757 bool enable_stats = !!(pf->flags & I40E_FLAG_VEB_STATS_ENABLED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010758 int ret;
10759
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010760 ret = i40e_aq_add_veb(&pf->hw, veb->uplink_seid, vsi->seid,
Mitch Williams5bc16032016-05-16 10:26:43 -070010761 veb->enabled_tc, false,
Shannon Nelson66fc3602016-01-13 16:51:42 -080010762 &veb->seid, enable_stats, NULL);
Mitch Williams5bc16032016-05-16 10:26:43 -070010763
10764 /* get a VEB from the hardware */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010765 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010766 dev_info(&pf->pdev->dev,
10767 "couldn't add VEB, err %s aq_err %s\n",
10768 i40e_stat_str(&pf->hw, ret),
10769 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010770 return -EPERM;
10771 }
10772
10773 /* get statistics counter */
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010774 ret = i40e_aq_get_veb_parameters(&pf->hw, veb->seid, NULL, NULL,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010775 &veb->stats_idx, NULL, NULL, NULL);
10776 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010777 dev_info(&pf->pdev->dev,
10778 "couldn't get VEB statistics idx, err %s aq_err %s\n",
10779 i40e_stat_str(&pf->hw, ret),
10780 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010781 return -EPERM;
10782 }
10783 ret = i40e_veb_get_bw_info(veb);
10784 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010785 dev_info(&pf->pdev->dev,
10786 "couldn't get VEB bw info, err %s aq_err %s\n",
10787 i40e_stat_str(&pf->hw, ret),
10788 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10789 i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010790 return -ENOENT;
10791 }
10792
10793 vsi->uplink_seid = veb->seid;
10794 vsi->veb_idx = veb->idx;
10795 vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
10796
10797 return 0;
10798}
10799
10800/**
10801 * i40e_veb_setup - Set up a VEB
10802 * @pf: board private structure
10803 * @flags: VEB setup flags
10804 * @uplink_seid: the switch element to link to
10805 * @vsi_seid: the initial VSI seid
10806 * @enabled_tc: Enabled TC bit-map
10807 *
10808 * This allocates the sw VEB structure and links it into the switch
10809 * It is possible and legal for this to be a duplicate of an already
10810 * existing VEB. It is also possible for both uplink and vsi seids
10811 * to be zero, in order to create a floating VEB.
10812 *
10813 * Returns pointer to the successfully allocated VEB sw struct on
10814 * success, otherwise returns NULL on failure.
10815 **/
10816struct i40e_veb *i40e_veb_setup(struct i40e_pf *pf, u16 flags,
10817 u16 uplink_seid, u16 vsi_seid,
10818 u8 enabled_tc)
10819{
10820 struct i40e_veb *veb, *uplink_veb = NULL;
10821 int vsi_idx, veb_idx;
10822 int ret;
10823
10824 /* if one seid is 0, the other must be 0 to create a floating relay */
10825 if ((uplink_seid == 0 || vsi_seid == 0) &&
10826 (uplink_seid + vsi_seid != 0)) {
10827 dev_info(&pf->pdev->dev,
10828 "one, not both seid's are 0: uplink=%d vsi=%d\n",
10829 uplink_seid, vsi_seid);
10830 return NULL;
10831 }
10832
10833 /* make sure there is such a vsi and uplink */
Mitch Williams505682c2014-05-20 08:01:37 +000010834 for (vsi_idx = 0; vsi_idx < pf->num_alloc_vsi; vsi_idx++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010835 if (pf->vsi[vsi_idx] && pf->vsi[vsi_idx]->seid == vsi_seid)
10836 break;
Mitch Williams505682c2014-05-20 08:01:37 +000010837 if (vsi_idx >= pf->num_alloc_vsi && vsi_seid != 0) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010838 dev_info(&pf->pdev->dev, "vsi seid %d not found\n",
10839 vsi_seid);
10840 return NULL;
10841 }
10842
10843 if (uplink_seid && uplink_seid != pf->mac_seid) {
10844 for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
10845 if (pf->veb[veb_idx] &&
10846 pf->veb[veb_idx]->seid == uplink_seid) {
10847 uplink_veb = pf->veb[veb_idx];
10848 break;
10849 }
10850 }
10851 if (!uplink_veb) {
10852 dev_info(&pf->pdev->dev,
10853 "uplink seid %d not found\n", uplink_seid);
10854 return NULL;
10855 }
10856 }
10857
10858 /* get veb sw struct */
10859 veb_idx = i40e_veb_mem_alloc(pf);
10860 if (veb_idx < 0)
10861 goto err_alloc;
10862 veb = pf->veb[veb_idx];
10863 veb->flags = flags;
10864 veb->uplink_seid = uplink_seid;
10865 veb->veb_idx = (uplink_veb ? uplink_veb->idx : I40E_NO_VEB);
10866 veb->enabled_tc = (enabled_tc ? enabled_tc : 0x1);
10867
10868 /* create the VEB in the switch */
10869 ret = i40e_add_veb(veb, pf->vsi[vsi_idx]);
10870 if (ret)
10871 goto err_veb;
Shannon Nelson1bb8b932014-04-23 04:49:54 +000010872 if (vsi_idx == pf->lan_vsi)
10873 pf->lan_veb = veb->idx;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010874
10875 return veb;
10876
10877err_veb:
10878 i40e_veb_clear(veb);
10879err_alloc:
10880 return NULL;
10881}
10882
10883/**
Jeff Kirsherb40c82e62015-02-27 09:18:34 +000010884 * i40e_setup_pf_switch_element - set PF vars based on switch type
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010885 * @pf: board private structure
10886 * @ele: element we are building info from
10887 * @num_reported: total number of elements
10888 * @printconfig: should we print the contents
10889 *
10890 * helper function to assist in extracting a few useful SEID values.
10891 **/
10892static void i40e_setup_pf_switch_element(struct i40e_pf *pf,
10893 struct i40e_aqc_switch_config_element_resp *ele,
10894 u16 num_reported, bool printconfig)
10895{
10896 u16 downlink_seid = le16_to_cpu(ele->downlink_seid);
10897 u16 uplink_seid = le16_to_cpu(ele->uplink_seid);
10898 u8 element_type = ele->element_type;
10899 u16 seid = le16_to_cpu(ele->seid);
10900
10901 if (printconfig)
10902 dev_info(&pf->pdev->dev,
10903 "type=%d seid=%d uplink=%d downlink=%d\n",
10904 element_type, seid, uplink_seid, downlink_seid);
10905
10906 switch (element_type) {
10907 case I40E_SWITCH_ELEMENT_TYPE_MAC:
10908 pf->mac_seid = seid;
10909 break;
10910 case I40E_SWITCH_ELEMENT_TYPE_VEB:
10911 /* Main VEB? */
10912 if (uplink_seid != pf->mac_seid)
10913 break;
10914 if (pf->lan_veb == I40E_NO_VEB) {
10915 int v;
10916
10917 /* find existing or else empty VEB */
10918 for (v = 0; v < I40E_MAX_VEB; v++) {
10919 if (pf->veb[v] && (pf->veb[v]->seid == seid)) {
10920 pf->lan_veb = v;
10921 break;
10922 }
10923 }
10924 if (pf->lan_veb == I40E_NO_VEB) {
10925 v = i40e_veb_mem_alloc(pf);
10926 if (v < 0)
10927 break;
10928 pf->lan_veb = v;
10929 }
10930 }
10931
10932 pf->veb[pf->lan_veb]->seid = seid;
10933 pf->veb[pf->lan_veb]->uplink_seid = pf->mac_seid;
10934 pf->veb[pf->lan_veb]->pf = pf;
10935 pf->veb[pf->lan_veb]->veb_idx = I40E_NO_VEB;
10936 break;
10937 case I40E_SWITCH_ELEMENT_TYPE_VSI:
10938 if (num_reported != 1)
10939 break;
10940 /* This is immediately after a reset so we can assume this is
10941 * the PF's VSI
10942 */
10943 pf->mac_seid = uplink_seid;
10944 pf->pf_seid = downlink_seid;
10945 pf->main_vsi_seid = seid;
10946 if (printconfig)
10947 dev_info(&pf->pdev->dev,
10948 "pf_seid=%d main_vsi_seid=%d\n",
10949 pf->pf_seid, pf->main_vsi_seid);
10950 break;
10951 case I40E_SWITCH_ELEMENT_TYPE_PF:
10952 case I40E_SWITCH_ELEMENT_TYPE_VF:
10953 case I40E_SWITCH_ELEMENT_TYPE_EMP:
10954 case I40E_SWITCH_ELEMENT_TYPE_BMC:
10955 case I40E_SWITCH_ELEMENT_TYPE_PE:
10956 case I40E_SWITCH_ELEMENT_TYPE_PA:
10957 /* ignore these for now */
10958 break;
10959 default:
10960 dev_info(&pf->pdev->dev, "unknown element type=%d seid=%d\n",
10961 element_type, seid);
10962 break;
10963 }
10964}
10965
10966/**
10967 * i40e_fetch_switch_configuration - Get switch config from firmware
10968 * @pf: board private structure
10969 * @printconfig: should we print the contents
10970 *
10971 * Get the current switch configuration from the device and
10972 * extract a few useful SEID values.
10973 **/
10974int i40e_fetch_switch_configuration(struct i40e_pf *pf, bool printconfig)
10975{
10976 struct i40e_aqc_get_switch_config_resp *sw_config;
10977 u16 next_seid = 0;
10978 int ret = 0;
10979 u8 *aq_buf;
10980 int i;
10981
10982 aq_buf = kzalloc(I40E_AQ_LARGE_BUF, GFP_KERNEL);
10983 if (!aq_buf)
10984 return -ENOMEM;
10985
10986 sw_config = (struct i40e_aqc_get_switch_config_resp *)aq_buf;
10987 do {
10988 u16 num_reported, num_total;
10989
10990 ret = i40e_aq_get_switch_config(&pf->hw, sw_config,
10991 I40E_AQ_LARGE_BUF,
10992 &next_seid, NULL);
10993 if (ret) {
10994 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010995 "get switch config failed err %s aq_err %s\n",
10996 i40e_stat_str(&pf->hw, ret),
10997 i40e_aq_str(&pf->hw,
10998 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010999 kfree(aq_buf);
11000 return -ENOENT;
11001 }
11002
11003 num_reported = le16_to_cpu(sw_config->header.num_reported);
11004 num_total = le16_to_cpu(sw_config->header.num_total);
11005
11006 if (printconfig)
11007 dev_info(&pf->pdev->dev,
11008 "header: %d reported %d total\n",
11009 num_reported, num_total);
11010
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011011 for (i = 0; i < num_reported; i++) {
11012 struct i40e_aqc_switch_config_element_resp *ele =
11013 &sw_config->element[i];
11014
11015 i40e_setup_pf_switch_element(pf, ele, num_reported,
11016 printconfig);
11017 }
11018 } while (next_seid != 0);
11019
11020 kfree(aq_buf);
11021 return ret;
11022}
11023
11024/**
11025 * i40e_setup_pf_switch - Setup the HW switch on startup or after reset
11026 * @pf: board private structure
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000011027 * @reinit: if the Main VSI needs to re-initialized.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011028 *
11029 * Returns 0 on success, negative value on failure
11030 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000011031static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011032{
Anjali Singhai Jainb5569892016-05-03 15:13:12 -070011033 u16 flags = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011034 int ret;
11035
11036 /* find out what's out there already */
11037 ret = i40e_fetch_switch_configuration(pf, false);
11038 if (ret) {
11039 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040011040 "couldn't fetch switch config, err %s aq_err %s\n",
11041 i40e_stat_str(&pf->hw, ret),
11042 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011043 return ret;
11044 }
11045 i40e_pf_reset_stats(pf);
11046
Anjali Singhai Jainb5569892016-05-03 15:13:12 -070011047 /* set the switch config bit for the whole device to
11048 * support limited promisc or true promisc
11049 * when user requests promisc. The default is limited
11050 * promisc.
11051 */
11052
11053 if ((pf->hw.pf_id == 0) &&
11054 !(pf->flags & I40E_FLAG_TRUE_PROMISC_SUPPORT))
11055 flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
11056
11057 if (pf->hw.pf_id == 0) {
11058 u16 valid_flags;
11059
11060 valid_flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
11061 ret = i40e_aq_set_switch_config(&pf->hw, flags, valid_flags,
11062 NULL);
11063 if (ret && pf->hw.aq.asq_last_status != I40E_AQ_RC_ESRCH) {
11064 dev_info(&pf->pdev->dev,
11065 "couldn't set switch config bits, err %s aq_err %s\n",
11066 i40e_stat_str(&pf->hw, ret),
11067 i40e_aq_str(&pf->hw,
11068 pf->hw.aq.asq_last_status));
11069 /* not a fatal problem, just keep going */
11070 }
11071 }
11072
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011073 /* first time setup */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000011074 if (pf->lan_vsi == I40E_NO_VSI || reinit) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011075 struct i40e_vsi *vsi = NULL;
11076 u16 uplink_seid;
11077
11078 /* Set up the PF VSI associated with the PF's main VSI
11079 * that is already in the HW switch
11080 */
11081 if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
11082 uplink_seid = pf->veb[pf->lan_veb]->seid;
11083 else
11084 uplink_seid = pf->mac_seid;
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000011085 if (pf->lan_vsi == I40E_NO_VSI)
11086 vsi = i40e_vsi_setup(pf, I40E_VSI_MAIN, uplink_seid, 0);
11087 else if (reinit)
11088 vsi = i40e_vsi_reinit_setup(pf->vsi[pf->lan_vsi]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011089 if (!vsi) {
11090 dev_info(&pf->pdev->dev, "setup of MAIN VSI failed\n");
11091 i40e_fdir_teardown(pf);
11092 return -EAGAIN;
11093 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011094 } else {
11095 /* force a reset of TC and queue layout configurations */
11096 u8 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
Jesse Brandeburg6995b362015-08-28 17:55:54 -040011097
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011098 pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
11099 pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
11100 i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
11101 }
11102 i40e_vlan_stripping_disable(pf->vsi[pf->lan_vsi]);
11103
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080011104 i40e_fdir_sb_setup(pf);
11105
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011106 /* Setup static PF queue filter control settings */
11107 ret = i40e_setup_pf_filter_control(pf);
11108 if (ret) {
11109 dev_info(&pf->pdev->dev, "setup_pf_filter_control failed: %d\n",
11110 ret);
11111 /* Failure here should not stop continuing other steps */
11112 }
11113
11114 /* enable RSS in the HW, even for only one queue, as the stack can use
11115 * the hash
11116 */
11117 if ((pf->flags & I40E_FLAG_RSS_ENABLED))
Helin Zhang043dd652015-10-21 19:56:23 -040011118 i40e_pf_config_rss(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011119
11120 /* fill in link information and enable LSE reporting */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011121 i40e_link_event(pf);
11122
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +000011123 /* Initialize user-specific link properties */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011124 pf->fc_autoneg_status = ((pf->hw.phy.link_info.an_info &
11125 I40E_AQ_AN_COMPLETED) ? true : false);
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +000011126
Jacob Kellerbeb0dff2014-01-11 05:43:19 +000011127 i40e_ptp_init(pf);
11128
Alexander Duyck1f190d92017-04-19 09:25:51 -040011129 /* repopulate tunnel port filters */
11130 i40e_sync_udp_filters(pf);
11131
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011132 return ret;
11133}
11134
11135/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011136 * i40e_determine_queue_usage - Work out queue distribution
11137 * @pf: board private structure
11138 **/
11139static void i40e_determine_queue_usage(struct i40e_pf *pf)
11140{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011141 int queues_left;
Shannon Nelsone50d5752017-07-24 18:17:42 -070011142 int q_max;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011143
11144 pf->num_lan_qps = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011145
11146 /* Find the max queues to be put into basic use. We'll always be
11147 * using TC0, whether or not DCB is running, and TC0 will get the
11148 * big RSS set.
11149 */
11150 queues_left = pf->hw.func_caps.num_tx_qp;
11151
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080011152 if ((queues_left == 1) ||
Frank Zhang9aa7e932014-05-20 08:01:42 +000011153 !(pf->flags & I40E_FLAG_MSIX_ENABLED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011154 /* one qp for PF, no queues for anything else */
11155 queues_left = 0;
Helin Zhangacd65442015-10-26 19:44:28 -040011156 pf->alloc_rss_size = pf->num_lan_qps = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011157
11158 /* make sure all the fancies are disabled */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080011159 pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060011160 I40E_FLAG_IWARP_ENABLED |
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080011161 I40E_FLAG_FD_SB_ENABLED |
11162 I40E_FLAG_FD_ATR_ENABLED |
Neerav Parikh4d9b6042014-05-22 06:31:51 +000011163 I40E_FLAG_DCB_CAPABLE |
Dave Ertmana0362442016-08-29 17:38:26 -070011164 I40E_FLAG_DCB_ENABLED |
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080011165 I40E_FLAG_SRIOV_ENABLED |
11166 I40E_FLAG_VMDQ_ENABLED);
Frank Zhang9aa7e932014-05-20 08:01:42 +000011167 } else if (!(pf->flags & (I40E_FLAG_RSS_ENABLED |
11168 I40E_FLAG_FD_SB_ENABLED |
Anjali Singhai Jainbbe7d0e2014-05-20 08:01:44 +000011169 I40E_FLAG_FD_ATR_ENABLED |
Neerav Parikh4d9b6042014-05-22 06:31:51 +000011170 I40E_FLAG_DCB_CAPABLE))) {
Frank Zhang9aa7e932014-05-20 08:01:42 +000011171 /* one qp for PF */
Helin Zhangacd65442015-10-26 19:44:28 -040011172 pf->alloc_rss_size = pf->num_lan_qps = 1;
Frank Zhang9aa7e932014-05-20 08:01:42 +000011173 queues_left -= pf->num_lan_qps;
11174
11175 pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060011176 I40E_FLAG_IWARP_ENABLED |
Frank Zhang9aa7e932014-05-20 08:01:42 +000011177 I40E_FLAG_FD_SB_ENABLED |
11178 I40E_FLAG_FD_ATR_ENABLED |
11179 I40E_FLAG_DCB_ENABLED |
11180 I40E_FLAG_VMDQ_ENABLED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011181 } else {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080011182 /* Not enough queues for all TCs */
Neerav Parikh4d9b6042014-05-22 06:31:51 +000011183 if ((pf->flags & I40E_FLAG_DCB_CAPABLE) &&
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080011184 (queues_left < I40E_MAX_TRAFFIC_CLASS)) {
Dave Ertmana0362442016-08-29 17:38:26 -070011185 pf->flags &= ~(I40E_FLAG_DCB_CAPABLE |
11186 I40E_FLAG_DCB_ENABLED);
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080011187 dev_info(&pf->pdev->dev, "not enough queues for DCB. DCB is disabled.\n");
11188 }
Shannon Nelsone50d5752017-07-24 18:17:42 -070011189
11190 /* limit lan qps to the smaller of qps, cpus or msix */
11191 q_max = max_t(int, pf->rss_size_max, num_online_cpus());
11192 q_max = min_t(int, q_max, pf->hw.func_caps.num_tx_qp);
11193 q_max = min_t(int, q_max, pf->hw.func_caps.num_msix_vectors);
11194 pf->num_lan_qps = q_max;
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +000011195
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080011196 queues_left -= pf->num_lan_qps;
11197 }
11198
11199 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
11200 if (queues_left > 1) {
11201 queues_left -= 1; /* save 1 queue for FD */
11202 } else {
11203 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
11204 dev_info(&pf->pdev->dev, "not enough queues for Flow Director. Flow Director feature is disabled\n");
11205 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011206 }
11207
11208 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
11209 pf->num_vf_qps && pf->num_req_vfs && queues_left) {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080011210 pf->num_req_vfs = min_t(int, pf->num_req_vfs,
11211 (queues_left / pf->num_vf_qps));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011212 queues_left -= (pf->num_req_vfs * pf->num_vf_qps);
11213 }
11214
11215 if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
11216 pf->num_vmdq_vsis && pf->num_vmdq_qps && queues_left) {
11217 pf->num_vmdq_vsis = min_t(int, pf->num_vmdq_vsis,
11218 (queues_left / pf->num_vmdq_qps));
11219 queues_left -= (pf->num_vmdq_vsis * pf->num_vmdq_qps);
11220 }
11221
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +000011222 pf->queues_left = queues_left;
Neerav Parikh8279e492015-09-03 17:18:50 -040011223 dev_dbg(&pf->pdev->dev,
11224 "qs_avail=%d FD SB=%d lan_qs=%d lan_tc0=%d vf=%d*%d vmdq=%d*%d, remaining=%d\n",
11225 pf->hw.func_caps.num_tx_qp,
11226 !!(pf->flags & I40E_FLAG_FD_SB_ENABLED),
Helin Zhangacd65442015-10-26 19:44:28 -040011227 pf->num_lan_qps, pf->alloc_rss_size, pf->num_req_vfs,
11228 pf->num_vf_qps, pf->num_vmdq_vsis, pf->num_vmdq_qps,
11229 queues_left);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011230}
11231
11232/**
11233 * i40e_setup_pf_filter_control - Setup PF static filter control
11234 * @pf: PF to be setup
11235 *
Jeff Kirsherb40c82e62015-02-27 09:18:34 +000011236 * i40e_setup_pf_filter_control sets up a PF's initial filter control
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011237 * settings. If PE/FCoE are enabled then it will also set the per PF
11238 * based filter sizes required for them. It also enables Flow director,
11239 * ethertype and macvlan type filter settings for the pf.
11240 *
11241 * Returns 0 on success, negative on failure
11242 **/
11243static int i40e_setup_pf_filter_control(struct i40e_pf *pf)
11244{
11245 struct i40e_filter_control_settings *settings = &pf->filter_settings;
11246
11247 settings->hash_lut_size = I40E_HASH_LUT_SIZE_128;
11248
11249 /* Flow Director is enabled */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080011250 if (pf->flags & (I40E_FLAG_FD_SB_ENABLED | I40E_FLAG_FD_ATR_ENABLED))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011251 settings->enable_fdir = true;
11252
11253 /* Ethtype and MACVLAN filters enabled for PF */
11254 settings->enable_ethtype = true;
11255 settings->enable_macvlan = true;
11256
11257 if (i40e_set_filter_control(&pf->hw, settings))
11258 return -ENOENT;
11259
11260 return 0;
11261}
11262
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000011263#define INFO_STRING_LEN 255
Shannon Nelson7fd89542015-10-21 19:47:04 -040011264#define REMAIN(__x) (INFO_STRING_LEN - (__x))
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000011265static void i40e_print_features(struct i40e_pf *pf)
11266{
11267 struct i40e_hw *hw = &pf->hw;
Joe Perches3b195842015-12-03 04:20:57 -080011268 char *buf;
11269 int i;
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000011270
Joe Perches3b195842015-12-03 04:20:57 -080011271 buf = kmalloc(INFO_STRING_LEN, GFP_KERNEL);
11272 if (!buf)
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000011273 return;
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000011274
Joe Perches3b195842015-12-03 04:20:57 -080011275 i = snprintf(buf, INFO_STRING_LEN, "Features: PF-id[%d]", hw->pf_id);
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000011276#ifdef CONFIG_PCI_IOV
Joe Perches3b195842015-12-03 04:20:57 -080011277 i += snprintf(&buf[i], REMAIN(i), " VFs: %d", pf->num_req_vfs);
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000011278#endif
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -070011279 i += snprintf(&buf[i], REMAIN(i), " VSIs: %d QP: %d",
Shannon Nelson7fd89542015-10-21 19:47:04 -040011280 pf->hw.func_caps.num_vsis,
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -070011281 pf->vsi[pf->lan_vsi]->num_queue_pairs);
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000011282 if (pf->flags & I40E_FLAG_RSS_ENABLED)
Joe Perches3b195842015-12-03 04:20:57 -080011283 i += snprintf(&buf[i], REMAIN(i), " RSS");
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000011284 if (pf->flags & I40E_FLAG_FD_ATR_ENABLED)
Joe Perches3b195842015-12-03 04:20:57 -080011285 i += snprintf(&buf[i], REMAIN(i), " FD_ATR");
Akeem G Abodunrinc6423ff2014-05-10 04:49:08 +000011286 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
Joe Perches3b195842015-12-03 04:20:57 -080011287 i += snprintf(&buf[i], REMAIN(i), " FD_SB");
11288 i += snprintf(&buf[i], REMAIN(i), " NTUPLE");
Akeem G Abodunrinc6423ff2014-05-10 04:49:08 +000011289 }
Neerav Parikh4d9b6042014-05-22 06:31:51 +000011290 if (pf->flags & I40E_FLAG_DCB_CAPABLE)
Joe Perches3b195842015-12-03 04:20:57 -080011291 i += snprintf(&buf[i], REMAIN(i), " DCB");
Joe Perches3b195842015-12-03 04:20:57 -080011292 i += snprintf(&buf[i], REMAIN(i), " VxLAN");
Singhai, Anjali6a899022015-12-14 12:21:18 -080011293 i += snprintf(&buf[i], REMAIN(i), " Geneve");
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000011294 if (pf->flags & I40E_FLAG_PTP)
Joe Perches3b195842015-12-03 04:20:57 -080011295 i += snprintf(&buf[i], REMAIN(i), " PTP");
Shannon Nelson6dec1012015-09-28 14:12:30 -040011296 if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
Joe Perches3b195842015-12-03 04:20:57 -080011297 i += snprintf(&buf[i], REMAIN(i), " VEB");
Shannon Nelson6dec1012015-09-28 14:12:30 -040011298 else
Joe Perches3b195842015-12-03 04:20:57 -080011299 i += snprintf(&buf[i], REMAIN(i), " VEPA");
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000011300
Joe Perches3b195842015-12-03 04:20:57 -080011301 dev_info(&pf->pdev->dev, "%s\n", buf);
11302 kfree(buf);
Shannon Nelson7fd89542015-10-21 19:47:04 -040011303 WARN_ON(i > INFO_STRING_LEN);
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000011304}
11305
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011306/**
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050011307 * i40e_get_platform_mac_addr - get platform-specific MAC address
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050011308 * @pdev: PCI device information struct
11309 * @pf: board private structure
11310 *
Jacob Keller41c4c2b2017-04-05 07:50:57 -040011311 * Look up the MAC address for the device. First we'll try
11312 * eth_platform_get_mac_address, which will check Open Firmware, or arch
11313 * specific fallback. Otherwise, we'll default to the stored value in
11314 * firmware.
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050011315 **/
11316static void i40e_get_platform_mac_addr(struct pci_dev *pdev, struct i40e_pf *pf)
11317{
Jacob Keller41c4c2b2017-04-05 07:50:57 -040011318 if (eth_platform_get_mac_address(&pdev->dev, pf->hw.mac.addr))
11319 i40e_get_mac_addr(&pf->hw, pf->hw.mac.addr);
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050011320}
11321
11322/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011323 * i40e_probe - Device initialization routine
11324 * @pdev: PCI device information struct
11325 * @ent: entry in i40e_pci_tbl
11326 *
Jeff Kirsherb40c82e62015-02-27 09:18:34 +000011327 * i40e_probe initializes a PF identified by a pci_dev structure.
11328 * The OS initialization, configuring of the PF private structure,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011329 * and a hardware reset occur.
11330 *
11331 * Returns 0 on success, negative on failure
11332 **/
11333static int i40e_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
11334{
Catherine Sullivane8278452015-02-06 08:52:08 +000011335 struct i40e_aq_get_phy_abilities_resp abilities;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011336 struct i40e_pf *pf;
11337 struct i40e_hw *hw;
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +000011338 static u16 pfs_found;
Shannon Nelson1d5109d2015-08-26 15:14:08 -040011339 u16 wol_nvm_bits;
Catherine Sullivand4dfb812013-11-28 06:39:21 +000011340 u16 link_status;
Jean Sacren6f66a482015-09-19 05:08:45 -060011341 int err;
Anjali Singhai Jain4f2f017c2015-10-21 19:47:07 -040011342 u32 val;
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +000011343 u32 i;
Helin Zhang58fc3262015-10-01 14:37:38 -040011344 u8 set_fc_aq_fail;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011345
11346 err = pci_enable_device_mem(pdev);
11347 if (err)
11348 return err;
11349
11350 /* set up for high or low dma */
Mitch Williams64942942014-02-11 08:26:33 +000011351 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
Mitch Williams64942942014-02-11 08:26:33 +000011352 if (err) {
Jean Sacrene3e3bfd2014-03-25 04:30:27 +000011353 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
11354 if (err) {
11355 dev_err(&pdev->dev,
11356 "DMA configuration failed: 0x%x\n", err);
11357 goto err_dma;
11358 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011359 }
11360
11361 /* set up pci connections */
Johannes Thumshirn56d766d2016-06-07 09:44:05 +020011362 err = pci_request_mem_regions(pdev, i40e_driver_name);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011363 if (err) {
11364 dev_info(&pdev->dev,
11365 "pci_request_selected_regions failed %d\n", err);
11366 goto err_pci_reg;
11367 }
11368
11369 pci_enable_pcie_error_reporting(pdev);
11370 pci_set_master(pdev);
11371
11372 /* Now that we have a PCI connection, we need to do the
11373 * low level device setup. This is primarily setting up
11374 * the Admin Queue structures and then querying for the
11375 * device's current profile information.
11376 */
11377 pf = kzalloc(sizeof(*pf), GFP_KERNEL);
11378 if (!pf) {
11379 err = -ENOMEM;
11380 goto err_pf_alloc;
11381 }
11382 pf->next_vsi = 0;
11383 pf->pdev = pdev;
Mauro S. M. Rodrigues9e6c9c02017-05-12 23:26:56 -030011384 set_bit(__I40E_DOWN, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011385
11386 hw = &pf->hw;
11387 hw->back = pf;
Anjali Singhai232f4702015-02-26 16:15:39 +000011388
Shannon Nelson2ac8b672015-07-23 16:54:37 -040011389 pf->ioremap_len = min_t(int, pci_resource_len(pdev, 0),
11390 I40E_MAX_CSR_SPACE);
Anjali Singhai232f4702015-02-26 16:15:39 +000011391
Shannon Nelson2ac8b672015-07-23 16:54:37 -040011392 hw->hw_addr = ioremap(pci_resource_start(pdev, 0), pf->ioremap_len);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011393 if (!hw->hw_addr) {
11394 err = -EIO;
11395 dev_info(&pdev->dev, "ioremap(0x%04x, 0x%04x) failed: 0x%x\n",
11396 (unsigned int)pci_resource_start(pdev, 0),
Shannon Nelson2ac8b672015-07-23 16:54:37 -040011397 pf->ioremap_len, err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011398 goto err_ioremap;
11399 }
11400 hw->vendor_id = pdev->vendor;
11401 hw->device_id = pdev->device;
11402 pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
11403 hw->subsystem_vendor_id = pdev->subsystem_vendor;
11404 hw->subsystem_device_id = pdev->subsystem_device;
11405 hw->bus.device = PCI_SLOT(pdev->devfn);
11406 hw->bus.func = PCI_FUNC(pdev->devfn);
Sudheer Mogilappagarib3f028f2017-02-09 23:58:22 -080011407 hw->bus.bus_id = pdev->bus->number;
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +000011408 pf->instance = pfs_found;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011409
Scott Petersonab243ec2017-08-22 06:57:54 -040011410 /* Select something other than the 802.1ad ethertype for the
11411 * switch to use internally and drop on ingress.
11412 */
11413 hw->switch_tag = 0xffff;
11414 hw->first_tag = ETH_P_8021AD;
11415 hw->second_tag = ETH_P_8021Q;
11416
Jacob Keller0e588de2017-02-06 14:38:50 -080011417 INIT_LIST_HEAD(&pf->l3_flex_pit_list);
11418 INIT_LIST_HEAD(&pf->l4_flex_pit_list);
11419
Shannon Nelsonde03d2b2016-03-10 14:59:44 -080011420 /* set up the locks for the AQ, do this only once in probe
11421 * and destroy them only once in remove
11422 */
11423 mutex_init(&hw->aq.asq_mutex);
11424 mutex_init(&hw->aq.arq_mutex);
11425
Alexander Duyck5d4ca232016-09-30 08:21:46 -040011426 pf->msg_enable = netif_msg_init(debug,
11427 NETIF_MSG_DRV |
11428 NETIF_MSG_PROBE |
11429 NETIF_MSG_LINK);
11430 if (debug < -1)
11431 pf->hw.debug_mask = debug;
Shannon Nelson5b5faa42014-10-17 03:14:51 +000011432
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +000011433 /* do a special CORER for clearing PXE mode once at init */
11434 if (hw->revision_id == 0 &&
11435 (rd32(hw, I40E_GLLAN_RCTL_0) & I40E_GLLAN_RCTL_0_PXE_MODE_MASK)) {
11436 wr32(hw, I40E_GLGEN_RTRIG, I40E_GLGEN_RTRIG_CORER_MASK);
11437 i40e_flush(hw);
11438 msleep(200);
11439 pf->corer_count++;
11440
11441 i40e_clear_pxe_mode(hw);
11442 }
11443
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011444 /* Reset here to make sure all is clean and to define PF 'n' */
Shannon Nelson838d41d2014-06-04 20:41:27 +000011445 i40e_clear_hw(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011446 err = i40e_pf_reset(hw);
11447 if (err) {
11448 dev_info(&pdev->dev, "Initial pf_reset failed: %d\n", err);
11449 goto err_pf_reset;
11450 }
11451 pf->pfr_count++;
11452
11453 hw->aq.num_arq_entries = I40E_AQ_LEN;
11454 hw->aq.num_asq_entries = I40E_AQ_LEN;
11455 hw->aq.arq_buf_size = I40E_MAX_AQ_BUF_SIZE;
11456 hw->aq.asq_buf_size = I40E_MAX_AQ_BUF_SIZE;
11457 pf->adminq_work_limit = I40E_AQ_WORK_LIMIT;
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +000011458
Carolyn Wybornyb294ac72014-12-11 07:06:39 +000011459 snprintf(pf->int_name, sizeof(pf->int_name) - 1,
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +000011460 "%s-%s:misc",
11461 dev_driver_string(&pf->pdev->dev), dev_name(&pdev->dev));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011462
11463 err = i40e_init_shared_code(hw);
11464 if (err) {
Anjali Singhai Jainb2a75c52015-04-27 14:57:20 -040011465 dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
11466 err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011467 goto err_pf_reset;
11468 }
11469
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +000011470 /* set up a default setting for link flow control */
11471 pf->hw.fc.requested_mode = I40E_FC_NONE;
11472
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011473 err = i40e_init_adminq(hw);
Carolyn Wyborny2b2426a762015-10-26 19:44:35 -040011474 if (err) {
11475 if (err == I40E_ERR_FIRMWARE_API_VERSION)
11476 dev_info(&pdev->dev,
11477 "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");
11478 else
11479 dev_info(&pdev->dev,
11480 "The driver for the device stopped because the device firmware failed to init. Try updating your NVM image.\n");
11481
11482 goto err_pf_reset;
11483 }
Filip Sadowski5bbb2e22017-06-07 05:43:09 -040011484 i40e_get_oem_version(hw);
Carolyn Wybornyf0b44442015-08-31 19:54:49 -040011485
Shannon Nelson6dec1012015-09-28 14:12:30 -040011486 /* provide nvm, fw, api versions */
11487 dev_info(&pdev->dev, "fw %d.%d.%05d api %d.%d nvm %s\n",
11488 hw->aq.fw_maj_ver, hw->aq.fw_min_ver, hw->aq.fw_build,
11489 hw->aq.api_maj_ver, hw->aq.api_min_ver,
11490 i40e_nvm_version_str(hw));
Carolyn Wybornyf0b44442015-08-31 19:54:49 -040011491
Catherine Sullivan7aa67612014-07-09 07:46:17 +000011492 if (hw->aq.api_maj_ver == I40E_FW_API_VERSION_MAJOR &&
Mitch Williams22b965512017-07-14 09:27:09 -040011493 hw->aq.api_min_ver > I40E_FW_MINOR_VERSION(hw))
Shannon Nelson278b6f62014-06-04 01:41:03 +000011494 dev_info(&pdev->dev,
Catherine Sullivan7aa67612014-07-09 07:46:17 +000011495 "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 -040011496 else if (hw->aq.api_maj_ver == 1 && hw->aq.api_min_ver < 4)
Shannon Nelson278b6f62014-06-04 01:41:03 +000011497 dev_info(&pdev->dev,
Catherine Sullivan7aa67612014-07-09 07:46:17 +000011498 "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 +000011499
Shannon Nelson4eb3f762014-03-06 08:59:58 +000011500 i40e_verify_eeprom(pf);
11501
Jesse Brandeburg2c5fe332014-04-23 04:49:57 +000011502 /* Rev 0 hardware was never productized */
11503 if (hw->revision_id < 1)
11504 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");
11505
Shannon Nelson6ff4ef82013-12-21 05:44:49 +000011506 i40e_clear_pxe_mode(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011507 err = i40e_get_capabilities(pf);
11508 if (err)
11509 goto err_adminq_setup;
11510
11511 err = i40e_sw_init(pf);
11512 if (err) {
11513 dev_info(&pdev->dev, "sw_init failed: %d\n", err);
11514 goto err_sw_init;
11515 }
11516
11517 err = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
Alexander Duyckc76cb6e2017-02-21 15:55:48 -080011518 hw->func_caps.num_rx_qp, 0, 0);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011519 if (err) {
11520 dev_info(&pdev->dev, "init_lan_hmc failed: %d\n", err);
11521 goto err_init_lan_hmc;
11522 }
11523
11524 err = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
11525 if (err) {
11526 dev_info(&pdev->dev, "configure_lan_hmc failed: %d\n", err);
11527 err = -ENOENT;
11528 goto err_configure_lan_hmc;
11529 }
11530
Neerav Parikhb686ece2014-12-14 01:55:11 +000011531 /* Disable LLDP for NICs that have firmware versions lower than v4.3.
11532 * Ignore error return codes because if it was already disabled via
11533 * hardware settings this will fail
11534 */
Jacob Kellerd36e41d2017-06-23 04:24:46 -040011535 if (pf->hw_features & I40E_HW_STOP_FW_LLDP) {
Neerav Parikhb686ece2014-12-14 01:55:11 +000011536 dev_info(&pdev->dev, "Stopping firmware LLDP agent.\n");
11537 i40e_aq_stop_lldp(hw, true, NULL);
11538 }
11539
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050011540 /* allow a platform config to override the HW addr */
11541 i40e_get_platform_mac_addr(pdev, pf);
Jacob Keller41c4c2b2017-04-05 07:50:57 -040011542
Jesse Brandeburgf62b5062013-11-28 06:39:27 +000011543 if (!is_valid_ether_addr(hw->mac.addr)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011544 dev_info(&pdev->dev, "invalid MAC address %pM\n", hw->mac.addr);
11545 err = -EIO;
11546 goto err_mac_addr;
11547 }
11548 dev_info(&pdev->dev, "MAC address: %pM\n", hw->mac.addr);
Greg Rose9a173902014-05-22 06:32:02 +000011549 ether_addr_copy(hw->mac.perm_addr, hw->mac.addr);
Neerav Parikh1f224ad2014-02-12 01:45:31 +000011550 i40e_get_port_mac_addr(hw, hw->mac.port_addr);
11551 if (is_valid_ether_addr(hw->mac.port_addr))
Jacob Kellerd36e41d2017-06-23 04:24:46 -040011552 pf->hw_features |= I40E_HW_PORT_ID_VALID;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011553
11554 pci_set_drvdata(pdev, pf);
11555 pci_save_state(pdev);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -080011556#ifdef CONFIG_I40E_DCB
11557 err = i40e_init_pf_dcb(pf);
11558 if (err) {
Shannon Nelsonaebfc812014-12-11 07:06:38 +000011559 dev_info(&pdev->dev, "DCB init failed %d, disabled\n", err);
David Ertmanc17ef432016-09-30 01:36:21 -070011560 pf->flags &= ~(I40E_FLAG_DCB_CAPABLE | I40E_FLAG_DCB_ENABLED);
Neerav Parikh014269f2014-04-01 07:11:48 +000011561 /* Continue without DCB enabled */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -080011562 }
11563#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011564
11565 /* set up periodic task facility */
11566 setup_timer(&pf->service_timer, i40e_service_timer, (unsigned long)pf);
11567 pf->service_timer_period = HZ;
11568
11569 INIT_WORK(&pf->service_task, i40e_service_task);
Jacob Keller0da36b92017-04-19 09:25:55 -040011570 clear_bit(__I40E_SERVICE_SCHED, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011571
Shannon Nelson1d5109d2015-08-26 15:14:08 -040011572 /* NVM bit on means WoL disabled for the port */
11573 i40e_read_nvm_word(hw, I40E_SR_NVM_WAKE_ON_LAN, &wol_nvm_bits);
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -080011574 if (BIT (hw->port) & wol_nvm_bits || hw->partition_id != 1)
Shannon Nelson1d5109d2015-08-26 15:14:08 -040011575 pf->wol_en = false;
11576 else
11577 pf->wol_en = true;
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011578 device_set_wakeup_enable(&pf->pdev->dev, pf->wol_en);
11579
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011580 /* set up the main switch operations */
11581 i40e_determine_queue_usage(pf);
Jesse Brandeburgc11472802015-04-07 19:45:39 -040011582 err = i40e_init_interrupt_scheme(pf);
11583 if (err)
11584 goto err_switch_setup;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011585
Mitch Williams505682c2014-05-20 08:01:37 +000011586 /* The number of VSIs reported by the FW is the minimum guaranteed
11587 * to us; HW supports far more and we share the remaining pool with
11588 * the other PFs. We allocate space for more than the guarantee with
11589 * the understanding that we might not get them all later.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011590 */
Mitch Williams505682c2014-05-20 08:01:37 +000011591 if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
11592 pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
11593 else
11594 pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
11595
11596 /* Set up the *vsi struct and our local tracking of the MAIN PF vsi. */
Jesse Brandeburgd17038d2015-12-23 12:05:55 -080011597 pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
11598 GFP_KERNEL);
Wei Yongjuned87ac02013-09-24 05:17:25 +000011599 if (!pf->vsi) {
11600 err = -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011601 goto err_switch_setup;
Wei Yongjuned87ac02013-09-24 05:17:25 +000011602 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011603
Anjali Singhai Jainfa11cb32015-05-27 12:06:14 -040011604#ifdef CONFIG_PCI_IOV
11605 /* prep for VF support */
11606 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
11607 (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
Jacob Keller0da36b92017-04-19 09:25:55 -040011608 !test_bit(__I40E_BAD_EEPROM, pf->state)) {
Anjali Singhai Jainfa11cb32015-05-27 12:06:14 -040011609 if (pci_num_vf(pdev))
11610 pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
11611 }
11612#endif
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000011613 err = i40e_setup_pf_switch(pf, false);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011614 if (err) {
11615 dev_info(&pdev->dev, "setup_pf_switch failed: %d\n", err);
11616 goto err_vsis;
11617 }
Helin Zhang58fc3262015-10-01 14:37:38 -040011618
11619 /* Make sure flow control is set according to current settings */
11620 err = i40e_set_fc(hw, &set_fc_aq_fail, true);
11621 if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_GET)
11622 dev_dbg(&pf->pdev->dev,
11623 "Set fc with err %s aq_err %s on get_phy_cap\n",
11624 i40e_stat_str(hw, err),
11625 i40e_aq_str(hw, hw->aq.asq_last_status));
11626 if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_SET)
11627 dev_dbg(&pf->pdev->dev,
11628 "Set fc with err %s aq_err %s on set_phy_config\n",
11629 i40e_stat_str(hw, err),
11630 i40e_aq_str(hw, hw->aq.asq_last_status));
11631 if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_UPDATE)
11632 dev_dbg(&pf->pdev->dev,
11633 "Set fc with err %s aq_err %s on get_link_info\n",
11634 i40e_stat_str(hw, err),
11635 i40e_aq_str(hw, hw->aq.asq_last_status));
11636
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +000011637 /* if FDIR VSI was set up, start it now */
Mitch Williams505682c2014-05-20 08:01:37 +000011638 for (i = 0; i < pf->num_alloc_vsi; i++) {
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +000011639 if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
11640 i40e_vsi_open(pf->vsi[i]);
11641 break;
11642 }
11643 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011644
Shannon Nelson2f0aff42016-01-04 10:33:08 -080011645 /* The driver only wants link up/down and module qualification
11646 * reports from firmware. Note the negative logic.
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +000011647 */
11648 err = i40e_aq_set_phy_int_mask(&pf->hw,
Shannon Nelson2f0aff42016-01-04 10:33:08 -080011649 ~(I40E_AQ_EVENT_LINK_UPDOWN |
Shannon Nelson867a79e2016-03-18 12:18:15 -070011650 I40E_AQ_EVENT_MEDIA_NA |
Shannon Nelson2f0aff42016-01-04 10:33:08 -080011651 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +000011652 if (err)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040011653 dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
11654 i40e_stat_str(&pf->hw, err),
11655 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +000011656
Anjali Singhai Jain4f2f017c2015-10-21 19:47:07 -040011657 /* Reconfigure hardware for allowing smaller MSS in the case
11658 * of TSO, so that we avoid the MDD being fired and causing
11659 * a reset in the case of small MSS+TSO.
11660 */
11661 val = rd32(hw, I40E_REG_MSS);
11662 if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
11663 val &= ~I40E_REG_MSS_MIN_MASK;
11664 val |= I40E_64BYTE_MSS;
11665 wr32(hw, I40E_REG_MSS, val);
11666 }
11667
Jacob Kellerd36e41d2017-06-23 04:24:46 -040011668 if (pf->hw_features & I40E_HW_RESTART_AUTONEG) {
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +000011669 msleep(75);
11670 err = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
11671 if (err)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040011672 dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
11673 i40e_stat_str(&pf->hw, err),
11674 i40e_aq_str(&pf->hw,
11675 pf->hw.aq.asq_last_status));
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +000011676 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011677 /* The main driver is (mostly) up and happy. We need to set this state
11678 * before setting up the misc vector or we get a race and the vector
11679 * ends up disabled forever.
11680 */
Mauro S. M. Rodrigues9e6c9c02017-05-12 23:26:56 -030011681 clear_bit(__I40E_DOWN, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011682
11683 /* In case of MSIX we are going to setup the misc vector right here
11684 * to handle admin queue events etc. In case of legacy and MSI
11685 * the misc functionality and queue processing is combined in
11686 * the same vector and that gets setup at open.
11687 */
11688 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
11689 err = i40e_setup_misc_vector(pf);
11690 if (err) {
11691 dev_info(&pdev->dev,
11692 "setup of misc vector failed: %d\n", err);
11693 goto err_vsis;
11694 }
11695 }
11696
Greg Rosedf805f62014-04-04 04:43:16 +000011697#ifdef CONFIG_PCI_IOV
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011698 /* prep for VF support */
11699 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
Shannon Nelson4eb3f762014-03-06 08:59:58 +000011700 (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
Jacob Keller0da36b92017-04-19 09:25:55 -040011701 !test_bit(__I40E_BAD_EEPROM, pf->state)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011702 /* disable link interrupts for VFs */
11703 val = rd32(hw, I40E_PFGEN_PORTMDIO_NUM);
11704 val &= ~I40E_PFGEN_PORTMDIO_NUM_VFLINK_STAT_ENA_MASK;
11705 wr32(hw, I40E_PFGEN_PORTMDIO_NUM, val);
11706 i40e_flush(hw);
Mitch Williams4aeec012014-02-13 03:48:47 -080011707
11708 if (pci_num_vf(pdev)) {
11709 dev_info(&pdev->dev,
11710 "Active VFs found, allocating resources.\n");
11711 err = i40e_alloc_vfs(pf, pci_num_vf(pdev));
11712 if (err)
11713 dev_info(&pdev->dev,
11714 "Error %d allocating resources for existing VFs\n",
11715 err);
11716 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011717 }
Greg Rosedf805f62014-04-04 04:43:16 +000011718#endif /* CONFIG_PCI_IOV */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011719
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060011720 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11721 pf->iwarp_base_vector = i40e_get_lump(pf, pf->irq_pile,
11722 pf->num_iwarp_msix,
11723 I40E_IWARP_IRQ_PILE_ID);
11724 if (pf->iwarp_base_vector < 0) {
11725 dev_info(&pdev->dev,
11726 "failed to get tracking for %d vectors for IWARP err=%d\n",
11727 pf->num_iwarp_msix, pf->iwarp_base_vector);
11728 pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
11729 }
11730 }
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +000011731
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011732 i40e_dbg_pf_init(pf);
11733
11734 /* tell the firmware that we're starting */
Jesse Brandeburg44033fa2014-04-23 04:50:15 +000011735 i40e_send_version(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011736
11737 /* since everything's happy, start the service_task timer */
11738 mod_timer(&pf->service_timer,
11739 round_jiffies(jiffies + pf->service_timer_period));
11740
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060011741 /* add this PF to client device list and launch a client service task */
Mitch Williams004eb612017-04-04 12:40:16 -070011742 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11743 err = i40e_lan_add_device(pf);
11744 if (err)
11745 dev_info(&pdev->dev, "Failed to add PF to client API service list: %d\n",
11746 err);
11747 }
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060011748
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040011749#define PCI_SPEED_SIZE 8
11750#define PCI_WIDTH_SIZE 8
11751 /* Devices on the IOSF bus do not have this information
11752 * and will report PCI Gen 1 x 1 by default so don't bother
11753 * checking them.
11754 */
Jacob Kellerd36e41d2017-06-23 04:24:46 -040011755 if (!(pf->hw_features & I40E_HW_NO_PCI_LINK_CHECK)) {
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040011756 char speed[PCI_SPEED_SIZE] = "Unknown";
11757 char width[PCI_WIDTH_SIZE] = "Unknown";
Catherine Sullivand4dfb812013-11-28 06:39:21 +000011758
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040011759 /* Get the negotiated link width and speed from PCI config
11760 * space
11761 */
11762 pcie_capability_read_word(pf->pdev, PCI_EXP_LNKSTA,
11763 &link_status);
Catherine Sullivand4dfb812013-11-28 06:39:21 +000011764
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040011765 i40e_set_pci_config_data(hw, link_status);
Catherine Sullivand4dfb812013-11-28 06:39:21 +000011766
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040011767 switch (hw->bus.speed) {
11768 case i40e_bus_speed_8000:
11769 strncpy(speed, "8.0", PCI_SPEED_SIZE); break;
11770 case i40e_bus_speed_5000:
11771 strncpy(speed, "5.0", PCI_SPEED_SIZE); break;
11772 case i40e_bus_speed_2500:
11773 strncpy(speed, "2.5", PCI_SPEED_SIZE); break;
11774 default:
11775 break;
11776 }
11777 switch (hw->bus.width) {
11778 case i40e_bus_width_pcie_x8:
11779 strncpy(width, "8", PCI_WIDTH_SIZE); break;
11780 case i40e_bus_width_pcie_x4:
11781 strncpy(width, "4", PCI_WIDTH_SIZE); break;
11782 case i40e_bus_width_pcie_x2:
11783 strncpy(width, "2", PCI_WIDTH_SIZE); break;
11784 case i40e_bus_width_pcie_x1:
11785 strncpy(width, "1", PCI_WIDTH_SIZE); break;
11786 default:
11787 break;
11788 }
11789
11790 dev_info(&pdev->dev, "PCI-Express: Speed %sGT/s Width x%s\n",
11791 speed, width);
11792
11793 if (hw->bus.width < i40e_bus_width_pcie_x8 ||
11794 hw->bus.speed < i40e_bus_speed_8000) {
11795 dev_warn(&pdev->dev, "PCI-Express bandwidth available for this device may be insufficient for optimal performance.\n");
11796 dev_warn(&pdev->dev, "Please move the device to a different PCI-e link with more lanes and/or higher transfer rate.\n");
11797 }
Catherine Sullivand4dfb812013-11-28 06:39:21 +000011798 }
11799
Catherine Sullivane8278452015-02-06 08:52:08 +000011800 /* get the requested speeds from the fw */
11801 err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities, NULL);
11802 if (err)
Neerav Parikh8279e492015-09-03 17:18:50 -040011803 dev_dbg(&pf->pdev->dev, "get requested speeds ret = %s last_status = %s\n",
11804 i40e_stat_str(&pf->hw, err),
11805 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Catherine Sullivane8278452015-02-06 08:52:08 +000011806 pf->hw.phy.link_info.requested_speeds = abilities.link_speed;
11807
Catherine Sullivanfc72dbc2015-09-01 11:36:30 -040011808 /* get the supported phy types from the fw */
11809 err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities, NULL);
11810 if (err)
11811 dev_dbg(&pf->pdev->dev, "get supported phy types ret = %s last_status = %s\n",
11812 i40e_stat_str(&pf->hw, err),
11813 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Catherine Sullivanfc72dbc2015-09-01 11:36:30 -040011814
Anjali Singhai Jaine7358f52015-10-01 14:37:34 -040011815 /* Add a filter to drop all Flow control frames from any VSI from being
11816 * transmitted. By doing so we stop a malicious VF from sending out
11817 * PAUSE or PFC frames and potentially controlling traffic for other
11818 * PF/VF VSIs.
11819 * The FW can still send Flow control frames if enabled.
11820 */
11821 i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
11822 pf->main_vsi_seid);
11823
Carolyn Wyborny31b606d2016-02-17 16:12:12 -080011824 if ((pf->hw.device_id == I40E_DEV_ID_10G_BASE_T) ||
Henry Tieman4f9b4302016-11-08 13:05:18 -080011825 (pf->hw.device_id == I40E_DEV_ID_10G_BASE_T4))
Jacob Kellerd36e41d2017-06-23 04:24:46 -040011826 pf->hw_features |= I40E_HW_PHY_CONTROLS_LEDS;
Harshitha Ramamurthy4ad9f4f2016-11-08 13:05:09 -080011827 if (pf->hw.device_id == I40E_DEV_ID_SFP_I_X722)
Jacob Kellerd36e41d2017-06-23 04:24:46 -040011828 pf->hw_features |= I40E_HW_HAVE_CRT_RETIMER;
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000011829 /* print a string summarizing features */
11830 i40e_print_features(pf);
11831
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011832 return 0;
11833
11834 /* Unwind what we've done if something failed in the setup */
11835err_vsis:
Mauro S. M. Rodrigues9e6c9c02017-05-12 23:26:56 -030011836 set_bit(__I40E_DOWN, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011837 i40e_clear_interrupt_scheme(pf);
11838 kfree(pf->vsi);
Shannon Nelson04b03012013-11-28 06:39:34 +000011839err_switch_setup:
11840 i40e_reset_interrupt_capability(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011841 del_timer_sync(&pf->service_timer);
11842err_mac_addr:
11843err_configure_lan_hmc:
11844 (void)i40e_shutdown_lan_hmc(hw);
11845err_init_lan_hmc:
11846 kfree(pf->qp_pile);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011847err_sw_init:
11848err_adminq_setup:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011849err_pf_reset:
11850 iounmap(hw->hw_addr);
11851err_ioremap:
11852 kfree(pf);
11853err_pf_alloc:
11854 pci_disable_pcie_error_reporting(pdev);
Johannes Thumshirn56d766d2016-06-07 09:44:05 +020011855 pci_release_mem_regions(pdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011856err_pci_reg:
11857err_dma:
11858 pci_disable_device(pdev);
11859 return err;
11860}
11861
11862/**
11863 * i40e_remove - Device removal routine
11864 * @pdev: PCI device information struct
11865 *
11866 * i40e_remove is called by the PCI subsystem to alert the driver
11867 * that is should release a PCI device. This could be caused by a
11868 * Hot-Plug event, or because the driver is going to be removed from
11869 * memory.
11870 **/
11871static void i40e_remove(struct pci_dev *pdev)
11872{
11873 struct i40e_pf *pf = pci_get_drvdata(pdev);
Carolyn Wybornybcab2db2015-09-28 14:16:55 -040011874 struct i40e_hw *hw = &pf->hw;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011875 i40e_status ret_code;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011876 int i;
11877
11878 i40e_dbg_pf_exit(pf);
11879
Jacob Kellerbeb0dff2014-01-11 05:43:19 +000011880 i40e_ptp_stop(pf);
11881
Carolyn Wybornybcab2db2015-09-28 14:16:55 -040011882 /* Disable RSS in hw */
Shannon Nelson272cdaf22016-02-17 16:12:21 -080011883 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), 0);
11884 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), 0);
Carolyn Wybornybcab2db2015-09-28 14:16:55 -040011885
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011886 /* no more scheduling of any task */
Jacob Keller0da36b92017-04-19 09:25:55 -040011887 set_bit(__I40E_SUSPENDED, pf->state);
Mauro S. M. Rodrigues9e6c9c02017-05-12 23:26:56 -030011888 set_bit(__I40E_DOWN, pf->state);
Shannon Nelsonc99abb42016-03-10 14:59:45 -080011889 if (pf->service_timer.data)
11890 del_timer_sync(&pf->service_timer);
11891 if (pf->service_task.func)
11892 cancel_work_sync(&pf->service_task);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011893
Mitch Williams921c4672017-03-30 00:46:08 -070011894 /* Client close must be called explicitly here because the timer
11895 * has been stopped.
11896 */
11897 i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
11898
Mitch Williamseb2d80b2014-02-13 03:48:48 -080011899 if (pf->flags & I40E_FLAG_SRIOV_ENABLED) {
11900 i40e_free_vfs(pf);
11901 pf->flags &= ~I40E_FLAG_SRIOV_ENABLED;
11902 }
11903
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011904 i40e_fdir_teardown(pf);
11905
11906 /* If there is a switch structure or any orphans, remove them.
11907 * This will leave only the PF's VSI remaining.
11908 */
11909 for (i = 0; i < I40E_MAX_VEB; i++) {
11910 if (!pf->veb[i])
11911 continue;
11912
11913 if (pf->veb[i]->uplink_seid == pf->mac_seid ||
11914 pf->veb[i]->uplink_seid == 0)
11915 i40e_switch_branch_release(pf->veb[i]);
11916 }
11917
11918 /* Now we can shutdown the PF's VSI, just before we kill
11919 * adminq and hmc.
11920 */
11921 if (pf->vsi[pf->lan_vsi])
11922 i40e_vsi_release(pf->vsi[pf->lan_vsi]);
11923
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060011924 /* remove attached clients */
Mitch Williams004eb612017-04-04 12:40:16 -070011925 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11926 ret_code = i40e_lan_del_device(pf);
11927 if (ret_code)
11928 dev_warn(&pdev->dev, "Failed to delete client device: %d\n",
11929 ret_code);
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060011930 }
11931
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011932 /* shutdown and destroy the HMC */
Jesse Brandeburgf734dff2016-01-15 14:33:11 -080011933 if (hw->hmc.hmc_obj) {
11934 ret_code = i40e_shutdown_lan_hmc(hw);
Shannon Nelson60442de2014-04-23 04:50:13 +000011935 if (ret_code)
11936 dev_warn(&pdev->dev,
11937 "Failed to destroy the HMC resources: %d\n",
11938 ret_code);
11939 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011940
11941 /* shutdown the adminq */
Henry Tiemanac9c5c62016-09-06 18:05:11 -070011942 i40e_shutdown_adminq(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011943
Jesse Brandeburg8ddb3322015-11-18 15:47:06 -080011944 /* destroy the locks only once, here */
11945 mutex_destroy(&hw->aq.arq_mutex);
11946 mutex_destroy(&hw->aq.asq_mutex);
11947
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011948 /* Clear all dynamic memory lists of rings, q_vectors, and VSIs */
11949 i40e_clear_interrupt_scheme(pf);
Mitch Williams505682c2014-05-20 08:01:37 +000011950 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011951 if (pf->vsi[i]) {
11952 i40e_vsi_clear_rings(pf->vsi[i]);
11953 i40e_vsi_clear(pf->vsi[i]);
11954 pf->vsi[i] = NULL;
11955 }
11956 }
11957
11958 for (i = 0; i < I40E_MAX_VEB; i++) {
11959 kfree(pf->veb[i]);
11960 pf->veb[i] = NULL;
11961 }
11962
11963 kfree(pf->qp_pile);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011964 kfree(pf->vsi);
11965
Jesse Brandeburgf734dff2016-01-15 14:33:11 -080011966 iounmap(hw->hw_addr);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011967 kfree(pf);
Johannes Thumshirn56d766d2016-06-07 09:44:05 +020011968 pci_release_mem_regions(pdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011969
11970 pci_disable_pcie_error_reporting(pdev);
11971 pci_disable_device(pdev);
11972}
11973
11974/**
11975 * i40e_pci_error_detected - warning that something funky happened in PCI land
11976 * @pdev: PCI device information struct
11977 *
11978 * Called to warn that something happened and the error handling steps
11979 * are in progress. Allows the driver to quiesce things, be ready for
11980 * remediation.
11981 **/
11982static pci_ers_result_t i40e_pci_error_detected(struct pci_dev *pdev,
11983 enum pci_channel_state error)
11984{
11985 struct i40e_pf *pf = pci_get_drvdata(pdev);
11986
11987 dev_info(&pdev->dev, "%s: error %d\n", __func__, error);
11988
Guilherme G Piccoliedfc23ee2016-10-03 00:31:12 -070011989 if (!pf) {
11990 dev_info(&pdev->dev,
11991 "Cannot recover - error happened during device probe\n");
11992 return PCI_ERS_RESULT_DISCONNECT;
11993 }
11994
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011995 /* shutdown all operations */
Jacob Kellerdfc4ff62017-06-07 05:43:13 -040011996 if (!test_bit(__I40E_SUSPENDED, pf->state))
11997 i40e_prep_for_reset(pf, false);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011998
11999 /* Request a slot reset */
12000 return PCI_ERS_RESULT_NEED_RESET;
12001}
12002
12003/**
12004 * i40e_pci_error_slot_reset - a PCI slot reset just happened
12005 * @pdev: PCI device information struct
12006 *
12007 * Called to find if the driver can work with the device now that
12008 * the pci slot has been reset. If a basic connection seems good
12009 * (registers are readable and have sane content) then return a
12010 * happy little PCI_ERS_RESULT_xxx.
12011 **/
12012static pci_ers_result_t i40e_pci_error_slot_reset(struct pci_dev *pdev)
12013{
12014 struct i40e_pf *pf = pci_get_drvdata(pdev);
12015 pci_ers_result_t result;
12016 int err;
12017 u32 reg;
12018
Shannon Nelsonfb43201f2015-08-26 15:14:17 -040012019 dev_dbg(&pdev->dev, "%s\n", __func__);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012020 if (pci_enable_device_mem(pdev)) {
12021 dev_info(&pdev->dev,
12022 "Cannot re-enable PCI device after reset.\n");
12023 result = PCI_ERS_RESULT_DISCONNECT;
12024 } else {
12025 pci_set_master(pdev);
12026 pci_restore_state(pdev);
12027 pci_save_state(pdev);
12028 pci_wake_from_d3(pdev, false);
12029
12030 reg = rd32(&pf->hw, I40E_GLGEN_RTRIG);
12031 if (reg == 0)
12032 result = PCI_ERS_RESULT_RECOVERED;
12033 else
12034 result = PCI_ERS_RESULT_DISCONNECT;
12035 }
12036
12037 err = pci_cleanup_aer_uncorrect_error_status(pdev);
12038 if (err) {
12039 dev_info(&pdev->dev,
12040 "pci_cleanup_aer_uncorrect_error_status failed 0x%0x\n",
12041 err);
12042 /* non-fatal, continue */
12043 }
12044
12045 return result;
12046}
12047
12048/**
Alan Brady19b79602017-08-29 05:32:39 -040012049 * i40e_pci_error_reset_prepare - prepare device driver for pci reset
12050 * @pdev: PCI device information struct
12051 */
12052static void i40e_pci_error_reset_prepare(struct pci_dev *pdev)
12053{
12054 struct i40e_pf *pf = pci_get_drvdata(pdev);
12055
12056 i40e_prep_for_reset(pf, false);
12057}
12058
12059/**
12060 * i40e_pci_error_reset_done - pci reset done, device driver reset can begin
12061 * @pdev: PCI device information struct
12062 */
12063static void i40e_pci_error_reset_done(struct pci_dev *pdev)
12064{
12065 struct i40e_pf *pf = pci_get_drvdata(pdev);
12066
12067 i40e_reset_and_rebuild(pf, false, false);
12068}
12069
12070/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012071 * i40e_pci_error_resume - restart operations after PCI error recovery
12072 * @pdev: PCI device information struct
12073 *
12074 * Called to allow the driver to bring things back up after PCI error
12075 * and/or reset recovery has finished.
12076 **/
12077static void i40e_pci_error_resume(struct pci_dev *pdev)
12078{
12079 struct i40e_pf *pf = pci_get_drvdata(pdev);
12080
Shannon Nelsonfb43201f2015-08-26 15:14:17 -040012081 dev_dbg(&pdev->dev, "%s\n", __func__);
Jacob Keller0da36b92017-04-19 09:25:55 -040012082 if (test_bit(__I40E_SUSPENDED, pf->state))
Shannon Nelson9007bcc2013-11-26 10:49:23 +000012083 return;
12084
Jacob Kellerdfc4ff62017-06-07 05:43:13 -040012085 i40e_handle_reset_warning(pf, false);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012086}
12087
Shannon Nelson9007bcc2013-11-26 10:49:23 +000012088/**
Joshua Hay1d680052016-12-12 15:44:08 -080012089 * i40e_enable_mc_magic_wake - enable multicast magic packet wake up
12090 * using the mac_address_write admin q function
12091 * @pf: pointer to i40e_pf struct
12092 **/
12093static void i40e_enable_mc_magic_wake(struct i40e_pf *pf)
12094{
12095 struct i40e_hw *hw = &pf->hw;
12096 i40e_status ret;
12097 u8 mac_addr[6];
12098 u16 flags = 0;
12099
12100 /* Get current MAC address in case it's an LAA */
12101 if (pf->vsi[pf->lan_vsi] && pf->vsi[pf->lan_vsi]->netdev) {
12102 ether_addr_copy(mac_addr,
12103 pf->vsi[pf->lan_vsi]->netdev->dev_addr);
12104 } else {
12105 dev_err(&pf->pdev->dev,
12106 "Failed to retrieve MAC address; using default\n");
12107 ether_addr_copy(mac_addr, hw->mac.addr);
12108 }
12109
12110 /* The FW expects the mac address write cmd to first be called with
12111 * one of these flags before calling it again with the multicast
12112 * enable flags.
12113 */
12114 flags = I40E_AQC_WRITE_TYPE_LAA_WOL;
12115
12116 if (hw->func_caps.flex10_enable && hw->partition_id != 1)
12117 flags = I40E_AQC_WRITE_TYPE_LAA_ONLY;
12118
12119 ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
12120 if (ret) {
12121 dev_err(&pf->pdev->dev,
12122 "Failed to update MAC address registers; cannot enable Multicast Magic packet wake up");
12123 return;
12124 }
12125
12126 flags = I40E_AQC_MC_MAG_EN
12127 | I40E_AQC_WOL_PRESERVE_ON_PFR
12128 | I40E_AQC_WRITE_TYPE_UPDATE_MC_MAG;
12129 ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
12130 if (ret)
12131 dev_err(&pf->pdev->dev,
12132 "Failed to enable Multicast Magic Packet wake up\n");
12133}
12134
12135/**
Shannon Nelson9007bcc2013-11-26 10:49:23 +000012136 * i40e_shutdown - PCI callback for shutting down
12137 * @pdev: PCI device information struct
12138 **/
12139static void i40e_shutdown(struct pci_dev *pdev)
12140{
12141 struct i40e_pf *pf = pci_get_drvdata(pdev);
Shannon Nelson8e2773a2013-11-28 06:39:22 +000012142 struct i40e_hw *hw = &pf->hw;
Shannon Nelson9007bcc2013-11-26 10:49:23 +000012143
Jacob Keller0da36b92017-04-19 09:25:55 -040012144 set_bit(__I40E_SUSPENDED, pf->state);
Mauro S. M. Rodrigues9e6c9c02017-05-12 23:26:56 -030012145 set_bit(__I40E_DOWN, pf->state);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000012146 rtnl_lock();
Maciej Sosin373149f2017-04-05 07:50:55 -040012147 i40e_prep_for_reset(pf, true);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000012148 rtnl_unlock();
12149
Shannon Nelson8e2773a2013-11-28 06:39:22 +000012150 wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
12151 wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
12152
Catherine Sullivan02b42492015-07-10 19:35:59 -040012153 del_timer_sync(&pf->service_timer);
12154 cancel_work_sync(&pf->service_task);
12155 i40e_fdir_teardown(pf);
12156
Mitch Williams921c4672017-03-30 00:46:08 -070012157 /* Client close must be called explicitly here because the timer
12158 * has been stopped.
12159 */
12160 i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
12161
Jacob Kellerd36e41d2017-06-23 04:24:46 -040012162 if (pf->wol_en && (pf->hw_features & I40E_HW_WOL_MC_MAGIC_PKT_WAKE))
Joshua Hay1d680052016-12-12 15:44:08 -080012163 i40e_enable_mc_magic_wake(pf);
12164
Jacob Kellerdfc4ff62017-06-07 05:43:13 -040012165 i40e_prep_for_reset(pf, false);
Catherine Sullivan02b42492015-07-10 19:35:59 -040012166
12167 wr32(hw, I40E_PFPM_APM,
12168 (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
12169 wr32(hw, I40E_PFPM_WUFC,
12170 (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
12171
Shannon Nelsone1477582015-02-21 06:44:33 +000012172 i40e_clear_interrupt_scheme(pf);
12173
Shannon Nelson9007bcc2013-11-26 10:49:23 +000012174 if (system_state == SYSTEM_POWER_OFF) {
Shannon Nelson8e2773a2013-11-28 06:39:22 +000012175 pci_wake_from_d3(pdev, pf->wol_en);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000012176 pci_set_power_state(pdev, PCI_D3hot);
12177 }
12178}
12179
12180#ifdef CONFIG_PM
12181/**
Jacob Keller0e5d3da2017-07-14 09:27:03 -040012182 * i40e_suspend - PM callback for moving to D3
12183 * @dev: generic device information structure
Shannon Nelson9007bcc2013-11-26 10:49:23 +000012184 **/
Jacob Keller0e5d3da2017-07-14 09:27:03 -040012185static int i40e_suspend(struct device *dev)
Shannon Nelson9007bcc2013-11-26 10:49:23 +000012186{
Jacob Keller0e5d3da2017-07-14 09:27:03 -040012187 struct pci_dev *pdev = to_pci_dev(dev);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000012188 struct i40e_pf *pf = pci_get_drvdata(pdev);
Shannon Nelson8e2773a2013-11-28 06:39:22 +000012189 struct i40e_hw *hw = &pf->hw;
Shannon Nelson9007bcc2013-11-26 10:49:23 +000012190
Jacob Keller401586c2017-07-14 09:27:04 -040012191 /* If we're already suspended, then there is nothing to do */
12192 if (test_and_set_bit(__I40E_SUSPENDED, pf->state))
12193 return 0;
12194
Mauro S. M. Rodrigues9e6c9c02017-05-12 23:26:56 -030012195 set_bit(__I40E_DOWN, pf->state);
Mitch Williams3932dbf2015-03-31 00:45:04 -070012196
Jacob Keller5c499222017-07-14 09:27:05 -040012197 /* Ensure service task will not be running */
12198 del_timer_sync(&pf->service_timer);
12199 cancel_work_sync(&pf->service_task);
12200
Jacob Kellerd36e41d2017-06-23 04:24:46 -040012201 if (pf->wol_en && (pf->hw_features & I40E_HW_WOL_MC_MAGIC_PKT_WAKE))
Joshua Hay1d680052016-12-12 15:44:08 -080012202 i40e_enable_mc_magic_wake(pf);
12203
Jacob Kellerdfc4ff62017-06-07 05:43:13 -040012204 i40e_prep_for_reset(pf, false);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000012205
Shannon Nelson8e2773a2013-11-28 06:39:22 +000012206 wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
12207 wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
12208
Jacob Kellerb980c062017-07-14 09:27:06 -040012209 /* Clear the interrupt scheme and release our IRQs so that the system
12210 * can safely hibernate even when there are a large number of CPUs.
12211 * Otherwise hibernation might fail when mapping all the vectors back
12212 * to CPU0.
12213 */
12214 i40e_clear_interrupt_scheme(pf);
Jacob Kellerc17401a2017-07-14 09:27:02 -040012215
Jacob Keller0e5d3da2017-07-14 09:27:03 -040012216 return 0;
Shannon Nelson9007bcc2013-11-26 10:49:23 +000012217}
12218
12219/**
Jacob Keller0e5d3da2017-07-14 09:27:03 -040012220 * i40e_resume - PM callback for waking up from D3
12221 * @dev: generic device information structure
Shannon Nelson9007bcc2013-11-26 10:49:23 +000012222 **/
Jacob Keller0e5d3da2017-07-14 09:27:03 -040012223static int i40e_resume(struct device *dev)
Shannon Nelson9007bcc2013-11-26 10:49:23 +000012224{
Jacob Keller0e5d3da2017-07-14 09:27:03 -040012225 struct pci_dev *pdev = to_pci_dev(dev);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000012226 struct i40e_pf *pf = pci_get_drvdata(pdev);
Jacob Kellerb980c062017-07-14 09:27:06 -040012227 int err;
Shannon Nelson9007bcc2013-11-26 10:49:23 +000012228
Jacob Keller401586c2017-07-14 09:27:04 -040012229 /* If we're not suspended, then there is nothing to do */
12230 if (!test_bit(__I40E_SUSPENDED, pf->state))
12231 return 0;
12232
Jacob Kellerb980c062017-07-14 09:27:06 -040012233 /* We cleared the interrupt scheme when we suspended, so we need to
12234 * restore it now to resume device functionality.
12235 */
12236 err = i40e_restore_interrupt_scheme(pf);
12237 if (err) {
12238 dev_err(&pdev->dev, "Cannot restore interrupt scheme: %d\n",
12239 err);
12240 }
12241
Jacob Keller401586c2017-07-14 09:27:04 -040012242 clear_bit(__I40E_DOWN, pf->state);
12243 i40e_reset_and_rebuild(pf, false, false);
12244
12245 /* Clear suspended state last after everything is recovered */
12246 clear_bit(__I40E_SUSPENDED, pf->state);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000012247
Jacob Keller5c499222017-07-14 09:27:05 -040012248 /* Restart the service task */
12249 mod_timer(&pf->service_timer,
12250 round_jiffies(jiffies + pf->service_timer_period));
12251
Shannon Nelson9007bcc2013-11-26 10:49:23 +000012252 return 0;
12253}
12254
Jacob Keller0e5d3da2017-07-14 09:27:03 -040012255#endif /* CONFIG_PM */
12256
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012257static const struct pci_error_handlers i40e_err_handler = {
12258 .error_detected = i40e_pci_error_detected,
12259 .slot_reset = i40e_pci_error_slot_reset,
Alan Brady19b79602017-08-29 05:32:39 -040012260 .reset_prepare = i40e_pci_error_reset_prepare,
12261 .reset_done = i40e_pci_error_reset_done,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012262 .resume = i40e_pci_error_resume,
12263};
12264
Jacob Keller0e5d3da2017-07-14 09:27:03 -040012265static SIMPLE_DEV_PM_OPS(i40e_pm_ops, i40e_suspend, i40e_resume);
12266
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012267static struct pci_driver i40e_driver = {
12268 .name = i40e_driver_name,
12269 .id_table = i40e_pci_tbl,
12270 .probe = i40e_probe,
12271 .remove = i40e_remove,
Shannon Nelson9007bcc2013-11-26 10:49:23 +000012272#ifdef CONFIG_PM
Jacob Keller0e5d3da2017-07-14 09:27:03 -040012273 .driver = {
12274 .pm = &i40e_pm_ops,
12275 },
12276#endif /* CONFIG_PM */
Shannon Nelson9007bcc2013-11-26 10:49:23 +000012277 .shutdown = i40e_shutdown,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012278 .err_handler = &i40e_err_handler,
12279 .sriov_configure = i40e_pci_sriov_configure,
12280};
12281
12282/**
12283 * i40e_init_module - Driver registration routine
12284 *
12285 * i40e_init_module is the first routine called when the driver is
12286 * loaded. All it does is register with the PCI subsystem.
12287 **/
12288static int __init i40e_init_module(void)
12289{
12290 pr_info("%s: %s - version %s\n", i40e_driver_name,
12291 i40e_driver_string, i40e_driver_version_str);
12292 pr_info("%s: %s\n", i40e_driver_name, i40e_copyright);
Greg Rose96664482015-02-06 08:52:13 +000012293
Jacob Keller4d5957c2017-06-20 15:16:54 -070012294 /* There is no need to throttle the number of active tasks because
12295 * each device limits its own task using a state bit for scheduling
12296 * the service task, and the device tasks do not interfere with each
12297 * other, so we don't set a max task limit. We must set WQ_MEM_RECLAIM
12298 * since we need to be able to guarantee forward progress even under
12299 * memory pressure.
Jesse Brandeburg2803b162015-12-22 14:25:08 -080012300 */
Jacob Keller4d5957c2017-06-20 15:16:54 -070012301 i40e_wq = alloc_workqueue("%s", WQ_MEM_RECLAIM, 0, i40e_driver_name);
Jesse Brandeburg2803b162015-12-22 14:25:08 -080012302 if (!i40e_wq) {
12303 pr_err("%s: Failed to create workqueue\n", i40e_driver_name);
12304 return -ENOMEM;
12305 }
12306
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012307 i40e_dbg_init();
12308 return pci_register_driver(&i40e_driver);
12309}
12310module_init(i40e_init_module);
12311
12312/**
12313 * i40e_exit_module - Driver exit cleanup routine
12314 *
12315 * i40e_exit_module is called just before the driver is removed
12316 * from memory.
12317 **/
12318static void __exit i40e_exit_module(void)
12319{
12320 pci_unregister_driver(&i40e_driver);
Jesse Brandeburg2803b162015-12-22 14:25:08 -080012321 destroy_workqueue(i40e_wq);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012322 i40e_dbg_exit();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012323}
12324module_exit(i40e_exit_module);