blob: 69d15560fdf982ee2078605487b62f69bd229657 [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
Alice Michaelf2fc31e2017-10-27 11:06:56 -040050#define DRV_VERSION_MINOR 3
51#define DRV_VERSION_BUILD 2
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);
Amritha Nambiar2f4b4112017-10-27 02:36:01 -070072static int i40e_add_del_cloud_filter(struct i40e_vsi *vsi,
73 struct i40e_cloud_filter *filter,
74 bool add);
75static int i40e_add_del_cloud_filter_big_buf(struct i40e_vsi *vsi,
76 struct i40e_cloud_filter *filter,
77 bool add);
78static int i40e_get_capabilities(struct i40e_pf *pf,
79 enum i40e_admin_queue_opc list_type);
80
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000081
82/* i40e_pci_tbl - PCI Device ID Table
83 *
84 * Last entry must be all 0s
85 *
86 * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
87 * Class, Class Mask, private data (not used) }
88 */
Benoit Taine9baa3c32014-08-08 15:56:03 +020089static const struct pci_device_id i40e_pci_tbl[] = {
Shannon Nelsonab600852014-01-17 15:36:39 -080090 {PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_XL710), 0},
Shannon Nelsonab600852014-01-17 15:36:39 -080091 {PCI_VDEVICE(INTEL, I40E_DEV_ID_QEMU), 0},
Shannon Nelsonab600852014-01-17 15:36:39 -080092 {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_B), 0},
93 {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_C), 0},
Shannon Nelsonab600852014-01-17 15:36:39 -080094 {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_A), 0},
95 {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_B), 0},
96 {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_C), 0},
Mitch Williams5960d332014-09-13 07:40:47 +000097 {PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T), 0},
Shannon Nelsonbc5166b92015-08-26 15:14:10 -040098 {PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T4), 0},
Anjali Singhai Jain35dae512015-12-22 14:25:03 -080099 {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_X722), 0},
100 {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_X722), 0},
Anjali Singhai Jain87e6c1d2015-06-05 12:20:25 -0400101 {PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_X722), 0},
102 {PCI_VDEVICE(INTEL, I40E_DEV_ID_1G_BASE_T_X722), 0},
103 {PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T_X722), 0},
Catherine Sullivand6bf58c2016-03-18 12:18:08 -0700104 {PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_I_X722), 0},
Shannon Nelson48a3b512015-07-23 16:54:39 -0400105 {PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2), 0},
106 {PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2_A), 0},
Carolyn Wyborny31232372016-11-21 13:03:48 -0800107 {PCI_VDEVICE(INTEL, I40E_DEV_ID_25G_B), 0},
108 {PCI_VDEVICE(INTEL, I40E_DEV_ID_25G_SFP28), 0},
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000109 /* required last entry */
110 {0, }
111};
112MODULE_DEVICE_TABLE(pci, i40e_pci_tbl);
113
114#define I40E_MAX_VF_COUNT 128
115static int debug = -1;
Alexander Duyck5d4ca232016-09-30 08:21:46 -0400116module_param(debug, uint, 0);
117MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all), Debug mask (0x8XXXXXXX)");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000118
119MODULE_AUTHOR("Intel Corporation, <e1000-devel@lists.sourceforge.net>");
120MODULE_DESCRIPTION("Intel(R) Ethernet Connection XL710 Network Driver");
121MODULE_LICENSE("GPL");
122MODULE_VERSION(DRV_VERSION);
123
Jesse Brandeburg2803b162015-12-22 14:25:08 -0800124static struct workqueue_struct *i40e_wq;
125
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000126/**
127 * i40e_allocate_dma_mem_d - OS specific memory alloc for shared code
128 * @hw: pointer to the HW structure
129 * @mem: ptr to mem struct to fill out
130 * @size: size of memory requested
131 * @alignment: what to align the allocation to
132 **/
133int i40e_allocate_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem,
134 u64 size, u32 alignment)
135{
136 struct i40e_pf *pf = (struct i40e_pf *)hw->back;
137
138 mem->size = ALIGN(size, alignment);
139 mem->va = dma_zalloc_coherent(&pf->pdev->dev, mem->size,
140 &mem->pa, GFP_KERNEL);
Jesse Brandeburg93bc73b2013-09-13 08:23:18 +0000141 if (!mem->va)
142 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000143
Jesse Brandeburg93bc73b2013-09-13 08:23:18 +0000144 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000145}
146
147/**
148 * i40e_free_dma_mem_d - OS specific memory free for shared code
149 * @hw: pointer to the HW structure
150 * @mem: ptr to mem struct to free
151 **/
152int i40e_free_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem)
153{
154 struct i40e_pf *pf = (struct i40e_pf *)hw->back;
155
156 dma_free_coherent(&pf->pdev->dev, mem->size, mem->va, mem->pa);
157 mem->va = NULL;
158 mem->pa = 0;
159 mem->size = 0;
160
161 return 0;
162}
163
164/**
165 * i40e_allocate_virt_mem_d - OS specific memory alloc for shared code
166 * @hw: pointer to the HW structure
167 * @mem: ptr to mem struct to fill out
168 * @size: size of memory requested
169 **/
170int i40e_allocate_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem,
171 u32 size)
172{
173 mem->size = size;
174 mem->va = kzalloc(size, GFP_KERNEL);
175
Jesse Brandeburg93bc73b2013-09-13 08:23:18 +0000176 if (!mem->va)
177 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000178
Jesse Brandeburg93bc73b2013-09-13 08:23:18 +0000179 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000180}
181
182/**
183 * i40e_free_virt_mem_d - OS specific memory free for shared code
184 * @hw: pointer to the HW structure
185 * @mem: ptr to mem struct to free
186 **/
187int i40e_free_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem)
188{
189 /* it's ok to kfree a NULL pointer */
190 kfree(mem->va);
191 mem->va = NULL;
192 mem->size = 0;
193
194 return 0;
195}
196
197/**
198 * i40e_get_lump - find a lump of free generic resource
199 * @pf: board private structure
200 * @pile: the pile of resource to search
201 * @needed: the number of items needed
202 * @id: an owner id to stick on the items assigned
203 *
204 * Returns the base item index of the lump, or negative for error
205 *
206 * The search_hint trick and lack of advanced fit-finding only work
207 * because we're highly likely to have all the same size lump requests.
208 * Linear search time and any fragmentation should be minimal.
209 **/
210static int i40e_get_lump(struct i40e_pf *pf, struct i40e_lump_tracking *pile,
211 u16 needed, u16 id)
212{
213 int ret = -ENOMEM;
Jesse Brandeburgddf434a2013-09-13 08:23:19 +0000214 int i, j;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000215
216 if (!pile || needed == 0 || id >= I40E_PILE_VALID_BIT) {
217 dev_info(&pf->pdev->dev,
218 "param err: pile=%p needed=%d id=0x%04x\n",
219 pile, needed, id);
220 return -EINVAL;
221 }
222
223 /* start the linear search with an imperfect hint */
224 i = pile->search_hint;
Jesse Brandeburgddf434a2013-09-13 08:23:19 +0000225 while (i < pile->num_entries) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000226 /* skip already allocated entries */
227 if (pile->list[i] & I40E_PILE_VALID_BIT) {
228 i++;
229 continue;
230 }
231
232 /* do we have enough in this lump? */
233 for (j = 0; (j < needed) && ((i+j) < pile->num_entries); j++) {
234 if (pile->list[i+j] & I40E_PILE_VALID_BIT)
235 break;
236 }
237
238 if (j == needed) {
239 /* there was enough, so assign it to the requestor */
240 for (j = 0; j < needed; j++)
241 pile->list[i+j] = id | I40E_PILE_VALID_BIT;
242 ret = i;
243 pile->search_hint = i + j;
Jesse Brandeburgddf434a2013-09-13 08:23:19 +0000244 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000245 }
Jesse Brandeburg6995b362015-08-28 17:55:54 -0400246
247 /* not enough, so skip over it and continue looking */
248 i += j;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000249 }
250
251 return ret;
252}
253
254/**
255 * i40e_put_lump - return a lump of generic resource
256 * @pile: the pile of resource to search
257 * @index: the base item index
258 * @id: the owner id of the items assigned
259 *
260 * Returns the count of items in the lump
261 **/
262static int i40e_put_lump(struct i40e_lump_tracking *pile, u16 index, u16 id)
263{
264 int valid_id = (id | I40E_PILE_VALID_BIT);
265 int count = 0;
266 int i;
267
268 if (!pile || index >= pile->num_entries)
269 return -EINVAL;
270
271 for (i = index;
272 i < pile->num_entries && pile->list[i] == valid_id;
273 i++) {
274 pile->list[i] = 0;
275 count++;
276 }
277
278 if (count && index < pile->search_hint)
279 pile->search_hint = index;
280
281 return count;
282}
283
284/**
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700285 * i40e_find_vsi_from_id - searches for the vsi with the given id
286 * @pf - the pf structure to search for the vsi
287 * @id - id of the vsi it is searching for
288 **/
289struct i40e_vsi *i40e_find_vsi_from_id(struct i40e_pf *pf, u16 id)
290{
291 int i;
292
293 for (i = 0; i < pf->num_alloc_vsi; i++)
294 if (pf->vsi[i] && (pf->vsi[i]->id == id))
295 return pf->vsi[i];
296
297 return NULL;
298}
299
300/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000301 * i40e_service_event_schedule - Schedule the service task to wake up
302 * @pf: board private structure
303 *
304 * If not already scheduled, this puts the task into the work queue
305 **/
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -0600306void i40e_service_event_schedule(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000307{
Mauro S. M. Rodrigues9e6c9c02017-05-12 23:26:56 -0300308 if (!test_bit(__I40E_DOWN, pf->state) &&
Jacob Keller0da36b92017-04-19 09:25:55 -0400309 !test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
Jesse Brandeburg2803b162015-12-22 14:25:08 -0800310 queue_work(i40e_wq, &pf->service_task);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000311}
312
313/**
314 * i40e_tx_timeout - Respond to a Tx Hang
315 * @netdev: network interface device structure
316 *
317 * If any port has noticed a Tx timeout, it is likely that the whole
318 * device is munged, not just the one netdev port, so go for the full
319 * reset.
320 **/
321static void i40e_tx_timeout(struct net_device *netdev)
322{
323 struct i40e_netdev_priv *np = netdev_priv(netdev);
324 struct i40e_vsi *vsi = np->vsi;
325 struct i40e_pf *pf = vsi->back;
Kiran Patilb03a8c12015-09-24 18:13:15 -0400326 struct i40e_ring *tx_ring = NULL;
327 unsigned int i, hung_queue = 0;
328 u32 head, val;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000329
330 pf->tx_timeout_count++;
331
Kiran Patilb03a8c12015-09-24 18:13:15 -0400332 /* find the stopped queue the same way the stack does */
333 for (i = 0; i < netdev->num_tx_queues; i++) {
334 struct netdev_queue *q;
335 unsigned long trans_start;
336
337 q = netdev_get_tx_queue(netdev, i);
Florian Westphal9b366272016-05-03 16:33:14 +0200338 trans_start = q->trans_start;
Kiran Patilb03a8c12015-09-24 18:13:15 -0400339 if (netif_xmit_stopped(q) &&
340 time_after(jiffies,
341 (trans_start + netdev->watchdog_timeo))) {
342 hung_queue = i;
343 break;
344 }
345 }
346
347 if (i == netdev->num_tx_queues) {
348 netdev_info(netdev, "tx_timeout: no netdev hung queue found\n");
349 } else {
350 /* now that we have an index, find the tx_ring struct */
351 for (i = 0; i < vsi->num_queue_pairs; i++) {
352 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc) {
353 if (hung_queue ==
354 vsi->tx_rings[i]->queue_index) {
355 tx_ring = vsi->tx_rings[i];
356 break;
357 }
358 }
359 }
360 }
361
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000362 if (time_after(jiffies, (pf->tx_timeout_last_recovery + HZ*20)))
Kiran Patilb03a8c12015-09-24 18:13:15 -0400363 pf->tx_timeout_recovery_level = 1; /* reset after some time */
364 else if (time_before(jiffies,
365 (pf->tx_timeout_last_recovery + netdev->watchdog_timeo)))
366 return; /* don't do any new action before the next timeout */
367
368 if (tx_ring) {
369 head = i40e_get_head(tx_ring);
370 /* Read interrupt register */
371 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
372 val = rd32(&pf->hw,
373 I40E_PFINT_DYN_CTLN(tx_ring->q_vector->v_idx +
374 tx_ring->vsi->base_vector - 1));
375 else
376 val = rd32(&pf->hw, I40E_PFINT_DYN_CTL0);
377
378 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",
379 vsi->seid, hung_queue, tx_ring->next_to_clean,
380 head, tx_ring->next_to_use,
381 readl(tx_ring->tail), val);
382 }
383
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000384 pf->tx_timeout_last_recovery = jiffies;
Kiran Patilb03a8c12015-09-24 18:13:15 -0400385 netdev_info(netdev, "tx_timeout recovery level %d, hung_queue %d\n",
386 pf->tx_timeout_recovery_level, hung_queue);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000387
388 switch (pf->tx_timeout_recovery_level) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000389 case 1:
Jacob Keller0da36b92017-04-19 09:25:55 -0400390 set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000391 break;
392 case 2:
Jacob Keller0da36b92017-04-19 09:25:55 -0400393 set_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000394 break;
395 case 3:
Jacob Keller0da36b92017-04-19 09:25:55 -0400396 set_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000397 break;
398 default:
399 netdev_err(netdev, "tx_timeout recovery unsuccessful\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000400 break;
401 }
Kiran Patilb03a8c12015-09-24 18:13:15 -0400402
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000403 i40e_service_event_schedule(pf);
404 pf->tx_timeout_recovery_level++;
405}
406
407/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000408 * i40e_get_vsi_stats_struct - Get System Network Statistics
409 * @vsi: the VSI we care about
410 *
411 * Returns the address of the device statistics structure.
412 * The statistics are actually updated from the service task.
413 **/
414struct rtnl_link_stats64 *i40e_get_vsi_stats_struct(struct i40e_vsi *vsi)
415{
416 return &vsi->net_stats;
417}
418
419/**
Björn Töpel74608d12017-05-24 07:55:35 +0200420 * i40e_get_netdev_stats_struct_tx - populate stats from a Tx ring
421 * @ring: Tx ring to get statistics from
422 * @stats: statistics entry to be updated
423 **/
424static void i40e_get_netdev_stats_struct_tx(struct i40e_ring *ring,
425 struct rtnl_link_stats64 *stats)
426{
427 u64 bytes, packets;
428 unsigned int start;
429
430 do {
431 start = u64_stats_fetch_begin_irq(&ring->syncp);
432 packets = ring->stats.packets;
433 bytes = ring->stats.bytes;
434 } while (u64_stats_fetch_retry_irq(&ring->syncp, start));
435
436 stats->tx_packets += packets;
437 stats->tx_bytes += bytes;
438}
439
440/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000441 * i40e_get_netdev_stats_struct - Get statistics for netdev interface
442 * @netdev: network interface device structure
443 *
444 * Returns the address of the device statistics structure.
445 * The statistics are actually updated from the service task.
446 **/
Alexander Duyck9eed69a2017-02-21 15:55:47 -0800447static void i40e_get_netdev_stats_struct(struct net_device *netdev,
stephen hemmingerbc1f4472017-01-06 19:12:52 -0800448 struct rtnl_link_stats64 *stats)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000449{
450 struct i40e_netdev_priv *np = netdev_priv(netdev);
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +0000451 struct i40e_ring *tx_ring, *rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000452 struct i40e_vsi *vsi = np->vsi;
Alexander Duyck980e9b12013-09-28 06:01:03 +0000453 struct rtnl_link_stats64 *vsi_stats = i40e_get_vsi_stats_struct(vsi);
454 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000455
Jacob Keller0da36b92017-04-19 09:25:55 -0400456 if (test_bit(__I40E_VSI_DOWN, vsi->state))
stephen hemmingerbc1f4472017-01-06 19:12:52 -0800457 return;
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +0000458
Jesse Brandeburg3c325ce2013-12-14 03:26:45 -0800459 if (!vsi->tx_rings)
stephen hemmingerbc1f4472017-01-06 19:12:52 -0800460 return;
Jesse Brandeburg3c325ce2013-12-14 03:26:45 -0800461
Alexander Duyck980e9b12013-09-28 06:01:03 +0000462 rcu_read_lock();
463 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck980e9b12013-09-28 06:01:03 +0000464 u64 bytes, packets;
465 unsigned int start;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000466
Mark Rutland6aa7de02017-10-23 14:07:29 -0700467 tx_ring = READ_ONCE(vsi->tx_rings[i]);
Alexander Duyck980e9b12013-09-28 06:01:03 +0000468 if (!tx_ring)
469 continue;
Björn Töpel74608d12017-05-24 07:55:35 +0200470 i40e_get_netdev_stats_struct_tx(tx_ring, stats);
Alexander Duyck980e9b12013-09-28 06:01:03 +0000471
Alexander Duyck980e9b12013-09-28 06:01:03 +0000472 rx_ring = &tx_ring[1];
473
474 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700475 start = u64_stats_fetch_begin_irq(&rx_ring->syncp);
Alexander Duyck980e9b12013-09-28 06:01:03 +0000476 packets = rx_ring->stats.packets;
477 bytes = rx_ring->stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700478 } while (u64_stats_fetch_retry_irq(&rx_ring->syncp, start));
Alexander Duyck980e9b12013-09-28 06:01:03 +0000479
480 stats->rx_packets += packets;
481 stats->rx_bytes += bytes;
Björn Töpel74608d12017-05-24 07:55:35 +0200482
483 if (i40e_enabled_xdp_vsi(vsi))
484 i40e_get_netdev_stats_struct_tx(&rx_ring[1], stats);
Alexander Duyck980e9b12013-09-28 06:01:03 +0000485 }
486 rcu_read_unlock();
487
Akeem G Abodunrina5282f42014-05-10 04:49:03 +0000488 /* following stats updated by i40e_watchdog_subtask() */
Alexander Duyck980e9b12013-09-28 06:01:03 +0000489 stats->multicast = vsi_stats->multicast;
490 stats->tx_errors = vsi_stats->tx_errors;
491 stats->tx_dropped = vsi_stats->tx_dropped;
492 stats->rx_errors = vsi_stats->rx_errors;
Jesse Brandeburgd8201e22015-07-23 16:54:35 -0400493 stats->rx_dropped = vsi_stats->rx_dropped;
Alexander Duyck980e9b12013-09-28 06:01:03 +0000494 stats->rx_crc_errors = vsi_stats->rx_crc_errors;
495 stats->rx_length_errors = vsi_stats->rx_length_errors;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000496}
497
498/**
499 * i40e_vsi_reset_stats - Resets all stats of the given vsi
500 * @vsi: the VSI to have its stats reset
501 **/
502void i40e_vsi_reset_stats(struct i40e_vsi *vsi)
503{
504 struct rtnl_link_stats64 *ns;
505 int i;
506
507 if (!vsi)
508 return;
509
510 ns = i40e_get_vsi_stats_struct(vsi);
511 memset(ns, 0, sizeof(*ns));
512 memset(&vsi->net_stats_offsets, 0, sizeof(vsi->net_stats_offsets));
513 memset(&vsi->eth_stats, 0, sizeof(vsi->eth_stats));
514 memset(&vsi->eth_stats_offsets, 0, sizeof(vsi->eth_stats_offsets));
Greg Rose8e9dca52013-12-18 13:45:53 +0000515 if (vsi->rx_rings && vsi->rx_rings[0]) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000516 for (i = 0; i < vsi->num_queue_pairs; i++) {
Jesse Brandeburg6995b362015-08-28 17:55:54 -0400517 memset(&vsi->rx_rings[i]->stats, 0,
Alexander Duyck9f65e152013-09-28 06:00:58 +0000518 sizeof(vsi->rx_rings[i]->stats));
Jesse Brandeburg6995b362015-08-28 17:55:54 -0400519 memset(&vsi->rx_rings[i]->rx_stats, 0,
Alexander Duyck9f65e152013-09-28 06:00:58 +0000520 sizeof(vsi->rx_rings[i]->rx_stats));
Jesse Brandeburg6995b362015-08-28 17:55:54 -0400521 memset(&vsi->tx_rings[i]->stats, 0,
Alexander Duyck9f65e152013-09-28 06:00:58 +0000522 sizeof(vsi->tx_rings[i]->stats));
523 memset(&vsi->tx_rings[i]->tx_stats, 0,
524 sizeof(vsi->tx_rings[i]->tx_stats));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000525 }
Greg Rose8e9dca52013-12-18 13:45:53 +0000526 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000527 vsi->stat_offsets_loaded = false;
528}
529
530/**
Jeff Kirsherb40c82e62015-02-27 09:18:34 +0000531 * i40e_pf_reset_stats - Reset all of the stats for the given PF
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000532 * @pf: the PF to be reset
533 **/
534void i40e_pf_reset_stats(struct i40e_pf *pf)
535{
Shannon Nelsone91fdf72014-06-03 23:50:18 +0000536 int i;
537
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000538 memset(&pf->stats, 0, sizeof(pf->stats));
539 memset(&pf->stats_offsets, 0, sizeof(pf->stats_offsets));
540 pf->stat_offsets_loaded = false;
Shannon Nelsone91fdf72014-06-03 23:50:18 +0000541
542 for (i = 0; i < I40E_MAX_VEB; i++) {
543 if (pf->veb[i]) {
544 memset(&pf->veb[i]->stats, 0,
545 sizeof(pf->veb[i]->stats));
546 memset(&pf->veb[i]->stats_offsets, 0,
547 sizeof(pf->veb[i]->stats_offsets));
548 pf->veb[i]->stat_offsets_loaded = false;
549 }
550 }
Catherine Sullivan42bce042016-07-27 12:02:32 -0700551 pf->hw_csum_rx_error = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000552}
553
554/**
555 * i40e_stat_update48 - read and update a 48 bit stat from the chip
556 * @hw: ptr to the hardware info
557 * @hireg: the high 32 bit reg to read
558 * @loreg: the low 32 bit reg to read
559 * @offset_loaded: has the initial offset been loaded yet
560 * @offset: ptr to current offset value
561 * @stat: ptr to the stat
562 *
563 * Since the device stats are not reset at PFReset, they likely will not
564 * be zeroed when the driver starts. We'll save the first values read
565 * and use them as offsets to be subtracted from the raw values in order
566 * to report stats that count from zero. In the process, we also manage
567 * the potential roll-over.
568 **/
569static void i40e_stat_update48(struct i40e_hw *hw, u32 hireg, u32 loreg,
570 bool offset_loaded, u64 *offset, u64 *stat)
571{
572 u64 new_data;
573
Shannon Nelsonab600852014-01-17 15:36:39 -0800574 if (hw->device_id == I40E_DEV_ID_QEMU) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000575 new_data = rd32(hw, loreg);
576 new_data |= ((u64)(rd32(hw, hireg) & 0xFFFF)) << 32;
577 } else {
578 new_data = rd64(hw, loreg);
579 }
580 if (!offset_loaded)
581 *offset = new_data;
582 if (likely(new_data >= *offset))
583 *stat = new_data - *offset;
584 else
Jesse Brandeburg41a1d042015-06-04 16:24:02 -0400585 *stat = (new_data + BIT_ULL(48)) - *offset;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000586 *stat &= 0xFFFFFFFFFFFFULL;
587}
588
589/**
590 * i40e_stat_update32 - read and update a 32 bit stat from the chip
591 * @hw: ptr to the hardware info
592 * @reg: the hw reg to read
593 * @offset_loaded: has the initial offset been loaded yet
594 * @offset: ptr to current offset value
595 * @stat: ptr to the stat
596 **/
597static void i40e_stat_update32(struct i40e_hw *hw, u32 reg,
598 bool offset_loaded, u64 *offset, u64 *stat)
599{
600 u32 new_data;
601
602 new_data = rd32(hw, reg);
603 if (!offset_loaded)
604 *offset = new_data;
605 if (likely(new_data >= *offset))
606 *stat = (u32)(new_data - *offset);
607 else
Jesse Brandeburg41a1d042015-06-04 16:24:02 -0400608 *stat = (u32)((new_data + BIT_ULL(32)) - *offset);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000609}
610
611/**
Mariusz Stachura0dc86922017-07-14 09:27:00 -0400612 * i40e_stat_update_and_clear32 - read and clear hw reg, update a 32 bit stat
613 * @hw: ptr to the hardware info
614 * @reg: the hw reg to read and clear
615 * @stat: ptr to the stat
616 **/
617static void i40e_stat_update_and_clear32(struct i40e_hw *hw, u32 reg, u64 *stat)
618{
619 u32 new_data = rd32(hw, reg);
620
621 wr32(hw, reg, 1); /* must write a nonzero value to clear register */
622 *stat += new_data;
623}
624
625/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000626 * i40e_update_eth_stats - Update VSI-specific ethernet statistics counters.
627 * @vsi: the VSI to be updated
628 **/
629void i40e_update_eth_stats(struct i40e_vsi *vsi)
630{
631 int stat_idx = le16_to_cpu(vsi->info.stat_counter_idx);
632 struct i40e_pf *pf = vsi->back;
633 struct i40e_hw *hw = &pf->hw;
634 struct i40e_eth_stats *oes;
635 struct i40e_eth_stats *es; /* device's eth stats */
636
637 es = &vsi->eth_stats;
638 oes = &vsi->eth_stats_offsets;
639
640 /* Gather up the stats that the hw collects */
641 i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
642 vsi->stat_offsets_loaded,
643 &oes->tx_errors, &es->tx_errors);
644 i40e_stat_update32(hw, I40E_GLV_RDPC(stat_idx),
645 vsi->stat_offsets_loaded,
646 &oes->rx_discards, &es->rx_discards);
Shannon Nelson41a9e552014-04-23 04:50:20 +0000647 i40e_stat_update32(hw, I40E_GLV_RUPP(stat_idx),
648 vsi->stat_offsets_loaded,
649 &oes->rx_unknown_protocol, &es->rx_unknown_protocol);
650 i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
651 vsi->stat_offsets_loaded,
652 &oes->tx_errors, &es->tx_errors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000653
654 i40e_stat_update48(hw, I40E_GLV_GORCH(stat_idx),
655 I40E_GLV_GORCL(stat_idx),
656 vsi->stat_offsets_loaded,
657 &oes->rx_bytes, &es->rx_bytes);
658 i40e_stat_update48(hw, I40E_GLV_UPRCH(stat_idx),
659 I40E_GLV_UPRCL(stat_idx),
660 vsi->stat_offsets_loaded,
661 &oes->rx_unicast, &es->rx_unicast);
662 i40e_stat_update48(hw, I40E_GLV_MPRCH(stat_idx),
663 I40E_GLV_MPRCL(stat_idx),
664 vsi->stat_offsets_loaded,
665 &oes->rx_multicast, &es->rx_multicast);
666 i40e_stat_update48(hw, I40E_GLV_BPRCH(stat_idx),
667 I40E_GLV_BPRCL(stat_idx),
668 vsi->stat_offsets_loaded,
669 &oes->rx_broadcast, &es->rx_broadcast);
670
671 i40e_stat_update48(hw, I40E_GLV_GOTCH(stat_idx),
672 I40E_GLV_GOTCL(stat_idx),
673 vsi->stat_offsets_loaded,
674 &oes->tx_bytes, &es->tx_bytes);
675 i40e_stat_update48(hw, I40E_GLV_UPTCH(stat_idx),
676 I40E_GLV_UPTCL(stat_idx),
677 vsi->stat_offsets_loaded,
678 &oes->tx_unicast, &es->tx_unicast);
679 i40e_stat_update48(hw, I40E_GLV_MPTCH(stat_idx),
680 I40E_GLV_MPTCL(stat_idx),
681 vsi->stat_offsets_loaded,
682 &oes->tx_multicast, &es->tx_multicast);
683 i40e_stat_update48(hw, I40E_GLV_BPTCH(stat_idx),
684 I40E_GLV_BPTCL(stat_idx),
685 vsi->stat_offsets_loaded,
686 &oes->tx_broadcast, &es->tx_broadcast);
687 vsi->stat_offsets_loaded = true;
688}
689
690/**
691 * i40e_update_veb_stats - Update Switch component statistics
692 * @veb: the VEB being updated
693 **/
694static void i40e_update_veb_stats(struct i40e_veb *veb)
695{
696 struct i40e_pf *pf = veb->pf;
697 struct i40e_hw *hw = &pf->hw;
698 struct i40e_eth_stats *oes;
699 struct i40e_eth_stats *es; /* device's eth stats */
Neerav Parikhfe860af2015-07-10 19:36:02 -0400700 struct i40e_veb_tc_stats *veb_oes;
701 struct i40e_veb_tc_stats *veb_es;
702 int i, idx = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000703
704 idx = veb->stats_idx;
705 es = &veb->stats;
706 oes = &veb->stats_offsets;
Neerav Parikhfe860af2015-07-10 19:36:02 -0400707 veb_es = &veb->tc_stats;
708 veb_oes = &veb->tc_stats_offsets;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000709
710 /* Gather up the stats that the hw collects */
711 i40e_stat_update32(hw, I40E_GLSW_TDPC(idx),
712 veb->stat_offsets_loaded,
713 &oes->tx_discards, &es->tx_discards);
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +0000714 if (hw->revision_id > 0)
715 i40e_stat_update32(hw, I40E_GLSW_RUPP(idx),
716 veb->stat_offsets_loaded,
717 &oes->rx_unknown_protocol,
718 &es->rx_unknown_protocol);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000719 i40e_stat_update48(hw, I40E_GLSW_GORCH(idx), I40E_GLSW_GORCL(idx),
720 veb->stat_offsets_loaded,
721 &oes->rx_bytes, &es->rx_bytes);
722 i40e_stat_update48(hw, I40E_GLSW_UPRCH(idx), I40E_GLSW_UPRCL(idx),
723 veb->stat_offsets_loaded,
724 &oes->rx_unicast, &es->rx_unicast);
725 i40e_stat_update48(hw, I40E_GLSW_MPRCH(idx), I40E_GLSW_MPRCL(idx),
726 veb->stat_offsets_loaded,
727 &oes->rx_multicast, &es->rx_multicast);
728 i40e_stat_update48(hw, I40E_GLSW_BPRCH(idx), I40E_GLSW_BPRCL(idx),
729 veb->stat_offsets_loaded,
730 &oes->rx_broadcast, &es->rx_broadcast);
731
732 i40e_stat_update48(hw, I40E_GLSW_GOTCH(idx), I40E_GLSW_GOTCL(idx),
733 veb->stat_offsets_loaded,
734 &oes->tx_bytes, &es->tx_bytes);
735 i40e_stat_update48(hw, I40E_GLSW_UPTCH(idx), I40E_GLSW_UPTCL(idx),
736 veb->stat_offsets_loaded,
737 &oes->tx_unicast, &es->tx_unicast);
738 i40e_stat_update48(hw, I40E_GLSW_MPTCH(idx), I40E_GLSW_MPTCL(idx),
739 veb->stat_offsets_loaded,
740 &oes->tx_multicast, &es->tx_multicast);
741 i40e_stat_update48(hw, I40E_GLSW_BPTCH(idx), I40E_GLSW_BPTCL(idx),
742 veb->stat_offsets_loaded,
743 &oes->tx_broadcast, &es->tx_broadcast);
Neerav Parikhfe860af2015-07-10 19:36:02 -0400744 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
745 i40e_stat_update48(hw, I40E_GLVEBTC_RPCH(i, idx),
746 I40E_GLVEBTC_RPCL(i, idx),
747 veb->stat_offsets_loaded,
748 &veb_oes->tc_rx_packets[i],
749 &veb_es->tc_rx_packets[i]);
750 i40e_stat_update48(hw, I40E_GLVEBTC_RBCH(i, idx),
751 I40E_GLVEBTC_RBCL(i, idx),
752 veb->stat_offsets_loaded,
753 &veb_oes->tc_rx_bytes[i],
754 &veb_es->tc_rx_bytes[i]);
755 i40e_stat_update48(hw, I40E_GLVEBTC_TPCH(i, idx),
756 I40E_GLVEBTC_TPCL(i, idx),
757 veb->stat_offsets_loaded,
758 &veb_oes->tc_tx_packets[i],
759 &veb_es->tc_tx_packets[i]);
760 i40e_stat_update48(hw, I40E_GLVEBTC_TBCH(i, idx),
761 I40E_GLVEBTC_TBCL(i, idx),
762 veb->stat_offsets_loaded,
763 &veb_oes->tc_tx_bytes[i],
764 &veb_es->tc_tx_bytes[i]);
765 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000766 veb->stat_offsets_loaded = true;
767}
768
769/**
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000770 * i40e_update_vsi_stats - Update the vsi statistics counters.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000771 * @vsi: the VSI to be updated
772 *
773 * There are a few instances where we store the same stat in a
774 * couple of different structs. This is partly because we have
775 * the netdev stats that need to be filled out, which is slightly
776 * different from the "eth_stats" defined by the chip and used in
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000777 * VF communications. We sort it out here.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000778 **/
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000779static void i40e_update_vsi_stats(struct i40e_vsi *vsi)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000780{
781 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000782 struct rtnl_link_stats64 *ons;
783 struct rtnl_link_stats64 *ns; /* netdev stats */
784 struct i40e_eth_stats *oes;
785 struct i40e_eth_stats *es; /* device's eth stats */
786 u32 tx_restart, tx_busy;
Akeem G Abodunrinbf00b372014-10-17 03:14:39 +0000787 struct i40e_ring *p;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000788 u32 rx_page, rx_buf;
Akeem G Abodunrinbf00b372014-10-17 03:14:39 +0000789 u64 bytes, packets;
790 unsigned int start;
Anjali Singhai Jain2fc3d712015-08-27 11:42:29 -0400791 u64 tx_linearize;
Anjali Singhai Jain164c9f52015-10-21 19:47:08 -0400792 u64 tx_force_wb;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000793 u64 rx_p, rx_b;
794 u64 tx_p, tx_b;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000795 u16 q;
796
Jacob Keller0da36b92017-04-19 09:25:55 -0400797 if (test_bit(__I40E_VSI_DOWN, vsi->state) ||
798 test_bit(__I40E_CONFIG_BUSY, pf->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000799 return;
800
801 ns = i40e_get_vsi_stats_struct(vsi);
802 ons = &vsi->net_stats_offsets;
803 es = &vsi->eth_stats;
804 oes = &vsi->eth_stats_offsets;
805
806 /* Gather up the netdev and vsi stats that the driver collects
807 * on the fly during packet processing
808 */
809 rx_b = rx_p = 0;
810 tx_b = tx_p = 0;
Anjali Singhai Jain164c9f52015-10-21 19:47:08 -0400811 tx_restart = tx_busy = tx_linearize = tx_force_wb = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000812 rx_page = 0;
813 rx_buf = 0;
Alexander Duyck980e9b12013-09-28 06:01:03 +0000814 rcu_read_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000815 for (q = 0; q < vsi->num_queue_pairs; q++) {
Alexander Duyck980e9b12013-09-28 06:01:03 +0000816 /* locate Tx ring */
Mark Rutland6aa7de02017-10-23 14:07:29 -0700817 p = READ_ONCE(vsi->tx_rings[q]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000818
Alexander Duyck980e9b12013-09-28 06:01:03 +0000819 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700820 start = u64_stats_fetch_begin_irq(&p->syncp);
Alexander Duyck980e9b12013-09-28 06:01:03 +0000821 packets = p->stats.packets;
822 bytes = p->stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700823 } while (u64_stats_fetch_retry_irq(&p->syncp, start));
Alexander Duyck980e9b12013-09-28 06:01:03 +0000824 tx_b += bytes;
825 tx_p += packets;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000826 tx_restart += p->tx_stats.restart_queue;
827 tx_busy += p->tx_stats.tx_busy;
Anjali Singhai Jain2fc3d712015-08-27 11:42:29 -0400828 tx_linearize += p->tx_stats.tx_linearize;
Anjali Singhai Jain164c9f52015-10-21 19:47:08 -0400829 tx_force_wb += p->tx_stats.tx_force_wb;
Alexander Duyck980e9b12013-09-28 06:01:03 +0000830
831 /* Rx queue is part of the same block as Tx queue */
832 p = &p[1];
833 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700834 start = u64_stats_fetch_begin_irq(&p->syncp);
Alexander Duyck980e9b12013-09-28 06:01:03 +0000835 packets = p->stats.packets;
836 bytes = p->stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700837 } while (u64_stats_fetch_retry_irq(&p->syncp, start));
Alexander Duyck980e9b12013-09-28 06:01:03 +0000838 rx_b += bytes;
839 rx_p += packets;
Mitch Williams420136c2013-12-18 13:45:59 +0000840 rx_buf += p->rx_stats.alloc_buff_failed;
841 rx_page += p->rx_stats.alloc_page_failed;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000842 }
Alexander Duyck980e9b12013-09-28 06:01:03 +0000843 rcu_read_unlock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000844 vsi->tx_restart = tx_restart;
845 vsi->tx_busy = tx_busy;
Anjali Singhai Jain2fc3d712015-08-27 11:42:29 -0400846 vsi->tx_linearize = tx_linearize;
Anjali Singhai Jain164c9f52015-10-21 19:47:08 -0400847 vsi->tx_force_wb = tx_force_wb;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000848 vsi->rx_page_failed = rx_page;
849 vsi->rx_buf_failed = rx_buf;
850
851 ns->rx_packets = rx_p;
852 ns->rx_bytes = rx_b;
853 ns->tx_packets = tx_p;
854 ns->tx_bytes = tx_b;
855
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000856 /* update netdev stats from eth stats */
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000857 i40e_update_eth_stats(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000858 ons->tx_errors = oes->tx_errors;
859 ns->tx_errors = es->tx_errors;
860 ons->multicast = oes->rx_multicast;
861 ns->multicast = es->rx_multicast;
Shannon Nelson41a9e552014-04-23 04:50:20 +0000862 ons->rx_dropped = oes->rx_discards;
863 ns->rx_dropped = es->rx_discards;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000864 ons->tx_dropped = oes->tx_discards;
865 ns->tx_dropped = es->tx_discards;
866
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000867 /* pull in a couple PF stats if this is the main vsi */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000868 if (vsi == pf->vsi[pf->lan_vsi]) {
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000869 ns->rx_crc_errors = pf->stats.crc_errors;
870 ns->rx_errors = pf->stats.crc_errors + pf->stats.illegal_bytes;
871 ns->rx_length_errors = pf->stats.rx_length_errors;
872 }
873}
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000874
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000875/**
Jeff Kirsherb40c82e62015-02-27 09:18:34 +0000876 * i40e_update_pf_stats - Update the PF statistics counters.
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000877 * @pf: the PF to be updated
878 **/
879static void i40e_update_pf_stats(struct i40e_pf *pf)
880{
881 struct i40e_hw_port_stats *osd = &pf->stats_offsets;
882 struct i40e_hw_port_stats *nsd = &pf->stats;
883 struct i40e_hw *hw = &pf->hw;
884 u32 val;
885 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000886
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000887 i40e_stat_update48(hw, I40E_GLPRT_GORCH(hw->port),
888 I40E_GLPRT_GORCL(hw->port),
889 pf->stat_offsets_loaded,
890 &osd->eth.rx_bytes, &nsd->eth.rx_bytes);
891 i40e_stat_update48(hw, I40E_GLPRT_GOTCH(hw->port),
892 I40E_GLPRT_GOTCL(hw->port),
893 pf->stat_offsets_loaded,
894 &osd->eth.tx_bytes, &nsd->eth.tx_bytes);
895 i40e_stat_update32(hw, I40E_GLPRT_RDPC(hw->port),
896 pf->stat_offsets_loaded,
897 &osd->eth.rx_discards,
898 &nsd->eth.rx_discards);
Shannon Nelson532d2832014-04-23 04:50:09 +0000899 i40e_stat_update48(hw, I40E_GLPRT_UPRCH(hw->port),
900 I40E_GLPRT_UPRCL(hw->port),
901 pf->stat_offsets_loaded,
902 &osd->eth.rx_unicast,
903 &nsd->eth.rx_unicast);
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000904 i40e_stat_update48(hw, I40E_GLPRT_MPRCH(hw->port),
905 I40E_GLPRT_MPRCL(hw->port),
906 pf->stat_offsets_loaded,
907 &osd->eth.rx_multicast,
908 &nsd->eth.rx_multicast);
Shannon Nelson532d2832014-04-23 04:50:09 +0000909 i40e_stat_update48(hw, I40E_GLPRT_BPRCH(hw->port),
910 I40E_GLPRT_BPRCL(hw->port),
911 pf->stat_offsets_loaded,
912 &osd->eth.rx_broadcast,
913 &nsd->eth.rx_broadcast);
914 i40e_stat_update48(hw, I40E_GLPRT_UPTCH(hw->port),
915 I40E_GLPRT_UPTCL(hw->port),
916 pf->stat_offsets_loaded,
917 &osd->eth.tx_unicast,
918 &nsd->eth.tx_unicast);
919 i40e_stat_update48(hw, I40E_GLPRT_MPTCH(hw->port),
920 I40E_GLPRT_MPTCL(hw->port),
921 pf->stat_offsets_loaded,
922 &osd->eth.tx_multicast,
923 &nsd->eth.tx_multicast);
924 i40e_stat_update48(hw, I40E_GLPRT_BPTCH(hw->port),
925 I40E_GLPRT_BPTCL(hw->port),
926 pf->stat_offsets_loaded,
927 &osd->eth.tx_broadcast,
928 &nsd->eth.tx_broadcast);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000929
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000930 i40e_stat_update32(hw, I40E_GLPRT_TDOLD(hw->port),
931 pf->stat_offsets_loaded,
932 &osd->tx_dropped_link_down,
933 &nsd->tx_dropped_link_down);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000934
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000935 i40e_stat_update32(hw, I40E_GLPRT_CRCERRS(hw->port),
936 pf->stat_offsets_loaded,
937 &osd->crc_errors, &nsd->crc_errors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000938
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000939 i40e_stat_update32(hw, I40E_GLPRT_ILLERRC(hw->port),
940 pf->stat_offsets_loaded,
941 &osd->illegal_bytes, &nsd->illegal_bytes);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000942
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000943 i40e_stat_update32(hw, I40E_GLPRT_MLFC(hw->port),
944 pf->stat_offsets_loaded,
945 &osd->mac_local_faults,
946 &nsd->mac_local_faults);
947 i40e_stat_update32(hw, I40E_GLPRT_MRFC(hw->port),
948 pf->stat_offsets_loaded,
949 &osd->mac_remote_faults,
950 &nsd->mac_remote_faults);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000951
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000952 i40e_stat_update32(hw, I40E_GLPRT_RLEC(hw->port),
953 pf->stat_offsets_loaded,
954 &osd->rx_length_errors,
955 &nsd->rx_length_errors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000956
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000957 i40e_stat_update32(hw, I40E_GLPRT_LXONRXC(hw->port),
958 pf->stat_offsets_loaded,
959 &osd->link_xon_rx, &nsd->link_xon_rx);
960 i40e_stat_update32(hw, I40E_GLPRT_LXONTXC(hw->port),
961 pf->stat_offsets_loaded,
962 &osd->link_xon_tx, &nsd->link_xon_tx);
Neerav Parikh95db2392015-11-06 15:26:09 -0800963 i40e_stat_update32(hw, I40E_GLPRT_LXOFFRXC(hw->port),
964 pf->stat_offsets_loaded,
965 &osd->link_xoff_rx, &nsd->link_xoff_rx);
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000966 i40e_stat_update32(hw, I40E_GLPRT_LXOFFTXC(hw->port),
967 pf->stat_offsets_loaded,
968 &osd->link_xoff_tx, &nsd->link_xoff_tx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000969
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000970 for (i = 0; i < 8; i++) {
Neerav Parikh95db2392015-11-06 15:26:09 -0800971 i40e_stat_update32(hw, I40E_GLPRT_PXOFFRXC(hw->port, i),
972 pf->stat_offsets_loaded,
973 &osd->priority_xoff_rx[i],
974 &nsd->priority_xoff_rx[i]);
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000975 i40e_stat_update32(hw, I40E_GLPRT_PXONRXC(hw->port, i),
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000976 pf->stat_offsets_loaded,
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000977 &osd->priority_xon_rx[i],
978 &nsd->priority_xon_rx[i]);
979 i40e_stat_update32(hw, I40E_GLPRT_PXONTXC(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_tx[i],
982 &nsd->priority_xon_tx[i]);
983 i40e_stat_update32(hw, I40E_GLPRT_PXOFFTXC(hw->port, i),
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000984 pf->stat_offsets_loaded,
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000985 &osd->priority_xoff_tx[i],
986 &nsd->priority_xoff_tx[i]);
987 i40e_stat_update32(hw,
988 I40E_GLPRT_RXON2OFFCNT(hw->port, i),
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000989 pf->stat_offsets_loaded,
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000990 &osd->priority_xon_2_xoff[i],
991 &nsd->priority_xon_2_xoff[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000992 }
993
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000994 i40e_stat_update48(hw, I40E_GLPRT_PRC64H(hw->port),
995 I40E_GLPRT_PRC64L(hw->port),
996 pf->stat_offsets_loaded,
997 &osd->rx_size_64, &nsd->rx_size_64);
998 i40e_stat_update48(hw, I40E_GLPRT_PRC127H(hw->port),
999 I40E_GLPRT_PRC127L(hw->port),
1000 pf->stat_offsets_loaded,
1001 &osd->rx_size_127, &nsd->rx_size_127);
1002 i40e_stat_update48(hw, I40E_GLPRT_PRC255H(hw->port),
1003 I40E_GLPRT_PRC255L(hw->port),
1004 pf->stat_offsets_loaded,
1005 &osd->rx_size_255, &nsd->rx_size_255);
1006 i40e_stat_update48(hw, I40E_GLPRT_PRC511H(hw->port),
1007 I40E_GLPRT_PRC511L(hw->port),
1008 pf->stat_offsets_loaded,
1009 &osd->rx_size_511, &nsd->rx_size_511);
1010 i40e_stat_update48(hw, I40E_GLPRT_PRC1023H(hw->port),
1011 I40E_GLPRT_PRC1023L(hw->port),
1012 pf->stat_offsets_loaded,
1013 &osd->rx_size_1023, &nsd->rx_size_1023);
1014 i40e_stat_update48(hw, I40E_GLPRT_PRC1522H(hw->port),
1015 I40E_GLPRT_PRC1522L(hw->port),
1016 pf->stat_offsets_loaded,
1017 &osd->rx_size_1522, &nsd->rx_size_1522);
1018 i40e_stat_update48(hw, I40E_GLPRT_PRC9522H(hw->port),
1019 I40E_GLPRT_PRC9522L(hw->port),
1020 pf->stat_offsets_loaded,
1021 &osd->rx_size_big, &nsd->rx_size_big);
1022
1023 i40e_stat_update48(hw, I40E_GLPRT_PTC64H(hw->port),
1024 I40E_GLPRT_PTC64L(hw->port),
1025 pf->stat_offsets_loaded,
1026 &osd->tx_size_64, &nsd->tx_size_64);
1027 i40e_stat_update48(hw, I40E_GLPRT_PTC127H(hw->port),
1028 I40E_GLPRT_PTC127L(hw->port),
1029 pf->stat_offsets_loaded,
1030 &osd->tx_size_127, &nsd->tx_size_127);
1031 i40e_stat_update48(hw, I40E_GLPRT_PTC255H(hw->port),
1032 I40E_GLPRT_PTC255L(hw->port),
1033 pf->stat_offsets_loaded,
1034 &osd->tx_size_255, &nsd->tx_size_255);
1035 i40e_stat_update48(hw, I40E_GLPRT_PTC511H(hw->port),
1036 I40E_GLPRT_PTC511L(hw->port),
1037 pf->stat_offsets_loaded,
1038 &osd->tx_size_511, &nsd->tx_size_511);
1039 i40e_stat_update48(hw, I40E_GLPRT_PTC1023H(hw->port),
1040 I40E_GLPRT_PTC1023L(hw->port),
1041 pf->stat_offsets_loaded,
1042 &osd->tx_size_1023, &nsd->tx_size_1023);
1043 i40e_stat_update48(hw, I40E_GLPRT_PTC1522H(hw->port),
1044 I40E_GLPRT_PTC1522L(hw->port),
1045 pf->stat_offsets_loaded,
1046 &osd->tx_size_1522, &nsd->tx_size_1522);
1047 i40e_stat_update48(hw, I40E_GLPRT_PTC9522H(hw->port),
1048 I40E_GLPRT_PTC9522L(hw->port),
1049 pf->stat_offsets_loaded,
1050 &osd->tx_size_big, &nsd->tx_size_big);
1051
1052 i40e_stat_update32(hw, I40E_GLPRT_RUC(hw->port),
1053 pf->stat_offsets_loaded,
1054 &osd->rx_undersize, &nsd->rx_undersize);
1055 i40e_stat_update32(hw, I40E_GLPRT_RFC(hw->port),
1056 pf->stat_offsets_loaded,
1057 &osd->rx_fragments, &nsd->rx_fragments);
1058 i40e_stat_update32(hw, I40E_GLPRT_ROC(hw->port),
1059 pf->stat_offsets_loaded,
1060 &osd->rx_oversize, &nsd->rx_oversize);
1061 i40e_stat_update32(hw, I40E_GLPRT_RJC(hw->port),
1062 pf->stat_offsets_loaded,
1063 &osd->rx_jabber, &nsd->rx_jabber);
1064
Anjali Singhai Jain433c47d2014-05-22 06:32:17 +00001065 /* FDIR stats */
Mariusz Stachura0dc86922017-07-14 09:27:00 -04001066 i40e_stat_update_and_clear32(hw,
1067 I40E_GLQF_PCNT(I40E_FD_ATR_STAT_IDX(hw->pf_id)),
1068 &nsd->fd_atr_match);
1069 i40e_stat_update_and_clear32(hw,
1070 I40E_GLQF_PCNT(I40E_FD_SB_STAT_IDX(hw->pf_id)),
1071 &nsd->fd_sb_match);
1072 i40e_stat_update_and_clear32(hw,
1073 I40E_GLQF_PCNT(I40E_FD_ATR_TUNNEL_STAT_IDX(hw->pf_id)),
1074 &nsd->fd_atr_tunnel_match);
Anjali Singhai Jain433c47d2014-05-22 06:32:17 +00001075
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001076 val = rd32(hw, I40E_PRTPM_EEE_STAT);
1077 nsd->tx_lpi_status =
1078 (val & I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_MASK) >>
1079 I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_SHIFT;
1080 nsd->rx_lpi_status =
1081 (val & I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_MASK) >>
1082 I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_SHIFT;
1083 i40e_stat_update32(hw, I40E_PRTPM_TLPIC,
1084 pf->stat_offsets_loaded,
1085 &osd->tx_lpi_count, &nsd->tx_lpi_count);
1086 i40e_stat_update32(hw, I40E_PRTPM_RLPIC,
1087 pf->stat_offsets_loaded,
1088 &osd->rx_lpi_count, &nsd->rx_lpi_count);
1089
Anjali Singhai Jaind0389e52015-04-22 19:34:05 -04001090 if (pf->flags & I40E_FLAG_FD_SB_ENABLED &&
Jacob Keller47994c12017-04-19 09:25:57 -04001091 !(pf->flags & I40E_FLAG_FD_SB_AUTO_DISABLED))
Anjali Singhai Jaind0389e52015-04-22 19:34:05 -04001092 nsd->fd_sb_status = true;
1093 else
1094 nsd->fd_sb_status = false;
1095
1096 if (pf->flags & I40E_FLAG_FD_ATR_ENABLED &&
Jacob Keller47994c12017-04-19 09:25:57 -04001097 !(pf->flags & I40E_FLAG_FD_ATR_AUTO_DISABLED))
Anjali Singhai Jaind0389e52015-04-22 19:34:05 -04001098 nsd->fd_atr_status = true;
1099 else
1100 nsd->fd_atr_status = false;
1101
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001102 pf->stat_offsets_loaded = true;
1103}
1104
1105/**
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001106 * i40e_update_stats - Update the various statistics counters.
1107 * @vsi: the VSI to be updated
1108 *
1109 * Update the various stats for this VSI and its related entities.
1110 **/
1111void i40e_update_stats(struct i40e_vsi *vsi)
1112{
1113 struct i40e_pf *pf = vsi->back;
1114
1115 if (vsi == pf->vsi[pf->lan_vsi])
1116 i40e_update_pf_stats(pf);
1117
1118 i40e_update_vsi_stats(vsi);
1119}
1120
1121/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001122 * i40e_find_filter - Search VSI filter list for specific mac/vlan filter
1123 * @vsi: the VSI to be searched
1124 * @macaddr: the MAC address
1125 * @vlan: the vlan
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001126 *
1127 * Returns ptr to the filter object or NULL
1128 **/
1129static struct i40e_mac_filter *i40e_find_filter(struct i40e_vsi *vsi,
Jacob Keller6622f5c2016-10-05 09:30:32 -07001130 const u8 *macaddr, s16 vlan)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001131{
1132 struct i40e_mac_filter *f;
Jacob Keller278e7d02016-10-05 09:30:37 -07001133 u64 key;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001134
1135 if (!vsi || !macaddr)
1136 return NULL;
1137
Jacob Keller278e7d02016-10-05 09:30:37 -07001138 key = i40e_addr_to_hkey(macaddr);
1139 hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001140 if ((ether_addr_equal(macaddr, f->macaddr)) &&
Jacob Keller1bc87e82016-10-05 09:30:31 -07001141 (vlan == f->vlan))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001142 return f;
1143 }
1144 return NULL;
1145}
1146
1147/**
1148 * i40e_find_mac - Find a mac addr in the macvlan filters list
1149 * @vsi: the VSI to be searched
1150 * @macaddr: the MAC address we are searching for
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001151 *
1152 * Returns the first filter with the provided MAC address or NULL if
1153 * MAC address was not found
1154 **/
Jacob Keller6622f5c2016-10-05 09:30:32 -07001155struct i40e_mac_filter *i40e_find_mac(struct i40e_vsi *vsi, const u8 *macaddr)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001156{
1157 struct i40e_mac_filter *f;
Jacob Keller278e7d02016-10-05 09:30:37 -07001158 u64 key;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001159
1160 if (!vsi || !macaddr)
1161 return NULL;
1162
Jacob Keller278e7d02016-10-05 09:30:37 -07001163 key = i40e_addr_to_hkey(macaddr);
1164 hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
Jacob Keller1bc87e82016-10-05 09:30:31 -07001165 if ((ether_addr_equal(macaddr, f->macaddr)))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001166 return f;
1167 }
1168 return NULL;
1169}
1170
1171/**
1172 * i40e_is_vsi_in_vlan - Check if VSI is in vlan mode
1173 * @vsi: the VSI to be searched
1174 *
1175 * Returns true if VSI is in vlan mode or false otherwise
1176 **/
1177bool i40e_is_vsi_in_vlan(struct i40e_vsi *vsi)
1178{
Jacob Kellercbebb852016-10-05 09:30:40 -07001179 /* If we have a PVID, always operate in VLAN mode */
1180 if (vsi->info.pvid)
1181 return true;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001182
Jacob Kellercbebb852016-10-05 09:30:40 -07001183 /* We need to operate in VLAN mode whenever we have any filters with
1184 * a VLAN other than I40E_VLAN_ALL. We could check the table each
1185 * time, incurring search cost repeatedly. However, we can notice two
1186 * things:
1187 *
1188 * 1) the only place where we can gain a VLAN filter is in
1189 * i40e_add_filter.
1190 *
1191 * 2) the only place where filters are actually removed is in
Jacob Keller0b7c8b52016-10-25 16:08:52 -07001192 * i40e_sync_filters_subtask.
Jacob Kellercbebb852016-10-05 09:30:40 -07001193 *
1194 * Thus, we can simply use a boolean value, has_vlan_filters which we
1195 * will set to true when we add a VLAN filter in i40e_add_filter. Then
1196 * we have to perform the full search after deleting filters in
Jacob Keller0b7c8b52016-10-25 16:08:52 -07001197 * i40e_sync_filters_subtask, but we already have to search
Jacob Kellercbebb852016-10-05 09:30:40 -07001198 * filters here and can perform the check at the same time. This
1199 * results in avoiding embedding a loop for VLAN mode inside another
1200 * loop over all the filters, and should maintain correctness as noted
1201 * above.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001202 */
Jacob Kellercbebb852016-10-05 09:30:40 -07001203 return vsi->has_vlan_filter;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001204}
1205
1206/**
Jacob Keller489a3262016-11-11 12:39:31 -08001207 * i40e_correct_mac_vlan_filters - Correct non-VLAN filters if necessary
1208 * @vsi: the VSI to configure
1209 * @tmp_add_list: list of filters ready to be added
1210 * @tmp_del_list: list of filters ready to be deleted
1211 * @vlan_filters: the number of active VLAN filters
1212 *
1213 * Update VLAN=0 and VLAN=-1 (I40E_VLAN_ANY) filters properly so that they
1214 * behave as expected. If we have any active VLAN filters remaining or about
1215 * to be added then we need to update non-VLAN filters to be marked as VLAN=0
1216 * so that they only match against untagged traffic. If we no longer have any
1217 * active VLAN filters, we need to make all non-VLAN filters marked as VLAN=-1
1218 * so that they match against both tagged and untagged traffic. In this way,
1219 * we ensure that we correctly receive the desired traffic. This ensures that
1220 * when we have an active VLAN we will receive only untagged traffic and
1221 * traffic matching active VLANs. If we have no active VLANs then we will
1222 * operate in non-VLAN mode and receive all traffic, tagged or untagged.
1223 *
1224 * Finally, in a similar fashion, this function also corrects filters when
1225 * there is an active PVID assigned to this VSI.
1226 *
1227 * In case of memory allocation failure return -ENOMEM. Otherwise, return 0.
1228 *
1229 * This function is only expected to be called from within
1230 * i40e_sync_vsi_filters.
1231 *
1232 * NOTE: This function expects to be called while under the
1233 * mac_filter_hash_lock
1234 */
1235static int i40e_correct_mac_vlan_filters(struct i40e_vsi *vsi,
1236 struct hlist_head *tmp_add_list,
1237 struct hlist_head *tmp_del_list,
1238 int vlan_filters)
1239{
Jacob Keller5cb25902016-12-12 15:44:15 -08001240 s16 pvid = le16_to_cpu(vsi->info.pvid);
Jacob Keller489a3262016-11-11 12:39:31 -08001241 struct i40e_mac_filter *f, *add_head;
Jacob Keller671889e2016-12-02 12:33:00 -08001242 struct i40e_new_mac_filter *new;
Jacob Keller489a3262016-11-11 12:39:31 -08001243 struct hlist_node *h;
1244 int bkt, new_vlan;
1245
1246 /* To determine if a particular filter needs to be replaced we
1247 * have the three following conditions:
1248 *
1249 * a) if we have a PVID assigned, then all filters which are
1250 * not marked as VLAN=PVID must be replaced with filters that
1251 * are.
1252 * b) otherwise, if we have any active VLANS, all filters
1253 * which are marked as VLAN=-1 must be replaced with
1254 * filters marked as VLAN=0
1255 * c) finally, if we do not have any active VLANS, all filters
1256 * which are marked as VLAN=0 must be replaced with filters
1257 * marked as VLAN=-1
1258 */
1259
1260 /* Update the filters about to be added in place */
Jacob Keller671889e2016-12-02 12:33:00 -08001261 hlist_for_each_entry(new, tmp_add_list, hlist) {
Jacob Keller5cb25902016-12-12 15:44:15 -08001262 if (pvid && new->f->vlan != pvid)
1263 new->f->vlan = pvid;
Jacob Keller671889e2016-12-02 12:33:00 -08001264 else if (vlan_filters && new->f->vlan == I40E_VLAN_ANY)
1265 new->f->vlan = 0;
1266 else if (!vlan_filters && new->f->vlan == 0)
1267 new->f->vlan = I40E_VLAN_ANY;
Jacob Keller489a3262016-11-11 12:39:31 -08001268 }
1269
1270 /* Update the remaining active filters */
1271 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1272 /* Combine the checks for whether a filter needs to be changed
1273 * and then determine the new VLAN inside the if block, in
1274 * order to avoid duplicating code for adding the new filter
1275 * then deleting the old filter.
1276 */
Jacob Keller5cb25902016-12-12 15:44:15 -08001277 if ((pvid && f->vlan != pvid) ||
Jacob Keller489a3262016-11-11 12:39:31 -08001278 (vlan_filters && f->vlan == I40E_VLAN_ANY) ||
1279 (!vlan_filters && f->vlan == 0)) {
1280 /* Determine the new vlan we will be adding */
Jacob Keller5cb25902016-12-12 15:44:15 -08001281 if (pvid)
1282 new_vlan = pvid;
Jacob Keller489a3262016-11-11 12:39:31 -08001283 else if (vlan_filters)
1284 new_vlan = 0;
1285 else
1286 new_vlan = I40E_VLAN_ANY;
1287
1288 /* Create the new filter */
1289 add_head = i40e_add_filter(vsi, f->macaddr, new_vlan);
1290 if (!add_head)
1291 return -ENOMEM;
1292
Jacob Keller671889e2016-12-02 12:33:00 -08001293 /* Create a temporary i40e_new_mac_filter */
1294 new = kzalloc(sizeof(*new), GFP_ATOMIC);
1295 if (!new)
1296 return -ENOMEM;
1297
1298 new->f = add_head;
1299 new->state = add_head->state;
1300
1301 /* Add the new filter to the tmp list */
1302 hlist_add_head(&new->hlist, tmp_add_list);
Jacob Keller489a3262016-11-11 12:39:31 -08001303
1304 /* Put the original filter into the delete list */
1305 f->state = I40E_FILTER_REMOVE;
1306 hash_del(&f->hlist);
1307 hlist_add_head(&f->hlist, tmp_del_list);
1308 }
1309 }
1310
1311 vsi->has_vlan_filter = !!vlan_filters;
1312
1313 return 0;
1314}
1315
1316/**
Jacob Keller1596b5d2016-11-08 13:05:15 -08001317 * i40e_rm_default_mac_filter - Remove the default MAC filter set by NVM
1318 * @vsi: the PF Main VSI - inappropriate for any other VSI
1319 * @macaddr: the MAC address
1320 *
1321 * Remove whatever filter the firmware set up so the driver can manage
1322 * its own filtering intelligently.
1323 **/
1324static void i40e_rm_default_mac_filter(struct i40e_vsi *vsi, u8 *macaddr)
1325{
1326 struct i40e_aqc_remove_macvlan_element_data element;
1327 struct i40e_pf *pf = vsi->back;
1328
1329 /* Only appropriate for the PF main VSI */
1330 if (vsi->type != I40E_VSI_MAIN)
1331 return;
1332
1333 memset(&element, 0, sizeof(element));
1334 ether_addr_copy(element.mac_addr, macaddr);
1335 element.vlan_tag = 0;
1336 /* Ignore error returns, some firmware does it this way... */
1337 element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
1338 i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1339
1340 memset(&element, 0, sizeof(element));
1341 ether_addr_copy(element.mac_addr, macaddr);
1342 element.vlan_tag = 0;
1343 /* ...and some firmware does it this way. */
1344 element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH |
1345 I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
1346 i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1347}
1348
1349/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001350 * i40e_add_filter - Add a mac/vlan filter to the VSI
1351 * @vsi: the VSI to be searched
1352 * @macaddr: the MAC address
1353 * @vlan: the vlan
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001354 *
1355 * Returns ptr to the filter object or NULL when no memory available.
Kiran Patil21659032015-09-30 14:09:03 -04001356 *
Jacob Keller278e7d02016-10-05 09:30:37 -07001357 * NOTE: This function is expected to be called with mac_filter_hash_lock
Kiran Patil21659032015-09-30 14:09:03 -04001358 * being held.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001359 **/
1360struct i40e_mac_filter *i40e_add_filter(struct i40e_vsi *vsi,
Jacob Keller6622f5c2016-10-05 09:30:32 -07001361 const u8 *macaddr, s16 vlan)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001362{
1363 struct i40e_mac_filter *f;
Jacob Keller278e7d02016-10-05 09:30:37 -07001364 u64 key;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001365
1366 if (!vsi || !macaddr)
1367 return NULL;
1368
Jacob Keller1bc87e82016-10-05 09:30:31 -07001369 f = i40e_find_filter(vsi, macaddr, vlan);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001370 if (!f) {
1371 f = kzalloc(sizeof(*f), GFP_ATOMIC);
1372 if (!f)
Jacob Keller1bc87e82016-10-05 09:30:31 -07001373 return NULL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001374
Jacob Kellercbebb852016-10-05 09:30:40 -07001375 /* Update the boolean indicating if we need to function in
1376 * VLAN mode.
1377 */
1378 if (vlan >= 0)
1379 vsi->has_vlan_filter = true;
1380
Greg Rose9a173902014-05-22 06:32:02 +00001381 ether_addr_copy(f->macaddr, macaddr);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001382 f->vlan = vlan;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001383 /* If we're in overflow promisc mode, set the state directly
1384 * to failed, so we don't bother to try sending the filter
1385 * to the hardware.
1386 */
Jacob Keller0da36b92017-04-19 09:25:55 -04001387 if (test_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state))
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001388 f->state = I40E_FILTER_FAILED;
1389 else
1390 f->state = I40E_FILTER_NEW;
Jacob Keller278e7d02016-10-05 09:30:37 -07001391 INIT_HLIST_NODE(&f->hlist);
1392
1393 key = i40e_addr_to_hkey(macaddr);
1394 hash_add(vsi->mac_filter_hash, &f->hlist, key);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001395
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001396 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1397 vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
1398 }
1399
Jacob Keller1bc87e82016-10-05 09:30:31 -07001400 /* If we're asked to add a filter that has been marked for removal, it
1401 * is safe to simply restore it to active state. __i40e_del_filter
1402 * will have simply deleted any filters which were previously marked
1403 * NEW or FAILED, so if it is currently marked REMOVE it must have
1404 * previously been ACTIVE. Since we haven't yet run the sync filters
1405 * task, just restore this filter to the ACTIVE state so that the
1406 * sync task leaves it in place
1407 */
1408 if (f->state == I40E_FILTER_REMOVE)
1409 f->state = I40E_FILTER_ACTIVE;
1410
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001411 return f;
1412}
1413
1414/**
Jacob Keller290d2552016-10-05 09:30:36 -07001415 * __i40e_del_filter - Remove a specific filter from the VSI
1416 * @vsi: VSI to remove from
1417 * @f: the filter to remove from the list
1418 *
1419 * This function should be called instead of i40e_del_filter only if you know
1420 * the exact filter you will remove already, such as via i40e_find_filter or
1421 * i40e_find_mac.
Kiran Patil21659032015-09-30 14:09:03 -04001422 *
Jacob Keller278e7d02016-10-05 09:30:37 -07001423 * NOTE: This function is expected to be called with mac_filter_hash_lock
Kiran Patil21659032015-09-30 14:09:03 -04001424 * being held.
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001425 * ANOTHER NOTE: This function MUST be called from within the context of
1426 * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
1427 * instead of list_for_each_entry().
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001428 **/
Jacob Keller148141b2016-11-11 12:39:36 -08001429void __i40e_del_filter(struct i40e_vsi *vsi, struct i40e_mac_filter *f)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001430{
Jacob Keller1bc87e82016-10-05 09:30:31 -07001431 if (!f)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001432 return;
1433
Alan Bradya410c822016-12-12 15:44:07 -08001434 /* If the filter was never added to firmware then we can just delete it
1435 * directly and we don't want to set the status to remove or else an
1436 * admin queue command will unnecessarily fire.
1437 */
Jacob Keller1bc87e82016-10-05 09:30:31 -07001438 if ((f->state == I40E_FILTER_FAILED) ||
1439 (f->state == I40E_FILTER_NEW)) {
Jacob Keller278e7d02016-10-05 09:30:37 -07001440 hash_del(&f->hlist);
Jacob Keller1bc87e82016-10-05 09:30:31 -07001441 kfree(f);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001442 } else {
Jacob Keller1bc87e82016-10-05 09:30:31 -07001443 f->state = I40E_FILTER_REMOVE;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001444 }
Alan Bradya410c822016-12-12 15:44:07 -08001445
1446 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1447 vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001448}
1449
1450/**
Jacob Keller290d2552016-10-05 09:30:36 -07001451 * i40e_del_filter - Remove a MAC/VLAN filter from the VSI
1452 * @vsi: the VSI to be searched
1453 * @macaddr: the MAC address
1454 * @vlan: the VLAN
1455 *
Jacob Keller278e7d02016-10-05 09:30:37 -07001456 * NOTE: This function is expected to be called with mac_filter_hash_lock
Jacob Keller290d2552016-10-05 09:30:36 -07001457 * being held.
1458 * ANOTHER NOTE: This function MUST be called from within the context of
1459 * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
1460 * instead of list_for_each_entry().
1461 **/
1462void i40e_del_filter(struct i40e_vsi *vsi, const u8 *macaddr, s16 vlan)
1463{
1464 struct i40e_mac_filter *f;
1465
1466 if (!vsi || !macaddr)
1467 return;
1468
1469 f = i40e_find_filter(vsi, macaddr, vlan);
1470 __i40e_del_filter(vsi, f);
1471}
1472
1473/**
Jacob Kellerfeffdbe2016-11-11 12:39:35 -08001474 * i40e_add_mac_filter - Add a MAC filter for all active VLANs
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001475 * @vsi: the VSI to be searched
1476 * @macaddr: the mac address to be filtered
1477 *
Jacob Kellerfeffdbe2016-11-11 12:39:35 -08001478 * If we're not in VLAN mode, just add the filter to I40E_VLAN_ANY. Otherwise,
1479 * go through all the macvlan filters and add a macvlan filter for each
Jacob Keller5feb3d72016-10-05 09:30:34 -07001480 * unique vlan that already exists. If a PVID has been assigned, instead only
1481 * add the macaddr to that VLAN.
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001482 *
Jacob Keller5feb3d72016-10-05 09:30:34 -07001483 * Returns last filter added on success, else NULL
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001484 **/
Jacob Kellerfeffdbe2016-11-11 12:39:35 -08001485struct i40e_mac_filter *i40e_add_mac_filter(struct i40e_vsi *vsi,
1486 const u8 *macaddr)
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001487{
Jacob Keller5feb3d72016-10-05 09:30:34 -07001488 struct i40e_mac_filter *f, *add = NULL;
Jacob Keller278e7d02016-10-05 09:30:37 -07001489 struct hlist_node *h;
1490 int bkt;
Jacob Keller5feb3d72016-10-05 09:30:34 -07001491
1492 if (vsi->info.pvid)
1493 return i40e_add_filter(vsi, macaddr,
1494 le16_to_cpu(vsi->info.pvid));
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001495
Jacob Keller7aaf95362016-11-11 12:39:33 -08001496 if (!i40e_is_vsi_in_vlan(vsi))
1497 return i40e_add_filter(vsi, macaddr, I40E_VLAN_ANY);
1498
Jacob Keller278e7d02016-10-05 09:30:37 -07001499 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Jacob Keller57b341d2016-10-05 09:30:35 -07001500 if (f->state == I40E_FILTER_REMOVE)
1501 continue;
Jacob Keller5feb3d72016-10-05 09:30:34 -07001502 add = i40e_add_filter(vsi, macaddr, f->vlan);
1503 if (!add)
1504 return NULL;
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001505 }
1506
Jacob Keller5feb3d72016-10-05 09:30:34 -07001507 return add;
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001508}
1509
1510/**
Jacob Kellerfeffdbe2016-11-11 12:39:35 -08001511 * i40e_del_mac_filter - Remove a MAC filter from all VLANs
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001512 * @vsi: the VSI to be searched
1513 * @macaddr: the mac address to be removed
1514 *
Jacob Kellerfeffdbe2016-11-11 12:39:35 -08001515 * Removes a given MAC address from a VSI regardless of what VLAN it has been
1516 * associated with.
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001517 *
1518 * Returns 0 for success, or error
1519 **/
Jacob Kellerfeffdbe2016-11-11 12:39:35 -08001520int i40e_del_mac_filter(struct i40e_vsi *vsi, const u8 *macaddr)
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001521{
Jacob Keller278e7d02016-10-05 09:30:37 -07001522 struct i40e_mac_filter *f;
1523 struct hlist_node *h;
Jacob Keller290d2552016-10-05 09:30:36 -07001524 bool found = false;
Jacob Keller278e7d02016-10-05 09:30:37 -07001525 int bkt;
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001526
Jacob Keller278e7d02016-10-05 09:30:37 -07001527 WARN(!spin_is_locked(&vsi->mac_filter_hash_lock),
1528 "Missing mac_filter_hash_lock\n");
1529 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Jacob Keller290d2552016-10-05 09:30:36 -07001530 if (ether_addr_equal(macaddr, f->macaddr)) {
1531 __i40e_del_filter(vsi, f);
1532 found = true;
1533 }
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001534 }
Jacob Keller290d2552016-10-05 09:30:36 -07001535
1536 if (found)
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001537 return 0;
Jacob Keller290d2552016-10-05 09:30:36 -07001538 else
1539 return -ENOENT;
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001540}
1541
1542/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001543 * i40e_set_mac - NDO callback to set mac address
1544 * @netdev: network interface device structure
1545 * @p: pointer to an address structure
1546 *
1547 * Returns 0 on success, negative on failure
1548 **/
1549static int i40e_set_mac(struct net_device *netdev, void *p)
1550{
1551 struct i40e_netdev_priv *np = netdev_priv(netdev);
1552 struct i40e_vsi *vsi = np->vsi;
Shannon Nelson30650cc2014-07-29 04:01:50 +00001553 struct i40e_pf *pf = vsi->back;
1554 struct i40e_hw *hw = &pf->hw;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001555 struct sockaddr *addr = p;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001556
1557 if (!is_valid_ether_addr(addr->sa_data))
1558 return -EADDRNOTAVAIL;
1559
Shannon Nelson30650cc2014-07-29 04:01:50 +00001560 if (ether_addr_equal(netdev->dev_addr, addr->sa_data)) {
1561 netdev_info(netdev, "already using mac address %pM\n",
1562 addr->sa_data);
1563 return 0;
1564 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001565
Jacob Keller0da36b92017-04-19 09:25:55 -04001566 if (test_bit(__I40E_VSI_DOWN, vsi->back->state) ||
1567 test_bit(__I40E_RESET_RECOVERY_PENDING, vsi->back->state))
Anjali Singhai Jain80f64282013-11-28 06:39:47 +00001568 return -EADDRNOTAVAIL;
1569
Shannon Nelson30650cc2014-07-29 04:01:50 +00001570 if (ether_addr_equal(hw->mac.addr, addr->sa_data))
1571 netdev_info(netdev, "returning to hw mac address %pM\n",
1572 hw->mac.addr);
1573 else
1574 netdev_info(netdev, "set new mac address %pM\n", addr->sa_data);
1575
Jacob Keller458867b2017-12-20 11:04:36 -05001576 /* Copy the address first, so that we avoid a possible race with
1577 * .set_rx_mode(). If we copy after changing the address in the filter
1578 * list, we might open ourselves to a narrow race window where
1579 * .set_rx_mode could delete our dev_addr filter and prevent traffic
1580 * from passing.
1581 */
1582 ether_addr_copy(netdev->dev_addr, addr->sa_data);
1583
Jacob Keller278e7d02016-10-05 09:30:37 -07001584 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Kellerfeffdbe2016-11-11 12:39:35 -08001585 i40e_del_mac_filter(vsi, netdev->dev_addr);
1586 i40e_add_mac_filter(vsi, addr->sa_data);
Jacob Keller278e7d02016-10-05 09:30:37 -07001587 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001588 if (vsi->type == I40E_VSI_MAIN) {
1589 i40e_status ret;
Jesse Brandeburg6995b362015-08-28 17:55:54 -04001590
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001591 ret = i40e_aq_mac_address_write(&vsi->back->hw,
Shannon Nelsoncc412222014-06-04 01:23:21 +00001592 I40E_AQC_WRITE_TYPE_LAA_WOL,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001593 addr->sa_data, NULL);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001594 if (ret)
1595 netdev_info(netdev, "Ignoring error from firmware on LAA update, status %s, AQ ret %s\n",
1596 i40e_stat_str(hw, ret),
1597 i40e_aq_str(hw, hw->aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001598 }
1599
Jesse Brandeburgc53934c2016-01-04 10:33:06 -08001600 /* schedule our worker thread which will take care of
1601 * applying the new filter changes
1602 */
1603 i40e_service_event_schedule(vsi->back);
1604 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001605}
1606
1607/**
Amritha Nambiara9ce82f2017-09-07 04:00:22 -07001608 * i40e_config_rss_aq - Prepare for RSS using AQ commands
1609 * @vsi: vsi structure
1610 * @seed: RSS hash seed
1611 **/
1612static int i40e_config_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
1613 u8 *lut, u16 lut_size)
1614{
1615 struct i40e_pf *pf = vsi->back;
1616 struct i40e_hw *hw = &pf->hw;
1617 int ret = 0;
1618
1619 if (seed) {
1620 struct i40e_aqc_get_set_rss_key_data *seed_dw =
1621 (struct i40e_aqc_get_set_rss_key_data *)seed;
1622 ret = i40e_aq_set_rss_key(hw, vsi->id, seed_dw);
1623 if (ret) {
1624 dev_info(&pf->pdev->dev,
1625 "Cannot set RSS key, err %s aq_err %s\n",
1626 i40e_stat_str(hw, ret),
1627 i40e_aq_str(hw, hw->aq.asq_last_status));
1628 return ret;
1629 }
1630 }
1631 if (lut) {
1632 bool pf_lut = vsi->type == I40E_VSI_MAIN ? true : false;
1633
1634 ret = i40e_aq_set_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
1635 if (ret) {
1636 dev_info(&pf->pdev->dev,
1637 "Cannot set RSS lut, err %s aq_err %s\n",
1638 i40e_stat_str(hw, ret),
1639 i40e_aq_str(hw, hw->aq.asq_last_status));
1640 return ret;
1641 }
1642 }
1643 return ret;
1644}
1645
1646/**
1647 * i40e_vsi_config_rss - Prepare for VSI(VMDq) RSS if used
1648 * @vsi: VSI structure
1649 **/
1650static int i40e_vsi_config_rss(struct i40e_vsi *vsi)
1651{
1652 struct i40e_pf *pf = vsi->back;
1653 u8 seed[I40E_HKEY_ARRAY_SIZE];
1654 u8 *lut;
1655 int ret;
1656
1657 if (!(pf->hw_features & I40E_HW_RSS_AQ_CAPABLE))
1658 return 0;
1659 if (!vsi->rss_size)
1660 vsi->rss_size = min_t(int, pf->alloc_rss_size,
1661 vsi->num_queue_pairs);
1662 if (!vsi->rss_size)
1663 return -EINVAL;
1664 lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
1665 if (!lut)
1666 return -ENOMEM;
1667
1668 /* Use the user configured hash keys and lookup table if there is one,
1669 * otherwise use default
1670 */
1671 if (vsi->rss_lut_user)
1672 memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
1673 else
1674 i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
1675 if (vsi->rss_hkey_user)
1676 memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
1677 else
1678 netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
1679 ret = i40e_config_rss_aq(vsi, seed, lut, vsi->rss_table_size);
1680 kfree(lut);
1681 return ret;
1682}
1683
1684/**
1685 * i40e_vsi_setup_queue_map_mqprio - Prepares mqprio based tc_config
1686 * @vsi: the VSI being configured,
1687 * @ctxt: VSI context structure
1688 * @enabled_tc: number of traffic classes to enable
1689 *
1690 * Prepares VSI tc_config to have queue configurations based on MQPRIO options.
1691 **/
1692static int i40e_vsi_setup_queue_map_mqprio(struct i40e_vsi *vsi,
1693 struct i40e_vsi_context *ctxt,
1694 u8 enabled_tc)
1695{
1696 u16 qcount = 0, max_qcount, qmap, sections = 0;
1697 int i, override_q, pow, num_qps, ret;
1698 u8 netdev_tc = 0, offset = 0;
1699
1700 if (vsi->type != I40E_VSI_MAIN)
1701 return -EINVAL;
1702 sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
1703 sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
1704 vsi->tc_config.numtc = vsi->mqprio_qopt.qopt.num_tc;
1705 vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
1706 num_qps = vsi->mqprio_qopt.qopt.count[0];
1707
1708 /* find the next higher power-of-2 of num queue pairs */
1709 pow = ilog2(num_qps);
1710 if (!is_power_of_2(num_qps))
1711 pow++;
1712 qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
1713 (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
1714
1715 /* Setup queue offset/count for all TCs for given VSI */
1716 max_qcount = vsi->mqprio_qopt.qopt.count[0];
1717 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1718 /* See if the given TC is enabled for the given VSI */
1719 if (vsi->tc_config.enabled_tc & BIT(i)) {
1720 offset = vsi->mqprio_qopt.qopt.offset[i];
1721 qcount = vsi->mqprio_qopt.qopt.count[i];
1722 if (qcount > max_qcount)
1723 max_qcount = qcount;
1724 vsi->tc_config.tc_info[i].qoffset = offset;
1725 vsi->tc_config.tc_info[i].qcount = qcount;
1726 vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
1727 } else {
1728 /* TC is not enabled so set the offset to
1729 * default queue and allocate one queue
1730 * for the given TC.
1731 */
1732 vsi->tc_config.tc_info[i].qoffset = 0;
1733 vsi->tc_config.tc_info[i].qcount = 1;
1734 vsi->tc_config.tc_info[i].netdev_tc = 0;
1735 }
1736 }
1737
1738 /* Set actual Tx/Rx queue pairs */
1739 vsi->num_queue_pairs = offset + qcount;
1740
1741 /* Setup queue TC[0].qmap for given VSI context */
1742 ctxt->info.tc_mapping[0] = cpu_to_le16(qmap);
1743 ctxt->info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
1744 ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
1745 ctxt->info.valid_sections |= cpu_to_le16(sections);
1746
1747 /* Reconfigure RSS for main VSI with max queue count */
1748 vsi->rss_size = max_qcount;
1749 ret = i40e_vsi_config_rss(vsi);
1750 if (ret) {
1751 dev_info(&vsi->back->pdev->dev,
1752 "Failed to reconfig rss for num_queues (%u)\n",
1753 max_qcount);
1754 return ret;
1755 }
1756 vsi->reconfig_rss = true;
1757 dev_dbg(&vsi->back->pdev->dev,
1758 "Reconfigured rss with num_queues (%u)\n", max_qcount);
1759
1760 /* Find queue count available for channel VSIs and starting offset
1761 * for channel VSIs
1762 */
1763 override_q = vsi->mqprio_qopt.qopt.count[0];
1764 if (override_q && override_q < vsi->num_queue_pairs) {
1765 vsi->cnt_q_avail = vsi->num_queue_pairs - override_q;
1766 vsi->next_base_queue = override_q;
1767 }
1768 return 0;
1769}
1770
1771/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001772 * i40e_vsi_setup_queue_map - Setup a VSI queue map based on enabled_tc
1773 * @vsi: the VSI being setup
1774 * @ctxt: VSI context structure
1775 * @enabled_tc: Enabled TCs bitmap
1776 * @is_add: True if called before Add VSI
1777 *
1778 * Setup VSI queue mapping for enabled traffic classes.
1779 **/
1780static void i40e_vsi_setup_queue_map(struct i40e_vsi *vsi,
1781 struct i40e_vsi_context *ctxt,
1782 u8 enabled_tc,
1783 bool is_add)
1784{
1785 struct i40e_pf *pf = vsi->back;
1786 u16 sections = 0;
1787 u8 netdev_tc = 0;
1788 u16 numtc = 0;
1789 u16 qcount;
1790 u8 offset;
1791 u16 qmap;
1792 int i;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001793 u16 num_tc_qps = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001794
1795 sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
1796 offset = 0;
1797
1798 if (enabled_tc && (vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
1799 /* Find numtc from enabled TC bitmap */
1800 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08001801 if (enabled_tc & BIT(i)) /* TC is enabled */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001802 numtc++;
1803 }
1804 if (!numtc) {
1805 dev_warn(&pf->pdev->dev, "DCB is enabled but no TC enabled, forcing TC0\n");
1806 numtc = 1;
1807 }
1808 } else {
Amritha Nambiara9ce82f2017-09-07 04:00:22 -07001809 /* At least TC0 is enabled in non-DCB, non-MQPRIO case */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001810 numtc = 1;
1811 }
1812
1813 vsi->tc_config.numtc = numtc;
1814 vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001815 /* Number of queues per enabled TC */
Catherine Sullivan7d644022016-05-16 10:26:41 -07001816 qcount = vsi->alloc_queue_pairs;
1817
Anjali Singhai7f9ff472015-02-21 06:43:19 +00001818 num_tc_qps = qcount / numtc;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001819 num_tc_qps = min_t(int, num_tc_qps, i40e_pf_get_max_q_per_tc(pf));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001820
1821 /* Setup queue offset/count for all TCs for given VSI */
1822 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1823 /* See if the given TC is enabled for the given VSI */
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08001824 if (vsi->tc_config.enabled_tc & BIT(i)) {
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04001825 /* TC is enabled */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001826 int pow, num_qps;
1827
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001828 switch (vsi->type) {
1829 case I40E_VSI_MAIN:
Helin Zhangacd65442015-10-26 19:44:28 -04001830 qcount = min_t(int, pf->alloc_rss_size,
1831 num_tc_qps);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001832 break;
1833 case I40E_VSI_FDIR:
1834 case I40E_VSI_SRIOV:
1835 case I40E_VSI_VMDQ2:
1836 default:
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001837 qcount = num_tc_qps;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001838 WARN_ON(i != 0);
1839 break;
1840 }
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001841 vsi->tc_config.tc_info[i].qoffset = offset;
1842 vsi->tc_config.tc_info[i].qcount = qcount;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001843
Shannon Nelson1e200e42015-02-27 09:15:24 +00001844 /* find the next higher power-of-2 of num queue pairs */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001845 num_qps = qcount;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001846 pow = 0;
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04001847 while (num_qps && (BIT_ULL(pow) < qcount)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001848 pow++;
1849 num_qps >>= 1;
1850 }
1851
1852 vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
1853 qmap =
1854 (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
1855 (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
1856
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001857 offset += qcount;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001858 } else {
1859 /* TC is not enabled so set the offset to
1860 * default queue and allocate one queue
1861 * for the given TC.
1862 */
1863 vsi->tc_config.tc_info[i].qoffset = 0;
1864 vsi->tc_config.tc_info[i].qcount = 1;
1865 vsi->tc_config.tc_info[i].netdev_tc = 0;
1866
1867 qmap = 0;
1868 }
1869 ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
1870 }
1871
1872 /* Set actual Tx/Rx queue pairs */
1873 vsi->num_queue_pairs = offset;
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00001874 if ((vsi->type == I40E_VSI_MAIN) && (numtc == 1)) {
1875 if (vsi->req_queue_pairs > 0)
1876 vsi->num_queue_pairs = vsi->req_queue_pairs;
Anjali Singhai Jain26cdc442015-07-10 19:36:00 -04001877 else if (pf->flags & I40E_FLAG_MSIX_ENABLED)
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00001878 vsi->num_queue_pairs = pf->num_lan_msix;
1879 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001880
1881 /* Scheduler section valid can only be set for ADD VSI */
1882 if (is_add) {
1883 sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
1884
1885 ctxt->info.up_enable_bits = enabled_tc;
1886 }
1887 if (vsi->type == I40E_VSI_SRIOV) {
1888 ctxt->info.mapping_flags |=
1889 cpu_to_le16(I40E_AQ_VSI_QUE_MAP_NONCONTIG);
1890 for (i = 0; i < vsi->num_queue_pairs; i++)
1891 ctxt->info.queue_mapping[i] =
1892 cpu_to_le16(vsi->base_queue + i);
1893 } else {
1894 ctxt->info.mapping_flags |=
1895 cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
1896 ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
1897 }
1898 ctxt->info.valid_sections |= cpu_to_le16(sections);
1899}
1900
1901/**
Jacob Keller6622f5c2016-10-05 09:30:32 -07001902 * i40e_addr_sync - Callback for dev_(mc|uc)_sync to add address
1903 * @netdev: the netdevice
1904 * @addr: address to add
1905 *
1906 * Called by __dev_(mc|uc)_sync when an address needs to be added. We call
1907 * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
1908 */
1909static int i40e_addr_sync(struct net_device *netdev, const u8 *addr)
1910{
1911 struct i40e_netdev_priv *np = netdev_priv(netdev);
1912 struct i40e_vsi *vsi = np->vsi;
Jacob Keller6622f5c2016-10-05 09:30:32 -07001913
Jacob Kellerfeffdbe2016-11-11 12:39:35 -08001914 if (i40e_add_mac_filter(vsi, addr))
Jacob Keller6622f5c2016-10-05 09:30:32 -07001915 return 0;
1916 else
1917 return -ENOMEM;
1918}
1919
1920/**
1921 * i40e_addr_unsync - Callback for dev_(mc|uc)_sync to remove address
1922 * @netdev: the netdevice
1923 * @addr: address to add
1924 *
1925 * Called by __dev_(mc|uc)_sync when an address needs to be removed. We call
1926 * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
1927 */
1928static int i40e_addr_unsync(struct net_device *netdev, const u8 *addr)
1929{
1930 struct i40e_netdev_priv *np = netdev_priv(netdev);
1931 struct i40e_vsi *vsi = np->vsi;
1932
Jacob Keller458867b2017-12-20 11:04:36 -05001933 /* Under some circumstances, we might receive a request to delete
1934 * our own device address from our uc list. Because we store the
1935 * device address in the VSI's MAC/VLAN filter list, we need to ignore
1936 * such requests and not delete our device address from this list.
1937 */
1938 if (ether_addr_equal(addr, netdev->dev_addr))
1939 return 0;
1940
Jacob Kellerfeffdbe2016-11-11 12:39:35 -08001941 i40e_del_mac_filter(vsi, addr);
Jacob Keller6622f5c2016-10-05 09:30:32 -07001942
1943 return 0;
1944}
1945
1946/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001947 * i40e_set_rx_mode - NDO callback to set the netdev filters
1948 * @netdev: network interface device structure
1949 **/
1950static void i40e_set_rx_mode(struct net_device *netdev)
1951{
1952 struct i40e_netdev_priv *np = netdev_priv(netdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001953 struct i40e_vsi *vsi = np->vsi;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001954
Jacob Keller278e7d02016-10-05 09:30:37 -07001955 spin_lock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -04001956
Jacob Keller6622f5c2016-10-05 09:30:32 -07001957 __dev_uc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
1958 __dev_mc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001959
Jacob Keller278e7d02016-10-05 09:30:37 -07001960 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001961
1962 /* check for other flag changes */
1963 if (vsi->current_netdev_flags != vsi->netdev->flags) {
1964 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1965 vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
1966 }
1967}
1968
1969/**
Jacob Keller671889e2016-12-02 12:33:00 -08001970 * i40e_undo_del_filter_entries - Undo the changes made to MAC filter entries
Jacob Keller4a2ce272016-10-05 09:30:38 -07001971 * @vsi: Pointer to VSI struct
Kiran Patil21659032015-09-30 14:09:03 -04001972 * @from: Pointer to list which contains MAC filter entries - changes to
1973 * those entries needs to be undone.
1974 *
Jacob Keller671889e2016-12-02 12:33:00 -08001975 * MAC filter entries from this list were slated for deletion.
Kiran Patil21659032015-09-30 14:09:03 -04001976 **/
Jacob Keller671889e2016-12-02 12:33:00 -08001977static void i40e_undo_del_filter_entries(struct i40e_vsi *vsi,
1978 struct hlist_head *from)
Kiran Patil21659032015-09-30 14:09:03 -04001979{
Jacob Keller278e7d02016-10-05 09:30:37 -07001980 struct i40e_mac_filter *f;
1981 struct hlist_node *h;
Kiran Patil21659032015-09-30 14:09:03 -04001982
Jacob Keller278e7d02016-10-05 09:30:37 -07001983 hlist_for_each_entry_safe(f, h, from, hlist) {
1984 u64 key = i40e_addr_to_hkey(f->macaddr);
1985
Kiran Patil21659032015-09-30 14:09:03 -04001986 /* Move the element back into MAC filter list*/
Jacob Keller278e7d02016-10-05 09:30:37 -07001987 hlist_del(&f->hlist);
1988 hash_add(vsi->mac_filter_hash, &f->hlist, key);
Kiran Patil21659032015-09-30 14:09:03 -04001989 }
1990}
1991
1992/**
Jacob Keller671889e2016-12-02 12:33:00 -08001993 * i40e_undo_add_filter_entries - Undo the changes made to MAC filter entries
1994 * @vsi: Pointer to vsi struct
1995 * @from: Pointer to list which contains MAC filter entries - changes to
1996 * those entries needs to be undone.
1997 *
1998 * MAC filter entries from this list were slated for addition.
1999 **/
2000static void i40e_undo_add_filter_entries(struct i40e_vsi *vsi,
2001 struct hlist_head *from)
2002{
2003 struct i40e_new_mac_filter *new;
2004 struct hlist_node *h;
2005
2006 hlist_for_each_entry_safe(new, h, from, hlist) {
2007 /* We can simply free the wrapper structure */
2008 hlist_del(&new->hlist);
2009 kfree(new);
2010 }
2011}
2012
2013/**
Jacob Kellerd88d40b2016-12-02 12:32:59 -08002014 * i40e_next_entry - Get the next non-broadcast filter from a list
Jacob Keller671889e2016-12-02 12:33:00 -08002015 * @next: pointer to filter in list
Jacob Kellerd88d40b2016-12-02 12:32:59 -08002016 *
2017 * Returns the next non-broadcast filter in the list. Required so that we
2018 * ignore broadcast filters within the list, since these are not handled via
2019 * the normal firmware update path.
2020 */
Jacob Keller671889e2016-12-02 12:33:00 -08002021static
2022struct i40e_new_mac_filter *i40e_next_filter(struct i40e_new_mac_filter *next)
Jacob Kellerd88d40b2016-12-02 12:32:59 -08002023{
Jacob Keller584a8872017-03-07 15:17:52 -08002024 hlist_for_each_entry_continue(next, hlist) {
2025 if (!is_broadcast_ether_addr(next->f->macaddr))
2026 return next;
Jacob Kellerd88d40b2016-12-02 12:32:59 -08002027 }
2028
Jacob Keller584a8872017-03-07 15:17:52 -08002029 return NULL;
Jacob Kellerd88d40b2016-12-02 12:32:59 -08002030}
2031
2032/**
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002033 * i40e_update_filter_state - Update filter state based on return data
2034 * from firmware
2035 * @count: Number of filters added
2036 * @add_list: return data from fw
2037 * @head: pointer to first filter in current batch
Kiran Patil21659032015-09-30 14:09:03 -04002038 *
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002039 * MAC filter entries from list were slated to be added to device. Returns
2040 * number of successful filters. Note that 0 does NOT mean success!
Kiran Patil21659032015-09-30 14:09:03 -04002041 **/
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002042static int
2043i40e_update_filter_state(int count,
2044 struct i40e_aqc_add_macvlan_element_data *add_list,
Jacob Keller671889e2016-12-02 12:33:00 -08002045 struct i40e_new_mac_filter *add_head)
Kiran Patil21659032015-09-30 14:09:03 -04002046{
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002047 int retval = 0;
2048 int i;
Kiran Patil21659032015-09-30 14:09:03 -04002049
Jacob Kellerac9e2392016-11-11 12:39:27 -08002050 for (i = 0; i < count; i++) {
2051 /* Always check status of each filter. We don't need to check
2052 * the firmware return status because we pre-set the filter
2053 * status to I40E_AQC_MM_ERR_NO_RES when sending the filter
2054 * request to the adminq. Thus, if it no longer matches then
2055 * we know the filter is active.
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002056 */
Jacob Kellerac9e2392016-11-11 12:39:27 -08002057 if (add_list[i].match_method == I40E_AQC_MM_ERR_NO_RES) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002058 add_head->state = I40E_FILTER_FAILED;
Jacob Kellerac9e2392016-11-11 12:39:27 -08002059 } else {
2060 add_head->state = I40E_FILTER_ACTIVE;
2061 retval++;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002062 }
Jacob Kellerac9e2392016-11-11 12:39:27 -08002063
Jacob Kellerd88d40b2016-12-02 12:32:59 -08002064 add_head = i40e_next_filter(add_head);
2065 if (!add_head)
2066 break;
Kiran Patil21659032015-09-30 14:09:03 -04002067 }
Jacob Kellerac9e2392016-11-11 12:39:27 -08002068
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002069 return retval;
Kiran Patil21659032015-09-30 14:09:03 -04002070}
2071
2072/**
Jacob Keller00936312016-10-05 09:30:41 -07002073 * i40e_aqc_del_filters - Request firmware to delete a set of filters
2074 * @vsi: ptr to the VSI
2075 * @vsi_name: name to display in messages
2076 * @list: the list of filters to send to firmware
2077 * @num_del: the number of filters to delete
2078 * @retval: Set to -EIO on failure to delete
2079 *
2080 * Send a request to firmware via AdminQ to delete a set of filters. Uses
2081 * *retval instead of a return value so that success does not force ret_val to
2082 * be set to 0. This ensures that a sequence of calls to this function
2083 * preserve the previous value of *retval on successful delete.
2084 */
2085static
2086void i40e_aqc_del_filters(struct i40e_vsi *vsi, const char *vsi_name,
2087 struct i40e_aqc_remove_macvlan_element_data *list,
2088 int num_del, int *retval)
2089{
2090 struct i40e_hw *hw = &vsi->back->hw;
2091 i40e_status aq_ret;
2092 int aq_err;
2093
2094 aq_ret = i40e_aq_remove_macvlan(hw, vsi->seid, list, num_del, NULL);
2095 aq_err = hw->aq.asq_last_status;
2096
2097 /* Explicitly ignore and do not report when firmware returns ENOENT */
2098 if (aq_ret && !(aq_err == I40E_AQ_RC_ENOENT)) {
2099 *retval = -EIO;
2100 dev_info(&vsi->back->pdev->dev,
2101 "ignoring delete macvlan error on %s, err %s, aq_err %s\n",
2102 vsi_name, i40e_stat_str(hw, aq_ret),
2103 i40e_aq_str(hw, aq_err));
2104 }
2105}
2106
2107/**
2108 * i40e_aqc_add_filters - Request firmware to add a set of filters
2109 * @vsi: ptr to the VSI
2110 * @vsi_name: name to display in messages
2111 * @list: the list of filters to send to firmware
2112 * @add_head: Position in the add hlist
2113 * @num_add: the number of filters to add
2114 * @promisc_change: set to true on exit if promiscuous mode was forced on
2115 *
2116 * Send a request to firmware via AdminQ to add a chunk of filters. Will set
2117 * promisc_changed to true if the firmware has run out of space for more
2118 * filters.
2119 */
2120static
2121void i40e_aqc_add_filters(struct i40e_vsi *vsi, const char *vsi_name,
2122 struct i40e_aqc_add_macvlan_element_data *list,
Jacob Keller671889e2016-12-02 12:33:00 -08002123 struct i40e_new_mac_filter *add_head,
Jacob Keller00936312016-10-05 09:30:41 -07002124 int num_add, bool *promisc_changed)
2125{
2126 struct i40e_hw *hw = &vsi->back->hw;
Jacob Keller00936312016-10-05 09:30:41 -07002127 int aq_err, fcnt;
2128
Jacob Kellerac9e2392016-11-11 12:39:27 -08002129 i40e_aq_add_macvlan(hw, vsi->seid, list, num_add, NULL);
Jacob Keller00936312016-10-05 09:30:41 -07002130 aq_err = hw->aq.asq_last_status;
Jacob Kellerac9e2392016-11-11 12:39:27 -08002131 fcnt = i40e_update_filter_state(num_add, list, add_head);
Jacob Keller00936312016-10-05 09:30:41 -07002132
2133 if (fcnt != num_add) {
2134 *promisc_changed = true;
Jacob Keller0da36b92017-04-19 09:25:55 -04002135 set_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
Jacob Keller00936312016-10-05 09:30:41 -07002136 dev_warn(&vsi->back->pdev->dev,
2137 "Error %s adding RX filters on %s, promiscuous mode forced on\n",
2138 i40e_aq_str(hw, aq_err),
2139 vsi_name);
2140 }
2141}
2142
2143/**
Jacob Keller435c0842016-11-08 13:05:10 -08002144 * i40e_aqc_broadcast_filter - Set promiscuous broadcast flags
2145 * @vsi: pointer to the VSI
2146 * @f: filter data
2147 *
2148 * This function sets or clears the promiscuous broadcast flags for VLAN
2149 * filters in order to properly receive broadcast frames. Assumes that only
2150 * broadcast filters are passed.
Jacob Keller671889e2016-12-02 12:33:00 -08002151 *
2152 * Returns status indicating success or failure;
Jacob Keller435c0842016-11-08 13:05:10 -08002153 **/
Jacob Keller671889e2016-12-02 12:33:00 -08002154static i40e_status
2155i40e_aqc_broadcast_filter(struct i40e_vsi *vsi, const char *vsi_name,
2156 struct i40e_mac_filter *f)
Jacob Keller435c0842016-11-08 13:05:10 -08002157{
2158 bool enable = f->state == I40E_FILTER_NEW;
2159 struct i40e_hw *hw = &vsi->back->hw;
2160 i40e_status aq_ret;
2161
2162 if (f->vlan == I40E_VLAN_ANY) {
2163 aq_ret = i40e_aq_set_vsi_broadcast(hw,
2164 vsi->seid,
2165 enable,
2166 NULL);
2167 } else {
2168 aq_ret = i40e_aq_set_vsi_bc_promisc_on_vlan(hw,
2169 vsi->seid,
2170 enable,
2171 f->vlan,
2172 NULL);
2173 }
2174
Jacob Keller671889e2016-12-02 12:33:00 -08002175 if (aq_ret)
Jacob Keller435c0842016-11-08 13:05:10 -08002176 dev_warn(&vsi->back->pdev->dev,
2177 "Error %s setting broadcast promiscuous mode on %s\n",
2178 i40e_aq_str(hw, hw->aq.asq_last_status),
2179 vsi_name);
Jacob Keller671889e2016-12-02 12:33:00 -08002180
2181 return aq_ret;
Jacob Keller435c0842016-11-08 13:05:10 -08002182}
2183
2184/**
Alan Bradybd5608b2017-11-14 07:00:52 -05002185 * i40e_set_promiscuous - set promiscuous mode
2186 * @pf: board private structure
2187 * @promisc: promisc on or off
2188 *
2189 * There are different ways of setting promiscuous mode on a PF depending on
2190 * what state/environment we're in. This identifies and sets it appropriately.
2191 * Returns 0 on success.
2192 **/
2193static int i40e_set_promiscuous(struct i40e_pf *pf, bool promisc)
2194{
2195 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
2196 struct i40e_hw *hw = &pf->hw;
2197 i40e_status aq_ret;
2198
2199 if (vsi->type == I40E_VSI_MAIN &&
2200 pf->lan_veb != I40E_NO_VEB &&
2201 !(pf->flags & I40E_FLAG_MFP_ENABLED)) {
2202 /* set defport ON for Main VSI instead of true promisc
2203 * this way we will get all unicast/multicast and VLAN
2204 * promisc behavior but will not get VF or VMDq traffic
2205 * replicated on the Main VSI.
2206 */
2207 if (promisc)
2208 aq_ret = i40e_aq_set_default_vsi(hw,
2209 vsi->seid,
2210 NULL);
2211 else
2212 aq_ret = i40e_aq_clear_default_vsi(hw,
2213 vsi->seid,
2214 NULL);
2215 if (aq_ret) {
2216 dev_info(&pf->pdev->dev,
2217 "Set default VSI failed, err %s, aq_err %s\n",
2218 i40e_stat_str(hw, aq_ret),
2219 i40e_aq_str(hw, hw->aq.asq_last_status));
2220 }
2221 } else {
2222 aq_ret = i40e_aq_set_vsi_unicast_promiscuous(
2223 hw,
2224 vsi->seid,
2225 promisc, NULL,
2226 true);
2227 if (aq_ret) {
2228 dev_info(&pf->pdev->dev,
2229 "set unicast promisc failed, err %s, aq_err %s\n",
2230 i40e_stat_str(hw, aq_ret),
2231 i40e_aq_str(hw, hw->aq.asq_last_status));
2232 }
2233 aq_ret = i40e_aq_set_vsi_multicast_promiscuous(
2234 hw,
2235 vsi->seid,
2236 promisc, NULL);
2237 if (aq_ret) {
2238 dev_info(&pf->pdev->dev,
2239 "set multicast promisc failed, err %s, aq_err %s\n",
2240 i40e_stat_str(hw, aq_ret),
2241 i40e_aq_str(hw, hw->aq.asq_last_status));
2242 }
2243 }
2244
2245 if (!aq_ret)
2246 pf->cur_promisc = promisc;
2247
2248 return aq_ret;
2249}
2250
2251/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002252 * i40e_sync_vsi_filters - Update the VSI filter list to the HW
2253 * @vsi: ptr to the VSI
2254 *
2255 * Push any outstanding VSI filter changes through the AdminQ.
2256 *
2257 * Returns 0 or error value
2258 **/
Jesse Brandeburg17652c62015-11-05 17:01:02 -08002259int i40e_sync_vsi_filters(struct i40e_vsi *vsi)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002260{
Jacob Keller278e7d02016-10-05 09:30:37 -07002261 struct hlist_head tmp_add_list, tmp_del_list;
Jacob Keller671889e2016-12-02 12:33:00 -08002262 struct i40e_mac_filter *f;
2263 struct i40e_new_mac_filter *new, *add_head = NULL;
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002264 struct i40e_hw *hw = &vsi->back->hw;
Jacob Keller38326212016-11-11 12:39:26 -08002265 unsigned int failed_filters = 0;
Alan Brady84f5ca62016-10-05 09:30:39 -07002266 unsigned int vlan_filters = 0;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002267 bool promisc_changed = false;
Shannon Nelson2d1de822016-05-16 10:26:44 -07002268 char vsi_name[16] = "PF";
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002269 int filter_list_len = 0;
Mitch Williamsea02e902015-11-09 15:35:50 -08002270 i40e_status aq_ret = 0;
Alan Brady84f5ca62016-10-05 09:30:39 -07002271 u32 changed_flags = 0;
Jacob Keller278e7d02016-10-05 09:30:37 -07002272 struct hlist_node *h;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002273 struct i40e_pf *pf;
2274 int num_add = 0;
2275 int num_del = 0;
Alan Brady84f5ca62016-10-05 09:30:39 -07002276 int retval = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002277 u16 cmd_flags;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002278 int list_size;
Jacob Keller278e7d02016-10-05 09:30:37 -07002279 int bkt;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002280
2281 /* empty array typed pointers, kcalloc later */
2282 struct i40e_aqc_add_macvlan_element_data *add_list;
2283 struct i40e_aqc_remove_macvlan_element_data *del_list;
2284
Jacob Keller0da36b92017-04-19 09:25:55 -04002285 while (test_and_set_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002286 usleep_range(1000, 2000);
2287 pf = vsi->back;
2288
2289 if (vsi->netdev) {
2290 changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
2291 vsi->current_netdev_flags = vsi->netdev->flags;
2292 }
2293
Jacob Keller278e7d02016-10-05 09:30:37 -07002294 INIT_HLIST_HEAD(&tmp_add_list);
2295 INIT_HLIST_HEAD(&tmp_del_list);
Kiran Patil21659032015-09-30 14:09:03 -04002296
Shannon Nelson2d1de822016-05-16 10:26:44 -07002297 if (vsi->type == I40E_VSI_SRIOV)
2298 snprintf(vsi_name, sizeof(vsi_name) - 1, "VF %d", vsi->vf_id);
2299 else if (vsi->type != I40E_VSI_MAIN)
2300 snprintf(vsi_name, sizeof(vsi_name) - 1, "vsi %d", vsi->seid);
2301
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002302 if (vsi->flags & I40E_VSI_FLAG_FILTER_CHANGED) {
2303 vsi->flags &= ~I40E_VSI_FLAG_FILTER_CHANGED;
2304
Jacob Keller278e7d02016-10-05 09:30:37 -07002305 spin_lock_bh(&vsi->mac_filter_hash_lock);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002306 /* Create a list of filters to delete. */
Jacob Keller278e7d02016-10-05 09:30:37 -07002307 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002308 if (f->state == I40E_FILTER_REMOVE) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002309 /* Move the element into temporary del_list */
Jacob Keller278e7d02016-10-05 09:30:37 -07002310 hash_del(&f->hlist);
2311 hlist_add_head(&f->hlist, &tmp_del_list);
Alan Brady84f5ca62016-10-05 09:30:39 -07002312
2313 /* Avoid counting removed filters */
2314 continue;
Kiran Patil21659032015-09-30 14:09:03 -04002315 }
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002316 if (f->state == I40E_FILTER_NEW) {
Jacob Keller671889e2016-12-02 12:33:00 -08002317 /* Create a temporary i40e_new_mac_filter */
2318 new = kzalloc(sizeof(*new), GFP_ATOMIC);
2319 if (!new)
2320 goto err_no_memory_locked;
2321
2322 /* Store pointer to the real filter */
2323 new->f = f;
2324 new->state = f->state;
2325
2326 /* Add it to the hash list */
2327 hlist_add_head(&new->hlist, &tmp_add_list);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002328 }
Alan Brady84f5ca62016-10-05 09:30:39 -07002329
Jacob Keller489a3262016-11-11 12:39:31 -08002330 /* Count the number of active (current and new) VLAN
2331 * filters we have now. Does not count filters which
2332 * are marked for deletion.
Alan Brady84f5ca62016-10-05 09:30:39 -07002333 */
2334 if (f->vlan > 0)
2335 vlan_filters++;
Alan Brady84f5ca62016-10-05 09:30:39 -07002336 }
2337
Jacob Keller489a3262016-11-11 12:39:31 -08002338 retval = i40e_correct_mac_vlan_filters(vsi,
2339 &tmp_add_list,
2340 &tmp_del_list,
2341 vlan_filters);
2342 if (retval)
2343 goto err_no_memory_locked;
Alan Brady84f5ca62016-10-05 09:30:39 -07002344
Jacob Keller278e7d02016-10-05 09:30:37 -07002345 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -04002346 }
2347
2348 /* Now process 'del_list' outside the lock */
Jacob Keller278e7d02016-10-05 09:30:37 -07002349 if (!hlist_empty(&tmp_del_list)) {
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002350 filter_list_len = hw->aq.asq_buf_size /
Kiran Patil21659032015-09-30 14:09:03 -04002351 sizeof(struct i40e_aqc_remove_macvlan_element_data);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002352 list_size = filter_list_len *
Shannon Nelsonf1199992015-11-19 11:34:23 -08002353 sizeof(struct i40e_aqc_remove_macvlan_element_data);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002354 del_list = kzalloc(list_size, GFP_ATOMIC);
Jacob Keller4a2ce272016-10-05 09:30:38 -07002355 if (!del_list)
2356 goto err_no_memory;
Kiran Patil21659032015-09-30 14:09:03 -04002357
Jacob Keller278e7d02016-10-05 09:30:37 -07002358 hlist_for_each_entry_safe(f, h, &tmp_del_list, hlist) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002359 cmd_flags = 0;
2360
Jacob Keller435c0842016-11-08 13:05:10 -08002361 /* handle broadcast filters by updating the broadcast
Jacob Kellerd88d40b2016-12-02 12:32:59 -08002362 * promiscuous flag and release filter list.
Jacob Keller435c0842016-11-08 13:05:10 -08002363 */
2364 if (is_broadcast_ether_addr(f->macaddr)) {
2365 i40e_aqc_broadcast_filter(vsi, vsi_name, f);
2366
2367 hlist_del(&f->hlist);
2368 kfree(f);
2369 continue;
2370 }
2371
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002372 /* add to delete list */
Greg Rose9a173902014-05-22 06:32:02 +00002373 ether_addr_copy(del_list[num_del].mac_addr, f->macaddr);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002374 if (f->vlan == I40E_VLAN_ANY) {
2375 del_list[num_del].vlan_tag = 0;
Alan Bradya6cb9142016-09-06 18:05:07 -07002376 cmd_flags |= I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002377 } else {
2378 del_list[num_del].vlan_tag =
2379 cpu_to_le16((u16)(f->vlan));
2380 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002381
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002382 cmd_flags |= I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
2383 del_list[num_del].flags = cmd_flags;
2384 num_del++;
2385
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002386 /* flush a full buffer */
2387 if (num_del == filter_list_len) {
Jacob Keller00936312016-10-05 09:30:41 -07002388 i40e_aqc_del_filters(vsi, vsi_name, del_list,
2389 num_del, &retval);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002390 memset(del_list, 0, list_size);
Jacob Keller00936312016-10-05 09:30:41 -07002391 num_del = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002392 }
Kiran Patil21659032015-09-30 14:09:03 -04002393 /* Release memory for MAC filter entries which were
2394 * synced up with HW.
2395 */
Jacob Keller278e7d02016-10-05 09:30:37 -07002396 hlist_del(&f->hlist);
Kiran Patil21659032015-09-30 14:09:03 -04002397 kfree(f);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002398 }
Kiran Patil21659032015-09-30 14:09:03 -04002399
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002400 if (num_del) {
Jacob Keller00936312016-10-05 09:30:41 -07002401 i40e_aqc_del_filters(vsi, vsi_name, del_list,
2402 num_del, &retval);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002403 }
2404
2405 kfree(del_list);
2406 del_list = NULL;
Kiran Patil21659032015-09-30 14:09:03 -04002407 }
2408
Jacob Keller278e7d02016-10-05 09:30:37 -07002409 if (!hlist_empty(&tmp_add_list)) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002410 /* Do all the adds now. */
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002411 filter_list_len = hw->aq.asq_buf_size /
Shannon Nelsonf1199992015-11-19 11:34:23 -08002412 sizeof(struct i40e_aqc_add_macvlan_element_data);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002413 list_size = filter_list_len *
2414 sizeof(struct i40e_aqc_add_macvlan_element_data);
2415 add_list = kzalloc(list_size, GFP_ATOMIC);
Jacob Keller4a2ce272016-10-05 09:30:38 -07002416 if (!add_list)
2417 goto err_no_memory;
2418
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002419 num_add = 0;
Jacob Keller671889e2016-12-02 12:33:00 -08002420 hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
Jacob Kellerd19cb642017-04-21 13:38:05 -07002421 if (test_bit(__I40E_VSI_OVERFLOW_PROMISC,
Jacob Keller0da36b92017-04-19 09:25:55 -04002422 vsi->state)) {
Jacob Keller671889e2016-12-02 12:33:00 -08002423 new->state = I40E_FILTER_FAILED;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002424 continue;
2425 }
Jacob Keller435c0842016-11-08 13:05:10 -08002426
2427 /* handle broadcast filters by updating the broadcast
2428 * promiscuous flag instead of adding a MAC filter.
2429 */
Jacob Keller671889e2016-12-02 12:33:00 -08002430 if (is_broadcast_ether_addr(new->f->macaddr)) {
2431 if (i40e_aqc_broadcast_filter(vsi, vsi_name,
2432 new->f))
2433 new->state = I40E_FILTER_FAILED;
2434 else
2435 new->state = I40E_FILTER_ACTIVE;
Jacob Keller435c0842016-11-08 13:05:10 -08002436 continue;
2437 }
2438
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002439 /* add to add array */
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002440 if (num_add == 0)
Jacob Keller671889e2016-12-02 12:33:00 -08002441 add_head = new;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002442 cmd_flags = 0;
Jacob Keller671889e2016-12-02 12:33:00 -08002443 ether_addr_copy(add_list[num_add].mac_addr,
2444 new->f->macaddr);
2445 if (new->f->vlan == I40E_VLAN_ANY) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002446 add_list[num_add].vlan_tag = 0;
2447 cmd_flags |= I40E_AQC_MACVLAN_ADD_IGNORE_VLAN;
2448 } else {
2449 add_list[num_add].vlan_tag =
Jacob Keller671889e2016-12-02 12:33:00 -08002450 cpu_to_le16((u16)(new->f->vlan));
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002451 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002452 add_list[num_add].queue_number = 0;
Jacob Kellerac9e2392016-11-11 12:39:27 -08002453 /* set invalid match method for later detection */
Keller, Jacob E0266ac42016-12-09 13:39:21 -08002454 add_list[num_add].match_method = I40E_AQC_MM_ERR_NO_RES;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002455 cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002456 add_list[num_add].flags = cpu_to_le16(cmd_flags);
2457 num_add++;
2458
2459 /* flush a full buffer */
2460 if (num_add == filter_list_len) {
Jacob Keller00936312016-10-05 09:30:41 -07002461 i40e_aqc_add_filters(vsi, vsi_name, add_list,
2462 add_head, num_add,
2463 &promisc_changed);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002464 memset(add_list, 0, list_size);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002465 num_add = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002466 }
2467 }
2468 if (num_add) {
Jacob Keller00936312016-10-05 09:30:41 -07002469 i40e_aqc_add_filters(vsi, vsi_name, add_list, add_head,
2470 num_add, &promisc_changed);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002471 }
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002472 /* Now move all of the filters from the temp add list back to
2473 * the VSI's list.
2474 */
Jacob Keller278e7d02016-10-05 09:30:37 -07002475 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller671889e2016-12-02 12:33:00 -08002476 hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
2477 /* Only update the state if we're still NEW */
2478 if (new->f->state == I40E_FILTER_NEW)
2479 new->f->state = new->state;
2480 hlist_del(&new->hlist);
2481 kfree(new);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002482 }
Jacob Keller278e7d02016-10-05 09:30:37 -07002483 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002484 kfree(add_list);
2485 add_list = NULL;
2486 }
2487
Jacob Keller38326212016-11-11 12:39:26 -08002488 /* Determine the number of active and failed filters. */
2489 spin_lock_bh(&vsi->mac_filter_hash_lock);
2490 vsi->active_filters = 0;
2491 hash_for_each(vsi->mac_filter_hash, bkt, f, hlist) {
2492 if (f->state == I40E_FILTER_ACTIVE)
2493 vsi->active_filters++;
2494 else if (f->state == I40E_FILTER_FAILED)
2495 failed_filters++;
2496 }
2497 spin_unlock_bh(&vsi->mac_filter_hash_lock);
2498
2499 /* If promiscuous mode has changed, we need to calculate a new
2500 * threshold for when we are safe to exit
2501 */
2502 if (promisc_changed)
2503 vsi->promisc_threshold = (vsi->active_filters * 3) / 4;
2504
2505 /* Check if we are able to exit overflow promiscuous mode. We can
2506 * safely exit if we didn't just enter, we no longer have any failed
2507 * filters, and we have reduced filters below the threshold value.
2508 */
Jacob Keller0da36b92017-04-19 09:25:55 -04002509 if (test_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state) &&
Jacob Keller38326212016-11-11 12:39:26 -08002510 !promisc_changed && !failed_filters &&
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002511 (vsi->active_filters < vsi->promisc_threshold)) {
Jacob Keller38326212016-11-11 12:39:26 -08002512 dev_info(&pf->pdev->dev,
2513 "filter logjam cleared on %s, leaving overflow promiscuous mode\n",
2514 vsi_name);
Jacob Keller0da36b92017-04-19 09:25:55 -04002515 clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
Jacob Keller38326212016-11-11 12:39:26 -08002516 promisc_changed = true;
2517 vsi->promisc_threshold = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002518 }
2519
Anjali Singhai Jaina856b5c2016-04-13 03:08:23 -07002520 /* if the VF is not trusted do not do promisc */
2521 if ((vsi->type == I40E_VSI_SRIOV) && !pf->vf[vsi->vf_id].trusted) {
Jacob Keller0da36b92017-04-19 09:25:55 -04002522 clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
Anjali Singhai Jaina856b5c2016-04-13 03:08:23 -07002523 goto out;
2524 }
2525
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002526 /* check for changes in promiscuous modes */
2527 if (changed_flags & IFF_ALLMULTI) {
2528 bool cur_multipromisc;
Jesse Brandeburg6995b362015-08-28 17:55:54 -04002529
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002530 cur_multipromisc = !!(vsi->current_netdev_flags & IFF_ALLMULTI);
Mitch Williamsea02e902015-11-09 15:35:50 -08002531 aq_ret = i40e_aq_set_vsi_multicast_promiscuous(&vsi->back->hw,
2532 vsi->seid,
2533 cur_multipromisc,
2534 NULL);
2535 if (aq_ret) {
2536 retval = i40e_aq_rc_to_posix(aq_ret,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002537 hw->aq.asq_last_status);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002538 dev_info(&pf->pdev->dev,
Shannon Nelson2d1de822016-05-16 10:26:44 -07002539 "set multi promisc failed on %s, err %s aq_err %s\n",
2540 vsi_name,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002541 i40e_stat_str(hw, aq_ret),
2542 i40e_aq_str(hw, hw->aq.asq_last_status));
Mitch Williamsea02e902015-11-09 15:35:50 -08002543 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002544 }
Alan Bradye5887232017-06-07 05:43:10 -04002545
2546 if ((changed_flags & IFF_PROMISC) || promisc_changed) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002547 bool cur_promisc;
Jesse Brandeburg6995b362015-08-28 17:55:54 -04002548
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002549 cur_promisc = (!!(vsi->current_netdev_flags & IFF_PROMISC) ||
Jacob Kellerd19cb642017-04-21 13:38:05 -07002550 test_bit(__I40E_VSI_OVERFLOW_PROMISC,
Jacob Keller0da36b92017-04-19 09:25:55 -04002551 vsi->state));
Alan Bradybd5608b2017-11-14 07:00:52 -05002552 aq_ret = i40e_set_promiscuous(pf, cur_promisc);
Mitch Williamsea02e902015-11-09 15:35:50 -08002553 if (aq_ret) {
2554 retval = i40e_aq_rc_to_posix(aq_ret,
Alan Bradybd5608b2017-11-14 07:00:52 -05002555 hw->aq.asq_last_status);
Greg Rose1a103702013-11-28 06:42:39 +00002556 dev_info(&pf->pdev->dev,
Alan Bradybd5608b2017-11-14 07:00:52 -05002557 "Setting promiscuous %s failed on %s, err %s aq_err %s\n",
2558 cur_promisc ? "on" : "off",
2559 vsi_name,
2560 i40e_stat_str(hw, aq_ret),
2561 i40e_aq_str(hw, hw->aq.asq_last_status));
Mitch Williamsea02e902015-11-09 15:35:50 -08002562 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002563 }
Mitch Williamsea02e902015-11-09 15:35:50 -08002564out:
Jesse Brandeburg2818ccd2016-01-13 16:51:38 -08002565 /* if something went wrong then set the changed flag so we try again */
2566 if (retval)
2567 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2568
Jacob Keller0da36b92017-04-19 09:25:55 -04002569 clear_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state);
Mitch Williamsea02e902015-11-09 15:35:50 -08002570 return retval;
Jacob Keller4a2ce272016-10-05 09:30:38 -07002571
2572err_no_memory:
2573 /* Restore elements on the temporary add and delete lists */
2574 spin_lock_bh(&vsi->mac_filter_hash_lock);
Alan Brady84f5ca62016-10-05 09:30:39 -07002575err_no_memory_locked:
Jacob Keller671889e2016-12-02 12:33:00 -08002576 i40e_undo_del_filter_entries(vsi, &tmp_del_list);
2577 i40e_undo_add_filter_entries(vsi, &tmp_add_list);
Jacob Keller4a2ce272016-10-05 09:30:38 -07002578 spin_unlock_bh(&vsi->mac_filter_hash_lock);
2579
2580 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
Jacob Keller0da36b92017-04-19 09:25:55 -04002581 clear_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state);
Jacob Keller4a2ce272016-10-05 09:30:38 -07002582 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002583}
2584
2585/**
2586 * i40e_sync_filters_subtask - Sync the VSI filter list with HW
2587 * @pf: board private structure
2588 **/
2589static void i40e_sync_filters_subtask(struct i40e_pf *pf)
2590{
2591 int v;
2592
2593 if (!pf || !(pf->flags & I40E_FLAG_FILTER_SYNC))
2594 return;
2595 pf->flags &= ~I40E_FLAG_FILTER_SYNC;
2596
Mitch Williams505682c2014-05-20 08:01:37 +00002597 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002598 if (pf->vsi[v] &&
Jesse Brandeburg17652c62015-11-05 17:01:02 -08002599 (pf->vsi[v]->flags & I40E_VSI_FLAG_FILTER_CHANGED)) {
2600 int ret = i40e_sync_vsi_filters(pf->vsi[v]);
2601
2602 if (ret) {
2603 /* come back and try again later */
2604 pf->flags |= I40E_FLAG_FILTER_SYNC;
2605 break;
2606 }
2607 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002608 }
2609}
2610
2611/**
Björn Töpel0c8493d2017-05-24 07:55:34 +02002612 * i40e_max_xdp_frame_size - returns the maximum allowed frame size for XDP
2613 * @vsi: the vsi
2614 **/
2615static int i40e_max_xdp_frame_size(struct i40e_vsi *vsi)
2616{
2617 if (PAGE_SIZE >= 8192 || (vsi->back->flags & I40E_FLAG_LEGACY_RX))
2618 return I40E_RXBUFFER_2048;
2619 else
2620 return I40E_RXBUFFER_3072;
2621}
2622
2623/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002624 * i40e_change_mtu - NDO callback to change the Maximum Transfer Unit
2625 * @netdev: network interface device structure
2626 * @new_mtu: new value for maximum frame size
2627 *
2628 * Returns 0 on success, negative on failure
2629 **/
2630static int i40e_change_mtu(struct net_device *netdev, int new_mtu)
2631{
2632 struct i40e_netdev_priv *np = netdev_priv(netdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002633 struct i40e_vsi *vsi = np->vsi;
Mitch Williams0ef2d5a2017-01-24 10:24:00 -08002634 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002635
Björn Töpel0c8493d2017-05-24 07:55:34 +02002636 if (i40e_enabled_xdp_vsi(vsi)) {
2637 int frame_size = new_mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
2638
2639 if (frame_size > i40e_max_xdp_frame_size(vsi))
2640 return -EINVAL;
2641 }
2642
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002643 netdev_info(netdev, "changing MTU from %d to %d\n",
2644 netdev->mtu, new_mtu);
2645 netdev->mtu = new_mtu;
2646 if (netif_running(netdev))
2647 i40e_vsi_reinit_locked(vsi);
Mitch Williams0ef2d5a2017-01-24 10:24:00 -08002648 pf->flags |= (I40E_FLAG_SERVICE_CLIENT_REQUESTED |
2649 I40E_FLAG_CLIENT_L2_CHANGE);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002650 return 0;
2651}
2652
2653/**
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00002654 * i40e_ioctl - Access the hwtstamp interface
2655 * @netdev: network interface device structure
2656 * @ifr: interface request data
2657 * @cmd: ioctl command
2658 **/
2659int i40e_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
2660{
2661 struct i40e_netdev_priv *np = netdev_priv(netdev);
2662 struct i40e_pf *pf = np->vsi->back;
2663
2664 switch (cmd) {
2665 case SIOCGHWTSTAMP:
2666 return i40e_ptp_get_ts_config(pf, ifr);
2667 case SIOCSHWTSTAMP:
2668 return i40e_ptp_set_ts_config(pf, ifr);
2669 default:
2670 return -EOPNOTSUPP;
2671 }
2672}
2673
2674/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002675 * i40e_vlan_stripping_enable - Turn on vlan stripping for the VSI
2676 * @vsi: the vsi being adjusted
2677 **/
2678void i40e_vlan_stripping_enable(struct i40e_vsi *vsi)
2679{
2680 struct i40e_vsi_context ctxt;
2681 i40e_status ret;
2682
2683 if ((vsi->info.valid_sections &
2684 cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
2685 ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_MODE_MASK) == 0))
2686 return; /* already enabled */
2687
2688 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2689 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
2690 I40E_AQ_VSI_PVLAN_EMOD_STR_BOTH;
2691
2692 ctxt.seid = vsi->seid;
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07002693 ctxt.info = vsi->info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002694 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2695 if (ret) {
2696 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002697 "update vlan stripping failed, err %s aq_err %s\n",
2698 i40e_stat_str(&vsi->back->hw, ret),
2699 i40e_aq_str(&vsi->back->hw,
2700 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002701 }
2702}
2703
2704/**
2705 * i40e_vlan_stripping_disable - Turn off vlan stripping for the VSI
2706 * @vsi: the vsi being adjusted
2707 **/
2708void i40e_vlan_stripping_disable(struct i40e_vsi *vsi)
2709{
2710 struct i40e_vsi_context ctxt;
2711 i40e_status ret;
2712
2713 if ((vsi->info.valid_sections &
2714 cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
2715 ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_EMOD_MASK) ==
2716 I40E_AQ_VSI_PVLAN_EMOD_MASK))
2717 return; /* already disabled */
2718
2719 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2720 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
2721 I40E_AQ_VSI_PVLAN_EMOD_NOTHING;
2722
2723 ctxt.seid = vsi->seid;
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07002724 ctxt.info = vsi->info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002725 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2726 if (ret) {
2727 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002728 "update vlan stripping failed, err %s aq_err %s\n",
2729 i40e_stat_str(&vsi->back->hw, ret),
2730 i40e_aq_str(&vsi->back->hw,
2731 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002732 }
2733}
2734
2735/**
2736 * i40e_vlan_rx_register - Setup or shutdown vlan offload
2737 * @netdev: network interface to be adjusted
2738 * @features: netdev features to test if VLAN offload is enabled or not
2739 **/
2740static void i40e_vlan_rx_register(struct net_device *netdev, u32 features)
2741{
2742 struct i40e_netdev_priv *np = netdev_priv(netdev);
2743 struct i40e_vsi *vsi = np->vsi;
2744
2745 if (features & NETIF_F_HW_VLAN_CTAG_RX)
2746 i40e_vlan_stripping_enable(vsi);
2747 else
2748 i40e_vlan_stripping_disable(vsi);
2749}
2750
2751/**
Jacob Keller490a4ad2016-11-11 12:39:29 -08002752 * i40e_add_vlan_all_mac - Add a MAC/VLAN filter for each existing MAC address
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002753 * @vsi: the vsi being configured
2754 * @vid: vlan id to be added (0 = untagged only , -1 = any)
Jacob Keller490a4ad2016-11-11 12:39:29 -08002755 *
2756 * This is a helper function for adding a new MAC/VLAN filter with the
2757 * specified VLAN for each existing MAC address already in the hash table.
2758 * This function does *not* perform any accounting to update filters based on
2759 * VLAN mode.
2760 *
2761 * NOTE: this function expects to be called while under the
2762 * mac_filter_hash_lock
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002763 **/
Jacob Keller9af52f62016-11-11 12:39:30 -08002764int i40e_add_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002765{
Jacob Keller490a4ad2016-11-11 12:39:29 -08002766 struct i40e_mac_filter *f, *add_f;
Jacob Keller278e7d02016-10-05 09:30:37 -07002767 struct hlist_node *h;
2768 int bkt;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002769
Jacob Keller278e7d02016-10-05 09:30:37 -07002770 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Jacob Keller57b341d2016-10-05 09:30:35 -07002771 if (f->state == I40E_FILTER_REMOVE)
2772 continue;
Jacob Keller1bc87e82016-10-05 09:30:31 -07002773 add_f = i40e_add_filter(vsi, f->macaddr, vid);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002774 if (!add_f) {
2775 dev_info(&vsi->back->pdev->dev,
2776 "Could not add vlan filter %d for %pM\n",
2777 vid, f->macaddr);
2778 return -ENOMEM;
2779 }
2780 }
2781
Jacob Keller490a4ad2016-11-11 12:39:29 -08002782 return 0;
2783}
2784
2785/**
2786 * i40e_vsi_add_vlan - Add VSI membership for given VLAN
2787 * @vsi: the VSI being configured
Jacob Kellerf94484b2016-12-07 14:05:34 -08002788 * @vid: VLAN id to be added
Jacob Keller490a4ad2016-11-11 12:39:29 -08002789 **/
Jacob Kellerf94484b2016-12-07 14:05:34 -08002790int i40e_vsi_add_vlan(struct i40e_vsi *vsi, u16 vid)
Jacob Keller490a4ad2016-11-11 12:39:29 -08002791{
Jacob Keller489a3262016-11-11 12:39:31 -08002792 int err;
Jacob Keller490a4ad2016-11-11 12:39:29 -08002793
Jacob Kellerfcf6cfc2017-06-23 04:24:51 -04002794 if (vsi->info.pvid)
Jacob Kellerf94484b2016-12-07 14:05:34 -08002795 return -EINVAL;
2796
Jacob Kellerfcf6cfc2017-06-23 04:24:51 -04002797 /* The network stack will attempt to add VID=0, with the intention to
2798 * receive priority tagged packets with a VLAN of 0. Our HW receives
2799 * these packets by default when configured to receive untagged
2800 * packets, so we don't need to add a filter for this case.
2801 * Additionally, HW interprets adding a VID=0 filter as meaning to
2802 * receive *only* tagged traffic and stops receiving untagged traffic.
2803 * Thus, we do not want to actually add a filter for VID=0
2804 */
2805 if (!vid)
2806 return 0;
2807
Jacob Keller490a4ad2016-11-11 12:39:29 -08002808 /* Locked once because all functions invoked below iterates list*/
2809 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller490a4ad2016-11-11 12:39:29 -08002810 err = i40e_add_vlan_all_mac(vsi, vid);
Jacob Keller278e7d02016-10-05 09:30:37 -07002811 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller489a3262016-11-11 12:39:31 -08002812 if (err)
2813 return err;
Kiran Patil21659032015-09-30 14:09:03 -04002814
Jesse Brandeburg0e4425e2015-11-05 17:01:01 -08002815 /* schedule our worker thread which will take care of
2816 * applying the new filter changes
2817 */
2818 i40e_service_event_schedule(vsi->back);
2819 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002820}
2821
2822/**
Jacob Keller490a4ad2016-11-11 12:39:29 -08002823 * i40e_rm_vlan_all_mac - Remove MAC/VLAN pair for all MAC with the given VLAN
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002824 * @vsi: the vsi being configured
2825 * @vid: vlan id to be removed (0 = untagged only , -1 = any)
Jacob Keller490a4ad2016-11-11 12:39:29 -08002826 *
2827 * This function should be used to remove all VLAN filters which match the
2828 * given VID. It does not schedule the service event and does not take the
2829 * mac_filter_hash_lock so it may be combined with other operations under
2830 * a single invocation of the mac_filter_hash_lock.
2831 *
2832 * NOTE: this function expects to be called while under the
2833 * mac_filter_hash_lock
2834 */
Jacob Keller9af52f62016-11-11 12:39:30 -08002835void i40e_rm_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002836{
Alan Brady84f5ca62016-10-05 09:30:39 -07002837 struct i40e_mac_filter *f;
Jacob Keller278e7d02016-10-05 09:30:37 -07002838 struct hlist_node *h;
Jacob Keller278e7d02016-10-05 09:30:37 -07002839 int bkt;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002840
Jacob Keller278e7d02016-10-05 09:30:37 -07002841 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Jacob Keller290d2552016-10-05 09:30:36 -07002842 if (f->vlan == vid)
2843 __i40e_del_filter(vsi, f);
2844 }
Jacob Keller490a4ad2016-11-11 12:39:29 -08002845}
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002846
Jacob Keller490a4ad2016-11-11 12:39:29 -08002847/**
2848 * i40e_vsi_kill_vlan - Remove VSI membership for given VLAN
2849 * @vsi: the VSI being configured
Jacob Kellerf94484b2016-12-07 14:05:34 -08002850 * @vid: VLAN id to be removed
Jacob Keller490a4ad2016-11-11 12:39:29 -08002851 **/
Jacob Kellerf94484b2016-12-07 14:05:34 -08002852void i40e_vsi_kill_vlan(struct i40e_vsi *vsi, u16 vid)
Jacob Keller490a4ad2016-11-11 12:39:29 -08002853{
Jacob Kellerf94484b2016-12-07 14:05:34 -08002854 if (!vid || vsi->info.pvid)
2855 return;
2856
Jacob Keller490a4ad2016-11-11 12:39:29 -08002857 spin_lock_bh(&vsi->mac_filter_hash_lock);
2858 i40e_rm_vlan_all_mac(vsi, vid);
Jacob Keller278e7d02016-10-05 09:30:37 -07002859 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -04002860
Jesse Brandeburg0e4425e2015-11-05 17:01:01 -08002861 /* schedule our worker thread which will take care of
2862 * applying the new filter changes
2863 */
2864 i40e_service_event_schedule(vsi->back);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002865}
2866
2867/**
2868 * i40e_vlan_rx_add_vid - Add a vlan id filter to HW offload
2869 * @netdev: network interface to be adjusted
2870 * @vid: vlan id to be added
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002871 *
2872 * net_device_ops implementation for adding vlan ids
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002873 **/
2874static int i40e_vlan_rx_add_vid(struct net_device *netdev,
2875 __always_unused __be16 proto, u16 vid)
2876{
2877 struct i40e_netdev_priv *np = netdev_priv(netdev);
2878 struct i40e_vsi *vsi = np->vsi;
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002879 int ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002880
Jacob Keller6a1127852016-10-25 16:08:49 -07002881 if (vid >= VLAN_N_VID)
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002882 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002883
Jacob Kellerfcf6cfc2017-06-23 04:24:51 -04002884 ret = i40e_vsi_add_vlan(vsi, vid);
Jacob Keller6a1127852016-10-25 16:08:49 -07002885 if (!ret)
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002886 set_bit(vid, vsi->active_vlans);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002887
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002888 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002889}
2890
2891/**
2892 * i40e_vlan_rx_kill_vid - Remove a vlan id filter from HW offload
2893 * @netdev: network interface to be adjusted
2894 * @vid: vlan id to be removed
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002895 *
Akeem G Abodunrinfdfd9432014-02-11 08:24:15 +00002896 * net_device_ops implementation for removing vlan ids
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002897 **/
2898static int i40e_vlan_rx_kill_vid(struct net_device *netdev,
2899 __always_unused __be16 proto, u16 vid)
2900{
2901 struct i40e_netdev_priv *np = netdev_priv(netdev);
2902 struct i40e_vsi *vsi = np->vsi;
2903
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002904 /* return code is ignored as there is nothing a user
2905 * can do about failure to remove and a log message was
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002906 * already printed from the other function
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002907 */
2908 i40e_vsi_kill_vlan(vsi, vid);
2909
2910 clear_bit(vid, vsi->active_vlans);
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002911
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002912 return 0;
2913}
2914
2915/**
2916 * i40e_restore_vlan - Reinstate vlans when vsi/netdev comes back up
2917 * @vsi: the vsi being brought back up
2918 **/
2919static void i40e_restore_vlan(struct i40e_vsi *vsi)
2920{
2921 u16 vid;
2922
2923 if (!vsi->netdev)
2924 return;
2925
2926 i40e_vlan_rx_register(vsi->netdev, vsi->netdev->features);
2927
2928 for_each_set_bit(vid, vsi->active_vlans, VLAN_N_VID)
2929 i40e_vlan_rx_add_vid(vsi->netdev, htons(ETH_P_8021Q),
2930 vid);
2931}
2932
2933/**
2934 * i40e_vsi_add_pvid - Add pvid for the VSI
2935 * @vsi: the vsi being adjusted
2936 * @vid: the vlan id to set as a PVID
2937 **/
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002938int i40e_vsi_add_pvid(struct i40e_vsi *vsi, u16 vid)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002939{
2940 struct i40e_vsi_context ctxt;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002941 i40e_status ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002942
2943 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2944 vsi->info.pvid = cpu_to_le16(vid);
Greg Rose6c12fcb2013-11-28 06:39:34 +00002945 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_TAGGED |
2946 I40E_AQ_VSI_PVLAN_INSERT_PVID |
Greg Roseb774c7d2013-11-28 06:39:44 +00002947 I40E_AQ_VSI_PVLAN_EMOD_STR;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002948
2949 ctxt.seid = vsi->seid;
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07002950 ctxt.info = vsi->info;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002951 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2952 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002953 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002954 "add pvid failed, err %s aq_err %s\n",
2955 i40e_stat_str(&vsi->back->hw, ret),
2956 i40e_aq_str(&vsi->back->hw,
2957 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002958 return -ENOENT;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002959 }
2960
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002961 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002962}
2963
2964/**
2965 * i40e_vsi_remove_pvid - Remove the pvid from the VSI
2966 * @vsi: the vsi being adjusted
2967 *
2968 * Just use the vlan_rx_register() service to put it back to normal
2969 **/
2970void i40e_vsi_remove_pvid(struct i40e_vsi *vsi)
2971{
Greg Rose6c12fcb2013-11-28 06:39:34 +00002972 i40e_vlan_stripping_disable(vsi);
2973
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002974 vsi->info.pvid = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002975}
2976
2977/**
2978 * i40e_vsi_setup_tx_resources - Allocate VSI Tx queue resources
2979 * @vsi: ptr to the VSI
2980 *
2981 * If this function returns with an error, then it's possible one or
2982 * more of the rings is populated (while the rest are not). It is the
2983 * callers duty to clean those orphaned rings.
2984 *
2985 * Return 0 on success, negative on failure
2986 **/
2987static int i40e_vsi_setup_tx_resources(struct i40e_vsi *vsi)
2988{
2989 int i, err = 0;
2990
2991 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e152013-09-28 06:00:58 +00002992 err = i40e_setup_tx_descriptors(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002993
Björn Töpel74608d12017-05-24 07:55:35 +02002994 if (!i40e_enabled_xdp_vsi(vsi))
2995 return err;
2996
2997 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
2998 err = i40e_setup_tx_descriptors(vsi->xdp_rings[i]);
2999
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003000 return err;
3001}
3002
3003/**
3004 * i40e_vsi_free_tx_resources - Free Tx resources for VSI queues
3005 * @vsi: ptr to the VSI
3006 *
3007 * Free VSI's transmit software resources
3008 **/
3009static void i40e_vsi_free_tx_resources(struct i40e_vsi *vsi)
3010{
3011 int i;
3012
Björn Töpel74608d12017-05-24 07:55:35 +02003013 if (vsi->tx_rings) {
3014 for (i = 0; i < vsi->num_queue_pairs; i++)
3015 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
3016 i40e_free_tx_resources(vsi->tx_rings[i]);
3017 }
Greg Rose8e9dca52013-12-18 13:45:53 +00003018
Björn Töpel74608d12017-05-24 07:55:35 +02003019 if (vsi->xdp_rings) {
3020 for (i = 0; i < vsi->num_queue_pairs; i++)
3021 if (vsi->xdp_rings[i] && vsi->xdp_rings[i]->desc)
3022 i40e_free_tx_resources(vsi->xdp_rings[i]);
3023 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003024}
3025
3026/**
3027 * i40e_vsi_setup_rx_resources - Allocate VSI queues Rx resources
3028 * @vsi: ptr to the VSI
3029 *
3030 * If this function returns with an error, then it's possible one or
3031 * more of the rings is populated (while the rest are not). It is the
3032 * callers duty to clean those orphaned rings.
3033 *
3034 * Return 0 on success, negative on failure
3035 **/
3036static int i40e_vsi_setup_rx_resources(struct i40e_vsi *vsi)
3037{
3038 int i, err = 0;
3039
3040 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e152013-09-28 06:00:58 +00003041 err = i40e_setup_rx_descriptors(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003042 return err;
3043}
3044
3045/**
3046 * i40e_vsi_free_rx_resources - Free Rx Resources for VSI queues
3047 * @vsi: ptr to the VSI
3048 *
3049 * Free all receive software resources
3050 **/
3051static void i40e_vsi_free_rx_resources(struct i40e_vsi *vsi)
3052{
3053 int i;
3054
Greg Rose8e9dca52013-12-18 13:45:53 +00003055 if (!vsi->rx_rings)
3056 return;
3057
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003058 for (i = 0; i < vsi->num_queue_pairs; i++)
Greg Rose8e9dca52013-12-18 13:45:53 +00003059 if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
Alexander Duyck9f65e152013-09-28 06:00:58 +00003060 i40e_free_rx_resources(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003061}
3062
3063/**
Neerav Parikh3ffa0372014-11-12 00:19:02 +00003064 * i40e_config_xps_tx_ring - Configure XPS for a Tx ring
3065 * @ring: The Tx ring to configure
3066 *
3067 * This enables/disables XPS for a given Tx descriptor ring
3068 * based on the TCs enabled for the VSI that ring belongs to.
3069 **/
3070static void i40e_config_xps_tx_ring(struct i40e_ring *ring)
3071{
Jacob Kellerbe664cb2017-08-29 05:32:31 -04003072 int cpu;
Neerav Parikh3ffa0372014-11-12 00:19:02 +00003073
Amritha Nambiar8f88b302017-09-07 04:00:17 -07003074 if (!ring->q_vector || !ring->netdev || ring->ch)
Jesse Brandeburg9a660ee2015-02-26 16:13:22 +00003075 return;
3076
Jacob Keller6f853d42017-09-07 08:05:53 -04003077 /* We only initialize XPS once, so as not to overwrite user settings */
3078 if (test_and_set_bit(__I40E_TX_XPS_INIT_DONE, ring->state))
3079 return;
Jesse Brandeburg0e4425e2015-11-05 17:01:01 -08003080
Jacob Keller6f853d42017-09-07 08:05:53 -04003081 cpu = cpumask_local_spread(ring->q_vector->v_idx, -1);
3082 netif_set_xps_queue(ring->netdev, get_cpu_mask(cpu),
3083 ring->queue_index);
Neerav Parikh3ffa0372014-11-12 00:19:02 +00003084}
3085
3086/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003087 * i40e_configure_tx_ring - Configure a transmit ring context and rest
3088 * @ring: The Tx ring to configure
3089 *
3090 * Configure the Tx descriptor ring in the HMC context.
3091 **/
3092static int i40e_configure_tx_ring(struct i40e_ring *ring)
3093{
3094 struct i40e_vsi *vsi = ring->vsi;
3095 u16 pf_q = vsi->base_queue + ring->queue_index;
3096 struct i40e_hw *hw = &vsi->back->hw;
3097 struct i40e_hmc_obj_txq tx_ctx;
3098 i40e_status err = 0;
3099 u32 qtx_ctl = 0;
3100
3101 /* some ATR related tx ring init */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08003102 if (vsi->back->flags & I40E_FLAG_FD_ATR_ENABLED) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003103 ring->atr_sample_rate = vsi->back->atr_sample_rate;
3104 ring->atr_count = 0;
3105 } else {
3106 ring->atr_sample_rate = 0;
3107 }
3108
Neerav Parikh3ffa0372014-11-12 00:19:02 +00003109 /* configure XPS */
3110 i40e_config_xps_tx_ring(ring);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003111
3112 /* clear the context structure first */
3113 memset(&tx_ctx, 0, sizeof(tx_ctx));
3114
3115 tx_ctx.new_context = 1;
3116 tx_ctx.base = (ring->dma / 128);
3117 tx_ctx.qlen = ring->count;
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08003118 tx_ctx.fd_ena = !!(vsi->back->flags & (I40E_FLAG_FD_SB_ENABLED |
3119 I40E_FLAG_FD_ATR_ENABLED));
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003120 tx_ctx.timesync_ena = !!(vsi->back->flags & I40E_FLAG_PTP);
Jesse Brandeburg1943d8b2014-02-14 02:14:40 +00003121 /* FDIR VSI tx ring can still use RS bit and writebacks */
3122 if (vsi->type != I40E_VSI_FDIR)
3123 tx_ctx.head_wb_ena = 1;
3124 tx_ctx.head_wb_addr = ring->dma +
3125 (ring->count * sizeof(struct i40e_tx_desc));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003126
3127 /* As part of VSI creation/update, FW allocates certain
3128 * Tx arbitration queue sets for each TC enabled for
3129 * the VSI. The FW returns the handles to these queue
3130 * sets as part of the response buffer to Add VSI,
3131 * Update VSI, etc. AQ commands. It is expected that
3132 * these queue set handles be associated with the Tx
3133 * queues by the driver as part of the TX queue context
3134 * initialization. This has to be done regardless of
3135 * DCB as by default everything is mapped to TC0.
3136 */
Amritha Nambiar8f88b302017-09-07 04:00:17 -07003137
3138 if (ring->ch)
3139 tx_ctx.rdylist =
3140 le16_to_cpu(ring->ch->info.qs_handle[ring->dcb_tc]);
3141
3142 else
3143 tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[ring->dcb_tc]);
3144
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003145 tx_ctx.rdylist_act = 0;
3146
3147 /* clear the context in the HMC */
3148 err = i40e_clear_lan_tx_queue_context(hw, pf_q);
3149 if (err) {
3150 dev_info(&vsi->back->pdev->dev,
3151 "Failed to clear LAN Tx queue context on Tx ring %d (pf_q %d), error: %d\n",
3152 ring->queue_index, pf_q, err);
3153 return -ENOMEM;
3154 }
3155
3156 /* set the context in the HMC */
3157 err = i40e_set_lan_tx_queue_context(hw, pf_q, &tx_ctx);
3158 if (err) {
3159 dev_info(&vsi->back->pdev->dev,
3160 "Failed to set LAN Tx queue context on Tx ring %d (pf_q %d, error: %d\n",
3161 ring->queue_index, pf_q, err);
3162 return -ENOMEM;
3163 }
3164
3165 /* Now associate this queue with this PCI function */
Amritha Nambiar8f88b302017-09-07 04:00:17 -07003166 if (ring->ch) {
3167 if (ring->ch->type == I40E_VSI_VMDQ2)
3168 qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
3169 else
3170 return -EINVAL;
3171
3172 qtx_ctl |= (ring->ch->vsi_number <<
3173 I40E_QTX_CTL_VFVM_INDX_SHIFT) &
3174 I40E_QTX_CTL_VFVM_INDX_MASK;
Mitch Williams7a28d882014-10-17 03:14:52 +00003175 } else {
Amritha Nambiar8f88b302017-09-07 04:00:17 -07003176 if (vsi->type == I40E_VSI_VMDQ2) {
3177 qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
3178 qtx_ctl |= ((vsi->id) << I40E_QTX_CTL_VFVM_INDX_SHIFT) &
3179 I40E_QTX_CTL_VFVM_INDX_MASK;
3180 } else {
3181 qtx_ctl = I40E_QTX_CTL_PF_QUEUE;
3182 }
Mitch Williams7a28d882014-10-17 03:14:52 +00003183 }
3184
Shannon Nelson13fd9772013-09-28 07:14:19 +00003185 qtx_ctl |= ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT) &
3186 I40E_QTX_CTL_PF_INDX_MASK);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003187 wr32(hw, I40E_QTX_CTL(pf_q), qtx_ctl);
3188 i40e_flush(hw);
3189
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003190 /* cache tail off for easier writes later */
3191 ring->tail = hw->hw_addr + I40E_QTX_TAIL(pf_q);
3192
3193 return 0;
3194}
3195
3196/**
3197 * i40e_configure_rx_ring - Configure a receive ring context
3198 * @ring: The Rx ring to configure
3199 *
3200 * Configure the Rx descriptor ring in the HMC context.
3201 **/
3202static int i40e_configure_rx_ring(struct i40e_ring *ring)
3203{
3204 struct i40e_vsi *vsi = ring->vsi;
3205 u32 chain_len = vsi->back->hw.func_caps.rx_buf_chain_len;
3206 u16 pf_q = vsi->base_queue + ring->queue_index;
3207 struct i40e_hw *hw = &vsi->back->hw;
3208 struct i40e_hmc_obj_rxq rx_ctx;
3209 i40e_status err = 0;
3210
Jesse Brandeburgbd6cd4e2017-08-29 05:32:35 -04003211 bitmap_zero(ring->state, __I40E_RING_STATE_NBITS);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003212
3213 /* clear the context structure first */
3214 memset(&rx_ctx, 0, sizeof(rx_ctx));
3215
3216 ring->rx_buf_len = vsi->rx_buf_len;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003217
Alexander Duyckdab86af2017-03-14 10:15:27 -07003218 rx_ctx.dbuff = DIV_ROUND_UP(ring->rx_buf_len,
3219 BIT_ULL(I40E_RXQ_CTX_DBUFF_SHIFT));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003220
3221 rx_ctx.base = (ring->dma / 128);
3222 rx_ctx.qlen = ring->count;
3223
Jesse Brandeburgbec60fc2016-04-18 11:33:47 -07003224 /* use 32 byte descriptors */
3225 rx_ctx.dsize = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003226
Jesse Brandeburgbec60fc2016-04-18 11:33:47 -07003227 /* descriptor type is always zero
3228 * rx_ctx.dtype = 0;
3229 */
Jesse Brandeburgb32bfa172016-04-18 11:33:42 -07003230 rx_ctx.hsplit_0 = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003231
Jesse Brandeburgb32bfa172016-04-18 11:33:42 -07003232 rx_ctx.rxmax = min_t(u16, vsi->max_frame, chain_len * ring->rx_buf_len);
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00003233 if (hw->revision_id == 0)
3234 rx_ctx.lrxqthresh = 0;
3235 else
Jacob Keller7362be92017-09-07 08:05:50 -04003236 rx_ctx.lrxqthresh = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003237 rx_ctx.crcstrip = 1;
3238 rx_ctx.l2tsel = 1;
Jesse Brandeburgc4bbac32015-09-28 11:21:48 -07003239 /* this controls whether VLAN is stripped from inner headers */
3240 rx_ctx.showiv = 0;
Catherine Sullivanacb36762014-03-06 09:02:30 +00003241 /* set the prefena field to 1 because the manual says to */
3242 rx_ctx.prefena = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003243
3244 /* clear the context in the HMC */
3245 err = i40e_clear_lan_rx_queue_context(hw, pf_q);
3246 if (err) {
3247 dev_info(&vsi->back->pdev->dev,
3248 "Failed to clear LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
3249 ring->queue_index, pf_q, err);
3250 return -ENOMEM;
3251 }
3252
3253 /* set the context in the HMC */
3254 err = i40e_set_lan_rx_queue_context(hw, pf_q, &rx_ctx);
3255 if (err) {
3256 dev_info(&vsi->back->pdev->dev,
3257 "Failed to set LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
3258 ring->queue_index, pf_q, err);
3259 return -ENOMEM;
3260 }
3261
Alexander Duyckca9ec082017-04-05 07:51:02 -04003262 /* configure Rx buffer alignment */
3263 if (!vsi->netdev || (vsi->back->flags & I40E_FLAG_LEGACY_RX))
3264 clear_ring_build_skb_enabled(ring);
3265 else
3266 set_ring_build_skb_enabled(ring);
3267
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003268 /* cache tail for quicker writes, and clear the reg before use */
3269 ring->tail = hw->hw_addr + I40E_QRX_TAIL(pf_q);
3270 writel(0, ring->tail);
3271
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07003272 i40e_alloc_rx_buffers(ring, I40E_DESC_UNUSED(ring));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003273
3274 return 0;
3275}
3276
3277/**
3278 * i40e_vsi_configure_tx - Configure the VSI for Tx
3279 * @vsi: VSI structure describing this set of rings and resources
3280 *
3281 * Configure the Tx VSI for operation.
3282 **/
3283static int i40e_vsi_configure_tx(struct i40e_vsi *vsi)
3284{
3285 int err = 0;
3286 u16 i;
3287
Alexander Duyck9f65e152013-09-28 06:00:58 +00003288 for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
3289 err = i40e_configure_tx_ring(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003290
Björn Töpel74608d12017-05-24 07:55:35 +02003291 if (!i40e_enabled_xdp_vsi(vsi))
3292 return err;
3293
3294 for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
3295 err = i40e_configure_tx_ring(vsi->xdp_rings[i]);
3296
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003297 return err;
3298}
3299
3300/**
3301 * i40e_vsi_configure_rx - Configure the VSI for Rx
3302 * @vsi: the VSI being configured
3303 *
3304 * Configure the Rx VSI for operation.
3305 **/
3306static int i40e_vsi_configure_rx(struct i40e_vsi *vsi)
3307{
3308 int err = 0;
3309 u16 i;
3310
Alexander Duyckdab86af2017-03-14 10:15:27 -07003311 if (!vsi->netdev || (vsi->back->flags & I40E_FLAG_LEGACY_RX)) {
3312 vsi->max_frame = I40E_MAX_RXBUFFER;
3313 vsi->rx_buf_len = I40E_RXBUFFER_2048;
3314#if (PAGE_SIZE < 8192)
Alexander Duyckca9ec082017-04-05 07:51:02 -04003315 } else if (!I40E_2K_TOO_SMALL_WITH_PADDING &&
3316 (vsi->netdev->mtu <= ETH_DATA_LEN)) {
Alexander Duyckdab86af2017-03-14 10:15:27 -07003317 vsi->max_frame = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
3318 vsi->rx_buf_len = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
3319#endif
3320 } else {
3321 vsi->max_frame = I40E_MAX_RXBUFFER;
Alexander Duyck98efd692017-04-05 07:51:01 -04003322 vsi->rx_buf_len = (PAGE_SIZE < 8192) ? I40E_RXBUFFER_3072 :
3323 I40E_RXBUFFER_2048;
Alexander Duyckdab86af2017-03-14 10:15:27 -07003324 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003325
3326 /* set up individual rings */
3327 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e152013-09-28 06:00:58 +00003328 err = i40e_configure_rx_ring(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003329
3330 return err;
3331}
3332
3333/**
3334 * i40e_vsi_config_dcb_rings - Update rings to reflect DCB TC
3335 * @vsi: ptr to the VSI
3336 **/
3337static void i40e_vsi_config_dcb_rings(struct i40e_vsi *vsi)
3338{
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +00003339 struct i40e_ring *tx_ring, *rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003340 u16 qoffset, qcount;
3341 int i, n;
3342
Parikh, Neeravcd238a32015-02-21 06:43:37 +00003343 if (!(vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
3344 /* Reset the TC information */
3345 for (i = 0; i < vsi->num_queue_pairs; i++) {
3346 rx_ring = vsi->rx_rings[i];
3347 tx_ring = vsi->tx_rings[i];
3348 rx_ring->dcb_tc = 0;
3349 tx_ring->dcb_tc = 0;
3350 }
Amritha Nambiara9ce82f2017-09-07 04:00:22 -07003351 return;
Parikh, Neeravcd238a32015-02-21 06:43:37 +00003352 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003353
3354 for (n = 0; n < I40E_MAX_TRAFFIC_CLASS; n++) {
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04003355 if (!(vsi->tc_config.enabled_tc & BIT_ULL(n)))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003356 continue;
3357
3358 qoffset = vsi->tc_config.tc_info[n].qoffset;
3359 qcount = vsi->tc_config.tc_info[n].qcount;
3360 for (i = qoffset; i < (qoffset + qcount); i++) {
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +00003361 rx_ring = vsi->rx_rings[i];
3362 tx_ring = vsi->tx_rings[i];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003363 rx_ring->dcb_tc = n;
3364 tx_ring->dcb_tc = n;
3365 }
3366 }
3367}
3368
3369/**
3370 * i40e_set_vsi_rx_mode - Call set_rx_mode on a VSI
3371 * @vsi: ptr to the VSI
3372 **/
3373static void i40e_set_vsi_rx_mode(struct i40e_vsi *vsi)
3374{
3375 if (vsi->netdev)
3376 i40e_set_rx_mode(vsi->netdev);
3377}
3378
3379/**
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00003380 * i40e_fdir_filter_restore - Restore the Sideband Flow Director filters
3381 * @vsi: Pointer to the targeted VSI
3382 *
3383 * This function replays the hlist on the hw where all the SB Flow Director
3384 * filters were saved.
3385 **/
3386static void i40e_fdir_filter_restore(struct i40e_vsi *vsi)
3387{
3388 struct i40e_fdir_filter *filter;
3389 struct i40e_pf *pf = vsi->back;
3390 struct hlist_node *node;
3391
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00003392 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
3393 return;
3394
Jacob Keller6d069422017-02-06 14:38:44 -08003395 /* Reset FDir counters as we're replaying all existing filters */
Jacob Keller097dbf52017-02-06 14:38:46 -08003396 pf->fd_tcp4_filter_cnt = 0;
3397 pf->fd_udp4_filter_cnt = 0;
Jacob Kellerf223c872017-02-06 14:38:51 -08003398 pf->fd_sctp4_filter_cnt = 0;
Jacob Keller097dbf52017-02-06 14:38:46 -08003399 pf->fd_ip4_filter_cnt = 0;
Jacob Keller6d069422017-02-06 14:38:44 -08003400
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00003401 hlist_for_each_entry_safe(filter, node,
3402 &pf->fdir_filter_list, fdir_node) {
3403 i40e_add_del_fdir(vsi, filter, true);
3404 }
3405}
3406
3407/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003408 * i40e_vsi_configure - Set up the VSI for action
3409 * @vsi: the VSI being configured
3410 **/
3411static int i40e_vsi_configure(struct i40e_vsi *vsi)
3412{
3413 int err;
3414
3415 i40e_set_vsi_rx_mode(vsi);
3416 i40e_restore_vlan(vsi);
3417 i40e_vsi_config_dcb_rings(vsi);
3418 err = i40e_vsi_configure_tx(vsi);
3419 if (!err)
3420 err = i40e_vsi_configure_rx(vsi);
3421
3422 return err;
3423}
3424
3425/**
3426 * i40e_vsi_configure_msix - MSIX mode Interrupt Config in the HW
3427 * @vsi: the VSI being configured
3428 **/
3429static void i40e_vsi_configure_msix(struct i40e_vsi *vsi)
3430{
Björn Töpel74608d12017-05-24 07:55:35 +02003431 bool has_xdp = i40e_enabled_xdp_vsi(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003432 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003433 struct i40e_hw *hw = &pf->hw;
3434 u16 vector;
3435 int i, q;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003436 u32 qp;
3437
3438 /* The interrupt indexing is offset by 1 in the PFINT_ITRn
3439 * and PFINT_LNKLSTn registers, e.g.:
3440 * PFINT_ITRn[0..n-1] gets msix-1..msix-n (qpair interrupts)
3441 */
3442 qp = vsi->base_queue;
3443 vector = vsi->base_vector;
Alexander Duyck493fb302013-09-28 07:01:44 +00003444 for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
Jesse Brandeburgac26fc12015-09-28 14:12:37 -04003445 struct i40e_q_vector *q_vector = vsi->q_vectors[i];
3446
Jesse Brandeburgee2319c2015-09-28 14:16:54 -04003447 q_vector->itr_countdown = ITR_COUNTDOWN_START;
Kan Lianga75e8002016-02-19 09:24:04 -05003448 q_vector->rx.itr = ITR_TO_REG(vsi->rx_rings[i]->rx_itr_setting);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003449 q_vector->rx.latency_range = I40E_LOW_LATENCY;
3450 wr32(hw, I40E_PFINT_ITRN(I40E_RX_ITR, vector - 1),
3451 q_vector->rx.itr);
Kan Lianga75e8002016-02-19 09:24:04 -05003452 q_vector->tx.itr = ITR_TO_REG(vsi->tx_rings[i]->tx_itr_setting);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003453 q_vector->tx.latency_range = I40E_LOW_LATENCY;
3454 wr32(hw, I40E_PFINT_ITRN(I40E_TX_ITR, vector - 1),
3455 q_vector->tx.itr);
Jesse Brandeburgac26fc12015-09-28 14:12:37 -04003456 wr32(hw, I40E_PFINT_RATEN(vector - 1),
Alan Brady1c0e6a32016-11-28 16:06:02 -08003457 i40e_intrl_usec_to_reg(vsi->int_rate_limit));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003458
3459 /* Linked list for the queuepairs assigned to this vector */
3460 wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), qp);
3461 for (q = 0; q < q_vector->num_ringpairs; q++) {
Björn Töpel74608d12017-05-24 07:55:35 +02003462 u32 nextqp = has_xdp ? qp + vsi->alloc_queue_pairs : qp;
Jesse Brandeburgac26fc12015-09-28 14:12:37 -04003463 u32 val;
3464
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003465 val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
Björn Töpel74608d12017-05-24 07:55:35 +02003466 (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
3467 (vector << I40E_QINT_RQCTL_MSIX_INDX_SHIFT) |
3468 (nextqp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT) |
3469 (I40E_QUEUE_TYPE_TX <<
3470 I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003471
3472 wr32(hw, I40E_QINT_RQCTL(qp), val);
3473
Björn Töpel74608d12017-05-24 07:55:35 +02003474 if (has_xdp) {
3475 val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3476 (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3477 (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
3478 (qp << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) |
3479 (I40E_QUEUE_TYPE_TX <<
3480 I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3481
3482 wr32(hw, I40E_QINT_TQCTL(nextqp), val);
3483 }
3484
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003485 val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
Björn Töpel74608d12017-05-24 07:55:35 +02003486 (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3487 (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
3488 ((qp + 1) << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) |
3489 (I40E_QUEUE_TYPE_RX <<
3490 I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003491
3492 /* Terminate the linked list */
3493 if (q == (q_vector->num_ringpairs - 1))
Björn Töpel74608d12017-05-24 07:55:35 +02003494 val |= (I40E_QUEUE_END_OF_LIST <<
3495 I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003496
3497 wr32(hw, I40E_QINT_TQCTL(qp), val);
3498 qp++;
3499 }
3500 }
3501
3502 i40e_flush(hw);
3503}
3504
3505/**
3506 * i40e_enable_misc_int_causes - enable the non-queue interrupts
3507 * @hw: ptr to the hardware info
3508 **/
Jacob Kellerab437b52014-12-14 01:55:08 +00003509static void i40e_enable_misc_int_causes(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003510{
Jacob Kellerab437b52014-12-14 01:55:08 +00003511 struct i40e_hw *hw = &pf->hw;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003512 u32 val;
3513
3514 /* clear things first */
3515 wr32(hw, I40E_PFINT_ICR0_ENA, 0); /* disable all */
3516 rd32(hw, I40E_PFINT_ICR0); /* read to clear */
3517
3518 val = I40E_PFINT_ICR0_ENA_ECC_ERR_MASK |
3519 I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK |
3520 I40E_PFINT_ICR0_ENA_GRST_MASK |
3521 I40E_PFINT_ICR0_ENA_PCI_EXCEPTION_MASK |
3522 I40E_PFINT_ICR0_ENA_GPIO_MASK |
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003523 I40E_PFINT_ICR0_ENA_HMC_ERR_MASK |
3524 I40E_PFINT_ICR0_ENA_VFLR_MASK |
3525 I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
3526
Anjali Singhai Jain0d8e1432015-06-05 12:20:32 -04003527 if (pf->flags & I40E_FLAG_IWARP_ENABLED)
3528 val |= I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
3529
Jacob Kellerab437b52014-12-14 01:55:08 +00003530 if (pf->flags & I40E_FLAG_PTP)
3531 val |= I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
3532
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003533 wr32(hw, I40E_PFINT_ICR0_ENA, val);
3534
3535 /* SW_ITR_IDX = 0, but don't change INTENA */
Anjali Singhai Jain84ed40e2013-11-26 10:49:32 +00003536 wr32(hw, I40E_PFINT_DYN_CTL0, I40E_PFINT_DYN_CTL0_SW_ITR_INDX_MASK |
3537 I40E_PFINT_DYN_CTL0_INTENA_MSK_MASK);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003538
3539 /* OTHER_ITR_IDX = 0 */
3540 wr32(hw, I40E_PFINT_STAT_CTL0, 0);
3541}
3542
3543/**
3544 * i40e_configure_msi_and_legacy - Legacy mode interrupt config in the HW
3545 * @vsi: the VSI being configured
3546 **/
3547static void i40e_configure_msi_and_legacy(struct i40e_vsi *vsi)
3548{
Björn Töpel74608d12017-05-24 07:55:35 +02003549 u32 nextqp = i40e_enabled_xdp_vsi(vsi) ? vsi->alloc_queue_pairs : 0;
Alexander Duyck493fb302013-09-28 07:01:44 +00003550 struct i40e_q_vector *q_vector = vsi->q_vectors[0];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003551 struct i40e_pf *pf = vsi->back;
3552 struct i40e_hw *hw = &pf->hw;
3553 u32 val;
3554
3555 /* set the ITR configuration */
Jesse Brandeburgee2319c2015-09-28 14:16:54 -04003556 q_vector->itr_countdown = ITR_COUNTDOWN_START;
Kan Lianga75e8002016-02-19 09:24:04 -05003557 q_vector->rx.itr = ITR_TO_REG(vsi->rx_rings[0]->rx_itr_setting);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003558 q_vector->rx.latency_range = I40E_LOW_LATENCY;
3559 wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), q_vector->rx.itr);
Kan Lianga75e8002016-02-19 09:24:04 -05003560 q_vector->tx.itr = ITR_TO_REG(vsi->tx_rings[0]->tx_itr_setting);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003561 q_vector->tx.latency_range = I40E_LOW_LATENCY;
3562 wr32(hw, I40E_PFINT_ITR0(I40E_TX_ITR), q_vector->tx.itr);
3563
Jacob Kellerab437b52014-12-14 01:55:08 +00003564 i40e_enable_misc_int_causes(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003565
3566 /* FIRSTQ_INDX = 0, FIRSTQ_TYPE = 0 (rx) */
3567 wr32(hw, I40E_PFINT_LNKLST0, 0);
3568
Jesse Brandeburgf29eaa32014-02-11 08:24:12 +00003569 /* Associate the queue pair to the vector and enable the queue int */
Björn Töpel74608d12017-05-24 07:55:35 +02003570 val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3571 (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
3572 (nextqp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT)|
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003573 (I40E_QUEUE_TYPE_TX << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3574
3575 wr32(hw, I40E_QINT_RQCTL(0), val);
3576
Björn Töpel74608d12017-05-24 07:55:35 +02003577 if (i40e_enabled_xdp_vsi(vsi)) {
3578 val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3579 (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT)|
3580 (I40E_QUEUE_TYPE_TX
3581 << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3582
3583 wr32(hw, I40E_QINT_TQCTL(nextqp), val);
3584 }
3585
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003586 val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3587 (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3588 (I40E_QUEUE_END_OF_LIST << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
3589
3590 wr32(hw, I40E_QINT_TQCTL(0), val);
3591 i40e_flush(hw);
3592}
3593
3594/**
Mitch Williams2ef28cf2013-11-28 06:39:32 +00003595 * i40e_irq_dynamic_disable_icr0 - Disable default interrupt generation for icr0
3596 * @pf: board private structure
3597 **/
3598void i40e_irq_dynamic_disable_icr0(struct i40e_pf *pf)
3599{
3600 struct i40e_hw *hw = &pf->hw;
3601
3602 wr32(hw, I40E_PFINT_DYN_CTL0,
3603 I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
3604 i40e_flush(hw);
3605}
3606
3607/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003608 * i40e_irq_dynamic_enable_icr0 - Enable default interrupt generation for icr0
3609 * @pf: board private structure
3610 **/
Jacob Kellerdbadbbe2017-09-07 08:05:49 -04003611void i40e_irq_dynamic_enable_icr0(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003612{
3613 struct i40e_hw *hw = &pf->hw;
3614 u32 val;
3615
3616 val = I40E_PFINT_DYN_CTL0_INTENA_MASK |
Jacob Kellerdbadbbe2017-09-07 08:05:49 -04003617 I40E_PFINT_DYN_CTL0_CLEARPBA_MASK |
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003618 (I40E_ITR_NONE << I40E_PFINT_DYN_CTL0_ITR_INDX_SHIFT);
3619
3620 wr32(hw, I40E_PFINT_DYN_CTL0, val);
3621 i40e_flush(hw);
3622}
3623
3624/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003625 * i40e_msix_clean_rings - MSIX mode Interrupt Handler
3626 * @irq: interrupt number
3627 * @data: pointer to a q_vector
3628 **/
3629static irqreturn_t i40e_msix_clean_rings(int irq, void *data)
3630{
3631 struct i40e_q_vector *q_vector = data;
3632
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003633 if (!q_vector->tx.ring && !q_vector->rx.ring)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003634 return IRQ_HANDLED;
3635
Alexander Duyck5d3465a2015-09-29 15:19:50 -07003636 napi_schedule_irqoff(&q_vector->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003637
3638 return IRQ_HANDLED;
3639}
3640
3641/**
Alan Brady96db7762016-09-14 16:24:38 -07003642 * i40e_irq_affinity_notify - Callback for affinity changes
3643 * @notify: context as to what irq was changed
3644 * @mask: the new affinity mask
3645 *
3646 * This is a callback function used by the irq_set_affinity_notifier function
3647 * so that we may register to receive changes to the irq affinity masks.
3648 **/
3649static void i40e_irq_affinity_notify(struct irq_affinity_notify *notify,
3650 const cpumask_t *mask)
3651{
3652 struct i40e_q_vector *q_vector =
3653 container_of(notify, struct i40e_q_vector, affinity_notify);
3654
Jacob Keller7e4d01e2017-07-12 05:46:05 -04003655 cpumask_copy(&q_vector->affinity_mask, mask);
Alan Brady96db7762016-09-14 16:24:38 -07003656}
3657
3658/**
3659 * i40e_irq_affinity_release - Callback for affinity notifier release
3660 * @ref: internal core kernel usage
3661 *
3662 * This is a callback function used by the irq_set_affinity_notifier function
3663 * to inform the current notification subscriber that they will no longer
3664 * receive notifications.
3665 **/
3666static void i40e_irq_affinity_release(struct kref *ref) {}
3667
3668/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003669 * i40e_vsi_request_irq_msix - Initialize MSI-X interrupts
3670 * @vsi: the VSI being configured
3671 * @basename: name for the vector
3672 *
3673 * Allocates MSI-X vectors and requests interrupts from the kernel.
3674 **/
3675static int i40e_vsi_request_irq_msix(struct i40e_vsi *vsi, char *basename)
3676{
3677 int q_vectors = vsi->num_q_vectors;
3678 struct i40e_pf *pf = vsi->back;
3679 int base = vsi->base_vector;
3680 int rx_int_idx = 0;
3681 int tx_int_idx = 0;
3682 int vector, err;
Alan Brady96db7762016-09-14 16:24:38 -07003683 int irq_num;
Jacob Kellerbe664cb2017-08-29 05:32:31 -04003684 int cpu;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003685
3686 for (vector = 0; vector < q_vectors; vector++) {
Alexander Duyck493fb302013-09-28 07:01:44 +00003687 struct i40e_q_vector *q_vector = vsi->q_vectors[vector];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003688
Alan Brady96db7762016-09-14 16:24:38 -07003689 irq_num = pf->msix_entries[base + vector].vector;
3690
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003691 if (q_vector->tx.ring && q_vector->rx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003692 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3693 "%s-%s-%d", basename, "TxRx", rx_int_idx++);
3694 tx_int_idx++;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003695 } else if (q_vector->rx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003696 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3697 "%s-%s-%d", basename, "rx", rx_int_idx++);
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003698 } else if (q_vector->tx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003699 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3700 "%s-%s-%d", basename, "tx", tx_int_idx++);
3701 } else {
3702 /* skip this unused q_vector */
3703 continue;
3704 }
Alan Brady96db7762016-09-14 16:24:38 -07003705 err = request_irq(irq_num,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003706 vsi->irq_handler,
3707 0,
3708 q_vector->name,
3709 q_vector);
3710 if (err) {
3711 dev_info(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04003712 "MSIX request_irq failed, error: %d\n", err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003713 goto free_queue_irqs;
3714 }
Alan Brady96db7762016-09-14 16:24:38 -07003715
3716 /* register for affinity change notifications */
3717 q_vector->affinity_notify.notify = i40e_irq_affinity_notify;
3718 q_vector->affinity_notify.release = i40e_irq_affinity_release;
3719 irq_set_affinity_notifier(irq_num, &q_vector->affinity_notify);
Jacob Kellerbe664cb2017-08-29 05:32:31 -04003720 /* Spread affinity hints out across online CPUs.
3721 *
3722 * get_cpu_mask returns a static constant mask with
3723 * a permanent lifetime so it's ok to pass to
3724 * irq_set_affinity_hint without making a copy.
Jacob Keller759dc4a2017-07-14 09:10:10 -04003725 */
Jacob Kellerbe664cb2017-08-29 05:32:31 -04003726 cpu = cpumask_local_spread(q_vector->v_idx, -1);
3727 irq_set_affinity_hint(irq_num, get_cpu_mask(cpu));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003728 }
3729
Shannon Nelson63741842014-04-23 04:50:16 +00003730 vsi->irqs_ready = true;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003731 return 0;
3732
3733free_queue_irqs:
3734 while (vector) {
3735 vector--;
Alan Brady96db7762016-09-14 16:24:38 -07003736 irq_num = pf->msix_entries[base + vector].vector;
3737 irq_set_affinity_notifier(irq_num, NULL);
3738 irq_set_affinity_hint(irq_num, NULL);
3739 free_irq(irq_num, &vsi->q_vectors[vector]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003740 }
3741 return err;
3742}
3743
3744/**
3745 * i40e_vsi_disable_irq - Mask off queue interrupt generation on the VSI
3746 * @vsi: the VSI being un-configured
3747 **/
3748static void i40e_vsi_disable_irq(struct i40e_vsi *vsi)
3749{
3750 struct i40e_pf *pf = vsi->back;
3751 struct i40e_hw *hw = &pf->hw;
3752 int base = vsi->base_vector;
3753 int i;
3754
Shannon Nelson2e5c26ea2017-06-07 05:43:11 -04003755 /* disable interrupt causation from each queue */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003756 for (i = 0; i < vsi->num_queue_pairs; i++) {
Shannon Nelson2e5c26ea2017-06-07 05:43:11 -04003757 u32 val;
3758
3759 val = rd32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx));
3760 val &= ~I40E_QINT_TQCTL_CAUSE_ENA_MASK;
3761 wr32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx), val);
3762
3763 val = rd32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx));
3764 val &= ~I40E_QINT_RQCTL_CAUSE_ENA_MASK;
3765 wr32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx), val);
3766
Björn Töpel74608d12017-05-24 07:55:35 +02003767 if (!i40e_enabled_xdp_vsi(vsi))
3768 continue;
3769 wr32(hw, I40E_QINT_TQCTL(vsi->xdp_rings[i]->reg_idx), 0);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003770 }
3771
Shannon Nelson2e5c26ea2017-06-07 05:43:11 -04003772 /* disable each interrupt */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003773 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3774 for (i = vsi->base_vector;
3775 i < (vsi->num_q_vectors + vsi->base_vector); i++)
3776 wr32(hw, I40E_PFINT_DYN_CTLN(i - 1), 0);
3777
3778 i40e_flush(hw);
3779 for (i = 0; i < vsi->num_q_vectors; i++)
3780 synchronize_irq(pf->msix_entries[i + base].vector);
3781 } else {
3782 /* Legacy and MSI mode - this stops all interrupt handling */
3783 wr32(hw, I40E_PFINT_ICR0_ENA, 0);
3784 wr32(hw, I40E_PFINT_DYN_CTL0, 0);
3785 i40e_flush(hw);
3786 synchronize_irq(pf->pdev->irq);
3787 }
3788}
3789
3790/**
3791 * i40e_vsi_enable_irq - Enable IRQ for the given VSI
3792 * @vsi: the VSI being configured
3793 **/
3794static int i40e_vsi_enable_irq(struct i40e_vsi *vsi)
3795{
3796 struct i40e_pf *pf = vsi->back;
3797 int i;
3798
3799 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
Jesse Brandeburg78455482015-07-23 16:54:41 -04003800 for (i = 0; i < vsi->num_q_vectors; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003801 i40e_irq_dynamic_enable(vsi, i);
3802 } else {
Jacob Kellerdbadbbe2017-09-07 08:05:49 -04003803 i40e_irq_dynamic_enable_icr0(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003804 }
3805
Jesse Brandeburg1022cb62013-09-28 07:13:08 +00003806 i40e_flush(&pf->hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003807 return 0;
3808}
3809
3810/**
Jacob Kellerc17401a2017-07-14 09:27:02 -04003811 * i40e_free_misc_vector - Free the vector that handles non-queue events
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003812 * @pf: board private structure
3813 **/
Jacob Kellerc17401a2017-07-14 09:27:02 -04003814static void i40e_free_misc_vector(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003815{
3816 /* Disable ICR 0 */
3817 wr32(&pf->hw, I40E_PFINT_ICR0_ENA, 0);
3818 i40e_flush(&pf->hw);
Jacob Kellerc17401a2017-07-14 09:27:02 -04003819
3820 if (pf->flags & I40E_FLAG_MSIX_ENABLED && pf->msix_entries) {
3821 synchronize_irq(pf->msix_entries[0].vector);
3822 free_irq(pf->msix_entries[0].vector, pf);
3823 clear_bit(__I40E_MISC_IRQ_REQUESTED, pf->state);
3824 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003825}
3826
3827/**
3828 * i40e_intr - MSI/Legacy and non-queue interrupt handler
3829 * @irq: interrupt number
3830 * @data: pointer to a q_vector
3831 *
3832 * This is the handler used for all MSI/Legacy interrupts, and deals
3833 * with both queue and non-queue interrupts. This is also used in
3834 * MSIX mode to handle the non-queue interrupts.
3835 **/
3836static irqreturn_t i40e_intr(int irq, void *data)
3837{
3838 struct i40e_pf *pf = (struct i40e_pf *)data;
3839 struct i40e_hw *hw = &pf->hw;
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003840 irqreturn_t ret = IRQ_NONE;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003841 u32 icr0, icr0_remaining;
3842 u32 val, ena_mask;
3843
3844 icr0 = rd32(hw, I40E_PFINT_ICR0);
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003845 ena_mask = rd32(hw, I40E_PFINT_ICR0_ENA);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003846
Shannon Nelson116a57d2013-09-28 07:13:59 +00003847 /* if sharing a legacy IRQ, we might get called w/o an intr pending */
3848 if ((icr0 & I40E_PFINT_ICR0_INTEVENT_MASK) == 0)
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003849 goto enable_intr;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003850
Shannon Nelsoncd92e722013-11-16 10:00:44 +00003851 /* if interrupt but no bits showing, must be SWINT */
3852 if (((icr0 & ~I40E_PFINT_ICR0_INTEVENT_MASK) == 0) ||
3853 (icr0 & I40E_PFINT_ICR0_SWINT_MASK))
3854 pf->sw_int_count++;
3855
Anjali Singhai Jain0d8e1432015-06-05 12:20:32 -04003856 if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
Catherine Sullivan76429842017-06-07 05:43:12 -04003857 (icr0 & I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK)) {
Anjali Singhai Jain0d8e1432015-06-05 12:20:32 -04003858 ena_mask &= ~I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
Carolyn Wyborny23bb6dc2016-11-08 13:05:12 -08003859 dev_dbg(&pf->pdev->dev, "cleared PE_CRITERR\n");
Catherine Sullivan76429842017-06-07 05:43:12 -04003860 set_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
Anjali Singhai Jain0d8e1432015-06-05 12:20:32 -04003861 }
3862
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003863 /* only q0 is used in MSI/Legacy mode, and none are used in MSIX */
3864 if (icr0 & I40E_PFINT_ICR0_QUEUE_0_MASK) {
Alexander Duyck5d3465a2015-09-29 15:19:50 -07003865 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
3866 struct i40e_q_vector *q_vector = vsi->q_vectors[0];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003867
Anjali Singhai Jaina16ae2d2016-01-15 14:33:16 -08003868 /* We do not have a way to disarm Queue causes while leaving
3869 * interrupt enabled for all other causes, ideally
3870 * interrupt should be disabled while we are in NAPI but
3871 * this is not a performance path and napi_schedule()
3872 * can deal with rescheduling.
3873 */
Mauro S. M. Rodrigues9e6c9c02017-05-12 23:26:56 -03003874 if (!test_bit(__I40E_DOWN, pf->state))
Alexander Duyck5d3465a2015-09-29 15:19:50 -07003875 napi_schedule_irqoff(&q_vector->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003876 }
3877
3878 if (icr0 & I40E_PFINT_ICR0_ADMINQ_MASK) {
3879 ena_mask &= ~I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
Jacob Keller0da36b92017-04-19 09:25:55 -04003880 set_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state);
Shannon Nelson6e93d0c2016-01-15 14:33:18 -08003881 i40e_debug(&pf->hw, I40E_DEBUG_NVM, "AdminQ event\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003882 }
3883
3884 if (icr0 & I40E_PFINT_ICR0_MAL_DETECT_MASK) {
3885 ena_mask &= ~I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
Jacob Keller0da36b92017-04-19 09:25:55 -04003886 set_bit(__I40E_MDD_EVENT_PENDING, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003887 }
3888
3889 if (icr0 & I40E_PFINT_ICR0_VFLR_MASK) {
3890 ena_mask &= ~I40E_PFINT_ICR0_ENA_VFLR_MASK;
Jacob Keller0da36b92017-04-19 09:25:55 -04003891 set_bit(__I40E_VFLR_EVENT_PENDING, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003892 }
3893
3894 if (icr0 & I40E_PFINT_ICR0_GRST_MASK) {
Jacob Keller0da36b92017-04-19 09:25:55 -04003895 if (!test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
3896 set_bit(__I40E_RESET_INTR_RECEIVED, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003897 ena_mask &= ~I40E_PFINT_ICR0_ENA_GRST_MASK;
3898 val = rd32(hw, I40E_GLGEN_RSTAT);
3899 val = (val & I40E_GLGEN_RSTAT_RESET_TYPE_MASK)
3900 >> I40E_GLGEN_RSTAT_RESET_TYPE_SHIFT;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003901 if (val == I40E_RESET_CORER) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003902 pf->corer_count++;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003903 } else if (val == I40E_RESET_GLOBR) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003904 pf->globr_count++;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003905 } else if (val == I40E_RESET_EMPR) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003906 pf->empr_count++;
Jacob Keller0da36b92017-04-19 09:25:55 -04003907 set_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state);
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003908 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003909 }
3910
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003911 if (icr0 & I40E_PFINT_ICR0_HMC_ERR_MASK) {
3912 icr0 &= ~I40E_PFINT_ICR0_HMC_ERR_MASK;
3913 dev_info(&pf->pdev->dev, "HMC error interrupt\n");
Anjali Singhai Jain25fc0e62015-03-31 00:45:01 -07003914 dev_info(&pf->pdev->dev, "HMC error info 0x%x, HMC error data 0x%x\n",
3915 rd32(hw, I40E_PFHMC_ERRORINFO),
3916 rd32(hw, I40E_PFHMC_ERRORDATA));
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003917 }
3918
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003919 if (icr0 & I40E_PFINT_ICR0_TIMESYNC_MASK) {
3920 u32 prttsyn_stat = rd32(hw, I40E_PRTTSYN_STAT_0);
3921
3922 if (prttsyn_stat & I40E_PRTTSYN_STAT_0_TXTIME_MASK) {
Jacob Kellercafa1fc2014-04-24 18:05:03 -07003923 icr0 &= ~I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003924 i40e_ptp_tx_hwtstamp(pf);
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003925 }
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003926 }
3927
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003928 /* If a critical error is pending we have no choice but to reset the
3929 * device.
3930 * Report and mask out any remaining unexpected interrupts.
3931 */
3932 icr0_remaining = icr0 & ena_mask;
3933 if (icr0_remaining) {
3934 dev_info(&pf->pdev->dev, "unhandled interrupt icr0=0x%08x\n",
3935 icr0_remaining);
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003936 if ((icr0_remaining & I40E_PFINT_ICR0_PE_CRITERR_MASK) ||
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003937 (icr0_remaining & I40E_PFINT_ICR0_PCI_EXCEPTION_MASK) ||
Anjali Singhai Jainc0c28972014-02-12 01:45:34 +00003938 (icr0_remaining & I40E_PFINT_ICR0_ECC_ERR_MASK)) {
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003939 dev_info(&pf->pdev->dev, "device will be reset\n");
Jacob Keller0da36b92017-04-19 09:25:55 -04003940 set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003941 i40e_service_event_schedule(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003942 }
3943 ena_mask &= ~icr0_remaining;
3944 }
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003945 ret = IRQ_HANDLED;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003946
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003947enable_intr:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003948 /* re-enable interrupt causes */
3949 wr32(hw, I40E_PFINT_ICR0_ENA, ena_mask);
Mauro S. M. Rodrigues9e6c9c02017-05-12 23:26:56 -03003950 if (!test_bit(__I40E_DOWN, pf->state)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003951 i40e_service_event_schedule(pf);
Jacob Kellerdbadbbe2017-09-07 08:05:49 -04003952 i40e_irq_dynamic_enable_icr0(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003953 }
3954
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003955 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003956}
3957
3958/**
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003959 * i40e_clean_fdir_tx_irq - Reclaim resources after transmit completes
3960 * @tx_ring: tx ring to clean
3961 * @budget: how many cleans we're allowed
3962 *
3963 * Returns true if there's any budget left (e.g. the clean is finished)
3964 **/
3965static bool i40e_clean_fdir_tx_irq(struct i40e_ring *tx_ring, int budget)
3966{
3967 struct i40e_vsi *vsi = tx_ring->vsi;
3968 u16 i = tx_ring->next_to_clean;
3969 struct i40e_tx_buffer *tx_buf;
3970 struct i40e_tx_desc *tx_desc;
3971
3972 tx_buf = &tx_ring->tx_bi[i];
3973 tx_desc = I40E_TX_DESC(tx_ring, i);
3974 i -= tx_ring->count;
3975
3976 do {
3977 struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
3978
3979 /* if next_to_watch is not set then there is no work pending */
3980 if (!eop_desc)
3981 break;
3982
3983 /* prevent any other reads prior to eop_desc */
Brian King52c69122017-11-17 11:05:44 -06003984 smp_rmb();
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003985
3986 /* if the descriptor isn't done, no work yet to do */
3987 if (!(eop_desc->cmd_type_offset_bsz &
3988 cpu_to_le64(I40E_TX_DESC_DTYPE_DESC_DONE)))
3989 break;
3990
3991 /* clear next_to_watch to prevent false hangs */
3992 tx_buf->next_to_watch = NULL;
3993
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003994 tx_desc->buffer_addr = 0;
3995 tx_desc->cmd_type_offset_bsz = 0;
3996 /* move past filter desc */
3997 tx_buf++;
3998 tx_desc++;
3999 i++;
4000 if (unlikely(!i)) {
4001 i -= tx_ring->count;
4002 tx_buf = tx_ring->tx_bi;
4003 tx_desc = I40E_TX_DESC(tx_ring, 0);
4004 }
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08004005 /* unmap skb header data */
4006 dma_unmap_single(tx_ring->dev,
4007 dma_unmap_addr(tx_buf, dma),
4008 dma_unmap_len(tx_buf, len),
4009 DMA_TO_DEVICE);
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00004010 if (tx_buf->tx_flags & I40E_TX_FLAGS_FD_SB)
4011 kfree(tx_buf->raw_buf);
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08004012
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00004013 tx_buf->raw_buf = NULL;
4014 tx_buf->tx_flags = 0;
4015 tx_buf->next_to_watch = NULL;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08004016 dma_unmap_len_set(tx_buf, len, 0);
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00004017 tx_desc->buffer_addr = 0;
4018 tx_desc->cmd_type_offset_bsz = 0;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08004019
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00004020 /* move us past the eop_desc for start of next FD desc */
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08004021 tx_buf++;
4022 tx_desc++;
4023 i++;
4024 if (unlikely(!i)) {
4025 i -= tx_ring->count;
4026 tx_buf = tx_ring->tx_bi;
4027 tx_desc = I40E_TX_DESC(tx_ring, 0);
4028 }
4029
4030 /* update budget accounting */
4031 budget--;
4032 } while (likely(budget));
4033
4034 i += tx_ring->count;
4035 tx_ring->next_to_clean = i;
4036
Jesse Brandeburg6995b362015-08-28 17:55:54 -04004037 if (vsi->back->flags & I40E_FLAG_MSIX_ENABLED)
Jesse Brandeburg78455482015-07-23 16:54:41 -04004038 i40e_irq_dynamic_enable(vsi, tx_ring->q_vector->v_idx);
Jesse Brandeburg6995b362015-08-28 17:55:54 -04004039
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08004040 return budget > 0;
4041}
4042
4043/**
4044 * i40e_fdir_clean_ring - Interrupt Handler for FDIR SB ring
4045 * @irq: interrupt number
4046 * @data: pointer to a q_vector
4047 **/
4048static irqreturn_t i40e_fdir_clean_ring(int irq, void *data)
4049{
4050 struct i40e_q_vector *q_vector = data;
4051 struct i40e_vsi *vsi;
4052
4053 if (!q_vector->tx.ring)
4054 return IRQ_HANDLED;
4055
4056 vsi = q_vector->tx.ring->vsi;
4057 i40e_clean_fdir_tx_irq(q_vector->tx.ring, vsi->work_limit);
4058
4059 return IRQ_HANDLED;
4060}
4061
4062/**
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00004063 * i40e_map_vector_to_qp - Assigns the queue pair to the vector
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004064 * @vsi: the VSI being configured
4065 * @v_idx: vector index
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00004066 * @qp_idx: queue pair index
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004067 **/
Anjali Singhai Jain26cdc442015-07-10 19:36:00 -04004068static void i40e_map_vector_to_qp(struct i40e_vsi *vsi, int v_idx, int qp_idx)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004069{
Alexander Duyck493fb302013-09-28 07:01:44 +00004070 struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
Alexander Duyck9f65e152013-09-28 06:00:58 +00004071 struct i40e_ring *tx_ring = vsi->tx_rings[qp_idx];
4072 struct i40e_ring *rx_ring = vsi->rx_rings[qp_idx];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004073
4074 tx_ring->q_vector = q_vector;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00004075 tx_ring->next = q_vector->tx.ring;
4076 q_vector->tx.ring = tx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004077 q_vector->tx.count++;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00004078
Björn Töpel74608d12017-05-24 07:55:35 +02004079 /* Place XDP Tx ring in the same q_vector ring list as regular Tx */
4080 if (i40e_enabled_xdp_vsi(vsi)) {
4081 struct i40e_ring *xdp_ring = vsi->xdp_rings[qp_idx];
4082
4083 xdp_ring->q_vector = q_vector;
4084 xdp_ring->next = q_vector->tx.ring;
4085 q_vector->tx.ring = xdp_ring;
4086 q_vector->tx.count++;
4087 }
4088
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00004089 rx_ring->q_vector = q_vector;
4090 rx_ring->next = q_vector->rx.ring;
4091 q_vector->rx.ring = rx_ring;
4092 q_vector->rx.count++;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004093}
4094
4095/**
4096 * i40e_vsi_map_rings_to_vectors - Maps descriptor rings to vectors
4097 * @vsi: the VSI being configured
4098 *
4099 * This function maps descriptor rings to the queue-specific vectors
4100 * we were allotted through the MSI-X enabling code. Ideally, we'd have
4101 * one vector per queue pair, but on a constrained vector budget, we
4102 * group the queue pairs as "efficiently" as possible.
4103 **/
4104static void i40e_vsi_map_rings_to_vectors(struct i40e_vsi *vsi)
4105{
4106 int qp_remaining = vsi->num_queue_pairs;
4107 int q_vectors = vsi->num_q_vectors;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00004108 int num_ringpairs;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004109 int v_start = 0;
4110 int qp_idx = 0;
4111
4112 /* If we don't have enough vectors for a 1-to-1 mapping, we'll have to
4113 * group them so there are multiple queues per vector.
Anjali Singhai Jain70114ec2014-06-03 23:50:14 +00004114 * It is also important to go through all the vectors available to be
4115 * sure that if we don't use all the vectors, that the remaining vectors
4116 * are cleared. This is especially important when decreasing the
4117 * number of queues in use.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004118 */
Anjali Singhai Jain70114ec2014-06-03 23:50:14 +00004119 for (; v_start < q_vectors; v_start++) {
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00004120 struct i40e_q_vector *q_vector = vsi->q_vectors[v_start];
4121
4122 num_ringpairs = DIV_ROUND_UP(qp_remaining, q_vectors - v_start);
4123
4124 q_vector->num_ringpairs = num_ringpairs;
4125
4126 q_vector->rx.count = 0;
4127 q_vector->tx.count = 0;
4128 q_vector->rx.ring = NULL;
4129 q_vector->tx.ring = NULL;
4130
4131 while (num_ringpairs--) {
Anjali Singhai Jain26cdc442015-07-10 19:36:00 -04004132 i40e_map_vector_to_qp(vsi, v_start, qp_idx);
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00004133 qp_idx++;
4134 qp_remaining--;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004135 }
4136 }
4137}
4138
4139/**
4140 * i40e_vsi_request_irq - Request IRQ from the OS
4141 * @vsi: the VSI being configured
4142 * @basename: name for the vector
4143 **/
4144static int i40e_vsi_request_irq(struct i40e_vsi *vsi, char *basename)
4145{
4146 struct i40e_pf *pf = vsi->back;
4147 int err;
4148
4149 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
4150 err = i40e_vsi_request_irq_msix(vsi, basename);
4151 else if (pf->flags & I40E_FLAG_MSI_ENABLED)
4152 err = request_irq(pf->pdev->irq, i40e_intr, 0,
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00004153 pf->int_name, pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004154 else
4155 err = request_irq(pf->pdev->irq, i40e_intr, IRQF_SHARED,
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00004156 pf->int_name, pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004157
4158 if (err)
4159 dev_info(&pf->pdev->dev, "request_irq failed, Error %d\n", err);
4160
4161 return err;
4162}
4163
4164#ifdef CONFIG_NET_POLL_CONTROLLER
4165/**
Jesse Brandeburgd89d9672016-01-04 10:33:02 -08004166 * i40e_netpoll - A Polling 'interrupt' handler
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004167 * @netdev: network interface device structure
4168 *
4169 * This is used by netconsole to send skbs without having to re-enable
4170 * interrupts. It's not called while the normal interrupt routine is executing.
4171 **/
4172static void i40e_netpoll(struct net_device *netdev)
4173{
4174 struct i40e_netdev_priv *np = netdev_priv(netdev);
4175 struct i40e_vsi *vsi = np->vsi;
4176 struct i40e_pf *pf = vsi->back;
4177 int i;
4178
4179 /* if interface is down do nothing */
Jacob Keller0da36b92017-04-19 09:25:55 -04004180 if (test_bit(__I40E_VSI_DOWN, vsi->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004181 return;
4182
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004183 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4184 for (i = 0; i < vsi->num_q_vectors; i++)
Alexander Duyck493fb302013-09-28 07:01:44 +00004185 i40e_msix_clean_rings(0, vsi->q_vectors[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004186 } else {
4187 i40e_intr(pf->pdev->irq, netdev);
4188 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004189}
4190#endif
4191
Jacob Kellerc768e492017-04-13 04:45:47 -04004192#define I40E_QTX_ENA_WAIT_COUNT 50
4193
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004194/**
Neerav Parikh23527302014-06-03 23:50:15 +00004195 * i40e_pf_txq_wait - Wait for a PF's Tx queue to be enabled or disabled
4196 * @pf: the PF being configured
4197 * @pf_q: the PF queue
4198 * @enable: enable or disable state of the queue
4199 *
4200 * This routine will wait for the given Tx queue of the PF to reach the
4201 * enabled or disabled state.
4202 * Returns -ETIMEDOUT in case of failing to reach the requested state after
4203 * multiple retries; else will return 0 in case of success.
4204 **/
4205static int i40e_pf_txq_wait(struct i40e_pf *pf, int pf_q, bool enable)
4206{
4207 int i;
4208 u32 tx_reg;
4209
4210 for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
4211 tx_reg = rd32(&pf->hw, I40E_QTX_ENA(pf_q));
4212 if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
4213 break;
4214
Neerav Parikhf98a2002014-09-13 07:40:44 +00004215 usleep_range(10, 20);
Neerav Parikh23527302014-06-03 23:50:15 +00004216 }
4217 if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
4218 return -ETIMEDOUT;
4219
4220 return 0;
4221}
4222
4223/**
Jacob Kellerc768e492017-04-13 04:45:47 -04004224 * i40e_control_tx_q - Start or stop a particular Tx queue
4225 * @pf: the PF structure
4226 * @pf_q: the PF queue to configure
4227 * @enable: start or stop the queue
4228 *
4229 * This function enables or disables a single queue. Note that any delay
4230 * required after the operation is expected to be handled by the caller of
4231 * this function.
4232 **/
4233static void i40e_control_tx_q(struct i40e_pf *pf, int pf_q, bool enable)
4234{
4235 struct i40e_hw *hw = &pf->hw;
4236 u32 tx_reg;
4237 int i;
4238
4239 /* warn the TX unit of coming changes */
4240 i40e_pre_tx_queue_cfg(&pf->hw, pf_q, enable);
4241 if (!enable)
4242 usleep_range(10, 20);
4243
4244 for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
4245 tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
4246 if (((tx_reg >> I40E_QTX_ENA_QENA_REQ_SHIFT) & 1) ==
4247 ((tx_reg >> I40E_QTX_ENA_QENA_STAT_SHIFT) & 1))
4248 break;
4249 usleep_range(1000, 2000);
4250 }
4251
4252 /* Skip if the queue is already in the requested state */
4253 if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
4254 return;
4255
4256 /* turn on/off the queue */
4257 if (enable) {
4258 wr32(hw, I40E_QTX_HEAD(pf_q), 0);
4259 tx_reg |= I40E_QTX_ENA_QENA_REQ_MASK;
4260 } else {
4261 tx_reg &= ~I40E_QTX_ENA_QENA_REQ_MASK;
4262 }
4263
4264 wr32(hw, I40E_QTX_ENA(pf_q), tx_reg);
4265}
4266
4267/**
Björn Töpel74608d12017-05-24 07:55:35 +02004268 * i40e_control_wait_tx_q - Start/stop Tx queue and wait for completion
4269 * @seid: VSI SEID
4270 * @pf: the PF structure
4271 * @pf_q: the PF queue to configure
4272 * @is_xdp: true if the queue is used for XDP
4273 * @enable: start or stop the queue
4274 **/
4275static int i40e_control_wait_tx_q(int seid, struct i40e_pf *pf, int pf_q,
4276 bool is_xdp, bool enable)
4277{
4278 int ret;
4279
4280 i40e_control_tx_q(pf, pf_q, enable);
4281
4282 /* wait for the change to finish */
4283 ret = i40e_pf_txq_wait(pf, pf_q, enable);
4284 if (ret) {
4285 dev_info(&pf->pdev->dev,
4286 "VSI seid %d %sTx ring %d %sable timeout\n",
4287 seid, (is_xdp ? "XDP " : ""), pf_q,
4288 (enable ? "en" : "dis"));
4289 }
4290
4291 return ret;
4292}
4293
4294/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004295 * i40e_vsi_control_tx - Start or stop a VSI's rings
4296 * @vsi: the VSI being configured
4297 * @enable: start or stop the rings
4298 **/
4299static int i40e_vsi_control_tx(struct i40e_vsi *vsi, bool enable)
4300{
4301 struct i40e_pf *pf = vsi->back;
Jacob Kellerc768e492017-04-13 04:45:47 -04004302 int i, pf_q, ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004303
4304 pf_q = vsi->base_queue;
4305 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
Björn Töpel74608d12017-05-24 07:55:35 +02004306 ret = i40e_control_wait_tx_q(vsi->seid, pf,
4307 pf_q,
4308 false /*is xdp*/, enable);
4309 if (ret)
Neerav Parikh23527302014-06-03 23:50:15 +00004310 break;
Björn Töpel74608d12017-05-24 07:55:35 +02004311
4312 if (!i40e_enabled_xdp_vsi(vsi))
4313 continue;
4314
4315 ret = i40e_control_wait_tx_q(vsi->seid, pf,
4316 pf_q + vsi->alloc_queue_pairs,
4317 true /*is xdp*/, enable);
4318 if (ret)
4319 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004320 }
4321
Neerav Parikh23527302014-06-03 23:50:15 +00004322 return ret;
4323}
4324
4325/**
4326 * i40e_pf_rxq_wait - Wait for a PF's Rx queue to be enabled or disabled
4327 * @pf: the PF being configured
4328 * @pf_q: the PF queue
4329 * @enable: enable or disable state of the queue
4330 *
4331 * This routine will wait for the given Rx queue of the PF to reach the
4332 * enabled or disabled state.
4333 * Returns -ETIMEDOUT in case of failing to reach the requested state after
4334 * multiple retries; else will return 0 in case of success.
4335 **/
4336static int i40e_pf_rxq_wait(struct i40e_pf *pf, int pf_q, bool enable)
4337{
4338 int i;
4339 u32 rx_reg;
4340
4341 for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
4342 rx_reg = rd32(&pf->hw, I40E_QRX_ENA(pf_q));
4343 if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4344 break;
4345
Neerav Parikhf98a2002014-09-13 07:40:44 +00004346 usleep_range(10, 20);
Neerav Parikh23527302014-06-03 23:50:15 +00004347 }
4348 if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
4349 return -ETIMEDOUT;
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00004350
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004351 return 0;
4352}
4353
4354/**
Jacob Kellerc768e492017-04-13 04:45:47 -04004355 * i40e_control_rx_q - Start or stop a particular Rx queue
4356 * @pf: the PF structure
4357 * @pf_q: the PF queue to configure
4358 * @enable: start or stop the queue
4359 *
4360 * This function enables or disables a single queue. Note that any delay
4361 * required after the operation is expected to be handled by the caller of
4362 * this function.
4363 **/
4364static void i40e_control_rx_q(struct i40e_pf *pf, int pf_q, bool enable)
4365{
4366 struct i40e_hw *hw = &pf->hw;
4367 u32 rx_reg;
4368 int i;
4369
4370 for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
4371 rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
4372 if (((rx_reg >> I40E_QRX_ENA_QENA_REQ_SHIFT) & 1) ==
4373 ((rx_reg >> I40E_QRX_ENA_QENA_STAT_SHIFT) & 1))
4374 break;
4375 usleep_range(1000, 2000);
4376 }
4377
4378 /* Skip if the queue is already in the requested state */
4379 if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4380 return;
4381
4382 /* turn on/off the queue */
4383 if (enable)
4384 rx_reg |= I40E_QRX_ENA_QENA_REQ_MASK;
4385 else
4386 rx_reg &= ~I40E_QRX_ENA_QENA_REQ_MASK;
4387
4388 wr32(hw, I40E_QRX_ENA(pf_q), rx_reg);
4389}
4390
4391/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004392 * i40e_vsi_control_rx - Start or stop a VSI's rings
4393 * @vsi: the VSI being configured
4394 * @enable: start or stop the rings
4395 **/
4396static int i40e_vsi_control_rx(struct i40e_vsi *vsi, bool enable)
4397{
4398 struct i40e_pf *pf = vsi->back;
Jacob Kellerc768e492017-04-13 04:45:47 -04004399 int i, pf_q, ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004400
4401 pf_q = vsi->base_queue;
4402 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
Jacob Kellerc768e492017-04-13 04:45:47 -04004403 i40e_control_rx_q(pf, pf_q, enable);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004404
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004405 /* wait for the change to finish */
Neerav Parikh23527302014-06-03 23:50:15 +00004406 ret = i40e_pf_rxq_wait(pf, pf_q, enable);
4407 if (ret) {
4408 dev_info(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04004409 "VSI seid %d Rx ring %d %sable timeout\n",
4410 vsi->seid, pf_q, (enable ? "en" : "dis"));
Neerav Parikh23527302014-06-03 23:50:15 +00004411 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004412 }
4413 }
4414
Wyborny, Carolynd08a9f62017-03-28 08:00:48 -07004415 /* Due to HW errata, on Rx disable only, the register can indicate done
4416 * before it really is. Needs 50ms to be sure
4417 */
4418 if (!enable)
4419 mdelay(50);
4420
Neerav Parikh23527302014-06-03 23:50:15 +00004421 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004422}
4423
4424/**
Filip Sadowski3aa7b742016-10-11 15:26:58 -07004425 * i40e_vsi_start_rings - Start a VSI's rings
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004426 * @vsi: the VSI being configured
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004427 **/
Filip Sadowski3aa7b742016-10-11 15:26:58 -07004428int i40e_vsi_start_rings(struct i40e_vsi *vsi)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004429{
Anjali Singhai Jain3b867b22013-12-21 05:44:44 +00004430 int ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004431
4432 /* do rx first for enable and last for disable */
Filip Sadowski3aa7b742016-10-11 15:26:58 -07004433 ret = i40e_vsi_control_rx(vsi, true);
4434 if (ret)
4435 return ret;
4436 ret = i40e_vsi_control_tx(vsi, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004437
4438 return ret;
4439}
4440
4441/**
Filip Sadowski3aa7b742016-10-11 15:26:58 -07004442 * i40e_vsi_stop_rings - Stop a VSI's rings
4443 * @vsi: the VSI being configured
4444 **/
4445void i40e_vsi_stop_rings(struct i40e_vsi *vsi)
4446{
Jacob Keller34807562017-04-13 04:45:53 -04004447 /* When port TX is suspended, don't wait */
Jacob Keller0da36b92017-04-19 09:25:55 -04004448 if (test_bit(__I40E_PORT_SUSPENDED, vsi->back->state))
Jacob Keller34807562017-04-13 04:45:53 -04004449 return i40e_vsi_stop_rings_no_wait(vsi);
4450
Filip Sadowski3aa7b742016-10-11 15:26:58 -07004451 /* do rx first for enable and last for disable
4452 * Ignore return value, we need to shutdown whatever we can
4453 */
4454 i40e_vsi_control_tx(vsi, false);
4455 i40e_vsi_control_rx(vsi, false);
4456}
4457
4458/**
Jacob Kellere4b433f2017-04-13 04:45:52 -04004459 * i40e_vsi_stop_rings_no_wait - Stop a VSI's rings and do not delay
4460 * @vsi: the VSI being shutdown
4461 *
4462 * This function stops all the rings for a VSI but does not delay to verify
4463 * that rings have been disabled. It is expected that the caller is shutting
4464 * down multiple VSIs at once and will delay together for all the VSIs after
4465 * initiating the shutdown. This is particularly useful for shutting down lots
4466 * of VFs together. Otherwise, a large delay can be incurred while configuring
4467 * each VSI in serial.
4468 **/
4469void i40e_vsi_stop_rings_no_wait(struct i40e_vsi *vsi)
4470{
4471 struct i40e_pf *pf = vsi->back;
4472 int i, pf_q;
4473
4474 pf_q = vsi->base_queue;
4475 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4476 i40e_control_tx_q(pf, pf_q, false);
4477 i40e_control_rx_q(pf, pf_q, false);
4478 }
4479}
4480
4481/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004482 * i40e_vsi_free_irq - Free the irq association with the OS
4483 * @vsi: the VSI being configured
4484 **/
4485static void i40e_vsi_free_irq(struct i40e_vsi *vsi)
4486{
4487 struct i40e_pf *pf = vsi->back;
4488 struct i40e_hw *hw = &pf->hw;
4489 int base = vsi->base_vector;
4490 u32 val, qp;
4491 int i;
4492
4493 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4494 if (!vsi->q_vectors)
4495 return;
4496
Shannon Nelson63741842014-04-23 04:50:16 +00004497 if (!vsi->irqs_ready)
4498 return;
4499
4500 vsi->irqs_ready = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004501 for (i = 0; i < vsi->num_q_vectors; i++) {
Alan Brady96db7762016-09-14 16:24:38 -07004502 int irq_num;
4503 u16 vector;
4504
4505 vector = i + base;
4506 irq_num = pf->msix_entries[vector].vector;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004507
4508 /* free only the irqs that were actually requested */
Shannon Nelson78681b12013-11-28 06:39:36 +00004509 if (!vsi->q_vectors[i] ||
4510 !vsi->q_vectors[i]->num_ringpairs)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004511 continue;
4512
Alan Brady96db7762016-09-14 16:24:38 -07004513 /* clear the affinity notifier in the IRQ descriptor */
4514 irq_set_affinity_notifier(irq_num, NULL);
Jacob Keller759dc4a2017-07-14 09:10:10 -04004515 /* remove our suggested affinity mask for this IRQ */
Alan Brady96db7762016-09-14 16:24:38 -07004516 irq_set_affinity_hint(irq_num, NULL);
4517 synchronize_irq(irq_num);
4518 free_irq(irq_num, vsi->q_vectors[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004519
4520 /* Tear down the interrupt queue link list
4521 *
4522 * We know that they come in pairs and always
4523 * the Rx first, then the Tx. To clear the
4524 * link list, stick the EOL value into the
4525 * next_q field of the registers.
4526 */
4527 val = rd32(hw, I40E_PFINT_LNKLSTN(vector - 1));
4528 qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
4529 >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4530 val |= I40E_QUEUE_END_OF_LIST
4531 << I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4532 wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), val);
4533
4534 while (qp != I40E_QUEUE_END_OF_LIST) {
4535 u32 next;
4536
4537 val = rd32(hw, I40E_QINT_RQCTL(qp));
4538
4539 val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
4540 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
4541 I40E_QINT_RQCTL_CAUSE_ENA_MASK |
4542 I40E_QINT_RQCTL_INTEVENT_MASK);
4543
4544 val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
4545 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
4546
4547 wr32(hw, I40E_QINT_RQCTL(qp), val);
4548
4549 val = rd32(hw, I40E_QINT_TQCTL(qp));
4550
4551 next = (val & I40E_QINT_TQCTL_NEXTQ_INDX_MASK)
4552 >> I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT;
4553
4554 val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
4555 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
4556 I40E_QINT_TQCTL_CAUSE_ENA_MASK |
4557 I40E_QINT_TQCTL_INTEVENT_MASK);
4558
4559 val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
4560 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
4561
4562 wr32(hw, I40E_QINT_TQCTL(qp), val);
4563 qp = next;
4564 }
4565 }
4566 } else {
4567 free_irq(pf->pdev->irq, pf);
4568
4569 val = rd32(hw, I40E_PFINT_LNKLST0);
4570 qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
4571 >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4572 val |= I40E_QUEUE_END_OF_LIST
4573 << I40E_PFINT_LNKLST0_FIRSTQ_INDX_SHIFT;
4574 wr32(hw, I40E_PFINT_LNKLST0, val);
4575
4576 val = rd32(hw, I40E_QINT_RQCTL(qp));
4577 val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
4578 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
4579 I40E_QINT_RQCTL_CAUSE_ENA_MASK |
4580 I40E_QINT_RQCTL_INTEVENT_MASK);
4581
4582 val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
4583 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
4584
4585 wr32(hw, I40E_QINT_RQCTL(qp), val);
4586
4587 val = rd32(hw, I40E_QINT_TQCTL(qp));
4588
4589 val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
4590 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
4591 I40E_QINT_TQCTL_CAUSE_ENA_MASK |
4592 I40E_QINT_TQCTL_INTEVENT_MASK);
4593
4594 val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
4595 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
4596
4597 wr32(hw, I40E_QINT_TQCTL(qp), val);
4598 }
4599}
4600
4601/**
Alexander Duyck493fb302013-09-28 07:01:44 +00004602 * i40e_free_q_vector - Free memory allocated for specific interrupt vector
4603 * @vsi: the VSI being configured
4604 * @v_idx: Index of vector to be freed
4605 *
4606 * This function frees the memory allocated to the q_vector. In addition if
4607 * NAPI is enabled it will delete any references to the NAPI struct prior
4608 * to freeing the q_vector.
4609 **/
4610static void i40e_free_q_vector(struct i40e_vsi *vsi, int v_idx)
4611{
4612 struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00004613 struct i40e_ring *ring;
Alexander Duyck493fb302013-09-28 07:01:44 +00004614
4615 if (!q_vector)
4616 return;
4617
4618 /* disassociate q_vector from rings */
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00004619 i40e_for_each_ring(ring, q_vector->tx)
4620 ring->q_vector = NULL;
4621
4622 i40e_for_each_ring(ring, q_vector->rx)
4623 ring->q_vector = NULL;
Alexander Duyck493fb302013-09-28 07:01:44 +00004624
4625 /* only VSI w/ an associated netdev is set up w/ NAPI */
4626 if (vsi->netdev)
4627 netif_napi_del(&q_vector->napi);
4628
4629 vsi->q_vectors[v_idx] = NULL;
4630
4631 kfree_rcu(q_vector, rcu);
4632}
4633
4634/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004635 * i40e_vsi_free_q_vectors - Free memory allocated for interrupt vectors
4636 * @vsi: the VSI being un-configured
4637 *
4638 * This frees the memory allocated to the q_vectors and
4639 * deletes references to the NAPI struct.
4640 **/
4641static void i40e_vsi_free_q_vectors(struct i40e_vsi *vsi)
4642{
4643 int v_idx;
4644
Alexander Duyck493fb302013-09-28 07:01:44 +00004645 for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++)
4646 i40e_free_q_vector(vsi, v_idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004647}
4648
4649/**
4650 * i40e_reset_interrupt_capability - Disable interrupt setup in OS
4651 * @pf: board private structure
4652 **/
4653static void i40e_reset_interrupt_capability(struct i40e_pf *pf)
4654{
4655 /* If we're in Legacy mode, the interrupt was cleaned in vsi_close */
4656 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4657 pci_disable_msix(pf->pdev);
4658 kfree(pf->msix_entries);
4659 pf->msix_entries = NULL;
Shannon Nelson3b444392015-02-26 16:15:57 +00004660 kfree(pf->irq_pile);
4661 pf->irq_pile = NULL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004662 } else if (pf->flags & I40E_FLAG_MSI_ENABLED) {
4663 pci_disable_msi(pf->pdev);
4664 }
4665 pf->flags &= ~(I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
4666}
4667
4668/**
4669 * i40e_clear_interrupt_scheme - Clear the current interrupt scheme settings
4670 * @pf: board private structure
4671 *
4672 * We go through and clear interrupt specific resources and reset the structure
4673 * to pre-load conditions
4674 **/
4675static void i40e_clear_interrupt_scheme(struct i40e_pf *pf)
4676{
4677 int i;
4678
Jacob Kellerc17401a2017-07-14 09:27:02 -04004679 i40e_free_misc_vector(pf);
Shannon Nelsone1477582015-02-21 06:44:33 +00004680
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06004681 i40e_put_lump(pf->irq_pile, pf->iwarp_base_vector,
4682 I40E_IWARP_IRQ_PILE_ID);
4683
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004684 i40e_put_lump(pf->irq_pile, 0, I40E_PILE_VALID_BIT-1);
Mitch Williams505682c2014-05-20 08:01:37 +00004685 for (i = 0; i < pf->num_alloc_vsi; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004686 if (pf->vsi[i])
4687 i40e_vsi_free_q_vectors(pf->vsi[i]);
4688 i40e_reset_interrupt_capability(pf);
4689}
4690
4691/**
4692 * i40e_napi_enable_all - Enable NAPI for all q_vectors in the VSI
4693 * @vsi: the VSI being configured
4694 **/
4695static void i40e_napi_enable_all(struct i40e_vsi *vsi)
4696{
4697 int q_idx;
4698
4699 if (!vsi->netdev)
4700 return;
4701
Alexander Duyck13a8cd12017-03-24 15:01:42 -07004702 for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
4703 struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
4704
4705 if (q_vector->rx.ring || q_vector->tx.ring)
4706 napi_enable(&q_vector->napi);
4707 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004708}
4709
4710/**
4711 * i40e_napi_disable_all - Disable NAPI for all q_vectors in the VSI
4712 * @vsi: the VSI being configured
4713 **/
4714static void i40e_napi_disable_all(struct i40e_vsi *vsi)
4715{
4716 int q_idx;
4717
4718 if (!vsi->netdev)
4719 return;
4720
Alexander Duyck13a8cd12017-03-24 15:01:42 -07004721 for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
4722 struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
4723
4724 if (q_vector->rx.ring || q_vector->tx.ring)
4725 napi_disable(&q_vector->napi);
4726 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004727}
4728
4729/**
Shannon Nelson90ef8d42014-03-14 07:32:26 +00004730 * i40e_vsi_close - Shut down a VSI
4731 * @vsi: the vsi to be quelled
4732 **/
4733static void i40e_vsi_close(struct i40e_vsi *vsi)
4734{
Mitch Williams0ef2d5a2017-01-24 10:24:00 -08004735 struct i40e_pf *pf = vsi->back;
Jacob Keller0da36b92017-04-19 09:25:55 -04004736 if (!test_and_set_bit(__I40E_VSI_DOWN, vsi->state))
Shannon Nelson90ef8d42014-03-14 07:32:26 +00004737 i40e_down(vsi);
4738 i40e_vsi_free_irq(vsi);
4739 i40e_vsi_free_tx_resources(vsi);
4740 i40e_vsi_free_rx_resources(vsi);
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04004741 vsi->current_netdev_flags = 0;
Mitch Williams0ef2d5a2017-01-24 10:24:00 -08004742 pf->flags |= I40E_FLAG_SERVICE_CLIENT_REQUESTED;
Jacob Keller0da36b92017-04-19 09:25:55 -04004743 if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
Mitch Williams0ef2d5a2017-01-24 10:24:00 -08004744 pf->flags |= I40E_FLAG_CLIENT_RESET;
Shannon Nelson90ef8d42014-03-14 07:32:26 +00004745}
4746
4747/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004748 * i40e_quiesce_vsi - Pause a given VSI
4749 * @vsi: the VSI being paused
4750 **/
4751static void i40e_quiesce_vsi(struct i40e_vsi *vsi)
4752{
Jacob Keller0da36b92017-04-19 09:25:55 -04004753 if (test_bit(__I40E_VSI_DOWN, vsi->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004754 return;
4755
Jacob Keller0da36b92017-04-19 09:25:55 -04004756 set_bit(__I40E_VSI_NEEDS_RESTART, vsi->state);
Jesse Brandeburg6995b362015-08-28 17:55:54 -04004757 if (vsi->netdev && netif_running(vsi->netdev))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004758 vsi->netdev->netdev_ops->ndo_stop(vsi->netdev);
Jesse Brandeburg6995b362015-08-28 17:55:54 -04004759 else
Shannon Nelson90ef8d42014-03-14 07:32:26 +00004760 i40e_vsi_close(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004761}
4762
4763/**
4764 * i40e_unquiesce_vsi - Resume a given VSI
4765 * @vsi: the VSI being resumed
4766 **/
4767static void i40e_unquiesce_vsi(struct i40e_vsi *vsi)
4768{
Jacob Keller0da36b92017-04-19 09:25:55 -04004769 if (!test_and_clear_bit(__I40E_VSI_NEEDS_RESTART, vsi->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004770 return;
4771
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004772 if (vsi->netdev && netif_running(vsi->netdev))
4773 vsi->netdev->netdev_ops->ndo_open(vsi->netdev);
4774 else
Shannon Nelson8276f752014-03-14 07:32:27 +00004775 i40e_vsi_open(vsi); /* this clears the DOWN bit */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004776}
4777
4778/**
4779 * i40e_pf_quiesce_all_vsi - Pause all VSIs on a PF
4780 * @pf: the PF
4781 **/
4782static void i40e_pf_quiesce_all_vsi(struct i40e_pf *pf)
4783{
4784 int v;
4785
Mitch Williams505682c2014-05-20 08:01:37 +00004786 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004787 if (pf->vsi[v])
4788 i40e_quiesce_vsi(pf->vsi[v]);
4789 }
4790}
4791
4792/**
4793 * i40e_pf_unquiesce_all_vsi - Resume all VSIs on a PF
4794 * @pf: the PF
4795 **/
4796static void i40e_pf_unquiesce_all_vsi(struct i40e_pf *pf)
4797{
4798 int v;
4799
Mitch Williams505682c2014-05-20 08:01:37 +00004800 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004801 if (pf->vsi[v])
4802 i40e_unquiesce_vsi(pf->vsi[v]);
4803 }
4804}
4805
Neerav Parikh69129dc2014-11-12 00:18:46 +00004806/**
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004807 * i40e_vsi_wait_queues_disabled - Wait for VSI's queues to be disabled
Neerav Parikh69129dc2014-11-12 00:18:46 +00004808 * @vsi: the VSI being configured
4809 *
Jacob Kelleraf26ce22017-04-05 07:50:58 -04004810 * Wait until all queues on a given VSI have been disabled.
Neerav Parikh69129dc2014-11-12 00:18:46 +00004811 **/
Jacob Kellere4b433f2017-04-13 04:45:52 -04004812int i40e_vsi_wait_queues_disabled(struct i40e_vsi *vsi)
Neerav Parikh69129dc2014-11-12 00:18:46 +00004813{
4814 struct i40e_pf *pf = vsi->back;
4815 int i, pf_q, ret;
4816
4817 pf_q = vsi->base_queue;
4818 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
Jacob Kelleraf26ce22017-04-05 07:50:58 -04004819 /* Check and wait for the Tx queue */
Neerav Parikh69129dc2014-11-12 00:18:46 +00004820 ret = i40e_pf_txq_wait(pf, pf_q, false);
4821 if (ret) {
4822 dev_info(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04004823 "VSI seid %d Tx ring %d disable timeout\n",
4824 vsi->seid, pf_q);
Neerav Parikh69129dc2014-11-12 00:18:46 +00004825 return ret;
4826 }
Björn Töpel74608d12017-05-24 07:55:35 +02004827
4828 if (!i40e_enabled_xdp_vsi(vsi))
4829 goto wait_rx;
4830
4831 /* Check and wait for the XDP Tx queue */
4832 ret = i40e_pf_txq_wait(pf, pf_q + vsi->alloc_queue_pairs,
4833 false);
4834 if (ret) {
4835 dev_info(&pf->pdev->dev,
4836 "VSI seid %d XDP Tx ring %d disable timeout\n",
4837 vsi->seid, pf_q);
4838 return ret;
4839 }
4840wait_rx:
4841 /* Check and wait for the Rx queue */
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004842 ret = i40e_pf_rxq_wait(pf, pf_q, false);
4843 if (ret) {
4844 dev_info(&pf->pdev->dev,
4845 "VSI seid %d Rx ring %d disable timeout\n",
4846 vsi->seid, pf_q);
4847 return ret;
4848 }
4849 }
4850
Neerav Parikh69129dc2014-11-12 00:18:46 +00004851 return 0;
4852}
4853
Jacob Kellere4b433f2017-04-13 04:45:52 -04004854#ifdef CONFIG_I40E_DCB
Neerav Parikh69129dc2014-11-12 00:18:46 +00004855/**
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004856 * i40e_pf_wait_queues_disabled - Wait for all queues of PF VSIs to be disabled
Neerav Parikh69129dc2014-11-12 00:18:46 +00004857 * @pf: the PF
4858 *
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004859 * This function waits for the queues to be in disabled state for all the
Neerav Parikh69129dc2014-11-12 00:18:46 +00004860 * VSIs that are managed by this PF.
4861 **/
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004862static int i40e_pf_wait_queues_disabled(struct i40e_pf *pf)
Neerav Parikh69129dc2014-11-12 00:18:46 +00004863{
4864 int v, ret = 0;
4865
4866 for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
Alexander Duyckc76cb6e2017-02-21 15:55:48 -08004867 if (pf->vsi[v]) {
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004868 ret = i40e_vsi_wait_queues_disabled(pf->vsi[v]);
Neerav Parikh69129dc2014-11-12 00:18:46 +00004869 if (ret)
4870 break;
4871 }
4872 }
4873
4874 return ret;
4875}
4876
4877#endif
Kiran Patilb03a8c12015-09-24 18:13:15 -04004878
4879/**
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004880 * i40e_get_iscsi_tc_map - Return TC map for iSCSI APP
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00004881 * @pf: pointer to PF
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004882 *
4883 * Get TC map for ISCSI PF type that will include iSCSI TC
4884 * and LAN TC.
4885 **/
4886static u8 i40e_get_iscsi_tc_map(struct i40e_pf *pf)
4887{
4888 struct i40e_dcb_app_priority_table app;
4889 struct i40e_hw *hw = &pf->hw;
4890 u8 enabled_tc = 1; /* TC0 is always enabled */
4891 u8 tc, i;
4892 /* Get the iSCSI APP TLV */
4893 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
4894
4895 for (i = 0; i < dcbcfg->numapps; i++) {
4896 app = dcbcfg->app[i];
4897 if (app.selector == I40E_APP_SEL_TCPIP &&
4898 app.protocolid == I40E_APP_PROTOID_ISCSI) {
4899 tc = dcbcfg->etscfg.prioritytable[app.priority];
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08004900 enabled_tc |= BIT(tc);
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004901 break;
4902 }
4903 }
4904
4905 return enabled_tc;
4906}
4907
4908/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004909 * i40e_dcb_get_num_tc - Get the number of TCs from DCBx config
4910 * @dcbcfg: the corresponding DCBx configuration structure
4911 *
4912 * Return the number of TCs from given DCBx configuration
4913 **/
4914static u8 i40e_dcb_get_num_tc(struct i40e_dcbx_config *dcbcfg)
4915{
Dave Ertmanfbfe12c2016-08-12 09:56:32 -07004916 int i, tc_unused = 0;
Jesse Brandeburg078b5872013-09-25 23:41:14 +00004917 u8 num_tc = 0;
Dave Ertmanfbfe12c2016-08-12 09:56:32 -07004918 u8 ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004919
4920 /* Scan the ETS Config Priority Table to find
4921 * traffic class enabled for a given priority
Dave Ertmanfbfe12c2016-08-12 09:56:32 -07004922 * and create a bitmask of enabled TCs
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004923 */
Dave Ertmanfbfe12c2016-08-12 09:56:32 -07004924 for (i = 0; i < I40E_MAX_USER_PRIORITY; i++)
4925 num_tc |= BIT(dcbcfg->etscfg.prioritytable[i]);
4926
4927 /* Now scan the bitmask to check for
4928 * contiguous TCs starting with TC0
4929 */
4930 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
4931 if (num_tc & BIT(i)) {
4932 if (!tc_unused) {
4933 ret++;
4934 } else {
4935 pr_err("Non-contiguous TC - Disabling DCB\n");
4936 return 1;
4937 }
4938 } else {
4939 tc_unused = 1;
4940 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004941 }
4942
Dave Ertmanfbfe12c2016-08-12 09:56:32 -07004943 /* There is always at least TC0 */
4944 if (!ret)
4945 ret = 1;
4946
4947 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004948}
4949
4950/**
4951 * i40e_dcb_get_enabled_tc - Get enabled traffic classes
4952 * @dcbcfg: the corresponding DCBx configuration structure
4953 *
4954 * Query the current DCB configuration and return the number of
4955 * traffic classes enabled from the given DCBX config
4956 **/
4957static u8 i40e_dcb_get_enabled_tc(struct i40e_dcbx_config *dcbcfg)
4958{
4959 u8 num_tc = i40e_dcb_get_num_tc(dcbcfg);
4960 u8 enabled_tc = 1;
4961 u8 i;
4962
4963 for (i = 0; i < num_tc; i++)
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04004964 enabled_tc |= BIT(i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004965
4966 return enabled_tc;
4967}
4968
4969/**
Amritha Nambiara9ce82f2017-09-07 04:00:22 -07004970 * i40e_mqprio_get_enabled_tc - Get enabled traffic classes
4971 * @pf: PF being queried
4972 *
4973 * Query the current MQPRIO configuration and return the number of
4974 * traffic classes enabled.
4975 **/
4976static u8 i40e_mqprio_get_enabled_tc(struct i40e_pf *pf)
4977{
4978 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
4979 u8 num_tc = vsi->mqprio_qopt.qopt.num_tc;
4980 u8 enabled_tc = 1, i;
4981
4982 for (i = 1; i < num_tc; i++)
4983 enabled_tc |= BIT(i);
4984 return enabled_tc;
4985}
4986
4987/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004988 * i40e_pf_get_num_tc - Get enabled traffic classes for PF
4989 * @pf: PF being queried
4990 *
4991 * Return number of traffic classes enabled for the given PF
4992 **/
4993static u8 i40e_pf_get_num_tc(struct i40e_pf *pf)
4994{
4995 struct i40e_hw *hw = &pf->hw;
Dave Ertman52a08ca2016-07-27 12:02:34 -07004996 u8 i, enabled_tc = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004997 u8 num_tc = 0;
4998 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
4999
Amritha Nambiara9ce82f2017-09-07 04:00:22 -07005000 if (pf->flags & I40E_FLAG_TC_MQPRIO)
5001 return pf->vsi[pf->lan_vsi]->mqprio_qopt.qopt.num_tc;
5002
5003 /* If neither MQPRIO nor DCB is enabled, then always use single TC */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005004 if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
5005 return 1;
5006
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005007 /* SFP mode will be enabled for all TCs on port */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00005008 if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
5009 return i40e_dcb_get_num_tc(dcbcfg);
5010
5011 /* MFP mode return count of enabled TCs for this PF */
5012 if (pf->hw.func_caps.iscsi)
5013 enabled_tc = i40e_get_iscsi_tc_map(pf);
5014 else
Neerav Parikhfc51de92015-02-24 06:58:53 +00005015 return 1; /* Only TC0 */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00005016
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00005017 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08005018 if (enabled_tc & BIT(i))
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00005019 num_tc++;
5020 }
5021 return num_tc;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005022}
5023
5024/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005025 * i40e_pf_get_pf_tc_map - Get bitmap for enabled traffic classes
5026 * @pf: PF being queried
5027 *
5028 * Return a bitmap for enabled traffic classes for this PF.
5029 **/
5030static u8 i40e_pf_get_tc_map(struct i40e_pf *pf)
5031{
Amritha Nambiara9ce82f2017-09-07 04:00:22 -07005032 if (pf->flags & I40E_FLAG_TC_MQPRIO)
5033 return i40e_mqprio_get_enabled_tc(pf);
5034
5035 /* If neither MQPRIO nor DCB is enabled for this PF then just return
5036 * default TC
5037 */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005038 if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
David Ertmanea6acb72016-09-20 07:10:50 -07005039 return I40E_DEFAULT_TRAFFIC_CLASS;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005040
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005041 /* SFP mode we want PF to be enabled for all TCs */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00005042 if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
5043 return i40e_dcb_get_enabled_tc(&pf->hw.local_dcbx_config);
5044
Neerav Parikhfc51de92015-02-24 06:58:53 +00005045 /* MFP enabled and iSCSI PF type */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00005046 if (pf->hw.func_caps.iscsi)
5047 return i40e_get_iscsi_tc_map(pf);
5048 else
David Ertmanea6acb72016-09-20 07:10:50 -07005049 return I40E_DEFAULT_TRAFFIC_CLASS;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005050}
5051
5052/**
5053 * i40e_vsi_get_bw_info - Query VSI BW Information
5054 * @vsi: the VSI being queried
5055 *
5056 * Returns 0 on success, negative value on failure
5057 **/
5058static int i40e_vsi_get_bw_info(struct i40e_vsi *vsi)
5059{
5060 struct i40e_aqc_query_vsi_ets_sla_config_resp bw_ets_config = {0};
5061 struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
5062 struct i40e_pf *pf = vsi->back;
5063 struct i40e_hw *hw = &pf->hw;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005064 i40e_status ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005065 u32 tc_bw_max;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005066 int i;
5067
5068 /* Get the VSI level BW configuration */
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005069 ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid, &bw_config, NULL);
5070 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005071 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005072 "couldn't get PF vsi bw config, err %s aq_err %s\n",
5073 i40e_stat_str(&pf->hw, ret),
5074 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00005075 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005076 }
5077
5078 /* Get the VSI level BW configuration per TC */
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005079 ret = i40e_aq_query_vsi_ets_sla_config(hw, vsi->seid, &bw_ets_config,
5080 NULL);
5081 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005082 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005083 "couldn't get PF vsi ets bw config, err %s aq_err %s\n",
5084 i40e_stat_str(&pf->hw, ret),
5085 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00005086 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005087 }
5088
5089 if (bw_config.tc_valid_bits != bw_ets_config.tc_valid_bits) {
5090 dev_info(&pf->pdev->dev,
5091 "Enabled TCs mismatch from querying VSI BW info 0x%08x 0x%08x\n",
5092 bw_config.tc_valid_bits,
5093 bw_ets_config.tc_valid_bits);
5094 /* Still continuing */
5095 }
5096
5097 vsi->bw_limit = le16_to_cpu(bw_config.port_bw_limit);
5098 vsi->bw_max_quanta = bw_config.max_bw;
5099 tc_bw_max = le16_to_cpu(bw_ets_config.tc_bw_max[0]) |
5100 (le16_to_cpu(bw_ets_config.tc_bw_max[1]) << 16);
5101 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5102 vsi->bw_ets_share_credits[i] = bw_ets_config.share_credits[i];
5103 vsi->bw_ets_limit_credits[i] =
5104 le16_to_cpu(bw_ets_config.credits[i]);
5105 /* 3 bits out of 4 for each TC */
5106 vsi->bw_ets_max_quanta[i] = (u8)((tc_bw_max >> (i*4)) & 0x7);
5107 }
Jesse Brandeburg078b5872013-09-25 23:41:14 +00005108
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00005109 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005110}
5111
5112/**
5113 * i40e_vsi_configure_bw_alloc - Configure VSI BW allocation per TC
5114 * @vsi: the VSI being configured
5115 * @enabled_tc: TC bitmap
5116 * @bw_credits: BW shared credits per TC
5117 *
5118 * Returns 0 on success, negative value on failure
5119 **/
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00005120static int i40e_vsi_configure_bw_alloc(struct i40e_vsi *vsi, u8 enabled_tc,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005121 u8 *bw_share)
5122{
5123 struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005124 i40e_status ret;
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00005125 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005126
Amritha Nambiar2027d4d2017-09-07 04:00:32 -07005127 if (vsi->back->flags & I40E_FLAG_TC_MQPRIO)
Amritha Nambiara9ce82f2017-09-07 04:00:22 -07005128 return 0;
Amritha Nambiar2027d4d2017-09-07 04:00:32 -07005129 if (!vsi->mqprio_qopt.qopt.hw) {
5130 ret = i40e_set_bw_limit(vsi, vsi->seid, 0);
5131 if (ret)
5132 dev_info(&vsi->back->pdev->dev,
5133 "Failed to reset tx rate for vsi->seid %u\n",
5134 vsi->seid);
5135 return ret;
5136 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005137 bw_data.tc_valid_bits = enabled_tc;
5138 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5139 bw_data.tc_bw_credits[i] = bw_share[i];
5140
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005141 ret = i40e_aq_config_vsi_tc_bw(&vsi->back->hw, vsi->seid, &bw_data,
5142 NULL);
5143 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005144 dev_info(&vsi->back->pdev->dev,
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005145 "AQ command Config VSI BW allocation per TC failed = %d\n",
5146 vsi->back->hw.aq.asq_last_status);
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00005147 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005148 }
5149
5150 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5151 vsi->info.qs_handle[i] = bw_data.qs_handles[i];
5152
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00005153 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005154}
5155
5156/**
5157 * i40e_vsi_config_netdev_tc - Setup the netdev TC configuration
5158 * @vsi: the VSI being configured
5159 * @enabled_tc: TC map to be enabled
5160 *
5161 **/
5162static void i40e_vsi_config_netdev_tc(struct i40e_vsi *vsi, u8 enabled_tc)
5163{
5164 struct net_device *netdev = vsi->netdev;
5165 struct i40e_pf *pf = vsi->back;
5166 struct i40e_hw *hw = &pf->hw;
5167 u8 netdev_tc = 0;
5168 int i;
5169 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5170
5171 if (!netdev)
5172 return;
5173
5174 if (!enabled_tc) {
5175 netdev_reset_tc(netdev);
5176 return;
5177 }
5178
5179 /* Set up actual enabled TCs on the VSI */
5180 if (netdev_set_num_tc(netdev, vsi->tc_config.numtc))
5181 return;
5182
5183 /* set per TC queues for the VSI */
5184 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5185 /* Only set TC queues for enabled tcs
5186 *
5187 * e.g. For a VSI that has TC0 and TC3 enabled the
5188 * enabled_tc bitmap would be 0x00001001; the driver
5189 * will set the numtc for netdev as 2 that will be
5190 * referenced by the netdev layer as TC 0 and 1.
5191 */
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08005192 if (vsi->tc_config.enabled_tc & BIT(i))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005193 netdev_set_tc_queue(netdev,
5194 vsi->tc_config.tc_info[i].netdev_tc,
5195 vsi->tc_config.tc_info[i].qcount,
5196 vsi->tc_config.tc_info[i].qoffset);
5197 }
5198
Amritha Nambiara9ce82f2017-09-07 04:00:22 -07005199 if (pf->flags & I40E_FLAG_TC_MQPRIO)
5200 return;
5201
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005202 /* Assign UP2TC map for the VSI */
5203 for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
5204 /* Get the actual TC# for the UP */
5205 u8 ets_tc = dcbcfg->etscfg.prioritytable[i];
5206 /* Get the mapped netdev TC# for the UP */
5207 netdev_tc = vsi->tc_config.tc_info[ets_tc].netdev_tc;
5208 netdev_set_prio_tc_map(netdev, i, netdev_tc);
5209 }
5210}
5211
5212/**
5213 * i40e_vsi_update_queue_map - Update our copy of VSi info with new queue map
5214 * @vsi: the VSI being configured
5215 * @ctxt: the ctxt buffer returned from AQ VSI update param command
5216 **/
5217static void i40e_vsi_update_queue_map(struct i40e_vsi *vsi,
5218 struct i40e_vsi_context *ctxt)
5219{
5220 /* copy just the sections touched not the entire info
5221 * since not all sections are valid as returned by
5222 * update vsi params
5223 */
5224 vsi->info.mapping_flags = ctxt->info.mapping_flags;
5225 memcpy(&vsi->info.queue_mapping,
5226 &ctxt->info.queue_mapping, sizeof(vsi->info.queue_mapping));
5227 memcpy(&vsi->info.tc_mapping, ctxt->info.tc_mapping,
5228 sizeof(vsi->info.tc_mapping));
5229}
5230
5231/**
5232 * i40e_vsi_config_tc - Configure VSI Tx Scheduler for given TC map
5233 * @vsi: VSI to be configured
5234 * @enabled_tc: TC bitmap
5235 *
5236 * This configures a particular VSI for TCs that are mapped to the
5237 * given TC bitmap. It uses default bandwidth share for TCs across
5238 * VSIs to configure TC for a particular VSI.
5239 *
5240 * NOTE:
5241 * It is expected that the VSI queues have been quisced before calling
5242 * this function.
5243 **/
5244static int i40e_vsi_config_tc(struct i40e_vsi *vsi, u8 enabled_tc)
5245{
5246 u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
5247 struct i40e_vsi_context ctxt;
5248 int ret = 0;
5249 int i;
5250
5251 /* Check if enabled_tc is same as existing or new TCs */
Amritha Nambiara9ce82f2017-09-07 04:00:22 -07005252 if (vsi->tc_config.enabled_tc == enabled_tc &&
5253 vsi->mqprio_qopt.mode != TC_MQPRIO_MODE_CHANNEL)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005254 return ret;
5255
5256 /* Enable ETS TCs with equal BW Share for now across all VSIs */
5257 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08005258 if (enabled_tc & BIT(i))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005259 bw_share[i] = 1;
5260 }
5261
5262 ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
5263 if (ret) {
5264 dev_info(&vsi->back->pdev->dev,
5265 "Failed configuring TC map %d for VSI %d\n",
5266 enabled_tc, vsi->seid);
5267 goto out;
5268 }
5269
5270 /* Update Queue Pairs Mapping for currently enabled UPs */
5271 ctxt.seid = vsi->seid;
5272 ctxt.pf_num = vsi->back->hw.pf_id;
5273 ctxt.vf_num = 0;
5274 ctxt.uplink_seid = vsi->uplink_seid;
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07005275 ctxt.info = vsi->info;
Amritha Nambiara9ce82f2017-09-07 04:00:22 -07005276 if (vsi->back->flags & I40E_FLAG_TC_MQPRIO) {
5277 ret = i40e_vsi_setup_queue_map_mqprio(vsi, &ctxt, enabled_tc);
5278 if (ret)
5279 goto out;
5280 } else {
5281 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
5282 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005283
Amritha Nambiara9ce82f2017-09-07 04:00:22 -07005284 /* On destroying the qdisc, reset vsi->rss_size, as number of enabled
5285 * queues changed.
5286 */
5287 if (!vsi->mqprio_qopt.qopt.hw && vsi->reconfig_rss) {
5288 vsi->rss_size = min_t(int, vsi->back->alloc_rss_size,
5289 vsi->num_queue_pairs);
5290 ret = i40e_vsi_config_rss(vsi);
5291 if (ret) {
5292 dev_info(&vsi->back->pdev->dev,
5293 "Failed to reconfig rss for num_queues\n");
5294 return ret;
5295 }
5296 vsi->reconfig_rss = false;
5297 }
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06005298 if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
5299 ctxt.info.valid_sections |=
5300 cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
5301 ctxt.info.queueing_opt_flags |= I40E_AQ_VSI_QUE_OPT_TCP_ENA;
5302 }
5303
Amritha Nambiara9ce82f2017-09-07 04:00:22 -07005304 /* Update the VSI after updating the VSI queue-mapping
5305 * information
5306 */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005307 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
5308 if (ret) {
5309 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005310 "Update vsi tc config failed, err %s aq_err %s\n",
5311 i40e_stat_str(&vsi->back->hw, ret),
5312 i40e_aq_str(&vsi->back->hw,
5313 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005314 goto out;
5315 }
5316 /* update the local VSI info with updated queue map */
5317 i40e_vsi_update_queue_map(vsi, &ctxt);
5318 vsi->info.valid_sections = 0;
5319
5320 /* Update current VSI BW information */
5321 ret = i40e_vsi_get_bw_info(vsi);
5322 if (ret) {
5323 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005324 "Failed updating vsi bw info, err %s aq_err %s\n",
5325 i40e_stat_str(&vsi->back->hw, ret),
5326 i40e_aq_str(&vsi->back->hw,
5327 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005328 goto out;
5329 }
5330
5331 /* Update the netdev TC setup */
5332 i40e_vsi_config_netdev_tc(vsi, enabled_tc);
5333out:
5334 return ret;
5335}
5336
5337/**
Amritha Nambiar5ecae412017-09-07 04:00:27 -07005338 * i40e_get_link_speed - Returns link speed for the interface
5339 * @vsi: VSI to be configured
5340 *
5341 **/
5342int i40e_get_link_speed(struct i40e_vsi *vsi)
5343{
5344 struct i40e_pf *pf = vsi->back;
5345
5346 switch (pf->hw.phy.link_info.link_speed) {
5347 case I40E_LINK_SPEED_40GB:
5348 return 40000;
5349 case I40E_LINK_SPEED_25GB:
5350 return 25000;
5351 case I40E_LINK_SPEED_20GB:
5352 return 20000;
5353 case I40E_LINK_SPEED_10GB:
5354 return 10000;
5355 case I40E_LINK_SPEED_1GB:
5356 return 1000;
5357 default:
5358 return -EINVAL;
5359 }
5360}
5361
5362/**
5363 * i40e_set_bw_limit - setup BW limit for Tx traffic based on max_tx_rate
5364 * @vsi: VSI to be configured
5365 * @seid: seid of the channel/VSI
5366 * @max_tx_rate: max TX rate to be configured as BW limit
5367 *
5368 * Helper function to set BW limit for a given VSI
5369 **/
5370int i40e_set_bw_limit(struct i40e_vsi *vsi, u16 seid, u64 max_tx_rate)
5371{
5372 struct i40e_pf *pf = vsi->back;
Alan Brady6c32e0d2017-10-09 15:48:45 -07005373 u64 credits = 0;
Amritha Nambiar5ecae412017-09-07 04:00:27 -07005374 int speed = 0;
5375 int ret = 0;
5376
5377 speed = i40e_get_link_speed(vsi);
5378 if (max_tx_rate > speed) {
5379 dev_err(&pf->pdev->dev,
5380 "Invalid max tx rate %llu specified for VSI seid %d.",
5381 max_tx_rate, seid);
5382 return -EINVAL;
5383 }
5384 if (max_tx_rate && max_tx_rate < 50) {
5385 dev_warn(&pf->pdev->dev,
5386 "Setting max tx rate to minimum usable value of 50Mbps.\n");
5387 max_tx_rate = 50;
5388 }
5389
5390 /* Tx rate credits are in values of 50Mbps, 0 is disabled */
Alan Brady6c32e0d2017-10-09 15:48:45 -07005391 credits = max_tx_rate;
5392 do_div(credits, I40E_BW_CREDIT_DIVISOR);
5393 ret = i40e_aq_config_vsi_bw_limit(&pf->hw, seid, credits,
Amritha Nambiar5ecae412017-09-07 04:00:27 -07005394 I40E_MAX_BW_INACTIVE_ACCUM, NULL);
5395 if (ret)
5396 dev_err(&pf->pdev->dev,
5397 "Failed set tx rate (%llu Mbps) for vsi->seid %u, err %s aq_err %s\n",
5398 max_tx_rate, seid, i40e_stat_str(&pf->hw, ret),
5399 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
5400 return ret;
5401}
5402
5403/**
Amritha Nambiar8f88b302017-09-07 04:00:17 -07005404 * i40e_remove_queue_channels - Remove queue channels for the TCs
5405 * @vsi: VSI to be configured
5406 *
5407 * Remove queue channels for the TCs
5408 **/
5409static void i40e_remove_queue_channels(struct i40e_vsi *vsi)
5410{
Amritha Nambiar2f4b4112017-10-27 02:36:01 -07005411 enum i40e_admin_queue_err last_aq_status;
5412 struct i40e_cloud_filter *cfilter;
Amritha Nambiar8f88b302017-09-07 04:00:17 -07005413 struct i40e_channel *ch, *ch_tmp;
Amritha Nambiar2f4b4112017-10-27 02:36:01 -07005414 struct i40e_pf *pf = vsi->back;
5415 struct hlist_node *node;
Amritha Nambiar8f88b302017-09-07 04:00:17 -07005416 int ret, i;
5417
5418 /* Reset rss size that was stored when reconfiguring rss for
5419 * channel VSIs with non-power-of-2 queue count.
5420 */
5421 vsi->current_rss_size = 0;
5422
5423 /* perform cleanup for channels if they exist */
5424 if (list_empty(&vsi->ch_list))
5425 return;
5426
5427 list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
5428 struct i40e_vsi *p_vsi;
5429
5430 list_del(&ch->list);
5431 p_vsi = ch->parent_vsi;
5432 if (!p_vsi || !ch->initialized) {
5433 kfree(ch);
5434 continue;
5435 }
5436 /* Reset queue contexts */
5437 for (i = 0; i < ch->num_queue_pairs; i++) {
5438 struct i40e_ring *tx_ring, *rx_ring;
5439 u16 pf_q;
5440
5441 pf_q = ch->base_queue + i;
5442 tx_ring = vsi->tx_rings[pf_q];
5443 tx_ring->ch = NULL;
5444
5445 rx_ring = vsi->rx_rings[pf_q];
5446 rx_ring->ch = NULL;
5447 }
5448
Amritha Nambiar2027d4d2017-09-07 04:00:32 -07005449 /* Reset BW configured for this VSI via mqprio */
5450 ret = i40e_set_bw_limit(vsi, ch->seid, 0);
5451 if (ret)
5452 dev_info(&vsi->back->pdev->dev,
5453 "Failed to reset tx rate for ch->seid %u\n",
5454 ch->seid);
5455
Amritha Nambiar2f4b4112017-10-27 02:36:01 -07005456 /* delete cloud filters associated with this channel */
5457 hlist_for_each_entry_safe(cfilter, node,
5458 &pf->cloud_filter_list, cloud_node) {
5459 if (cfilter->seid != ch->seid)
5460 continue;
5461
5462 hash_del(&cfilter->cloud_node);
5463 if (cfilter->dst_port)
5464 ret = i40e_add_del_cloud_filter_big_buf(vsi,
5465 cfilter,
5466 false);
5467 else
5468 ret = i40e_add_del_cloud_filter(vsi, cfilter,
5469 false);
5470 last_aq_status = pf->hw.aq.asq_last_status;
5471 if (ret)
5472 dev_info(&pf->pdev->dev,
5473 "Failed to delete cloud filter, err %s aq_err %s\n",
5474 i40e_stat_str(&pf->hw, ret),
5475 i40e_aq_str(&pf->hw, last_aq_status));
5476 kfree(cfilter);
5477 }
5478
Amritha Nambiar8f88b302017-09-07 04:00:17 -07005479 /* delete VSI from FW */
5480 ret = i40e_aq_delete_element(&vsi->back->hw, ch->seid,
5481 NULL);
5482 if (ret)
5483 dev_err(&vsi->back->pdev->dev,
5484 "unable to remove channel (%d) for parent VSI(%d)\n",
5485 ch->seid, p_vsi->seid);
5486 kfree(ch);
5487 }
5488 INIT_LIST_HEAD(&vsi->ch_list);
5489}
5490
5491/**
5492 * i40e_is_any_channel - channel exist or not
5493 * @vsi: ptr to VSI to which channels are associated with
5494 *
5495 * Returns true or false if channel(s) exist for associated VSI or not
5496 **/
5497static bool i40e_is_any_channel(struct i40e_vsi *vsi)
5498{
5499 struct i40e_channel *ch, *ch_tmp;
5500
5501 list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
5502 if (ch->initialized)
5503 return true;
5504 }
5505
5506 return false;
5507}
5508
5509/**
5510 * i40e_get_max_queues_for_channel
5511 * @vsi: ptr to VSI to which channels are associated with
5512 *
5513 * Helper function which returns max value among the queue counts set on the
5514 * channels/TCs created.
5515 **/
5516static int i40e_get_max_queues_for_channel(struct i40e_vsi *vsi)
5517{
5518 struct i40e_channel *ch, *ch_tmp;
5519 int max = 0;
5520
5521 list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
5522 if (!ch->initialized)
5523 continue;
5524 if (ch->num_queue_pairs > max)
5525 max = ch->num_queue_pairs;
5526 }
5527
5528 return max;
5529}
5530
5531/**
5532 * i40e_validate_num_queues - validate num_queues w.r.t channel
5533 * @pf: ptr to PF device
5534 * @num_queues: number of queues
5535 * @vsi: the parent VSI
5536 * @reconfig_rss: indicates should the RSS be reconfigured or not
5537 *
5538 * This function validates number of queues in the context of new channel
5539 * which is being established and determines if RSS should be reconfigured
5540 * or not for parent VSI.
5541 **/
5542static int i40e_validate_num_queues(struct i40e_pf *pf, int num_queues,
5543 struct i40e_vsi *vsi, bool *reconfig_rss)
5544{
5545 int max_ch_queues;
5546
5547 if (!reconfig_rss)
5548 return -EINVAL;
5549
5550 *reconfig_rss = false;
Amritha Nambiar8f88b302017-09-07 04:00:17 -07005551 if (vsi->current_rss_size) {
5552 if (num_queues > vsi->current_rss_size) {
5553 dev_dbg(&pf->pdev->dev,
5554 "Error: num_queues (%d) > vsi's current_size(%d)\n",
5555 num_queues, vsi->current_rss_size);
5556 return -EINVAL;
5557 } else if ((num_queues < vsi->current_rss_size) &&
5558 (!is_power_of_2(num_queues))) {
5559 dev_dbg(&pf->pdev->dev,
5560 "Error: num_queues (%d) < vsi's current_size(%d), but not power of 2\n",
5561 num_queues, vsi->current_rss_size);
5562 return -EINVAL;
5563 }
5564 }
5565
5566 if (!is_power_of_2(num_queues)) {
5567 /* Find the max num_queues configured for channel if channel
5568 * exist.
5569 * if channel exist, then enforce 'num_queues' to be more than
5570 * max ever queues configured for channel.
5571 */
5572 max_ch_queues = i40e_get_max_queues_for_channel(vsi);
5573 if (num_queues < max_ch_queues) {
5574 dev_dbg(&pf->pdev->dev,
5575 "Error: num_queues (%d) < max queues configured for channel(%d)\n",
5576 num_queues, max_ch_queues);
5577 return -EINVAL;
5578 }
5579 *reconfig_rss = true;
5580 }
5581
5582 return 0;
5583}
5584
5585/**
5586 * i40e_vsi_reconfig_rss - reconfig RSS based on specified rss_size
5587 * @vsi: the VSI being setup
5588 * @rss_size: size of RSS, accordingly LUT gets reprogrammed
5589 *
5590 * This function reconfigures RSS by reprogramming LUTs using 'rss_size'
5591 **/
5592static int i40e_vsi_reconfig_rss(struct i40e_vsi *vsi, u16 rss_size)
5593{
5594 struct i40e_pf *pf = vsi->back;
5595 u8 seed[I40E_HKEY_ARRAY_SIZE];
5596 struct i40e_hw *hw = &pf->hw;
5597 int local_rss_size;
5598 u8 *lut;
5599 int ret;
5600
5601 if (!vsi->rss_size)
5602 return -EINVAL;
5603
5604 if (rss_size > vsi->rss_size)
5605 return -EINVAL;
5606
5607 local_rss_size = min_t(int, vsi->rss_size, rss_size);
5608 lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
5609 if (!lut)
5610 return -ENOMEM;
5611
5612 /* Ignoring user configured lut if there is one */
5613 i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, local_rss_size);
5614
5615 /* Use user configured hash key if there is one, otherwise
5616 * use default.
5617 */
5618 if (vsi->rss_hkey_user)
5619 memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
5620 else
5621 netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
5622
5623 ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
5624 if (ret) {
5625 dev_info(&pf->pdev->dev,
5626 "Cannot set RSS lut, err %s aq_err %s\n",
5627 i40e_stat_str(hw, ret),
5628 i40e_aq_str(hw, hw->aq.asq_last_status));
5629 kfree(lut);
5630 return ret;
5631 }
5632 kfree(lut);
5633
5634 /* Do the update w.r.t. storing rss_size */
5635 if (!vsi->orig_rss_size)
5636 vsi->orig_rss_size = vsi->rss_size;
5637 vsi->current_rss_size = local_rss_size;
5638
5639 return ret;
5640}
5641
5642/**
5643 * i40e_channel_setup_queue_map - Setup a channel queue map
5644 * @pf: ptr to PF device
5645 * @vsi: the VSI being setup
5646 * @ctxt: VSI context structure
5647 * @ch: ptr to channel structure
5648 *
5649 * Setup queue map for a specific channel
5650 **/
5651static void i40e_channel_setup_queue_map(struct i40e_pf *pf,
5652 struct i40e_vsi_context *ctxt,
5653 struct i40e_channel *ch)
5654{
5655 u16 qcount, qmap, sections = 0;
5656 u8 offset = 0;
5657 int pow;
5658
5659 sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
5660 sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
5661
5662 qcount = min_t(int, ch->num_queue_pairs, pf->num_lan_msix);
5663 ch->num_queue_pairs = qcount;
5664
5665 /* find the next higher power-of-2 of num queue pairs */
5666 pow = ilog2(qcount);
5667 if (!is_power_of_2(qcount))
5668 pow++;
5669
5670 qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
5671 (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
5672
5673 /* Setup queue TC[0].qmap for given VSI context */
5674 ctxt->info.tc_mapping[0] = cpu_to_le16(qmap);
5675
5676 ctxt->info.up_enable_bits = 0x1; /* TC0 enabled */
5677 ctxt->info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
5678 ctxt->info.queue_mapping[0] = cpu_to_le16(ch->base_queue);
5679 ctxt->info.valid_sections |= cpu_to_le16(sections);
5680}
5681
5682/**
5683 * i40e_add_channel - add a channel by adding VSI
5684 * @pf: ptr to PF device
5685 * @uplink_seid: underlying HW switching element (VEB) ID
5686 * @ch: ptr to channel structure
5687 *
5688 * Add a channel (VSI) using add_vsi and queue_map
5689 **/
5690static int i40e_add_channel(struct i40e_pf *pf, u16 uplink_seid,
5691 struct i40e_channel *ch)
5692{
5693 struct i40e_hw *hw = &pf->hw;
5694 struct i40e_vsi_context ctxt;
5695 u8 enabled_tc = 0x1; /* TC0 enabled */
5696 int ret;
5697
5698 if (ch->type != I40E_VSI_VMDQ2) {
5699 dev_info(&pf->pdev->dev,
5700 "add new vsi failed, ch->type %d\n", ch->type);
5701 return -EINVAL;
5702 }
5703
5704 memset(&ctxt, 0, sizeof(ctxt));
5705 ctxt.pf_num = hw->pf_id;
5706 ctxt.vf_num = 0;
5707 ctxt.uplink_seid = uplink_seid;
5708 ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
5709 if (ch->type == I40E_VSI_VMDQ2)
5710 ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
5711
5712 if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED) {
5713 ctxt.info.valid_sections |=
5714 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
5715 ctxt.info.switch_id =
5716 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
5717 }
5718
5719 /* Set queue map for a given VSI context */
5720 i40e_channel_setup_queue_map(pf, &ctxt, ch);
5721
5722 /* Now time to create VSI */
5723 ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
5724 if (ret) {
5725 dev_info(&pf->pdev->dev,
5726 "add new vsi failed, err %s aq_err %s\n",
5727 i40e_stat_str(&pf->hw, ret),
5728 i40e_aq_str(&pf->hw,
5729 pf->hw.aq.asq_last_status));
5730 return -ENOENT;
5731 }
5732
5733 /* Success, update channel */
5734 ch->enabled_tc = enabled_tc;
5735 ch->seid = ctxt.seid;
5736 ch->vsi_number = ctxt.vsi_number;
5737 ch->stat_counter_idx = cpu_to_le16(ctxt.info.stat_counter_idx);
5738
5739 /* copy just the sections touched not the entire info
5740 * since not all sections are valid as returned by
5741 * update vsi params
5742 */
5743 ch->info.mapping_flags = ctxt.info.mapping_flags;
5744 memcpy(&ch->info.queue_mapping,
5745 &ctxt.info.queue_mapping, sizeof(ctxt.info.queue_mapping));
5746 memcpy(&ch->info.tc_mapping, ctxt.info.tc_mapping,
5747 sizeof(ctxt.info.tc_mapping));
5748
5749 return 0;
5750}
5751
5752static int i40e_channel_config_bw(struct i40e_vsi *vsi, struct i40e_channel *ch,
5753 u8 *bw_share)
5754{
5755 struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
5756 i40e_status ret;
5757 int i;
5758
5759 bw_data.tc_valid_bits = ch->enabled_tc;
5760 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5761 bw_data.tc_bw_credits[i] = bw_share[i];
5762
5763 ret = i40e_aq_config_vsi_tc_bw(&vsi->back->hw, ch->seid,
5764 &bw_data, NULL);
5765 if (ret) {
5766 dev_info(&vsi->back->pdev->dev,
5767 "Config VSI BW allocation per TC failed, aq_err: %d for new_vsi->seid %u\n",
5768 vsi->back->hw.aq.asq_last_status, ch->seid);
5769 return -EINVAL;
5770 }
5771
5772 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5773 ch->info.qs_handle[i] = bw_data.qs_handles[i];
5774
5775 return 0;
5776}
5777
5778/**
5779 * i40e_channel_config_tx_ring - config TX ring associated with new channel
5780 * @pf: ptr to PF device
5781 * @vsi: the VSI being setup
5782 * @ch: ptr to channel structure
5783 *
5784 * Configure TX rings associated with channel (VSI) since queues are being
5785 * from parent VSI.
5786 **/
5787static int i40e_channel_config_tx_ring(struct i40e_pf *pf,
5788 struct i40e_vsi *vsi,
5789 struct i40e_channel *ch)
5790{
5791 i40e_status ret;
5792 int i;
5793 u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
5794
5795 /* Enable ETS TCs with equal BW Share for now across all VSIs */
5796 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5797 if (ch->enabled_tc & BIT(i))
5798 bw_share[i] = 1;
5799 }
5800
5801 /* configure BW for new VSI */
5802 ret = i40e_channel_config_bw(vsi, ch, bw_share);
5803 if (ret) {
5804 dev_info(&vsi->back->pdev->dev,
5805 "Failed configuring TC map %d for channel (seid %u)\n",
5806 ch->enabled_tc, ch->seid);
5807 return ret;
5808 }
5809
5810 for (i = 0; i < ch->num_queue_pairs; i++) {
5811 struct i40e_ring *tx_ring, *rx_ring;
5812 u16 pf_q;
5813
5814 pf_q = ch->base_queue + i;
5815
5816 /* Get to TX ring ptr of main VSI, for re-setup TX queue
5817 * context
5818 */
5819 tx_ring = vsi->tx_rings[pf_q];
5820 tx_ring->ch = ch;
5821
5822 /* Get the RX ring ptr */
5823 rx_ring = vsi->rx_rings[pf_q];
5824 rx_ring->ch = ch;
5825 }
5826
5827 return 0;
5828}
5829
5830/**
5831 * i40e_setup_hw_channel - setup new channel
5832 * @pf: ptr to PF device
5833 * @vsi: the VSI being setup
5834 * @ch: ptr to channel structure
5835 * @uplink_seid: underlying HW switching element (VEB) ID
5836 * @type: type of channel to be created (VMDq2/VF)
5837 *
5838 * Setup new channel (VSI) based on specified type (VMDq2/VF)
5839 * and configures TX rings accordingly
5840 **/
5841static inline int i40e_setup_hw_channel(struct i40e_pf *pf,
5842 struct i40e_vsi *vsi,
5843 struct i40e_channel *ch,
5844 u16 uplink_seid, u8 type)
5845{
5846 int ret;
5847
5848 ch->initialized = false;
5849 ch->base_queue = vsi->next_base_queue;
5850 ch->type = type;
5851
5852 /* Proceed with creation of channel (VMDq2) VSI */
5853 ret = i40e_add_channel(pf, uplink_seid, ch);
5854 if (ret) {
5855 dev_info(&pf->pdev->dev,
5856 "failed to add_channel using uplink_seid %u\n",
5857 uplink_seid);
5858 return ret;
5859 }
5860
5861 /* Mark the successful creation of channel */
5862 ch->initialized = true;
5863
5864 /* Reconfigure TX queues using QTX_CTL register */
5865 ret = i40e_channel_config_tx_ring(pf, vsi, ch);
5866 if (ret) {
5867 dev_info(&pf->pdev->dev,
5868 "failed to configure TX rings for channel %u\n",
5869 ch->seid);
5870 return ret;
5871 }
5872
5873 /* update 'next_base_queue' */
5874 vsi->next_base_queue = vsi->next_base_queue + ch->num_queue_pairs;
5875 dev_dbg(&pf->pdev->dev,
5876 "Added channel: vsi_seid %u, vsi_number %u, stat_counter_idx %u, num_queue_pairs %u, pf->next_base_queue %d\n",
5877 ch->seid, ch->vsi_number, ch->stat_counter_idx,
5878 ch->num_queue_pairs,
5879 vsi->next_base_queue);
5880 return ret;
5881}
5882
5883/**
5884 * i40e_setup_channel - setup new channel using uplink element
5885 * @pf: ptr to PF device
5886 * @type: type of channel to be created (VMDq2/VF)
5887 * @uplink_seid: underlying HW switching element (VEB) ID
5888 * @ch: ptr to channel structure
5889 *
5890 * Setup new channel (VSI) based on specified type (VMDq2/VF)
5891 * and uplink switching element (uplink_seid)
5892 **/
5893static bool i40e_setup_channel(struct i40e_pf *pf, struct i40e_vsi *vsi,
5894 struct i40e_channel *ch)
5895{
5896 u8 vsi_type;
5897 u16 seid;
5898 int ret;
5899
5900 if (vsi->type == I40E_VSI_MAIN) {
5901 vsi_type = I40E_VSI_VMDQ2;
5902 } else {
5903 dev_err(&pf->pdev->dev, "unsupported parent vsi type(%d)\n",
5904 vsi->type);
5905 return false;
5906 }
5907
5908 /* underlying switching element */
5909 seid = pf->vsi[pf->lan_vsi]->uplink_seid;
5910
5911 /* create channel (VSI), configure TX rings */
5912 ret = i40e_setup_hw_channel(pf, vsi, ch, seid, vsi_type);
5913 if (ret) {
5914 dev_err(&pf->pdev->dev, "failed to setup hw_channel\n");
5915 return false;
5916 }
5917
5918 return ch->initialized ? true : false;
5919}
5920
5921/**
Amritha Nambiar2f4b4112017-10-27 02:36:01 -07005922 * i40e_validate_and_set_switch_mode - sets up switch mode correctly
5923 * @vsi: ptr to VSI which has PF backing
5924 *
5925 * Sets up switch mode correctly if it needs to be changed and perform
5926 * what are allowed modes.
5927 **/
5928static int i40e_validate_and_set_switch_mode(struct i40e_vsi *vsi)
5929{
5930 u8 mode;
5931 struct i40e_pf *pf = vsi->back;
5932 struct i40e_hw *hw = &pf->hw;
5933 int ret;
5934
5935 ret = i40e_get_capabilities(pf, i40e_aqc_opc_list_dev_capabilities);
5936 if (ret)
5937 return -EINVAL;
5938
5939 if (hw->dev_caps.switch_mode) {
5940 /* if switch mode is set, support mode2 (non-tunneled for
5941 * cloud filter) for now
5942 */
5943 u32 switch_mode = hw->dev_caps.switch_mode &
5944 I40E_SWITCH_MODE_MASK;
5945 if (switch_mode >= I40E_CLOUD_FILTER_MODE1) {
5946 if (switch_mode == I40E_CLOUD_FILTER_MODE2)
5947 return 0;
5948 dev_err(&pf->pdev->dev,
5949 "Invalid switch_mode (%d), only non-tunneled mode for cloud filter is supported\n",
5950 hw->dev_caps.switch_mode);
5951 return -EINVAL;
5952 }
5953 }
5954
5955 /* Set Bit 7 to be valid */
5956 mode = I40E_AQ_SET_SWITCH_BIT7_VALID;
5957
Amritha Nambiar64e711c2017-11-17 15:51:47 -08005958 /* Set L4type for TCP support */
5959 mode |= I40E_AQ_SET_SWITCH_L4_TYPE_TCP;
Amritha Nambiar2f4b4112017-10-27 02:36:01 -07005960
5961 /* Set cloud filter mode */
5962 mode |= I40E_AQ_SET_SWITCH_MODE_NON_TUNNEL;
5963
5964 /* Prep mode field for set_switch_config */
5965 ret = i40e_aq_set_switch_config(hw, pf->last_sw_conf_flags,
5966 pf->last_sw_conf_valid_flags,
5967 mode, NULL);
5968 if (ret && hw->aq.asq_last_status != I40E_AQ_RC_ESRCH)
5969 dev_err(&pf->pdev->dev,
5970 "couldn't set switch config bits, err %s aq_err %s\n",
5971 i40e_stat_str(hw, ret),
5972 i40e_aq_str(hw,
5973 hw->aq.asq_last_status));
5974
5975 return ret;
5976}
5977
5978/**
Amritha Nambiar8f88b302017-09-07 04:00:17 -07005979 * i40e_create_queue_channel - function to create channel
5980 * @vsi: VSI to be configured
5981 * @ch: ptr to channel (it contains channel specific params)
5982 *
5983 * This function creates channel (VSI) using num_queues specified by user,
5984 * reconfigs RSS if needed.
5985 **/
5986int i40e_create_queue_channel(struct i40e_vsi *vsi,
5987 struct i40e_channel *ch)
5988{
5989 struct i40e_pf *pf = vsi->back;
5990 bool reconfig_rss;
5991 int err;
5992
5993 if (!ch)
5994 return -EINVAL;
5995
5996 if (!ch->num_queue_pairs) {
5997 dev_err(&pf->pdev->dev, "Invalid num_queues requested: %d\n",
5998 ch->num_queue_pairs);
5999 return -EINVAL;
6000 }
6001
6002 /* validate user requested num_queues for channel */
6003 err = i40e_validate_num_queues(pf, ch->num_queue_pairs, vsi,
6004 &reconfig_rss);
6005 if (err) {
6006 dev_info(&pf->pdev->dev, "Failed to validate num_queues (%d)\n",
6007 ch->num_queue_pairs);
6008 return -EINVAL;
6009 }
6010
6011 /* By default we are in VEPA mode, if this is the first VF/VMDq
6012 * VSI to be added switch to VEB mode.
6013 */
6014 if ((!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) ||
6015 (!i40e_is_any_channel(vsi))) {
6016 if (!is_power_of_2(vsi->tc_config.tc_info[0].qcount)) {
6017 dev_dbg(&pf->pdev->dev,
6018 "Failed to create channel. Override queues (%u) not power of 2\n",
6019 vsi->tc_config.tc_info[0].qcount);
6020 return -EINVAL;
6021 }
6022
6023 if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
6024 pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
6025
6026 if (vsi->type == I40E_VSI_MAIN) {
6027 if (pf->flags & I40E_FLAG_TC_MQPRIO)
6028 i40e_do_reset(pf, I40E_PF_RESET_FLAG,
6029 true);
6030 else
6031 i40e_do_reset_safe(pf,
6032 I40E_PF_RESET_FLAG);
6033 }
6034 }
6035 /* now onwards for main VSI, number of queues will be value
6036 * of TC0's queue count
6037 */
6038 }
6039
6040 /* By this time, vsi->cnt_q_avail shall be set to non-zero and
6041 * it should be more than num_queues
6042 */
6043 if (!vsi->cnt_q_avail || vsi->cnt_q_avail < ch->num_queue_pairs) {
6044 dev_dbg(&pf->pdev->dev,
6045 "Error: cnt_q_avail (%u) less than num_queues %d\n",
6046 vsi->cnt_q_avail, ch->num_queue_pairs);
6047 return -EINVAL;
6048 }
6049
6050 /* reconfig_rss only if vsi type is MAIN_VSI */
6051 if (reconfig_rss && (vsi->type == I40E_VSI_MAIN)) {
6052 err = i40e_vsi_reconfig_rss(vsi, ch->num_queue_pairs);
6053 if (err) {
6054 dev_info(&pf->pdev->dev,
6055 "Error: unable to reconfig rss for num_queues (%u)\n",
6056 ch->num_queue_pairs);
6057 return -EINVAL;
6058 }
6059 }
6060
6061 if (!i40e_setup_channel(pf, vsi, ch)) {
6062 dev_info(&pf->pdev->dev, "Failed to setup channel\n");
6063 return -EINVAL;
6064 }
6065
6066 dev_info(&pf->pdev->dev,
6067 "Setup channel (id:%u) utilizing num_queues %d\n",
6068 ch->seid, ch->num_queue_pairs);
6069
Amritha Nambiar2027d4d2017-09-07 04:00:32 -07006070 /* configure VSI for BW limit */
6071 if (ch->max_tx_rate) {
Alan Brady6c32e0d2017-10-09 15:48:45 -07006072 u64 credits = ch->max_tx_rate;
6073
Amritha Nambiar2027d4d2017-09-07 04:00:32 -07006074 if (i40e_set_bw_limit(vsi, ch->seid, ch->max_tx_rate))
6075 return -EINVAL;
6076
Alan Brady6c32e0d2017-10-09 15:48:45 -07006077 do_div(credits, I40E_BW_CREDIT_DIVISOR);
Amritha Nambiar2027d4d2017-09-07 04:00:32 -07006078 dev_dbg(&pf->pdev->dev,
6079 "Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
6080 ch->max_tx_rate,
Alan Brady6c32e0d2017-10-09 15:48:45 -07006081 credits,
6082 ch->seid);
Amritha Nambiar2027d4d2017-09-07 04:00:32 -07006083 }
6084
Amritha Nambiar8f88b302017-09-07 04:00:17 -07006085 /* in case of VF, this will be main SRIOV VSI */
6086 ch->parent_vsi = vsi;
6087
6088 /* and update main_vsi's count for queue_available to use */
6089 vsi->cnt_q_avail -= ch->num_queue_pairs;
6090
6091 return 0;
6092}
6093
6094/**
6095 * i40e_configure_queue_channels - Add queue channel for the given TCs
6096 * @vsi: VSI to be configured
6097 *
6098 * Configures queue channel mapping to the given TCs
6099 **/
6100static int i40e_configure_queue_channels(struct i40e_vsi *vsi)
6101{
6102 struct i40e_channel *ch;
Alan Brady6c32e0d2017-10-09 15:48:45 -07006103 u64 max_rate = 0;
Amritha Nambiar8f88b302017-09-07 04:00:17 -07006104 int ret = 0, i;
6105
6106 /* Create app vsi with the TCs. Main VSI with TC0 is already set up */
Amritha Nambiaraa5cb02a2017-10-27 02:35:40 -07006107 vsi->tc_seid_map[0] = vsi->seid;
Amritha Nambiar8f88b302017-09-07 04:00:17 -07006108 for (i = 1; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6109 if (vsi->tc_config.enabled_tc & BIT(i)) {
6110 ch = kzalloc(sizeof(*ch), GFP_KERNEL);
6111 if (!ch) {
6112 ret = -ENOMEM;
6113 goto err_free;
6114 }
6115
6116 INIT_LIST_HEAD(&ch->list);
6117 ch->num_queue_pairs =
6118 vsi->tc_config.tc_info[i].qcount;
6119 ch->base_queue =
6120 vsi->tc_config.tc_info[i].qoffset;
6121
Amritha Nambiar2027d4d2017-09-07 04:00:32 -07006122 /* Bandwidth limit through tc interface is in bytes/s,
6123 * change to Mbit/s
6124 */
Alan Brady6c32e0d2017-10-09 15:48:45 -07006125 max_rate = vsi->mqprio_qopt.max_rate[i];
6126 do_div(max_rate, I40E_BW_MBPS_DIVISOR);
6127 ch->max_tx_rate = max_rate;
Amritha Nambiar2027d4d2017-09-07 04:00:32 -07006128
Amritha Nambiar8f88b302017-09-07 04:00:17 -07006129 list_add_tail(&ch->list, &vsi->ch_list);
6130
6131 ret = i40e_create_queue_channel(vsi, ch);
6132 if (ret) {
6133 dev_err(&vsi->back->pdev->dev,
6134 "Failed creating queue channel with TC%d: queues %d\n",
6135 i, ch->num_queue_pairs);
6136 goto err_free;
6137 }
Amritha Nambiaraa5cb02a2017-10-27 02:35:40 -07006138 vsi->tc_seid_map[i] = ch->seid;
Amritha Nambiar8f88b302017-09-07 04:00:17 -07006139 }
6140 }
6141 return ret;
6142
6143err_free:
6144 i40e_remove_queue_channels(vsi);
6145 return ret;
6146}
6147
6148/**
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006149 * i40e_veb_config_tc - Configure TCs for given VEB
6150 * @veb: given VEB
6151 * @enabled_tc: TC bitmap
6152 *
6153 * Configures given TC bitmap for VEB (switching) element
6154 **/
6155int i40e_veb_config_tc(struct i40e_veb *veb, u8 enabled_tc)
6156{
6157 struct i40e_aqc_configure_switching_comp_bw_config_data bw_data = {0};
6158 struct i40e_pf *pf = veb->pf;
6159 int ret = 0;
6160 int i;
6161
6162 /* No TCs or already enabled TCs just return */
6163 if (!enabled_tc || veb->enabled_tc == enabled_tc)
6164 return ret;
6165
6166 bw_data.tc_valid_bits = enabled_tc;
6167 /* bw_data.absolute_credits is not set (relative) */
6168
6169 /* Enable ETS TCs with equal BW Share for now */
6170 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006171 if (enabled_tc & BIT(i))
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006172 bw_data.tc_bw_share_credits[i] = 1;
6173 }
6174
6175 ret = i40e_aq_config_switch_comp_bw_config(&pf->hw, veb->seid,
6176 &bw_data, NULL);
6177 if (ret) {
6178 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006179 "VEB bw config failed, err %s aq_err %s\n",
6180 i40e_stat_str(&pf->hw, ret),
6181 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006182 goto out;
6183 }
6184
6185 /* Update the BW information */
6186 ret = i40e_veb_get_bw_info(veb);
6187 if (ret) {
6188 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006189 "Failed getting veb bw config, err %s aq_err %s\n",
6190 i40e_stat_str(&pf->hw, ret),
6191 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006192 }
6193
6194out:
6195 return ret;
6196}
6197
6198#ifdef CONFIG_I40E_DCB
6199/**
6200 * i40e_dcb_reconfigure - Reconfigure all VEBs and VSIs
6201 * @pf: PF struct
6202 *
6203 * Reconfigure VEB/VSIs on a given PF; it is assumed that
6204 * the caller would've quiesce all the VSIs before calling
6205 * this function
6206 **/
6207static void i40e_dcb_reconfigure(struct i40e_pf *pf)
6208{
6209 u8 tc_map = 0;
6210 int ret;
6211 u8 v;
6212
6213 /* Enable the TCs available on PF to all VEBs */
6214 tc_map = i40e_pf_get_tc_map(pf);
6215 for (v = 0; v < I40E_MAX_VEB; v++) {
6216 if (!pf->veb[v])
6217 continue;
6218 ret = i40e_veb_config_tc(pf->veb[v], tc_map);
6219 if (ret) {
6220 dev_info(&pf->pdev->dev,
6221 "Failed configuring TC for VEB seid=%d\n",
6222 pf->veb[v]->seid);
6223 /* Will try to configure as many components */
6224 }
6225 }
6226
6227 /* Update each VSI */
Mitch Williams505682c2014-05-20 08:01:37 +00006228 for (v = 0; v < pf->num_alloc_vsi; v++) {
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006229 if (!pf->vsi[v])
6230 continue;
6231
6232 /* - Enable all TCs for the LAN VSI
6233 * - For all others keep them at TC0 for now
6234 */
6235 if (v == pf->lan_vsi)
6236 tc_map = i40e_pf_get_tc_map(pf);
6237 else
David Ertmanea6acb72016-09-20 07:10:50 -07006238 tc_map = I40E_DEFAULT_TRAFFIC_CLASS;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006239
6240 ret = i40e_vsi_config_tc(pf->vsi[v], tc_map);
6241 if (ret) {
6242 dev_info(&pf->pdev->dev,
6243 "Failed configuring TC for VSI seid=%d\n",
6244 pf->vsi[v]->seid);
6245 /* Will try to configure as many components */
6246 } else {
Neerav Parikh0672a092014-04-01 07:11:47 +00006247 /* Re-configure VSI vectors based on updated TC map */
6248 i40e_vsi_map_rings_to_vectors(pf->vsi[v]);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006249 if (pf->vsi[v]->netdev)
6250 i40e_dcbnl_set_all(pf->vsi[v]);
6251 }
6252 }
6253}
6254
6255/**
Neerav Parikh2fd75f32014-11-12 00:18:20 +00006256 * i40e_resume_port_tx - Resume port Tx
6257 * @pf: PF struct
6258 *
6259 * Resume a port's Tx and issue a PF reset in case of failure to
6260 * resume.
6261 **/
6262static int i40e_resume_port_tx(struct i40e_pf *pf)
6263{
6264 struct i40e_hw *hw = &pf->hw;
6265 int ret;
6266
6267 ret = i40e_aq_resume_port_tx(hw, NULL);
6268 if (ret) {
6269 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006270 "Resume Port Tx failed, err %s aq_err %s\n",
6271 i40e_stat_str(&pf->hw, ret),
6272 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh2fd75f32014-11-12 00:18:20 +00006273 /* Schedule PF reset to recover */
Jacob Keller0da36b92017-04-19 09:25:55 -04006274 set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
Neerav Parikh2fd75f32014-11-12 00:18:20 +00006275 i40e_service_event_schedule(pf);
6276 }
6277
6278 return ret;
6279}
6280
6281/**
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006282 * i40e_init_pf_dcb - Initialize DCB configuration
6283 * @pf: PF being configured
6284 *
6285 * Query the current DCB configuration and cache it
6286 * in the hardware structure
6287 **/
6288static int i40e_init_pf_dcb(struct i40e_pf *pf)
6289{
6290 struct i40e_hw *hw = &pf->hw;
6291 int err = 0;
6292
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +00006293 /* Do not enable DCB for SW1 and SW2 images even if the FW is capable */
Jacob Kellerd36e41d2017-06-23 04:24:46 -04006294 if (pf->hw_features & I40E_HW_NO_DCB_SUPPORT)
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +00006295 goto out;
6296
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006297 /* Get the initial DCB configuration */
6298 err = i40e_init_dcb(hw);
6299 if (!err) {
6300 /* Device/Function is not DCBX capable */
6301 if ((!hw->func_caps.dcb) ||
6302 (hw->dcbx_status == I40E_DCBX_STATUS_DISABLED)) {
6303 dev_info(&pf->pdev->dev,
6304 "DCBX offload is not supported or is disabled for this PF.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006305 } else {
6306 /* When status is not DISABLED then DCBX in FW */
6307 pf->dcbx_cap = DCB_CAP_DCBX_LLD_MANAGED |
6308 DCB_CAP_DCBX_VER_IEEE;
Neerav Parikh4d9b6042014-05-22 06:31:51 +00006309
6310 pf->flags |= I40E_FLAG_DCB_CAPABLE;
Dave Ertmana0362442016-08-29 17:38:26 -07006311 /* Enable DCB tagging only when more than one TC
6312 * or explicitly disable if only one TC
6313 */
Neerav Parikh4d9b6042014-05-22 06:31:51 +00006314 if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
6315 pf->flags |= I40E_FLAG_DCB_ENABLED;
Dave Ertmana0362442016-08-29 17:38:26 -07006316 else
6317 pf->flags &= ~I40E_FLAG_DCB_ENABLED;
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00006318 dev_dbg(&pf->pdev->dev,
6319 "DCBX offload is supported for this PF.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006320 }
Neerav Parikh014269f2014-04-01 07:11:48 +00006321 } else {
Shannon Nelsonaebfc812014-12-11 07:06:38 +00006322 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006323 "Query for DCB configuration failed, err %s aq_err %s\n",
6324 i40e_stat_str(&pf->hw, err),
6325 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006326 }
6327
6328out:
6329 return err;
6330}
6331#endif /* CONFIG_I40E_DCB */
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00006332#define SPEED_SIZE 14
6333#define FC_SIZE 8
6334/**
6335 * i40e_print_link_message - print link up or down
6336 * @vsi: the VSI for which link needs a message
6337 */
Matt Jaredc156f852015-08-27 11:42:39 -04006338void i40e_print_link_message(struct i40e_vsi *vsi, bool isup)
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00006339{
Filip Sadowski7ec9ba12016-11-08 13:05:13 -08006340 enum i40e_aq_link_speed new_speed;
Sudheer Mogilappagari3fded462017-07-14 09:10:18 -04006341 struct i40e_pf *pf = vsi->back;
Shannon Nelsona9165492015-09-03 17:19:00 -04006342 char *speed = "Unknown";
6343 char *fc = "Unknown";
Henry Tieman3e03d7c2016-12-02 12:32:57 -08006344 char *fec = "";
Mariusz Stachura68e49702017-07-12 05:46:14 -04006345 char *req_fec = "";
Henry Tieman3e03d7c2016-12-02 12:32:57 -08006346 char *an = "";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00006347
Sudheer Mogilappagari3fded462017-07-14 09:10:18 -04006348 new_speed = pf->hw.phy.link_info.link_speed;
Filip Sadowski7ec9ba12016-11-08 13:05:13 -08006349
6350 if ((vsi->current_isup == isup) && (vsi->current_speed == new_speed))
Matt Jaredc156f852015-08-27 11:42:39 -04006351 return;
6352 vsi->current_isup = isup;
Filip Sadowski7ec9ba12016-11-08 13:05:13 -08006353 vsi->current_speed = new_speed;
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00006354 if (!isup) {
6355 netdev_info(vsi->netdev, "NIC Link is Down\n");
6356 return;
6357 }
6358
Greg Rose148c2d82014-12-11 07:06:27 +00006359 /* Warn user if link speed on NPAR enabled partition is not at
6360 * least 10GB
6361 */
Sudheer Mogilappagari3fded462017-07-14 09:10:18 -04006362 if (pf->hw.func_caps.npar_enable &&
6363 (pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_1GB ||
6364 pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_100MB))
Greg Rose148c2d82014-12-11 07:06:27 +00006365 netdev_warn(vsi->netdev,
6366 "The partition detected link speed that is less than 10Gbps\n");
6367
Sudheer Mogilappagari3fded462017-07-14 09:10:18 -04006368 switch (pf->hw.phy.link_info.link_speed) {
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00006369 case I40E_LINK_SPEED_40GB:
Shannon Nelsona9165492015-09-03 17:19:00 -04006370 speed = "40 G";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00006371 break;
Jesse Brandeburgae24b402015-03-27 00:12:09 -07006372 case I40E_LINK_SPEED_20GB:
Shannon Nelsona9165492015-09-03 17:19:00 -04006373 speed = "20 G";
Jesse Brandeburgae24b402015-03-27 00:12:09 -07006374 break;
Carolyn Wyborny31232372016-11-21 13:03:48 -08006375 case I40E_LINK_SPEED_25GB:
6376 speed = "25 G";
6377 break;
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00006378 case I40E_LINK_SPEED_10GB:
Shannon Nelsona9165492015-09-03 17:19:00 -04006379 speed = "10 G";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00006380 break;
6381 case I40E_LINK_SPEED_1GB:
Shannon Nelsona9165492015-09-03 17:19:00 -04006382 speed = "1000 M";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00006383 break;
Mitch Williams5960d332014-09-13 07:40:47 +00006384 case I40E_LINK_SPEED_100MB:
Shannon Nelsona9165492015-09-03 17:19:00 -04006385 speed = "100 M";
Mitch Williams5960d332014-09-13 07:40:47 +00006386 break;
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00006387 default:
6388 break;
6389 }
6390
Sudheer Mogilappagari3fded462017-07-14 09:10:18 -04006391 switch (pf->hw.fc.current_mode) {
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00006392 case I40E_FC_FULL:
Shannon Nelsona9165492015-09-03 17:19:00 -04006393 fc = "RX/TX";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00006394 break;
6395 case I40E_FC_TX_PAUSE:
Shannon Nelsona9165492015-09-03 17:19:00 -04006396 fc = "TX";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00006397 break;
6398 case I40E_FC_RX_PAUSE:
Shannon Nelsona9165492015-09-03 17:19:00 -04006399 fc = "RX";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00006400 break;
6401 default:
Shannon Nelsona9165492015-09-03 17:19:00 -04006402 fc = "None";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00006403 break;
6404 }
6405
Sudheer Mogilappagari3fded462017-07-14 09:10:18 -04006406 if (pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_25GB) {
Mariusz Stachura68e49702017-07-12 05:46:14 -04006407 req_fec = ", Requested FEC: None";
Henry Tieman3e03d7c2016-12-02 12:32:57 -08006408 fec = ", FEC: None";
6409 an = ", Autoneg: False";
6410
Sudheer Mogilappagari3fded462017-07-14 09:10:18 -04006411 if (pf->hw.phy.link_info.an_info & I40E_AQ_AN_COMPLETED)
Henry Tieman3e03d7c2016-12-02 12:32:57 -08006412 an = ", Autoneg: True";
6413
Sudheer Mogilappagari3fded462017-07-14 09:10:18 -04006414 if (pf->hw.phy.link_info.fec_info &
Henry Tieman3e03d7c2016-12-02 12:32:57 -08006415 I40E_AQ_CONFIG_FEC_KR_ENA)
6416 fec = ", FEC: CL74 FC-FEC/BASE-R";
Sudheer Mogilappagari3fded462017-07-14 09:10:18 -04006417 else if (pf->hw.phy.link_info.fec_info &
Henry Tieman3e03d7c2016-12-02 12:32:57 -08006418 I40E_AQ_CONFIG_FEC_RS_ENA)
6419 fec = ", FEC: CL108 RS-FEC";
Mariusz Stachura68e49702017-07-12 05:46:14 -04006420
6421 /* 'CL108 RS-FEC' should be displayed when RS is requested, or
6422 * both RS and FC are requested
6423 */
6424 if (vsi->back->hw.phy.link_info.req_fec_info &
6425 (I40E_AQ_REQUEST_FEC_KR | I40E_AQ_REQUEST_FEC_RS)) {
6426 if (vsi->back->hw.phy.link_info.req_fec_info &
6427 I40E_AQ_REQUEST_FEC_RS)
6428 req_fec = ", Requested FEC: CL108 RS-FEC";
6429 else
6430 req_fec = ", Requested FEC: CL74 FC-FEC/BASE-R";
6431 }
Henry Tieman3e03d7c2016-12-02 12:32:57 -08006432 }
6433
Mariusz Stachura68e49702017-07-12 05:46:14 -04006434 netdev_info(vsi->netdev, "NIC Link is Up, %sbps Full Duplex%s%s%s, Flow Control: %s\n",
6435 speed, req_fec, fec, an, fc);
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00006436}
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006437
6438/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006439 * i40e_up_complete - Finish the last steps of bringing up a connection
6440 * @vsi: the VSI being configured
6441 **/
6442static int i40e_up_complete(struct i40e_vsi *vsi)
6443{
6444 struct i40e_pf *pf = vsi->back;
6445 int err;
6446
6447 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
6448 i40e_vsi_configure_msix(vsi);
6449 else
6450 i40e_configure_msi_and_legacy(vsi);
6451
6452 /* start rings */
Filip Sadowski3aa7b742016-10-11 15:26:58 -07006453 err = i40e_vsi_start_rings(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006454 if (err)
6455 return err;
6456
Jacob Keller0da36b92017-04-19 09:25:55 -04006457 clear_bit(__I40E_VSI_DOWN, vsi->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006458 i40e_napi_enable_all(vsi);
6459 i40e_vsi_enable_irq(vsi);
6460
6461 if ((pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP) &&
6462 (vsi->netdev)) {
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00006463 i40e_print_link_message(vsi, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006464 netif_tx_start_all_queues(vsi->netdev);
6465 netif_carrier_on(vsi->netdev);
6466 }
Anjali Singhai Jainca64fa42014-02-11 08:26:30 +00006467
6468 /* replay FDIR SB filters */
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006469 if (vsi->type == I40E_VSI_FDIR) {
6470 /* reset fd counters */
Jacob Keller097dbf52017-02-06 14:38:46 -08006471 pf->fd_add_err = 0;
6472 pf->fd_atr_cnt = 0;
Anjali Singhai Jainca64fa42014-02-11 08:26:30 +00006473 i40e_fdir_filter_restore(vsi);
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006474 }
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06006475
6476 /* On the next run of the service_task, notify any clients of the new
6477 * opened netdev
6478 */
6479 pf->flags |= I40E_FLAG_SERVICE_CLIENT_REQUESTED;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006480 i40e_service_event_schedule(pf);
6481
6482 return 0;
6483}
6484
6485/**
6486 * i40e_vsi_reinit_locked - Reset the VSI
6487 * @vsi: the VSI being configured
6488 *
6489 * Rebuild the ring structs after some configuration
6490 * has changed, e.g. MTU size.
6491 **/
6492static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi)
6493{
6494 struct i40e_pf *pf = vsi->back;
6495
6496 WARN_ON(in_interrupt());
Jacob Keller0da36b92017-04-19 09:25:55 -04006497 while (test_and_set_bit(__I40E_CONFIG_BUSY, pf->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006498 usleep_range(1000, 2000);
6499 i40e_down(vsi);
6500
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006501 i40e_up(vsi);
Jacob Keller0da36b92017-04-19 09:25:55 -04006502 clear_bit(__I40E_CONFIG_BUSY, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006503}
6504
6505/**
6506 * i40e_up - Bring the connection back up after being down
6507 * @vsi: the VSI being configured
6508 **/
6509int i40e_up(struct i40e_vsi *vsi)
6510{
6511 int err;
6512
6513 err = i40e_vsi_configure(vsi);
6514 if (!err)
6515 err = i40e_up_complete(vsi);
6516
6517 return err;
6518}
6519
6520/**
6521 * i40e_down - Shutdown the connection processing
6522 * @vsi: the VSI being stopped
6523 **/
6524void i40e_down(struct i40e_vsi *vsi)
6525{
6526 int i;
6527
6528 /* It is assumed that the caller of this function
Jacob Kellerd19cb642017-04-21 13:38:05 -07006529 * sets the vsi->state __I40E_VSI_DOWN bit.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006530 */
6531 if (vsi->netdev) {
6532 netif_carrier_off(vsi->netdev);
6533 netif_tx_disable(vsi->netdev);
6534 }
6535 i40e_vsi_disable_irq(vsi);
Filip Sadowski3aa7b742016-10-11 15:26:58 -07006536 i40e_vsi_stop_rings(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006537 i40e_napi_disable_all(vsi);
6538
6539 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck9f65e152013-09-28 06:00:58 +00006540 i40e_clean_tx_ring(vsi->tx_rings[i]);
Björn Töpel74608d12017-05-24 07:55:35 +02006541 if (i40e_enabled_xdp_vsi(vsi))
6542 i40e_clean_tx_ring(vsi->xdp_rings[i]);
Alexander Duyck9f65e152013-09-28 06:00:58 +00006543 i40e_clean_rx_ring(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006544 }
Catherine Sullivanf980d442016-05-16 10:26:34 -07006545
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006546}
6547
6548/**
Amritha Nambiara9ce82f2017-09-07 04:00:22 -07006549 * i40e_validate_mqprio_qopt- validate queue mapping info
6550 * @vsi: the VSI being configured
6551 * @mqprio_qopt: queue parametrs
6552 **/
6553static int i40e_validate_mqprio_qopt(struct i40e_vsi *vsi,
6554 struct tc_mqprio_qopt_offload *mqprio_qopt)
6555{
Amritha Nambiar2027d4d2017-09-07 04:00:32 -07006556 u64 sum_max_rate = 0;
Alan Brady6c32e0d2017-10-09 15:48:45 -07006557 u64 max_rate = 0;
Amritha Nambiara9ce82f2017-09-07 04:00:22 -07006558 int i;
6559
6560 if (mqprio_qopt->qopt.offset[0] != 0 ||
6561 mqprio_qopt->qopt.num_tc < 1 ||
6562 mqprio_qopt->qopt.num_tc > I40E_MAX_TRAFFIC_CLASS)
6563 return -EINVAL;
6564 for (i = 0; ; i++) {
6565 if (!mqprio_qopt->qopt.count[i])
6566 return -EINVAL;
Amritha Nambiar2027d4d2017-09-07 04:00:32 -07006567 if (mqprio_qopt->min_rate[i]) {
6568 dev_err(&vsi->back->pdev->dev,
6569 "Invalid min tx rate (greater than 0) specified\n");
Amritha Nambiara9ce82f2017-09-07 04:00:22 -07006570 return -EINVAL;
Amritha Nambiar2027d4d2017-09-07 04:00:32 -07006571 }
Alan Brady6c32e0d2017-10-09 15:48:45 -07006572 max_rate = mqprio_qopt->max_rate[i];
6573 do_div(max_rate, I40E_BW_MBPS_DIVISOR);
6574 sum_max_rate += max_rate;
Amritha Nambiar2027d4d2017-09-07 04:00:32 -07006575
Amritha Nambiara9ce82f2017-09-07 04:00:22 -07006576 if (i >= mqprio_qopt->qopt.num_tc - 1)
6577 break;
6578 if (mqprio_qopt->qopt.offset[i + 1] !=
6579 (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i]))
6580 return -EINVAL;
6581 }
6582 if (vsi->num_queue_pairs <
6583 (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i])) {
6584 return -EINVAL;
6585 }
Amritha Nambiar2027d4d2017-09-07 04:00:32 -07006586 if (sum_max_rate > i40e_get_link_speed(vsi)) {
6587 dev_err(&vsi->back->pdev->dev,
6588 "Invalid max tx rate specified\n");
6589 return -EINVAL;
6590 }
Amritha Nambiara9ce82f2017-09-07 04:00:22 -07006591 return 0;
6592}
6593
6594/**
6595 * i40e_vsi_set_default_tc_config - set default values for tc configuration
6596 * @vsi: the VSI being configured
6597 **/
6598static void i40e_vsi_set_default_tc_config(struct i40e_vsi *vsi)
6599{
6600 u16 qcount;
6601 int i;
6602
6603 /* Only TC0 is enabled */
6604 vsi->tc_config.numtc = 1;
6605 vsi->tc_config.enabled_tc = 1;
6606 qcount = min_t(int, vsi->alloc_queue_pairs,
6607 i40e_pf_get_max_q_per_tc(vsi->back));
6608 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6609 /* For the TC that is not enabled set the offset to to default
6610 * queue and allocate one queue for the given TC.
6611 */
6612 vsi->tc_config.tc_info[i].qoffset = 0;
6613 if (i == 0)
6614 vsi->tc_config.tc_info[i].qcount = qcount;
6615 else
6616 vsi->tc_config.tc_info[i].qcount = 1;
6617 vsi->tc_config.tc_info[i].netdev_tc = 0;
6618 }
6619}
6620
6621/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006622 * i40e_setup_tc - configure multiple traffic classes
6623 * @netdev: net device to configure
Amritha Nambiara9ce82f2017-09-07 04:00:22 -07006624 * @type_data: tc offload data
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006625 **/
Amritha Nambiara9ce82f2017-09-07 04:00:22 -07006626static int i40e_setup_tc(struct net_device *netdev, void *type_data)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006627{
Amritha Nambiara9ce82f2017-09-07 04:00:22 -07006628 struct tc_mqprio_qopt_offload *mqprio_qopt = type_data;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006629 struct i40e_netdev_priv *np = netdev_priv(netdev);
6630 struct i40e_vsi *vsi = np->vsi;
6631 struct i40e_pf *pf = vsi->back;
Amritha Nambiara9ce82f2017-09-07 04:00:22 -07006632 u8 enabled_tc = 0, num_tc, hw;
6633 bool need_reset = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006634 int ret = -EINVAL;
Amritha Nambiara9ce82f2017-09-07 04:00:22 -07006635 u16 mode;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006636 int i;
6637
Amritha Nambiara9ce82f2017-09-07 04:00:22 -07006638 num_tc = mqprio_qopt->qopt.num_tc;
6639 hw = mqprio_qopt->qopt.hw;
6640 mode = mqprio_qopt->mode;
6641 if (!hw) {
6642 pf->flags &= ~I40E_FLAG_TC_MQPRIO;
6643 memcpy(&vsi->mqprio_qopt, mqprio_qopt, sizeof(*mqprio_qopt));
6644 goto config_tc;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006645 }
6646
6647 /* Check if MFP enabled */
6648 if (pf->flags & I40E_FLAG_MFP_ENABLED) {
Amritha Nambiara9ce82f2017-09-07 04:00:22 -07006649 netdev_info(netdev,
6650 "Configuring TC not supported in MFP mode\n");
6651 return ret;
6652 }
6653 switch (mode) {
6654 case TC_MQPRIO_MODE_DCB:
6655 pf->flags &= ~I40E_FLAG_TC_MQPRIO;
6656
6657 /* Check if DCB enabled to continue */
6658 if (!(pf->flags & I40E_FLAG_DCB_ENABLED)) {
6659 netdev_info(netdev,
6660 "DCB is not enabled for adapter\n");
6661 return ret;
6662 }
6663
6664 /* Check whether tc count is within enabled limit */
6665 if (num_tc > i40e_pf_get_num_tc(pf)) {
6666 netdev_info(netdev,
6667 "TC count greater than enabled on link for adapter\n");
6668 return ret;
6669 }
6670 break;
6671 case TC_MQPRIO_MODE_CHANNEL:
6672 if (pf->flags & I40E_FLAG_DCB_ENABLED) {
6673 netdev_info(netdev,
6674 "Full offload of TC Mqprio options is not supported when DCB is enabled\n");
6675 return ret;
6676 }
6677 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
6678 return ret;
6679 ret = i40e_validate_mqprio_qopt(vsi, mqprio_qopt);
6680 if (ret)
6681 return ret;
6682 memcpy(&vsi->mqprio_qopt, mqprio_qopt,
6683 sizeof(*mqprio_qopt));
6684 pf->flags |= I40E_FLAG_TC_MQPRIO;
6685 pf->flags &= ~I40E_FLAG_DCB_ENABLED;
6686 break;
6687 default:
6688 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006689 }
6690
Amritha Nambiara9ce82f2017-09-07 04:00:22 -07006691config_tc:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006692 /* Generate TC map for number of tc requested */
Amritha Nambiara9ce82f2017-09-07 04:00:22 -07006693 for (i = 0; i < num_tc; i++)
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006694 enabled_tc |= BIT(i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006695
6696 /* Requesting same TC configuration as already enabled */
Amritha Nambiara9ce82f2017-09-07 04:00:22 -07006697 if (enabled_tc == vsi->tc_config.enabled_tc &&
6698 mode != TC_MQPRIO_MODE_CHANNEL)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006699 return 0;
6700
6701 /* Quiesce VSI queues */
6702 i40e_quiesce_vsi(vsi);
6703
Amritha Nambiara9ce82f2017-09-07 04:00:22 -07006704 if (!hw && !(pf->flags & I40E_FLAG_TC_MQPRIO))
6705 i40e_remove_queue_channels(vsi);
6706
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006707 /* Configure VSI for enabled TCs */
6708 ret = i40e_vsi_config_tc(vsi, enabled_tc);
6709 if (ret) {
6710 netdev_info(netdev, "Failed configuring TC for VSI seid=%d\n",
6711 vsi->seid);
Amritha Nambiara9ce82f2017-09-07 04:00:22 -07006712 need_reset = true;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006713 goto exit;
6714 }
6715
Amritha Nambiar8f88b302017-09-07 04:00:17 -07006716 if (pf->flags & I40E_FLAG_TC_MQPRIO) {
Amritha Nambiar2027d4d2017-09-07 04:00:32 -07006717 if (vsi->mqprio_qopt.max_rate[0]) {
Alan Brady6c32e0d2017-10-09 15:48:45 -07006718 u64 max_tx_rate = vsi->mqprio_qopt.max_rate[0];
6719
6720 do_div(max_tx_rate, I40E_BW_MBPS_DIVISOR);
Amritha Nambiar2027d4d2017-09-07 04:00:32 -07006721 ret = i40e_set_bw_limit(vsi, vsi->seid, max_tx_rate);
6722 if (!ret) {
Alan Brady6c32e0d2017-10-09 15:48:45 -07006723 u64 credits = max_tx_rate;
6724
6725 do_div(credits, I40E_BW_CREDIT_DIVISOR);
Amritha Nambiar2027d4d2017-09-07 04:00:32 -07006726 dev_dbg(&vsi->back->pdev->dev,
6727 "Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
6728 max_tx_rate,
Alan Brady6c32e0d2017-10-09 15:48:45 -07006729 credits,
Amritha Nambiar2027d4d2017-09-07 04:00:32 -07006730 vsi->seid);
6731 } else {
6732 need_reset = true;
6733 goto exit;
6734 }
6735 }
Amritha Nambiar8f88b302017-09-07 04:00:17 -07006736 ret = i40e_configure_queue_channels(vsi);
6737 if (ret) {
6738 netdev_info(netdev,
6739 "Failed configuring queue channels\n");
Amritha Nambiara9ce82f2017-09-07 04:00:22 -07006740 need_reset = true;
Amritha Nambiar8f88b302017-09-07 04:00:17 -07006741 goto exit;
6742 }
6743 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006744
6745exit:
Amritha Nambiara9ce82f2017-09-07 04:00:22 -07006746 /* Reset the configuration data to defaults, only TC0 is enabled */
6747 if (need_reset) {
6748 i40e_vsi_set_default_tc_config(vsi);
6749 need_reset = false;
6750 }
6751
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006752 /* Unquiesce VSI */
6753 i40e_unquiesce_vsi(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006754 return ret;
6755}
6756
Amritha Nambiar2f4b4112017-10-27 02:36:01 -07006757/**
6758 * i40e_set_cld_element - sets cloud filter element data
6759 * @filter: cloud filter rule
6760 * @cld: ptr to cloud filter element data
6761 *
6762 * This is helper function to copy data into cloud filter element
6763 **/
6764static inline void
6765i40e_set_cld_element(struct i40e_cloud_filter *filter,
6766 struct i40e_aqc_cloud_filters_element_data *cld)
6767{
6768 int i, j;
6769 u32 ipa;
6770
6771 memset(cld, 0, sizeof(*cld));
6772 ether_addr_copy(cld->outer_mac, filter->dst_mac);
6773 ether_addr_copy(cld->inner_mac, filter->src_mac);
6774
6775 if (filter->n_proto != ETH_P_IP && filter->n_proto != ETH_P_IPV6)
6776 return;
6777
6778 if (filter->n_proto == ETH_P_IPV6) {
6779#define IPV6_MAX_INDEX (ARRAY_SIZE(filter->dst_ipv6) - 1)
6780 for (i = 0, j = 0; i < ARRAY_SIZE(filter->dst_ipv6);
6781 i++, j += 2) {
6782 ipa = be32_to_cpu(filter->dst_ipv6[IPV6_MAX_INDEX - i]);
6783 ipa = cpu_to_le32(ipa);
6784 memcpy(&cld->ipaddr.raw_v6.data[j], &ipa, sizeof(ipa));
6785 }
6786 } else {
6787 ipa = be32_to_cpu(filter->dst_ipv4);
6788 memcpy(&cld->ipaddr.v4.data, &ipa, sizeof(ipa));
6789 }
6790
6791 cld->inner_vlan = cpu_to_le16(ntohs(filter->vlan_id));
6792
6793 /* tenant_id is not supported by FW now, once the support is enabled
6794 * fill the cld->tenant_id with cpu_to_le32(filter->tenant_id)
6795 */
6796 if (filter->tenant_id)
6797 return;
6798}
6799
6800/**
6801 * i40e_add_del_cloud_filter - Add/del cloud filter
6802 * @vsi: pointer to VSI
6803 * @filter: cloud filter rule
6804 * @add: if true, add, if false, delete
6805 *
6806 * Add or delete a cloud filter for a specific flow spec.
6807 * Returns 0 if the filter were successfully added.
6808 **/
6809static int i40e_add_del_cloud_filter(struct i40e_vsi *vsi,
6810 struct i40e_cloud_filter *filter, bool add)
6811{
6812 struct i40e_aqc_cloud_filters_element_data cld_filter;
6813 struct i40e_pf *pf = vsi->back;
6814 int ret;
6815 static const u16 flag_table[128] = {
6816 [I40E_CLOUD_FILTER_FLAGS_OMAC] =
6817 I40E_AQC_ADD_CLOUD_FILTER_OMAC,
6818 [I40E_CLOUD_FILTER_FLAGS_IMAC] =
6819 I40E_AQC_ADD_CLOUD_FILTER_IMAC,
6820 [I40E_CLOUD_FILTER_FLAGS_IMAC_IVLAN] =
6821 I40E_AQC_ADD_CLOUD_FILTER_IMAC_IVLAN,
6822 [I40E_CLOUD_FILTER_FLAGS_IMAC_TEN_ID] =
6823 I40E_AQC_ADD_CLOUD_FILTER_IMAC_TEN_ID,
6824 [I40E_CLOUD_FILTER_FLAGS_OMAC_TEN_ID_IMAC] =
6825 I40E_AQC_ADD_CLOUD_FILTER_OMAC_TEN_ID_IMAC,
6826 [I40E_CLOUD_FILTER_FLAGS_IMAC_IVLAN_TEN_ID] =
6827 I40E_AQC_ADD_CLOUD_FILTER_IMAC_IVLAN_TEN_ID,
6828 [I40E_CLOUD_FILTER_FLAGS_IIP] =
6829 I40E_AQC_ADD_CLOUD_FILTER_IIP,
6830 };
6831
6832 if (filter->flags >= ARRAY_SIZE(flag_table))
6833 return I40E_ERR_CONFIG;
6834
6835 /* copy element needed to add cloud filter from filter */
6836 i40e_set_cld_element(filter, &cld_filter);
6837
6838 if (filter->tunnel_type != I40E_CLOUD_TNL_TYPE_NONE)
6839 cld_filter.flags = cpu_to_le16(filter->tunnel_type <<
6840 I40E_AQC_ADD_CLOUD_TNL_TYPE_SHIFT);
6841
6842 if (filter->n_proto == ETH_P_IPV6)
6843 cld_filter.flags |= cpu_to_le16(flag_table[filter->flags] |
6844 I40E_AQC_ADD_CLOUD_FLAGS_IPV6);
6845 else
6846 cld_filter.flags |= cpu_to_le16(flag_table[filter->flags] |
6847 I40E_AQC_ADD_CLOUD_FLAGS_IPV4);
6848
6849 if (add)
6850 ret = i40e_aq_add_cloud_filters(&pf->hw, filter->seid,
6851 &cld_filter, 1);
6852 else
6853 ret = i40e_aq_rem_cloud_filters(&pf->hw, filter->seid,
6854 &cld_filter, 1);
6855 if (ret)
6856 dev_dbg(&pf->pdev->dev,
6857 "Failed to %s cloud filter using l4 port %u, err %d aq_err %d\n",
6858 add ? "add" : "delete", filter->dst_port, ret,
6859 pf->hw.aq.asq_last_status);
6860 else
6861 dev_info(&pf->pdev->dev,
6862 "%s cloud filter for VSI: %d\n",
6863 add ? "Added" : "Deleted", filter->seid);
6864 return ret;
6865}
6866
6867/**
6868 * i40e_add_del_cloud_filter_big_buf - Add/del cloud filter using big_buf
6869 * @vsi: pointer to VSI
6870 * @filter: cloud filter rule
6871 * @add: if true, add, if false, delete
6872 *
6873 * Add or delete a cloud filter for a specific flow spec using big buffer.
6874 * Returns 0 if the filter were successfully added.
6875 **/
6876static int i40e_add_del_cloud_filter_big_buf(struct i40e_vsi *vsi,
6877 struct i40e_cloud_filter *filter,
6878 bool add)
6879{
6880 struct i40e_aqc_cloud_filters_element_bb cld_filter;
6881 struct i40e_pf *pf = vsi->back;
6882 int ret;
6883
6884 /* Both (src/dst) valid mac_addr are not supported */
6885 if ((is_valid_ether_addr(filter->dst_mac) &&
6886 is_valid_ether_addr(filter->src_mac)) ||
6887 (is_multicast_ether_addr(filter->dst_mac) &&
6888 is_multicast_ether_addr(filter->src_mac)))
Amritha Nambiar64e711c2017-11-17 15:51:47 -08006889 return -EOPNOTSUPP;
Amritha Nambiar2f4b4112017-10-27 02:36:01 -07006890
Amritha Nambiar64e711c2017-11-17 15:51:47 -08006891 /* Big buffer cloud filter needs 'L4 port' to be non-zero. Also, UDP
6892 * ports are not supported via big buffer now.
Amritha Nambiar2f4b4112017-10-27 02:36:01 -07006893 */
Amritha Nambiar64e711c2017-11-17 15:51:47 -08006894 if (!filter->dst_port || filter->ip_proto == IPPROTO_UDP)
6895 return -EOPNOTSUPP;
Amritha Nambiar2f4b4112017-10-27 02:36:01 -07006896
6897 /* adding filter using src_port/src_ip is not supported at this stage */
6898 if (filter->src_port || filter->src_ipv4 ||
6899 !ipv6_addr_any(&filter->ip.v6.src_ip6))
Amritha Nambiar64e711c2017-11-17 15:51:47 -08006900 return -EOPNOTSUPP;
Amritha Nambiar2f4b4112017-10-27 02:36:01 -07006901
6902 /* copy element needed to add cloud filter from filter */
6903 i40e_set_cld_element(filter, &cld_filter.element);
6904
6905 if (is_valid_ether_addr(filter->dst_mac) ||
6906 is_valid_ether_addr(filter->src_mac) ||
6907 is_multicast_ether_addr(filter->dst_mac) ||
6908 is_multicast_ether_addr(filter->src_mac)) {
6909 /* MAC + IP : unsupported mode */
6910 if (filter->dst_ipv4)
Amritha Nambiar64e711c2017-11-17 15:51:47 -08006911 return -EOPNOTSUPP;
Amritha Nambiar2f4b4112017-10-27 02:36:01 -07006912
6913 /* since we validated that L4 port must be valid before
6914 * we get here, start with respective "flags" value
6915 * and update if vlan is present or not
6916 */
6917 cld_filter.element.flags =
6918 cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_MAC_PORT);
6919
6920 if (filter->vlan_id) {
6921 cld_filter.element.flags =
6922 cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_MAC_VLAN_PORT);
6923 }
6924
6925 } else if (filter->dst_ipv4 ||
6926 !ipv6_addr_any(&filter->ip.v6.dst_ip6)) {
6927 cld_filter.element.flags =
6928 cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_IP_PORT);
6929 if (filter->n_proto == ETH_P_IPV6)
6930 cld_filter.element.flags |=
6931 cpu_to_le16(I40E_AQC_ADD_CLOUD_FLAGS_IPV6);
6932 else
6933 cld_filter.element.flags |=
6934 cpu_to_le16(I40E_AQC_ADD_CLOUD_FLAGS_IPV4);
6935 } else {
6936 dev_err(&pf->pdev->dev,
6937 "either mac or ip has to be valid for cloud filter\n");
6938 return -EINVAL;
6939 }
6940
6941 /* Now copy L4 port in Byte 6..7 in general fields */
6942 cld_filter.general_fields[I40E_AQC_ADD_CLOUD_FV_FLU_0X16_WORD0] =
6943 be16_to_cpu(filter->dst_port);
6944
6945 if (add) {
6946 /* Validate current device switch mode, change if necessary */
6947 ret = i40e_validate_and_set_switch_mode(vsi);
6948 if (ret) {
6949 dev_err(&pf->pdev->dev,
6950 "failed to set switch mode, ret %d\n",
6951 ret);
6952 return ret;
6953 }
6954
6955 ret = i40e_aq_add_cloud_filters_bb(&pf->hw, filter->seid,
6956 &cld_filter, 1);
6957 } else {
6958 ret = i40e_aq_rem_cloud_filters_bb(&pf->hw, filter->seid,
6959 &cld_filter, 1);
6960 }
6961
6962 if (ret)
6963 dev_dbg(&pf->pdev->dev,
6964 "Failed to %s cloud filter(big buffer) err %d aq_err %d\n",
6965 add ? "add" : "delete", ret, pf->hw.aq.asq_last_status);
6966 else
6967 dev_info(&pf->pdev->dev,
6968 "%s cloud filter for VSI: %d, L4 port: %d\n",
6969 add ? "add" : "delete", filter->seid,
6970 ntohs(filter->dst_port));
6971 return ret;
6972}
6973
6974/**
6975 * i40e_parse_cls_flower - Parse tc flower filters provided by kernel
6976 * @vsi: Pointer to VSI
6977 * @cls_flower: Pointer to struct tc_cls_flower_offload
6978 * @filter: Pointer to cloud filter structure
6979 *
6980 **/
6981static int i40e_parse_cls_flower(struct i40e_vsi *vsi,
6982 struct tc_cls_flower_offload *f,
6983 struct i40e_cloud_filter *filter)
6984{
6985 u16 n_proto_mask = 0, n_proto_key = 0, addr_type = 0;
6986 struct i40e_pf *pf = vsi->back;
6987 u8 field_flags = 0;
6988
6989 if (f->dissector->used_keys &
6990 ~(BIT(FLOW_DISSECTOR_KEY_CONTROL) |
6991 BIT(FLOW_DISSECTOR_KEY_BASIC) |
6992 BIT(FLOW_DISSECTOR_KEY_ETH_ADDRS) |
6993 BIT(FLOW_DISSECTOR_KEY_VLAN) |
6994 BIT(FLOW_DISSECTOR_KEY_IPV4_ADDRS) |
6995 BIT(FLOW_DISSECTOR_KEY_IPV6_ADDRS) |
6996 BIT(FLOW_DISSECTOR_KEY_PORTS) |
6997 BIT(FLOW_DISSECTOR_KEY_ENC_KEYID))) {
6998 dev_err(&pf->pdev->dev, "Unsupported key used: 0x%x\n",
6999 f->dissector->used_keys);
7000 return -EOPNOTSUPP;
7001 }
7002
7003 if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_ENC_KEYID)) {
7004 struct flow_dissector_key_keyid *key =
7005 skb_flow_dissector_target(f->dissector,
7006 FLOW_DISSECTOR_KEY_ENC_KEYID,
7007 f->key);
7008
7009 struct flow_dissector_key_keyid *mask =
7010 skb_flow_dissector_target(f->dissector,
7011 FLOW_DISSECTOR_KEY_ENC_KEYID,
7012 f->mask);
7013
7014 if (mask->keyid != 0)
7015 field_flags |= I40E_CLOUD_FIELD_TEN_ID;
7016
7017 filter->tenant_id = be32_to_cpu(key->keyid);
7018 }
7019
7020 if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_BASIC)) {
7021 struct flow_dissector_key_basic *key =
7022 skb_flow_dissector_target(f->dissector,
7023 FLOW_DISSECTOR_KEY_BASIC,
7024 f->key);
7025
7026 struct flow_dissector_key_basic *mask =
7027 skb_flow_dissector_target(f->dissector,
7028 FLOW_DISSECTOR_KEY_BASIC,
7029 f->mask);
7030
7031 n_proto_key = ntohs(key->n_proto);
7032 n_proto_mask = ntohs(mask->n_proto);
7033
7034 if (n_proto_key == ETH_P_ALL) {
7035 n_proto_key = 0;
7036 n_proto_mask = 0;
7037 }
7038 filter->n_proto = n_proto_key & n_proto_mask;
7039 filter->ip_proto = key->ip_proto;
7040 }
7041
7042 if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
7043 struct flow_dissector_key_eth_addrs *key =
7044 skb_flow_dissector_target(f->dissector,
7045 FLOW_DISSECTOR_KEY_ETH_ADDRS,
7046 f->key);
7047
7048 struct flow_dissector_key_eth_addrs *mask =
7049 skb_flow_dissector_target(f->dissector,
7050 FLOW_DISSECTOR_KEY_ETH_ADDRS,
7051 f->mask);
7052
7053 /* use is_broadcast and is_zero to check for all 0xf or 0 */
7054 if (!is_zero_ether_addr(mask->dst)) {
7055 if (is_broadcast_ether_addr(mask->dst)) {
7056 field_flags |= I40E_CLOUD_FIELD_OMAC;
7057 } else {
7058 dev_err(&pf->pdev->dev, "Bad ether dest mask %pM\n",
7059 mask->dst);
7060 return I40E_ERR_CONFIG;
7061 }
7062 }
7063
7064 if (!is_zero_ether_addr(mask->src)) {
7065 if (is_broadcast_ether_addr(mask->src)) {
7066 field_flags |= I40E_CLOUD_FIELD_IMAC;
7067 } else {
7068 dev_err(&pf->pdev->dev, "Bad ether src mask %pM\n",
7069 mask->src);
7070 return I40E_ERR_CONFIG;
7071 }
7072 }
7073 ether_addr_copy(filter->dst_mac, key->dst);
7074 ether_addr_copy(filter->src_mac, key->src);
7075 }
7076
7077 if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_VLAN)) {
7078 struct flow_dissector_key_vlan *key =
7079 skb_flow_dissector_target(f->dissector,
7080 FLOW_DISSECTOR_KEY_VLAN,
7081 f->key);
7082 struct flow_dissector_key_vlan *mask =
7083 skb_flow_dissector_target(f->dissector,
7084 FLOW_DISSECTOR_KEY_VLAN,
7085 f->mask);
7086
7087 if (mask->vlan_id) {
7088 if (mask->vlan_id == VLAN_VID_MASK) {
7089 field_flags |= I40E_CLOUD_FIELD_IVLAN;
7090
7091 } else {
7092 dev_err(&pf->pdev->dev, "Bad vlan mask 0x%04x\n",
7093 mask->vlan_id);
7094 return I40E_ERR_CONFIG;
7095 }
7096 }
7097
7098 filter->vlan_id = cpu_to_be16(key->vlan_id);
7099 }
7100
7101 if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_CONTROL)) {
7102 struct flow_dissector_key_control *key =
7103 skb_flow_dissector_target(f->dissector,
7104 FLOW_DISSECTOR_KEY_CONTROL,
7105 f->key);
7106
7107 addr_type = key->addr_type;
7108 }
7109
7110 if (addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
7111 struct flow_dissector_key_ipv4_addrs *key =
7112 skb_flow_dissector_target(f->dissector,
7113 FLOW_DISSECTOR_KEY_IPV4_ADDRS,
7114 f->key);
7115 struct flow_dissector_key_ipv4_addrs *mask =
7116 skb_flow_dissector_target(f->dissector,
7117 FLOW_DISSECTOR_KEY_IPV4_ADDRS,
7118 f->mask);
7119
7120 if (mask->dst) {
7121 if (mask->dst == cpu_to_be32(0xffffffff)) {
7122 field_flags |= I40E_CLOUD_FIELD_IIP;
7123 } else {
7124 mask->dst = be32_to_cpu(mask->dst);
7125 dev_err(&pf->pdev->dev, "Bad ip dst mask %pI4\n",
7126 &mask->dst);
7127 return I40E_ERR_CONFIG;
7128 }
7129 }
7130
7131 if (mask->src) {
7132 if (mask->src == cpu_to_be32(0xffffffff)) {
7133 field_flags |= I40E_CLOUD_FIELD_IIP;
7134 } else {
7135 mask->src = be32_to_cpu(mask->src);
7136 dev_err(&pf->pdev->dev, "Bad ip src mask %pI4\n",
7137 &mask->src);
7138 return I40E_ERR_CONFIG;
7139 }
7140 }
7141
7142 if (field_flags & I40E_CLOUD_FIELD_TEN_ID) {
7143 dev_err(&pf->pdev->dev, "Tenant id not allowed for ip filter\n");
7144 return I40E_ERR_CONFIG;
7145 }
7146 filter->dst_ipv4 = key->dst;
7147 filter->src_ipv4 = key->src;
7148 }
7149
7150 if (addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
7151 struct flow_dissector_key_ipv6_addrs *key =
7152 skb_flow_dissector_target(f->dissector,
7153 FLOW_DISSECTOR_KEY_IPV6_ADDRS,
7154 f->key);
7155 struct flow_dissector_key_ipv6_addrs *mask =
7156 skb_flow_dissector_target(f->dissector,
7157 FLOW_DISSECTOR_KEY_IPV6_ADDRS,
7158 f->mask);
7159
7160 /* src and dest IPV6 address should not be LOOPBACK
7161 * (0:0:0:0:0:0:0:1), which can be represented as ::1
7162 */
7163 if (ipv6_addr_loopback(&key->dst) ||
7164 ipv6_addr_loopback(&key->src)) {
7165 dev_err(&pf->pdev->dev,
7166 "Bad ipv6, addr is LOOPBACK\n");
7167 return I40E_ERR_CONFIG;
7168 }
7169 if (!ipv6_addr_any(&mask->dst) || !ipv6_addr_any(&mask->src))
7170 field_flags |= I40E_CLOUD_FIELD_IIP;
7171
7172 memcpy(&filter->src_ipv6, &key->src.s6_addr32,
7173 sizeof(filter->src_ipv6));
7174 memcpy(&filter->dst_ipv6, &key->dst.s6_addr32,
7175 sizeof(filter->dst_ipv6));
7176 }
7177
7178 if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_PORTS)) {
7179 struct flow_dissector_key_ports *key =
7180 skb_flow_dissector_target(f->dissector,
7181 FLOW_DISSECTOR_KEY_PORTS,
7182 f->key);
7183 struct flow_dissector_key_ports *mask =
7184 skb_flow_dissector_target(f->dissector,
7185 FLOW_DISSECTOR_KEY_PORTS,
7186 f->mask);
7187
7188 if (mask->src) {
7189 if (mask->src == cpu_to_be16(0xffff)) {
7190 field_flags |= I40E_CLOUD_FIELD_IIP;
7191 } else {
7192 dev_err(&pf->pdev->dev, "Bad src port mask 0x%04x\n",
7193 be16_to_cpu(mask->src));
7194 return I40E_ERR_CONFIG;
7195 }
7196 }
7197
7198 if (mask->dst) {
7199 if (mask->dst == cpu_to_be16(0xffff)) {
7200 field_flags |= I40E_CLOUD_FIELD_IIP;
7201 } else {
7202 dev_err(&pf->pdev->dev, "Bad dst port mask 0x%04x\n",
7203 be16_to_cpu(mask->dst));
7204 return I40E_ERR_CONFIG;
7205 }
7206 }
7207
7208 filter->dst_port = key->dst;
7209 filter->src_port = key->src;
7210
7211 switch (filter->ip_proto) {
7212 case IPPROTO_TCP:
7213 case IPPROTO_UDP:
7214 break;
7215 default:
7216 dev_err(&pf->pdev->dev,
7217 "Only UDP and TCP transport are supported\n");
7218 return -EINVAL;
7219 }
7220 }
7221 filter->flags = field_flags;
7222 return 0;
7223}
7224
7225/**
7226 * i40e_handle_tclass: Forward to a traffic class on the device
7227 * @vsi: Pointer to VSI
7228 * @tc: traffic class index on the device
7229 * @filter: Pointer to cloud filter structure
7230 *
7231 **/
7232static int i40e_handle_tclass(struct i40e_vsi *vsi, u32 tc,
7233 struct i40e_cloud_filter *filter)
7234{
7235 struct i40e_channel *ch, *ch_tmp;
7236
7237 /* direct to a traffic class on the same device */
7238 if (tc == 0) {
7239 filter->seid = vsi->seid;
7240 return 0;
7241 } else if (vsi->tc_config.enabled_tc & BIT(tc)) {
7242 if (!filter->dst_port) {
7243 dev_err(&vsi->back->pdev->dev,
7244 "Specify destination port to direct to traffic class that is not default\n");
7245 return -EINVAL;
7246 }
7247 if (list_empty(&vsi->ch_list))
7248 return -EINVAL;
7249 list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list,
7250 list) {
7251 if (ch->seid == vsi->tc_seid_map[tc])
7252 filter->seid = ch->seid;
7253 }
7254 return 0;
7255 }
7256 dev_err(&vsi->back->pdev->dev, "TC is not enabled\n");
7257 return -EINVAL;
7258}
7259
7260/**
7261 * i40e_configure_clsflower - Configure tc flower filters
7262 * @vsi: Pointer to VSI
7263 * @cls_flower: Pointer to struct tc_cls_flower_offload
7264 *
7265 **/
7266static int i40e_configure_clsflower(struct i40e_vsi *vsi,
7267 struct tc_cls_flower_offload *cls_flower)
7268{
7269 int tc = tc_classid_to_hwtc(vsi->netdev, cls_flower->classid);
7270 struct i40e_cloud_filter *filter = NULL;
7271 struct i40e_pf *pf = vsi->back;
7272 int err = 0;
7273
7274 if (tc < 0) {
7275 dev_err(&vsi->back->pdev->dev, "Invalid traffic class\n");
Jiri Pirkobc4244c2017-12-22 12:45:16 +01007276 return -EOPNOTSUPP;
Amritha Nambiar2f4b4112017-10-27 02:36:01 -07007277 }
7278
7279 if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state) ||
7280 test_bit(__I40E_RESET_INTR_RECEIVED, pf->state))
7281 return -EBUSY;
7282
7283 if (pf->fdir_pf_active_filters ||
7284 (!hlist_empty(&pf->fdir_filter_list))) {
7285 dev_err(&vsi->back->pdev->dev,
7286 "Flow Director Sideband filters exists, turn ntuple off to configure cloud filters\n");
7287 return -EINVAL;
7288 }
7289
7290 if (vsi->back->flags & I40E_FLAG_FD_SB_ENABLED) {
7291 dev_err(&vsi->back->pdev->dev,
7292 "Disable Flow Director Sideband, configuring Cloud filters via tc-flower\n");
7293 vsi->back->flags &= ~I40E_FLAG_FD_SB_ENABLED;
7294 vsi->back->flags |= I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
7295 }
7296
7297 filter = kzalloc(sizeof(*filter), GFP_KERNEL);
7298 if (!filter)
7299 return -ENOMEM;
7300
7301 filter->cookie = cls_flower->cookie;
7302
7303 err = i40e_parse_cls_flower(vsi, cls_flower, filter);
7304 if (err < 0)
7305 goto err;
7306
7307 err = i40e_handle_tclass(vsi, tc, filter);
7308 if (err < 0)
7309 goto err;
7310
7311 /* Add cloud filter */
7312 if (filter->dst_port)
7313 err = i40e_add_del_cloud_filter_big_buf(vsi, filter, true);
7314 else
7315 err = i40e_add_del_cloud_filter(vsi, filter, true);
7316
7317 if (err) {
7318 dev_err(&pf->pdev->dev,
7319 "Failed to add cloud filter, err %s\n",
7320 i40e_stat_str(&pf->hw, err));
Amritha Nambiar2f4b4112017-10-27 02:36:01 -07007321 goto err;
7322 }
7323
7324 /* add filter to the ordered list */
7325 INIT_HLIST_NODE(&filter->cloud_node);
7326
7327 hlist_add_head(&filter->cloud_node, &pf->cloud_filter_list);
7328
7329 pf->num_cloud_filters++;
7330
7331 return err;
7332err:
7333 kfree(filter);
7334 return err;
7335}
7336
7337/**
7338 * i40e_find_cloud_filter - Find the could filter in the list
7339 * @vsi: Pointer to VSI
7340 * @cookie: filter specific cookie
7341 *
7342 **/
7343static struct i40e_cloud_filter *i40e_find_cloud_filter(struct i40e_vsi *vsi,
7344 unsigned long *cookie)
7345{
7346 struct i40e_cloud_filter *filter = NULL;
7347 struct hlist_node *node2;
7348
7349 hlist_for_each_entry_safe(filter, node2,
7350 &vsi->back->cloud_filter_list, cloud_node)
7351 if (!memcmp(cookie, &filter->cookie, sizeof(filter->cookie)))
7352 return filter;
7353 return NULL;
7354}
7355
7356/**
7357 * i40e_delete_clsflower - Remove tc flower filters
7358 * @vsi: Pointer to VSI
7359 * @cls_flower: Pointer to struct tc_cls_flower_offload
7360 *
7361 **/
7362static int i40e_delete_clsflower(struct i40e_vsi *vsi,
7363 struct tc_cls_flower_offload *cls_flower)
7364{
7365 struct i40e_cloud_filter *filter = NULL;
7366 struct i40e_pf *pf = vsi->back;
7367 int err = 0;
7368
7369 filter = i40e_find_cloud_filter(vsi, &cls_flower->cookie);
7370
7371 if (!filter)
7372 return -EINVAL;
7373
7374 hash_del(&filter->cloud_node);
7375
7376 if (filter->dst_port)
7377 err = i40e_add_del_cloud_filter_big_buf(vsi, filter, false);
7378 else
7379 err = i40e_add_del_cloud_filter(vsi, filter, false);
7380
7381 kfree(filter);
7382 if (err) {
7383 dev_err(&pf->pdev->dev,
7384 "Failed to delete cloud filter, err %s\n",
7385 i40e_stat_str(&pf->hw, err));
7386 return i40e_aq_rc_to_posix(err, pf->hw.aq.asq_last_status);
7387 }
7388
7389 pf->num_cloud_filters--;
7390 if (!pf->num_cloud_filters)
7391 if ((pf->flags & I40E_FLAG_FD_SB_TO_CLOUD_FILTER) &&
7392 !(pf->flags & I40E_FLAG_FD_SB_INACTIVE)) {
7393 pf->flags |= I40E_FLAG_FD_SB_ENABLED;
7394 pf->flags &= ~I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
7395 pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
7396 }
7397 return 0;
7398}
7399
7400/**
7401 * i40e_setup_tc_cls_flower - flower classifier offloads
7402 * @netdev: net device to configure
7403 * @type_data: offload data
7404 **/
7405static int i40e_setup_tc_cls_flower(struct i40e_netdev_priv *np,
7406 struct tc_cls_flower_offload *cls_flower)
7407{
7408 struct i40e_vsi *vsi = np->vsi;
7409
7410 if (cls_flower->common.chain_index)
7411 return -EOPNOTSUPP;
7412
7413 switch (cls_flower->command) {
7414 case TC_CLSFLOWER_REPLACE:
7415 return i40e_configure_clsflower(vsi, cls_flower);
7416 case TC_CLSFLOWER_DESTROY:
7417 return i40e_delete_clsflower(vsi, cls_flower);
7418 case TC_CLSFLOWER_STATS:
7419 return -EOPNOTSUPP;
7420 default:
7421 return -EINVAL;
7422 }
7423}
7424
7425static int i40e_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
7426 void *cb_priv)
7427{
7428 struct i40e_netdev_priv *np = cb_priv;
7429
7430 switch (type) {
7431 case TC_SETUP_CLSFLOWER:
7432 return i40e_setup_tc_cls_flower(np, type_data);
7433
7434 default:
7435 return -EOPNOTSUPP;
7436 }
7437}
7438
7439static int i40e_setup_tc_block(struct net_device *dev,
7440 struct tc_block_offload *f)
7441{
7442 struct i40e_netdev_priv *np = netdev_priv(dev);
7443
7444 if (f->binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
7445 return -EOPNOTSUPP;
7446
7447 switch (f->command) {
7448 case TC_BLOCK_BIND:
7449 return tcf_block_cb_register(f->block, i40e_setup_tc_block_cb,
7450 np, np);
7451 case TC_BLOCK_UNBIND:
7452 tcf_block_cb_unregister(f->block, i40e_setup_tc_block_cb, np);
7453 return 0;
7454 default:
7455 return -EOPNOTSUPP;
7456 }
7457}
7458
Jiri Pirko2572ac52017-08-07 10:15:17 +02007459static int __i40e_setup_tc(struct net_device *netdev, enum tc_setup_type type,
Jiri Pirkode4784c2017-08-07 10:15:32 +02007460 void *type_data)
John Fastabende4c67342016-02-16 21:16:15 -08007461{
Amritha Nambiar2f4b4112017-10-27 02:36:01 -07007462 switch (type) {
Nogah Frankel575ed7d2017-11-06 07:23:42 +01007463 case TC_SETUP_QDISC_MQPRIO:
Amritha Nambiar2f4b4112017-10-27 02:36:01 -07007464 return i40e_setup_tc(netdev, type_data);
7465 case TC_SETUP_BLOCK:
7466 return i40e_setup_tc_block(netdev, type_data);
7467 default:
Jiri Pirko38cf0422017-08-07 10:15:31 +02007468 return -EOPNOTSUPP;
Amritha Nambiar2f4b4112017-10-27 02:36:01 -07007469 }
John Fastabende4c67342016-02-16 21:16:15 -08007470}
7471
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007472/**
7473 * i40e_open - Called when a network interface is made active
7474 * @netdev: network interface device structure
7475 *
7476 * The open entry point is called when a network interface is made
7477 * active by the system (IFF_UP). At this point all resources needed
7478 * for transmit and receive operations are allocated, the interrupt
7479 * handler is registered with the OS, the netdev watchdog subtask is
7480 * enabled, and the stack is notified that the interface is ready.
7481 *
7482 * Returns 0 on success, negative value on failure
7483 **/
Vasu Dev38e00432014-08-01 13:27:03 -07007484int i40e_open(struct net_device *netdev)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007485{
7486 struct i40e_netdev_priv *np = netdev_priv(netdev);
7487 struct i40e_vsi *vsi = np->vsi;
7488 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007489 int err;
7490
Shannon Nelson4eb3f762014-03-06 08:59:58 +00007491 /* disallow open during test or if eeprom is broken */
Jacob Keller0da36b92017-04-19 09:25:55 -04007492 if (test_bit(__I40E_TESTING, pf->state) ||
7493 test_bit(__I40E_BAD_EEPROM, pf->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007494 return -EBUSY;
7495
7496 netif_carrier_off(netdev);
7497
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00007498 err = i40e_vsi_open(vsi);
7499 if (err)
7500 return err;
7501
Jesse Brandeburg059dab62014-04-01 09:07:20 +00007502 /* configure global TSO hardware offload settings */
7503 wr32(&pf->hw, I40E_GLLAN_TSOMSK_F, be32_to_cpu(TCP_FLAG_PSH |
7504 TCP_FLAG_FIN) >> 16);
7505 wr32(&pf->hw, I40E_GLLAN_TSOMSK_M, be32_to_cpu(TCP_FLAG_PSH |
7506 TCP_FLAG_FIN |
7507 TCP_FLAG_CWR) >> 16);
7508 wr32(&pf->hw, I40E_GLLAN_TSOMSK_L, be32_to_cpu(TCP_FLAG_CWR) >> 16);
7509
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07007510 udp_tunnel_get_rx_info(netdev);
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007511
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00007512 return 0;
7513}
7514
7515/**
7516 * i40e_vsi_open -
7517 * @vsi: the VSI to open
7518 *
7519 * Finish initialization of the VSI.
7520 *
7521 * Returns 0 on success, negative value on failure
Maciej Sosin373149f2017-04-05 07:50:55 -04007522 *
7523 * Note: expects to be called while under rtnl_lock()
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00007524 **/
7525int i40e_vsi_open(struct i40e_vsi *vsi)
7526{
7527 struct i40e_pf *pf = vsi->back;
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00007528 char int_name[I40E_INT_NAME_STR_LEN];
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00007529 int err;
7530
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007531 /* allocate descriptors */
7532 err = i40e_vsi_setup_tx_resources(vsi);
7533 if (err)
7534 goto err_setup_tx;
7535 err = i40e_vsi_setup_rx_resources(vsi);
7536 if (err)
7537 goto err_setup_rx;
7538
7539 err = i40e_vsi_configure(vsi);
7540 if (err)
7541 goto err_setup_rx;
7542
Shannon Nelsonc22e3c62014-03-14 07:32:25 +00007543 if (vsi->netdev) {
7544 snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
7545 dev_driver_string(&pf->pdev->dev), vsi->netdev->name);
7546 err = i40e_vsi_request_irq(vsi, int_name);
7547 if (err)
7548 goto err_setup_rx;
7549
7550 /* Notify the stack of the actual queue counts. */
7551 err = netif_set_real_num_tx_queues(vsi->netdev,
7552 vsi->num_queue_pairs);
7553 if (err)
7554 goto err_set_queues;
7555
7556 err = netif_set_real_num_rx_queues(vsi->netdev,
7557 vsi->num_queue_pairs);
7558 if (err)
7559 goto err_set_queues;
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00007560
7561 } else if (vsi->type == I40E_VSI_FDIR) {
Carolyn Wybornye240f672014-12-11 07:06:37 +00007562 snprintf(int_name, sizeof(int_name) - 1, "%s-%s:fdir",
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +00007563 dev_driver_string(&pf->pdev->dev),
7564 dev_name(&pf->pdev->dev));
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00007565 err = i40e_vsi_request_irq(vsi, int_name);
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +00007566
Shannon Nelsonc22e3c62014-03-14 07:32:25 +00007567 } else {
Jean Sacrence9ccb12014-05-01 14:31:18 +00007568 err = -EINVAL;
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00007569 goto err_setup_rx;
7570 }
Anjali Singhai Jain25946dd2013-11-26 10:49:14 +00007571
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007572 err = i40e_up_complete(vsi);
7573 if (err)
7574 goto err_up_complete;
7575
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007576 return 0;
7577
7578err_up_complete:
7579 i40e_down(vsi);
Anjali Singhai Jain25946dd2013-11-26 10:49:14 +00007580err_set_queues:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007581 i40e_vsi_free_irq(vsi);
7582err_setup_rx:
7583 i40e_vsi_free_rx_resources(vsi);
7584err_setup_tx:
7585 i40e_vsi_free_tx_resources(vsi);
7586 if (vsi == pf->vsi[pf->lan_vsi])
Amritha Nambiarff424182017-09-07 04:00:11 -07007587 i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007588
7589 return err;
7590}
7591
7592/**
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00007593 * i40e_fdir_filter_exit - Cleans up the Flow Director accounting
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00007594 * @pf: Pointer to PF
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00007595 *
7596 * This function destroys the hlist where all the Flow Director
7597 * filters were saved.
7598 **/
7599static void i40e_fdir_filter_exit(struct i40e_pf *pf)
7600{
7601 struct i40e_fdir_filter *filter;
Jacob Keller0e588de2017-02-06 14:38:50 -08007602 struct i40e_flex_pit *pit_entry, *tmp;
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00007603 struct hlist_node *node2;
7604
7605 hlist_for_each_entry_safe(filter, node2,
7606 &pf->fdir_filter_list, fdir_node) {
7607 hlist_del(&filter->fdir_node);
7608 kfree(filter);
7609 }
Jacob Keller097dbf52017-02-06 14:38:46 -08007610
Jacob Keller0e588de2017-02-06 14:38:50 -08007611 list_for_each_entry_safe(pit_entry, tmp, &pf->l3_flex_pit_list, list) {
7612 list_del(&pit_entry->list);
7613 kfree(pit_entry);
7614 }
7615 INIT_LIST_HEAD(&pf->l3_flex_pit_list);
7616
7617 list_for_each_entry_safe(pit_entry, tmp, &pf->l4_flex_pit_list, list) {
7618 list_del(&pit_entry->list);
7619 kfree(pit_entry);
7620 }
7621 INIT_LIST_HEAD(&pf->l4_flex_pit_list);
7622
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00007623 pf->fdir_pf_active_filters = 0;
Jacob Keller097dbf52017-02-06 14:38:46 -08007624 pf->fd_tcp4_filter_cnt = 0;
7625 pf->fd_udp4_filter_cnt = 0;
Jacob Kellerf223c872017-02-06 14:38:51 -08007626 pf->fd_sctp4_filter_cnt = 0;
Jacob Keller097dbf52017-02-06 14:38:46 -08007627 pf->fd_ip4_filter_cnt = 0;
Jacob Keller3bcee1e2017-02-06 14:38:46 -08007628
7629 /* Reprogram the default input set for TCP/IPv4 */
7630 i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_TCP,
7631 I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
7632 I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
7633
7634 /* Reprogram the default input set for UDP/IPv4 */
7635 i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_UDP,
7636 I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
7637 I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
7638
7639 /* Reprogram the default input set for SCTP/IPv4 */
7640 i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_SCTP,
7641 I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
7642 I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
7643
7644 /* Reprogram the default input set for Other/IPv4 */
7645 i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_OTHER,
7646 I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00007647}
7648
7649/**
Amritha Nambiaraaf66502017-10-27 02:35:56 -07007650 * i40e_cloud_filter_exit - Cleans up the cloud filters
7651 * @pf: Pointer to PF
7652 *
7653 * This function destroys the hlist where all the cloud filters
7654 * were saved.
7655 **/
7656static void i40e_cloud_filter_exit(struct i40e_pf *pf)
7657{
7658 struct i40e_cloud_filter *cfilter;
7659 struct hlist_node *node;
7660
7661 hlist_for_each_entry_safe(cfilter, node,
7662 &pf->cloud_filter_list, cloud_node) {
7663 hlist_del(&cfilter->cloud_node);
7664 kfree(cfilter);
7665 }
7666 pf->num_cloud_filters = 0;
Amritha Nambiar2f4b4112017-10-27 02:36:01 -07007667
7668 if ((pf->flags & I40E_FLAG_FD_SB_TO_CLOUD_FILTER) &&
7669 !(pf->flags & I40E_FLAG_FD_SB_INACTIVE)) {
7670 pf->flags |= I40E_FLAG_FD_SB_ENABLED;
7671 pf->flags &= ~I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
7672 pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
7673 }
Amritha Nambiaraaf66502017-10-27 02:35:56 -07007674}
7675
7676/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007677 * i40e_close - Disables a network interface
7678 * @netdev: network interface device structure
7679 *
7680 * The close entry point is called when an interface is de-activated
7681 * by the OS. The hardware is still under the driver's control, but
7682 * this netdev interface is disabled.
7683 *
7684 * Returns 0, this is not allowed to fail
7685 **/
Vasu Dev38e00432014-08-01 13:27:03 -07007686int i40e_close(struct net_device *netdev)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007687{
7688 struct i40e_netdev_priv *np = netdev_priv(netdev);
7689 struct i40e_vsi *vsi = np->vsi;
7690
Shannon Nelson90ef8d42014-03-14 07:32:26 +00007691 i40e_vsi_close(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007692
7693 return 0;
7694}
7695
7696/**
7697 * i40e_do_reset - Start a PF or Core Reset sequence
7698 * @pf: board private structure
7699 * @reset_flags: which reset is requested
Maciej Sosin373149f2017-04-05 07:50:55 -04007700 * @lock_acquired: indicates whether or not the lock has been acquired
7701 * before this function was called.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007702 *
7703 * The essential difference in resets is that the PF Reset
7704 * doesn't clear the packet buffers, doesn't reset the PE
7705 * firmware, and doesn't bother the other PFs on the chip.
7706 **/
Maciej Sosin373149f2017-04-05 07:50:55 -04007707void i40e_do_reset(struct i40e_pf *pf, u32 reset_flags, bool lock_acquired)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007708{
7709 u32 val;
7710
7711 WARN_ON(in_interrupt());
7712
Mitch Williams263fc482014-04-23 04:50:11 +00007713
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007714 /* do the biggest reset indicated */
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04007715 if (reset_flags & BIT_ULL(__I40E_GLOBAL_RESET_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007716
7717 /* Request a Global Reset
7718 *
7719 * This will start the chip's countdown to the actual full
7720 * chip reset event, and a warning interrupt to be sent
7721 * to all PFs, including the requestor. Our handler
7722 * for the warning interrupt will deal with the shutdown
7723 * and recovery of the switch setup.
7724 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00007725 dev_dbg(&pf->pdev->dev, "GlobalR requested\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007726 val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
7727 val |= I40E_GLGEN_RTRIG_GLOBR_MASK;
7728 wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
7729
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04007730 } else if (reset_flags & BIT_ULL(__I40E_CORE_RESET_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007731
7732 /* Request a Core Reset
7733 *
7734 * Same as Global Reset, except does *not* include the MAC/PHY
7735 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00007736 dev_dbg(&pf->pdev->dev, "CoreR requested\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007737 val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
7738 val |= I40E_GLGEN_RTRIG_CORER_MASK;
7739 wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
7740 i40e_flush(&pf->hw);
7741
Amritha Nambiarff424182017-09-07 04:00:11 -07007742 } else if (reset_flags & I40E_PF_RESET_FLAG) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007743
7744 /* Request a PF Reset
7745 *
7746 * Resets only the PF-specific registers
7747 *
7748 * This goes directly to the tear-down and rebuild of
7749 * the switch, since we need to do all the recovery as
7750 * for the Core Reset.
7751 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00007752 dev_dbg(&pf->pdev->dev, "PFR requested\n");
Maciej Sosin373149f2017-04-05 07:50:55 -04007753 i40e_handle_reset_warning(pf, lock_acquired);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007754
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04007755 } else if (reset_flags & BIT_ULL(__I40E_REINIT_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007756 int v;
7757
7758 /* Find the VSI(s) that requested a re-init */
7759 dev_info(&pf->pdev->dev,
7760 "VSI reinit requested\n");
Mitch Williams505682c2014-05-20 08:01:37 +00007761 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007762 struct i40e_vsi *vsi = pf->vsi[v];
Jesse Brandeburg6995b362015-08-28 17:55:54 -04007763
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007764 if (vsi != NULL &&
Jacob Kellerd19cb642017-04-21 13:38:05 -07007765 test_and_clear_bit(__I40E_VSI_REINIT_REQUESTED,
Jacob Keller0da36b92017-04-19 09:25:55 -04007766 vsi->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007767 i40e_vsi_reinit_locked(pf->vsi[v]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007768 }
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04007769 } else if (reset_flags & BIT_ULL(__I40E_DOWN_REQUESTED)) {
Neerav Parikhb5d06f02014-06-03 23:50:17 +00007770 int v;
7771
7772 /* Find the VSI(s) that needs to be brought down */
7773 dev_info(&pf->pdev->dev, "VSI down requested\n");
7774 for (v = 0; v < pf->num_alloc_vsi; v++) {
7775 struct i40e_vsi *vsi = pf->vsi[v];
Jesse Brandeburg6995b362015-08-28 17:55:54 -04007776
Neerav Parikhb5d06f02014-06-03 23:50:17 +00007777 if (vsi != NULL &&
Jacob Kellerd19cb642017-04-21 13:38:05 -07007778 test_and_clear_bit(__I40E_VSI_DOWN_REQUESTED,
Jacob Keller0da36b92017-04-19 09:25:55 -04007779 vsi->state)) {
7780 set_bit(__I40E_VSI_DOWN, vsi->state);
Neerav Parikhb5d06f02014-06-03 23:50:17 +00007781 i40e_down(vsi);
Neerav Parikhb5d06f02014-06-03 23:50:17 +00007782 }
7783 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007784 } else {
7785 dev_info(&pf->pdev->dev,
7786 "bad reset request 0x%08x\n", reset_flags);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007787 }
7788}
7789
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08007790#ifdef CONFIG_I40E_DCB
7791/**
7792 * i40e_dcb_need_reconfig - Check if DCB needs reconfig
7793 * @pf: board private structure
7794 * @old_cfg: current DCB config
7795 * @new_cfg: new DCB config
7796 **/
7797bool i40e_dcb_need_reconfig(struct i40e_pf *pf,
7798 struct i40e_dcbx_config *old_cfg,
7799 struct i40e_dcbx_config *new_cfg)
7800{
7801 bool need_reconfig = false;
7802
7803 /* Check if ETS configuration has changed */
7804 if (memcmp(&new_cfg->etscfg,
7805 &old_cfg->etscfg,
7806 sizeof(new_cfg->etscfg))) {
7807 /* If Priority Table has changed reconfig is needed */
7808 if (memcmp(&new_cfg->etscfg.prioritytable,
7809 &old_cfg->etscfg.prioritytable,
7810 sizeof(new_cfg->etscfg.prioritytable))) {
7811 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00007812 dev_dbg(&pf->pdev->dev, "ETS UP2TC changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08007813 }
7814
7815 if (memcmp(&new_cfg->etscfg.tcbwtable,
7816 &old_cfg->etscfg.tcbwtable,
7817 sizeof(new_cfg->etscfg.tcbwtable)))
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00007818 dev_dbg(&pf->pdev->dev, "ETS TC BW Table changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08007819
7820 if (memcmp(&new_cfg->etscfg.tsatable,
7821 &old_cfg->etscfg.tsatable,
7822 sizeof(new_cfg->etscfg.tsatable)))
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00007823 dev_dbg(&pf->pdev->dev, "ETS TSA Table changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08007824 }
7825
7826 /* Check if PFC configuration has changed */
7827 if (memcmp(&new_cfg->pfc,
7828 &old_cfg->pfc,
7829 sizeof(new_cfg->pfc))) {
7830 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00007831 dev_dbg(&pf->pdev->dev, "PFC config change detected.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08007832 }
7833
7834 /* Check if APP Table has changed */
7835 if (memcmp(&new_cfg->app,
7836 &old_cfg->app,
Dave Jones3d9667a2014-01-27 23:11:09 -05007837 sizeof(new_cfg->app))) {
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08007838 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00007839 dev_dbg(&pf->pdev->dev, "APP Table change detected.\n");
Dave Jones3d9667a2014-01-27 23:11:09 -05007840 }
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08007841
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04007842 dev_dbg(&pf->pdev->dev, "dcb need_reconfig=%d\n", need_reconfig);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08007843 return need_reconfig;
7844}
7845
7846/**
7847 * i40e_handle_lldp_event - Handle LLDP Change MIB event
7848 * @pf: board private structure
7849 * @e: event info posted on ARQ
7850 **/
7851static int i40e_handle_lldp_event(struct i40e_pf *pf,
7852 struct i40e_arq_event_info *e)
7853{
7854 struct i40e_aqc_lldp_get_mib *mib =
7855 (struct i40e_aqc_lldp_get_mib *)&e->desc.params.raw;
7856 struct i40e_hw *hw = &pf->hw;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08007857 struct i40e_dcbx_config tmp_dcbx_cfg;
7858 bool need_reconfig = false;
7859 int ret = 0;
7860 u8 type;
7861
Neerav Parikh4d9b6042014-05-22 06:31:51 +00007862 /* Not DCB capable or capability disabled */
David Ertmanea6acb72016-09-20 07:10:50 -07007863 if (!(pf->flags & I40E_FLAG_DCB_CAPABLE))
Neerav Parikh4d9b6042014-05-22 06:31:51 +00007864 return ret;
7865
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08007866 /* Ignore if event is not for Nearest Bridge */
7867 type = ((mib->type >> I40E_AQ_LLDP_BRIDGE_TYPE_SHIFT)
7868 & I40E_AQ_LLDP_BRIDGE_TYPE_MASK);
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04007869 dev_dbg(&pf->pdev->dev, "LLDP event mib bridge type 0x%x\n", type);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08007870 if (type != I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE)
7871 return ret;
7872
7873 /* Check MIB Type and return if event for Remote MIB update */
7874 type = mib->type & I40E_AQ_LLDP_MIB_TYPE_MASK;
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00007875 dev_dbg(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04007876 "LLDP event mib type %s\n", type ? "remote" : "local");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08007877 if (type == I40E_AQ_LLDP_MIB_REMOTE) {
7878 /* Update the remote cached instance and return */
7879 ret = i40e_aq_get_dcb_config(hw, I40E_AQ_LLDP_MIB_REMOTE,
7880 I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE,
7881 &hw->remote_dcbx_config);
7882 goto exit;
7883 }
7884
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00007885 /* Store the old configuration */
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07007886 tmp_dcbx_cfg = hw->local_dcbx_config;
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00007887
Neerav Parikh750fcbc2015-02-24 06:58:47 +00007888 /* Reset the old DCBx configuration data */
7889 memset(&hw->local_dcbx_config, 0, sizeof(hw->local_dcbx_config));
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00007890 /* Get updated DCBX data from firmware */
7891 ret = i40e_get_dcb_config(&pf->hw);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08007892 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04007893 dev_info(&pf->pdev->dev,
7894 "Failed querying DCB configuration data from firmware, err %s aq_err %s\n",
7895 i40e_stat_str(&pf->hw, ret),
7896 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08007897 goto exit;
7898 }
7899
7900 /* No change detected in DCBX configs */
Neerav Parikh750fcbc2015-02-24 06:58:47 +00007901 if (!memcmp(&tmp_dcbx_cfg, &hw->local_dcbx_config,
7902 sizeof(tmp_dcbx_cfg))) {
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00007903 dev_dbg(&pf->pdev->dev, "No change detected in DCBX configuration.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08007904 goto exit;
7905 }
7906
Neerav Parikh750fcbc2015-02-24 06:58:47 +00007907 need_reconfig = i40e_dcb_need_reconfig(pf, &tmp_dcbx_cfg,
7908 &hw->local_dcbx_config);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08007909
Neerav Parikh750fcbc2015-02-24 06:58:47 +00007910 i40e_dcbnl_flush_apps(pf, &tmp_dcbx_cfg, &hw->local_dcbx_config);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08007911
7912 if (!need_reconfig)
7913 goto exit;
7914
Neerav Parikh4d9b6042014-05-22 06:31:51 +00007915 /* Enable DCB tagging only when more than one TC */
Neerav Parikh750fcbc2015-02-24 06:58:47 +00007916 if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
Neerav Parikh4d9b6042014-05-22 06:31:51 +00007917 pf->flags |= I40E_FLAG_DCB_ENABLED;
7918 else
7919 pf->flags &= ~I40E_FLAG_DCB_ENABLED;
7920
Jacob Keller0da36b92017-04-19 09:25:55 -04007921 set_bit(__I40E_PORT_SUSPENDED, pf->state);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08007922 /* Reconfiguration needed quiesce all VSIs */
7923 i40e_pf_quiesce_all_vsi(pf);
7924
7925 /* Changes in configuration update VEB/VSI */
7926 i40e_dcb_reconfigure(pf);
7927
Neerav Parikh2fd75f32014-11-12 00:18:20 +00007928 ret = i40e_resume_port_tx(pf);
7929
Jacob Keller0da36b92017-04-19 09:25:55 -04007930 clear_bit(__I40E_PORT_SUSPENDED, pf->state);
Neerav Parikh2fd75f32014-11-12 00:18:20 +00007931 /* In case of error no point in resuming VSIs */
Neerav Parikh69129dc2014-11-12 00:18:46 +00007932 if (ret)
7933 goto exit;
7934
Neerav Parikh3fe06f42016-02-17 16:12:15 -08007935 /* Wait for the PF's queues to be disabled */
7936 ret = i40e_pf_wait_queues_disabled(pf);
Parikh, Neerav11e47702015-02-21 06:43:55 +00007937 if (ret) {
7938 /* Schedule PF reset to recover */
Jacob Keller0da36b92017-04-19 09:25:55 -04007939 set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
Parikh, Neerav11e47702015-02-21 06:43:55 +00007940 i40e_service_event_schedule(pf);
7941 } else {
Neerav Parikh2fd75f32014-11-12 00:18:20 +00007942 i40e_pf_unquiesce_all_vsi(pf);
Mitch Williams0ef2d5a2017-01-24 10:24:00 -08007943 pf->flags |= (I40E_FLAG_SERVICE_CLIENT_REQUESTED |
7944 I40E_FLAG_CLIENT_L2_CHANGE);
Parikh, Neerav11e47702015-02-21 06:43:55 +00007945 }
7946
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08007947exit:
7948 return ret;
7949}
7950#endif /* CONFIG_I40E_DCB */
7951
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007952/**
Anjali Singhai Jain233261862013-11-26 10:49:22 +00007953 * i40e_do_reset_safe - Protected reset path for userland calls.
7954 * @pf: board private structure
7955 * @reset_flags: which reset is requested
7956 *
7957 **/
7958void i40e_do_reset_safe(struct i40e_pf *pf, u32 reset_flags)
7959{
7960 rtnl_lock();
Maciej Sosin373149f2017-04-05 07:50:55 -04007961 i40e_do_reset(pf, reset_flags, true);
Anjali Singhai Jain233261862013-11-26 10:49:22 +00007962 rtnl_unlock();
7963}
7964
7965/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007966 * i40e_handle_lan_overflow_event - Handler for LAN queue overflow event
7967 * @pf: board private structure
7968 * @e: event info posted on ARQ
7969 *
7970 * Handler for LAN Queue Overflow Event generated by the firmware for PF
7971 * and VF queues
7972 **/
7973static void i40e_handle_lan_overflow_event(struct i40e_pf *pf,
7974 struct i40e_arq_event_info *e)
7975{
7976 struct i40e_aqc_lan_overflow *data =
7977 (struct i40e_aqc_lan_overflow *)&e->desc.params.raw;
7978 u32 queue = le32_to_cpu(data->prtdcb_rupto);
7979 u32 qtx_ctl = le32_to_cpu(data->otx_ctl);
7980 struct i40e_hw *hw = &pf->hw;
7981 struct i40e_vf *vf;
7982 u16 vf_id;
7983
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00007984 dev_dbg(&pf->pdev->dev, "overflow Rx Queue Number = %d QTX_CTL=0x%08x\n",
7985 queue, qtx_ctl);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007986
7987 /* Queue belongs to VF, find the VF and issue VF reset */
7988 if (((qtx_ctl & I40E_QTX_CTL_PFVF_Q_MASK)
7989 >> I40E_QTX_CTL_PFVF_Q_SHIFT) == I40E_QTX_CTL_VF_QUEUE) {
7990 vf_id = (u16)((qtx_ctl & I40E_QTX_CTL_VFVM_INDX_MASK)
7991 >> I40E_QTX_CTL_VFVM_INDX_SHIFT);
7992 vf_id -= hw->func_caps.vf_base_id;
7993 vf = &pf->vf[vf_id];
7994 i40e_vc_notify_vf_reset(vf);
7995 /* Allow VF to process pending reset notification */
7996 msleep(20);
7997 i40e_reset_vf(vf, false);
7998 }
7999}
8000
8001/**
Anjali Singhai Jain12957382014-06-04 04:22:47 +00008002 * i40e_get_cur_guaranteed_fd_count - Get the consumed guaranteed FD filters
8003 * @pf: board private structure
8004 **/
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00008005u32 i40e_get_cur_guaranteed_fd_count(struct i40e_pf *pf)
Anjali Singhai Jain12957382014-06-04 04:22:47 +00008006{
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00008007 u32 val, fcnt_prog;
Anjali Singhai Jain12957382014-06-04 04:22:47 +00008008
8009 val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
8010 fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK);
8011 return fcnt_prog;
8012}
8013
8014/**
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00008015 * i40e_get_current_fd_count - Get total FD filters programmed for this PF
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00008016 * @pf: board private structure
8017 **/
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00008018u32 i40e_get_current_fd_count(struct i40e_pf *pf)
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00008019{
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00008020 u32 val, fcnt_prog;
8021
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00008022 val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
8023 fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK) +
8024 ((val & I40E_PFQF_FDSTAT_BEST_CNT_MASK) >>
8025 I40E_PFQF_FDSTAT_BEST_CNT_SHIFT);
8026 return fcnt_prog;
8027}
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00008028
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00008029/**
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00008030 * i40e_get_global_fd_count - Get total FD filters programmed on device
8031 * @pf: board private structure
8032 **/
8033u32 i40e_get_global_fd_count(struct i40e_pf *pf)
8034{
8035 u32 val, fcnt_prog;
8036
8037 val = rd32(&pf->hw, I40E_GLQF_FDCNT_0);
8038 fcnt_prog = (val & I40E_GLQF_FDCNT_0_GUARANT_CNT_MASK) +
8039 ((val & I40E_GLQF_FDCNT_0_BESTCNT_MASK) >>
8040 I40E_GLQF_FDCNT_0_BESTCNT_SHIFT);
8041 return fcnt_prog;
8042}
8043
8044/**
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00008045 * i40e_fdir_check_and_reenable - Function to reenabe FD ATR or SB if disabled
8046 * @pf: board private structure
8047 **/
8048void i40e_fdir_check_and_reenable(struct i40e_pf *pf)
8049{
Carolyn Wyborny3487b6c2015-08-27 11:42:38 -04008050 struct i40e_fdir_filter *filter;
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00008051 u32 fcnt_prog, fcnt_avail;
Carolyn Wyborny3487b6c2015-08-27 11:42:38 -04008052 struct hlist_node *node;
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00008053
Jacob Keller0da36b92017-04-19 09:25:55 -04008054 if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00008055 return;
8056
Jacob Keller47994c12017-04-19 09:25:57 -04008057 /* Check if we have enough room to re-enable FDir SB capability. */
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00008058 fcnt_prog = i40e_get_global_fd_count(pf);
Anjali Singhai Jain12957382014-06-04 04:22:47 +00008059 fcnt_avail = pf->fdir_pf_filter_count;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00008060 if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM)) ||
8061 (pf->fd_add_err == 0) ||
8062 (i40e_get_current_atr_cnt(pf) < pf->fd_atr_cnt)) {
Jacob Keller47994c12017-04-19 09:25:57 -04008063 if (pf->flags & I40E_FLAG_FD_SB_AUTO_DISABLED) {
8064 pf->flags &= ~I40E_FLAG_FD_SB_AUTO_DISABLED;
8065 if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
8066 (I40E_DEBUG_FD & pf->hw.debug_mask))
Anjali Singhai Jain2e4875e2015-04-16 20:06:06 -04008067 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 +00008068 }
8069 }
Jacob Kellera3417d22016-09-06 18:05:10 -07008070
Jacob Keller47994c12017-04-19 09:25:57 -04008071 /* We should wait for even more space before re-enabling ATR.
8072 * Additionally, we cannot enable ATR as long as we still have TCP SB
8073 * rules active.
Jacob Kellera3417d22016-09-06 18:05:10 -07008074 */
Jacob Keller47994c12017-04-19 09:25:57 -04008075 if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) &&
8076 (pf->fd_tcp4_filter_cnt == 0)) {
8077 if (pf->flags & I40E_FLAG_FD_ATR_AUTO_DISABLED) {
8078 pf->flags &= ~I40E_FLAG_FD_ATR_AUTO_DISABLED;
8079 if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
8080 (I40E_DEBUG_FD & pf->hw.debug_mask))
Jacob Kellera3417d22016-09-06 18:05:10 -07008081 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 +00008082 }
8083 }
Carolyn Wyborny3487b6c2015-08-27 11:42:38 -04008084
8085 /* if hw had a problem adding a filter, delete it */
8086 if (pf->fd_inv > 0) {
8087 hlist_for_each_entry_safe(filter, node,
8088 &pf->fdir_filter_list, fdir_node) {
8089 if (filter->fd_id == pf->fd_inv) {
8090 hlist_del(&filter->fdir_node);
8091 kfree(filter);
8092 pf->fdir_pf_active_filters--;
Filip Sadowski013df592017-08-29 05:32:38 -04008093 pf->fd_inv = 0;
Carolyn Wyborny3487b6c2015-08-27 11:42:38 -04008094 }
8095 }
8096 }
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00008097}
8098
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00008099#define I40E_MIN_FD_FLUSH_INTERVAL 10
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00008100#define I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE 30
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00008101/**
8102 * i40e_fdir_flush_and_replay - Function to flush all FD filters and replay SB
8103 * @pf: board private structure
8104 **/
8105static void i40e_fdir_flush_and_replay(struct i40e_pf *pf)
8106{
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00008107 unsigned long min_flush_time;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00008108 int flush_wait_retry = 50;
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00008109 bool disable_atr = false;
8110 int fd_room;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00008111 int reg;
8112
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04008113 if (!time_after(jiffies, pf->fd_flush_timestamp +
8114 (I40E_MIN_FD_FLUSH_INTERVAL * HZ)))
8115 return;
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00008116
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04008117 /* If the flush is happening too quick and we have mostly SB rules we
8118 * should not re-enable ATR for some time.
8119 */
8120 min_flush_time = pf->fd_flush_timestamp +
8121 (I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE * HZ);
8122 fd_room = pf->fdir_pf_filter_count - pf->fdir_pf_active_filters;
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00008123
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04008124 if (!(time_after(jiffies, min_flush_time)) &&
8125 (fd_room < I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) {
8126 if (I40E_DEBUG_FD & pf->hw.debug_mask)
8127 dev_info(&pf->pdev->dev, "ATR disabled, not enough FD filter space.\n");
8128 disable_atr = true;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00008129 }
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04008130
8131 pf->fd_flush_timestamp = jiffies;
Jacob Keller47994c12017-04-19 09:25:57 -04008132 pf->flags |= I40E_FLAG_FD_ATR_AUTO_DISABLED;
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04008133 /* flush all filters */
8134 wr32(&pf->hw, I40E_PFQF_CTL_1,
8135 I40E_PFQF_CTL_1_CLEARFDTABLE_MASK);
8136 i40e_flush(&pf->hw);
8137 pf->fd_flush_cnt++;
8138 pf->fd_add_err = 0;
8139 do {
8140 /* Check FD flush status every 5-6msec */
8141 usleep_range(5000, 6000);
8142 reg = rd32(&pf->hw, I40E_PFQF_CTL_1);
8143 if (!(reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK))
8144 break;
8145 } while (flush_wait_retry--);
8146 if (reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK) {
8147 dev_warn(&pf->pdev->dev, "FD table did not flush, needs more time\n");
8148 } else {
8149 /* replay sideband filters */
8150 i40e_fdir_filter_restore(pf->vsi[pf->lan_vsi]);
Jacob Keller097dbf52017-02-06 14:38:46 -08008151 if (!disable_atr && !pf->fd_tcp4_filter_cnt)
Jacob Keller47994c12017-04-19 09:25:57 -04008152 pf->flags &= ~I40E_FLAG_FD_ATR_AUTO_DISABLED;
Jacob Keller0da36b92017-04-19 09:25:55 -04008153 clear_bit(__I40E_FD_FLUSH_REQUESTED, pf->state);
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04008154 if (I40E_DEBUG_FD & pf->hw.debug_mask)
8155 dev_info(&pf->pdev->dev, "FD Filter table flushed and FD-SB replayed.\n");
8156 }
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00008157}
8158
8159/**
8160 * i40e_get_current_atr_count - Get the count of total FD ATR filters programmed
8161 * @pf: board private structure
8162 **/
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00008163u32 i40e_get_current_atr_cnt(struct i40e_pf *pf)
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00008164{
8165 return i40e_get_current_fd_count(pf) - pf->fdir_pf_active_filters;
8166}
8167
8168/* We can see up to 256 filter programming desc in transit if the filters are
8169 * being applied really fast; before we see the first
8170 * filter miss error on Rx queue 0. Accumulating enough error messages before
8171 * reacting will make sure we don't cause flush too often.
8172 */
8173#define I40E_MAX_FD_PROGRAM_ERROR 256
8174
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00008175/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008176 * i40e_fdir_reinit_subtask - Worker thread to reinit FDIR filter table
8177 * @pf: board private structure
8178 **/
8179static void i40e_fdir_reinit_subtask(struct i40e_pf *pf)
8180{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008181
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008182 /* if interface is down do nothing */
Mauro S. M. Rodrigues9e6c9c02017-05-12 23:26:56 -03008183 if (test_bit(__I40E_DOWN, pf->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008184 return;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00008185
Jacob Keller0da36b92017-04-19 09:25:55 -04008186 if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00008187 i40e_fdir_flush_and_replay(pf);
8188
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00008189 i40e_fdir_check_and_reenable(pf);
8190
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008191}
8192
8193/**
8194 * i40e_vsi_link_event - notify VSI of a link event
8195 * @vsi: vsi to be notified
8196 * @link_up: link up or down
8197 **/
8198static void i40e_vsi_link_event(struct i40e_vsi *vsi, bool link_up)
8199{
Jacob Keller0da36b92017-04-19 09:25:55 -04008200 if (!vsi || test_bit(__I40E_VSI_DOWN, vsi->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008201 return;
8202
8203 switch (vsi->type) {
8204 case I40E_VSI_MAIN:
8205 if (!vsi->netdev || !vsi->netdev_registered)
8206 break;
8207
8208 if (link_up) {
8209 netif_carrier_on(vsi->netdev);
8210 netif_tx_wake_all_queues(vsi->netdev);
8211 } else {
8212 netif_carrier_off(vsi->netdev);
8213 netif_tx_stop_all_queues(vsi->netdev);
8214 }
8215 break;
8216
8217 case I40E_VSI_SRIOV:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008218 case I40E_VSI_VMDQ2:
8219 case I40E_VSI_CTRL:
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06008220 case I40E_VSI_IWARP:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008221 case I40E_VSI_MIRROR:
8222 default:
8223 /* there is no notification for other VSIs */
8224 break;
8225 }
8226}
8227
8228/**
8229 * i40e_veb_link_event - notify elements on the veb of a link event
8230 * @veb: veb to be notified
8231 * @link_up: link up or down
8232 **/
8233static void i40e_veb_link_event(struct i40e_veb *veb, bool link_up)
8234{
8235 struct i40e_pf *pf;
8236 int i;
8237
8238 if (!veb || !veb->pf)
8239 return;
8240 pf = veb->pf;
8241
8242 /* depth first... */
8243 for (i = 0; i < I40E_MAX_VEB; i++)
8244 if (pf->veb[i] && (pf->veb[i]->uplink_seid == veb->seid))
8245 i40e_veb_link_event(pf->veb[i], link_up);
8246
8247 /* ... now the local VSIs */
Mitch Williams505682c2014-05-20 08:01:37 +00008248 for (i = 0; i < pf->num_alloc_vsi; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008249 if (pf->vsi[i] && (pf->vsi[i]->uplink_seid == veb->seid))
8250 i40e_vsi_link_event(pf->vsi[i], link_up);
8251}
8252
8253/**
8254 * i40e_link_event - Update netif_carrier status
8255 * @pf: board private structure
8256 **/
8257static void i40e_link_event(struct i40e_pf *pf)
8258{
Mitch Williams320684c2014-10-17 03:14:43 +00008259 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
Catherine Sullivanfef59dd2014-12-11 07:06:33 +00008260 u8 new_link_speed, old_link_speed;
Jesse Brandeburga72a5abc2015-08-26 15:14:19 -04008261 i40e_status status;
8262 bool new_link, old_link;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008263
Catherine Sullivan1f9610e2015-10-21 19:47:09 -04008264 /* save off old link status information */
8265 pf->hw.phy.link_info_old = pf->hw.phy.link_info;
8266
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00008267 /* set this to force the get_link_status call to refresh state */
8268 pf->hw.phy.get_link_info = true;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008269
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00008270 old_link = (pf->hw.phy.link_info_old.link_info & I40E_AQ_LINK_UP);
Jesse Brandeburga72a5abc2015-08-26 15:14:19 -04008271
8272 status = i40e_get_link_status(&pf->hw, &new_link);
Harshitha Ramamurthyae136702016-12-12 15:44:16 -08008273
8274 /* On success, disable temp link polling */
8275 if (status == I40E_SUCCESS) {
8276 if (pf->flags & I40E_FLAG_TEMP_LINK_POLLING)
8277 pf->flags &= ~I40E_FLAG_TEMP_LINK_POLLING;
8278 } else {
8279 /* Enable link polling temporarily until i40e_get_link_status
8280 * returns I40E_SUCCESS
8281 */
8282 pf->flags |= I40E_FLAG_TEMP_LINK_POLLING;
Jesse Brandeburga72a5abc2015-08-26 15:14:19 -04008283 dev_dbg(&pf->pdev->dev, "couldn't get link state, status: %d\n",
8284 status);
8285 return;
8286 }
8287
Catherine Sullivanfef59dd2014-12-11 07:06:33 +00008288 old_link_speed = pf->hw.phy.link_info_old.link_speed;
8289 new_link_speed = pf->hw.phy.link_info.link_speed;
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00008290
8291 if (new_link == old_link &&
Catherine Sullivanfef59dd2014-12-11 07:06:33 +00008292 new_link_speed == old_link_speed &&
Jacob Keller0da36b92017-04-19 09:25:55 -04008293 (test_bit(__I40E_VSI_DOWN, vsi->state) ||
Mitch Williams320684c2014-10-17 03:14:43 +00008294 new_link == netif_carrier_ok(vsi->netdev)))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008295 return;
Mitch Williams320684c2014-10-17 03:14:43 +00008296
Sudheer Mogilappagari9a034492017-07-14 09:10:16 -04008297 i40e_print_link_message(vsi, new_link);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008298
8299 /* Notify the base of the switch tree connected to
8300 * the link. Floating VEBs are not notified.
8301 */
8302 if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
8303 i40e_veb_link_event(pf->veb[pf->lan_veb], new_link);
8304 else
Mitch Williams320684c2014-10-17 03:14:43 +00008305 i40e_vsi_link_event(vsi, new_link);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008306
8307 if (pf->vf)
8308 i40e_vc_notify_link_state(pf);
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00008309
8310 if (pf->flags & I40E_FLAG_PTP)
8311 i40e_ptp_set_increment(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008312}
8313
8314/**
Shannon Nelson21536712014-10-25 10:35:25 +00008315 * i40e_watchdog_subtask - periodic checks not using event driven response
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008316 * @pf: board private structure
8317 **/
8318static void i40e_watchdog_subtask(struct i40e_pf *pf)
8319{
8320 int i;
8321
8322 /* if interface is down do nothing */
Mauro S. M. Rodrigues9e6c9c02017-05-12 23:26:56 -03008323 if (test_bit(__I40E_DOWN, pf->state) ||
Jacob Keller0da36b92017-04-19 09:25:55 -04008324 test_bit(__I40E_CONFIG_BUSY, pf->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008325 return;
8326
Shannon Nelson21536712014-10-25 10:35:25 +00008327 /* make sure we don't do these things too often */
8328 if (time_before(jiffies, (pf->service_timer_previous +
8329 pf->service_timer_period)))
8330 return;
8331 pf->service_timer_previous = jiffies;
8332
Harshitha Ramamurthyae136702016-12-12 15:44:16 -08008333 if ((pf->flags & I40E_FLAG_LINK_POLLING_ENABLED) ||
8334 (pf->flags & I40E_FLAG_TEMP_LINK_POLLING))
Shannon Nelson9ac77262015-08-27 11:42:40 -04008335 i40e_link_event(pf);
Shannon Nelson21536712014-10-25 10:35:25 +00008336
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008337 /* Update the stats for active netdevs so the network stack
8338 * can look at updated numbers whenever it cares to
8339 */
Mitch Williams505682c2014-05-20 08:01:37 +00008340 for (i = 0; i < pf->num_alloc_vsi; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008341 if (pf->vsi[i] && pf->vsi[i]->netdev)
8342 i40e_update_stats(pf->vsi[i]);
8343
Anjali Singhai Jaind1a8d272015-07-23 16:54:40 -04008344 if (pf->flags & I40E_FLAG_VEB_STATS_ENABLED) {
8345 /* Update the stats for the active switching components */
8346 for (i = 0; i < I40E_MAX_VEB; i++)
8347 if (pf->veb[i])
8348 i40e_update_veb_stats(pf->veb[i]);
8349 }
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00008350
Jacob Keller61189552017-05-03 10:29:01 -07008351 i40e_ptp_rx_hang(pf);
Jacob Keller0bc07062017-05-03 10:29:02 -07008352 i40e_ptp_tx_hang(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008353}
8354
8355/**
8356 * i40e_reset_subtask - Set up for resetting the device and driver
8357 * @pf: board private structure
8358 **/
8359static void i40e_reset_subtask(struct i40e_pf *pf)
8360{
8361 u32 reset_flags = 0;
8362
Jacob Keller0da36b92017-04-19 09:25:55 -04008363 if (test_bit(__I40E_REINIT_REQUESTED, pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08008364 reset_flags |= BIT(__I40E_REINIT_REQUESTED);
Jacob Keller0da36b92017-04-19 09:25:55 -04008365 clear_bit(__I40E_REINIT_REQUESTED, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008366 }
Jacob Keller0da36b92017-04-19 09:25:55 -04008367 if (test_bit(__I40E_PF_RESET_REQUESTED, pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08008368 reset_flags |= BIT(__I40E_PF_RESET_REQUESTED);
Jacob Keller0da36b92017-04-19 09:25:55 -04008369 clear_bit(__I40E_PF_RESET_REQUESTED, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008370 }
Jacob Keller0da36b92017-04-19 09:25:55 -04008371 if (test_bit(__I40E_CORE_RESET_REQUESTED, pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08008372 reset_flags |= BIT(__I40E_CORE_RESET_REQUESTED);
Jacob Keller0da36b92017-04-19 09:25:55 -04008373 clear_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008374 }
Jacob Keller0da36b92017-04-19 09:25:55 -04008375 if (test_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08008376 reset_flags |= BIT(__I40E_GLOBAL_RESET_REQUESTED);
Jacob Keller0da36b92017-04-19 09:25:55 -04008377 clear_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008378 }
Mauro S. M. Rodrigues9e6c9c02017-05-12 23:26:56 -03008379 if (test_bit(__I40E_DOWN_REQUESTED, pf->state)) {
8380 reset_flags |= BIT(__I40E_DOWN_REQUESTED);
8381 clear_bit(__I40E_DOWN_REQUESTED, pf->state);
Neerav Parikhb5d06f02014-06-03 23:50:17 +00008382 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008383
8384 /* If there's a recovery already waiting, it takes
8385 * precedence before starting a new reset sequence.
8386 */
Jacob Keller0da36b92017-04-19 09:25:55 -04008387 if (test_bit(__I40E_RESET_INTR_RECEIVED, pf->state)) {
Maciej Sosin373149f2017-04-05 07:50:55 -04008388 i40e_prep_for_reset(pf, false);
8389 i40e_reset(pf);
8390 i40e_rebuild(pf, false, false);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008391 }
8392
8393 /* If we're already down or resetting, just bail */
8394 if (reset_flags &&
Mauro S. M. Rodrigues9e6c9c02017-05-12 23:26:56 -03008395 !test_bit(__I40E_DOWN, pf->state) &&
Jacob Keller0da36b92017-04-19 09:25:55 -04008396 !test_bit(__I40E_CONFIG_BUSY, pf->state)) {
Jacob Kellerdfc4ff62017-06-07 05:43:13 -04008397 i40e_do_reset(pf, reset_flags, false);
Maciej Sosin373149f2017-04-05 07:50:55 -04008398 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008399}
8400
8401/**
8402 * i40e_handle_link_event - Handle link event
8403 * @pf: board private structure
8404 * @e: event info posted on ARQ
8405 **/
8406static void i40e_handle_link_event(struct i40e_pf *pf,
8407 struct i40e_arq_event_info *e)
8408{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008409 struct i40e_aqc_get_link_status *status =
8410 (struct i40e_aqc_get_link_status *)&e->desc.params.raw;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008411
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00008412 /* Do a new status request to re-enable LSE reporting
8413 * and load new status information into the hw struct
8414 * This completely ignores any state information
8415 * in the ARQ event info, instead choosing to always
8416 * issue the AQ update link status command.
8417 */
8418 i40e_link_event(pf);
8419
Filip Sadowski9a858172017-08-29 05:32:37 -04008420 /* Check if module meets thermal requirements */
8421 if (status->phy_type == I40E_PHY_TYPE_NOT_SUPPORTED_HIGH_TEMP) {
Carolyn Wyborny7b592f62014-07-10 07:58:19 +00008422 dev_err(&pf->pdev->dev,
Filip Sadowski9a858172017-08-29 05:32:37 -04008423 "Rx/Tx is disabled on this device because the module does not meet thermal requirements.\n");
8424 dev_err(&pf->pdev->dev,
8425 "Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules.\n");
8426 } else {
8427 /* check for unqualified module, if link is down, suppress
8428 * the message if link was forced to be down.
8429 */
8430 if ((status->link_info & I40E_AQ_MEDIA_AVAILABLE) &&
8431 (!(status->an_info & I40E_AQ_QUALIFIED_MODULE)) &&
8432 (!(status->link_info & I40E_AQ_LINK_UP)) &&
8433 (!(pf->flags & I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED))) {
8434 dev_err(&pf->pdev->dev,
8435 "Rx/Tx is disabled on this device because an unsupported SFP module type was detected.\n");
8436 dev_err(&pf->pdev->dev,
8437 "Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules.\n");
8438 }
8439 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008440}
8441
8442/**
8443 * i40e_clean_adminq_subtask - Clean the AdminQ rings
8444 * @pf: board private structure
8445 **/
8446static void i40e_clean_adminq_subtask(struct i40e_pf *pf)
8447{
8448 struct i40e_arq_event_info event;
8449 struct i40e_hw *hw = &pf->hw;
8450 u16 pending, i = 0;
8451 i40e_status ret;
8452 u16 opcode;
Shannon Nelson86df2422014-05-20 08:01:35 +00008453 u32 oldval;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008454 u32 val;
8455
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00008456 /* Do not run clean AQ when PF reset fails */
Jacob Keller0da36b92017-04-19 09:25:55 -04008457 if (test_bit(__I40E_RESET_FAILED, pf->state))
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00008458 return;
8459
Shannon Nelson86df2422014-05-20 08:01:35 +00008460 /* check for error indications */
8461 val = rd32(&pf->hw, pf->hw.aq.arq.len);
8462 oldval = val;
8463 if (val & I40E_PF_ARQLEN_ARQVFE_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08008464 if (hw->debug_mask & I40E_DEBUG_AQ)
8465 dev_info(&pf->pdev->dev, "ARQ VF Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00008466 val &= ~I40E_PF_ARQLEN_ARQVFE_MASK;
8467 }
8468 if (val & I40E_PF_ARQLEN_ARQOVFL_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08008469 if (hw->debug_mask & I40E_DEBUG_AQ)
8470 dev_info(&pf->pdev->dev, "ARQ Overflow Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00008471 val &= ~I40E_PF_ARQLEN_ARQOVFL_MASK;
Mitch Williams1d0a4ad2015-12-23 12:05:48 -08008472 pf->arq_overflows++;
Shannon Nelson86df2422014-05-20 08:01:35 +00008473 }
8474 if (val & I40E_PF_ARQLEN_ARQCRIT_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08008475 if (hw->debug_mask & I40E_DEBUG_AQ)
8476 dev_info(&pf->pdev->dev, "ARQ Critical Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00008477 val &= ~I40E_PF_ARQLEN_ARQCRIT_MASK;
8478 }
8479 if (oldval != val)
8480 wr32(&pf->hw, pf->hw.aq.arq.len, val);
8481
8482 val = rd32(&pf->hw, pf->hw.aq.asq.len);
8483 oldval = val;
8484 if (val & I40E_PF_ATQLEN_ATQVFE_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08008485 if (pf->hw.debug_mask & I40E_DEBUG_AQ)
8486 dev_info(&pf->pdev->dev, "ASQ VF Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00008487 val &= ~I40E_PF_ATQLEN_ATQVFE_MASK;
8488 }
8489 if (val & I40E_PF_ATQLEN_ATQOVFL_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08008490 if (pf->hw.debug_mask & I40E_DEBUG_AQ)
8491 dev_info(&pf->pdev->dev, "ASQ Overflow Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00008492 val &= ~I40E_PF_ATQLEN_ATQOVFL_MASK;
8493 }
8494 if (val & I40E_PF_ATQLEN_ATQCRIT_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08008495 if (pf->hw.debug_mask & I40E_DEBUG_AQ)
8496 dev_info(&pf->pdev->dev, "ASQ Critical Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00008497 val &= ~I40E_PF_ATQLEN_ATQCRIT_MASK;
8498 }
8499 if (oldval != val)
8500 wr32(&pf->hw, pf->hw.aq.asq.len, val);
8501
Mitch Williams1001dc32014-11-11 20:02:19 +00008502 event.buf_len = I40E_MAX_AQ_BUF_SIZE;
8503 event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008504 if (!event.msg_buf)
8505 return;
8506
8507 do {
8508 ret = i40e_clean_arq_element(hw, &event, &pending);
Mitch Williams56497972014-06-04 08:45:18 +00008509 if (ret == I40E_ERR_ADMIN_QUEUE_NO_WORK)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008510 break;
Mitch Williams56497972014-06-04 08:45:18 +00008511 else if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008512 dev_info(&pf->pdev->dev, "ARQ event error %d\n", ret);
8513 break;
8514 }
8515
8516 opcode = le16_to_cpu(event.desc.opcode);
8517 switch (opcode) {
8518
8519 case i40e_aqc_opc_get_link_status:
8520 i40e_handle_link_event(pf, &event);
8521 break;
8522 case i40e_aqc_opc_send_msg_to_pf:
8523 ret = i40e_vc_process_vf_msg(pf,
8524 le16_to_cpu(event.desc.retval),
8525 le32_to_cpu(event.desc.cookie_high),
8526 le32_to_cpu(event.desc.cookie_low),
8527 event.msg_buf,
Mitch Williams1001dc32014-11-11 20:02:19 +00008528 event.msg_len);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008529 break;
8530 case i40e_aqc_opc_lldp_update_mib:
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00008531 dev_dbg(&pf->pdev->dev, "ARQ: Update LLDP MIB event received\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08008532#ifdef CONFIG_I40E_DCB
8533 rtnl_lock();
8534 ret = i40e_handle_lldp_event(pf, &event);
8535 rtnl_unlock();
8536#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008537 break;
8538 case i40e_aqc_opc_event_lan_overflow:
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00008539 dev_dbg(&pf->pdev->dev, "ARQ LAN queue overflow event received\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008540 i40e_handle_lan_overflow_event(pf, &event);
8541 break;
Shannon Nelson0467bc92013-12-18 13:45:58 +00008542 case i40e_aqc_opc_send_msg_to_peer:
8543 dev_info(&pf->pdev->dev, "ARQ: Msg from other pf\n");
8544 break;
Shannon Nelson91a0f932015-03-19 14:32:01 -07008545 case i40e_aqc_opc_nvm_erase:
8546 case i40e_aqc_opc_nvm_update:
Michal Kosiarz00ada502015-11-19 11:34:20 -08008547 case i40e_aqc_opc_oem_post_update:
Shannon Nelson6e93d0c2016-01-15 14:33:18 -08008548 i40e_debug(&pf->hw, I40E_DEBUG_NVM,
8549 "ARQ NVM operation 0x%04x completed\n",
8550 opcode);
Shannon Nelson91a0f932015-03-19 14:32:01 -07008551 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008552 default:
8553 dev_info(&pf->pdev->dev,
Shannon Nelson56e5ca62016-03-10 14:59:48 -08008554 "ARQ: Unknown event 0x%04x ignored\n",
Shannon Nelson0467bc92013-12-18 13:45:58 +00008555 opcode);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008556 break;
8557 }
Christopher N Bednarz1fca3262017-03-10 12:22:03 -08008558 } while (i++ < pf->adminq_work_limit);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008559
Christopher N Bednarz1fca3262017-03-10 12:22:03 -08008560 if (i < pf->adminq_work_limit)
Jacob Keller0da36b92017-04-19 09:25:55 -04008561 clear_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state);
Christopher N Bednarz1fca3262017-03-10 12:22:03 -08008562
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008563 /* re-enable Admin queue interrupt cause */
8564 val = rd32(hw, I40E_PFINT_ICR0_ENA);
8565 val |= I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
8566 wr32(hw, I40E_PFINT_ICR0_ENA, val);
8567 i40e_flush(hw);
8568
8569 kfree(event.msg_buf);
8570}
8571
8572/**
Shannon Nelson4eb3f762014-03-06 08:59:58 +00008573 * i40e_verify_eeprom - make sure eeprom is good to use
8574 * @pf: board private structure
8575 **/
8576static void i40e_verify_eeprom(struct i40e_pf *pf)
8577{
8578 int err;
8579
8580 err = i40e_diag_eeprom_test(&pf->hw);
8581 if (err) {
8582 /* retry in case of garbage read */
8583 err = i40e_diag_eeprom_test(&pf->hw);
8584 if (err) {
8585 dev_info(&pf->pdev->dev, "eeprom check failed (%d), Tx/Rx traffic disabled\n",
8586 err);
Jacob Keller0da36b92017-04-19 09:25:55 -04008587 set_bit(__I40E_BAD_EEPROM, pf->state);
Shannon Nelson4eb3f762014-03-06 08:59:58 +00008588 }
8589 }
8590
Jacob Keller0da36b92017-04-19 09:25:55 -04008591 if (!err && test_bit(__I40E_BAD_EEPROM, pf->state)) {
Shannon Nelson4eb3f762014-03-06 08:59:58 +00008592 dev_info(&pf->pdev->dev, "eeprom check passed, Tx/Rx traffic enabled\n");
Jacob Keller0da36b92017-04-19 09:25:55 -04008593 clear_bit(__I40E_BAD_EEPROM, pf->state);
Shannon Nelson4eb3f762014-03-06 08:59:58 +00008594 }
8595}
8596
8597/**
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00008598 * i40e_enable_pf_switch_lb
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00008599 * @pf: pointer to the PF structure
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00008600 *
8601 * enable switch loop back or die - no point in a return value
8602 **/
8603static void i40e_enable_pf_switch_lb(struct i40e_pf *pf)
8604{
8605 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
8606 struct i40e_vsi_context ctxt;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008607 int ret;
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00008608
8609 ctxt.seid = pf->main_vsi_seid;
8610 ctxt.pf_num = pf->hw.pf_id;
8611 ctxt.vf_num = 0;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008612 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
8613 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00008614 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008615 "couldn't get PF vsi config, err %s aq_err %s\n",
8616 i40e_stat_str(&pf->hw, ret),
8617 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00008618 return;
8619 }
8620 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
8621 ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
8622 ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
8623
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008624 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
8625 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00008626 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008627 "update vsi switch failed, err %s aq_err %s\n",
8628 i40e_stat_str(&pf->hw, ret),
8629 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00008630 }
8631}
8632
8633/**
8634 * i40e_disable_pf_switch_lb
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00008635 * @pf: pointer to the PF structure
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00008636 *
8637 * disable switch loop back or die - no point in a return value
8638 **/
8639static void i40e_disable_pf_switch_lb(struct i40e_pf *pf)
8640{
8641 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
8642 struct i40e_vsi_context ctxt;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008643 int ret;
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00008644
8645 ctxt.seid = pf->main_vsi_seid;
8646 ctxt.pf_num = pf->hw.pf_id;
8647 ctxt.vf_num = 0;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008648 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
8649 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00008650 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008651 "couldn't get PF vsi config, err %s aq_err %s\n",
8652 i40e_stat_str(&pf->hw, ret),
8653 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00008654 return;
8655 }
8656 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
8657 ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
8658 ctxt.info.switch_id &= ~cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
8659
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008660 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
8661 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00008662 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008663 "update vsi switch failed, err %s aq_err %s\n",
8664 i40e_stat_str(&pf->hw, ret),
8665 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00008666 }
8667}
8668
8669/**
Neerav Parikh51616012015-02-06 08:52:14 +00008670 * i40e_config_bridge_mode - Configure the HW bridge mode
8671 * @veb: pointer to the bridge instance
8672 *
8673 * Configure the loop back mode for the LAN VSI that is downlink to the
8674 * specified HW bridge instance. It is expected this function is called
8675 * when a new HW bridge is instantiated.
8676 **/
8677static void i40e_config_bridge_mode(struct i40e_veb *veb)
8678{
8679 struct i40e_pf *pf = veb->pf;
8680
Shannon Nelson6dec1012015-09-28 14:12:30 -04008681 if (pf->hw.debug_mask & I40E_DEBUG_LAN)
8682 dev_info(&pf->pdev->dev, "enabling bridge mode: %s\n",
8683 veb->bridge_mode == BRIDGE_MODE_VEPA ? "VEPA" : "VEB");
Neerav Parikh51616012015-02-06 08:52:14 +00008684 if (veb->bridge_mode & BRIDGE_MODE_VEPA)
8685 i40e_disable_pf_switch_lb(pf);
8686 else
8687 i40e_enable_pf_switch_lb(pf);
8688}
8689
8690/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008691 * i40e_reconstitute_veb - rebuild the VEB and anything connected to it
8692 * @veb: pointer to the VEB instance
8693 *
8694 * This is a recursive function that first builds the attached VSIs then
8695 * recurses in to build the next layer of VEB. We track the connections
8696 * through our own index numbers because the seid's from the HW could
8697 * change across the reset.
8698 **/
8699static int i40e_reconstitute_veb(struct i40e_veb *veb)
8700{
8701 struct i40e_vsi *ctl_vsi = NULL;
8702 struct i40e_pf *pf = veb->pf;
8703 int v, veb_idx;
8704 int ret;
8705
8706 /* build VSI that owns this VEB, temporarily attached to base VEB */
Mitch Williams505682c2014-05-20 08:01:37 +00008707 for (v = 0; v < pf->num_alloc_vsi && !ctl_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008708 if (pf->vsi[v] &&
8709 pf->vsi[v]->veb_idx == veb->idx &&
8710 pf->vsi[v]->flags & I40E_VSI_FLAG_VEB_OWNER) {
8711 ctl_vsi = pf->vsi[v];
8712 break;
8713 }
8714 }
8715 if (!ctl_vsi) {
8716 dev_info(&pf->pdev->dev,
8717 "missing owner VSI for veb_idx %d\n", veb->idx);
8718 ret = -ENOENT;
8719 goto end_reconstitute;
8720 }
8721 if (ctl_vsi != pf->vsi[pf->lan_vsi])
8722 ctl_vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
8723 ret = i40e_add_vsi(ctl_vsi);
8724 if (ret) {
8725 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008726 "rebuild of veb_idx %d owner VSI failed: %d\n",
8727 veb->idx, ret);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008728 goto end_reconstitute;
8729 }
8730 i40e_vsi_reset_stats(ctl_vsi);
8731
8732 /* create the VEB in the switch and move the VSI onto the VEB */
8733 ret = i40e_add_veb(veb, ctl_vsi);
8734 if (ret)
8735 goto end_reconstitute;
8736
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07008737 if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
8738 veb->bridge_mode = BRIDGE_MODE_VEB;
8739 else
8740 veb->bridge_mode = BRIDGE_MODE_VEPA;
Neerav Parikh51616012015-02-06 08:52:14 +00008741 i40e_config_bridge_mode(veb);
Anjali Singhai Jainb64ba082014-11-13 03:06:15 +00008742
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008743 /* create the remaining VSIs attached to this VEB */
Mitch Williams505682c2014-05-20 08:01:37 +00008744 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008745 if (!pf->vsi[v] || pf->vsi[v] == ctl_vsi)
8746 continue;
8747
8748 if (pf->vsi[v]->veb_idx == veb->idx) {
8749 struct i40e_vsi *vsi = pf->vsi[v];
Jesse Brandeburg6995b362015-08-28 17:55:54 -04008750
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008751 vsi->uplink_seid = veb->seid;
8752 ret = i40e_add_vsi(vsi);
8753 if (ret) {
8754 dev_info(&pf->pdev->dev,
8755 "rebuild of vsi_idx %d failed: %d\n",
8756 v, ret);
8757 goto end_reconstitute;
8758 }
8759 i40e_vsi_reset_stats(vsi);
8760 }
8761 }
8762
8763 /* create any VEBs attached to this VEB - RECURSION */
8764 for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
8765 if (pf->veb[veb_idx] && pf->veb[veb_idx]->veb_idx == veb->idx) {
8766 pf->veb[veb_idx]->uplink_seid = veb->seid;
8767 ret = i40e_reconstitute_veb(pf->veb[veb_idx]);
8768 if (ret)
8769 break;
8770 }
8771 }
8772
8773end_reconstitute:
8774 return ret;
8775}
8776
8777/**
8778 * i40e_get_capabilities - get info about the HW
8779 * @pf: the PF struct
8780 **/
Amritha Nambiar2f4b4112017-10-27 02:36:01 -07008781static int i40e_get_capabilities(struct i40e_pf *pf,
8782 enum i40e_admin_queue_opc list_type)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008783{
8784 struct i40e_aqc_list_capabilities_element_resp *cap_buf;
8785 u16 data_size;
8786 int buf_len;
8787 int err;
8788
8789 buf_len = 40 * sizeof(struct i40e_aqc_list_capabilities_element_resp);
8790 do {
8791 cap_buf = kzalloc(buf_len, GFP_KERNEL);
8792 if (!cap_buf)
8793 return -ENOMEM;
8794
8795 /* this loads the data into the hw struct for us */
8796 err = i40e_aq_discover_capabilities(&pf->hw, cap_buf, buf_len,
Amritha Nambiar2f4b4112017-10-27 02:36:01 -07008797 &data_size, list_type,
8798 NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008799 /* data loaded, buffer no longer needed */
8800 kfree(cap_buf);
8801
8802 if (pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOMEM) {
8803 /* retry with a larger buffer */
8804 buf_len = data_size;
8805 } else if (pf->hw.aq.asq_last_status != I40E_AQ_RC_OK) {
8806 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008807 "capability discovery failed, err %s aq_err %s\n",
8808 i40e_stat_str(&pf->hw, err),
8809 i40e_aq_str(&pf->hw,
8810 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008811 return -ENODEV;
8812 }
8813 } while (err);
8814
Amritha Nambiar2f4b4112017-10-27 02:36:01 -07008815 if (pf->hw.debug_mask & I40E_DEBUG_USER) {
8816 if (list_type == i40e_aqc_opc_list_func_capabilities) {
8817 dev_info(&pf->pdev->dev,
8818 "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",
8819 pf->hw.pf_id, pf->hw.func_caps.num_vfs,
8820 pf->hw.func_caps.num_msix_vectors,
8821 pf->hw.func_caps.num_msix_vectors_vf,
8822 pf->hw.func_caps.fd_filters_guaranteed,
8823 pf->hw.func_caps.fd_filters_best_effort,
8824 pf->hw.func_caps.num_tx_qp,
8825 pf->hw.func_caps.num_vsis);
8826 } else if (list_type == i40e_aqc_opc_list_dev_capabilities) {
8827 dev_info(&pf->pdev->dev,
8828 "switch_mode=0x%04x, function_valid=0x%08x\n",
8829 pf->hw.dev_caps.switch_mode,
8830 pf->hw.dev_caps.valid_functions);
8831 dev_info(&pf->pdev->dev,
8832 "SR-IOV=%d, num_vfs for all function=%u\n",
8833 pf->hw.dev_caps.sr_iov_1_1,
8834 pf->hw.dev_caps.num_vfs);
8835 dev_info(&pf->pdev->dev,
8836 "num_vsis=%u, num_rx:%u, num_tx=%u\n",
8837 pf->hw.dev_caps.num_vsis,
8838 pf->hw.dev_caps.num_rx_qp,
8839 pf->hw.dev_caps.num_tx_qp);
8840 }
8841 }
8842 if (list_type == i40e_aqc_opc_list_func_capabilities) {
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00008843#define DEF_NUM_VSI (1 + (pf->hw.func_caps.fcoe ? 1 : 0) \
8844 + pf->hw.func_caps.num_vfs)
Amritha Nambiar2f4b4112017-10-27 02:36:01 -07008845 if (pf->hw.revision_id == 0 &&
8846 pf->hw.func_caps.num_vsis < DEF_NUM_VSI) {
8847 dev_info(&pf->pdev->dev,
8848 "got num_vsis %d, setting num_vsis to %d\n",
8849 pf->hw.func_caps.num_vsis, DEF_NUM_VSI);
8850 pf->hw.func_caps.num_vsis = DEF_NUM_VSI;
8851 }
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00008852 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008853 return 0;
8854}
8855
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008856static int i40e_vsi_clear(struct i40e_vsi *vsi);
8857
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008858/**
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008859 * i40e_fdir_sb_setup - initialize the Flow Director resources for Sideband
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008860 * @pf: board private structure
8861 **/
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008862static void i40e_fdir_sb_setup(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008863{
8864 struct i40e_vsi *vsi;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008865
Jesse Brandeburg407e0632014-06-03 23:50:12 +00008866 /* quick workaround for an NVM issue that leaves a critical register
8867 * uninitialized
8868 */
8869 if (!rd32(&pf->hw, I40E_GLQF_HKEY(0))) {
8870 static const u32 hkey[] = {
8871 0xe640d33f, 0xcdfe98ab, 0x73fa7161, 0x0d7a7d36,
8872 0xeacb7d61, 0xaa4f05b6, 0x9c5c89ed, 0xfc425ddb,
8873 0xa4654832, 0xfc7461d4, 0x8f827619, 0xf5c63c21,
8874 0x95b3a76d};
Alexander Duyck4b816442016-10-11 15:26:53 -07008875 int i;
Jesse Brandeburg407e0632014-06-03 23:50:12 +00008876
8877 for (i = 0; i <= I40E_GLQF_HKEY_MAX_INDEX; i++)
8878 wr32(&pf->hw, I40E_GLQF_HKEY(i), hkey[i]);
8879 }
8880
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008881 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008882 return;
8883
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008884 /* find existing VSI and see if it needs configuring */
Alexander Duyck4b816442016-10-11 15:26:53 -07008885 vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008886
8887 /* create a new VSI if none exists */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008888 if (!vsi) {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008889 vsi = i40e_vsi_setup(pf, I40E_VSI_FDIR,
8890 pf->vsi[pf->lan_vsi]->seid, 0);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008891 if (!vsi) {
8892 dev_info(&pf->pdev->dev, "Couldn't create FDir VSI\n");
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00008893 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
Amritha Nambiar2f4b4112017-10-27 02:36:01 -07008894 pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00008895 return;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008896 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008897 }
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00008898
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008899 i40e_vsi_setup_irqhandler(vsi, i40e_fdir_clean_ring);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008900}
8901
8902/**
8903 * i40e_fdir_teardown - release the Flow Director resources
8904 * @pf: board private structure
8905 **/
8906static void i40e_fdir_teardown(struct i40e_pf *pf)
8907{
Alexander Duyck4b816442016-10-11 15:26:53 -07008908 struct i40e_vsi *vsi;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008909
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00008910 i40e_fdir_filter_exit(pf);
Alexander Duyck4b816442016-10-11 15:26:53 -07008911 vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
8912 if (vsi)
8913 i40e_vsi_release(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008914}
8915
8916/**
Amritha Nambiar2f4b4112017-10-27 02:36:01 -07008917 * i40e_rebuild_cloud_filters - Rebuilds cloud filters for VSIs
8918 * @vsi: PF main vsi
8919 * @seid: seid of main or channel VSIs
8920 *
8921 * Rebuilds cloud filters associated with main VSI and channel VSIs if they
8922 * existed before reset
8923 **/
8924static int i40e_rebuild_cloud_filters(struct i40e_vsi *vsi, u16 seid)
8925{
8926 struct i40e_cloud_filter *cfilter;
8927 struct i40e_pf *pf = vsi->back;
8928 struct hlist_node *node;
8929 i40e_status ret;
8930
8931 /* Add cloud filters back if they exist */
8932 hlist_for_each_entry_safe(cfilter, node, &pf->cloud_filter_list,
8933 cloud_node) {
8934 if (cfilter->seid != seid)
8935 continue;
8936
8937 if (cfilter->dst_port)
8938 ret = i40e_add_del_cloud_filter_big_buf(vsi, cfilter,
8939 true);
8940 else
8941 ret = i40e_add_del_cloud_filter(vsi, cfilter, true);
8942
8943 if (ret) {
8944 dev_dbg(&pf->pdev->dev,
8945 "Failed to rebuild cloud filter, err %s aq_err %s\n",
8946 i40e_stat_str(&pf->hw, ret),
8947 i40e_aq_str(&pf->hw,
8948 pf->hw.aq.asq_last_status));
8949 return ret;
8950 }
8951 }
8952 return 0;
8953}
8954
8955/**
Amritha Nambiar8f88b302017-09-07 04:00:17 -07008956 * i40e_rebuild_channels - Rebuilds channel VSIs if they existed before reset
8957 * @vsi: PF main vsi
8958 *
8959 * Rebuilds channel VSIs if they existed before reset
8960 **/
8961static int i40e_rebuild_channels(struct i40e_vsi *vsi)
8962{
8963 struct i40e_channel *ch, *ch_tmp;
8964 i40e_status ret;
8965
8966 if (list_empty(&vsi->ch_list))
8967 return 0;
8968
8969 list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
8970 if (!ch->initialized)
8971 break;
8972 /* Proceed with creation of channel (VMDq2) VSI */
8973 ret = i40e_add_channel(vsi->back, vsi->uplink_seid, ch);
8974 if (ret) {
8975 dev_info(&vsi->back->pdev->dev,
8976 "failed to rebuild channels using uplink_seid %u\n",
8977 vsi->uplink_seid);
8978 return ret;
8979 }
Amritha Nambiar2027d4d2017-09-07 04:00:32 -07008980 if (ch->max_tx_rate) {
Alan Brady6c32e0d2017-10-09 15:48:45 -07008981 u64 credits = ch->max_tx_rate;
8982
Amritha Nambiar2027d4d2017-09-07 04:00:32 -07008983 if (i40e_set_bw_limit(vsi, ch->seid,
8984 ch->max_tx_rate))
8985 return -EINVAL;
8986
Alan Brady6c32e0d2017-10-09 15:48:45 -07008987 do_div(credits, I40E_BW_CREDIT_DIVISOR);
Amritha Nambiar2027d4d2017-09-07 04:00:32 -07008988 dev_dbg(&vsi->back->pdev->dev,
8989 "Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
8990 ch->max_tx_rate,
Alan Brady6c32e0d2017-10-09 15:48:45 -07008991 credits,
Amritha Nambiar2027d4d2017-09-07 04:00:32 -07008992 ch->seid);
8993 }
Amritha Nambiar2f4b4112017-10-27 02:36:01 -07008994 ret = i40e_rebuild_cloud_filters(vsi, ch->seid);
8995 if (ret) {
8996 dev_dbg(&vsi->back->pdev->dev,
8997 "Failed to rebuild cloud filters for channel VSI %u\n",
8998 ch->seid);
8999 return ret;
9000 }
Amritha Nambiar8f88b302017-09-07 04:00:17 -07009001 }
9002 return 0;
9003}
9004
9005/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00009006 * i40e_prep_for_reset - prep for the core to reset
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009007 * @pf: board private structure
Maciej Sosin373149f2017-04-05 07:50:55 -04009008 * @lock_acquired: indicates whether or not the lock has been acquired
9009 * before this function was called.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009010 *
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00009011 * Close up the VFs and other things in prep for PF Reset.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00009012 **/
Maciej Sosin373149f2017-04-05 07:50:55 -04009013static void i40e_prep_for_reset(struct i40e_pf *pf, bool lock_acquired)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009014{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009015 struct i40e_hw *hw = &pf->hw;
Shannon Nelson60442de2014-04-23 04:50:13 +00009016 i40e_status ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009017 u32 v;
9018
Jacob Keller0da36b92017-04-19 09:25:55 -04009019 clear_bit(__I40E_RESET_INTR_RECEIVED, pf->state);
9020 if (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
Shannon Nelson23cfbe02014-06-04 01:23:14 +00009021 return;
Mitch Williamsd3ce57342016-03-10 14:59:46 -08009022 if (i40e_check_asq_alive(&pf->hw))
9023 i40e_vc_notify_reset(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009024
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00009025 dev_dbg(&pf->pdev->dev, "Tearing down internal switch for reset\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009026
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009027 /* quiesce the VSIs and their queues that are not already DOWN */
Maciej Sosin373149f2017-04-05 07:50:55 -04009028 /* pf_quiesce_all_vsi modifies netdev structures -rtnl_lock needed */
9029 if (!lock_acquired)
9030 rtnl_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009031 i40e_pf_quiesce_all_vsi(pf);
Maciej Sosin373149f2017-04-05 07:50:55 -04009032 if (!lock_acquired)
9033 rtnl_unlock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009034
Mitch Williams505682c2014-05-20 08:01:37 +00009035 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009036 if (pf->vsi[v])
9037 pf->vsi[v]->seid = 0;
9038 }
9039
9040 i40e_shutdown_adminq(&pf->hw);
9041
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00009042 /* call shutdown HMC */
Shannon Nelson60442de2014-04-23 04:50:13 +00009043 if (hw->hmc.hmc_obj) {
9044 ret = i40e_shutdown_lan_hmc(hw);
Shannon Nelson23cfbe02014-06-04 01:23:14 +00009045 if (ret)
Shannon Nelson60442de2014-04-23 04:50:13 +00009046 dev_warn(&pf->pdev->dev,
9047 "shutdown_lan_hmc failed: %d\n", ret);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00009048 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00009049}
9050
9051/**
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00009052 * i40e_send_version - update firmware with driver version
9053 * @pf: PF struct
9054 */
9055static void i40e_send_version(struct i40e_pf *pf)
9056{
9057 struct i40e_driver_version dv;
9058
9059 dv.major_version = DRV_VERSION_MAJOR;
9060 dv.minor_version = DRV_VERSION_MINOR;
9061 dv.build_version = DRV_VERSION_BUILD;
9062 dv.subbuild_version = 0;
Rickard Strandqvist35a7d802014-07-29 09:26:25 +00009063 strlcpy(dv.driver_string, DRV_VERSION, sizeof(dv.driver_string));
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00009064 i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
9065}
9066
9067/**
Filip Sadowski5bbb2e22017-06-07 05:43:09 -04009068 * i40e_get_oem_version - get OEM specific version information
9069 * @hw: pointer to the hardware structure
9070 **/
9071static void i40e_get_oem_version(struct i40e_hw *hw)
9072{
9073 u16 block_offset = 0xffff;
9074 u16 block_length = 0;
9075 u16 capabilities = 0;
9076 u16 gen_snap = 0;
9077 u16 release = 0;
9078
9079#define I40E_SR_NVM_OEM_VERSION_PTR 0x1B
9080#define I40E_NVM_OEM_LENGTH_OFFSET 0x00
9081#define I40E_NVM_OEM_CAPABILITIES_OFFSET 0x01
9082#define I40E_NVM_OEM_GEN_OFFSET 0x02
9083#define I40E_NVM_OEM_RELEASE_OFFSET 0x03
9084#define I40E_NVM_OEM_CAPABILITIES_MASK 0x000F
9085#define I40E_NVM_OEM_LENGTH 3
9086
9087 /* Check if pointer to OEM version block is valid. */
9088 i40e_read_nvm_word(hw, I40E_SR_NVM_OEM_VERSION_PTR, &block_offset);
9089 if (block_offset == 0xffff)
9090 return;
9091
9092 /* Check if OEM version block has correct length. */
9093 i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_LENGTH_OFFSET,
9094 &block_length);
9095 if (block_length < I40E_NVM_OEM_LENGTH)
9096 return;
9097
9098 /* Check if OEM version format is as expected. */
9099 i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_CAPABILITIES_OFFSET,
9100 &capabilities);
9101 if ((capabilities & I40E_NVM_OEM_CAPABILITIES_MASK) != 0)
9102 return;
9103
9104 i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_GEN_OFFSET,
9105 &gen_snap);
9106 i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_RELEASE_OFFSET,
9107 &release);
9108 hw->nvm.oem_ver = (gen_snap << I40E_OEM_SNAP_SHIFT) | release;
9109 hw->nvm.eetrack = I40E_OEM_EETRACK_ID;
9110}
9111
9112/**
Maciej Sosin373149f2017-04-05 07:50:55 -04009113 * i40e_reset - wait for core reset to finish reset, reset pf if corer not seen
9114 * @pf: board private structure
9115 **/
9116static int i40e_reset(struct i40e_pf *pf)
9117{
9118 struct i40e_hw *hw = &pf->hw;
9119 i40e_status ret;
9120
9121 ret = i40e_pf_reset(hw);
9122 if (ret) {
9123 dev_info(&pf->pdev->dev, "PF reset failed, %d\n", ret);
Jacob Keller0da36b92017-04-19 09:25:55 -04009124 set_bit(__I40E_RESET_FAILED, pf->state);
9125 clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
Maciej Sosin373149f2017-04-05 07:50:55 -04009126 } else {
9127 pf->pfr_count++;
9128 }
9129 return ret;
9130}
9131
9132/**
9133 * i40e_rebuild - rebuild using a saved config
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00009134 * @pf: board private structure
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00009135 * @reinit: if the Main VSI needs to re-initialized.
Maciej Sosin373149f2017-04-05 07:50:55 -04009136 * @lock_acquired: indicates whether or not the lock has been acquired
9137 * before this function was called.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00009138 **/
Maciej Sosin373149f2017-04-05 07:50:55 -04009139static void i40e_rebuild(struct i40e_pf *pf, bool reinit, bool lock_acquired)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00009140{
Amritha Nambiar2027d4d2017-09-07 04:00:32 -07009141 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00009142 struct i40e_hw *hw = &pf->hw;
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00009143 u8 set_fc_aq_fail = 0;
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00009144 i40e_status ret;
Anjali Singhai Jain4f2f017c2015-10-21 19:47:07 -04009145 u32 val;
Maciej Sosin373149f2017-04-05 07:50:55 -04009146 int v;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009147
Mauro S. M. Rodrigues9e6c9c02017-05-12 23:26:56 -03009148 if (test_bit(__I40E_DOWN, pf->state))
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00009149 goto clear_recovery;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00009150 dev_dbg(&pf->pdev->dev, "Rebuilding internal switch\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009151
9152 /* rebuild the basics for the AdminQ, HMC, and initial HW switch */
9153 ret = i40e_init_adminq(&pf->hw);
9154 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009155 dev_info(&pf->pdev->dev, "Rebuild AdminQ failed, err %s aq_err %s\n",
9156 i40e_stat_str(&pf->hw, ret),
9157 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00009158 goto clear_recovery;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009159 }
Filip Sadowski5bbb2e22017-06-07 05:43:09 -04009160 i40e_get_oem_version(&pf->hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009161
Shannon Nelson4eb3f762014-03-06 08:59:58 +00009162 /* re-verify the eeprom if we just had an EMP reset */
Jacob Keller0da36b92017-04-19 09:25:55 -04009163 if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state))
Shannon Nelson4eb3f762014-03-06 08:59:58 +00009164 i40e_verify_eeprom(pf);
Shannon Nelson4eb3f762014-03-06 08:59:58 +00009165
Shannon Nelsone78ac4bf2014-05-10 04:49:09 +00009166 i40e_clear_pxe_mode(hw);
Amritha Nambiar2f4b4112017-10-27 02:36:01 -07009167 ret = i40e_get_capabilities(pf, i40e_aqc_opc_list_func_capabilities);
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009168 if (ret)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009169 goto end_core_reset;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009170
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009171 ret = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
Alexander Duyckc76cb6e2017-02-21 15:55:48 -08009172 hw->func_caps.num_rx_qp, 0, 0);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009173 if (ret) {
9174 dev_info(&pf->pdev->dev, "init_lan_hmc failed: %d\n", ret);
9175 goto end_core_reset;
9176 }
9177 ret = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
9178 if (ret) {
9179 dev_info(&pf->pdev->dev, "configure_lan_hmc failed: %d\n", ret);
9180 goto end_core_reset;
9181 }
9182
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08009183#ifdef CONFIG_I40E_DCB
9184 ret = i40e_init_pf_dcb(pf);
9185 if (ret) {
Shannon Nelsonaebfc812014-12-11 07:06:38 +00009186 dev_info(&pf->pdev->dev, "DCB init failed %d, disabled\n", ret);
9187 pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
9188 /* Continue without DCB enabled */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08009189 }
9190#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009191 /* do basic switch setup */
Maciej Sosin373149f2017-04-05 07:50:55 -04009192 if (!lock_acquired)
9193 rtnl_lock();
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00009194 ret = i40e_setup_pf_switch(pf, reinit);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009195 if (ret)
Maciej Sosin373149f2017-04-05 07:50:55 -04009196 goto end_unlock;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009197
Shannon Nelson2f0aff42016-01-04 10:33:08 -08009198 /* The driver only wants link up/down and module qualification
9199 * reports from firmware. Note the negative logic.
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +00009200 */
9201 ret = i40e_aq_set_phy_int_mask(&pf->hw,
Shannon Nelson2f0aff42016-01-04 10:33:08 -08009202 ~(I40E_AQ_EVENT_LINK_UPDOWN |
Shannon Nelson867a79e2016-03-18 12:18:15 -07009203 I40E_AQ_EVENT_MEDIA_NA |
Shannon Nelson2f0aff42016-01-04 10:33:08 -08009204 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +00009205 if (ret)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009206 dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
9207 i40e_stat_str(&pf->hw, ret),
9208 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +00009209
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00009210 /* make sure our flow control settings are restored */
9211 ret = i40e_set_fc(&pf->hw, &set_fc_aq_fail, true);
9212 if (ret)
Neerav Parikh8279e492015-09-03 17:18:50 -04009213 dev_dbg(&pf->pdev->dev, "setting flow control: ret = %s last_status = %s\n",
9214 i40e_stat_str(&pf->hw, ret),
9215 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00009216
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009217 /* Rebuild the VSIs and VEBs that existed before reset.
9218 * They are still in our local switch element arrays, so only
9219 * need to rebuild the switch model in the HW.
9220 *
9221 * If there were VEBs but the reconstitution failed, we'll try
9222 * try to recover minimal use by getting the basic PF VSI working.
9223 */
Amritha Nambiar2027d4d2017-09-07 04:00:32 -07009224 if (vsi->uplink_seid != pf->mac_seid) {
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00009225 dev_dbg(&pf->pdev->dev, "attempting to rebuild switch\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009226 /* find the one VEB connected to the MAC, and find orphans */
9227 for (v = 0; v < I40E_MAX_VEB; v++) {
9228 if (!pf->veb[v])
9229 continue;
9230
9231 if (pf->veb[v]->uplink_seid == pf->mac_seid ||
9232 pf->veb[v]->uplink_seid == 0) {
9233 ret = i40e_reconstitute_veb(pf->veb[v]);
9234
9235 if (!ret)
9236 continue;
9237
9238 /* If Main VEB failed, we're in deep doodoo,
9239 * so give up rebuilding the switch and set up
9240 * for minimal rebuild of PF VSI.
9241 * If orphan failed, we'll report the error
9242 * but try to keep going.
9243 */
9244 if (pf->veb[v]->uplink_seid == pf->mac_seid) {
9245 dev_info(&pf->pdev->dev,
9246 "rebuild of switch failed: %d, will try to set up simple PF connection\n",
9247 ret);
Amritha Nambiar2027d4d2017-09-07 04:00:32 -07009248 vsi->uplink_seid = pf->mac_seid;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009249 break;
9250 } else if (pf->veb[v]->uplink_seid == 0) {
9251 dev_info(&pf->pdev->dev,
9252 "rebuild of orphan VEB failed: %d\n",
9253 ret);
9254 }
9255 }
9256 }
9257 }
9258
Amritha Nambiar2027d4d2017-09-07 04:00:32 -07009259 if (vsi->uplink_seid == pf->mac_seid) {
Shannon Nelsoncde4cbc2014-06-04 01:23:17 +00009260 dev_dbg(&pf->pdev->dev, "attempting to rebuild PF VSI\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009261 /* no VEB, so rebuild only the Main VSI */
Amritha Nambiar2027d4d2017-09-07 04:00:32 -07009262 ret = i40e_add_vsi(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009263 if (ret) {
9264 dev_info(&pf->pdev->dev,
9265 "rebuild of Main VSI failed: %d\n", ret);
Maciej Sosin373149f2017-04-05 07:50:55 -04009266 goto end_unlock;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009267 }
9268 }
9269
Amritha Nambiar2027d4d2017-09-07 04:00:32 -07009270 if (vsi->mqprio_qopt.max_rate[0]) {
Alan Brady6c32e0d2017-10-09 15:48:45 -07009271 u64 max_tx_rate = vsi->mqprio_qopt.max_rate[0];
9272 u64 credits = 0;
Amritha Nambiar2027d4d2017-09-07 04:00:32 -07009273
Alan Brady6c32e0d2017-10-09 15:48:45 -07009274 do_div(max_tx_rate, I40E_BW_MBPS_DIVISOR);
Amritha Nambiar2027d4d2017-09-07 04:00:32 -07009275 ret = i40e_set_bw_limit(vsi, vsi->seid, max_tx_rate);
Alan Brady6c32e0d2017-10-09 15:48:45 -07009276 if (ret)
Amritha Nambiar2027d4d2017-09-07 04:00:32 -07009277 goto end_unlock;
Alan Brady6c32e0d2017-10-09 15:48:45 -07009278
9279 credits = max_tx_rate;
9280 do_div(credits, I40E_BW_CREDIT_DIVISOR);
9281 dev_dbg(&vsi->back->pdev->dev,
9282 "Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
9283 max_tx_rate,
9284 credits,
9285 vsi->seid);
Amritha Nambiar2027d4d2017-09-07 04:00:32 -07009286 }
9287
Amritha Nambiar2f4b4112017-10-27 02:36:01 -07009288 ret = i40e_rebuild_cloud_filters(vsi, vsi->seid);
9289 if (ret)
9290 goto end_unlock;
9291
Amritha Nambiar8f88b302017-09-07 04:00:17 -07009292 /* PF Main VSI is rebuild by now, go ahead and rebuild channel VSIs
9293 * for this main VSI if they exist
9294 */
Amritha Nambiar2027d4d2017-09-07 04:00:32 -07009295 ret = i40e_rebuild_channels(vsi);
Amritha Nambiar8f88b302017-09-07 04:00:17 -07009296 if (ret)
9297 goto end_unlock;
9298
Anjali Singhai Jain4f2f017c2015-10-21 19:47:07 -04009299 /* Reconfigure hardware for allowing smaller MSS in the case
9300 * of TSO, so that we avoid the MDD being fired and causing
9301 * a reset in the case of small MSS+TSO.
9302 */
9303#define I40E_REG_MSS 0x000E64DC
9304#define I40E_REG_MSS_MIN_MASK 0x3FF0000
9305#define I40E_64BYTE_MSS 0x400000
9306 val = rd32(hw, I40E_REG_MSS);
9307 if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
9308 val &= ~I40E_REG_MSS_MIN_MASK;
9309 val |= I40E_64BYTE_MSS;
9310 wr32(hw, I40E_REG_MSS, val);
9311 }
9312
Jacob Kellerd36e41d2017-06-23 04:24:46 -04009313 if (pf->hw_features & I40E_HW_RESTART_AUTONEG) {
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +00009314 msleep(75);
9315 ret = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
9316 if (ret)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009317 dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
9318 i40e_stat_str(&pf->hw, ret),
9319 i40e_aq_str(&pf->hw,
9320 pf->hw.aq.asq_last_status));
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00009321 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009322 /* reinit the misc interrupt */
9323 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
9324 ret = i40e_setup_misc_vector(pf);
9325
Anjali Singhai Jaine7358f52015-10-01 14:37:34 -04009326 /* Add a filter to drop all Flow control frames from any VSI from being
9327 * transmitted. By doing so we stop a malicious VF from sending out
9328 * PAUSE or PFC frames and potentially controlling traffic for other
9329 * PF/VF VSIs.
9330 * The FW can still send Flow control frames if enabled.
9331 */
9332 i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
9333 pf->main_vsi_seid);
9334
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009335 /* restart the VSIs that were rebuilt and running before the reset */
9336 i40e_pf_unquiesce_all_vsi(pf);
9337
Jacob Keller024b05f2017-04-13 04:45:46 -04009338 /* Release the RTNL lock before we start resetting VFs */
9339 if (!lock_acquired)
9340 rtnl_unlock();
9341
Alan Bradybd5608b2017-11-14 07:00:52 -05009342 /* Restore promiscuous settings */
9343 ret = i40e_set_promiscuous(pf, pf->cur_promisc);
9344 if (ret)
9345 dev_warn(&pf->pdev->dev,
9346 "Failed to restore promiscuous setting: %s, err %s aq_err %s\n",
9347 pf->cur_promisc ? "on" : "off",
9348 i40e_stat_str(&pf->hw, ret),
9349 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
9350
Jacob Kellere4b433f2017-04-13 04:45:52 -04009351 i40e_reset_all_vfs(pf, true);
Mitch Williams69f64b22014-02-13 03:48:46 -08009352
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009353 /* tell the firmware that we're starting */
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00009354 i40e_send_version(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009355
Jacob Keller024b05f2017-04-13 04:45:46 -04009356 /* We've already released the lock, so don't do it again */
9357 goto end_core_reset;
9358
Maciej Sosin373149f2017-04-05 07:50:55 -04009359end_unlock:
Jacob Keller024b05f2017-04-13 04:45:46 -04009360 if (!lock_acquired)
9361 rtnl_unlock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009362end_core_reset:
Jacob Keller0da36b92017-04-19 09:25:55 -04009363 clear_bit(__I40E_RESET_FAILED, pf->state);
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00009364clear_recovery:
Jacob Keller0da36b92017-04-19 09:25:55 -04009365 clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009366}
9367
9368/**
Maciej Sosin373149f2017-04-05 07:50:55 -04009369 * i40e_reset_and_rebuild - reset and rebuild using a saved config
9370 * @pf: board private structure
9371 * @reinit: if the Main VSI needs to re-initialized.
9372 * @lock_acquired: indicates whether or not the lock has been acquired
9373 * before this function was called.
9374 **/
9375static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit,
9376 bool lock_acquired)
9377{
9378 int ret;
9379 /* Now we wait for GRST to settle out.
9380 * We don't have to delete the VEBs or VSIs from the hw switch
9381 * because the reset will make them disappear.
9382 */
9383 ret = i40e_reset(pf);
9384 if (!ret)
9385 i40e_rebuild(pf, reinit, lock_acquired);
9386}
9387
9388/**
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00009389 * i40e_handle_reset_warning - prep for the PF to reset, reset and rebuild
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00009390 * @pf: board private structure
9391 *
9392 * Close up the VFs and other things in prep for a Core Reset,
9393 * then get ready to rebuild the world.
Maciej Sosin373149f2017-04-05 07:50:55 -04009394 * @lock_acquired: indicates whether or not the lock has been acquired
9395 * before this function was called.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00009396 **/
Maciej Sosin373149f2017-04-05 07:50:55 -04009397static void i40e_handle_reset_warning(struct i40e_pf *pf, bool lock_acquired)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00009398{
Maciej Sosin373149f2017-04-05 07:50:55 -04009399 i40e_prep_for_reset(pf, lock_acquired);
9400 i40e_reset_and_rebuild(pf, false, lock_acquired);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00009401}
9402
9403/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009404 * i40e_handle_mdd_event
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00009405 * @pf: pointer to the PF structure
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009406 *
9407 * Called from the MDD irq handler to identify possibly malicious vfs
9408 **/
9409static void i40e_handle_mdd_event(struct i40e_pf *pf)
9410{
9411 struct i40e_hw *hw = &pf->hw;
9412 bool mdd_detected = false;
Neerav Parikhdf430b12014-06-04 01:23:15 +00009413 bool pf_mdd_detected = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009414 struct i40e_vf *vf;
9415 u32 reg;
9416 int i;
9417
Jacob Keller0da36b92017-04-19 09:25:55 -04009418 if (!test_bit(__I40E_MDD_EVENT_PENDING, pf->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009419 return;
9420
9421 /* find what triggered the MDD event */
9422 reg = rd32(hw, I40E_GL_MDET_TX);
9423 if (reg & I40E_GL_MDET_TX_VALID_MASK) {
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00009424 u8 pf_num = (reg & I40E_GL_MDET_TX_PF_NUM_MASK) >>
9425 I40E_GL_MDET_TX_PF_NUM_SHIFT;
Mitch Williams2089ad02014-10-17 03:14:53 +00009426 u16 vf_num = (reg & I40E_GL_MDET_TX_VF_NUM_MASK) >>
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00009427 I40E_GL_MDET_TX_VF_NUM_SHIFT;
Dan Carpenter013f6572014-10-22 20:06:29 -07009428 u8 event = (reg & I40E_GL_MDET_TX_EVENT_MASK) >>
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00009429 I40E_GL_MDET_TX_EVENT_SHIFT;
Mitch Williams2089ad02014-10-17 03:14:53 +00009430 u16 queue = ((reg & I40E_GL_MDET_TX_QUEUE_MASK) >>
9431 I40E_GL_MDET_TX_QUEUE_SHIFT) -
9432 pf->hw.func_caps.base_queue;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00009433 if (netif_msg_tx_err(pf))
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00009434 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 +00009435 event, queue, pf_num, vf_num);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009436 wr32(hw, I40E_GL_MDET_TX, 0xffffffff);
9437 mdd_detected = true;
9438 }
9439 reg = rd32(hw, I40E_GL_MDET_RX);
9440 if (reg & I40E_GL_MDET_RX_VALID_MASK) {
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00009441 u8 func = (reg & I40E_GL_MDET_RX_FUNCTION_MASK) >>
9442 I40E_GL_MDET_RX_FUNCTION_SHIFT;
Dan Carpenter013f6572014-10-22 20:06:29 -07009443 u8 event = (reg & I40E_GL_MDET_RX_EVENT_MASK) >>
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00009444 I40E_GL_MDET_RX_EVENT_SHIFT;
Mitch Williams2089ad02014-10-17 03:14:53 +00009445 u16 queue = ((reg & I40E_GL_MDET_RX_QUEUE_MASK) >>
9446 I40E_GL_MDET_RX_QUEUE_SHIFT) -
9447 pf->hw.func_caps.base_queue;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00009448 if (netif_msg_rx_err(pf))
9449 dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on RX queue %d of function 0x%02x\n",
9450 event, queue, func);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009451 wr32(hw, I40E_GL_MDET_RX, 0xffffffff);
9452 mdd_detected = true;
9453 }
9454
Neerav Parikhdf430b12014-06-04 01:23:15 +00009455 if (mdd_detected) {
9456 reg = rd32(hw, I40E_PF_MDET_TX);
9457 if (reg & I40E_PF_MDET_TX_VALID_MASK) {
9458 wr32(hw, I40E_PF_MDET_TX, 0xFFFF);
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00009459 dev_info(&pf->pdev->dev, "TX driver issue detected, PF reset issued\n");
Neerav Parikhdf430b12014-06-04 01:23:15 +00009460 pf_mdd_detected = true;
9461 }
9462 reg = rd32(hw, I40E_PF_MDET_RX);
9463 if (reg & I40E_PF_MDET_RX_VALID_MASK) {
9464 wr32(hw, I40E_PF_MDET_RX, 0xFFFF);
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00009465 dev_info(&pf->pdev->dev, "RX driver issue detected, PF reset issued\n");
Neerav Parikhdf430b12014-06-04 01:23:15 +00009466 pf_mdd_detected = true;
9467 }
9468 /* Queue belongs to the PF, initiate a reset */
9469 if (pf_mdd_detected) {
Jacob Keller0da36b92017-04-19 09:25:55 -04009470 set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
Neerav Parikhdf430b12014-06-04 01:23:15 +00009471 i40e_service_event_schedule(pf);
9472 }
9473 }
9474
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009475 /* see if one of the VFs needs its hand slapped */
9476 for (i = 0; i < pf->num_alloc_vfs && mdd_detected; i++) {
9477 vf = &(pf->vf[i]);
9478 reg = rd32(hw, I40E_VP_MDET_TX(i));
9479 if (reg & I40E_VP_MDET_TX_VALID_MASK) {
9480 wr32(hw, I40E_VP_MDET_TX(i), 0xFFFF);
9481 vf->num_mdd_events++;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00009482 dev_info(&pf->pdev->dev, "TX driver issue detected on VF %d\n",
9483 i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009484 }
9485
9486 reg = rd32(hw, I40E_VP_MDET_RX(i));
9487 if (reg & I40E_VP_MDET_RX_VALID_MASK) {
9488 wr32(hw, I40E_VP_MDET_RX(i), 0xFFFF);
9489 vf->num_mdd_events++;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00009490 dev_info(&pf->pdev->dev, "RX driver issue detected on VF %d\n",
9491 i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009492 }
9493
9494 if (vf->num_mdd_events > I40E_DEFAULT_NUM_MDD_EVENTS_ALLOWED) {
9495 dev_info(&pf->pdev->dev,
9496 "Too many MDD events on VF %d, disabled\n", i);
9497 dev_info(&pf->pdev->dev,
9498 "Use PF Control I/F to re-enable the VF\n");
Jacob Keller6322e632017-04-13 04:45:54 -04009499 set_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009500 }
9501 }
9502
9503 /* re-enable mdd interrupt cause */
Jacob Keller0da36b92017-04-19 09:25:55 -04009504 clear_bit(__I40E_MDD_EVENT_PENDING, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009505 reg = rd32(hw, I40E_PFINT_ICR0_ENA);
9506 reg |= I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
9507 wr32(hw, I40E_PFINT_ICR0_ENA, reg);
9508 i40e_flush(hw);
9509}
9510
Jacob Kellerd8b2c702017-06-20 15:17:00 -07009511static const char *i40e_tunnel_name(struct i40e_udp_port_config *port)
9512{
9513 switch (port->type) {
9514 case UDP_TUNNEL_TYPE_VXLAN:
9515 return "vxlan";
9516 case UDP_TUNNEL_TYPE_GENEVE:
9517 return "geneve";
9518 default:
9519 return "unknown";
9520 }
9521}
9522
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009523/**
Alexander Duyck1f190d92017-04-19 09:25:51 -04009524 * i40e_sync_udp_filters - Trigger a sync event for existing UDP filters
9525 * @pf: board private structure
9526 **/
9527static void i40e_sync_udp_filters(struct i40e_pf *pf)
9528{
9529 int i;
9530
9531 /* loop through and set pending bit for all active UDP filters */
9532 for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
9533 if (pf->udp_ports[i].port)
9534 pf->pending_udp_bitmap |= BIT_ULL(i);
9535 }
9536
9537 pf->flags |= I40E_FLAG_UDP_FILTER_SYNC;
9538}
9539
9540/**
Singhai, Anjali6a899022015-12-14 12:21:18 -08009541 * i40e_sync_udp_filters_subtask - Sync the VSI filter list with HW
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009542 * @pf: board private structure
9543 **/
Singhai, Anjali6a899022015-12-14 12:21:18 -08009544static void i40e_sync_udp_filters_subtask(struct i40e_pf *pf)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009545{
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009546 struct i40e_hw *hw = &pf->hw;
9547 i40e_status ret;
Jacob Kellerfe0b0cd2017-02-06 14:38:38 -08009548 u16 port;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009549 int i;
9550
Singhai, Anjali6a899022015-12-14 12:21:18 -08009551 if (!(pf->flags & I40E_FLAG_UDP_FILTER_SYNC))
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009552 return;
9553
Singhai, Anjali6a899022015-12-14 12:21:18 -08009554 pf->flags &= ~I40E_FLAG_UDP_FILTER_SYNC;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009555
9556 for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
Singhai, Anjali6a899022015-12-14 12:21:18 -08009557 if (pf->pending_udp_bitmap & BIT_ULL(i)) {
9558 pf->pending_udp_bitmap &= ~BIT_ULL(i);
Jacob Keller27826fd2017-04-19 09:25:50 -04009559 port = pf->udp_ports[i].port;
Shannon Nelsonc22c06c2015-03-31 00:45:04 -07009560 if (port)
Carolyn Wybornyb3f5c7b2016-08-24 11:33:51 -07009561 ret = i40e_aq_add_udp_tunnel(hw, port,
9562 pf->udp_ports[i].type,
9563 NULL, NULL);
Shannon Nelsonc22c06c2015-03-31 00:45:04 -07009564 else
9565 ret = i40e_aq_del_udp_tunnel(hw, i, NULL);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009566
9567 if (ret) {
Jacob Kellerd8b2c702017-06-20 15:17:00 -07009568 dev_info(&pf->pdev->dev,
9569 "%s %s port %d, index %d failed, err %s aq_err %s\n",
9570 i40e_tunnel_name(&pf->udp_ports[i]),
9571 port ? "add" : "delete",
9572 port, i,
9573 i40e_stat_str(&pf->hw, ret),
9574 i40e_aq_str(&pf->hw,
9575 pf->hw.aq.asq_last_status));
Jacob Keller27826fd2017-04-19 09:25:50 -04009576 pf->udp_ports[i].port = 0;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009577 }
9578 }
9579 }
9580}
9581
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009582/**
9583 * i40e_service_task - Run the driver's async subtasks
9584 * @work: pointer to work_struct containing our data
9585 **/
9586static void i40e_service_task(struct work_struct *work)
9587{
9588 struct i40e_pf *pf = container_of(work,
9589 struct i40e_pf,
9590 service_task);
9591 unsigned long start_time = jiffies;
9592
Shannon Nelsone57a2fe2014-06-03 23:50:19 +00009593 /* don't bother with service tasks if a reset is in progress */
Jacob Keller0da36b92017-04-19 09:25:55 -04009594 if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
Shannon Nelsone57a2fe2014-06-03 23:50:19 +00009595 return;
Shannon Nelsone57a2fe2014-06-03 23:50:19 +00009596
Jacob Keller0da36b92017-04-19 09:25:55 -04009597 if (test_and_set_bit(__I40E_SERVICE_SCHED, pf->state))
Mitch Williams91089032016-11-21 13:03:51 -08009598 return;
9599
Sudheer Mogilappagari07d44192017-12-18 05:17:25 -05009600 i40e_detect_recover_hung(pf->vsi[pf->lan_vsi]);
Jesse Brandeburg2818ccd2016-01-13 16:51:38 -08009601 i40e_sync_filters_subtask(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009602 i40e_reset_subtask(pf);
9603 i40e_handle_mdd_event(pf);
9604 i40e_vc_process_vflr_event(pf);
9605 i40e_watchdog_subtask(pf);
9606 i40e_fdir_reinit_subtask(pf);
Mitch Williams0ef2d5a2017-01-24 10:24:00 -08009607 if (pf->flags & I40E_FLAG_CLIENT_RESET) {
9608 /* Client subtask will reopen next time through. */
9609 i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], true);
9610 pf->flags &= ~I40E_FLAG_CLIENT_RESET;
9611 } else {
9612 i40e_client_subtask(pf);
9613 if (pf->flags & I40E_FLAG_CLIENT_L2_CHANGE) {
9614 i40e_notify_client_of_l2_param_changes(
9615 pf->vsi[pf->lan_vsi]);
9616 pf->flags &= ~I40E_FLAG_CLIENT_L2_CHANGE;
9617 }
9618 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009619 i40e_sync_filters_subtask(pf);
Singhai, Anjali6a899022015-12-14 12:21:18 -08009620 i40e_sync_udp_filters_subtask(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009621 i40e_clean_adminq_subtask(pf);
9622
Mitch Williams91089032016-11-21 13:03:51 -08009623 /* flush memory to make sure state is correct before next watchdog */
9624 smp_mb__before_atomic();
Jacob Keller0da36b92017-04-19 09:25:55 -04009625 clear_bit(__I40E_SERVICE_SCHED, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009626
9627 /* If the tasks have taken longer than one timer cycle or there
9628 * is more work to be done, reschedule the service task now
9629 * rather than wait for the timer to tick again.
9630 */
9631 if (time_after(jiffies, (start_time + pf->service_timer_period)) ||
Jacob Keller0da36b92017-04-19 09:25:55 -04009632 test_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state) ||
9633 test_bit(__I40E_MDD_EVENT_PENDING, pf->state) ||
9634 test_bit(__I40E_VFLR_EVENT_PENDING, pf->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009635 i40e_service_event_schedule(pf);
9636}
9637
9638/**
9639 * i40e_service_timer - timer callback
9640 * @data: pointer to PF struct
9641 **/
Kees Cook26566ea2017-10-16 17:29:35 -07009642static void i40e_service_timer(struct timer_list *t)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009643{
Kees Cook26566ea2017-10-16 17:29:35 -07009644 struct i40e_pf *pf = from_timer(pf, t, service_timer);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009645
9646 mod_timer(&pf->service_timer,
9647 round_jiffies(jiffies + pf->service_timer_period));
9648 i40e_service_event_schedule(pf);
9649}
9650
9651/**
9652 * i40e_set_num_rings_in_vsi - Determine number of rings in the VSI
9653 * @vsi: the VSI being configured
9654 **/
9655static int i40e_set_num_rings_in_vsi(struct i40e_vsi *vsi)
9656{
9657 struct i40e_pf *pf = vsi->back;
9658
9659 switch (vsi->type) {
9660 case I40E_VSI_MAIN:
9661 vsi->alloc_queue_pairs = pf->num_lan_qps;
9662 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
9663 I40E_REQ_DESCRIPTOR_MULTIPLE);
9664 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
9665 vsi->num_q_vectors = pf->num_lan_msix;
9666 else
9667 vsi->num_q_vectors = 1;
9668
9669 break;
9670
9671 case I40E_VSI_FDIR:
9672 vsi->alloc_queue_pairs = 1;
9673 vsi->num_desc = ALIGN(I40E_FDIR_RING_COUNT,
9674 I40E_REQ_DESCRIPTOR_MULTIPLE);
Tushar Davea70e4072016-05-16 12:40:53 -07009675 vsi->num_q_vectors = pf->num_fdsb_msix;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009676 break;
9677
9678 case I40E_VSI_VMDQ2:
9679 vsi->alloc_queue_pairs = pf->num_vmdq_qps;
9680 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
9681 I40E_REQ_DESCRIPTOR_MULTIPLE);
9682 vsi->num_q_vectors = pf->num_vmdq_msix;
9683 break;
9684
9685 case I40E_VSI_SRIOV:
9686 vsi->alloc_queue_pairs = pf->num_vf_qps;
9687 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
9688 I40E_REQ_DESCRIPTOR_MULTIPLE);
9689 break;
9690
9691 default:
9692 WARN_ON(1);
9693 return -ENODATA;
9694 }
9695
9696 return 0;
9697}
9698
9699/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00009700 * i40e_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the vsi
Rami Rosen3d7d7a82017-09-16 05:49:48 +03009701 * @vsi: VSI pointer
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00009702 * @alloc_qvectors: a bool to specify if q_vectors need to be allocated.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00009703 *
9704 * On error: returns error code (negative)
9705 * On success: returns 0
9706 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00009707static int i40e_vsi_alloc_arrays(struct i40e_vsi *vsi, bool alloc_qvectors)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00009708{
Björn Töpel74608d12017-05-24 07:55:35 +02009709 struct i40e_ring **next_rings;
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00009710 int size;
9711 int ret = 0;
9712
Björn Töpel74608d12017-05-24 07:55:35 +02009713 /* allocate memory for both Tx, XDP Tx and Rx ring pointers */
9714 size = sizeof(struct i40e_ring *) * vsi->alloc_queue_pairs *
9715 (i40e_enabled_xdp_vsi(vsi) ? 3 : 2);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00009716 vsi->tx_rings = kzalloc(size, GFP_KERNEL);
9717 if (!vsi->tx_rings)
9718 return -ENOMEM;
Björn Töpel74608d12017-05-24 07:55:35 +02009719 next_rings = vsi->tx_rings + vsi->alloc_queue_pairs;
9720 if (i40e_enabled_xdp_vsi(vsi)) {
9721 vsi->xdp_rings = next_rings;
9722 next_rings += vsi->alloc_queue_pairs;
9723 }
9724 vsi->rx_rings = next_rings;
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00009725
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00009726 if (alloc_qvectors) {
9727 /* allocate memory for q_vector pointers */
Julia Lawallf57e4fb2014-07-30 03:11:09 +00009728 size = sizeof(struct i40e_q_vector *) * vsi->num_q_vectors;
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00009729 vsi->q_vectors = kzalloc(size, GFP_KERNEL);
9730 if (!vsi->q_vectors) {
9731 ret = -ENOMEM;
9732 goto err_vectors;
9733 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00009734 }
9735 return ret;
9736
9737err_vectors:
9738 kfree(vsi->tx_rings);
9739 return ret;
9740}
9741
9742/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009743 * i40e_vsi_mem_alloc - Allocates the next available struct vsi in the PF
9744 * @pf: board private structure
9745 * @type: type of VSI
9746 *
9747 * On error: returns error code (negative)
9748 * On success: returns vsi index in PF (positive)
9749 **/
9750static int i40e_vsi_mem_alloc(struct i40e_pf *pf, enum i40e_vsi_type type)
9751{
9752 int ret = -ENODEV;
9753 struct i40e_vsi *vsi;
9754 int vsi_idx;
9755 int i;
9756
9757 /* Need to protect the allocation of the VSIs at the PF level */
9758 mutex_lock(&pf->switch_mutex);
9759
9760 /* VSI list may be fragmented if VSI creation/destruction has
9761 * been happening. We can afford to do a quick scan to look
9762 * for any free VSIs in the list.
9763 *
9764 * find next empty vsi slot, looping back around if necessary
9765 */
9766 i = pf->next_vsi;
Mitch Williams505682c2014-05-20 08:01:37 +00009767 while (i < pf->num_alloc_vsi && pf->vsi[i])
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009768 i++;
Mitch Williams505682c2014-05-20 08:01:37 +00009769 if (i >= pf->num_alloc_vsi) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009770 i = 0;
9771 while (i < pf->next_vsi && pf->vsi[i])
9772 i++;
9773 }
9774
Mitch Williams505682c2014-05-20 08:01:37 +00009775 if (i < pf->num_alloc_vsi && !pf->vsi[i]) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009776 vsi_idx = i; /* Found one! */
9777 } else {
9778 ret = -ENODEV;
Alexander Duyck493fb302013-09-28 07:01:44 +00009779 goto unlock_pf; /* out of VSI slots! */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009780 }
9781 pf->next_vsi = ++i;
9782
9783 vsi = kzalloc(sizeof(*vsi), GFP_KERNEL);
9784 if (!vsi) {
9785 ret = -ENOMEM;
Alexander Duyck493fb302013-09-28 07:01:44 +00009786 goto unlock_pf;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009787 }
9788 vsi->type = type;
9789 vsi->back = pf;
Jacob Keller0da36b92017-04-19 09:25:55 -04009790 set_bit(__I40E_VSI_DOWN, vsi->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009791 vsi->flags = 0;
9792 vsi->idx = vsi_idx;
Jesse Brandeburgac26fc12015-09-28 14:12:37 -04009793 vsi->int_rate_limit = 0;
Anjali Singhai Jain5db4cb52015-02-24 06:58:49 +00009794 vsi->rss_table_size = (vsi->type == I40E_VSI_MAIN) ?
9795 pf->rss_table_size : 64;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009796 vsi->netdev_registered = false;
9797 vsi->work_limit = I40E_DEFAULT_IRQ_WORK;
Jacob Keller278e7d02016-10-05 09:30:37 -07009798 hash_init(vsi->mac_filter_hash);
Shannon Nelson63741842014-04-23 04:50:16 +00009799 vsi->irqs_ready = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009800
Alexander Duyck9f65e152013-09-28 06:00:58 +00009801 ret = i40e_set_num_rings_in_vsi(vsi);
9802 if (ret)
9803 goto err_rings;
9804
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00009805 ret = i40e_vsi_alloc_arrays(vsi, true);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00009806 if (ret)
Alexander Duyck9f65e152013-09-28 06:00:58 +00009807 goto err_rings;
Alexander Duyck493fb302013-09-28 07:01:44 +00009808
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009809 /* Setup default MSIX irq handler for VSI */
9810 i40e_vsi_setup_irqhandler(vsi, i40e_msix_clean_rings);
9811
Kiran Patil21659032015-09-30 14:09:03 -04009812 /* Initialize VSI lock */
Jacob Keller278e7d02016-10-05 09:30:37 -07009813 spin_lock_init(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009814 pf->vsi[vsi_idx] = vsi;
9815 ret = vsi_idx;
Alexander Duyck493fb302013-09-28 07:01:44 +00009816 goto unlock_pf;
9817
Alexander Duyck9f65e152013-09-28 06:00:58 +00009818err_rings:
Alexander Duyck493fb302013-09-28 07:01:44 +00009819 pf->next_vsi = i - 1;
9820 kfree(vsi);
9821unlock_pf:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009822 mutex_unlock(&pf->switch_mutex);
9823 return ret;
9824}
9825
9826/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00009827 * i40e_vsi_free_arrays - Free queue and vector pointer arrays for the VSI
9828 * @type: VSI pointer
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00009829 * @free_qvectors: a bool to specify if q_vectors need to be freed.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00009830 *
9831 * On error: returns error code (negative)
9832 * On success: returns 0
9833 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00009834static void i40e_vsi_free_arrays(struct i40e_vsi *vsi, bool free_qvectors)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00009835{
9836 /* free the ring and vector containers */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00009837 if (free_qvectors) {
9838 kfree(vsi->q_vectors);
9839 vsi->q_vectors = NULL;
9840 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00009841 kfree(vsi->tx_rings);
9842 vsi->tx_rings = NULL;
9843 vsi->rx_rings = NULL;
Björn Töpel74608d12017-05-24 07:55:35 +02009844 vsi->xdp_rings = NULL;
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00009845}
9846
9847/**
Helin Zhang28c58692015-10-26 19:44:27 -04009848 * i40e_clear_rss_config_user - clear the user configured RSS hash keys
9849 * and lookup table
9850 * @vsi: Pointer to VSI structure
9851 */
9852static void i40e_clear_rss_config_user(struct i40e_vsi *vsi)
9853{
9854 if (!vsi)
9855 return;
9856
9857 kfree(vsi->rss_hkey_user);
9858 vsi->rss_hkey_user = NULL;
9859
9860 kfree(vsi->rss_lut_user);
9861 vsi->rss_lut_user = NULL;
9862}
9863
9864/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009865 * i40e_vsi_clear - Deallocate the VSI provided
9866 * @vsi: the VSI being un-configured
9867 **/
9868static int i40e_vsi_clear(struct i40e_vsi *vsi)
9869{
9870 struct i40e_pf *pf;
9871
9872 if (!vsi)
9873 return 0;
9874
9875 if (!vsi->back)
9876 goto free_vsi;
9877 pf = vsi->back;
9878
9879 mutex_lock(&pf->switch_mutex);
9880 if (!pf->vsi[vsi->idx]) {
9881 dev_err(&pf->pdev->dev, "pf->vsi[%d] is NULL, just free vsi[%d](%p,type %d)\n",
9882 vsi->idx, vsi->idx, vsi, vsi->type);
9883 goto unlock_vsi;
9884 }
9885
9886 if (pf->vsi[vsi->idx] != vsi) {
9887 dev_err(&pf->pdev->dev,
9888 "pf->vsi[%d](%p, type %d) != vsi[%d](%p,type %d): no free!\n",
9889 pf->vsi[vsi->idx]->idx,
9890 pf->vsi[vsi->idx],
9891 pf->vsi[vsi->idx]->type,
9892 vsi->idx, vsi, vsi->type);
9893 goto unlock_vsi;
9894 }
9895
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00009896 /* updates the PF for this cleared vsi */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009897 i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
9898 i40e_put_lump(pf->irq_pile, vsi->base_vector, vsi->idx);
9899
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00009900 i40e_vsi_free_arrays(vsi, true);
Helin Zhang28c58692015-10-26 19:44:27 -04009901 i40e_clear_rss_config_user(vsi);
Alexander Duyck493fb302013-09-28 07:01:44 +00009902
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009903 pf->vsi[vsi->idx] = NULL;
9904 if (vsi->idx < pf->next_vsi)
9905 pf->next_vsi = vsi->idx;
9906
9907unlock_vsi:
9908 mutex_unlock(&pf->switch_mutex);
9909free_vsi:
9910 kfree(vsi);
9911
9912 return 0;
9913}
9914
9915/**
Alexander Duyck9f65e152013-09-28 06:00:58 +00009916 * i40e_vsi_clear_rings - Deallocates the Rx and Tx rings for the provided VSI
9917 * @vsi: the VSI being cleaned
9918 **/
Shannon Nelsonbe1d5ee2013-11-28 06:39:23 +00009919static void i40e_vsi_clear_rings(struct i40e_vsi *vsi)
Alexander Duyck9f65e152013-09-28 06:00:58 +00009920{
9921 int i;
9922
Greg Rose8e9dca52013-12-18 13:45:53 +00009923 if (vsi->tx_rings && vsi->tx_rings[0]) {
Neerav Parikhd7397642013-11-28 06:39:37 +00009924 for (i = 0; i < vsi->alloc_queue_pairs; i++) {
Mitch Williams00403f02013-09-28 07:13:13 +00009925 kfree_rcu(vsi->tx_rings[i], rcu);
9926 vsi->tx_rings[i] = NULL;
9927 vsi->rx_rings[i] = NULL;
Björn Töpel74608d12017-05-24 07:55:35 +02009928 if (vsi->xdp_rings)
9929 vsi->xdp_rings[i] = NULL;
Mitch Williams00403f02013-09-28 07:13:13 +00009930 }
Shannon Nelsonbe1d5ee2013-11-28 06:39:23 +00009931 }
Alexander Duyck9f65e152013-09-28 06:00:58 +00009932}
9933
9934/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009935 * i40e_alloc_rings - Allocates the Rx and Tx rings for the provided VSI
9936 * @vsi: the VSI being configured
9937 **/
9938static int i40e_alloc_rings(struct i40e_vsi *vsi)
9939{
Björn Töpel74608d12017-05-24 07:55:35 +02009940 int i, qpv = i40e_enabled_xdp_vsi(vsi) ? 3 : 2;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009941 struct i40e_pf *pf = vsi->back;
Björn Töpel74608d12017-05-24 07:55:35 +02009942 struct i40e_ring *ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009943
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009944 /* Set basic values in the rings to be used later during open() */
Neerav Parikhd7397642013-11-28 06:39:37 +00009945 for (i = 0; i < vsi->alloc_queue_pairs; i++) {
Shannon Nelsonac6c5e32013-11-20 10:02:57 +00009946 /* allocate space for both Tx and Rx in one shot */
Björn Töpel74608d12017-05-24 07:55:35 +02009947 ring = kcalloc(qpv, sizeof(struct i40e_ring), GFP_KERNEL);
9948 if (!ring)
Alexander Duyck9f65e152013-09-28 06:00:58 +00009949 goto err_out;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009950
Björn Töpel74608d12017-05-24 07:55:35 +02009951 ring->queue_index = i;
9952 ring->reg_idx = vsi->base_queue + i;
9953 ring->ring_active = false;
9954 ring->vsi = vsi;
9955 ring->netdev = vsi->netdev;
9956 ring->dev = &pf->pdev->dev;
9957 ring->count = vsi->num_desc;
9958 ring->size = 0;
9959 ring->dcb_tc = 0;
Jacob Kellerd36e41d2017-06-23 04:24:46 -04009960 if (vsi->back->hw_features & I40E_HW_WB_ON_ITR_CAPABLE)
Björn Töpel74608d12017-05-24 07:55:35 +02009961 ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
9962 ring->tx_itr_setting = pf->tx_itr_default;
9963 vsi->tx_rings[i] = ring++;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009964
Björn Töpel74608d12017-05-24 07:55:35 +02009965 if (!i40e_enabled_xdp_vsi(vsi))
9966 goto setup_rx;
9967
9968 ring->queue_index = vsi->alloc_queue_pairs + i;
9969 ring->reg_idx = vsi->base_queue + ring->queue_index;
9970 ring->ring_active = false;
9971 ring->vsi = vsi;
9972 ring->netdev = NULL;
9973 ring->dev = &pf->pdev->dev;
9974 ring->count = vsi->num_desc;
9975 ring->size = 0;
9976 ring->dcb_tc = 0;
Jacob Kellerd36e41d2017-06-23 04:24:46 -04009977 if (vsi->back->hw_features & I40E_HW_WB_ON_ITR_CAPABLE)
Björn Töpel74608d12017-05-24 07:55:35 +02009978 ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
9979 set_ring_xdp(ring);
9980 ring->tx_itr_setting = pf->tx_itr_default;
9981 vsi->xdp_rings[i] = ring++;
9982
9983setup_rx:
9984 ring->queue_index = i;
9985 ring->reg_idx = vsi->base_queue + i;
9986 ring->ring_active = false;
9987 ring->vsi = vsi;
9988 ring->netdev = vsi->netdev;
9989 ring->dev = &pf->pdev->dev;
9990 ring->count = vsi->num_desc;
9991 ring->size = 0;
9992 ring->dcb_tc = 0;
9993 ring->rx_itr_setting = pf->rx_itr_default;
9994 vsi->rx_rings[i] = ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009995 }
9996
9997 return 0;
Alexander Duyck9f65e152013-09-28 06:00:58 +00009998
9999err_out:
10000 i40e_vsi_clear_rings(vsi);
10001 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010002}
10003
10004/**
10005 * i40e_reserve_msix_vectors - Reserve MSI-X vectors in the kernel
10006 * @pf: board private structure
10007 * @vectors: the number of MSI-X vectors to request
10008 *
10009 * Returns the number of vectors reserved, or error
10010 **/
10011static int i40e_reserve_msix_vectors(struct i40e_pf *pf, int vectors)
10012{
Alexander Gordeev7b37f372014-02-18 11:11:42 +010010013 vectors = pci_enable_msix_range(pf->pdev, pf->msix_entries,
10014 I40E_MIN_MSIX, vectors);
10015 if (vectors < 0) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010016 dev_info(&pf->pdev->dev,
Alexander Gordeev7b37f372014-02-18 11:11:42 +010010017 "MSI-X vector reservation failed: %d\n", vectors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010018 vectors = 0;
10019 }
10020
10021 return vectors;
10022}
10023
10024/**
10025 * i40e_init_msix - Setup the MSIX capability
10026 * @pf: board private structure
10027 *
10028 * Work with the OS to set up the MSIX vectors needed.
10029 *
Shannon Nelson3b444392015-02-26 16:15:57 +000010030 * Returns the number of vectors reserved or negative on failure
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010031 **/
10032static int i40e_init_msix(struct i40e_pf *pf)
10033{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010034 struct i40e_hw *hw = &pf->hw;
Jacob Kellerc0cf70a2017-01-24 10:24:01 -080010035 int cpus, extra_vectors;
Shannon Nelson1e200e42015-02-27 09:15:24 +000010036 int vectors_left;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010037 int v_budget, i;
Shannon Nelson3b444392015-02-26 16:15:57 +000010038 int v_actual;
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060010039 int iwarp_requested = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010040
10041 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
10042 return -ENODEV;
10043
10044 /* The number of vectors we'll request will be comprised of:
10045 * - Add 1 for "other" cause for Admin Queue events, etc.
10046 * - The number of LAN queue pairs
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +000010047 * - Queues being used for RSS.
10048 * We don't need as many as max_rss_size vectors.
10049 * use rss_size instead in the calculation since that
10050 * is governed by number of cpus in the system.
10051 * - assumes symmetric Tx/Rx pairing
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010052 * - The number of VMDq pairs
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060010053 * - The CPU count within the NUMA node if iWARP is enabled
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010054 * Once we count this up, try the request.
10055 *
10056 * If we can't get what we want, we'll simplify to nearly nothing
10057 * and try again. If that still fails, we punt.
10058 */
Shannon Nelson1e200e42015-02-27 09:15:24 +000010059 vectors_left = hw->func_caps.num_msix_vectors;
10060 v_budget = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010061
Shannon Nelson1e200e42015-02-27 09:15:24 +000010062 /* reserve one vector for miscellaneous handler */
10063 if (vectors_left) {
10064 v_budget++;
10065 vectors_left--;
10066 }
10067
Jacob Kellerc0cf70a2017-01-24 10:24:01 -080010068 /* reserve some vectors for the main PF traffic queues. Initially we
10069 * only reserve at most 50% of the available vectors, in the case that
10070 * the number of online CPUs is large. This ensures that we can enable
10071 * extra features as well. Once we've enabled the other features, we
10072 * will use any remaining vectors to reach as close as we can to the
10073 * number of online CPUs.
10074 */
10075 cpus = num_online_cpus();
10076 pf->num_lan_msix = min_t(int, cpus, vectors_left / 2);
Shannon Nelson1e200e42015-02-27 09:15:24 +000010077 vectors_left -= pf->num_lan_msix;
Shannon Nelson1e200e42015-02-27 09:15:24 +000010078
10079 /* reserve one vector for sideband flow director */
10080 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
10081 if (vectors_left) {
Tushar Davea70e4072016-05-16 12:40:53 -070010082 pf->num_fdsb_msix = 1;
Shannon Nelson1e200e42015-02-27 09:15:24 +000010083 v_budget++;
10084 vectors_left--;
10085 } else {
Tushar Davea70e4072016-05-16 12:40:53 -070010086 pf->num_fdsb_msix = 0;
Shannon Nelson1e200e42015-02-27 09:15:24 +000010087 }
10088 }
John W Linville83840e42015-01-14 03:06:28 +000010089
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060010090 /* can we reserve enough for iWARP? */
10091 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
Stefan Assmann4ce20ab2016-09-19 13:37:50 +020010092 iwarp_requested = pf->num_iwarp_msix;
10093
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060010094 if (!vectors_left)
10095 pf->num_iwarp_msix = 0;
10096 else if (vectors_left < pf->num_iwarp_msix)
10097 pf->num_iwarp_msix = 1;
10098 v_budget += pf->num_iwarp_msix;
10099 vectors_left -= pf->num_iwarp_msix;
10100 }
10101
Shannon Nelson1e200e42015-02-27 09:15:24 +000010102 /* any vectors left over go for VMDq support */
10103 if (pf->flags & I40E_FLAG_VMDQ_ENABLED) {
10104 int vmdq_vecs_wanted = pf->num_vmdq_vsis * pf->num_vmdq_qps;
10105 int vmdq_vecs = min_t(int, vectors_left, vmdq_vecs_wanted);
10106
Stefan Assmann9ca57e92016-09-19 13:37:49 +020010107 if (!vectors_left) {
10108 pf->num_vmdq_msix = 0;
10109 pf->num_vmdq_qps = 0;
10110 } else {
10111 /* if we're short on vectors for what's desired, we limit
10112 * the queues per vmdq. If this is still more than are
10113 * available, the user will need to change the number of
10114 * queues/vectors used by the PF later with the ethtool
10115 * channels command
10116 */
10117 if (vmdq_vecs < vmdq_vecs_wanted)
10118 pf->num_vmdq_qps = 1;
10119 pf->num_vmdq_msix = pf->num_vmdq_qps;
Shannon Nelson1e200e42015-02-27 09:15:24 +000010120
Stefan Assmann9ca57e92016-09-19 13:37:49 +020010121 v_budget += vmdq_vecs;
10122 vectors_left -= vmdq_vecs;
10123 }
Shannon Nelson1e200e42015-02-27 09:15:24 +000010124 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010125
Jacob Kellerc0cf70a2017-01-24 10:24:01 -080010126 /* On systems with a large number of SMP cores, we previously limited
10127 * the number of vectors for num_lan_msix to be at most 50% of the
10128 * available vectors, to allow for other features. Now, we add back
10129 * the remaining vectors. However, we ensure that the total
10130 * num_lan_msix will not exceed num_online_cpus(). To do this, we
10131 * calculate the number of vectors we can add without going over the
10132 * cap of CPUs. For systems with a small number of CPUs this will be
10133 * zero.
10134 */
10135 extra_vectors = min_t(int, cpus - pf->num_lan_msix, vectors_left);
10136 pf->num_lan_msix += extra_vectors;
10137 vectors_left -= extra_vectors;
10138
10139 WARN(vectors_left < 0,
10140 "Calculation of remaining vectors underflowed. This is an accounting bug when determining total MSI-X vectors.\n");
10141
10142 v_budget += pf->num_lan_msix;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010143 pf->msix_entries = kcalloc(v_budget, sizeof(struct msix_entry),
10144 GFP_KERNEL);
10145 if (!pf->msix_entries)
10146 return -ENOMEM;
10147
10148 for (i = 0; i < v_budget; i++)
10149 pf->msix_entries[i].entry = i;
Shannon Nelson3b444392015-02-26 16:15:57 +000010150 v_actual = i40e_reserve_msix_vectors(pf, v_budget);
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +000010151
Shannon Nelson3b444392015-02-26 16:15:57 +000010152 if (v_actual < I40E_MIN_MSIX) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010153 pf->flags &= ~I40E_FLAG_MSIX_ENABLED;
10154 kfree(pf->msix_entries);
10155 pf->msix_entries = NULL;
Guilherme G Piccoli4c95aa52016-09-22 10:03:58 -030010156 pci_disable_msix(pf->pdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010157 return -ENODEV;
10158
Shannon Nelson3b444392015-02-26 16:15:57 +000010159 } else if (v_actual == I40E_MIN_MSIX) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010160 /* Adjust for minimal MSIX use */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010161 pf->num_vmdq_vsis = 0;
10162 pf->num_vmdq_qps = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010163 pf->num_lan_qps = 1;
10164 pf->num_lan_msix = 1;
10165
Shannon Nelson3e6b1cf2017-10-10 14:56:58 -070010166 } else if (v_actual != v_budget) {
Stefan Assmann4ce20ab2016-09-19 13:37:50 +020010167 /* If we have limited resources, we will start with no vectors
10168 * for the special features and then allocate vectors to some
10169 * of these features based on the policy and at the end disable
10170 * the features that did not get any vectors.
10171 */
Shannon Nelson3b444392015-02-26 16:15:57 +000010172 int vec;
10173
Stefan Assmann4ce20ab2016-09-19 13:37:50 +020010174 dev_info(&pf->pdev->dev,
Shannon Nelson3e6b1cf2017-10-10 14:56:58 -070010175 "MSI-X vector limit reached with %d, wanted %d, attempting to redistribute vectors\n",
10176 v_actual, v_budget);
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +000010177 /* reserve the misc vector */
Shannon Nelson3b444392015-02-26 16:15:57 +000010178 vec = v_actual - 1;
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +000010179
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010180 /* Scale vector usage down */
10181 pf->num_vmdq_msix = 1; /* force VMDqs to only one vector */
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +000010182 pf->num_vmdq_vsis = 1;
Shannon Nelson1e200e42015-02-27 09:15:24 +000010183 pf->num_vmdq_qps = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010184
10185 /* partition out the remaining vectors */
10186 switch (vec) {
10187 case 2:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010188 pf->num_lan_msix = 1;
10189 break;
10190 case 3:
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060010191 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
10192 pf->num_lan_msix = 1;
10193 pf->num_iwarp_msix = 1;
10194 } else {
10195 pf->num_lan_msix = 2;
10196 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010197 break;
10198 default:
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060010199 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
10200 pf->num_iwarp_msix = min_t(int, (vec / 3),
10201 iwarp_requested);
10202 pf->num_vmdq_vsis = min_t(int, (vec / 3),
10203 I40E_DEFAULT_NUM_VMDQ_VSI);
10204 } else {
10205 pf->num_vmdq_vsis = min_t(int, (vec / 2),
10206 I40E_DEFAULT_NUM_VMDQ_VSI);
10207 }
Stefan Assmannabd97a92016-09-19 13:37:51 +020010208 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
10209 pf->num_fdsb_msix = 1;
10210 vec--;
10211 }
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060010212 pf->num_lan_msix = min_t(int,
10213 (vec - (pf->num_iwarp_msix + pf->num_vmdq_vsis)),
10214 pf->num_lan_msix);
Stefan Assmann4ce20ab2016-09-19 13:37:50 +020010215 pf->num_lan_qps = pf->num_lan_msix;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010216 break;
10217 }
10218 }
10219
Stefan Assmannabd97a92016-09-19 13:37:51 +020010220 if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
10221 (pf->num_fdsb_msix == 0)) {
10222 dev_info(&pf->pdev->dev, "Sideband Flowdir disabled, not enough MSI-X vectors\n");
10223 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
Amritha Nambiar2f4b4112017-10-27 02:36:01 -070010224 pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
Stefan Assmannabd97a92016-09-19 13:37:51 +020010225 }
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +000010226 if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
10227 (pf->num_vmdq_msix == 0)) {
10228 dev_info(&pf->pdev->dev, "VMDq disabled, not enough MSI-X vectors\n");
10229 pf->flags &= ~I40E_FLAG_VMDQ_ENABLED;
10230 }
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060010231
10232 if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
10233 (pf->num_iwarp_msix == 0)) {
10234 dev_info(&pf->pdev->dev, "IWARP disabled, not enough MSI-X vectors\n");
10235 pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
10236 }
Stefan Assmann4ce20ab2016-09-19 13:37:50 +020010237 i40e_debug(&pf->hw, I40E_DEBUG_INIT,
10238 "MSI-X vector distribution: PF %d, VMDq %d, FDSB %d, iWARP %d\n",
10239 pf->num_lan_msix,
10240 pf->num_vmdq_msix * pf->num_vmdq_vsis,
10241 pf->num_fdsb_msix,
10242 pf->num_iwarp_msix);
10243
Shannon Nelson3b444392015-02-26 16:15:57 +000010244 return v_actual;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010245}
10246
10247/**
Greg Rose90e04072014-03-06 08:59:57 +000010248 * i40e_vsi_alloc_q_vector - Allocate memory for a single interrupt vector
Alexander Duyck493fb302013-09-28 07:01:44 +000010249 * @vsi: the VSI being configured
10250 * @v_idx: index of the vector in the vsi struct
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -030010251 * @cpu: cpu to be used on affinity_mask
Alexander Duyck493fb302013-09-28 07:01:44 +000010252 *
10253 * We allocate one q_vector. If allocation fails we return -ENOMEM.
10254 **/
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -030010255static int i40e_vsi_alloc_q_vector(struct i40e_vsi *vsi, int v_idx, int cpu)
Alexander Duyck493fb302013-09-28 07:01:44 +000010256{
10257 struct i40e_q_vector *q_vector;
10258
10259 /* allocate q_vector */
10260 q_vector = kzalloc(sizeof(struct i40e_q_vector), GFP_KERNEL);
10261 if (!q_vector)
10262 return -ENOMEM;
10263
10264 q_vector->vsi = vsi;
10265 q_vector->v_idx = v_idx;
Jacob Keller759dc4a2017-07-14 09:10:10 -040010266 cpumask_copy(&q_vector->affinity_mask, cpu_possible_mask);
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -030010267
Alexander Duyck493fb302013-09-28 07:01:44 +000010268 if (vsi->netdev)
10269 netif_napi_add(vsi->netdev, &q_vector->napi,
Jesse Brandeburgeefeace2014-05-10 04:49:13 +000010270 i40e_napi_poll, NAPI_POLL_WEIGHT);
Alexander Duyck493fb302013-09-28 07:01:44 +000010271
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +000010272 q_vector->rx.latency_range = I40E_LOW_LATENCY;
10273 q_vector->tx.latency_range = I40E_LOW_LATENCY;
10274
Alexander Duyck493fb302013-09-28 07:01:44 +000010275 /* tie q_vector and vsi together */
10276 vsi->q_vectors[v_idx] = q_vector;
10277
10278 return 0;
10279}
10280
10281/**
Greg Rose90e04072014-03-06 08:59:57 +000010282 * i40e_vsi_alloc_q_vectors - Allocate memory for interrupt vectors
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010283 * @vsi: the VSI being configured
10284 *
10285 * We allocate one q_vector per queue interrupt. If allocation fails we
10286 * return -ENOMEM.
10287 **/
Greg Rose90e04072014-03-06 08:59:57 +000010288static int i40e_vsi_alloc_q_vectors(struct i40e_vsi *vsi)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010289{
10290 struct i40e_pf *pf = vsi->back;
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -030010291 int err, v_idx, num_q_vectors, current_cpu;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010292
10293 /* if not MSIX, give the one vector only to the LAN VSI */
10294 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
10295 num_q_vectors = vsi->num_q_vectors;
10296 else if (vsi == pf->vsi[pf->lan_vsi])
10297 num_q_vectors = 1;
10298 else
10299 return -EINVAL;
10300
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -030010301 current_cpu = cpumask_first(cpu_online_mask);
10302
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010303 for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -030010304 err = i40e_vsi_alloc_q_vector(vsi, v_idx, current_cpu);
Alexander Duyck493fb302013-09-28 07:01:44 +000010305 if (err)
10306 goto err_out;
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -030010307 current_cpu = cpumask_next(current_cpu, cpu_online_mask);
10308 if (unlikely(current_cpu >= nr_cpu_ids))
10309 current_cpu = cpumask_first(cpu_online_mask);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010310 }
10311
10312 return 0;
Alexander Duyck493fb302013-09-28 07:01:44 +000010313
10314err_out:
10315 while (v_idx--)
10316 i40e_free_q_vector(vsi, v_idx);
10317
10318 return err;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010319}
10320
10321/**
10322 * i40e_init_interrupt_scheme - Determine proper interrupt scheme
10323 * @pf: board private structure to initialize
10324 **/
Jesse Brandeburgc11472802015-04-07 19:45:39 -040010325static int i40e_init_interrupt_scheme(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010326{
Shannon Nelson3b444392015-02-26 16:15:57 +000010327 int vectors = 0;
10328 ssize_t size;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010329
10330 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
Shannon Nelson3b444392015-02-26 16:15:57 +000010331 vectors = i40e_init_msix(pf);
10332 if (vectors < 0) {
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080010333 pf->flags &= ~(I40E_FLAG_MSIX_ENABLED |
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060010334 I40E_FLAG_IWARP_ENABLED |
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080010335 I40E_FLAG_RSS_ENABLED |
Neerav Parikh4d9b6042014-05-22 06:31:51 +000010336 I40E_FLAG_DCB_CAPABLE |
Dave Ertmana0362442016-08-29 17:38:26 -070010337 I40E_FLAG_DCB_ENABLED |
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080010338 I40E_FLAG_SRIOV_ENABLED |
10339 I40E_FLAG_FD_SB_ENABLED |
10340 I40E_FLAG_FD_ATR_ENABLED |
10341 I40E_FLAG_VMDQ_ENABLED);
Amritha Nambiar2f4b4112017-10-27 02:36:01 -070010342 pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010343
10344 /* rework the queue expectations without MSIX */
10345 i40e_determine_queue_usage(pf);
10346 }
10347 }
10348
10349 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED) &&
10350 (pf->flags & I40E_FLAG_MSI_ENABLED)) {
Catherine Sullivan77fa28b2014-02-20 19:29:17 -080010351 dev_info(&pf->pdev->dev, "MSI-X not available, trying MSI\n");
Shannon Nelson3b444392015-02-26 16:15:57 +000010352 vectors = pci_enable_msi(pf->pdev);
10353 if (vectors < 0) {
10354 dev_info(&pf->pdev->dev, "MSI init failed - %d\n",
10355 vectors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010356 pf->flags &= ~I40E_FLAG_MSI_ENABLED;
10357 }
Shannon Nelson3b444392015-02-26 16:15:57 +000010358 vectors = 1; /* one MSI or Legacy vector */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010359 }
10360
Shannon Nelson958a3e32013-09-28 07:13:28 +000010361 if (!(pf->flags & (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED)))
Catherine Sullivan77fa28b2014-02-20 19:29:17 -080010362 dev_info(&pf->pdev->dev, "MSI-X and MSI not available, falling back to Legacy IRQ\n");
Shannon Nelson958a3e32013-09-28 07:13:28 +000010363
Shannon Nelson3b444392015-02-26 16:15:57 +000010364 /* set up vector assignment tracking */
10365 size = sizeof(struct i40e_lump_tracking) + (sizeof(u16) * vectors);
10366 pf->irq_pile = kzalloc(size, GFP_KERNEL);
Jesse Brandeburgc11472802015-04-07 19:45:39 -040010367 if (!pf->irq_pile) {
10368 dev_err(&pf->pdev->dev, "error allocating irq_pile memory\n");
10369 return -ENOMEM;
10370 }
Shannon Nelson3b444392015-02-26 16:15:57 +000010371 pf->irq_pile->num_entries = vectors;
10372 pf->irq_pile->search_hint = 0;
10373
Jesse Brandeburgc11472802015-04-07 19:45:39 -040010374 /* track first vector for misc interrupts, ignore return */
Shannon Nelson3b444392015-02-26 16:15:57 +000010375 (void)i40e_get_lump(pf, pf->irq_pile, 1, I40E_PILE_VALID_BIT - 1);
Jesse Brandeburgc11472802015-04-07 19:45:39 -040010376
10377 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010378}
10379
10380/**
Jacob Kellerb980c062017-07-14 09:27:06 -040010381 * i40e_restore_interrupt_scheme - Restore the interrupt scheme
10382 * @pf: private board data structure
10383 *
10384 * Restore the interrupt scheme that was cleared when we suspended the
10385 * device. This should be called during resume to re-allocate the q_vectors
10386 * and reacquire IRQs.
10387 */
10388static int i40e_restore_interrupt_scheme(struct i40e_pf *pf)
10389{
10390 int err, i;
10391
10392 /* We cleared the MSI and MSI-X flags when disabling the old interrupt
10393 * scheme. We need to re-enabled them here in order to attempt to
10394 * re-acquire the MSI or MSI-X vectors
10395 */
10396 pf->flags |= (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
10397
10398 err = i40e_init_interrupt_scheme(pf);
10399 if (err)
10400 return err;
10401
10402 /* Now that we've re-acquired IRQs, we need to remap the vectors and
10403 * rings together again.
10404 */
10405 for (i = 0; i < pf->num_alloc_vsi; i++) {
10406 if (pf->vsi[i]) {
10407 err = i40e_vsi_alloc_q_vectors(pf->vsi[i]);
10408 if (err)
10409 goto err_unwind;
10410 i40e_vsi_map_rings_to_vectors(pf->vsi[i]);
10411 }
10412 }
10413
10414 err = i40e_setup_misc_vector(pf);
10415 if (err)
10416 goto err_unwind;
10417
10418 return 0;
10419
10420err_unwind:
10421 while (i--) {
10422 if (pf->vsi[i])
10423 i40e_vsi_free_q_vectors(pf->vsi[i]);
10424 }
10425
10426 return err;
10427}
Jacob Kellerb980c062017-07-14 09:27:06 -040010428
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010429/**
10430 * i40e_setup_misc_vector - Setup the misc vector to handle non queue events
10431 * @pf: board private structure
10432 *
10433 * This sets up the handler for MSIX 0, which is used to manage the
10434 * non-queue interrupts, e.g. AdminQ and errors. This is not used
10435 * when in MSI or Legacy interrupt mode.
10436 **/
10437static int i40e_setup_misc_vector(struct i40e_pf *pf)
10438{
10439 struct i40e_hw *hw = &pf->hw;
10440 int err = 0;
10441
Jacob Kellerc17401a2017-07-14 09:27:02 -040010442 /* Only request the IRQ once, the first time through. */
10443 if (!test_and_set_bit(__I40E_MISC_IRQ_REQUESTED, pf->state)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010444 err = request_irq(pf->msix_entries[0].vector,
Carolyn Wybornyb294ac72014-12-11 07:06:39 +000010445 i40e_intr, 0, pf->int_name, pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010446 if (err) {
Jacob Kellerc17401a2017-07-14 09:27:02 -040010447 clear_bit(__I40E_MISC_IRQ_REQUESTED, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010448 dev_info(&pf->pdev->dev,
Catherine Sullivan77fa28b2014-02-20 19:29:17 -080010449 "request_irq for %s failed: %d\n",
Carolyn Wybornyb294ac72014-12-11 07:06:39 +000010450 pf->int_name, err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010451 return -EFAULT;
10452 }
10453 }
10454
Jacob Kellerab437b52014-12-14 01:55:08 +000010455 i40e_enable_misc_int_causes(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010456
10457 /* associate no queues to the misc vector */
10458 wr32(hw, I40E_PFINT_LNKLST0, I40E_QUEUE_END_OF_LIST);
10459 wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), I40E_ITR_8K);
10460
10461 i40e_flush(hw);
10462
Jacob Kellerdbadbbe2017-09-07 08:05:49 -040010463 i40e_irq_dynamic_enable_icr0(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010464
10465 return err;
10466}
10467
10468/**
Anjali Singhai Jain95a73782015-12-22 14:25:04 -080010469 * i40e_get_rss_aq - Get RSS keys and lut by using AQ commands
10470 * @vsi: Pointer to vsi structure
10471 * @seed: Buffter to store the hash keys
10472 * @lut: Buffer to store the lookup table entries
10473 * @lut_size: Size of buffer to store the lookup table entries
10474 *
10475 * Return 0 on success, negative on failure
10476 */
10477static int i40e_get_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
10478 u8 *lut, u16 lut_size)
10479{
10480 struct i40e_pf *pf = vsi->back;
10481 struct i40e_hw *hw = &pf->hw;
10482 int ret = 0;
10483
10484 if (seed) {
10485 ret = i40e_aq_get_rss_key(hw, vsi->id,
10486 (struct i40e_aqc_get_set_rss_key_data *)seed);
10487 if (ret) {
10488 dev_info(&pf->pdev->dev,
10489 "Cannot get RSS key, err %s aq_err %s\n",
10490 i40e_stat_str(&pf->hw, ret),
10491 i40e_aq_str(&pf->hw,
10492 pf->hw.aq.asq_last_status));
10493 return ret;
10494 }
10495 }
10496
10497 if (lut) {
10498 bool pf_lut = vsi->type == I40E_VSI_MAIN ? true : false;
10499
10500 ret = i40e_aq_get_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
10501 if (ret) {
10502 dev_info(&pf->pdev->dev,
10503 "Cannot get RSS lut, err %s aq_err %s\n",
10504 i40e_stat_str(&pf->hw, ret),
10505 i40e_aq_str(&pf->hw,
10506 pf->hw.aq.asq_last_status));
10507 return ret;
10508 }
10509 }
10510
10511 return ret;
10512}
10513
10514/**
Helin Zhang043dd652015-10-21 19:56:23 -040010515 * i40e_config_rss_reg - Configure RSS keys and lut by writing registers
Helin Zhange69ff812015-10-21 19:56:22 -040010516 * @vsi: Pointer to vsi structure
10517 * @seed: RSS hash seed
10518 * @lut: Lookup table
10519 * @lut_size: Lookup table size
10520 *
10521 * Returns 0 on success, negative on failure
10522 **/
10523static int i40e_config_rss_reg(struct i40e_vsi *vsi, const u8 *seed,
10524 const u8 *lut, u16 lut_size)
10525{
10526 struct i40e_pf *pf = vsi->back;
10527 struct i40e_hw *hw = &pf->hw;
Mitch Williamsc4e18682016-04-12 08:30:40 -070010528 u16 vf_id = vsi->vf_id;
Helin Zhange69ff812015-10-21 19:56:22 -040010529 u8 i;
10530
10531 /* Fill out hash function seed */
10532 if (seed) {
10533 u32 *seed_dw = (u32 *)seed;
10534
Mitch Williamsc4e18682016-04-12 08:30:40 -070010535 if (vsi->type == I40E_VSI_MAIN) {
10536 for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
Lihong Yang26f77e52017-01-30 12:29:33 -080010537 wr32(hw, I40E_PFQF_HKEY(i), seed_dw[i]);
Mitch Williamsc4e18682016-04-12 08:30:40 -070010538 } else if (vsi->type == I40E_VSI_SRIOV) {
10539 for (i = 0; i <= I40E_VFQF_HKEY1_MAX_INDEX; i++)
Lihong Yang26f77e52017-01-30 12:29:33 -080010540 wr32(hw, I40E_VFQF_HKEY1(i, vf_id), seed_dw[i]);
Mitch Williamsc4e18682016-04-12 08:30:40 -070010541 } else {
10542 dev_err(&pf->pdev->dev, "Cannot set RSS seed - invalid VSI type\n");
10543 }
Helin Zhange69ff812015-10-21 19:56:22 -040010544 }
10545
10546 if (lut) {
10547 u32 *lut_dw = (u32 *)lut;
10548
Mitch Williamsc4e18682016-04-12 08:30:40 -070010549 if (vsi->type == I40E_VSI_MAIN) {
10550 if (lut_size != I40E_HLUT_ARRAY_SIZE)
10551 return -EINVAL;
10552 for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
10553 wr32(hw, I40E_PFQF_HLUT(i), lut_dw[i]);
10554 } else if (vsi->type == I40E_VSI_SRIOV) {
10555 if (lut_size != I40E_VF_HLUT_ARRAY_SIZE)
10556 return -EINVAL;
10557 for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
Lihong Yang26f77e52017-01-30 12:29:33 -080010558 wr32(hw, I40E_VFQF_HLUT1(i, vf_id), lut_dw[i]);
Mitch Williamsc4e18682016-04-12 08:30:40 -070010559 } else {
10560 dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
10561 }
Helin Zhange69ff812015-10-21 19:56:22 -040010562 }
10563 i40e_flush(hw);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -040010564
10565 return 0;
10566}
10567
10568/**
Helin Zhang043dd652015-10-21 19:56:23 -040010569 * i40e_get_rss_reg - Get the RSS keys and lut by reading registers
10570 * @vsi: Pointer to VSI structure
10571 * @seed: Buffer to store the keys
10572 * @lut: Buffer to store the lookup table entries
10573 * @lut_size: Size of buffer to store the lookup table entries
10574 *
10575 * Returns 0 on success, negative on failure
10576 */
10577static int i40e_get_rss_reg(struct i40e_vsi *vsi, u8 *seed,
10578 u8 *lut, u16 lut_size)
10579{
10580 struct i40e_pf *pf = vsi->back;
10581 struct i40e_hw *hw = &pf->hw;
10582 u16 i;
10583
10584 if (seed) {
10585 u32 *seed_dw = (u32 *)seed;
10586
10587 for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
Shannon Nelson272cdaf22016-02-17 16:12:21 -080010588 seed_dw[i] = i40e_read_rx_ctl(hw, I40E_PFQF_HKEY(i));
Helin Zhang043dd652015-10-21 19:56:23 -040010589 }
10590 if (lut) {
10591 u32 *lut_dw = (u32 *)lut;
10592
10593 if (lut_size != I40E_HLUT_ARRAY_SIZE)
10594 return -EINVAL;
10595 for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
10596 lut_dw[i] = rd32(hw, I40E_PFQF_HLUT(i));
10597 }
10598
10599 return 0;
10600}
10601
10602/**
10603 * i40e_config_rss - Configure RSS keys and lut
10604 * @vsi: Pointer to VSI structure
10605 * @seed: RSS hash seed
10606 * @lut: Lookup table
10607 * @lut_size: Lookup table size
10608 *
10609 * Returns 0 on success, negative on failure
10610 */
10611int i40e_config_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
10612{
10613 struct i40e_pf *pf = vsi->back;
10614
Jacob Kellerd36e41d2017-06-23 04:24:46 -040010615 if (pf->hw_features & I40E_HW_RSS_AQ_CAPABLE)
Helin Zhang043dd652015-10-21 19:56:23 -040010616 return i40e_config_rss_aq(vsi, seed, lut, lut_size);
10617 else
10618 return i40e_config_rss_reg(vsi, seed, lut, lut_size);
10619}
10620
10621/**
10622 * i40e_get_rss - Get RSS keys and lut
10623 * @vsi: Pointer to VSI structure
10624 * @seed: Buffer to store the keys
10625 * @lut: Buffer to store the lookup table entries
10626 * lut_size: Size of buffer to store the lookup table entries
10627 *
10628 * Returns 0 on success, negative on failure
10629 */
10630int i40e_get_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
10631{
Anjali Singhai Jain95a73782015-12-22 14:25:04 -080010632 struct i40e_pf *pf = vsi->back;
10633
Jacob Kellerd36e41d2017-06-23 04:24:46 -040010634 if (pf->hw_features & I40E_HW_RSS_AQ_CAPABLE)
Anjali Singhai Jain95a73782015-12-22 14:25:04 -080010635 return i40e_get_rss_aq(vsi, seed, lut, lut_size);
10636 else
10637 return i40e_get_rss_reg(vsi, seed, lut, lut_size);
Helin Zhang043dd652015-10-21 19:56:23 -040010638}
10639
10640/**
Helin Zhange69ff812015-10-21 19:56:22 -040010641 * i40e_fill_rss_lut - Fill the RSS lookup table with default values
10642 * @pf: Pointer to board private structure
10643 * @lut: Lookup table
10644 * @rss_table_size: Lookup table size
10645 * @rss_size: Range of queue number for hashing
10646 */
Alan Bradyf1582352016-08-24 11:33:46 -070010647void i40e_fill_rss_lut(struct i40e_pf *pf, u8 *lut,
10648 u16 rss_table_size, u16 rss_size)
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -040010649{
Helin Zhange69ff812015-10-21 19:56:22 -040010650 u16 i;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -040010651
Helin Zhange69ff812015-10-21 19:56:22 -040010652 for (i = 0; i < rss_table_size; i++)
10653 lut[i] = i % rss_size;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -040010654}
10655
10656/**
Helin Zhang043dd652015-10-21 19:56:23 -040010657 * i40e_pf_config_rss - Prepare for RSS if used
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010658 * @pf: board private structure
10659 **/
Helin Zhang043dd652015-10-21 19:56:23 -040010660static int i40e_pf_config_rss(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010661{
Anjali Singhai Jain66ddcff2015-02-24 06:58:50 +000010662 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -040010663 u8 seed[I40E_HKEY_ARRAY_SIZE];
Helin Zhange69ff812015-10-21 19:56:22 -040010664 u8 *lut;
Anjali Singhai Jain4617e8c2013-11-20 10:02:56 +000010665 struct i40e_hw *hw = &pf->hw;
Carolyn Wybornye157ea32014-06-03 23:50:22 +000010666 u32 reg_val;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -040010667 u64 hena;
Helin Zhange69ff812015-10-21 19:56:22 -040010668 int ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010669
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010670 /* By default we enable TCP/UDP with IPv4/IPv6 ptypes */
Shannon Nelson272cdaf22016-02-17 16:12:21 -080010671 hena = (u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(0)) |
10672 ((u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(1)) << 32);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -040010673 hena |= i40e_pf_get_default_rss_hena(pf);
10674
Shannon Nelson272cdaf22016-02-17 16:12:21 -080010675 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), (u32)hena);
10676 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010677
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -040010678 /* Determine the RSS table size based on the hardware capabilities */
Shannon Nelson272cdaf22016-02-17 16:12:21 -080010679 reg_val = i40e_read_rx_ctl(hw, I40E_PFQF_CTL_0);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -040010680 reg_val = (pf->rss_table_size == 512) ?
10681 (reg_val | I40E_PFQF_CTL_0_HASHLUTSIZE_512) :
10682 (reg_val & ~I40E_PFQF_CTL_0_HASHLUTSIZE_512);
Shannon Nelson272cdaf22016-02-17 16:12:21 -080010683 i40e_write_rx_ctl(hw, I40E_PFQF_CTL_0, reg_val);
Carolyn Wybornye157ea32014-06-03 23:50:22 +000010684
Helin Zhang28c58692015-10-26 19:44:27 -040010685 /* Determine the RSS size of the VSI */
Harshitha Ramamurthyf25571b2017-02-21 15:55:40 -080010686 if (!vsi->rss_size) {
10687 u16 qcount;
Mitch Williamsb356dac2017-12-18 05:15:25 -050010688 /* If the firmware does something weird during VSI init, we
10689 * could end up with zero TCs. Check for that to avoid
10690 * divide-by-zero. It probably won't pass traffic, but it also
10691 * won't panic.
10692 */
10693 qcount = vsi->num_queue_pairs /
10694 (vsi->tc_config.numtc ? vsi->tc_config.numtc : 1);
Harshitha Ramamurthyf25571b2017-02-21 15:55:40 -080010695 vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
10696 }
Mitch Williamsa4fa59c2016-09-12 14:18:43 -070010697 if (!vsi->rss_size)
10698 return -EINVAL;
Helin Zhang28c58692015-10-26 19:44:27 -040010699
Helin Zhange69ff812015-10-21 19:56:22 -040010700 lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
10701 if (!lut)
10702 return -ENOMEM;
10703
Helin Zhang28c58692015-10-26 19:44:27 -040010704 /* Use user configured lut if there is one, otherwise use default */
10705 if (vsi->rss_lut_user)
10706 memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
10707 else
10708 i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
Helin Zhange69ff812015-10-21 19:56:22 -040010709
Helin Zhang28c58692015-10-26 19:44:27 -040010710 /* Use user configured hash key if there is one, otherwise
10711 * use default.
10712 */
10713 if (vsi->rss_hkey_user)
10714 memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
10715 else
10716 netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
Helin Zhang043dd652015-10-21 19:56:23 -040010717 ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
Helin Zhange69ff812015-10-21 19:56:22 -040010718 kfree(lut);
10719
10720 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010721}
10722
10723/**
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +000010724 * i40e_reconfig_rss_queues - change number of queues for rss and rebuild
10725 * @pf: board private structure
10726 * @queue_count: the requested queue count for rss.
10727 *
10728 * returns 0 if rss is not enabled, if enabled returns the final rss queue
10729 * count which may be different from the requested queue count.
Maciej Sosin373149f2017-04-05 07:50:55 -040010730 * Note: expects to be called while under rtnl_lock()
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +000010731 **/
10732int i40e_reconfig_rss_queues(struct i40e_pf *pf, int queue_count)
10733{
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +000010734 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
10735 int new_rss_size;
10736
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +000010737 if (!(pf->flags & I40E_FLAG_RSS_ENABLED))
10738 return 0;
10739
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +000010740 new_rss_size = min_t(int, queue_count, pf->rss_size_max);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +000010741
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +000010742 if (queue_count != vsi->num_queue_pairs) {
Harshitha Ramamurthyf25571b2017-02-21 15:55:40 -080010743 u16 qcount;
10744
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +000010745 vsi->req_queue_pairs = queue_count;
Maciej Sosin373149f2017-04-05 07:50:55 -040010746 i40e_prep_for_reset(pf, true);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +000010747
Helin Zhangacd65442015-10-26 19:44:28 -040010748 pf->alloc_rss_size = new_rss_size;
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +000010749
Maciej Sosin373149f2017-04-05 07:50:55 -040010750 i40e_reset_and_rebuild(pf, true, true);
Helin Zhang28c58692015-10-26 19:44:27 -040010751
10752 /* Discard the user configured hash keys and lut, if less
10753 * queues are enabled.
10754 */
10755 if (queue_count < vsi->rss_size) {
10756 i40e_clear_rss_config_user(vsi);
10757 dev_dbg(&pf->pdev->dev,
10758 "discard user configured hash keys and lut\n");
10759 }
10760
10761 /* Reset vsi->rss_size, as number of enabled queues changed */
Harshitha Ramamurthyf25571b2017-02-21 15:55:40 -080010762 qcount = vsi->num_queue_pairs / vsi->tc_config.numtc;
10763 vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
Helin Zhang28c58692015-10-26 19:44:27 -040010764
Helin Zhang043dd652015-10-21 19:56:23 -040010765 i40e_pf_config_rss(pf);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +000010766 }
Lihong Yang12815052016-09-27 11:28:48 -070010767 dev_info(&pf->pdev->dev, "User requested queue count/HW max RSS count: %d/%d\n",
10768 vsi->req_queue_pairs, pf->rss_size_max);
Helin Zhangacd65442015-10-26 19:44:28 -040010769 return pf->alloc_rss_size;
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +000010770}
10771
10772/**
Shannon Nelson4fc8c672017-06-07 05:43:08 -040010773 * i40e_get_partition_bw_setting - Retrieve BW settings for this PF partition
Greg Rosef4492db2015-02-06 08:52:12 +000010774 * @pf: board private structure
10775 **/
Shannon Nelson4fc8c672017-06-07 05:43:08 -040010776i40e_status i40e_get_partition_bw_setting(struct i40e_pf *pf)
Greg Rosef4492db2015-02-06 08:52:12 +000010777{
10778 i40e_status status;
10779 bool min_valid, max_valid;
10780 u32 max_bw, min_bw;
10781
10782 status = i40e_read_bw_from_alt_ram(&pf->hw, &max_bw, &min_bw,
10783 &min_valid, &max_valid);
10784
10785 if (!status) {
10786 if (min_valid)
Shannon Nelson4fc8c672017-06-07 05:43:08 -040010787 pf->min_bw = min_bw;
Greg Rosef4492db2015-02-06 08:52:12 +000010788 if (max_valid)
Shannon Nelson4fc8c672017-06-07 05:43:08 -040010789 pf->max_bw = max_bw;
Greg Rosef4492db2015-02-06 08:52:12 +000010790 }
10791
10792 return status;
10793}
10794
10795/**
Shannon Nelson4fc8c672017-06-07 05:43:08 -040010796 * i40e_set_partition_bw_setting - Set BW settings for this PF partition
Greg Rosef4492db2015-02-06 08:52:12 +000010797 * @pf: board private structure
10798 **/
Shannon Nelson4fc8c672017-06-07 05:43:08 -040010799i40e_status i40e_set_partition_bw_setting(struct i40e_pf *pf)
Greg Rosef4492db2015-02-06 08:52:12 +000010800{
10801 struct i40e_aqc_configure_partition_bw_data bw_data;
10802 i40e_status status;
10803
Jeff Kirsherb40c82e62015-02-27 09:18:34 +000010804 /* Set the valid bit for this PF */
Jesse Brandeburg41a1d042015-06-04 16:24:02 -040010805 bw_data.pf_valid_bits = cpu_to_le16(BIT(pf->hw.pf_id));
Shannon Nelson4fc8c672017-06-07 05:43:08 -040010806 bw_data.max_bw[pf->hw.pf_id] = pf->max_bw & I40E_ALT_BW_VALUE_MASK;
10807 bw_data.min_bw[pf->hw.pf_id] = pf->min_bw & I40E_ALT_BW_VALUE_MASK;
Greg Rosef4492db2015-02-06 08:52:12 +000010808
10809 /* Set the new bandwidths */
10810 status = i40e_aq_configure_partition_bw(&pf->hw, &bw_data, NULL);
10811
10812 return status;
10813}
10814
10815/**
Shannon Nelson4fc8c672017-06-07 05:43:08 -040010816 * i40e_commit_partition_bw_setting - Commit BW settings for this PF partition
Greg Rosef4492db2015-02-06 08:52:12 +000010817 * @pf: board private structure
10818 **/
Shannon Nelson4fc8c672017-06-07 05:43:08 -040010819i40e_status i40e_commit_partition_bw_setting(struct i40e_pf *pf)
Greg Rosef4492db2015-02-06 08:52:12 +000010820{
10821 /* Commit temporary BW setting to permanent NVM image */
10822 enum i40e_admin_queue_err last_aq_status;
10823 i40e_status ret;
10824 u16 nvm_word;
10825
10826 if (pf->hw.partition_id != 1) {
10827 dev_info(&pf->pdev->dev,
10828 "Commit BW only works on partition 1! This is partition %d",
10829 pf->hw.partition_id);
10830 ret = I40E_NOT_SUPPORTED;
10831 goto bw_commit_out;
10832 }
10833
10834 /* Acquire NVM for read access */
10835 ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_READ);
10836 last_aq_status = pf->hw.aq.asq_last_status;
10837 if (ret) {
10838 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010839 "Cannot acquire NVM for read access, err %s aq_err %s\n",
10840 i40e_stat_str(&pf->hw, ret),
10841 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +000010842 goto bw_commit_out;
10843 }
10844
10845 /* Read word 0x10 of NVM - SW compatibility word 1 */
10846 ret = i40e_aq_read_nvm(&pf->hw,
10847 I40E_SR_NVM_CONTROL_WORD,
10848 0x10, sizeof(nvm_word), &nvm_word,
10849 false, NULL);
10850 /* Save off last admin queue command status before releasing
10851 * the NVM
10852 */
10853 last_aq_status = pf->hw.aq.asq_last_status;
10854 i40e_release_nvm(&pf->hw);
10855 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010856 dev_info(&pf->pdev->dev, "NVM read error, err %s aq_err %s\n",
10857 i40e_stat_str(&pf->hw, ret),
10858 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +000010859 goto bw_commit_out;
10860 }
10861
10862 /* Wait a bit for NVM release to complete */
10863 msleep(50);
10864
10865 /* Acquire NVM for write access */
10866 ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_WRITE);
10867 last_aq_status = pf->hw.aq.asq_last_status;
10868 if (ret) {
10869 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010870 "Cannot acquire NVM for write access, err %s aq_err %s\n",
10871 i40e_stat_str(&pf->hw, ret),
10872 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +000010873 goto bw_commit_out;
10874 }
10875 /* Write it back out unchanged to initiate update NVM,
10876 * which will force a write of the shadow (alt) RAM to
10877 * the NVM - thus storing the bandwidth values permanently.
10878 */
10879 ret = i40e_aq_update_nvm(&pf->hw,
10880 I40E_SR_NVM_CONTROL_WORD,
10881 0x10, sizeof(nvm_word),
Pawel Jablonskie3a5d6e2017-12-18 05:14:44 -050010882 &nvm_word, true, 0, NULL);
Greg Rosef4492db2015-02-06 08:52:12 +000010883 /* Save off last admin queue command status before releasing
10884 * the NVM
10885 */
10886 last_aq_status = pf->hw.aq.asq_last_status;
10887 i40e_release_nvm(&pf->hw);
10888 if (ret)
10889 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010890 "BW settings NOT SAVED, err %s aq_err %s\n",
10891 i40e_stat_str(&pf->hw, ret),
10892 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +000010893bw_commit_out:
10894
10895 return ret;
10896}
10897
10898/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010899 * i40e_sw_init - Initialize general software structures (struct i40e_pf)
10900 * @pf: board private structure to initialize
10901 *
10902 * i40e_sw_init initializes the Adapter private data structure.
10903 * Fields are initialized based on PCI device information and
10904 * OS network device settings (MTU size).
10905 **/
10906static int i40e_sw_init(struct i40e_pf *pf)
10907{
10908 int err = 0;
10909 int size;
10910
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010911 /* Set default capability flags */
10912 pf->flags = I40E_FLAG_RX_CSUM_ENABLED |
10913 I40E_FLAG_MSI_ENABLED |
Mitch Williams2bc7ee82015-02-06 08:52:11 +000010914 I40E_FLAG_MSIX_ENABLED;
10915
Mitch Williamsca99eb92014-04-04 04:43:07 +000010916 /* Set default ITR */
Jacob Keller42702552017-09-07 08:05:48 -040010917 pf->rx_itr_default = I40E_ITR_RX_DEF;
10918 pf->tx_itr_default = I40E_ITR_TX_DEF;
Mitch Williamsca99eb92014-04-04 04:43:07 +000010919
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +000010920 /* Depending on PF configurations, it is possible that the RSS
10921 * maximum might end up larger than the available queues
10922 */
Jesse Brandeburg41a1d042015-06-04 16:24:02 -040010923 pf->rss_size_max = BIT(pf->hw.func_caps.rss_table_entry_width);
Helin Zhangacd65442015-10-26 19:44:28 -040010924 pf->alloc_rss_size = 1;
Anjali Singhai Jain5db4cb52015-02-24 06:58:49 +000010925 pf->rss_table_size = pf->hw.func_caps.rss_table_size;
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +000010926 pf->rss_size_max = min_t(int, pf->rss_size_max,
10927 pf->hw.func_caps.num_tx_qp);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010928 if (pf->hw.func_caps.rss) {
10929 pf->flags |= I40E_FLAG_RSS_ENABLED;
Helin Zhangacd65442015-10-26 19:44:28 -040010930 pf->alloc_rss_size = min_t(int, pf->rss_size_max,
10931 num_online_cpus());
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010932 }
10933
Catherine Sullivan2050bc62013-12-18 13:46:03 +000010934 /* MFP mode enabled */
Pawel Orlowskic78b9532015-04-22 19:34:06 -040010935 if (pf->hw.func_caps.npar_enable || pf->hw.func_caps.flex10_enable) {
Catherine Sullivan2050bc62013-12-18 13:46:03 +000010936 pf->flags |= I40E_FLAG_MFP_ENABLED;
10937 dev_info(&pf->pdev->dev, "MFP mode Enabled\n");
Shannon Nelson4fc8c672017-06-07 05:43:08 -040010938 if (i40e_get_partition_bw_setting(pf)) {
Greg Rosef4492db2015-02-06 08:52:12 +000010939 dev_warn(&pf->pdev->dev,
Shannon Nelson4fc8c672017-06-07 05:43:08 -040010940 "Could not get partition bw settings\n");
10941 } else {
Greg Rosef4492db2015-02-06 08:52:12 +000010942 dev_info(&pf->pdev->dev,
Shannon Nelson4fc8c672017-06-07 05:43:08 -040010943 "Partition BW Min = %8.8x, Max = %8.8x\n",
10944 pf->min_bw, pf->max_bw);
10945
10946 /* nudge the Tx scheduler */
10947 i40e_set_partition_bw_setting(pf);
10948 }
Catherine Sullivan2050bc62013-12-18 13:46:03 +000010949 }
10950
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010951 if ((pf->hw.func_caps.fd_filters_guaranteed > 0) ||
10952 (pf->hw.func_caps.fd_filters_best_effort > 0)) {
10953 pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
10954 pf->atr_sample_rate = I40E_DEFAULT_ATR_SAMPLE_RATE;
Shannon Nelson6eae9c62015-09-03 17:18:55 -040010955 if (pf->flags & I40E_FLAG_MFP_ENABLED &&
10956 pf->hw.num_partitions > 1)
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010957 dev_info(&pf->pdev->dev,
Anjali Singhai Jain0b675842014-03-06 08:59:51 +000010958 "Flow Director Sideband mode Disabled in MFP mode\n");
Shannon Nelson6eae9c62015-09-03 17:18:55 -040010959 else
10960 pf->flags |= I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010961 pf->fdir_pf_filter_count =
10962 pf->hw.func_caps.fd_filters_guaranteed;
10963 pf->hw.fdir_shared_filter_count =
10964 pf->hw.func_caps.fd_filters_best_effort;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010965 }
10966
Anjali Singhai Jain5a433192017-06-23 04:24:45 -040010967 if (pf->hw.mac.type == I40E_MAC_X722) {
Jacob Kellerd36e41d2017-06-23 04:24:46 -040010968 pf->hw_features |= (I40E_HW_RSS_AQ_CAPABLE |
10969 I40E_HW_128_QP_RSS_CAPABLE |
10970 I40E_HW_ATR_EVICT_CAPABLE |
10971 I40E_HW_WB_ON_ITR_CAPABLE |
10972 I40E_HW_MULTIPLE_TCP_UDP_RSS_PCTYPE |
10973 I40E_HW_NO_PCI_LINK_CHECK |
10974 I40E_HW_USE_SET_LLDP_MIB |
10975 I40E_HW_GENEVE_OFFLOAD_CAPABLE |
10976 I40E_HW_PTP_L4_CAPABLE |
10977 I40E_HW_WOL_MC_MAGIC_PKT_WAKE |
10978 I40E_HW_OUTER_UDP_CSUM_CAPABLE);
Anjali Singhai Jain10a955f2017-06-23 04:24:48 -040010979
10980#define I40E_FDEVICT_PCTYPE_DEFAULT 0xc03
10981 if (rd32(&pf->hw, I40E_GLQF_FDEVICTENA(1)) !=
10982 I40E_FDEVICT_PCTYPE_DEFAULT) {
10983 dev_warn(&pf->pdev->dev,
10984 "FD EVICT PCTYPES are not right, disable FD HW EVICT\n");
10985 pf->hw_features &= ~I40E_HW_ATR_EVICT_CAPABLE;
10986 }
Anjali Singhai Jain5a433192017-06-23 04:24:45 -040010987 } else if ((pf->hw.aq.api_maj_ver > 1) ||
10988 ((pf->hw.aq.api_maj_ver == 1) &&
10989 (pf->hw.aq.api_min_ver > 4))) {
10990 /* Supported in FW API version higher than 1.4 */
Jacob Kellerd36e41d2017-06-23 04:24:46 -040010991 pf->hw_features |= I40E_HW_GENEVE_OFFLOAD_CAPABLE;
Anjali Singhai Jain5a433192017-06-23 04:24:45 -040010992 }
10993
10994 /* Enable HW ATR eviction if possible */
Jacob Kellerd36e41d2017-06-23 04:24:46 -040010995 if (pf->hw_features & I40E_HW_ATR_EVICT_CAPABLE)
Anjali Singhai Jain5a433192017-06-23 04:24:45 -040010996 pf->flags |= I40E_FLAG_HW_ATR_EVICT_ENABLED;
10997
Bimmy Pujari6de432c2016-11-11 12:39:38 -080010998 if ((pf->hw.mac.type == I40E_MAC_XL710) &&
Anjali Singhai Jain8eed76f2015-12-09 15:50:31 -080010999 (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 33)) ||
Neerav Parikhf1bbad32016-01-13 16:51:39 -080011000 (pf->hw.aq.fw_maj_ver < 4))) {
Jacob Kellerd36e41d2017-06-23 04:24:46 -040011001 pf->hw_features |= I40E_HW_RESTART_AUTONEG;
Neerav Parikhf1bbad32016-01-13 16:51:39 -080011002 /* No DCB support for FW < v4.33 */
Jacob Kellerd36e41d2017-06-23 04:24:46 -040011003 pf->hw_features |= I40E_HW_NO_DCB_SUPPORT;
Neerav Parikhf1bbad32016-01-13 16:51:39 -080011004 }
11005
11006 /* Disable FW LLDP if FW < v4.3 */
Bimmy Pujari6de432c2016-11-11 12:39:38 -080011007 if ((pf->hw.mac.type == I40E_MAC_XL710) &&
Neerav Parikhf1bbad32016-01-13 16:51:39 -080011008 (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 3)) ||
11009 (pf->hw.aq.fw_maj_ver < 4)))
Jacob Kellerd36e41d2017-06-23 04:24:46 -040011010 pf->hw_features |= I40E_HW_STOP_FW_LLDP;
Neerav Parikhf1bbad32016-01-13 16:51:39 -080011011
11012 /* Use the FW Set LLDP MIB API if FW > v4.40 */
Bimmy Pujari6de432c2016-11-11 12:39:38 -080011013 if ((pf->hw.mac.type == I40E_MAC_XL710) &&
Neerav Parikhf1bbad32016-01-13 16:51:39 -080011014 (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver >= 40)) ||
11015 (pf->hw.aq.fw_maj_ver >= 5)))
Jacob Kellerd36e41d2017-06-23 04:24:46 -040011016 pf->hw_features |= I40E_HW_USE_SET_LLDP_MIB;
Anjali Singhai Jain8eed76f2015-12-09 15:50:31 -080011017
Alan Bradyc3d26b72017-08-29 05:32:32 -040011018 /* Enable PTP L4 if FW > v6.0 */
11019 if (pf->hw.mac.type == I40E_MAC_XL710 &&
11020 pf->hw.aq.fw_maj_ver >= 6)
11021 pf->hw_features |= I40E_HW_PTP_L4_CAPABLE;
11022
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011023 if (pf->hw.func_caps.vmdq) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011024 pf->num_vmdq_vsis = I40E_DEFAULT_NUM_VMDQ_VSI;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -040011025 pf->flags |= I40E_FLAG_VMDQ_ENABLED;
Jesse Brandeburge9e53662015-10-02 17:57:21 -070011026 pf->num_vmdq_qps = i40e_default_queues_per_vmdq(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011027 }
11028
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060011029 if (pf->hw.func_caps.iwarp) {
11030 pf->flags |= I40E_FLAG_IWARP_ENABLED;
11031 /* IWARP needs one extra vector for CQP just like MISC.*/
11032 pf->num_iwarp_msix = (int)num_online_cpus() + 1;
11033 }
11034
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011035#ifdef CONFIG_PCI_IOV
Shannon Nelsonba252f132014-12-11 07:06:34 +000011036 if (pf->hw.func_caps.num_vfs && pf->hw.partition_id == 1) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011037 pf->num_vf_qps = I40E_DEFAULT_QUEUES_PER_VF;
11038 pf->flags |= I40E_FLAG_SRIOV_ENABLED;
11039 pf->num_req_vfs = min_t(int,
11040 pf->hw.func_caps.num_vfs,
11041 I40E_MAX_VF_COUNT);
11042 }
11043#endif /* CONFIG_PCI_IOV */
11044 pf->eeprom_version = 0xDEAD;
11045 pf->lan_veb = I40E_NO_VEB;
11046 pf->lan_vsi = I40E_NO_VSI;
11047
Anjali Singhai Jaind1a8d272015-07-23 16:54:40 -040011048 /* By default FW has this off for performance reasons */
11049 pf->flags &= ~I40E_FLAG_VEB_STATS_ENABLED;
11050
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011051 /* set up queue assignment tracking */
11052 size = sizeof(struct i40e_lump_tracking)
11053 + (sizeof(u16) * pf->hw.func_caps.num_tx_qp);
11054 pf->qp_pile = kzalloc(size, GFP_KERNEL);
11055 if (!pf->qp_pile) {
11056 err = -ENOMEM;
11057 goto sw_init_done;
11058 }
11059 pf->qp_pile->num_entries = pf->hw.func_caps.num_tx_qp;
11060 pf->qp_pile->search_hint = 0;
11061
Anjali Singhai Jain327fe042014-06-04 01:23:26 +000011062 pf->tx_timeout_recovery_level = 1;
11063
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011064 mutex_init(&pf->switch_mutex);
11065
11066sw_init_done:
11067 return err;
11068}
11069
11070/**
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +000011071 * i40e_set_ntuple - set the ntuple feature flag and take action
11072 * @pf: board private structure to initialize
11073 * @features: the feature set that the stack is suggesting
11074 *
11075 * returns a bool to indicate if reset needs to happen
11076 **/
11077bool i40e_set_ntuple(struct i40e_pf *pf, netdev_features_t features)
11078{
11079 bool need_reset = false;
11080
11081 /* Check if Flow Director n-tuple support was enabled or disabled. If
11082 * the state changed, we need to reset.
11083 */
11084 if (features & NETIF_F_NTUPLE) {
11085 /* Enable filters and mark for reset */
11086 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
11087 need_reset = true;
Amritha Nambiar2f4b4112017-10-27 02:36:01 -070011088 /* enable FD_SB only if there is MSI-X vector and no cloud
11089 * filters exist
11090 */
11091 if (pf->num_fdsb_msix > 0 && !pf->num_cloud_filters) {
Tushar Davea70e4072016-05-16 12:40:53 -070011092 pf->flags |= I40E_FLAG_FD_SB_ENABLED;
Amritha Nambiar2f4b4112017-10-27 02:36:01 -070011093 pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
11094 }
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +000011095 } else {
11096 /* turn off filters, mark for reset and clear SW filter list */
11097 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
11098 need_reset = true;
11099 i40e_fdir_filter_exit(pf);
11100 }
Jacob Keller47994c12017-04-19 09:25:57 -040011101 pf->flags &= ~(I40E_FLAG_FD_SB_ENABLED |
11102 I40E_FLAG_FD_SB_AUTO_DISABLED);
Amritha Nambiar2f4b4112017-10-27 02:36:01 -070011103 pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
11104
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +000011105 /* reset fd counters */
Jacob Keller097dbf52017-02-06 14:38:46 -080011106 pf->fd_add_err = 0;
11107 pf->fd_atr_cnt = 0;
Anjali Singhai Jain8a4f34f2014-06-04 08:45:20 +000011108 /* if ATR was auto disabled it can be re-enabled. */
Jacob Keller47994c12017-04-19 09:25:57 -040011109 if (pf->flags & I40E_FLAG_FD_ATR_AUTO_DISABLED) {
11110 pf->flags &= ~I40E_FLAG_FD_ATR_AUTO_DISABLED;
11111 if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
11112 (I40E_DEBUG_FD & pf->hw.debug_mask))
Jacob Keller234dc4e2016-09-06 18:05:09 -070011113 dev_info(&pf->pdev->dev, "ATR re-enabled.\n");
11114 }
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +000011115 }
11116 return need_reset;
11117}
11118
11119/**
Alan Bradyd8ec9862016-07-27 12:02:38 -070011120 * i40e_clear_rss_lut - clear the rx hash lookup table
11121 * @vsi: the VSI being configured
11122 **/
11123static void i40e_clear_rss_lut(struct i40e_vsi *vsi)
11124{
11125 struct i40e_pf *pf = vsi->back;
11126 struct i40e_hw *hw = &pf->hw;
11127 u16 vf_id = vsi->vf_id;
11128 u8 i;
11129
11130 if (vsi->type == I40E_VSI_MAIN) {
11131 for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
11132 wr32(hw, I40E_PFQF_HLUT(i), 0);
11133 } else if (vsi->type == I40E_VSI_SRIOV) {
11134 for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
11135 i40e_write_rx_ctl(hw, I40E_VFQF_HLUT1(i, vf_id), 0);
11136 } else {
11137 dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
11138 }
11139}
11140
11141/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011142 * i40e_set_features - set the netdev feature flags
11143 * @netdev: ptr to the netdev being adjusted
11144 * @features: the feature set that the stack is suggesting
Maciej Sosin373149f2017-04-05 07:50:55 -040011145 * Note: expects to be called while under rtnl_lock()
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011146 **/
11147static int i40e_set_features(struct net_device *netdev,
11148 netdev_features_t features)
11149{
11150 struct i40e_netdev_priv *np = netdev_priv(netdev);
11151 struct i40e_vsi *vsi = np->vsi;
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +000011152 struct i40e_pf *pf = vsi->back;
11153 bool need_reset;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011154
Alan Bradyd8ec9862016-07-27 12:02:38 -070011155 if (features & NETIF_F_RXHASH && !(netdev->features & NETIF_F_RXHASH))
11156 i40e_pf_config_rss(pf);
11157 else if (!(features & NETIF_F_RXHASH) &&
11158 netdev->features & NETIF_F_RXHASH)
11159 i40e_clear_rss_lut(vsi);
11160
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011161 if (features & NETIF_F_HW_VLAN_CTAG_RX)
11162 i40e_vlan_stripping_enable(vsi);
11163 else
11164 i40e_vlan_stripping_disable(vsi);
11165
Amritha Nambiar2f4b4112017-10-27 02:36:01 -070011166 if (!(features & NETIF_F_HW_TC) && pf->num_cloud_filters) {
11167 dev_err(&pf->pdev->dev,
11168 "Offloaded tc filters active, can't turn hw_tc_offload off");
11169 return -EINVAL;
11170 }
11171
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +000011172 need_reset = i40e_set_ntuple(pf, features);
11173
11174 if (need_reset)
Amritha Nambiarff424182017-09-07 04:00:11 -070011175 i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +000011176
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011177 return 0;
11178}
11179
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +000011180/**
Singhai, Anjali6a899022015-12-14 12:21:18 -080011181 * i40e_get_udp_port_idx - Lookup a possibly offloaded for Rx UDP port
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +000011182 * @pf: board private structure
11183 * @port: The UDP port to look up
11184 *
11185 * Returns the index number or I40E_MAX_PF_UDP_OFFLOAD_PORTS if port not found
11186 **/
Jacob Kellerfe0b0cd2017-02-06 14:38:38 -080011187static u8 i40e_get_udp_port_idx(struct i40e_pf *pf, u16 port)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +000011188{
11189 u8 i;
11190
11191 for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
Jacob Keller27826fd2017-04-19 09:25:50 -040011192 if (pf->udp_ports[i].port == port)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +000011193 return i;
11194 }
11195
11196 return i;
11197}
11198
11199/**
Alexander Duyck06a5f7f2016-06-16 12:22:06 -070011200 * i40e_udp_tunnel_add - Get notifications about UDP tunnel ports that come up
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +000011201 * @netdev: This physical port's netdev
Alexander Duyck06a5f7f2016-06-16 12:22:06 -070011202 * @ti: Tunnel endpoint information
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +000011203 **/
Alexander Duyck06a5f7f2016-06-16 12:22:06 -070011204static void i40e_udp_tunnel_add(struct net_device *netdev,
11205 struct udp_tunnel_info *ti)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +000011206{
11207 struct i40e_netdev_priv *np = netdev_priv(netdev);
11208 struct i40e_vsi *vsi = np->vsi;
11209 struct i40e_pf *pf = vsi->back;
Jacob Kellerfe0b0cd2017-02-06 14:38:38 -080011210 u16 port = ntohs(ti->port);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +000011211 u8 next_idx;
11212 u8 idx;
11213
Singhai, Anjali6a899022015-12-14 12:21:18 -080011214 idx = i40e_get_udp_port_idx(pf, port);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +000011215
11216 /* Check if port already exists */
11217 if (idx < I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
Jacob Kellerfe0b0cd2017-02-06 14:38:38 -080011218 netdev_info(netdev, "port %d already offloaded\n", port);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +000011219 return;
11220 }
11221
11222 /* Now check if there is space to add the new port */
Singhai, Anjali6a899022015-12-14 12:21:18 -080011223 next_idx = i40e_get_udp_port_idx(pf, 0);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +000011224
11225 if (next_idx == I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
Alexander Duyck06a5f7f2016-06-16 12:22:06 -070011226 netdev_info(netdev, "maximum number of offloaded UDP ports reached, not adding port %d\n",
Jacob Kellerfe0b0cd2017-02-06 14:38:38 -080011227 port);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +000011228 return;
11229 }
11230
Alexander Duyck06a5f7f2016-06-16 12:22:06 -070011231 switch (ti->type) {
11232 case UDP_TUNNEL_TYPE_VXLAN:
11233 pf->udp_ports[next_idx].type = I40E_AQC_TUNNEL_TYPE_VXLAN;
11234 break;
11235 case UDP_TUNNEL_TYPE_GENEVE:
Jacob Kellerd36e41d2017-06-23 04:24:46 -040011236 if (!(pf->hw_features & I40E_HW_GENEVE_OFFLOAD_CAPABLE))
Alexander Duyck06a5f7f2016-06-16 12:22:06 -070011237 return;
11238 pf->udp_ports[next_idx].type = I40E_AQC_TUNNEL_TYPE_NGE;
11239 break;
11240 default:
11241 return;
11242 }
11243
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +000011244 /* New port: add it and mark its index in the bitmap */
Jacob Keller27826fd2017-04-19 09:25:50 -040011245 pf->udp_ports[next_idx].port = port;
Singhai, Anjali6a899022015-12-14 12:21:18 -080011246 pf->pending_udp_bitmap |= BIT_ULL(next_idx);
11247 pf->flags |= I40E_FLAG_UDP_FILTER_SYNC;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +000011248}
11249
11250/**
Alexander Duyck06a5f7f2016-06-16 12:22:06 -070011251 * i40e_udp_tunnel_del - Get notifications about UDP tunnel ports that go away
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +000011252 * @netdev: This physical port's netdev
Alexander Duyck06a5f7f2016-06-16 12:22:06 -070011253 * @ti: Tunnel endpoint information
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +000011254 **/
Alexander Duyck06a5f7f2016-06-16 12:22:06 -070011255static void i40e_udp_tunnel_del(struct net_device *netdev,
11256 struct udp_tunnel_info *ti)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +000011257{
11258 struct i40e_netdev_priv *np = netdev_priv(netdev);
11259 struct i40e_vsi *vsi = np->vsi;
11260 struct i40e_pf *pf = vsi->back;
Jacob Kellerfe0b0cd2017-02-06 14:38:38 -080011261 u16 port = ntohs(ti->port);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +000011262 u8 idx;
11263
Singhai, Anjali6a899022015-12-14 12:21:18 -080011264 idx = i40e_get_udp_port_idx(pf, port);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +000011265
11266 /* Check if port already exists */
Alexander Duyck06a5f7f2016-06-16 12:22:06 -070011267 if (idx >= I40E_MAX_PF_UDP_OFFLOAD_PORTS)
11268 goto not_found;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +000011269
Alexander Duyck06a5f7f2016-06-16 12:22:06 -070011270 switch (ti->type) {
11271 case UDP_TUNNEL_TYPE_VXLAN:
11272 if (pf->udp_ports[idx].type != I40E_AQC_TUNNEL_TYPE_VXLAN)
11273 goto not_found;
11274 break;
11275 case UDP_TUNNEL_TYPE_GENEVE:
11276 if (pf->udp_ports[idx].type != I40E_AQC_TUNNEL_TYPE_NGE)
11277 goto not_found;
11278 break;
11279 default:
11280 goto not_found;
Singhai, Anjali6a899022015-12-14 12:21:18 -080011281 }
11282
Alexander Duyck06a5f7f2016-06-16 12:22:06 -070011283 /* if port exists, set it to 0 (mark for deletion)
11284 * and make it pending
11285 */
Jacob Keller27826fd2017-04-19 09:25:50 -040011286 pf->udp_ports[idx].port = 0;
Alexander Duyck06a5f7f2016-06-16 12:22:06 -070011287 pf->pending_udp_bitmap |= BIT_ULL(idx);
Singhai, Anjali6a899022015-12-14 12:21:18 -080011288 pf->flags |= I40E_FLAG_UDP_FILTER_SYNC;
11289
Alexander Duyck06a5f7f2016-06-16 12:22:06 -070011290 return;
11291not_found:
11292 netdev_warn(netdev, "UDP port %d was not found, not deleting\n",
Jacob Kellerfe0b0cd2017-02-06 14:38:38 -080011293 port);
Singhai, Anjali6a899022015-12-14 12:21:18 -080011294}
11295
Neerav Parikh1f224ad2014-02-12 01:45:31 +000011296static int i40e_get_phys_port_id(struct net_device *netdev,
Jiri Pirko02637fc2014-11-28 14:34:16 +010011297 struct netdev_phys_item_id *ppid)
Neerav Parikh1f224ad2014-02-12 01:45:31 +000011298{
11299 struct i40e_netdev_priv *np = netdev_priv(netdev);
11300 struct i40e_pf *pf = np->vsi->back;
11301 struct i40e_hw *hw = &pf->hw;
11302
Jacob Kellerd36e41d2017-06-23 04:24:46 -040011303 if (!(pf->hw_features & I40E_HW_PORT_ID_VALID))
Neerav Parikh1f224ad2014-02-12 01:45:31 +000011304 return -EOPNOTSUPP;
11305
11306 ppid->id_len = min_t(int, sizeof(hw->mac.port_addr), sizeof(ppid->id));
11307 memcpy(ppid->id, hw->mac.port_addr, ppid->id_len);
11308
11309 return 0;
11310}
11311
Jesse Brandeburg2f90ade2014-11-20 16:30:02 -080011312/**
11313 * i40e_ndo_fdb_add - add an entry to the hardware database
11314 * @ndm: the input from the stack
11315 * @tb: pointer to array of nladdr (unused)
11316 * @dev: the net device pointer
11317 * @addr: the MAC address entry being added
11318 * @flags: instructions from stack about fdb operation
11319 */
Greg Rose4ba0dea2014-03-06 08:59:55 +000011320static int i40e_ndo_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
11321 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +010011322 const unsigned char *addr, u16 vid,
Greg Rose4ba0dea2014-03-06 08:59:55 +000011323 u16 flags)
Greg Rose4ba0dea2014-03-06 08:59:55 +000011324{
11325 struct i40e_netdev_priv *np = netdev_priv(dev);
11326 struct i40e_pf *pf = np->vsi->back;
11327 int err = 0;
11328
11329 if (!(pf->flags & I40E_FLAG_SRIOV_ENABLED))
11330 return -EOPNOTSUPP;
11331
Or Gerlitz65891fe2014-12-14 18:19:05 +020011332 if (vid) {
11333 pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
11334 return -EINVAL;
11335 }
11336
Greg Rose4ba0dea2014-03-06 08:59:55 +000011337 /* Hardware does not support aging addresses so if a
11338 * ndm_state is given only allow permanent addresses
11339 */
11340 if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
11341 netdev_info(dev, "FDB only supports static addresses\n");
11342 return -EINVAL;
11343 }
11344
11345 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
11346 err = dev_uc_add_excl(dev, addr);
11347 else if (is_multicast_ether_addr(addr))
11348 err = dev_mc_add_excl(dev, addr);
11349 else
11350 err = -EINVAL;
11351
11352 /* Only return duplicate errors if NLM_F_EXCL is set */
11353 if (err == -EEXIST && !(flags & NLM_F_EXCL))
11354 err = 0;
11355
11356 return err;
11357}
11358
Neerav Parikh51616012015-02-06 08:52:14 +000011359/**
11360 * i40e_ndo_bridge_setlink - Set the hardware bridge mode
11361 * @dev: the netdev being configured
11362 * @nlh: RTNL message
11363 *
11364 * Inserts a new hardware bridge if not already created and
11365 * enables the bridging mode requested (VEB or VEPA). If the
11366 * hardware bridge has already been inserted and the request
11367 * is to change the mode then that requires a PF reset to
11368 * allow rebuild of the components with required hardware
11369 * bridge mode enabled.
Maciej Sosin373149f2017-04-05 07:50:55 -040011370 *
11371 * Note: expects to be called while under rtnl_lock()
Neerav Parikh51616012015-02-06 08:52:14 +000011372 **/
11373static int i40e_ndo_bridge_setlink(struct net_device *dev,
Carolyn Wyborny9df70b62015-04-27 14:57:11 -040011374 struct nlmsghdr *nlh,
11375 u16 flags)
Neerav Parikh51616012015-02-06 08:52:14 +000011376{
11377 struct i40e_netdev_priv *np = netdev_priv(dev);
11378 struct i40e_vsi *vsi = np->vsi;
11379 struct i40e_pf *pf = vsi->back;
11380 struct i40e_veb *veb = NULL;
11381 struct nlattr *attr, *br_spec;
11382 int i, rem;
11383
11384 /* Only for PF VSI for now */
11385 if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
11386 return -EOPNOTSUPP;
11387
11388 /* Find the HW bridge for PF VSI */
11389 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
11390 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
11391 veb = pf->veb[i];
11392 }
11393
11394 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
11395
11396 nla_for_each_nested(attr, br_spec, rem) {
11397 __u16 mode;
11398
11399 if (nla_type(attr) != IFLA_BRIDGE_MODE)
11400 continue;
11401
11402 mode = nla_get_u16(attr);
11403 if ((mode != BRIDGE_MODE_VEPA) &&
11404 (mode != BRIDGE_MODE_VEB))
11405 return -EINVAL;
11406
11407 /* Insert a new HW bridge */
11408 if (!veb) {
11409 veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
11410 vsi->tc_config.enabled_tc);
11411 if (veb) {
11412 veb->bridge_mode = mode;
11413 i40e_config_bridge_mode(veb);
11414 } else {
11415 /* No Bridge HW offload available */
11416 return -ENOENT;
11417 }
11418 break;
11419 } else if (mode != veb->bridge_mode) {
11420 /* Existing HW bridge but different mode needs reset */
11421 veb->bridge_mode = mode;
Anjali Singhai Jainfc608612015-05-08 15:35:57 -070011422 /* TODO: If no VFs or VMDq VSIs, disallow VEB mode */
11423 if (mode == BRIDGE_MODE_VEB)
11424 pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
11425 else
11426 pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
Amritha Nambiarff424182017-09-07 04:00:11 -070011427 i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
Neerav Parikh51616012015-02-06 08:52:14 +000011428 break;
11429 }
11430 }
11431
11432 return 0;
11433}
11434
11435/**
11436 * i40e_ndo_bridge_getlink - Get the hardware bridge mode
11437 * @skb: skb buff
11438 * @pid: process id
11439 * @seq: RTNL message seq #
11440 * @dev: the netdev being configured
11441 * @filter_mask: unused
Jesse Brandeburgd4b2f9f2015-09-03 17:18:48 -040011442 * @nlflags: netlink flags passed in
Neerav Parikh51616012015-02-06 08:52:14 +000011443 *
11444 * Return the mode in which the hardware bridge is operating in
11445 * i.e VEB or VEPA.
11446 **/
Neerav Parikh51616012015-02-06 08:52:14 +000011447static int i40e_ndo_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
11448 struct net_device *dev,
Carolyn Wyborny9f4ffc42015-08-31 19:54:42 -040011449 u32 __always_unused filter_mask,
11450 int nlflags)
Neerav Parikh51616012015-02-06 08:52:14 +000011451{
11452 struct i40e_netdev_priv *np = netdev_priv(dev);
11453 struct i40e_vsi *vsi = np->vsi;
11454 struct i40e_pf *pf = vsi->back;
11455 struct i40e_veb *veb = NULL;
11456 int i;
11457
11458 /* Only for PF VSI for now */
11459 if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
11460 return -EOPNOTSUPP;
11461
11462 /* Find the HW bridge for the PF VSI */
11463 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
11464 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
11465 veb = pf->veb[i];
11466 }
11467
11468 if (!veb)
11469 return 0;
11470
Nicolas Dichtel46c264d2015-04-28 18:33:49 +020011471 return ndo_dflt_bridge_getlink(skb, pid, seq, dev, veb->bridge_mode,
Huaibin Wang599b0762016-09-26 09:51:18 +020011472 0, 0, nlflags, filter_mask, NULL);
Neerav Parikh51616012015-02-06 08:52:14 +000011473}
Neerav Parikh51616012015-02-06 08:52:14 +000011474
Joe Stringerf44a75e2015-04-14 17:09:14 -070011475/**
11476 * i40e_features_check - Validate encapsulated packet conforms to limits
11477 * @skb: skb buff
Jean Sacren2bc11c62015-09-19 05:08:43 -060011478 * @dev: This physical port's netdev
Joe Stringerf44a75e2015-04-14 17:09:14 -070011479 * @features: Offload features that the stack believes apply
11480 **/
11481static netdev_features_t i40e_features_check(struct sk_buff *skb,
11482 struct net_device *dev,
11483 netdev_features_t features)
11484{
Alexander Duyckf114dca2016-10-25 16:08:46 -070011485 size_t len;
11486
11487 /* No point in doing any of this if neither checksum nor GSO are
11488 * being requested for this frame. We can rule out both by just
11489 * checking for CHECKSUM_PARTIAL
11490 */
11491 if (skb->ip_summed != CHECKSUM_PARTIAL)
11492 return features;
11493
11494 /* We cannot support GSO if the MSS is going to be less than
11495 * 64 bytes. If it is then we need to drop support for GSO.
11496 */
11497 if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
11498 features &= ~NETIF_F_GSO_MASK;
11499
11500 /* MACLEN can support at most 63 words */
11501 len = skb_network_header(skb) - skb->data;
11502 if (len & ~(63 * 2))
11503 goto out_err;
11504
11505 /* IPLEN and EIPLEN can support at most 127 dwords */
11506 len = skb_transport_header(skb) - skb_network_header(skb);
11507 if (len & ~(127 * 4))
11508 goto out_err;
11509
11510 if (skb->encapsulation) {
11511 /* L4TUNLEN can support 127 words */
11512 len = skb_inner_network_header(skb) - skb_transport_header(skb);
11513 if (len & ~(127 * 2))
11514 goto out_err;
11515
11516 /* IPLEN can support at most 127 dwords */
11517 len = skb_inner_transport_header(skb) -
11518 skb_inner_network_header(skb);
11519 if (len & ~(127 * 4))
11520 goto out_err;
11521 }
11522
11523 /* No need to validate L4LEN as TCP is the only protocol with a
11524 * a flexible value and we support all possible values supported
11525 * by TCP, which is at most 15 dwords
11526 */
Joe Stringerf44a75e2015-04-14 17:09:14 -070011527
11528 return features;
Alexander Duyckf114dca2016-10-25 16:08:46 -070011529out_err:
11530 return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
Joe Stringerf44a75e2015-04-14 17:09:14 -070011531}
11532
Björn Töpel0c8493d2017-05-24 07:55:34 +020011533/**
11534 * i40e_xdp_setup - add/remove an XDP program
11535 * @vsi: VSI to changed
11536 * @prog: XDP program
11537 **/
11538static int i40e_xdp_setup(struct i40e_vsi *vsi,
11539 struct bpf_prog *prog)
11540{
11541 int frame_size = vsi->netdev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
11542 struct i40e_pf *pf = vsi->back;
11543 struct bpf_prog *old_prog;
11544 bool need_reset;
11545 int i;
11546
11547 /* Don't allow frames that span over multiple buffers */
11548 if (frame_size > vsi->rx_buf_len)
11549 return -EINVAL;
11550
11551 if (!i40e_enabled_xdp_vsi(vsi) && !prog)
11552 return 0;
11553
11554 /* When turning XDP on->off/off->on we reset and rebuild the rings. */
11555 need_reset = (i40e_enabled_xdp_vsi(vsi) != !!prog);
11556
11557 if (need_reset)
11558 i40e_prep_for_reset(pf, true);
11559
11560 old_prog = xchg(&vsi->xdp_prog, prog);
11561
11562 if (need_reset)
11563 i40e_reset_and_rebuild(pf, true, true);
11564
11565 for (i = 0; i < vsi->num_queue_pairs; i++)
11566 WRITE_ONCE(vsi->rx_rings[i]->xdp_prog, vsi->xdp_prog);
11567
11568 if (old_prog)
11569 bpf_prog_put(old_prog);
11570
11571 return 0;
11572}
11573
11574/**
Jakub Kicinskif4e63522017-11-03 13:56:16 -070011575 * i40e_xdp - implements ndo_bpf for i40e
Björn Töpel0c8493d2017-05-24 07:55:34 +020011576 * @dev: netdevice
11577 * @xdp: XDP command
11578 **/
11579static int i40e_xdp(struct net_device *dev,
Jakub Kicinskif4e63522017-11-03 13:56:16 -070011580 struct netdev_bpf *xdp)
Björn Töpel0c8493d2017-05-24 07:55:34 +020011581{
11582 struct i40e_netdev_priv *np = netdev_priv(dev);
11583 struct i40e_vsi *vsi = np->vsi;
11584
11585 if (vsi->type != I40E_VSI_MAIN)
11586 return -EINVAL;
11587
11588 switch (xdp->command) {
11589 case XDP_SETUP_PROG:
11590 return i40e_xdp_setup(vsi, xdp->prog);
11591 case XDP_QUERY_PROG:
11592 xdp->prog_attached = i40e_enabled_xdp_vsi(vsi);
Daniel Borkmanneb230392017-06-24 21:13:52 +020011593 xdp->prog_id = vsi->xdp_prog ? vsi->xdp_prog->aux->id : 0;
Björn Töpel0c8493d2017-05-24 07:55:34 +020011594 return 0;
11595 default:
11596 return -EINVAL;
11597 }
11598}
11599
Shannon Nelson37a29732015-02-27 09:15:19 +000011600static const struct net_device_ops i40e_netdev_ops = {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011601 .ndo_open = i40e_open,
11602 .ndo_stop = i40e_close,
11603 .ndo_start_xmit = i40e_lan_xmit_frame,
11604 .ndo_get_stats64 = i40e_get_netdev_stats_struct,
11605 .ndo_set_rx_mode = i40e_set_rx_mode,
11606 .ndo_validate_addr = eth_validate_addr,
11607 .ndo_set_mac_address = i40e_set_mac,
11608 .ndo_change_mtu = i40e_change_mtu,
Jacob Kellerbeb0dff2014-01-11 05:43:19 +000011609 .ndo_do_ioctl = i40e_ioctl,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011610 .ndo_tx_timeout = i40e_tx_timeout,
11611 .ndo_vlan_rx_add_vid = i40e_vlan_rx_add_vid,
11612 .ndo_vlan_rx_kill_vid = i40e_vlan_rx_kill_vid,
11613#ifdef CONFIG_NET_POLL_CONTROLLER
11614 .ndo_poll_controller = i40e_netpoll,
11615#endif
John Fastabende4c67342016-02-16 21:16:15 -080011616 .ndo_setup_tc = __i40e_setup_tc,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011617 .ndo_set_features = i40e_set_features,
11618 .ndo_set_vf_mac = i40e_ndo_set_vf_mac,
11619 .ndo_set_vf_vlan = i40e_ndo_set_vf_port_vlan,
Sucheta Chakrabortyed616682014-05-22 09:59:05 -040011620 .ndo_set_vf_rate = i40e_ndo_set_vf_bw,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011621 .ndo_get_vf_config = i40e_ndo_get_vf_config,
Mitch Williams588aefa2014-02-11 08:27:49 +000011622 .ndo_set_vf_link_state = i40e_ndo_set_vf_link_state,
Serey Konge6d90042014-07-12 07:28:14 +000011623 .ndo_set_vf_spoofchk = i40e_ndo_set_vf_spoofchk,
Anjali Singhai Jainc3bbbd22016-04-01 03:56:07 -070011624 .ndo_set_vf_trust = i40e_ndo_set_vf_trust,
Alexander Duyck06a5f7f2016-06-16 12:22:06 -070011625 .ndo_udp_tunnel_add = i40e_udp_tunnel_add,
11626 .ndo_udp_tunnel_del = i40e_udp_tunnel_del,
Neerav Parikh1f224ad2014-02-12 01:45:31 +000011627 .ndo_get_phys_port_id = i40e_get_phys_port_id,
Greg Rose4ba0dea2014-03-06 08:59:55 +000011628 .ndo_fdb_add = i40e_ndo_fdb_add,
Joe Stringerf44a75e2015-04-14 17:09:14 -070011629 .ndo_features_check = i40e_features_check,
Neerav Parikh51616012015-02-06 08:52:14 +000011630 .ndo_bridge_getlink = i40e_ndo_bridge_getlink,
11631 .ndo_bridge_setlink = i40e_ndo_bridge_setlink,
Jakub Kicinskif4e63522017-11-03 13:56:16 -070011632 .ndo_bpf = i40e_xdp,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011633};
11634
11635/**
11636 * i40e_config_netdev - Setup the netdev flags
11637 * @vsi: the VSI being configured
11638 *
11639 * Returns 0 on success, negative value on failure
11640 **/
11641static int i40e_config_netdev(struct i40e_vsi *vsi)
11642{
11643 struct i40e_pf *pf = vsi->back;
11644 struct i40e_hw *hw = &pf->hw;
11645 struct i40e_netdev_priv *np;
11646 struct net_device *netdev;
Jacob Keller435c0842016-11-08 13:05:10 -080011647 u8 broadcast[ETH_ALEN];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011648 u8 mac_addr[ETH_ALEN];
11649 int etherdev_size;
Preethi Banalabacd75c2017-03-27 14:43:18 -070011650 netdev_features_t hw_enc_features;
11651 netdev_features_t hw_features;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011652
11653 etherdev_size = sizeof(struct i40e_netdev_priv);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +000011654 netdev = alloc_etherdev_mq(etherdev_size, vsi->alloc_queue_pairs);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011655 if (!netdev)
11656 return -ENOMEM;
11657
11658 vsi->netdev = netdev;
11659 np = netdev_priv(netdev);
11660 np->vsi = vsi;
11661
Preethi Banalabacd75c2017-03-27 14:43:18 -070011662 hw_enc_features = NETIF_F_SG |
11663 NETIF_F_IP_CSUM |
11664 NETIF_F_IPV6_CSUM |
11665 NETIF_F_HIGHDMA |
11666 NETIF_F_SOFT_FEATURES |
11667 NETIF_F_TSO |
11668 NETIF_F_TSO_ECN |
11669 NETIF_F_TSO6 |
11670 NETIF_F_GSO_GRE |
11671 NETIF_F_GSO_GRE_CSUM |
11672 NETIF_F_GSO_PARTIAL |
11673 NETIF_F_GSO_UDP_TUNNEL |
11674 NETIF_F_GSO_UDP_TUNNEL_CSUM |
11675 NETIF_F_SCTP_CRC |
11676 NETIF_F_RXHASH |
11677 NETIF_F_RXCSUM |
11678 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011679
Jacob Kellerd36e41d2017-06-23 04:24:46 -040011680 if (!(pf->hw_features & I40E_HW_OUTER_UDP_CSUM_CAPABLE))
Alexander Duyck1c7b4a22016-04-14 17:19:25 -040011681 netdev->gso_partial_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM;
11682
11683 netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
Alexander Duyckb0fe3302016-04-02 00:05:14 -070011684
Preethi Banalabacd75c2017-03-27 14:43:18 -070011685 netdev->hw_enc_features |= hw_enc_features;
11686
Alexander Duyckb0fe3302016-04-02 00:05:14 -070011687 /* record features VLANs can make use of */
Preethi Banalabacd75c2017-03-27 14:43:18 -070011688 netdev->vlan_features |= hw_enc_features | NETIF_F_TSO_MANGLEID;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011689
Anjali Singhai Jain2e86a0b2014-04-01 07:11:53 +000011690 if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
Amritha Nambiar2f4b4112017-10-27 02:36:01 -070011691 netdev->hw_features |= NETIF_F_NTUPLE | NETIF_F_HW_TC;
11692
Preethi Banalabacd75c2017-03-27 14:43:18 -070011693 hw_features = hw_enc_features |
11694 NETIF_F_HW_VLAN_CTAG_TX |
11695 NETIF_F_HW_VLAN_CTAG_RX;
Anjali Singhai Jain2e86a0b2014-04-01 07:11:53 +000011696
Preethi Banalabacd75c2017-03-27 14:43:18 -070011697 netdev->hw_features |= hw_features;
Alexander Duyckb0fe3302016-04-02 00:05:14 -070011698
Preethi Banalabacd75c2017-03-27 14:43:18 -070011699 netdev->features |= hw_features | NETIF_F_HW_VLAN_CTAG_FILTER;
Alexander Duyck1c7b4a22016-04-14 17:19:25 -040011700 netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011701
11702 if (vsi->type == I40E_VSI_MAIN) {
11703 SET_NETDEV_DEV(netdev, &pf->pdev->dev);
Greg Rose9a173902014-05-22 06:32:02 +000011704 ether_addr_copy(mac_addr, hw->mac.perm_addr);
Jacob Keller41c4c2b2017-04-05 07:50:57 -040011705 /* The following steps are necessary for two reasons. First,
11706 * some older NVM configurations load a default MAC-VLAN
11707 * filter that will accept any tagged packet, and we want to
11708 * replace this with a normal filter. Additionally, it is
11709 * possible our MAC address was provided by the platform using
11710 * Open Firmware or similar.
11711 *
11712 * Thus, we need to remove the default filter and install one
11713 * specific to the MAC address.
Jacob Keller1596b5d2016-11-08 13:05:15 -080011714 */
11715 i40e_rm_default_mac_filter(vsi, mac_addr);
Jacob Keller278e7d02016-10-05 09:30:37 -070011716 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller9569a9a2016-11-11 12:39:37 -080011717 i40e_add_mac_filter(vsi, mac_addr);
Jacob Keller278e7d02016-10-05 09:30:37 -070011718 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011719 } else {
Jacob Keller8c9eb352017-07-12 05:46:12 -040011720 /* Relate the VSI_VMDQ name to the VSI_MAIN name. Note that we
11721 * are still limited by IFNAMSIZ, but we're adding 'v%d\0' to
11722 * the end, which is 4 bytes long, so force truncation of the
11723 * original name by IFNAMSIZ - 4
11724 */
11725 snprintf(netdev->name, IFNAMSIZ, "%.*sv%%d",
11726 IFNAMSIZ - 4,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011727 pf->vsi[pf->lan_vsi]->netdev->name);
11728 random_ether_addr(mac_addr);
Kiran Patil21659032015-09-30 14:09:03 -040011729
Jacob Keller278e7d02016-10-05 09:30:37 -070011730 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller9569a9a2016-11-11 12:39:37 -080011731 i40e_add_mac_filter(vsi, mac_addr);
Jacob Keller278e7d02016-10-05 09:30:37 -070011732 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011733 }
Kiran Patil21659032015-09-30 14:09:03 -040011734
Jacob Keller435c0842016-11-08 13:05:10 -080011735 /* Add the broadcast filter so that we initially will receive
11736 * broadcast packets. Note that when a new VLAN is first added the
11737 * driver will convert all filters marked I40E_VLAN_ANY into VLAN
11738 * specific filters as part of transitioning into "vlan" operation.
11739 * When more VLANs are added, the driver will copy each existing MAC
11740 * filter and add it for the new VLAN.
11741 *
11742 * Broadcast filters are handled specially by
11743 * i40e_sync_filters_subtask, as the driver must to set the broadcast
11744 * promiscuous bit instead of adding this directly as a MAC/VLAN
11745 * filter. The subtask will update the correct broadcast promiscuous
11746 * bits as VLANs become active or inactive.
11747 */
11748 eth_broadcast_addr(broadcast);
11749 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller9569a9a2016-11-11 12:39:37 -080011750 i40e_add_mac_filter(vsi, broadcast);
Jacob Keller435c0842016-11-08 13:05:10 -080011751 spin_unlock_bh(&vsi->mac_filter_hash_lock);
11752
Greg Rose9a173902014-05-22 06:32:02 +000011753 ether_addr_copy(netdev->dev_addr, mac_addr);
11754 ether_addr_copy(netdev->perm_addr, mac_addr);
Alexander Duyckb0fe3302016-04-02 00:05:14 -070011755
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011756 netdev->priv_flags |= IFF_UNICAST_FLT;
11757 netdev->priv_flags |= IFF_SUPP_NOFCS;
11758 /* Setup netdev TC information */
11759 i40e_vsi_config_netdev_tc(vsi, vsi->tc_config.enabled_tc);
11760
11761 netdev->netdev_ops = &i40e_netdev_ops;
11762 netdev->watchdog_timeo = 5 * HZ;
11763 i40e_set_ethtool_ops(netdev);
11764
Jarod Wilson91c527a2016-10-17 15:54:05 -040011765 /* MTU range: 68 - 9706 */
11766 netdev->min_mtu = ETH_MIN_MTU;
Mitch Williams1e3a5fd2017-06-23 04:24:43 -040011767 netdev->max_mtu = I40E_MAX_RXBUFFER - I40E_PACKET_HDR_PAD;
Jarod Wilson91c527a2016-10-17 15:54:05 -040011768
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011769 return 0;
11770}
11771
11772/**
11773 * i40e_vsi_delete - Delete a VSI from the switch
11774 * @vsi: the VSI being removed
11775 *
11776 * Returns 0 on success, negative value on failure
11777 **/
11778static void i40e_vsi_delete(struct i40e_vsi *vsi)
11779{
11780 /* remove default VSI is not allowed */
11781 if (vsi == vsi->back->vsi[vsi->back->lan_vsi])
11782 return;
11783
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011784 i40e_aq_delete_element(&vsi->back->hw, vsi->seid, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011785}
11786
11787/**
Neerav Parikh51616012015-02-06 08:52:14 +000011788 * i40e_is_vsi_uplink_mode_veb - Check if the VSI's uplink bridge mode is VEB
11789 * @vsi: the VSI being queried
11790 *
11791 * Returns 1 if HW bridge mode is VEB and return 0 in case of VEPA mode
11792 **/
11793int i40e_is_vsi_uplink_mode_veb(struct i40e_vsi *vsi)
11794{
11795 struct i40e_veb *veb;
11796 struct i40e_pf *pf = vsi->back;
11797
11798 /* Uplink is not a bridge so default to VEB */
11799 if (vsi->veb_idx == I40E_NO_VEB)
11800 return 1;
11801
11802 veb = pf->veb[vsi->veb_idx];
Akeem G Abodunrin09603ea2015-10-01 14:37:36 -040011803 if (!veb) {
11804 dev_info(&pf->pdev->dev,
11805 "There is no veb associated with the bridge\n");
11806 return -ENOENT;
11807 }
Neerav Parikh51616012015-02-06 08:52:14 +000011808
Akeem G Abodunrin09603ea2015-10-01 14:37:36 -040011809 /* Uplink is a bridge in VEPA mode */
11810 if (veb->bridge_mode & BRIDGE_MODE_VEPA) {
11811 return 0;
11812 } else {
11813 /* Uplink is a bridge in VEB mode */
11814 return 1;
11815 }
11816
11817 /* VEPA is now default bridge, so return 0 */
11818 return 0;
Neerav Parikh51616012015-02-06 08:52:14 +000011819}
11820
11821/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011822 * i40e_add_vsi - Add a VSI to the switch
11823 * @vsi: the VSI being configured
11824 *
11825 * This initializes a VSI context depending on the VSI type to be added and
11826 * passes it down to the add_vsi aq command.
11827 **/
11828static int i40e_add_vsi(struct i40e_vsi *vsi)
11829{
11830 int ret = -ENODEV;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011831 struct i40e_pf *pf = vsi->back;
11832 struct i40e_hw *hw = &pf->hw;
11833 struct i40e_vsi_context ctxt;
Jacob Keller278e7d02016-10-05 09:30:37 -070011834 struct i40e_mac_filter *f;
11835 struct hlist_node *h;
11836 int bkt;
Kiran Patil21659032015-09-30 14:09:03 -040011837
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011838 u8 enabled_tc = 0x1; /* TC0 enabled */
11839 int f_count = 0;
11840
11841 memset(&ctxt, 0, sizeof(ctxt));
11842 switch (vsi->type) {
11843 case I40E_VSI_MAIN:
11844 /* The PF's main VSI is already setup as part of the
11845 * device initialization, so we'll not bother with
11846 * the add_vsi call, but we will retrieve the current
11847 * VSI context.
11848 */
11849 ctxt.seid = pf->main_vsi_seid;
11850 ctxt.pf_num = pf->hw.pf_id;
11851 ctxt.vf_num = 0;
11852 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
11853 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
11854 if (ret) {
11855 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040011856 "couldn't get PF vsi config, err %s aq_err %s\n",
11857 i40e_stat_str(&pf->hw, ret),
11858 i40e_aq_str(&pf->hw,
11859 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011860 return -ENOENT;
11861 }
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -070011862 vsi->info = ctxt.info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011863 vsi->info.valid_sections = 0;
11864
11865 vsi->seid = ctxt.seid;
11866 vsi->id = ctxt.vsi_number;
11867
11868 enabled_tc = i40e_pf_get_tc_map(pf);
11869
Mitch Williams64615b52017-08-29 05:32:30 -040011870 /* Source pruning is enabled by default, so the flag is
11871 * negative logic - if it's set, we need to fiddle with
11872 * the VSI to disable source pruning.
11873 */
11874 if (pf->flags & I40E_FLAG_SOURCE_PRUNING_DISABLED) {
11875 memset(&ctxt, 0, sizeof(ctxt));
11876 ctxt.seid = pf->main_vsi_seid;
11877 ctxt.pf_num = pf->hw.pf_id;
11878 ctxt.vf_num = 0;
11879 ctxt.info.valid_sections |=
11880 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
11881 ctxt.info.switch_id =
11882 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_LOCAL_LB);
11883 ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
11884 if (ret) {
11885 dev_info(&pf->pdev->dev,
11886 "update vsi failed, err %s aq_err %s\n",
11887 i40e_stat_str(&pf->hw, ret),
11888 i40e_aq_str(&pf->hw,
11889 pf->hw.aq.asq_last_status));
11890 ret = -ENOENT;
11891 goto err;
11892 }
11893 }
11894
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011895 /* MFP mode setup queue map and update VSI */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +000011896 if ((pf->flags & I40E_FLAG_MFP_ENABLED) &&
11897 !(pf->hw.func_caps.iscsi)) { /* NIC type PF */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011898 memset(&ctxt, 0, sizeof(ctxt));
11899 ctxt.seid = pf->main_vsi_seid;
11900 ctxt.pf_num = pf->hw.pf_id;
11901 ctxt.vf_num = 0;
11902 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
11903 ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
11904 if (ret) {
11905 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040011906 "update vsi failed, err %s aq_err %s\n",
11907 i40e_stat_str(&pf->hw, ret),
11908 i40e_aq_str(&pf->hw,
11909 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011910 ret = -ENOENT;
11911 goto err;
11912 }
11913 /* update the local VSI info queue map */
11914 i40e_vsi_update_queue_map(vsi, &ctxt);
11915 vsi->info.valid_sections = 0;
11916 } else {
11917 /* Default/Main VSI is only enabled for TC0
11918 * reconfigure it to enable all TCs that are
11919 * available on the port in SFP mode.
Neerav Parikh63d7e5a2014-12-14 01:55:16 +000011920 * For MFP case the iSCSI PF would use this
11921 * flow to enable LAN+iSCSI TC.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011922 */
11923 ret = i40e_vsi_config_tc(vsi, enabled_tc);
11924 if (ret) {
Carolyn Wyborny19279232017-07-14 09:10:07 -040011925 /* Single TC condition is not fatal,
11926 * message and continue
11927 */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011928 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040011929 "failed to configure TCs for main VSI tc_map 0x%08x, err %s aq_err %s\n",
11930 enabled_tc,
11931 i40e_stat_str(&pf->hw, ret),
11932 i40e_aq_str(&pf->hw,
11933 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011934 }
11935 }
11936 break;
11937
11938 case I40E_VSI_FDIR:
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080011939 ctxt.pf_num = hw->pf_id;
11940 ctxt.vf_num = 0;
11941 ctxt.uplink_seid = vsi->uplink_seid;
Neerav Parikh2b18e592015-01-24 09:58:38 +000011942 ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080011943 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
Anjali Singhai Jainfc608612015-05-08 15:35:57 -070011944 if ((pf->flags & I40E_FLAG_VEB_MODE_ENABLED) &&
11945 (i40e_is_vsi_uplink_mode_veb(vsi))) {
Neerav Parikh51616012015-02-06 08:52:14 +000011946 ctxt.info.valid_sections |=
Anjali Singhai Jainfc608612015-05-08 15:35:57 -070011947 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
Neerav Parikh51616012015-02-06 08:52:14 +000011948 ctxt.info.switch_id =
Anjali Singhai Jainfc608612015-05-08 15:35:57 -070011949 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
Neerav Parikh51616012015-02-06 08:52:14 +000011950 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011951 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011952 break;
11953
11954 case I40E_VSI_VMDQ2:
11955 ctxt.pf_num = hw->pf_id;
11956 ctxt.vf_num = 0;
11957 ctxt.uplink_seid = vsi->uplink_seid;
Neerav Parikh2b18e592015-01-24 09:58:38 +000011958 ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011959 ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
11960
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011961 /* This VSI is connected to VEB so the switch_id
11962 * should be set to zero by default.
11963 */
Neerav Parikh51616012015-02-06 08:52:14 +000011964 if (i40e_is_vsi_uplink_mode_veb(vsi)) {
11965 ctxt.info.valid_sections |=
11966 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
11967 ctxt.info.switch_id =
11968 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
11969 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011970
11971 /* Setup the VSI tx/rx queue map for TC0 only for now */
11972 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
11973 break;
11974
11975 case I40E_VSI_SRIOV:
11976 ctxt.pf_num = hw->pf_id;
11977 ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
11978 ctxt.uplink_seid = vsi->uplink_seid;
Neerav Parikh2b18e592015-01-24 09:58:38 +000011979 ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011980 ctxt.flags = I40E_AQ_VSI_TYPE_VF;
11981
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011982 /* This VSI is connected to VEB so the switch_id
11983 * should be set to zero by default.
11984 */
Neerav Parikh51616012015-02-06 08:52:14 +000011985 if (i40e_is_vsi_uplink_mode_veb(vsi)) {
11986 ctxt.info.valid_sections |=
11987 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
11988 ctxt.info.switch_id =
11989 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
11990 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011991
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060011992 if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
11993 ctxt.info.valid_sections |=
11994 cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
11995 ctxt.info.queueing_opt_flags |=
Ashish Shah4b28cdb2016-05-03 15:13:17 -070011996 (I40E_AQ_VSI_QUE_OPT_TCP_ENA |
11997 I40E_AQ_VSI_QUE_OPT_RSS_LUT_VSI);
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060011998 }
11999
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012000 ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
12001 ctxt.info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_MODE_ALL;
Mitch Williamsc674d122014-05-20 08:01:40 +000012002 if (pf->vf[vsi->vf_id].spoofchk) {
12003 ctxt.info.valid_sections |=
12004 cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
12005 ctxt.info.sec_flags |=
12006 (I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
12007 I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
12008 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012009 /* Setup the VSI tx/rx queue map for TC0 only for now */
12010 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
12011 break;
12012
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060012013 case I40E_VSI_IWARP:
12014 /* send down message to iWARP */
12015 break;
12016
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012017 default:
12018 return -ENODEV;
12019 }
12020
12021 if (vsi->type != I40E_VSI_MAIN) {
12022 ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
12023 if (ret) {
12024 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040012025 "add vsi failed, err %s aq_err %s\n",
12026 i40e_stat_str(&pf->hw, ret),
12027 i40e_aq_str(&pf->hw,
12028 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012029 ret = -ENOENT;
12030 goto err;
12031 }
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -070012032 vsi->info = ctxt.info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012033 vsi->info.valid_sections = 0;
12034 vsi->seid = ctxt.seid;
12035 vsi->id = ctxt.vsi_number;
12036 }
12037
Mitch Williamsc3c7ea22016-06-20 09:10:38 -070012038 vsi->active_filters = 0;
Jacob Keller0da36b92017-04-19 09:25:55 -040012039 clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
Jacob Keller278e7d02016-10-05 09:30:37 -070012040 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012041 /* If macvlan filters already exist, force them to get loaded */
Jacob Keller278e7d02016-10-05 09:30:37 -070012042 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -070012043 f->state = I40E_FILTER_NEW;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012044 f_count++;
12045 }
Jacob Keller278e7d02016-10-05 09:30:37 -070012046 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -040012047
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012048 if (f_count) {
12049 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
12050 pf->flags |= I40E_FLAG_FILTER_SYNC;
12051 }
12052
12053 /* Update VSI BW information */
12054 ret = i40e_vsi_get_bw_info(vsi);
12055 if (ret) {
12056 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040012057 "couldn't get vsi bw info, err %s aq_err %s\n",
12058 i40e_stat_str(&pf->hw, ret),
12059 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012060 /* VSI is already added so not tearing that up */
12061 ret = 0;
12062 }
12063
12064err:
12065 return ret;
12066}
12067
12068/**
12069 * i40e_vsi_release - Delete a VSI and free its resources
12070 * @vsi: the VSI being removed
12071 *
12072 * Returns 0 on success or < 0 on error
12073 **/
12074int i40e_vsi_release(struct i40e_vsi *vsi)
12075{
Jacob Keller278e7d02016-10-05 09:30:37 -070012076 struct i40e_mac_filter *f;
12077 struct hlist_node *h;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012078 struct i40e_veb *veb = NULL;
12079 struct i40e_pf *pf;
12080 u16 uplink_seid;
Jacob Keller278e7d02016-10-05 09:30:37 -070012081 int i, n, bkt;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012082
12083 pf = vsi->back;
12084
12085 /* release of a VEB-owner or last VSI is not allowed */
12086 if (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) {
12087 dev_info(&pf->pdev->dev, "VSI %d has existing VEB %d\n",
12088 vsi->seid, vsi->uplink_seid);
12089 return -ENODEV;
12090 }
12091 if (vsi == pf->vsi[pf->lan_vsi] &&
Mauro S. M. Rodrigues9e6c9c02017-05-12 23:26:56 -030012092 !test_bit(__I40E_DOWN, pf->state)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012093 dev_info(&pf->pdev->dev, "Can't remove PF VSI\n");
12094 return -ENODEV;
12095 }
12096
12097 uplink_seid = vsi->uplink_seid;
12098 if (vsi->type != I40E_VSI_SRIOV) {
12099 if (vsi->netdev_registered) {
12100 vsi->netdev_registered = false;
12101 if (vsi->netdev) {
12102 /* results in a call to i40e_close() */
12103 unregister_netdev(vsi->netdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012104 }
12105 } else {
Shannon Nelson90ef8d42014-03-14 07:32:26 +000012106 i40e_vsi_close(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012107 }
12108 i40e_vsi_disable_irq(vsi);
12109 }
12110
Jacob Keller278e7d02016-10-05 09:30:37 -070012111 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller6622f5c2016-10-05 09:30:32 -070012112
12113 /* clear the sync flag on all filters */
12114 if (vsi->netdev) {
12115 __dev_uc_unsync(vsi->netdev, NULL);
12116 __dev_mc_unsync(vsi->netdev, NULL);
12117 }
12118
12119 /* make sure any remaining filters are marked for deletion */
Jacob Keller278e7d02016-10-05 09:30:37 -070012120 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
Jacob Keller290d2552016-10-05 09:30:36 -070012121 __i40e_del_filter(vsi, f);
Jacob Keller6622f5c2016-10-05 09:30:32 -070012122
Jacob Keller278e7d02016-10-05 09:30:37 -070012123 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -040012124
Jesse Brandeburg17652c62015-11-05 17:01:02 -080012125 i40e_sync_vsi_filters(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012126
12127 i40e_vsi_delete(vsi);
12128 i40e_vsi_free_q_vectors(vsi);
Shannon Nelsona4866592014-02-11 08:24:07 +000012129 if (vsi->netdev) {
12130 free_netdev(vsi->netdev);
12131 vsi->netdev = NULL;
12132 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012133 i40e_vsi_clear_rings(vsi);
12134 i40e_vsi_clear(vsi);
12135
12136 /* If this was the last thing on the VEB, except for the
12137 * controlling VSI, remove the VEB, which puts the controlling
12138 * VSI onto the next level down in the switch.
12139 *
12140 * Well, okay, there's one more exception here: don't remove
12141 * the orphan VEBs yet. We'll wait for an explicit remove request
12142 * from up the network stack.
12143 */
Mitch Williams505682c2014-05-20 08:01:37 +000012144 for (n = 0, i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012145 if (pf->vsi[i] &&
12146 pf->vsi[i]->uplink_seid == uplink_seid &&
12147 (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
12148 n++; /* count the VSIs */
12149 }
12150 }
12151 for (i = 0; i < I40E_MAX_VEB; i++) {
12152 if (!pf->veb[i])
12153 continue;
12154 if (pf->veb[i]->uplink_seid == uplink_seid)
12155 n++; /* count the VEBs */
12156 if (pf->veb[i]->seid == uplink_seid)
12157 veb = pf->veb[i];
12158 }
12159 if (n == 0 && veb && veb->uplink_seid != 0)
12160 i40e_veb_release(veb);
12161
12162 return 0;
12163}
12164
12165/**
12166 * i40e_vsi_setup_vectors - Set up the q_vectors for the given VSI
12167 * @vsi: ptr to the VSI
12168 *
12169 * This should only be called after i40e_vsi_mem_alloc() which allocates the
12170 * corresponding SW VSI structure and initializes num_queue_pairs for the
12171 * newly allocated VSI.
12172 *
12173 * Returns 0 on success or negative on failure
12174 **/
12175static int i40e_vsi_setup_vectors(struct i40e_vsi *vsi)
12176{
12177 int ret = -ENOENT;
12178 struct i40e_pf *pf = vsi->back;
12179
Alexander Duyck493fb302013-09-28 07:01:44 +000012180 if (vsi->q_vectors[0]) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012181 dev_info(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
12182 vsi->seid);
12183 return -EEXIST;
12184 }
12185
12186 if (vsi->base_vector) {
Jesse Brandeburgf29eaa32014-02-11 08:24:12 +000012187 dev_info(&pf->pdev->dev, "VSI %d has non-zero base vector %d\n",
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012188 vsi->seid, vsi->base_vector);
12189 return -EEXIST;
12190 }
12191
Greg Rose90e04072014-03-06 08:59:57 +000012192 ret = i40e_vsi_alloc_q_vectors(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012193 if (ret) {
12194 dev_info(&pf->pdev->dev,
12195 "failed to allocate %d q_vector for VSI %d, ret=%d\n",
12196 vsi->num_q_vectors, vsi->seid, ret);
12197 vsi->num_q_vectors = 0;
12198 goto vector_setup_out;
12199 }
12200
Anjali Singhai Jain26cdc442015-07-10 19:36:00 -040012201 /* In Legacy mode, we do not have to get any other vector since we
12202 * piggyback on the misc/ICR0 for queue interrupts.
12203 */
12204 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
12205 return ret;
Shannon Nelson958a3e32013-09-28 07:13:28 +000012206 if (vsi->num_q_vectors)
12207 vsi->base_vector = i40e_get_lump(pf, pf->irq_pile,
12208 vsi->num_q_vectors, vsi->idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012209 if (vsi->base_vector < 0) {
12210 dev_info(&pf->pdev->dev,
Shannon Nelson049a2be2014-10-17 03:14:50 +000012211 "failed to get tracking for %d vectors for VSI %d, err=%d\n",
12212 vsi->num_q_vectors, vsi->seid, vsi->base_vector);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012213 i40e_vsi_free_q_vectors(vsi);
12214 ret = -ENOENT;
12215 goto vector_setup_out;
12216 }
12217
12218vector_setup_out:
12219 return ret;
12220}
12221
12222/**
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000012223 * i40e_vsi_reinit_setup - return and reallocate resources for a VSI
12224 * @vsi: pointer to the vsi.
12225 *
12226 * This re-allocates a vsi's queue resources.
12227 *
12228 * Returns pointer to the successfully allocated and configured VSI sw struct
12229 * on success, otherwise returns NULL on failure.
12230 **/
12231static struct i40e_vsi *i40e_vsi_reinit_setup(struct i40e_vsi *vsi)
12232{
Björn Töpel74608d12017-05-24 07:55:35 +020012233 u16 alloc_queue_pairs;
John Underwoodf5340392016-02-18 09:19:24 -080012234 struct i40e_pf *pf;
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000012235 u8 enabled_tc;
12236 int ret;
12237
John Underwoodf5340392016-02-18 09:19:24 -080012238 if (!vsi)
12239 return NULL;
12240
12241 pf = vsi->back;
12242
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000012243 i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
12244 i40e_vsi_clear_rings(vsi);
12245
12246 i40e_vsi_free_arrays(vsi, false);
12247 i40e_set_num_rings_in_vsi(vsi);
12248 ret = i40e_vsi_alloc_arrays(vsi, false);
12249 if (ret)
12250 goto err_vsi;
12251
Björn Töpel74608d12017-05-24 07:55:35 +020012252 alloc_queue_pairs = vsi->alloc_queue_pairs *
12253 (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
12254
12255 ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000012256 if (ret < 0) {
Shannon Nelson049a2be2014-10-17 03:14:50 +000012257 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040012258 "failed to get tracking for %d queues for VSI %d err %d\n",
Björn Töpel74608d12017-05-24 07:55:35 +020012259 alloc_queue_pairs, vsi->seid, ret);
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000012260 goto err_vsi;
12261 }
12262 vsi->base_queue = ret;
12263
12264 /* Update the FW view of the VSI. Force a reset of TC and queue
12265 * layout configurations.
12266 */
12267 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
12268 pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
12269 pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
12270 i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
Jacob Keller1596b5d2016-11-08 13:05:15 -080012271 if (vsi->type == I40E_VSI_MAIN)
12272 i40e_rm_default_mac_filter(vsi, pf->hw.mac.perm_addr);
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000012273
12274 /* assign it some queues */
12275 ret = i40e_alloc_rings(vsi);
12276 if (ret)
12277 goto err_rings;
12278
12279 /* map all of the rings to the q_vectors */
12280 i40e_vsi_map_rings_to_vectors(vsi);
12281 return vsi;
12282
12283err_rings:
12284 i40e_vsi_free_q_vectors(vsi);
12285 if (vsi->netdev_registered) {
12286 vsi->netdev_registered = false;
12287 unregister_netdev(vsi->netdev);
12288 free_netdev(vsi->netdev);
12289 vsi->netdev = NULL;
12290 }
12291 i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
12292err_vsi:
12293 i40e_vsi_clear(vsi);
12294 return NULL;
12295}
12296
12297/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012298 * i40e_vsi_setup - Set up a VSI by a given type
12299 * @pf: board private structure
12300 * @type: VSI type
12301 * @uplink_seid: the switch element to link to
12302 * @param1: usage depends upon VSI type. For VF types, indicates VF id
12303 *
12304 * This allocates the sw VSI structure and its queue resources, then add a VSI
12305 * to the identified VEB.
12306 *
12307 * Returns pointer to the successfully allocated and configure VSI sw struct on
12308 * success, otherwise returns NULL on failure.
12309 **/
12310struct i40e_vsi *i40e_vsi_setup(struct i40e_pf *pf, u8 type,
12311 u16 uplink_seid, u32 param1)
12312{
12313 struct i40e_vsi *vsi = NULL;
12314 struct i40e_veb *veb = NULL;
Björn Töpel74608d12017-05-24 07:55:35 +020012315 u16 alloc_queue_pairs;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012316 int ret, i;
12317 int v_idx;
12318
12319 /* The requested uplink_seid must be either
12320 * - the PF's port seid
12321 * no VEB is needed because this is the PF
12322 * or this is a Flow Director special case VSI
12323 * - seid of an existing VEB
12324 * - seid of a VSI that owns an existing VEB
12325 * - seid of a VSI that doesn't own a VEB
12326 * a new VEB is created and the VSI becomes the owner
12327 * - seid of the PF VSI, which is what creates the first VEB
12328 * this is a special case of the previous
12329 *
12330 * Find which uplink_seid we were given and create a new VEB if needed
12331 */
12332 for (i = 0; i < I40E_MAX_VEB; i++) {
12333 if (pf->veb[i] && pf->veb[i]->seid == uplink_seid) {
12334 veb = pf->veb[i];
12335 break;
12336 }
12337 }
12338
12339 if (!veb && uplink_seid != pf->mac_seid) {
12340
Mitch Williams505682c2014-05-20 08:01:37 +000012341 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012342 if (pf->vsi[i] && pf->vsi[i]->seid == uplink_seid) {
12343 vsi = pf->vsi[i];
12344 break;
12345 }
12346 }
12347 if (!vsi) {
12348 dev_info(&pf->pdev->dev, "no such uplink_seid %d\n",
12349 uplink_seid);
12350 return NULL;
12351 }
12352
12353 if (vsi->uplink_seid == pf->mac_seid)
12354 veb = i40e_veb_setup(pf, 0, pf->mac_seid, vsi->seid,
12355 vsi->tc_config.enabled_tc);
12356 else if ((vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
12357 veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
12358 vsi->tc_config.enabled_tc);
Anjali Singhai Jain79c21a82014-11-13 03:06:14 +000012359 if (veb) {
12360 if (vsi->seid != pf->vsi[pf->lan_vsi]->seid) {
12361 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -040012362 "New VSI creation error, uplink seid of LAN VSI expected.\n");
Anjali Singhai Jain79c21a82014-11-13 03:06:14 +000012363 return NULL;
12364 }
Anjali Singhai Jainfa11cb32015-05-27 12:06:14 -040012365 /* We come up by default in VEPA mode if SRIOV is not
12366 * already enabled, in which case we can't force VEPA
12367 * mode.
12368 */
12369 if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
12370 veb->bridge_mode = BRIDGE_MODE_VEPA;
12371 pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
12372 }
Neerav Parikh51616012015-02-06 08:52:14 +000012373 i40e_config_bridge_mode(veb);
Anjali Singhai Jain79c21a82014-11-13 03:06:14 +000012374 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012375 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
12376 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
12377 veb = pf->veb[i];
12378 }
12379 if (!veb) {
12380 dev_info(&pf->pdev->dev, "couldn't add VEB\n");
12381 return NULL;
12382 }
12383
12384 vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
12385 uplink_seid = veb->seid;
12386 }
12387
12388 /* get vsi sw struct */
12389 v_idx = i40e_vsi_mem_alloc(pf, type);
12390 if (v_idx < 0)
12391 goto err_alloc;
12392 vsi = pf->vsi[v_idx];
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080012393 if (!vsi)
12394 goto err_alloc;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012395 vsi->type = type;
12396 vsi->veb_idx = (veb ? veb->idx : I40E_NO_VEB);
12397
12398 if (type == I40E_VSI_MAIN)
12399 pf->lan_vsi = v_idx;
12400 else if (type == I40E_VSI_SRIOV)
12401 vsi->vf_id = param1;
12402 /* assign it some queues */
Björn Töpel74608d12017-05-24 07:55:35 +020012403 alloc_queue_pairs = vsi->alloc_queue_pairs *
12404 (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
12405
12406 ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012407 if (ret < 0) {
Shannon Nelson049a2be2014-10-17 03:14:50 +000012408 dev_info(&pf->pdev->dev,
12409 "failed to get tracking for %d queues for VSI %d err=%d\n",
Björn Töpel74608d12017-05-24 07:55:35 +020012410 alloc_queue_pairs, vsi->seid, ret);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012411 goto err_vsi;
12412 }
12413 vsi->base_queue = ret;
12414
12415 /* get a VSI from the hardware */
12416 vsi->uplink_seid = uplink_seid;
12417 ret = i40e_add_vsi(vsi);
12418 if (ret)
12419 goto err_vsi;
12420
12421 switch (vsi->type) {
12422 /* setup the netdev if needed */
12423 case I40E_VSI_MAIN:
12424 case I40E_VSI_VMDQ2:
12425 ret = i40e_config_netdev(vsi);
12426 if (ret)
12427 goto err_netdev;
12428 ret = register_netdev(vsi->netdev);
12429 if (ret)
12430 goto err_netdev;
12431 vsi->netdev_registered = true;
12432 netif_carrier_off(vsi->netdev);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -080012433#ifdef CONFIG_I40E_DCB
12434 /* Setup DCB netlink interface */
12435 i40e_dcbnl_setup(vsi);
12436#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012437 /* fall through */
12438
12439 case I40E_VSI_FDIR:
12440 /* set up vectors and rings if needed */
12441 ret = i40e_vsi_setup_vectors(vsi);
12442 if (ret)
12443 goto err_msix;
12444
12445 ret = i40e_alloc_rings(vsi);
12446 if (ret)
12447 goto err_rings;
12448
12449 /* map all of the rings to the q_vectors */
12450 i40e_vsi_map_rings_to_vectors(vsi);
12451
12452 i40e_vsi_reset_stats(vsi);
12453 break;
12454
12455 default:
12456 /* no netdev or rings for the other VSI types */
12457 break;
12458 }
12459
Jacob Kellerd36e41d2017-06-23 04:24:46 -040012460 if ((pf->hw_features & I40E_HW_RSS_AQ_CAPABLE) &&
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -040012461 (vsi->type == I40E_VSI_VMDQ2)) {
12462 ret = i40e_vsi_config_rss(vsi);
12463 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012464 return vsi;
12465
12466err_rings:
12467 i40e_vsi_free_q_vectors(vsi);
12468err_msix:
12469 if (vsi->netdev_registered) {
12470 vsi->netdev_registered = false;
12471 unregister_netdev(vsi->netdev);
12472 free_netdev(vsi->netdev);
12473 vsi->netdev = NULL;
12474 }
12475err_netdev:
12476 i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
12477err_vsi:
12478 i40e_vsi_clear(vsi);
12479err_alloc:
12480 return NULL;
12481}
12482
12483/**
12484 * i40e_veb_get_bw_info - Query VEB BW information
12485 * @veb: the veb to query
12486 *
12487 * Query the Tx scheduler BW configuration data for given VEB
12488 **/
12489static int i40e_veb_get_bw_info(struct i40e_veb *veb)
12490{
12491 struct i40e_aqc_query_switching_comp_ets_config_resp ets_data;
12492 struct i40e_aqc_query_switching_comp_bw_config_resp bw_data;
12493 struct i40e_pf *pf = veb->pf;
12494 struct i40e_hw *hw = &pf->hw;
12495 u32 tc_bw_max;
12496 int ret = 0;
12497 int i;
12498
12499 ret = i40e_aq_query_switch_comp_bw_config(hw, veb->seid,
12500 &bw_data, NULL);
12501 if (ret) {
12502 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040012503 "query veb bw config failed, err %s aq_err %s\n",
12504 i40e_stat_str(&pf->hw, ret),
12505 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012506 goto out;
12507 }
12508
12509 ret = i40e_aq_query_switch_comp_ets_config(hw, veb->seid,
12510 &ets_data, NULL);
12511 if (ret) {
12512 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040012513 "query veb bw ets config failed, err %s aq_err %s\n",
12514 i40e_stat_str(&pf->hw, ret),
12515 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012516 goto out;
12517 }
12518
12519 veb->bw_limit = le16_to_cpu(ets_data.port_bw_limit);
12520 veb->bw_max_quanta = ets_data.tc_bw_max;
12521 veb->is_abs_credits = bw_data.absolute_credits_enable;
Neerav Parikh23cd1f02014-11-12 00:18:41 +000012522 veb->enabled_tc = ets_data.tc_valid_bits;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012523 tc_bw_max = le16_to_cpu(bw_data.tc_bw_max[0]) |
12524 (le16_to_cpu(bw_data.tc_bw_max[1]) << 16);
12525 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
12526 veb->bw_tc_share_credits[i] = bw_data.tc_bw_share_credits[i];
12527 veb->bw_tc_limit_credits[i] =
12528 le16_to_cpu(bw_data.tc_bw_limits[i]);
12529 veb->bw_tc_max_quanta[i] = ((tc_bw_max >> (i*4)) & 0x7);
12530 }
12531
12532out:
12533 return ret;
12534}
12535
12536/**
12537 * i40e_veb_mem_alloc - Allocates the next available struct veb in the PF
12538 * @pf: board private structure
12539 *
12540 * On error: returns error code (negative)
12541 * On success: returns vsi index in PF (positive)
12542 **/
12543static int i40e_veb_mem_alloc(struct i40e_pf *pf)
12544{
12545 int ret = -ENOENT;
12546 struct i40e_veb *veb;
12547 int i;
12548
12549 /* Need to protect the allocation of switch elements at the PF level */
12550 mutex_lock(&pf->switch_mutex);
12551
12552 /* VEB list may be fragmented if VEB creation/destruction has
12553 * been happening. We can afford to do a quick scan to look
12554 * for any free slots in the list.
12555 *
12556 * find next empty veb slot, looping back around if necessary
12557 */
12558 i = 0;
12559 while ((i < I40E_MAX_VEB) && (pf->veb[i] != NULL))
12560 i++;
12561 if (i >= I40E_MAX_VEB) {
12562 ret = -ENOMEM;
12563 goto err_alloc_veb; /* out of VEB slots! */
12564 }
12565
12566 veb = kzalloc(sizeof(*veb), GFP_KERNEL);
12567 if (!veb) {
12568 ret = -ENOMEM;
12569 goto err_alloc_veb;
12570 }
12571 veb->pf = pf;
12572 veb->idx = i;
12573 veb->enabled_tc = 1;
12574
12575 pf->veb[i] = veb;
12576 ret = i;
12577err_alloc_veb:
12578 mutex_unlock(&pf->switch_mutex);
12579 return ret;
12580}
12581
12582/**
12583 * i40e_switch_branch_release - Delete a branch of the switch tree
12584 * @branch: where to start deleting
12585 *
12586 * This uses recursion to find the tips of the branch to be
12587 * removed, deleting until we get back to and can delete this VEB.
12588 **/
12589static void i40e_switch_branch_release(struct i40e_veb *branch)
12590{
12591 struct i40e_pf *pf = branch->pf;
12592 u16 branch_seid = branch->seid;
12593 u16 veb_idx = branch->idx;
12594 int i;
12595
12596 /* release any VEBs on this VEB - RECURSION */
12597 for (i = 0; i < I40E_MAX_VEB; i++) {
12598 if (!pf->veb[i])
12599 continue;
12600 if (pf->veb[i]->uplink_seid == branch->seid)
12601 i40e_switch_branch_release(pf->veb[i]);
12602 }
12603
12604 /* Release the VSIs on this VEB, but not the owner VSI.
12605 *
12606 * NOTE: Removing the last VSI on a VEB has the SIDE EFFECT of removing
12607 * the VEB itself, so don't use (*branch) after this loop.
12608 */
Mitch Williams505682c2014-05-20 08:01:37 +000012609 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012610 if (!pf->vsi[i])
12611 continue;
12612 if (pf->vsi[i]->uplink_seid == branch_seid &&
12613 (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
12614 i40e_vsi_release(pf->vsi[i]);
12615 }
12616 }
12617
12618 /* There's one corner case where the VEB might not have been
12619 * removed, so double check it here and remove it if needed.
12620 * This case happens if the veb was created from the debugfs
12621 * commands and no VSIs were added to it.
12622 */
12623 if (pf->veb[veb_idx])
12624 i40e_veb_release(pf->veb[veb_idx]);
12625}
12626
12627/**
12628 * i40e_veb_clear - remove veb struct
12629 * @veb: the veb to remove
12630 **/
12631static void i40e_veb_clear(struct i40e_veb *veb)
12632{
12633 if (!veb)
12634 return;
12635
12636 if (veb->pf) {
12637 struct i40e_pf *pf = veb->pf;
12638
12639 mutex_lock(&pf->switch_mutex);
12640 if (pf->veb[veb->idx] == veb)
12641 pf->veb[veb->idx] = NULL;
12642 mutex_unlock(&pf->switch_mutex);
12643 }
12644
12645 kfree(veb);
12646}
12647
12648/**
12649 * i40e_veb_release - Delete a VEB and free its resources
12650 * @veb: the VEB being removed
12651 **/
12652void i40e_veb_release(struct i40e_veb *veb)
12653{
12654 struct i40e_vsi *vsi = NULL;
12655 struct i40e_pf *pf;
12656 int i, n = 0;
12657
12658 pf = veb->pf;
12659
12660 /* find the remaining VSI and check for extras */
Mitch Williams505682c2014-05-20 08:01:37 +000012661 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012662 if (pf->vsi[i] && pf->vsi[i]->uplink_seid == veb->seid) {
12663 n++;
12664 vsi = pf->vsi[i];
12665 }
12666 }
12667 if (n != 1) {
12668 dev_info(&pf->pdev->dev,
12669 "can't remove VEB %d with %d VSIs left\n",
12670 veb->seid, n);
12671 return;
12672 }
12673
12674 /* move the remaining VSI to uplink veb */
12675 vsi->flags &= ~I40E_VSI_FLAG_VEB_OWNER;
12676 if (veb->uplink_seid) {
12677 vsi->uplink_seid = veb->uplink_seid;
12678 if (veb->uplink_seid == pf->mac_seid)
12679 vsi->veb_idx = I40E_NO_VEB;
12680 else
12681 vsi->veb_idx = veb->veb_idx;
12682 } else {
12683 /* floating VEB */
12684 vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
12685 vsi->veb_idx = pf->vsi[pf->lan_vsi]->veb_idx;
12686 }
12687
12688 i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
12689 i40e_veb_clear(veb);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012690}
12691
12692/**
12693 * i40e_add_veb - create the VEB in the switch
12694 * @veb: the VEB to be instantiated
12695 * @vsi: the controlling VSI
12696 **/
12697static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi)
12698{
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040012699 struct i40e_pf *pf = veb->pf;
Shannon Nelson66fc3602016-01-13 16:51:42 -080012700 bool enable_stats = !!(pf->flags & I40E_FLAG_VEB_STATS_ENABLED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012701 int ret;
12702
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040012703 ret = i40e_aq_add_veb(&pf->hw, veb->uplink_seid, vsi->seid,
Mitch Williams5bc16032016-05-16 10:26:43 -070012704 veb->enabled_tc, false,
Shannon Nelson66fc3602016-01-13 16:51:42 -080012705 &veb->seid, enable_stats, NULL);
Mitch Williams5bc16032016-05-16 10:26:43 -070012706
12707 /* get a VEB from the hardware */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012708 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040012709 dev_info(&pf->pdev->dev,
12710 "couldn't add VEB, err %s aq_err %s\n",
12711 i40e_stat_str(&pf->hw, ret),
12712 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012713 return -EPERM;
12714 }
12715
12716 /* get statistics counter */
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040012717 ret = i40e_aq_get_veb_parameters(&pf->hw, veb->seid, NULL, NULL,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012718 &veb->stats_idx, NULL, NULL, NULL);
12719 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040012720 dev_info(&pf->pdev->dev,
12721 "couldn't get VEB statistics idx, err %s aq_err %s\n",
12722 i40e_stat_str(&pf->hw, ret),
12723 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012724 return -EPERM;
12725 }
12726 ret = i40e_veb_get_bw_info(veb);
12727 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040012728 dev_info(&pf->pdev->dev,
12729 "couldn't get VEB bw info, err %s aq_err %s\n",
12730 i40e_stat_str(&pf->hw, ret),
12731 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
12732 i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012733 return -ENOENT;
12734 }
12735
12736 vsi->uplink_seid = veb->seid;
12737 vsi->veb_idx = veb->idx;
12738 vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
12739
12740 return 0;
12741}
12742
12743/**
12744 * i40e_veb_setup - Set up a VEB
12745 * @pf: board private structure
12746 * @flags: VEB setup flags
12747 * @uplink_seid: the switch element to link to
12748 * @vsi_seid: the initial VSI seid
12749 * @enabled_tc: Enabled TC bit-map
12750 *
12751 * This allocates the sw VEB structure and links it into the switch
12752 * It is possible and legal for this to be a duplicate of an already
12753 * existing VEB. It is also possible for both uplink and vsi seids
12754 * to be zero, in order to create a floating VEB.
12755 *
12756 * Returns pointer to the successfully allocated VEB sw struct on
12757 * success, otherwise returns NULL on failure.
12758 **/
12759struct i40e_veb *i40e_veb_setup(struct i40e_pf *pf, u16 flags,
12760 u16 uplink_seid, u16 vsi_seid,
12761 u8 enabled_tc)
12762{
12763 struct i40e_veb *veb, *uplink_veb = NULL;
12764 int vsi_idx, veb_idx;
12765 int ret;
12766
12767 /* if one seid is 0, the other must be 0 to create a floating relay */
12768 if ((uplink_seid == 0 || vsi_seid == 0) &&
12769 (uplink_seid + vsi_seid != 0)) {
12770 dev_info(&pf->pdev->dev,
12771 "one, not both seid's are 0: uplink=%d vsi=%d\n",
12772 uplink_seid, vsi_seid);
12773 return NULL;
12774 }
12775
12776 /* make sure there is such a vsi and uplink */
Mitch Williams505682c2014-05-20 08:01:37 +000012777 for (vsi_idx = 0; vsi_idx < pf->num_alloc_vsi; vsi_idx++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012778 if (pf->vsi[vsi_idx] && pf->vsi[vsi_idx]->seid == vsi_seid)
12779 break;
Mitch Williams505682c2014-05-20 08:01:37 +000012780 if (vsi_idx >= pf->num_alloc_vsi && vsi_seid != 0) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012781 dev_info(&pf->pdev->dev, "vsi seid %d not found\n",
12782 vsi_seid);
12783 return NULL;
12784 }
12785
12786 if (uplink_seid && uplink_seid != pf->mac_seid) {
12787 for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
12788 if (pf->veb[veb_idx] &&
12789 pf->veb[veb_idx]->seid == uplink_seid) {
12790 uplink_veb = pf->veb[veb_idx];
12791 break;
12792 }
12793 }
12794 if (!uplink_veb) {
12795 dev_info(&pf->pdev->dev,
12796 "uplink seid %d not found\n", uplink_seid);
12797 return NULL;
12798 }
12799 }
12800
12801 /* get veb sw struct */
12802 veb_idx = i40e_veb_mem_alloc(pf);
12803 if (veb_idx < 0)
12804 goto err_alloc;
12805 veb = pf->veb[veb_idx];
12806 veb->flags = flags;
12807 veb->uplink_seid = uplink_seid;
12808 veb->veb_idx = (uplink_veb ? uplink_veb->idx : I40E_NO_VEB);
12809 veb->enabled_tc = (enabled_tc ? enabled_tc : 0x1);
12810
12811 /* create the VEB in the switch */
12812 ret = i40e_add_veb(veb, pf->vsi[vsi_idx]);
12813 if (ret)
12814 goto err_veb;
Shannon Nelson1bb8b932014-04-23 04:49:54 +000012815 if (vsi_idx == pf->lan_vsi)
12816 pf->lan_veb = veb->idx;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012817
12818 return veb;
12819
12820err_veb:
12821 i40e_veb_clear(veb);
12822err_alloc:
12823 return NULL;
12824}
12825
12826/**
Jeff Kirsherb40c82e62015-02-27 09:18:34 +000012827 * i40e_setup_pf_switch_element - set PF vars based on switch type
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012828 * @pf: board private structure
12829 * @ele: element we are building info from
12830 * @num_reported: total number of elements
12831 * @printconfig: should we print the contents
12832 *
12833 * helper function to assist in extracting a few useful SEID values.
12834 **/
12835static void i40e_setup_pf_switch_element(struct i40e_pf *pf,
12836 struct i40e_aqc_switch_config_element_resp *ele,
12837 u16 num_reported, bool printconfig)
12838{
12839 u16 downlink_seid = le16_to_cpu(ele->downlink_seid);
12840 u16 uplink_seid = le16_to_cpu(ele->uplink_seid);
12841 u8 element_type = ele->element_type;
12842 u16 seid = le16_to_cpu(ele->seid);
12843
12844 if (printconfig)
12845 dev_info(&pf->pdev->dev,
12846 "type=%d seid=%d uplink=%d downlink=%d\n",
12847 element_type, seid, uplink_seid, downlink_seid);
12848
12849 switch (element_type) {
12850 case I40E_SWITCH_ELEMENT_TYPE_MAC:
12851 pf->mac_seid = seid;
12852 break;
12853 case I40E_SWITCH_ELEMENT_TYPE_VEB:
12854 /* Main VEB? */
12855 if (uplink_seid != pf->mac_seid)
12856 break;
12857 if (pf->lan_veb == I40E_NO_VEB) {
12858 int v;
12859
12860 /* find existing or else empty VEB */
12861 for (v = 0; v < I40E_MAX_VEB; v++) {
12862 if (pf->veb[v] && (pf->veb[v]->seid == seid)) {
12863 pf->lan_veb = v;
12864 break;
12865 }
12866 }
12867 if (pf->lan_veb == I40E_NO_VEB) {
12868 v = i40e_veb_mem_alloc(pf);
12869 if (v < 0)
12870 break;
12871 pf->lan_veb = v;
12872 }
12873 }
12874
12875 pf->veb[pf->lan_veb]->seid = seid;
12876 pf->veb[pf->lan_veb]->uplink_seid = pf->mac_seid;
12877 pf->veb[pf->lan_veb]->pf = pf;
12878 pf->veb[pf->lan_veb]->veb_idx = I40E_NO_VEB;
12879 break;
12880 case I40E_SWITCH_ELEMENT_TYPE_VSI:
12881 if (num_reported != 1)
12882 break;
12883 /* This is immediately after a reset so we can assume this is
12884 * the PF's VSI
12885 */
12886 pf->mac_seid = uplink_seid;
12887 pf->pf_seid = downlink_seid;
12888 pf->main_vsi_seid = seid;
12889 if (printconfig)
12890 dev_info(&pf->pdev->dev,
12891 "pf_seid=%d main_vsi_seid=%d\n",
12892 pf->pf_seid, pf->main_vsi_seid);
12893 break;
12894 case I40E_SWITCH_ELEMENT_TYPE_PF:
12895 case I40E_SWITCH_ELEMENT_TYPE_VF:
12896 case I40E_SWITCH_ELEMENT_TYPE_EMP:
12897 case I40E_SWITCH_ELEMENT_TYPE_BMC:
12898 case I40E_SWITCH_ELEMENT_TYPE_PE:
12899 case I40E_SWITCH_ELEMENT_TYPE_PA:
12900 /* ignore these for now */
12901 break;
12902 default:
12903 dev_info(&pf->pdev->dev, "unknown element type=%d seid=%d\n",
12904 element_type, seid);
12905 break;
12906 }
12907}
12908
12909/**
12910 * i40e_fetch_switch_configuration - Get switch config from firmware
12911 * @pf: board private structure
12912 * @printconfig: should we print the contents
12913 *
12914 * Get the current switch configuration from the device and
12915 * extract a few useful SEID values.
12916 **/
12917int i40e_fetch_switch_configuration(struct i40e_pf *pf, bool printconfig)
12918{
12919 struct i40e_aqc_get_switch_config_resp *sw_config;
12920 u16 next_seid = 0;
12921 int ret = 0;
12922 u8 *aq_buf;
12923 int i;
12924
12925 aq_buf = kzalloc(I40E_AQ_LARGE_BUF, GFP_KERNEL);
12926 if (!aq_buf)
12927 return -ENOMEM;
12928
12929 sw_config = (struct i40e_aqc_get_switch_config_resp *)aq_buf;
12930 do {
12931 u16 num_reported, num_total;
12932
12933 ret = i40e_aq_get_switch_config(&pf->hw, sw_config,
12934 I40E_AQ_LARGE_BUF,
12935 &next_seid, NULL);
12936 if (ret) {
12937 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040012938 "get switch config failed err %s aq_err %s\n",
12939 i40e_stat_str(&pf->hw, ret),
12940 i40e_aq_str(&pf->hw,
12941 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012942 kfree(aq_buf);
12943 return -ENOENT;
12944 }
12945
12946 num_reported = le16_to_cpu(sw_config->header.num_reported);
12947 num_total = le16_to_cpu(sw_config->header.num_total);
12948
12949 if (printconfig)
12950 dev_info(&pf->pdev->dev,
12951 "header: %d reported %d total\n",
12952 num_reported, num_total);
12953
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012954 for (i = 0; i < num_reported; i++) {
12955 struct i40e_aqc_switch_config_element_resp *ele =
12956 &sw_config->element[i];
12957
12958 i40e_setup_pf_switch_element(pf, ele, num_reported,
12959 printconfig);
12960 }
12961 } while (next_seid != 0);
12962
12963 kfree(aq_buf);
12964 return ret;
12965}
12966
12967/**
12968 * i40e_setup_pf_switch - Setup the HW switch on startup or after reset
12969 * @pf: board private structure
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000012970 * @reinit: if the Main VSI needs to re-initialized.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012971 *
12972 * Returns 0 on success, negative value on failure
12973 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000012974static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012975{
Anjali Singhai Jainb5569892016-05-03 15:13:12 -070012976 u16 flags = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012977 int ret;
12978
12979 /* find out what's out there already */
12980 ret = i40e_fetch_switch_configuration(pf, false);
12981 if (ret) {
12982 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040012983 "couldn't fetch switch config, err %s aq_err %s\n",
12984 i40e_stat_str(&pf->hw, ret),
12985 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012986 return ret;
12987 }
12988 i40e_pf_reset_stats(pf);
12989
Anjali Singhai Jainb5569892016-05-03 15:13:12 -070012990 /* set the switch config bit for the whole device to
12991 * support limited promisc or true promisc
12992 * when user requests promisc. The default is limited
12993 * promisc.
12994 */
12995
12996 if ((pf->hw.pf_id == 0) &&
Amritha Nambiar2f4b4112017-10-27 02:36:01 -070012997 !(pf->flags & I40E_FLAG_TRUE_PROMISC_SUPPORT)) {
Anjali Singhai Jainb5569892016-05-03 15:13:12 -070012998 flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
Amritha Nambiar2f4b4112017-10-27 02:36:01 -070012999 pf->last_sw_conf_flags = flags;
13000 }
Anjali Singhai Jainb5569892016-05-03 15:13:12 -070013001
13002 if (pf->hw.pf_id == 0) {
13003 u16 valid_flags;
13004
13005 valid_flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
Amritha Nambiar5efe0c62017-10-27 02:35:45 -070013006 ret = i40e_aq_set_switch_config(&pf->hw, flags, valid_flags, 0,
Anjali Singhai Jainb5569892016-05-03 15:13:12 -070013007 NULL);
13008 if (ret && pf->hw.aq.asq_last_status != I40E_AQ_RC_ESRCH) {
13009 dev_info(&pf->pdev->dev,
13010 "couldn't set switch config bits, err %s aq_err %s\n",
13011 i40e_stat_str(&pf->hw, ret),
13012 i40e_aq_str(&pf->hw,
13013 pf->hw.aq.asq_last_status));
13014 /* not a fatal problem, just keep going */
13015 }
Amritha Nambiar2f4b4112017-10-27 02:36:01 -070013016 pf->last_sw_conf_valid_flags = valid_flags;
Anjali Singhai Jainb5569892016-05-03 15:13:12 -070013017 }
13018
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013019 /* first time setup */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000013020 if (pf->lan_vsi == I40E_NO_VSI || reinit) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013021 struct i40e_vsi *vsi = NULL;
13022 u16 uplink_seid;
13023
13024 /* Set up the PF VSI associated with the PF's main VSI
13025 * that is already in the HW switch
13026 */
13027 if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
13028 uplink_seid = pf->veb[pf->lan_veb]->seid;
13029 else
13030 uplink_seid = pf->mac_seid;
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000013031 if (pf->lan_vsi == I40E_NO_VSI)
13032 vsi = i40e_vsi_setup(pf, I40E_VSI_MAIN, uplink_seid, 0);
13033 else if (reinit)
13034 vsi = i40e_vsi_reinit_setup(pf->vsi[pf->lan_vsi]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013035 if (!vsi) {
13036 dev_info(&pf->pdev->dev, "setup of MAIN VSI failed\n");
Amritha Nambiaraaf66502017-10-27 02:35:56 -070013037 i40e_cloud_filter_exit(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013038 i40e_fdir_teardown(pf);
13039 return -EAGAIN;
13040 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013041 } else {
13042 /* force a reset of TC and queue layout configurations */
13043 u8 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
Jesse Brandeburg6995b362015-08-28 17:55:54 -040013044
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013045 pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
13046 pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
13047 i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
13048 }
13049 i40e_vlan_stripping_disable(pf->vsi[pf->lan_vsi]);
13050
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080013051 i40e_fdir_sb_setup(pf);
13052
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013053 /* Setup static PF queue filter control settings */
13054 ret = i40e_setup_pf_filter_control(pf);
13055 if (ret) {
13056 dev_info(&pf->pdev->dev, "setup_pf_filter_control failed: %d\n",
13057 ret);
13058 /* Failure here should not stop continuing other steps */
13059 }
13060
13061 /* enable RSS in the HW, even for only one queue, as the stack can use
13062 * the hash
13063 */
13064 if ((pf->flags & I40E_FLAG_RSS_ENABLED))
Helin Zhang043dd652015-10-21 19:56:23 -040013065 i40e_pf_config_rss(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013066
13067 /* fill in link information and enable LSE reporting */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013068 i40e_link_event(pf);
13069
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +000013070 /* Initialize user-specific link properties */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013071 pf->fc_autoneg_status = ((pf->hw.phy.link_info.an_info &
13072 I40E_AQ_AN_COMPLETED) ? true : false);
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +000013073
Jacob Kellerbeb0dff2014-01-11 05:43:19 +000013074 i40e_ptp_init(pf);
13075
Alexander Duyck1f190d92017-04-19 09:25:51 -040013076 /* repopulate tunnel port filters */
13077 i40e_sync_udp_filters(pf);
13078
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013079 return ret;
13080}
13081
13082/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013083 * i40e_determine_queue_usage - Work out queue distribution
13084 * @pf: board private structure
13085 **/
13086static void i40e_determine_queue_usage(struct i40e_pf *pf)
13087{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013088 int queues_left;
Shannon Nelsone50d5752017-07-24 18:17:42 -070013089 int q_max;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013090
13091 pf->num_lan_qps = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013092
13093 /* Find the max queues to be put into basic use. We'll always be
13094 * using TC0, whether or not DCB is running, and TC0 will get the
13095 * big RSS set.
13096 */
13097 queues_left = pf->hw.func_caps.num_tx_qp;
13098
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080013099 if ((queues_left == 1) ||
Frank Zhang9aa7e932014-05-20 08:01:42 +000013100 !(pf->flags & I40E_FLAG_MSIX_ENABLED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013101 /* one qp for PF, no queues for anything else */
13102 queues_left = 0;
Helin Zhangacd65442015-10-26 19:44:28 -040013103 pf->alloc_rss_size = pf->num_lan_qps = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013104
13105 /* make sure all the fancies are disabled */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080013106 pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060013107 I40E_FLAG_IWARP_ENABLED |
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080013108 I40E_FLAG_FD_SB_ENABLED |
13109 I40E_FLAG_FD_ATR_ENABLED |
Neerav Parikh4d9b6042014-05-22 06:31:51 +000013110 I40E_FLAG_DCB_CAPABLE |
Dave Ertmana0362442016-08-29 17:38:26 -070013111 I40E_FLAG_DCB_ENABLED |
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080013112 I40E_FLAG_SRIOV_ENABLED |
13113 I40E_FLAG_VMDQ_ENABLED);
Amritha Nambiar2f4b4112017-10-27 02:36:01 -070013114 pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
Frank Zhang9aa7e932014-05-20 08:01:42 +000013115 } else if (!(pf->flags & (I40E_FLAG_RSS_ENABLED |
13116 I40E_FLAG_FD_SB_ENABLED |
Anjali Singhai Jainbbe7d0e2014-05-20 08:01:44 +000013117 I40E_FLAG_FD_ATR_ENABLED |
Neerav Parikh4d9b6042014-05-22 06:31:51 +000013118 I40E_FLAG_DCB_CAPABLE))) {
Frank Zhang9aa7e932014-05-20 08:01:42 +000013119 /* one qp for PF */
Helin Zhangacd65442015-10-26 19:44:28 -040013120 pf->alloc_rss_size = pf->num_lan_qps = 1;
Frank Zhang9aa7e932014-05-20 08:01:42 +000013121 queues_left -= pf->num_lan_qps;
13122
13123 pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060013124 I40E_FLAG_IWARP_ENABLED |
Frank Zhang9aa7e932014-05-20 08:01:42 +000013125 I40E_FLAG_FD_SB_ENABLED |
13126 I40E_FLAG_FD_ATR_ENABLED |
13127 I40E_FLAG_DCB_ENABLED |
13128 I40E_FLAG_VMDQ_ENABLED);
Amritha Nambiar2f4b4112017-10-27 02:36:01 -070013129 pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013130 } else {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080013131 /* Not enough queues for all TCs */
Neerav Parikh4d9b6042014-05-22 06:31:51 +000013132 if ((pf->flags & I40E_FLAG_DCB_CAPABLE) &&
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080013133 (queues_left < I40E_MAX_TRAFFIC_CLASS)) {
Dave Ertmana0362442016-08-29 17:38:26 -070013134 pf->flags &= ~(I40E_FLAG_DCB_CAPABLE |
13135 I40E_FLAG_DCB_ENABLED);
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080013136 dev_info(&pf->pdev->dev, "not enough queues for DCB. DCB is disabled.\n");
13137 }
Shannon Nelsone50d5752017-07-24 18:17:42 -070013138
13139 /* limit lan qps to the smaller of qps, cpus or msix */
13140 q_max = max_t(int, pf->rss_size_max, num_online_cpus());
13141 q_max = min_t(int, q_max, pf->hw.func_caps.num_tx_qp);
13142 q_max = min_t(int, q_max, pf->hw.func_caps.num_msix_vectors);
13143 pf->num_lan_qps = q_max;
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +000013144
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080013145 queues_left -= pf->num_lan_qps;
13146 }
13147
13148 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
13149 if (queues_left > 1) {
13150 queues_left -= 1; /* save 1 queue for FD */
13151 } else {
13152 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
Amritha Nambiar2f4b4112017-10-27 02:36:01 -070013153 pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080013154 dev_info(&pf->pdev->dev, "not enough queues for Flow Director. Flow Director feature is disabled\n");
13155 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013156 }
13157
13158 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
13159 pf->num_vf_qps && pf->num_req_vfs && queues_left) {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080013160 pf->num_req_vfs = min_t(int, pf->num_req_vfs,
13161 (queues_left / pf->num_vf_qps));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013162 queues_left -= (pf->num_req_vfs * pf->num_vf_qps);
13163 }
13164
13165 if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
13166 pf->num_vmdq_vsis && pf->num_vmdq_qps && queues_left) {
13167 pf->num_vmdq_vsis = min_t(int, pf->num_vmdq_vsis,
13168 (queues_left / pf->num_vmdq_qps));
13169 queues_left -= (pf->num_vmdq_vsis * pf->num_vmdq_qps);
13170 }
13171
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +000013172 pf->queues_left = queues_left;
Neerav Parikh8279e492015-09-03 17:18:50 -040013173 dev_dbg(&pf->pdev->dev,
13174 "qs_avail=%d FD SB=%d lan_qs=%d lan_tc0=%d vf=%d*%d vmdq=%d*%d, remaining=%d\n",
13175 pf->hw.func_caps.num_tx_qp,
13176 !!(pf->flags & I40E_FLAG_FD_SB_ENABLED),
Helin Zhangacd65442015-10-26 19:44:28 -040013177 pf->num_lan_qps, pf->alloc_rss_size, pf->num_req_vfs,
13178 pf->num_vf_qps, pf->num_vmdq_vsis, pf->num_vmdq_qps,
13179 queues_left);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013180}
13181
13182/**
13183 * i40e_setup_pf_filter_control - Setup PF static filter control
13184 * @pf: PF to be setup
13185 *
Jeff Kirsherb40c82e62015-02-27 09:18:34 +000013186 * i40e_setup_pf_filter_control sets up a PF's initial filter control
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013187 * settings. If PE/FCoE are enabled then it will also set the per PF
13188 * based filter sizes required for them. It also enables Flow director,
13189 * ethertype and macvlan type filter settings for the pf.
13190 *
13191 * Returns 0 on success, negative on failure
13192 **/
13193static int i40e_setup_pf_filter_control(struct i40e_pf *pf)
13194{
13195 struct i40e_filter_control_settings *settings = &pf->filter_settings;
13196
13197 settings->hash_lut_size = I40E_HASH_LUT_SIZE_128;
13198
13199 /* Flow Director is enabled */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080013200 if (pf->flags & (I40E_FLAG_FD_SB_ENABLED | I40E_FLAG_FD_ATR_ENABLED))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013201 settings->enable_fdir = true;
13202
13203 /* Ethtype and MACVLAN filters enabled for PF */
13204 settings->enable_ethtype = true;
13205 settings->enable_macvlan = true;
13206
13207 if (i40e_set_filter_control(&pf->hw, settings))
13208 return -ENOENT;
13209
13210 return 0;
13211}
13212
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000013213#define INFO_STRING_LEN 255
Shannon Nelson7fd89542015-10-21 19:47:04 -040013214#define REMAIN(__x) (INFO_STRING_LEN - (__x))
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000013215static void i40e_print_features(struct i40e_pf *pf)
13216{
13217 struct i40e_hw *hw = &pf->hw;
Joe Perches3b195842015-12-03 04:20:57 -080013218 char *buf;
13219 int i;
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000013220
Joe Perches3b195842015-12-03 04:20:57 -080013221 buf = kmalloc(INFO_STRING_LEN, GFP_KERNEL);
13222 if (!buf)
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000013223 return;
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000013224
Joe Perches3b195842015-12-03 04:20:57 -080013225 i = snprintf(buf, INFO_STRING_LEN, "Features: PF-id[%d]", hw->pf_id);
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000013226#ifdef CONFIG_PCI_IOV
Joe Perches3b195842015-12-03 04:20:57 -080013227 i += snprintf(&buf[i], REMAIN(i), " VFs: %d", pf->num_req_vfs);
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000013228#endif
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -070013229 i += snprintf(&buf[i], REMAIN(i), " VSIs: %d QP: %d",
Shannon Nelson7fd89542015-10-21 19:47:04 -040013230 pf->hw.func_caps.num_vsis,
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -070013231 pf->vsi[pf->lan_vsi]->num_queue_pairs);
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000013232 if (pf->flags & I40E_FLAG_RSS_ENABLED)
Joe Perches3b195842015-12-03 04:20:57 -080013233 i += snprintf(&buf[i], REMAIN(i), " RSS");
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000013234 if (pf->flags & I40E_FLAG_FD_ATR_ENABLED)
Joe Perches3b195842015-12-03 04:20:57 -080013235 i += snprintf(&buf[i], REMAIN(i), " FD_ATR");
Akeem G Abodunrinc6423ff2014-05-10 04:49:08 +000013236 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
Joe Perches3b195842015-12-03 04:20:57 -080013237 i += snprintf(&buf[i], REMAIN(i), " FD_SB");
13238 i += snprintf(&buf[i], REMAIN(i), " NTUPLE");
Akeem G Abodunrinc6423ff2014-05-10 04:49:08 +000013239 }
Neerav Parikh4d9b6042014-05-22 06:31:51 +000013240 if (pf->flags & I40E_FLAG_DCB_CAPABLE)
Joe Perches3b195842015-12-03 04:20:57 -080013241 i += snprintf(&buf[i], REMAIN(i), " DCB");
Joe Perches3b195842015-12-03 04:20:57 -080013242 i += snprintf(&buf[i], REMAIN(i), " VxLAN");
Singhai, Anjali6a899022015-12-14 12:21:18 -080013243 i += snprintf(&buf[i], REMAIN(i), " Geneve");
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000013244 if (pf->flags & I40E_FLAG_PTP)
Joe Perches3b195842015-12-03 04:20:57 -080013245 i += snprintf(&buf[i], REMAIN(i), " PTP");
Shannon Nelson6dec1012015-09-28 14:12:30 -040013246 if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
Joe Perches3b195842015-12-03 04:20:57 -080013247 i += snprintf(&buf[i], REMAIN(i), " VEB");
Shannon Nelson6dec1012015-09-28 14:12:30 -040013248 else
Joe Perches3b195842015-12-03 04:20:57 -080013249 i += snprintf(&buf[i], REMAIN(i), " VEPA");
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000013250
Joe Perches3b195842015-12-03 04:20:57 -080013251 dev_info(&pf->pdev->dev, "%s\n", buf);
13252 kfree(buf);
Shannon Nelson7fd89542015-10-21 19:47:04 -040013253 WARN_ON(i > INFO_STRING_LEN);
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000013254}
13255
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013256/**
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050013257 * i40e_get_platform_mac_addr - get platform-specific MAC address
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050013258 * @pdev: PCI device information struct
13259 * @pf: board private structure
13260 *
Jacob Keller41c4c2b2017-04-05 07:50:57 -040013261 * Look up the MAC address for the device. First we'll try
13262 * eth_platform_get_mac_address, which will check Open Firmware, or arch
13263 * specific fallback. Otherwise, we'll default to the stored value in
13264 * firmware.
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050013265 **/
13266static void i40e_get_platform_mac_addr(struct pci_dev *pdev, struct i40e_pf *pf)
13267{
Jacob Keller41c4c2b2017-04-05 07:50:57 -040013268 if (eth_platform_get_mac_address(&pdev->dev, pf->hw.mac.addr))
13269 i40e_get_mac_addr(&pf->hw, pf->hw.mac.addr);
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050013270}
13271
13272/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013273 * i40e_probe - Device initialization routine
13274 * @pdev: PCI device information struct
13275 * @ent: entry in i40e_pci_tbl
13276 *
Jeff Kirsherb40c82e62015-02-27 09:18:34 +000013277 * i40e_probe initializes a PF identified by a pci_dev structure.
13278 * The OS initialization, configuring of the PF private structure,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013279 * and a hardware reset occur.
13280 *
13281 * Returns 0 on success, negative on failure
13282 **/
13283static int i40e_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
13284{
Catherine Sullivane8278452015-02-06 08:52:08 +000013285 struct i40e_aq_get_phy_abilities_resp abilities;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013286 struct i40e_pf *pf;
13287 struct i40e_hw *hw;
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +000013288 static u16 pfs_found;
Shannon Nelson1d5109d2015-08-26 15:14:08 -040013289 u16 wol_nvm_bits;
Catherine Sullivand4dfb812013-11-28 06:39:21 +000013290 u16 link_status;
Jean Sacren6f66a482015-09-19 05:08:45 -060013291 int err;
Anjali Singhai Jain4f2f017c2015-10-21 19:47:07 -040013292 u32 val;
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +000013293 u32 i;
Helin Zhang58fc3262015-10-01 14:37:38 -040013294 u8 set_fc_aq_fail;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013295
13296 err = pci_enable_device_mem(pdev);
13297 if (err)
13298 return err;
13299
13300 /* set up for high or low dma */
Mitch Williams64942942014-02-11 08:26:33 +000013301 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
Mitch Williams64942942014-02-11 08:26:33 +000013302 if (err) {
Jean Sacrene3e3bfd2014-03-25 04:30:27 +000013303 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
13304 if (err) {
13305 dev_err(&pdev->dev,
13306 "DMA configuration failed: 0x%x\n", err);
13307 goto err_dma;
13308 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013309 }
13310
13311 /* set up pci connections */
Johannes Thumshirn56d766d2016-06-07 09:44:05 +020013312 err = pci_request_mem_regions(pdev, i40e_driver_name);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013313 if (err) {
13314 dev_info(&pdev->dev,
13315 "pci_request_selected_regions failed %d\n", err);
13316 goto err_pci_reg;
13317 }
13318
13319 pci_enable_pcie_error_reporting(pdev);
13320 pci_set_master(pdev);
13321
13322 /* Now that we have a PCI connection, we need to do the
13323 * low level device setup. This is primarily setting up
13324 * the Admin Queue structures and then querying for the
13325 * device's current profile information.
13326 */
13327 pf = kzalloc(sizeof(*pf), GFP_KERNEL);
13328 if (!pf) {
13329 err = -ENOMEM;
13330 goto err_pf_alloc;
13331 }
13332 pf->next_vsi = 0;
13333 pf->pdev = pdev;
Mauro S. M. Rodrigues9e6c9c02017-05-12 23:26:56 -030013334 set_bit(__I40E_DOWN, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013335
13336 hw = &pf->hw;
13337 hw->back = pf;
Anjali Singhai232f4702015-02-26 16:15:39 +000013338
Shannon Nelson2ac8b672015-07-23 16:54:37 -040013339 pf->ioremap_len = min_t(int, pci_resource_len(pdev, 0),
13340 I40E_MAX_CSR_SPACE);
Anjali Singhai232f4702015-02-26 16:15:39 +000013341
Shannon Nelson2ac8b672015-07-23 16:54:37 -040013342 hw->hw_addr = ioremap(pci_resource_start(pdev, 0), pf->ioremap_len);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013343 if (!hw->hw_addr) {
13344 err = -EIO;
13345 dev_info(&pdev->dev, "ioremap(0x%04x, 0x%04x) failed: 0x%x\n",
13346 (unsigned int)pci_resource_start(pdev, 0),
Shannon Nelson2ac8b672015-07-23 16:54:37 -040013347 pf->ioremap_len, err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013348 goto err_ioremap;
13349 }
13350 hw->vendor_id = pdev->vendor;
13351 hw->device_id = pdev->device;
13352 pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
13353 hw->subsystem_vendor_id = pdev->subsystem_vendor;
13354 hw->subsystem_device_id = pdev->subsystem_device;
13355 hw->bus.device = PCI_SLOT(pdev->devfn);
13356 hw->bus.func = PCI_FUNC(pdev->devfn);
Sudheer Mogilappagarib3f028f2017-02-09 23:58:22 -080013357 hw->bus.bus_id = pdev->bus->number;
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +000013358 pf->instance = pfs_found;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013359
Scott Petersonab243ec2017-08-22 06:57:54 -040013360 /* Select something other than the 802.1ad ethertype for the
13361 * switch to use internally and drop on ingress.
13362 */
13363 hw->switch_tag = 0xffff;
13364 hw->first_tag = ETH_P_8021AD;
13365 hw->second_tag = ETH_P_8021Q;
13366
Jacob Keller0e588de2017-02-06 14:38:50 -080013367 INIT_LIST_HEAD(&pf->l3_flex_pit_list);
13368 INIT_LIST_HEAD(&pf->l4_flex_pit_list);
13369
Shannon Nelsonde03d2b2016-03-10 14:59:44 -080013370 /* set up the locks for the AQ, do this only once in probe
13371 * and destroy them only once in remove
13372 */
13373 mutex_init(&hw->aq.asq_mutex);
13374 mutex_init(&hw->aq.arq_mutex);
13375
Alexander Duyck5d4ca232016-09-30 08:21:46 -040013376 pf->msg_enable = netif_msg_init(debug,
13377 NETIF_MSG_DRV |
13378 NETIF_MSG_PROBE |
13379 NETIF_MSG_LINK);
13380 if (debug < -1)
13381 pf->hw.debug_mask = debug;
Shannon Nelson5b5faa42014-10-17 03:14:51 +000013382
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +000013383 /* do a special CORER for clearing PXE mode once at init */
13384 if (hw->revision_id == 0 &&
13385 (rd32(hw, I40E_GLLAN_RCTL_0) & I40E_GLLAN_RCTL_0_PXE_MODE_MASK)) {
13386 wr32(hw, I40E_GLGEN_RTRIG, I40E_GLGEN_RTRIG_CORER_MASK);
13387 i40e_flush(hw);
13388 msleep(200);
13389 pf->corer_count++;
13390
13391 i40e_clear_pxe_mode(hw);
13392 }
13393
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013394 /* Reset here to make sure all is clean and to define PF 'n' */
Shannon Nelson838d41d2014-06-04 20:41:27 +000013395 i40e_clear_hw(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013396 err = i40e_pf_reset(hw);
13397 if (err) {
13398 dev_info(&pdev->dev, "Initial pf_reset failed: %d\n", err);
13399 goto err_pf_reset;
13400 }
13401 pf->pfr_count++;
13402
13403 hw->aq.num_arq_entries = I40E_AQ_LEN;
13404 hw->aq.num_asq_entries = I40E_AQ_LEN;
13405 hw->aq.arq_buf_size = I40E_MAX_AQ_BUF_SIZE;
13406 hw->aq.asq_buf_size = I40E_MAX_AQ_BUF_SIZE;
13407 pf->adminq_work_limit = I40E_AQ_WORK_LIMIT;
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +000013408
Carolyn Wybornyb294ac72014-12-11 07:06:39 +000013409 snprintf(pf->int_name, sizeof(pf->int_name) - 1,
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +000013410 "%s-%s:misc",
13411 dev_driver_string(&pf->pdev->dev), dev_name(&pdev->dev));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013412
13413 err = i40e_init_shared_code(hw);
13414 if (err) {
Anjali Singhai Jainb2a75c52015-04-27 14:57:20 -040013415 dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
13416 err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013417 goto err_pf_reset;
13418 }
13419
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +000013420 /* set up a default setting for link flow control */
13421 pf->hw.fc.requested_mode = I40E_FC_NONE;
13422
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013423 err = i40e_init_adminq(hw);
Carolyn Wyborny2b2426a762015-10-26 19:44:35 -040013424 if (err) {
13425 if (err == I40E_ERR_FIRMWARE_API_VERSION)
13426 dev_info(&pdev->dev,
13427 "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");
13428 else
13429 dev_info(&pdev->dev,
13430 "The driver for the device stopped because the device firmware failed to init. Try updating your NVM image.\n");
13431
13432 goto err_pf_reset;
13433 }
Filip Sadowski5bbb2e22017-06-07 05:43:09 -040013434 i40e_get_oem_version(hw);
Carolyn Wybornyf0b44442015-08-31 19:54:49 -040013435
Shannon Nelson6dec1012015-09-28 14:12:30 -040013436 /* provide nvm, fw, api versions */
13437 dev_info(&pdev->dev, "fw %d.%d.%05d api %d.%d nvm %s\n",
13438 hw->aq.fw_maj_ver, hw->aq.fw_min_ver, hw->aq.fw_build,
13439 hw->aq.api_maj_ver, hw->aq.api_min_ver,
13440 i40e_nvm_version_str(hw));
Carolyn Wybornyf0b44442015-08-31 19:54:49 -040013441
Catherine Sullivan7aa67612014-07-09 07:46:17 +000013442 if (hw->aq.api_maj_ver == I40E_FW_API_VERSION_MAJOR &&
Mitch Williams22b965512017-07-14 09:27:09 -040013443 hw->aq.api_min_ver > I40E_FW_MINOR_VERSION(hw))
Shannon Nelson278b6f62014-06-04 01:41:03 +000013444 dev_info(&pdev->dev,
Catherine Sullivan7aa67612014-07-09 07:46:17 +000013445 "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 -040013446 else if (hw->aq.api_maj_ver == 1 && hw->aq.api_min_ver < 4)
Shannon Nelson278b6f62014-06-04 01:41:03 +000013447 dev_info(&pdev->dev,
Catherine Sullivan7aa67612014-07-09 07:46:17 +000013448 "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 +000013449
Shannon Nelson4eb3f762014-03-06 08:59:58 +000013450 i40e_verify_eeprom(pf);
13451
Jesse Brandeburg2c5fe332014-04-23 04:49:57 +000013452 /* Rev 0 hardware was never productized */
13453 if (hw->revision_id < 1)
13454 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");
13455
Shannon Nelson6ff4ef82013-12-21 05:44:49 +000013456 i40e_clear_pxe_mode(hw);
Amritha Nambiar2f4b4112017-10-27 02:36:01 -070013457 err = i40e_get_capabilities(pf, i40e_aqc_opc_list_func_capabilities);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013458 if (err)
13459 goto err_adminq_setup;
13460
13461 err = i40e_sw_init(pf);
13462 if (err) {
13463 dev_info(&pdev->dev, "sw_init failed: %d\n", err);
13464 goto err_sw_init;
13465 }
13466
13467 err = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
Alexander Duyckc76cb6e2017-02-21 15:55:48 -080013468 hw->func_caps.num_rx_qp, 0, 0);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013469 if (err) {
13470 dev_info(&pdev->dev, "init_lan_hmc failed: %d\n", err);
13471 goto err_init_lan_hmc;
13472 }
13473
13474 err = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
13475 if (err) {
13476 dev_info(&pdev->dev, "configure_lan_hmc failed: %d\n", err);
13477 err = -ENOENT;
13478 goto err_configure_lan_hmc;
13479 }
13480
Neerav Parikhb686ece2014-12-14 01:55:11 +000013481 /* Disable LLDP for NICs that have firmware versions lower than v4.3.
13482 * Ignore error return codes because if it was already disabled via
13483 * hardware settings this will fail
13484 */
Jacob Kellerd36e41d2017-06-23 04:24:46 -040013485 if (pf->hw_features & I40E_HW_STOP_FW_LLDP) {
Neerav Parikhb686ece2014-12-14 01:55:11 +000013486 dev_info(&pdev->dev, "Stopping firmware LLDP agent.\n");
13487 i40e_aq_stop_lldp(hw, true, NULL);
13488 }
13489
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050013490 /* allow a platform config to override the HW addr */
13491 i40e_get_platform_mac_addr(pdev, pf);
Jacob Keller41c4c2b2017-04-05 07:50:57 -040013492
Jesse Brandeburgf62b5062013-11-28 06:39:27 +000013493 if (!is_valid_ether_addr(hw->mac.addr)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013494 dev_info(&pdev->dev, "invalid MAC address %pM\n", hw->mac.addr);
13495 err = -EIO;
13496 goto err_mac_addr;
13497 }
13498 dev_info(&pdev->dev, "MAC address: %pM\n", hw->mac.addr);
Greg Rose9a173902014-05-22 06:32:02 +000013499 ether_addr_copy(hw->mac.perm_addr, hw->mac.addr);
Neerav Parikh1f224ad2014-02-12 01:45:31 +000013500 i40e_get_port_mac_addr(hw, hw->mac.port_addr);
13501 if (is_valid_ether_addr(hw->mac.port_addr))
Jacob Kellerd36e41d2017-06-23 04:24:46 -040013502 pf->hw_features |= I40E_HW_PORT_ID_VALID;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013503
13504 pci_set_drvdata(pdev, pf);
13505 pci_save_state(pdev);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -080013506#ifdef CONFIG_I40E_DCB
13507 err = i40e_init_pf_dcb(pf);
13508 if (err) {
Shannon Nelsonaebfc812014-12-11 07:06:38 +000013509 dev_info(&pdev->dev, "DCB init failed %d, disabled\n", err);
David Ertmanc17ef432016-09-30 01:36:21 -070013510 pf->flags &= ~(I40E_FLAG_DCB_CAPABLE | I40E_FLAG_DCB_ENABLED);
Neerav Parikh014269f2014-04-01 07:11:48 +000013511 /* Continue without DCB enabled */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -080013512 }
13513#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013514
13515 /* set up periodic task facility */
Kees Cook26566ea2017-10-16 17:29:35 -070013516 timer_setup(&pf->service_timer, i40e_service_timer, 0);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013517 pf->service_timer_period = HZ;
13518
13519 INIT_WORK(&pf->service_task, i40e_service_task);
Jacob Keller0da36b92017-04-19 09:25:55 -040013520 clear_bit(__I40E_SERVICE_SCHED, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013521
Shannon Nelson1d5109d2015-08-26 15:14:08 -040013522 /* NVM bit on means WoL disabled for the port */
13523 i40e_read_nvm_word(hw, I40E_SR_NVM_WAKE_ON_LAN, &wol_nvm_bits);
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -080013524 if (BIT (hw->port) & wol_nvm_bits || hw->partition_id != 1)
Shannon Nelson1d5109d2015-08-26 15:14:08 -040013525 pf->wol_en = false;
13526 else
13527 pf->wol_en = true;
Shannon Nelson8e2773a2013-11-28 06:39:22 +000013528 device_set_wakeup_enable(&pf->pdev->dev, pf->wol_en);
13529
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013530 /* set up the main switch operations */
13531 i40e_determine_queue_usage(pf);
Jesse Brandeburgc11472802015-04-07 19:45:39 -040013532 err = i40e_init_interrupt_scheme(pf);
13533 if (err)
13534 goto err_switch_setup;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013535
Mitch Williams505682c2014-05-20 08:01:37 +000013536 /* The number of VSIs reported by the FW is the minimum guaranteed
13537 * to us; HW supports far more and we share the remaining pool with
13538 * the other PFs. We allocate space for more than the guarantee with
13539 * the understanding that we might not get them all later.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013540 */
Mitch Williams505682c2014-05-20 08:01:37 +000013541 if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
13542 pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
13543 else
13544 pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
13545
13546 /* Set up the *vsi struct and our local tracking of the MAIN PF vsi. */
Jesse Brandeburgd17038d2015-12-23 12:05:55 -080013547 pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
13548 GFP_KERNEL);
Wei Yongjuned87ac02013-09-24 05:17:25 +000013549 if (!pf->vsi) {
13550 err = -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013551 goto err_switch_setup;
Wei Yongjuned87ac02013-09-24 05:17:25 +000013552 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013553
Anjali Singhai Jainfa11cb32015-05-27 12:06:14 -040013554#ifdef CONFIG_PCI_IOV
13555 /* prep for VF support */
13556 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
13557 (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
Jacob Keller0da36b92017-04-19 09:25:55 -040013558 !test_bit(__I40E_BAD_EEPROM, pf->state)) {
Anjali Singhai Jainfa11cb32015-05-27 12:06:14 -040013559 if (pci_num_vf(pdev))
13560 pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
13561 }
13562#endif
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000013563 err = i40e_setup_pf_switch(pf, false);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013564 if (err) {
13565 dev_info(&pdev->dev, "setup_pf_switch failed: %d\n", err);
13566 goto err_vsis;
13567 }
Amritha Nambiar8f88b302017-09-07 04:00:17 -070013568 INIT_LIST_HEAD(&pf->vsi[pf->lan_vsi]->ch_list);
Helin Zhang58fc3262015-10-01 14:37:38 -040013569
13570 /* Make sure flow control is set according to current settings */
13571 err = i40e_set_fc(hw, &set_fc_aq_fail, true);
13572 if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_GET)
13573 dev_dbg(&pf->pdev->dev,
13574 "Set fc with err %s aq_err %s on get_phy_cap\n",
13575 i40e_stat_str(hw, err),
13576 i40e_aq_str(hw, hw->aq.asq_last_status));
13577 if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_SET)
13578 dev_dbg(&pf->pdev->dev,
13579 "Set fc with err %s aq_err %s on set_phy_config\n",
13580 i40e_stat_str(hw, err),
13581 i40e_aq_str(hw, hw->aq.asq_last_status));
13582 if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_UPDATE)
13583 dev_dbg(&pf->pdev->dev,
13584 "Set fc with err %s aq_err %s on get_link_info\n",
13585 i40e_stat_str(hw, err),
13586 i40e_aq_str(hw, hw->aq.asq_last_status));
13587
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +000013588 /* if FDIR VSI was set up, start it now */
Mitch Williams505682c2014-05-20 08:01:37 +000013589 for (i = 0; i < pf->num_alloc_vsi; i++) {
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +000013590 if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
13591 i40e_vsi_open(pf->vsi[i]);
13592 break;
13593 }
13594 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013595
Shannon Nelson2f0aff42016-01-04 10:33:08 -080013596 /* The driver only wants link up/down and module qualification
13597 * reports from firmware. Note the negative logic.
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +000013598 */
13599 err = i40e_aq_set_phy_int_mask(&pf->hw,
Shannon Nelson2f0aff42016-01-04 10:33:08 -080013600 ~(I40E_AQ_EVENT_LINK_UPDOWN |
Shannon Nelson867a79e2016-03-18 12:18:15 -070013601 I40E_AQ_EVENT_MEDIA_NA |
Shannon Nelson2f0aff42016-01-04 10:33:08 -080013602 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +000013603 if (err)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040013604 dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
13605 i40e_stat_str(&pf->hw, err),
13606 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +000013607
Anjali Singhai Jain4f2f017c2015-10-21 19:47:07 -040013608 /* Reconfigure hardware for allowing smaller MSS in the case
13609 * of TSO, so that we avoid the MDD being fired and causing
13610 * a reset in the case of small MSS+TSO.
13611 */
13612 val = rd32(hw, I40E_REG_MSS);
13613 if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
13614 val &= ~I40E_REG_MSS_MIN_MASK;
13615 val |= I40E_64BYTE_MSS;
13616 wr32(hw, I40E_REG_MSS, val);
13617 }
13618
Jacob Kellerd36e41d2017-06-23 04:24:46 -040013619 if (pf->hw_features & I40E_HW_RESTART_AUTONEG) {
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +000013620 msleep(75);
13621 err = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
13622 if (err)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040013623 dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
13624 i40e_stat_str(&pf->hw, err),
13625 i40e_aq_str(&pf->hw,
13626 pf->hw.aq.asq_last_status));
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +000013627 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013628 /* The main driver is (mostly) up and happy. We need to set this state
13629 * before setting up the misc vector or we get a race and the vector
13630 * ends up disabled forever.
13631 */
Mauro S. M. Rodrigues9e6c9c02017-05-12 23:26:56 -030013632 clear_bit(__I40E_DOWN, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013633
13634 /* In case of MSIX we are going to setup the misc vector right here
13635 * to handle admin queue events etc. In case of legacy and MSI
13636 * the misc functionality and queue processing is combined in
13637 * the same vector and that gets setup at open.
13638 */
13639 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
13640 err = i40e_setup_misc_vector(pf);
13641 if (err) {
13642 dev_info(&pdev->dev,
13643 "setup of misc vector failed: %d\n", err);
13644 goto err_vsis;
13645 }
13646 }
13647
Greg Rosedf805f62014-04-04 04:43:16 +000013648#ifdef CONFIG_PCI_IOV
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013649 /* prep for VF support */
13650 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
Shannon Nelson4eb3f762014-03-06 08:59:58 +000013651 (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
Jacob Keller0da36b92017-04-19 09:25:55 -040013652 !test_bit(__I40E_BAD_EEPROM, pf->state)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013653 /* disable link interrupts for VFs */
13654 val = rd32(hw, I40E_PFGEN_PORTMDIO_NUM);
13655 val &= ~I40E_PFGEN_PORTMDIO_NUM_VFLINK_STAT_ENA_MASK;
13656 wr32(hw, I40E_PFGEN_PORTMDIO_NUM, val);
13657 i40e_flush(hw);
Mitch Williams4aeec012014-02-13 03:48:47 -080013658
13659 if (pci_num_vf(pdev)) {
13660 dev_info(&pdev->dev,
13661 "Active VFs found, allocating resources.\n");
13662 err = i40e_alloc_vfs(pf, pci_num_vf(pdev));
13663 if (err)
13664 dev_info(&pdev->dev,
13665 "Error %d allocating resources for existing VFs\n",
13666 err);
13667 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013668 }
Greg Rosedf805f62014-04-04 04:43:16 +000013669#endif /* CONFIG_PCI_IOV */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013670
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060013671 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
13672 pf->iwarp_base_vector = i40e_get_lump(pf, pf->irq_pile,
13673 pf->num_iwarp_msix,
13674 I40E_IWARP_IRQ_PILE_ID);
13675 if (pf->iwarp_base_vector < 0) {
13676 dev_info(&pdev->dev,
13677 "failed to get tracking for %d vectors for IWARP err=%d\n",
13678 pf->num_iwarp_msix, pf->iwarp_base_vector);
13679 pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
13680 }
13681 }
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +000013682
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013683 i40e_dbg_pf_init(pf);
13684
13685 /* tell the firmware that we're starting */
Jesse Brandeburg44033fa2014-04-23 04:50:15 +000013686 i40e_send_version(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013687
13688 /* since everything's happy, start the service_task timer */
13689 mod_timer(&pf->service_timer,
13690 round_jiffies(jiffies + pf->service_timer_period));
13691
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060013692 /* add this PF to client device list and launch a client service task */
Mitch Williams004eb612017-04-04 12:40:16 -070013693 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
13694 err = i40e_lan_add_device(pf);
13695 if (err)
13696 dev_info(&pdev->dev, "Failed to add PF to client API service list: %d\n",
13697 err);
13698 }
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060013699
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040013700#define PCI_SPEED_SIZE 8
13701#define PCI_WIDTH_SIZE 8
13702 /* Devices on the IOSF bus do not have this information
13703 * and will report PCI Gen 1 x 1 by default so don't bother
13704 * checking them.
13705 */
Jacob Kellerd36e41d2017-06-23 04:24:46 -040013706 if (!(pf->hw_features & I40E_HW_NO_PCI_LINK_CHECK)) {
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040013707 char speed[PCI_SPEED_SIZE] = "Unknown";
13708 char width[PCI_WIDTH_SIZE] = "Unknown";
Catherine Sullivand4dfb812013-11-28 06:39:21 +000013709
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040013710 /* Get the negotiated link width and speed from PCI config
13711 * space
13712 */
13713 pcie_capability_read_word(pf->pdev, PCI_EXP_LNKSTA,
13714 &link_status);
Catherine Sullivand4dfb812013-11-28 06:39:21 +000013715
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040013716 i40e_set_pci_config_data(hw, link_status);
Catherine Sullivand4dfb812013-11-28 06:39:21 +000013717
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040013718 switch (hw->bus.speed) {
13719 case i40e_bus_speed_8000:
13720 strncpy(speed, "8.0", PCI_SPEED_SIZE); break;
13721 case i40e_bus_speed_5000:
13722 strncpy(speed, "5.0", PCI_SPEED_SIZE); break;
13723 case i40e_bus_speed_2500:
13724 strncpy(speed, "2.5", PCI_SPEED_SIZE); break;
13725 default:
13726 break;
13727 }
13728 switch (hw->bus.width) {
13729 case i40e_bus_width_pcie_x8:
13730 strncpy(width, "8", PCI_WIDTH_SIZE); break;
13731 case i40e_bus_width_pcie_x4:
13732 strncpy(width, "4", PCI_WIDTH_SIZE); break;
13733 case i40e_bus_width_pcie_x2:
13734 strncpy(width, "2", PCI_WIDTH_SIZE); break;
13735 case i40e_bus_width_pcie_x1:
13736 strncpy(width, "1", PCI_WIDTH_SIZE); break;
13737 default:
13738 break;
13739 }
13740
13741 dev_info(&pdev->dev, "PCI-Express: Speed %sGT/s Width x%s\n",
13742 speed, width);
13743
13744 if (hw->bus.width < i40e_bus_width_pcie_x8 ||
13745 hw->bus.speed < i40e_bus_speed_8000) {
13746 dev_warn(&pdev->dev, "PCI-Express bandwidth available for this device may be insufficient for optimal performance.\n");
13747 dev_warn(&pdev->dev, "Please move the device to a different PCI-e link with more lanes and/or higher transfer rate.\n");
13748 }
Catherine Sullivand4dfb812013-11-28 06:39:21 +000013749 }
13750
Catherine Sullivane8278452015-02-06 08:52:08 +000013751 /* get the requested speeds from the fw */
13752 err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities, NULL);
13753 if (err)
Neerav Parikh8279e492015-09-03 17:18:50 -040013754 dev_dbg(&pf->pdev->dev, "get requested speeds ret = %s last_status = %s\n",
13755 i40e_stat_str(&pf->hw, err),
13756 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Catherine Sullivane8278452015-02-06 08:52:08 +000013757 pf->hw.phy.link_info.requested_speeds = abilities.link_speed;
13758
Catherine Sullivanfc72dbc2015-09-01 11:36:30 -040013759 /* get the supported phy types from the fw */
13760 err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities, NULL);
13761 if (err)
13762 dev_dbg(&pf->pdev->dev, "get supported phy types ret = %s last_status = %s\n",
13763 i40e_stat_str(&pf->hw, err),
13764 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Catherine Sullivanfc72dbc2015-09-01 11:36:30 -040013765
Anjali Singhai Jaine7358f52015-10-01 14:37:34 -040013766 /* Add a filter to drop all Flow control frames from any VSI from being
13767 * transmitted. By doing so we stop a malicious VF from sending out
13768 * PAUSE or PFC frames and potentially controlling traffic for other
13769 * PF/VF VSIs.
13770 * The FW can still send Flow control frames if enabled.
13771 */
13772 i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
13773 pf->main_vsi_seid);
13774
Carolyn Wyborny31b606d2016-02-17 16:12:12 -080013775 if ((pf->hw.device_id == I40E_DEV_ID_10G_BASE_T) ||
Henry Tieman4f9b4302016-11-08 13:05:18 -080013776 (pf->hw.device_id == I40E_DEV_ID_10G_BASE_T4))
Jacob Kellerd36e41d2017-06-23 04:24:46 -040013777 pf->hw_features |= I40E_HW_PHY_CONTROLS_LEDS;
Harshitha Ramamurthy4ad9f4f2016-11-08 13:05:09 -080013778 if (pf->hw.device_id == I40E_DEV_ID_SFP_I_X722)
Jacob Kellerd36e41d2017-06-23 04:24:46 -040013779 pf->hw_features |= I40E_HW_HAVE_CRT_RETIMER;
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000013780 /* print a string summarizing features */
13781 i40e_print_features(pf);
13782
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013783 return 0;
13784
13785 /* Unwind what we've done if something failed in the setup */
13786err_vsis:
Mauro S. M. Rodrigues9e6c9c02017-05-12 23:26:56 -030013787 set_bit(__I40E_DOWN, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013788 i40e_clear_interrupt_scheme(pf);
13789 kfree(pf->vsi);
Shannon Nelson04b03012013-11-28 06:39:34 +000013790err_switch_setup:
13791 i40e_reset_interrupt_capability(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013792 del_timer_sync(&pf->service_timer);
13793err_mac_addr:
13794err_configure_lan_hmc:
13795 (void)i40e_shutdown_lan_hmc(hw);
13796err_init_lan_hmc:
13797 kfree(pf->qp_pile);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013798err_sw_init:
13799err_adminq_setup:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013800err_pf_reset:
13801 iounmap(hw->hw_addr);
13802err_ioremap:
13803 kfree(pf);
13804err_pf_alloc:
13805 pci_disable_pcie_error_reporting(pdev);
Johannes Thumshirn56d766d2016-06-07 09:44:05 +020013806 pci_release_mem_regions(pdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013807err_pci_reg:
13808err_dma:
13809 pci_disable_device(pdev);
13810 return err;
13811}
13812
13813/**
13814 * i40e_remove - Device removal routine
13815 * @pdev: PCI device information struct
13816 *
13817 * i40e_remove is called by the PCI subsystem to alert the driver
13818 * that is should release a PCI device. This could be caused by a
13819 * Hot-Plug event, or because the driver is going to be removed from
13820 * memory.
13821 **/
13822static void i40e_remove(struct pci_dev *pdev)
13823{
13824 struct i40e_pf *pf = pci_get_drvdata(pdev);
Carolyn Wybornybcab2db2015-09-28 14:16:55 -040013825 struct i40e_hw *hw = &pf->hw;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013826 i40e_status ret_code;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013827 int i;
13828
13829 i40e_dbg_pf_exit(pf);
13830
Jacob Kellerbeb0dff2014-01-11 05:43:19 +000013831 i40e_ptp_stop(pf);
13832
Carolyn Wybornybcab2db2015-09-28 14:16:55 -040013833 /* Disable RSS in hw */
Shannon Nelson272cdaf22016-02-17 16:12:21 -080013834 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), 0);
13835 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), 0);
Carolyn Wybornybcab2db2015-09-28 14:16:55 -040013836
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013837 /* no more scheduling of any task */
Jacob Keller0da36b92017-04-19 09:25:55 -040013838 set_bit(__I40E_SUSPENDED, pf->state);
Mauro S. M. Rodrigues9e6c9c02017-05-12 23:26:56 -030013839 set_bit(__I40E_DOWN, pf->state);
Kees Cook26566ea2017-10-16 17:29:35 -070013840 if (pf->service_timer.function)
Shannon Nelsonc99abb42016-03-10 14:59:45 -080013841 del_timer_sync(&pf->service_timer);
13842 if (pf->service_task.func)
13843 cancel_work_sync(&pf->service_task);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013844
Mitch Williams921c4672017-03-30 00:46:08 -070013845 /* Client close must be called explicitly here because the timer
13846 * has been stopped.
13847 */
13848 i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
13849
Mitch Williamseb2d80b2014-02-13 03:48:48 -080013850 if (pf->flags & I40E_FLAG_SRIOV_ENABLED) {
13851 i40e_free_vfs(pf);
13852 pf->flags &= ~I40E_FLAG_SRIOV_ENABLED;
13853 }
13854
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013855 i40e_fdir_teardown(pf);
13856
13857 /* If there is a switch structure or any orphans, remove them.
13858 * This will leave only the PF's VSI remaining.
13859 */
13860 for (i = 0; i < I40E_MAX_VEB; i++) {
13861 if (!pf->veb[i])
13862 continue;
13863
13864 if (pf->veb[i]->uplink_seid == pf->mac_seid ||
13865 pf->veb[i]->uplink_seid == 0)
13866 i40e_switch_branch_release(pf->veb[i]);
13867 }
13868
13869 /* Now we can shutdown the PF's VSI, just before we kill
13870 * adminq and hmc.
13871 */
13872 if (pf->vsi[pf->lan_vsi])
13873 i40e_vsi_release(pf->vsi[pf->lan_vsi]);
13874
Amritha Nambiaraaf66502017-10-27 02:35:56 -070013875 i40e_cloud_filter_exit(pf);
13876
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060013877 /* remove attached clients */
Mitch Williams004eb612017-04-04 12:40:16 -070013878 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
13879 ret_code = i40e_lan_del_device(pf);
13880 if (ret_code)
13881 dev_warn(&pdev->dev, "Failed to delete client device: %d\n",
13882 ret_code);
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060013883 }
13884
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013885 /* shutdown and destroy the HMC */
Jesse Brandeburgf734dff2016-01-15 14:33:11 -080013886 if (hw->hmc.hmc_obj) {
13887 ret_code = i40e_shutdown_lan_hmc(hw);
Shannon Nelson60442de2014-04-23 04:50:13 +000013888 if (ret_code)
13889 dev_warn(&pdev->dev,
13890 "Failed to destroy the HMC resources: %d\n",
13891 ret_code);
13892 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013893
13894 /* shutdown the adminq */
Henry Tiemanac9c5c62016-09-06 18:05:11 -070013895 i40e_shutdown_adminq(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013896
Jesse Brandeburg8ddb3322015-11-18 15:47:06 -080013897 /* destroy the locks only once, here */
13898 mutex_destroy(&hw->aq.arq_mutex);
13899 mutex_destroy(&hw->aq.asq_mutex);
13900
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013901 /* Clear all dynamic memory lists of rings, q_vectors, and VSIs */
13902 i40e_clear_interrupt_scheme(pf);
Mitch Williams505682c2014-05-20 08:01:37 +000013903 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013904 if (pf->vsi[i]) {
13905 i40e_vsi_clear_rings(pf->vsi[i]);
13906 i40e_vsi_clear(pf->vsi[i]);
13907 pf->vsi[i] = NULL;
13908 }
13909 }
13910
13911 for (i = 0; i < I40E_MAX_VEB; i++) {
13912 kfree(pf->veb[i]);
13913 pf->veb[i] = NULL;
13914 }
13915
13916 kfree(pf->qp_pile);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013917 kfree(pf->vsi);
13918
Jesse Brandeburgf734dff2016-01-15 14:33:11 -080013919 iounmap(hw->hw_addr);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013920 kfree(pf);
Johannes Thumshirn56d766d2016-06-07 09:44:05 +020013921 pci_release_mem_regions(pdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013922
13923 pci_disable_pcie_error_reporting(pdev);
13924 pci_disable_device(pdev);
13925}
13926
13927/**
13928 * i40e_pci_error_detected - warning that something funky happened in PCI land
13929 * @pdev: PCI device information struct
13930 *
13931 * Called to warn that something happened and the error handling steps
13932 * are in progress. Allows the driver to quiesce things, be ready for
13933 * remediation.
13934 **/
13935static pci_ers_result_t i40e_pci_error_detected(struct pci_dev *pdev,
13936 enum pci_channel_state error)
13937{
13938 struct i40e_pf *pf = pci_get_drvdata(pdev);
13939
13940 dev_info(&pdev->dev, "%s: error %d\n", __func__, error);
13941
Guilherme G Piccoliedfc23ee2016-10-03 00:31:12 -070013942 if (!pf) {
13943 dev_info(&pdev->dev,
13944 "Cannot recover - error happened during device probe\n");
13945 return PCI_ERS_RESULT_DISCONNECT;
13946 }
13947
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013948 /* shutdown all operations */
Jacob Kellerdfc4ff62017-06-07 05:43:13 -040013949 if (!test_bit(__I40E_SUSPENDED, pf->state))
13950 i40e_prep_for_reset(pf, false);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013951
13952 /* Request a slot reset */
13953 return PCI_ERS_RESULT_NEED_RESET;
13954}
13955
13956/**
13957 * i40e_pci_error_slot_reset - a PCI slot reset just happened
13958 * @pdev: PCI device information struct
13959 *
13960 * Called to find if the driver can work with the device now that
13961 * the pci slot has been reset. If a basic connection seems good
13962 * (registers are readable and have sane content) then return a
13963 * happy little PCI_ERS_RESULT_xxx.
13964 **/
13965static pci_ers_result_t i40e_pci_error_slot_reset(struct pci_dev *pdev)
13966{
13967 struct i40e_pf *pf = pci_get_drvdata(pdev);
13968 pci_ers_result_t result;
13969 int err;
13970 u32 reg;
13971
Shannon Nelsonfb43201f2015-08-26 15:14:17 -040013972 dev_dbg(&pdev->dev, "%s\n", __func__);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013973 if (pci_enable_device_mem(pdev)) {
13974 dev_info(&pdev->dev,
13975 "Cannot re-enable PCI device after reset.\n");
13976 result = PCI_ERS_RESULT_DISCONNECT;
13977 } else {
13978 pci_set_master(pdev);
13979 pci_restore_state(pdev);
13980 pci_save_state(pdev);
13981 pci_wake_from_d3(pdev, false);
13982
13983 reg = rd32(&pf->hw, I40E_GLGEN_RTRIG);
13984 if (reg == 0)
13985 result = PCI_ERS_RESULT_RECOVERED;
13986 else
13987 result = PCI_ERS_RESULT_DISCONNECT;
13988 }
13989
13990 err = pci_cleanup_aer_uncorrect_error_status(pdev);
13991 if (err) {
13992 dev_info(&pdev->dev,
13993 "pci_cleanup_aer_uncorrect_error_status failed 0x%0x\n",
13994 err);
13995 /* non-fatal, continue */
13996 }
13997
13998 return result;
13999}
14000
14001/**
Alan Brady19b79602017-08-29 05:32:39 -040014002 * i40e_pci_error_reset_prepare - prepare device driver for pci reset
14003 * @pdev: PCI device information struct
14004 */
14005static void i40e_pci_error_reset_prepare(struct pci_dev *pdev)
14006{
14007 struct i40e_pf *pf = pci_get_drvdata(pdev);
14008
14009 i40e_prep_for_reset(pf, false);
14010}
14011
14012/**
14013 * i40e_pci_error_reset_done - pci reset done, device driver reset can begin
14014 * @pdev: PCI device information struct
14015 */
14016static void i40e_pci_error_reset_done(struct pci_dev *pdev)
14017{
14018 struct i40e_pf *pf = pci_get_drvdata(pdev);
14019
14020 i40e_reset_and_rebuild(pf, false, false);
14021}
14022
14023/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000014024 * i40e_pci_error_resume - restart operations after PCI error recovery
14025 * @pdev: PCI device information struct
14026 *
14027 * Called to allow the driver to bring things back up after PCI error
14028 * and/or reset recovery has finished.
14029 **/
14030static void i40e_pci_error_resume(struct pci_dev *pdev)
14031{
14032 struct i40e_pf *pf = pci_get_drvdata(pdev);
14033
Shannon Nelsonfb43201f2015-08-26 15:14:17 -040014034 dev_dbg(&pdev->dev, "%s\n", __func__);
Jacob Keller0da36b92017-04-19 09:25:55 -040014035 if (test_bit(__I40E_SUSPENDED, pf->state))
Shannon Nelson9007bcc2013-11-26 10:49:23 +000014036 return;
14037
Jacob Kellerdfc4ff62017-06-07 05:43:13 -040014038 i40e_handle_reset_warning(pf, false);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000014039}
14040
Shannon Nelson9007bcc2013-11-26 10:49:23 +000014041/**
Joshua Hay1d680052016-12-12 15:44:08 -080014042 * i40e_enable_mc_magic_wake - enable multicast magic packet wake up
14043 * using the mac_address_write admin q function
14044 * @pf: pointer to i40e_pf struct
14045 **/
14046static void i40e_enable_mc_magic_wake(struct i40e_pf *pf)
14047{
14048 struct i40e_hw *hw = &pf->hw;
14049 i40e_status ret;
14050 u8 mac_addr[6];
14051 u16 flags = 0;
14052
14053 /* Get current MAC address in case it's an LAA */
14054 if (pf->vsi[pf->lan_vsi] && pf->vsi[pf->lan_vsi]->netdev) {
14055 ether_addr_copy(mac_addr,
14056 pf->vsi[pf->lan_vsi]->netdev->dev_addr);
14057 } else {
14058 dev_err(&pf->pdev->dev,
14059 "Failed to retrieve MAC address; using default\n");
14060 ether_addr_copy(mac_addr, hw->mac.addr);
14061 }
14062
14063 /* The FW expects the mac address write cmd to first be called with
14064 * one of these flags before calling it again with the multicast
14065 * enable flags.
14066 */
14067 flags = I40E_AQC_WRITE_TYPE_LAA_WOL;
14068
14069 if (hw->func_caps.flex10_enable && hw->partition_id != 1)
14070 flags = I40E_AQC_WRITE_TYPE_LAA_ONLY;
14071
14072 ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
14073 if (ret) {
14074 dev_err(&pf->pdev->dev,
14075 "Failed to update MAC address registers; cannot enable Multicast Magic packet wake up");
14076 return;
14077 }
14078
14079 flags = I40E_AQC_MC_MAG_EN
14080 | I40E_AQC_WOL_PRESERVE_ON_PFR
14081 | I40E_AQC_WRITE_TYPE_UPDATE_MC_MAG;
14082 ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
14083 if (ret)
14084 dev_err(&pf->pdev->dev,
14085 "Failed to enable Multicast Magic Packet wake up\n");
14086}
14087
14088/**
Shannon Nelson9007bcc2013-11-26 10:49:23 +000014089 * i40e_shutdown - PCI callback for shutting down
14090 * @pdev: PCI device information struct
14091 **/
14092static void i40e_shutdown(struct pci_dev *pdev)
14093{
14094 struct i40e_pf *pf = pci_get_drvdata(pdev);
Shannon Nelson8e2773a2013-11-28 06:39:22 +000014095 struct i40e_hw *hw = &pf->hw;
Shannon Nelson9007bcc2013-11-26 10:49:23 +000014096
Jacob Keller0da36b92017-04-19 09:25:55 -040014097 set_bit(__I40E_SUSPENDED, pf->state);
Mauro S. M. Rodrigues9e6c9c02017-05-12 23:26:56 -030014098 set_bit(__I40E_DOWN, pf->state);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000014099 rtnl_lock();
Maciej Sosin373149f2017-04-05 07:50:55 -040014100 i40e_prep_for_reset(pf, true);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000014101 rtnl_unlock();
14102
Shannon Nelson8e2773a2013-11-28 06:39:22 +000014103 wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
14104 wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
14105
Catherine Sullivan02b42492015-07-10 19:35:59 -040014106 del_timer_sync(&pf->service_timer);
14107 cancel_work_sync(&pf->service_task);
Amritha Nambiaraaf66502017-10-27 02:35:56 -070014108 i40e_cloud_filter_exit(pf);
Catherine Sullivan02b42492015-07-10 19:35:59 -040014109 i40e_fdir_teardown(pf);
14110
Mitch Williams921c4672017-03-30 00:46:08 -070014111 /* Client close must be called explicitly here because the timer
14112 * has been stopped.
14113 */
14114 i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
14115
Jacob Kellerd36e41d2017-06-23 04:24:46 -040014116 if (pf->wol_en && (pf->hw_features & I40E_HW_WOL_MC_MAGIC_PKT_WAKE))
Joshua Hay1d680052016-12-12 15:44:08 -080014117 i40e_enable_mc_magic_wake(pf);
14118
Jacob Kellerdfc4ff62017-06-07 05:43:13 -040014119 i40e_prep_for_reset(pf, false);
Catherine Sullivan02b42492015-07-10 19:35:59 -040014120
14121 wr32(hw, I40E_PFPM_APM,
14122 (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
14123 wr32(hw, I40E_PFPM_WUFC,
14124 (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
14125
Shannon Nelsone1477582015-02-21 06:44:33 +000014126 i40e_clear_interrupt_scheme(pf);
14127
Shannon Nelson9007bcc2013-11-26 10:49:23 +000014128 if (system_state == SYSTEM_POWER_OFF) {
Shannon Nelson8e2773a2013-11-28 06:39:22 +000014129 pci_wake_from_d3(pdev, pf->wol_en);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000014130 pci_set_power_state(pdev, PCI_D3hot);
14131 }
14132}
14133
Shannon Nelson9007bcc2013-11-26 10:49:23 +000014134/**
Jacob Keller0e5d3da2017-07-14 09:27:03 -040014135 * i40e_suspend - PM callback for moving to D3
14136 * @dev: generic device information structure
Shannon Nelson9007bcc2013-11-26 10:49:23 +000014137 **/
Arnd Bergmann254d1522017-10-10 10:17:38 +020014138static int __maybe_unused i40e_suspend(struct device *dev)
Shannon Nelson9007bcc2013-11-26 10:49:23 +000014139{
Jacob Keller0e5d3da2017-07-14 09:27:03 -040014140 struct pci_dev *pdev = to_pci_dev(dev);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000014141 struct i40e_pf *pf = pci_get_drvdata(pdev);
Shannon Nelson8e2773a2013-11-28 06:39:22 +000014142 struct i40e_hw *hw = &pf->hw;
Shannon Nelson9007bcc2013-11-26 10:49:23 +000014143
Jacob Keller401586c2017-07-14 09:27:04 -040014144 /* If we're already suspended, then there is nothing to do */
14145 if (test_and_set_bit(__I40E_SUSPENDED, pf->state))
14146 return 0;
14147
Mauro S. M. Rodrigues9e6c9c02017-05-12 23:26:56 -030014148 set_bit(__I40E_DOWN, pf->state);
Mitch Williams3932dbf2015-03-31 00:45:04 -070014149
Jacob Keller5c499222017-07-14 09:27:05 -040014150 /* Ensure service task will not be running */
14151 del_timer_sync(&pf->service_timer);
14152 cancel_work_sync(&pf->service_task);
14153
Jacob Kellerd36e41d2017-06-23 04:24:46 -040014154 if (pf->wol_en && (pf->hw_features & I40E_HW_WOL_MC_MAGIC_PKT_WAKE))
Joshua Hay1d680052016-12-12 15:44:08 -080014155 i40e_enable_mc_magic_wake(pf);
14156
Jacob Kellerdfc4ff62017-06-07 05:43:13 -040014157 i40e_prep_for_reset(pf, false);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000014158
Shannon Nelson8e2773a2013-11-28 06:39:22 +000014159 wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
14160 wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
14161
Jacob Kellerb980c062017-07-14 09:27:06 -040014162 /* Clear the interrupt scheme and release our IRQs so that the system
14163 * can safely hibernate even when there are a large number of CPUs.
14164 * Otherwise hibernation might fail when mapping all the vectors back
14165 * to CPU0.
Shannon Nelson9007bcc2013-11-26 10:49:23 +000014166 */
Jacob Kellerb980c062017-07-14 09:27:06 -040014167 i40e_clear_interrupt_scheme(pf);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000014168
14169 return 0;
14170}
14171
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000014172/**
Jacob Keller0e5d3da2017-07-14 09:27:03 -040014173 * i40e_resume - PM callback for waking up from D3
14174 * @dev: generic device information structure
Shannon Nelson9007bcc2013-11-26 10:49:23 +000014175 **/
Arnd Bergmann254d1522017-10-10 10:17:38 +020014176static int __maybe_unused i40e_resume(struct device *dev)
Shannon Nelson9007bcc2013-11-26 10:49:23 +000014177{
Jacob Keller0e5d3da2017-07-14 09:27:03 -040014178 struct pci_dev *pdev = to_pci_dev(dev);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000014179 struct i40e_pf *pf = pci_get_drvdata(pdev);
Jacob Kellerb980c062017-07-14 09:27:06 -040014180 int err;
Jesse Brandeburg2803b162015-12-22 14:25:08 -080014181
Jacob Keller401586c2017-07-14 09:27:04 -040014182 /* If we're not suspended, then there is nothing to do */
14183 if (!test_bit(__I40E_SUSPENDED, pf->state))
14184 return 0;
14185
Jacob Kellerb980c062017-07-14 09:27:06 -040014186 /* We cleared the interrupt scheme when we suspended, so we need to
14187 * restore it now to resume device functionality.
14188 */
14189 err = i40e_restore_interrupt_scheme(pf);
14190 if (err) {
14191 dev_err(&pdev->dev, "Cannot restore interrupt scheme: %d\n",
14192 err);
14193 }
14194
Jacob Keller401586c2017-07-14 09:27:04 -040014195 clear_bit(__I40E_DOWN, pf->state);
14196 i40e_reset_and_rebuild(pf, false, false);
14197
14198 /* Clear suspended state last after everything is recovered */
14199 clear_bit(__I40E_SUSPENDED, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000014200
Jacob Keller5c499222017-07-14 09:27:05 -040014201 /* Restart the service task */
14202 mod_timer(&pf->service_timer,
14203 round_jiffies(jiffies + pf->service_timer_period));
14204
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000014205 return 0;
14206}
14207
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000014208static const struct pci_error_handlers i40e_err_handler = {
14209 .error_detected = i40e_pci_error_detected,
14210 .slot_reset = i40e_pci_error_slot_reset,
Alan Brady19b79602017-08-29 05:32:39 -040014211 .reset_prepare = i40e_pci_error_reset_prepare,
14212 .reset_done = i40e_pci_error_reset_done,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000014213 .resume = i40e_pci_error_resume,
14214};
14215
Jacob Keller0e5d3da2017-07-14 09:27:03 -040014216static SIMPLE_DEV_PM_OPS(i40e_pm_ops, i40e_suspend, i40e_resume);
14217
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000014218static struct pci_driver i40e_driver = {
14219 .name = i40e_driver_name,
14220 .id_table = i40e_pci_tbl,
14221 .probe = i40e_probe,
14222 .remove = i40e_remove,
Jacob Keller0e5d3da2017-07-14 09:27:03 -040014223 .driver = {
14224 .pm = &i40e_pm_ops,
14225 },
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000014226 .shutdown = i40e_shutdown,
14227 .err_handler = &i40e_err_handler,
14228 .sriov_configure = i40e_pci_sriov_configure,
14229};
14230
14231/**
14232 * i40e_init_module - Driver registration routine
14233 *
14234 * i40e_init_module is the first routine called when the driver is
14235 * loaded. All it does is register with the PCI subsystem.
14236 **/
14237static int __init i40e_init_module(void)
14238{
14239 pr_info("%s: %s - version %s\n", i40e_driver_name,
14240 i40e_driver_string, i40e_driver_version_str);
14241 pr_info("%s: %s\n", i40e_driver_name, i40e_copyright);
Greg Rose96664482015-02-06 08:52:13 +000014242
Jacob Keller4d5957c2017-06-20 15:16:54 -070014243 /* There is no need to throttle the number of active tasks because
14244 * each device limits its own task using a state bit for scheduling
14245 * the service task, and the device tasks do not interfere with each
14246 * other, so we don't set a max task limit. We must set WQ_MEM_RECLAIM
14247 * since we need to be able to guarantee forward progress even under
14248 * memory pressure.
Jesse Brandeburg2803b162015-12-22 14:25:08 -080014249 */
Jacob Keller4d5957c2017-06-20 15:16:54 -070014250 i40e_wq = alloc_workqueue("%s", WQ_MEM_RECLAIM, 0, i40e_driver_name);
Jesse Brandeburg2803b162015-12-22 14:25:08 -080014251 if (!i40e_wq) {
14252 pr_err("%s: Failed to create workqueue\n", i40e_driver_name);
14253 return -ENOMEM;
14254 }
14255
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000014256 i40e_dbg_init();
14257 return pci_register_driver(&i40e_driver);
14258}
14259module_init(i40e_init_module);
14260
14261/**
14262 * i40e_exit_module - Driver exit cleanup routine
14263 *
14264 * i40e_exit_module is called just before the driver is removed
14265 * from memory.
14266 **/
14267static void __exit i40e_exit_module(void)
14268{
14269 pci_unregister_driver(&i40e_driver);
Jesse Brandeburg2803b162015-12-22 14:25:08 -080014270 destroy_workqueue(i40e_wq);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000014271 i40e_dbg_exit();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000014272}
14273module_exit(i40e_exit_module);