blob: e23105bee6d10ee7db4d0dbb7dae5ee196268c7f [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{
Jacob Kellerbe664cb2017-08-29 05:32:31 -04002882 int cpu;
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002883
Amritha Nambiar8f88b302017-09-07 04:00:17 -07002884 if (!ring->q_vector || !ring->netdev || ring->ch)
Jesse Brandeburg9a660ee2015-02-26 16:13:22 +00002885 return;
2886
Jacob Keller6f853d42017-09-07 08:05:53 -04002887 /* We only initialize XPS once, so as not to overwrite user settings */
2888 if (test_and_set_bit(__I40E_TX_XPS_INIT_DONE, ring->state))
2889 return;
Jesse Brandeburg0e4425e2015-11-05 17:01:01 -08002890
Jacob Keller6f853d42017-09-07 08:05:53 -04002891 cpu = cpumask_local_spread(ring->q_vector->v_idx, -1);
2892 netif_set_xps_queue(ring->netdev, get_cpu_mask(cpu),
2893 ring->queue_index);
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002894}
2895
2896/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002897 * i40e_configure_tx_ring - Configure a transmit ring context and rest
2898 * @ring: The Tx ring to configure
2899 *
2900 * Configure the Tx descriptor ring in the HMC context.
2901 **/
2902static int i40e_configure_tx_ring(struct i40e_ring *ring)
2903{
2904 struct i40e_vsi *vsi = ring->vsi;
2905 u16 pf_q = vsi->base_queue + ring->queue_index;
2906 struct i40e_hw *hw = &vsi->back->hw;
2907 struct i40e_hmc_obj_txq tx_ctx;
2908 i40e_status err = 0;
2909 u32 qtx_ctl = 0;
2910
2911 /* some ATR related tx ring init */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08002912 if (vsi->back->flags & I40E_FLAG_FD_ATR_ENABLED) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002913 ring->atr_sample_rate = vsi->back->atr_sample_rate;
2914 ring->atr_count = 0;
2915 } else {
2916 ring->atr_sample_rate = 0;
2917 }
2918
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002919 /* configure XPS */
2920 i40e_config_xps_tx_ring(ring);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002921
2922 /* clear the context structure first */
2923 memset(&tx_ctx, 0, sizeof(tx_ctx));
2924
2925 tx_ctx.new_context = 1;
2926 tx_ctx.base = (ring->dma / 128);
2927 tx_ctx.qlen = ring->count;
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08002928 tx_ctx.fd_ena = !!(vsi->back->flags & (I40E_FLAG_FD_SB_ENABLED |
2929 I40E_FLAG_FD_ATR_ENABLED));
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00002930 tx_ctx.timesync_ena = !!(vsi->back->flags & I40E_FLAG_PTP);
Jesse Brandeburg1943d8b2014-02-14 02:14:40 +00002931 /* FDIR VSI tx ring can still use RS bit and writebacks */
2932 if (vsi->type != I40E_VSI_FDIR)
2933 tx_ctx.head_wb_ena = 1;
2934 tx_ctx.head_wb_addr = ring->dma +
2935 (ring->count * sizeof(struct i40e_tx_desc));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002936
2937 /* As part of VSI creation/update, FW allocates certain
2938 * Tx arbitration queue sets for each TC enabled for
2939 * the VSI. The FW returns the handles to these queue
2940 * sets as part of the response buffer to Add VSI,
2941 * Update VSI, etc. AQ commands. It is expected that
2942 * these queue set handles be associated with the Tx
2943 * queues by the driver as part of the TX queue context
2944 * initialization. This has to be done regardless of
2945 * DCB as by default everything is mapped to TC0.
2946 */
Amritha Nambiar8f88b302017-09-07 04:00:17 -07002947
2948 if (ring->ch)
2949 tx_ctx.rdylist =
2950 le16_to_cpu(ring->ch->info.qs_handle[ring->dcb_tc]);
2951
2952 else
2953 tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[ring->dcb_tc]);
2954
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002955 tx_ctx.rdylist_act = 0;
2956
2957 /* clear the context in the HMC */
2958 err = i40e_clear_lan_tx_queue_context(hw, pf_q);
2959 if (err) {
2960 dev_info(&vsi->back->pdev->dev,
2961 "Failed to clear LAN Tx queue context on Tx ring %d (pf_q %d), error: %d\n",
2962 ring->queue_index, pf_q, err);
2963 return -ENOMEM;
2964 }
2965
2966 /* set the context in the HMC */
2967 err = i40e_set_lan_tx_queue_context(hw, pf_q, &tx_ctx);
2968 if (err) {
2969 dev_info(&vsi->back->pdev->dev,
2970 "Failed to set LAN Tx queue context on Tx ring %d (pf_q %d, error: %d\n",
2971 ring->queue_index, pf_q, err);
2972 return -ENOMEM;
2973 }
2974
2975 /* Now associate this queue with this PCI function */
Amritha Nambiar8f88b302017-09-07 04:00:17 -07002976 if (ring->ch) {
2977 if (ring->ch->type == I40E_VSI_VMDQ2)
2978 qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
2979 else
2980 return -EINVAL;
2981
2982 qtx_ctl |= (ring->ch->vsi_number <<
2983 I40E_QTX_CTL_VFVM_INDX_SHIFT) &
2984 I40E_QTX_CTL_VFVM_INDX_MASK;
Mitch Williams7a28d882014-10-17 03:14:52 +00002985 } else {
Amritha Nambiar8f88b302017-09-07 04:00:17 -07002986 if (vsi->type == I40E_VSI_VMDQ2) {
2987 qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
2988 qtx_ctl |= ((vsi->id) << I40E_QTX_CTL_VFVM_INDX_SHIFT) &
2989 I40E_QTX_CTL_VFVM_INDX_MASK;
2990 } else {
2991 qtx_ctl = I40E_QTX_CTL_PF_QUEUE;
2992 }
Mitch Williams7a28d882014-10-17 03:14:52 +00002993 }
2994
Shannon Nelson13fd9772013-09-28 07:14:19 +00002995 qtx_ctl |= ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT) &
2996 I40E_QTX_CTL_PF_INDX_MASK);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002997 wr32(hw, I40E_QTX_CTL(pf_q), qtx_ctl);
2998 i40e_flush(hw);
2999
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003000 /* cache tail off for easier writes later */
3001 ring->tail = hw->hw_addr + I40E_QTX_TAIL(pf_q);
3002
3003 return 0;
3004}
3005
3006/**
3007 * i40e_configure_rx_ring - Configure a receive ring context
3008 * @ring: The Rx ring to configure
3009 *
3010 * Configure the Rx descriptor ring in the HMC context.
3011 **/
3012static int i40e_configure_rx_ring(struct i40e_ring *ring)
3013{
3014 struct i40e_vsi *vsi = ring->vsi;
3015 u32 chain_len = vsi->back->hw.func_caps.rx_buf_chain_len;
3016 u16 pf_q = vsi->base_queue + ring->queue_index;
3017 struct i40e_hw *hw = &vsi->back->hw;
3018 struct i40e_hmc_obj_rxq rx_ctx;
3019 i40e_status err = 0;
3020
Jesse Brandeburgbd6cd4e2017-08-29 05:32:35 -04003021 bitmap_zero(ring->state, __I40E_RING_STATE_NBITS);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003022
3023 /* clear the context structure first */
3024 memset(&rx_ctx, 0, sizeof(rx_ctx));
3025
3026 ring->rx_buf_len = vsi->rx_buf_len;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003027
Alexander Duyckdab86af2017-03-14 10:15:27 -07003028 rx_ctx.dbuff = DIV_ROUND_UP(ring->rx_buf_len,
3029 BIT_ULL(I40E_RXQ_CTX_DBUFF_SHIFT));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003030
3031 rx_ctx.base = (ring->dma / 128);
3032 rx_ctx.qlen = ring->count;
3033
Jesse Brandeburgbec60fc2016-04-18 11:33:47 -07003034 /* use 32 byte descriptors */
3035 rx_ctx.dsize = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003036
Jesse Brandeburgbec60fc2016-04-18 11:33:47 -07003037 /* descriptor type is always zero
3038 * rx_ctx.dtype = 0;
3039 */
Jesse Brandeburgb32bfa172016-04-18 11:33:42 -07003040 rx_ctx.hsplit_0 = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003041
Jesse Brandeburgb32bfa172016-04-18 11:33:42 -07003042 rx_ctx.rxmax = min_t(u16, vsi->max_frame, chain_len * ring->rx_buf_len);
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00003043 if (hw->revision_id == 0)
3044 rx_ctx.lrxqthresh = 0;
3045 else
Jacob Keller7362be92017-09-07 08:05:50 -04003046 rx_ctx.lrxqthresh = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003047 rx_ctx.crcstrip = 1;
3048 rx_ctx.l2tsel = 1;
Jesse Brandeburgc4bbac32015-09-28 11:21:48 -07003049 /* this controls whether VLAN is stripped from inner headers */
3050 rx_ctx.showiv = 0;
Catherine Sullivanacb36762014-03-06 09:02:30 +00003051 /* set the prefena field to 1 because the manual says to */
3052 rx_ctx.prefena = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003053
3054 /* clear the context in the HMC */
3055 err = i40e_clear_lan_rx_queue_context(hw, pf_q);
3056 if (err) {
3057 dev_info(&vsi->back->pdev->dev,
3058 "Failed to clear LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
3059 ring->queue_index, pf_q, err);
3060 return -ENOMEM;
3061 }
3062
3063 /* set the context in the HMC */
3064 err = i40e_set_lan_rx_queue_context(hw, pf_q, &rx_ctx);
3065 if (err) {
3066 dev_info(&vsi->back->pdev->dev,
3067 "Failed to set LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
3068 ring->queue_index, pf_q, err);
3069 return -ENOMEM;
3070 }
3071
Alexander Duyckca9ec082017-04-05 07:51:02 -04003072 /* configure Rx buffer alignment */
3073 if (!vsi->netdev || (vsi->back->flags & I40E_FLAG_LEGACY_RX))
3074 clear_ring_build_skb_enabled(ring);
3075 else
3076 set_ring_build_skb_enabled(ring);
3077
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003078 /* cache tail for quicker writes, and clear the reg before use */
3079 ring->tail = hw->hw_addr + I40E_QRX_TAIL(pf_q);
3080 writel(0, ring->tail);
3081
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07003082 i40e_alloc_rx_buffers(ring, I40E_DESC_UNUSED(ring));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003083
3084 return 0;
3085}
3086
3087/**
3088 * i40e_vsi_configure_tx - Configure the VSI for Tx
3089 * @vsi: VSI structure describing this set of rings and resources
3090 *
3091 * Configure the Tx VSI for operation.
3092 **/
3093static int i40e_vsi_configure_tx(struct i40e_vsi *vsi)
3094{
3095 int err = 0;
3096 u16 i;
3097
Alexander Duyck9f65e152013-09-28 06:00:58 +00003098 for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
3099 err = i40e_configure_tx_ring(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003100
Björn Töpel74608d12017-05-24 07:55:35 +02003101 if (!i40e_enabled_xdp_vsi(vsi))
3102 return err;
3103
3104 for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
3105 err = i40e_configure_tx_ring(vsi->xdp_rings[i]);
3106
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003107 return err;
3108}
3109
3110/**
3111 * i40e_vsi_configure_rx - Configure the VSI for Rx
3112 * @vsi: the VSI being configured
3113 *
3114 * Configure the Rx VSI for operation.
3115 **/
3116static int i40e_vsi_configure_rx(struct i40e_vsi *vsi)
3117{
3118 int err = 0;
3119 u16 i;
3120
Alexander Duyckdab86af2017-03-14 10:15:27 -07003121 if (!vsi->netdev || (vsi->back->flags & I40E_FLAG_LEGACY_RX)) {
3122 vsi->max_frame = I40E_MAX_RXBUFFER;
3123 vsi->rx_buf_len = I40E_RXBUFFER_2048;
3124#if (PAGE_SIZE < 8192)
Alexander Duyckca9ec082017-04-05 07:51:02 -04003125 } else if (!I40E_2K_TOO_SMALL_WITH_PADDING &&
3126 (vsi->netdev->mtu <= ETH_DATA_LEN)) {
Alexander Duyckdab86af2017-03-14 10:15:27 -07003127 vsi->max_frame = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
3128 vsi->rx_buf_len = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
3129#endif
3130 } else {
3131 vsi->max_frame = I40E_MAX_RXBUFFER;
Alexander Duyck98efd692017-04-05 07:51:01 -04003132 vsi->rx_buf_len = (PAGE_SIZE < 8192) ? I40E_RXBUFFER_3072 :
3133 I40E_RXBUFFER_2048;
Alexander Duyckdab86af2017-03-14 10:15:27 -07003134 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003135
3136 /* set up individual rings */
3137 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e152013-09-28 06:00:58 +00003138 err = i40e_configure_rx_ring(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003139
3140 return err;
3141}
3142
3143/**
3144 * i40e_vsi_config_dcb_rings - Update rings to reflect DCB TC
3145 * @vsi: ptr to the VSI
3146 **/
3147static void i40e_vsi_config_dcb_rings(struct i40e_vsi *vsi)
3148{
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +00003149 struct i40e_ring *tx_ring, *rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003150 u16 qoffset, qcount;
3151 int i, n;
3152
Parikh, Neeravcd238a32015-02-21 06:43:37 +00003153 if (!(vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
3154 /* Reset the TC information */
3155 for (i = 0; i < vsi->num_queue_pairs; i++) {
3156 rx_ring = vsi->rx_rings[i];
3157 tx_ring = vsi->tx_rings[i];
3158 rx_ring->dcb_tc = 0;
3159 tx_ring->dcb_tc = 0;
3160 }
3161 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003162
3163 for (n = 0; n < I40E_MAX_TRAFFIC_CLASS; n++) {
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04003164 if (!(vsi->tc_config.enabled_tc & BIT_ULL(n)))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003165 continue;
3166
3167 qoffset = vsi->tc_config.tc_info[n].qoffset;
3168 qcount = vsi->tc_config.tc_info[n].qcount;
3169 for (i = qoffset; i < (qoffset + qcount); i++) {
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +00003170 rx_ring = vsi->rx_rings[i];
3171 tx_ring = vsi->tx_rings[i];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003172 rx_ring->dcb_tc = n;
3173 tx_ring->dcb_tc = n;
3174 }
3175 }
3176}
3177
3178/**
3179 * i40e_set_vsi_rx_mode - Call set_rx_mode on a VSI
3180 * @vsi: ptr to the VSI
3181 **/
3182static void i40e_set_vsi_rx_mode(struct i40e_vsi *vsi)
3183{
3184 if (vsi->netdev)
3185 i40e_set_rx_mode(vsi->netdev);
3186}
3187
3188/**
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00003189 * i40e_fdir_filter_restore - Restore the Sideband Flow Director filters
3190 * @vsi: Pointer to the targeted VSI
3191 *
3192 * This function replays the hlist on the hw where all the SB Flow Director
3193 * filters were saved.
3194 **/
3195static void i40e_fdir_filter_restore(struct i40e_vsi *vsi)
3196{
3197 struct i40e_fdir_filter *filter;
3198 struct i40e_pf *pf = vsi->back;
3199 struct hlist_node *node;
3200
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00003201 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
3202 return;
3203
Jacob Keller6d069422017-02-06 14:38:44 -08003204 /* Reset FDir counters as we're replaying all existing filters */
Jacob Keller097dbf52017-02-06 14:38:46 -08003205 pf->fd_tcp4_filter_cnt = 0;
3206 pf->fd_udp4_filter_cnt = 0;
Jacob Kellerf223c872017-02-06 14:38:51 -08003207 pf->fd_sctp4_filter_cnt = 0;
Jacob Keller097dbf52017-02-06 14:38:46 -08003208 pf->fd_ip4_filter_cnt = 0;
Jacob Keller6d069422017-02-06 14:38:44 -08003209
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00003210 hlist_for_each_entry_safe(filter, node,
3211 &pf->fdir_filter_list, fdir_node) {
3212 i40e_add_del_fdir(vsi, filter, true);
3213 }
3214}
3215
3216/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003217 * i40e_vsi_configure - Set up the VSI for action
3218 * @vsi: the VSI being configured
3219 **/
3220static int i40e_vsi_configure(struct i40e_vsi *vsi)
3221{
3222 int err;
3223
3224 i40e_set_vsi_rx_mode(vsi);
3225 i40e_restore_vlan(vsi);
3226 i40e_vsi_config_dcb_rings(vsi);
3227 err = i40e_vsi_configure_tx(vsi);
3228 if (!err)
3229 err = i40e_vsi_configure_rx(vsi);
3230
3231 return err;
3232}
3233
3234/**
3235 * i40e_vsi_configure_msix - MSIX mode Interrupt Config in the HW
3236 * @vsi: the VSI being configured
3237 **/
3238static void i40e_vsi_configure_msix(struct i40e_vsi *vsi)
3239{
Björn Töpel74608d12017-05-24 07:55:35 +02003240 bool has_xdp = i40e_enabled_xdp_vsi(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003241 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003242 struct i40e_hw *hw = &pf->hw;
3243 u16 vector;
3244 int i, q;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003245 u32 qp;
3246
3247 /* The interrupt indexing is offset by 1 in the PFINT_ITRn
3248 * and PFINT_LNKLSTn registers, e.g.:
3249 * PFINT_ITRn[0..n-1] gets msix-1..msix-n (qpair interrupts)
3250 */
3251 qp = vsi->base_queue;
3252 vector = vsi->base_vector;
Alexander Duyck493fb302013-09-28 07:01:44 +00003253 for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
Jesse Brandeburgac26fc12015-09-28 14:12:37 -04003254 struct i40e_q_vector *q_vector = vsi->q_vectors[i];
3255
Jesse Brandeburgee2319c2015-09-28 14:16:54 -04003256 q_vector->itr_countdown = ITR_COUNTDOWN_START;
Kan Lianga75e8002016-02-19 09:24:04 -05003257 q_vector->rx.itr = ITR_TO_REG(vsi->rx_rings[i]->rx_itr_setting);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003258 q_vector->rx.latency_range = I40E_LOW_LATENCY;
3259 wr32(hw, I40E_PFINT_ITRN(I40E_RX_ITR, vector - 1),
3260 q_vector->rx.itr);
Kan Lianga75e8002016-02-19 09:24:04 -05003261 q_vector->tx.itr = ITR_TO_REG(vsi->tx_rings[i]->tx_itr_setting);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003262 q_vector->tx.latency_range = I40E_LOW_LATENCY;
3263 wr32(hw, I40E_PFINT_ITRN(I40E_TX_ITR, vector - 1),
3264 q_vector->tx.itr);
Jesse Brandeburgac26fc12015-09-28 14:12:37 -04003265 wr32(hw, I40E_PFINT_RATEN(vector - 1),
Alan Brady1c0e6a32016-11-28 16:06:02 -08003266 i40e_intrl_usec_to_reg(vsi->int_rate_limit));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003267
3268 /* Linked list for the queuepairs assigned to this vector */
3269 wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), qp);
3270 for (q = 0; q < q_vector->num_ringpairs; q++) {
Björn Töpel74608d12017-05-24 07:55:35 +02003271 u32 nextqp = has_xdp ? qp + vsi->alloc_queue_pairs : qp;
Jesse Brandeburgac26fc12015-09-28 14:12:37 -04003272 u32 val;
3273
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003274 val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
Björn Töpel74608d12017-05-24 07:55:35 +02003275 (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
3276 (vector << I40E_QINT_RQCTL_MSIX_INDX_SHIFT) |
3277 (nextqp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT) |
3278 (I40E_QUEUE_TYPE_TX <<
3279 I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003280
3281 wr32(hw, I40E_QINT_RQCTL(qp), val);
3282
Björn Töpel74608d12017-05-24 07:55:35 +02003283 if (has_xdp) {
3284 val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3285 (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3286 (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
3287 (qp << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) |
3288 (I40E_QUEUE_TYPE_TX <<
3289 I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3290
3291 wr32(hw, I40E_QINT_TQCTL(nextqp), val);
3292 }
3293
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003294 val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
Björn Töpel74608d12017-05-24 07:55:35 +02003295 (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3296 (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
3297 ((qp + 1) << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) |
3298 (I40E_QUEUE_TYPE_RX <<
3299 I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003300
3301 /* Terminate the linked list */
3302 if (q == (q_vector->num_ringpairs - 1))
Björn Töpel74608d12017-05-24 07:55:35 +02003303 val |= (I40E_QUEUE_END_OF_LIST <<
3304 I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003305
3306 wr32(hw, I40E_QINT_TQCTL(qp), val);
3307 qp++;
3308 }
3309 }
3310
3311 i40e_flush(hw);
3312}
3313
3314/**
3315 * i40e_enable_misc_int_causes - enable the non-queue interrupts
3316 * @hw: ptr to the hardware info
3317 **/
Jacob Kellerab437b52014-12-14 01:55:08 +00003318static void i40e_enable_misc_int_causes(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003319{
Jacob Kellerab437b52014-12-14 01:55:08 +00003320 struct i40e_hw *hw = &pf->hw;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003321 u32 val;
3322
3323 /* clear things first */
3324 wr32(hw, I40E_PFINT_ICR0_ENA, 0); /* disable all */
3325 rd32(hw, I40E_PFINT_ICR0); /* read to clear */
3326
3327 val = I40E_PFINT_ICR0_ENA_ECC_ERR_MASK |
3328 I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK |
3329 I40E_PFINT_ICR0_ENA_GRST_MASK |
3330 I40E_PFINT_ICR0_ENA_PCI_EXCEPTION_MASK |
3331 I40E_PFINT_ICR0_ENA_GPIO_MASK |
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003332 I40E_PFINT_ICR0_ENA_HMC_ERR_MASK |
3333 I40E_PFINT_ICR0_ENA_VFLR_MASK |
3334 I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
3335
Anjali Singhai Jain0d8e1432015-06-05 12:20:32 -04003336 if (pf->flags & I40E_FLAG_IWARP_ENABLED)
3337 val |= I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
3338
Jacob Kellerab437b52014-12-14 01:55:08 +00003339 if (pf->flags & I40E_FLAG_PTP)
3340 val |= I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
3341
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003342 wr32(hw, I40E_PFINT_ICR0_ENA, val);
3343
3344 /* SW_ITR_IDX = 0, but don't change INTENA */
Anjali Singhai Jain84ed40e2013-11-26 10:49:32 +00003345 wr32(hw, I40E_PFINT_DYN_CTL0, I40E_PFINT_DYN_CTL0_SW_ITR_INDX_MASK |
3346 I40E_PFINT_DYN_CTL0_INTENA_MSK_MASK);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003347
3348 /* OTHER_ITR_IDX = 0 */
3349 wr32(hw, I40E_PFINT_STAT_CTL0, 0);
3350}
3351
3352/**
3353 * i40e_configure_msi_and_legacy - Legacy mode interrupt config in the HW
3354 * @vsi: the VSI being configured
3355 **/
3356static void i40e_configure_msi_and_legacy(struct i40e_vsi *vsi)
3357{
Björn Töpel74608d12017-05-24 07:55:35 +02003358 u32 nextqp = i40e_enabled_xdp_vsi(vsi) ? vsi->alloc_queue_pairs : 0;
Alexander Duyck493fb302013-09-28 07:01:44 +00003359 struct i40e_q_vector *q_vector = vsi->q_vectors[0];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003360 struct i40e_pf *pf = vsi->back;
3361 struct i40e_hw *hw = &pf->hw;
3362 u32 val;
3363
3364 /* set the ITR configuration */
Jesse Brandeburgee2319c2015-09-28 14:16:54 -04003365 q_vector->itr_countdown = ITR_COUNTDOWN_START;
Kan Lianga75e8002016-02-19 09:24:04 -05003366 q_vector->rx.itr = ITR_TO_REG(vsi->rx_rings[0]->rx_itr_setting);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003367 q_vector->rx.latency_range = I40E_LOW_LATENCY;
3368 wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), q_vector->rx.itr);
Kan Lianga75e8002016-02-19 09:24:04 -05003369 q_vector->tx.itr = ITR_TO_REG(vsi->tx_rings[0]->tx_itr_setting);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003370 q_vector->tx.latency_range = I40E_LOW_LATENCY;
3371 wr32(hw, I40E_PFINT_ITR0(I40E_TX_ITR), q_vector->tx.itr);
3372
Jacob Kellerab437b52014-12-14 01:55:08 +00003373 i40e_enable_misc_int_causes(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003374
3375 /* FIRSTQ_INDX = 0, FIRSTQ_TYPE = 0 (rx) */
3376 wr32(hw, I40E_PFINT_LNKLST0, 0);
3377
Jesse Brandeburgf29eaa32014-02-11 08:24:12 +00003378 /* Associate the queue pair to the vector and enable the queue int */
Björn Töpel74608d12017-05-24 07:55:35 +02003379 val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3380 (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
3381 (nextqp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT)|
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003382 (I40E_QUEUE_TYPE_TX << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3383
3384 wr32(hw, I40E_QINT_RQCTL(0), val);
3385
Björn Töpel74608d12017-05-24 07:55:35 +02003386 if (i40e_enabled_xdp_vsi(vsi)) {
3387 val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3388 (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT)|
3389 (I40E_QUEUE_TYPE_TX
3390 << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3391
3392 wr32(hw, I40E_QINT_TQCTL(nextqp), val);
3393 }
3394
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003395 val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3396 (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3397 (I40E_QUEUE_END_OF_LIST << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
3398
3399 wr32(hw, I40E_QINT_TQCTL(0), val);
3400 i40e_flush(hw);
3401}
3402
3403/**
Mitch Williams2ef28cf2013-11-28 06:39:32 +00003404 * i40e_irq_dynamic_disable_icr0 - Disable default interrupt generation for icr0
3405 * @pf: board private structure
3406 **/
3407void i40e_irq_dynamic_disable_icr0(struct i40e_pf *pf)
3408{
3409 struct i40e_hw *hw = &pf->hw;
3410
3411 wr32(hw, I40E_PFINT_DYN_CTL0,
3412 I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
3413 i40e_flush(hw);
3414}
3415
3416/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003417 * i40e_irq_dynamic_enable_icr0 - Enable default interrupt generation for icr0
3418 * @pf: board private structure
3419 **/
Jacob Kellerdbadbbe2017-09-07 08:05:49 -04003420void i40e_irq_dynamic_enable_icr0(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003421{
3422 struct i40e_hw *hw = &pf->hw;
3423 u32 val;
3424
3425 val = I40E_PFINT_DYN_CTL0_INTENA_MASK |
Jacob Kellerdbadbbe2017-09-07 08:05:49 -04003426 I40E_PFINT_DYN_CTL0_CLEARPBA_MASK |
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003427 (I40E_ITR_NONE << I40E_PFINT_DYN_CTL0_ITR_INDX_SHIFT);
3428
3429 wr32(hw, I40E_PFINT_DYN_CTL0, val);
3430 i40e_flush(hw);
3431}
3432
3433/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003434 * i40e_msix_clean_rings - MSIX mode Interrupt Handler
3435 * @irq: interrupt number
3436 * @data: pointer to a q_vector
3437 **/
3438static irqreturn_t i40e_msix_clean_rings(int irq, void *data)
3439{
3440 struct i40e_q_vector *q_vector = data;
3441
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003442 if (!q_vector->tx.ring && !q_vector->rx.ring)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003443 return IRQ_HANDLED;
3444
Alexander Duyck5d3465a2015-09-29 15:19:50 -07003445 napi_schedule_irqoff(&q_vector->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003446
3447 return IRQ_HANDLED;
3448}
3449
3450/**
Alan Brady96db7762016-09-14 16:24:38 -07003451 * i40e_irq_affinity_notify - Callback for affinity changes
3452 * @notify: context as to what irq was changed
3453 * @mask: the new affinity mask
3454 *
3455 * This is a callback function used by the irq_set_affinity_notifier function
3456 * so that we may register to receive changes to the irq affinity masks.
3457 **/
3458static void i40e_irq_affinity_notify(struct irq_affinity_notify *notify,
3459 const cpumask_t *mask)
3460{
3461 struct i40e_q_vector *q_vector =
3462 container_of(notify, struct i40e_q_vector, affinity_notify);
3463
Jacob Keller7e4d01e2017-07-12 05:46:05 -04003464 cpumask_copy(&q_vector->affinity_mask, mask);
Alan Brady96db7762016-09-14 16:24:38 -07003465}
3466
3467/**
3468 * i40e_irq_affinity_release - Callback for affinity notifier release
3469 * @ref: internal core kernel usage
3470 *
3471 * This is a callback function used by the irq_set_affinity_notifier function
3472 * to inform the current notification subscriber that they will no longer
3473 * receive notifications.
3474 **/
3475static void i40e_irq_affinity_release(struct kref *ref) {}
3476
3477/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003478 * i40e_vsi_request_irq_msix - Initialize MSI-X interrupts
3479 * @vsi: the VSI being configured
3480 * @basename: name for the vector
3481 *
3482 * Allocates MSI-X vectors and requests interrupts from the kernel.
3483 **/
3484static int i40e_vsi_request_irq_msix(struct i40e_vsi *vsi, char *basename)
3485{
3486 int q_vectors = vsi->num_q_vectors;
3487 struct i40e_pf *pf = vsi->back;
3488 int base = vsi->base_vector;
3489 int rx_int_idx = 0;
3490 int tx_int_idx = 0;
3491 int vector, err;
Alan Brady96db7762016-09-14 16:24:38 -07003492 int irq_num;
Jacob Kellerbe664cb2017-08-29 05:32:31 -04003493 int cpu;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003494
3495 for (vector = 0; vector < q_vectors; vector++) {
Alexander Duyck493fb302013-09-28 07:01:44 +00003496 struct i40e_q_vector *q_vector = vsi->q_vectors[vector];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003497
Alan Brady96db7762016-09-14 16:24:38 -07003498 irq_num = pf->msix_entries[base + vector].vector;
3499
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003500 if (q_vector->tx.ring && q_vector->rx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003501 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3502 "%s-%s-%d", basename, "TxRx", rx_int_idx++);
3503 tx_int_idx++;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003504 } else if (q_vector->rx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003505 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3506 "%s-%s-%d", basename, "rx", rx_int_idx++);
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003507 } else if (q_vector->tx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003508 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3509 "%s-%s-%d", basename, "tx", tx_int_idx++);
3510 } else {
3511 /* skip this unused q_vector */
3512 continue;
3513 }
Alan Brady96db7762016-09-14 16:24:38 -07003514 err = request_irq(irq_num,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003515 vsi->irq_handler,
3516 0,
3517 q_vector->name,
3518 q_vector);
3519 if (err) {
3520 dev_info(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04003521 "MSIX request_irq failed, error: %d\n", err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003522 goto free_queue_irqs;
3523 }
Alan Brady96db7762016-09-14 16:24:38 -07003524
3525 /* register for affinity change notifications */
3526 q_vector->affinity_notify.notify = i40e_irq_affinity_notify;
3527 q_vector->affinity_notify.release = i40e_irq_affinity_release;
3528 irq_set_affinity_notifier(irq_num, &q_vector->affinity_notify);
Jacob Kellerbe664cb2017-08-29 05:32:31 -04003529 /* Spread affinity hints out across online CPUs.
3530 *
3531 * get_cpu_mask returns a static constant mask with
3532 * a permanent lifetime so it's ok to pass to
3533 * irq_set_affinity_hint without making a copy.
Jacob Keller759dc4a2017-07-14 09:10:10 -04003534 */
Jacob Kellerbe664cb2017-08-29 05:32:31 -04003535 cpu = cpumask_local_spread(q_vector->v_idx, -1);
3536 irq_set_affinity_hint(irq_num, get_cpu_mask(cpu));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003537 }
3538
Shannon Nelson63741842014-04-23 04:50:16 +00003539 vsi->irqs_ready = true;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003540 return 0;
3541
3542free_queue_irqs:
3543 while (vector) {
3544 vector--;
Alan Brady96db7762016-09-14 16:24:38 -07003545 irq_num = pf->msix_entries[base + vector].vector;
3546 irq_set_affinity_notifier(irq_num, NULL);
3547 irq_set_affinity_hint(irq_num, NULL);
3548 free_irq(irq_num, &vsi->q_vectors[vector]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003549 }
3550 return err;
3551}
3552
3553/**
3554 * i40e_vsi_disable_irq - Mask off queue interrupt generation on the VSI
3555 * @vsi: the VSI being un-configured
3556 **/
3557static void i40e_vsi_disable_irq(struct i40e_vsi *vsi)
3558{
3559 struct i40e_pf *pf = vsi->back;
3560 struct i40e_hw *hw = &pf->hw;
3561 int base = vsi->base_vector;
3562 int i;
3563
Shannon Nelson2e5c26ea2017-06-07 05:43:11 -04003564 /* disable interrupt causation from each queue */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003565 for (i = 0; i < vsi->num_queue_pairs; i++) {
Shannon Nelson2e5c26ea2017-06-07 05:43:11 -04003566 u32 val;
3567
3568 val = rd32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx));
3569 val &= ~I40E_QINT_TQCTL_CAUSE_ENA_MASK;
3570 wr32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx), val);
3571
3572 val = rd32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx));
3573 val &= ~I40E_QINT_RQCTL_CAUSE_ENA_MASK;
3574 wr32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx), val);
3575
Björn Töpel74608d12017-05-24 07:55:35 +02003576 if (!i40e_enabled_xdp_vsi(vsi))
3577 continue;
3578 wr32(hw, I40E_QINT_TQCTL(vsi->xdp_rings[i]->reg_idx), 0);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003579 }
3580
Shannon Nelson2e5c26ea2017-06-07 05:43:11 -04003581 /* disable each interrupt */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003582 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3583 for (i = vsi->base_vector;
3584 i < (vsi->num_q_vectors + vsi->base_vector); i++)
3585 wr32(hw, I40E_PFINT_DYN_CTLN(i - 1), 0);
3586
3587 i40e_flush(hw);
3588 for (i = 0; i < vsi->num_q_vectors; i++)
3589 synchronize_irq(pf->msix_entries[i + base].vector);
3590 } else {
3591 /* Legacy and MSI mode - this stops all interrupt handling */
3592 wr32(hw, I40E_PFINT_ICR0_ENA, 0);
3593 wr32(hw, I40E_PFINT_DYN_CTL0, 0);
3594 i40e_flush(hw);
3595 synchronize_irq(pf->pdev->irq);
3596 }
3597}
3598
3599/**
3600 * i40e_vsi_enable_irq - Enable IRQ for the given VSI
3601 * @vsi: the VSI being configured
3602 **/
3603static int i40e_vsi_enable_irq(struct i40e_vsi *vsi)
3604{
3605 struct i40e_pf *pf = vsi->back;
3606 int i;
3607
3608 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
Jesse Brandeburg78455482015-07-23 16:54:41 -04003609 for (i = 0; i < vsi->num_q_vectors; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003610 i40e_irq_dynamic_enable(vsi, i);
3611 } else {
Jacob Kellerdbadbbe2017-09-07 08:05:49 -04003612 i40e_irq_dynamic_enable_icr0(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003613 }
3614
Jesse Brandeburg1022cb62013-09-28 07:13:08 +00003615 i40e_flush(&pf->hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003616 return 0;
3617}
3618
3619/**
Jacob Kellerc17401a2017-07-14 09:27:02 -04003620 * i40e_free_misc_vector - Free the vector that handles non-queue events
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003621 * @pf: board private structure
3622 **/
Jacob Kellerc17401a2017-07-14 09:27:02 -04003623static void i40e_free_misc_vector(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003624{
3625 /* Disable ICR 0 */
3626 wr32(&pf->hw, I40E_PFINT_ICR0_ENA, 0);
3627 i40e_flush(&pf->hw);
Jacob Kellerc17401a2017-07-14 09:27:02 -04003628
3629 if (pf->flags & I40E_FLAG_MSIX_ENABLED && pf->msix_entries) {
3630 synchronize_irq(pf->msix_entries[0].vector);
3631 free_irq(pf->msix_entries[0].vector, pf);
3632 clear_bit(__I40E_MISC_IRQ_REQUESTED, pf->state);
3633 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003634}
3635
3636/**
3637 * i40e_intr - MSI/Legacy and non-queue interrupt handler
3638 * @irq: interrupt number
3639 * @data: pointer to a q_vector
3640 *
3641 * This is the handler used for all MSI/Legacy interrupts, and deals
3642 * with both queue and non-queue interrupts. This is also used in
3643 * MSIX mode to handle the non-queue interrupts.
3644 **/
3645static irqreturn_t i40e_intr(int irq, void *data)
3646{
3647 struct i40e_pf *pf = (struct i40e_pf *)data;
3648 struct i40e_hw *hw = &pf->hw;
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003649 irqreturn_t ret = IRQ_NONE;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003650 u32 icr0, icr0_remaining;
3651 u32 val, ena_mask;
3652
3653 icr0 = rd32(hw, I40E_PFINT_ICR0);
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003654 ena_mask = rd32(hw, I40E_PFINT_ICR0_ENA);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003655
Shannon Nelson116a57d2013-09-28 07:13:59 +00003656 /* if sharing a legacy IRQ, we might get called w/o an intr pending */
3657 if ((icr0 & I40E_PFINT_ICR0_INTEVENT_MASK) == 0)
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003658 goto enable_intr;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003659
Shannon Nelsoncd92e722013-11-16 10:00:44 +00003660 /* if interrupt but no bits showing, must be SWINT */
3661 if (((icr0 & ~I40E_PFINT_ICR0_INTEVENT_MASK) == 0) ||
3662 (icr0 & I40E_PFINT_ICR0_SWINT_MASK))
3663 pf->sw_int_count++;
3664
Anjali Singhai Jain0d8e1432015-06-05 12:20:32 -04003665 if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
Catherine Sullivan76429842017-06-07 05:43:12 -04003666 (icr0 & I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK)) {
Anjali Singhai Jain0d8e1432015-06-05 12:20:32 -04003667 ena_mask &= ~I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
Carolyn Wyborny23bb6dc2016-11-08 13:05:12 -08003668 dev_dbg(&pf->pdev->dev, "cleared PE_CRITERR\n");
Catherine Sullivan76429842017-06-07 05:43:12 -04003669 set_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
Anjali Singhai Jain0d8e1432015-06-05 12:20:32 -04003670 }
3671
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003672 /* only q0 is used in MSI/Legacy mode, and none are used in MSIX */
3673 if (icr0 & I40E_PFINT_ICR0_QUEUE_0_MASK) {
Alexander Duyck5d3465a2015-09-29 15:19:50 -07003674 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
3675 struct i40e_q_vector *q_vector = vsi->q_vectors[0];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003676
Anjali Singhai Jaina16ae2d2016-01-15 14:33:16 -08003677 /* We do not have a way to disarm Queue causes while leaving
3678 * interrupt enabled for all other causes, ideally
3679 * interrupt should be disabled while we are in NAPI but
3680 * this is not a performance path and napi_schedule()
3681 * can deal with rescheduling.
3682 */
Mauro S. M. Rodrigues9e6c9c02017-05-12 23:26:56 -03003683 if (!test_bit(__I40E_DOWN, pf->state))
Alexander Duyck5d3465a2015-09-29 15:19:50 -07003684 napi_schedule_irqoff(&q_vector->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003685 }
3686
3687 if (icr0 & I40E_PFINT_ICR0_ADMINQ_MASK) {
3688 ena_mask &= ~I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
Jacob Keller0da36b92017-04-19 09:25:55 -04003689 set_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state);
Shannon Nelson6e93d0c2016-01-15 14:33:18 -08003690 i40e_debug(&pf->hw, I40E_DEBUG_NVM, "AdminQ event\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003691 }
3692
3693 if (icr0 & I40E_PFINT_ICR0_MAL_DETECT_MASK) {
3694 ena_mask &= ~I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
Jacob Keller0da36b92017-04-19 09:25:55 -04003695 set_bit(__I40E_MDD_EVENT_PENDING, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003696 }
3697
3698 if (icr0 & I40E_PFINT_ICR0_VFLR_MASK) {
3699 ena_mask &= ~I40E_PFINT_ICR0_ENA_VFLR_MASK;
Jacob Keller0da36b92017-04-19 09:25:55 -04003700 set_bit(__I40E_VFLR_EVENT_PENDING, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003701 }
3702
3703 if (icr0 & I40E_PFINT_ICR0_GRST_MASK) {
Jacob Keller0da36b92017-04-19 09:25:55 -04003704 if (!test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
3705 set_bit(__I40E_RESET_INTR_RECEIVED, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003706 ena_mask &= ~I40E_PFINT_ICR0_ENA_GRST_MASK;
3707 val = rd32(hw, I40E_GLGEN_RSTAT);
3708 val = (val & I40E_GLGEN_RSTAT_RESET_TYPE_MASK)
3709 >> I40E_GLGEN_RSTAT_RESET_TYPE_SHIFT;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003710 if (val == I40E_RESET_CORER) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003711 pf->corer_count++;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003712 } else if (val == I40E_RESET_GLOBR) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003713 pf->globr_count++;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003714 } else if (val == I40E_RESET_EMPR) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003715 pf->empr_count++;
Jacob Keller0da36b92017-04-19 09:25:55 -04003716 set_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state);
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003717 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003718 }
3719
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003720 if (icr0 & I40E_PFINT_ICR0_HMC_ERR_MASK) {
3721 icr0 &= ~I40E_PFINT_ICR0_HMC_ERR_MASK;
3722 dev_info(&pf->pdev->dev, "HMC error interrupt\n");
Anjali Singhai Jain25fc0e62015-03-31 00:45:01 -07003723 dev_info(&pf->pdev->dev, "HMC error info 0x%x, HMC error data 0x%x\n",
3724 rd32(hw, I40E_PFHMC_ERRORINFO),
3725 rd32(hw, I40E_PFHMC_ERRORDATA));
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003726 }
3727
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003728 if (icr0 & I40E_PFINT_ICR0_TIMESYNC_MASK) {
3729 u32 prttsyn_stat = rd32(hw, I40E_PRTTSYN_STAT_0);
3730
3731 if (prttsyn_stat & I40E_PRTTSYN_STAT_0_TXTIME_MASK) {
Jacob Kellercafa1fc2014-04-24 18:05:03 -07003732 icr0 &= ~I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003733 i40e_ptp_tx_hwtstamp(pf);
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003734 }
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003735 }
3736
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003737 /* If a critical error is pending we have no choice but to reset the
3738 * device.
3739 * Report and mask out any remaining unexpected interrupts.
3740 */
3741 icr0_remaining = icr0 & ena_mask;
3742 if (icr0_remaining) {
3743 dev_info(&pf->pdev->dev, "unhandled interrupt icr0=0x%08x\n",
3744 icr0_remaining);
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003745 if ((icr0_remaining & I40E_PFINT_ICR0_PE_CRITERR_MASK) ||
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003746 (icr0_remaining & I40E_PFINT_ICR0_PCI_EXCEPTION_MASK) ||
Anjali Singhai Jainc0c28972014-02-12 01:45:34 +00003747 (icr0_remaining & I40E_PFINT_ICR0_ECC_ERR_MASK)) {
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003748 dev_info(&pf->pdev->dev, "device will be reset\n");
Jacob Keller0da36b92017-04-19 09:25:55 -04003749 set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003750 i40e_service_event_schedule(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003751 }
3752 ena_mask &= ~icr0_remaining;
3753 }
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003754 ret = IRQ_HANDLED;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003755
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003756enable_intr:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003757 /* re-enable interrupt causes */
3758 wr32(hw, I40E_PFINT_ICR0_ENA, ena_mask);
Mauro S. M. Rodrigues9e6c9c02017-05-12 23:26:56 -03003759 if (!test_bit(__I40E_DOWN, pf->state)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003760 i40e_service_event_schedule(pf);
Jacob Kellerdbadbbe2017-09-07 08:05:49 -04003761 i40e_irq_dynamic_enable_icr0(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003762 }
3763
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003764 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003765}
3766
3767/**
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003768 * i40e_clean_fdir_tx_irq - Reclaim resources after transmit completes
3769 * @tx_ring: tx ring to clean
3770 * @budget: how many cleans we're allowed
3771 *
3772 * Returns true if there's any budget left (e.g. the clean is finished)
3773 **/
3774static bool i40e_clean_fdir_tx_irq(struct i40e_ring *tx_ring, int budget)
3775{
3776 struct i40e_vsi *vsi = tx_ring->vsi;
3777 u16 i = tx_ring->next_to_clean;
3778 struct i40e_tx_buffer *tx_buf;
3779 struct i40e_tx_desc *tx_desc;
3780
3781 tx_buf = &tx_ring->tx_bi[i];
3782 tx_desc = I40E_TX_DESC(tx_ring, i);
3783 i -= tx_ring->count;
3784
3785 do {
3786 struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
3787
3788 /* if next_to_watch is not set then there is no work pending */
3789 if (!eop_desc)
3790 break;
3791
3792 /* prevent any other reads prior to eop_desc */
3793 read_barrier_depends();
3794
3795 /* if the descriptor isn't done, no work yet to do */
3796 if (!(eop_desc->cmd_type_offset_bsz &
3797 cpu_to_le64(I40E_TX_DESC_DTYPE_DESC_DONE)))
3798 break;
3799
3800 /* clear next_to_watch to prevent false hangs */
3801 tx_buf->next_to_watch = NULL;
3802
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003803 tx_desc->buffer_addr = 0;
3804 tx_desc->cmd_type_offset_bsz = 0;
3805 /* move past filter desc */
3806 tx_buf++;
3807 tx_desc++;
3808 i++;
3809 if (unlikely(!i)) {
3810 i -= tx_ring->count;
3811 tx_buf = tx_ring->tx_bi;
3812 tx_desc = I40E_TX_DESC(tx_ring, 0);
3813 }
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003814 /* unmap skb header data */
3815 dma_unmap_single(tx_ring->dev,
3816 dma_unmap_addr(tx_buf, dma),
3817 dma_unmap_len(tx_buf, len),
3818 DMA_TO_DEVICE);
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003819 if (tx_buf->tx_flags & I40E_TX_FLAGS_FD_SB)
3820 kfree(tx_buf->raw_buf);
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003821
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003822 tx_buf->raw_buf = NULL;
3823 tx_buf->tx_flags = 0;
3824 tx_buf->next_to_watch = NULL;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003825 dma_unmap_len_set(tx_buf, len, 0);
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003826 tx_desc->buffer_addr = 0;
3827 tx_desc->cmd_type_offset_bsz = 0;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003828
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003829 /* move us past the eop_desc for start of next FD desc */
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003830 tx_buf++;
3831 tx_desc++;
3832 i++;
3833 if (unlikely(!i)) {
3834 i -= tx_ring->count;
3835 tx_buf = tx_ring->tx_bi;
3836 tx_desc = I40E_TX_DESC(tx_ring, 0);
3837 }
3838
3839 /* update budget accounting */
3840 budget--;
3841 } while (likely(budget));
3842
3843 i += tx_ring->count;
3844 tx_ring->next_to_clean = i;
3845
Jesse Brandeburg6995b362015-08-28 17:55:54 -04003846 if (vsi->back->flags & I40E_FLAG_MSIX_ENABLED)
Jesse Brandeburg78455482015-07-23 16:54:41 -04003847 i40e_irq_dynamic_enable(vsi, tx_ring->q_vector->v_idx);
Jesse Brandeburg6995b362015-08-28 17:55:54 -04003848
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003849 return budget > 0;
3850}
3851
3852/**
3853 * i40e_fdir_clean_ring - Interrupt Handler for FDIR SB ring
3854 * @irq: interrupt number
3855 * @data: pointer to a q_vector
3856 **/
3857static irqreturn_t i40e_fdir_clean_ring(int irq, void *data)
3858{
3859 struct i40e_q_vector *q_vector = data;
3860 struct i40e_vsi *vsi;
3861
3862 if (!q_vector->tx.ring)
3863 return IRQ_HANDLED;
3864
3865 vsi = q_vector->tx.ring->vsi;
3866 i40e_clean_fdir_tx_irq(q_vector->tx.ring, vsi->work_limit);
3867
3868 return IRQ_HANDLED;
3869}
3870
3871/**
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003872 * i40e_map_vector_to_qp - Assigns the queue pair to the vector
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003873 * @vsi: the VSI being configured
3874 * @v_idx: vector index
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003875 * @qp_idx: queue pair index
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003876 **/
Anjali Singhai Jain26cdc442015-07-10 19:36:00 -04003877static void i40e_map_vector_to_qp(struct i40e_vsi *vsi, int v_idx, int qp_idx)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003878{
Alexander Duyck493fb302013-09-28 07:01:44 +00003879 struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
Alexander Duyck9f65e152013-09-28 06:00:58 +00003880 struct i40e_ring *tx_ring = vsi->tx_rings[qp_idx];
3881 struct i40e_ring *rx_ring = vsi->rx_rings[qp_idx];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003882
3883 tx_ring->q_vector = q_vector;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003884 tx_ring->next = q_vector->tx.ring;
3885 q_vector->tx.ring = tx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003886 q_vector->tx.count++;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003887
Björn Töpel74608d12017-05-24 07:55:35 +02003888 /* Place XDP Tx ring in the same q_vector ring list as regular Tx */
3889 if (i40e_enabled_xdp_vsi(vsi)) {
3890 struct i40e_ring *xdp_ring = vsi->xdp_rings[qp_idx];
3891
3892 xdp_ring->q_vector = q_vector;
3893 xdp_ring->next = q_vector->tx.ring;
3894 q_vector->tx.ring = xdp_ring;
3895 q_vector->tx.count++;
3896 }
3897
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003898 rx_ring->q_vector = q_vector;
3899 rx_ring->next = q_vector->rx.ring;
3900 q_vector->rx.ring = rx_ring;
3901 q_vector->rx.count++;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003902}
3903
3904/**
3905 * i40e_vsi_map_rings_to_vectors - Maps descriptor rings to vectors
3906 * @vsi: the VSI being configured
3907 *
3908 * This function maps descriptor rings to the queue-specific vectors
3909 * we were allotted through the MSI-X enabling code. Ideally, we'd have
3910 * one vector per queue pair, but on a constrained vector budget, we
3911 * group the queue pairs as "efficiently" as possible.
3912 **/
3913static void i40e_vsi_map_rings_to_vectors(struct i40e_vsi *vsi)
3914{
3915 int qp_remaining = vsi->num_queue_pairs;
3916 int q_vectors = vsi->num_q_vectors;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003917 int num_ringpairs;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003918 int v_start = 0;
3919 int qp_idx = 0;
3920
3921 /* If we don't have enough vectors for a 1-to-1 mapping, we'll have to
3922 * group them so there are multiple queues per vector.
Anjali Singhai Jain70114ec2014-06-03 23:50:14 +00003923 * It is also important to go through all the vectors available to be
3924 * sure that if we don't use all the vectors, that the remaining vectors
3925 * are cleared. This is especially important when decreasing the
3926 * number of queues in use.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003927 */
Anjali Singhai Jain70114ec2014-06-03 23:50:14 +00003928 for (; v_start < q_vectors; v_start++) {
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003929 struct i40e_q_vector *q_vector = vsi->q_vectors[v_start];
3930
3931 num_ringpairs = DIV_ROUND_UP(qp_remaining, q_vectors - v_start);
3932
3933 q_vector->num_ringpairs = num_ringpairs;
3934
3935 q_vector->rx.count = 0;
3936 q_vector->tx.count = 0;
3937 q_vector->rx.ring = NULL;
3938 q_vector->tx.ring = NULL;
3939
3940 while (num_ringpairs--) {
Anjali Singhai Jain26cdc442015-07-10 19:36:00 -04003941 i40e_map_vector_to_qp(vsi, v_start, qp_idx);
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003942 qp_idx++;
3943 qp_remaining--;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003944 }
3945 }
3946}
3947
3948/**
3949 * i40e_vsi_request_irq - Request IRQ from the OS
3950 * @vsi: the VSI being configured
3951 * @basename: name for the vector
3952 **/
3953static int i40e_vsi_request_irq(struct i40e_vsi *vsi, char *basename)
3954{
3955 struct i40e_pf *pf = vsi->back;
3956 int err;
3957
3958 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
3959 err = i40e_vsi_request_irq_msix(vsi, basename);
3960 else if (pf->flags & I40E_FLAG_MSI_ENABLED)
3961 err = request_irq(pf->pdev->irq, i40e_intr, 0,
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00003962 pf->int_name, pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003963 else
3964 err = request_irq(pf->pdev->irq, i40e_intr, IRQF_SHARED,
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00003965 pf->int_name, pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003966
3967 if (err)
3968 dev_info(&pf->pdev->dev, "request_irq failed, Error %d\n", err);
3969
3970 return err;
3971}
3972
3973#ifdef CONFIG_NET_POLL_CONTROLLER
3974/**
Jesse Brandeburgd89d9672016-01-04 10:33:02 -08003975 * i40e_netpoll - A Polling 'interrupt' handler
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003976 * @netdev: network interface device structure
3977 *
3978 * This is used by netconsole to send skbs without having to re-enable
3979 * interrupts. It's not called while the normal interrupt routine is executing.
3980 **/
3981static void i40e_netpoll(struct net_device *netdev)
3982{
3983 struct i40e_netdev_priv *np = netdev_priv(netdev);
3984 struct i40e_vsi *vsi = np->vsi;
3985 struct i40e_pf *pf = vsi->back;
3986 int i;
3987
3988 /* if interface is down do nothing */
Jacob Keller0da36b92017-04-19 09:25:55 -04003989 if (test_bit(__I40E_VSI_DOWN, vsi->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003990 return;
3991
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003992 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3993 for (i = 0; i < vsi->num_q_vectors; i++)
Alexander Duyck493fb302013-09-28 07:01:44 +00003994 i40e_msix_clean_rings(0, vsi->q_vectors[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003995 } else {
3996 i40e_intr(pf->pdev->irq, netdev);
3997 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003998}
3999#endif
4000
Jacob Kellerc768e492017-04-13 04:45:47 -04004001#define I40E_QTX_ENA_WAIT_COUNT 50
4002
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004003/**
Neerav Parikh23527302014-06-03 23:50:15 +00004004 * i40e_pf_txq_wait - Wait for a PF's Tx queue to be enabled or disabled
4005 * @pf: the PF being configured
4006 * @pf_q: the PF queue
4007 * @enable: enable or disable state of the queue
4008 *
4009 * This routine will wait for the given Tx queue of the PF to reach the
4010 * enabled or disabled state.
4011 * Returns -ETIMEDOUT in case of failing to reach the requested state after
4012 * multiple retries; else will return 0 in case of success.
4013 **/
4014static int i40e_pf_txq_wait(struct i40e_pf *pf, int pf_q, bool enable)
4015{
4016 int i;
4017 u32 tx_reg;
4018
4019 for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
4020 tx_reg = rd32(&pf->hw, I40E_QTX_ENA(pf_q));
4021 if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
4022 break;
4023
Neerav Parikhf98a2002014-09-13 07:40:44 +00004024 usleep_range(10, 20);
Neerav Parikh23527302014-06-03 23:50:15 +00004025 }
4026 if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
4027 return -ETIMEDOUT;
4028
4029 return 0;
4030}
4031
4032/**
Jacob Kellerc768e492017-04-13 04:45:47 -04004033 * i40e_control_tx_q - Start or stop a particular Tx queue
4034 * @pf: the PF structure
4035 * @pf_q: the PF queue to configure
4036 * @enable: start or stop the queue
4037 *
4038 * This function enables or disables a single queue. Note that any delay
4039 * required after the operation is expected to be handled by the caller of
4040 * this function.
4041 **/
4042static void i40e_control_tx_q(struct i40e_pf *pf, int pf_q, bool enable)
4043{
4044 struct i40e_hw *hw = &pf->hw;
4045 u32 tx_reg;
4046 int i;
4047
4048 /* warn the TX unit of coming changes */
4049 i40e_pre_tx_queue_cfg(&pf->hw, pf_q, enable);
4050 if (!enable)
4051 usleep_range(10, 20);
4052
4053 for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
4054 tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
4055 if (((tx_reg >> I40E_QTX_ENA_QENA_REQ_SHIFT) & 1) ==
4056 ((tx_reg >> I40E_QTX_ENA_QENA_STAT_SHIFT) & 1))
4057 break;
4058 usleep_range(1000, 2000);
4059 }
4060
4061 /* Skip if the queue is already in the requested state */
4062 if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
4063 return;
4064
4065 /* turn on/off the queue */
4066 if (enable) {
4067 wr32(hw, I40E_QTX_HEAD(pf_q), 0);
4068 tx_reg |= I40E_QTX_ENA_QENA_REQ_MASK;
4069 } else {
4070 tx_reg &= ~I40E_QTX_ENA_QENA_REQ_MASK;
4071 }
4072
4073 wr32(hw, I40E_QTX_ENA(pf_q), tx_reg);
4074}
4075
4076/**
Björn Töpel74608d12017-05-24 07:55:35 +02004077 * i40e_control_wait_tx_q - Start/stop Tx queue and wait for completion
4078 * @seid: VSI SEID
4079 * @pf: the PF structure
4080 * @pf_q: the PF queue to configure
4081 * @is_xdp: true if the queue is used for XDP
4082 * @enable: start or stop the queue
4083 **/
4084static int i40e_control_wait_tx_q(int seid, struct i40e_pf *pf, int pf_q,
4085 bool is_xdp, bool enable)
4086{
4087 int ret;
4088
4089 i40e_control_tx_q(pf, pf_q, enable);
4090
4091 /* wait for the change to finish */
4092 ret = i40e_pf_txq_wait(pf, pf_q, enable);
4093 if (ret) {
4094 dev_info(&pf->pdev->dev,
4095 "VSI seid %d %sTx ring %d %sable timeout\n",
4096 seid, (is_xdp ? "XDP " : ""), pf_q,
4097 (enable ? "en" : "dis"));
4098 }
4099
4100 return ret;
4101}
4102
4103/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004104 * i40e_vsi_control_tx - Start or stop a VSI's rings
4105 * @vsi: the VSI being configured
4106 * @enable: start or stop the rings
4107 **/
4108static int i40e_vsi_control_tx(struct i40e_vsi *vsi, bool enable)
4109{
4110 struct i40e_pf *pf = vsi->back;
Jacob Kellerc768e492017-04-13 04:45:47 -04004111 int i, pf_q, ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004112
4113 pf_q = vsi->base_queue;
4114 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
Björn Töpel74608d12017-05-24 07:55:35 +02004115 ret = i40e_control_wait_tx_q(vsi->seid, pf,
4116 pf_q,
4117 false /*is xdp*/, enable);
4118 if (ret)
Neerav Parikh23527302014-06-03 23:50:15 +00004119 break;
Björn Töpel74608d12017-05-24 07:55:35 +02004120
4121 if (!i40e_enabled_xdp_vsi(vsi))
4122 continue;
4123
4124 ret = i40e_control_wait_tx_q(vsi->seid, pf,
4125 pf_q + vsi->alloc_queue_pairs,
4126 true /*is xdp*/, enable);
4127 if (ret)
4128 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004129 }
4130
Neerav Parikh23527302014-06-03 23:50:15 +00004131 return ret;
4132}
4133
4134/**
4135 * i40e_pf_rxq_wait - Wait for a PF's Rx queue to be enabled or disabled
4136 * @pf: the PF being configured
4137 * @pf_q: the PF queue
4138 * @enable: enable or disable state of the queue
4139 *
4140 * This routine will wait for the given Rx queue of the PF to reach the
4141 * enabled or disabled state.
4142 * Returns -ETIMEDOUT in case of failing to reach the requested state after
4143 * multiple retries; else will return 0 in case of success.
4144 **/
4145static int i40e_pf_rxq_wait(struct i40e_pf *pf, int pf_q, bool enable)
4146{
4147 int i;
4148 u32 rx_reg;
4149
4150 for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
4151 rx_reg = rd32(&pf->hw, I40E_QRX_ENA(pf_q));
4152 if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4153 break;
4154
Neerav Parikhf98a2002014-09-13 07:40:44 +00004155 usleep_range(10, 20);
Neerav Parikh23527302014-06-03 23:50:15 +00004156 }
4157 if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
4158 return -ETIMEDOUT;
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00004159
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004160 return 0;
4161}
4162
4163/**
Jacob Kellerc768e492017-04-13 04:45:47 -04004164 * i40e_control_rx_q - Start or stop a particular Rx queue
4165 * @pf: the PF structure
4166 * @pf_q: the PF queue to configure
4167 * @enable: start or stop the queue
4168 *
4169 * This function enables or disables a single queue. Note that any delay
4170 * required after the operation is expected to be handled by the caller of
4171 * this function.
4172 **/
4173static void i40e_control_rx_q(struct i40e_pf *pf, int pf_q, bool enable)
4174{
4175 struct i40e_hw *hw = &pf->hw;
4176 u32 rx_reg;
4177 int i;
4178
4179 for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
4180 rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
4181 if (((rx_reg >> I40E_QRX_ENA_QENA_REQ_SHIFT) & 1) ==
4182 ((rx_reg >> I40E_QRX_ENA_QENA_STAT_SHIFT) & 1))
4183 break;
4184 usleep_range(1000, 2000);
4185 }
4186
4187 /* Skip if the queue is already in the requested state */
4188 if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4189 return;
4190
4191 /* turn on/off the queue */
4192 if (enable)
4193 rx_reg |= I40E_QRX_ENA_QENA_REQ_MASK;
4194 else
4195 rx_reg &= ~I40E_QRX_ENA_QENA_REQ_MASK;
4196
4197 wr32(hw, I40E_QRX_ENA(pf_q), rx_reg);
4198}
4199
4200/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004201 * i40e_vsi_control_rx - Start or stop a VSI's rings
4202 * @vsi: the VSI being configured
4203 * @enable: start or stop the rings
4204 **/
4205static int i40e_vsi_control_rx(struct i40e_vsi *vsi, bool enable)
4206{
4207 struct i40e_pf *pf = vsi->back;
Jacob Kellerc768e492017-04-13 04:45:47 -04004208 int i, pf_q, ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004209
4210 pf_q = vsi->base_queue;
4211 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
Jacob Kellerc768e492017-04-13 04:45:47 -04004212 i40e_control_rx_q(pf, pf_q, enable);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004213
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004214 /* wait for the change to finish */
Neerav Parikh23527302014-06-03 23:50:15 +00004215 ret = i40e_pf_rxq_wait(pf, pf_q, enable);
4216 if (ret) {
4217 dev_info(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04004218 "VSI seid %d Rx ring %d %sable timeout\n",
4219 vsi->seid, pf_q, (enable ? "en" : "dis"));
Neerav Parikh23527302014-06-03 23:50:15 +00004220 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004221 }
4222 }
4223
Wyborny, Carolynd08a9f62017-03-28 08:00:48 -07004224 /* Due to HW errata, on Rx disable only, the register can indicate done
4225 * before it really is. Needs 50ms to be sure
4226 */
4227 if (!enable)
4228 mdelay(50);
4229
Neerav Parikh23527302014-06-03 23:50:15 +00004230 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004231}
4232
4233/**
Filip Sadowski3aa7b742016-10-11 15:26:58 -07004234 * i40e_vsi_start_rings - Start a VSI's rings
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004235 * @vsi: the VSI being configured
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004236 **/
Filip Sadowski3aa7b742016-10-11 15:26:58 -07004237int i40e_vsi_start_rings(struct i40e_vsi *vsi)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004238{
Anjali Singhai Jain3b867b22013-12-21 05:44:44 +00004239 int ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004240
4241 /* do rx first for enable and last for disable */
Filip Sadowski3aa7b742016-10-11 15:26:58 -07004242 ret = i40e_vsi_control_rx(vsi, true);
4243 if (ret)
4244 return ret;
4245 ret = i40e_vsi_control_tx(vsi, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004246
4247 return ret;
4248}
4249
4250/**
Filip Sadowski3aa7b742016-10-11 15:26:58 -07004251 * i40e_vsi_stop_rings - Stop a VSI's rings
4252 * @vsi: the VSI being configured
4253 **/
4254void i40e_vsi_stop_rings(struct i40e_vsi *vsi)
4255{
Jacob Keller34807562017-04-13 04:45:53 -04004256 /* When port TX is suspended, don't wait */
Jacob Keller0da36b92017-04-19 09:25:55 -04004257 if (test_bit(__I40E_PORT_SUSPENDED, vsi->back->state))
Jacob Keller34807562017-04-13 04:45:53 -04004258 return i40e_vsi_stop_rings_no_wait(vsi);
4259
Filip Sadowski3aa7b742016-10-11 15:26:58 -07004260 /* do rx first for enable and last for disable
4261 * Ignore return value, we need to shutdown whatever we can
4262 */
4263 i40e_vsi_control_tx(vsi, false);
4264 i40e_vsi_control_rx(vsi, false);
4265}
4266
4267/**
Jacob Kellere4b433f2017-04-13 04:45:52 -04004268 * i40e_vsi_stop_rings_no_wait - Stop a VSI's rings and do not delay
4269 * @vsi: the VSI being shutdown
4270 *
4271 * This function stops all the rings for a VSI but does not delay to verify
4272 * that rings have been disabled. It is expected that the caller is shutting
4273 * down multiple VSIs at once and will delay together for all the VSIs after
4274 * initiating the shutdown. This is particularly useful for shutting down lots
4275 * of VFs together. Otherwise, a large delay can be incurred while configuring
4276 * each VSI in serial.
4277 **/
4278void i40e_vsi_stop_rings_no_wait(struct i40e_vsi *vsi)
4279{
4280 struct i40e_pf *pf = vsi->back;
4281 int i, pf_q;
4282
4283 pf_q = vsi->base_queue;
4284 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4285 i40e_control_tx_q(pf, pf_q, false);
4286 i40e_control_rx_q(pf, pf_q, false);
4287 }
4288}
4289
4290/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004291 * i40e_vsi_free_irq - Free the irq association with the OS
4292 * @vsi: the VSI being configured
4293 **/
4294static void i40e_vsi_free_irq(struct i40e_vsi *vsi)
4295{
4296 struct i40e_pf *pf = vsi->back;
4297 struct i40e_hw *hw = &pf->hw;
4298 int base = vsi->base_vector;
4299 u32 val, qp;
4300 int i;
4301
4302 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4303 if (!vsi->q_vectors)
4304 return;
4305
Shannon Nelson63741842014-04-23 04:50:16 +00004306 if (!vsi->irqs_ready)
4307 return;
4308
4309 vsi->irqs_ready = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004310 for (i = 0; i < vsi->num_q_vectors; i++) {
Alan Brady96db7762016-09-14 16:24:38 -07004311 int irq_num;
4312 u16 vector;
4313
4314 vector = i + base;
4315 irq_num = pf->msix_entries[vector].vector;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004316
4317 /* free only the irqs that were actually requested */
Shannon Nelson78681b12013-11-28 06:39:36 +00004318 if (!vsi->q_vectors[i] ||
4319 !vsi->q_vectors[i]->num_ringpairs)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004320 continue;
4321
Alan Brady96db7762016-09-14 16:24:38 -07004322 /* clear the affinity notifier in the IRQ descriptor */
4323 irq_set_affinity_notifier(irq_num, NULL);
Jacob Keller759dc4a2017-07-14 09:10:10 -04004324 /* remove our suggested affinity mask for this IRQ */
Alan Brady96db7762016-09-14 16:24:38 -07004325 irq_set_affinity_hint(irq_num, NULL);
4326 synchronize_irq(irq_num);
4327 free_irq(irq_num, vsi->q_vectors[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004328
4329 /* Tear down the interrupt queue link list
4330 *
4331 * We know that they come in pairs and always
4332 * the Rx first, then the Tx. To clear the
4333 * link list, stick the EOL value into the
4334 * next_q field of the registers.
4335 */
4336 val = rd32(hw, I40E_PFINT_LNKLSTN(vector - 1));
4337 qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
4338 >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4339 val |= I40E_QUEUE_END_OF_LIST
4340 << I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4341 wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), val);
4342
4343 while (qp != I40E_QUEUE_END_OF_LIST) {
4344 u32 next;
4345
4346 val = rd32(hw, I40E_QINT_RQCTL(qp));
4347
4348 val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
4349 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
4350 I40E_QINT_RQCTL_CAUSE_ENA_MASK |
4351 I40E_QINT_RQCTL_INTEVENT_MASK);
4352
4353 val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
4354 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
4355
4356 wr32(hw, I40E_QINT_RQCTL(qp), val);
4357
4358 val = rd32(hw, I40E_QINT_TQCTL(qp));
4359
4360 next = (val & I40E_QINT_TQCTL_NEXTQ_INDX_MASK)
4361 >> I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT;
4362
4363 val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
4364 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
4365 I40E_QINT_TQCTL_CAUSE_ENA_MASK |
4366 I40E_QINT_TQCTL_INTEVENT_MASK);
4367
4368 val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
4369 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
4370
4371 wr32(hw, I40E_QINT_TQCTL(qp), val);
4372 qp = next;
4373 }
4374 }
4375 } else {
4376 free_irq(pf->pdev->irq, pf);
4377
4378 val = rd32(hw, I40E_PFINT_LNKLST0);
4379 qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
4380 >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4381 val |= I40E_QUEUE_END_OF_LIST
4382 << I40E_PFINT_LNKLST0_FIRSTQ_INDX_SHIFT;
4383 wr32(hw, I40E_PFINT_LNKLST0, val);
4384
4385 val = rd32(hw, I40E_QINT_RQCTL(qp));
4386 val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
4387 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
4388 I40E_QINT_RQCTL_CAUSE_ENA_MASK |
4389 I40E_QINT_RQCTL_INTEVENT_MASK);
4390
4391 val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
4392 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
4393
4394 wr32(hw, I40E_QINT_RQCTL(qp), val);
4395
4396 val = rd32(hw, I40E_QINT_TQCTL(qp));
4397
4398 val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
4399 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
4400 I40E_QINT_TQCTL_CAUSE_ENA_MASK |
4401 I40E_QINT_TQCTL_INTEVENT_MASK);
4402
4403 val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
4404 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
4405
4406 wr32(hw, I40E_QINT_TQCTL(qp), val);
4407 }
4408}
4409
4410/**
Alexander Duyck493fb302013-09-28 07:01:44 +00004411 * i40e_free_q_vector - Free memory allocated for specific interrupt vector
4412 * @vsi: the VSI being configured
4413 * @v_idx: Index of vector to be freed
4414 *
4415 * This function frees the memory allocated to the q_vector. In addition if
4416 * NAPI is enabled it will delete any references to the NAPI struct prior
4417 * to freeing the q_vector.
4418 **/
4419static void i40e_free_q_vector(struct i40e_vsi *vsi, int v_idx)
4420{
4421 struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00004422 struct i40e_ring *ring;
Alexander Duyck493fb302013-09-28 07:01:44 +00004423
4424 if (!q_vector)
4425 return;
4426
4427 /* disassociate q_vector from rings */
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00004428 i40e_for_each_ring(ring, q_vector->tx)
4429 ring->q_vector = NULL;
4430
4431 i40e_for_each_ring(ring, q_vector->rx)
4432 ring->q_vector = NULL;
Alexander Duyck493fb302013-09-28 07:01:44 +00004433
4434 /* only VSI w/ an associated netdev is set up w/ NAPI */
4435 if (vsi->netdev)
4436 netif_napi_del(&q_vector->napi);
4437
4438 vsi->q_vectors[v_idx] = NULL;
4439
4440 kfree_rcu(q_vector, rcu);
4441}
4442
4443/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004444 * i40e_vsi_free_q_vectors - Free memory allocated for interrupt vectors
4445 * @vsi: the VSI being un-configured
4446 *
4447 * This frees the memory allocated to the q_vectors and
4448 * deletes references to the NAPI struct.
4449 **/
4450static void i40e_vsi_free_q_vectors(struct i40e_vsi *vsi)
4451{
4452 int v_idx;
4453
Alexander Duyck493fb302013-09-28 07:01:44 +00004454 for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++)
4455 i40e_free_q_vector(vsi, v_idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004456}
4457
4458/**
4459 * i40e_reset_interrupt_capability - Disable interrupt setup in OS
4460 * @pf: board private structure
4461 **/
4462static void i40e_reset_interrupt_capability(struct i40e_pf *pf)
4463{
4464 /* If we're in Legacy mode, the interrupt was cleaned in vsi_close */
4465 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4466 pci_disable_msix(pf->pdev);
4467 kfree(pf->msix_entries);
4468 pf->msix_entries = NULL;
Shannon Nelson3b444392015-02-26 16:15:57 +00004469 kfree(pf->irq_pile);
4470 pf->irq_pile = NULL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004471 } else if (pf->flags & I40E_FLAG_MSI_ENABLED) {
4472 pci_disable_msi(pf->pdev);
4473 }
4474 pf->flags &= ~(I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
4475}
4476
4477/**
4478 * i40e_clear_interrupt_scheme - Clear the current interrupt scheme settings
4479 * @pf: board private structure
4480 *
4481 * We go through and clear interrupt specific resources and reset the structure
4482 * to pre-load conditions
4483 **/
4484static void i40e_clear_interrupt_scheme(struct i40e_pf *pf)
4485{
4486 int i;
4487
Jacob Kellerc17401a2017-07-14 09:27:02 -04004488 i40e_free_misc_vector(pf);
Shannon Nelsone1477582015-02-21 06:44:33 +00004489
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06004490 i40e_put_lump(pf->irq_pile, pf->iwarp_base_vector,
4491 I40E_IWARP_IRQ_PILE_ID);
4492
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004493 i40e_put_lump(pf->irq_pile, 0, I40E_PILE_VALID_BIT-1);
Mitch Williams505682c2014-05-20 08:01:37 +00004494 for (i = 0; i < pf->num_alloc_vsi; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004495 if (pf->vsi[i])
4496 i40e_vsi_free_q_vectors(pf->vsi[i]);
4497 i40e_reset_interrupt_capability(pf);
4498}
4499
4500/**
4501 * i40e_napi_enable_all - Enable NAPI for all q_vectors in the VSI
4502 * @vsi: the VSI being configured
4503 **/
4504static void i40e_napi_enable_all(struct i40e_vsi *vsi)
4505{
4506 int q_idx;
4507
4508 if (!vsi->netdev)
4509 return;
4510
Alexander Duyck13a8cd12017-03-24 15:01:42 -07004511 for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
4512 struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
4513
4514 if (q_vector->rx.ring || q_vector->tx.ring)
4515 napi_enable(&q_vector->napi);
4516 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004517}
4518
4519/**
4520 * i40e_napi_disable_all - Disable NAPI for all q_vectors in the VSI
4521 * @vsi: the VSI being configured
4522 **/
4523static void i40e_napi_disable_all(struct i40e_vsi *vsi)
4524{
4525 int q_idx;
4526
4527 if (!vsi->netdev)
4528 return;
4529
Alexander Duyck13a8cd12017-03-24 15:01:42 -07004530 for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
4531 struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
4532
4533 if (q_vector->rx.ring || q_vector->tx.ring)
4534 napi_disable(&q_vector->napi);
4535 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004536}
4537
4538/**
Shannon Nelson90ef8d42014-03-14 07:32:26 +00004539 * i40e_vsi_close - Shut down a VSI
4540 * @vsi: the vsi to be quelled
4541 **/
4542static void i40e_vsi_close(struct i40e_vsi *vsi)
4543{
Mitch Williams0ef2d5a2017-01-24 10:24:00 -08004544 struct i40e_pf *pf = vsi->back;
Jacob Keller0da36b92017-04-19 09:25:55 -04004545 if (!test_and_set_bit(__I40E_VSI_DOWN, vsi->state))
Shannon Nelson90ef8d42014-03-14 07:32:26 +00004546 i40e_down(vsi);
4547 i40e_vsi_free_irq(vsi);
4548 i40e_vsi_free_tx_resources(vsi);
4549 i40e_vsi_free_rx_resources(vsi);
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04004550 vsi->current_netdev_flags = 0;
Mitch Williams0ef2d5a2017-01-24 10:24:00 -08004551 pf->flags |= I40E_FLAG_SERVICE_CLIENT_REQUESTED;
Jacob Keller0da36b92017-04-19 09:25:55 -04004552 if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
Mitch Williams0ef2d5a2017-01-24 10:24:00 -08004553 pf->flags |= I40E_FLAG_CLIENT_RESET;
Shannon Nelson90ef8d42014-03-14 07:32:26 +00004554}
4555
4556/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004557 * i40e_quiesce_vsi - Pause a given VSI
4558 * @vsi: the VSI being paused
4559 **/
4560static void i40e_quiesce_vsi(struct i40e_vsi *vsi)
4561{
Jacob Keller0da36b92017-04-19 09:25:55 -04004562 if (test_bit(__I40E_VSI_DOWN, vsi->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004563 return;
4564
Jacob Keller0da36b92017-04-19 09:25:55 -04004565 set_bit(__I40E_VSI_NEEDS_RESTART, vsi->state);
Jesse Brandeburg6995b362015-08-28 17:55:54 -04004566 if (vsi->netdev && netif_running(vsi->netdev))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004567 vsi->netdev->netdev_ops->ndo_stop(vsi->netdev);
Jesse Brandeburg6995b362015-08-28 17:55:54 -04004568 else
Shannon Nelson90ef8d42014-03-14 07:32:26 +00004569 i40e_vsi_close(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004570}
4571
4572/**
4573 * i40e_unquiesce_vsi - Resume a given VSI
4574 * @vsi: the VSI being resumed
4575 **/
4576static void i40e_unquiesce_vsi(struct i40e_vsi *vsi)
4577{
Jacob Keller0da36b92017-04-19 09:25:55 -04004578 if (!test_and_clear_bit(__I40E_VSI_NEEDS_RESTART, vsi->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004579 return;
4580
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004581 if (vsi->netdev && netif_running(vsi->netdev))
4582 vsi->netdev->netdev_ops->ndo_open(vsi->netdev);
4583 else
Shannon Nelson8276f752014-03-14 07:32:27 +00004584 i40e_vsi_open(vsi); /* this clears the DOWN bit */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004585}
4586
4587/**
4588 * i40e_pf_quiesce_all_vsi - Pause all VSIs on a PF
4589 * @pf: the PF
4590 **/
4591static void i40e_pf_quiesce_all_vsi(struct i40e_pf *pf)
4592{
4593 int v;
4594
Mitch Williams505682c2014-05-20 08:01:37 +00004595 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004596 if (pf->vsi[v])
4597 i40e_quiesce_vsi(pf->vsi[v]);
4598 }
4599}
4600
4601/**
4602 * i40e_pf_unquiesce_all_vsi - Resume all VSIs on a PF
4603 * @pf: the PF
4604 **/
4605static void i40e_pf_unquiesce_all_vsi(struct i40e_pf *pf)
4606{
4607 int v;
4608
Mitch Williams505682c2014-05-20 08:01:37 +00004609 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004610 if (pf->vsi[v])
4611 i40e_unquiesce_vsi(pf->vsi[v]);
4612 }
4613}
4614
Neerav Parikh69129dc2014-11-12 00:18:46 +00004615/**
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004616 * i40e_vsi_wait_queues_disabled - Wait for VSI's queues to be disabled
Neerav Parikh69129dc2014-11-12 00:18:46 +00004617 * @vsi: the VSI being configured
4618 *
Jacob Kelleraf26ce22017-04-05 07:50:58 -04004619 * Wait until all queues on a given VSI have been disabled.
Neerav Parikh69129dc2014-11-12 00:18:46 +00004620 **/
Jacob Kellere4b433f2017-04-13 04:45:52 -04004621int i40e_vsi_wait_queues_disabled(struct i40e_vsi *vsi)
Neerav Parikh69129dc2014-11-12 00:18:46 +00004622{
4623 struct i40e_pf *pf = vsi->back;
4624 int i, pf_q, ret;
4625
4626 pf_q = vsi->base_queue;
4627 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
Jacob Kelleraf26ce22017-04-05 07:50:58 -04004628 /* Check and wait for the Tx queue */
Neerav Parikh69129dc2014-11-12 00:18:46 +00004629 ret = i40e_pf_txq_wait(pf, pf_q, false);
4630 if (ret) {
4631 dev_info(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04004632 "VSI seid %d Tx ring %d disable timeout\n",
4633 vsi->seid, pf_q);
Neerav Parikh69129dc2014-11-12 00:18:46 +00004634 return ret;
4635 }
Björn Töpel74608d12017-05-24 07:55:35 +02004636
4637 if (!i40e_enabled_xdp_vsi(vsi))
4638 goto wait_rx;
4639
4640 /* Check and wait for the XDP Tx queue */
4641 ret = i40e_pf_txq_wait(pf, pf_q + vsi->alloc_queue_pairs,
4642 false);
4643 if (ret) {
4644 dev_info(&pf->pdev->dev,
4645 "VSI seid %d XDP Tx ring %d disable timeout\n",
4646 vsi->seid, pf_q);
4647 return ret;
4648 }
4649wait_rx:
4650 /* Check and wait for the Rx queue */
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004651 ret = i40e_pf_rxq_wait(pf, pf_q, false);
4652 if (ret) {
4653 dev_info(&pf->pdev->dev,
4654 "VSI seid %d Rx ring %d disable timeout\n",
4655 vsi->seid, pf_q);
4656 return ret;
4657 }
4658 }
4659
Neerav Parikh69129dc2014-11-12 00:18:46 +00004660 return 0;
4661}
4662
Jacob Kellere4b433f2017-04-13 04:45:52 -04004663#ifdef CONFIG_I40E_DCB
Neerav Parikh69129dc2014-11-12 00:18:46 +00004664/**
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004665 * i40e_pf_wait_queues_disabled - Wait for all queues of PF VSIs to be disabled
Neerav Parikh69129dc2014-11-12 00:18:46 +00004666 * @pf: the PF
4667 *
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004668 * This function waits for the queues to be in disabled state for all the
Neerav Parikh69129dc2014-11-12 00:18:46 +00004669 * VSIs that are managed by this PF.
4670 **/
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004671static int i40e_pf_wait_queues_disabled(struct i40e_pf *pf)
Neerav Parikh69129dc2014-11-12 00:18:46 +00004672{
4673 int v, ret = 0;
4674
4675 for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
Alexander Duyckc76cb6e2017-02-21 15:55:48 -08004676 if (pf->vsi[v]) {
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004677 ret = i40e_vsi_wait_queues_disabled(pf->vsi[v]);
Neerav Parikh69129dc2014-11-12 00:18:46 +00004678 if (ret)
4679 break;
4680 }
4681 }
4682
4683 return ret;
4684}
4685
4686#endif
Kiran Patilb03a8c12015-09-24 18:13:15 -04004687
4688/**
4689 * i40e_detect_recover_hung_queue - Function to detect and recover hung_queue
4690 * @q_idx: TX queue number
4691 * @vsi: Pointer to VSI struct
4692 *
4693 * This function checks specified queue for given VSI. Detects hung condition.
Alan Brady17daabb2017-04-05 07:50:56 -04004694 * We proactively detect hung TX queues by checking if interrupts are disabled
4695 * but there are pending descriptors. If it appears hung, attempt to recover
4696 * by triggering a SW interrupt.
Kiran Patilb03a8c12015-09-24 18:13:15 -04004697 **/
4698static void i40e_detect_recover_hung_queue(int q_idx, struct i40e_vsi *vsi)
4699{
4700 struct i40e_ring *tx_ring = NULL;
4701 struct i40e_pf *pf;
Alan Brady17daabb2017-04-05 07:50:56 -04004702 u32 val, tx_pending;
Kiran Patilb03a8c12015-09-24 18:13:15 -04004703 int i;
4704
4705 pf = vsi->back;
4706
4707 /* now that we have an index, find the tx_ring struct */
4708 for (i = 0; i < vsi->num_queue_pairs; i++) {
4709 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc) {
4710 if (q_idx == vsi->tx_rings[i]->queue_index) {
4711 tx_ring = vsi->tx_rings[i];
4712 break;
4713 }
4714 }
4715 }
4716
4717 if (!tx_ring)
4718 return;
4719
4720 /* Read interrupt register */
4721 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
4722 val = rd32(&pf->hw,
4723 I40E_PFINT_DYN_CTLN(tx_ring->q_vector->v_idx +
4724 tx_ring->vsi->base_vector - 1));
4725 else
4726 val = rd32(&pf->hw, I40E_PFINT_DYN_CTL0);
4727
Alan Brady17daabb2017-04-05 07:50:56 -04004728 tx_pending = i40e_get_tx_pending(tx_ring);
Kiran Patilb03a8c12015-09-24 18:13:15 -04004729
Alan Brady17daabb2017-04-05 07:50:56 -04004730 /* Interrupts are disabled and TX pending is non-zero,
4731 * trigger the SW interrupt (don't wait). Worst case
4732 * there will be one extra interrupt which may result
4733 * into not cleaning any queues because queues are cleaned.
Kiran Patilb03a8c12015-09-24 18:13:15 -04004734 */
Alan Brady17daabb2017-04-05 07:50:56 -04004735 if (tx_pending && (!(val & I40E_PFINT_DYN_CTLN_INTENA_MASK)))
4736 i40e_force_wb(vsi, tx_ring->q_vector);
Kiran Patilb03a8c12015-09-24 18:13:15 -04004737}
4738
4739/**
4740 * i40e_detect_recover_hung - Function to detect and recover hung_queues
4741 * @pf: pointer to PF struct
4742 *
4743 * LAN VSI has netdev and netdev has TX queues. This function is to check
4744 * each of those TX queues if they are hung, trigger recovery by issuing
4745 * SW interrupt.
4746 **/
4747static void i40e_detect_recover_hung(struct i40e_pf *pf)
4748{
4749 struct net_device *netdev;
4750 struct i40e_vsi *vsi;
Jesse Brandeburgb85c94b2017-06-20 15:16:59 -07004751 unsigned int i;
Kiran Patilb03a8c12015-09-24 18:13:15 -04004752
4753 /* Only for LAN VSI */
4754 vsi = pf->vsi[pf->lan_vsi];
4755
4756 if (!vsi)
4757 return;
4758
4759 /* Make sure, VSI state is not DOWN/RECOVERY_PENDING */
Jacob Keller0da36b92017-04-19 09:25:55 -04004760 if (test_bit(__I40E_VSI_DOWN, vsi->back->state) ||
4761 test_bit(__I40E_RESET_RECOVERY_PENDING, vsi->back->state))
Kiran Patilb03a8c12015-09-24 18:13:15 -04004762 return;
4763
4764 /* Make sure type is MAIN VSI */
4765 if (vsi->type != I40E_VSI_MAIN)
4766 return;
4767
4768 netdev = vsi->netdev;
4769 if (!netdev)
4770 return;
4771
4772 /* Bail out if netif_carrier is not OK */
4773 if (!netif_carrier_ok(netdev))
4774 return;
4775
4776 /* Go thru' TX queues for netdev */
4777 for (i = 0; i < netdev->num_tx_queues; i++) {
4778 struct netdev_queue *q;
4779
4780 q = netdev_get_tx_queue(netdev, i);
4781 if (q)
4782 i40e_detect_recover_hung_queue(i, vsi);
4783 }
4784}
4785
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004786/**
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004787 * i40e_get_iscsi_tc_map - Return TC map for iSCSI APP
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00004788 * @pf: pointer to PF
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004789 *
4790 * Get TC map for ISCSI PF type that will include iSCSI TC
4791 * and LAN TC.
4792 **/
4793static u8 i40e_get_iscsi_tc_map(struct i40e_pf *pf)
4794{
4795 struct i40e_dcb_app_priority_table app;
4796 struct i40e_hw *hw = &pf->hw;
4797 u8 enabled_tc = 1; /* TC0 is always enabled */
4798 u8 tc, i;
4799 /* Get the iSCSI APP TLV */
4800 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
4801
4802 for (i = 0; i < dcbcfg->numapps; i++) {
4803 app = dcbcfg->app[i];
4804 if (app.selector == I40E_APP_SEL_TCPIP &&
4805 app.protocolid == I40E_APP_PROTOID_ISCSI) {
4806 tc = dcbcfg->etscfg.prioritytable[app.priority];
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08004807 enabled_tc |= BIT(tc);
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004808 break;
4809 }
4810 }
4811
4812 return enabled_tc;
4813}
4814
4815/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004816 * i40e_dcb_get_num_tc - Get the number of TCs from DCBx config
4817 * @dcbcfg: the corresponding DCBx configuration structure
4818 *
4819 * Return the number of TCs from given DCBx configuration
4820 **/
4821static u8 i40e_dcb_get_num_tc(struct i40e_dcbx_config *dcbcfg)
4822{
Dave Ertmanfbfe12c2016-08-12 09:56:32 -07004823 int i, tc_unused = 0;
Jesse Brandeburg078b5872013-09-25 23:41:14 +00004824 u8 num_tc = 0;
Dave Ertmanfbfe12c2016-08-12 09:56:32 -07004825 u8 ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004826
4827 /* Scan the ETS Config Priority Table to find
4828 * traffic class enabled for a given priority
Dave Ertmanfbfe12c2016-08-12 09:56:32 -07004829 * and create a bitmask of enabled TCs
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004830 */
Dave Ertmanfbfe12c2016-08-12 09:56:32 -07004831 for (i = 0; i < I40E_MAX_USER_PRIORITY; i++)
4832 num_tc |= BIT(dcbcfg->etscfg.prioritytable[i]);
4833
4834 /* Now scan the bitmask to check for
4835 * contiguous TCs starting with TC0
4836 */
4837 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
4838 if (num_tc & BIT(i)) {
4839 if (!tc_unused) {
4840 ret++;
4841 } else {
4842 pr_err("Non-contiguous TC - Disabling DCB\n");
4843 return 1;
4844 }
4845 } else {
4846 tc_unused = 1;
4847 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004848 }
4849
Dave Ertmanfbfe12c2016-08-12 09:56:32 -07004850 /* There is always at least TC0 */
4851 if (!ret)
4852 ret = 1;
4853
4854 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004855}
4856
4857/**
4858 * i40e_dcb_get_enabled_tc - Get enabled traffic classes
4859 * @dcbcfg: the corresponding DCBx configuration structure
4860 *
4861 * Query the current DCB configuration and return the number of
4862 * traffic classes enabled from the given DCBX config
4863 **/
4864static u8 i40e_dcb_get_enabled_tc(struct i40e_dcbx_config *dcbcfg)
4865{
4866 u8 num_tc = i40e_dcb_get_num_tc(dcbcfg);
4867 u8 enabled_tc = 1;
4868 u8 i;
4869
4870 for (i = 0; i < num_tc; i++)
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04004871 enabled_tc |= BIT(i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004872
4873 return enabled_tc;
4874}
4875
4876/**
4877 * i40e_pf_get_num_tc - Get enabled traffic classes for PF
4878 * @pf: PF being queried
4879 *
4880 * Return number of traffic classes enabled for the given PF
4881 **/
4882static u8 i40e_pf_get_num_tc(struct i40e_pf *pf)
4883{
4884 struct i40e_hw *hw = &pf->hw;
Dave Ertman52a08ca2016-07-27 12:02:34 -07004885 u8 i, enabled_tc = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004886 u8 num_tc = 0;
4887 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
4888
4889 /* If DCB is not enabled then always in single TC */
4890 if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
4891 return 1;
4892
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004893 /* SFP mode will be enabled for all TCs on port */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004894 if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
4895 return i40e_dcb_get_num_tc(dcbcfg);
4896
4897 /* MFP mode return count of enabled TCs for this PF */
4898 if (pf->hw.func_caps.iscsi)
4899 enabled_tc = i40e_get_iscsi_tc_map(pf);
4900 else
Neerav Parikhfc51de92015-02-24 06:58:53 +00004901 return 1; /* Only TC0 */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004902
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004903 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08004904 if (enabled_tc & BIT(i))
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004905 num_tc++;
4906 }
4907 return num_tc;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004908}
4909
4910/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004911 * i40e_pf_get_pf_tc_map - Get bitmap for enabled traffic classes
4912 * @pf: PF being queried
4913 *
4914 * Return a bitmap for enabled traffic classes for this PF.
4915 **/
4916static u8 i40e_pf_get_tc_map(struct i40e_pf *pf)
4917{
4918 /* If DCB is not enabled for this PF then just return default TC */
4919 if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
David Ertmanea6acb72016-09-20 07:10:50 -07004920 return I40E_DEFAULT_TRAFFIC_CLASS;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004921
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004922 /* SFP mode we want PF to be enabled for all TCs */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004923 if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
4924 return i40e_dcb_get_enabled_tc(&pf->hw.local_dcbx_config);
4925
Neerav Parikhfc51de92015-02-24 06:58:53 +00004926 /* MFP enabled and iSCSI PF type */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004927 if (pf->hw.func_caps.iscsi)
4928 return i40e_get_iscsi_tc_map(pf);
4929 else
David Ertmanea6acb72016-09-20 07:10:50 -07004930 return I40E_DEFAULT_TRAFFIC_CLASS;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004931}
4932
4933/**
4934 * i40e_vsi_get_bw_info - Query VSI BW Information
4935 * @vsi: the VSI being queried
4936 *
4937 * Returns 0 on success, negative value on failure
4938 **/
4939static int i40e_vsi_get_bw_info(struct i40e_vsi *vsi)
4940{
4941 struct i40e_aqc_query_vsi_ets_sla_config_resp bw_ets_config = {0};
4942 struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
4943 struct i40e_pf *pf = vsi->back;
4944 struct i40e_hw *hw = &pf->hw;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004945 i40e_status ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004946 u32 tc_bw_max;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004947 int i;
4948
4949 /* Get the VSI level BW configuration */
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004950 ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid, &bw_config, NULL);
4951 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004952 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004953 "couldn't get PF vsi bw config, err %s aq_err %s\n",
4954 i40e_stat_str(&pf->hw, ret),
4955 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004956 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004957 }
4958
4959 /* Get the VSI level BW configuration per TC */
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004960 ret = i40e_aq_query_vsi_ets_sla_config(hw, vsi->seid, &bw_ets_config,
4961 NULL);
4962 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004963 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004964 "couldn't get PF vsi ets bw config, err %s aq_err %s\n",
4965 i40e_stat_str(&pf->hw, ret),
4966 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004967 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004968 }
4969
4970 if (bw_config.tc_valid_bits != bw_ets_config.tc_valid_bits) {
4971 dev_info(&pf->pdev->dev,
4972 "Enabled TCs mismatch from querying VSI BW info 0x%08x 0x%08x\n",
4973 bw_config.tc_valid_bits,
4974 bw_ets_config.tc_valid_bits);
4975 /* Still continuing */
4976 }
4977
4978 vsi->bw_limit = le16_to_cpu(bw_config.port_bw_limit);
4979 vsi->bw_max_quanta = bw_config.max_bw;
4980 tc_bw_max = le16_to_cpu(bw_ets_config.tc_bw_max[0]) |
4981 (le16_to_cpu(bw_ets_config.tc_bw_max[1]) << 16);
4982 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
4983 vsi->bw_ets_share_credits[i] = bw_ets_config.share_credits[i];
4984 vsi->bw_ets_limit_credits[i] =
4985 le16_to_cpu(bw_ets_config.credits[i]);
4986 /* 3 bits out of 4 for each TC */
4987 vsi->bw_ets_max_quanta[i] = (u8)((tc_bw_max >> (i*4)) & 0x7);
4988 }
Jesse Brandeburg078b5872013-09-25 23:41:14 +00004989
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004990 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004991}
4992
4993/**
4994 * i40e_vsi_configure_bw_alloc - Configure VSI BW allocation per TC
4995 * @vsi: the VSI being configured
4996 * @enabled_tc: TC bitmap
4997 * @bw_credits: BW shared credits per TC
4998 *
4999 * Returns 0 on success, negative value on failure
5000 **/
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00005001static int i40e_vsi_configure_bw_alloc(struct i40e_vsi *vsi, u8 enabled_tc,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005002 u8 *bw_share)
5003{
5004 struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005005 i40e_status ret;
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00005006 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005007
5008 bw_data.tc_valid_bits = enabled_tc;
5009 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5010 bw_data.tc_bw_credits[i] = bw_share[i];
5011
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005012 ret = i40e_aq_config_vsi_tc_bw(&vsi->back->hw, vsi->seid, &bw_data,
5013 NULL);
5014 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005015 dev_info(&vsi->back->pdev->dev,
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005016 "AQ command Config VSI BW allocation per TC failed = %d\n",
5017 vsi->back->hw.aq.asq_last_status);
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00005018 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005019 }
5020
5021 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5022 vsi->info.qs_handle[i] = bw_data.qs_handles[i];
5023
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00005024 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005025}
5026
5027/**
5028 * i40e_vsi_config_netdev_tc - Setup the netdev TC configuration
5029 * @vsi: the VSI being configured
5030 * @enabled_tc: TC map to be enabled
5031 *
5032 **/
5033static void i40e_vsi_config_netdev_tc(struct i40e_vsi *vsi, u8 enabled_tc)
5034{
5035 struct net_device *netdev = vsi->netdev;
5036 struct i40e_pf *pf = vsi->back;
5037 struct i40e_hw *hw = &pf->hw;
5038 u8 netdev_tc = 0;
5039 int i;
5040 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5041
5042 if (!netdev)
5043 return;
5044
5045 if (!enabled_tc) {
5046 netdev_reset_tc(netdev);
5047 return;
5048 }
5049
5050 /* Set up actual enabled TCs on the VSI */
5051 if (netdev_set_num_tc(netdev, vsi->tc_config.numtc))
5052 return;
5053
5054 /* set per TC queues for the VSI */
5055 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5056 /* Only set TC queues for enabled tcs
5057 *
5058 * e.g. For a VSI that has TC0 and TC3 enabled the
5059 * enabled_tc bitmap would be 0x00001001; the driver
5060 * will set the numtc for netdev as 2 that will be
5061 * referenced by the netdev layer as TC 0 and 1.
5062 */
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08005063 if (vsi->tc_config.enabled_tc & BIT(i))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005064 netdev_set_tc_queue(netdev,
5065 vsi->tc_config.tc_info[i].netdev_tc,
5066 vsi->tc_config.tc_info[i].qcount,
5067 vsi->tc_config.tc_info[i].qoffset);
5068 }
5069
5070 /* Assign UP2TC map for the VSI */
5071 for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
5072 /* Get the actual TC# for the UP */
5073 u8 ets_tc = dcbcfg->etscfg.prioritytable[i];
5074 /* Get the mapped netdev TC# for the UP */
5075 netdev_tc = vsi->tc_config.tc_info[ets_tc].netdev_tc;
5076 netdev_set_prio_tc_map(netdev, i, netdev_tc);
5077 }
5078}
5079
5080/**
5081 * i40e_vsi_update_queue_map - Update our copy of VSi info with new queue map
5082 * @vsi: the VSI being configured
5083 * @ctxt: the ctxt buffer returned from AQ VSI update param command
5084 **/
5085static void i40e_vsi_update_queue_map(struct i40e_vsi *vsi,
5086 struct i40e_vsi_context *ctxt)
5087{
5088 /* copy just the sections touched not the entire info
5089 * since not all sections are valid as returned by
5090 * update vsi params
5091 */
5092 vsi->info.mapping_flags = ctxt->info.mapping_flags;
5093 memcpy(&vsi->info.queue_mapping,
5094 &ctxt->info.queue_mapping, sizeof(vsi->info.queue_mapping));
5095 memcpy(&vsi->info.tc_mapping, ctxt->info.tc_mapping,
5096 sizeof(vsi->info.tc_mapping));
5097}
5098
5099/**
5100 * i40e_vsi_config_tc - Configure VSI Tx Scheduler for given TC map
5101 * @vsi: VSI to be configured
5102 * @enabled_tc: TC bitmap
5103 *
5104 * This configures a particular VSI for TCs that are mapped to the
5105 * given TC bitmap. It uses default bandwidth share for TCs across
5106 * VSIs to configure TC for a particular VSI.
5107 *
5108 * NOTE:
5109 * It is expected that the VSI queues have been quisced before calling
5110 * this function.
5111 **/
5112static int i40e_vsi_config_tc(struct i40e_vsi *vsi, u8 enabled_tc)
5113{
5114 u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
5115 struct i40e_vsi_context ctxt;
5116 int ret = 0;
5117 int i;
5118
5119 /* Check if enabled_tc is same as existing or new TCs */
5120 if (vsi->tc_config.enabled_tc == enabled_tc)
5121 return ret;
5122
5123 /* Enable ETS TCs with equal BW Share for now across all VSIs */
5124 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08005125 if (enabled_tc & BIT(i))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005126 bw_share[i] = 1;
5127 }
5128
5129 ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
5130 if (ret) {
5131 dev_info(&vsi->back->pdev->dev,
5132 "Failed configuring TC map %d for VSI %d\n",
5133 enabled_tc, vsi->seid);
5134 goto out;
5135 }
5136
5137 /* Update Queue Pairs Mapping for currently enabled UPs */
5138 ctxt.seid = vsi->seid;
5139 ctxt.pf_num = vsi->back->hw.pf_id;
5140 ctxt.vf_num = 0;
5141 ctxt.uplink_seid = vsi->uplink_seid;
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07005142 ctxt.info = vsi->info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005143 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
5144
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06005145 if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
5146 ctxt.info.valid_sections |=
5147 cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
5148 ctxt.info.queueing_opt_flags |= I40E_AQ_VSI_QUE_OPT_TCP_ENA;
5149 }
5150
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005151 /* Update the VSI after updating the VSI queue-mapping information */
5152 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
5153 if (ret) {
5154 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005155 "Update vsi tc config failed, err %s aq_err %s\n",
5156 i40e_stat_str(&vsi->back->hw, ret),
5157 i40e_aq_str(&vsi->back->hw,
5158 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005159 goto out;
5160 }
5161 /* update the local VSI info with updated queue map */
5162 i40e_vsi_update_queue_map(vsi, &ctxt);
5163 vsi->info.valid_sections = 0;
5164
5165 /* Update current VSI BW information */
5166 ret = i40e_vsi_get_bw_info(vsi);
5167 if (ret) {
5168 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005169 "Failed updating vsi bw info, err %s aq_err %s\n",
5170 i40e_stat_str(&vsi->back->hw, ret),
5171 i40e_aq_str(&vsi->back->hw,
5172 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005173 goto out;
5174 }
5175
5176 /* Update the netdev TC setup */
5177 i40e_vsi_config_netdev_tc(vsi, enabled_tc);
5178out:
5179 return ret;
5180}
5181
5182/**
Amritha Nambiar8f88b302017-09-07 04:00:17 -07005183 * i40e_remove_queue_channels - Remove queue channels for the TCs
5184 * @vsi: VSI to be configured
5185 *
5186 * Remove queue channels for the TCs
5187 **/
5188static void i40e_remove_queue_channels(struct i40e_vsi *vsi)
5189{
5190 struct i40e_channel *ch, *ch_tmp;
5191 int ret, i;
5192
5193 /* Reset rss size that was stored when reconfiguring rss for
5194 * channel VSIs with non-power-of-2 queue count.
5195 */
5196 vsi->current_rss_size = 0;
5197
5198 /* perform cleanup for channels if they exist */
5199 if (list_empty(&vsi->ch_list))
5200 return;
5201
5202 list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
5203 struct i40e_vsi *p_vsi;
5204
5205 list_del(&ch->list);
5206 p_vsi = ch->parent_vsi;
5207 if (!p_vsi || !ch->initialized) {
5208 kfree(ch);
5209 continue;
5210 }
5211 /* Reset queue contexts */
5212 for (i = 0; i < ch->num_queue_pairs; i++) {
5213 struct i40e_ring *tx_ring, *rx_ring;
5214 u16 pf_q;
5215
5216 pf_q = ch->base_queue + i;
5217 tx_ring = vsi->tx_rings[pf_q];
5218 tx_ring->ch = NULL;
5219
5220 rx_ring = vsi->rx_rings[pf_q];
5221 rx_ring->ch = NULL;
5222 }
5223
5224 /* delete VSI from FW */
5225 ret = i40e_aq_delete_element(&vsi->back->hw, ch->seid,
5226 NULL);
5227 if (ret)
5228 dev_err(&vsi->back->pdev->dev,
5229 "unable to remove channel (%d) for parent VSI(%d)\n",
5230 ch->seid, p_vsi->seid);
5231 kfree(ch);
5232 }
5233 INIT_LIST_HEAD(&vsi->ch_list);
5234}
5235
5236/**
5237 * i40e_is_any_channel - channel exist or not
5238 * @vsi: ptr to VSI to which channels are associated with
5239 *
5240 * Returns true or false if channel(s) exist for associated VSI or not
5241 **/
5242static bool i40e_is_any_channel(struct i40e_vsi *vsi)
5243{
5244 struct i40e_channel *ch, *ch_tmp;
5245
5246 list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
5247 if (ch->initialized)
5248 return true;
5249 }
5250
5251 return false;
5252}
5253
5254/**
5255 * i40e_get_max_queues_for_channel
5256 * @vsi: ptr to VSI to which channels are associated with
5257 *
5258 * Helper function which returns max value among the queue counts set on the
5259 * channels/TCs created.
5260 **/
5261static int i40e_get_max_queues_for_channel(struct i40e_vsi *vsi)
5262{
5263 struct i40e_channel *ch, *ch_tmp;
5264 int max = 0;
5265
5266 list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
5267 if (!ch->initialized)
5268 continue;
5269 if (ch->num_queue_pairs > max)
5270 max = ch->num_queue_pairs;
5271 }
5272
5273 return max;
5274}
5275
5276/**
5277 * i40e_validate_num_queues - validate num_queues w.r.t channel
5278 * @pf: ptr to PF device
5279 * @num_queues: number of queues
5280 * @vsi: the parent VSI
5281 * @reconfig_rss: indicates should the RSS be reconfigured or not
5282 *
5283 * This function validates number of queues in the context of new channel
5284 * which is being established and determines if RSS should be reconfigured
5285 * or not for parent VSI.
5286 **/
5287static int i40e_validate_num_queues(struct i40e_pf *pf, int num_queues,
5288 struct i40e_vsi *vsi, bool *reconfig_rss)
5289{
5290 int max_ch_queues;
5291
5292 if (!reconfig_rss)
5293 return -EINVAL;
5294
5295 *reconfig_rss = false;
5296
5297 if (num_queues > I40E_MAX_QUEUES_PER_CH) {
5298 dev_err(&pf->pdev->dev,
5299 "Failed to create VMDq VSI. User requested num_queues (%d) > I40E_MAX_QUEUES_PER_VSI (%u)\n",
5300 num_queues, I40E_MAX_QUEUES_PER_CH);
5301 return -EINVAL;
5302 }
5303
5304 if (vsi->current_rss_size) {
5305 if (num_queues > vsi->current_rss_size) {
5306 dev_dbg(&pf->pdev->dev,
5307 "Error: num_queues (%d) > vsi's current_size(%d)\n",
5308 num_queues, vsi->current_rss_size);
5309 return -EINVAL;
5310 } else if ((num_queues < vsi->current_rss_size) &&
5311 (!is_power_of_2(num_queues))) {
5312 dev_dbg(&pf->pdev->dev,
5313 "Error: num_queues (%d) < vsi's current_size(%d), but not power of 2\n",
5314 num_queues, vsi->current_rss_size);
5315 return -EINVAL;
5316 }
5317 }
5318
5319 if (!is_power_of_2(num_queues)) {
5320 /* Find the max num_queues configured for channel if channel
5321 * exist.
5322 * if channel exist, then enforce 'num_queues' to be more than
5323 * max ever queues configured for channel.
5324 */
5325 max_ch_queues = i40e_get_max_queues_for_channel(vsi);
5326 if (num_queues < max_ch_queues) {
5327 dev_dbg(&pf->pdev->dev,
5328 "Error: num_queues (%d) < max queues configured for channel(%d)\n",
5329 num_queues, max_ch_queues);
5330 return -EINVAL;
5331 }
5332 *reconfig_rss = true;
5333 }
5334
5335 return 0;
5336}
5337
5338/**
5339 * i40e_vsi_reconfig_rss - reconfig RSS based on specified rss_size
5340 * @vsi: the VSI being setup
5341 * @rss_size: size of RSS, accordingly LUT gets reprogrammed
5342 *
5343 * This function reconfigures RSS by reprogramming LUTs using 'rss_size'
5344 **/
5345static int i40e_vsi_reconfig_rss(struct i40e_vsi *vsi, u16 rss_size)
5346{
5347 struct i40e_pf *pf = vsi->back;
5348 u8 seed[I40E_HKEY_ARRAY_SIZE];
5349 struct i40e_hw *hw = &pf->hw;
5350 int local_rss_size;
5351 u8 *lut;
5352 int ret;
5353
5354 if (!vsi->rss_size)
5355 return -EINVAL;
5356
5357 if (rss_size > vsi->rss_size)
5358 return -EINVAL;
5359
5360 local_rss_size = min_t(int, vsi->rss_size, rss_size);
5361 lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
5362 if (!lut)
5363 return -ENOMEM;
5364
5365 /* Ignoring user configured lut if there is one */
5366 i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, local_rss_size);
5367
5368 /* Use user configured hash key if there is one, otherwise
5369 * use default.
5370 */
5371 if (vsi->rss_hkey_user)
5372 memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
5373 else
5374 netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
5375
5376 ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
5377 if (ret) {
5378 dev_info(&pf->pdev->dev,
5379 "Cannot set RSS lut, err %s aq_err %s\n",
5380 i40e_stat_str(hw, ret),
5381 i40e_aq_str(hw, hw->aq.asq_last_status));
5382 kfree(lut);
5383 return ret;
5384 }
5385 kfree(lut);
5386
5387 /* Do the update w.r.t. storing rss_size */
5388 if (!vsi->orig_rss_size)
5389 vsi->orig_rss_size = vsi->rss_size;
5390 vsi->current_rss_size = local_rss_size;
5391
5392 return ret;
5393}
5394
5395/**
5396 * i40e_channel_setup_queue_map - Setup a channel queue map
5397 * @pf: ptr to PF device
5398 * @vsi: the VSI being setup
5399 * @ctxt: VSI context structure
5400 * @ch: ptr to channel structure
5401 *
5402 * Setup queue map for a specific channel
5403 **/
5404static void i40e_channel_setup_queue_map(struct i40e_pf *pf,
5405 struct i40e_vsi_context *ctxt,
5406 struct i40e_channel *ch)
5407{
5408 u16 qcount, qmap, sections = 0;
5409 u8 offset = 0;
5410 int pow;
5411
5412 sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
5413 sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
5414
5415 qcount = min_t(int, ch->num_queue_pairs, pf->num_lan_msix);
5416 ch->num_queue_pairs = qcount;
5417
5418 /* find the next higher power-of-2 of num queue pairs */
5419 pow = ilog2(qcount);
5420 if (!is_power_of_2(qcount))
5421 pow++;
5422
5423 qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
5424 (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
5425
5426 /* Setup queue TC[0].qmap for given VSI context */
5427 ctxt->info.tc_mapping[0] = cpu_to_le16(qmap);
5428
5429 ctxt->info.up_enable_bits = 0x1; /* TC0 enabled */
5430 ctxt->info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
5431 ctxt->info.queue_mapping[0] = cpu_to_le16(ch->base_queue);
5432 ctxt->info.valid_sections |= cpu_to_le16(sections);
5433}
5434
5435/**
5436 * i40e_add_channel - add a channel by adding VSI
5437 * @pf: ptr to PF device
5438 * @uplink_seid: underlying HW switching element (VEB) ID
5439 * @ch: ptr to channel structure
5440 *
5441 * Add a channel (VSI) using add_vsi and queue_map
5442 **/
5443static int i40e_add_channel(struct i40e_pf *pf, u16 uplink_seid,
5444 struct i40e_channel *ch)
5445{
5446 struct i40e_hw *hw = &pf->hw;
5447 struct i40e_vsi_context ctxt;
5448 u8 enabled_tc = 0x1; /* TC0 enabled */
5449 int ret;
5450
5451 if (ch->type != I40E_VSI_VMDQ2) {
5452 dev_info(&pf->pdev->dev,
5453 "add new vsi failed, ch->type %d\n", ch->type);
5454 return -EINVAL;
5455 }
5456
5457 memset(&ctxt, 0, sizeof(ctxt));
5458 ctxt.pf_num = hw->pf_id;
5459 ctxt.vf_num = 0;
5460 ctxt.uplink_seid = uplink_seid;
5461 ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
5462 if (ch->type == I40E_VSI_VMDQ2)
5463 ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
5464
5465 if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED) {
5466 ctxt.info.valid_sections |=
5467 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
5468 ctxt.info.switch_id =
5469 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
5470 }
5471
5472 /* Set queue map for a given VSI context */
5473 i40e_channel_setup_queue_map(pf, &ctxt, ch);
5474
5475 /* Now time to create VSI */
5476 ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
5477 if (ret) {
5478 dev_info(&pf->pdev->dev,
5479 "add new vsi failed, err %s aq_err %s\n",
5480 i40e_stat_str(&pf->hw, ret),
5481 i40e_aq_str(&pf->hw,
5482 pf->hw.aq.asq_last_status));
5483 return -ENOENT;
5484 }
5485
5486 /* Success, update channel */
5487 ch->enabled_tc = enabled_tc;
5488 ch->seid = ctxt.seid;
5489 ch->vsi_number = ctxt.vsi_number;
5490 ch->stat_counter_idx = cpu_to_le16(ctxt.info.stat_counter_idx);
5491
5492 /* copy just the sections touched not the entire info
5493 * since not all sections are valid as returned by
5494 * update vsi params
5495 */
5496 ch->info.mapping_flags = ctxt.info.mapping_flags;
5497 memcpy(&ch->info.queue_mapping,
5498 &ctxt.info.queue_mapping, sizeof(ctxt.info.queue_mapping));
5499 memcpy(&ch->info.tc_mapping, ctxt.info.tc_mapping,
5500 sizeof(ctxt.info.tc_mapping));
5501
5502 return 0;
5503}
5504
5505static int i40e_channel_config_bw(struct i40e_vsi *vsi, struct i40e_channel *ch,
5506 u8 *bw_share)
5507{
5508 struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
5509 i40e_status ret;
5510 int i;
5511
5512 bw_data.tc_valid_bits = ch->enabled_tc;
5513 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5514 bw_data.tc_bw_credits[i] = bw_share[i];
5515
5516 ret = i40e_aq_config_vsi_tc_bw(&vsi->back->hw, ch->seid,
5517 &bw_data, NULL);
5518 if (ret) {
5519 dev_info(&vsi->back->pdev->dev,
5520 "Config VSI BW allocation per TC failed, aq_err: %d for new_vsi->seid %u\n",
5521 vsi->back->hw.aq.asq_last_status, ch->seid);
5522 return -EINVAL;
5523 }
5524
5525 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5526 ch->info.qs_handle[i] = bw_data.qs_handles[i];
5527
5528 return 0;
5529}
5530
5531/**
5532 * i40e_channel_config_tx_ring - config TX ring associated with new channel
5533 * @pf: ptr to PF device
5534 * @vsi: the VSI being setup
5535 * @ch: ptr to channel structure
5536 *
5537 * Configure TX rings associated with channel (VSI) since queues are being
5538 * from parent VSI.
5539 **/
5540static int i40e_channel_config_tx_ring(struct i40e_pf *pf,
5541 struct i40e_vsi *vsi,
5542 struct i40e_channel *ch)
5543{
5544 i40e_status ret;
5545 int i;
5546 u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
5547
5548 /* Enable ETS TCs with equal BW Share for now across all VSIs */
5549 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5550 if (ch->enabled_tc & BIT(i))
5551 bw_share[i] = 1;
5552 }
5553
5554 /* configure BW for new VSI */
5555 ret = i40e_channel_config_bw(vsi, ch, bw_share);
5556 if (ret) {
5557 dev_info(&vsi->back->pdev->dev,
5558 "Failed configuring TC map %d for channel (seid %u)\n",
5559 ch->enabled_tc, ch->seid);
5560 return ret;
5561 }
5562
5563 for (i = 0; i < ch->num_queue_pairs; i++) {
5564 struct i40e_ring *tx_ring, *rx_ring;
5565 u16 pf_q;
5566
5567 pf_q = ch->base_queue + i;
5568
5569 /* Get to TX ring ptr of main VSI, for re-setup TX queue
5570 * context
5571 */
5572 tx_ring = vsi->tx_rings[pf_q];
5573 tx_ring->ch = ch;
5574
5575 /* Get the RX ring ptr */
5576 rx_ring = vsi->rx_rings[pf_q];
5577 rx_ring->ch = ch;
5578 }
5579
5580 return 0;
5581}
5582
5583/**
5584 * i40e_setup_hw_channel - setup new channel
5585 * @pf: ptr to PF device
5586 * @vsi: the VSI being setup
5587 * @ch: ptr to channel structure
5588 * @uplink_seid: underlying HW switching element (VEB) ID
5589 * @type: type of channel to be created (VMDq2/VF)
5590 *
5591 * Setup new channel (VSI) based on specified type (VMDq2/VF)
5592 * and configures TX rings accordingly
5593 **/
5594static inline int i40e_setup_hw_channel(struct i40e_pf *pf,
5595 struct i40e_vsi *vsi,
5596 struct i40e_channel *ch,
5597 u16 uplink_seid, u8 type)
5598{
5599 int ret;
5600
5601 ch->initialized = false;
5602 ch->base_queue = vsi->next_base_queue;
5603 ch->type = type;
5604
5605 /* Proceed with creation of channel (VMDq2) VSI */
5606 ret = i40e_add_channel(pf, uplink_seid, ch);
5607 if (ret) {
5608 dev_info(&pf->pdev->dev,
5609 "failed to add_channel using uplink_seid %u\n",
5610 uplink_seid);
5611 return ret;
5612 }
5613
5614 /* Mark the successful creation of channel */
5615 ch->initialized = true;
5616
5617 /* Reconfigure TX queues using QTX_CTL register */
5618 ret = i40e_channel_config_tx_ring(pf, vsi, ch);
5619 if (ret) {
5620 dev_info(&pf->pdev->dev,
5621 "failed to configure TX rings for channel %u\n",
5622 ch->seid);
5623 return ret;
5624 }
5625
5626 /* update 'next_base_queue' */
5627 vsi->next_base_queue = vsi->next_base_queue + ch->num_queue_pairs;
5628 dev_dbg(&pf->pdev->dev,
5629 "Added channel: vsi_seid %u, vsi_number %u, stat_counter_idx %u, num_queue_pairs %u, pf->next_base_queue %d\n",
5630 ch->seid, ch->vsi_number, ch->stat_counter_idx,
5631 ch->num_queue_pairs,
5632 vsi->next_base_queue);
5633 return ret;
5634}
5635
5636/**
5637 * i40e_setup_channel - setup new channel using uplink element
5638 * @pf: ptr to PF device
5639 * @type: type of channel to be created (VMDq2/VF)
5640 * @uplink_seid: underlying HW switching element (VEB) ID
5641 * @ch: ptr to channel structure
5642 *
5643 * Setup new channel (VSI) based on specified type (VMDq2/VF)
5644 * and uplink switching element (uplink_seid)
5645 **/
5646static bool i40e_setup_channel(struct i40e_pf *pf, struct i40e_vsi *vsi,
5647 struct i40e_channel *ch)
5648{
5649 u8 vsi_type;
5650 u16 seid;
5651 int ret;
5652
5653 if (vsi->type == I40E_VSI_MAIN) {
5654 vsi_type = I40E_VSI_VMDQ2;
5655 } else {
5656 dev_err(&pf->pdev->dev, "unsupported parent vsi type(%d)\n",
5657 vsi->type);
5658 return false;
5659 }
5660
5661 /* underlying switching element */
5662 seid = pf->vsi[pf->lan_vsi]->uplink_seid;
5663
5664 /* create channel (VSI), configure TX rings */
5665 ret = i40e_setup_hw_channel(pf, vsi, ch, seid, vsi_type);
5666 if (ret) {
5667 dev_err(&pf->pdev->dev, "failed to setup hw_channel\n");
5668 return false;
5669 }
5670
5671 return ch->initialized ? true : false;
5672}
5673
5674/**
5675 * i40e_create_queue_channel - function to create channel
5676 * @vsi: VSI to be configured
5677 * @ch: ptr to channel (it contains channel specific params)
5678 *
5679 * This function creates channel (VSI) using num_queues specified by user,
5680 * reconfigs RSS if needed.
5681 **/
5682int i40e_create_queue_channel(struct i40e_vsi *vsi,
5683 struct i40e_channel *ch)
5684{
5685 struct i40e_pf *pf = vsi->back;
5686 bool reconfig_rss;
5687 int err;
5688
5689 if (!ch)
5690 return -EINVAL;
5691
5692 if (!ch->num_queue_pairs) {
5693 dev_err(&pf->pdev->dev, "Invalid num_queues requested: %d\n",
5694 ch->num_queue_pairs);
5695 return -EINVAL;
5696 }
5697
5698 /* validate user requested num_queues for channel */
5699 err = i40e_validate_num_queues(pf, ch->num_queue_pairs, vsi,
5700 &reconfig_rss);
5701 if (err) {
5702 dev_info(&pf->pdev->dev, "Failed to validate num_queues (%d)\n",
5703 ch->num_queue_pairs);
5704 return -EINVAL;
5705 }
5706
5707 /* By default we are in VEPA mode, if this is the first VF/VMDq
5708 * VSI to be added switch to VEB mode.
5709 */
5710 if ((!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) ||
5711 (!i40e_is_any_channel(vsi))) {
5712 if (!is_power_of_2(vsi->tc_config.tc_info[0].qcount)) {
5713 dev_dbg(&pf->pdev->dev,
5714 "Failed to create channel. Override queues (%u) not power of 2\n",
5715 vsi->tc_config.tc_info[0].qcount);
5716 return -EINVAL;
5717 }
5718
5719 if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
5720 pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
5721
5722 if (vsi->type == I40E_VSI_MAIN) {
5723 if (pf->flags & I40E_FLAG_TC_MQPRIO)
5724 i40e_do_reset(pf, I40E_PF_RESET_FLAG,
5725 true);
5726 else
5727 i40e_do_reset_safe(pf,
5728 I40E_PF_RESET_FLAG);
5729 }
5730 }
5731 /* now onwards for main VSI, number of queues will be value
5732 * of TC0's queue count
5733 */
5734 }
5735
5736 /* By this time, vsi->cnt_q_avail shall be set to non-zero and
5737 * it should be more than num_queues
5738 */
5739 if (!vsi->cnt_q_avail || vsi->cnt_q_avail < ch->num_queue_pairs) {
5740 dev_dbg(&pf->pdev->dev,
5741 "Error: cnt_q_avail (%u) less than num_queues %d\n",
5742 vsi->cnt_q_avail, ch->num_queue_pairs);
5743 return -EINVAL;
5744 }
5745
5746 /* reconfig_rss only if vsi type is MAIN_VSI */
5747 if (reconfig_rss && (vsi->type == I40E_VSI_MAIN)) {
5748 err = i40e_vsi_reconfig_rss(vsi, ch->num_queue_pairs);
5749 if (err) {
5750 dev_info(&pf->pdev->dev,
5751 "Error: unable to reconfig rss for num_queues (%u)\n",
5752 ch->num_queue_pairs);
5753 return -EINVAL;
5754 }
5755 }
5756
5757 if (!i40e_setup_channel(pf, vsi, ch)) {
5758 dev_info(&pf->pdev->dev, "Failed to setup channel\n");
5759 return -EINVAL;
5760 }
5761
5762 dev_info(&pf->pdev->dev,
5763 "Setup channel (id:%u) utilizing num_queues %d\n",
5764 ch->seid, ch->num_queue_pairs);
5765
5766 /* in case of VF, this will be main SRIOV VSI */
5767 ch->parent_vsi = vsi;
5768
5769 /* and update main_vsi's count for queue_available to use */
5770 vsi->cnt_q_avail -= ch->num_queue_pairs;
5771
5772 return 0;
5773}
5774
5775/**
5776 * i40e_configure_queue_channels - Add queue channel for the given TCs
5777 * @vsi: VSI to be configured
5778 *
5779 * Configures queue channel mapping to the given TCs
5780 **/
5781static int i40e_configure_queue_channels(struct i40e_vsi *vsi)
5782{
5783 struct i40e_channel *ch;
5784 int ret = 0, i;
5785
5786 /* Create app vsi with the TCs. Main VSI with TC0 is already set up */
5787 for (i = 1; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5788 if (vsi->tc_config.enabled_tc & BIT(i)) {
5789 ch = kzalloc(sizeof(*ch), GFP_KERNEL);
5790 if (!ch) {
5791 ret = -ENOMEM;
5792 goto err_free;
5793 }
5794
5795 INIT_LIST_HEAD(&ch->list);
5796 ch->num_queue_pairs =
5797 vsi->tc_config.tc_info[i].qcount;
5798 ch->base_queue =
5799 vsi->tc_config.tc_info[i].qoffset;
5800
5801 list_add_tail(&ch->list, &vsi->ch_list);
5802
5803 ret = i40e_create_queue_channel(vsi, ch);
5804 if (ret) {
5805 dev_err(&vsi->back->pdev->dev,
5806 "Failed creating queue channel with TC%d: queues %d\n",
5807 i, ch->num_queue_pairs);
5808 goto err_free;
5809 }
5810 }
5811 }
5812 return ret;
5813
5814err_free:
5815 i40e_remove_queue_channels(vsi);
5816 return ret;
5817}
5818
5819/**
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005820 * i40e_veb_config_tc - Configure TCs for given VEB
5821 * @veb: given VEB
5822 * @enabled_tc: TC bitmap
5823 *
5824 * Configures given TC bitmap for VEB (switching) element
5825 **/
5826int i40e_veb_config_tc(struct i40e_veb *veb, u8 enabled_tc)
5827{
5828 struct i40e_aqc_configure_switching_comp_bw_config_data bw_data = {0};
5829 struct i40e_pf *pf = veb->pf;
5830 int ret = 0;
5831 int i;
5832
5833 /* No TCs or already enabled TCs just return */
5834 if (!enabled_tc || veb->enabled_tc == enabled_tc)
5835 return ret;
5836
5837 bw_data.tc_valid_bits = enabled_tc;
5838 /* bw_data.absolute_credits is not set (relative) */
5839
5840 /* Enable ETS TCs with equal BW Share for now */
5841 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08005842 if (enabled_tc & BIT(i))
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005843 bw_data.tc_bw_share_credits[i] = 1;
5844 }
5845
5846 ret = i40e_aq_config_switch_comp_bw_config(&pf->hw, veb->seid,
5847 &bw_data, NULL);
5848 if (ret) {
5849 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005850 "VEB bw config failed, err %s aq_err %s\n",
5851 i40e_stat_str(&pf->hw, ret),
5852 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005853 goto out;
5854 }
5855
5856 /* Update the BW information */
5857 ret = i40e_veb_get_bw_info(veb);
5858 if (ret) {
5859 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005860 "Failed getting veb bw config, err %s aq_err %s\n",
5861 i40e_stat_str(&pf->hw, ret),
5862 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005863 }
5864
5865out:
5866 return ret;
5867}
5868
5869#ifdef CONFIG_I40E_DCB
5870/**
5871 * i40e_dcb_reconfigure - Reconfigure all VEBs and VSIs
5872 * @pf: PF struct
5873 *
5874 * Reconfigure VEB/VSIs on a given PF; it is assumed that
5875 * the caller would've quiesce all the VSIs before calling
5876 * this function
5877 **/
5878static void i40e_dcb_reconfigure(struct i40e_pf *pf)
5879{
5880 u8 tc_map = 0;
5881 int ret;
5882 u8 v;
5883
5884 /* Enable the TCs available on PF to all VEBs */
5885 tc_map = i40e_pf_get_tc_map(pf);
5886 for (v = 0; v < I40E_MAX_VEB; v++) {
5887 if (!pf->veb[v])
5888 continue;
5889 ret = i40e_veb_config_tc(pf->veb[v], tc_map);
5890 if (ret) {
5891 dev_info(&pf->pdev->dev,
5892 "Failed configuring TC for VEB seid=%d\n",
5893 pf->veb[v]->seid);
5894 /* Will try to configure as many components */
5895 }
5896 }
5897
5898 /* Update each VSI */
Mitch Williams505682c2014-05-20 08:01:37 +00005899 for (v = 0; v < pf->num_alloc_vsi; v++) {
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005900 if (!pf->vsi[v])
5901 continue;
5902
5903 /* - Enable all TCs for the LAN VSI
5904 * - For all others keep them at TC0 for now
5905 */
5906 if (v == pf->lan_vsi)
5907 tc_map = i40e_pf_get_tc_map(pf);
5908 else
David Ertmanea6acb72016-09-20 07:10:50 -07005909 tc_map = I40E_DEFAULT_TRAFFIC_CLASS;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005910
5911 ret = i40e_vsi_config_tc(pf->vsi[v], tc_map);
5912 if (ret) {
5913 dev_info(&pf->pdev->dev,
5914 "Failed configuring TC for VSI seid=%d\n",
5915 pf->vsi[v]->seid);
5916 /* Will try to configure as many components */
5917 } else {
Neerav Parikh0672a092014-04-01 07:11:47 +00005918 /* Re-configure VSI vectors based on updated TC map */
5919 i40e_vsi_map_rings_to_vectors(pf->vsi[v]);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005920 if (pf->vsi[v]->netdev)
5921 i40e_dcbnl_set_all(pf->vsi[v]);
5922 }
5923 }
5924}
5925
5926/**
Neerav Parikh2fd75f32014-11-12 00:18:20 +00005927 * i40e_resume_port_tx - Resume port Tx
5928 * @pf: PF struct
5929 *
5930 * Resume a port's Tx and issue a PF reset in case of failure to
5931 * resume.
5932 **/
5933static int i40e_resume_port_tx(struct i40e_pf *pf)
5934{
5935 struct i40e_hw *hw = &pf->hw;
5936 int ret;
5937
5938 ret = i40e_aq_resume_port_tx(hw, NULL);
5939 if (ret) {
5940 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005941 "Resume Port Tx failed, err %s aq_err %s\n",
5942 i40e_stat_str(&pf->hw, ret),
5943 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh2fd75f32014-11-12 00:18:20 +00005944 /* Schedule PF reset to recover */
Jacob Keller0da36b92017-04-19 09:25:55 -04005945 set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
Neerav Parikh2fd75f32014-11-12 00:18:20 +00005946 i40e_service_event_schedule(pf);
5947 }
5948
5949 return ret;
5950}
5951
5952/**
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005953 * i40e_init_pf_dcb - Initialize DCB configuration
5954 * @pf: PF being configured
5955 *
5956 * Query the current DCB configuration and cache it
5957 * in the hardware structure
5958 **/
5959static int i40e_init_pf_dcb(struct i40e_pf *pf)
5960{
5961 struct i40e_hw *hw = &pf->hw;
5962 int err = 0;
5963
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +00005964 /* Do not enable DCB for SW1 and SW2 images even if the FW is capable */
Jacob Kellerd36e41d2017-06-23 04:24:46 -04005965 if (pf->hw_features & I40E_HW_NO_DCB_SUPPORT)
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +00005966 goto out;
5967
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005968 /* Get the initial DCB configuration */
5969 err = i40e_init_dcb(hw);
5970 if (!err) {
5971 /* Device/Function is not DCBX capable */
5972 if ((!hw->func_caps.dcb) ||
5973 (hw->dcbx_status == I40E_DCBX_STATUS_DISABLED)) {
5974 dev_info(&pf->pdev->dev,
5975 "DCBX offload is not supported or is disabled for this PF.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005976 } else {
5977 /* When status is not DISABLED then DCBX in FW */
5978 pf->dcbx_cap = DCB_CAP_DCBX_LLD_MANAGED |
5979 DCB_CAP_DCBX_VER_IEEE;
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005980
5981 pf->flags |= I40E_FLAG_DCB_CAPABLE;
Dave Ertmana0362442016-08-29 17:38:26 -07005982 /* Enable DCB tagging only when more than one TC
5983 * or explicitly disable if only one TC
5984 */
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005985 if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
5986 pf->flags |= I40E_FLAG_DCB_ENABLED;
Dave Ertmana0362442016-08-29 17:38:26 -07005987 else
5988 pf->flags &= ~I40E_FLAG_DCB_ENABLED;
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005989 dev_dbg(&pf->pdev->dev,
5990 "DCBX offload is supported for this PF.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005991 }
Neerav Parikh014269f2014-04-01 07:11:48 +00005992 } else {
Shannon Nelsonaebfc812014-12-11 07:06:38 +00005993 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005994 "Query for DCB configuration failed, err %s aq_err %s\n",
5995 i40e_stat_str(&pf->hw, err),
5996 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005997 }
5998
5999out:
6000 return err;
6001}
6002#endif /* CONFIG_I40E_DCB */
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00006003#define SPEED_SIZE 14
6004#define FC_SIZE 8
6005/**
6006 * i40e_print_link_message - print link up or down
6007 * @vsi: the VSI for which link needs a message
6008 */
Matt Jaredc156f852015-08-27 11:42:39 -04006009void i40e_print_link_message(struct i40e_vsi *vsi, bool isup)
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00006010{
Filip Sadowski7ec9ba12016-11-08 13:05:13 -08006011 enum i40e_aq_link_speed new_speed;
Sudheer Mogilappagari3fded462017-07-14 09:10:18 -04006012 struct i40e_pf *pf = vsi->back;
Shannon Nelsona9165492015-09-03 17:19:00 -04006013 char *speed = "Unknown";
6014 char *fc = "Unknown";
Henry Tieman3e03d7c2016-12-02 12:32:57 -08006015 char *fec = "";
Mariusz Stachura68e49702017-07-12 05:46:14 -04006016 char *req_fec = "";
Henry Tieman3e03d7c2016-12-02 12:32:57 -08006017 char *an = "";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00006018
Sudheer Mogilappagari3fded462017-07-14 09:10:18 -04006019 new_speed = pf->hw.phy.link_info.link_speed;
Filip Sadowski7ec9ba12016-11-08 13:05:13 -08006020
6021 if ((vsi->current_isup == isup) && (vsi->current_speed == new_speed))
Matt Jaredc156f852015-08-27 11:42:39 -04006022 return;
6023 vsi->current_isup = isup;
Filip Sadowski7ec9ba12016-11-08 13:05:13 -08006024 vsi->current_speed = new_speed;
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00006025 if (!isup) {
6026 netdev_info(vsi->netdev, "NIC Link is Down\n");
6027 return;
6028 }
6029
Greg Rose148c2d82014-12-11 07:06:27 +00006030 /* Warn user if link speed on NPAR enabled partition is not at
6031 * least 10GB
6032 */
Sudheer Mogilappagari3fded462017-07-14 09:10:18 -04006033 if (pf->hw.func_caps.npar_enable &&
6034 (pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_1GB ||
6035 pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_100MB))
Greg Rose148c2d82014-12-11 07:06:27 +00006036 netdev_warn(vsi->netdev,
6037 "The partition detected link speed that is less than 10Gbps\n");
6038
Sudheer Mogilappagari3fded462017-07-14 09:10:18 -04006039 switch (pf->hw.phy.link_info.link_speed) {
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00006040 case I40E_LINK_SPEED_40GB:
Shannon Nelsona9165492015-09-03 17:19:00 -04006041 speed = "40 G";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00006042 break;
Jesse Brandeburgae24b402015-03-27 00:12:09 -07006043 case I40E_LINK_SPEED_20GB:
Shannon Nelsona9165492015-09-03 17:19:00 -04006044 speed = "20 G";
Jesse Brandeburgae24b402015-03-27 00:12:09 -07006045 break;
Carolyn Wyborny31232372016-11-21 13:03:48 -08006046 case I40E_LINK_SPEED_25GB:
6047 speed = "25 G";
6048 break;
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00006049 case I40E_LINK_SPEED_10GB:
Shannon Nelsona9165492015-09-03 17:19:00 -04006050 speed = "10 G";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00006051 break;
6052 case I40E_LINK_SPEED_1GB:
Shannon Nelsona9165492015-09-03 17:19:00 -04006053 speed = "1000 M";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00006054 break;
Mitch Williams5960d332014-09-13 07:40:47 +00006055 case I40E_LINK_SPEED_100MB:
Shannon Nelsona9165492015-09-03 17:19:00 -04006056 speed = "100 M";
Mitch Williams5960d332014-09-13 07:40:47 +00006057 break;
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00006058 default:
6059 break;
6060 }
6061
Sudheer Mogilappagari3fded462017-07-14 09:10:18 -04006062 switch (pf->hw.fc.current_mode) {
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00006063 case I40E_FC_FULL:
Shannon Nelsona9165492015-09-03 17:19:00 -04006064 fc = "RX/TX";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00006065 break;
6066 case I40E_FC_TX_PAUSE:
Shannon Nelsona9165492015-09-03 17:19:00 -04006067 fc = "TX";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00006068 break;
6069 case I40E_FC_RX_PAUSE:
Shannon Nelsona9165492015-09-03 17:19:00 -04006070 fc = "RX";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00006071 break;
6072 default:
Shannon Nelsona9165492015-09-03 17:19:00 -04006073 fc = "None";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00006074 break;
6075 }
6076
Sudheer Mogilappagari3fded462017-07-14 09:10:18 -04006077 if (pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_25GB) {
Mariusz Stachura68e49702017-07-12 05:46:14 -04006078 req_fec = ", Requested FEC: None";
Henry Tieman3e03d7c2016-12-02 12:32:57 -08006079 fec = ", FEC: None";
6080 an = ", Autoneg: False";
6081
Sudheer Mogilappagari3fded462017-07-14 09:10:18 -04006082 if (pf->hw.phy.link_info.an_info & I40E_AQ_AN_COMPLETED)
Henry Tieman3e03d7c2016-12-02 12:32:57 -08006083 an = ", Autoneg: True";
6084
Sudheer Mogilappagari3fded462017-07-14 09:10:18 -04006085 if (pf->hw.phy.link_info.fec_info &
Henry Tieman3e03d7c2016-12-02 12:32:57 -08006086 I40E_AQ_CONFIG_FEC_KR_ENA)
6087 fec = ", FEC: CL74 FC-FEC/BASE-R";
Sudheer Mogilappagari3fded462017-07-14 09:10:18 -04006088 else if (pf->hw.phy.link_info.fec_info &
Henry Tieman3e03d7c2016-12-02 12:32:57 -08006089 I40E_AQ_CONFIG_FEC_RS_ENA)
6090 fec = ", FEC: CL108 RS-FEC";
Mariusz Stachura68e49702017-07-12 05:46:14 -04006091
6092 /* 'CL108 RS-FEC' should be displayed when RS is requested, or
6093 * both RS and FC are requested
6094 */
6095 if (vsi->back->hw.phy.link_info.req_fec_info &
6096 (I40E_AQ_REQUEST_FEC_KR | I40E_AQ_REQUEST_FEC_RS)) {
6097 if (vsi->back->hw.phy.link_info.req_fec_info &
6098 I40E_AQ_REQUEST_FEC_RS)
6099 req_fec = ", Requested FEC: CL108 RS-FEC";
6100 else
6101 req_fec = ", Requested FEC: CL74 FC-FEC/BASE-R";
6102 }
Henry Tieman3e03d7c2016-12-02 12:32:57 -08006103 }
6104
Mariusz Stachura68e49702017-07-12 05:46:14 -04006105 netdev_info(vsi->netdev, "NIC Link is Up, %sbps Full Duplex%s%s%s, Flow Control: %s\n",
6106 speed, req_fec, fec, an, fc);
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00006107}
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006108
6109/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006110 * i40e_up_complete - Finish the last steps of bringing up a connection
6111 * @vsi: the VSI being configured
6112 **/
6113static int i40e_up_complete(struct i40e_vsi *vsi)
6114{
6115 struct i40e_pf *pf = vsi->back;
6116 int err;
6117
6118 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
6119 i40e_vsi_configure_msix(vsi);
6120 else
6121 i40e_configure_msi_and_legacy(vsi);
6122
6123 /* start rings */
Filip Sadowski3aa7b742016-10-11 15:26:58 -07006124 err = i40e_vsi_start_rings(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006125 if (err)
6126 return err;
6127
Jacob Keller0da36b92017-04-19 09:25:55 -04006128 clear_bit(__I40E_VSI_DOWN, vsi->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006129 i40e_napi_enable_all(vsi);
6130 i40e_vsi_enable_irq(vsi);
6131
6132 if ((pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP) &&
6133 (vsi->netdev)) {
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00006134 i40e_print_link_message(vsi, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006135 netif_tx_start_all_queues(vsi->netdev);
6136 netif_carrier_on(vsi->netdev);
6137 }
Anjali Singhai Jainca64fa42014-02-11 08:26:30 +00006138
6139 /* replay FDIR SB filters */
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006140 if (vsi->type == I40E_VSI_FDIR) {
6141 /* reset fd counters */
Jacob Keller097dbf52017-02-06 14:38:46 -08006142 pf->fd_add_err = 0;
6143 pf->fd_atr_cnt = 0;
Anjali Singhai Jainca64fa42014-02-11 08:26:30 +00006144 i40e_fdir_filter_restore(vsi);
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006145 }
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06006146
6147 /* On the next run of the service_task, notify any clients of the new
6148 * opened netdev
6149 */
6150 pf->flags |= I40E_FLAG_SERVICE_CLIENT_REQUESTED;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006151 i40e_service_event_schedule(pf);
6152
6153 return 0;
6154}
6155
6156/**
6157 * i40e_vsi_reinit_locked - Reset the VSI
6158 * @vsi: the VSI being configured
6159 *
6160 * Rebuild the ring structs after some configuration
6161 * has changed, e.g. MTU size.
6162 **/
6163static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi)
6164{
6165 struct i40e_pf *pf = vsi->back;
6166
6167 WARN_ON(in_interrupt());
Jacob Keller0da36b92017-04-19 09:25:55 -04006168 while (test_and_set_bit(__I40E_CONFIG_BUSY, pf->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006169 usleep_range(1000, 2000);
6170 i40e_down(vsi);
6171
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006172 i40e_up(vsi);
Jacob Keller0da36b92017-04-19 09:25:55 -04006173 clear_bit(__I40E_CONFIG_BUSY, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006174}
6175
6176/**
6177 * i40e_up - Bring the connection back up after being down
6178 * @vsi: the VSI being configured
6179 **/
6180int i40e_up(struct i40e_vsi *vsi)
6181{
6182 int err;
6183
6184 err = i40e_vsi_configure(vsi);
6185 if (!err)
6186 err = i40e_up_complete(vsi);
6187
6188 return err;
6189}
6190
6191/**
6192 * i40e_down - Shutdown the connection processing
6193 * @vsi: the VSI being stopped
6194 **/
6195void i40e_down(struct i40e_vsi *vsi)
6196{
6197 int i;
6198
6199 /* It is assumed that the caller of this function
Jacob Kellerd19cb642017-04-21 13:38:05 -07006200 * sets the vsi->state __I40E_VSI_DOWN bit.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006201 */
6202 if (vsi->netdev) {
6203 netif_carrier_off(vsi->netdev);
6204 netif_tx_disable(vsi->netdev);
6205 }
6206 i40e_vsi_disable_irq(vsi);
Filip Sadowski3aa7b742016-10-11 15:26:58 -07006207 i40e_vsi_stop_rings(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006208 i40e_napi_disable_all(vsi);
6209
6210 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck9f65e152013-09-28 06:00:58 +00006211 i40e_clean_tx_ring(vsi->tx_rings[i]);
Björn Töpel74608d12017-05-24 07:55:35 +02006212 if (i40e_enabled_xdp_vsi(vsi))
6213 i40e_clean_tx_ring(vsi->xdp_rings[i]);
Alexander Duyck9f65e152013-09-28 06:00:58 +00006214 i40e_clean_rx_ring(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006215 }
Catherine Sullivanf980d442016-05-16 10:26:34 -07006216
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006217}
6218
6219/**
6220 * i40e_setup_tc - configure multiple traffic classes
6221 * @netdev: net device to configure
6222 * @tc: number of traffic classes to enable
6223 **/
6224static int i40e_setup_tc(struct net_device *netdev, u8 tc)
6225{
6226 struct i40e_netdev_priv *np = netdev_priv(netdev);
6227 struct i40e_vsi *vsi = np->vsi;
6228 struct i40e_pf *pf = vsi->back;
6229 u8 enabled_tc = 0;
6230 int ret = -EINVAL;
6231 int i;
6232
6233 /* Check if DCB enabled to continue */
6234 if (!(pf->flags & I40E_FLAG_DCB_ENABLED)) {
6235 netdev_info(netdev, "DCB is not enabled for adapter\n");
6236 goto exit;
6237 }
6238
6239 /* Check if MFP enabled */
6240 if (pf->flags & I40E_FLAG_MFP_ENABLED) {
6241 netdev_info(netdev, "Configuring TC not supported in MFP mode\n");
6242 goto exit;
6243 }
6244
6245 /* Check whether tc count is within enabled limit */
6246 if (tc > i40e_pf_get_num_tc(pf)) {
6247 netdev_info(netdev, "TC count greater than enabled on link for adapter\n");
6248 goto exit;
6249 }
6250
6251 /* Generate TC map for number of tc requested */
6252 for (i = 0; i < tc; i++)
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006253 enabled_tc |= BIT(i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006254
6255 /* Requesting same TC configuration as already enabled */
6256 if (enabled_tc == vsi->tc_config.enabled_tc)
6257 return 0;
6258
6259 /* Quiesce VSI queues */
6260 i40e_quiesce_vsi(vsi);
6261
6262 /* Configure VSI for enabled TCs */
6263 ret = i40e_vsi_config_tc(vsi, enabled_tc);
6264 if (ret) {
6265 netdev_info(netdev, "Failed configuring TC for VSI seid=%d\n",
6266 vsi->seid);
6267 goto exit;
6268 }
6269
Amritha Nambiar8f88b302017-09-07 04:00:17 -07006270 if (pf->flags & I40E_FLAG_TC_MQPRIO) {
6271 ret = i40e_configure_queue_channels(vsi);
6272 if (ret) {
6273 netdev_info(netdev,
6274 "Failed configuring queue channels\n");
6275 goto exit;
6276 }
6277 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006278
6279exit:
Amritha Nambiar8f88b302017-09-07 04:00:17 -07006280 /* Unquiesce VSI */
6281 i40e_unquiesce_vsi(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006282 return ret;
6283}
6284
Jiri Pirko2572ac52017-08-07 10:15:17 +02006285static int __i40e_setup_tc(struct net_device *netdev, enum tc_setup_type type,
Jiri Pirkode4784c2017-08-07 10:15:32 +02006286 void *type_data)
John Fastabende4c67342016-02-16 21:16:15 -08006287{
Jiri Pirkode4784c2017-08-07 10:15:32 +02006288 struct tc_mqprio_qopt *mqprio = type_data;
6289
Jiri Pirko2572ac52017-08-07 10:15:17 +02006290 if (type != TC_SETUP_MQPRIO)
Jiri Pirko38cf0422017-08-07 10:15:31 +02006291 return -EOPNOTSUPP;
Amritha Nambiar56f36ac2017-03-15 10:39:25 -07006292
Jiri Pirkode4784c2017-08-07 10:15:32 +02006293 mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
Amritha Nambiar56f36ac2017-03-15 10:39:25 -07006294
Jiri Pirkode4784c2017-08-07 10:15:32 +02006295 return i40e_setup_tc(netdev, mqprio->num_tc);
John Fastabende4c67342016-02-16 21:16:15 -08006296}
6297
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006298/**
6299 * i40e_open - Called when a network interface is made active
6300 * @netdev: network interface device structure
6301 *
6302 * The open entry point is called when a network interface is made
6303 * active by the system (IFF_UP). At this point all resources needed
6304 * for transmit and receive operations are allocated, the interrupt
6305 * handler is registered with the OS, the netdev watchdog subtask is
6306 * enabled, and the stack is notified that the interface is ready.
6307 *
6308 * Returns 0 on success, negative value on failure
6309 **/
Vasu Dev38e00432014-08-01 13:27:03 -07006310int i40e_open(struct net_device *netdev)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006311{
6312 struct i40e_netdev_priv *np = netdev_priv(netdev);
6313 struct i40e_vsi *vsi = np->vsi;
6314 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006315 int err;
6316
Shannon Nelson4eb3f762014-03-06 08:59:58 +00006317 /* disallow open during test or if eeprom is broken */
Jacob Keller0da36b92017-04-19 09:25:55 -04006318 if (test_bit(__I40E_TESTING, pf->state) ||
6319 test_bit(__I40E_BAD_EEPROM, pf->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006320 return -EBUSY;
6321
6322 netif_carrier_off(netdev);
6323
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00006324 err = i40e_vsi_open(vsi);
6325 if (err)
6326 return err;
6327
Jesse Brandeburg059dab62014-04-01 09:07:20 +00006328 /* configure global TSO hardware offload settings */
6329 wr32(&pf->hw, I40E_GLLAN_TSOMSK_F, be32_to_cpu(TCP_FLAG_PSH |
6330 TCP_FLAG_FIN) >> 16);
6331 wr32(&pf->hw, I40E_GLLAN_TSOMSK_M, be32_to_cpu(TCP_FLAG_PSH |
6332 TCP_FLAG_FIN |
6333 TCP_FLAG_CWR) >> 16);
6334 wr32(&pf->hw, I40E_GLLAN_TSOMSK_L, be32_to_cpu(TCP_FLAG_CWR) >> 16);
6335
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07006336 udp_tunnel_get_rx_info(netdev);
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06006337
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00006338 return 0;
6339}
6340
6341/**
6342 * i40e_vsi_open -
6343 * @vsi: the VSI to open
6344 *
6345 * Finish initialization of the VSI.
6346 *
6347 * Returns 0 on success, negative value on failure
Maciej Sosin373149f2017-04-05 07:50:55 -04006348 *
6349 * Note: expects to be called while under rtnl_lock()
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00006350 **/
6351int i40e_vsi_open(struct i40e_vsi *vsi)
6352{
6353 struct i40e_pf *pf = vsi->back;
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00006354 char int_name[I40E_INT_NAME_STR_LEN];
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00006355 int err;
6356
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006357 /* allocate descriptors */
6358 err = i40e_vsi_setup_tx_resources(vsi);
6359 if (err)
6360 goto err_setup_tx;
6361 err = i40e_vsi_setup_rx_resources(vsi);
6362 if (err)
6363 goto err_setup_rx;
6364
6365 err = i40e_vsi_configure(vsi);
6366 if (err)
6367 goto err_setup_rx;
6368
Shannon Nelsonc22e3c62014-03-14 07:32:25 +00006369 if (vsi->netdev) {
6370 snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
6371 dev_driver_string(&pf->pdev->dev), vsi->netdev->name);
6372 err = i40e_vsi_request_irq(vsi, int_name);
6373 if (err)
6374 goto err_setup_rx;
6375
6376 /* Notify the stack of the actual queue counts. */
6377 err = netif_set_real_num_tx_queues(vsi->netdev,
6378 vsi->num_queue_pairs);
6379 if (err)
6380 goto err_set_queues;
6381
6382 err = netif_set_real_num_rx_queues(vsi->netdev,
6383 vsi->num_queue_pairs);
6384 if (err)
6385 goto err_set_queues;
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00006386
6387 } else if (vsi->type == I40E_VSI_FDIR) {
Carolyn Wybornye240f672014-12-11 07:06:37 +00006388 snprintf(int_name, sizeof(int_name) - 1, "%s-%s:fdir",
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +00006389 dev_driver_string(&pf->pdev->dev),
6390 dev_name(&pf->pdev->dev));
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00006391 err = i40e_vsi_request_irq(vsi, int_name);
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +00006392
Shannon Nelsonc22e3c62014-03-14 07:32:25 +00006393 } else {
Jean Sacrence9ccb12014-05-01 14:31:18 +00006394 err = -EINVAL;
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00006395 goto err_setup_rx;
6396 }
Anjali Singhai Jain25946dd2013-11-26 10:49:14 +00006397
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006398 err = i40e_up_complete(vsi);
6399 if (err)
6400 goto err_up_complete;
6401
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006402 return 0;
6403
6404err_up_complete:
6405 i40e_down(vsi);
Anjali Singhai Jain25946dd2013-11-26 10:49:14 +00006406err_set_queues:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006407 i40e_vsi_free_irq(vsi);
6408err_setup_rx:
6409 i40e_vsi_free_rx_resources(vsi);
6410err_setup_tx:
6411 i40e_vsi_free_tx_resources(vsi);
6412 if (vsi == pf->vsi[pf->lan_vsi])
Amritha Nambiarff424182017-09-07 04:00:11 -07006413 i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006414
6415 return err;
6416}
6417
6418/**
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00006419 * i40e_fdir_filter_exit - Cleans up the Flow Director accounting
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00006420 * @pf: Pointer to PF
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00006421 *
6422 * This function destroys the hlist where all the Flow Director
6423 * filters were saved.
6424 **/
6425static void i40e_fdir_filter_exit(struct i40e_pf *pf)
6426{
6427 struct i40e_fdir_filter *filter;
Jacob Keller0e588de2017-02-06 14:38:50 -08006428 struct i40e_flex_pit *pit_entry, *tmp;
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00006429 struct hlist_node *node2;
6430
6431 hlist_for_each_entry_safe(filter, node2,
6432 &pf->fdir_filter_list, fdir_node) {
6433 hlist_del(&filter->fdir_node);
6434 kfree(filter);
6435 }
Jacob Keller097dbf52017-02-06 14:38:46 -08006436
Jacob Keller0e588de2017-02-06 14:38:50 -08006437 list_for_each_entry_safe(pit_entry, tmp, &pf->l3_flex_pit_list, list) {
6438 list_del(&pit_entry->list);
6439 kfree(pit_entry);
6440 }
6441 INIT_LIST_HEAD(&pf->l3_flex_pit_list);
6442
6443 list_for_each_entry_safe(pit_entry, tmp, &pf->l4_flex_pit_list, list) {
6444 list_del(&pit_entry->list);
6445 kfree(pit_entry);
6446 }
6447 INIT_LIST_HEAD(&pf->l4_flex_pit_list);
6448
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00006449 pf->fdir_pf_active_filters = 0;
Jacob Keller097dbf52017-02-06 14:38:46 -08006450 pf->fd_tcp4_filter_cnt = 0;
6451 pf->fd_udp4_filter_cnt = 0;
Jacob Kellerf223c872017-02-06 14:38:51 -08006452 pf->fd_sctp4_filter_cnt = 0;
Jacob Keller097dbf52017-02-06 14:38:46 -08006453 pf->fd_ip4_filter_cnt = 0;
Jacob Keller3bcee1e2017-02-06 14:38:46 -08006454
6455 /* Reprogram the default input set for TCP/IPv4 */
6456 i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_TCP,
6457 I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
6458 I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
6459
6460 /* Reprogram the default input set for UDP/IPv4 */
6461 i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_UDP,
6462 I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
6463 I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
6464
6465 /* Reprogram the default input set for SCTP/IPv4 */
6466 i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_SCTP,
6467 I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
6468 I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
6469
6470 /* Reprogram the default input set for Other/IPv4 */
6471 i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_OTHER,
6472 I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00006473}
6474
6475/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006476 * i40e_close - Disables a network interface
6477 * @netdev: network interface device structure
6478 *
6479 * The close entry point is called when an interface is de-activated
6480 * by the OS. The hardware is still under the driver's control, but
6481 * this netdev interface is disabled.
6482 *
6483 * Returns 0, this is not allowed to fail
6484 **/
Vasu Dev38e00432014-08-01 13:27:03 -07006485int i40e_close(struct net_device *netdev)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006486{
6487 struct i40e_netdev_priv *np = netdev_priv(netdev);
6488 struct i40e_vsi *vsi = np->vsi;
6489
Shannon Nelson90ef8d42014-03-14 07:32:26 +00006490 i40e_vsi_close(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006491
6492 return 0;
6493}
6494
6495/**
6496 * i40e_do_reset - Start a PF or Core Reset sequence
6497 * @pf: board private structure
6498 * @reset_flags: which reset is requested
Maciej Sosin373149f2017-04-05 07:50:55 -04006499 * @lock_acquired: indicates whether or not the lock has been acquired
6500 * before this function was called.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006501 *
6502 * The essential difference in resets is that the PF Reset
6503 * doesn't clear the packet buffers, doesn't reset the PE
6504 * firmware, and doesn't bother the other PFs on the chip.
6505 **/
Maciej Sosin373149f2017-04-05 07:50:55 -04006506void i40e_do_reset(struct i40e_pf *pf, u32 reset_flags, bool lock_acquired)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006507{
6508 u32 val;
6509
6510 WARN_ON(in_interrupt());
6511
Mitch Williams263fc482014-04-23 04:50:11 +00006512
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006513 /* do the biggest reset indicated */
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04006514 if (reset_flags & BIT_ULL(__I40E_GLOBAL_RESET_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006515
6516 /* Request a Global Reset
6517 *
6518 * This will start the chip's countdown to the actual full
6519 * chip reset event, and a warning interrupt to be sent
6520 * to all PFs, including the requestor. Our handler
6521 * for the warning interrupt will deal with the shutdown
6522 * and recovery of the switch setup.
6523 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006524 dev_dbg(&pf->pdev->dev, "GlobalR requested\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006525 val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
6526 val |= I40E_GLGEN_RTRIG_GLOBR_MASK;
6527 wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
6528
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04006529 } else if (reset_flags & BIT_ULL(__I40E_CORE_RESET_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006530
6531 /* Request a Core Reset
6532 *
6533 * Same as Global Reset, except does *not* include the MAC/PHY
6534 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006535 dev_dbg(&pf->pdev->dev, "CoreR requested\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006536 val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
6537 val |= I40E_GLGEN_RTRIG_CORER_MASK;
6538 wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
6539 i40e_flush(&pf->hw);
6540
Amritha Nambiarff424182017-09-07 04:00:11 -07006541 } else if (reset_flags & I40E_PF_RESET_FLAG) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006542
6543 /* Request a PF Reset
6544 *
6545 * Resets only the PF-specific registers
6546 *
6547 * This goes directly to the tear-down and rebuild of
6548 * the switch, since we need to do all the recovery as
6549 * for the Core Reset.
6550 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006551 dev_dbg(&pf->pdev->dev, "PFR requested\n");
Maciej Sosin373149f2017-04-05 07:50:55 -04006552 i40e_handle_reset_warning(pf, lock_acquired);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006553
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04006554 } else if (reset_flags & BIT_ULL(__I40E_REINIT_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006555 int v;
6556
6557 /* Find the VSI(s) that requested a re-init */
6558 dev_info(&pf->pdev->dev,
6559 "VSI reinit requested\n");
Mitch Williams505682c2014-05-20 08:01:37 +00006560 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006561 struct i40e_vsi *vsi = pf->vsi[v];
Jesse Brandeburg6995b362015-08-28 17:55:54 -04006562
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006563 if (vsi != NULL &&
Jacob Kellerd19cb642017-04-21 13:38:05 -07006564 test_and_clear_bit(__I40E_VSI_REINIT_REQUESTED,
Jacob Keller0da36b92017-04-19 09:25:55 -04006565 vsi->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006566 i40e_vsi_reinit_locked(pf->vsi[v]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006567 }
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04006568 } else if (reset_flags & BIT_ULL(__I40E_DOWN_REQUESTED)) {
Neerav Parikhb5d06f02014-06-03 23:50:17 +00006569 int v;
6570
6571 /* Find the VSI(s) that needs to be brought down */
6572 dev_info(&pf->pdev->dev, "VSI down requested\n");
6573 for (v = 0; v < pf->num_alloc_vsi; v++) {
6574 struct i40e_vsi *vsi = pf->vsi[v];
Jesse Brandeburg6995b362015-08-28 17:55:54 -04006575
Neerav Parikhb5d06f02014-06-03 23:50:17 +00006576 if (vsi != NULL &&
Jacob Kellerd19cb642017-04-21 13:38:05 -07006577 test_and_clear_bit(__I40E_VSI_DOWN_REQUESTED,
Jacob Keller0da36b92017-04-19 09:25:55 -04006578 vsi->state)) {
6579 set_bit(__I40E_VSI_DOWN, vsi->state);
Neerav Parikhb5d06f02014-06-03 23:50:17 +00006580 i40e_down(vsi);
Neerav Parikhb5d06f02014-06-03 23:50:17 +00006581 }
6582 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006583 } else {
6584 dev_info(&pf->pdev->dev,
6585 "bad reset request 0x%08x\n", reset_flags);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006586 }
6587}
6588
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006589#ifdef CONFIG_I40E_DCB
6590/**
6591 * i40e_dcb_need_reconfig - Check if DCB needs reconfig
6592 * @pf: board private structure
6593 * @old_cfg: current DCB config
6594 * @new_cfg: new DCB config
6595 **/
6596bool i40e_dcb_need_reconfig(struct i40e_pf *pf,
6597 struct i40e_dcbx_config *old_cfg,
6598 struct i40e_dcbx_config *new_cfg)
6599{
6600 bool need_reconfig = false;
6601
6602 /* Check if ETS configuration has changed */
6603 if (memcmp(&new_cfg->etscfg,
6604 &old_cfg->etscfg,
6605 sizeof(new_cfg->etscfg))) {
6606 /* If Priority Table has changed reconfig is needed */
6607 if (memcmp(&new_cfg->etscfg.prioritytable,
6608 &old_cfg->etscfg.prioritytable,
6609 sizeof(new_cfg->etscfg.prioritytable))) {
6610 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006611 dev_dbg(&pf->pdev->dev, "ETS UP2TC changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006612 }
6613
6614 if (memcmp(&new_cfg->etscfg.tcbwtable,
6615 &old_cfg->etscfg.tcbwtable,
6616 sizeof(new_cfg->etscfg.tcbwtable)))
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006617 dev_dbg(&pf->pdev->dev, "ETS TC BW Table changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006618
6619 if (memcmp(&new_cfg->etscfg.tsatable,
6620 &old_cfg->etscfg.tsatable,
6621 sizeof(new_cfg->etscfg.tsatable)))
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006622 dev_dbg(&pf->pdev->dev, "ETS TSA Table changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006623 }
6624
6625 /* Check if PFC configuration has changed */
6626 if (memcmp(&new_cfg->pfc,
6627 &old_cfg->pfc,
6628 sizeof(new_cfg->pfc))) {
6629 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006630 dev_dbg(&pf->pdev->dev, "PFC config change detected.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006631 }
6632
6633 /* Check if APP Table has changed */
6634 if (memcmp(&new_cfg->app,
6635 &old_cfg->app,
Dave Jones3d9667a2014-01-27 23:11:09 -05006636 sizeof(new_cfg->app))) {
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006637 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006638 dev_dbg(&pf->pdev->dev, "APP Table change detected.\n");
Dave Jones3d9667a2014-01-27 23:11:09 -05006639 }
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006640
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04006641 dev_dbg(&pf->pdev->dev, "dcb need_reconfig=%d\n", need_reconfig);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006642 return need_reconfig;
6643}
6644
6645/**
6646 * i40e_handle_lldp_event - Handle LLDP Change MIB event
6647 * @pf: board private structure
6648 * @e: event info posted on ARQ
6649 **/
6650static int i40e_handle_lldp_event(struct i40e_pf *pf,
6651 struct i40e_arq_event_info *e)
6652{
6653 struct i40e_aqc_lldp_get_mib *mib =
6654 (struct i40e_aqc_lldp_get_mib *)&e->desc.params.raw;
6655 struct i40e_hw *hw = &pf->hw;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006656 struct i40e_dcbx_config tmp_dcbx_cfg;
6657 bool need_reconfig = false;
6658 int ret = 0;
6659 u8 type;
6660
Neerav Parikh4d9b6042014-05-22 06:31:51 +00006661 /* Not DCB capable or capability disabled */
David Ertmanea6acb72016-09-20 07:10:50 -07006662 if (!(pf->flags & I40E_FLAG_DCB_CAPABLE))
Neerav Parikh4d9b6042014-05-22 06:31:51 +00006663 return ret;
6664
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006665 /* Ignore if event is not for Nearest Bridge */
6666 type = ((mib->type >> I40E_AQ_LLDP_BRIDGE_TYPE_SHIFT)
6667 & I40E_AQ_LLDP_BRIDGE_TYPE_MASK);
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04006668 dev_dbg(&pf->pdev->dev, "LLDP event mib bridge type 0x%x\n", type);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006669 if (type != I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE)
6670 return ret;
6671
6672 /* Check MIB Type and return if event for Remote MIB update */
6673 type = mib->type & I40E_AQ_LLDP_MIB_TYPE_MASK;
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00006674 dev_dbg(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04006675 "LLDP event mib type %s\n", type ? "remote" : "local");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006676 if (type == I40E_AQ_LLDP_MIB_REMOTE) {
6677 /* Update the remote cached instance and return */
6678 ret = i40e_aq_get_dcb_config(hw, I40E_AQ_LLDP_MIB_REMOTE,
6679 I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE,
6680 &hw->remote_dcbx_config);
6681 goto exit;
6682 }
6683
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00006684 /* Store the old configuration */
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07006685 tmp_dcbx_cfg = hw->local_dcbx_config;
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00006686
Neerav Parikh750fcbc2015-02-24 06:58:47 +00006687 /* Reset the old DCBx configuration data */
6688 memset(&hw->local_dcbx_config, 0, sizeof(hw->local_dcbx_config));
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00006689 /* Get updated DCBX data from firmware */
6690 ret = i40e_get_dcb_config(&pf->hw);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006691 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006692 dev_info(&pf->pdev->dev,
6693 "Failed querying DCB configuration data from firmware, err %s aq_err %s\n",
6694 i40e_stat_str(&pf->hw, ret),
6695 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006696 goto exit;
6697 }
6698
6699 /* No change detected in DCBX configs */
Neerav Parikh750fcbc2015-02-24 06:58:47 +00006700 if (!memcmp(&tmp_dcbx_cfg, &hw->local_dcbx_config,
6701 sizeof(tmp_dcbx_cfg))) {
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006702 dev_dbg(&pf->pdev->dev, "No change detected in DCBX configuration.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006703 goto exit;
6704 }
6705
Neerav Parikh750fcbc2015-02-24 06:58:47 +00006706 need_reconfig = i40e_dcb_need_reconfig(pf, &tmp_dcbx_cfg,
6707 &hw->local_dcbx_config);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006708
Neerav Parikh750fcbc2015-02-24 06:58:47 +00006709 i40e_dcbnl_flush_apps(pf, &tmp_dcbx_cfg, &hw->local_dcbx_config);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006710
6711 if (!need_reconfig)
6712 goto exit;
6713
Neerav Parikh4d9b6042014-05-22 06:31:51 +00006714 /* Enable DCB tagging only when more than one TC */
Neerav Parikh750fcbc2015-02-24 06:58:47 +00006715 if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
Neerav Parikh4d9b6042014-05-22 06:31:51 +00006716 pf->flags |= I40E_FLAG_DCB_ENABLED;
6717 else
6718 pf->flags &= ~I40E_FLAG_DCB_ENABLED;
6719
Jacob Keller0da36b92017-04-19 09:25:55 -04006720 set_bit(__I40E_PORT_SUSPENDED, pf->state);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006721 /* Reconfiguration needed quiesce all VSIs */
6722 i40e_pf_quiesce_all_vsi(pf);
6723
6724 /* Changes in configuration update VEB/VSI */
6725 i40e_dcb_reconfigure(pf);
6726
Neerav Parikh2fd75f32014-11-12 00:18:20 +00006727 ret = i40e_resume_port_tx(pf);
6728
Jacob Keller0da36b92017-04-19 09:25:55 -04006729 clear_bit(__I40E_PORT_SUSPENDED, pf->state);
Neerav Parikh2fd75f32014-11-12 00:18:20 +00006730 /* In case of error no point in resuming VSIs */
Neerav Parikh69129dc2014-11-12 00:18:46 +00006731 if (ret)
6732 goto exit;
6733
Neerav Parikh3fe06f42016-02-17 16:12:15 -08006734 /* Wait for the PF's queues to be disabled */
6735 ret = i40e_pf_wait_queues_disabled(pf);
Parikh, Neerav11e47702015-02-21 06:43:55 +00006736 if (ret) {
6737 /* Schedule PF reset to recover */
Jacob Keller0da36b92017-04-19 09:25:55 -04006738 set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
Parikh, Neerav11e47702015-02-21 06:43:55 +00006739 i40e_service_event_schedule(pf);
6740 } else {
Neerav Parikh2fd75f32014-11-12 00:18:20 +00006741 i40e_pf_unquiesce_all_vsi(pf);
Mitch Williams0ef2d5a2017-01-24 10:24:00 -08006742 pf->flags |= (I40E_FLAG_SERVICE_CLIENT_REQUESTED |
6743 I40E_FLAG_CLIENT_L2_CHANGE);
Parikh, Neerav11e47702015-02-21 06:43:55 +00006744 }
6745
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006746exit:
6747 return ret;
6748}
6749#endif /* CONFIG_I40E_DCB */
6750
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006751/**
Anjali Singhai Jain233261862013-11-26 10:49:22 +00006752 * i40e_do_reset_safe - Protected reset path for userland calls.
6753 * @pf: board private structure
6754 * @reset_flags: which reset is requested
6755 *
6756 **/
6757void i40e_do_reset_safe(struct i40e_pf *pf, u32 reset_flags)
6758{
6759 rtnl_lock();
Maciej Sosin373149f2017-04-05 07:50:55 -04006760 i40e_do_reset(pf, reset_flags, true);
Anjali Singhai Jain233261862013-11-26 10:49:22 +00006761 rtnl_unlock();
6762}
6763
6764/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006765 * i40e_handle_lan_overflow_event - Handler for LAN queue overflow event
6766 * @pf: board private structure
6767 * @e: event info posted on ARQ
6768 *
6769 * Handler for LAN Queue Overflow Event generated by the firmware for PF
6770 * and VF queues
6771 **/
6772static void i40e_handle_lan_overflow_event(struct i40e_pf *pf,
6773 struct i40e_arq_event_info *e)
6774{
6775 struct i40e_aqc_lan_overflow *data =
6776 (struct i40e_aqc_lan_overflow *)&e->desc.params.raw;
6777 u32 queue = le32_to_cpu(data->prtdcb_rupto);
6778 u32 qtx_ctl = le32_to_cpu(data->otx_ctl);
6779 struct i40e_hw *hw = &pf->hw;
6780 struct i40e_vf *vf;
6781 u16 vf_id;
6782
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006783 dev_dbg(&pf->pdev->dev, "overflow Rx Queue Number = %d QTX_CTL=0x%08x\n",
6784 queue, qtx_ctl);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006785
6786 /* Queue belongs to VF, find the VF and issue VF reset */
6787 if (((qtx_ctl & I40E_QTX_CTL_PFVF_Q_MASK)
6788 >> I40E_QTX_CTL_PFVF_Q_SHIFT) == I40E_QTX_CTL_VF_QUEUE) {
6789 vf_id = (u16)((qtx_ctl & I40E_QTX_CTL_VFVM_INDX_MASK)
6790 >> I40E_QTX_CTL_VFVM_INDX_SHIFT);
6791 vf_id -= hw->func_caps.vf_base_id;
6792 vf = &pf->vf[vf_id];
6793 i40e_vc_notify_vf_reset(vf);
6794 /* Allow VF to process pending reset notification */
6795 msleep(20);
6796 i40e_reset_vf(vf, false);
6797 }
6798}
6799
6800/**
Anjali Singhai Jain12957382014-06-04 04:22:47 +00006801 * i40e_get_cur_guaranteed_fd_count - Get the consumed guaranteed FD filters
6802 * @pf: board private structure
6803 **/
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006804u32 i40e_get_cur_guaranteed_fd_count(struct i40e_pf *pf)
Anjali Singhai Jain12957382014-06-04 04:22:47 +00006805{
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006806 u32 val, fcnt_prog;
Anjali Singhai Jain12957382014-06-04 04:22:47 +00006807
6808 val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
6809 fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK);
6810 return fcnt_prog;
6811}
6812
6813/**
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006814 * i40e_get_current_fd_count - Get total FD filters programmed for this PF
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006815 * @pf: board private structure
6816 **/
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006817u32 i40e_get_current_fd_count(struct i40e_pf *pf)
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006818{
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006819 u32 val, fcnt_prog;
6820
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006821 val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
6822 fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK) +
6823 ((val & I40E_PFQF_FDSTAT_BEST_CNT_MASK) >>
6824 I40E_PFQF_FDSTAT_BEST_CNT_SHIFT);
6825 return fcnt_prog;
6826}
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006827
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006828/**
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006829 * i40e_get_global_fd_count - Get total FD filters programmed on device
6830 * @pf: board private structure
6831 **/
6832u32 i40e_get_global_fd_count(struct i40e_pf *pf)
6833{
6834 u32 val, fcnt_prog;
6835
6836 val = rd32(&pf->hw, I40E_GLQF_FDCNT_0);
6837 fcnt_prog = (val & I40E_GLQF_FDCNT_0_GUARANT_CNT_MASK) +
6838 ((val & I40E_GLQF_FDCNT_0_BESTCNT_MASK) >>
6839 I40E_GLQF_FDCNT_0_BESTCNT_SHIFT);
6840 return fcnt_prog;
6841}
6842
6843/**
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006844 * i40e_fdir_check_and_reenable - Function to reenabe FD ATR or SB if disabled
6845 * @pf: board private structure
6846 **/
6847void i40e_fdir_check_and_reenable(struct i40e_pf *pf)
6848{
Carolyn Wyborny3487b6c2015-08-27 11:42:38 -04006849 struct i40e_fdir_filter *filter;
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006850 u32 fcnt_prog, fcnt_avail;
Carolyn Wyborny3487b6c2015-08-27 11:42:38 -04006851 struct hlist_node *node;
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006852
Jacob Keller0da36b92017-04-19 09:25:55 -04006853 if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006854 return;
6855
Jacob Keller47994c12017-04-19 09:25:57 -04006856 /* Check if we have enough room to re-enable FDir SB capability. */
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006857 fcnt_prog = i40e_get_global_fd_count(pf);
Anjali Singhai Jain12957382014-06-04 04:22:47 +00006858 fcnt_avail = pf->fdir_pf_filter_count;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006859 if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM)) ||
6860 (pf->fd_add_err == 0) ||
6861 (i40e_get_current_atr_cnt(pf) < pf->fd_atr_cnt)) {
Jacob Keller47994c12017-04-19 09:25:57 -04006862 if (pf->flags & I40E_FLAG_FD_SB_AUTO_DISABLED) {
6863 pf->flags &= ~I40E_FLAG_FD_SB_AUTO_DISABLED;
6864 if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
6865 (I40E_DEBUG_FD & pf->hw.debug_mask))
Anjali Singhai Jain2e4875e2015-04-16 20:06:06 -04006866 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 +00006867 }
6868 }
Jacob Kellera3417d22016-09-06 18:05:10 -07006869
Jacob Keller47994c12017-04-19 09:25:57 -04006870 /* We should wait for even more space before re-enabling ATR.
6871 * Additionally, we cannot enable ATR as long as we still have TCP SB
6872 * rules active.
Jacob Kellera3417d22016-09-06 18:05:10 -07006873 */
Jacob Keller47994c12017-04-19 09:25:57 -04006874 if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) &&
6875 (pf->fd_tcp4_filter_cnt == 0)) {
6876 if (pf->flags & I40E_FLAG_FD_ATR_AUTO_DISABLED) {
6877 pf->flags &= ~I40E_FLAG_FD_ATR_AUTO_DISABLED;
6878 if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
6879 (I40E_DEBUG_FD & pf->hw.debug_mask))
Jacob Kellera3417d22016-09-06 18:05:10 -07006880 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 +00006881 }
6882 }
Carolyn Wyborny3487b6c2015-08-27 11:42:38 -04006883
6884 /* if hw had a problem adding a filter, delete it */
6885 if (pf->fd_inv > 0) {
6886 hlist_for_each_entry_safe(filter, node,
6887 &pf->fdir_filter_list, fdir_node) {
6888 if (filter->fd_id == pf->fd_inv) {
6889 hlist_del(&filter->fdir_node);
6890 kfree(filter);
6891 pf->fdir_pf_active_filters--;
Filip Sadowski013df592017-08-29 05:32:38 -04006892 pf->fd_inv = 0;
Carolyn Wyborny3487b6c2015-08-27 11:42:38 -04006893 }
6894 }
6895 }
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006896}
6897
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006898#define I40E_MIN_FD_FLUSH_INTERVAL 10
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006899#define I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE 30
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006900/**
6901 * i40e_fdir_flush_and_replay - Function to flush all FD filters and replay SB
6902 * @pf: board private structure
6903 **/
6904static void i40e_fdir_flush_and_replay(struct i40e_pf *pf)
6905{
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006906 unsigned long min_flush_time;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006907 int flush_wait_retry = 50;
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006908 bool disable_atr = false;
6909 int fd_room;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006910 int reg;
6911
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04006912 if (!time_after(jiffies, pf->fd_flush_timestamp +
6913 (I40E_MIN_FD_FLUSH_INTERVAL * HZ)))
6914 return;
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006915
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04006916 /* If the flush is happening too quick and we have mostly SB rules we
6917 * should not re-enable ATR for some time.
6918 */
6919 min_flush_time = pf->fd_flush_timestamp +
6920 (I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE * HZ);
6921 fd_room = pf->fdir_pf_filter_count - pf->fdir_pf_active_filters;
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006922
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04006923 if (!(time_after(jiffies, min_flush_time)) &&
6924 (fd_room < I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) {
6925 if (I40E_DEBUG_FD & pf->hw.debug_mask)
6926 dev_info(&pf->pdev->dev, "ATR disabled, not enough FD filter space.\n");
6927 disable_atr = true;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006928 }
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04006929
6930 pf->fd_flush_timestamp = jiffies;
Jacob Keller47994c12017-04-19 09:25:57 -04006931 pf->flags |= I40E_FLAG_FD_ATR_AUTO_DISABLED;
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04006932 /* flush all filters */
6933 wr32(&pf->hw, I40E_PFQF_CTL_1,
6934 I40E_PFQF_CTL_1_CLEARFDTABLE_MASK);
6935 i40e_flush(&pf->hw);
6936 pf->fd_flush_cnt++;
6937 pf->fd_add_err = 0;
6938 do {
6939 /* Check FD flush status every 5-6msec */
6940 usleep_range(5000, 6000);
6941 reg = rd32(&pf->hw, I40E_PFQF_CTL_1);
6942 if (!(reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK))
6943 break;
6944 } while (flush_wait_retry--);
6945 if (reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK) {
6946 dev_warn(&pf->pdev->dev, "FD table did not flush, needs more time\n");
6947 } else {
6948 /* replay sideband filters */
6949 i40e_fdir_filter_restore(pf->vsi[pf->lan_vsi]);
Jacob Keller097dbf52017-02-06 14:38:46 -08006950 if (!disable_atr && !pf->fd_tcp4_filter_cnt)
Jacob Keller47994c12017-04-19 09:25:57 -04006951 pf->flags &= ~I40E_FLAG_FD_ATR_AUTO_DISABLED;
Jacob Keller0da36b92017-04-19 09:25:55 -04006952 clear_bit(__I40E_FD_FLUSH_REQUESTED, pf->state);
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04006953 if (I40E_DEBUG_FD & pf->hw.debug_mask)
6954 dev_info(&pf->pdev->dev, "FD Filter table flushed and FD-SB replayed.\n");
6955 }
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006956}
6957
6958/**
6959 * i40e_get_current_atr_count - Get the count of total FD ATR filters programmed
6960 * @pf: board private structure
6961 **/
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006962u32 i40e_get_current_atr_cnt(struct i40e_pf *pf)
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006963{
6964 return i40e_get_current_fd_count(pf) - pf->fdir_pf_active_filters;
6965}
6966
6967/* We can see up to 256 filter programming desc in transit if the filters are
6968 * being applied really fast; before we see the first
6969 * filter miss error on Rx queue 0. Accumulating enough error messages before
6970 * reacting will make sure we don't cause flush too often.
6971 */
6972#define I40E_MAX_FD_PROGRAM_ERROR 256
6973
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006974/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006975 * i40e_fdir_reinit_subtask - Worker thread to reinit FDIR filter table
6976 * @pf: board private structure
6977 **/
6978static void i40e_fdir_reinit_subtask(struct i40e_pf *pf)
6979{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006980
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006981 /* if interface is down do nothing */
Mauro S. M. Rodrigues9e6c9c02017-05-12 23:26:56 -03006982 if (test_bit(__I40E_DOWN, pf->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006983 return;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006984
Jacob Keller0da36b92017-04-19 09:25:55 -04006985 if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006986 i40e_fdir_flush_and_replay(pf);
6987
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006988 i40e_fdir_check_and_reenable(pf);
6989
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006990}
6991
6992/**
6993 * i40e_vsi_link_event - notify VSI of a link event
6994 * @vsi: vsi to be notified
6995 * @link_up: link up or down
6996 **/
6997static void i40e_vsi_link_event(struct i40e_vsi *vsi, bool link_up)
6998{
Jacob Keller0da36b92017-04-19 09:25:55 -04006999 if (!vsi || test_bit(__I40E_VSI_DOWN, vsi->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007000 return;
7001
7002 switch (vsi->type) {
7003 case I40E_VSI_MAIN:
7004 if (!vsi->netdev || !vsi->netdev_registered)
7005 break;
7006
7007 if (link_up) {
7008 netif_carrier_on(vsi->netdev);
7009 netif_tx_wake_all_queues(vsi->netdev);
7010 } else {
7011 netif_carrier_off(vsi->netdev);
7012 netif_tx_stop_all_queues(vsi->netdev);
7013 }
7014 break;
7015
7016 case I40E_VSI_SRIOV:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007017 case I40E_VSI_VMDQ2:
7018 case I40E_VSI_CTRL:
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007019 case I40E_VSI_IWARP:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007020 case I40E_VSI_MIRROR:
7021 default:
7022 /* there is no notification for other VSIs */
7023 break;
7024 }
7025}
7026
7027/**
7028 * i40e_veb_link_event - notify elements on the veb of a link event
7029 * @veb: veb to be notified
7030 * @link_up: link up or down
7031 **/
7032static void i40e_veb_link_event(struct i40e_veb *veb, bool link_up)
7033{
7034 struct i40e_pf *pf;
7035 int i;
7036
7037 if (!veb || !veb->pf)
7038 return;
7039 pf = veb->pf;
7040
7041 /* depth first... */
7042 for (i = 0; i < I40E_MAX_VEB; i++)
7043 if (pf->veb[i] && (pf->veb[i]->uplink_seid == veb->seid))
7044 i40e_veb_link_event(pf->veb[i], link_up);
7045
7046 /* ... now the local VSIs */
Mitch Williams505682c2014-05-20 08:01:37 +00007047 for (i = 0; i < pf->num_alloc_vsi; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007048 if (pf->vsi[i] && (pf->vsi[i]->uplink_seid == veb->seid))
7049 i40e_vsi_link_event(pf->vsi[i], link_up);
7050}
7051
7052/**
7053 * i40e_link_event - Update netif_carrier status
7054 * @pf: board private structure
7055 **/
7056static void i40e_link_event(struct i40e_pf *pf)
7057{
Mitch Williams320684c2014-10-17 03:14:43 +00007058 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
Catherine Sullivanfef59dd2014-12-11 07:06:33 +00007059 u8 new_link_speed, old_link_speed;
Jesse Brandeburga72a5abc2015-08-26 15:14:19 -04007060 i40e_status status;
7061 bool new_link, old_link;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007062
Catherine Sullivan1f9610e2015-10-21 19:47:09 -04007063 /* save off old link status information */
7064 pf->hw.phy.link_info_old = pf->hw.phy.link_info;
7065
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00007066 /* set this to force the get_link_status call to refresh state */
7067 pf->hw.phy.get_link_info = true;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007068
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00007069 old_link = (pf->hw.phy.link_info_old.link_info & I40E_AQ_LINK_UP);
Jesse Brandeburga72a5abc2015-08-26 15:14:19 -04007070
7071 status = i40e_get_link_status(&pf->hw, &new_link);
Harshitha Ramamurthyae136702016-12-12 15:44:16 -08007072
7073 /* On success, disable temp link polling */
7074 if (status == I40E_SUCCESS) {
7075 if (pf->flags & I40E_FLAG_TEMP_LINK_POLLING)
7076 pf->flags &= ~I40E_FLAG_TEMP_LINK_POLLING;
7077 } else {
7078 /* Enable link polling temporarily until i40e_get_link_status
7079 * returns I40E_SUCCESS
7080 */
7081 pf->flags |= I40E_FLAG_TEMP_LINK_POLLING;
Jesse Brandeburga72a5abc2015-08-26 15:14:19 -04007082 dev_dbg(&pf->pdev->dev, "couldn't get link state, status: %d\n",
7083 status);
7084 return;
7085 }
7086
Catherine Sullivanfef59dd2014-12-11 07:06:33 +00007087 old_link_speed = pf->hw.phy.link_info_old.link_speed;
7088 new_link_speed = pf->hw.phy.link_info.link_speed;
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00007089
7090 if (new_link == old_link &&
Catherine Sullivanfef59dd2014-12-11 07:06:33 +00007091 new_link_speed == old_link_speed &&
Jacob Keller0da36b92017-04-19 09:25:55 -04007092 (test_bit(__I40E_VSI_DOWN, vsi->state) ||
Mitch Williams320684c2014-10-17 03:14:43 +00007093 new_link == netif_carrier_ok(vsi->netdev)))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007094 return;
Mitch Williams320684c2014-10-17 03:14:43 +00007095
Sudheer Mogilappagari9a034492017-07-14 09:10:16 -04007096 i40e_print_link_message(vsi, new_link);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007097
7098 /* Notify the base of the switch tree connected to
7099 * the link. Floating VEBs are not notified.
7100 */
7101 if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
7102 i40e_veb_link_event(pf->veb[pf->lan_veb], new_link);
7103 else
Mitch Williams320684c2014-10-17 03:14:43 +00007104 i40e_vsi_link_event(vsi, new_link);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007105
7106 if (pf->vf)
7107 i40e_vc_notify_link_state(pf);
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00007108
7109 if (pf->flags & I40E_FLAG_PTP)
7110 i40e_ptp_set_increment(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007111}
7112
7113/**
Shannon Nelson21536712014-10-25 10:35:25 +00007114 * i40e_watchdog_subtask - periodic checks not using event driven response
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007115 * @pf: board private structure
7116 **/
7117static void i40e_watchdog_subtask(struct i40e_pf *pf)
7118{
7119 int i;
7120
7121 /* if interface is down do nothing */
Mauro S. M. Rodrigues9e6c9c02017-05-12 23:26:56 -03007122 if (test_bit(__I40E_DOWN, pf->state) ||
Jacob Keller0da36b92017-04-19 09:25:55 -04007123 test_bit(__I40E_CONFIG_BUSY, pf->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007124 return;
7125
Shannon Nelson21536712014-10-25 10:35:25 +00007126 /* make sure we don't do these things too often */
7127 if (time_before(jiffies, (pf->service_timer_previous +
7128 pf->service_timer_period)))
7129 return;
7130 pf->service_timer_previous = jiffies;
7131
Harshitha Ramamurthyae136702016-12-12 15:44:16 -08007132 if ((pf->flags & I40E_FLAG_LINK_POLLING_ENABLED) ||
7133 (pf->flags & I40E_FLAG_TEMP_LINK_POLLING))
Shannon Nelson9ac77262015-08-27 11:42:40 -04007134 i40e_link_event(pf);
Shannon Nelson21536712014-10-25 10:35:25 +00007135
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007136 /* Update the stats for active netdevs so the network stack
7137 * can look at updated numbers whenever it cares to
7138 */
Mitch Williams505682c2014-05-20 08:01:37 +00007139 for (i = 0; i < pf->num_alloc_vsi; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007140 if (pf->vsi[i] && pf->vsi[i]->netdev)
7141 i40e_update_stats(pf->vsi[i]);
7142
Anjali Singhai Jaind1a8d272015-07-23 16:54:40 -04007143 if (pf->flags & I40E_FLAG_VEB_STATS_ENABLED) {
7144 /* Update the stats for the active switching components */
7145 for (i = 0; i < I40E_MAX_VEB; i++)
7146 if (pf->veb[i])
7147 i40e_update_veb_stats(pf->veb[i]);
7148 }
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00007149
Jacob Keller61189552017-05-03 10:29:01 -07007150 i40e_ptp_rx_hang(pf);
Jacob Keller0bc07062017-05-03 10:29:02 -07007151 i40e_ptp_tx_hang(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007152}
7153
7154/**
7155 * i40e_reset_subtask - Set up for resetting the device and driver
7156 * @pf: board private structure
7157 **/
7158static void i40e_reset_subtask(struct i40e_pf *pf)
7159{
7160 u32 reset_flags = 0;
7161
Jacob Keller0da36b92017-04-19 09:25:55 -04007162 if (test_bit(__I40E_REINIT_REQUESTED, pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08007163 reset_flags |= BIT(__I40E_REINIT_REQUESTED);
Jacob Keller0da36b92017-04-19 09:25:55 -04007164 clear_bit(__I40E_REINIT_REQUESTED, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007165 }
Jacob Keller0da36b92017-04-19 09:25:55 -04007166 if (test_bit(__I40E_PF_RESET_REQUESTED, pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08007167 reset_flags |= BIT(__I40E_PF_RESET_REQUESTED);
Jacob Keller0da36b92017-04-19 09:25:55 -04007168 clear_bit(__I40E_PF_RESET_REQUESTED, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007169 }
Jacob Keller0da36b92017-04-19 09:25:55 -04007170 if (test_bit(__I40E_CORE_RESET_REQUESTED, pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08007171 reset_flags |= BIT(__I40E_CORE_RESET_REQUESTED);
Jacob Keller0da36b92017-04-19 09:25:55 -04007172 clear_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007173 }
Jacob Keller0da36b92017-04-19 09:25:55 -04007174 if (test_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08007175 reset_flags |= BIT(__I40E_GLOBAL_RESET_REQUESTED);
Jacob Keller0da36b92017-04-19 09:25:55 -04007176 clear_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007177 }
Mauro S. M. Rodrigues9e6c9c02017-05-12 23:26:56 -03007178 if (test_bit(__I40E_DOWN_REQUESTED, pf->state)) {
7179 reset_flags |= BIT(__I40E_DOWN_REQUESTED);
7180 clear_bit(__I40E_DOWN_REQUESTED, pf->state);
Neerav Parikhb5d06f02014-06-03 23:50:17 +00007181 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007182
7183 /* If there's a recovery already waiting, it takes
7184 * precedence before starting a new reset sequence.
7185 */
Jacob Keller0da36b92017-04-19 09:25:55 -04007186 if (test_bit(__I40E_RESET_INTR_RECEIVED, pf->state)) {
Maciej Sosin373149f2017-04-05 07:50:55 -04007187 i40e_prep_for_reset(pf, false);
7188 i40e_reset(pf);
7189 i40e_rebuild(pf, false, false);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007190 }
7191
7192 /* If we're already down or resetting, just bail */
7193 if (reset_flags &&
Mauro S. M. Rodrigues9e6c9c02017-05-12 23:26:56 -03007194 !test_bit(__I40E_DOWN, pf->state) &&
Jacob Keller0da36b92017-04-19 09:25:55 -04007195 !test_bit(__I40E_CONFIG_BUSY, pf->state)) {
Jacob Kellerdfc4ff62017-06-07 05:43:13 -04007196 i40e_do_reset(pf, reset_flags, false);
Maciej Sosin373149f2017-04-05 07:50:55 -04007197 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007198}
7199
7200/**
7201 * i40e_handle_link_event - Handle link event
7202 * @pf: board private structure
7203 * @e: event info posted on ARQ
7204 **/
7205static void i40e_handle_link_event(struct i40e_pf *pf,
7206 struct i40e_arq_event_info *e)
7207{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007208 struct i40e_aqc_get_link_status *status =
7209 (struct i40e_aqc_get_link_status *)&e->desc.params.raw;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007210
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00007211 /* Do a new status request to re-enable LSE reporting
7212 * and load new status information into the hw struct
7213 * This completely ignores any state information
7214 * in the ARQ event info, instead choosing to always
7215 * issue the AQ update link status command.
7216 */
7217 i40e_link_event(pf);
7218
Filip Sadowski9a858172017-08-29 05:32:37 -04007219 /* Check if module meets thermal requirements */
7220 if (status->phy_type == I40E_PHY_TYPE_NOT_SUPPORTED_HIGH_TEMP) {
Carolyn Wyborny7b592f62014-07-10 07:58:19 +00007221 dev_err(&pf->pdev->dev,
Filip Sadowski9a858172017-08-29 05:32:37 -04007222 "Rx/Tx is disabled on this device because the module does not meet thermal requirements.\n");
7223 dev_err(&pf->pdev->dev,
7224 "Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules.\n");
7225 } else {
7226 /* check for unqualified module, if link is down, suppress
7227 * the message if link was forced to be down.
7228 */
7229 if ((status->link_info & I40E_AQ_MEDIA_AVAILABLE) &&
7230 (!(status->an_info & I40E_AQ_QUALIFIED_MODULE)) &&
7231 (!(status->link_info & I40E_AQ_LINK_UP)) &&
7232 (!(pf->flags & I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED))) {
7233 dev_err(&pf->pdev->dev,
7234 "Rx/Tx is disabled on this device because an unsupported SFP module type was detected.\n");
7235 dev_err(&pf->pdev->dev,
7236 "Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules.\n");
7237 }
7238 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007239}
7240
7241/**
7242 * i40e_clean_adminq_subtask - Clean the AdminQ rings
7243 * @pf: board private structure
7244 **/
7245static void i40e_clean_adminq_subtask(struct i40e_pf *pf)
7246{
7247 struct i40e_arq_event_info event;
7248 struct i40e_hw *hw = &pf->hw;
7249 u16 pending, i = 0;
7250 i40e_status ret;
7251 u16 opcode;
Shannon Nelson86df2422014-05-20 08:01:35 +00007252 u32 oldval;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007253 u32 val;
7254
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00007255 /* Do not run clean AQ when PF reset fails */
Jacob Keller0da36b92017-04-19 09:25:55 -04007256 if (test_bit(__I40E_RESET_FAILED, pf->state))
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00007257 return;
7258
Shannon Nelson86df2422014-05-20 08:01:35 +00007259 /* check for error indications */
7260 val = rd32(&pf->hw, pf->hw.aq.arq.len);
7261 oldval = val;
7262 if (val & I40E_PF_ARQLEN_ARQVFE_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08007263 if (hw->debug_mask & I40E_DEBUG_AQ)
7264 dev_info(&pf->pdev->dev, "ARQ VF Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00007265 val &= ~I40E_PF_ARQLEN_ARQVFE_MASK;
7266 }
7267 if (val & I40E_PF_ARQLEN_ARQOVFL_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08007268 if (hw->debug_mask & I40E_DEBUG_AQ)
7269 dev_info(&pf->pdev->dev, "ARQ Overflow Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00007270 val &= ~I40E_PF_ARQLEN_ARQOVFL_MASK;
Mitch Williams1d0a4ad2015-12-23 12:05:48 -08007271 pf->arq_overflows++;
Shannon Nelson86df2422014-05-20 08:01:35 +00007272 }
7273 if (val & I40E_PF_ARQLEN_ARQCRIT_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08007274 if (hw->debug_mask & I40E_DEBUG_AQ)
7275 dev_info(&pf->pdev->dev, "ARQ Critical Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00007276 val &= ~I40E_PF_ARQLEN_ARQCRIT_MASK;
7277 }
7278 if (oldval != val)
7279 wr32(&pf->hw, pf->hw.aq.arq.len, val);
7280
7281 val = rd32(&pf->hw, pf->hw.aq.asq.len);
7282 oldval = val;
7283 if (val & I40E_PF_ATQLEN_ATQVFE_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08007284 if (pf->hw.debug_mask & I40E_DEBUG_AQ)
7285 dev_info(&pf->pdev->dev, "ASQ VF Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00007286 val &= ~I40E_PF_ATQLEN_ATQVFE_MASK;
7287 }
7288 if (val & I40E_PF_ATQLEN_ATQOVFL_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08007289 if (pf->hw.debug_mask & I40E_DEBUG_AQ)
7290 dev_info(&pf->pdev->dev, "ASQ Overflow Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00007291 val &= ~I40E_PF_ATQLEN_ATQOVFL_MASK;
7292 }
7293 if (val & I40E_PF_ATQLEN_ATQCRIT_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08007294 if (pf->hw.debug_mask & I40E_DEBUG_AQ)
7295 dev_info(&pf->pdev->dev, "ASQ Critical Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00007296 val &= ~I40E_PF_ATQLEN_ATQCRIT_MASK;
7297 }
7298 if (oldval != val)
7299 wr32(&pf->hw, pf->hw.aq.asq.len, val);
7300
Mitch Williams1001dc32014-11-11 20:02:19 +00007301 event.buf_len = I40E_MAX_AQ_BUF_SIZE;
7302 event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007303 if (!event.msg_buf)
7304 return;
7305
7306 do {
7307 ret = i40e_clean_arq_element(hw, &event, &pending);
Mitch Williams56497972014-06-04 08:45:18 +00007308 if (ret == I40E_ERR_ADMIN_QUEUE_NO_WORK)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007309 break;
Mitch Williams56497972014-06-04 08:45:18 +00007310 else if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007311 dev_info(&pf->pdev->dev, "ARQ event error %d\n", ret);
7312 break;
7313 }
7314
7315 opcode = le16_to_cpu(event.desc.opcode);
7316 switch (opcode) {
7317
7318 case i40e_aqc_opc_get_link_status:
7319 i40e_handle_link_event(pf, &event);
7320 break;
7321 case i40e_aqc_opc_send_msg_to_pf:
7322 ret = i40e_vc_process_vf_msg(pf,
7323 le16_to_cpu(event.desc.retval),
7324 le32_to_cpu(event.desc.cookie_high),
7325 le32_to_cpu(event.desc.cookie_low),
7326 event.msg_buf,
Mitch Williams1001dc32014-11-11 20:02:19 +00007327 event.msg_len);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007328 break;
7329 case i40e_aqc_opc_lldp_update_mib:
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00007330 dev_dbg(&pf->pdev->dev, "ARQ: Update LLDP MIB event received\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08007331#ifdef CONFIG_I40E_DCB
7332 rtnl_lock();
7333 ret = i40e_handle_lldp_event(pf, &event);
7334 rtnl_unlock();
7335#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007336 break;
7337 case i40e_aqc_opc_event_lan_overflow:
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00007338 dev_dbg(&pf->pdev->dev, "ARQ LAN queue overflow event received\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007339 i40e_handle_lan_overflow_event(pf, &event);
7340 break;
Shannon Nelson0467bc92013-12-18 13:45:58 +00007341 case i40e_aqc_opc_send_msg_to_peer:
7342 dev_info(&pf->pdev->dev, "ARQ: Msg from other pf\n");
7343 break;
Shannon Nelson91a0f932015-03-19 14:32:01 -07007344 case i40e_aqc_opc_nvm_erase:
7345 case i40e_aqc_opc_nvm_update:
Michal Kosiarz00ada502015-11-19 11:34:20 -08007346 case i40e_aqc_opc_oem_post_update:
Shannon Nelson6e93d0c2016-01-15 14:33:18 -08007347 i40e_debug(&pf->hw, I40E_DEBUG_NVM,
7348 "ARQ NVM operation 0x%04x completed\n",
7349 opcode);
Shannon Nelson91a0f932015-03-19 14:32:01 -07007350 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007351 default:
7352 dev_info(&pf->pdev->dev,
Shannon Nelson56e5ca62016-03-10 14:59:48 -08007353 "ARQ: Unknown event 0x%04x ignored\n",
Shannon Nelson0467bc92013-12-18 13:45:58 +00007354 opcode);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007355 break;
7356 }
Christopher N Bednarz1fca3262017-03-10 12:22:03 -08007357 } while (i++ < pf->adminq_work_limit);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007358
Christopher N Bednarz1fca3262017-03-10 12:22:03 -08007359 if (i < pf->adminq_work_limit)
Jacob Keller0da36b92017-04-19 09:25:55 -04007360 clear_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state);
Christopher N Bednarz1fca3262017-03-10 12:22:03 -08007361
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007362 /* re-enable Admin queue interrupt cause */
7363 val = rd32(hw, I40E_PFINT_ICR0_ENA);
7364 val |= I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
7365 wr32(hw, I40E_PFINT_ICR0_ENA, val);
7366 i40e_flush(hw);
7367
7368 kfree(event.msg_buf);
7369}
7370
7371/**
Shannon Nelson4eb3f762014-03-06 08:59:58 +00007372 * i40e_verify_eeprom - make sure eeprom is good to use
7373 * @pf: board private structure
7374 **/
7375static void i40e_verify_eeprom(struct i40e_pf *pf)
7376{
7377 int err;
7378
7379 err = i40e_diag_eeprom_test(&pf->hw);
7380 if (err) {
7381 /* retry in case of garbage read */
7382 err = i40e_diag_eeprom_test(&pf->hw);
7383 if (err) {
7384 dev_info(&pf->pdev->dev, "eeprom check failed (%d), Tx/Rx traffic disabled\n",
7385 err);
Jacob Keller0da36b92017-04-19 09:25:55 -04007386 set_bit(__I40E_BAD_EEPROM, pf->state);
Shannon Nelson4eb3f762014-03-06 08:59:58 +00007387 }
7388 }
7389
Jacob Keller0da36b92017-04-19 09:25:55 -04007390 if (!err && test_bit(__I40E_BAD_EEPROM, pf->state)) {
Shannon Nelson4eb3f762014-03-06 08:59:58 +00007391 dev_info(&pf->pdev->dev, "eeprom check passed, Tx/Rx traffic enabled\n");
Jacob Keller0da36b92017-04-19 09:25:55 -04007392 clear_bit(__I40E_BAD_EEPROM, pf->state);
Shannon Nelson4eb3f762014-03-06 08:59:58 +00007393 }
7394}
7395
7396/**
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00007397 * i40e_enable_pf_switch_lb
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00007398 * @pf: pointer to the PF structure
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00007399 *
7400 * enable switch loop back or die - no point in a return value
7401 **/
7402static void i40e_enable_pf_switch_lb(struct i40e_pf *pf)
7403{
7404 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
7405 struct i40e_vsi_context ctxt;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04007406 int ret;
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00007407
7408 ctxt.seid = pf->main_vsi_seid;
7409 ctxt.pf_num = pf->hw.pf_id;
7410 ctxt.vf_num = 0;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04007411 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
7412 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00007413 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04007414 "couldn't get PF vsi config, err %s aq_err %s\n",
7415 i40e_stat_str(&pf->hw, ret),
7416 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00007417 return;
7418 }
7419 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
7420 ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
7421 ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
7422
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04007423 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
7424 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00007425 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04007426 "update vsi switch failed, err %s aq_err %s\n",
7427 i40e_stat_str(&pf->hw, ret),
7428 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00007429 }
7430}
7431
7432/**
7433 * i40e_disable_pf_switch_lb
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00007434 * @pf: pointer to the PF structure
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00007435 *
7436 * disable switch loop back or die - no point in a return value
7437 **/
7438static void i40e_disable_pf_switch_lb(struct i40e_pf *pf)
7439{
7440 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
7441 struct i40e_vsi_context ctxt;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04007442 int ret;
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00007443
7444 ctxt.seid = pf->main_vsi_seid;
7445 ctxt.pf_num = pf->hw.pf_id;
7446 ctxt.vf_num = 0;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04007447 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
7448 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00007449 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04007450 "couldn't get PF vsi config, err %s aq_err %s\n",
7451 i40e_stat_str(&pf->hw, ret),
7452 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00007453 return;
7454 }
7455 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
7456 ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
7457 ctxt.info.switch_id &= ~cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
7458
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04007459 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
7460 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00007461 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04007462 "update vsi switch failed, err %s aq_err %s\n",
7463 i40e_stat_str(&pf->hw, ret),
7464 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00007465 }
7466}
7467
7468/**
Neerav Parikh51616012015-02-06 08:52:14 +00007469 * i40e_config_bridge_mode - Configure the HW bridge mode
7470 * @veb: pointer to the bridge instance
7471 *
7472 * Configure the loop back mode for the LAN VSI that is downlink to the
7473 * specified HW bridge instance. It is expected this function is called
7474 * when a new HW bridge is instantiated.
7475 **/
7476static void i40e_config_bridge_mode(struct i40e_veb *veb)
7477{
7478 struct i40e_pf *pf = veb->pf;
7479
Shannon Nelson6dec1012015-09-28 14:12:30 -04007480 if (pf->hw.debug_mask & I40E_DEBUG_LAN)
7481 dev_info(&pf->pdev->dev, "enabling bridge mode: %s\n",
7482 veb->bridge_mode == BRIDGE_MODE_VEPA ? "VEPA" : "VEB");
Neerav Parikh51616012015-02-06 08:52:14 +00007483 if (veb->bridge_mode & BRIDGE_MODE_VEPA)
7484 i40e_disable_pf_switch_lb(pf);
7485 else
7486 i40e_enable_pf_switch_lb(pf);
7487}
7488
7489/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007490 * i40e_reconstitute_veb - rebuild the VEB and anything connected to it
7491 * @veb: pointer to the VEB instance
7492 *
7493 * This is a recursive function that first builds the attached VSIs then
7494 * recurses in to build the next layer of VEB. We track the connections
7495 * through our own index numbers because the seid's from the HW could
7496 * change across the reset.
7497 **/
7498static int i40e_reconstitute_veb(struct i40e_veb *veb)
7499{
7500 struct i40e_vsi *ctl_vsi = NULL;
7501 struct i40e_pf *pf = veb->pf;
7502 int v, veb_idx;
7503 int ret;
7504
7505 /* build VSI that owns this VEB, temporarily attached to base VEB */
Mitch Williams505682c2014-05-20 08:01:37 +00007506 for (v = 0; v < pf->num_alloc_vsi && !ctl_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007507 if (pf->vsi[v] &&
7508 pf->vsi[v]->veb_idx == veb->idx &&
7509 pf->vsi[v]->flags & I40E_VSI_FLAG_VEB_OWNER) {
7510 ctl_vsi = pf->vsi[v];
7511 break;
7512 }
7513 }
7514 if (!ctl_vsi) {
7515 dev_info(&pf->pdev->dev,
7516 "missing owner VSI for veb_idx %d\n", veb->idx);
7517 ret = -ENOENT;
7518 goto end_reconstitute;
7519 }
7520 if (ctl_vsi != pf->vsi[pf->lan_vsi])
7521 ctl_vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
7522 ret = i40e_add_vsi(ctl_vsi);
7523 if (ret) {
7524 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04007525 "rebuild of veb_idx %d owner VSI failed: %d\n",
7526 veb->idx, ret);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007527 goto end_reconstitute;
7528 }
7529 i40e_vsi_reset_stats(ctl_vsi);
7530
7531 /* create the VEB in the switch and move the VSI onto the VEB */
7532 ret = i40e_add_veb(veb, ctl_vsi);
7533 if (ret)
7534 goto end_reconstitute;
7535
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07007536 if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
7537 veb->bridge_mode = BRIDGE_MODE_VEB;
7538 else
7539 veb->bridge_mode = BRIDGE_MODE_VEPA;
Neerav Parikh51616012015-02-06 08:52:14 +00007540 i40e_config_bridge_mode(veb);
Anjali Singhai Jainb64ba082014-11-13 03:06:15 +00007541
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007542 /* create the remaining VSIs attached to this VEB */
Mitch Williams505682c2014-05-20 08:01:37 +00007543 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007544 if (!pf->vsi[v] || pf->vsi[v] == ctl_vsi)
7545 continue;
7546
7547 if (pf->vsi[v]->veb_idx == veb->idx) {
7548 struct i40e_vsi *vsi = pf->vsi[v];
Jesse Brandeburg6995b362015-08-28 17:55:54 -04007549
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007550 vsi->uplink_seid = veb->seid;
7551 ret = i40e_add_vsi(vsi);
7552 if (ret) {
7553 dev_info(&pf->pdev->dev,
7554 "rebuild of vsi_idx %d failed: %d\n",
7555 v, ret);
7556 goto end_reconstitute;
7557 }
7558 i40e_vsi_reset_stats(vsi);
7559 }
7560 }
7561
7562 /* create any VEBs attached to this VEB - RECURSION */
7563 for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
7564 if (pf->veb[veb_idx] && pf->veb[veb_idx]->veb_idx == veb->idx) {
7565 pf->veb[veb_idx]->uplink_seid = veb->seid;
7566 ret = i40e_reconstitute_veb(pf->veb[veb_idx]);
7567 if (ret)
7568 break;
7569 }
7570 }
7571
7572end_reconstitute:
7573 return ret;
7574}
7575
7576/**
7577 * i40e_get_capabilities - get info about the HW
7578 * @pf: the PF struct
7579 **/
7580static int i40e_get_capabilities(struct i40e_pf *pf)
7581{
7582 struct i40e_aqc_list_capabilities_element_resp *cap_buf;
7583 u16 data_size;
7584 int buf_len;
7585 int err;
7586
7587 buf_len = 40 * sizeof(struct i40e_aqc_list_capabilities_element_resp);
7588 do {
7589 cap_buf = kzalloc(buf_len, GFP_KERNEL);
7590 if (!cap_buf)
7591 return -ENOMEM;
7592
7593 /* this loads the data into the hw struct for us */
7594 err = i40e_aq_discover_capabilities(&pf->hw, cap_buf, buf_len,
7595 &data_size,
7596 i40e_aqc_opc_list_func_capabilities,
7597 NULL);
7598 /* data loaded, buffer no longer needed */
7599 kfree(cap_buf);
7600
7601 if (pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOMEM) {
7602 /* retry with a larger buffer */
7603 buf_len = data_size;
7604 } else if (pf->hw.aq.asq_last_status != I40E_AQ_RC_OK) {
7605 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04007606 "capability discovery failed, err %s aq_err %s\n",
7607 i40e_stat_str(&pf->hw, err),
7608 i40e_aq_str(&pf->hw,
7609 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007610 return -ENODEV;
7611 }
7612 } while (err);
7613
7614 if (pf->hw.debug_mask & I40E_DEBUG_USER)
7615 dev_info(&pf->pdev->dev,
7616 "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",
7617 pf->hw.pf_id, pf->hw.func_caps.num_vfs,
7618 pf->hw.func_caps.num_msix_vectors,
7619 pf->hw.func_caps.num_msix_vectors_vf,
7620 pf->hw.func_caps.fd_filters_guaranteed,
7621 pf->hw.func_caps.fd_filters_best_effort,
7622 pf->hw.func_caps.num_tx_qp,
7623 pf->hw.func_caps.num_vsis);
7624
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00007625#define DEF_NUM_VSI (1 + (pf->hw.func_caps.fcoe ? 1 : 0) \
7626 + pf->hw.func_caps.num_vfs)
7627 if (pf->hw.revision_id == 0 && (DEF_NUM_VSI > pf->hw.func_caps.num_vsis)) {
7628 dev_info(&pf->pdev->dev,
7629 "got num_vsis %d, setting num_vsis to %d\n",
7630 pf->hw.func_caps.num_vsis, DEF_NUM_VSI);
7631 pf->hw.func_caps.num_vsis = DEF_NUM_VSI;
7632 }
7633
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007634 return 0;
7635}
7636
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08007637static int i40e_vsi_clear(struct i40e_vsi *vsi);
7638
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007639/**
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08007640 * i40e_fdir_sb_setup - initialize the Flow Director resources for Sideband
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007641 * @pf: board private structure
7642 **/
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08007643static void i40e_fdir_sb_setup(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007644{
7645 struct i40e_vsi *vsi;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007646
Jesse Brandeburg407e0632014-06-03 23:50:12 +00007647 /* quick workaround for an NVM issue that leaves a critical register
7648 * uninitialized
7649 */
7650 if (!rd32(&pf->hw, I40E_GLQF_HKEY(0))) {
7651 static const u32 hkey[] = {
7652 0xe640d33f, 0xcdfe98ab, 0x73fa7161, 0x0d7a7d36,
7653 0xeacb7d61, 0xaa4f05b6, 0x9c5c89ed, 0xfc425ddb,
7654 0xa4654832, 0xfc7461d4, 0x8f827619, 0xf5c63c21,
7655 0x95b3a76d};
Alexander Duyck4b816442016-10-11 15:26:53 -07007656 int i;
Jesse Brandeburg407e0632014-06-03 23:50:12 +00007657
7658 for (i = 0; i <= I40E_GLQF_HKEY_MAX_INDEX; i++)
7659 wr32(&pf->hw, I40E_GLQF_HKEY(i), hkey[i]);
7660 }
7661
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08007662 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007663 return;
7664
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08007665 /* find existing VSI and see if it needs configuring */
Alexander Duyck4b816442016-10-11 15:26:53 -07007666 vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08007667
7668 /* create a new VSI if none exists */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007669 if (!vsi) {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08007670 vsi = i40e_vsi_setup(pf, I40E_VSI_FDIR,
7671 pf->vsi[pf->lan_vsi]->seid, 0);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007672 if (!vsi) {
7673 dev_info(&pf->pdev->dev, "Couldn't create FDir VSI\n");
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00007674 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
7675 return;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007676 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007677 }
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00007678
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08007679 i40e_vsi_setup_irqhandler(vsi, i40e_fdir_clean_ring);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007680}
7681
7682/**
7683 * i40e_fdir_teardown - release the Flow Director resources
7684 * @pf: board private structure
7685 **/
7686static void i40e_fdir_teardown(struct i40e_pf *pf)
7687{
Alexander Duyck4b816442016-10-11 15:26:53 -07007688 struct i40e_vsi *vsi;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007689
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00007690 i40e_fdir_filter_exit(pf);
Alexander Duyck4b816442016-10-11 15:26:53 -07007691 vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
7692 if (vsi)
7693 i40e_vsi_release(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007694}
7695
7696/**
Amritha Nambiar8f88b302017-09-07 04:00:17 -07007697 * i40e_rebuild_channels - Rebuilds channel VSIs if they existed before reset
7698 * @vsi: PF main vsi
7699 *
7700 * Rebuilds channel VSIs if they existed before reset
7701 **/
7702static int i40e_rebuild_channels(struct i40e_vsi *vsi)
7703{
7704 struct i40e_channel *ch, *ch_tmp;
7705 i40e_status ret;
7706
7707 if (list_empty(&vsi->ch_list))
7708 return 0;
7709
7710 list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
7711 if (!ch->initialized)
7712 break;
7713 /* Proceed with creation of channel (VMDq2) VSI */
7714 ret = i40e_add_channel(vsi->back, vsi->uplink_seid, ch);
7715 if (ret) {
7716 dev_info(&vsi->back->pdev->dev,
7717 "failed to rebuild channels using uplink_seid %u\n",
7718 vsi->uplink_seid);
7719 return ret;
7720 }
7721 }
7722 return 0;
7723}
7724
7725/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007726 * i40e_prep_for_reset - prep for the core to reset
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007727 * @pf: board private structure
Maciej Sosin373149f2017-04-05 07:50:55 -04007728 * @lock_acquired: indicates whether or not the lock has been acquired
7729 * before this function was called.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007730 *
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00007731 * Close up the VFs and other things in prep for PF Reset.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007732 **/
Maciej Sosin373149f2017-04-05 07:50:55 -04007733static void i40e_prep_for_reset(struct i40e_pf *pf, bool lock_acquired)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007734{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007735 struct i40e_hw *hw = &pf->hw;
Shannon Nelson60442de2014-04-23 04:50:13 +00007736 i40e_status ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007737 u32 v;
7738
Jacob Keller0da36b92017-04-19 09:25:55 -04007739 clear_bit(__I40E_RESET_INTR_RECEIVED, pf->state);
7740 if (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
Shannon Nelson23cfbe02014-06-04 01:23:14 +00007741 return;
Mitch Williamsd3ce57342016-03-10 14:59:46 -08007742 if (i40e_check_asq_alive(&pf->hw))
7743 i40e_vc_notify_reset(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007744
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00007745 dev_dbg(&pf->pdev->dev, "Tearing down internal switch for reset\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007746
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007747 /* quiesce the VSIs and their queues that are not already DOWN */
Maciej Sosin373149f2017-04-05 07:50:55 -04007748 /* pf_quiesce_all_vsi modifies netdev structures -rtnl_lock needed */
7749 if (!lock_acquired)
7750 rtnl_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007751 i40e_pf_quiesce_all_vsi(pf);
Maciej Sosin373149f2017-04-05 07:50:55 -04007752 if (!lock_acquired)
7753 rtnl_unlock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007754
Mitch Williams505682c2014-05-20 08:01:37 +00007755 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007756 if (pf->vsi[v])
7757 pf->vsi[v]->seid = 0;
7758 }
7759
7760 i40e_shutdown_adminq(&pf->hw);
7761
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007762 /* call shutdown HMC */
Shannon Nelson60442de2014-04-23 04:50:13 +00007763 if (hw->hmc.hmc_obj) {
7764 ret = i40e_shutdown_lan_hmc(hw);
Shannon Nelson23cfbe02014-06-04 01:23:14 +00007765 if (ret)
Shannon Nelson60442de2014-04-23 04:50:13 +00007766 dev_warn(&pf->pdev->dev,
7767 "shutdown_lan_hmc failed: %d\n", ret);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007768 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007769}
7770
7771/**
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00007772 * i40e_send_version - update firmware with driver version
7773 * @pf: PF struct
7774 */
7775static void i40e_send_version(struct i40e_pf *pf)
7776{
7777 struct i40e_driver_version dv;
7778
7779 dv.major_version = DRV_VERSION_MAJOR;
7780 dv.minor_version = DRV_VERSION_MINOR;
7781 dv.build_version = DRV_VERSION_BUILD;
7782 dv.subbuild_version = 0;
Rickard Strandqvist35a7d802014-07-29 09:26:25 +00007783 strlcpy(dv.driver_string, DRV_VERSION, sizeof(dv.driver_string));
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00007784 i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
7785}
7786
7787/**
Filip Sadowski5bbb2e22017-06-07 05:43:09 -04007788 * i40e_get_oem_version - get OEM specific version information
7789 * @hw: pointer to the hardware structure
7790 **/
7791static void i40e_get_oem_version(struct i40e_hw *hw)
7792{
7793 u16 block_offset = 0xffff;
7794 u16 block_length = 0;
7795 u16 capabilities = 0;
7796 u16 gen_snap = 0;
7797 u16 release = 0;
7798
7799#define I40E_SR_NVM_OEM_VERSION_PTR 0x1B
7800#define I40E_NVM_OEM_LENGTH_OFFSET 0x00
7801#define I40E_NVM_OEM_CAPABILITIES_OFFSET 0x01
7802#define I40E_NVM_OEM_GEN_OFFSET 0x02
7803#define I40E_NVM_OEM_RELEASE_OFFSET 0x03
7804#define I40E_NVM_OEM_CAPABILITIES_MASK 0x000F
7805#define I40E_NVM_OEM_LENGTH 3
7806
7807 /* Check if pointer to OEM version block is valid. */
7808 i40e_read_nvm_word(hw, I40E_SR_NVM_OEM_VERSION_PTR, &block_offset);
7809 if (block_offset == 0xffff)
7810 return;
7811
7812 /* Check if OEM version block has correct length. */
7813 i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_LENGTH_OFFSET,
7814 &block_length);
7815 if (block_length < I40E_NVM_OEM_LENGTH)
7816 return;
7817
7818 /* Check if OEM version format is as expected. */
7819 i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_CAPABILITIES_OFFSET,
7820 &capabilities);
7821 if ((capabilities & I40E_NVM_OEM_CAPABILITIES_MASK) != 0)
7822 return;
7823
7824 i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_GEN_OFFSET,
7825 &gen_snap);
7826 i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_RELEASE_OFFSET,
7827 &release);
7828 hw->nvm.oem_ver = (gen_snap << I40E_OEM_SNAP_SHIFT) | release;
7829 hw->nvm.eetrack = I40E_OEM_EETRACK_ID;
7830}
7831
7832/**
Maciej Sosin373149f2017-04-05 07:50:55 -04007833 * i40e_reset - wait for core reset to finish reset, reset pf if corer not seen
7834 * @pf: board private structure
7835 **/
7836static int i40e_reset(struct i40e_pf *pf)
7837{
7838 struct i40e_hw *hw = &pf->hw;
7839 i40e_status ret;
7840
7841 ret = i40e_pf_reset(hw);
7842 if (ret) {
7843 dev_info(&pf->pdev->dev, "PF reset failed, %d\n", ret);
Jacob Keller0da36b92017-04-19 09:25:55 -04007844 set_bit(__I40E_RESET_FAILED, pf->state);
7845 clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
Maciej Sosin373149f2017-04-05 07:50:55 -04007846 } else {
7847 pf->pfr_count++;
7848 }
7849 return ret;
7850}
7851
7852/**
7853 * i40e_rebuild - rebuild using a saved config
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007854 * @pf: board private structure
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007855 * @reinit: if the Main VSI needs to re-initialized.
Maciej Sosin373149f2017-04-05 07:50:55 -04007856 * @lock_acquired: indicates whether or not the lock has been acquired
7857 * before this function was called.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007858 **/
Maciej Sosin373149f2017-04-05 07:50:55 -04007859static void i40e_rebuild(struct i40e_pf *pf, bool reinit, bool lock_acquired)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007860{
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007861 struct i40e_hw *hw = &pf->hw;
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00007862 u8 set_fc_aq_fail = 0;
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007863 i40e_status ret;
Anjali Singhai Jain4f2f017c2015-10-21 19:47:07 -04007864 u32 val;
Maciej Sosin373149f2017-04-05 07:50:55 -04007865 int v;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007866
Mauro S. M. Rodrigues9e6c9c02017-05-12 23:26:56 -03007867 if (test_bit(__I40E_DOWN, pf->state))
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00007868 goto clear_recovery;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00007869 dev_dbg(&pf->pdev->dev, "Rebuilding internal switch\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007870
7871 /* rebuild the basics for the AdminQ, HMC, and initial HW switch */
7872 ret = i40e_init_adminq(&pf->hw);
7873 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04007874 dev_info(&pf->pdev->dev, "Rebuild AdminQ failed, err %s aq_err %s\n",
7875 i40e_stat_str(&pf->hw, ret),
7876 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00007877 goto clear_recovery;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007878 }
Filip Sadowski5bbb2e22017-06-07 05:43:09 -04007879 i40e_get_oem_version(&pf->hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007880
Shannon Nelson4eb3f762014-03-06 08:59:58 +00007881 /* re-verify the eeprom if we just had an EMP reset */
Jacob Keller0da36b92017-04-19 09:25:55 -04007882 if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state))
Shannon Nelson4eb3f762014-03-06 08:59:58 +00007883 i40e_verify_eeprom(pf);
Shannon Nelson4eb3f762014-03-06 08:59:58 +00007884
Shannon Nelsone78ac4bf2014-05-10 04:49:09 +00007885 i40e_clear_pxe_mode(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007886 ret = i40e_get_capabilities(pf);
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04007887 if (ret)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007888 goto end_core_reset;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007889
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007890 ret = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
Alexander Duyckc76cb6e2017-02-21 15:55:48 -08007891 hw->func_caps.num_rx_qp, 0, 0);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007892 if (ret) {
7893 dev_info(&pf->pdev->dev, "init_lan_hmc failed: %d\n", ret);
7894 goto end_core_reset;
7895 }
7896 ret = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
7897 if (ret) {
7898 dev_info(&pf->pdev->dev, "configure_lan_hmc failed: %d\n", ret);
7899 goto end_core_reset;
7900 }
7901
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08007902#ifdef CONFIG_I40E_DCB
7903 ret = i40e_init_pf_dcb(pf);
7904 if (ret) {
Shannon Nelsonaebfc812014-12-11 07:06:38 +00007905 dev_info(&pf->pdev->dev, "DCB init failed %d, disabled\n", ret);
7906 pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
7907 /* Continue without DCB enabled */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08007908 }
7909#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007910 /* do basic switch setup */
Maciej Sosin373149f2017-04-05 07:50:55 -04007911 if (!lock_acquired)
7912 rtnl_lock();
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007913 ret = i40e_setup_pf_switch(pf, reinit);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007914 if (ret)
Maciej Sosin373149f2017-04-05 07:50:55 -04007915 goto end_unlock;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007916
Shannon Nelson2f0aff42016-01-04 10:33:08 -08007917 /* The driver only wants link up/down and module qualification
7918 * reports from firmware. Note the negative logic.
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +00007919 */
7920 ret = i40e_aq_set_phy_int_mask(&pf->hw,
Shannon Nelson2f0aff42016-01-04 10:33:08 -08007921 ~(I40E_AQ_EVENT_LINK_UPDOWN |
Shannon Nelson867a79e2016-03-18 12:18:15 -07007922 I40E_AQ_EVENT_MEDIA_NA |
Shannon Nelson2f0aff42016-01-04 10:33:08 -08007923 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +00007924 if (ret)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04007925 dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
7926 i40e_stat_str(&pf->hw, ret),
7927 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +00007928
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00007929 /* make sure our flow control settings are restored */
7930 ret = i40e_set_fc(&pf->hw, &set_fc_aq_fail, true);
7931 if (ret)
Neerav Parikh8279e492015-09-03 17:18:50 -04007932 dev_dbg(&pf->pdev->dev, "setting flow control: ret = %s last_status = %s\n",
7933 i40e_stat_str(&pf->hw, ret),
7934 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00007935
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007936 /* Rebuild the VSIs and VEBs that existed before reset.
7937 * They are still in our local switch element arrays, so only
7938 * need to rebuild the switch model in the HW.
7939 *
7940 * If there were VEBs but the reconstitution failed, we'll try
7941 * try to recover minimal use by getting the basic PF VSI working.
7942 */
7943 if (pf->vsi[pf->lan_vsi]->uplink_seid != pf->mac_seid) {
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00007944 dev_dbg(&pf->pdev->dev, "attempting to rebuild switch\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007945 /* find the one VEB connected to the MAC, and find orphans */
7946 for (v = 0; v < I40E_MAX_VEB; v++) {
7947 if (!pf->veb[v])
7948 continue;
7949
7950 if (pf->veb[v]->uplink_seid == pf->mac_seid ||
7951 pf->veb[v]->uplink_seid == 0) {
7952 ret = i40e_reconstitute_veb(pf->veb[v]);
7953
7954 if (!ret)
7955 continue;
7956
7957 /* If Main VEB failed, we're in deep doodoo,
7958 * so give up rebuilding the switch and set up
7959 * for minimal rebuild of PF VSI.
7960 * If orphan failed, we'll report the error
7961 * but try to keep going.
7962 */
7963 if (pf->veb[v]->uplink_seid == pf->mac_seid) {
7964 dev_info(&pf->pdev->dev,
7965 "rebuild of switch failed: %d, will try to set up simple PF connection\n",
7966 ret);
7967 pf->vsi[pf->lan_vsi]->uplink_seid
7968 = pf->mac_seid;
7969 break;
7970 } else if (pf->veb[v]->uplink_seid == 0) {
7971 dev_info(&pf->pdev->dev,
7972 "rebuild of orphan VEB failed: %d\n",
7973 ret);
7974 }
7975 }
7976 }
7977 }
7978
7979 if (pf->vsi[pf->lan_vsi]->uplink_seid == pf->mac_seid) {
Shannon Nelsoncde4cbc2014-06-04 01:23:17 +00007980 dev_dbg(&pf->pdev->dev, "attempting to rebuild PF VSI\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007981 /* no VEB, so rebuild only the Main VSI */
7982 ret = i40e_add_vsi(pf->vsi[pf->lan_vsi]);
7983 if (ret) {
7984 dev_info(&pf->pdev->dev,
7985 "rebuild of Main VSI failed: %d\n", ret);
Maciej Sosin373149f2017-04-05 07:50:55 -04007986 goto end_unlock;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007987 }
7988 }
7989
Amritha Nambiar8f88b302017-09-07 04:00:17 -07007990 /* PF Main VSI is rebuild by now, go ahead and rebuild channel VSIs
7991 * for this main VSI if they exist
7992 */
7993 ret = i40e_rebuild_channels(pf->vsi[pf->lan_vsi]);
7994 if (ret)
7995 goto end_unlock;
7996
Anjali Singhai Jain4f2f017c2015-10-21 19:47:07 -04007997 /* Reconfigure hardware for allowing smaller MSS in the case
7998 * of TSO, so that we avoid the MDD being fired and causing
7999 * a reset in the case of small MSS+TSO.
8000 */
8001#define I40E_REG_MSS 0x000E64DC
8002#define I40E_REG_MSS_MIN_MASK 0x3FF0000
8003#define I40E_64BYTE_MSS 0x400000
8004 val = rd32(hw, I40E_REG_MSS);
8005 if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
8006 val &= ~I40E_REG_MSS_MIN_MASK;
8007 val |= I40E_64BYTE_MSS;
8008 wr32(hw, I40E_REG_MSS, val);
8009 }
8010
Jacob Kellerd36e41d2017-06-23 04:24:46 -04008011 if (pf->hw_features & I40E_HW_RESTART_AUTONEG) {
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +00008012 msleep(75);
8013 ret = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
8014 if (ret)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008015 dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
8016 i40e_stat_str(&pf->hw, ret),
8017 i40e_aq_str(&pf->hw,
8018 pf->hw.aq.asq_last_status));
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00008019 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008020 /* reinit the misc interrupt */
8021 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
8022 ret = i40e_setup_misc_vector(pf);
8023
Anjali Singhai Jaine7358f52015-10-01 14:37:34 -04008024 /* Add a filter to drop all Flow control frames from any VSI from being
8025 * transmitted. By doing so we stop a malicious VF from sending out
8026 * PAUSE or PFC frames and potentially controlling traffic for other
8027 * PF/VF VSIs.
8028 * The FW can still send Flow control frames if enabled.
8029 */
8030 i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
8031 pf->main_vsi_seid);
8032
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008033 /* restart the VSIs that were rebuilt and running before the reset */
8034 i40e_pf_unquiesce_all_vsi(pf);
8035
Jacob Keller024b05f2017-04-13 04:45:46 -04008036 /* Release the RTNL lock before we start resetting VFs */
8037 if (!lock_acquired)
8038 rtnl_unlock();
8039
Jacob Kellere4b433f2017-04-13 04:45:52 -04008040 i40e_reset_all_vfs(pf, true);
Mitch Williams69f64b22014-02-13 03:48:46 -08008041
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008042 /* tell the firmware that we're starting */
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00008043 i40e_send_version(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008044
Jacob Keller024b05f2017-04-13 04:45:46 -04008045 /* We've already released the lock, so don't do it again */
8046 goto end_core_reset;
8047
Maciej Sosin373149f2017-04-05 07:50:55 -04008048end_unlock:
Jacob Keller024b05f2017-04-13 04:45:46 -04008049 if (!lock_acquired)
8050 rtnl_unlock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008051end_core_reset:
Jacob Keller0da36b92017-04-19 09:25:55 -04008052 clear_bit(__I40E_RESET_FAILED, pf->state);
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00008053clear_recovery:
Jacob Keller0da36b92017-04-19 09:25:55 -04008054 clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008055}
8056
8057/**
Maciej Sosin373149f2017-04-05 07:50:55 -04008058 * i40e_reset_and_rebuild - reset and rebuild using a saved config
8059 * @pf: board private structure
8060 * @reinit: if the Main VSI needs to re-initialized.
8061 * @lock_acquired: indicates whether or not the lock has been acquired
8062 * before this function was called.
8063 **/
8064static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit,
8065 bool lock_acquired)
8066{
8067 int ret;
8068 /* Now we wait for GRST to settle out.
8069 * We don't have to delete the VEBs or VSIs from the hw switch
8070 * because the reset will make them disappear.
8071 */
8072 ret = i40e_reset(pf);
8073 if (!ret)
8074 i40e_rebuild(pf, reinit, lock_acquired);
8075}
8076
8077/**
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00008078 * i40e_handle_reset_warning - prep for the PF to reset, reset and rebuild
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00008079 * @pf: board private structure
8080 *
8081 * Close up the VFs and other things in prep for a Core Reset,
8082 * then get ready to rebuild the world.
Maciej Sosin373149f2017-04-05 07:50:55 -04008083 * @lock_acquired: indicates whether or not the lock has been acquired
8084 * before this function was called.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00008085 **/
Maciej Sosin373149f2017-04-05 07:50:55 -04008086static void i40e_handle_reset_warning(struct i40e_pf *pf, bool lock_acquired)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00008087{
Maciej Sosin373149f2017-04-05 07:50:55 -04008088 i40e_prep_for_reset(pf, lock_acquired);
8089 i40e_reset_and_rebuild(pf, false, lock_acquired);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00008090}
8091
8092/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008093 * i40e_handle_mdd_event
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00008094 * @pf: pointer to the PF structure
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008095 *
8096 * Called from the MDD irq handler to identify possibly malicious vfs
8097 **/
8098static void i40e_handle_mdd_event(struct i40e_pf *pf)
8099{
8100 struct i40e_hw *hw = &pf->hw;
8101 bool mdd_detected = false;
Neerav Parikhdf430b12014-06-04 01:23:15 +00008102 bool pf_mdd_detected = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008103 struct i40e_vf *vf;
8104 u32 reg;
8105 int i;
8106
Jacob Keller0da36b92017-04-19 09:25:55 -04008107 if (!test_bit(__I40E_MDD_EVENT_PENDING, pf->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008108 return;
8109
8110 /* find what triggered the MDD event */
8111 reg = rd32(hw, I40E_GL_MDET_TX);
8112 if (reg & I40E_GL_MDET_TX_VALID_MASK) {
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00008113 u8 pf_num = (reg & I40E_GL_MDET_TX_PF_NUM_MASK) >>
8114 I40E_GL_MDET_TX_PF_NUM_SHIFT;
Mitch Williams2089ad02014-10-17 03:14:53 +00008115 u16 vf_num = (reg & I40E_GL_MDET_TX_VF_NUM_MASK) >>
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00008116 I40E_GL_MDET_TX_VF_NUM_SHIFT;
Dan Carpenter013f6572014-10-22 20:06:29 -07008117 u8 event = (reg & I40E_GL_MDET_TX_EVENT_MASK) >>
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00008118 I40E_GL_MDET_TX_EVENT_SHIFT;
Mitch Williams2089ad02014-10-17 03:14:53 +00008119 u16 queue = ((reg & I40E_GL_MDET_TX_QUEUE_MASK) >>
8120 I40E_GL_MDET_TX_QUEUE_SHIFT) -
8121 pf->hw.func_caps.base_queue;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00008122 if (netif_msg_tx_err(pf))
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00008123 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 +00008124 event, queue, pf_num, vf_num);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008125 wr32(hw, I40E_GL_MDET_TX, 0xffffffff);
8126 mdd_detected = true;
8127 }
8128 reg = rd32(hw, I40E_GL_MDET_RX);
8129 if (reg & I40E_GL_MDET_RX_VALID_MASK) {
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00008130 u8 func = (reg & I40E_GL_MDET_RX_FUNCTION_MASK) >>
8131 I40E_GL_MDET_RX_FUNCTION_SHIFT;
Dan Carpenter013f6572014-10-22 20:06:29 -07008132 u8 event = (reg & I40E_GL_MDET_RX_EVENT_MASK) >>
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00008133 I40E_GL_MDET_RX_EVENT_SHIFT;
Mitch Williams2089ad02014-10-17 03:14:53 +00008134 u16 queue = ((reg & I40E_GL_MDET_RX_QUEUE_MASK) >>
8135 I40E_GL_MDET_RX_QUEUE_SHIFT) -
8136 pf->hw.func_caps.base_queue;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00008137 if (netif_msg_rx_err(pf))
8138 dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on RX queue %d of function 0x%02x\n",
8139 event, queue, func);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008140 wr32(hw, I40E_GL_MDET_RX, 0xffffffff);
8141 mdd_detected = true;
8142 }
8143
Neerav Parikhdf430b12014-06-04 01:23:15 +00008144 if (mdd_detected) {
8145 reg = rd32(hw, I40E_PF_MDET_TX);
8146 if (reg & I40E_PF_MDET_TX_VALID_MASK) {
8147 wr32(hw, I40E_PF_MDET_TX, 0xFFFF);
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00008148 dev_info(&pf->pdev->dev, "TX driver issue detected, PF reset issued\n");
Neerav Parikhdf430b12014-06-04 01:23:15 +00008149 pf_mdd_detected = true;
8150 }
8151 reg = rd32(hw, I40E_PF_MDET_RX);
8152 if (reg & I40E_PF_MDET_RX_VALID_MASK) {
8153 wr32(hw, I40E_PF_MDET_RX, 0xFFFF);
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00008154 dev_info(&pf->pdev->dev, "RX driver issue detected, PF reset issued\n");
Neerav Parikhdf430b12014-06-04 01:23:15 +00008155 pf_mdd_detected = true;
8156 }
8157 /* Queue belongs to the PF, initiate a reset */
8158 if (pf_mdd_detected) {
Jacob Keller0da36b92017-04-19 09:25:55 -04008159 set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
Neerav Parikhdf430b12014-06-04 01:23:15 +00008160 i40e_service_event_schedule(pf);
8161 }
8162 }
8163
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008164 /* see if one of the VFs needs its hand slapped */
8165 for (i = 0; i < pf->num_alloc_vfs && mdd_detected; i++) {
8166 vf = &(pf->vf[i]);
8167 reg = rd32(hw, I40E_VP_MDET_TX(i));
8168 if (reg & I40E_VP_MDET_TX_VALID_MASK) {
8169 wr32(hw, I40E_VP_MDET_TX(i), 0xFFFF);
8170 vf->num_mdd_events++;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00008171 dev_info(&pf->pdev->dev, "TX driver issue detected on VF %d\n",
8172 i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008173 }
8174
8175 reg = rd32(hw, I40E_VP_MDET_RX(i));
8176 if (reg & I40E_VP_MDET_RX_VALID_MASK) {
8177 wr32(hw, I40E_VP_MDET_RX(i), 0xFFFF);
8178 vf->num_mdd_events++;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00008179 dev_info(&pf->pdev->dev, "RX driver issue detected on VF %d\n",
8180 i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008181 }
8182
8183 if (vf->num_mdd_events > I40E_DEFAULT_NUM_MDD_EVENTS_ALLOWED) {
8184 dev_info(&pf->pdev->dev,
8185 "Too many MDD events on VF %d, disabled\n", i);
8186 dev_info(&pf->pdev->dev,
8187 "Use PF Control I/F to re-enable the VF\n");
Jacob Keller6322e632017-04-13 04:45:54 -04008188 set_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008189 }
8190 }
8191
8192 /* re-enable mdd interrupt cause */
Jacob Keller0da36b92017-04-19 09:25:55 -04008193 clear_bit(__I40E_MDD_EVENT_PENDING, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008194 reg = rd32(hw, I40E_PFINT_ICR0_ENA);
8195 reg |= I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
8196 wr32(hw, I40E_PFINT_ICR0_ENA, reg);
8197 i40e_flush(hw);
8198}
8199
Jacob Kellerd8b2c702017-06-20 15:17:00 -07008200static const char *i40e_tunnel_name(struct i40e_udp_port_config *port)
8201{
8202 switch (port->type) {
8203 case UDP_TUNNEL_TYPE_VXLAN:
8204 return "vxlan";
8205 case UDP_TUNNEL_TYPE_GENEVE:
8206 return "geneve";
8207 default:
8208 return "unknown";
8209 }
8210}
8211
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008212/**
Alexander Duyck1f190d92017-04-19 09:25:51 -04008213 * i40e_sync_udp_filters - Trigger a sync event for existing UDP filters
8214 * @pf: board private structure
8215 **/
8216static void i40e_sync_udp_filters(struct i40e_pf *pf)
8217{
8218 int i;
8219
8220 /* loop through and set pending bit for all active UDP filters */
8221 for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
8222 if (pf->udp_ports[i].port)
8223 pf->pending_udp_bitmap |= BIT_ULL(i);
8224 }
8225
8226 pf->flags |= I40E_FLAG_UDP_FILTER_SYNC;
8227}
8228
8229/**
Singhai, Anjali6a899022015-12-14 12:21:18 -08008230 * i40e_sync_udp_filters_subtask - Sync the VSI filter list with HW
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008231 * @pf: board private structure
8232 **/
Singhai, Anjali6a899022015-12-14 12:21:18 -08008233static void i40e_sync_udp_filters_subtask(struct i40e_pf *pf)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008234{
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008235 struct i40e_hw *hw = &pf->hw;
8236 i40e_status ret;
Jacob Kellerfe0b0cd2017-02-06 14:38:38 -08008237 u16 port;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008238 int i;
8239
Singhai, Anjali6a899022015-12-14 12:21:18 -08008240 if (!(pf->flags & I40E_FLAG_UDP_FILTER_SYNC))
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008241 return;
8242
Singhai, Anjali6a899022015-12-14 12:21:18 -08008243 pf->flags &= ~I40E_FLAG_UDP_FILTER_SYNC;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008244
8245 for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
Singhai, Anjali6a899022015-12-14 12:21:18 -08008246 if (pf->pending_udp_bitmap & BIT_ULL(i)) {
8247 pf->pending_udp_bitmap &= ~BIT_ULL(i);
Jacob Keller27826fd2017-04-19 09:25:50 -04008248 port = pf->udp_ports[i].port;
Shannon Nelsonc22c06c2015-03-31 00:45:04 -07008249 if (port)
Carolyn Wybornyb3f5c7b2016-08-24 11:33:51 -07008250 ret = i40e_aq_add_udp_tunnel(hw, port,
8251 pf->udp_ports[i].type,
8252 NULL, NULL);
Shannon Nelsonc22c06c2015-03-31 00:45:04 -07008253 else
8254 ret = i40e_aq_del_udp_tunnel(hw, i, NULL);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008255
8256 if (ret) {
Jacob Kellerd8b2c702017-06-20 15:17:00 -07008257 dev_info(&pf->pdev->dev,
8258 "%s %s port %d, index %d failed, err %s aq_err %s\n",
8259 i40e_tunnel_name(&pf->udp_ports[i]),
8260 port ? "add" : "delete",
8261 port, i,
8262 i40e_stat_str(&pf->hw, ret),
8263 i40e_aq_str(&pf->hw,
8264 pf->hw.aq.asq_last_status));
Jacob Keller27826fd2017-04-19 09:25:50 -04008265 pf->udp_ports[i].port = 0;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008266 }
8267 }
8268 }
8269}
8270
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008271/**
8272 * i40e_service_task - Run the driver's async subtasks
8273 * @work: pointer to work_struct containing our data
8274 **/
8275static void i40e_service_task(struct work_struct *work)
8276{
8277 struct i40e_pf *pf = container_of(work,
8278 struct i40e_pf,
8279 service_task);
8280 unsigned long start_time = jiffies;
8281
Shannon Nelsone57a2fe2014-06-03 23:50:19 +00008282 /* don't bother with service tasks if a reset is in progress */
Jacob Keller0da36b92017-04-19 09:25:55 -04008283 if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
Shannon Nelsone57a2fe2014-06-03 23:50:19 +00008284 return;
Shannon Nelsone57a2fe2014-06-03 23:50:19 +00008285
Jacob Keller0da36b92017-04-19 09:25:55 -04008286 if (test_and_set_bit(__I40E_SERVICE_SCHED, pf->state))
Mitch Williams91089032016-11-21 13:03:51 -08008287 return;
8288
Kiran Patilb03a8c12015-09-24 18:13:15 -04008289 i40e_detect_recover_hung(pf);
Jesse Brandeburg2818ccd2016-01-13 16:51:38 -08008290 i40e_sync_filters_subtask(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008291 i40e_reset_subtask(pf);
8292 i40e_handle_mdd_event(pf);
8293 i40e_vc_process_vflr_event(pf);
8294 i40e_watchdog_subtask(pf);
8295 i40e_fdir_reinit_subtask(pf);
Mitch Williams0ef2d5a2017-01-24 10:24:00 -08008296 if (pf->flags & I40E_FLAG_CLIENT_RESET) {
8297 /* Client subtask will reopen next time through. */
8298 i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], true);
8299 pf->flags &= ~I40E_FLAG_CLIENT_RESET;
8300 } else {
8301 i40e_client_subtask(pf);
8302 if (pf->flags & I40E_FLAG_CLIENT_L2_CHANGE) {
8303 i40e_notify_client_of_l2_param_changes(
8304 pf->vsi[pf->lan_vsi]);
8305 pf->flags &= ~I40E_FLAG_CLIENT_L2_CHANGE;
8306 }
8307 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008308 i40e_sync_filters_subtask(pf);
Singhai, Anjali6a899022015-12-14 12:21:18 -08008309 i40e_sync_udp_filters_subtask(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008310 i40e_clean_adminq_subtask(pf);
8311
Mitch Williams91089032016-11-21 13:03:51 -08008312 /* flush memory to make sure state is correct before next watchdog */
8313 smp_mb__before_atomic();
Jacob Keller0da36b92017-04-19 09:25:55 -04008314 clear_bit(__I40E_SERVICE_SCHED, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008315
8316 /* If the tasks have taken longer than one timer cycle or there
8317 * is more work to be done, reschedule the service task now
8318 * rather than wait for the timer to tick again.
8319 */
8320 if (time_after(jiffies, (start_time + pf->service_timer_period)) ||
Jacob Keller0da36b92017-04-19 09:25:55 -04008321 test_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state) ||
8322 test_bit(__I40E_MDD_EVENT_PENDING, pf->state) ||
8323 test_bit(__I40E_VFLR_EVENT_PENDING, pf->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008324 i40e_service_event_schedule(pf);
8325}
8326
8327/**
8328 * i40e_service_timer - timer callback
8329 * @data: pointer to PF struct
8330 **/
8331static void i40e_service_timer(unsigned long data)
8332{
8333 struct i40e_pf *pf = (struct i40e_pf *)data;
8334
8335 mod_timer(&pf->service_timer,
8336 round_jiffies(jiffies + pf->service_timer_period));
8337 i40e_service_event_schedule(pf);
8338}
8339
8340/**
8341 * i40e_set_num_rings_in_vsi - Determine number of rings in the VSI
8342 * @vsi: the VSI being configured
8343 **/
8344static int i40e_set_num_rings_in_vsi(struct i40e_vsi *vsi)
8345{
8346 struct i40e_pf *pf = vsi->back;
8347
8348 switch (vsi->type) {
8349 case I40E_VSI_MAIN:
8350 vsi->alloc_queue_pairs = pf->num_lan_qps;
8351 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
8352 I40E_REQ_DESCRIPTOR_MULTIPLE);
8353 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
8354 vsi->num_q_vectors = pf->num_lan_msix;
8355 else
8356 vsi->num_q_vectors = 1;
8357
8358 break;
8359
8360 case I40E_VSI_FDIR:
8361 vsi->alloc_queue_pairs = 1;
8362 vsi->num_desc = ALIGN(I40E_FDIR_RING_COUNT,
8363 I40E_REQ_DESCRIPTOR_MULTIPLE);
Tushar Davea70e4072016-05-16 12:40:53 -07008364 vsi->num_q_vectors = pf->num_fdsb_msix;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008365 break;
8366
8367 case I40E_VSI_VMDQ2:
8368 vsi->alloc_queue_pairs = pf->num_vmdq_qps;
8369 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
8370 I40E_REQ_DESCRIPTOR_MULTIPLE);
8371 vsi->num_q_vectors = pf->num_vmdq_msix;
8372 break;
8373
8374 case I40E_VSI_SRIOV:
8375 vsi->alloc_queue_pairs = pf->num_vf_qps;
8376 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
8377 I40E_REQ_DESCRIPTOR_MULTIPLE);
8378 break;
8379
8380 default:
8381 WARN_ON(1);
8382 return -ENODATA;
8383 }
8384
8385 return 0;
8386}
8387
8388/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00008389 * i40e_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the vsi
Rami Rosen3d7d7a82017-09-16 05:49:48 +03008390 * @vsi: VSI pointer
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00008391 * @alloc_qvectors: a bool to specify if q_vectors need to be allocated.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00008392 *
8393 * On error: returns error code (negative)
8394 * On success: returns 0
8395 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00008396static int i40e_vsi_alloc_arrays(struct i40e_vsi *vsi, bool alloc_qvectors)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00008397{
Björn Töpel74608d12017-05-24 07:55:35 +02008398 struct i40e_ring **next_rings;
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00008399 int size;
8400 int ret = 0;
8401
Björn Töpel74608d12017-05-24 07:55:35 +02008402 /* allocate memory for both Tx, XDP Tx and Rx ring pointers */
8403 size = sizeof(struct i40e_ring *) * vsi->alloc_queue_pairs *
8404 (i40e_enabled_xdp_vsi(vsi) ? 3 : 2);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00008405 vsi->tx_rings = kzalloc(size, GFP_KERNEL);
8406 if (!vsi->tx_rings)
8407 return -ENOMEM;
Björn Töpel74608d12017-05-24 07:55:35 +02008408 next_rings = vsi->tx_rings + vsi->alloc_queue_pairs;
8409 if (i40e_enabled_xdp_vsi(vsi)) {
8410 vsi->xdp_rings = next_rings;
8411 next_rings += vsi->alloc_queue_pairs;
8412 }
8413 vsi->rx_rings = next_rings;
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00008414
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00008415 if (alloc_qvectors) {
8416 /* allocate memory for q_vector pointers */
Julia Lawallf57e4fb2014-07-30 03:11:09 +00008417 size = sizeof(struct i40e_q_vector *) * vsi->num_q_vectors;
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00008418 vsi->q_vectors = kzalloc(size, GFP_KERNEL);
8419 if (!vsi->q_vectors) {
8420 ret = -ENOMEM;
8421 goto err_vectors;
8422 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00008423 }
8424 return ret;
8425
8426err_vectors:
8427 kfree(vsi->tx_rings);
8428 return ret;
8429}
8430
8431/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008432 * i40e_vsi_mem_alloc - Allocates the next available struct vsi in the PF
8433 * @pf: board private structure
8434 * @type: type of VSI
8435 *
8436 * On error: returns error code (negative)
8437 * On success: returns vsi index in PF (positive)
8438 **/
8439static int i40e_vsi_mem_alloc(struct i40e_pf *pf, enum i40e_vsi_type type)
8440{
8441 int ret = -ENODEV;
8442 struct i40e_vsi *vsi;
8443 int vsi_idx;
8444 int i;
8445
8446 /* Need to protect the allocation of the VSIs at the PF level */
8447 mutex_lock(&pf->switch_mutex);
8448
8449 /* VSI list may be fragmented if VSI creation/destruction has
8450 * been happening. We can afford to do a quick scan to look
8451 * for any free VSIs in the list.
8452 *
8453 * find next empty vsi slot, looping back around if necessary
8454 */
8455 i = pf->next_vsi;
Mitch Williams505682c2014-05-20 08:01:37 +00008456 while (i < pf->num_alloc_vsi && pf->vsi[i])
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008457 i++;
Mitch Williams505682c2014-05-20 08:01:37 +00008458 if (i >= pf->num_alloc_vsi) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008459 i = 0;
8460 while (i < pf->next_vsi && pf->vsi[i])
8461 i++;
8462 }
8463
Mitch Williams505682c2014-05-20 08:01:37 +00008464 if (i < pf->num_alloc_vsi && !pf->vsi[i]) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008465 vsi_idx = i; /* Found one! */
8466 } else {
8467 ret = -ENODEV;
Alexander Duyck493fb302013-09-28 07:01:44 +00008468 goto unlock_pf; /* out of VSI slots! */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008469 }
8470 pf->next_vsi = ++i;
8471
8472 vsi = kzalloc(sizeof(*vsi), GFP_KERNEL);
8473 if (!vsi) {
8474 ret = -ENOMEM;
Alexander Duyck493fb302013-09-28 07:01:44 +00008475 goto unlock_pf;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008476 }
8477 vsi->type = type;
8478 vsi->back = pf;
Jacob Keller0da36b92017-04-19 09:25:55 -04008479 set_bit(__I40E_VSI_DOWN, vsi->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008480 vsi->flags = 0;
8481 vsi->idx = vsi_idx;
Jesse Brandeburgac26fc12015-09-28 14:12:37 -04008482 vsi->int_rate_limit = 0;
Anjali Singhai Jain5db4cb52015-02-24 06:58:49 +00008483 vsi->rss_table_size = (vsi->type == I40E_VSI_MAIN) ?
8484 pf->rss_table_size : 64;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008485 vsi->netdev_registered = false;
8486 vsi->work_limit = I40E_DEFAULT_IRQ_WORK;
Jacob Keller278e7d02016-10-05 09:30:37 -07008487 hash_init(vsi->mac_filter_hash);
Shannon Nelson63741842014-04-23 04:50:16 +00008488 vsi->irqs_ready = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008489
Alexander Duyck9f65e152013-09-28 06:00:58 +00008490 ret = i40e_set_num_rings_in_vsi(vsi);
8491 if (ret)
8492 goto err_rings;
8493
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00008494 ret = i40e_vsi_alloc_arrays(vsi, true);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00008495 if (ret)
Alexander Duyck9f65e152013-09-28 06:00:58 +00008496 goto err_rings;
Alexander Duyck493fb302013-09-28 07:01:44 +00008497
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008498 /* Setup default MSIX irq handler for VSI */
8499 i40e_vsi_setup_irqhandler(vsi, i40e_msix_clean_rings);
8500
Kiran Patil21659032015-09-30 14:09:03 -04008501 /* Initialize VSI lock */
Jacob Keller278e7d02016-10-05 09:30:37 -07008502 spin_lock_init(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008503 pf->vsi[vsi_idx] = vsi;
8504 ret = vsi_idx;
Alexander Duyck493fb302013-09-28 07:01:44 +00008505 goto unlock_pf;
8506
Alexander Duyck9f65e152013-09-28 06:00:58 +00008507err_rings:
Alexander Duyck493fb302013-09-28 07:01:44 +00008508 pf->next_vsi = i - 1;
8509 kfree(vsi);
8510unlock_pf:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008511 mutex_unlock(&pf->switch_mutex);
8512 return ret;
8513}
8514
8515/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00008516 * i40e_vsi_free_arrays - Free queue and vector pointer arrays for the VSI
8517 * @type: VSI pointer
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00008518 * @free_qvectors: a bool to specify if q_vectors need to be freed.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00008519 *
8520 * On error: returns error code (negative)
8521 * On success: returns 0
8522 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00008523static void i40e_vsi_free_arrays(struct i40e_vsi *vsi, bool free_qvectors)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00008524{
8525 /* free the ring and vector containers */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00008526 if (free_qvectors) {
8527 kfree(vsi->q_vectors);
8528 vsi->q_vectors = NULL;
8529 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00008530 kfree(vsi->tx_rings);
8531 vsi->tx_rings = NULL;
8532 vsi->rx_rings = NULL;
Björn Töpel74608d12017-05-24 07:55:35 +02008533 vsi->xdp_rings = NULL;
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00008534}
8535
8536/**
Helin Zhang28c58692015-10-26 19:44:27 -04008537 * i40e_clear_rss_config_user - clear the user configured RSS hash keys
8538 * and lookup table
8539 * @vsi: Pointer to VSI structure
8540 */
8541static void i40e_clear_rss_config_user(struct i40e_vsi *vsi)
8542{
8543 if (!vsi)
8544 return;
8545
8546 kfree(vsi->rss_hkey_user);
8547 vsi->rss_hkey_user = NULL;
8548
8549 kfree(vsi->rss_lut_user);
8550 vsi->rss_lut_user = NULL;
8551}
8552
8553/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008554 * i40e_vsi_clear - Deallocate the VSI provided
8555 * @vsi: the VSI being un-configured
8556 **/
8557static int i40e_vsi_clear(struct i40e_vsi *vsi)
8558{
8559 struct i40e_pf *pf;
8560
8561 if (!vsi)
8562 return 0;
8563
8564 if (!vsi->back)
8565 goto free_vsi;
8566 pf = vsi->back;
8567
8568 mutex_lock(&pf->switch_mutex);
8569 if (!pf->vsi[vsi->idx]) {
8570 dev_err(&pf->pdev->dev, "pf->vsi[%d] is NULL, just free vsi[%d](%p,type %d)\n",
8571 vsi->idx, vsi->idx, vsi, vsi->type);
8572 goto unlock_vsi;
8573 }
8574
8575 if (pf->vsi[vsi->idx] != vsi) {
8576 dev_err(&pf->pdev->dev,
8577 "pf->vsi[%d](%p, type %d) != vsi[%d](%p,type %d): no free!\n",
8578 pf->vsi[vsi->idx]->idx,
8579 pf->vsi[vsi->idx],
8580 pf->vsi[vsi->idx]->type,
8581 vsi->idx, vsi, vsi->type);
8582 goto unlock_vsi;
8583 }
8584
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00008585 /* updates the PF for this cleared vsi */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008586 i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
8587 i40e_put_lump(pf->irq_pile, vsi->base_vector, vsi->idx);
8588
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00008589 i40e_vsi_free_arrays(vsi, true);
Helin Zhang28c58692015-10-26 19:44:27 -04008590 i40e_clear_rss_config_user(vsi);
Alexander Duyck493fb302013-09-28 07:01:44 +00008591
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008592 pf->vsi[vsi->idx] = NULL;
8593 if (vsi->idx < pf->next_vsi)
8594 pf->next_vsi = vsi->idx;
8595
8596unlock_vsi:
8597 mutex_unlock(&pf->switch_mutex);
8598free_vsi:
8599 kfree(vsi);
8600
8601 return 0;
8602}
8603
8604/**
Alexander Duyck9f65e152013-09-28 06:00:58 +00008605 * i40e_vsi_clear_rings - Deallocates the Rx and Tx rings for the provided VSI
8606 * @vsi: the VSI being cleaned
8607 **/
Shannon Nelsonbe1d5ee2013-11-28 06:39:23 +00008608static void i40e_vsi_clear_rings(struct i40e_vsi *vsi)
Alexander Duyck9f65e152013-09-28 06:00:58 +00008609{
8610 int i;
8611
Greg Rose8e9dca52013-12-18 13:45:53 +00008612 if (vsi->tx_rings && vsi->tx_rings[0]) {
Neerav Parikhd7397642013-11-28 06:39:37 +00008613 for (i = 0; i < vsi->alloc_queue_pairs; i++) {
Mitch Williams00403f02013-09-28 07:13:13 +00008614 kfree_rcu(vsi->tx_rings[i], rcu);
8615 vsi->tx_rings[i] = NULL;
8616 vsi->rx_rings[i] = NULL;
Björn Töpel74608d12017-05-24 07:55:35 +02008617 if (vsi->xdp_rings)
8618 vsi->xdp_rings[i] = NULL;
Mitch Williams00403f02013-09-28 07:13:13 +00008619 }
Shannon Nelsonbe1d5ee2013-11-28 06:39:23 +00008620 }
Alexander Duyck9f65e152013-09-28 06:00:58 +00008621}
8622
8623/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008624 * i40e_alloc_rings - Allocates the Rx and Tx rings for the provided VSI
8625 * @vsi: the VSI being configured
8626 **/
8627static int i40e_alloc_rings(struct i40e_vsi *vsi)
8628{
Björn Töpel74608d12017-05-24 07:55:35 +02008629 int i, qpv = i40e_enabled_xdp_vsi(vsi) ? 3 : 2;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008630 struct i40e_pf *pf = vsi->back;
Björn Töpel74608d12017-05-24 07:55:35 +02008631 struct i40e_ring *ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008632
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008633 /* Set basic values in the rings to be used later during open() */
Neerav Parikhd7397642013-11-28 06:39:37 +00008634 for (i = 0; i < vsi->alloc_queue_pairs; i++) {
Shannon Nelsonac6c5e32013-11-20 10:02:57 +00008635 /* allocate space for both Tx and Rx in one shot */
Björn Töpel74608d12017-05-24 07:55:35 +02008636 ring = kcalloc(qpv, sizeof(struct i40e_ring), GFP_KERNEL);
8637 if (!ring)
Alexander Duyck9f65e152013-09-28 06:00:58 +00008638 goto err_out;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008639
Björn Töpel74608d12017-05-24 07:55:35 +02008640 ring->queue_index = i;
8641 ring->reg_idx = vsi->base_queue + i;
8642 ring->ring_active = false;
8643 ring->vsi = vsi;
8644 ring->netdev = vsi->netdev;
8645 ring->dev = &pf->pdev->dev;
8646 ring->count = vsi->num_desc;
8647 ring->size = 0;
8648 ring->dcb_tc = 0;
Jacob Kellerd36e41d2017-06-23 04:24:46 -04008649 if (vsi->back->hw_features & I40E_HW_WB_ON_ITR_CAPABLE)
Björn Töpel74608d12017-05-24 07:55:35 +02008650 ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
8651 ring->tx_itr_setting = pf->tx_itr_default;
8652 vsi->tx_rings[i] = ring++;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008653
Björn Töpel74608d12017-05-24 07:55:35 +02008654 if (!i40e_enabled_xdp_vsi(vsi))
8655 goto setup_rx;
8656
8657 ring->queue_index = vsi->alloc_queue_pairs + i;
8658 ring->reg_idx = vsi->base_queue + ring->queue_index;
8659 ring->ring_active = false;
8660 ring->vsi = vsi;
8661 ring->netdev = NULL;
8662 ring->dev = &pf->pdev->dev;
8663 ring->count = vsi->num_desc;
8664 ring->size = 0;
8665 ring->dcb_tc = 0;
Jacob Kellerd36e41d2017-06-23 04:24:46 -04008666 if (vsi->back->hw_features & I40E_HW_WB_ON_ITR_CAPABLE)
Björn Töpel74608d12017-05-24 07:55:35 +02008667 ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
8668 set_ring_xdp(ring);
8669 ring->tx_itr_setting = pf->tx_itr_default;
8670 vsi->xdp_rings[i] = ring++;
8671
8672setup_rx:
8673 ring->queue_index = i;
8674 ring->reg_idx = vsi->base_queue + i;
8675 ring->ring_active = false;
8676 ring->vsi = vsi;
8677 ring->netdev = vsi->netdev;
8678 ring->dev = &pf->pdev->dev;
8679 ring->count = vsi->num_desc;
8680 ring->size = 0;
8681 ring->dcb_tc = 0;
8682 ring->rx_itr_setting = pf->rx_itr_default;
8683 vsi->rx_rings[i] = ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008684 }
8685
8686 return 0;
Alexander Duyck9f65e152013-09-28 06:00:58 +00008687
8688err_out:
8689 i40e_vsi_clear_rings(vsi);
8690 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008691}
8692
8693/**
8694 * i40e_reserve_msix_vectors - Reserve MSI-X vectors in the kernel
8695 * @pf: board private structure
8696 * @vectors: the number of MSI-X vectors to request
8697 *
8698 * Returns the number of vectors reserved, or error
8699 **/
8700static int i40e_reserve_msix_vectors(struct i40e_pf *pf, int vectors)
8701{
Alexander Gordeev7b37f372014-02-18 11:11:42 +01008702 vectors = pci_enable_msix_range(pf->pdev, pf->msix_entries,
8703 I40E_MIN_MSIX, vectors);
8704 if (vectors < 0) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008705 dev_info(&pf->pdev->dev,
Alexander Gordeev7b37f372014-02-18 11:11:42 +01008706 "MSI-X vector reservation failed: %d\n", vectors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008707 vectors = 0;
8708 }
8709
8710 return vectors;
8711}
8712
8713/**
8714 * i40e_init_msix - Setup the MSIX capability
8715 * @pf: board private structure
8716 *
8717 * Work with the OS to set up the MSIX vectors needed.
8718 *
Shannon Nelson3b444392015-02-26 16:15:57 +00008719 * Returns the number of vectors reserved or negative on failure
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008720 **/
8721static int i40e_init_msix(struct i40e_pf *pf)
8722{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008723 struct i40e_hw *hw = &pf->hw;
Jacob Kellerc0cf70a2017-01-24 10:24:01 -08008724 int cpus, extra_vectors;
Shannon Nelson1e200e42015-02-27 09:15:24 +00008725 int vectors_left;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008726 int v_budget, i;
Shannon Nelson3b444392015-02-26 16:15:57 +00008727 int v_actual;
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06008728 int iwarp_requested = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008729
8730 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
8731 return -ENODEV;
8732
8733 /* The number of vectors we'll request will be comprised of:
8734 * - Add 1 for "other" cause for Admin Queue events, etc.
8735 * - The number of LAN queue pairs
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008736 * - Queues being used for RSS.
8737 * We don't need as many as max_rss_size vectors.
8738 * use rss_size instead in the calculation since that
8739 * is governed by number of cpus in the system.
8740 * - assumes symmetric Tx/Rx pairing
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008741 * - The number of VMDq pairs
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06008742 * - The CPU count within the NUMA node if iWARP is enabled
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008743 * Once we count this up, try the request.
8744 *
8745 * If we can't get what we want, we'll simplify to nearly nothing
8746 * and try again. If that still fails, we punt.
8747 */
Shannon Nelson1e200e42015-02-27 09:15:24 +00008748 vectors_left = hw->func_caps.num_msix_vectors;
8749 v_budget = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008750
Shannon Nelson1e200e42015-02-27 09:15:24 +00008751 /* reserve one vector for miscellaneous handler */
8752 if (vectors_left) {
8753 v_budget++;
8754 vectors_left--;
8755 }
8756
Jacob Kellerc0cf70a2017-01-24 10:24:01 -08008757 /* reserve some vectors for the main PF traffic queues. Initially we
8758 * only reserve at most 50% of the available vectors, in the case that
8759 * the number of online CPUs is large. This ensures that we can enable
8760 * extra features as well. Once we've enabled the other features, we
8761 * will use any remaining vectors to reach as close as we can to the
8762 * number of online CPUs.
8763 */
8764 cpus = num_online_cpus();
8765 pf->num_lan_msix = min_t(int, cpus, vectors_left / 2);
Shannon Nelson1e200e42015-02-27 09:15:24 +00008766 vectors_left -= pf->num_lan_msix;
Shannon Nelson1e200e42015-02-27 09:15:24 +00008767
8768 /* reserve one vector for sideband flow director */
8769 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
8770 if (vectors_left) {
Tushar Davea70e4072016-05-16 12:40:53 -07008771 pf->num_fdsb_msix = 1;
Shannon Nelson1e200e42015-02-27 09:15:24 +00008772 v_budget++;
8773 vectors_left--;
8774 } else {
Tushar Davea70e4072016-05-16 12:40:53 -07008775 pf->num_fdsb_msix = 0;
Shannon Nelson1e200e42015-02-27 09:15:24 +00008776 }
8777 }
John W Linville83840e42015-01-14 03:06:28 +00008778
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06008779 /* can we reserve enough for iWARP? */
8780 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02008781 iwarp_requested = pf->num_iwarp_msix;
8782
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06008783 if (!vectors_left)
8784 pf->num_iwarp_msix = 0;
8785 else if (vectors_left < pf->num_iwarp_msix)
8786 pf->num_iwarp_msix = 1;
8787 v_budget += pf->num_iwarp_msix;
8788 vectors_left -= pf->num_iwarp_msix;
8789 }
8790
Shannon Nelson1e200e42015-02-27 09:15:24 +00008791 /* any vectors left over go for VMDq support */
8792 if (pf->flags & I40E_FLAG_VMDQ_ENABLED) {
8793 int vmdq_vecs_wanted = pf->num_vmdq_vsis * pf->num_vmdq_qps;
8794 int vmdq_vecs = min_t(int, vectors_left, vmdq_vecs_wanted);
8795
Stefan Assmann9ca57e92016-09-19 13:37:49 +02008796 if (!vectors_left) {
8797 pf->num_vmdq_msix = 0;
8798 pf->num_vmdq_qps = 0;
8799 } else {
8800 /* if we're short on vectors for what's desired, we limit
8801 * the queues per vmdq. If this is still more than are
8802 * available, the user will need to change the number of
8803 * queues/vectors used by the PF later with the ethtool
8804 * channels command
8805 */
8806 if (vmdq_vecs < vmdq_vecs_wanted)
8807 pf->num_vmdq_qps = 1;
8808 pf->num_vmdq_msix = pf->num_vmdq_qps;
Shannon Nelson1e200e42015-02-27 09:15:24 +00008809
Stefan Assmann9ca57e92016-09-19 13:37:49 +02008810 v_budget += vmdq_vecs;
8811 vectors_left -= vmdq_vecs;
8812 }
Shannon Nelson1e200e42015-02-27 09:15:24 +00008813 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008814
Jacob Kellerc0cf70a2017-01-24 10:24:01 -08008815 /* On systems with a large number of SMP cores, we previously limited
8816 * the number of vectors for num_lan_msix to be at most 50% of the
8817 * available vectors, to allow for other features. Now, we add back
8818 * the remaining vectors. However, we ensure that the total
8819 * num_lan_msix will not exceed num_online_cpus(). To do this, we
8820 * calculate the number of vectors we can add without going over the
8821 * cap of CPUs. For systems with a small number of CPUs this will be
8822 * zero.
8823 */
8824 extra_vectors = min_t(int, cpus - pf->num_lan_msix, vectors_left);
8825 pf->num_lan_msix += extra_vectors;
8826 vectors_left -= extra_vectors;
8827
8828 WARN(vectors_left < 0,
8829 "Calculation of remaining vectors underflowed. This is an accounting bug when determining total MSI-X vectors.\n");
8830
8831 v_budget += pf->num_lan_msix;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008832 pf->msix_entries = kcalloc(v_budget, sizeof(struct msix_entry),
8833 GFP_KERNEL);
8834 if (!pf->msix_entries)
8835 return -ENOMEM;
8836
8837 for (i = 0; i < v_budget; i++)
8838 pf->msix_entries[i].entry = i;
Shannon Nelson3b444392015-02-26 16:15:57 +00008839 v_actual = i40e_reserve_msix_vectors(pf, v_budget);
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00008840
Shannon Nelson3b444392015-02-26 16:15:57 +00008841 if (v_actual < I40E_MIN_MSIX) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008842 pf->flags &= ~I40E_FLAG_MSIX_ENABLED;
8843 kfree(pf->msix_entries);
8844 pf->msix_entries = NULL;
Guilherme G Piccoli4c95aa52016-09-22 10:03:58 -03008845 pci_disable_msix(pf->pdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008846 return -ENODEV;
8847
Shannon Nelson3b444392015-02-26 16:15:57 +00008848 } else if (v_actual == I40E_MIN_MSIX) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008849 /* Adjust for minimal MSIX use */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008850 pf->num_vmdq_vsis = 0;
8851 pf->num_vmdq_qps = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008852 pf->num_lan_qps = 1;
8853 pf->num_lan_msix = 1;
8854
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02008855 } else if (!vectors_left) {
8856 /* If we have limited resources, we will start with no vectors
8857 * for the special features and then allocate vectors to some
8858 * of these features based on the policy and at the end disable
8859 * the features that did not get any vectors.
8860 */
Shannon Nelson3b444392015-02-26 16:15:57 +00008861 int vec;
8862
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02008863 dev_info(&pf->pdev->dev,
8864 "MSI-X vector limit reached, attempting to redistribute vectors\n");
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00008865 /* reserve the misc vector */
Shannon Nelson3b444392015-02-26 16:15:57 +00008866 vec = v_actual - 1;
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00008867
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008868 /* Scale vector usage down */
8869 pf->num_vmdq_msix = 1; /* force VMDqs to only one vector */
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00008870 pf->num_vmdq_vsis = 1;
Shannon Nelson1e200e42015-02-27 09:15:24 +00008871 pf->num_vmdq_qps = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008872
8873 /* partition out the remaining vectors */
8874 switch (vec) {
8875 case 2:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008876 pf->num_lan_msix = 1;
8877 break;
8878 case 3:
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06008879 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
8880 pf->num_lan_msix = 1;
8881 pf->num_iwarp_msix = 1;
8882 } else {
8883 pf->num_lan_msix = 2;
8884 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008885 break;
8886 default:
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06008887 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
8888 pf->num_iwarp_msix = min_t(int, (vec / 3),
8889 iwarp_requested);
8890 pf->num_vmdq_vsis = min_t(int, (vec / 3),
8891 I40E_DEFAULT_NUM_VMDQ_VSI);
8892 } else {
8893 pf->num_vmdq_vsis = min_t(int, (vec / 2),
8894 I40E_DEFAULT_NUM_VMDQ_VSI);
8895 }
Stefan Assmannabd97a92016-09-19 13:37:51 +02008896 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
8897 pf->num_fdsb_msix = 1;
8898 vec--;
8899 }
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06008900 pf->num_lan_msix = min_t(int,
8901 (vec - (pf->num_iwarp_msix + pf->num_vmdq_vsis)),
8902 pf->num_lan_msix);
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02008903 pf->num_lan_qps = pf->num_lan_msix;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008904 break;
8905 }
8906 }
8907
Stefan Assmannabd97a92016-09-19 13:37:51 +02008908 if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
8909 (pf->num_fdsb_msix == 0)) {
8910 dev_info(&pf->pdev->dev, "Sideband Flowdir disabled, not enough MSI-X vectors\n");
8911 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
8912 }
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00008913 if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
8914 (pf->num_vmdq_msix == 0)) {
8915 dev_info(&pf->pdev->dev, "VMDq disabled, not enough MSI-X vectors\n");
8916 pf->flags &= ~I40E_FLAG_VMDQ_ENABLED;
8917 }
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06008918
8919 if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
8920 (pf->num_iwarp_msix == 0)) {
8921 dev_info(&pf->pdev->dev, "IWARP disabled, not enough MSI-X vectors\n");
8922 pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
8923 }
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02008924 i40e_debug(&pf->hw, I40E_DEBUG_INIT,
8925 "MSI-X vector distribution: PF %d, VMDq %d, FDSB %d, iWARP %d\n",
8926 pf->num_lan_msix,
8927 pf->num_vmdq_msix * pf->num_vmdq_vsis,
8928 pf->num_fdsb_msix,
8929 pf->num_iwarp_msix);
8930
Shannon Nelson3b444392015-02-26 16:15:57 +00008931 return v_actual;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008932}
8933
8934/**
Greg Rose90e04072014-03-06 08:59:57 +00008935 * i40e_vsi_alloc_q_vector - Allocate memory for a single interrupt vector
Alexander Duyck493fb302013-09-28 07:01:44 +00008936 * @vsi: the VSI being configured
8937 * @v_idx: index of the vector in the vsi struct
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03008938 * @cpu: cpu to be used on affinity_mask
Alexander Duyck493fb302013-09-28 07:01:44 +00008939 *
8940 * We allocate one q_vector. If allocation fails we return -ENOMEM.
8941 **/
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03008942static int i40e_vsi_alloc_q_vector(struct i40e_vsi *vsi, int v_idx, int cpu)
Alexander Duyck493fb302013-09-28 07:01:44 +00008943{
8944 struct i40e_q_vector *q_vector;
8945
8946 /* allocate q_vector */
8947 q_vector = kzalloc(sizeof(struct i40e_q_vector), GFP_KERNEL);
8948 if (!q_vector)
8949 return -ENOMEM;
8950
8951 q_vector->vsi = vsi;
8952 q_vector->v_idx = v_idx;
Jacob Keller759dc4a2017-07-14 09:10:10 -04008953 cpumask_copy(&q_vector->affinity_mask, cpu_possible_mask);
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03008954
Alexander Duyck493fb302013-09-28 07:01:44 +00008955 if (vsi->netdev)
8956 netif_napi_add(vsi->netdev, &q_vector->napi,
Jesse Brandeburgeefeace2014-05-10 04:49:13 +00008957 i40e_napi_poll, NAPI_POLL_WEIGHT);
Alexander Duyck493fb302013-09-28 07:01:44 +00008958
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00008959 q_vector->rx.latency_range = I40E_LOW_LATENCY;
8960 q_vector->tx.latency_range = I40E_LOW_LATENCY;
8961
Alexander Duyck493fb302013-09-28 07:01:44 +00008962 /* tie q_vector and vsi together */
8963 vsi->q_vectors[v_idx] = q_vector;
8964
8965 return 0;
8966}
8967
8968/**
Greg Rose90e04072014-03-06 08:59:57 +00008969 * i40e_vsi_alloc_q_vectors - Allocate memory for interrupt vectors
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008970 * @vsi: the VSI being configured
8971 *
8972 * We allocate one q_vector per queue interrupt. If allocation fails we
8973 * return -ENOMEM.
8974 **/
Greg Rose90e04072014-03-06 08:59:57 +00008975static int i40e_vsi_alloc_q_vectors(struct i40e_vsi *vsi)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008976{
8977 struct i40e_pf *pf = vsi->back;
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03008978 int err, v_idx, num_q_vectors, current_cpu;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008979
8980 /* if not MSIX, give the one vector only to the LAN VSI */
8981 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
8982 num_q_vectors = vsi->num_q_vectors;
8983 else if (vsi == pf->vsi[pf->lan_vsi])
8984 num_q_vectors = 1;
8985 else
8986 return -EINVAL;
8987
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03008988 current_cpu = cpumask_first(cpu_online_mask);
8989
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008990 for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03008991 err = i40e_vsi_alloc_q_vector(vsi, v_idx, current_cpu);
Alexander Duyck493fb302013-09-28 07:01:44 +00008992 if (err)
8993 goto err_out;
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03008994 current_cpu = cpumask_next(current_cpu, cpu_online_mask);
8995 if (unlikely(current_cpu >= nr_cpu_ids))
8996 current_cpu = cpumask_first(cpu_online_mask);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008997 }
8998
8999 return 0;
Alexander Duyck493fb302013-09-28 07:01:44 +00009000
9001err_out:
9002 while (v_idx--)
9003 i40e_free_q_vector(vsi, v_idx);
9004
9005 return err;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009006}
9007
9008/**
9009 * i40e_init_interrupt_scheme - Determine proper interrupt scheme
9010 * @pf: board private structure to initialize
9011 **/
Jesse Brandeburgc11472802015-04-07 19:45:39 -04009012static int i40e_init_interrupt_scheme(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009013{
Shannon Nelson3b444392015-02-26 16:15:57 +00009014 int vectors = 0;
9015 ssize_t size;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009016
9017 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
Shannon Nelson3b444392015-02-26 16:15:57 +00009018 vectors = i40e_init_msix(pf);
9019 if (vectors < 0) {
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08009020 pf->flags &= ~(I40E_FLAG_MSIX_ENABLED |
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06009021 I40E_FLAG_IWARP_ENABLED |
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08009022 I40E_FLAG_RSS_ENABLED |
Neerav Parikh4d9b6042014-05-22 06:31:51 +00009023 I40E_FLAG_DCB_CAPABLE |
Dave Ertmana0362442016-08-29 17:38:26 -07009024 I40E_FLAG_DCB_ENABLED |
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08009025 I40E_FLAG_SRIOV_ENABLED |
9026 I40E_FLAG_FD_SB_ENABLED |
9027 I40E_FLAG_FD_ATR_ENABLED |
9028 I40E_FLAG_VMDQ_ENABLED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009029
9030 /* rework the queue expectations without MSIX */
9031 i40e_determine_queue_usage(pf);
9032 }
9033 }
9034
9035 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED) &&
9036 (pf->flags & I40E_FLAG_MSI_ENABLED)) {
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08009037 dev_info(&pf->pdev->dev, "MSI-X not available, trying MSI\n");
Shannon Nelson3b444392015-02-26 16:15:57 +00009038 vectors = pci_enable_msi(pf->pdev);
9039 if (vectors < 0) {
9040 dev_info(&pf->pdev->dev, "MSI init failed - %d\n",
9041 vectors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009042 pf->flags &= ~I40E_FLAG_MSI_ENABLED;
9043 }
Shannon Nelson3b444392015-02-26 16:15:57 +00009044 vectors = 1; /* one MSI or Legacy vector */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009045 }
9046
Shannon Nelson958a3e32013-09-28 07:13:28 +00009047 if (!(pf->flags & (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED)))
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08009048 dev_info(&pf->pdev->dev, "MSI-X and MSI not available, falling back to Legacy IRQ\n");
Shannon Nelson958a3e32013-09-28 07:13:28 +00009049
Shannon Nelson3b444392015-02-26 16:15:57 +00009050 /* set up vector assignment tracking */
9051 size = sizeof(struct i40e_lump_tracking) + (sizeof(u16) * vectors);
9052 pf->irq_pile = kzalloc(size, GFP_KERNEL);
Jesse Brandeburgc11472802015-04-07 19:45:39 -04009053 if (!pf->irq_pile) {
9054 dev_err(&pf->pdev->dev, "error allocating irq_pile memory\n");
9055 return -ENOMEM;
9056 }
Shannon Nelson3b444392015-02-26 16:15:57 +00009057 pf->irq_pile->num_entries = vectors;
9058 pf->irq_pile->search_hint = 0;
9059
Jesse Brandeburgc11472802015-04-07 19:45:39 -04009060 /* track first vector for misc interrupts, ignore return */
Shannon Nelson3b444392015-02-26 16:15:57 +00009061 (void)i40e_get_lump(pf, pf->irq_pile, 1, I40E_PILE_VALID_BIT - 1);
Jesse Brandeburgc11472802015-04-07 19:45:39 -04009062
9063 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009064}
9065
Jacob Kellerb980c062017-07-14 09:27:06 -04009066#ifdef CONFIG_PM
9067/**
9068 * i40e_restore_interrupt_scheme - Restore the interrupt scheme
9069 * @pf: private board data structure
9070 *
9071 * Restore the interrupt scheme that was cleared when we suspended the
9072 * device. This should be called during resume to re-allocate the q_vectors
9073 * and reacquire IRQs.
9074 */
9075static int i40e_restore_interrupt_scheme(struct i40e_pf *pf)
9076{
9077 int err, i;
9078
9079 /* We cleared the MSI and MSI-X flags when disabling the old interrupt
9080 * scheme. We need to re-enabled them here in order to attempt to
9081 * re-acquire the MSI or MSI-X vectors
9082 */
9083 pf->flags |= (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
9084
9085 err = i40e_init_interrupt_scheme(pf);
9086 if (err)
9087 return err;
9088
9089 /* Now that we've re-acquired IRQs, we need to remap the vectors and
9090 * rings together again.
9091 */
9092 for (i = 0; i < pf->num_alloc_vsi; i++) {
9093 if (pf->vsi[i]) {
9094 err = i40e_vsi_alloc_q_vectors(pf->vsi[i]);
9095 if (err)
9096 goto err_unwind;
9097 i40e_vsi_map_rings_to_vectors(pf->vsi[i]);
9098 }
9099 }
9100
9101 err = i40e_setup_misc_vector(pf);
9102 if (err)
9103 goto err_unwind;
9104
9105 return 0;
9106
9107err_unwind:
9108 while (i--) {
9109 if (pf->vsi[i])
9110 i40e_vsi_free_q_vectors(pf->vsi[i]);
9111 }
9112
9113 return err;
9114}
9115#endif /* CONFIG_PM */
9116
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009117/**
9118 * i40e_setup_misc_vector - Setup the misc vector to handle non queue events
9119 * @pf: board private structure
9120 *
9121 * This sets up the handler for MSIX 0, which is used to manage the
9122 * non-queue interrupts, e.g. AdminQ and errors. This is not used
9123 * when in MSI or Legacy interrupt mode.
9124 **/
9125static int i40e_setup_misc_vector(struct i40e_pf *pf)
9126{
9127 struct i40e_hw *hw = &pf->hw;
9128 int err = 0;
9129
Jacob Kellerc17401a2017-07-14 09:27:02 -04009130 /* Only request the IRQ once, the first time through. */
9131 if (!test_and_set_bit(__I40E_MISC_IRQ_REQUESTED, pf->state)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009132 err = request_irq(pf->msix_entries[0].vector,
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00009133 i40e_intr, 0, pf->int_name, pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009134 if (err) {
Jacob Kellerc17401a2017-07-14 09:27:02 -04009135 clear_bit(__I40E_MISC_IRQ_REQUESTED, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009136 dev_info(&pf->pdev->dev,
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08009137 "request_irq for %s failed: %d\n",
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00009138 pf->int_name, err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009139 return -EFAULT;
9140 }
9141 }
9142
Jacob Kellerab437b52014-12-14 01:55:08 +00009143 i40e_enable_misc_int_causes(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009144
9145 /* associate no queues to the misc vector */
9146 wr32(hw, I40E_PFINT_LNKLST0, I40E_QUEUE_END_OF_LIST);
9147 wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), I40E_ITR_8K);
9148
9149 i40e_flush(hw);
9150
Jacob Kellerdbadbbe2017-09-07 08:05:49 -04009151 i40e_irq_dynamic_enable_icr0(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009152
9153 return err;
9154}
9155
9156/**
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04009157 * i40e_config_rss_aq - Prepare for RSS using AQ commands
9158 * @vsi: vsi structure
9159 * @seed: RSS hash seed
9160 **/
Helin Zhange69ff812015-10-21 19:56:22 -04009161static int i40e_config_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
9162 u8 *lut, u16 lut_size)
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04009163{
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04009164 struct i40e_pf *pf = vsi->back;
9165 struct i40e_hw *hw = &pf->hw;
Jacob Keller776b2e12016-07-19 16:23:30 -07009166 int ret = 0;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04009167
Jacob Keller776b2e12016-07-19 16:23:30 -07009168 if (seed) {
9169 struct i40e_aqc_get_set_rss_key_data *seed_dw =
9170 (struct i40e_aqc_get_set_rss_key_data *)seed;
9171 ret = i40e_aq_set_rss_key(hw, vsi->id, seed_dw);
9172 if (ret) {
9173 dev_info(&pf->pdev->dev,
9174 "Cannot set RSS key, err %s aq_err %s\n",
9175 i40e_stat_str(hw, ret),
9176 i40e_aq_str(hw, hw->aq.asq_last_status));
9177 return ret;
9178 }
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04009179 }
Jacob Keller776b2e12016-07-19 16:23:30 -07009180 if (lut) {
9181 bool pf_lut = vsi->type == I40E_VSI_MAIN ? true : false;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04009182
Jacob Keller776b2e12016-07-19 16:23:30 -07009183 ret = i40e_aq_set_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
9184 if (ret) {
9185 dev_info(&pf->pdev->dev,
9186 "Cannot set RSS lut, err %s aq_err %s\n",
9187 i40e_stat_str(hw, ret),
9188 i40e_aq_str(hw, hw->aq.asq_last_status));
9189 return ret;
9190 }
9191 }
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04009192 return ret;
9193}
9194
9195/**
Anjali Singhai Jain95a73782015-12-22 14:25:04 -08009196 * i40e_get_rss_aq - Get RSS keys and lut by using AQ commands
9197 * @vsi: Pointer to vsi structure
9198 * @seed: Buffter to store the hash keys
9199 * @lut: Buffer to store the lookup table entries
9200 * @lut_size: Size of buffer to store the lookup table entries
9201 *
9202 * Return 0 on success, negative on failure
9203 */
9204static int i40e_get_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
9205 u8 *lut, u16 lut_size)
9206{
9207 struct i40e_pf *pf = vsi->back;
9208 struct i40e_hw *hw = &pf->hw;
9209 int ret = 0;
9210
9211 if (seed) {
9212 ret = i40e_aq_get_rss_key(hw, vsi->id,
9213 (struct i40e_aqc_get_set_rss_key_data *)seed);
9214 if (ret) {
9215 dev_info(&pf->pdev->dev,
9216 "Cannot get RSS key, err %s aq_err %s\n",
9217 i40e_stat_str(&pf->hw, ret),
9218 i40e_aq_str(&pf->hw,
9219 pf->hw.aq.asq_last_status));
9220 return ret;
9221 }
9222 }
9223
9224 if (lut) {
9225 bool pf_lut = vsi->type == I40E_VSI_MAIN ? true : false;
9226
9227 ret = i40e_aq_get_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
9228 if (ret) {
9229 dev_info(&pf->pdev->dev,
9230 "Cannot get RSS lut, err %s aq_err %s\n",
9231 i40e_stat_str(&pf->hw, ret),
9232 i40e_aq_str(&pf->hw,
9233 pf->hw.aq.asq_last_status));
9234 return ret;
9235 }
9236 }
9237
9238 return ret;
9239}
9240
9241/**
Jacob Keller0582b962016-07-19 16:23:29 -07009242 * i40e_vsi_config_rss - Prepare for VSI(VMDq) RSS if used
9243 * @vsi: VSI structure
9244 **/
9245static int i40e_vsi_config_rss(struct i40e_vsi *vsi)
9246{
9247 u8 seed[I40E_HKEY_ARRAY_SIZE];
9248 struct i40e_pf *pf = vsi->back;
9249 u8 *lut;
9250 int ret;
9251
Jacob Kellerd36e41d2017-06-23 04:24:46 -04009252 if (!(pf->hw_features & I40E_HW_RSS_AQ_CAPABLE))
Jacob Keller0582b962016-07-19 16:23:29 -07009253 return 0;
9254
Jacob Keller552b9962016-07-19 16:23:31 -07009255 if (!vsi->rss_size)
9256 vsi->rss_size = min_t(int, pf->alloc_rss_size,
9257 vsi->num_queue_pairs);
9258 if (!vsi->rss_size)
9259 return -EINVAL;
9260
Jacob Keller0582b962016-07-19 16:23:29 -07009261 lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
9262 if (!lut)
9263 return -ENOMEM;
Jacob Keller552b9962016-07-19 16:23:31 -07009264 /* Use the user configured hash keys and lookup table if there is one,
9265 * otherwise use default
9266 */
9267 if (vsi->rss_lut_user)
9268 memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
9269 else
9270 i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
9271 if (vsi->rss_hkey_user)
9272 memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
9273 else
9274 netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
Jacob Keller0582b962016-07-19 16:23:29 -07009275 ret = i40e_config_rss_aq(vsi, seed, lut, vsi->rss_table_size);
9276 kfree(lut);
9277
9278 return ret;
9279}
9280
9281/**
Helin Zhang043dd652015-10-21 19:56:23 -04009282 * i40e_config_rss_reg - Configure RSS keys and lut by writing registers
Helin Zhange69ff812015-10-21 19:56:22 -04009283 * @vsi: Pointer to vsi structure
9284 * @seed: RSS hash seed
9285 * @lut: Lookup table
9286 * @lut_size: Lookup table size
9287 *
9288 * Returns 0 on success, negative on failure
9289 **/
9290static int i40e_config_rss_reg(struct i40e_vsi *vsi, const u8 *seed,
9291 const u8 *lut, u16 lut_size)
9292{
9293 struct i40e_pf *pf = vsi->back;
9294 struct i40e_hw *hw = &pf->hw;
Mitch Williamsc4e18682016-04-12 08:30:40 -07009295 u16 vf_id = vsi->vf_id;
Helin Zhange69ff812015-10-21 19:56:22 -04009296 u8 i;
9297
9298 /* Fill out hash function seed */
9299 if (seed) {
9300 u32 *seed_dw = (u32 *)seed;
9301
Mitch Williamsc4e18682016-04-12 08:30:40 -07009302 if (vsi->type == I40E_VSI_MAIN) {
9303 for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
Lihong Yang26f77e52017-01-30 12:29:33 -08009304 wr32(hw, I40E_PFQF_HKEY(i), seed_dw[i]);
Mitch Williamsc4e18682016-04-12 08:30:40 -07009305 } else if (vsi->type == I40E_VSI_SRIOV) {
9306 for (i = 0; i <= I40E_VFQF_HKEY1_MAX_INDEX; i++)
Lihong Yang26f77e52017-01-30 12:29:33 -08009307 wr32(hw, I40E_VFQF_HKEY1(i, vf_id), seed_dw[i]);
Mitch Williamsc4e18682016-04-12 08:30:40 -07009308 } else {
9309 dev_err(&pf->pdev->dev, "Cannot set RSS seed - invalid VSI type\n");
9310 }
Helin Zhange69ff812015-10-21 19:56:22 -04009311 }
9312
9313 if (lut) {
9314 u32 *lut_dw = (u32 *)lut;
9315
Mitch Williamsc4e18682016-04-12 08:30:40 -07009316 if (vsi->type == I40E_VSI_MAIN) {
9317 if (lut_size != I40E_HLUT_ARRAY_SIZE)
9318 return -EINVAL;
9319 for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
9320 wr32(hw, I40E_PFQF_HLUT(i), lut_dw[i]);
9321 } else if (vsi->type == I40E_VSI_SRIOV) {
9322 if (lut_size != I40E_VF_HLUT_ARRAY_SIZE)
9323 return -EINVAL;
9324 for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
Lihong Yang26f77e52017-01-30 12:29:33 -08009325 wr32(hw, I40E_VFQF_HLUT1(i, vf_id), lut_dw[i]);
Mitch Williamsc4e18682016-04-12 08:30:40 -07009326 } else {
9327 dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
9328 }
Helin Zhange69ff812015-10-21 19:56:22 -04009329 }
9330 i40e_flush(hw);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04009331
9332 return 0;
9333}
9334
9335/**
Helin Zhang043dd652015-10-21 19:56:23 -04009336 * i40e_get_rss_reg - Get the RSS keys and lut by reading registers
9337 * @vsi: Pointer to VSI structure
9338 * @seed: Buffer to store the keys
9339 * @lut: Buffer to store the lookup table entries
9340 * @lut_size: Size of buffer to store the lookup table entries
9341 *
9342 * Returns 0 on success, negative on failure
9343 */
9344static int i40e_get_rss_reg(struct i40e_vsi *vsi, u8 *seed,
9345 u8 *lut, u16 lut_size)
9346{
9347 struct i40e_pf *pf = vsi->back;
9348 struct i40e_hw *hw = &pf->hw;
9349 u16 i;
9350
9351 if (seed) {
9352 u32 *seed_dw = (u32 *)seed;
9353
9354 for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
Shannon Nelson272cdaf22016-02-17 16:12:21 -08009355 seed_dw[i] = i40e_read_rx_ctl(hw, I40E_PFQF_HKEY(i));
Helin Zhang043dd652015-10-21 19:56:23 -04009356 }
9357 if (lut) {
9358 u32 *lut_dw = (u32 *)lut;
9359
9360 if (lut_size != I40E_HLUT_ARRAY_SIZE)
9361 return -EINVAL;
9362 for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
9363 lut_dw[i] = rd32(hw, I40E_PFQF_HLUT(i));
9364 }
9365
9366 return 0;
9367}
9368
9369/**
9370 * i40e_config_rss - Configure RSS keys and lut
9371 * @vsi: Pointer to VSI structure
9372 * @seed: RSS hash seed
9373 * @lut: Lookup table
9374 * @lut_size: Lookup table size
9375 *
9376 * Returns 0 on success, negative on failure
9377 */
9378int i40e_config_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
9379{
9380 struct i40e_pf *pf = vsi->back;
9381
Jacob Kellerd36e41d2017-06-23 04:24:46 -04009382 if (pf->hw_features & I40E_HW_RSS_AQ_CAPABLE)
Helin Zhang043dd652015-10-21 19:56:23 -04009383 return i40e_config_rss_aq(vsi, seed, lut, lut_size);
9384 else
9385 return i40e_config_rss_reg(vsi, seed, lut, lut_size);
9386}
9387
9388/**
9389 * i40e_get_rss - Get RSS keys and lut
9390 * @vsi: Pointer to VSI structure
9391 * @seed: Buffer to store the keys
9392 * @lut: Buffer to store the lookup table entries
9393 * lut_size: Size of buffer to store the lookup table entries
9394 *
9395 * Returns 0 on success, negative on failure
9396 */
9397int i40e_get_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
9398{
Anjali Singhai Jain95a73782015-12-22 14:25:04 -08009399 struct i40e_pf *pf = vsi->back;
9400
Jacob Kellerd36e41d2017-06-23 04:24:46 -04009401 if (pf->hw_features & I40E_HW_RSS_AQ_CAPABLE)
Anjali Singhai Jain95a73782015-12-22 14:25:04 -08009402 return i40e_get_rss_aq(vsi, seed, lut, lut_size);
9403 else
9404 return i40e_get_rss_reg(vsi, seed, lut, lut_size);
Helin Zhang043dd652015-10-21 19:56:23 -04009405}
9406
9407/**
Helin Zhange69ff812015-10-21 19:56:22 -04009408 * i40e_fill_rss_lut - Fill the RSS lookup table with default values
9409 * @pf: Pointer to board private structure
9410 * @lut: Lookup table
9411 * @rss_table_size: Lookup table size
9412 * @rss_size: Range of queue number for hashing
9413 */
Alan Bradyf1582352016-08-24 11:33:46 -07009414void i40e_fill_rss_lut(struct i40e_pf *pf, u8 *lut,
9415 u16 rss_table_size, u16 rss_size)
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04009416{
Helin Zhange69ff812015-10-21 19:56:22 -04009417 u16 i;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04009418
Helin Zhange69ff812015-10-21 19:56:22 -04009419 for (i = 0; i < rss_table_size; i++)
9420 lut[i] = i % rss_size;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04009421}
9422
9423/**
Helin Zhang043dd652015-10-21 19:56:23 -04009424 * i40e_pf_config_rss - Prepare for RSS if used
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009425 * @pf: board private structure
9426 **/
Helin Zhang043dd652015-10-21 19:56:23 -04009427static int i40e_pf_config_rss(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009428{
Anjali Singhai Jain66ddcff2015-02-24 06:58:50 +00009429 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04009430 u8 seed[I40E_HKEY_ARRAY_SIZE];
Helin Zhange69ff812015-10-21 19:56:22 -04009431 u8 *lut;
Anjali Singhai Jain4617e8c2013-11-20 10:02:56 +00009432 struct i40e_hw *hw = &pf->hw;
Carolyn Wybornye157ea32014-06-03 23:50:22 +00009433 u32 reg_val;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04009434 u64 hena;
Helin Zhange69ff812015-10-21 19:56:22 -04009435 int ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009436
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009437 /* By default we enable TCP/UDP with IPv4/IPv6 ptypes */
Shannon Nelson272cdaf22016-02-17 16:12:21 -08009438 hena = (u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(0)) |
9439 ((u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(1)) << 32);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04009440 hena |= i40e_pf_get_default_rss_hena(pf);
9441
Shannon Nelson272cdaf22016-02-17 16:12:21 -08009442 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), (u32)hena);
9443 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009444
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04009445 /* Determine the RSS table size based on the hardware capabilities */
Shannon Nelson272cdaf22016-02-17 16:12:21 -08009446 reg_val = i40e_read_rx_ctl(hw, I40E_PFQF_CTL_0);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04009447 reg_val = (pf->rss_table_size == 512) ?
9448 (reg_val | I40E_PFQF_CTL_0_HASHLUTSIZE_512) :
9449 (reg_val & ~I40E_PFQF_CTL_0_HASHLUTSIZE_512);
Shannon Nelson272cdaf22016-02-17 16:12:21 -08009450 i40e_write_rx_ctl(hw, I40E_PFQF_CTL_0, reg_val);
Carolyn Wybornye157ea32014-06-03 23:50:22 +00009451
Helin Zhang28c58692015-10-26 19:44:27 -04009452 /* Determine the RSS size of the VSI */
Harshitha Ramamurthyf25571b2017-02-21 15:55:40 -08009453 if (!vsi->rss_size) {
9454 u16 qcount;
9455
9456 qcount = vsi->num_queue_pairs / vsi->tc_config.numtc;
9457 vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
9458 }
Mitch Williamsa4fa59c2016-09-12 14:18:43 -07009459 if (!vsi->rss_size)
9460 return -EINVAL;
Helin Zhang28c58692015-10-26 19:44:27 -04009461
Helin Zhange69ff812015-10-21 19:56:22 -04009462 lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
9463 if (!lut)
9464 return -ENOMEM;
9465
Helin Zhang28c58692015-10-26 19:44:27 -04009466 /* Use user configured lut if there is one, otherwise use default */
9467 if (vsi->rss_lut_user)
9468 memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
9469 else
9470 i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
Helin Zhange69ff812015-10-21 19:56:22 -04009471
Helin Zhang28c58692015-10-26 19:44:27 -04009472 /* Use user configured hash key if there is one, otherwise
9473 * use default.
9474 */
9475 if (vsi->rss_hkey_user)
9476 memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
9477 else
9478 netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
Helin Zhang043dd652015-10-21 19:56:23 -04009479 ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
Helin Zhange69ff812015-10-21 19:56:22 -04009480 kfree(lut);
9481
9482 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009483}
9484
9485/**
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00009486 * i40e_reconfig_rss_queues - change number of queues for rss and rebuild
9487 * @pf: board private structure
9488 * @queue_count: the requested queue count for rss.
9489 *
9490 * returns 0 if rss is not enabled, if enabled returns the final rss queue
9491 * count which may be different from the requested queue count.
Maciej Sosin373149f2017-04-05 07:50:55 -04009492 * Note: expects to be called while under rtnl_lock()
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00009493 **/
9494int i40e_reconfig_rss_queues(struct i40e_pf *pf, int queue_count)
9495{
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00009496 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
9497 int new_rss_size;
9498
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00009499 if (!(pf->flags & I40E_FLAG_RSS_ENABLED))
9500 return 0;
9501
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00009502 new_rss_size = min_t(int, queue_count, pf->rss_size_max);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00009503
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00009504 if (queue_count != vsi->num_queue_pairs) {
Harshitha Ramamurthyf25571b2017-02-21 15:55:40 -08009505 u16 qcount;
9506
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00009507 vsi->req_queue_pairs = queue_count;
Maciej Sosin373149f2017-04-05 07:50:55 -04009508 i40e_prep_for_reset(pf, true);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00009509
Helin Zhangacd65442015-10-26 19:44:28 -04009510 pf->alloc_rss_size = new_rss_size;
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00009511
Maciej Sosin373149f2017-04-05 07:50:55 -04009512 i40e_reset_and_rebuild(pf, true, true);
Helin Zhang28c58692015-10-26 19:44:27 -04009513
9514 /* Discard the user configured hash keys and lut, if less
9515 * queues are enabled.
9516 */
9517 if (queue_count < vsi->rss_size) {
9518 i40e_clear_rss_config_user(vsi);
9519 dev_dbg(&pf->pdev->dev,
9520 "discard user configured hash keys and lut\n");
9521 }
9522
9523 /* Reset vsi->rss_size, as number of enabled queues changed */
Harshitha Ramamurthyf25571b2017-02-21 15:55:40 -08009524 qcount = vsi->num_queue_pairs / vsi->tc_config.numtc;
9525 vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
Helin Zhang28c58692015-10-26 19:44:27 -04009526
Helin Zhang043dd652015-10-21 19:56:23 -04009527 i40e_pf_config_rss(pf);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00009528 }
Lihong Yang12815052016-09-27 11:28:48 -07009529 dev_info(&pf->pdev->dev, "User requested queue count/HW max RSS count: %d/%d\n",
9530 vsi->req_queue_pairs, pf->rss_size_max);
Helin Zhangacd65442015-10-26 19:44:28 -04009531 return pf->alloc_rss_size;
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00009532}
9533
9534/**
Shannon Nelson4fc8c672017-06-07 05:43:08 -04009535 * i40e_get_partition_bw_setting - Retrieve BW settings for this PF partition
Greg Rosef4492db2015-02-06 08:52:12 +00009536 * @pf: board private structure
9537 **/
Shannon Nelson4fc8c672017-06-07 05:43:08 -04009538i40e_status i40e_get_partition_bw_setting(struct i40e_pf *pf)
Greg Rosef4492db2015-02-06 08:52:12 +00009539{
9540 i40e_status status;
9541 bool min_valid, max_valid;
9542 u32 max_bw, min_bw;
9543
9544 status = i40e_read_bw_from_alt_ram(&pf->hw, &max_bw, &min_bw,
9545 &min_valid, &max_valid);
9546
9547 if (!status) {
9548 if (min_valid)
Shannon Nelson4fc8c672017-06-07 05:43:08 -04009549 pf->min_bw = min_bw;
Greg Rosef4492db2015-02-06 08:52:12 +00009550 if (max_valid)
Shannon Nelson4fc8c672017-06-07 05:43:08 -04009551 pf->max_bw = max_bw;
Greg Rosef4492db2015-02-06 08:52:12 +00009552 }
9553
9554 return status;
9555}
9556
9557/**
Shannon Nelson4fc8c672017-06-07 05:43:08 -04009558 * i40e_set_partition_bw_setting - Set BW settings for this PF partition
Greg Rosef4492db2015-02-06 08:52:12 +00009559 * @pf: board private structure
9560 **/
Shannon Nelson4fc8c672017-06-07 05:43:08 -04009561i40e_status i40e_set_partition_bw_setting(struct i40e_pf *pf)
Greg Rosef4492db2015-02-06 08:52:12 +00009562{
9563 struct i40e_aqc_configure_partition_bw_data bw_data;
9564 i40e_status status;
9565
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00009566 /* Set the valid bit for this PF */
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04009567 bw_data.pf_valid_bits = cpu_to_le16(BIT(pf->hw.pf_id));
Shannon Nelson4fc8c672017-06-07 05:43:08 -04009568 bw_data.max_bw[pf->hw.pf_id] = pf->max_bw & I40E_ALT_BW_VALUE_MASK;
9569 bw_data.min_bw[pf->hw.pf_id] = pf->min_bw & I40E_ALT_BW_VALUE_MASK;
Greg Rosef4492db2015-02-06 08:52:12 +00009570
9571 /* Set the new bandwidths */
9572 status = i40e_aq_configure_partition_bw(&pf->hw, &bw_data, NULL);
9573
9574 return status;
9575}
9576
9577/**
Shannon Nelson4fc8c672017-06-07 05:43:08 -04009578 * i40e_commit_partition_bw_setting - Commit BW settings for this PF partition
Greg Rosef4492db2015-02-06 08:52:12 +00009579 * @pf: board private structure
9580 **/
Shannon Nelson4fc8c672017-06-07 05:43:08 -04009581i40e_status i40e_commit_partition_bw_setting(struct i40e_pf *pf)
Greg Rosef4492db2015-02-06 08:52:12 +00009582{
9583 /* Commit temporary BW setting to permanent NVM image */
9584 enum i40e_admin_queue_err last_aq_status;
9585 i40e_status ret;
9586 u16 nvm_word;
9587
9588 if (pf->hw.partition_id != 1) {
9589 dev_info(&pf->pdev->dev,
9590 "Commit BW only works on partition 1! This is partition %d",
9591 pf->hw.partition_id);
9592 ret = I40E_NOT_SUPPORTED;
9593 goto bw_commit_out;
9594 }
9595
9596 /* Acquire NVM for read access */
9597 ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_READ);
9598 last_aq_status = pf->hw.aq.asq_last_status;
9599 if (ret) {
9600 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009601 "Cannot acquire NVM for read access, err %s aq_err %s\n",
9602 i40e_stat_str(&pf->hw, ret),
9603 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +00009604 goto bw_commit_out;
9605 }
9606
9607 /* Read word 0x10 of NVM - SW compatibility word 1 */
9608 ret = i40e_aq_read_nvm(&pf->hw,
9609 I40E_SR_NVM_CONTROL_WORD,
9610 0x10, sizeof(nvm_word), &nvm_word,
9611 false, NULL);
9612 /* Save off last admin queue command status before releasing
9613 * the NVM
9614 */
9615 last_aq_status = pf->hw.aq.asq_last_status;
9616 i40e_release_nvm(&pf->hw);
9617 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009618 dev_info(&pf->pdev->dev, "NVM read error, err %s aq_err %s\n",
9619 i40e_stat_str(&pf->hw, ret),
9620 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +00009621 goto bw_commit_out;
9622 }
9623
9624 /* Wait a bit for NVM release to complete */
9625 msleep(50);
9626
9627 /* Acquire NVM for write access */
9628 ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_WRITE);
9629 last_aq_status = pf->hw.aq.asq_last_status;
9630 if (ret) {
9631 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009632 "Cannot acquire NVM for write access, err %s aq_err %s\n",
9633 i40e_stat_str(&pf->hw, ret),
9634 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +00009635 goto bw_commit_out;
9636 }
9637 /* Write it back out unchanged to initiate update NVM,
9638 * which will force a write of the shadow (alt) RAM to
9639 * the NVM - thus storing the bandwidth values permanently.
9640 */
9641 ret = i40e_aq_update_nvm(&pf->hw,
9642 I40E_SR_NVM_CONTROL_WORD,
9643 0x10, sizeof(nvm_word),
9644 &nvm_word, true, NULL);
9645 /* Save off last admin queue command status before releasing
9646 * the NVM
9647 */
9648 last_aq_status = pf->hw.aq.asq_last_status;
9649 i40e_release_nvm(&pf->hw);
9650 if (ret)
9651 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009652 "BW settings NOT SAVED, err %s aq_err %s\n",
9653 i40e_stat_str(&pf->hw, ret),
9654 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +00009655bw_commit_out:
9656
9657 return ret;
9658}
9659
9660/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009661 * i40e_sw_init - Initialize general software structures (struct i40e_pf)
9662 * @pf: board private structure to initialize
9663 *
9664 * i40e_sw_init initializes the Adapter private data structure.
9665 * Fields are initialized based on PCI device information and
9666 * OS network device settings (MTU size).
9667 **/
9668static int i40e_sw_init(struct i40e_pf *pf)
9669{
9670 int err = 0;
9671 int size;
9672
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009673 /* Set default capability flags */
9674 pf->flags = I40E_FLAG_RX_CSUM_ENABLED |
9675 I40E_FLAG_MSI_ENABLED |
Mitch Williams2bc7ee82015-02-06 08:52:11 +00009676 I40E_FLAG_MSIX_ENABLED;
9677
Mitch Williamsca99eb92014-04-04 04:43:07 +00009678 /* Set default ITR */
Jacob Keller42702552017-09-07 08:05:48 -04009679 pf->rx_itr_default = I40E_ITR_RX_DEF;
9680 pf->tx_itr_default = I40E_ITR_TX_DEF;
Mitch Williamsca99eb92014-04-04 04:43:07 +00009681
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00009682 /* Depending on PF configurations, it is possible that the RSS
9683 * maximum might end up larger than the available queues
9684 */
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04009685 pf->rss_size_max = BIT(pf->hw.func_caps.rss_table_entry_width);
Helin Zhangacd65442015-10-26 19:44:28 -04009686 pf->alloc_rss_size = 1;
Anjali Singhai Jain5db4cb52015-02-24 06:58:49 +00009687 pf->rss_table_size = pf->hw.func_caps.rss_table_size;
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00009688 pf->rss_size_max = min_t(int, pf->rss_size_max,
9689 pf->hw.func_caps.num_tx_qp);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009690 if (pf->hw.func_caps.rss) {
9691 pf->flags |= I40E_FLAG_RSS_ENABLED;
Helin Zhangacd65442015-10-26 19:44:28 -04009692 pf->alloc_rss_size = min_t(int, pf->rss_size_max,
9693 num_online_cpus());
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009694 }
9695
Catherine Sullivan2050bc62013-12-18 13:46:03 +00009696 /* MFP mode enabled */
Pawel Orlowskic78b9532015-04-22 19:34:06 -04009697 if (pf->hw.func_caps.npar_enable || pf->hw.func_caps.flex10_enable) {
Catherine Sullivan2050bc62013-12-18 13:46:03 +00009698 pf->flags |= I40E_FLAG_MFP_ENABLED;
9699 dev_info(&pf->pdev->dev, "MFP mode Enabled\n");
Shannon Nelson4fc8c672017-06-07 05:43:08 -04009700 if (i40e_get_partition_bw_setting(pf)) {
Greg Rosef4492db2015-02-06 08:52:12 +00009701 dev_warn(&pf->pdev->dev,
Shannon Nelson4fc8c672017-06-07 05:43:08 -04009702 "Could not get partition bw settings\n");
9703 } else {
Greg Rosef4492db2015-02-06 08:52:12 +00009704 dev_info(&pf->pdev->dev,
Shannon Nelson4fc8c672017-06-07 05:43:08 -04009705 "Partition BW Min = %8.8x, Max = %8.8x\n",
9706 pf->min_bw, pf->max_bw);
9707
9708 /* nudge the Tx scheduler */
9709 i40e_set_partition_bw_setting(pf);
9710 }
Catherine Sullivan2050bc62013-12-18 13:46:03 +00009711 }
9712
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009713 if ((pf->hw.func_caps.fd_filters_guaranteed > 0) ||
9714 (pf->hw.func_caps.fd_filters_best_effort > 0)) {
9715 pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
9716 pf->atr_sample_rate = I40E_DEFAULT_ATR_SAMPLE_RATE;
Shannon Nelson6eae9c62015-09-03 17:18:55 -04009717 if (pf->flags & I40E_FLAG_MFP_ENABLED &&
9718 pf->hw.num_partitions > 1)
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009719 dev_info(&pf->pdev->dev,
Anjali Singhai Jain0b675842014-03-06 08:59:51 +00009720 "Flow Director Sideband mode Disabled in MFP mode\n");
Shannon Nelson6eae9c62015-09-03 17:18:55 -04009721 else
9722 pf->flags |= I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009723 pf->fdir_pf_filter_count =
9724 pf->hw.func_caps.fd_filters_guaranteed;
9725 pf->hw.fdir_shared_filter_count =
9726 pf->hw.func_caps.fd_filters_best_effort;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009727 }
9728
Anjali Singhai Jain5a433192017-06-23 04:24:45 -04009729 if (pf->hw.mac.type == I40E_MAC_X722) {
Jacob Kellerd36e41d2017-06-23 04:24:46 -04009730 pf->hw_features |= (I40E_HW_RSS_AQ_CAPABLE |
9731 I40E_HW_128_QP_RSS_CAPABLE |
9732 I40E_HW_ATR_EVICT_CAPABLE |
9733 I40E_HW_WB_ON_ITR_CAPABLE |
9734 I40E_HW_MULTIPLE_TCP_UDP_RSS_PCTYPE |
9735 I40E_HW_NO_PCI_LINK_CHECK |
9736 I40E_HW_USE_SET_LLDP_MIB |
9737 I40E_HW_GENEVE_OFFLOAD_CAPABLE |
9738 I40E_HW_PTP_L4_CAPABLE |
9739 I40E_HW_WOL_MC_MAGIC_PKT_WAKE |
9740 I40E_HW_OUTER_UDP_CSUM_CAPABLE);
Anjali Singhai Jain10a955f2017-06-23 04:24:48 -04009741
9742#define I40E_FDEVICT_PCTYPE_DEFAULT 0xc03
9743 if (rd32(&pf->hw, I40E_GLQF_FDEVICTENA(1)) !=
9744 I40E_FDEVICT_PCTYPE_DEFAULT) {
9745 dev_warn(&pf->pdev->dev,
9746 "FD EVICT PCTYPES are not right, disable FD HW EVICT\n");
9747 pf->hw_features &= ~I40E_HW_ATR_EVICT_CAPABLE;
9748 }
Anjali Singhai Jain5a433192017-06-23 04:24:45 -04009749 } else if ((pf->hw.aq.api_maj_ver > 1) ||
9750 ((pf->hw.aq.api_maj_ver == 1) &&
9751 (pf->hw.aq.api_min_ver > 4))) {
9752 /* Supported in FW API version higher than 1.4 */
Jacob Kellerd36e41d2017-06-23 04:24:46 -04009753 pf->hw_features |= I40E_HW_GENEVE_OFFLOAD_CAPABLE;
Anjali Singhai Jain5a433192017-06-23 04:24:45 -04009754 }
9755
9756 /* Enable HW ATR eviction if possible */
Jacob Kellerd36e41d2017-06-23 04:24:46 -04009757 if (pf->hw_features & I40E_HW_ATR_EVICT_CAPABLE)
Anjali Singhai Jain5a433192017-06-23 04:24:45 -04009758 pf->flags |= I40E_FLAG_HW_ATR_EVICT_ENABLED;
9759
Bimmy Pujari6de432c2016-11-11 12:39:38 -08009760 if ((pf->hw.mac.type == I40E_MAC_XL710) &&
Anjali Singhai Jain8eed76f2015-12-09 15:50:31 -08009761 (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 33)) ||
Neerav Parikhf1bbad32016-01-13 16:51:39 -08009762 (pf->hw.aq.fw_maj_ver < 4))) {
Jacob Kellerd36e41d2017-06-23 04:24:46 -04009763 pf->hw_features |= I40E_HW_RESTART_AUTONEG;
Neerav Parikhf1bbad32016-01-13 16:51:39 -08009764 /* No DCB support for FW < v4.33 */
Jacob Kellerd36e41d2017-06-23 04:24:46 -04009765 pf->hw_features |= I40E_HW_NO_DCB_SUPPORT;
Neerav Parikhf1bbad32016-01-13 16:51:39 -08009766 }
9767
9768 /* Disable FW LLDP if FW < v4.3 */
Bimmy Pujari6de432c2016-11-11 12:39:38 -08009769 if ((pf->hw.mac.type == I40E_MAC_XL710) &&
Neerav Parikhf1bbad32016-01-13 16:51:39 -08009770 (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 3)) ||
9771 (pf->hw.aq.fw_maj_ver < 4)))
Jacob Kellerd36e41d2017-06-23 04:24:46 -04009772 pf->hw_features |= I40E_HW_STOP_FW_LLDP;
Neerav Parikhf1bbad32016-01-13 16:51:39 -08009773
9774 /* Use the FW Set LLDP MIB API if FW > v4.40 */
Bimmy Pujari6de432c2016-11-11 12:39:38 -08009775 if ((pf->hw.mac.type == I40E_MAC_XL710) &&
Neerav Parikhf1bbad32016-01-13 16:51:39 -08009776 (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver >= 40)) ||
9777 (pf->hw.aq.fw_maj_ver >= 5)))
Jacob Kellerd36e41d2017-06-23 04:24:46 -04009778 pf->hw_features |= I40E_HW_USE_SET_LLDP_MIB;
Anjali Singhai Jain8eed76f2015-12-09 15:50:31 -08009779
Alan Bradyc3d26b72017-08-29 05:32:32 -04009780 /* Enable PTP L4 if FW > v6.0 */
9781 if (pf->hw.mac.type == I40E_MAC_XL710 &&
9782 pf->hw.aq.fw_maj_ver >= 6)
9783 pf->hw_features |= I40E_HW_PTP_L4_CAPABLE;
9784
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009785 if (pf->hw.func_caps.vmdq) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009786 pf->num_vmdq_vsis = I40E_DEFAULT_NUM_VMDQ_VSI;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04009787 pf->flags |= I40E_FLAG_VMDQ_ENABLED;
Jesse Brandeburge9e53662015-10-02 17:57:21 -07009788 pf->num_vmdq_qps = i40e_default_queues_per_vmdq(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009789 }
9790
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06009791 if (pf->hw.func_caps.iwarp) {
9792 pf->flags |= I40E_FLAG_IWARP_ENABLED;
9793 /* IWARP needs one extra vector for CQP just like MISC.*/
9794 pf->num_iwarp_msix = (int)num_online_cpus() + 1;
9795 }
9796
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009797#ifdef CONFIG_PCI_IOV
Shannon Nelsonba252f132014-12-11 07:06:34 +00009798 if (pf->hw.func_caps.num_vfs && pf->hw.partition_id == 1) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009799 pf->num_vf_qps = I40E_DEFAULT_QUEUES_PER_VF;
9800 pf->flags |= I40E_FLAG_SRIOV_ENABLED;
9801 pf->num_req_vfs = min_t(int,
9802 pf->hw.func_caps.num_vfs,
9803 I40E_MAX_VF_COUNT);
9804 }
9805#endif /* CONFIG_PCI_IOV */
9806 pf->eeprom_version = 0xDEAD;
9807 pf->lan_veb = I40E_NO_VEB;
9808 pf->lan_vsi = I40E_NO_VSI;
9809
Anjali Singhai Jaind1a8d272015-07-23 16:54:40 -04009810 /* By default FW has this off for performance reasons */
9811 pf->flags &= ~I40E_FLAG_VEB_STATS_ENABLED;
9812
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009813 /* set up queue assignment tracking */
9814 size = sizeof(struct i40e_lump_tracking)
9815 + (sizeof(u16) * pf->hw.func_caps.num_tx_qp);
9816 pf->qp_pile = kzalloc(size, GFP_KERNEL);
9817 if (!pf->qp_pile) {
9818 err = -ENOMEM;
9819 goto sw_init_done;
9820 }
9821 pf->qp_pile->num_entries = pf->hw.func_caps.num_tx_qp;
9822 pf->qp_pile->search_hint = 0;
9823
Anjali Singhai Jain327fe042014-06-04 01:23:26 +00009824 pf->tx_timeout_recovery_level = 1;
9825
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009826 mutex_init(&pf->switch_mutex);
9827
9828sw_init_done:
9829 return err;
9830}
9831
9832/**
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00009833 * i40e_set_ntuple - set the ntuple feature flag and take action
9834 * @pf: board private structure to initialize
9835 * @features: the feature set that the stack is suggesting
9836 *
9837 * returns a bool to indicate if reset needs to happen
9838 **/
9839bool i40e_set_ntuple(struct i40e_pf *pf, netdev_features_t features)
9840{
9841 bool need_reset = false;
9842
9843 /* Check if Flow Director n-tuple support was enabled or disabled. If
9844 * the state changed, we need to reset.
9845 */
9846 if (features & NETIF_F_NTUPLE) {
9847 /* Enable filters and mark for reset */
9848 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
9849 need_reset = true;
Tushar Davea70e4072016-05-16 12:40:53 -07009850 /* enable FD_SB only if there is MSI-X vector */
9851 if (pf->num_fdsb_msix > 0)
9852 pf->flags |= I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00009853 } else {
9854 /* turn off filters, mark for reset and clear SW filter list */
9855 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
9856 need_reset = true;
9857 i40e_fdir_filter_exit(pf);
9858 }
Jacob Keller47994c12017-04-19 09:25:57 -04009859 pf->flags &= ~(I40E_FLAG_FD_SB_ENABLED |
9860 I40E_FLAG_FD_SB_AUTO_DISABLED);
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00009861 /* reset fd counters */
Jacob Keller097dbf52017-02-06 14:38:46 -08009862 pf->fd_add_err = 0;
9863 pf->fd_atr_cnt = 0;
Anjali Singhai Jain8a4f34f2014-06-04 08:45:20 +00009864 /* if ATR was auto disabled it can be re-enabled. */
Jacob Keller47994c12017-04-19 09:25:57 -04009865 if (pf->flags & I40E_FLAG_FD_ATR_AUTO_DISABLED) {
9866 pf->flags &= ~I40E_FLAG_FD_ATR_AUTO_DISABLED;
9867 if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
9868 (I40E_DEBUG_FD & pf->hw.debug_mask))
Jacob Keller234dc4e2016-09-06 18:05:09 -07009869 dev_info(&pf->pdev->dev, "ATR re-enabled.\n");
9870 }
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00009871 }
9872 return need_reset;
9873}
9874
9875/**
Alan Bradyd8ec9862016-07-27 12:02:38 -07009876 * i40e_clear_rss_lut - clear the rx hash lookup table
9877 * @vsi: the VSI being configured
9878 **/
9879static void i40e_clear_rss_lut(struct i40e_vsi *vsi)
9880{
9881 struct i40e_pf *pf = vsi->back;
9882 struct i40e_hw *hw = &pf->hw;
9883 u16 vf_id = vsi->vf_id;
9884 u8 i;
9885
9886 if (vsi->type == I40E_VSI_MAIN) {
9887 for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
9888 wr32(hw, I40E_PFQF_HLUT(i), 0);
9889 } else if (vsi->type == I40E_VSI_SRIOV) {
9890 for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
9891 i40e_write_rx_ctl(hw, I40E_VFQF_HLUT1(i, vf_id), 0);
9892 } else {
9893 dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
9894 }
9895}
9896
9897/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009898 * i40e_set_features - set the netdev feature flags
9899 * @netdev: ptr to the netdev being adjusted
9900 * @features: the feature set that the stack is suggesting
Maciej Sosin373149f2017-04-05 07:50:55 -04009901 * Note: expects to be called while under rtnl_lock()
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009902 **/
9903static int i40e_set_features(struct net_device *netdev,
9904 netdev_features_t features)
9905{
9906 struct i40e_netdev_priv *np = netdev_priv(netdev);
9907 struct i40e_vsi *vsi = np->vsi;
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00009908 struct i40e_pf *pf = vsi->back;
9909 bool need_reset;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009910
Alan Bradyd8ec9862016-07-27 12:02:38 -07009911 if (features & NETIF_F_RXHASH && !(netdev->features & NETIF_F_RXHASH))
9912 i40e_pf_config_rss(pf);
9913 else if (!(features & NETIF_F_RXHASH) &&
9914 netdev->features & NETIF_F_RXHASH)
9915 i40e_clear_rss_lut(vsi);
9916
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009917 if (features & NETIF_F_HW_VLAN_CTAG_RX)
9918 i40e_vlan_stripping_enable(vsi);
9919 else
9920 i40e_vlan_stripping_disable(vsi);
9921
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00009922 need_reset = i40e_set_ntuple(pf, features);
9923
9924 if (need_reset)
Amritha Nambiarff424182017-09-07 04:00:11 -07009925 i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00009926
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009927 return 0;
9928}
9929
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009930/**
Singhai, Anjali6a899022015-12-14 12:21:18 -08009931 * i40e_get_udp_port_idx - Lookup a possibly offloaded for Rx UDP port
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009932 * @pf: board private structure
9933 * @port: The UDP port to look up
9934 *
9935 * Returns the index number or I40E_MAX_PF_UDP_OFFLOAD_PORTS if port not found
9936 **/
Jacob Kellerfe0b0cd2017-02-06 14:38:38 -08009937static u8 i40e_get_udp_port_idx(struct i40e_pf *pf, u16 port)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009938{
9939 u8 i;
9940
9941 for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
Jacob Keller27826fd2017-04-19 09:25:50 -04009942 if (pf->udp_ports[i].port == port)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009943 return i;
9944 }
9945
9946 return i;
9947}
9948
9949/**
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009950 * i40e_udp_tunnel_add - Get notifications about UDP tunnel ports that come up
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009951 * @netdev: This physical port's netdev
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009952 * @ti: Tunnel endpoint information
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009953 **/
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009954static void i40e_udp_tunnel_add(struct net_device *netdev,
9955 struct udp_tunnel_info *ti)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009956{
9957 struct i40e_netdev_priv *np = netdev_priv(netdev);
9958 struct i40e_vsi *vsi = np->vsi;
9959 struct i40e_pf *pf = vsi->back;
Jacob Kellerfe0b0cd2017-02-06 14:38:38 -08009960 u16 port = ntohs(ti->port);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009961 u8 next_idx;
9962 u8 idx;
9963
Singhai, Anjali6a899022015-12-14 12:21:18 -08009964 idx = i40e_get_udp_port_idx(pf, port);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009965
9966 /* Check if port already exists */
9967 if (idx < I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
Jacob Kellerfe0b0cd2017-02-06 14:38:38 -08009968 netdev_info(netdev, "port %d already offloaded\n", port);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009969 return;
9970 }
9971
9972 /* Now check if there is space to add the new port */
Singhai, Anjali6a899022015-12-14 12:21:18 -08009973 next_idx = i40e_get_udp_port_idx(pf, 0);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009974
9975 if (next_idx == I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009976 netdev_info(netdev, "maximum number of offloaded UDP ports reached, not adding port %d\n",
Jacob Kellerfe0b0cd2017-02-06 14:38:38 -08009977 port);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009978 return;
9979 }
9980
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009981 switch (ti->type) {
9982 case UDP_TUNNEL_TYPE_VXLAN:
9983 pf->udp_ports[next_idx].type = I40E_AQC_TUNNEL_TYPE_VXLAN;
9984 break;
9985 case UDP_TUNNEL_TYPE_GENEVE:
Jacob Kellerd36e41d2017-06-23 04:24:46 -04009986 if (!(pf->hw_features & I40E_HW_GENEVE_OFFLOAD_CAPABLE))
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009987 return;
9988 pf->udp_ports[next_idx].type = I40E_AQC_TUNNEL_TYPE_NGE;
9989 break;
9990 default:
9991 return;
9992 }
9993
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009994 /* New port: add it and mark its index in the bitmap */
Jacob Keller27826fd2017-04-19 09:25:50 -04009995 pf->udp_ports[next_idx].port = port;
Singhai, Anjali6a899022015-12-14 12:21:18 -08009996 pf->pending_udp_bitmap |= BIT_ULL(next_idx);
9997 pf->flags |= I40E_FLAG_UDP_FILTER_SYNC;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009998}
9999
10000/**
Alexander Duyck06a5f7f2016-06-16 12:22:06 -070010001 * i40e_udp_tunnel_del - Get notifications about UDP tunnel ports that go away
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +000010002 * @netdev: This physical port's netdev
Alexander Duyck06a5f7f2016-06-16 12:22:06 -070010003 * @ti: Tunnel endpoint information
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +000010004 **/
Alexander Duyck06a5f7f2016-06-16 12:22:06 -070010005static void i40e_udp_tunnel_del(struct net_device *netdev,
10006 struct udp_tunnel_info *ti)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +000010007{
10008 struct i40e_netdev_priv *np = netdev_priv(netdev);
10009 struct i40e_vsi *vsi = np->vsi;
10010 struct i40e_pf *pf = vsi->back;
Jacob Kellerfe0b0cd2017-02-06 14:38:38 -080010011 u16 port = ntohs(ti->port);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +000010012 u8 idx;
10013
Singhai, Anjali6a899022015-12-14 12:21:18 -080010014 idx = i40e_get_udp_port_idx(pf, port);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +000010015
10016 /* Check if port already exists */
Alexander Duyck06a5f7f2016-06-16 12:22:06 -070010017 if (idx >= I40E_MAX_PF_UDP_OFFLOAD_PORTS)
10018 goto not_found;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +000010019
Alexander Duyck06a5f7f2016-06-16 12:22:06 -070010020 switch (ti->type) {
10021 case UDP_TUNNEL_TYPE_VXLAN:
10022 if (pf->udp_ports[idx].type != I40E_AQC_TUNNEL_TYPE_VXLAN)
10023 goto not_found;
10024 break;
10025 case UDP_TUNNEL_TYPE_GENEVE:
10026 if (pf->udp_ports[idx].type != I40E_AQC_TUNNEL_TYPE_NGE)
10027 goto not_found;
10028 break;
10029 default:
10030 goto not_found;
Singhai, Anjali6a899022015-12-14 12:21:18 -080010031 }
10032
Alexander Duyck06a5f7f2016-06-16 12:22:06 -070010033 /* if port exists, set it to 0 (mark for deletion)
10034 * and make it pending
10035 */
Jacob Keller27826fd2017-04-19 09:25:50 -040010036 pf->udp_ports[idx].port = 0;
Alexander Duyck06a5f7f2016-06-16 12:22:06 -070010037 pf->pending_udp_bitmap |= BIT_ULL(idx);
Singhai, Anjali6a899022015-12-14 12:21:18 -080010038 pf->flags |= I40E_FLAG_UDP_FILTER_SYNC;
10039
Alexander Duyck06a5f7f2016-06-16 12:22:06 -070010040 return;
10041not_found:
10042 netdev_warn(netdev, "UDP port %d was not found, not deleting\n",
Jacob Kellerfe0b0cd2017-02-06 14:38:38 -080010043 port);
Singhai, Anjali6a899022015-12-14 12:21:18 -080010044}
10045
Neerav Parikh1f224ad2014-02-12 01:45:31 +000010046static int i40e_get_phys_port_id(struct net_device *netdev,
Jiri Pirko02637fc2014-11-28 14:34:16 +010010047 struct netdev_phys_item_id *ppid)
Neerav Parikh1f224ad2014-02-12 01:45:31 +000010048{
10049 struct i40e_netdev_priv *np = netdev_priv(netdev);
10050 struct i40e_pf *pf = np->vsi->back;
10051 struct i40e_hw *hw = &pf->hw;
10052
Jacob Kellerd36e41d2017-06-23 04:24:46 -040010053 if (!(pf->hw_features & I40E_HW_PORT_ID_VALID))
Neerav Parikh1f224ad2014-02-12 01:45:31 +000010054 return -EOPNOTSUPP;
10055
10056 ppid->id_len = min_t(int, sizeof(hw->mac.port_addr), sizeof(ppid->id));
10057 memcpy(ppid->id, hw->mac.port_addr, ppid->id_len);
10058
10059 return 0;
10060}
10061
Jesse Brandeburg2f90ade2014-11-20 16:30:02 -080010062/**
10063 * i40e_ndo_fdb_add - add an entry to the hardware database
10064 * @ndm: the input from the stack
10065 * @tb: pointer to array of nladdr (unused)
10066 * @dev: the net device pointer
10067 * @addr: the MAC address entry being added
10068 * @flags: instructions from stack about fdb operation
10069 */
Greg Rose4ba0dea2014-03-06 08:59:55 +000010070static int i40e_ndo_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
10071 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +010010072 const unsigned char *addr, u16 vid,
Greg Rose4ba0dea2014-03-06 08:59:55 +000010073 u16 flags)
Greg Rose4ba0dea2014-03-06 08:59:55 +000010074{
10075 struct i40e_netdev_priv *np = netdev_priv(dev);
10076 struct i40e_pf *pf = np->vsi->back;
10077 int err = 0;
10078
10079 if (!(pf->flags & I40E_FLAG_SRIOV_ENABLED))
10080 return -EOPNOTSUPP;
10081
Or Gerlitz65891fe2014-12-14 18:19:05 +020010082 if (vid) {
10083 pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
10084 return -EINVAL;
10085 }
10086
Greg Rose4ba0dea2014-03-06 08:59:55 +000010087 /* Hardware does not support aging addresses so if a
10088 * ndm_state is given only allow permanent addresses
10089 */
10090 if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
10091 netdev_info(dev, "FDB only supports static addresses\n");
10092 return -EINVAL;
10093 }
10094
10095 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
10096 err = dev_uc_add_excl(dev, addr);
10097 else if (is_multicast_ether_addr(addr))
10098 err = dev_mc_add_excl(dev, addr);
10099 else
10100 err = -EINVAL;
10101
10102 /* Only return duplicate errors if NLM_F_EXCL is set */
10103 if (err == -EEXIST && !(flags & NLM_F_EXCL))
10104 err = 0;
10105
10106 return err;
10107}
10108
Neerav Parikh51616012015-02-06 08:52:14 +000010109/**
10110 * i40e_ndo_bridge_setlink - Set the hardware bridge mode
10111 * @dev: the netdev being configured
10112 * @nlh: RTNL message
10113 *
10114 * Inserts a new hardware bridge if not already created and
10115 * enables the bridging mode requested (VEB or VEPA). If the
10116 * hardware bridge has already been inserted and the request
10117 * is to change the mode then that requires a PF reset to
10118 * allow rebuild of the components with required hardware
10119 * bridge mode enabled.
Maciej Sosin373149f2017-04-05 07:50:55 -040010120 *
10121 * Note: expects to be called while under rtnl_lock()
Neerav Parikh51616012015-02-06 08:52:14 +000010122 **/
10123static int i40e_ndo_bridge_setlink(struct net_device *dev,
Carolyn Wyborny9df70b62015-04-27 14:57:11 -040010124 struct nlmsghdr *nlh,
10125 u16 flags)
Neerav Parikh51616012015-02-06 08:52:14 +000010126{
10127 struct i40e_netdev_priv *np = netdev_priv(dev);
10128 struct i40e_vsi *vsi = np->vsi;
10129 struct i40e_pf *pf = vsi->back;
10130 struct i40e_veb *veb = NULL;
10131 struct nlattr *attr, *br_spec;
10132 int i, rem;
10133
10134 /* Only for PF VSI for now */
10135 if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
10136 return -EOPNOTSUPP;
10137
10138 /* Find the HW bridge for PF VSI */
10139 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
10140 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
10141 veb = pf->veb[i];
10142 }
10143
10144 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
10145
10146 nla_for_each_nested(attr, br_spec, rem) {
10147 __u16 mode;
10148
10149 if (nla_type(attr) != IFLA_BRIDGE_MODE)
10150 continue;
10151
10152 mode = nla_get_u16(attr);
10153 if ((mode != BRIDGE_MODE_VEPA) &&
10154 (mode != BRIDGE_MODE_VEB))
10155 return -EINVAL;
10156
10157 /* Insert a new HW bridge */
10158 if (!veb) {
10159 veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
10160 vsi->tc_config.enabled_tc);
10161 if (veb) {
10162 veb->bridge_mode = mode;
10163 i40e_config_bridge_mode(veb);
10164 } else {
10165 /* No Bridge HW offload available */
10166 return -ENOENT;
10167 }
10168 break;
10169 } else if (mode != veb->bridge_mode) {
10170 /* Existing HW bridge but different mode needs reset */
10171 veb->bridge_mode = mode;
Anjali Singhai Jainfc608612015-05-08 15:35:57 -070010172 /* TODO: If no VFs or VMDq VSIs, disallow VEB mode */
10173 if (mode == BRIDGE_MODE_VEB)
10174 pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
10175 else
10176 pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
Amritha Nambiarff424182017-09-07 04:00:11 -070010177 i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
Neerav Parikh51616012015-02-06 08:52:14 +000010178 break;
10179 }
10180 }
10181
10182 return 0;
10183}
10184
10185/**
10186 * i40e_ndo_bridge_getlink - Get the hardware bridge mode
10187 * @skb: skb buff
10188 * @pid: process id
10189 * @seq: RTNL message seq #
10190 * @dev: the netdev being configured
10191 * @filter_mask: unused
Jesse Brandeburgd4b2f9f2015-09-03 17:18:48 -040010192 * @nlflags: netlink flags passed in
Neerav Parikh51616012015-02-06 08:52:14 +000010193 *
10194 * Return the mode in which the hardware bridge is operating in
10195 * i.e VEB or VEPA.
10196 **/
Neerav Parikh51616012015-02-06 08:52:14 +000010197static int i40e_ndo_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
10198 struct net_device *dev,
Carolyn Wyborny9f4ffc42015-08-31 19:54:42 -040010199 u32 __always_unused filter_mask,
10200 int nlflags)
Neerav Parikh51616012015-02-06 08:52:14 +000010201{
10202 struct i40e_netdev_priv *np = netdev_priv(dev);
10203 struct i40e_vsi *vsi = np->vsi;
10204 struct i40e_pf *pf = vsi->back;
10205 struct i40e_veb *veb = NULL;
10206 int i;
10207
10208 /* Only for PF VSI for now */
10209 if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
10210 return -EOPNOTSUPP;
10211
10212 /* Find the HW bridge for the PF VSI */
10213 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
10214 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
10215 veb = pf->veb[i];
10216 }
10217
10218 if (!veb)
10219 return 0;
10220
Nicolas Dichtel46c264d2015-04-28 18:33:49 +020010221 return ndo_dflt_bridge_getlink(skb, pid, seq, dev, veb->bridge_mode,
Huaibin Wang599b0762016-09-26 09:51:18 +020010222 0, 0, nlflags, filter_mask, NULL);
Neerav Parikh51616012015-02-06 08:52:14 +000010223}
Neerav Parikh51616012015-02-06 08:52:14 +000010224
Joe Stringerf44a75e2015-04-14 17:09:14 -070010225/**
10226 * i40e_features_check - Validate encapsulated packet conforms to limits
10227 * @skb: skb buff
Jean Sacren2bc11c62015-09-19 05:08:43 -060010228 * @dev: This physical port's netdev
Joe Stringerf44a75e2015-04-14 17:09:14 -070010229 * @features: Offload features that the stack believes apply
10230 **/
10231static netdev_features_t i40e_features_check(struct sk_buff *skb,
10232 struct net_device *dev,
10233 netdev_features_t features)
10234{
Alexander Duyckf114dca2016-10-25 16:08:46 -070010235 size_t len;
10236
10237 /* No point in doing any of this if neither checksum nor GSO are
10238 * being requested for this frame. We can rule out both by just
10239 * checking for CHECKSUM_PARTIAL
10240 */
10241 if (skb->ip_summed != CHECKSUM_PARTIAL)
10242 return features;
10243
10244 /* We cannot support GSO if the MSS is going to be less than
10245 * 64 bytes. If it is then we need to drop support for GSO.
10246 */
10247 if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
10248 features &= ~NETIF_F_GSO_MASK;
10249
10250 /* MACLEN can support at most 63 words */
10251 len = skb_network_header(skb) - skb->data;
10252 if (len & ~(63 * 2))
10253 goto out_err;
10254
10255 /* IPLEN and EIPLEN can support at most 127 dwords */
10256 len = skb_transport_header(skb) - skb_network_header(skb);
10257 if (len & ~(127 * 4))
10258 goto out_err;
10259
10260 if (skb->encapsulation) {
10261 /* L4TUNLEN can support 127 words */
10262 len = skb_inner_network_header(skb) - skb_transport_header(skb);
10263 if (len & ~(127 * 2))
10264 goto out_err;
10265
10266 /* IPLEN can support at most 127 dwords */
10267 len = skb_inner_transport_header(skb) -
10268 skb_inner_network_header(skb);
10269 if (len & ~(127 * 4))
10270 goto out_err;
10271 }
10272
10273 /* No need to validate L4LEN as TCP is the only protocol with a
10274 * a flexible value and we support all possible values supported
10275 * by TCP, which is at most 15 dwords
10276 */
Joe Stringerf44a75e2015-04-14 17:09:14 -070010277
10278 return features;
Alexander Duyckf114dca2016-10-25 16:08:46 -070010279out_err:
10280 return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
Joe Stringerf44a75e2015-04-14 17:09:14 -070010281}
10282
Björn Töpel0c8493d2017-05-24 07:55:34 +020010283/**
10284 * i40e_xdp_setup - add/remove an XDP program
10285 * @vsi: VSI to changed
10286 * @prog: XDP program
10287 **/
10288static int i40e_xdp_setup(struct i40e_vsi *vsi,
10289 struct bpf_prog *prog)
10290{
10291 int frame_size = vsi->netdev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
10292 struct i40e_pf *pf = vsi->back;
10293 struct bpf_prog *old_prog;
10294 bool need_reset;
10295 int i;
10296
10297 /* Don't allow frames that span over multiple buffers */
10298 if (frame_size > vsi->rx_buf_len)
10299 return -EINVAL;
10300
10301 if (!i40e_enabled_xdp_vsi(vsi) && !prog)
10302 return 0;
10303
10304 /* When turning XDP on->off/off->on we reset and rebuild the rings. */
10305 need_reset = (i40e_enabled_xdp_vsi(vsi) != !!prog);
10306
10307 if (need_reset)
10308 i40e_prep_for_reset(pf, true);
10309
10310 old_prog = xchg(&vsi->xdp_prog, prog);
10311
10312 if (need_reset)
10313 i40e_reset_and_rebuild(pf, true, true);
10314
10315 for (i = 0; i < vsi->num_queue_pairs; i++)
10316 WRITE_ONCE(vsi->rx_rings[i]->xdp_prog, vsi->xdp_prog);
10317
10318 if (old_prog)
10319 bpf_prog_put(old_prog);
10320
10321 return 0;
10322}
10323
10324/**
10325 * i40e_xdp - implements ndo_xdp for i40e
10326 * @dev: netdevice
10327 * @xdp: XDP command
10328 **/
10329static int i40e_xdp(struct net_device *dev,
10330 struct netdev_xdp *xdp)
10331{
10332 struct i40e_netdev_priv *np = netdev_priv(dev);
10333 struct i40e_vsi *vsi = np->vsi;
10334
10335 if (vsi->type != I40E_VSI_MAIN)
10336 return -EINVAL;
10337
10338 switch (xdp->command) {
10339 case XDP_SETUP_PROG:
10340 return i40e_xdp_setup(vsi, xdp->prog);
10341 case XDP_QUERY_PROG:
10342 xdp->prog_attached = i40e_enabled_xdp_vsi(vsi);
Daniel Borkmanneb230392017-06-24 21:13:52 +020010343 xdp->prog_id = vsi->xdp_prog ? vsi->xdp_prog->aux->id : 0;
Björn Töpel0c8493d2017-05-24 07:55:34 +020010344 return 0;
10345 default:
10346 return -EINVAL;
10347 }
10348}
10349
Shannon Nelson37a29732015-02-27 09:15:19 +000010350static const struct net_device_ops i40e_netdev_ops = {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010351 .ndo_open = i40e_open,
10352 .ndo_stop = i40e_close,
10353 .ndo_start_xmit = i40e_lan_xmit_frame,
10354 .ndo_get_stats64 = i40e_get_netdev_stats_struct,
10355 .ndo_set_rx_mode = i40e_set_rx_mode,
10356 .ndo_validate_addr = eth_validate_addr,
10357 .ndo_set_mac_address = i40e_set_mac,
10358 .ndo_change_mtu = i40e_change_mtu,
Jacob Kellerbeb0dff2014-01-11 05:43:19 +000010359 .ndo_do_ioctl = i40e_ioctl,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010360 .ndo_tx_timeout = i40e_tx_timeout,
10361 .ndo_vlan_rx_add_vid = i40e_vlan_rx_add_vid,
10362 .ndo_vlan_rx_kill_vid = i40e_vlan_rx_kill_vid,
10363#ifdef CONFIG_NET_POLL_CONTROLLER
10364 .ndo_poll_controller = i40e_netpoll,
10365#endif
John Fastabende4c67342016-02-16 21:16:15 -080010366 .ndo_setup_tc = __i40e_setup_tc,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010367 .ndo_set_features = i40e_set_features,
10368 .ndo_set_vf_mac = i40e_ndo_set_vf_mac,
10369 .ndo_set_vf_vlan = i40e_ndo_set_vf_port_vlan,
Sucheta Chakrabortyed616682014-05-22 09:59:05 -040010370 .ndo_set_vf_rate = i40e_ndo_set_vf_bw,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010371 .ndo_get_vf_config = i40e_ndo_get_vf_config,
Mitch Williams588aefa2014-02-11 08:27:49 +000010372 .ndo_set_vf_link_state = i40e_ndo_set_vf_link_state,
Serey Konge6d90042014-07-12 07:28:14 +000010373 .ndo_set_vf_spoofchk = i40e_ndo_set_vf_spoofchk,
Anjali Singhai Jainc3bbbd22016-04-01 03:56:07 -070010374 .ndo_set_vf_trust = i40e_ndo_set_vf_trust,
Alexander Duyck06a5f7f2016-06-16 12:22:06 -070010375 .ndo_udp_tunnel_add = i40e_udp_tunnel_add,
10376 .ndo_udp_tunnel_del = i40e_udp_tunnel_del,
Neerav Parikh1f224ad2014-02-12 01:45:31 +000010377 .ndo_get_phys_port_id = i40e_get_phys_port_id,
Greg Rose4ba0dea2014-03-06 08:59:55 +000010378 .ndo_fdb_add = i40e_ndo_fdb_add,
Joe Stringerf44a75e2015-04-14 17:09:14 -070010379 .ndo_features_check = i40e_features_check,
Neerav Parikh51616012015-02-06 08:52:14 +000010380 .ndo_bridge_getlink = i40e_ndo_bridge_getlink,
10381 .ndo_bridge_setlink = i40e_ndo_bridge_setlink,
Björn Töpel0c8493d2017-05-24 07:55:34 +020010382 .ndo_xdp = i40e_xdp,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010383};
10384
10385/**
10386 * i40e_config_netdev - Setup the netdev flags
10387 * @vsi: the VSI being configured
10388 *
10389 * Returns 0 on success, negative value on failure
10390 **/
10391static int i40e_config_netdev(struct i40e_vsi *vsi)
10392{
10393 struct i40e_pf *pf = vsi->back;
10394 struct i40e_hw *hw = &pf->hw;
10395 struct i40e_netdev_priv *np;
10396 struct net_device *netdev;
Jacob Keller435c0842016-11-08 13:05:10 -080010397 u8 broadcast[ETH_ALEN];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010398 u8 mac_addr[ETH_ALEN];
10399 int etherdev_size;
Preethi Banalabacd75c2017-03-27 14:43:18 -070010400 netdev_features_t hw_enc_features;
10401 netdev_features_t hw_features;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010402
10403 etherdev_size = sizeof(struct i40e_netdev_priv);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +000010404 netdev = alloc_etherdev_mq(etherdev_size, vsi->alloc_queue_pairs);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010405 if (!netdev)
10406 return -ENOMEM;
10407
10408 vsi->netdev = netdev;
10409 np = netdev_priv(netdev);
10410 np->vsi = vsi;
10411
Preethi Banalabacd75c2017-03-27 14:43:18 -070010412 hw_enc_features = NETIF_F_SG |
10413 NETIF_F_IP_CSUM |
10414 NETIF_F_IPV6_CSUM |
10415 NETIF_F_HIGHDMA |
10416 NETIF_F_SOFT_FEATURES |
10417 NETIF_F_TSO |
10418 NETIF_F_TSO_ECN |
10419 NETIF_F_TSO6 |
10420 NETIF_F_GSO_GRE |
10421 NETIF_F_GSO_GRE_CSUM |
10422 NETIF_F_GSO_PARTIAL |
10423 NETIF_F_GSO_UDP_TUNNEL |
10424 NETIF_F_GSO_UDP_TUNNEL_CSUM |
10425 NETIF_F_SCTP_CRC |
10426 NETIF_F_RXHASH |
10427 NETIF_F_RXCSUM |
10428 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010429
Jacob Kellerd36e41d2017-06-23 04:24:46 -040010430 if (!(pf->hw_features & I40E_HW_OUTER_UDP_CSUM_CAPABLE))
Alexander Duyck1c7b4a22016-04-14 17:19:25 -040010431 netdev->gso_partial_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM;
10432
10433 netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
Alexander Duyckb0fe3302016-04-02 00:05:14 -070010434
Preethi Banalabacd75c2017-03-27 14:43:18 -070010435 netdev->hw_enc_features |= hw_enc_features;
10436
Alexander Duyckb0fe3302016-04-02 00:05:14 -070010437 /* record features VLANs can make use of */
Preethi Banalabacd75c2017-03-27 14:43:18 -070010438 netdev->vlan_features |= hw_enc_features | NETIF_F_TSO_MANGLEID;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010439
Anjali Singhai Jain2e86a0b2014-04-01 07:11:53 +000010440 if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
Alexander Duyckb0fe3302016-04-02 00:05:14 -070010441 netdev->hw_features |= NETIF_F_NTUPLE;
Preethi Banalabacd75c2017-03-27 14:43:18 -070010442 hw_features = hw_enc_features |
10443 NETIF_F_HW_VLAN_CTAG_TX |
10444 NETIF_F_HW_VLAN_CTAG_RX;
Anjali Singhai Jain2e86a0b2014-04-01 07:11:53 +000010445
Preethi Banalabacd75c2017-03-27 14:43:18 -070010446 netdev->hw_features |= hw_features;
Alexander Duyckb0fe3302016-04-02 00:05:14 -070010447
Preethi Banalabacd75c2017-03-27 14:43:18 -070010448 netdev->features |= hw_features | NETIF_F_HW_VLAN_CTAG_FILTER;
Alexander Duyck1c7b4a22016-04-14 17:19:25 -040010449 netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010450
10451 if (vsi->type == I40E_VSI_MAIN) {
10452 SET_NETDEV_DEV(netdev, &pf->pdev->dev);
Greg Rose9a173902014-05-22 06:32:02 +000010453 ether_addr_copy(mac_addr, hw->mac.perm_addr);
Jacob Keller41c4c2b2017-04-05 07:50:57 -040010454 /* The following steps are necessary for two reasons. First,
10455 * some older NVM configurations load a default MAC-VLAN
10456 * filter that will accept any tagged packet, and we want to
10457 * replace this with a normal filter. Additionally, it is
10458 * possible our MAC address was provided by the platform using
10459 * Open Firmware or similar.
10460 *
10461 * Thus, we need to remove the default filter and install one
10462 * specific to the MAC address.
Jacob Keller1596b5d2016-11-08 13:05:15 -080010463 */
10464 i40e_rm_default_mac_filter(vsi, mac_addr);
Jacob Keller278e7d02016-10-05 09:30:37 -070010465 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller9569a9a2016-11-11 12:39:37 -080010466 i40e_add_mac_filter(vsi, mac_addr);
Jacob Keller278e7d02016-10-05 09:30:37 -070010467 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010468 } else {
Jacob Keller8c9eb352017-07-12 05:46:12 -040010469 /* Relate the VSI_VMDQ name to the VSI_MAIN name. Note that we
10470 * are still limited by IFNAMSIZ, but we're adding 'v%d\0' to
10471 * the end, which is 4 bytes long, so force truncation of the
10472 * original name by IFNAMSIZ - 4
10473 */
10474 snprintf(netdev->name, IFNAMSIZ, "%.*sv%%d",
10475 IFNAMSIZ - 4,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010476 pf->vsi[pf->lan_vsi]->netdev->name);
10477 random_ether_addr(mac_addr);
Kiran Patil21659032015-09-30 14:09:03 -040010478
Jacob Keller278e7d02016-10-05 09:30:37 -070010479 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller9569a9a2016-11-11 12:39:37 -080010480 i40e_add_mac_filter(vsi, mac_addr);
Jacob Keller278e7d02016-10-05 09:30:37 -070010481 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010482 }
Kiran Patil21659032015-09-30 14:09:03 -040010483
Jacob Keller435c0842016-11-08 13:05:10 -080010484 /* Add the broadcast filter so that we initially will receive
10485 * broadcast packets. Note that when a new VLAN is first added the
10486 * driver will convert all filters marked I40E_VLAN_ANY into VLAN
10487 * specific filters as part of transitioning into "vlan" operation.
10488 * When more VLANs are added, the driver will copy each existing MAC
10489 * filter and add it for the new VLAN.
10490 *
10491 * Broadcast filters are handled specially by
10492 * i40e_sync_filters_subtask, as the driver must to set the broadcast
10493 * promiscuous bit instead of adding this directly as a MAC/VLAN
10494 * filter. The subtask will update the correct broadcast promiscuous
10495 * bits as VLANs become active or inactive.
10496 */
10497 eth_broadcast_addr(broadcast);
10498 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller9569a9a2016-11-11 12:39:37 -080010499 i40e_add_mac_filter(vsi, broadcast);
Jacob Keller435c0842016-11-08 13:05:10 -080010500 spin_unlock_bh(&vsi->mac_filter_hash_lock);
10501
Greg Rose9a173902014-05-22 06:32:02 +000010502 ether_addr_copy(netdev->dev_addr, mac_addr);
10503 ether_addr_copy(netdev->perm_addr, mac_addr);
Alexander Duyckb0fe3302016-04-02 00:05:14 -070010504
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010505 netdev->priv_flags |= IFF_UNICAST_FLT;
10506 netdev->priv_flags |= IFF_SUPP_NOFCS;
10507 /* Setup netdev TC information */
10508 i40e_vsi_config_netdev_tc(vsi, vsi->tc_config.enabled_tc);
10509
10510 netdev->netdev_ops = &i40e_netdev_ops;
10511 netdev->watchdog_timeo = 5 * HZ;
10512 i40e_set_ethtool_ops(netdev);
10513
Jarod Wilson91c527a2016-10-17 15:54:05 -040010514 /* MTU range: 68 - 9706 */
10515 netdev->min_mtu = ETH_MIN_MTU;
Mitch Williams1e3a5fd2017-06-23 04:24:43 -040010516 netdev->max_mtu = I40E_MAX_RXBUFFER - I40E_PACKET_HDR_PAD;
Jarod Wilson91c527a2016-10-17 15:54:05 -040010517
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010518 return 0;
10519}
10520
10521/**
10522 * i40e_vsi_delete - Delete a VSI from the switch
10523 * @vsi: the VSI being removed
10524 *
10525 * Returns 0 on success, negative value on failure
10526 **/
10527static void i40e_vsi_delete(struct i40e_vsi *vsi)
10528{
10529 /* remove default VSI is not allowed */
10530 if (vsi == vsi->back->vsi[vsi->back->lan_vsi])
10531 return;
10532
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010533 i40e_aq_delete_element(&vsi->back->hw, vsi->seid, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010534}
10535
10536/**
Neerav Parikh51616012015-02-06 08:52:14 +000010537 * i40e_is_vsi_uplink_mode_veb - Check if the VSI's uplink bridge mode is VEB
10538 * @vsi: the VSI being queried
10539 *
10540 * Returns 1 if HW bridge mode is VEB and return 0 in case of VEPA mode
10541 **/
10542int i40e_is_vsi_uplink_mode_veb(struct i40e_vsi *vsi)
10543{
10544 struct i40e_veb *veb;
10545 struct i40e_pf *pf = vsi->back;
10546
10547 /* Uplink is not a bridge so default to VEB */
10548 if (vsi->veb_idx == I40E_NO_VEB)
10549 return 1;
10550
10551 veb = pf->veb[vsi->veb_idx];
Akeem G Abodunrin09603ea2015-10-01 14:37:36 -040010552 if (!veb) {
10553 dev_info(&pf->pdev->dev,
10554 "There is no veb associated with the bridge\n");
10555 return -ENOENT;
10556 }
Neerav Parikh51616012015-02-06 08:52:14 +000010557
Akeem G Abodunrin09603ea2015-10-01 14:37:36 -040010558 /* Uplink is a bridge in VEPA mode */
10559 if (veb->bridge_mode & BRIDGE_MODE_VEPA) {
10560 return 0;
10561 } else {
10562 /* Uplink is a bridge in VEB mode */
10563 return 1;
10564 }
10565
10566 /* VEPA is now default bridge, so return 0 */
10567 return 0;
Neerav Parikh51616012015-02-06 08:52:14 +000010568}
10569
10570/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010571 * i40e_add_vsi - Add a VSI to the switch
10572 * @vsi: the VSI being configured
10573 *
10574 * This initializes a VSI context depending on the VSI type to be added and
10575 * passes it down to the add_vsi aq command.
10576 **/
10577static int i40e_add_vsi(struct i40e_vsi *vsi)
10578{
10579 int ret = -ENODEV;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010580 struct i40e_pf *pf = vsi->back;
10581 struct i40e_hw *hw = &pf->hw;
10582 struct i40e_vsi_context ctxt;
Jacob Keller278e7d02016-10-05 09:30:37 -070010583 struct i40e_mac_filter *f;
10584 struct hlist_node *h;
10585 int bkt;
Kiran Patil21659032015-09-30 14:09:03 -040010586
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010587 u8 enabled_tc = 0x1; /* TC0 enabled */
10588 int f_count = 0;
10589
10590 memset(&ctxt, 0, sizeof(ctxt));
10591 switch (vsi->type) {
10592 case I40E_VSI_MAIN:
10593 /* The PF's main VSI is already setup as part of the
10594 * device initialization, so we'll not bother with
10595 * the add_vsi call, but we will retrieve the current
10596 * VSI context.
10597 */
10598 ctxt.seid = pf->main_vsi_seid;
10599 ctxt.pf_num = pf->hw.pf_id;
10600 ctxt.vf_num = 0;
10601 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
10602 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
10603 if (ret) {
10604 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010605 "couldn't get PF vsi config, err %s aq_err %s\n",
10606 i40e_stat_str(&pf->hw, ret),
10607 i40e_aq_str(&pf->hw,
10608 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010609 return -ENOENT;
10610 }
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -070010611 vsi->info = ctxt.info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010612 vsi->info.valid_sections = 0;
10613
10614 vsi->seid = ctxt.seid;
10615 vsi->id = ctxt.vsi_number;
10616
10617 enabled_tc = i40e_pf_get_tc_map(pf);
10618
Mitch Williams64615b52017-08-29 05:32:30 -040010619 /* Source pruning is enabled by default, so the flag is
10620 * negative logic - if it's set, we need to fiddle with
10621 * the VSI to disable source pruning.
10622 */
10623 if (pf->flags & I40E_FLAG_SOURCE_PRUNING_DISABLED) {
10624 memset(&ctxt, 0, sizeof(ctxt));
10625 ctxt.seid = pf->main_vsi_seid;
10626 ctxt.pf_num = pf->hw.pf_id;
10627 ctxt.vf_num = 0;
10628 ctxt.info.valid_sections |=
10629 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
10630 ctxt.info.switch_id =
10631 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_LOCAL_LB);
10632 ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
10633 if (ret) {
10634 dev_info(&pf->pdev->dev,
10635 "update vsi failed, err %s aq_err %s\n",
10636 i40e_stat_str(&pf->hw, ret),
10637 i40e_aq_str(&pf->hw,
10638 pf->hw.aq.asq_last_status));
10639 ret = -ENOENT;
10640 goto err;
10641 }
10642 }
10643
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010644 /* MFP mode setup queue map and update VSI */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +000010645 if ((pf->flags & I40E_FLAG_MFP_ENABLED) &&
10646 !(pf->hw.func_caps.iscsi)) { /* NIC type PF */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010647 memset(&ctxt, 0, sizeof(ctxt));
10648 ctxt.seid = pf->main_vsi_seid;
10649 ctxt.pf_num = pf->hw.pf_id;
10650 ctxt.vf_num = 0;
10651 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
10652 ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
10653 if (ret) {
10654 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010655 "update vsi failed, err %s aq_err %s\n",
10656 i40e_stat_str(&pf->hw, ret),
10657 i40e_aq_str(&pf->hw,
10658 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010659 ret = -ENOENT;
10660 goto err;
10661 }
10662 /* update the local VSI info queue map */
10663 i40e_vsi_update_queue_map(vsi, &ctxt);
10664 vsi->info.valid_sections = 0;
10665 } else {
10666 /* Default/Main VSI is only enabled for TC0
10667 * reconfigure it to enable all TCs that are
10668 * available on the port in SFP mode.
Neerav Parikh63d7e5a2014-12-14 01:55:16 +000010669 * For MFP case the iSCSI PF would use this
10670 * flow to enable LAN+iSCSI TC.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010671 */
10672 ret = i40e_vsi_config_tc(vsi, enabled_tc);
10673 if (ret) {
Carolyn Wyborny19279232017-07-14 09:10:07 -040010674 /* Single TC condition is not fatal,
10675 * message and continue
10676 */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010677 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010678 "failed to configure TCs for main VSI tc_map 0x%08x, err %s aq_err %s\n",
10679 enabled_tc,
10680 i40e_stat_str(&pf->hw, ret),
10681 i40e_aq_str(&pf->hw,
10682 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010683 }
10684 }
10685 break;
10686
10687 case I40E_VSI_FDIR:
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010688 ctxt.pf_num = hw->pf_id;
10689 ctxt.vf_num = 0;
10690 ctxt.uplink_seid = vsi->uplink_seid;
Neerav Parikh2b18e592015-01-24 09:58:38 +000010691 ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010692 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
Anjali Singhai Jainfc608612015-05-08 15:35:57 -070010693 if ((pf->flags & I40E_FLAG_VEB_MODE_ENABLED) &&
10694 (i40e_is_vsi_uplink_mode_veb(vsi))) {
Neerav Parikh51616012015-02-06 08:52:14 +000010695 ctxt.info.valid_sections |=
Anjali Singhai Jainfc608612015-05-08 15:35:57 -070010696 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
Neerav Parikh51616012015-02-06 08:52:14 +000010697 ctxt.info.switch_id =
Anjali Singhai Jainfc608612015-05-08 15:35:57 -070010698 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
Neerav Parikh51616012015-02-06 08:52:14 +000010699 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010700 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010701 break;
10702
10703 case I40E_VSI_VMDQ2:
10704 ctxt.pf_num = hw->pf_id;
10705 ctxt.vf_num = 0;
10706 ctxt.uplink_seid = vsi->uplink_seid;
Neerav Parikh2b18e592015-01-24 09:58:38 +000010707 ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010708 ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
10709
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010710 /* This VSI is connected to VEB so the switch_id
10711 * should be set to zero by default.
10712 */
Neerav Parikh51616012015-02-06 08:52:14 +000010713 if (i40e_is_vsi_uplink_mode_veb(vsi)) {
10714 ctxt.info.valid_sections |=
10715 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
10716 ctxt.info.switch_id =
10717 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
10718 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010719
10720 /* Setup the VSI tx/rx queue map for TC0 only for now */
10721 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
10722 break;
10723
10724 case I40E_VSI_SRIOV:
10725 ctxt.pf_num = hw->pf_id;
10726 ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
10727 ctxt.uplink_seid = vsi->uplink_seid;
Neerav Parikh2b18e592015-01-24 09:58:38 +000010728 ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010729 ctxt.flags = I40E_AQ_VSI_TYPE_VF;
10730
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010731 /* This VSI is connected to VEB so the switch_id
10732 * should be set to zero by default.
10733 */
Neerav Parikh51616012015-02-06 08:52:14 +000010734 if (i40e_is_vsi_uplink_mode_veb(vsi)) {
10735 ctxt.info.valid_sections |=
10736 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
10737 ctxt.info.switch_id =
10738 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
10739 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010740
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060010741 if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
10742 ctxt.info.valid_sections |=
10743 cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
10744 ctxt.info.queueing_opt_flags |=
Ashish Shah4b28cdb2016-05-03 15:13:17 -070010745 (I40E_AQ_VSI_QUE_OPT_TCP_ENA |
10746 I40E_AQ_VSI_QUE_OPT_RSS_LUT_VSI);
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060010747 }
10748
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010749 ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
10750 ctxt.info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_MODE_ALL;
Mitch Williamsc674d122014-05-20 08:01:40 +000010751 if (pf->vf[vsi->vf_id].spoofchk) {
10752 ctxt.info.valid_sections |=
10753 cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
10754 ctxt.info.sec_flags |=
10755 (I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
10756 I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
10757 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010758 /* Setup the VSI tx/rx queue map for TC0 only for now */
10759 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
10760 break;
10761
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060010762 case I40E_VSI_IWARP:
10763 /* send down message to iWARP */
10764 break;
10765
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010766 default:
10767 return -ENODEV;
10768 }
10769
10770 if (vsi->type != I40E_VSI_MAIN) {
10771 ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
10772 if (ret) {
10773 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010774 "add vsi failed, err %s aq_err %s\n",
10775 i40e_stat_str(&pf->hw, ret),
10776 i40e_aq_str(&pf->hw,
10777 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010778 ret = -ENOENT;
10779 goto err;
10780 }
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -070010781 vsi->info = ctxt.info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010782 vsi->info.valid_sections = 0;
10783 vsi->seid = ctxt.seid;
10784 vsi->id = ctxt.vsi_number;
10785 }
10786
Mitch Williamsc3c7ea22016-06-20 09:10:38 -070010787 vsi->active_filters = 0;
Jacob Keller0da36b92017-04-19 09:25:55 -040010788 clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
Jacob Keller278e7d02016-10-05 09:30:37 -070010789 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010790 /* If macvlan filters already exist, force them to get loaded */
Jacob Keller278e7d02016-10-05 09:30:37 -070010791 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -070010792 f->state = I40E_FILTER_NEW;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010793 f_count++;
10794 }
Jacob Keller278e7d02016-10-05 09:30:37 -070010795 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -040010796
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010797 if (f_count) {
10798 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
10799 pf->flags |= I40E_FLAG_FILTER_SYNC;
10800 }
10801
10802 /* Update VSI BW information */
10803 ret = i40e_vsi_get_bw_info(vsi);
10804 if (ret) {
10805 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010806 "couldn't get vsi bw info, err %s aq_err %s\n",
10807 i40e_stat_str(&pf->hw, ret),
10808 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010809 /* VSI is already added so not tearing that up */
10810 ret = 0;
10811 }
10812
10813err:
10814 return ret;
10815}
10816
10817/**
10818 * i40e_vsi_release - Delete a VSI and free its resources
10819 * @vsi: the VSI being removed
10820 *
10821 * Returns 0 on success or < 0 on error
10822 **/
10823int i40e_vsi_release(struct i40e_vsi *vsi)
10824{
Jacob Keller278e7d02016-10-05 09:30:37 -070010825 struct i40e_mac_filter *f;
10826 struct hlist_node *h;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010827 struct i40e_veb *veb = NULL;
10828 struct i40e_pf *pf;
10829 u16 uplink_seid;
Jacob Keller278e7d02016-10-05 09:30:37 -070010830 int i, n, bkt;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010831
10832 pf = vsi->back;
10833
10834 /* release of a VEB-owner or last VSI is not allowed */
10835 if (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) {
10836 dev_info(&pf->pdev->dev, "VSI %d has existing VEB %d\n",
10837 vsi->seid, vsi->uplink_seid);
10838 return -ENODEV;
10839 }
10840 if (vsi == pf->vsi[pf->lan_vsi] &&
Mauro S. M. Rodrigues9e6c9c02017-05-12 23:26:56 -030010841 !test_bit(__I40E_DOWN, pf->state)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010842 dev_info(&pf->pdev->dev, "Can't remove PF VSI\n");
10843 return -ENODEV;
10844 }
10845
10846 uplink_seid = vsi->uplink_seid;
10847 if (vsi->type != I40E_VSI_SRIOV) {
10848 if (vsi->netdev_registered) {
10849 vsi->netdev_registered = false;
10850 if (vsi->netdev) {
10851 /* results in a call to i40e_close() */
10852 unregister_netdev(vsi->netdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010853 }
10854 } else {
Shannon Nelson90ef8d42014-03-14 07:32:26 +000010855 i40e_vsi_close(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010856 }
10857 i40e_vsi_disable_irq(vsi);
10858 }
10859
Jacob Keller278e7d02016-10-05 09:30:37 -070010860 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller6622f5c2016-10-05 09:30:32 -070010861
10862 /* clear the sync flag on all filters */
10863 if (vsi->netdev) {
10864 __dev_uc_unsync(vsi->netdev, NULL);
10865 __dev_mc_unsync(vsi->netdev, NULL);
10866 }
10867
10868 /* make sure any remaining filters are marked for deletion */
Jacob Keller278e7d02016-10-05 09:30:37 -070010869 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
Jacob Keller290d2552016-10-05 09:30:36 -070010870 __i40e_del_filter(vsi, f);
Jacob Keller6622f5c2016-10-05 09:30:32 -070010871
Jacob Keller278e7d02016-10-05 09:30:37 -070010872 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -040010873
Jesse Brandeburg17652c62015-11-05 17:01:02 -080010874 i40e_sync_vsi_filters(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010875
10876 i40e_vsi_delete(vsi);
10877 i40e_vsi_free_q_vectors(vsi);
Shannon Nelsona4866592014-02-11 08:24:07 +000010878 if (vsi->netdev) {
10879 free_netdev(vsi->netdev);
10880 vsi->netdev = NULL;
10881 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010882 i40e_vsi_clear_rings(vsi);
10883 i40e_vsi_clear(vsi);
10884
10885 /* If this was the last thing on the VEB, except for the
10886 * controlling VSI, remove the VEB, which puts the controlling
10887 * VSI onto the next level down in the switch.
10888 *
10889 * Well, okay, there's one more exception here: don't remove
10890 * the orphan VEBs yet. We'll wait for an explicit remove request
10891 * from up the network stack.
10892 */
Mitch Williams505682c2014-05-20 08:01:37 +000010893 for (n = 0, i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010894 if (pf->vsi[i] &&
10895 pf->vsi[i]->uplink_seid == uplink_seid &&
10896 (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
10897 n++; /* count the VSIs */
10898 }
10899 }
10900 for (i = 0; i < I40E_MAX_VEB; i++) {
10901 if (!pf->veb[i])
10902 continue;
10903 if (pf->veb[i]->uplink_seid == uplink_seid)
10904 n++; /* count the VEBs */
10905 if (pf->veb[i]->seid == uplink_seid)
10906 veb = pf->veb[i];
10907 }
10908 if (n == 0 && veb && veb->uplink_seid != 0)
10909 i40e_veb_release(veb);
10910
10911 return 0;
10912}
10913
10914/**
10915 * i40e_vsi_setup_vectors - Set up the q_vectors for the given VSI
10916 * @vsi: ptr to the VSI
10917 *
10918 * This should only be called after i40e_vsi_mem_alloc() which allocates the
10919 * corresponding SW VSI structure and initializes num_queue_pairs for the
10920 * newly allocated VSI.
10921 *
10922 * Returns 0 on success or negative on failure
10923 **/
10924static int i40e_vsi_setup_vectors(struct i40e_vsi *vsi)
10925{
10926 int ret = -ENOENT;
10927 struct i40e_pf *pf = vsi->back;
10928
Alexander Duyck493fb302013-09-28 07:01:44 +000010929 if (vsi->q_vectors[0]) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010930 dev_info(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
10931 vsi->seid);
10932 return -EEXIST;
10933 }
10934
10935 if (vsi->base_vector) {
Jesse Brandeburgf29eaa32014-02-11 08:24:12 +000010936 dev_info(&pf->pdev->dev, "VSI %d has non-zero base vector %d\n",
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010937 vsi->seid, vsi->base_vector);
10938 return -EEXIST;
10939 }
10940
Greg Rose90e04072014-03-06 08:59:57 +000010941 ret = i40e_vsi_alloc_q_vectors(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010942 if (ret) {
10943 dev_info(&pf->pdev->dev,
10944 "failed to allocate %d q_vector for VSI %d, ret=%d\n",
10945 vsi->num_q_vectors, vsi->seid, ret);
10946 vsi->num_q_vectors = 0;
10947 goto vector_setup_out;
10948 }
10949
Anjali Singhai Jain26cdc442015-07-10 19:36:00 -040010950 /* In Legacy mode, we do not have to get any other vector since we
10951 * piggyback on the misc/ICR0 for queue interrupts.
10952 */
10953 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
10954 return ret;
Shannon Nelson958a3e32013-09-28 07:13:28 +000010955 if (vsi->num_q_vectors)
10956 vsi->base_vector = i40e_get_lump(pf, pf->irq_pile,
10957 vsi->num_q_vectors, vsi->idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010958 if (vsi->base_vector < 0) {
10959 dev_info(&pf->pdev->dev,
Shannon Nelson049a2be2014-10-17 03:14:50 +000010960 "failed to get tracking for %d vectors for VSI %d, err=%d\n",
10961 vsi->num_q_vectors, vsi->seid, vsi->base_vector);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010962 i40e_vsi_free_q_vectors(vsi);
10963 ret = -ENOENT;
10964 goto vector_setup_out;
10965 }
10966
10967vector_setup_out:
10968 return ret;
10969}
10970
10971/**
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010972 * i40e_vsi_reinit_setup - return and reallocate resources for a VSI
10973 * @vsi: pointer to the vsi.
10974 *
10975 * This re-allocates a vsi's queue resources.
10976 *
10977 * Returns pointer to the successfully allocated and configured VSI sw struct
10978 * on success, otherwise returns NULL on failure.
10979 **/
10980static struct i40e_vsi *i40e_vsi_reinit_setup(struct i40e_vsi *vsi)
10981{
Björn Töpel74608d12017-05-24 07:55:35 +020010982 u16 alloc_queue_pairs;
John Underwoodf5340392016-02-18 09:19:24 -080010983 struct i40e_pf *pf;
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010984 u8 enabled_tc;
10985 int ret;
10986
John Underwoodf5340392016-02-18 09:19:24 -080010987 if (!vsi)
10988 return NULL;
10989
10990 pf = vsi->back;
10991
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010992 i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
10993 i40e_vsi_clear_rings(vsi);
10994
10995 i40e_vsi_free_arrays(vsi, false);
10996 i40e_set_num_rings_in_vsi(vsi);
10997 ret = i40e_vsi_alloc_arrays(vsi, false);
10998 if (ret)
10999 goto err_vsi;
11000
Björn Töpel74608d12017-05-24 07:55:35 +020011001 alloc_queue_pairs = vsi->alloc_queue_pairs *
11002 (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
11003
11004 ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000011005 if (ret < 0) {
Shannon Nelson049a2be2014-10-17 03:14:50 +000011006 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040011007 "failed to get tracking for %d queues for VSI %d err %d\n",
Björn Töpel74608d12017-05-24 07:55:35 +020011008 alloc_queue_pairs, vsi->seid, ret);
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000011009 goto err_vsi;
11010 }
11011 vsi->base_queue = ret;
11012
11013 /* Update the FW view of the VSI. Force a reset of TC and queue
11014 * layout configurations.
11015 */
11016 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
11017 pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
11018 pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
11019 i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
Jacob Keller1596b5d2016-11-08 13:05:15 -080011020 if (vsi->type == I40E_VSI_MAIN)
11021 i40e_rm_default_mac_filter(vsi, pf->hw.mac.perm_addr);
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000011022
11023 /* assign it some queues */
11024 ret = i40e_alloc_rings(vsi);
11025 if (ret)
11026 goto err_rings;
11027
11028 /* map all of the rings to the q_vectors */
11029 i40e_vsi_map_rings_to_vectors(vsi);
11030 return vsi;
11031
11032err_rings:
11033 i40e_vsi_free_q_vectors(vsi);
11034 if (vsi->netdev_registered) {
11035 vsi->netdev_registered = false;
11036 unregister_netdev(vsi->netdev);
11037 free_netdev(vsi->netdev);
11038 vsi->netdev = NULL;
11039 }
11040 i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
11041err_vsi:
11042 i40e_vsi_clear(vsi);
11043 return NULL;
11044}
11045
11046/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011047 * i40e_vsi_setup - Set up a VSI by a given type
11048 * @pf: board private structure
11049 * @type: VSI type
11050 * @uplink_seid: the switch element to link to
11051 * @param1: usage depends upon VSI type. For VF types, indicates VF id
11052 *
11053 * This allocates the sw VSI structure and its queue resources, then add a VSI
11054 * to the identified VEB.
11055 *
11056 * Returns pointer to the successfully allocated and configure VSI sw struct on
11057 * success, otherwise returns NULL on failure.
11058 **/
11059struct i40e_vsi *i40e_vsi_setup(struct i40e_pf *pf, u8 type,
11060 u16 uplink_seid, u32 param1)
11061{
11062 struct i40e_vsi *vsi = NULL;
11063 struct i40e_veb *veb = NULL;
Björn Töpel74608d12017-05-24 07:55:35 +020011064 u16 alloc_queue_pairs;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011065 int ret, i;
11066 int v_idx;
11067
11068 /* The requested uplink_seid must be either
11069 * - the PF's port seid
11070 * no VEB is needed because this is the PF
11071 * or this is a Flow Director special case VSI
11072 * - seid of an existing VEB
11073 * - seid of a VSI that owns an existing VEB
11074 * - seid of a VSI that doesn't own a VEB
11075 * a new VEB is created and the VSI becomes the owner
11076 * - seid of the PF VSI, which is what creates the first VEB
11077 * this is a special case of the previous
11078 *
11079 * Find which uplink_seid we were given and create a new VEB if needed
11080 */
11081 for (i = 0; i < I40E_MAX_VEB; i++) {
11082 if (pf->veb[i] && pf->veb[i]->seid == uplink_seid) {
11083 veb = pf->veb[i];
11084 break;
11085 }
11086 }
11087
11088 if (!veb && uplink_seid != pf->mac_seid) {
11089
Mitch Williams505682c2014-05-20 08:01:37 +000011090 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011091 if (pf->vsi[i] && pf->vsi[i]->seid == uplink_seid) {
11092 vsi = pf->vsi[i];
11093 break;
11094 }
11095 }
11096 if (!vsi) {
11097 dev_info(&pf->pdev->dev, "no such uplink_seid %d\n",
11098 uplink_seid);
11099 return NULL;
11100 }
11101
11102 if (vsi->uplink_seid == pf->mac_seid)
11103 veb = i40e_veb_setup(pf, 0, pf->mac_seid, vsi->seid,
11104 vsi->tc_config.enabled_tc);
11105 else if ((vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
11106 veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
11107 vsi->tc_config.enabled_tc);
Anjali Singhai Jain79c21a82014-11-13 03:06:14 +000011108 if (veb) {
11109 if (vsi->seid != pf->vsi[pf->lan_vsi]->seid) {
11110 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -040011111 "New VSI creation error, uplink seid of LAN VSI expected.\n");
Anjali Singhai Jain79c21a82014-11-13 03:06:14 +000011112 return NULL;
11113 }
Anjali Singhai Jainfa11cb32015-05-27 12:06:14 -040011114 /* We come up by default in VEPA mode if SRIOV is not
11115 * already enabled, in which case we can't force VEPA
11116 * mode.
11117 */
11118 if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
11119 veb->bridge_mode = BRIDGE_MODE_VEPA;
11120 pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
11121 }
Neerav Parikh51616012015-02-06 08:52:14 +000011122 i40e_config_bridge_mode(veb);
Anjali Singhai Jain79c21a82014-11-13 03:06:14 +000011123 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011124 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
11125 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
11126 veb = pf->veb[i];
11127 }
11128 if (!veb) {
11129 dev_info(&pf->pdev->dev, "couldn't add VEB\n");
11130 return NULL;
11131 }
11132
11133 vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
11134 uplink_seid = veb->seid;
11135 }
11136
11137 /* get vsi sw struct */
11138 v_idx = i40e_vsi_mem_alloc(pf, type);
11139 if (v_idx < 0)
11140 goto err_alloc;
11141 vsi = pf->vsi[v_idx];
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080011142 if (!vsi)
11143 goto err_alloc;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011144 vsi->type = type;
11145 vsi->veb_idx = (veb ? veb->idx : I40E_NO_VEB);
11146
11147 if (type == I40E_VSI_MAIN)
11148 pf->lan_vsi = v_idx;
11149 else if (type == I40E_VSI_SRIOV)
11150 vsi->vf_id = param1;
11151 /* assign it some queues */
Björn Töpel74608d12017-05-24 07:55:35 +020011152 alloc_queue_pairs = vsi->alloc_queue_pairs *
11153 (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
11154
11155 ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011156 if (ret < 0) {
Shannon Nelson049a2be2014-10-17 03:14:50 +000011157 dev_info(&pf->pdev->dev,
11158 "failed to get tracking for %d queues for VSI %d err=%d\n",
Björn Töpel74608d12017-05-24 07:55:35 +020011159 alloc_queue_pairs, vsi->seid, ret);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011160 goto err_vsi;
11161 }
11162 vsi->base_queue = ret;
11163
11164 /* get a VSI from the hardware */
11165 vsi->uplink_seid = uplink_seid;
11166 ret = i40e_add_vsi(vsi);
11167 if (ret)
11168 goto err_vsi;
11169
11170 switch (vsi->type) {
11171 /* setup the netdev if needed */
11172 case I40E_VSI_MAIN:
11173 case I40E_VSI_VMDQ2:
11174 ret = i40e_config_netdev(vsi);
11175 if (ret)
11176 goto err_netdev;
11177 ret = register_netdev(vsi->netdev);
11178 if (ret)
11179 goto err_netdev;
11180 vsi->netdev_registered = true;
11181 netif_carrier_off(vsi->netdev);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -080011182#ifdef CONFIG_I40E_DCB
11183 /* Setup DCB netlink interface */
11184 i40e_dcbnl_setup(vsi);
11185#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011186 /* fall through */
11187
11188 case I40E_VSI_FDIR:
11189 /* set up vectors and rings if needed */
11190 ret = i40e_vsi_setup_vectors(vsi);
11191 if (ret)
11192 goto err_msix;
11193
11194 ret = i40e_alloc_rings(vsi);
11195 if (ret)
11196 goto err_rings;
11197
11198 /* map all of the rings to the q_vectors */
11199 i40e_vsi_map_rings_to_vectors(vsi);
11200
11201 i40e_vsi_reset_stats(vsi);
11202 break;
11203
11204 default:
11205 /* no netdev or rings for the other VSI types */
11206 break;
11207 }
11208
Jacob Kellerd36e41d2017-06-23 04:24:46 -040011209 if ((pf->hw_features & I40E_HW_RSS_AQ_CAPABLE) &&
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -040011210 (vsi->type == I40E_VSI_VMDQ2)) {
11211 ret = i40e_vsi_config_rss(vsi);
11212 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011213 return vsi;
11214
11215err_rings:
11216 i40e_vsi_free_q_vectors(vsi);
11217err_msix:
11218 if (vsi->netdev_registered) {
11219 vsi->netdev_registered = false;
11220 unregister_netdev(vsi->netdev);
11221 free_netdev(vsi->netdev);
11222 vsi->netdev = NULL;
11223 }
11224err_netdev:
11225 i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
11226err_vsi:
11227 i40e_vsi_clear(vsi);
11228err_alloc:
11229 return NULL;
11230}
11231
11232/**
11233 * i40e_veb_get_bw_info - Query VEB BW information
11234 * @veb: the veb to query
11235 *
11236 * Query the Tx scheduler BW configuration data for given VEB
11237 **/
11238static int i40e_veb_get_bw_info(struct i40e_veb *veb)
11239{
11240 struct i40e_aqc_query_switching_comp_ets_config_resp ets_data;
11241 struct i40e_aqc_query_switching_comp_bw_config_resp bw_data;
11242 struct i40e_pf *pf = veb->pf;
11243 struct i40e_hw *hw = &pf->hw;
11244 u32 tc_bw_max;
11245 int ret = 0;
11246 int i;
11247
11248 ret = i40e_aq_query_switch_comp_bw_config(hw, veb->seid,
11249 &bw_data, NULL);
11250 if (ret) {
11251 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040011252 "query veb bw config failed, err %s aq_err %s\n",
11253 i40e_stat_str(&pf->hw, ret),
11254 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011255 goto out;
11256 }
11257
11258 ret = i40e_aq_query_switch_comp_ets_config(hw, veb->seid,
11259 &ets_data, NULL);
11260 if (ret) {
11261 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040011262 "query veb bw ets config failed, err %s aq_err %s\n",
11263 i40e_stat_str(&pf->hw, ret),
11264 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011265 goto out;
11266 }
11267
11268 veb->bw_limit = le16_to_cpu(ets_data.port_bw_limit);
11269 veb->bw_max_quanta = ets_data.tc_bw_max;
11270 veb->is_abs_credits = bw_data.absolute_credits_enable;
Neerav Parikh23cd1f02014-11-12 00:18:41 +000011271 veb->enabled_tc = ets_data.tc_valid_bits;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011272 tc_bw_max = le16_to_cpu(bw_data.tc_bw_max[0]) |
11273 (le16_to_cpu(bw_data.tc_bw_max[1]) << 16);
11274 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
11275 veb->bw_tc_share_credits[i] = bw_data.tc_bw_share_credits[i];
11276 veb->bw_tc_limit_credits[i] =
11277 le16_to_cpu(bw_data.tc_bw_limits[i]);
11278 veb->bw_tc_max_quanta[i] = ((tc_bw_max >> (i*4)) & 0x7);
11279 }
11280
11281out:
11282 return ret;
11283}
11284
11285/**
11286 * i40e_veb_mem_alloc - Allocates the next available struct veb in the PF
11287 * @pf: board private structure
11288 *
11289 * On error: returns error code (negative)
11290 * On success: returns vsi index in PF (positive)
11291 **/
11292static int i40e_veb_mem_alloc(struct i40e_pf *pf)
11293{
11294 int ret = -ENOENT;
11295 struct i40e_veb *veb;
11296 int i;
11297
11298 /* Need to protect the allocation of switch elements at the PF level */
11299 mutex_lock(&pf->switch_mutex);
11300
11301 /* VEB list may be fragmented if VEB creation/destruction has
11302 * been happening. We can afford to do a quick scan to look
11303 * for any free slots in the list.
11304 *
11305 * find next empty veb slot, looping back around if necessary
11306 */
11307 i = 0;
11308 while ((i < I40E_MAX_VEB) && (pf->veb[i] != NULL))
11309 i++;
11310 if (i >= I40E_MAX_VEB) {
11311 ret = -ENOMEM;
11312 goto err_alloc_veb; /* out of VEB slots! */
11313 }
11314
11315 veb = kzalloc(sizeof(*veb), GFP_KERNEL);
11316 if (!veb) {
11317 ret = -ENOMEM;
11318 goto err_alloc_veb;
11319 }
11320 veb->pf = pf;
11321 veb->idx = i;
11322 veb->enabled_tc = 1;
11323
11324 pf->veb[i] = veb;
11325 ret = i;
11326err_alloc_veb:
11327 mutex_unlock(&pf->switch_mutex);
11328 return ret;
11329}
11330
11331/**
11332 * i40e_switch_branch_release - Delete a branch of the switch tree
11333 * @branch: where to start deleting
11334 *
11335 * This uses recursion to find the tips of the branch to be
11336 * removed, deleting until we get back to and can delete this VEB.
11337 **/
11338static void i40e_switch_branch_release(struct i40e_veb *branch)
11339{
11340 struct i40e_pf *pf = branch->pf;
11341 u16 branch_seid = branch->seid;
11342 u16 veb_idx = branch->idx;
11343 int i;
11344
11345 /* release any VEBs on this VEB - RECURSION */
11346 for (i = 0; i < I40E_MAX_VEB; i++) {
11347 if (!pf->veb[i])
11348 continue;
11349 if (pf->veb[i]->uplink_seid == branch->seid)
11350 i40e_switch_branch_release(pf->veb[i]);
11351 }
11352
11353 /* Release the VSIs on this VEB, but not the owner VSI.
11354 *
11355 * NOTE: Removing the last VSI on a VEB has the SIDE EFFECT of removing
11356 * the VEB itself, so don't use (*branch) after this loop.
11357 */
Mitch Williams505682c2014-05-20 08:01:37 +000011358 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011359 if (!pf->vsi[i])
11360 continue;
11361 if (pf->vsi[i]->uplink_seid == branch_seid &&
11362 (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
11363 i40e_vsi_release(pf->vsi[i]);
11364 }
11365 }
11366
11367 /* There's one corner case where the VEB might not have been
11368 * removed, so double check it here and remove it if needed.
11369 * This case happens if the veb was created from the debugfs
11370 * commands and no VSIs were added to it.
11371 */
11372 if (pf->veb[veb_idx])
11373 i40e_veb_release(pf->veb[veb_idx]);
11374}
11375
11376/**
11377 * i40e_veb_clear - remove veb struct
11378 * @veb: the veb to remove
11379 **/
11380static void i40e_veb_clear(struct i40e_veb *veb)
11381{
11382 if (!veb)
11383 return;
11384
11385 if (veb->pf) {
11386 struct i40e_pf *pf = veb->pf;
11387
11388 mutex_lock(&pf->switch_mutex);
11389 if (pf->veb[veb->idx] == veb)
11390 pf->veb[veb->idx] = NULL;
11391 mutex_unlock(&pf->switch_mutex);
11392 }
11393
11394 kfree(veb);
11395}
11396
11397/**
11398 * i40e_veb_release - Delete a VEB and free its resources
11399 * @veb: the VEB being removed
11400 **/
11401void i40e_veb_release(struct i40e_veb *veb)
11402{
11403 struct i40e_vsi *vsi = NULL;
11404 struct i40e_pf *pf;
11405 int i, n = 0;
11406
11407 pf = veb->pf;
11408
11409 /* find the remaining VSI and check for extras */
Mitch Williams505682c2014-05-20 08:01:37 +000011410 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011411 if (pf->vsi[i] && pf->vsi[i]->uplink_seid == veb->seid) {
11412 n++;
11413 vsi = pf->vsi[i];
11414 }
11415 }
11416 if (n != 1) {
11417 dev_info(&pf->pdev->dev,
11418 "can't remove VEB %d with %d VSIs left\n",
11419 veb->seid, n);
11420 return;
11421 }
11422
11423 /* move the remaining VSI to uplink veb */
11424 vsi->flags &= ~I40E_VSI_FLAG_VEB_OWNER;
11425 if (veb->uplink_seid) {
11426 vsi->uplink_seid = veb->uplink_seid;
11427 if (veb->uplink_seid == pf->mac_seid)
11428 vsi->veb_idx = I40E_NO_VEB;
11429 else
11430 vsi->veb_idx = veb->veb_idx;
11431 } else {
11432 /* floating VEB */
11433 vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
11434 vsi->veb_idx = pf->vsi[pf->lan_vsi]->veb_idx;
11435 }
11436
11437 i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
11438 i40e_veb_clear(veb);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011439}
11440
11441/**
11442 * i40e_add_veb - create the VEB in the switch
11443 * @veb: the VEB to be instantiated
11444 * @vsi: the controlling VSI
11445 **/
11446static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi)
11447{
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040011448 struct i40e_pf *pf = veb->pf;
Shannon Nelson66fc3602016-01-13 16:51:42 -080011449 bool enable_stats = !!(pf->flags & I40E_FLAG_VEB_STATS_ENABLED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011450 int ret;
11451
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040011452 ret = i40e_aq_add_veb(&pf->hw, veb->uplink_seid, vsi->seid,
Mitch Williams5bc16032016-05-16 10:26:43 -070011453 veb->enabled_tc, false,
Shannon Nelson66fc3602016-01-13 16:51:42 -080011454 &veb->seid, enable_stats, NULL);
Mitch Williams5bc16032016-05-16 10:26:43 -070011455
11456 /* get a VEB from the hardware */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011457 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040011458 dev_info(&pf->pdev->dev,
11459 "couldn't add VEB, err %s aq_err %s\n",
11460 i40e_stat_str(&pf->hw, ret),
11461 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011462 return -EPERM;
11463 }
11464
11465 /* get statistics counter */
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040011466 ret = i40e_aq_get_veb_parameters(&pf->hw, veb->seid, NULL, NULL,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011467 &veb->stats_idx, NULL, NULL, NULL);
11468 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040011469 dev_info(&pf->pdev->dev,
11470 "couldn't get VEB statistics idx, err %s aq_err %s\n",
11471 i40e_stat_str(&pf->hw, ret),
11472 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011473 return -EPERM;
11474 }
11475 ret = i40e_veb_get_bw_info(veb);
11476 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040011477 dev_info(&pf->pdev->dev,
11478 "couldn't get VEB bw info, err %s aq_err %s\n",
11479 i40e_stat_str(&pf->hw, ret),
11480 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
11481 i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011482 return -ENOENT;
11483 }
11484
11485 vsi->uplink_seid = veb->seid;
11486 vsi->veb_idx = veb->idx;
11487 vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
11488
11489 return 0;
11490}
11491
11492/**
11493 * i40e_veb_setup - Set up a VEB
11494 * @pf: board private structure
11495 * @flags: VEB setup flags
11496 * @uplink_seid: the switch element to link to
11497 * @vsi_seid: the initial VSI seid
11498 * @enabled_tc: Enabled TC bit-map
11499 *
11500 * This allocates the sw VEB structure and links it into the switch
11501 * It is possible and legal for this to be a duplicate of an already
11502 * existing VEB. It is also possible for both uplink and vsi seids
11503 * to be zero, in order to create a floating VEB.
11504 *
11505 * Returns pointer to the successfully allocated VEB sw struct on
11506 * success, otherwise returns NULL on failure.
11507 **/
11508struct i40e_veb *i40e_veb_setup(struct i40e_pf *pf, u16 flags,
11509 u16 uplink_seid, u16 vsi_seid,
11510 u8 enabled_tc)
11511{
11512 struct i40e_veb *veb, *uplink_veb = NULL;
11513 int vsi_idx, veb_idx;
11514 int ret;
11515
11516 /* if one seid is 0, the other must be 0 to create a floating relay */
11517 if ((uplink_seid == 0 || vsi_seid == 0) &&
11518 (uplink_seid + vsi_seid != 0)) {
11519 dev_info(&pf->pdev->dev,
11520 "one, not both seid's are 0: uplink=%d vsi=%d\n",
11521 uplink_seid, vsi_seid);
11522 return NULL;
11523 }
11524
11525 /* make sure there is such a vsi and uplink */
Mitch Williams505682c2014-05-20 08:01:37 +000011526 for (vsi_idx = 0; vsi_idx < pf->num_alloc_vsi; vsi_idx++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011527 if (pf->vsi[vsi_idx] && pf->vsi[vsi_idx]->seid == vsi_seid)
11528 break;
Mitch Williams505682c2014-05-20 08:01:37 +000011529 if (vsi_idx >= pf->num_alloc_vsi && vsi_seid != 0) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011530 dev_info(&pf->pdev->dev, "vsi seid %d not found\n",
11531 vsi_seid);
11532 return NULL;
11533 }
11534
11535 if (uplink_seid && uplink_seid != pf->mac_seid) {
11536 for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
11537 if (pf->veb[veb_idx] &&
11538 pf->veb[veb_idx]->seid == uplink_seid) {
11539 uplink_veb = pf->veb[veb_idx];
11540 break;
11541 }
11542 }
11543 if (!uplink_veb) {
11544 dev_info(&pf->pdev->dev,
11545 "uplink seid %d not found\n", uplink_seid);
11546 return NULL;
11547 }
11548 }
11549
11550 /* get veb sw struct */
11551 veb_idx = i40e_veb_mem_alloc(pf);
11552 if (veb_idx < 0)
11553 goto err_alloc;
11554 veb = pf->veb[veb_idx];
11555 veb->flags = flags;
11556 veb->uplink_seid = uplink_seid;
11557 veb->veb_idx = (uplink_veb ? uplink_veb->idx : I40E_NO_VEB);
11558 veb->enabled_tc = (enabled_tc ? enabled_tc : 0x1);
11559
11560 /* create the VEB in the switch */
11561 ret = i40e_add_veb(veb, pf->vsi[vsi_idx]);
11562 if (ret)
11563 goto err_veb;
Shannon Nelson1bb8b932014-04-23 04:49:54 +000011564 if (vsi_idx == pf->lan_vsi)
11565 pf->lan_veb = veb->idx;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011566
11567 return veb;
11568
11569err_veb:
11570 i40e_veb_clear(veb);
11571err_alloc:
11572 return NULL;
11573}
11574
11575/**
Jeff Kirsherb40c82e62015-02-27 09:18:34 +000011576 * i40e_setup_pf_switch_element - set PF vars based on switch type
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011577 * @pf: board private structure
11578 * @ele: element we are building info from
11579 * @num_reported: total number of elements
11580 * @printconfig: should we print the contents
11581 *
11582 * helper function to assist in extracting a few useful SEID values.
11583 **/
11584static void i40e_setup_pf_switch_element(struct i40e_pf *pf,
11585 struct i40e_aqc_switch_config_element_resp *ele,
11586 u16 num_reported, bool printconfig)
11587{
11588 u16 downlink_seid = le16_to_cpu(ele->downlink_seid);
11589 u16 uplink_seid = le16_to_cpu(ele->uplink_seid);
11590 u8 element_type = ele->element_type;
11591 u16 seid = le16_to_cpu(ele->seid);
11592
11593 if (printconfig)
11594 dev_info(&pf->pdev->dev,
11595 "type=%d seid=%d uplink=%d downlink=%d\n",
11596 element_type, seid, uplink_seid, downlink_seid);
11597
11598 switch (element_type) {
11599 case I40E_SWITCH_ELEMENT_TYPE_MAC:
11600 pf->mac_seid = seid;
11601 break;
11602 case I40E_SWITCH_ELEMENT_TYPE_VEB:
11603 /* Main VEB? */
11604 if (uplink_seid != pf->mac_seid)
11605 break;
11606 if (pf->lan_veb == I40E_NO_VEB) {
11607 int v;
11608
11609 /* find existing or else empty VEB */
11610 for (v = 0; v < I40E_MAX_VEB; v++) {
11611 if (pf->veb[v] && (pf->veb[v]->seid == seid)) {
11612 pf->lan_veb = v;
11613 break;
11614 }
11615 }
11616 if (pf->lan_veb == I40E_NO_VEB) {
11617 v = i40e_veb_mem_alloc(pf);
11618 if (v < 0)
11619 break;
11620 pf->lan_veb = v;
11621 }
11622 }
11623
11624 pf->veb[pf->lan_veb]->seid = seid;
11625 pf->veb[pf->lan_veb]->uplink_seid = pf->mac_seid;
11626 pf->veb[pf->lan_veb]->pf = pf;
11627 pf->veb[pf->lan_veb]->veb_idx = I40E_NO_VEB;
11628 break;
11629 case I40E_SWITCH_ELEMENT_TYPE_VSI:
11630 if (num_reported != 1)
11631 break;
11632 /* This is immediately after a reset so we can assume this is
11633 * the PF's VSI
11634 */
11635 pf->mac_seid = uplink_seid;
11636 pf->pf_seid = downlink_seid;
11637 pf->main_vsi_seid = seid;
11638 if (printconfig)
11639 dev_info(&pf->pdev->dev,
11640 "pf_seid=%d main_vsi_seid=%d\n",
11641 pf->pf_seid, pf->main_vsi_seid);
11642 break;
11643 case I40E_SWITCH_ELEMENT_TYPE_PF:
11644 case I40E_SWITCH_ELEMENT_TYPE_VF:
11645 case I40E_SWITCH_ELEMENT_TYPE_EMP:
11646 case I40E_SWITCH_ELEMENT_TYPE_BMC:
11647 case I40E_SWITCH_ELEMENT_TYPE_PE:
11648 case I40E_SWITCH_ELEMENT_TYPE_PA:
11649 /* ignore these for now */
11650 break;
11651 default:
11652 dev_info(&pf->pdev->dev, "unknown element type=%d seid=%d\n",
11653 element_type, seid);
11654 break;
11655 }
11656}
11657
11658/**
11659 * i40e_fetch_switch_configuration - Get switch config from firmware
11660 * @pf: board private structure
11661 * @printconfig: should we print the contents
11662 *
11663 * Get the current switch configuration from the device and
11664 * extract a few useful SEID values.
11665 **/
11666int i40e_fetch_switch_configuration(struct i40e_pf *pf, bool printconfig)
11667{
11668 struct i40e_aqc_get_switch_config_resp *sw_config;
11669 u16 next_seid = 0;
11670 int ret = 0;
11671 u8 *aq_buf;
11672 int i;
11673
11674 aq_buf = kzalloc(I40E_AQ_LARGE_BUF, GFP_KERNEL);
11675 if (!aq_buf)
11676 return -ENOMEM;
11677
11678 sw_config = (struct i40e_aqc_get_switch_config_resp *)aq_buf;
11679 do {
11680 u16 num_reported, num_total;
11681
11682 ret = i40e_aq_get_switch_config(&pf->hw, sw_config,
11683 I40E_AQ_LARGE_BUF,
11684 &next_seid, NULL);
11685 if (ret) {
11686 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040011687 "get switch config failed err %s aq_err %s\n",
11688 i40e_stat_str(&pf->hw, ret),
11689 i40e_aq_str(&pf->hw,
11690 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011691 kfree(aq_buf);
11692 return -ENOENT;
11693 }
11694
11695 num_reported = le16_to_cpu(sw_config->header.num_reported);
11696 num_total = le16_to_cpu(sw_config->header.num_total);
11697
11698 if (printconfig)
11699 dev_info(&pf->pdev->dev,
11700 "header: %d reported %d total\n",
11701 num_reported, num_total);
11702
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011703 for (i = 0; i < num_reported; i++) {
11704 struct i40e_aqc_switch_config_element_resp *ele =
11705 &sw_config->element[i];
11706
11707 i40e_setup_pf_switch_element(pf, ele, num_reported,
11708 printconfig);
11709 }
11710 } while (next_seid != 0);
11711
11712 kfree(aq_buf);
11713 return ret;
11714}
11715
11716/**
11717 * i40e_setup_pf_switch - Setup the HW switch on startup or after reset
11718 * @pf: board private structure
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000011719 * @reinit: if the Main VSI needs to re-initialized.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011720 *
11721 * Returns 0 on success, negative value on failure
11722 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000011723static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011724{
Anjali Singhai Jainb5569892016-05-03 15:13:12 -070011725 u16 flags = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011726 int ret;
11727
11728 /* find out what's out there already */
11729 ret = i40e_fetch_switch_configuration(pf, false);
11730 if (ret) {
11731 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040011732 "couldn't fetch switch config, err %s aq_err %s\n",
11733 i40e_stat_str(&pf->hw, ret),
11734 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011735 return ret;
11736 }
11737 i40e_pf_reset_stats(pf);
11738
Anjali Singhai Jainb5569892016-05-03 15:13:12 -070011739 /* set the switch config bit for the whole device to
11740 * support limited promisc or true promisc
11741 * when user requests promisc. The default is limited
11742 * promisc.
11743 */
11744
11745 if ((pf->hw.pf_id == 0) &&
11746 !(pf->flags & I40E_FLAG_TRUE_PROMISC_SUPPORT))
11747 flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
11748
11749 if (pf->hw.pf_id == 0) {
11750 u16 valid_flags;
11751
11752 valid_flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
11753 ret = i40e_aq_set_switch_config(&pf->hw, flags, valid_flags,
11754 NULL);
11755 if (ret && pf->hw.aq.asq_last_status != I40E_AQ_RC_ESRCH) {
11756 dev_info(&pf->pdev->dev,
11757 "couldn't set switch config bits, err %s aq_err %s\n",
11758 i40e_stat_str(&pf->hw, ret),
11759 i40e_aq_str(&pf->hw,
11760 pf->hw.aq.asq_last_status));
11761 /* not a fatal problem, just keep going */
11762 }
11763 }
11764
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011765 /* first time setup */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000011766 if (pf->lan_vsi == I40E_NO_VSI || reinit) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011767 struct i40e_vsi *vsi = NULL;
11768 u16 uplink_seid;
11769
11770 /* Set up the PF VSI associated with the PF's main VSI
11771 * that is already in the HW switch
11772 */
11773 if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
11774 uplink_seid = pf->veb[pf->lan_veb]->seid;
11775 else
11776 uplink_seid = pf->mac_seid;
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000011777 if (pf->lan_vsi == I40E_NO_VSI)
11778 vsi = i40e_vsi_setup(pf, I40E_VSI_MAIN, uplink_seid, 0);
11779 else if (reinit)
11780 vsi = i40e_vsi_reinit_setup(pf->vsi[pf->lan_vsi]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011781 if (!vsi) {
11782 dev_info(&pf->pdev->dev, "setup of MAIN VSI failed\n");
11783 i40e_fdir_teardown(pf);
11784 return -EAGAIN;
11785 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011786 } else {
11787 /* force a reset of TC and queue layout configurations */
11788 u8 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
Jesse Brandeburg6995b362015-08-28 17:55:54 -040011789
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011790 pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
11791 pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
11792 i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
11793 }
11794 i40e_vlan_stripping_disable(pf->vsi[pf->lan_vsi]);
11795
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080011796 i40e_fdir_sb_setup(pf);
11797
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011798 /* Setup static PF queue filter control settings */
11799 ret = i40e_setup_pf_filter_control(pf);
11800 if (ret) {
11801 dev_info(&pf->pdev->dev, "setup_pf_filter_control failed: %d\n",
11802 ret);
11803 /* Failure here should not stop continuing other steps */
11804 }
11805
11806 /* enable RSS in the HW, even for only one queue, as the stack can use
11807 * the hash
11808 */
11809 if ((pf->flags & I40E_FLAG_RSS_ENABLED))
Helin Zhang043dd652015-10-21 19:56:23 -040011810 i40e_pf_config_rss(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011811
11812 /* fill in link information and enable LSE reporting */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011813 i40e_link_event(pf);
11814
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +000011815 /* Initialize user-specific link properties */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011816 pf->fc_autoneg_status = ((pf->hw.phy.link_info.an_info &
11817 I40E_AQ_AN_COMPLETED) ? true : false);
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +000011818
Jacob Kellerbeb0dff2014-01-11 05:43:19 +000011819 i40e_ptp_init(pf);
11820
Alexander Duyck1f190d92017-04-19 09:25:51 -040011821 /* repopulate tunnel port filters */
11822 i40e_sync_udp_filters(pf);
11823
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011824 return ret;
11825}
11826
11827/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011828 * i40e_determine_queue_usage - Work out queue distribution
11829 * @pf: board private structure
11830 **/
11831static void i40e_determine_queue_usage(struct i40e_pf *pf)
11832{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011833 int queues_left;
Shannon Nelsone50d5752017-07-24 18:17:42 -070011834 int q_max;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011835
11836 pf->num_lan_qps = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011837
11838 /* Find the max queues to be put into basic use. We'll always be
11839 * using TC0, whether or not DCB is running, and TC0 will get the
11840 * big RSS set.
11841 */
11842 queues_left = pf->hw.func_caps.num_tx_qp;
11843
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080011844 if ((queues_left == 1) ||
Frank Zhang9aa7e932014-05-20 08:01:42 +000011845 !(pf->flags & I40E_FLAG_MSIX_ENABLED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011846 /* one qp for PF, no queues for anything else */
11847 queues_left = 0;
Helin Zhangacd65442015-10-26 19:44:28 -040011848 pf->alloc_rss_size = pf->num_lan_qps = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011849
11850 /* make sure all the fancies are disabled */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080011851 pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060011852 I40E_FLAG_IWARP_ENABLED |
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080011853 I40E_FLAG_FD_SB_ENABLED |
11854 I40E_FLAG_FD_ATR_ENABLED |
Neerav Parikh4d9b6042014-05-22 06:31:51 +000011855 I40E_FLAG_DCB_CAPABLE |
Dave Ertmana0362442016-08-29 17:38:26 -070011856 I40E_FLAG_DCB_ENABLED |
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080011857 I40E_FLAG_SRIOV_ENABLED |
11858 I40E_FLAG_VMDQ_ENABLED);
Frank Zhang9aa7e932014-05-20 08:01:42 +000011859 } else if (!(pf->flags & (I40E_FLAG_RSS_ENABLED |
11860 I40E_FLAG_FD_SB_ENABLED |
Anjali Singhai Jainbbe7d0e2014-05-20 08:01:44 +000011861 I40E_FLAG_FD_ATR_ENABLED |
Neerav Parikh4d9b6042014-05-22 06:31:51 +000011862 I40E_FLAG_DCB_CAPABLE))) {
Frank Zhang9aa7e932014-05-20 08:01:42 +000011863 /* one qp for PF */
Helin Zhangacd65442015-10-26 19:44:28 -040011864 pf->alloc_rss_size = pf->num_lan_qps = 1;
Frank Zhang9aa7e932014-05-20 08:01:42 +000011865 queues_left -= pf->num_lan_qps;
11866
11867 pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060011868 I40E_FLAG_IWARP_ENABLED |
Frank Zhang9aa7e932014-05-20 08:01:42 +000011869 I40E_FLAG_FD_SB_ENABLED |
11870 I40E_FLAG_FD_ATR_ENABLED |
11871 I40E_FLAG_DCB_ENABLED |
11872 I40E_FLAG_VMDQ_ENABLED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011873 } else {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080011874 /* Not enough queues for all TCs */
Neerav Parikh4d9b6042014-05-22 06:31:51 +000011875 if ((pf->flags & I40E_FLAG_DCB_CAPABLE) &&
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080011876 (queues_left < I40E_MAX_TRAFFIC_CLASS)) {
Dave Ertmana0362442016-08-29 17:38:26 -070011877 pf->flags &= ~(I40E_FLAG_DCB_CAPABLE |
11878 I40E_FLAG_DCB_ENABLED);
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080011879 dev_info(&pf->pdev->dev, "not enough queues for DCB. DCB is disabled.\n");
11880 }
Shannon Nelsone50d5752017-07-24 18:17:42 -070011881
11882 /* limit lan qps to the smaller of qps, cpus or msix */
11883 q_max = max_t(int, pf->rss_size_max, num_online_cpus());
11884 q_max = min_t(int, q_max, pf->hw.func_caps.num_tx_qp);
11885 q_max = min_t(int, q_max, pf->hw.func_caps.num_msix_vectors);
11886 pf->num_lan_qps = q_max;
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +000011887
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080011888 queues_left -= pf->num_lan_qps;
11889 }
11890
11891 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
11892 if (queues_left > 1) {
11893 queues_left -= 1; /* save 1 queue for FD */
11894 } else {
11895 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
11896 dev_info(&pf->pdev->dev, "not enough queues for Flow Director. Flow Director feature is disabled\n");
11897 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011898 }
11899
11900 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
11901 pf->num_vf_qps && pf->num_req_vfs && queues_left) {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080011902 pf->num_req_vfs = min_t(int, pf->num_req_vfs,
11903 (queues_left / pf->num_vf_qps));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011904 queues_left -= (pf->num_req_vfs * pf->num_vf_qps);
11905 }
11906
11907 if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
11908 pf->num_vmdq_vsis && pf->num_vmdq_qps && queues_left) {
11909 pf->num_vmdq_vsis = min_t(int, pf->num_vmdq_vsis,
11910 (queues_left / pf->num_vmdq_qps));
11911 queues_left -= (pf->num_vmdq_vsis * pf->num_vmdq_qps);
11912 }
11913
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +000011914 pf->queues_left = queues_left;
Neerav Parikh8279e492015-09-03 17:18:50 -040011915 dev_dbg(&pf->pdev->dev,
11916 "qs_avail=%d FD SB=%d lan_qs=%d lan_tc0=%d vf=%d*%d vmdq=%d*%d, remaining=%d\n",
11917 pf->hw.func_caps.num_tx_qp,
11918 !!(pf->flags & I40E_FLAG_FD_SB_ENABLED),
Helin Zhangacd65442015-10-26 19:44:28 -040011919 pf->num_lan_qps, pf->alloc_rss_size, pf->num_req_vfs,
11920 pf->num_vf_qps, pf->num_vmdq_vsis, pf->num_vmdq_qps,
11921 queues_left);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011922}
11923
11924/**
11925 * i40e_setup_pf_filter_control - Setup PF static filter control
11926 * @pf: PF to be setup
11927 *
Jeff Kirsherb40c82e62015-02-27 09:18:34 +000011928 * i40e_setup_pf_filter_control sets up a PF's initial filter control
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011929 * settings. If PE/FCoE are enabled then it will also set the per PF
11930 * based filter sizes required for them. It also enables Flow director,
11931 * ethertype and macvlan type filter settings for the pf.
11932 *
11933 * Returns 0 on success, negative on failure
11934 **/
11935static int i40e_setup_pf_filter_control(struct i40e_pf *pf)
11936{
11937 struct i40e_filter_control_settings *settings = &pf->filter_settings;
11938
11939 settings->hash_lut_size = I40E_HASH_LUT_SIZE_128;
11940
11941 /* Flow Director is enabled */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080011942 if (pf->flags & (I40E_FLAG_FD_SB_ENABLED | I40E_FLAG_FD_ATR_ENABLED))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011943 settings->enable_fdir = true;
11944
11945 /* Ethtype and MACVLAN filters enabled for PF */
11946 settings->enable_ethtype = true;
11947 settings->enable_macvlan = true;
11948
11949 if (i40e_set_filter_control(&pf->hw, settings))
11950 return -ENOENT;
11951
11952 return 0;
11953}
11954
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000011955#define INFO_STRING_LEN 255
Shannon Nelson7fd89542015-10-21 19:47:04 -040011956#define REMAIN(__x) (INFO_STRING_LEN - (__x))
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000011957static void i40e_print_features(struct i40e_pf *pf)
11958{
11959 struct i40e_hw *hw = &pf->hw;
Joe Perches3b195842015-12-03 04:20:57 -080011960 char *buf;
11961 int i;
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000011962
Joe Perches3b195842015-12-03 04:20:57 -080011963 buf = kmalloc(INFO_STRING_LEN, GFP_KERNEL);
11964 if (!buf)
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000011965 return;
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000011966
Joe Perches3b195842015-12-03 04:20:57 -080011967 i = snprintf(buf, INFO_STRING_LEN, "Features: PF-id[%d]", hw->pf_id);
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000011968#ifdef CONFIG_PCI_IOV
Joe Perches3b195842015-12-03 04:20:57 -080011969 i += snprintf(&buf[i], REMAIN(i), " VFs: %d", pf->num_req_vfs);
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000011970#endif
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -070011971 i += snprintf(&buf[i], REMAIN(i), " VSIs: %d QP: %d",
Shannon Nelson7fd89542015-10-21 19:47:04 -040011972 pf->hw.func_caps.num_vsis,
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -070011973 pf->vsi[pf->lan_vsi]->num_queue_pairs);
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000011974 if (pf->flags & I40E_FLAG_RSS_ENABLED)
Joe Perches3b195842015-12-03 04:20:57 -080011975 i += snprintf(&buf[i], REMAIN(i), " RSS");
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000011976 if (pf->flags & I40E_FLAG_FD_ATR_ENABLED)
Joe Perches3b195842015-12-03 04:20:57 -080011977 i += snprintf(&buf[i], REMAIN(i), " FD_ATR");
Akeem G Abodunrinc6423ff2014-05-10 04:49:08 +000011978 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
Joe Perches3b195842015-12-03 04:20:57 -080011979 i += snprintf(&buf[i], REMAIN(i), " FD_SB");
11980 i += snprintf(&buf[i], REMAIN(i), " NTUPLE");
Akeem G Abodunrinc6423ff2014-05-10 04:49:08 +000011981 }
Neerav Parikh4d9b6042014-05-22 06:31:51 +000011982 if (pf->flags & I40E_FLAG_DCB_CAPABLE)
Joe Perches3b195842015-12-03 04:20:57 -080011983 i += snprintf(&buf[i], REMAIN(i), " DCB");
Joe Perches3b195842015-12-03 04:20:57 -080011984 i += snprintf(&buf[i], REMAIN(i), " VxLAN");
Singhai, Anjali6a899022015-12-14 12:21:18 -080011985 i += snprintf(&buf[i], REMAIN(i), " Geneve");
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000011986 if (pf->flags & I40E_FLAG_PTP)
Joe Perches3b195842015-12-03 04:20:57 -080011987 i += snprintf(&buf[i], REMAIN(i), " PTP");
Shannon Nelson6dec1012015-09-28 14:12:30 -040011988 if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
Joe Perches3b195842015-12-03 04:20:57 -080011989 i += snprintf(&buf[i], REMAIN(i), " VEB");
Shannon Nelson6dec1012015-09-28 14:12:30 -040011990 else
Joe Perches3b195842015-12-03 04:20:57 -080011991 i += snprintf(&buf[i], REMAIN(i), " VEPA");
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000011992
Joe Perches3b195842015-12-03 04:20:57 -080011993 dev_info(&pf->pdev->dev, "%s\n", buf);
11994 kfree(buf);
Shannon Nelson7fd89542015-10-21 19:47:04 -040011995 WARN_ON(i > INFO_STRING_LEN);
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000011996}
11997
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011998/**
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050011999 * i40e_get_platform_mac_addr - get platform-specific MAC address
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050012000 * @pdev: PCI device information struct
12001 * @pf: board private structure
12002 *
Jacob Keller41c4c2b2017-04-05 07:50:57 -040012003 * Look up the MAC address for the device. First we'll try
12004 * eth_platform_get_mac_address, which will check Open Firmware, or arch
12005 * specific fallback. Otherwise, we'll default to the stored value in
12006 * firmware.
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050012007 **/
12008static void i40e_get_platform_mac_addr(struct pci_dev *pdev, struct i40e_pf *pf)
12009{
Jacob Keller41c4c2b2017-04-05 07:50:57 -040012010 if (eth_platform_get_mac_address(&pdev->dev, pf->hw.mac.addr))
12011 i40e_get_mac_addr(&pf->hw, pf->hw.mac.addr);
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050012012}
12013
12014/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012015 * i40e_probe - Device initialization routine
12016 * @pdev: PCI device information struct
12017 * @ent: entry in i40e_pci_tbl
12018 *
Jeff Kirsherb40c82e62015-02-27 09:18:34 +000012019 * i40e_probe initializes a PF identified by a pci_dev structure.
12020 * The OS initialization, configuring of the PF private structure,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012021 * and a hardware reset occur.
12022 *
12023 * Returns 0 on success, negative on failure
12024 **/
12025static int i40e_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
12026{
Catherine Sullivane8278452015-02-06 08:52:08 +000012027 struct i40e_aq_get_phy_abilities_resp abilities;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012028 struct i40e_pf *pf;
12029 struct i40e_hw *hw;
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +000012030 static u16 pfs_found;
Shannon Nelson1d5109d2015-08-26 15:14:08 -040012031 u16 wol_nvm_bits;
Catherine Sullivand4dfb812013-11-28 06:39:21 +000012032 u16 link_status;
Jean Sacren6f66a482015-09-19 05:08:45 -060012033 int err;
Anjali Singhai Jain4f2f017c2015-10-21 19:47:07 -040012034 u32 val;
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +000012035 u32 i;
Helin Zhang58fc3262015-10-01 14:37:38 -040012036 u8 set_fc_aq_fail;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012037
12038 err = pci_enable_device_mem(pdev);
12039 if (err)
12040 return err;
12041
12042 /* set up for high or low dma */
Mitch Williams64942942014-02-11 08:26:33 +000012043 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
Mitch Williams64942942014-02-11 08:26:33 +000012044 if (err) {
Jean Sacrene3e3bfd2014-03-25 04:30:27 +000012045 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
12046 if (err) {
12047 dev_err(&pdev->dev,
12048 "DMA configuration failed: 0x%x\n", err);
12049 goto err_dma;
12050 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012051 }
12052
12053 /* set up pci connections */
Johannes Thumshirn56d766d2016-06-07 09:44:05 +020012054 err = pci_request_mem_regions(pdev, i40e_driver_name);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012055 if (err) {
12056 dev_info(&pdev->dev,
12057 "pci_request_selected_regions failed %d\n", err);
12058 goto err_pci_reg;
12059 }
12060
12061 pci_enable_pcie_error_reporting(pdev);
12062 pci_set_master(pdev);
12063
12064 /* Now that we have a PCI connection, we need to do the
12065 * low level device setup. This is primarily setting up
12066 * the Admin Queue structures and then querying for the
12067 * device's current profile information.
12068 */
12069 pf = kzalloc(sizeof(*pf), GFP_KERNEL);
12070 if (!pf) {
12071 err = -ENOMEM;
12072 goto err_pf_alloc;
12073 }
12074 pf->next_vsi = 0;
12075 pf->pdev = pdev;
Mauro S. M. Rodrigues9e6c9c02017-05-12 23:26:56 -030012076 set_bit(__I40E_DOWN, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012077
12078 hw = &pf->hw;
12079 hw->back = pf;
Anjali Singhai232f4702015-02-26 16:15:39 +000012080
Shannon Nelson2ac8b672015-07-23 16:54:37 -040012081 pf->ioremap_len = min_t(int, pci_resource_len(pdev, 0),
12082 I40E_MAX_CSR_SPACE);
Anjali Singhai232f4702015-02-26 16:15:39 +000012083
Shannon Nelson2ac8b672015-07-23 16:54:37 -040012084 hw->hw_addr = ioremap(pci_resource_start(pdev, 0), pf->ioremap_len);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012085 if (!hw->hw_addr) {
12086 err = -EIO;
12087 dev_info(&pdev->dev, "ioremap(0x%04x, 0x%04x) failed: 0x%x\n",
12088 (unsigned int)pci_resource_start(pdev, 0),
Shannon Nelson2ac8b672015-07-23 16:54:37 -040012089 pf->ioremap_len, err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012090 goto err_ioremap;
12091 }
12092 hw->vendor_id = pdev->vendor;
12093 hw->device_id = pdev->device;
12094 pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
12095 hw->subsystem_vendor_id = pdev->subsystem_vendor;
12096 hw->subsystem_device_id = pdev->subsystem_device;
12097 hw->bus.device = PCI_SLOT(pdev->devfn);
12098 hw->bus.func = PCI_FUNC(pdev->devfn);
Sudheer Mogilappagarib3f028f2017-02-09 23:58:22 -080012099 hw->bus.bus_id = pdev->bus->number;
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +000012100 pf->instance = pfs_found;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012101
Scott Petersonab243ec2017-08-22 06:57:54 -040012102 /* Select something other than the 802.1ad ethertype for the
12103 * switch to use internally and drop on ingress.
12104 */
12105 hw->switch_tag = 0xffff;
12106 hw->first_tag = ETH_P_8021AD;
12107 hw->second_tag = ETH_P_8021Q;
12108
Jacob Keller0e588de2017-02-06 14:38:50 -080012109 INIT_LIST_HEAD(&pf->l3_flex_pit_list);
12110 INIT_LIST_HEAD(&pf->l4_flex_pit_list);
12111
Shannon Nelsonde03d2b2016-03-10 14:59:44 -080012112 /* set up the locks for the AQ, do this only once in probe
12113 * and destroy them only once in remove
12114 */
12115 mutex_init(&hw->aq.asq_mutex);
12116 mutex_init(&hw->aq.arq_mutex);
12117
Alexander Duyck5d4ca232016-09-30 08:21:46 -040012118 pf->msg_enable = netif_msg_init(debug,
12119 NETIF_MSG_DRV |
12120 NETIF_MSG_PROBE |
12121 NETIF_MSG_LINK);
12122 if (debug < -1)
12123 pf->hw.debug_mask = debug;
Shannon Nelson5b5faa42014-10-17 03:14:51 +000012124
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +000012125 /* do a special CORER for clearing PXE mode once at init */
12126 if (hw->revision_id == 0 &&
12127 (rd32(hw, I40E_GLLAN_RCTL_0) & I40E_GLLAN_RCTL_0_PXE_MODE_MASK)) {
12128 wr32(hw, I40E_GLGEN_RTRIG, I40E_GLGEN_RTRIG_CORER_MASK);
12129 i40e_flush(hw);
12130 msleep(200);
12131 pf->corer_count++;
12132
12133 i40e_clear_pxe_mode(hw);
12134 }
12135
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012136 /* Reset here to make sure all is clean and to define PF 'n' */
Shannon Nelson838d41d2014-06-04 20:41:27 +000012137 i40e_clear_hw(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012138 err = i40e_pf_reset(hw);
12139 if (err) {
12140 dev_info(&pdev->dev, "Initial pf_reset failed: %d\n", err);
12141 goto err_pf_reset;
12142 }
12143 pf->pfr_count++;
12144
12145 hw->aq.num_arq_entries = I40E_AQ_LEN;
12146 hw->aq.num_asq_entries = I40E_AQ_LEN;
12147 hw->aq.arq_buf_size = I40E_MAX_AQ_BUF_SIZE;
12148 hw->aq.asq_buf_size = I40E_MAX_AQ_BUF_SIZE;
12149 pf->adminq_work_limit = I40E_AQ_WORK_LIMIT;
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +000012150
Carolyn Wybornyb294ac72014-12-11 07:06:39 +000012151 snprintf(pf->int_name, sizeof(pf->int_name) - 1,
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +000012152 "%s-%s:misc",
12153 dev_driver_string(&pf->pdev->dev), dev_name(&pdev->dev));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012154
12155 err = i40e_init_shared_code(hw);
12156 if (err) {
Anjali Singhai Jainb2a75c52015-04-27 14:57:20 -040012157 dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
12158 err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012159 goto err_pf_reset;
12160 }
12161
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +000012162 /* set up a default setting for link flow control */
12163 pf->hw.fc.requested_mode = I40E_FC_NONE;
12164
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012165 err = i40e_init_adminq(hw);
Carolyn Wyborny2b2426a762015-10-26 19:44:35 -040012166 if (err) {
12167 if (err == I40E_ERR_FIRMWARE_API_VERSION)
12168 dev_info(&pdev->dev,
12169 "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");
12170 else
12171 dev_info(&pdev->dev,
12172 "The driver for the device stopped because the device firmware failed to init. Try updating your NVM image.\n");
12173
12174 goto err_pf_reset;
12175 }
Filip Sadowski5bbb2e22017-06-07 05:43:09 -040012176 i40e_get_oem_version(hw);
Carolyn Wybornyf0b44442015-08-31 19:54:49 -040012177
Shannon Nelson6dec1012015-09-28 14:12:30 -040012178 /* provide nvm, fw, api versions */
12179 dev_info(&pdev->dev, "fw %d.%d.%05d api %d.%d nvm %s\n",
12180 hw->aq.fw_maj_ver, hw->aq.fw_min_ver, hw->aq.fw_build,
12181 hw->aq.api_maj_ver, hw->aq.api_min_ver,
12182 i40e_nvm_version_str(hw));
Carolyn Wybornyf0b44442015-08-31 19:54:49 -040012183
Catherine Sullivan7aa67612014-07-09 07:46:17 +000012184 if (hw->aq.api_maj_ver == I40E_FW_API_VERSION_MAJOR &&
Mitch Williams22b965512017-07-14 09:27:09 -040012185 hw->aq.api_min_ver > I40E_FW_MINOR_VERSION(hw))
Shannon Nelson278b6f62014-06-04 01:41:03 +000012186 dev_info(&pdev->dev,
Catherine Sullivan7aa67612014-07-09 07:46:17 +000012187 "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 -040012188 else if (hw->aq.api_maj_ver == 1 && hw->aq.api_min_ver < 4)
Shannon Nelson278b6f62014-06-04 01:41:03 +000012189 dev_info(&pdev->dev,
Catherine Sullivan7aa67612014-07-09 07:46:17 +000012190 "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 +000012191
Shannon Nelson4eb3f762014-03-06 08:59:58 +000012192 i40e_verify_eeprom(pf);
12193
Jesse Brandeburg2c5fe332014-04-23 04:49:57 +000012194 /* Rev 0 hardware was never productized */
12195 if (hw->revision_id < 1)
12196 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");
12197
Shannon Nelson6ff4ef82013-12-21 05:44:49 +000012198 i40e_clear_pxe_mode(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012199 err = i40e_get_capabilities(pf);
12200 if (err)
12201 goto err_adminq_setup;
12202
12203 err = i40e_sw_init(pf);
12204 if (err) {
12205 dev_info(&pdev->dev, "sw_init failed: %d\n", err);
12206 goto err_sw_init;
12207 }
12208
12209 err = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
Alexander Duyckc76cb6e2017-02-21 15:55:48 -080012210 hw->func_caps.num_rx_qp, 0, 0);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012211 if (err) {
12212 dev_info(&pdev->dev, "init_lan_hmc failed: %d\n", err);
12213 goto err_init_lan_hmc;
12214 }
12215
12216 err = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
12217 if (err) {
12218 dev_info(&pdev->dev, "configure_lan_hmc failed: %d\n", err);
12219 err = -ENOENT;
12220 goto err_configure_lan_hmc;
12221 }
12222
Neerav Parikhb686ece2014-12-14 01:55:11 +000012223 /* Disable LLDP for NICs that have firmware versions lower than v4.3.
12224 * Ignore error return codes because if it was already disabled via
12225 * hardware settings this will fail
12226 */
Jacob Kellerd36e41d2017-06-23 04:24:46 -040012227 if (pf->hw_features & I40E_HW_STOP_FW_LLDP) {
Neerav Parikhb686ece2014-12-14 01:55:11 +000012228 dev_info(&pdev->dev, "Stopping firmware LLDP agent.\n");
12229 i40e_aq_stop_lldp(hw, true, NULL);
12230 }
12231
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050012232 /* allow a platform config to override the HW addr */
12233 i40e_get_platform_mac_addr(pdev, pf);
Jacob Keller41c4c2b2017-04-05 07:50:57 -040012234
Jesse Brandeburgf62b5062013-11-28 06:39:27 +000012235 if (!is_valid_ether_addr(hw->mac.addr)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012236 dev_info(&pdev->dev, "invalid MAC address %pM\n", hw->mac.addr);
12237 err = -EIO;
12238 goto err_mac_addr;
12239 }
12240 dev_info(&pdev->dev, "MAC address: %pM\n", hw->mac.addr);
Greg Rose9a173902014-05-22 06:32:02 +000012241 ether_addr_copy(hw->mac.perm_addr, hw->mac.addr);
Neerav Parikh1f224ad2014-02-12 01:45:31 +000012242 i40e_get_port_mac_addr(hw, hw->mac.port_addr);
12243 if (is_valid_ether_addr(hw->mac.port_addr))
Jacob Kellerd36e41d2017-06-23 04:24:46 -040012244 pf->hw_features |= I40E_HW_PORT_ID_VALID;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012245
12246 pci_set_drvdata(pdev, pf);
12247 pci_save_state(pdev);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -080012248#ifdef CONFIG_I40E_DCB
12249 err = i40e_init_pf_dcb(pf);
12250 if (err) {
Shannon Nelsonaebfc812014-12-11 07:06:38 +000012251 dev_info(&pdev->dev, "DCB init failed %d, disabled\n", err);
David Ertmanc17ef432016-09-30 01:36:21 -070012252 pf->flags &= ~(I40E_FLAG_DCB_CAPABLE | I40E_FLAG_DCB_ENABLED);
Neerav Parikh014269f2014-04-01 07:11:48 +000012253 /* Continue without DCB enabled */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -080012254 }
12255#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012256
12257 /* set up periodic task facility */
12258 setup_timer(&pf->service_timer, i40e_service_timer, (unsigned long)pf);
12259 pf->service_timer_period = HZ;
12260
12261 INIT_WORK(&pf->service_task, i40e_service_task);
Jacob Keller0da36b92017-04-19 09:25:55 -040012262 clear_bit(__I40E_SERVICE_SCHED, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012263
Shannon Nelson1d5109d2015-08-26 15:14:08 -040012264 /* NVM bit on means WoL disabled for the port */
12265 i40e_read_nvm_word(hw, I40E_SR_NVM_WAKE_ON_LAN, &wol_nvm_bits);
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -080012266 if (BIT (hw->port) & wol_nvm_bits || hw->partition_id != 1)
Shannon Nelson1d5109d2015-08-26 15:14:08 -040012267 pf->wol_en = false;
12268 else
12269 pf->wol_en = true;
Shannon Nelson8e2773a2013-11-28 06:39:22 +000012270 device_set_wakeup_enable(&pf->pdev->dev, pf->wol_en);
12271
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012272 /* set up the main switch operations */
12273 i40e_determine_queue_usage(pf);
Jesse Brandeburgc11472802015-04-07 19:45:39 -040012274 err = i40e_init_interrupt_scheme(pf);
12275 if (err)
12276 goto err_switch_setup;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012277
Mitch Williams505682c2014-05-20 08:01:37 +000012278 /* The number of VSIs reported by the FW is the minimum guaranteed
12279 * to us; HW supports far more and we share the remaining pool with
12280 * the other PFs. We allocate space for more than the guarantee with
12281 * the understanding that we might not get them all later.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012282 */
Mitch Williams505682c2014-05-20 08:01:37 +000012283 if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
12284 pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
12285 else
12286 pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
12287
12288 /* Set up the *vsi struct and our local tracking of the MAIN PF vsi. */
Jesse Brandeburgd17038d2015-12-23 12:05:55 -080012289 pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
12290 GFP_KERNEL);
Wei Yongjuned87ac02013-09-24 05:17:25 +000012291 if (!pf->vsi) {
12292 err = -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012293 goto err_switch_setup;
Wei Yongjuned87ac02013-09-24 05:17:25 +000012294 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012295
Anjali Singhai Jainfa11cb32015-05-27 12:06:14 -040012296#ifdef CONFIG_PCI_IOV
12297 /* prep for VF support */
12298 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
12299 (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
Jacob Keller0da36b92017-04-19 09:25:55 -040012300 !test_bit(__I40E_BAD_EEPROM, pf->state)) {
Anjali Singhai Jainfa11cb32015-05-27 12:06:14 -040012301 if (pci_num_vf(pdev))
12302 pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
12303 }
12304#endif
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000012305 err = i40e_setup_pf_switch(pf, false);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012306 if (err) {
12307 dev_info(&pdev->dev, "setup_pf_switch failed: %d\n", err);
12308 goto err_vsis;
12309 }
Amritha Nambiar8f88b302017-09-07 04:00:17 -070012310 INIT_LIST_HEAD(&pf->vsi[pf->lan_vsi]->ch_list);
Helin Zhang58fc3262015-10-01 14:37:38 -040012311
12312 /* Make sure flow control is set according to current settings */
12313 err = i40e_set_fc(hw, &set_fc_aq_fail, true);
12314 if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_GET)
12315 dev_dbg(&pf->pdev->dev,
12316 "Set fc with err %s aq_err %s on get_phy_cap\n",
12317 i40e_stat_str(hw, err),
12318 i40e_aq_str(hw, hw->aq.asq_last_status));
12319 if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_SET)
12320 dev_dbg(&pf->pdev->dev,
12321 "Set fc with err %s aq_err %s on set_phy_config\n",
12322 i40e_stat_str(hw, err),
12323 i40e_aq_str(hw, hw->aq.asq_last_status));
12324 if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_UPDATE)
12325 dev_dbg(&pf->pdev->dev,
12326 "Set fc with err %s aq_err %s on get_link_info\n",
12327 i40e_stat_str(hw, err),
12328 i40e_aq_str(hw, hw->aq.asq_last_status));
12329
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +000012330 /* if FDIR VSI was set up, start it now */
Mitch Williams505682c2014-05-20 08:01:37 +000012331 for (i = 0; i < pf->num_alloc_vsi; i++) {
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +000012332 if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
12333 i40e_vsi_open(pf->vsi[i]);
12334 break;
12335 }
12336 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012337
Shannon Nelson2f0aff42016-01-04 10:33:08 -080012338 /* The driver only wants link up/down and module qualification
12339 * reports from firmware. Note the negative logic.
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +000012340 */
12341 err = i40e_aq_set_phy_int_mask(&pf->hw,
Shannon Nelson2f0aff42016-01-04 10:33:08 -080012342 ~(I40E_AQ_EVENT_LINK_UPDOWN |
Shannon Nelson867a79e2016-03-18 12:18:15 -070012343 I40E_AQ_EVENT_MEDIA_NA |
Shannon Nelson2f0aff42016-01-04 10:33:08 -080012344 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +000012345 if (err)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040012346 dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
12347 i40e_stat_str(&pf->hw, err),
12348 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +000012349
Anjali Singhai Jain4f2f017c2015-10-21 19:47:07 -040012350 /* Reconfigure hardware for allowing smaller MSS in the case
12351 * of TSO, so that we avoid the MDD being fired and causing
12352 * a reset in the case of small MSS+TSO.
12353 */
12354 val = rd32(hw, I40E_REG_MSS);
12355 if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
12356 val &= ~I40E_REG_MSS_MIN_MASK;
12357 val |= I40E_64BYTE_MSS;
12358 wr32(hw, I40E_REG_MSS, val);
12359 }
12360
Jacob Kellerd36e41d2017-06-23 04:24:46 -040012361 if (pf->hw_features & I40E_HW_RESTART_AUTONEG) {
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +000012362 msleep(75);
12363 err = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
12364 if (err)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040012365 dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
12366 i40e_stat_str(&pf->hw, err),
12367 i40e_aq_str(&pf->hw,
12368 pf->hw.aq.asq_last_status));
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +000012369 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012370 /* The main driver is (mostly) up and happy. We need to set this state
12371 * before setting up the misc vector or we get a race and the vector
12372 * ends up disabled forever.
12373 */
Mauro S. M. Rodrigues9e6c9c02017-05-12 23:26:56 -030012374 clear_bit(__I40E_DOWN, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012375
12376 /* In case of MSIX we are going to setup the misc vector right here
12377 * to handle admin queue events etc. In case of legacy and MSI
12378 * the misc functionality and queue processing is combined in
12379 * the same vector and that gets setup at open.
12380 */
12381 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
12382 err = i40e_setup_misc_vector(pf);
12383 if (err) {
12384 dev_info(&pdev->dev,
12385 "setup of misc vector failed: %d\n", err);
12386 goto err_vsis;
12387 }
12388 }
12389
Greg Rosedf805f62014-04-04 04:43:16 +000012390#ifdef CONFIG_PCI_IOV
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012391 /* prep for VF support */
12392 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
Shannon Nelson4eb3f762014-03-06 08:59:58 +000012393 (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
Jacob Keller0da36b92017-04-19 09:25:55 -040012394 !test_bit(__I40E_BAD_EEPROM, pf->state)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012395 /* disable link interrupts for VFs */
12396 val = rd32(hw, I40E_PFGEN_PORTMDIO_NUM);
12397 val &= ~I40E_PFGEN_PORTMDIO_NUM_VFLINK_STAT_ENA_MASK;
12398 wr32(hw, I40E_PFGEN_PORTMDIO_NUM, val);
12399 i40e_flush(hw);
Mitch Williams4aeec012014-02-13 03:48:47 -080012400
12401 if (pci_num_vf(pdev)) {
12402 dev_info(&pdev->dev,
12403 "Active VFs found, allocating resources.\n");
12404 err = i40e_alloc_vfs(pf, pci_num_vf(pdev));
12405 if (err)
12406 dev_info(&pdev->dev,
12407 "Error %d allocating resources for existing VFs\n",
12408 err);
12409 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012410 }
Greg Rosedf805f62014-04-04 04:43:16 +000012411#endif /* CONFIG_PCI_IOV */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012412
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060012413 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
12414 pf->iwarp_base_vector = i40e_get_lump(pf, pf->irq_pile,
12415 pf->num_iwarp_msix,
12416 I40E_IWARP_IRQ_PILE_ID);
12417 if (pf->iwarp_base_vector < 0) {
12418 dev_info(&pdev->dev,
12419 "failed to get tracking for %d vectors for IWARP err=%d\n",
12420 pf->num_iwarp_msix, pf->iwarp_base_vector);
12421 pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
12422 }
12423 }
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +000012424
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012425 i40e_dbg_pf_init(pf);
12426
12427 /* tell the firmware that we're starting */
Jesse Brandeburg44033fa2014-04-23 04:50:15 +000012428 i40e_send_version(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012429
12430 /* since everything's happy, start the service_task timer */
12431 mod_timer(&pf->service_timer,
12432 round_jiffies(jiffies + pf->service_timer_period));
12433
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060012434 /* add this PF to client device list and launch a client service task */
Mitch Williams004eb612017-04-04 12:40:16 -070012435 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
12436 err = i40e_lan_add_device(pf);
12437 if (err)
12438 dev_info(&pdev->dev, "Failed to add PF to client API service list: %d\n",
12439 err);
12440 }
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060012441
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040012442#define PCI_SPEED_SIZE 8
12443#define PCI_WIDTH_SIZE 8
12444 /* Devices on the IOSF bus do not have this information
12445 * and will report PCI Gen 1 x 1 by default so don't bother
12446 * checking them.
12447 */
Jacob Kellerd36e41d2017-06-23 04:24:46 -040012448 if (!(pf->hw_features & I40E_HW_NO_PCI_LINK_CHECK)) {
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040012449 char speed[PCI_SPEED_SIZE] = "Unknown";
12450 char width[PCI_WIDTH_SIZE] = "Unknown";
Catherine Sullivand4dfb812013-11-28 06:39:21 +000012451
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040012452 /* Get the negotiated link width and speed from PCI config
12453 * space
12454 */
12455 pcie_capability_read_word(pf->pdev, PCI_EXP_LNKSTA,
12456 &link_status);
Catherine Sullivand4dfb812013-11-28 06:39:21 +000012457
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040012458 i40e_set_pci_config_data(hw, link_status);
Catherine Sullivand4dfb812013-11-28 06:39:21 +000012459
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040012460 switch (hw->bus.speed) {
12461 case i40e_bus_speed_8000:
12462 strncpy(speed, "8.0", PCI_SPEED_SIZE); break;
12463 case i40e_bus_speed_5000:
12464 strncpy(speed, "5.0", PCI_SPEED_SIZE); break;
12465 case i40e_bus_speed_2500:
12466 strncpy(speed, "2.5", PCI_SPEED_SIZE); break;
12467 default:
12468 break;
12469 }
12470 switch (hw->bus.width) {
12471 case i40e_bus_width_pcie_x8:
12472 strncpy(width, "8", PCI_WIDTH_SIZE); break;
12473 case i40e_bus_width_pcie_x4:
12474 strncpy(width, "4", PCI_WIDTH_SIZE); break;
12475 case i40e_bus_width_pcie_x2:
12476 strncpy(width, "2", PCI_WIDTH_SIZE); break;
12477 case i40e_bus_width_pcie_x1:
12478 strncpy(width, "1", PCI_WIDTH_SIZE); break;
12479 default:
12480 break;
12481 }
12482
12483 dev_info(&pdev->dev, "PCI-Express: Speed %sGT/s Width x%s\n",
12484 speed, width);
12485
12486 if (hw->bus.width < i40e_bus_width_pcie_x8 ||
12487 hw->bus.speed < i40e_bus_speed_8000) {
12488 dev_warn(&pdev->dev, "PCI-Express bandwidth available for this device may be insufficient for optimal performance.\n");
12489 dev_warn(&pdev->dev, "Please move the device to a different PCI-e link with more lanes and/or higher transfer rate.\n");
12490 }
Catherine Sullivand4dfb812013-11-28 06:39:21 +000012491 }
12492
Catherine Sullivane8278452015-02-06 08:52:08 +000012493 /* get the requested speeds from the fw */
12494 err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities, NULL);
12495 if (err)
Neerav Parikh8279e492015-09-03 17:18:50 -040012496 dev_dbg(&pf->pdev->dev, "get requested speeds ret = %s last_status = %s\n",
12497 i40e_stat_str(&pf->hw, err),
12498 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Catherine Sullivane8278452015-02-06 08:52:08 +000012499 pf->hw.phy.link_info.requested_speeds = abilities.link_speed;
12500
Catherine Sullivanfc72dbc2015-09-01 11:36:30 -040012501 /* get the supported phy types from the fw */
12502 err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities, NULL);
12503 if (err)
12504 dev_dbg(&pf->pdev->dev, "get supported phy types ret = %s last_status = %s\n",
12505 i40e_stat_str(&pf->hw, err),
12506 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Catherine Sullivanfc72dbc2015-09-01 11:36:30 -040012507
Anjali Singhai Jaine7358f52015-10-01 14:37:34 -040012508 /* Add a filter to drop all Flow control frames from any VSI from being
12509 * transmitted. By doing so we stop a malicious VF from sending out
12510 * PAUSE or PFC frames and potentially controlling traffic for other
12511 * PF/VF VSIs.
12512 * The FW can still send Flow control frames if enabled.
12513 */
12514 i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
12515 pf->main_vsi_seid);
12516
Carolyn Wyborny31b606d2016-02-17 16:12:12 -080012517 if ((pf->hw.device_id == I40E_DEV_ID_10G_BASE_T) ||
Henry Tieman4f9b4302016-11-08 13:05:18 -080012518 (pf->hw.device_id == I40E_DEV_ID_10G_BASE_T4))
Jacob Kellerd36e41d2017-06-23 04:24:46 -040012519 pf->hw_features |= I40E_HW_PHY_CONTROLS_LEDS;
Harshitha Ramamurthy4ad9f4f2016-11-08 13:05:09 -080012520 if (pf->hw.device_id == I40E_DEV_ID_SFP_I_X722)
Jacob Kellerd36e41d2017-06-23 04:24:46 -040012521 pf->hw_features |= I40E_HW_HAVE_CRT_RETIMER;
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000012522 /* print a string summarizing features */
12523 i40e_print_features(pf);
12524
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012525 return 0;
12526
12527 /* Unwind what we've done if something failed in the setup */
12528err_vsis:
Mauro S. M. Rodrigues9e6c9c02017-05-12 23:26:56 -030012529 set_bit(__I40E_DOWN, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012530 i40e_clear_interrupt_scheme(pf);
12531 kfree(pf->vsi);
Shannon Nelson04b03012013-11-28 06:39:34 +000012532err_switch_setup:
12533 i40e_reset_interrupt_capability(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012534 del_timer_sync(&pf->service_timer);
12535err_mac_addr:
12536err_configure_lan_hmc:
12537 (void)i40e_shutdown_lan_hmc(hw);
12538err_init_lan_hmc:
12539 kfree(pf->qp_pile);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012540err_sw_init:
12541err_adminq_setup:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012542err_pf_reset:
12543 iounmap(hw->hw_addr);
12544err_ioremap:
12545 kfree(pf);
12546err_pf_alloc:
12547 pci_disable_pcie_error_reporting(pdev);
Johannes Thumshirn56d766d2016-06-07 09:44:05 +020012548 pci_release_mem_regions(pdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012549err_pci_reg:
12550err_dma:
12551 pci_disable_device(pdev);
12552 return err;
12553}
12554
12555/**
12556 * i40e_remove - Device removal routine
12557 * @pdev: PCI device information struct
12558 *
12559 * i40e_remove is called by the PCI subsystem to alert the driver
12560 * that is should release a PCI device. This could be caused by a
12561 * Hot-Plug event, or because the driver is going to be removed from
12562 * memory.
12563 **/
12564static void i40e_remove(struct pci_dev *pdev)
12565{
12566 struct i40e_pf *pf = pci_get_drvdata(pdev);
Carolyn Wybornybcab2db2015-09-28 14:16:55 -040012567 struct i40e_hw *hw = &pf->hw;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012568 i40e_status ret_code;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012569 int i;
12570
12571 i40e_dbg_pf_exit(pf);
12572
Jacob Kellerbeb0dff2014-01-11 05:43:19 +000012573 i40e_ptp_stop(pf);
12574
Carolyn Wybornybcab2db2015-09-28 14:16:55 -040012575 /* Disable RSS in hw */
Shannon Nelson272cdaf22016-02-17 16:12:21 -080012576 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), 0);
12577 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), 0);
Carolyn Wybornybcab2db2015-09-28 14:16:55 -040012578
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012579 /* no more scheduling of any task */
Jacob Keller0da36b92017-04-19 09:25:55 -040012580 set_bit(__I40E_SUSPENDED, pf->state);
Mauro S. M. Rodrigues9e6c9c02017-05-12 23:26:56 -030012581 set_bit(__I40E_DOWN, pf->state);
Shannon Nelsonc99abb42016-03-10 14:59:45 -080012582 if (pf->service_timer.data)
12583 del_timer_sync(&pf->service_timer);
12584 if (pf->service_task.func)
12585 cancel_work_sync(&pf->service_task);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012586
Mitch Williams921c4672017-03-30 00:46:08 -070012587 /* Client close must be called explicitly here because the timer
12588 * has been stopped.
12589 */
12590 i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
12591
Mitch Williamseb2d80b2014-02-13 03:48:48 -080012592 if (pf->flags & I40E_FLAG_SRIOV_ENABLED) {
12593 i40e_free_vfs(pf);
12594 pf->flags &= ~I40E_FLAG_SRIOV_ENABLED;
12595 }
12596
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012597 i40e_fdir_teardown(pf);
12598
12599 /* If there is a switch structure or any orphans, remove them.
12600 * This will leave only the PF's VSI remaining.
12601 */
12602 for (i = 0; i < I40E_MAX_VEB; i++) {
12603 if (!pf->veb[i])
12604 continue;
12605
12606 if (pf->veb[i]->uplink_seid == pf->mac_seid ||
12607 pf->veb[i]->uplink_seid == 0)
12608 i40e_switch_branch_release(pf->veb[i]);
12609 }
12610
12611 /* Now we can shutdown the PF's VSI, just before we kill
12612 * adminq and hmc.
12613 */
12614 if (pf->vsi[pf->lan_vsi])
12615 i40e_vsi_release(pf->vsi[pf->lan_vsi]);
12616
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060012617 /* remove attached clients */
Mitch Williams004eb612017-04-04 12:40:16 -070012618 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
12619 ret_code = i40e_lan_del_device(pf);
12620 if (ret_code)
12621 dev_warn(&pdev->dev, "Failed to delete client device: %d\n",
12622 ret_code);
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060012623 }
12624
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012625 /* shutdown and destroy the HMC */
Jesse Brandeburgf734dff2016-01-15 14:33:11 -080012626 if (hw->hmc.hmc_obj) {
12627 ret_code = i40e_shutdown_lan_hmc(hw);
Shannon Nelson60442de2014-04-23 04:50:13 +000012628 if (ret_code)
12629 dev_warn(&pdev->dev,
12630 "Failed to destroy the HMC resources: %d\n",
12631 ret_code);
12632 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012633
12634 /* shutdown the adminq */
Henry Tiemanac9c5c62016-09-06 18:05:11 -070012635 i40e_shutdown_adminq(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012636
Jesse Brandeburg8ddb3322015-11-18 15:47:06 -080012637 /* destroy the locks only once, here */
12638 mutex_destroy(&hw->aq.arq_mutex);
12639 mutex_destroy(&hw->aq.asq_mutex);
12640
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012641 /* Clear all dynamic memory lists of rings, q_vectors, and VSIs */
12642 i40e_clear_interrupt_scheme(pf);
Mitch Williams505682c2014-05-20 08:01:37 +000012643 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012644 if (pf->vsi[i]) {
12645 i40e_vsi_clear_rings(pf->vsi[i]);
12646 i40e_vsi_clear(pf->vsi[i]);
12647 pf->vsi[i] = NULL;
12648 }
12649 }
12650
12651 for (i = 0; i < I40E_MAX_VEB; i++) {
12652 kfree(pf->veb[i]);
12653 pf->veb[i] = NULL;
12654 }
12655
12656 kfree(pf->qp_pile);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012657 kfree(pf->vsi);
12658
Jesse Brandeburgf734dff2016-01-15 14:33:11 -080012659 iounmap(hw->hw_addr);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012660 kfree(pf);
Johannes Thumshirn56d766d2016-06-07 09:44:05 +020012661 pci_release_mem_regions(pdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012662
12663 pci_disable_pcie_error_reporting(pdev);
12664 pci_disable_device(pdev);
12665}
12666
12667/**
12668 * i40e_pci_error_detected - warning that something funky happened in PCI land
12669 * @pdev: PCI device information struct
12670 *
12671 * Called to warn that something happened and the error handling steps
12672 * are in progress. Allows the driver to quiesce things, be ready for
12673 * remediation.
12674 **/
12675static pci_ers_result_t i40e_pci_error_detected(struct pci_dev *pdev,
12676 enum pci_channel_state error)
12677{
12678 struct i40e_pf *pf = pci_get_drvdata(pdev);
12679
12680 dev_info(&pdev->dev, "%s: error %d\n", __func__, error);
12681
Guilherme G Piccoliedfc23ee2016-10-03 00:31:12 -070012682 if (!pf) {
12683 dev_info(&pdev->dev,
12684 "Cannot recover - error happened during device probe\n");
12685 return PCI_ERS_RESULT_DISCONNECT;
12686 }
12687
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012688 /* shutdown all operations */
Jacob Kellerdfc4ff62017-06-07 05:43:13 -040012689 if (!test_bit(__I40E_SUSPENDED, pf->state))
12690 i40e_prep_for_reset(pf, false);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012691
12692 /* Request a slot reset */
12693 return PCI_ERS_RESULT_NEED_RESET;
12694}
12695
12696/**
12697 * i40e_pci_error_slot_reset - a PCI slot reset just happened
12698 * @pdev: PCI device information struct
12699 *
12700 * Called to find if the driver can work with the device now that
12701 * the pci slot has been reset. If a basic connection seems good
12702 * (registers are readable and have sane content) then return a
12703 * happy little PCI_ERS_RESULT_xxx.
12704 **/
12705static pci_ers_result_t i40e_pci_error_slot_reset(struct pci_dev *pdev)
12706{
12707 struct i40e_pf *pf = pci_get_drvdata(pdev);
12708 pci_ers_result_t result;
12709 int err;
12710 u32 reg;
12711
Shannon Nelsonfb43201f2015-08-26 15:14:17 -040012712 dev_dbg(&pdev->dev, "%s\n", __func__);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012713 if (pci_enable_device_mem(pdev)) {
12714 dev_info(&pdev->dev,
12715 "Cannot re-enable PCI device after reset.\n");
12716 result = PCI_ERS_RESULT_DISCONNECT;
12717 } else {
12718 pci_set_master(pdev);
12719 pci_restore_state(pdev);
12720 pci_save_state(pdev);
12721 pci_wake_from_d3(pdev, false);
12722
12723 reg = rd32(&pf->hw, I40E_GLGEN_RTRIG);
12724 if (reg == 0)
12725 result = PCI_ERS_RESULT_RECOVERED;
12726 else
12727 result = PCI_ERS_RESULT_DISCONNECT;
12728 }
12729
12730 err = pci_cleanup_aer_uncorrect_error_status(pdev);
12731 if (err) {
12732 dev_info(&pdev->dev,
12733 "pci_cleanup_aer_uncorrect_error_status failed 0x%0x\n",
12734 err);
12735 /* non-fatal, continue */
12736 }
12737
12738 return result;
12739}
12740
12741/**
Alan Brady19b79602017-08-29 05:32:39 -040012742 * i40e_pci_error_reset_prepare - prepare device driver for pci reset
12743 * @pdev: PCI device information struct
12744 */
12745static void i40e_pci_error_reset_prepare(struct pci_dev *pdev)
12746{
12747 struct i40e_pf *pf = pci_get_drvdata(pdev);
12748
12749 i40e_prep_for_reset(pf, false);
12750}
12751
12752/**
12753 * i40e_pci_error_reset_done - pci reset done, device driver reset can begin
12754 * @pdev: PCI device information struct
12755 */
12756static void i40e_pci_error_reset_done(struct pci_dev *pdev)
12757{
12758 struct i40e_pf *pf = pci_get_drvdata(pdev);
12759
12760 i40e_reset_and_rebuild(pf, false, false);
12761}
12762
12763/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012764 * i40e_pci_error_resume - restart operations after PCI error recovery
12765 * @pdev: PCI device information struct
12766 *
12767 * Called to allow the driver to bring things back up after PCI error
12768 * and/or reset recovery has finished.
12769 **/
12770static void i40e_pci_error_resume(struct pci_dev *pdev)
12771{
12772 struct i40e_pf *pf = pci_get_drvdata(pdev);
12773
Shannon Nelsonfb43201f2015-08-26 15:14:17 -040012774 dev_dbg(&pdev->dev, "%s\n", __func__);
Jacob Keller0da36b92017-04-19 09:25:55 -040012775 if (test_bit(__I40E_SUSPENDED, pf->state))
Shannon Nelson9007bcc2013-11-26 10:49:23 +000012776 return;
12777
Jacob Kellerdfc4ff62017-06-07 05:43:13 -040012778 i40e_handle_reset_warning(pf, false);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012779}
12780
Shannon Nelson9007bcc2013-11-26 10:49:23 +000012781/**
Joshua Hay1d680052016-12-12 15:44:08 -080012782 * i40e_enable_mc_magic_wake - enable multicast magic packet wake up
12783 * using the mac_address_write admin q function
12784 * @pf: pointer to i40e_pf struct
12785 **/
12786static void i40e_enable_mc_magic_wake(struct i40e_pf *pf)
12787{
12788 struct i40e_hw *hw = &pf->hw;
12789 i40e_status ret;
12790 u8 mac_addr[6];
12791 u16 flags = 0;
12792
12793 /* Get current MAC address in case it's an LAA */
12794 if (pf->vsi[pf->lan_vsi] && pf->vsi[pf->lan_vsi]->netdev) {
12795 ether_addr_copy(mac_addr,
12796 pf->vsi[pf->lan_vsi]->netdev->dev_addr);
12797 } else {
12798 dev_err(&pf->pdev->dev,
12799 "Failed to retrieve MAC address; using default\n");
12800 ether_addr_copy(mac_addr, hw->mac.addr);
12801 }
12802
12803 /* The FW expects the mac address write cmd to first be called with
12804 * one of these flags before calling it again with the multicast
12805 * enable flags.
12806 */
12807 flags = I40E_AQC_WRITE_TYPE_LAA_WOL;
12808
12809 if (hw->func_caps.flex10_enable && hw->partition_id != 1)
12810 flags = I40E_AQC_WRITE_TYPE_LAA_ONLY;
12811
12812 ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
12813 if (ret) {
12814 dev_err(&pf->pdev->dev,
12815 "Failed to update MAC address registers; cannot enable Multicast Magic packet wake up");
12816 return;
12817 }
12818
12819 flags = I40E_AQC_MC_MAG_EN
12820 | I40E_AQC_WOL_PRESERVE_ON_PFR
12821 | I40E_AQC_WRITE_TYPE_UPDATE_MC_MAG;
12822 ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
12823 if (ret)
12824 dev_err(&pf->pdev->dev,
12825 "Failed to enable Multicast Magic Packet wake up\n");
12826}
12827
12828/**
Shannon Nelson9007bcc2013-11-26 10:49:23 +000012829 * i40e_shutdown - PCI callback for shutting down
12830 * @pdev: PCI device information struct
12831 **/
12832static void i40e_shutdown(struct pci_dev *pdev)
12833{
12834 struct i40e_pf *pf = pci_get_drvdata(pdev);
Shannon Nelson8e2773a2013-11-28 06:39:22 +000012835 struct i40e_hw *hw = &pf->hw;
Shannon Nelson9007bcc2013-11-26 10:49:23 +000012836
Jacob Keller0da36b92017-04-19 09:25:55 -040012837 set_bit(__I40E_SUSPENDED, pf->state);
Mauro S. M. Rodrigues9e6c9c02017-05-12 23:26:56 -030012838 set_bit(__I40E_DOWN, pf->state);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000012839 rtnl_lock();
Maciej Sosin373149f2017-04-05 07:50:55 -040012840 i40e_prep_for_reset(pf, true);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000012841 rtnl_unlock();
12842
Shannon Nelson8e2773a2013-11-28 06:39:22 +000012843 wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
12844 wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
12845
Catherine Sullivan02b42492015-07-10 19:35:59 -040012846 del_timer_sync(&pf->service_timer);
12847 cancel_work_sync(&pf->service_task);
12848 i40e_fdir_teardown(pf);
12849
Mitch Williams921c4672017-03-30 00:46:08 -070012850 /* Client close must be called explicitly here because the timer
12851 * has been stopped.
12852 */
12853 i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
12854
Jacob Kellerd36e41d2017-06-23 04:24:46 -040012855 if (pf->wol_en && (pf->hw_features & I40E_HW_WOL_MC_MAGIC_PKT_WAKE))
Joshua Hay1d680052016-12-12 15:44:08 -080012856 i40e_enable_mc_magic_wake(pf);
12857
Jacob Kellerdfc4ff62017-06-07 05:43:13 -040012858 i40e_prep_for_reset(pf, false);
Catherine Sullivan02b42492015-07-10 19:35:59 -040012859
12860 wr32(hw, I40E_PFPM_APM,
12861 (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
12862 wr32(hw, I40E_PFPM_WUFC,
12863 (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
12864
Shannon Nelsone1477582015-02-21 06:44:33 +000012865 i40e_clear_interrupt_scheme(pf);
12866
Shannon Nelson9007bcc2013-11-26 10:49:23 +000012867 if (system_state == SYSTEM_POWER_OFF) {
Shannon Nelson8e2773a2013-11-28 06:39:22 +000012868 pci_wake_from_d3(pdev, pf->wol_en);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000012869 pci_set_power_state(pdev, PCI_D3hot);
12870 }
12871}
12872
12873#ifdef CONFIG_PM
12874/**
Jacob Keller0e5d3da2017-07-14 09:27:03 -040012875 * i40e_suspend - PM callback for moving to D3
12876 * @dev: generic device information structure
Shannon Nelson9007bcc2013-11-26 10:49:23 +000012877 **/
Jacob Keller0e5d3da2017-07-14 09:27:03 -040012878static int i40e_suspend(struct device *dev)
Shannon Nelson9007bcc2013-11-26 10:49:23 +000012879{
Jacob Keller0e5d3da2017-07-14 09:27:03 -040012880 struct pci_dev *pdev = to_pci_dev(dev);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000012881 struct i40e_pf *pf = pci_get_drvdata(pdev);
Shannon Nelson8e2773a2013-11-28 06:39:22 +000012882 struct i40e_hw *hw = &pf->hw;
Shannon Nelson9007bcc2013-11-26 10:49:23 +000012883
Jacob Keller401586c2017-07-14 09:27:04 -040012884 /* If we're already suspended, then there is nothing to do */
12885 if (test_and_set_bit(__I40E_SUSPENDED, pf->state))
12886 return 0;
12887
Mauro S. M. Rodrigues9e6c9c02017-05-12 23:26:56 -030012888 set_bit(__I40E_DOWN, pf->state);
Mitch Williams3932dbf2015-03-31 00:45:04 -070012889
Jacob Keller5c499222017-07-14 09:27:05 -040012890 /* Ensure service task will not be running */
12891 del_timer_sync(&pf->service_timer);
12892 cancel_work_sync(&pf->service_task);
12893
Jacob Kellerd36e41d2017-06-23 04:24:46 -040012894 if (pf->wol_en && (pf->hw_features & I40E_HW_WOL_MC_MAGIC_PKT_WAKE))
Joshua Hay1d680052016-12-12 15:44:08 -080012895 i40e_enable_mc_magic_wake(pf);
12896
Jacob Kellerdfc4ff62017-06-07 05:43:13 -040012897 i40e_prep_for_reset(pf, false);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000012898
Shannon Nelson8e2773a2013-11-28 06:39:22 +000012899 wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
12900 wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
12901
Jacob Kellerb980c062017-07-14 09:27:06 -040012902 /* Clear the interrupt scheme and release our IRQs so that the system
12903 * can safely hibernate even when there are a large number of CPUs.
12904 * Otherwise hibernation might fail when mapping all the vectors back
12905 * to CPU0.
12906 */
12907 i40e_clear_interrupt_scheme(pf);
Jacob Kellerc17401a2017-07-14 09:27:02 -040012908
Jacob Keller0e5d3da2017-07-14 09:27:03 -040012909 return 0;
Shannon Nelson9007bcc2013-11-26 10:49:23 +000012910}
12911
12912/**
Jacob Keller0e5d3da2017-07-14 09:27:03 -040012913 * i40e_resume - PM callback for waking up from D3
12914 * @dev: generic device information structure
Shannon Nelson9007bcc2013-11-26 10:49:23 +000012915 **/
Jacob Keller0e5d3da2017-07-14 09:27:03 -040012916static int i40e_resume(struct device *dev)
Shannon Nelson9007bcc2013-11-26 10:49:23 +000012917{
Jacob Keller0e5d3da2017-07-14 09:27:03 -040012918 struct pci_dev *pdev = to_pci_dev(dev);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000012919 struct i40e_pf *pf = pci_get_drvdata(pdev);
Jacob Kellerb980c062017-07-14 09:27:06 -040012920 int err;
Shannon Nelson9007bcc2013-11-26 10:49:23 +000012921
Jacob Keller401586c2017-07-14 09:27:04 -040012922 /* If we're not suspended, then there is nothing to do */
12923 if (!test_bit(__I40E_SUSPENDED, pf->state))
12924 return 0;
12925
Jacob Kellerb980c062017-07-14 09:27:06 -040012926 /* We cleared the interrupt scheme when we suspended, so we need to
12927 * restore it now to resume device functionality.
12928 */
12929 err = i40e_restore_interrupt_scheme(pf);
12930 if (err) {
12931 dev_err(&pdev->dev, "Cannot restore interrupt scheme: %d\n",
12932 err);
12933 }
12934
Jacob Keller401586c2017-07-14 09:27:04 -040012935 clear_bit(__I40E_DOWN, pf->state);
12936 i40e_reset_and_rebuild(pf, false, false);
12937
12938 /* Clear suspended state last after everything is recovered */
12939 clear_bit(__I40E_SUSPENDED, pf->state);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000012940
Jacob Keller5c499222017-07-14 09:27:05 -040012941 /* Restart the service task */
12942 mod_timer(&pf->service_timer,
12943 round_jiffies(jiffies + pf->service_timer_period));
12944
Shannon Nelson9007bcc2013-11-26 10:49:23 +000012945 return 0;
12946}
12947
Jacob Keller0e5d3da2017-07-14 09:27:03 -040012948#endif /* CONFIG_PM */
12949
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012950static const struct pci_error_handlers i40e_err_handler = {
12951 .error_detected = i40e_pci_error_detected,
12952 .slot_reset = i40e_pci_error_slot_reset,
Alan Brady19b79602017-08-29 05:32:39 -040012953 .reset_prepare = i40e_pci_error_reset_prepare,
12954 .reset_done = i40e_pci_error_reset_done,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012955 .resume = i40e_pci_error_resume,
12956};
12957
Jacob Keller0e5d3da2017-07-14 09:27:03 -040012958static SIMPLE_DEV_PM_OPS(i40e_pm_ops, i40e_suspend, i40e_resume);
12959
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012960static struct pci_driver i40e_driver = {
12961 .name = i40e_driver_name,
12962 .id_table = i40e_pci_tbl,
12963 .probe = i40e_probe,
12964 .remove = i40e_remove,
Shannon Nelson9007bcc2013-11-26 10:49:23 +000012965#ifdef CONFIG_PM
Jacob Keller0e5d3da2017-07-14 09:27:03 -040012966 .driver = {
12967 .pm = &i40e_pm_ops,
12968 },
12969#endif /* CONFIG_PM */
Shannon Nelson9007bcc2013-11-26 10:49:23 +000012970 .shutdown = i40e_shutdown,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012971 .err_handler = &i40e_err_handler,
12972 .sriov_configure = i40e_pci_sriov_configure,
12973};
12974
12975/**
12976 * i40e_init_module - Driver registration routine
12977 *
12978 * i40e_init_module is the first routine called when the driver is
12979 * loaded. All it does is register with the PCI subsystem.
12980 **/
12981static int __init i40e_init_module(void)
12982{
12983 pr_info("%s: %s - version %s\n", i40e_driver_name,
12984 i40e_driver_string, i40e_driver_version_str);
12985 pr_info("%s: %s\n", i40e_driver_name, i40e_copyright);
Greg Rose96664482015-02-06 08:52:13 +000012986
Jacob Keller4d5957c2017-06-20 15:16:54 -070012987 /* There is no need to throttle the number of active tasks because
12988 * each device limits its own task using a state bit for scheduling
12989 * the service task, and the device tasks do not interfere with each
12990 * other, so we don't set a max task limit. We must set WQ_MEM_RECLAIM
12991 * since we need to be able to guarantee forward progress even under
12992 * memory pressure.
Jesse Brandeburg2803b162015-12-22 14:25:08 -080012993 */
Jacob Keller4d5957c2017-06-20 15:16:54 -070012994 i40e_wq = alloc_workqueue("%s", WQ_MEM_RECLAIM, 0, i40e_driver_name);
Jesse Brandeburg2803b162015-12-22 14:25:08 -080012995 if (!i40e_wq) {
12996 pr_err("%s: Failed to create workqueue\n", i40e_driver_name);
12997 return -ENOMEM;
12998 }
12999
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013000 i40e_dbg_init();
13001 return pci_register_driver(&i40e_driver);
13002}
13003module_init(i40e_init_module);
13004
13005/**
13006 * i40e_exit_module - Driver exit cleanup routine
13007 *
13008 * i40e_exit_module is called just before the driver is removed
13009 * from memory.
13010 **/
13011static void __exit i40e_exit_module(void)
13012{
13013 pci_unregister_driver(&i40e_driver);
Jesse Brandeburg2803b162015-12-22 14:25:08 -080013014 destroy_workqueue(i40e_wq);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013015 i40e_dbg_exit();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013016}
13017module_exit(i40e_exit_module);